blob: a72d0314ca78e4b7c65ee8cc37ef9b2ca201931d [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 Carlson66711e662009-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 Carlsonf92d9dc12010-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 Carlsonf92d9dc12010-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 Carlsonf92d9dc12010-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 Carlsonf92d9dc12010-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 Carlsonf92d9dc12010-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 Carlsonf92d9dc12010-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 Carlsonf92d9dc12010-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 Carlsonf92d9dc12010-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 Carlsonf92d9dc12010-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 Carlsonb0988c12011-04-20 07:57:39 +0000884static int tg3_phy_cl45_write(struct tg3 *tp, u32 devad, u32 addr, u32 val)
885{
886 int err;
887
888 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
889 if (err)
890 goto done;
891
892 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
893 if (err)
894 goto done;
895
896 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
897 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
898 if (err)
899 goto done;
900
901 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, val);
902
903done:
904 return err;
905}
906
907static int tg3_phy_cl45_read(struct tg3 *tp, u32 devad, u32 addr, u32 *val)
908{
909 int err;
910
911 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
912 if (err)
913 goto done;
914
915 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
916 if (err)
917 goto done;
918
919 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
920 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
921 if (err)
922 goto done;
923
924 err = tg3_readphy(tp, MII_TG3_MMD_ADDRESS, val);
925
926done:
927 return err;
928}
929
930static int tg3_phydsp_read(struct tg3 *tp, u32 reg, u32 *val)
931{
932 int err;
933
934 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
935 if (!err)
936 err = tg3_readphy(tp, MII_TG3_DSP_RW_PORT, val);
937
938 return err;
939}
940
941static int tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
942{
943 int err;
944
945 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
946 if (!err)
947 err = tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
948
949 return err;
950}
951
Matt Carlson15ee95c2011-04-20 07:57:40 +0000952static int tg3_phy_auxctl_read(struct tg3 *tp, int reg, u32 *val)
953{
954 int err;
955
956 err = tg3_writephy(tp, MII_TG3_AUX_CTRL,
957 (reg << MII_TG3_AUXCTL_MISC_RDSEL_SHIFT) |
958 MII_TG3_AUXCTL_SHDWSEL_MISC);
959 if (!err)
960 err = tg3_readphy(tp, MII_TG3_AUX_CTRL, val);
961
962 return err;
963}
964
Matt Carlsonb4bd2922011-04-20 07:57:41 +0000965static int tg3_phy_auxctl_write(struct tg3 *tp, int reg, u32 set)
966{
967 if (reg == MII_TG3_AUXCTL_SHDWSEL_MISC)
968 set |= MII_TG3_AUXCTL_MISC_WREN;
969
970 return tg3_writephy(tp, MII_TG3_AUX_CTRL, set | reg);
971}
972
Matt Carlson1d36ba42011-04-20 07:57:42 +0000973#define TG3_PHY_AUXCTL_SMDSP_ENABLE(tp) \
974 tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
975 MII_TG3_AUXCTL_ACTL_SMDSP_ENA | \
976 MII_TG3_AUXCTL_ACTL_TX_6DB)
977
978#define TG3_PHY_AUXCTL_SMDSP_DISABLE(tp) \
979 tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
980 MII_TG3_AUXCTL_ACTL_TX_6DB);
981
Matt Carlson95e28692008-05-25 23:44:14 -0700982static int tg3_bmcr_reset(struct tg3 *tp)
983{
984 u32 phy_control;
985 int limit, err;
986
987 /* OK, reset it, and poll the BMCR_RESET bit until it
988 * clears or we time out.
989 */
990 phy_control = BMCR_RESET;
991 err = tg3_writephy(tp, MII_BMCR, phy_control);
992 if (err != 0)
993 return -EBUSY;
994
995 limit = 5000;
996 while (limit--) {
997 err = tg3_readphy(tp, MII_BMCR, &phy_control);
998 if (err != 0)
999 return -EBUSY;
1000
1001 if ((phy_control & BMCR_RESET) == 0) {
1002 udelay(40);
1003 break;
1004 }
1005 udelay(10);
1006 }
Roel Kluind4675b52009-02-12 16:33:27 -08001007 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -07001008 return -EBUSY;
1009
1010 return 0;
1011}
1012
Matt Carlson158d7ab2008-05-29 01:37:54 -07001013static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
1014{
Francois Romieu3d165432009-01-19 16:56:50 -08001015 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001016 u32 val;
1017
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001018 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001019
1020 if (tg3_readphy(tp, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001021 val = -EIO;
1022
1023 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001024
1025 return val;
1026}
1027
1028static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
1029{
Francois Romieu3d165432009-01-19 16:56:50 -08001030 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001031 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001032
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001033 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001034
1035 if (tg3_writephy(tp, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001036 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001037
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001038 spin_unlock_bh(&tp->lock);
1039
1040 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001041}
1042
1043static int tg3_mdio_reset(struct mii_bus *bp)
1044{
1045 return 0;
1046}
1047
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001048static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -07001049{
1050 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001051 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -07001052
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001053 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001054 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001055 case PHY_ID_BCM50610:
1056 case PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001057 val = MAC_PHYCFG2_50610_LED_MODES;
1058 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001059 case PHY_ID_BCMAC131:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001060 val = MAC_PHYCFG2_AC131_LED_MODES;
1061 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001062 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001063 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
1064 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001065 case PHY_ID_RTL8201E:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001066 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
1067 break;
1068 default:
Matt Carlsona9daf362008-05-25 23:49:44 -07001069 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001070 }
1071
1072 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
1073 tw32(MAC_PHYCFG2, val);
1074
1075 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001076 val &= ~(MAC_PHYCFG1_RGMII_INT |
1077 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
1078 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001079 tw32(MAC_PHYCFG1, val);
1080
1081 return;
1082 }
1083
Matt Carlson14417062010-02-17 15:16:59 +00001084 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE))
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001085 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
1086 MAC_PHYCFG2_FMODE_MASK_MASK |
1087 MAC_PHYCFG2_GMODE_MASK_MASK |
1088 MAC_PHYCFG2_ACT_MASK_MASK |
1089 MAC_PHYCFG2_QUAL_MASK_MASK |
1090 MAC_PHYCFG2_INBAND_ENABLE;
1091
1092 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001093
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001094 val = tr32(MAC_PHYCFG1);
1095 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
1096 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
Matt Carlson14417062010-02-17 15:16:59 +00001097 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE)) {
Matt Carlsona9daf362008-05-25 23:49:44 -07001098 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
1099 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
1100 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
1101 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1102 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001103 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1104 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1105 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001106
Matt Carlsona9daf362008-05-25 23:49:44 -07001107 val = tr32(MAC_EXT_RGMII_MODE);
1108 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1109 MAC_RGMII_MODE_RX_QUALITY |
1110 MAC_RGMII_MODE_RX_ACTIVITY |
1111 MAC_RGMII_MODE_RX_ENG_DET |
1112 MAC_RGMII_MODE_TX_ENABLE |
1113 MAC_RGMII_MODE_TX_LOWPWR |
1114 MAC_RGMII_MODE_TX_RESET);
Matt Carlson14417062010-02-17 15:16:59 +00001115 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE)) {
Matt Carlsona9daf362008-05-25 23:49:44 -07001116 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
1117 val |= MAC_RGMII_MODE_RX_INT_B |
1118 MAC_RGMII_MODE_RX_QUALITY |
1119 MAC_RGMII_MODE_RX_ACTIVITY |
1120 MAC_RGMII_MODE_RX_ENG_DET;
1121 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
1122 val |= MAC_RGMII_MODE_TX_ENABLE |
1123 MAC_RGMII_MODE_TX_LOWPWR |
1124 MAC_RGMII_MODE_TX_RESET;
1125 }
1126 tw32(MAC_EXT_RGMII_MODE, val);
1127}
1128
Matt Carlson158d7ab2008-05-29 01:37:54 -07001129static void tg3_mdio_start(struct tg3 *tp)
1130{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001131 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1132 tw32_f(MAC_MI_MODE, tp->mi_mode);
1133 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001134
Matt Carlson9ea48182010-02-17 15:17:01 +00001135 if ((tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED) &&
1136 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1137 tg3_mdio_config_5785(tp);
1138}
1139
1140static int tg3_mdio_init(struct tg3 *tp)
1141{
1142 int i;
1143 u32 reg;
1144 struct phy_device *phydev;
1145
Matt Carlson0a58d662011-04-05 14:22:45 +00001146 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
Matt Carlson9c7df912010-06-05 17:24:36 +00001147 u32 is_serdes;
Matt Carlson882e9792009-09-01 13:21:36 +00001148
Matt Carlson9c7df912010-06-05 17:24:36 +00001149 tp->phy_addr = PCI_FUNC(tp->pdev->devfn) + 1;
Matt Carlson882e9792009-09-01 13:21:36 +00001150
Matt Carlsond1ec96a2010-01-12 10:11:38 +00001151 if (tp->pci_chip_rev_id != CHIPREV_ID_5717_A0)
1152 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1153 else
1154 is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1155 TG3_CPMU_PHY_STRAP_IS_SERDES;
Matt Carlson882e9792009-09-01 13:21:36 +00001156 if (is_serdes)
1157 tp->phy_addr += 7;
1158 } else
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001159 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001160
Matt Carlson158d7ab2008-05-29 01:37:54 -07001161 tg3_mdio_start(tp);
1162
1163 if (!(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) ||
1164 (tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED))
1165 return 0;
1166
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001167 tp->mdio_bus = mdiobus_alloc();
1168 if (tp->mdio_bus == NULL)
1169 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001170
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001171 tp->mdio_bus->name = "tg3 mdio bus";
1172 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001173 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001174 tp->mdio_bus->priv = tp;
1175 tp->mdio_bus->parent = &tp->pdev->dev;
1176 tp->mdio_bus->read = &tg3_mdio_read;
1177 tp->mdio_bus->write = &tg3_mdio_write;
1178 tp->mdio_bus->reset = &tg3_mdio_reset;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001179 tp->mdio_bus->phy_mask = ~(1 << TG3_PHY_MII_ADDR);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001180 tp->mdio_bus->irq = &tp->mdio_irq[0];
Matt Carlson158d7ab2008-05-29 01:37:54 -07001181
1182 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001183 tp->mdio_bus->irq[i] = PHY_POLL;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001184
1185 /* The bus registration will look for all the PHYs on the mdio bus.
1186 * Unfortunately, it does not ensure the PHY is powered up before
1187 * accessing the PHY ID registers. A chip reset is the
1188 * quickest way to bring the device back to an operational state..
1189 */
1190 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1191 tg3_bmcr_reset(tp);
1192
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001193 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001194 if (i) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001195 dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001196 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001197 return i;
1198 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001199
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001200 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsona9daf362008-05-25 23:49:44 -07001201
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001202 if (!phydev || !phydev->drv) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001203 dev_warn(&tp->pdev->dev, "No PHY devices\n");
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001204 mdiobus_unregister(tp->mdio_bus);
1205 mdiobus_free(tp->mdio_bus);
1206 return -ENODEV;
1207 }
1208
1209 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001210 case PHY_ID_BCM57780:
Matt Carlson321d32a2008-11-21 17:22:19 -08001211 phydev->interface = PHY_INTERFACE_MODE_GMII;
Matt Carlsonc704dc22009-11-02 14:32:12 +00001212 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08001213 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001214 case PHY_ID_BCM50610:
1215 case PHY_ID_BCM50610M:
Matt Carlson32e5a8d2009-11-02 14:31:39 +00001216 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001217 PHY_BRCM_RX_REFCLK_UNUSED |
Matt Carlson52fae082009-11-02 14:32:38 +00001218 PHY_BRCM_DIS_TXCRXC_NOENRGY |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001219 PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson14417062010-02-17 15:16:59 +00001220 if (tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE)
Matt Carlsona9daf362008-05-25 23:49:44 -07001221 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
1222 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
1223 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
1224 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
1225 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001226 /* fallthru */
Matt Carlson6a443a02010-02-17 15:17:04 +00001227 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001228 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001229 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001230 case PHY_ID_RTL8201E:
1231 case PHY_ID_BCMAC131:
Matt Carlsona9daf362008-05-25 23:49:44 -07001232 phydev->interface = PHY_INTERFACE_MODE_MII;
Matt Carlsoncdd4e09d2009-11-02 14:31:11 +00001233 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001234 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlsona9daf362008-05-25 23:49:44 -07001235 break;
1236 }
1237
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001238 tp->tg3_flags3 |= TG3_FLG3_MDIOBUS_INITED;
1239
1240 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1241 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001242
1243 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001244}
1245
1246static void tg3_mdio_fini(struct tg3 *tp)
1247{
1248 if (tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED) {
1249 tp->tg3_flags3 &= ~TG3_FLG3_MDIOBUS_INITED;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001250 mdiobus_unregister(tp->mdio_bus);
1251 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001252 }
1253}
1254
Matt Carlson95e28692008-05-25 23:44:14 -07001255/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001256static inline void tg3_generate_fw_event(struct tg3 *tp)
1257{
1258 u32 val;
1259
1260 val = tr32(GRC_RX_CPU_EVENT);
1261 val |= GRC_RX_CPU_DRIVER_EVENT;
1262 tw32_f(GRC_RX_CPU_EVENT, val);
1263
1264 tp->last_event_jiffies = jiffies;
1265}
1266
1267#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1268
1269/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001270static void tg3_wait_for_event_ack(struct tg3 *tp)
1271{
1272 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001273 unsigned int delay_cnt;
1274 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001275
Matt Carlson4ba526c2008-08-15 14:10:04 -07001276 /* If enough time has passed, no wait is necessary. */
1277 time_remain = (long)(tp->last_event_jiffies + 1 +
1278 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1279 (long)jiffies;
1280 if (time_remain < 0)
1281 return;
1282
1283 /* Check if we can shorten the wait time. */
1284 delay_cnt = jiffies_to_usecs(time_remain);
1285 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1286 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1287 delay_cnt = (delay_cnt >> 3) + 1;
1288
1289 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001290 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1291 break;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001292 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001293 }
1294}
1295
1296/* tp->lock is held. */
1297static void tg3_ump_link_report(struct tg3 *tp)
1298{
1299 u32 reg;
1300 u32 val;
1301
1302 if (!(tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
1303 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
1304 return;
1305
1306 tg3_wait_for_event_ack(tp);
1307
1308 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1309
1310 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1311
1312 val = 0;
1313 if (!tg3_readphy(tp, MII_BMCR, &reg))
1314 val = reg << 16;
1315 if (!tg3_readphy(tp, MII_BMSR, &reg))
1316 val |= (reg & 0xffff);
1317 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX, val);
1318
1319 val = 0;
1320 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1321 val = reg << 16;
1322 if (!tg3_readphy(tp, MII_LPA, &reg))
1323 val |= (reg & 0xffff);
1324 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 4, val);
1325
1326 val = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001327 if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
Matt Carlson95e28692008-05-25 23:44:14 -07001328 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1329 val = reg << 16;
1330 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1331 val |= (reg & 0xffff);
1332 }
1333 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 8, val);
1334
1335 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1336 val = reg << 16;
1337 else
1338 val = 0;
1339 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 12, val);
1340
Matt Carlson4ba526c2008-08-15 14:10:04 -07001341 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001342}
1343
1344static void tg3_link_report(struct tg3 *tp)
1345{
1346 if (!netif_carrier_ok(tp->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001347 netif_info(tp, link, tp->dev, "Link is down\n");
Matt Carlson95e28692008-05-25 23:44:14 -07001348 tg3_ump_link_report(tp);
1349 } else if (netif_msg_link(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001350 netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
1351 (tp->link_config.active_speed == SPEED_1000 ?
1352 1000 :
1353 (tp->link_config.active_speed == SPEED_100 ?
1354 100 : 10)),
1355 (tp->link_config.active_duplex == DUPLEX_FULL ?
1356 "full" : "half"));
Matt Carlson95e28692008-05-25 23:44:14 -07001357
Joe Perches05dbe002010-02-17 19:44:19 +00001358 netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
1359 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
1360 "on" : "off",
1361 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
1362 "on" : "off");
Matt Carlson47007832011-04-20 07:57:43 +00001363
1364 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
1365 netdev_info(tp->dev, "EEE is %s\n",
1366 tp->setlpicnt ? "enabled" : "disabled");
1367
Matt Carlson95e28692008-05-25 23:44:14 -07001368 tg3_ump_link_report(tp);
1369 }
1370}
1371
1372static u16 tg3_advert_flowctrl_1000T(u8 flow_ctrl)
1373{
1374 u16 miireg;
1375
Steve Glendinninge18ce342008-12-16 02:00:00 -08001376 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001377 miireg = ADVERTISE_PAUSE_CAP;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001378 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001379 miireg = ADVERTISE_PAUSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001380 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001381 miireg = ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1382 else
1383 miireg = 0;
1384
1385 return miireg;
1386}
1387
1388static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1389{
1390 u16 miireg;
1391
Steve Glendinninge18ce342008-12-16 02:00:00 -08001392 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001393 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001394 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001395 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001396 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001397 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1398 else
1399 miireg = 0;
1400
1401 return miireg;
1402}
1403
Matt Carlson95e28692008-05-25 23:44:14 -07001404static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1405{
1406 u8 cap = 0;
1407
1408 if (lcladv & ADVERTISE_1000XPAUSE) {
1409 if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1410 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001411 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001412 else if (rmtadv & LPA_1000XPAUSE_ASYM)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001413 cap = FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001414 } else {
1415 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001416 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001417 }
1418 } else if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1419 if ((rmtadv & LPA_1000XPAUSE) && (rmtadv & LPA_1000XPAUSE_ASYM))
Steve Glendinninge18ce342008-12-16 02:00:00 -08001420 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001421 }
1422
1423 return cap;
1424}
1425
Matt Carlsonf51f3562008-05-25 23:45:08 -07001426static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001427{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001428 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001429 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001430 u32 old_rx_mode = tp->rx_mode;
1431 u32 old_tx_mode = tp->tx_mode;
1432
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001433 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001434 autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001435 else
1436 autoneg = tp->link_config.autoneg;
1437
1438 if (autoneg == AUTONEG_ENABLE &&
Matt Carlson95e28692008-05-25 23:44:14 -07001439 (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001440 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001441 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001442 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001443 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001444 } else
1445 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001446
Matt Carlsonf51f3562008-05-25 23:45:08 -07001447 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001448
Steve Glendinninge18ce342008-12-16 02:00:00 -08001449 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001450 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1451 else
1452 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1453
Matt Carlsonf51f3562008-05-25 23:45:08 -07001454 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001455 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001456
Steve Glendinninge18ce342008-12-16 02:00:00 -08001457 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001458 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1459 else
1460 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1461
Matt Carlsonf51f3562008-05-25 23:45:08 -07001462 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001463 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001464}
1465
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001466static void tg3_adjust_link(struct net_device *dev)
1467{
1468 u8 oldflowctrl, linkmesg = 0;
1469 u32 mac_mode, lcl_adv, rmt_adv;
1470 struct tg3 *tp = netdev_priv(dev);
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001471 struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001472
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001473 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001474
1475 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
1476 MAC_MODE_HALF_DUPLEX);
1477
1478 oldflowctrl = tp->link_config.active_flowctrl;
1479
1480 if (phydev->link) {
1481 lcl_adv = 0;
1482 rmt_adv = 0;
1483
1484 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
1485 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001486 else if (phydev->speed == SPEED_1000 ||
1487 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001488 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001489 else
1490 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001491
1492 if (phydev->duplex == DUPLEX_HALF)
1493 mac_mode |= MAC_MODE_HALF_DUPLEX;
1494 else {
1495 lcl_adv = tg3_advert_flowctrl_1000T(
1496 tp->link_config.flowctrl);
1497
1498 if (phydev->pause)
1499 rmt_adv = LPA_PAUSE_CAP;
1500 if (phydev->asym_pause)
1501 rmt_adv |= LPA_PAUSE_ASYM;
1502 }
1503
1504 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
1505 } else
1506 mac_mode |= MAC_MODE_PORT_MODE_GMII;
1507
1508 if (mac_mode != tp->mac_mode) {
1509 tp->mac_mode = mac_mode;
1510 tw32_f(MAC_MODE, tp->mac_mode);
1511 udelay(40);
1512 }
1513
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001514 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
1515 if (phydev->speed == SPEED_10)
1516 tw32(MAC_MI_STAT,
1517 MAC_MI_STAT_10MBPS_MODE |
1518 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1519 else
1520 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1521 }
1522
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001523 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
1524 tw32(MAC_TX_LENGTHS,
1525 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1526 (6 << TX_LENGTHS_IPG_SHIFT) |
1527 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
1528 else
1529 tw32(MAC_TX_LENGTHS,
1530 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1531 (6 << TX_LENGTHS_IPG_SHIFT) |
1532 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
1533
1534 if ((phydev->link && tp->link_config.active_speed == SPEED_INVALID) ||
1535 (!phydev->link && tp->link_config.active_speed != SPEED_INVALID) ||
1536 phydev->speed != tp->link_config.active_speed ||
1537 phydev->duplex != tp->link_config.active_duplex ||
1538 oldflowctrl != tp->link_config.active_flowctrl)
Matt Carlsonc6cdf432010-04-05 10:19:26 +00001539 linkmesg = 1;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001540
1541 tp->link_config.active_speed = phydev->speed;
1542 tp->link_config.active_duplex = phydev->duplex;
1543
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001544 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001545
1546 if (linkmesg)
1547 tg3_link_report(tp);
1548}
1549
1550static int tg3_phy_init(struct tg3 *tp)
1551{
1552 struct phy_device *phydev;
1553
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001554 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001555 return 0;
1556
1557 /* Bring the PHY back to a known state. */
1558 tg3_bmcr_reset(tp);
1559
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001560 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001561
1562 /* Attach the MAC to the PHY. */
Kay Sieversfb28ad32008-11-10 13:55:14 -08001563 phydev = phy_connect(tp->dev, dev_name(&phydev->dev), tg3_adjust_link,
Matt Carlsona9daf362008-05-25 23:49:44 -07001564 phydev->dev_flags, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001565 if (IS_ERR(phydev)) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001566 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001567 return PTR_ERR(phydev);
1568 }
1569
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001570 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001571 switch (phydev->interface) {
1572 case PHY_INTERFACE_MODE_GMII:
1573 case PHY_INTERFACE_MODE_RGMII:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001574 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Matt Carlson321d32a2008-11-21 17:22:19 -08001575 phydev->supported &= (PHY_GBIT_FEATURES |
1576 SUPPORTED_Pause |
1577 SUPPORTED_Asym_Pause);
1578 break;
1579 }
1580 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001581 case PHY_INTERFACE_MODE_MII:
1582 phydev->supported &= (PHY_BASIC_FEATURES |
1583 SUPPORTED_Pause |
1584 SUPPORTED_Asym_Pause);
1585 break;
1586 default:
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001587 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001588 return -EINVAL;
1589 }
1590
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001591 tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001592
1593 phydev->advertising = phydev->supported;
1594
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001595 return 0;
1596}
1597
1598static void tg3_phy_start(struct tg3 *tp)
1599{
1600 struct phy_device *phydev;
1601
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001602 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001603 return;
1604
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001605 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001606
Matt Carlson80096062010-08-02 11:26:06 +00001607 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
1608 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001609 phydev->speed = tp->link_config.orig_speed;
1610 phydev->duplex = tp->link_config.orig_duplex;
1611 phydev->autoneg = tp->link_config.orig_autoneg;
1612 phydev->advertising = tp->link_config.orig_advertising;
1613 }
1614
1615 phy_start(phydev);
1616
1617 phy_start_aneg(phydev);
1618}
1619
1620static void tg3_phy_stop(struct tg3 *tp)
1621{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001622 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001623 return;
1624
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001625 phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001626}
1627
1628static void tg3_phy_fini(struct tg3 *tp)
1629{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001630 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001631 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001632 tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001633 }
1634}
1635
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001636static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
1637{
1638 u32 phytest;
1639
1640 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
1641 u32 phy;
1642
1643 tg3_writephy(tp, MII_TG3_FET_TEST,
1644 phytest | MII_TG3_FET_SHADOW_EN);
1645 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
1646 if (enable)
1647 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
1648 else
1649 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
1650 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
1651 }
1652 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
1653 }
1654}
1655
Matt Carlson6833c042008-11-21 17:18:59 -08001656static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
1657{
1658 u32 reg;
1659
Matt Carlsonecf14102010-01-20 16:58:05 +00001660 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
Matt Carlson0a58d662011-04-05 14:22:45 +00001661 ((tp->tg3_flags3 & TG3_FLG3_5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001662 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08001663 return;
1664
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001665 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001666 tg3_phy_fet_toggle_apd(tp, enable);
1667 return;
1668 }
1669
Matt Carlson6833c042008-11-21 17:18:59 -08001670 reg = MII_TG3_MISC_SHDW_WREN |
1671 MII_TG3_MISC_SHDW_SCR5_SEL |
1672 MII_TG3_MISC_SHDW_SCR5_LPED |
1673 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
1674 MII_TG3_MISC_SHDW_SCR5_SDTL |
1675 MII_TG3_MISC_SHDW_SCR5_C125OE;
1676 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 || !enable)
1677 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
1678
1679 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1680
1681
1682 reg = MII_TG3_MISC_SHDW_WREN |
1683 MII_TG3_MISC_SHDW_APD_SEL |
1684 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
1685 if (enable)
1686 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
1687
1688 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1689}
1690
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001691static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
1692{
1693 u32 phy;
1694
1695 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001696 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001697 return;
1698
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001699 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001700 u32 ephy;
1701
Matt Carlson535ef6e2009-08-25 10:09:36 +00001702 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
1703 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
1704
1705 tg3_writephy(tp, MII_TG3_FET_TEST,
1706 ephy | MII_TG3_FET_SHADOW_EN);
1707 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001708 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00001709 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001710 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00001711 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
1712 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001713 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00001714 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001715 }
1716 } else {
Matt Carlson15ee95c2011-04-20 07:57:40 +00001717 int ret;
1718
1719 ret = tg3_phy_auxctl_read(tp,
1720 MII_TG3_AUXCTL_SHDWSEL_MISC, &phy);
1721 if (!ret) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001722 if (enable)
1723 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1724 else
1725 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001726 tg3_phy_auxctl_write(tp,
1727 MII_TG3_AUXCTL_SHDWSEL_MISC, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001728 }
1729 }
1730}
1731
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732static void tg3_phy_set_wirespeed(struct tg3 *tp)
1733{
Matt Carlson15ee95c2011-04-20 07:57:40 +00001734 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 u32 val;
1736
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001737 if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 return;
1739
Matt Carlson15ee95c2011-04-20 07:57:40 +00001740 ret = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_MISC, &val);
1741 if (!ret)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001742 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_MISC,
1743 val | MII_TG3_AUXCTL_MISC_WIRESPD_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744}
1745
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001746static void tg3_phy_apply_otp(struct tg3 *tp)
1747{
1748 u32 otp, phy;
1749
1750 if (!tp->phy_otp)
1751 return;
1752
1753 otp = tp->phy_otp;
1754
Matt Carlson1d36ba42011-04-20 07:57:42 +00001755 if (TG3_PHY_AUXCTL_SMDSP_ENABLE(tp))
1756 return;
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001757
1758 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
1759 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
1760 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
1761
1762 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
1763 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
1764 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
1765
1766 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
1767 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
1768 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
1769
1770 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
1771 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
1772
1773 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
1774 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
1775
1776 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
1777 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
1778 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
1779
Matt Carlson1d36ba42011-04-20 07:57:42 +00001780 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001781}
1782
Matt Carlson52b02d02010-10-14 10:37:41 +00001783static void tg3_phy_eee_adjust(struct tg3 *tp, u32 current_link_up)
1784{
1785 u32 val;
1786
1787 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
1788 return;
1789
1790 tp->setlpicnt = 0;
1791
1792 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
1793 current_link_up == 1 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +00001794 tp->link_config.active_duplex == DUPLEX_FULL &&
1795 (tp->link_config.active_speed == SPEED_100 ||
1796 tp->link_config.active_speed == SPEED_1000)) {
Matt Carlson52b02d02010-10-14 10:37:41 +00001797 u32 eeectl;
1798
1799 if (tp->link_config.active_speed == SPEED_1000)
1800 eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
1801 else
1802 eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
1803
1804 tw32(TG3_CPMU_EEE_CTRL, eeectl);
1805
Matt Carlson3110f5f52010-12-06 08:28:50 +00001806 tg3_phy_cl45_read(tp, MDIO_MMD_AN,
1807 TG3_CL45_D7_EEERES_STAT, &val);
Matt Carlson52b02d02010-10-14 10:37:41 +00001808
Matt Carlson21a00ab2011-01-25 15:58:55 +00001809 switch (val) {
1810 case TG3_CL45_D7_EEERES_STAT_LP_1000T:
1811 switch (GET_ASIC_REV(tp->pci_chip_rev_id)) {
1812 case ASIC_REV_5717:
1813 case ASIC_REV_5719:
1814 case ASIC_REV_57765:
Matt Carlson1d36ba42011-04-20 07:57:42 +00001815 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
1816 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26,
1817 0x0000);
1818 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
1819 }
Matt Carlson21a00ab2011-01-25 15:58:55 +00001820 }
1821 /* Fallthrough */
1822 case TG3_CL45_D7_EEERES_STAT_LP_100TX:
Matt Carlson52b02d02010-10-14 10:37:41 +00001823 tp->setlpicnt = 2;
Matt Carlson21a00ab2011-01-25 15:58:55 +00001824 }
Matt Carlson52b02d02010-10-14 10:37:41 +00001825 }
1826
1827 if (!tp->setlpicnt) {
1828 val = tr32(TG3_CPMU_EEE_MODE);
1829 tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
1830 }
1831}
1832
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833static int tg3_wait_macro_done(struct tg3 *tp)
1834{
1835 int limit = 100;
1836
1837 while (limit--) {
1838 u32 tmp32;
1839
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001840 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 if ((tmp32 & 0x1000) == 0)
1842 break;
1843 }
1844 }
Roel Kluind4675b52009-02-12 16:33:27 -08001845 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 return -EBUSY;
1847
1848 return 0;
1849}
1850
1851static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
1852{
1853 static const u32 test_pat[4][6] = {
1854 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
1855 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
1856 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
1857 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
1858 };
1859 int chan;
1860
1861 for (chan = 0; chan < 4; chan++) {
1862 int i;
1863
1864 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1865 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001866 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
1868 for (i = 0; i < 6; i++)
1869 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
1870 test_pat[chan][i]);
1871
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001872 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 if (tg3_wait_macro_done(tp)) {
1874 *resetp = 1;
1875 return -EBUSY;
1876 }
1877
1878 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1879 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001880 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 if (tg3_wait_macro_done(tp)) {
1882 *resetp = 1;
1883 return -EBUSY;
1884 }
1885
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001886 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 if (tg3_wait_macro_done(tp)) {
1888 *resetp = 1;
1889 return -EBUSY;
1890 }
1891
1892 for (i = 0; i < 6; i += 2) {
1893 u32 low, high;
1894
1895 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
1896 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
1897 tg3_wait_macro_done(tp)) {
1898 *resetp = 1;
1899 return -EBUSY;
1900 }
1901 low &= 0x7fff;
1902 high &= 0x000f;
1903 if (low != test_pat[chan][i] ||
1904 high != test_pat[chan][i+1]) {
1905 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
1906 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
1907 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
1908
1909 return -EBUSY;
1910 }
1911 }
1912 }
1913
1914 return 0;
1915}
1916
1917static int tg3_phy_reset_chanpat(struct tg3 *tp)
1918{
1919 int chan;
1920
1921 for (chan = 0; chan < 4; chan++) {
1922 int i;
1923
1924 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1925 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001926 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 for (i = 0; i < 6; i++)
1928 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001929 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 if (tg3_wait_macro_done(tp))
1931 return -EBUSY;
1932 }
1933
1934 return 0;
1935}
1936
1937static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
1938{
1939 u32 reg32, phy9_orig;
1940 int retries, do_phy_reset, err;
1941
1942 retries = 10;
1943 do_phy_reset = 1;
1944 do {
1945 if (do_phy_reset) {
1946 err = tg3_bmcr_reset(tp);
1947 if (err)
1948 return err;
1949 do_phy_reset = 0;
1950 }
1951
1952 /* Disable transmitter and interrupt. */
1953 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
1954 continue;
1955
1956 reg32 |= 0x3000;
1957 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
1958
1959 /* Set full-duplex, 1000 mbps. */
1960 tg3_writephy(tp, MII_BMCR,
1961 BMCR_FULLDPLX | TG3_BMCR_SPEED1000);
1962
1963 /* Set to master mode. */
1964 if (tg3_readphy(tp, MII_TG3_CTRL, &phy9_orig))
1965 continue;
1966
1967 tg3_writephy(tp, MII_TG3_CTRL,
1968 (MII_TG3_CTRL_AS_MASTER |
1969 MII_TG3_CTRL_ENABLE_AS_MASTER));
1970
Matt Carlson1d36ba42011-04-20 07:57:42 +00001971 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
1972 if (err)
1973 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
1975 /* Block the PHY control access. */
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00001976 tg3_phydsp_write(tp, 0x8005, 0x0800);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
1978 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
1979 if (!err)
1980 break;
1981 } while (--retries);
1982
1983 err = tg3_phy_reset_chanpat(tp);
1984 if (err)
1985 return err;
1986
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00001987 tg3_phydsp_write(tp, 0x8005, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
1989 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001990 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
Matt Carlson1d36ba42011-04-20 07:57:42 +00001992 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
1994 tg3_writephy(tp, MII_TG3_CTRL, phy9_orig);
1995
1996 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
1997 reg32 &= ~0x3000;
1998 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
1999 } else if (!err)
2000 err = -EBUSY;
2001
2002 return err;
2003}
2004
2005/* This will reset the tigon3 PHY if there is no valid
2006 * link unless the FORCE argument is non-zero.
2007 */
2008static int tg3_phy_reset(struct tg3 *tp)
2009{
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002010 u32 val, cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 int err;
2012
Michael Chan60189dd2006-12-17 17:08:07 -08002013 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002014 val = tr32(GRC_MISC_CFG);
2015 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
2016 udelay(40);
2017 }
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002018 err = tg3_readphy(tp, MII_BMSR, &val);
2019 err |= tg3_readphy(tp, MII_BMSR, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 if (err != 0)
2021 return -EBUSY;
2022
Michael Chanc8e1e822006-04-29 18:55:17 -07002023 if (netif_running(tp->dev) && netif_carrier_ok(tp->dev)) {
2024 netif_carrier_off(tp->dev);
2025 tg3_link_report(tp);
2026 }
2027
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2029 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2030 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
2031 err = tg3_phy_reset_5703_4_5(tp);
2032 if (err)
2033 return err;
2034 goto out;
2035 }
2036
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002037 cpmuctrl = 0;
2038 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
2039 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
2040 cpmuctrl = tr32(TG3_CPMU_CTRL);
2041 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
2042 tw32(TG3_CPMU_CTRL,
2043 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
2044 }
2045
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 err = tg3_bmcr_reset(tp);
2047 if (err)
2048 return err;
2049
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002050 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002051 val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
2052 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002053
2054 tw32(TG3_CPMU_CTRL, cpmuctrl);
2055 }
2056
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002057 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2058 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002059 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2060 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
2061 CPMU_LSPD_1000MB_MACCLK_12_5) {
2062 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2063 udelay(40);
2064 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2065 }
2066 }
2067
Matt Carlson0a58d662011-04-05 14:22:45 +00002068 if ((tp->tg3_flags3 & TG3_FLG3_5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002069 (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
Matt Carlsonecf14102010-01-20 16:58:05 +00002070 return 0;
2071
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002072 tg3_phy_apply_otp(tp);
2073
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002074 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -08002075 tg3_phy_toggle_apd(tp, true);
2076 else
2077 tg3_phy_toggle_apd(tp, false);
2078
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079out:
Matt Carlson1d36ba42011-04-20 07:57:42 +00002080 if ((tp->phy_flags & TG3_PHYFLG_ADC_BUG) &&
2081 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002082 tg3_phydsp_write(tp, 0x201f, 0x2aaa);
2083 tg3_phydsp_write(tp, 0x000a, 0x0323);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002084 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002086
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002087 if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002088 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2089 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002091
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002092 if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002093 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2094 tg3_phydsp_write(tp, 0x000a, 0x310b);
2095 tg3_phydsp_write(tp, 0x201f, 0x9506);
2096 tg3_phydsp_write(tp, 0x401f, 0x14e2);
2097 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2098 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002099 } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002100 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2101 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
2102 if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
2103 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2104 tg3_writephy(tp, MII_TG3_TEST1,
2105 MII_TG3_TEST1_TRIM_EN | 0x4);
2106 } else
2107 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
2108
2109 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2110 }
Michael Chanc424cb22006-04-29 18:56:34 -07002111 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002112
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 /* Set Extended packet length bit (bit 14) on all chips that */
2114 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002115 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 /* Cannot do read-modify-write on 5401 */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002117 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Matt Carlson8f666b02009-08-28 13:58:24 +00002118 } else if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 /* Set bit 14 with read-modify-write to preserve other bits */
Matt Carlson15ee95c2011-04-20 07:57:40 +00002120 err = tg3_phy_auxctl_read(tp,
2121 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2122 if (!err)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002123 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2124 val | MII_TG3_AUXCTL_ACTL_EXTPKTLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 }
2126
2127 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2128 * jumbo frames transmission.
2129 */
Matt Carlson8f666b02009-08-28 13:58:24 +00002130 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002131 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002132 tg3_writephy(tp, MII_TG3_EXT_CTRL,
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002133 val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 }
2135
Michael Chan715116a2006-09-27 16:09:25 -07002136 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002137 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002138 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002139 }
2140
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002141 tg3_phy_toggle_automdix(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 tg3_phy_set_wirespeed(tp);
2143 return 0;
2144}
2145
2146static void tg3_frob_aux_power(struct tg3 *tp)
2147{
Matt Carlson683644b2011-03-09 16:58:23 +00002148 bool need_vaux = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
Matt Carlson334355a2010-01-20 16:58:10 +00002150 /* The GPIOs do something completely different on 57765. */
2151 if ((tp->tg3_flags2 & TG3_FLG2_IS_NIC) == 0 ||
Matt Carlsona50d0792010-06-05 17:24:37 +00002152 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlson334355a2010-01-20 16:58:10 +00002153 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 return;
2155
Matt Carlson683644b2011-03-09 16:58:23 +00002156 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2157 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +00002158 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2159 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) &&
Matt Carlson683644b2011-03-09 16:58:23 +00002160 tp->pdev_peer != tp->pdev) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002161 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002163 dev_peer = pci_get_drvdata(tp->pdev_peer);
Matt Carlson683644b2011-03-09 16:58:23 +00002164
Michael Chanbc1c7562006-03-20 17:48:03 -08002165 /* remove_one() may have been run on the peer. */
Matt Carlson683644b2011-03-09 16:58:23 +00002166 if (dev_peer) {
2167 struct tg3 *tp_peer = netdev_priv(dev_peer);
2168
2169 if (tp_peer->tg3_flags & TG3_FLAG_INIT_COMPLETE)
2170 return;
2171
2172 if ((tp_peer->tg3_flags & TG3_FLAG_WOL_ENABLE) ||
2173 (tp_peer->tg3_flags & TG3_FLAG_ENABLE_ASF))
2174 need_vaux = true;
2175 }
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
Matt Carlson683644b2011-03-09 16:58:23 +00002178 if ((tp->tg3_flags & TG3_FLAG_WOL_ENABLE) ||
2179 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
2180 need_vaux = true;
2181
2182 if (need_vaux) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2184 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Michael Chanb401e9e2005-12-19 16:27:04 -08002185 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2186 (GRC_LCLCTRL_GPIO_OE0 |
2187 GRC_LCLCTRL_GPIO_OE1 |
2188 GRC_LCLCTRL_GPIO_OE2 |
2189 GRC_LCLCTRL_GPIO_OUTPUT0 |
2190 GRC_LCLCTRL_GPIO_OUTPUT1),
2191 100);
Matt Carlson8d519ab2009-04-20 06:58:01 +00002192 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2193 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -07002194 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2195 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2196 GRC_LCLCTRL_GPIO_OE1 |
2197 GRC_LCLCTRL_GPIO_OE2 |
2198 GRC_LCLCTRL_GPIO_OUTPUT0 |
2199 GRC_LCLCTRL_GPIO_OUTPUT1 |
2200 tp->grc_local_ctrl;
2201 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2202
2203 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2204 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2205
2206 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2207 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 } else {
2209 u32 no_gpio2;
Michael Chandc56b7d2005-12-19 16:26:28 -08002210 u32 grc_local_ctrl = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
Michael Chandc56b7d2005-12-19 16:26:28 -08002212 /* Workaround to prevent overdrawing Amps. */
2213 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2214 ASIC_REV_5714) {
2215 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chanb401e9e2005-12-19 16:27:04 -08002216 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2217 grc_local_ctrl, 100);
Michael Chandc56b7d2005-12-19 16:26:28 -08002218 }
2219
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 /* On 5753 and variants, GPIO2 cannot be used. */
2221 no_gpio2 = tp->nic_sram_data_cfg &
2222 NIC_SRAM_DATA_CFG_NO_GPIO2;
2223
Michael Chandc56b7d2005-12-19 16:26:28 -08002224 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 GRC_LCLCTRL_GPIO_OE1 |
2226 GRC_LCLCTRL_GPIO_OE2 |
2227 GRC_LCLCTRL_GPIO_OUTPUT1 |
2228 GRC_LCLCTRL_GPIO_OUTPUT2;
2229 if (no_gpio2) {
2230 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2231 GRC_LCLCTRL_GPIO_OUTPUT2);
2232 }
Michael Chanb401e9e2005-12-19 16:27:04 -08002233 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2234 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
2236 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2237
Michael Chanb401e9e2005-12-19 16:27:04 -08002238 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2239 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240
2241 if (!no_gpio2) {
2242 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chanb401e9e2005-12-19 16:27:04 -08002243 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2244 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 }
2246 }
2247 } else {
2248 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
2249 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
Michael Chanb401e9e2005-12-19 16:27:04 -08002250 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2251 (GRC_LCLCTRL_GPIO_OE1 |
2252 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
Michael Chanb401e9e2005-12-19 16:27:04 -08002254 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2255 GRC_LCLCTRL_GPIO_OE1, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
Michael Chanb401e9e2005-12-19 16:27:04 -08002257 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2258 (GRC_LCLCTRL_GPIO_OE1 |
2259 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 }
2261 }
2262}
2263
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002264static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2265{
2266 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2267 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00002268 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002269 if (speed != SPEED_10)
2270 return 1;
2271 } else if (speed == SPEED_10)
2272 return 1;
2273
2274 return 0;
2275}
2276
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277static int tg3_setup_phy(struct tg3 *, int);
2278
2279#define RESET_KIND_SHUTDOWN 0
2280#define RESET_KIND_INIT 1
2281#define RESET_KIND_SUSPEND 2
2282
2283static void tg3_write_sig_post_reset(struct tg3 *, int);
2284static int tg3_halt_cpu(struct tg3 *, u32);
2285
Matt Carlson0a459aa2008-11-03 16:54:15 -08002286static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002287{
Matt Carlsonce057f02007-11-12 21:08:03 -08002288 u32 val;
2289
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002290 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Michael Chan51297242007-02-13 12:17:57 -08002291 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2292 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2293 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2294
2295 sg_dig_ctrl |=
2296 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2297 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2298 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2299 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002300 return;
Michael Chan51297242007-02-13 12:17:57 -08002301 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002302
Michael Chan60189dd2006-12-17 17:08:07 -08002303 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002304 tg3_bmcr_reset(tp);
2305 val = tr32(GRC_MISC_CFG);
2306 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2307 udelay(40);
2308 return;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002309 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson0e5f7842009-11-02 14:26:38 +00002310 u32 phytest;
2311 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2312 u32 phy;
2313
2314 tg3_writephy(tp, MII_ADVERTISE, 0);
2315 tg3_writephy(tp, MII_BMCR,
2316 BMCR_ANENABLE | BMCR_ANRESTART);
2317
2318 tg3_writephy(tp, MII_TG3_FET_TEST,
2319 phytest | MII_TG3_FET_SHADOW_EN);
2320 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
2321 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
2322 tg3_writephy(tp,
2323 MII_TG3_FET_SHDW_AUXMODE4,
2324 phy);
2325 }
2326 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2327 }
2328 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002329 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07002330 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2331 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002332
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002333 val = MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2334 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2335 MII_TG3_AUXCTL_PCTL_VREG_11V;
2336 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, val);
Michael Chan715116a2006-09-27 16:09:25 -07002337 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002338
Michael Chan15c3b692006-03-22 01:06:52 -08002339 /* The PHY should not be powered down on some chips because
2340 * of bugs.
2341 */
2342 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2343 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2344 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002345 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Michael Chan15c3b692006-03-22 01:06:52 -08002346 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08002347
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002348 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2349 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002350 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2351 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2352 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2353 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2354 }
2355
Michael Chan15c3b692006-03-22 01:06:52 -08002356 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2357}
2358
Matt Carlson3f007892008-11-03 16:51:36 -08002359/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002360static int tg3_nvram_lock(struct tg3 *tp)
2361{
2362 if (tp->tg3_flags & TG3_FLAG_NVRAM) {
2363 int i;
2364
2365 if (tp->nvram_lock_cnt == 0) {
2366 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2367 for (i = 0; i < 8000; i++) {
2368 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2369 break;
2370 udelay(20);
2371 }
2372 if (i == 8000) {
2373 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2374 return -ENODEV;
2375 }
2376 }
2377 tp->nvram_lock_cnt++;
2378 }
2379 return 0;
2380}
2381
2382/* tp->lock is held. */
2383static void tg3_nvram_unlock(struct tg3 *tp)
2384{
2385 if (tp->tg3_flags & TG3_FLAG_NVRAM) {
2386 if (tp->nvram_lock_cnt > 0)
2387 tp->nvram_lock_cnt--;
2388 if (tp->nvram_lock_cnt == 0)
2389 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
2390 }
2391}
2392
2393/* tp->lock is held. */
2394static void tg3_enable_nvram_access(struct tg3 *tp)
2395{
2396 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Matt Carlsonf66a29b2009-11-13 13:03:36 +00002397 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002398 u32 nvaccess = tr32(NVRAM_ACCESS);
2399
2400 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
2401 }
2402}
2403
2404/* tp->lock is held. */
2405static void tg3_disable_nvram_access(struct tg3 *tp)
2406{
2407 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Matt Carlsonf66a29b2009-11-13 13:03:36 +00002408 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002409 u32 nvaccess = tr32(NVRAM_ACCESS);
2410
2411 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
2412 }
2413}
2414
2415static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
2416 u32 offset, u32 *val)
2417{
2418 u32 tmp;
2419 int i;
2420
2421 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
2422 return -EINVAL;
2423
2424 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
2425 EEPROM_ADDR_DEVID_MASK |
2426 EEPROM_ADDR_READ);
2427 tw32(GRC_EEPROM_ADDR,
2428 tmp |
2429 (0 << EEPROM_ADDR_DEVID_SHIFT) |
2430 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
2431 EEPROM_ADDR_ADDR_MASK) |
2432 EEPROM_ADDR_READ | EEPROM_ADDR_START);
2433
2434 for (i = 0; i < 1000; i++) {
2435 tmp = tr32(GRC_EEPROM_ADDR);
2436
2437 if (tmp & EEPROM_ADDR_COMPLETE)
2438 break;
2439 msleep(1);
2440 }
2441 if (!(tmp & EEPROM_ADDR_COMPLETE))
2442 return -EBUSY;
2443
Matt Carlson62cedd12009-04-20 14:52:29 -07002444 tmp = tr32(GRC_EEPROM_DATA);
2445
2446 /*
2447 * The data will always be opposite the native endian
2448 * format. Perform a blind byteswap to compensate.
2449 */
2450 *val = swab32(tmp);
2451
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002452 return 0;
2453}
2454
2455#define NVRAM_CMD_TIMEOUT 10000
2456
2457static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
2458{
2459 int i;
2460
2461 tw32(NVRAM_CMD, nvram_cmd);
2462 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
2463 udelay(10);
2464 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
2465 udelay(10);
2466 break;
2467 }
2468 }
2469
2470 if (i == NVRAM_CMD_TIMEOUT)
2471 return -EBUSY;
2472
2473 return 0;
2474}
2475
2476static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
2477{
2478 if ((tp->tg3_flags & TG3_FLAG_NVRAM) &&
2479 (tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) &&
2480 (tp->tg3_flags2 & TG3_FLG2_FLASH) &&
2481 !(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM_ADDR_TRANS) &&
2482 (tp->nvram_jedecnum == JEDEC_ATMEL))
2483
2484 addr = ((addr / tp->nvram_pagesize) <<
2485 ATMEL_AT45DB0X1B_PAGE_POS) +
2486 (addr % tp->nvram_pagesize);
2487
2488 return addr;
2489}
2490
2491static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
2492{
2493 if ((tp->tg3_flags & TG3_FLAG_NVRAM) &&
2494 (tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) &&
2495 (tp->tg3_flags2 & TG3_FLG2_FLASH) &&
2496 !(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM_ADDR_TRANS) &&
2497 (tp->nvram_jedecnum == JEDEC_ATMEL))
2498
2499 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
2500 tp->nvram_pagesize) +
2501 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
2502
2503 return addr;
2504}
2505
Matt Carlsone4f34112009-02-25 14:25:00 +00002506/* NOTE: Data read in from NVRAM is byteswapped according to
2507 * the byteswapping settings for all other register accesses.
2508 * tg3 devices are BE devices, so on a BE machine, the data
2509 * returned will be exactly as it is seen in NVRAM. On a LE
2510 * machine, the 32-bit value will be byteswapped.
2511 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002512static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
2513{
2514 int ret;
2515
2516 if (!(tp->tg3_flags & TG3_FLAG_NVRAM))
2517 return tg3_nvram_read_using_eeprom(tp, offset, val);
2518
2519 offset = tg3_nvram_phys_addr(tp, offset);
2520
2521 if (offset > NVRAM_ADDR_MSK)
2522 return -EINVAL;
2523
2524 ret = tg3_nvram_lock(tp);
2525 if (ret)
2526 return ret;
2527
2528 tg3_enable_nvram_access(tp);
2529
2530 tw32(NVRAM_ADDR, offset);
2531 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
2532 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
2533
2534 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00002535 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002536
2537 tg3_disable_nvram_access(tp);
2538
2539 tg3_nvram_unlock(tp);
2540
2541 return ret;
2542}
2543
Matt Carlsona9dc5292009-02-25 14:25:30 +00002544/* Ensures NVRAM data is in bytestream format. */
2545static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002546{
2547 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00002548 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002549 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00002550 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002551 return res;
2552}
2553
2554/* tp->lock is held. */
Matt Carlson3f007892008-11-03 16:51:36 -08002555static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
2556{
2557 u32 addr_high, addr_low;
2558 int i;
2559
2560 addr_high = ((tp->dev->dev_addr[0] << 8) |
2561 tp->dev->dev_addr[1]);
2562 addr_low = ((tp->dev->dev_addr[2] << 24) |
2563 (tp->dev->dev_addr[3] << 16) |
2564 (tp->dev->dev_addr[4] << 8) |
2565 (tp->dev->dev_addr[5] << 0));
2566 for (i = 0; i < 4; i++) {
2567 if (i == 1 && skip_mac_1)
2568 continue;
2569 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
2570 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
2571 }
2572
2573 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2574 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2575 for (i = 0; i < 12; i++) {
2576 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
2577 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
2578 }
2579 }
2580
2581 addr_high = (tp->dev->dev_addr[0] +
2582 tp->dev->dev_addr[1] +
2583 tp->dev->dev_addr[2] +
2584 tp->dev->dev_addr[3] +
2585 tp->dev->dev_addr[4] +
2586 tp->dev->dev_addr[5]) &
2587 TX_BACKOFF_SEED_MASK;
2588 tw32(MAC_TX_BACKOFF_SEED, addr_high);
2589}
2590
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002591static void tg3_enable_register_access(struct tg3 *tp)
2592{
2593 /*
2594 * Make sure register accesses (indirect or otherwise) will function
2595 * correctly.
2596 */
2597 pci_write_config_dword(tp->pdev,
2598 TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
2599}
2600
2601static int tg3_power_up(struct tg3 *tp)
2602{
2603 tg3_enable_register_access(tp);
2604
2605 pci_set_power_state(tp->pdev, PCI_D0);
2606
2607 /* Switch out of Vaux if it is a NIC */
2608 if (tp->tg3_flags2 & TG3_FLG2_IS_NIC)
2609 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl, 100);
2610
2611 return 0;
2612}
2613
2614static int tg3_power_down_prepare(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615{
2616 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002617 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002619 tg3_enable_register_access(tp);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002620
2621 /* Restore the CLKREQ setting. */
2622 if (tp->tg3_flags3 & TG3_FLG3_CLKREQ_BUG) {
2623 u16 lnkctl;
2624
2625 pci_read_config_word(tp->pdev,
2626 tp->pcie_cap + PCI_EXP_LNKCTL,
2627 &lnkctl);
2628 lnkctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
2629 pci_write_config_word(tp->pdev,
2630 tp->pcie_cap + PCI_EXP_LNKCTL,
2631 lnkctl);
2632 }
2633
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
2635 tw32(TG3PCI_MISC_HOST_CTRL,
2636 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
2637
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002638 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002639 (tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
2640
Matt Carlsondd477002008-05-25 23:45:58 -07002641 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08002642 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002643 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson80096062010-08-02 11:26:06 +00002644 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002645 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002646 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002647
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002648 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002649
Matt Carlson80096062010-08-02 11:26:06 +00002650 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002651
2652 tp->link_config.orig_speed = phydev->speed;
2653 tp->link_config.orig_duplex = phydev->duplex;
2654 tp->link_config.orig_autoneg = phydev->autoneg;
2655 tp->link_config.orig_advertising = phydev->advertising;
2656
2657 advertising = ADVERTISED_TP |
2658 ADVERTISED_Pause |
2659 ADVERTISED_Autoneg |
2660 ADVERTISED_10baseT_Half;
2661
2662 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002663 device_should_wake) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002664 if (tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB)
2665 advertising |=
2666 ADVERTISED_100baseT_Half |
2667 ADVERTISED_100baseT_Full |
2668 ADVERTISED_10baseT_Full;
2669 else
2670 advertising |= ADVERTISED_10baseT_Full;
2671 }
2672
2673 phydev->advertising = advertising;
2674
2675 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002676
2677 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00002678 if (phyid != PHY_ID_BCMAC131) {
2679 phyid &= PHY_BCM_OUI_MASK;
2680 if (phyid == PHY_BCM_OUI_1 ||
2681 phyid == PHY_BCM_OUI_2 ||
2682 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08002683 do_low_power = true;
2684 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002685 }
Matt Carlsondd477002008-05-25 23:45:58 -07002686 } else {
Matt Carlson20232762008-12-21 20:18:56 -08002687 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002688
Matt Carlson80096062010-08-02 11:26:06 +00002689 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
2690 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07002691 tp->link_config.orig_speed = tp->link_config.speed;
2692 tp->link_config.orig_duplex = tp->link_config.duplex;
2693 tp->link_config.orig_autoneg = tp->link_config.autoneg;
2694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002696 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Matt Carlsondd477002008-05-25 23:45:58 -07002697 tp->link_config.speed = SPEED_10;
2698 tp->link_config.duplex = DUPLEX_HALF;
2699 tp->link_config.autoneg = AUTONEG_ENABLE;
2700 tg3_setup_phy(tp, 0);
2701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 }
2703
Michael Chanb5d37722006-09-27 16:06:21 -07002704 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
2705 u32 val;
2706
2707 val = tr32(GRC_VCPU_EXT_CTRL);
2708 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
2709 } else if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08002710 int i;
2711 u32 val;
2712
2713 for (i = 0; i < 200; i++) {
2714 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
2715 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
2716 break;
2717 msleep(1);
2718 }
2719 }
Gary Zambranoa85feb82007-05-05 11:52:19 -07002720 if (tp->tg3_flags & TG3_FLAG_WOL_CAP)
2721 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
2722 WOL_DRV_STATE_SHUTDOWN |
2723 WOL_DRV_WOL |
2724 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08002725
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002726 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 u32 mac_mode;
2728
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002729 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002730 if (do_low_power &&
2731 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
2732 tg3_phy_auxctl_write(tp,
2733 MII_TG3_AUXCTL_SHDWSEL_PWRCTL,
2734 MII_TG3_AUXCTL_PCTL_WOL_EN |
2735 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2736 MII_TG3_AUXCTL_PCTL_CL_AB_TXDAC);
Matt Carlsondd477002008-05-25 23:45:58 -07002737 udelay(40);
2738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002740 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07002741 mac_mode = MAC_MODE_PORT_MODE_GMII;
2742 else
2743 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002745 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
2746 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2747 ASIC_REV_5700) {
2748 u32 speed = (tp->tg3_flags &
2749 TG3_FLAG_WOL_SPEED_100MB) ?
2750 SPEED_100 : SPEED_10;
2751 if (tg3_5700_link_polarity(tp, speed))
2752 mac_mode |= MAC_MODE_LINK_POLARITY;
2753 else
2754 mac_mode &= ~MAC_MODE_LINK_POLARITY;
2755 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 } else {
2757 mac_mode = MAC_MODE_PORT_MODE_TBI;
2758 }
2759
John W. Linvillecbf46852005-04-21 17:01:29 -07002760 if (!(tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 tw32(MAC_LED_CTRL, tp->led_ctrl);
2762
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002763 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
2764 if (((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
2765 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) &&
2766 ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
2767 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)))
2768 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769
Matt Carlsond2394e6b2010-11-24 08:31:47 +00002770 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
2771 mac_mode |= MAC_MODE_APE_TX_EN |
2772 MAC_MODE_APE_RX_EN |
2773 MAC_MODE_TDE_ENABLE;
Matt Carlson3bda1252008-08-15 14:08:22 -07002774
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 tw32_f(MAC_MODE, mac_mode);
2776 udelay(100);
2777
2778 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
2779 udelay(10);
2780 }
2781
2782 if (!(tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB) &&
2783 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2784 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
2785 u32 base_val;
2786
2787 base_val = tp->pci_clock_ctrl;
2788 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
2789 CLOCK_CTRL_TXCLK_DISABLE);
2790
Michael Chanb401e9e2005-12-19 16:27:04 -08002791 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
2792 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Michael Chand7b0a852007-02-13 12:17:38 -08002793 } else if ((tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
Matt Carlson795d01c2007-10-07 23:28:17 -07002794 (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
Michael Chand7b0a852007-02-13 12:17:38 -08002795 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)) {
Michael Chan4cf78e42005-07-25 12:29:19 -07002796 /* do nothing */
Michael Chan85e94ce2005-04-21 17:05:28 -07002797 } else if (!((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF))) {
2799 u32 newbits1, newbits2;
2800
2801 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2802 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2803 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
2804 CLOCK_CTRL_TXCLK_DISABLE |
2805 CLOCK_CTRL_ALTCLK);
2806 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2807 } else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
2808 newbits1 = CLOCK_CTRL_625_CORE;
2809 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
2810 } else {
2811 newbits1 = CLOCK_CTRL_ALTCLK;
2812 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2813 }
2814
Michael Chanb401e9e2005-12-19 16:27:04 -08002815 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
2816 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817
Michael Chanb401e9e2005-12-19 16:27:04 -08002818 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
2819 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820
2821 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
2822 u32 newbits3;
2823
2824 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2825 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2826 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
2827 CLOCK_CTRL_TXCLK_DISABLE |
2828 CLOCK_CTRL_44MHZ_CORE);
2829 } else {
2830 newbits3 = CLOCK_CTRL_44MHZ_CORE;
2831 }
2832
Michael Chanb401e9e2005-12-19 16:27:04 -08002833 tw32_wait_f(TG3PCI_CLOCK_CTRL,
2834 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 }
2836 }
2837
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002838 if (!(device_should_wake) &&
Matt Carlson22435842008-11-21 17:21:13 -08002839 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08002840 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08002841
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 tg3_frob_aux_power(tp);
2843
2844 /* Workaround for unstable PLL clock */
2845 if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
2846 (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
2847 u32 val = tr32(0x7d00);
2848
2849 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
2850 tw32(0x7d00, val);
Michael Chan6921d202005-12-13 21:15:53 -08002851 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08002852 int err;
2853
2854 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08002856 if (!err)
2857 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08002858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 }
2860
Michael Chanbbadf502006-04-06 21:46:34 -07002861 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
2862
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 return 0;
2864}
2865
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002866static void tg3_power_down(struct tg3 *tp)
2867{
2868 tg3_power_down_prepare(tp);
2869
2870 pci_wake_from_d3(tp->pdev, tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
2871 pci_set_power_state(tp->pdev, PCI_D3hot);
2872}
2873
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
2875{
2876 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
2877 case MII_TG3_AUX_STAT_10HALF:
2878 *speed = SPEED_10;
2879 *duplex = DUPLEX_HALF;
2880 break;
2881
2882 case MII_TG3_AUX_STAT_10FULL:
2883 *speed = SPEED_10;
2884 *duplex = DUPLEX_FULL;
2885 break;
2886
2887 case MII_TG3_AUX_STAT_100HALF:
2888 *speed = SPEED_100;
2889 *duplex = DUPLEX_HALF;
2890 break;
2891
2892 case MII_TG3_AUX_STAT_100FULL:
2893 *speed = SPEED_100;
2894 *duplex = DUPLEX_FULL;
2895 break;
2896
2897 case MII_TG3_AUX_STAT_1000HALF:
2898 *speed = SPEED_1000;
2899 *duplex = DUPLEX_HALF;
2900 break;
2901
2902 case MII_TG3_AUX_STAT_1000FULL:
2903 *speed = SPEED_1000;
2904 *duplex = DUPLEX_FULL;
2905 break;
2906
2907 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002908 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07002909 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
2910 SPEED_10;
2911 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
2912 DUPLEX_HALF;
2913 break;
2914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 *speed = SPEED_INVALID;
2916 *duplex = DUPLEX_INVALID;
2917 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919}
2920
2921static void tg3_phy_copper_begin(struct tg3 *tp)
2922{
2923 u32 new_adv;
2924 int i;
2925
Matt Carlson80096062010-08-02 11:26:06 +00002926 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 /* Entering low power mode. Disable gigabit and
2928 * 100baseT advertisements.
2929 */
2930 tg3_writephy(tp, MII_TG3_CTRL, 0);
2931
2932 new_adv = (ADVERTISE_10HALF | ADVERTISE_10FULL |
2933 ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
2934 if (tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB)
2935 new_adv |= (ADVERTISE_100HALF | ADVERTISE_100FULL);
2936
2937 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2938 } else if (tp->link_config.speed == SPEED_INVALID) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002939 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 tp->link_config.advertising &=
2941 ~(ADVERTISED_1000baseT_Half |
2942 ADVERTISED_1000baseT_Full);
2943
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002944 new_adv = ADVERTISE_CSMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 if (tp->link_config.advertising & ADVERTISED_10baseT_Half)
2946 new_adv |= ADVERTISE_10HALF;
2947 if (tp->link_config.advertising & ADVERTISED_10baseT_Full)
2948 new_adv |= ADVERTISE_10FULL;
2949 if (tp->link_config.advertising & ADVERTISED_100baseT_Half)
2950 new_adv |= ADVERTISE_100HALF;
2951 if (tp->link_config.advertising & ADVERTISED_100baseT_Full)
2952 new_adv |= ADVERTISE_100FULL;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002953
2954 new_adv |= tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2955
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2957
2958 if (tp->link_config.advertising &
2959 (ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full)) {
2960 new_adv = 0;
2961 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
2962 new_adv |= MII_TG3_CTRL_ADV_1000_HALF;
2963 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
2964 new_adv |= MII_TG3_CTRL_ADV_1000_FULL;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002965 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2967 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0))
2968 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2969 MII_TG3_CTRL_ENABLE_AS_MASTER);
2970 tg3_writephy(tp, MII_TG3_CTRL, new_adv);
2971 } else {
2972 tg3_writephy(tp, MII_TG3_CTRL, 0);
2973 }
2974 } else {
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002975 new_adv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2976 new_adv |= ADVERTISE_CSMA;
2977
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 /* Asking for a specific link mode. */
2979 if (tp->link_config.speed == SPEED_1000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2981
2982 if (tp->link_config.duplex == DUPLEX_FULL)
2983 new_adv = MII_TG3_CTRL_ADV_1000_FULL;
2984 else
2985 new_adv = MII_TG3_CTRL_ADV_1000_HALF;
2986 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2987 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
2988 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2989 MII_TG3_CTRL_ENABLE_AS_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 if (tp->link_config.speed == SPEED_100) {
2992 if (tp->link_config.duplex == DUPLEX_FULL)
2993 new_adv |= ADVERTISE_100FULL;
2994 else
2995 new_adv |= ADVERTISE_100HALF;
2996 } else {
2997 if (tp->link_config.duplex == DUPLEX_FULL)
2998 new_adv |= ADVERTISE_10FULL;
2999 else
3000 new_adv |= ADVERTISE_10HALF;
3001 }
3002 tg3_writephy(tp, MII_ADVERTISE, new_adv);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003003
3004 new_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003006
3007 tg3_writephy(tp, MII_TG3_CTRL, new_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 }
3009
Matt Carlson52b02d02010-10-14 10:37:41 +00003010 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
Matt Carlsona6b68da2010-12-06 08:28:52 +00003011 u32 val;
Matt Carlson52b02d02010-10-14 10:37:41 +00003012
3013 tw32(TG3_CPMU_EEE_MODE,
3014 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
3015
Matt Carlson1d36ba42011-04-20 07:57:42 +00003016 TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
Matt Carlson52b02d02010-10-14 10:37:41 +00003017
Matt Carlson21a00ab2011-01-25 15:58:55 +00003018 switch (GET_ASIC_REV(tp->pci_chip_rev_id)) {
3019 case ASIC_REV_5717:
3020 case ASIC_REV_57765:
3021 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
3022 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
3023 MII_TG3_DSP_CH34TP2_HIBW01);
3024 /* Fall through */
3025 case ASIC_REV_5719:
3026 val = MII_TG3_DSP_TAP26_ALNOKO |
3027 MII_TG3_DSP_TAP26_RMRXSTO |
3028 MII_TG3_DSP_TAP26_OPCSINPT;
3029 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
3030 }
Matt Carlson52b02d02010-10-14 10:37:41 +00003031
Matt Carlsona6b68da2010-12-06 08:28:52 +00003032 val = 0;
Matt Carlson52b02d02010-10-14 10:37:41 +00003033 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3034 /* Advertise 100-BaseTX EEE ability */
3035 if (tp->link_config.advertising &
Matt Carlson3110f5f52010-12-06 08:28:50 +00003036 ADVERTISED_100baseT_Full)
3037 val |= MDIO_AN_EEE_ADV_100TX;
Matt Carlson52b02d02010-10-14 10:37:41 +00003038 /* Advertise 1000-BaseT EEE ability */
3039 if (tp->link_config.advertising &
Matt Carlson3110f5f52010-12-06 08:28:50 +00003040 ADVERTISED_1000baseT_Full)
3041 val |= MDIO_AN_EEE_ADV_1000T;
Matt Carlson52b02d02010-10-14 10:37:41 +00003042 }
Matt Carlson3110f5f52010-12-06 08:28:50 +00003043 tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlson52b02d02010-10-14 10:37:41 +00003044
Matt Carlson1d36ba42011-04-20 07:57:42 +00003045 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Matt Carlson52b02d02010-10-14 10:37:41 +00003046 }
3047
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 if (tp->link_config.autoneg == AUTONEG_DISABLE &&
3049 tp->link_config.speed != SPEED_INVALID) {
3050 u32 bmcr, orig_bmcr;
3051
3052 tp->link_config.active_speed = tp->link_config.speed;
3053 tp->link_config.active_duplex = tp->link_config.duplex;
3054
3055 bmcr = 0;
3056 switch (tp->link_config.speed) {
3057 default:
3058 case SPEED_10:
3059 break;
3060
3061 case SPEED_100:
3062 bmcr |= BMCR_SPEED100;
3063 break;
3064
3065 case SPEED_1000:
3066 bmcr |= TG3_BMCR_SPEED1000;
3067 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069
3070 if (tp->link_config.duplex == DUPLEX_FULL)
3071 bmcr |= BMCR_FULLDPLX;
3072
3073 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
3074 (bmcr != orig_bmcr)) {
3075 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
3076 for (i = 0; i < 1500; i++) {
3077 u32 tmp;
3078
3079 udelay(10);
3080 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
3081 tg3_readphy(tp, MII_BMSR, &tmp))
3082 continue;
3083 if (!(tmp & BMSR_LSTATUS)) {
3084 udelay(40);
3085 break;
3086 }
3087 }
3088 tg3_writephy(tp, MII_BMCR, bmcr);
3089 udelay(40);
3090 }
3091 } else {
3092 tg3_writephy(tp, MII_BMCR,
3093 BMCR_ANENABLE | BMCR_ANRESTART);
3094 }
3095}
3096
3097static int tg3_init_5401phy_dsp(struct tg3 *tp)
3098{
3099 int err;
3100
3101 /* Turn off tap power management. */
3102 /* Set Extended packet length bit */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003103 err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00003105 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
3106 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
3107 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
3108 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
3109 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110
3111 udelay(40);
3112
3113 return err;
3114}
3115
Michael Chan3600d912006-12-07 00:21:48 -08003116static int tg3_copper_is_advertising_all(struct tg3 *tp, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117{
Michael Chan3600d912006-12-07 00:21:48 -08003118 u32 adv_reg, all_mask = 0;
3119
3120 if (mask & ADVERTISED_10baseT_Half)
3121 all_mask |= ADVERTISE_10HALF;
3122 if (mask & ADVERTISED_10baseT_Full)
3123 all_mask |= ADVERTISE_10FULL;
3124 if (mask & ADVERTISED_100baseT_Half)
3125 all_mask |= ADVERTISE_100HALF;
3126 if (mask & ADVERTISED_100baseT_Full)
3127 all_mask |= ADVERTISE_100FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128
3129 if (tg3_readphy(tp, MII_ADVERTISE, &adv_reg))
3130 return 0;
3131
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 if ((adv_reg & all_mask) != all_mask)
3133 return 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003134 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 u32 tg3_ctrl;
3136
Michael Chan3600d912006-12-07 00:21:48 -08003137 all_mask = 0;
3138 if (mask & ADVERTISED_1000baseT_Half)
3139 all_mask |= ADVERTISE_1000HALF;
3140 if (mask & ADVERTISED_1000baseT_Full)
3141 all_mask |= ADVERTISE_1000FULL;
3142
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 if (tg3_readphy(tp, MII_TG3_CTRL, &tg3_ctrl))
3144 return 0;
3145
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 if ((tg3_ctrl & all_mask) != all_mask)
3147 return 0;
3148 }
3149 return 1;
3150}
3151
Matt Carlsonef167e22007-12-20 20:10:01 -08003152static int tg3_adv_1000T_flowctrl_ok(struct tg3 *tp, u32 *lcladv, u32 *rmtadv)
3153{
3154 u32 curadv, reqadv;
3155
3156 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
3157 return 1;
3158
3159 curadv = *lcladv & (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
3160 reqadv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
3161
3162 if (tp->link_config.active_duplex == DUPLEX_FULL) {
3163 if (curadv != reqadv)
3164 return 0;
3165
3166 if (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG)
3167 tg3_readphy(tp, MII_LPA, rmtadv);
3168 } else {
3169 /* Reprogram the advertisement register, even if it
3170 * does not affect the current link. If the link
3171 * gets renegotiated in the future, we can save an
3172 * additional renegotiation cycle by advertising
3173 * it correctly in the first place.
3174 */
3175 if (curadv != reqadv) {
3176 *lcladv &= ~(ADVERTISE_PAUSE_CAP |
3177 ADVERTISE_PAUSE_ASYM);
3178 tg3_writephy(tp, MII_ADVERTISE, *lcladv | reqadv);
3179 }
3180 }
3181
3182 return 1;
3183}
3184
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
3186{
3187 int current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003188 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08003189 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 u16 current_speed;
3191 u8 current_duplex;
3192 int i, err;
3193
3194 tw32(MAC_EVENT, 0);
3195
3196 tw32_f(MAC_STATUS,
3197 (MAC_STATUS_SYNC_CHANGED |
3198 MAC_STATUS_CFG_CHANGED |
3199 MAC_STATUS_MI_COMPLETION |
3200 MAC_STATUS_LNKSTATE_CHANGED));
3201 udelay(40);
3202
Matt Carlson8ef21422008-05-02 16:47:53 -07003203 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
3204 tw32_f(MAC_MI_MODE,
3205 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
3206 udelay(80);
3207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003209 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210
3211 /* Some third-party PHYs need to be reset on link going
3212 * down.
3213 */
3214 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3215 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
3216 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
3217 netif_carrier_ok(tp->dev)) {
3218 tg3_readphy(tp, MII_BMSR, &bmsr);
3219 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3220 !(bmsr & BMSR_LSTATUS))
3221 force_reset = 1;
3222 }
3223 if (force_reset)
3224 tg3_phy_reset(tp);
3225
Matt Carlson79eb6902010-02-17 15:17:03 +00003226 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 tg3_readphy(tp, MII_BMSR, &bmsr);
3228 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
3229 !(tp->tg3_flags & TG3_FLAG_INIT_COMPLETE))
3230 bmsr = 0;
3231
3232 if (!(bmsr & BMSR_LSTATUS)) {
3233 err = tg3_init_5401phy_dsp(tp);
3234 if (err)
3235 return err;
3236
3237 tg3_readphy(tp, MII_BMSR, &bmsr);
3238 for (i = 0; i < 1000; i++) {
3239 udelay(10);
3240 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3241 (bmsr & BMSR_LSTATUS)) {
3242 udelay(40);
3243 break;
3244 }
3245 }
3246
Matt Carlson79eb6902010-02-17 15:17:03 +00003247 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
3248 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 !(bmsr & BMSR_LSTATUS) &&
3250 tp->link_config.active_speed == SPEED_1000) {
3251 err = tg3_phy_reset(tp);
3252 if (!err)
3253 err = tg3_init_5401phy_dsp(tp);
3254 if (err)
3255 return err;
3256 }
3257 }
3258 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3259 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
3260 /* 5701 {A0,B0} CRC bug workaround */
3261 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00003262 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
3263 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
3264 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 }
3266
3267 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003268 tg3_readphy(tp, MII_TG3_ISTAT, &val);
3269 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003271 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003273 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 tg3_writephy(tp, MII_TG3_IMASK, ~0);
3275
3276 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3277 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3278 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
3279 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3280 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
3281 else
3282 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
3283 }
3284
3285 current_link_up = 0;
3286 current_speed = SPEED_INVALID;
3287 current_duplex = DUPLEX_INVALID;
3288
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003289 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Matt Carlson15ee95c2011-04-20 07:57:40 +00003290 err = tg3_phy_auxctl_read(tp,
3291 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
3292 &val);
3293 if (!err && !(val & (1 << 10))) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003294 tg3_phy_auxctl_write(tp,
3295 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
3296 val | (1 << 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 goto relink;
3298 }
3299 }
3300
3301 bmsr = 0;
3302 for (i = 0; i < 100; i++) {
3303 tg3_readphy(tp, MII_BMSR, &bmsr);
3304 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3305 (bmsr & BMSR_LSTATUS))
3306 break;
3307 udelay(40);
3308 }
3309
3310 if (bmsr & BMSR_LSTATUS) {
3311 u32 aux_stat, bmcr;
3312
3313 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
3314 for (i = 0; i < 2000; i++) {
3315 udelay(10);
3316 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
3317 aux_stat)
3318 break;
3319 }
3320
3321 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
3322 &current_speed,
3323 &current_duplex);
3324
3325 bmcr = 0;
3326 for (i = 0; i < 200; i++) {
3327 tg3_readphy(tp, MII_BMCR, &bmcr);
3328 if (tg3_readphy(tp, MII_BMCR, &bmcr))
3329 continue;
3330 if (bmcr && bmcr != 0x7fff)
3331 break;
3332 udelay(10);
3333 }
3334
Matt Carlsonef167e22007-12-20 20:10:01 -08003335 lcl_adv = 0;
3336 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337
Matt Carlsonef167e22007-12-20 20:10:01 -08003338 tp->link_config.active_speed = current_speed;
3339 tp->link_config.active_duplex = current_duplex;
3340
3341 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3342 if ((bmcr & BMCR_ANENABLE) &&
3343 tg3_copper_is_advertising_all(tp,
3344 tp->link_config.advertising)) {
3345 if (tg3_adv_1000T_flowctrl_ok(tp, &lcl_adv,
3346 &rmt_adv))
3347 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 }
3349 } else {
3350 if (!(bmcr & BMCR_ANENABLE) &&
3351 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08003352 tp->link_config.duplex == current_duplex &&
3353 tp->link_config.flowctrl ==
3354 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 }
3357 }
3358
Matt Carlsonef167e22007-12-20 20:10:01 -08003359 if (current_link_up == 1 &&
3360 tp->link_config.active_duplex == DUPLEX_FULL)
3361 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 }
3363
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364relink:
Matt Carlson80096062010-08-02 11:26:06 +00003365 if (current_link_up == 0 || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 tg3_phy_copper_begin(tp);
3367
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003368 tg3_readphy(tp, MII_BMSR, &bmsr);
3369 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3370 (bmsr & BMSR_LSTATUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 current_link_up = 1;
3372 }
3373
3374 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
3375 if (current_link_up == 1) {
3376 if (tp->link_config.active_speed == SPEED_100 ||
3377 tp->link_config.active_speed == SPEED_10)
3378 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3379 else
3380 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003381 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00003382 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3383 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
3385
3386 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
3387 if (tp->link_config.active_duplex == DUPLEX_HALF)
3388 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
3389
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003391 if (current_link_up == 1 &&
3392 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003394 else
3395 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 }
3397
3398 /* ??? Without this setting Netgear GA302T PHY does not
3399 * ??? send/receive packets...
3400 */
Matt Carlson79eb6902010-02-17 15:17:03 +00003401 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
3403 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
3404 tw32_f(MAC_MI_MODE, tp->mi_mode);
3405 udelay(80);
3406 }
3407
3408 tw32_f(MAC_MODE, tp->mac_mode);
3409 udelay(40);
3410
Matt Carlson52b02d02010-10-14 10:37:41 +00003411 tg3_phy_eee_adjust(tp, current_link_up);
3412
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 if (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) {
3414 /* Polled via timer. */
3415 tw32_f(MAC_EVENT, 0);
3416 } else {
3417 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
3418 }
3419 udelay(40);
3420
3421 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
3422 current_link_up == 1 &&
3423 tp->link_config.active_speed == SPEED_1000 &&
3424 ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) ||
3425 (tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED))) {
3426 udelay(120);
3427 tw32_f(MAC_STATUS,
3428 (MAC_STATUS_SYNC_CHANGED |
3429 MAC_STATUS_CFG_CHANGED));
3430 udelay(40);
3431 tg3_write_mem(tp,
3432 NIC_SRAM_FIRMWARE_MBOX,
3433 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
3434 }
3435
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003436 /* Prevent send BD corruption. */
3437 if (tp->tg3_flags3 & TG3_FLG3_CLKREQ_BUG) {
3438 u16 oldlnkctl, newlnkctl;
3439
3440 pci_read_config_word(tp->pdev,
3441 tp->pcie_cap + PCI_EXP_LNKCTL,
3442 &oldlnkctl);
3443 if (tp->link_config.active_speed == SPEED_100 ||
3444 tp->link_config.active_speed == SPEED_10)
3445 newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
3446 else
3447 newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
3448 if (newlnkctl != oldlnkctl)
3449 pci_write_config_word(tp->pdev,
3450 tp->pcie_cap + PCI_EXP_LNKCTL,
3451 newlnkctl);
3452 }
3453
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 if (current_link_up != netif_carrier_ok(tp->dev)) {
3455 if (current_link_up)
3456 netif_carrier_on(tp->dev);
3457 else
3458 netif_carrier_off(tp->dev);
3459 tg3_link_report(tp);
3460 }
3461
3462 return 0;
3463}
3464
3465struct tg3_fiber_aneginfo {
3466 int state;
3467#define ANEG_STATE_UNKNOWN 0
3468#define ANEG_STATE_AN_ENABLE 1
3469#define ANEG_STATE_RESTART_INIT 2
3470#define ANEG_STATE_RESTART 3
3471#define ANEG_STATE_DISABLE_LINK_OK 4
3472#define ANEG_STATE_ABILITY_DETECT_INIT 5
3473#define ANEG_STATE_ABILITY_DETECT 6
3474#define ANEG_STATE_ACK_DETECT_INIT 7
3475#define ANEG_STATE_ACK_DETECT 8
3476#define ANEG_STATE_COMPLETE_ACK_INIT 9
3477#define ANEG_STATE_COMPLETE_ACK 10
3478#define ANEG_STATE_IDLE_DETECT_INIT 11
3479#define ANEG_STATE_IDLE_DETECT 12
3480#define ANEG_STATE_LINK_OK 13
3481#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
3482#define ANEG_STATE_NEXT_PAGE_WAIT 15
3483
3484 u32 flags;
3485#define MR_AN_ENABLE 0x00000001
3486#define MR_RESTART_AN 0x00000002
3487#define MR_AN_COMPLETE 0x00000004
3488#define MR_PAGE_RX 0x00000008
3489#define MR_NP_LOADED 0x00000010
3490#define MR_TOGGLE_TX 0x00000020
3491#define MR_LP_ADV_FULL_DUPLEX 0x00000040
3492#define MR_LP_ADV_HALF_DUPLEX 0x00000080
3493#define MR_LP_ADV_SYM_PAUSE 0x00000100
3494#define MR_LP_ADV_ASYM_PAUSE 0x00000200
3495#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
3496#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
3497#define MR_LP_ADV_NEXT_PAGE 0x00001000
3498#define MR_TOGGLE_RX 0x00002000
3499#define MR_NP_RX 0x00004000
3500
3501#define MR_LINK_OK 0x80000000
3502
3503 unsigned long link_time, cur_time;
3504
3505 u32 ability_match_cfg;
3506 int ability_match_count;
3507
3508 char ability_match, idle_match, ack_match;
3509
3510 u32 txconfig, rxconfig;
3511#define ANEG_CFG_NP 0x00000080
3512#define ANEG_CFG_ACK 0x00000040
3513#define ANEG_CFG_RF2 0x00000020
3514#define ANEG_CFG_RF1 0x00000010
3515#define ANEG_CFG_PS2 0x00000001
3516#define ANEG_CFG_PS1 0x00008000
3517#define ANEG_CFG_HD 0x00004000
3518#define ANEG_CFG_FD 0x00002000
3519#define ANEG_CFG_INVAL 0x00001f06
3520
3521};
3522#define ANEG_OK 0
3523#define ANEG_DONE 1
3524#define ANEG_TIMER_ENAB 2
3525#define ANEG_FAILED -1
3526
3527#define ANEG_STATE_SETTLE_TIME 10000
3528
3529static int tg3_fiber_aneg_smachine(struct tg3 *tp,
3530 struct tg3_fiber_aneginfo *ap)
3531{
Matt Carlson5be73b42007-12-20 20:09:29 -08003532 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 unsigned long delta;
3534 u32 rx_cfg_reg;
3535 int ret;
3536
3537 if (ap->state == ANEG_STATE_UNKNOWN) {
3538 ap->rxconfig = 0;
3539 ap->link_time = 0;
3540 ap->cur_time = 0;
3541 ap->ability_match_cfg = 0;
3542 ap->ability_match_count = 0;
3543 ap->ability_match = 0;
3544 ap->idle_match = 0;
3545 ap->ack_match = 0;
3546 }
3547 ap->cur_time++;
3548
3549 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
3550 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
3551
3552 if (rx_cfg_reg != ap->ability_match_cfg) {
3553 ap->ability_match_cfg = rx_cfg_reg;
3554 ap->ability_match = 0;
3555 ap->ability_match_count = 0;
3556 } else {
3557 if (++ap->ability_match_count > 1) {
3558 ap->ability_match = 1;
3559 ap->ability_match_cfg = rx_cfg_reg;
3560 }
3561 }
3562 if (rx_cfg_reg & ANEG_CFG_ACK)
3563 ap->ack_match = 1;
3564 else
3565 ap->ack_match = 0;
3566
3567 ap->idle_match = 0;
3568 } else {
3569 ap->idle_match = 1;
3570 ap->ability_match_cfg = 0;
3571 ap->ability_match_count = 0;
3572 ap->ability_match = 0;
3573 ap->ack_match = 0;
3574
3575 rx_cfg_reg = 0;
3576 }
3577
3578 ap->rxconfig = rx_cfg_reg;
3579 ret = ANEG_OK;
3580
Matt Carlson33f401a2010-04-05 10:19:27 +00003581 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 case ANEG_STATE_UNKNOWN:
3583 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
3584 ap->state = ANEG_STATE_AN_ENABLE;
3585
3586 /* fallthru */
3587 case ANEG_STATE_AN_ENABLE:
3588 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
3589 if (ap->flags & MR_AN_ENABLE) {
3590 ap->link_time = 0;
3591 ap->cur_time = 0;
3592 ap->ability_match_cfg = 0;
3593 ap->ability_match_count = 0;
3594 ap->ability_match = 0;
3595 ap->idle_match = 0;
3596 ap->ack_match = 0;
3597
3598 ap->state = ANEG_STATE_RESTART_INIT;
3599 } else {
3600 ap->state = ANEG_STATE_DISABLE_LINK_OK;
3601 }
3602 break;
3603
3604 case ANEG_STATE_RESTART_INIT:
3605 ap->link_time = ap->cur_time;
3606 ap->flags &= ~(MR_NP_LOADED);
3607 ap->txconfig = 0;
3608 tw32(MAC_TX_AUTO_NEG, 0);
3609 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3610 tw32_f(MAC_MODE, tp->mac_mode);
3611 udelay(40);
3612
3613 ret = ANEG_TIMER_ENAB;
3614 ap->state = ANEG_STATE_RESTART;
3615
3616 /* fallthru */
3617 case ANEG_STATE_RESTART:
3618 delta = ap->cur_time - ap->link_time;
Matt Carlson859a588792010-04-05 10:19:28 +00003619 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a588792010-04-05 10:19:28 +00003621 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 break;
3624
3625 case ANEG_STATE_DISABLE_LINK_OK:
3626 ret = ANEG_DONE;
3627 break;
3628
3629 case ANEG_STATE_ABILITY_DETECT_INIT:
3630 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08003631 ap->txconfig = ANEG_CFG_FD;
3632 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3633 if (flowctrl & ADVERTISE_1000XPAUSE)
3634 ap->txconfig |= ANEG_CFG_PS1;
3635 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3636 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3638 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3639 tw32_f(MAC_MODE, tp->mac_mode);
3640 udelay(40);
3641
3642 ap->state = ANEG_STATE_ABILITY_DETECT;
3643 break;
3644
3645 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a588792010-04-05 10:19:28 +00003646 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 break;
3649
3650 case ANEG_STATE_ACK_DETECT_INIT:
3651 ap->txconfig |= ANEG_CFG_ACK;
3652 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3653 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3654 tw32_f(MAC_MODE, tp->mac_mode);
3655 udelay(40);
3656
3657 ap->state = ANEG_STATE_ACK_DETECT;
3658
3659 /* fallthru */
3660 case ANEG_STATE_ACK_DETECT:
3661 if (ap->ack_match != 0) {
3662 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
3663 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
3664 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
3665 } else {
3666 ap->state = ANEG_STATE_AN_ENABLE;
3667 }
3668 } else if (ap->ability_match != 0 &&
3669 ap->rxconfig == 0) {
3670 ap->state = ANEG_STATE_AN_ENABLE;
3671 }
3672 break;
3673
3674 case ANEG_STATE_COMPLETE_ACK_INIT:
3675 if (ap->rxconfig & ANEG_CFG_INVAL) {
3676 ret = ANEG_FAILED;
3677 break;
3678 }
3679 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
3680 MR_LP_ADV_HALF_DUPLEX |
3681 MR_LP_ADV_SYM_PAUSE |
3682 MR_LP_ADV_ASYM_PAUSE |
3683 MR_LP_ADV_REMOTE_FAULT1 |
3684 MR_LP_ADV_REMOTE_FAULT2 |
3685 MR_LP_ADV_NEXT_PAGE |
3686 MR_TOGGLE_RX |
3687 MR_NP_RX);
3688 if (ap->rxconfig & ANEG_CFG_FD)
3689 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
3690 if (ap->rxconfig & ANEG_CFG_HD)
3691 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
3692 if (ap->rxconfig & ANEG_CFG_PS1)
3693 ap->flags |= MR_LP_ADV_SYM_PAUSE;
3694 if (ap->rxconfig & ANEG_CFG_PS2)
3695 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
3696 if (ap->rxconfig & ANEG_CFG_RF1)
3697 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
3698 if (ap->rxconfig & ANEG_CFG_RF2)
3699 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
3700 if (ap->rxconfig & ANEG_CFG_NP)
3701 ap->flags |= MR_LP_ADV_NEXT_PAGE;
3702
3703 ap->link_time = ap->cur_time;
3704
3705 ap->flags ^= (MR_TOGGLE_TX);
3706 if (ap->rxconfig & 0x0008)
3707 ap->flags |= MR_TOGGLE_RX;
3708 if (ap->rxconfig & ANEG_CFG_NP)
3709 ap->flags |= MR_NP_RX;
3710 ap->flags |= MR_PAGE_RX;
3711
3712 ap->state = ANEG_STATE_COMPLETE_ACK;
3713 ret = ANEG_TIMER_ENAB;
3714 break;
3715
3716 case ANEG_STATE_COMPLETE_ACK:
3717 if (ap->ability_match != 0 &&
3718 ap->rxconfig == 0) {
3719 ap->state = ANEG_STATE_AN_ENABLE;
3720 break;
3721 }
3722 delta = ap->cur_time - ap->link_time;
3723 if (delta > ANEG_STATE_SETTLE_TIME) {
3724 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
3725 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3726 } else {
3727 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
3728 !(ap->flags & MR_NP_RX)) {
3729 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3730 } else {
3731 ret = ANEG_FAILED;
3732 }
3733 }
3734 }
3735 break;
3736
3737 case ANEG_STATE_IDLE_DETECT_INIT:
3738 ap->link_time = ap->cur_time;
3739 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3740 tw32_f(MAC_MODE, tp->mac_mode);
3741 udelay(40);
3742
3743 ap->state = ANEG_STATE_IDLE_DETECT;
3744 ret = ANEG_TIMER_ENAB;
3745 break;
3746
3747 case ANEG_STATE_IDLE_DETECT:
3748 if (ap->ability_match != 0 &&
3749 ap->rxconfig == 0) {
3750 ap->state = ANEG_STATE_AN_ENABLE;
3751 break;
3752 }
3753 delta = ap->cur_time - ap->link_time;
3754 if (delta > ANEG_STATE_SETTLE_TIME) {
3755 /* XXX another gem from the Broadcom driver :( */
3756 ap->state = ANEG_STATE_LINK_OK;
3757 }
3758 break;
3759
3760 case ANEG_STATE_LINK_OK:
3761 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
3762 ret = ANEG_DONE;
3763 break;
3764
3765 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
3766 /* ??? unimplemented */
3767 break;
3768
3769 case ANEG_STATE_NEXT_PAGE_WAIT:
3770 /* ??? unimplemented */
3771 break;
3772
3773 default:
3774 ret = ANEG_FAILED;
3775 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777
3778 return ret;
3779}
3780
Matt Carlson5be73b42007-12-20 20:09:29 -08003781static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782{
3783 int res = 0;
3784 struct tg3_fiber_aneginfo aninfo;
3785 int status = ANEG_FAILED;
3786 unsigned int tick;
3787 u32 tmp;
3788
3789 tw32_f(MAC_TX_AUTO_NEG, 0);
3790
3791 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
3792 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
3793 udelay(40);
3794
3795 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
3796 udelay(40);
3797
3798 memset(&aninfo, 0, sizeof(aninfo));
3799 aninfo.flags |= MR_AN_ENABLE;
3800 aninfo.state = ANEG_STATE_UNKNOWN;
3801 aninfo.cur_time = 0;
3802 tick = 0;
3803 while (++tick < 195000) {
3804 status = tg3_fiber_aneg_smachine(tp, &aninfo);
3805 if (status == ANEG_DONE || status == ANEG_FAILED)
3806 break;
3807
3808 udelay(1);
3809 }
3810
3811 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3812 tw32_f(MAC_MODE, tp->mac_mode);
3813 udelay(40);
3814
Matt Carlson5be73b42007-12-20 20:09:29 -08003815 *txflags = aninfo.txconfig;
3816 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817
3818 if (status == ANEG_DONE &&
3819 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
3820 MR_LP_ADV_FULL_DUPLEX)))
3821 res = 1;
3822
3823 return res;
3824}
3825
3826static void tg3_init_bcm8002(struct tg3 *tp)
3827{
3828 u32 mac_status = tr32(MAC_STATUS);
3829 int i;
3830
3831 /* Reset when initting first time or we have a link. */
3832 if ((tp->tg3_flags & TG3_FLAG_INIT_COMPLETE) &&
3833 !(mac_status & MAC_STATUS_PCS_SYNCED))
3834 return;
3835
3836 /* Set PLL lock range. */
3837 tg3_writephy(tp, 0x16, 0x8007);
3838
3839 /* SW reset */
3840 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
3841
3842 /* Wait for reset to complete. */
3843 /* XXX schedule_timeout() ... */
3844 for (i = 0; i < 500; i++)
3845 udelay(10);
3846
3847 /* Config mode; select PMA/Ch 1 regs. */
3848 tg3_writephy(tp, 0x10, 0x8411);
3849
3850 /* Enable auto-lock and comdet, select txclk for tx. */
3851 tg3_writephy(tp, 0x11, 0x0a10);
3852
3853 tg3_writephy(tp, 0x18, 0x00a0);
3854 tg3_writephy(tp, 0x16, 0x41ff);
3855
3856 /* Assert and deassert POR. */
3857 tg3_writephy(tp, 0x13, 0x0400);
3858 udelay(40);
3859 tg3_writephy(tp, 0x13, 0x0000);
3860
3861 tg3_writephy(tp, 0x11, 0x0a50);
3862 udelay(40);
3863 tg3_writephy(tp, 0x11, 0x0a10);
3864
3865 /* Wait for signal to stabilize */
3866 /* XXX schedule_timeout() ... */
3867 for (i = 0; i < 15000; i++)
3868 udelay(10);
3869
3870 /* Deselect the channel register so we can read the PHYID
3871 * later.
3872 */
3873 tg3_writephy(tp, 0x10, 0x8011);
3874}
3875
3876static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
3877{
Matt Carlson82cd3d12007-12-20 20:09:00 -08003878 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879 u32 sg_dig_ctrl, sg_dig_status;
3880 u32 serdes_cfg, expected_sg_dig_ctrl;
3881 int workaround, port_a;
3882 int current_link_up;
3883
3884 serdes_cfg = 0;
3885 expected_sg_dig_ctrl = 0;
3886 workaround = 0;
3887 port_a = 1;
3888 current_link_up = 0;
3889
3890 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
3891 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
3892 workaround = 1;
3893 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
3894 port_a = 0;
3895
3896 /* preserve bits 0-11,13,14 for signal pre-emphasis */
3897 /* preserve bits 20-23 for voltage regulator */
3898 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
3899 }
3900
3901 sg_dig_ctrl = tr32(SG_DIG_CTRL);
3902
3903 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003904 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905 if (workaround) {
3906 u32 val = serdes_cfg;
3907
3908 if (port_a)
3909 val |= 0xc010000;
3910 else
3911 val |= 0x4010000;
3912 tw32_f(MAC_SERDES_CFG, val);
3913 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003914
3915 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 }
3917 if (mac_status & MAC_STATUS_PCS_SYNCED) {
3918 tg3_setup_flow_control(tp, 0, 0);
3919 current_link_up = 1;
3920 }
3921 goto out;
3922 }
3923
3924 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003925 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926
Matt Carlson82cd3d12007-12-20 20:09:00 -08003927 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3928 if (flowctrl & ADVERTISE_1000XPAUSE)
3929 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
3930 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3931 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932
3933 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003934 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07003935 tp->serdes_counter &&
3936 ((mac_status & (MAC_STATUS_PCS_SYNCED |
3937 MAC_STATUS_RCVD_CFG)) ==
3938 MAC_STATUS_PCS_SYNCED)) {
3939 tp->serdes_counter--;
3940 current_link_up = 1;
3941 goto out;
3942 }
3943restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 if (workaround)
3945 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003946 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 udelay(5);
3948 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
3949
Michael Chan3d3ebe72006-09-27 15:59:15 -07003950 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003951 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
3953 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003954 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 mac_status = tr32(MAC_STATUS);
3956
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003957 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08003959 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960
Matt Carlson82cd3d12007-12-20 20:09:00 -08003961 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
3962 local_adv |= ADVERTISE_1000XPAUSE;
3963 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
3964 local_adv |= ADVERTISE_1000XPSE_ASYM;
3965
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003966 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003967 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003968 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003969 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970
3971 tg3_setup_flow_control(tp, local_adv, remote_adv);
3972 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003973 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003974 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003975 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003976 if (tp->serdes_counter)
3977 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 else {
3979 if (workaround) {
3980 u32 val = serdes_cfg;
3981
3982 if (port_a)
3983 val |= 0xc010000;
3984 else
3985 val |= 0x4010000;
3986
3987 tw32_f(MAC_SERDES_CFG, val);
3988 }
3989
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003990 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 udelay(40);
3992
3993 /* Link parallel detection - link is up */
3994 /* only if we have PCS_SYNC and not */
3995 /* receiving config code words */
3996 mac_status = tr32(MAC_STATUS);
3997 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
3998 !(mac_status & MAC_STATUS_RCVD_CFG)) {
3999 tg3_setup_flow_control(tp, 0, 0);
4000 current_link_up = 1;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004001 tp->phy_flags |=
4002 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004003 tp->serdes_counter =
4004 SERDES_PARALLEL_DET_TIMEOUT;
4005 } else
4006 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 }
4008 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07004009 } else {
4010 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004011 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 }
4013
4014out:
4015 return current_link_up;
4016}
4017
4018static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
4019{
4020 int current_link_up = 0;
4021
Michael Chan5cf64b8a2007-05-05 12:11:21 -07004022 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024
4025 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08004026 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004028
Matt Carlson5be73b42007-12-20 20:09:29 -08004029 if (fiber_autoneg(tp, &txflags, &rxflags)) {
4030 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031
Matt Carlson5be73b42007-12-20 20:09:29 -08004032 if (txflags & ANEG_CFG_PS1)
4033 local_adv |= ADVERTISE_1000XPAUSE;
4034 if (txflags & ANEG_CFG_PS2)
4035 local_adv |= ADVERTISE_1000XPSE_ASYM;
4036
4037 if (rxflags & MR_LP_ADV_SYM_PAUSE)
4038 remote_adv |= LPA_1000XPAUSE;
4039 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
4040 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041
4042 tg3_setup_flow_control(tp, local_adv, remote_adv);
4043
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 current_link_up = 1;
4045 }
4046 for (i = 0; i < 30; i++) {
4047 udelay(20);
4048 tw32_f(MAC_STATUS,
4049 (MAC_STATUS_SYNC_CHANGED |
4050 MAC_STATUS_CFG_CHANGED));
4051 udelay(40);
4052 if ((tr32(MAC_STATUS) &
4053 (MAC_STATUS_SYNC_CHANGED |
4054 MAC_STATUS_CFG_CHANGED)) == 0)
4055 break;
4056 }
4057
4058 mac_status = tr32(MAC_STATUS);
4059 if (current_link_up == 0 &&
4060 (mac_status & MAC_STATUS_PCS_SYNCED) &&
4061 !(mac_status & MAC_STATUS_RCVD_CFG))
4062 current_link_up = 1;
4063 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08004064 tg3_setup_flow_control(tp, 0, 0);
4065
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 /* Forcing 1000FD link up. */
4067 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068
4069 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
4070 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004071
4072 tw32_f(MAC_MODE, tp->mac_mode);
4073 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 }
4075
4076out:
4077 return current_link_up;
4078}
4079
4080static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
4081{
4082 u32 orig_pause_cfg;
4083 u16 orig_active_speed;
4084 u8 orig_active_duplex;
4085 u32 mac_status;
4086 int current_link_up;
4087 int i;
4088
Matt Carlson8d018622007-12-20 20:05:44 -08004089 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090 orig_active_speed = tp->link_config.active_speed;
4091 orig_active_duplex = tp->link_config.active_duplex;
4092
4093 if (!(tp->tg3_flags2 & TG3_FLG2_HW_AUTONEG) &&
4094 netif_carrier_ok(tp->dev) &&
4095 (tp->tg3_flags & TG3_FLAG_INIT_COMPLETE)) {
4096 mac_status = tr32(MAC_STATUS);
4097 mac_status &= (MAC_STATUS_PCS_SYNCED |
4098 MAC_STATUS_SIGNAL_DET |
4099 MAC_STATUS_CFG_CHANGED |
4100 MAC_STATUS_RCVD_CFG);
4101 if (mac_status == (MAC_STATUS_PCS_SYNCED |
4102 MAC_STATUS_SIGNAL_DET)) {
4103 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4104 MAC_STATUS_CFG_CHANGED));
4105 return 0;
4106 }
4107 }
4108
4109 tw32_f(MAC_TX_AUTO_NEG, 0);
4110
4111 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
4112 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
4113 tw32_f(MAC_MODE, tp->mac_mode);
4114 udelay(40);
4115
Matt Carlson79eb6902010-02-17 15:17:03 +00004116 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 tg3_init_bcm8002(tp);
4118
4119 /* Enable link change event even when serdes polling. */
4120 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4121 udelay(40);
4122
4123 current_link_up = 0;
4124 mac_status = tr32(MAC_STATUS);
4125
4126 if (tp->tg3_flags2 & TG3_FLG2_HW_AUTONEG)
4127 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
4128 else
4129 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
4130
Matt Carlson898a56f2009-08-28 14:02:40 +00004131 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00004133 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134
4135 for (i = 0; i < 100; i++) {
4136 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4137 MAC_STATUS_CFG_CHANGED));
4138 udelay(5);
4139 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07004140 MAC_STATUS_CFG_CHANGED |
4141 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 break;
4143 }
4144
4145 mac_status = tr32(MAC_STATUS);
4146 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
4147 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004148 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
4149 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 tw32_f(MAC_MODE, (tp->mac_mode |
4151 MAC_MODE_SEND_CONFIGS));
4152 udelay(1);
4153 tw32_f(MAC_MODE, tp->mac_mode);
4154 }
4155 }
4156
4157 if (current_link_up == 1) {
4158 tp->link_config.active_speed = SPEED_1000;
4159 tp->link_config.active_duplex = DUPLEX_FULL;
4160 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4161 LED_CTRL_LNKLED_OVERRIDE |
4162 LED_CTRL_1000MBPS_ON));
4163 } else {
4164 tp->link_config.active_speed = SPEED_INVALID;
4165 tp->link_config.active_duplex = DUPLEX_INVALID;
4166 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4167 LED_CTRL_LNKLED_OVERRIDE |
4168 LED_CTRL_TRAFFIC_OVERRIDE));
4169 }
4170
4171 if (current_link_up != netif_carrier_ok(tp->dev)) {
4172 if (current_link_up)
4173 netif_carrier_on(tp->dev);
4174 else
4175 netif_carrier_off(tp->dev);
4176 tg3_link_report(tp);
4177 } else {
Matt Carlson8d018622007-12-20 20:05:44 -08004178 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 if (orig_pause_cfg != now_pause_cfg ||
4180 orig_active_speed != tp->link_config.active_speed ||
4181 orig_active_duplex != tp->link_config.active_duplex)
4182 tg3_link_report(tp);
4183 }
4184
4185 return 0;
4186}
4187
Michael Chan747e8f82005-07-25 12:33:22 -07004188static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
4189{
4190 int current_link_up, err = 0;
4191 u32 bmsr, bmcr;
4192 u16 current_speed;
4193 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08004194 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07004195
4196 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4197 tw32_f(MAC_MODE, tp->mac_mode);
4198 udelay(40);
4199
4200 tw32(MAC_EVENT, 0);
4201
4202 tw32_f(MAC_STATUS,
4203 (MAC_STATUS_SYNC_CHANGED |
4204 MAC_STATUS_CFG_CHANGED |
4205 MAC_STATUS_MI_COMPLETION |
4206 MAC_STATUS_LNKSTATE_CHANGED));
4207 udelay(40);
4208
4209 if (force_reset)
4210 tg3_phy_reset(tp);
4211
4212 current_link_up = 0;
4213 current_speed = SPEED_INVALID;
4214 current_duplex = DUPLEX_INVALID;
4215
4216 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4217 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004218 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
4219 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4220 bmsr |= BMSR_LSTATUS;
4221 else
4222 bmsr &= ~BMSR_LSTATUS;
4223 }
Michael Chan747e8f82005-07-25 12:33:22 -07004224
4225 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
4226
4227 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004228 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004229 /* do nothing, just check for link up at the end */
4230 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4231 u32 adv, new_adv;
4232
4233 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4234 new_adv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
4235 ADVERTISE_1000XPAUSE |
4236 ADVERTISE_1000XPSE_ASYM |
4237 ADVERTISE_SLCT);
4238
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004239 new_adv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Michael Chan747e8f82005-07-25 12:33:22 -07004240
4241 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
4242 new_adv |= ADVERTISE_1000XHALF;
4243 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
4244 new_adv |= ADVERTISE_1000XFULL;
4245
4246 if ((new_adv != adv) || !(bmcr & BMCR_ANENABLE)) {
4247 tg3_writephy(tp, MII_ADVERTISE, new_adv);
4248 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
4249 tg3_writephy(tp, MII_BMCR, bmcr);
4250
4251 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07004252 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004253 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004254
4255 return err;
4256 }
4257 } else {
4258 u32 new_bmcr;
4259
4260 bmcr &= ~BMCR_SPEED1000;
4261 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
4262
4263 if (tp->link_config.duplex == DUPLEX_FULL)
4264 new_bmcr |= BMCR_FULLDPLX;
4265
4266 if (new_bmcr != bmcr) {
4267 /* BMCR_SPEED1000 is a reserved bit that needs
4268 * to be set on write.
4269 */
4270 new_bmcr |= BMCR_SPEED1000;
4271
4272 /* Force a linkdown */
4273 if (netif_carrier_ok(tp->dev)) {
4274 u32 adv;
4275
4276 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4277 adv &= ~(ADVERTISE_1000XFULL |
4278 ADVERTISE_1000XHALF |
4279 ADVERTISE_SLCT);
4280 tg3_writephy(tp, MII_ADVERTISE, adv);
4281 tg3_writephy(tp, MII_BMCR, bmcr |
4282 BMCR_ANRESTART |
4283 BMCR_ANENABLE);
4284 udelay(10);
4285 netif_carrier_off(tp->dev);
4286 }
4287 tg3_writephy(tp, MII_BMCR, new_bmcr);
4288 bmcr = new_bmcr;
4289 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4290 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004291 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
4292 ASIC_REV_5714) {
4293 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4294 bmsr |= BMSR_LSTATUS;
4295 else
4296 bmsr &= ~BMSR_LSTATUS;
4297 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004298 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004299 }
4300 }
4301
4302 if (bmsr & BMSR_LSTATUS) {
4303 current_speed = SPEED_1000;
4304 current_link_up = 1;
4305 if (bmcr & BMCR_FULLDPLX)
4306 current_duplex = DUPLEX_FULL;
4307 else
4308 current_duplex = DUPLEX_HALF;
4309
Matt Carlsonef167e22007-12-20 20:10:01 -08004310 local_adv = 0;
4311 remote_adv = 0;
4312
Michael Chan747e8f82005-07-25 12:33:22 -07004313 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08004314 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07004315
4316 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
4317 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
4318 common = local_adv & remote_adv;
4319 if (common & (ADVERTISE_1000XHALF |
4320 ADVERTISE_1000XFULL)) {
4321 if (common & ADVERTISE_1000XFULL)
4322 current_duplex = DUPLEX_FULL;
4323 else
4324 current_duplex = DUPLEX_HALF;
Matt Carlson57d8b882010-06-05 17:24:35 +00004325 } else if (!(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
4326 /* Link is up via parallel detect */
Matt Carlson859a588792010-04-05 10:19:28 +00004327 } else {
Michael Chan747e8f82005-07-25 12:33:22 -07004328 current_link_up = 0;
Matt Carlson859a588792010-04-05 10:19:28 +00004329 }
Michael Chan747e8f82005-07-25 12:33:22 -07004330 }
4331 }
4332
Matt Carlsonef167e22007-12-20 20:10:01 -08004333 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
4334 tg3_setup_flow_control(tp, local_adv, remote_adv);
4335
Michael Chan747e8f82005-07-25 12:33:22 -07004336 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4337 if (tp->link_config.active_duplex == DUPLEX_HALF)
4338 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4339
4340 tw32_f(MAC_MODE, tp->mac_mode);
4341 udelay(40);
4342
4343 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4344
4345 tp->link_config.active_speed = current_speed;
4346 tp->link_config.active_duplex = current_duplex;
4347
4348 if (current_link_up != netif_carrier_ok(tp->dev)) {
4349 if (current_link_up)
4350 netif_carrier_on(tp->dev);
4351 else {
4352 netif_carrier_off(tp->dev);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004353 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004354 }
4355 tg3_link_report(tp);
4356 }
4357 return err;
4358}
4359
4360static void tg3_serdes_parallel_detect(struct tg3 *tp)
4361{
Michael Chan3d3ebe72006-09-27 15:59:15 -07004362 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07004363 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07004364 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07004365 return;
4366 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00004367
Michael Chan747e8f82005-07-25 12:33:22 -07004368 if (!netif_carrier_ok(tp->dev) &&
4369 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
4370 u32 bmcr;
4371
4372 tg3_readphy(tp, MII_BMCR, &bmcr);
4373 if (bmcr & BMCR_ANENABLE) {
4374 u32 phy1, phy2;
4375
4376 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004377 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
4378 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07004379
4380 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004381 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
4382 MII_TG3_DSP_EXP1_INT_STAT);
4383 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
4384 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07004385
4386 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
4387 /* We have signal detect and not receiving
4388 * config code words, link is up by parallel
4389 * detection.
4390 */
4391
4392 bmcr &= ~BMCR_ANENABLE;
4393 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
4394 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004395 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004396 }
4397 }
Matt Carlson859a588792010-04-05 10:19:28 +00004398 } else if (netif_carrier_ok(tp->dev) &&
4399 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004400 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004401 u32 phy2;
4402
4403 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004404 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
4405 MII_TG3_DSP_EXP1_INT_STAT);
4406 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07004407 if (phy2 & 0x20) {
4408 u32 bmcr;
4409
4410 /* Config code words received, turn on autoneg. */
4411 tg3_readphy(tp, MII_BMCR, &bmcr);
4412 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
4413
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004414 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004415
4416 }
4417 }
4418}
4419
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420static int tg3_setup_phy(struct tg3 *tp, int force_reset)
4421{
Matt Carlsonf2096f92011-04-05 14:22:48 +00004422 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 int err;
4424
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004425 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004427 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07004428 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a588792010-04-05 10:19:28 +00004429 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431
Matt Carlsonbcb37f62008-11-03 16:52:09 -08004432 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00004433 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08004434
4435 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
4436 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
4437 scale = 65;
4438 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
4439 scale = 6;
4440 else
4441 scale = 12;
4442
4443 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
4444 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
4445 tw32(GRC_MISC_CFG, val);
4446 }
4447
Matt Carlsonf2096f92011-04-05 14:22:48 +00004448 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4449 (6 << TX_LENGTHS_IPG_SHIFT);
4450 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
4451 val |= tr32(MAC_TX_LENGTHS) &
4452 (TX_LENGTHS_JMB_FRM_LEN_MSK |
4453 TX_LENGTHS_CNT_DWN_VAL_MSK);
4454
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 if (tp->link_config.active_speed == SPEED_1000 &&
4456 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00004457 tw32(MAC_TX_LENGTHS, val |
4458 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00004460 tw32(MAC_TX_LENGTHS, val |
4461 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462
4463 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
4464 if (netif_carrier_ok(tp->dev)) {
4465 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07004466 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467 } else {
4468 tw32(HOSTCC_STAT_COAL_TICKS, 0);
4469 }
4470 }
4471
Matt Carlson8ed5d972007-05-07 00:25:49 -07004472 if (tp->tg3_flags & TG3_FLAG_ASPM_WORKAROUND) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00004473 val = tr32(PCIE_PWR_MGMT_THRESH);
Matt Carlson8ed5d972007-05-07 00:25:49 -07004474 if (!netif_carrier_ok(tp->dev))
4475 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
4476 tp->pwrmgmt_thresh;
4477 else
4478 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
4479 tw32(PCIE_PWR_MGMT_THRESH, val);
4480 }
4481
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 return err;
4483}
4484
Matt Carlson66cfd1b2010-09-30 10:34:30 +00004485static inline int tg3_irq_sync(struct tg3 *tp)
4486{
4487 return tp->irq_sync;
4488}
4489
Matt Carlson97bd8e42011-04-13 11:05:04 +00004490static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
4491{
4492 int i;
4493
4494 dst = (u32 *)((u8 *)dst + off);
4495 for (i = 0; i < len; i += sizeof(u32))
4496 *dst++ = tr32(off + i);
4497}
4498
4499static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
4500{
4501 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
4502 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
4503 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
4504 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
4505 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
4506 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
4507 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
4508 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
4509 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
4510 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
4511 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
4512 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
4513 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
4514 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
4515 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
4516 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
4517 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
4518 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
4519 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
4520
4521 if (tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX)
4522 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
4523
4524 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
4525 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
4526 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
4527 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
4528 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
4529 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
4530 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
4531 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
4532
4533 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
4534 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
4535 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
4536 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
4537 }
4538
4539 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
4540 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
4541 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
4542 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
4543 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
4544
4545 if (tp->tg3_flags & TG3_FLAG_NVRAM)
4546 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
4547}
4548
4549static void tg3_dump_state(struct tg3 *tp)
4550{
4551 int i;
4552 u32 *regs;
4553
4554 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
4555 if (!regs) {
4556 netdev_err(tp->dev, "Failed allocating register dump buffer\n");
4557 return;
4558 }
4559
4560 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
4561 /* Read up to but not including private PCI registers */
4562 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
4563 regs[i / sizeof(u32)] = tr32(i);
4564 } else
4565 tg3_dump_legacy_regs(tp, regs);
4566
4567 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
4568 if (!regs[i + 0] && !regs[i + 1] &&
4569 !regs[i + 2] && !regs[i + 3])
4570 continue;
4571
4572 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
4573 i * 4,
4574 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
4575 }
4576
4577 kfree(regs);
4578
4579 for (i = 0; i < tp->irq_cnt; i++) {
4580 struct tg3_napi *tnapi = &tp->napi[i];
4581
4582 /* SW status block */
4583 netdev_err(tp->dev,
4584 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
4585 i,
4586 tnapi->hw_status->status,
4587 tnapi->hw_status->status_tag,
4588 tnapi->hw_status->rx_jumbo_consumer,
4589 tnapi->hw_status->rx_consumer,
4590 tnapi->hw_status->rx_mini_consumer,
4591 tnapi->hw_status->idx[0].rx_producer,
4592 tnapi->hw_status->idx[0].tx_consumer);
4593
4594 netdev_err(tp->dev,
4595 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
4596 i,
4597 tnapi->last_tag, tnapi->last_irq_tag,
4598 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
4599 tnapi->rx_rcb_ptr,
4600 tnapi->prodring.rx_std_prod_idx,
4601 tnapi->prodring.rx_std_cons_idx,
4602 tnapi->prodring.rx_jmb_prod_idx,
4603 tnapi->prodring.rx_jmb_cons_idx);
4604 }
4605}
4606
Michael Chandf3e6542006-05-26 17:48:07 -07004607/* This is called whenever we suspect that the system chipset is re-
4608 * ordering the sequence of MMIO to the tx send mailbox. The symptom
4609 * is bogus tx completions. We try to recover by setting the
4610 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
4611 * in the workqueue.
4612 */
4613static void tg3_tx_recover(struct tg3 *tp)
4614{
4615 BUG_ON((tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER) ||
4616 tp->write32_tx_mbox == tg3_write_indirect_mbox);
4617
Matt Carlson5129c3a2010-04-05 10:19:23 +00004618 netdev_warn(tp->dev,
4619 "The system may be re-ordering memory-mapped I/O "
4620 "cycles to the network device, attempting to recover. "
4621 "Please report the problem to the driver maintainer "
4622 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07004623
4624 spin_lock(&tp->lock);
Michael Chandf3e6542006-05-26 17:48:07 -07004625 tp->tg3_flags |= TG3_FLAG_TX_RECOVERY_PENDING;
Michael Chandf3e6542006-05-26 17:48:07 -07004626 spin_unlock(&tp->lock);
4627}
4628
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004629static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07004630{
Matt Carlsonf65aac12010-08-02 11:26:03 +00004631 /* Tell compiler to fetch tx indices from memory. */
4632 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004633 return tnapi->tx_pending -
4634 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07004635}
4636
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637/* Tigon3 never reports partial packet sends. So we do not
4638 * need special logic to handle SKBs that have not had all
4639 * of their frags sent yet, like SunGEM does.
4640 */
Matt Carlson17375d22009-08-28 14:02:18 +00004641static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642{
Matt Carlson17375d22009-08-28 14:02:18 +00004643 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00004644 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004645 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004646 struct netdev_queue *txq;
4647 int index = tnapi - tp->napi;
4648
Matt Carlson19cfaec2009-12-03 08:36:20 +00004649 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004650 index--;
4651
4652 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653
4654 while (sw_idx != hw_idx) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00004655 struct ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07004657 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658
Michael Chandf3e6542006-05-26 17:48:07 -07004659 if (unlikely(skb == NULL)) {
4660 tg3_tx_recover(tp);
4661 return;
4662 }
4663
Alexander Duyckf4188d82009-12-02 16:48:38 +00004664 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004665 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00004666 skb_headlen(skb),
4667 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668
4669 ri->skb = NULL;
4670
4671 sw_idx = NEXT_TX(sw_idx);
4672
4673 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004674 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07004675 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
4676 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00004677
4678 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004679 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00004680 skb_shinfo(skb)->frags[i].size,
4681 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 sw_idx = NEXT_TX(sw_idx);
4683 }
4684
David S. Millerf47c11e2005-06-24 20:18:35 -07004685 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07004686
4687 if (unlikely(tx_bug)) {
4688 tg3_tx_recover(tp);
4689 return;
4690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 }
4692
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004693 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694
Michael Chan1b2a7202006-08-07 21:46:02 -07004695 /* Need to make the tx_cons update visible to tg3_start_xmit()
4696 * before checking for netif_queue_stopped(). Without the
4697 * memory barrier, there is a small possibility that tg3_start_xmit()
4698 * will miss it and cause the queue to be stopped forever.
4699 */
4700 smp_mb();
4701
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004702 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004703 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004704 __netif_tx_lock(txq, smp_processor_id());
4705 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004706 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004707 netif_tx_wake_queue(txq);
4708 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07004709 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710}
4711
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004712static void tg3_rx_skb_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
4713{
4714 if (!ri->skb)
4715 return;
4716
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004717 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004718 map_sz, PCI_DMA_FROMDEVICE);
4719 dev_kfree_skb_any(ri->skb);
4720 ri->skb = NULL;
4721}
4722
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723/* Returns size of skb allocated or < 0 on error.
4724 *
4725 * We only need to fill in the address because the other members
4726 * of the RX descriptor are invariant, see tg3_init_rings.
4727 *
4728 * Note the purposeful assymetry of cpu vs. chip accesses. For
4729 * posting buffers we only dirty the first cache line of the RX
4730 * descriptor (containing the address). Whereas for the RX status
4731 * buffers the cpu only reads the last cacheline of the RX descriptor
4732 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
4733 */
Matt Carlson86b21e52009-11-13 13:03:45 +00004734static int tg3_alloc_rx_skb(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Matt Carlsona3896162009-11-13 13:03:44 +00004735 u32 opaque_key, u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736{
4737 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00004738 struct ring_info *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739 struct sk_buff *skb;
4740 dma_addr_t mapping;
4741 int skb_size, dest_idx;
4742
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743 switch (opaque_key) {
4744 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00004745 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00004746 desc = &tpr->rx_std[dest_idx];
4747 map = &tpr->rx_std_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004748 skb_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749 break;
4750
4751 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00004752 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00004753 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00004754 map = &tpr->rx_jmb_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004755 skb_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756 break;
4757
4758 default:
4759 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761
4762 /* Do not overwrite any of the map or rp information
4763 * until we are sure we can commit to a new buffer.
4764 *
4765 * Callers depend upon this behavior and assume that
4766 * we leave everything unchanged if we fail.
4767 */
Matt Carlson287be122009-08-28 13:58:46 +00004768 skb = netdev_alloc_skb(tp->dev, skb_size + tp->rx_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769 if (skb == NULL)
4770 return -ENOMEM;
4771
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 skb_reserve(skb, tp->rx_offset);
4773
Matt Carlson287be122009-08-28 13:58:46 +00004774 mapping = pci_map_single(tp->pdev, skb->data, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 PCI_DMA_FROMDEVICE);
Matt Carlsona21771d2009-11-02 14:25:31 +00004776 if (pci_dma_mapping_error(tp->pdev, mapping)) {
4777 dev_kfree_skb(skb);
4778 return -EIO;
4779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780
4781 map->skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004782 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 desc->addr_hi = ((u64)mapping >> 32);
4785 desc->addr_lo = ((u64)mapping & 0xffffffff);
4786
4787 return skb_size;
4788}
4789
4790/* We only need to move over in the address because the other
4791 * members of the RX descriptor are invariant. See notes above
4792 * tg3_alloc_rx_skb for full details.
4793 */
Matt Carlsona3896162009-11-13 13:03:44 +00004794static void tg3_recycle_rx(struct tg3_napi *tnapi,
4795 struct tg3_rx_prodring_set *dpr,
4796 u32 opaque_key, int src_idx,
4797 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798{
Matt Carlson17375d22009-08-28 14:02:18 +00004799 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
4801 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00004802 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00004803 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804
4805 switch (opaque_key) {
4806 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00004807 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00004808 dest_desc = &dpr->rx_std[dest_idx];
4809 dest_map = &dpr->rx_std_buffers[dest_idx];
4810 src_desc = &spr->rx_std[src_idx];
4811 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812 break;
4813
4814 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00004815 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00004816 dest_desc = &dpr->rx_jmb[dest_idx].std;
4817 dest_map = &dpr->rx_jmb_buffers[dest_idx];
4818 src_desc = &spr->rx_jmb[src_idx].std;
4819 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820 break;
4821
4822 default:
4823 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004824 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825
4826 dest_map->skb = src_map->skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004827 dma_unmap_addr_set(dest_map, mapping,
4828 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 dest_desc->addr_hi = src_desc->addr_hi;
4830 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00004831
4832 /* Ensure that the update to the skb happens after the physical
4833 * addresses have been transferred to the new BD location.
4834 */
4835 smp_wmb();
4836
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837 src_map->skb = NULL;
4838}
4839
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840/* The RX ring scheme is composed of multiple rings which post fresh
4841 * buffers to the chip, and one special ring the chip uses to report
4842 * status back to the host.
4843 *
4844 * The special ring reports the status of received packets to the
4845 * host. The chip does not write into the original descriptor the
4846 * RX buffer was obtained from. The chip simply takes the original
4847 * descriptor as provided by the host, updates the status and length
4848 * field, then writes this into the next status ring entry.
4849 *
4850 * Each ring the host uses to post buffers to the chip is described
4851 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
4852 * it is first placed into the on-chip ram. When the packet's length
4853 * is known, it walks down the TG3_BDINFO entries to select the ring.
4854 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
4855 * which is within the range of the new packet's length is chosen.
4856 *
4857 * The "separate ring for rx status" scheme may sound queer, but it makes
4858 * sense from a cache coherency perspective. If only the host writes
4859 * to the buffer post rings, and only the chip writes to the rx status
4860 * rings, then cache lines never move beyond shared-modified state.
4861 * If both the host and chip were to write into the same ring, cache line
4862 * eviction could occur since both entities want it in an exclusive state.
4863 */
Matt Carlson17375d22009-08-28 14:02:18 +00004864static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865{
Matt Carlson17375d22009-08-28 14:02:18 +00004866 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07004867 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00004868 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00004869 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07004870 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00004872 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004874 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875 /*
4876 * We need to order the read of hw_idx and the read of
4877 * the opaque cookie.
4878 */
4879 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880 work_mask = 0;
4881 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00004882 std_prod_idx = tpr->rx_std_prod_idx;
4883 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00004885 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00004886 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887 unsigned int len;
4888 struct sk_buff *skb;
4889 dma_addr_t dma_addr;
4890 u32 opaque_key, desc_idx, *post_ptr;
4891
4892 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
4893 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
4894 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00004895 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004896 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00004897 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00004898 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07004899 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00004901 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004902 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00004903 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00004904 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00004905 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907
4908 work_mask |= opaque_key;
4909
4910 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
4911 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
4912 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00004913 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 desc_idx, *post_ptr);
4915 drop_it_no_recycle:
4916 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00004917 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918 goto next_pkt;
4919 }
4920
Matt Carlsonad829262008-11-21 17:16:16 -08004921 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
4922 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923
Matt Carlsond2757fc2010-04-12 06:58:27 +00004924 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 int skb_size;
4926
Matt Carlson86b21e52009-11-13 13:03:45 +00004927 skb_size = tg3_alloc_rx_skb(tp, tpr, opaque_key,
Matt Carlsonafc081f2009-11-13 13:03:43 +00004928 *post_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929 if (skb_size < 0)
4930 goto drop_it;
4931
Matt Carlson287be122009-08-28 13:58:46 +00004932 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933 PCI_DMA_FROMDEVICE);
4934
Matt Carlson61e800c2010-02-17 15:16:54 +00004935 /* Ensure that the update to the skb happens
4936 * after the usage of the old DMA mapping.
4937 */
4938 smp_wmb();
4939
4940 ri->skb = NULL;
4941
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 skb_put(skb, len);
4943 } else {
4944 struct sk_buff *copy_skb;
4945
Matt Carlsona3896162009-11-13 13:03:44 +00004946 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947 desc_idx, *post_ptr);
4948
Matt Carlsonbf933c82011-01-25 15:58:49 +00004949 copy_skb = netdev_alloc_skb(tp->dev, len +
Matt Carlson9dc7a112010-04-12 06:58:28 +00004950 TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951 if (copy_skb == NULL)
4952 goto drop_it_no_recycle;
4953
Matt Carlsonbf933c82011-01-25 15:58:49 +00004954 skb_reserve(copy_skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955 skb_put(copy_skb, len);
4956 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03004957 skb_copy_from_linear_data(skb, copy_skb->data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
4959
4960 /* We'll reuse the original ring buffer. */
4961 skb = copy_skb;
4962 }
4963
Michał Mirosławdc668912011-04-07 03:35:07 +00004964 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
4966 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
4967 >> RXD_TCPCSUM_SHIFT) == 0xffff))
4968 skb->ip_summed = CHECKSUM_UNNECESSARY;
4969 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07004970 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971
4972 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00004973
4974 if (len > (tp->dev->mtu + ETH_HLEN) &&
4975 skb->protocol != htons(ETH_P_8021Q)) {
4976 dev_kfree_skb(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00004977 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00004978 }
4979
Matt Carlson9dc7a112010-04-12 06:58:28 +00004980 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00004981 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
4982 __vlan_hwaccel_put_tag(skb,
4983 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00004984
Matt Carlsonbf933c82011-01-25 15:58:49 +00004985 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987 received++;
4988 budget--;
4989
4990next_pkt:
4991 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07004992
4993 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00004994 tpr->rx_std_prod_idx = std_prod_idx &
4995 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00004996 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
4997 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07004998 work_mask &= ~RXD_OPAQUE_RING_STD;
4999 rx_std_posted = 0;
5000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07005002 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00005003 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07005004
5005 /* Refresh hw_idx to see if there is new work */
5006 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005007 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07005008 rmb();
5009 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010 }
5011
5012 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00005013 tnapi->rx_rcb_ptr = sw_idx;
5014 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015
5016 /* Refill RX ring(s). */
Matt Carlsone4af1af2010-02-12 14:47:05 +00005017 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005018 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005019 tpr->rx_std_prod_idx = std_prod_idx &
5020 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005021 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5022 tpr->rx_std_prod_idx);
5023 }
5024 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005025 tpr->rx_jmb_prod_idx = jmb_prod_idx &
5026 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005027 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5028 tpr->rx_jmb_prod_idx);
5029 }
5030 mmiowb();
5031 } else if (work_mask) {
5032 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
5033 * updated before the producer indices can be updated.
5034 */
5035 smp_wmb();
5036
Matt Carlson2c49a442010-09-30 10:34:35 +00005037 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
5038 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005039
Matt Carlsone4af1af2010-02-12 14:47:05 +00005040 if (tnapi != &tp->napi[1])
5041 napi_schedule(&tp->napi[1].napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043
5044 return received;
5045}
5046
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005047static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049 /* handle link change and other phy events */
5050 if (!(tp->tg3_flags &
5051 (TG3_FLAG_USE_LINKCHG_REG |
5052 TG3_FLAG_POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005053 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
5054
Linus Torvalds1da177e2005-04-16 15:20:36 -07005055 if (sblk->status & SD_STATUS_LINK_CHG) {
5056 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005057 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07005058 spin_lock(&tp->lock);
Matt Carlsondd477002008-05-25 23:45:58 -07005059 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
5060 tw32_f(MAC_STATUS,
5061 (MAC_STATUS_SYNC_CHANGED |
5062 MAC_STATUS_CFG_CHANGED |
5063 MAC_STATUS_MI_COMPLETION |
5064 MAC_STATUS_LNKSTATE_CHANGED));
5065 udelay(40);
5066 } else
5067 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07005068 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 }
5070 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005071}
5072
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005073static int tg3_rx_prodring_xfer(struct tg3 *tp,
5074 struct tg3_rx_prodring_set *dpr,
5075 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005076{
5077 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005078 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005079
5080 while (1) {
5081 src_prod_idx = spr->rx_std_prod_idx;
5082
5083 /* Make sure updates to the rx_std_buffers[] entries and the
5084 * standard producer index are seen in the correct order.
5085 */
5086 smp_rmb();
5087
5088 if (spr->rx_std_cons_idx == src_prod_idx)
5089 break;
5090
5091 if (spr->rx_std_cons_idx < src_prod_idx)
5092 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
5093 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005094 cpycnt = tp->rx_std_ring_mask + 1 -
5095 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005096
Matt Carlson2c49a442010-09-30 10:34:35 +00005097 cpycnt = min(cpycnt,
5098 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005099
5100 si = spr->rx_std_cons_idx;
5101 di = dpr->rx_std_prod_idx;
5102
Matt Carlsone92967b2010-02-12 14:47:06 +00005103 for (i = di; i < di + cpycnt; i++) {
5104 if (dpr->rx_std_buffers[i].skb) {
5105 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005106 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005107 break;
5108 }
5109 }
5110
5111 if (!cpycnt)
5112 break;
5113
5114 /* Ensure that updates to the rx_std_buffers ring and the
5115 * shadowed hardware producer ring from tg3_recycle_skb() are
5116 * ordered correctly WRT the skb check above.
5117 */
5118 smp_rmb();
5119
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005120 memcpy(&dpr->rx_std_buffers[di],
5121 &spr->rx_std_buffers[si],
5122 cpycnt * sizeof(struct ring_info));
5123
5124 for (i = 0; i < cpycnt; i++, di++, si++) {
5125 struct tg3_rx_buffer_desc *sbd, *dbd;
5126 sbd = &spr->rx_std[si];
5127 dbd = &dpr->rx_std[di];
5128 dbd->addr_hi = sbd->addr_hi;
5129 dbd->addr_lo = sbd->addr_lo;
5130 }
5131
Matt Carlson2c49a442010-09-30 10:34:35 +00005132 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
5133 tp->rx_std_ring_mask;
5134 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
5135 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005136 }
5137
5138 while (1) {
5139 src_prod_idx = spr->rx_jmb_prod_idx;
5140
5141 /* Make sure updates to the rx_jmb_buffers[] entries and
5142 * the jumbo producer index are seen in the correct order.
5143 */
5144 smp_rmb();
5145
5146 if (spr->rx_jmb_cons_idx == src_prod_idx)
5147 break;
5148
5149 if (spr->rx_jmb_cons_idx < src_prod_idx)
5150 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
5151 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005152 cpycnt = tp->rx_jmb_ring_mask + 1 -
5153 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005154
5155 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00005156 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005157
5158 si = spr->rx_jmb_cons_idx;
5159 di = dpr->rx_jmb_prod_idx;
5160
Matt Carlsone92967b2010-02-12 14:47:06 +00005161 for (i = di; i < di + cpycnt; i++) {
5162 if (dpr->rx_jmb_buffers[i].skb) {
5163 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005164 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005165 break;
5166 }
5167 }
5168
5169 if (!cpycnt)
5170 break;
5171
5172 /* Ensure that updates to the rx_jmb_buffers ring and the
5173 * shadowed hardware producer ring from tg3_recycle_skb() are
5174 * ordered correctly WRT the skb check above.
5175 */
5176 smp_rmb();
5177
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005178 memcpy(&dpr->rx_jmb_buffers[di],
5179 &spr->rx_jmb_buffers[si],
5180 cpycnt * sizeof(struct ring_info));
5181
5182 for (i = 0; i < cpycnt; i++, di++, si++) {
5183 struct tg3_rx_buffer_desc *sbd, *dbd;
5184 sbd = &spr->rx_jmb[si].std;
5185 dbd = &dpr->rx_jmb[di].std;
5186 dbd->addr_hi = sbd->addr_hi;
5187 dbd->addr_lo = sbd->addr_lo;
5188 }
5189
Matt Carlson2c49a442010-09-30 10:34:35 +00005190 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
5191 tp->rx_jmb_ring_mask;
5192 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
5193 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005194 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005195
5196 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005197}
5198
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005199static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
5200{
5201 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202
5203 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005204 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00005205 tg3_tx(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07005206 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
Michael Chan4fd7ab52007-10-12 01:39:50 -07005207 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208 }
5209
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210 /* run RX thread, within the bounds set by NAPI.
5211 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005212 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005214 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00005215 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005217 if ((tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005218 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005219 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00005220 u32 std_prod_idx = dpr->rx_std_prod_idx;
5221 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005222
Matt Carlsone4af1af2010-02-12 14:47:05 +00005223 for (i = 1; i < tp->irq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005224 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00005225 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005226
5227 wmb();
5228
Matt Carlsone4af1af2010-02-12 14:47:05 +00005229 if (std_prod_idx != dpr->rx_std_prod_idx)
5230 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5231 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005232
Matt Carlsone4af1af2010-02-12 14:47:05 +00005233 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
5234 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5235 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005236
5237 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005238
5239 if (err)
5240 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005241 }
5242
David S. Miller6f535762007-10-11 18:08:29 -07005243 return work_done;
5244}
David S. Millerf7383c22005-05-18 22:50:53 -07005245
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005246static int tg3_poll_msix(struct napi_struct *napi, int budget)
5247{
5248 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5249 struct tg3 *tp = tnapi->tp;
5250 int work_done = 0;
5251 struct tg3_hw_status *sblk = tnapi->hw_status;
5252
5253 while (1) {
5254 work_done = tg3_poll_work(tnapi, work_done, budget);
5255
5256 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
5257 goto tx_recovery;
5258
5259 if (unlikely(work_done >= budget))
5260 break;
5261
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005262 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005263 * to tell the hw how much work has been processed,
5264 * so we must read it before checking for more work.
5265 */
5266 tnapi->last_tag = sblk->status_tag;
5267 tnapi->last_irq_tag = tnapi->last_tag;
5268 rmb();
5269
5270 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00005271 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
5272 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005273 napi_complete(napi);
5274 /* Reenable interrupts. */
5275 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
5276 mmiowb();
5277 break;
5278 }
5279 }
5280
5281 return work_done;
5282
5283tx_recovery:
5284 /* work_done is guaranteed to be less than budget. */
5285 napi_complete(napi);
5286 schedule_work(&tp->reset_task);
5287 return work_done;
5288}
5289
Matt Carlsone64de4e2011-04-13 11:05:05 +00005290static void tg3_process_error(struct tg3 *tp)
5291{
5292 u32 val;
5293 bool real_error = false;
5294
5295 if (tp->tg3_flags & TG3_FLAG_ERROR_PROCESSED)
5296 return;
5297
5298 /* Check Flow Attention register */
5299 val = tr32(HOSTCC_FLOW_ATTN);
5300 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
5301 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
5302 real_error = true;
5303 }
5304
5305 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
5306 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
5307 real_error = true;
5308 }
5309
5310 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
5311 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
5312 real_error = true;
5313 }
5314
5315 if (!real_error)
5316 return;
5317
5318 tg3_dump_state(tp);
5319
5320 tp->tg3_flags |= TG3_FLAG_ERROR_PROCESSED;
5321 schedule_work(&tp->reset_task);
5322}
5323
David S. Miller6f535762007-10-11 18:08:29 -07005324static int tg3_poll(struct napi_struct *napi, int budget)
5325{
Matt Carlson8ef04422009-08-28 14:01:37 +00005326 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5327 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07005328 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00005329 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07005330
5331 while (1) {
Matt Carlsone64de4e2011-04-13 11:05:05 +00005332 if (sblk->status & SD_STATUS_ERROR)
5333 tg3_process_error(tp);
5334
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005335 tg3_poll_link(tp);
5336
Matt Carlson17375d22009-08-28 14:02:18 +00005337 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07005338
5339 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
5340 goto tx_recovery;
5341
5342 if (unlikely(work_done >= budget))
5343 break;
5344
Michael Chan4fd7ab52007-10-12 01:39:50 -07005345 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) {
Matt Carlson17375d22009-08-28 14:02:18 +00005346 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07005347 * to tell the hw how much work has been processed,
5348 * so we must read it before checking for more work.
5349 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005350 tnapi->last_tag = sblk->status_tag;
5351 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07005352 rmb();
5353 } else
5354 sblk->status &= ~SD_STATUS_UPDATED;
5355
Matt Carlson17375d22009-08-28 14:02:18 +00005356 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005357 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00005358 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07005359 break;
5360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361 }
5362
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005363 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07005364
5365tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07005366 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08005367 napi_complete(napi);
David S. Miller6f535762007-10-11 18:08:29 -07005368 schedule_work(&tp->reset_task);
Michael Chan4fd7ab52007-10-12 01:39:50 -07005369 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370}
5371
Matt Carlson66cfd1b2010-09-30 10:34:30 +00005372static void tg3_napi_disable(struct tg3 *tp)
5373{
5374 int i;
5375
5376 for (i = tp->irq_cnt - 1; i >= 0; i--)
5377 napi_disable(&tp->napi[i].napi);
5378}
5379
5380static void tg3_napi_enable(struct tg3 *tp)
5381{
5382 int i;
5383
5384 for (i = 0; i < tp->irq_cnt; i++)
5385 napi_enable(&tp->napi[i].napi);
5386}
5387
5388static void tg3_napi_init(struct tg3 *tp)
5389{
5390 int i;
5391
5392 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
5393 for (i = 1; i < tp->irq_cnt; i++)
5394 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
5395}
5396
5397static void tg3_napi_fini(struct tg3 *tp)
5398{
5399 int i;
5400
5401 for (i = 0; i < tp->irq_cnt; i++)
5402 netif_napi_del(&tp->napi[i].napi);
5403}
5404
5405static inline void tg3_netif_stop(struct tg3 *tp)
5406{
5407 tp->dev->trans_start = jiffies; /* prevent tx timeout */
5408 tg3_napi_disable(tp);
5409 netif_tx_disable(tp->dev);
5410}
5411
5412static inline void tg3_netif_start(struct tg3 *tp)
5413{
5414 /* NOTE: unconditional netif_tx_wake_all_queues is only
5415 * appropriate so long as all callers are assured to
5416 * have free tx slots (such as after tg3_init_hw)
5417 */
5418 netif_tx_wake_all_queues(tp->dev);
5419
5420 tg3_napi_enable(tp);
5421 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
5422 tg3_enable_ints(tp);
5423}
5424
David S. Millerf47c11e2005-06-24 20:18:35 -07005425static void tg3_irq_quiesce(struct tg3 *tp)
5426{
Matt Carlson4f125f42009-09-01 12:55:02 +00005427 int i;
5428
David S. Millerf47c11e2005-06-24 20:18:35 -07005429 BUG_ON(tp->irq_sync);
5430
5431 tp->irq_sync = 1;
5432 smp_mb();
5433
Matt Carlson4f125f42009-09-01 12:55:02 +00005434 for (i = 0; i < tp->irq_cnt; i++)
5435 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07005436}
5437
David S. Millerf47c11e2005-06-24 20:18:35 -07005438/* Fully shutdown all tg3 driver activity elsewhere in the system.
5439 * If irq_sync is non-zero, then the IRQ handler must be synchronized
5440 * with as well. Most of the time, this is not necessary except when
5441 * shutting down the device.
5442 */
5443static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
5444{
Michael Chan46966542007-07-11 19:47:19 -07005445 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07005446 if (irq_sync)
5447 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07005448}
5449
5450static inline void tg3_full_unlock(struct tg3 *tp)
5451{
David S. Millerf47c11e2005-06-24 20:18:35 -07005452 spin_unlock_bh(&tp->lock);
5453}
5454
Michael Chanfcfa0a32006-03-20 22:28:41 -08005455/* One-shot MSI handler - Chip automatically disables interrupt
5456 * after sending MSI so driver doesn't have to do it.
5457 */
David Howells7d12e782006-10-05 14:55:46 +01005458static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08005459{
Matt Carlson09943a12009-08-28 14:01:57 +00005460 struct tg3_napi *tnapi = dev_id;
5461 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08005462
Matt Carlson898a56f2009-08-28 14:02:40 +00005463 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005464 if (tnapi->rx_rcb)
5465 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005466
5467 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005468 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005469
5470 return IRQ_HANDLED;
5471}
5472
Michael Chan88b06bc22005-04-21 17:13:25 -07005473/* MSI ISR - No need to check for interrupt sharing and no need to
5474 * flush status block and interrupt mailbox. PCI ordering rules
5475 * guarantee that MSI will arrive after the status block.
5476 */
David Howells7d12e782006-10-05 14:55:46 +01005477static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07005478{
Matt Carlson09943a12009-08-28 14:01:57 +00005479 struct tg3_napi *tnapi = dev_id;
5480 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07005481
Matt Carlson898a56f2009-08-28 14:02:40 +00005482 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005483 if (tnapi->rx_rcb)
5484 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07005485 /*
David S. Millerfac9b832005-05-18 22:46:34 -07005486 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07005487 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07005488 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07005489 * NIC to stop sending us irqs, engaging "in-intr-handler"
5490 * event coalescing.
5491 */
5492 tw32_mailbox(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07005493 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005494 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07005495
Michael Chan88b06bc22005-04-21 17:13:25 -07005496 return IRQ_RETVAL(1);
5497}
5498
David Howells7d12e782006-10-05 14:55:46 +01005499static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500{
Matt Carlson09943a12009-08-28 14:01:57 +00005501 struct tg3_napi *tnapi = dev_id;
5502 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005503 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504 unsigned int handled = 1;
5505
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506 /* In INTx mode, it is possible for the interrupt to arrive at
5507 * the CPU before the status block posted prior to the interrupt.
5508 * Reading the PCI State register will confirm whether the
5509 * interrupt is ours and will flush the status block.
5510 */
Michael Chand18edcb2007-03-24 20:57:11 -07005511 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
5512 if ((tp->tg3_flags & TG3_FLAG_CHIP_RESETTING) ||
5513 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5514 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005515 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07005516 }
Michael Chand18edcb2007-03-24 20:57:11 -07005517 }
5518
5519 /*
5520 * Writing any value to intr-mbox-0 clears PCI INTA# and
5521 * chip-internal interrupt pending events.
5522 * Writing non-zero to intr-mbox-0 additional tells the
5523 * NIC to stop sending us irqs, engaging "in-intr-handler"
5524 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005525 *
5526 * Flush the mailbox to de-assert the IRQ immediately to prevent
5527 * spurious interrupts. The flush impacts performance but
5528 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005529 */
Michael Chanc04cb342007-05-07 00:26:15 -07005530 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07005531 if (tg3_irq_sync(tp))
5532 goto out;
5533 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00005534 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00005535 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00005536 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07005537 } else {
5538 /* No work, shared interrupt perhaps? re-enable
5539 * interrupts, and flush that PCI write
5540 */
5541 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
5542 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07005543 }
David S. Millerf47c11e2005-06-24 20:18:35 -07005544out:
David S. Millerfac9b832005-05-18 22:46:34 -07005545 return IRQ_RETVAL(handled);
5546}
5547
David Howells7d12e782006-10-05 14:55:46 +01005548static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07005549{
Matt Carlson09943a12009-08-28 14:01:57 +00005550 struct tg3_napi *tnapi = dev_id;
5551 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005552 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07005553 unsigned int handled = 1;
5554
David S. Millerfac9b832005-05-18 22:46:34 -07005555 /* In INTx mode, it is possible for the interrupt to arrive at
5556 * the CPU before the status block posted prior to the interrupt.
5557 * Reading the PCI State register will confirm whether the
5558 * interrupt is ours and will flush the status block.
5559 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005560 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Michael Chand18edcb2007-03-24 20:57:11 -07005561 if ((tp->tg3_flags & TG3_FLAG_CHIP_RESETTING) ||
5562 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5563 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005564 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565 }
Michael Chand18edcb2007-03-24 20:57:11 -07005566 }
5567
5568 /*
5569 * writing any value to intr-mbox-0 clears PCI INTA# and
5570 * chip-internal interrupt pending events.
5571 * writing non-zero to intr-mbox-0 additional tells the
5572 * NIC to stop sending us irqs, engaging "in-intr-handler"
5573 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005574 *
5575 * Flush the mailbox to de-assert the IRQ immediately to prevent
5576 * spurious interrupts. The flush impacts performance but
5577 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005578 */
Michael Chanc04cb342007-05-07 00:26:15 -07005579 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00005580
5581 /*
5582 * In a shared interrupt configuration, sometimes other devices'
5583 * interrupts will scream. We record the current status tag here
5584 * so that the above check can report that the screaming interrupts
5585 * are unhandled. Eventually they will be silenced.
5586 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005587 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00005588
Michael Chand18edcb2007-03-24 20:57:11 -07005589 if (tg3_irq_sync(tp))
5590 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00005591
Matt Carlson72334482009-08-28 14:03:01 +00005592 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00005593
Matt Carlson09943a12009-08-28 14:01:57 +00005594 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00005595
David S. Millerf47c11e2005-06-24 20:18:35 -07005596out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597 return IRQ_RETVAL(handled);
5598}
5599
Michael Chan79381092005-04-21 17:13:59 -07005600/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01005601static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07005602{
Matt Carlson09943a12009-08-28 14:01:57 +00005603 struct tg3_napi *tnapi = dev_id;
5604 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005605 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07005606
Michael Chanf9804dd2005-09-27 12:13:10 -07005607 if ((sblk->status & SD_STATUS_UPDATED) ||
5608 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07005609 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07005610 return IRQ_RETVAL(1);
5611 }
5612 return IRQ_RETVAL(0);
5613}
5614
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07005615static int tg3_init_hw(struct tg3 *, int);
Michael Chan944d9802005-05-29 14:57:48 -07005616static int tg3_halt(struct tg3 *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617
Michael Chanb9ec6c12006-07-25 16:37:27 -07005618/* Restart hardware after configuration changes, self-test, etc.
5619 * Invoked with tp->lock held.
5620 */
5621static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
Eric Dumazet78c61462008-04-24 23:33:06 -07005622 __releases(tp->lock)
5623 __acquires(tp->lock)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005624{
5625 int err;
5626
5627 err = tg3_init_hw(tp, reset_phy);
5628 if (err) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00005629 netdev_err(tp->dev,
5630 "Failed to re-initialize device, aborting\n");
Michael Chanb9ec6c12006-07-25 16:37:27 -07005631 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
5632 tg3_full_unlock(tp);
5633 del_timer_sync(&tp->timer);
5634 tp->irq_sync = 0;
Matt Carlsonfed97812009-09-01 13:10:19 +00005635 tg3_napi_enable(tp);
Michael Chanb9ec6c12006-07-25 16:37:27 -07005636 dev_close(tp->dev);
5637 tg3_full_lock(tp, 0);
5638 }
5639 return err;
5640}
5641
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642#ifdef CONFIG_NET_POLL_CONTROLLER
5643static void tg3_poll_controller(struct net_device *dev)
5644{
Matt Carlson4f125f42009-09-01 12:55:02 +00005645 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07005646 struct tg3 *tp = netdev_priv(dev);
5647
Matt Carlson4f125f42009-09-01 12:55:02 +00005648 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00005649 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650}
5651#endif
5652
David Howellsc4028952006-11-22 14:57:56 +00005653static void tg3_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654{
David Howellsc4028952006-11-22 14:57:56 +00005655 struct tg3 *tp = container_of(work, struct tg3, reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005656 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657 unsigned int restart_timer;
5658
Michael Chan7faa0062006-02-02 17:29:28 -08005659 tg3_full_lock(tp, 0);
Michael Chan7faa0062006-02-02 17:29:28 -08005660
5661 if (!netif_running(tp->dev)) {
Michael Chan7faa0062006-02-02 17:29:28 -08005662 tg3_full_unlock(tp);
5663 return;
5664 }
5665
5666 tg3_full_unlock(tp);
5667
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005668 tg3_phy_stop(tp);
5669
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670 tg3_netif_stop(tp);
5671
David S. Millerf47c11e2005-06-24 20:18:35 -07005672 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673
5674 restart_timer = tp->tg3_flags2 & TG3_FLG2_RESTART_TIMER;
5675 tp->tg3_flags2 &= ~TG3_FLG2_RESTART_TIMER;
5676
Michael Chandf3e6542006-05-26 17:48:07 -07005677 if (tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING) {
5678 tp->write32_tx_mbox = tg3_write32_tx_mbox;
5679 tp->write32_rx_mbox = tg3_write_flush_reg32;
5680 tp->tg3_flags |= TG3_FLAG_MBOX_WRITE_REORDER;
5681 tp->tg3_flags &= ~TG3_FLAG_TX_RECOVERY_PENDING;
5682 }
5683
Michael Chan944d9802005-05-29 14:57:48 -07005684 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005685 err = tg3_init_hw(tp, 1);
5686 if (err)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005687 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688
5689 tg3_netif_start(tp);
5690
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691 if (restart_timer)
5692 mod_timer(&tp->timer, jiffies + 1);
Michael Chan7faa0062006-02-02 17:29:28 -08005693
Michael Chanb9ec6c12006-07-25 16:37:27 -07005694out:
Michael Chan7faa0062006-02-02 17:29:28 -08005695 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005696
5697 if (!err)
5698 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699}
5700
5701static void tg3_tx_timeout(struct net_device *dev)
5702{
5703 struct tg3 *tp = netdev_priv(dev);
5704
Michael Chanb0408752007-02-13 12:18:30 -08005705 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00005706 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00005707 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08005708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709
5710 schedule_work(&tp->reset_task);
5711}
5712
Michael Chanc58ec932005-09-17 00:46:27 -07005713/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
5714static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
5715{
5716 u32 base = (u32) mapping & 0xffffffff;
5717
Eric Dumazet807540b2010-09-23 05:40:09 +00005718 return (base > 0xffffdcc0) && (base + len + 8 < base);
Michael Chanc58ec932005-09-17 00:46:27 -07005719}
5720
Michael Chan72f2afb2006-03-06 19:28:35 -08005721/* Test for DMA addresses > 40-bit */
5722static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
5723 int len)
5724{
5725#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Michael Chan6728a8e2006-03-27 23:16:49 -08005726 if (tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG)
Eric Dumazet807540b2010-09-23 05:40:09 +00005727 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08005728 return 0;
5729#else
5730 return 0;
5731#endif
5732}
5733
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005734static void tg3_set_txd(struct tg3_napi *, int, dma_addr_t, int, u32, u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735
Michael Chan72f2afb2006-03-06 19:28:35 -08005736/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00005737static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
5738 struct sk_buff *skb, u32 last_plus_one,
5739 u32 *start, u32 base_flags, u32 mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740{
Matt Carlson24f4efd2009-11-13 13:03:35 +00005741 struct tg3 *tp = tnapi->tp;
Matt Carlson41588ba2008-04-19 18:12:33 -07005742 struct sk_buff *new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07005743 dma_addr_t new_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744 u32 entry = *start;
Michael Chanc58ec932005-09-17 00:46:27 -07005745 int i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746
Matt Carlson41588ba2008-04-19 18:12:33 -07005747 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
5748 new_skb = skb_copy(skb, GFP_ATOMIC);
5749 else {
5750 int more_headroom = 4 - ((unsigned long)skb->data & 3);
5751
5752 new_skb = skb_copy_expand(skb,
5753 skb_headroom(skb) + more_headroom,
5754 skb_tailroom(skb), GFP_ATOMIC);
5755 }
5756
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07005758 ret = -1;
5759 } else {
5760 /* New SKB is guaranteed to be linear. */
5761 entry = *start;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005762 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
5763 PCI_DMA_TODEVICE);
5764 /* Make sure the mapping succeeded */
5765 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
5766 ret = -1;
5767 dev_kfree_skb(new_skb);
5768 new_skb = NULL;
David S. Miller90079ce2008-09-11 04:52:51 -07005769
Michael Chanc58ec932005-09-17 00:46:27 -07005770 /* Make sure new skb does not cross any 4G boundaries.
5771 * Drop the packet if it does.
5772 */
Alexander Duyckf4188d82009-12-02 16:48:38 +00005773 } else if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
5774 tg3_4g_overflow_test(new_addr, new_skb->len)) {
5775 pci_unmap_single(tp->pdev, new_addr, new_skb->len,
5776 PCI_DMA_TODEVICE);
Michael Chanc58ec932005-09-17 00:46:27 -07005777 ret = -1;
5778 dev_kfree_skb(new_skb);
5779 new_skb = NULL;
5780 } else {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005781 tg3_set_txd(tnapi, entry, new_addr, new_skb->len,
Michael Chanc58ec932005-09-17 00:46:27 -07005782 base_flags, 1 | (mss << 1));
5783 *start = NEXT_TX(entry);
5784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005785 }
5786
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787 /* Now clean up the sw ring entries. */
5788 i = 0;
5789 while (entry != last_plus_one) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00005790 int len;
5791
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005792 if (i == 0)
Alexander Duyckf4188d82009-12-02 16:48:38 +00005793 len = skb_headlen(skb);
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005794 else
Alexander Duyckf4188d82009-12-02 16:48:38 +00005795 len = skb_shinfo(skb)->frags[i-1].size;
5796
5797 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005798 dma_unmap_addr(&tnapi->tx_buffers[entry],
Alexander Duyckf4188d82009-12-02 16:48:38 +00005799 mapping),
5800 len, PCI_DMA_TODEVICE);
5801 if (i == 0) {
5802 tnapi->tx_buffers[entry].skb = new_skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005803 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00005804 new_addr);
5805 } else {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005806 tnapi->tx_buffers[entry].skb = NULL;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808 entry = NEXT_TX(entry);
5809 i++;
5810 }
5811
5812 dev_kfree_skb(skb);
5813
Michael Chanc58ec932005-09-17 00:46:27 -07005814 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005815}
5816
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005817static void tg3_set_txd(struct tg3_napi *tnapi, int entry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005818 dma_addr_t mapping, int len, u32 flags,
5819 u32 mss_and_is_end)
5820{
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005821 struct tg3_tx_buffer_desc *txd = &tnapi->tx_ring[entry];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005822 int is_end = (mss_and_is_end & 0x1);
5823 u32 mss = (mss_and_is_end >> 1);
5824 u32 vlan_tag = 0;
5825
5826 if (is_end)
5827 flags |= TXD_FLAG_END;
5828 if (flags & TXD_FLAG_VLAN) {
5829 vlan_tag = flags >> 16;
5830 flags &= 0xffff;
5831 }
5832 vlan_tag |= (mss << TXD_MSS_SHIFT);
5833
5834 txd->addr_hi = ((u64) mapping >> 32);
5835 txd->addr_lo = ((u64) mapping & 0xffffffff);
5836 txd->len_flags = (len << TXD_LEN_SHIFT) | flags;
5837 txd->vlan_tag = vlan_tag << TXD_VLAN_TAG_SHIFT;
5838}
5839
Michael Chan5a6f3072006-03-20 22:28:05 -08005840/* hard_start_xmit for devices that don't have any bugs and
Matt Carlsone849cdc2009-11-13 13:03:38 +00005841 * support TG3_FLG2_HW_TSO_2 and TG3_FLG2_HW_TSO_3 only.
Michael Chan5a6f3072006-03-20 22:28:05 -08005842 */
Stephen Hemminger613573252009-08-31 19:50:58 +00005843static netdev_tx_t tg3_start_xmit(struct sk_buff *skb,
5844 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005845{
5846 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005847 u32 len, entry, base_flags, mss;
David S. Miller90079ce2008-09-11 04:52:51 -07005848 dma_addr_t mapping;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005849 struct tg3_napi *tnapi;
5850 struct netdev_queue *txq;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005851 unsigned int i, last;
5852
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005853 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
5854 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Matt Carlson19cfaec2009-12-03 08:36:20 +00005855 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005856 tnapi++;
Michael Chan5a6f3072006-03-20 22:28:05 -08005857
Michael Chan00b70502006-06-17 21:58:45 -07005858 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005859 * and TX reclaim runs via tp->napi.poll inside of a software
Michael Chan5a6f3072006-03-20 22:28:05 -08005860 * interrupt. Furthermore, IRQ processing runs lockless so we have
5861 * no IRQ context deadlocks to worry about either. Rejoice!
5862 */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005863 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005864 if (!netif_tx_queue_stopped(txq)) {
5865 netif_tx_stop_queue(txq);
Michael Chan5a6f3072006-03-20 22:28:05 -08005866
5867 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00005868 netdev_err(dev,
5869 "BUG! Tx Ring full when queue awake!\n");
Michael Chan5a6f3072006-03-20 22:28:05 -08005870 }
Michael Chan5a6f3072006-03-20 22:28:05 -08005871 return NETDEV_TX_BUSY;
5872 }
5873
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005874 entry = tnapi->tx_prod;
Michael Chan5a6f3072006-03-20 22:28:05 -08005875 base_flags = 0;
Matt Carlsonbe98da62010-07-11 09:31:46 +00005876 mss = skb_shinfo(skb)->gso_size;
5877 if (mss) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005878 int tcp_opt_len, ip_tcp_len;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005879 u32 hdrlen;
Michael Chan5a6f3072006-03-20 22:28:05 -08005880
5881 if (skb_header_cloned(skb) &&
5882 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
5883 dev_kfree_skb(skb);
5884 goto out_unlock;
5885 }
5886
Matt Carlson02e96082010-09-15 08:59:59 +00005887 if (skb_is_gso_v6(skb)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005888 hdrlen = skb_headlen(skb) - ETH_HLEN;
Matt Carlson02e96082010-09-15 08:59:59 +00005889 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005890 struct iphdr *iph = ip_hdr(skb);
5891
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07005892 tcp_opt_len = tcp_optlen(skb);
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -03005893 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
Michael Chanb0026622006-07-03 19:42:14 -07005894
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005895 iph->check = 0;
5896 iph->tot_len = htons(mss + ip_tcp_len + tcp_opt_len);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005897 hdrlen = ip_tcp_len + tcp_opt_len;
Michael Chanb0026622006-07-03 19:42:14 -07005898 }
Michael Chan5a6f3072006-03-20 22:28:05 -08005899
Matt Carlsone849cdc2009-11-13 13:03:38 +00005900 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005901 mss |= (hdrlen & 0xc) << 12;
5902 if (hdrlen & 0x10)
5903 base_flags |= 0x00000010;
5904 base_flags |= (hdrlen & 0x3e0) << 5;
5905 } else
5906 mss |= hdrlen << 9;
5907
Michael Chan5a6f3072006-03-20 22:28:05 -08005908 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
5909 TXD_FLAG_CPU_POST_DMA);
5910
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005911 tcp_hdr(skb)->check = 0;
Michael Chan5a6f3072006-03-20 22:28:05 -08005912
Matt Carlson859a588792010-04-05 10:19:28 +00005913 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005914 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson859a588792010-04-05 10:19:28 +00005915 }
5916
Jesse Grosseab6d182010-10-20 13:56:03 +00005917 if (vlan_tx_tag_present(skb))
Michael Chan5a6f3072006-03-20 22:28:05 -08005918 base_flags |= (TXD_FLAG_VLAN |
5919 (vlan_tx_tag_get(skb) << 16));
Michael Chan5a6f3072006-03-20 22:28:05 -08005920
Alexander Duyckf4188d82009-12-02 16:48:38 +00005921 len = skb_headlen(skb);
5922
5923 /* Queue skb data, a.k.a. the main skb fragment. */
5924 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
5925 if (pci_dma_mapping_error(tp->pdev, mapping)) {
David S. Miller90079ce2008-09-11 04:52:51 -07005926 dev_kfree_skb(skb);
5927 goto out_unlock;
5928 }
5929
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005930 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005931 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005932
Matt Carlsonb703df62009-12-03 08:36:21 +00005933 if ((tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) &&
Matt Carlson8fc2f992010-12-06 08:28:49 +00005934 !mss && skb->len > VLAN_ETH_FRAME_LEN)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005935 base_flags |= TXD_FLAG_JMB_PKT;
5936
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005937 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
Michael Chan5a6f3072006-03-20 22:28:05 -08005938 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
5939
5940 entry = NEXT_TX(entry);
5941
5942 /* Now loop through additional data fragments, and queue them. */
5943 if (skb_shinfo(skb)->nr_frags > 0) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005944 last = skb_shinfo(skb)->nr_frags - 1;
5945 for (i = 0; i <= last; i++) {
5946 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5947
5948 len = frag->size;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005949 mapping = pci_map_page(tp->pdev,
5950 frag->page,
5951 frag->page_offset,
5952 len, PCI_DMA_TODEVICE);
5953 if (pci_dma_mapping_error(tp->pdev, mapping))
5954 goto dma_error;
5955
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005956 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005957 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00005958 mapping);
Michael Chan5a6f3072006-03-20 22:28:05 -08005959
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005960 tg3_set_txd(tnapi, entry, mapping, len,
Michael Chan5a6f3072006-03-20 22:28:05 -08005961 base_flags, (i == last) | (mss << 1));
5962
5963 entry = NEXT_TX(entry);
5964 }
5965 }
5966
5967 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005968 tw32_tx_mbox(tnapi->prodmbox, entry);
Michael Chan5a6f3072006-03-20 22:28:05 -08005969
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005970 tnapi->tx_prod = entry;
5971 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005972 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00005973
5974 /* netif_tx_stop_queue() must be done before checking
5975 * checking tx index in tg3_tx_avail() below, because in
5976 * tg3_tx(), we update tx index before checking for
5977 * netif_tx_queue_stopped().
5978 */
5979 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005980 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005981 netif_tx_wake_queue(txq);
Michael Chan5a6f3072006-03-20 22:28:05 -08005982 }
5983
5984out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00005985 mmiowb();
Michael Chan5a6f3072006-03-20 22:28:05 -08005986
5987 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005988
5989dma_error:
5990 last = i;
5991 entry = tnapi->tx_prod;
5992 tnapi->tx_buffers[entry].skb = NULL;
5993 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005994 dma_unmap_addr(&tnapi->tx_buffers[entry], mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005995 skb_headlen(skb),
5996 PCI_DMA_TODEVICE);
5997 for (i = 0; i <= last; i++) {
5998 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5999 entry = NEXT_TX(entry);
6000
6001 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006002 dma_unmap_addr(&tnapi->tx_buffers[entry],
Alexander Duyckf4188d82009-12-02 16:48:38 +00006003 mapping),
6004 frag->size, PCI_DMA_TODEVICE);
6005 }
6006
6007 dev_kfree_skb(skb);
6008 return NETDEV_TX_OK;
Michael Chan5a6f3072006-03-20 22:28:05 -08006009}
6010
Stephen Hemminger613573252009-08-31 19:50:58 +00006011static netdev_tx_t tg3_start_xmit_dma_bug(struct sk_buff *,
6012 struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07006013
6014/* Use GSO to workaround a rare TSO bug that may be triggered when the
6015 * TSO header is greater than 80 bytes.
6016 */
6017static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
6018{
6019 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006020 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07006021
6022 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006023 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07006024 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006025
6026 /* netif_tx_stop_queue() must be done before checking
6027 * checking tx index in tg3_tx_avail() below, because in
6028 * tg3_tx(), we update tx index before checking for
6029 * netif_tx_queue_stopped().
6030 */
6031 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006032 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08006033 return NETDEV_TX_BUSY;
6034
6035 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07006036 }
6037
6038 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07006039 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07006040 goto tg3_tso_bug_end;
6041
6042 do {
6043 nskb = segs;
6044 segs = segs->next;
6045 nskb->next = NULL;
6046 tg3_start_xmit_dma_bug(nskb, tp->dev);
6047 } while (segs);
6048
6049tg3_tso_bug_end:
6050 dev_kfree_skb(skb);
6051
6052 return NETDEV_TX_OK;
6053}
Michael Chan52c0fd82006-06-29 20:15:54 -07006054
Michael Chan5a6f3072006-03-20 22:28:05 -08006055/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
6056 * support TG3_FLG2_HW_TSO_1 or firmware TSO only.
6057 */
Stephen Hemminger613573252009-08-31 19:50:58 +00006058static netdev_tx_t tg3_start_xmit_dma_bug(struct sk_buff *skb,
6059 struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08006060{
6061 struct tg3 *tp = netdev_priv(dev);
Michael Chan5a6f3072006-03-20 22:28:05 -08006062 u32 len, entry, base_flags, mss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063 int would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07006064 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00006065 struct tg3_napi *tnapi;
6066 struct netdev_queue *txq;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006067 unsigned int i, last;
6068
Matt Carlson24f4efd2009-11-13 13:03:35 +00006069 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
6070 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Matt Carlson19cfaec2009-12-03 08:36:20 +00006071 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlson24f4efd2009-11-13 13:03:35 +00006072 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006073
Michael Chan00b70502006-06-17 21:58:45 -07006074 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006075 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07006076 * interrupt. Furthermore, IRQ processing runs lockless so we have
6077 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07006078 */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006079 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006080 if (!netif_tx_queue_stopped(txq)) {
6081 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08006082
6083 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00006084 netdev_err(dev,
6085 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08006086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006087 return NETDEV_TX_BUSY;
6088 }
6089
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006090 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006091 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07006092 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006093 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00006094
Matt Carlsonbe98da62010-07-11 09:31:46 +00006095 mss = skb_shinfo(skb)->gso_size;
6096 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006097 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00006098 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006099
6100 if (skb_header_cloned(skb) &&
6101 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
6102 dev_kfree_skb(skb);
6103 goto out_unlock;
6104 }
6105
Matt Carlson34195c32010-07-11 09:31:42 +00006106 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07006107 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006108
Matt Carlson02e96082010-09-15 08:59:59 +00006109 if (skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00006110 hdr_len = skb_headlen(skb) - ETH_HLEN;
6111 } else {
6112 u32 ip_tcp_len;
6113
6114 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
6115 hdr_len = ip_tcp_len + tcp_opt_len;
6116
6117 iph->check = 0;
6118 iph->tot_len = htons(mss + hdr_len);
6119 }
6120
Michael Chan52c0fd82006-06-29 20:15:54 -07006121 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Michael Chan7f62ad52007-02-20 23:25:40 -08006122 (tp->tg3_flags2 & TG3_FLG2_TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00006123 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07006124
Linus Torvalds1da177e2005-04-16 15:20:36 -07006125 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
6126 TXD_FLAG_CPU_POST_DMA);
6127
Linus Torvalds1da177e2005-04-16 15:20:36 -07006128 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006129 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006130 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006131 } else
6132 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
6133 iph->daddr, 0,
6134 IPPROTO_TCP,
6135 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006136
Matt Carlson615774f2009-11-13 13:03:39 +00006137 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) {
6138 mss |= (hdr_len & 0xc) << 12;
6139 if (hdr_len & 0x10)
6140 base_flags |= 0x00000010;
6141 base_flags |= (hdr_len & 0x3e0) << 5;
6142 } else if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_2)
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006143 mss |= hdr_len << 9;
6144 else if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_1) ||
6145 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006146 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006147 int tsflags;
6148
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006149 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006150 mss |= (tsflags << 11);
6151 }
6152 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006153 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006154 int tsflags;
6155
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006156 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006157 base_flags |= tsflags << 12;
6158 }
6159 }
6160 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00006161
Jesse Grosseab6d182010-10-20 13:56:03 +00006162 if (vlan_tx_tag_present(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006163 base_flags |= (TXD_FLAG_VLAN |
6164 (vlan_tx_tag_get(skb) << 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006165
Matt Carlsonb703df62009-12-03 08:36:21 +00006166 if ((tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) &&
Matt Carlson8fc2f992010-12-06 08:28:49 +00006167 !mss && skb->len > VLAN_ETH_FRAME_LEN)
Matt Carlson615774f2009-11-13 13:03:39 +00006168 base_flags |= TXD_FLAG_JMB_PKT;
6169
Alexander Duyckf4188d82009-12-02 16:48:38 +00006170 len = skb_headlen(skb);
6171
6172 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
6173 if (pci_dma_mapping_error(tp->pdev, mapping)) {
David S. Miller90079ce2008-09-11 04:52:51 -07006174 dev_kfree_skb(skb);
6175 goto out_unlock;
6176 }
6177
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006178 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006179 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006180
6181 would_hit_hwbug = 0;
6182
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006183 if ((tp->tg3_flags3 & TG3_FLG3_SHORT_DMA_BUG) && len <= 8)
6184 would_hit_hwbug = 1;
6185
Matt Carlson0e1406d2009-11-02 12:33:33 +00006186 if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
6187 tg3_4g_overflow_test(mapping, len))
Matt Carlson41588ba2008-04-19 18:12:33 -07006188 would_hit_hwbug = 1;
Matt Carlson0e1406d2009-11-02 12:33:33 +00006189
6190 if ((tp->tg3_flags3 & TG3_FLG3_40BIT_DMA_LIMIT_BUG) &&
6191 tg3_40bit_overflow_test(tp, mapping, len))
6192 would_hit_hwbug = 1;
6193
6194 if (tp->tg3_flags3 & TG3_FLG3_5701_DMA_BUG)
Michael Chanc58ec932005-09-17 00:46:27 -07006195 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006196
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006197 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006198 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
6199
6200 entry = NEXT_TX(entry);
6201
6202 /* Now loop through additional data fragments, and queue them. */
6203 if (skb_shinfo(skb)->nr_frags > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006204 last = skb_shinfo(skb)->nr_frags - 1;
6205 for (i = 0; i <= last; i++) {
6206 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6207
6208 len = frag->size;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006209 mapping = pci_map_page(tp->pdev,
6210 frag->page,
6211 frag->page_offset,
6212 len, PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006213
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006214 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006215 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00006216 mapping);
6217 if (pci_dma_mapping_error(tp->pdev, mapping))
6218 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006219
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006220 if ((tp->tg3_flags3 & TG3_FLG3_SHORT_DMA_BUG) &&
6221 len <= 8)
6222 would_hit_hwbug = 1;
6223
Matt Carlson0e1406d2009-11-02 12:33:33 +00006224 if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
6225 tg3_4g_overflow_test(mapping, len))
Michael Chanc58ec932005-09-17 00:46:27 -07006226 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006227
Matt Carlson0e1406d2009-11-02 12:33:33 +00006228 if ((tp->tg3_flags3 & TG3_FLG3_40BIT_DMA_LIMIT_BUG) &&
6229 tg3_40bit_overflow_test(tp, mapping, len))
Michael Chan72f2afb2006-03-06 19:28:35 -08006230 would_hit_hwbug = 1;
6231
Linus Torvalds1da177e2005-04-16 15:20:36 -07006232 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006233 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006234 base_flags, (i == last)|(mss << 1));
6235 else
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006236 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006237 base_flags, (i == last));
6238
6239 entry = NEXT_TX(entry);
6240 }
6241 }
6242
6243 if (would_hit_hwbug) {
6244 u32 last_plus_one = entry;
6245 u32 start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246
Michael Chanc58ec932005-09-17 00:46:27 -07006247 start = entry - 1 - skb_shinfo(skb)->nr_frags;
6248 start &= (TG3_TX_RING_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249
6250 /* If the workaround fails due to memory/mapping
6251 * failure, silently drop this packet.
6252 */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006253 if (tigon3_dma_hwbug_workaround(tnapi, skb, last_plus_one,
Michael Chanc58ec932005-09-17 00:46:27 -07006254 &start, base_flags, mss))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006255 goto out_unlock;
6256
6257 entry = start;
6258 }
6259
6260 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006261 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006262
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006263 tnapi->tx_prod = entry;
6264 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006265 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006266
6267 /* netif_tx_stop_queue() must be done before checking
6268 * checking tx index in tg3_tx_avail() below, because in
6269 * tg3_tx(), we update tx index before checking for
6270 * netif_tx_queue_stopped().
6271 */
6272 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006273 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006274 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07006275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006276
6277out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00006278 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006279
6280 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006281
6282dma_error:
6283 last = i;
6284 entry = tnapi->tx_prod;
6285 tnapi->tx_buffers[entry].skb = NULL;
6286 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006287 dma_unmap_addr(&tnapi->tx_buffers[entry], mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006288 skb_headlen(skb),
6289 PCI_DMA_TODEVICE);
6290 for (i = 0; i <= last; i++) {
6291 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6292 entry = NEXT_TX(entry);
6293
6294 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006295 dma_unmap_addr(&tnapi->tx_buffers[entry],
Alexander Duyckf4188d82009-12-02 16:48:38 +00006296 mapping),
6297 frag->size, PCI_DMA_TODEVICE);
6298 }
6299
6300 dev_kfree_skb(skb);
6301 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006302}
6303
Michał Mirosławdc668912011-04-07 03:35:07 +00006304static u32 tg3_fix_features(struct net_device *dev, u32 features)
6305{
6306 struct tg3 *tp = netdev_priv(dev);
6307
6308 if (dev->mtu > ETH_DATA_LEN && (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
6309 features &= ~NETIF_F_ALL_TSO;
6310
6311 return features;
6312}
6313
Linus Torvalds1da177e2005-04-16 15:20:36 -07006314static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
6315 int new_mtu)
6316{
6317 dev->mtu = new_mtu;
6318
Michael Chanef7f5ec2005-07-25 12:32:25 -07006319 if (new_mtu > ETH_DATA_LEN) {
Michael Chana4e2b342005-10-26 15:46:52 -07006320 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
Michał Mirosławdc668912011-04-07 03:35:07 +00006321 netdev_update_features(dev);
Michael Chanef7f5ec2005-07-25 12:32:25 -07006322 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
Matt Carlson859a588792010-04-05 10:19:28 +00006323 } else {
Michael Chanef7f5ec2005-07-25 12:32:25 -07006324 tp->tg3_flags |= TG3_FLAG_JUMBO_RING_ENABLE;
Matt Carlson859a588792010-04-05 10:19:28 +00006325 }
Michael Chanef7f5ec2005-07-25 12:32:25 -07006326 } else {
Michał Mirosławdc668912011-04-07 03:35:07 +00006327 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
Michael Chanef7f5ec2005-07-25 12:32:25 -07006328 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
Michał Mirosławdc668912011-04-07 03:35:07 +00006329 netdev_update_features(dev);
6330 }
Michael Chan0f893dc2005-07-25 12:30:38 -07006331 tp->tg3_flags &= ~TG3_FLAG_JUMBO_RING_ENABLE;
Michael Chanef7f5ec2005-07-25 12:32:25 -07006332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006333}
6334
6335static int tg3_change_mtu(struct net_device *dev, int new_mtu)
6336{
6337 struct tg3 *tp = netdev_priv(dev);
Michael Chanb9ec6c12006-07-25 16:37:27 -07006338 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006339
6340 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
6341 return -EINVAL;
6342
6343 if (!netif_running(dev)) {
6344 /* We'll just catch it later when the
6345 * device is up'd.
6346 */
6347 tg3_set_mtu(dev, tp, new_mtu);
6348 return 0;
6349 }
6350
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006351 tg3_phy_stop(tp);
6352
Linus Torvalds1da177e2005-04-16 15:20:36 -07006353 tg3_netif_stop(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07006354
6355 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006356
Michael Chan944d9802005-05-29 14:57:48 -07006357 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006358
6359 tg3_set_mtu(dev, tp, new_mtu);
6360
Michael Chanb9ec6c12006-07-25 16:37:27 -07006361 err = tg3_restart_hw(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006362
Michael Chanb9ec6c12006-07-25 16:37:27 -07006363 if (!err)
6364 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006365
David S. Millerf47c11e2005-06-24 20:18:35 -07006366 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006367
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006368 if (!err)
6369 tg3_phy_start(tp);
6370
Michael Chanb9ec6c12006-07-25 16:37:27 -07006371 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006372}
6373
Matt Carlson21f581a2009-08-28 14:00:25 +00006374static void tg3_rx_prodring_free(struct tg3 *tp,
6375 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006376{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377 int i;
6378
Matt Carlson8fea32b2010-09-15 08:59:58 +00006379 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006380 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00006381 i = (i + 1) & tp->rx_std_ring_mask)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006382 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6383 tp->rx_pkt_map_sz);
6384
6385 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
6386 for (i = tpr->rx_jmb_cons_idx;
6387 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00006388 i = (i + 1) & tp->rx_jmb_ring_mask) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006389 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6390 TG3_RX_JMB_MAP_SZ);
6391 }
6392 }
6393
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006394 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006396
Matt Carlson2c49a442010-09-30 10:34:35 +00006397 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006398 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6399 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006400
Matt Carlson48035722010-10-14 10:37:43 +00006401 if ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) &&
6402 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006403 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006404 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6405 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006406 }
6407}
6408
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006409/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006410 *
6411 * The chip has been shut down and the driver detached from
6412 * the networking, so no interrupts or new tx packets will
6413 * end up in the driver. tp->{tx,}lock are held and thus
6414 * we may not sleep.
6415 */
Matt Carlson21f581a2009-08-28 14:00:25 +00006416static int tg3_rx_prodring_alloc(struct tg3 *tp,
6417 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006418{
Matt Carlson287be122009-08-28 13:58:46 +00006419 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006420
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006421 tpr->rx_std_cons_idx = 0;
6422 tpr->rx_std_prod_idx = 0;
6423 tpr->rx_jmb_cons_idx = 0;
6424 tpr->rx_jmb_prod_idx = 0;
6425
Matt Carlson8fea32b2010-09-15 08:59:58 +00006426 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006427 memset(&tpr->rx_std_buffers[0], 0,
6428 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00006429 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006430 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00006431 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006432 goto done;
6433 }
6434
Linus Torvalds1da177e2005-04-16 15:20:36 -07006435 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00006436 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006437
Matt Carlson287be122009-08-28 13:58:46 +00006438 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Michael Chana4e2b342005-10-26 15:46:52 -07006439 if ((tp->tg3_flags2 & TG3_FLG2_5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00006440 tp->dev->mtu > ETH_DATA_LEN)
6441 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
6442 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07006443
Linus Torvalds1da177e2005-04-16 15:20:36 -07006444 /* Initialize invariants of the rings, we only set this
6445 * stuff once. This works because the card does not
6446 * write into the rx buffer posting rings.
6447 */
Matt Carlson2c49a442010-09-30 10:34:35 +00006448 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006449 struct tg3_rx_buffer_desc *rxd;
6450
Matt Carlson21f581a2009-08-28 14:00:25 +00006451 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00006452 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006453 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
6454 rxd->opaque = (RXD_OPAQUE_RING_STD |
6455 (i << RXD_OPAQUE_INDEX_SHIFT));
6456 }
6457
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006458 /* Now allocate fresh SKBs for each rx ring. */
6459 for (i = 0; i < tp->rx_pending; i++) {
Matt Carlson86b21e52009-11-13 13:03:45 +00006460 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_STD, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006461 netdev_warn(tp->dev,
6462 "Using a smaller RX standard ring. Only "
6463 "%d out of %d buffers were allocated "
6464 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006465 if (i == 0)
6466 goto initfail;
6467 tp->rx_pending = i;
6468 break;
6469 }
6470 }
6471
Matt Carlson48035722010-10-14 10:37:43 +00006472 if (!(tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) ||
6473 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006474 goto done;
6475
Matt Carlson2c49a442010-09-30 10:34:35 +00006476 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006477
Matt Carlson0d86df82010-02-17 15:17:00 +00006478 if (!(tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE))
6479 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006480
Matt Carlson2c49a442010-09-30 10:34:35 +00006481 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00006482 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006483
Matt Carlson0d86df82010-02-17 15:17:00 +00006484 rxd = &tpr->rx_jmb[i].std;
6485 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
6486 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
6487 RXD_FLAG_JUMBO;
6488 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
6489 (i << RXD_OPAQUE_INDEX_SHIFT));
6490 }
6491
6492 for (i = 0; i < tp->rx_jumbo_pending; i++) {
6493 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_JUMBO, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006494 netdev_warn(tp->dev,
6495 "Using a smaller RX jumbo ring. Only %d "
6496 "out of %d buffers were allocated "
6497 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00006498 if (i == 0)
6499 goto initfail;
6500 tp->rx_jumbo_pending = i;
6501 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006502 }
6503 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006504
6505done:
Michael Chan32d8c572006-07-25 16:38:29 -07006506 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006507
6508initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00006509 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006510 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006511}
6512
Matt Carlson21f581a2009-08-28 14:00:25 +00006513static void tg3_rx_prodring_fini(struct tg3 *tp,
6514 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006515{
Matt Carlson21f581a2009-08-28 14:00:25 +00006516 kfree(tpr->rx_std_buffers);
6517 tpr->rx_std_buffers = NULL;
6518 kfree(tpr->rx_jmb_buffers);
6519 tpr->rx_jmb_buffers = NULL;
6520 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006521 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
6522 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00006523 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006524 }
Matt Carlson21f581a2009-08-28 14:00:25 +00006525 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006526 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
6527 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00006528 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006529 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006530}
6531
Matt Carlson21f581a2009-08-28 14:00:25 +00006532static int tg3_rx_prodring_init(struct tg3 *tp,
6533 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006534{
Matt Carlson2c49a442010-09-30 10:34:35 +00006535 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
6536 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006537 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006538 return -ENOMEM;
6539
Matt Carlson4bae65c2010-11-24 08:31:52 +00006540 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
6541 TG3_RX_STD_RING_BYTES(tp),
6542 &tpr->rx_std_mapping,
6543 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006544 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006545 goto err_out;
6546
Matt Carlson48035722010-10-14 10:37:43 +00006547 if ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) &&
6548 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006549 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00006550 GFP_KERNEL);
6551 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006552 goto err_out;
6553
Matt Carlson4bae65c2010-11-24 08:31:52 +00006554 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
6555 TG3_RX_JMB_RING_BYTES(tp),
6556 &tpr->rx_jmb_mapping,
6557 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006558 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006559 goto err_out;
6560 }
6561
6562 return 0;
6563
6564err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00006565 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006566 return -ENOMEM;
6567}
6568
6569/* Free up pending packets in all rx/tx rings.
6570 *
6571 * The chip has been shut down and the driver detached from
6572 * the networking, so no interrupts or new tx packets will
6573 * end up in the driver. tp->{tx,}lock is not held and we are not
6574 * in an interrupt context and thus may sleep.
6575 */
6576static void tg3_free_rings(struct tg3 *tp)
6577{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006578 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006579
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006580 for (j = 0; j < tp->irq_cnt; j++) {
6581 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006582
Matt Carlson8fea32b2010-09-15 08:59:58 +00006583 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00006584
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006585 if (!tnapi->tx_buffers)
6586 continue;
6587
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006588 for (i = 0; i < TG3_TX_RING_SIZE; ) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00006589 struct ring_info *txp;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006590 struct sk_buff *skb;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006591 unsigned int k;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006592
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006593 txp = &tnapi->tx_buffers[i];
6594 skb = txp->skb;
6595
6596 if (skb == NULL) {
6597 i++;
6598 continue;
6599 }
6600
Alexander Duyckf4188d82009-12-02 16:48:38 +00006601 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006602 dma_unmap_addr(txp, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006603 skb_headlen(skb),
6604 PCI_DMA_TODEVICE);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006605 txp->skb = NULL;
6606
Alexander Duyckf4188d82009-12-02 16:48:38 +00006607 i++;
6608
6609 for (k = 0; k < skb_shinfo(skb)->nr_frags; k++) {
6610 txp = &tnapi->tx_buffers[i & (TG3_TX_RING_SIZE - 1)];
6611 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006612 dma_unmap_addr(txp, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006613 skb_shinfo(skb)->frags[k].size,
6614 PCI_DMA_TODEVICE);
6615 i++;
6616 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006617
6618 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006619 }
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006620 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006621}
6622
6623/* Initialize tx/rx rings for packet processing.
6624 *
6625 * The chip has been shut down and the driver detached from
6626 * the networking, so no interrupts or new tx packets will
6627 * end up in the driver. tp->{tx,}lock are held and thus
6628 * we may not sleep.
6629 */
6630static int tg3_init_rings(struct tg3 *tp)
6631{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006632 int i;
Matt Carlson72334482009-08-28 14:03:01 +00006633
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006634 /* Free up all the SKBs. */
6635 tg3_free_rings(tp);
6636
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006637 for (i = 0; i < tp->irq_cnt; i++) {
6638 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006639
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006640 tnapi->last_tag = 0;
6641 tnapi->last_irq_tag = 0;
6642 tnapi->hw_status->status = 0;
6643 tnapi->hw_status->status_tag = 0;
6644 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6645
6646 tnapi->tx_prod = 0;
6647 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006648 if (tnapi->tx_ring)
6649 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006650
6651 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006652 if (tnapi->rx_rcb)
6653 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006654
Matt Carlson8fea32b2010-09-15 08:59:58 +00006655 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00006656 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006657 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006658 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006659 }
Matt Carlson72334482009-08-28 14:03:01 +00006660
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006661 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006662}
6663
6664/*
6665 * Must not be invoked with interrupt sources disabled and
6666 * the hardware shutdown down.
6667 */
6668static void tg3_free_consistent(struct tg3 *tp)
6669{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006670 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006671
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006672 for (i = 0; i < tp->irq_cnt; i++) {
6673 struct tg3_napi *tnapi = &tp->napi[i];
6674
6675 if (tnapi->tx_ring) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006676 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006677 tnapi->tx_ring, tnapi->tx_desc_mapping);
6678 tnapi->tx_ring = NULL;
6679 }
6680
6681 kfree(tnapi->tx_buffers);
6682 tnapi->tx_buffers = NULL;
6683
6684 if (tnapi->rx_rcb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006685 dma_free_coherent(&tp->pdev->dev,
6686 TG3_RX_RCB_RING_BYTES(tp),
6687 tnapi->rx_rcb,
6688 tnapi->rx_rcb_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006689 tnapi->rx_rcb = NULL;
6690 }
6691
Matt Carlson8fea32b2010-09-15 08:59:58 +00006692 tg3_rx_prodring_fini(tp, &tnapi->prodring);
6693
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006694 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006695 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
6696 tnapi->hw_status,
6697 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006698 tnapi->hw_status = NULL;
6699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006700 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006701
Linus Torvalds1da177e2005-04-16 15:20:36 -07006702 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006703 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
6704 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006705 tp->hw_stats = NULL;
6706 }
6707}
6708
6709/*
6710 * Must not be invoked with interrupt sources disabled and
6711 * the hardware shutdown down. Can sleep.
6712 */
6713static int tg3_alloc_consistent(struct tg3 *tp)
6714{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006715 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006716
Matt Carlson4bae65c2010-11-24 08:31:52 +00006717 tp->hw_stats = dma_alloc_coherent(&tp->pdev->dev,
6718 sizeof(struct tg3_hw_stats),
6719 &tp->stats_mapping,
6720 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006721 if (!tp->hw_stats)
6722 goto err_out;
6723
Linus Torvalds1da177e2005-04-16 15:20:36 -07006724 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6725
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006726 for (i = 0; i < tp->irq_cnt; i++) {
6727 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006728 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006729
Matt Carlson4bae65c2010-11-24 08:31:52 +00006730 tnapi->hw_status = dma_alloc_coherent(&tp->pdev->dev,
6731 TG3_HW_STATUS_SIZE,
6732 &tnapi->status_mapping,
6733 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006734 if (!tnapi->hw_status)
6735 goto err_out;
6736
6737 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006738 sblk = tnapi->hw_status;
6739
Matt Carlson8fea32b2010-09-15 08:59:58 +00006740 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
6741 goto err_out;
6742
Matt Carlson19cfaec2009-12-03 08:36:20 +00006743 /* If multivector TSS is enabled, vector 0 does not handle
6744 * tx interrupts. Don't allocate any resources for it.
6745 */
6746 if ((!i && !(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)) ||
6747 (i && (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS))) {
6748 tnapi->tx_buffers = kzalloc(sizeof(struct ring_info) *
6749 TG3_TX_RING_SIZE,
6750 GFP_KERNEL);
6751 if (!tnapi->tx_buffers)
6752 goto err_out;
6753
Matt Carlson4bae65c2010-11-24 08:31:52 +00006754 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
6755 TG3_TX_RING_BYTES,
6756 &tnapi->tx_desc_mapping,
6757 GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00006758 if (!tnapi->tx_ring)
6759 goto err_out;
6760 }
6761
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006762 /*
6763 * When RSS is enabled, the status block format changes
6764 * slightly. The "rx_jumbo_consumer", "reserved",
6765 * and "rx_mini_consumer" members get mapped to the
6766 * other three rx return ring producer indexes.
6767 */
6768 switch (i) {
6769 default:
6770 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
6771 break;
6772 case 2:
6773 tnapi->rx_rcb_prod_idx = &sblk->rx_jumbo_consumer;
6774 break;
6775 case 3:
6776 tnapi->rx_rcb_prod_idx = &sblk->reserved;
6777 break;
6778 case 4:
6779 tnapi->rx_rcb_prod_idx = &sblk->rx_mini_consumer;
6780 break;
6781 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006782
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006783 /*
6784 * If multivector RSS is enabled, vector 0 does not handle
6785 * rx or tx interrupts. Don't allocate any resources for it.
6786 */
6787 if (!i && (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS))
6788 continue;
6789
Matt Carlson4bae65c2010-11-24 08:31:52 +00006790 tnapi->rx_rcb = dma_alloc_coherent(&tp->pdev->dev,
6791 TG3_RX_RCB_RING_BYTES(tp),
6792 &tnapi->rx_rcb_mapping,
6793 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006794 if (!tnapi->rx_rcb)
6795 goto err_out;
6796
6797 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006798 }
6799
Linus Torvalds1da177e2005-04-16 15:20:36 -07006800 return 0;
6801
6802err_out:
6803 tg3_free_consistent(tp);
6804 return -ENOMEM;
6805}
6806
6807#define MAX_WAIT_CNT 1000
6808
6809/* To stop a block, clear the enable bit and poll till it
6810 * clears. tp->lock is held.
6811 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006812static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006813{
6814 unsigned int i;
6815 u32 val;
6816
6817 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
6818 switch (ofs) {
6819 case RCVLSC_MODE:
6820 case DMAC_MODE:
6821 case MBFREE_MODE:
6822 case BUFMGR_MODE:
6823 case MEMARB_MODE:
6824 /* We can't enable/disable these bits of the
6825 * 5705/5750, just say success.
6826 */
6827 return 0;
6828
6829 default:
6830 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006832 }
6833
6834 val = tr32(ofs);
6835 val &= ~enable_bit;
6836 tw32_f(ofs, val);
6837
6838 for (i = 0; i < MAX_WAIT_CNT; i++) {
6839 udelay(100);
6840 val = tr32(ofs);
6841 if ((val & enable_bit) == 0)
6842 break;
6843 }
6844
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006845 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00006846 dev_err(&tp->pdev->dev,
6847 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
6848 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006849 return -ENODEV;
6850 }
6851
6852 return 0;
6853}
6854
6855/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006856static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006857{
6858 int i, err;
6859
6860 tg3_disable_ints(tp);
6861
6862 tp->rx_mode &= ~RX_MODE_ENABLE;
6863 tw32_f(MAC_RX_MODE, tp->rx_mode);
6864 udelay(10);
6865
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006866 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
6867 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
6868 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
6869 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
6870 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
6871 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006872
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006873 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
6874 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
6875 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
6876 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
6877 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
6878 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
6879 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006880
6881 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
6882 tw32_f(MAC_MODE, tp->mac_mode);
6883 udelay(40);
6884
6885 tp->tx_mode &= ~TX_MODE_ENABLE;
6886 tw32_f(MAC_TX_MODE, tp->tx_mode);
6887
6888 for (i = 0; i < MAX_WAIT_CNT; i++) {
6889 udelay(100);
6890 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
6891 break;
6892 }
6893 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00006894 dev_err(&tp->pdev->dev,
6895 "%s timed out, TX_MODE_ENABLE will not clear "
6896 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07006897 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006898 }
6899
Michael Chane6de8ad2005-05-05 14:42:41 -07006900 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006901 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
6902 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006903
6904 tw32(FTQ_RESET, 0xffffffff);
6905 tw32(FTQ_RESET, 0x00000000);
6906
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006907 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
6908 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006909
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006910 for (i = 0; i < tp->irq_cnt; i++) {
6911 struct tg3_napi *tnapi = &tp->napi[i];
6912 if (tnapi->hw_status)
6913 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006915 if (tp->hw_stats)
6916 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6917
Linus Torvalds1da177e2005-04-16 15:20:36 -07006918 return err;
6919}
6920
Matt Carlson0d3031d2007-10-10 18:02:43 -07006921static void tg3_ape_send_event(struct tg3 *tp, u32 event)
6922{
6923 int i;
6924 u32 apedata;
6925
Matt Carlsondc6d0742010-09-15 08:59:55 +00006926 /* NCSI does not support APE events */
6927 if (tp->tg3_flags3 & TG3_FLG3_APE_HAS_NCSI)
6928 return;
6929
Matt Carlson0d3031d2007-10-10 18:02:43 -07006930 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
6931 if (apedata != APE_SEG_SIG_MAGIC)
6932 return;
6933
6934 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
Matt Carlson731fd792008-08-15 14:07:51 -07006935 if (!(apedata & APE_FW_STATUS_READY))
Matt Carlson0d3031d2007-10-10 18:02:43 -07006936 return;
6937
6938 /* Wait for up to 1 millisecond for APE to service previous event. */
6939 for (i = 0; i < 10; i++) {
6940 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
6941 return;
6942
6943 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
6944
6945 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6946 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
6947 event | APE_EVENT_STATUS_EVENT_PENDING);
6948
6949 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
6950
6951 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6952 break;
6953
6954 udelay(100);
6955 }
6956
6957 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6958 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
6959}
6960
6961static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
6962{
6963 u32 event;
6964 u32 apedata;
6965
6966 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
6967 return;
6968
6969 switch (kind) {
Matt Carlson33f401a2010-04-05 10:19:27 +00006970 case RESET_KIND_INIT:
6971 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
6972 APE_HOST_SEG_SIG_MAGIC);
6973 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
6974 APE_HOST_SEG_LEN_MAGIC);
6975 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
6976 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
6977 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
Matt Carlson6867c842010-07-11 09:31:44 +00006978 APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
Matt Carlson33f401a2010-04-05 10:19:27 +00006979 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
6980 APE_HOST_BEHAV_NO_PHYLOCK);
Matt Carlsondc6d0742010-09-15 08:59:55 +00006981 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
6982 TG3_APE_HOST_DRVR_STATE_START);
Matt Carlson0d3031d2007-10-10 18:02:43 -07006983
Matt Carlson33f401a2010-04-05 10:19:27 +00006984 event = APE_EVENT_STATUS_STATE_START;
6985 break;
6986 case RESET_KIND_SHUTDOWN:
6987 /* With the interface we are currently using,
6988 * APE does not track driver state. Wiping
6989 * out the HOST SEGMENT SIGNATURE forces
6990 * the APE to assume OS absent status.
6991 */
6992 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
Matt Carlsonb2aee152008-11-03 16:51:11 -08006993
Matt Carlsondc6d0742010-09-15 08:59:55 +00006994 if (device_may_wakeup(&tp->pdev->dev) &&
6995 (tp->tg3_flags & TG3_FLAG_WOL_ENABLE)) {
6996 tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
6997 TG3_APE_HOST_WOL_SPEED_AUTO);
6998 apedata = TG3_APE_HOST_DRVR_STATE_WOL;
6999 } else
7000 apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
7001
7002 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
7003
Matt Carlson33f401a2010-04-05 10:19:27 +00007004 event = APE_EVENT_STATUS_STATE_UNLOAD;
7005 break;
7006 case RESET_KIND_SUSPEND:
7007 event = APE_EVENT_STATUS_STATE_SUSPEND;
7008 break;
7009 default:
7010 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -07007011 }
7012
7013 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
7014
7015 tg3_ape_send_event(tp, event);
7016}
7017
Michael Chane6af3012005-04-21 17:12:05 -07007018/* tp->lock is held. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007019static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
7020{
David S. Millerf49639e2006-06-09 11:58:36 -07007021 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
7022 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007023
7024 if (tp->tg3_flags2 & TG3_FLG2_ASF_NEW_HANDSHAKE) {
7025 switch (kind) {
7026 case RESET_KIND_INIT:
7027 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7028 DRV_STATE_START);
7029 break;
7030
7031 case RESET_KIND_SHUTDOWN:
7032 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7033 DRV_STATE_UNLOAD);
7034 break;
7035
7036 case RESET_KIND_SUSPEND:
7037 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7038 DRV_STATE_SUSPEND);
7039 break;
7040
7041 default:
7042 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007043 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007044 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07007045
7046 if (kind == RESET_KIND_INIT ||
7047 kind == RESET_KIND_SUSPEND)
7048 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007049}
7050
7051/* tp->lock is held. */
7052static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
7053{
7054 if (tp->tg3_flags2 & TG3_FLG2_ASF_NEW_HANDSHAKE) {
7055 switch (kind) {
7056 case RESET_KIND_INIT:
7057 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7058 DRV_STATE_START_DONE);
7059 break;
7060
7061 case RESET_KIND_SHUTDOWN:
7062 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7063 DRV_STATE_UNLOAD_DONE);
7064 break;
7065
7066 default:
7067 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007069 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07007070
7071 if (kind == RESET_KIND_SHUTDOWN)
7072 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007073}
7074
7075/* tp->lock is held. */
7076static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
7077{
7078 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) {
7079 switch (kind) {
7080 case RESET_KIND_INIT:
7081 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7082 DRV_STATE_START);
7083 break;
7084
7085 case RESET_KIND_SHUTDOWN:
7086 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7087 DRV_STATE_UNLOAD);
7088 break;
7089
7090 case RESET_KIND_SUSPEND:
7091 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7092 DRV_STATE_SUSPEND);
7093 break;
7094
7095 default:
7096 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007097 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007098 }
7099}
7100
Michael Chan7a6f4362006-09-27 16:03:31 -07007101static int tg3_poll_fw(struct tg3 *tp)
7102{
7103 int i;
7104 u32 val;
7105
Michael Chanb5d37722006-09-27 16:06:21 -07007106 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Gary Zambrano0ccead12006-11-14 16:34:00 -08007107 /* Wait up to 20ms for init done. */
7108 for (i = 0; i < 200; i++) {
Michael Chanb5d37722006-09-27 16:06:21 -07007109 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
7110 return 0;
Gary Zambrano0ccead12006-11-14 16:34:00 -08007111 udelay(100);
Michael Chanb5d37722006-09-27 16:06:21 -07007112 }
7113 return -ENODEV;
7114 }
7115
Michael Chan7a6f4362006-09-27 16:03:31 -07007116 /* Wait for firmware initialization to complete. */
7117 for (i = 0; i < 100000; i++) {
7118 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
7119 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
7120 break;
7121 udelay(10);
7122 }
7123
7124 /* Chip might not be fitted with firmware. Some Sun onboard
7125 * parts are configured like that. So don't signal the timeout
7126 * of the above loop as an error, but do report the lack of
7127 * running firmware once.
7128 */
7129 if (i >= 100000 &&
7130 !(tp->tg3_flags2 & TG3_FLG2_NO_FWARE_REPORTED)) {
7131 tp->tg3_flags2 |= TG3_FLG2_NO_FWARE_REPORTED;
7132
Joe Perches05dbe002010-02-17 19:44:19 +00007133 netdev_info(tp->dev, "No firmware running\n");
Michael Chan7a6f4362006-09-27 16:03:31 -07007134 }
7135
Matt Carlson6b10c162010-02-12 14:47:08 +00007136 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
7137 /* The 57765 A0 needs a little more
7138 * time to do some important work.
7139 */
7140 mdelay(10);
7141 }
7142
Michael Chan7a6f4362006-09-27 16:03:31 -07007143 return 0;
7144}
7145
Michael Chanee6a99b2007-07-18 21:49:10 -07007146/* Save PCI command register before chip reset */
7147static void tg3_save_pci_state(struct tg3 *tp)
7148{
Matt Carlson8a6eac92007-10-21 16:17:55 -07007149 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007150}
7151
7152/* Restore PCI state after chip reset */
7153static void tg3_restore_pci_state(struct tg3 *tp)
7154{
7155 u32 val;
7156
7157 /* Re-enable indirect register accesses. */
7158 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
7159 tp->misc_host_ctrl);
7160
7161 /* Set MAX PCI retry to zero. */
7162 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
7163 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
7164 (tp->tg3_flags & TG3_FLAG_PCIX_MODE))
7165 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07007166 /* Allow reads and writes to the APE register and memory space. */
7167 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
7168 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00007169 PCISTATE_ALLOW_APE_SHMEM_WR |
7170 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07007171 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
7172
Matt Carlson8a6eac92007-10-21 16:17:55 -07007173 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007174
Matt Carlsonfcb389d2008-11-03 16:55:44 -08007175 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785) {
7176 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)
Matt Carlsoncf790032010-11-24 08:31:48 +00007177 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlsonfcb389d2008-11-03 16:55:44 -08007178 else {
7179 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
7180 tp->pci_cacheline_sz);
7181 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
7182 tp->pci_lat_timer);
7183 }
Michael Chan114342f2007-10-15 02:12:26 -07007184 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08007185
Michael Chanee6a99b2007-07-18 21:49:10 -07007186 /* Make sure PCI-X relaxed ordering bit is clear. */
Matt Carlson52f44902008-11-21 17:17:04 -08007187 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
Matt Carlson9974a352007-10-07 23:27:28 -07007188 u16 pcix_cmd;
7189
7190 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7191 &pcix_cmd);
7192 pcix_cmd &= ~PCI_X_CMD_ERO;
7193 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7194 pcix_cmd);
7195 }
Michael Chanee6a99b2007-07-18 21:49:10 -07007196
7197 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007198
7199 /* Chip reset on 5780 will reset MSI enable bit,
7200 * so need to restore it.
7201 */
7202 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
7203 u16 ctrl;
7204
7205 pci_read_config_word(tp->pdev,
7206 tp->msi_cap + PCI_MSI_FLAGS,
7207 &ctrl);
7208 pci_write_config_word(tp->pdev,
7209 tp->msi_cap + PCI_MSI_FLAGS,
7210 ctrl | PCI_MSI_FLAGS_ENABLE);
7211 val = tr32(MSGINT_MODE);
7212 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
7213 }
7214 }
7215}
7216
Linus Torvalds1da177e2005-04-16 15:20:36 -07007217static void tg3_stop_fw(struct tg3 *);
7218
7219/* tp->lock is held. */
7220static int tg3_chip_reset(struct tg3 *tp)
7221{
7222 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07007223 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00007224 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007225
David S. Millerf49639e2006-06-09 11:58:36 -07007226 tg3_nvram_lock(tp);
7227
Matt Carlson77b483f2008-08-15 14:07:24 -07007228 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
7229
David S. Millerf49639e2006-06-09 11:58:36 -07007230 /* No matching tg3_nvram_unlock() after this because
7231 * chip reset below will undo the nvram lock.
7232 */
7233 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007234
Michael Chanee6a99b2007-07-18 21:49:10 -07007235 /* GRC_MISC_CFG core clock reset will clear the memory
7236 * enable bit in PCI register 4 and the MSI enable bit
7237 * on some chips, so we save relevant registers here.
7238 */
7239 tg3_save_pci_state(tp);
7240
Michael Chand9ab5ad12006-03-20 22:27:35 -08007241 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08007242 (tp->tg3_flags3 & TG3_FLG3_5755_PLUS))
Michael Chand9ab5ad12006-03-20 22:27:35 -08007243 tw32(GRC_FASTBOOT_PC, 0);
7244
Linus Torvalds1da177e2005-04-16 15:20:36 -07007245 /*
7246 * We must avoid the readl() that normally takes place.
7247 * It locks machines, causes machine checks, and other
7248 * fun things. So, temporarily disable the 5701
7249 * hardware workaround, while we do the reset.
7250 */
Michael Chan1ee582d2005-08-09 20:16:46 -07007251 write_op = tp->write32;
7252 if (write_op == tg3_write_flush_reg32)
7253 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007254
Michael Chand18edcb2007-03-24 20:57:11 -07007255 /* Prevent the irq handler from reading or writing PCI registers
7256 * during chip reset when the memory enable bit in the PCI command
7257 * register may be cleared. The chip does not generate interrupt
7258 * at this time, but the irq handler may still be called due to irq
7259 * sharing or irqpoll.
7260 */
7261 tp->tg3_flags |= TG3_FLAG_CHIP_RESETTING;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007262 for (i = 0; i < tp->irq_cnt; i++) {
7263 struct tg3_napi *tnapi = &tp->napi[i];
7264 if (tnapi->hw_status) {
7265 tnapi->hw_status->status = 0;
7266 tnapi->hw_status->status_tag = 0;
7267 }
7268 tnapi->last_tag = 0;
7269 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07007270 }
Michael Chand18edcb2007-03-24 20:57:11 -07007271 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00007272
7273 for (i = 0; i < tp->irq_cnt; i++)
7274 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07007275
Matt Carlson255ca312009-08-25 10:07:27 +00007276 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7277 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7278 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
7279 }
7280
Linus Torvalds1da177e2005-04-16 15:20:36 -07007281 /* do the reset */
7282 val = GRC_MISC_CFG_CORECLK_RESET;
7283
7284 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
Matt Carlson88075d92010-08-02 11:25:58 +00007285 /* Force PCIe 1.0a mode */
7286 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlson1407deb2011-04-05 14:22:44 +00007287 !(tp->tg3_flags3 & TG3_FLG3_57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00007288 tr32(TG3_PCIE_PHY_TSTCTL) ==
7289 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
7290 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
7291
Linus Torvalds1da177e2005-04-16 15:20:36 -07007292 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
7293 tw32(GRC_MISC_CFG, (1 << 29));
7294 val |= (1 << 29);
7295 }
7296 }
7297
Michael Chanb5d37722006-09-27 16:06:21 -07007298 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7299 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
7300 tw32(GRC_VCPU_EXT_CTRL,
7301 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
7302 }
7303
Matt Carlsonf37500d2010-08-02 11:25:59 +00007304 /* Manage gphy power for all CPMU absent PCIe devices. */
7305 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
7306 !(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007307 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00007308
Linus Torvalds1da177e2005-04-16 15:20:36 -07007309 tw32(GRC_MISC_CFG, val);
7310
Michael Chan1ee582d2005-08-09 20:16:46 -07007311 /* restore 5701 hardware bug workaround write method */
7312 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007313
7314 /* Unfortunately, we have to delay before the PCI read back.
7315 * Some 575X chips even will not respond to a PCI cfg access
7316 * when the reset command is given to the chip.
7317 *
7318 * How do these hardware designers expect things to work
7319 * properly if the PCI write is posted for a long period
7320 * of time? It is always necessary to have some method by
7321 * which a register read back can occur to push the write
7322 * out which does the reset.
7323 *
7324 * For most tg3 variants the trick below was working.
7325 * Ho hum...
7326 */
7327 udelay(120);
7328
7329 /* Flush PCI posted writes. The normal MMIO registers
7330 * are inaccessible at this time so this is the only
7331 * way to make this reliably (actually, this is no longer
7332 * the case, see above). I tried to use indirect
7333 * register read/write but this upset some 5701 variants.
7334 */
7335 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
7336
7337 udelay(120);
7338
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007339 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) && tp->pcie_cap) {
Matt Carlsone7126992009-08-25 10:08:16 +00007340 u16 val16;
7341
Linus Torvalds1da177e2005-04-16 15:20:36 -07007342 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
7343 int i;
7344 u32 cfg_val;
7345
7346 /* Wait for link training to complete. */
7347 for (i = 0; i < 5000; i++)
7348 udelay(100);
7349
7350 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
7351 pci_write_config_dword(tp->pdev, 0xc4,
7352 cfg_val | (1 << 15));
7353 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007354
Matt Carlsone7126992009-08-25 10:08:16 +00007355 /* Clear the "no snoop" and "relaxed ordering" bits. */
7356 pci_read_config_word(tp->pdev,
7357 tp->pcie_cap + PCI_EXP_DEVCTL,
7358 &val16);
7359 val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
7360 PCI_EXP_DEVCTL_NOSNOOP_EN);
7361 /*
7362 * Older PCIe devices only support the 128 byte
7363 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007364 */
Matt Carlson6de34cb2010-08-02 11:25:55 +00007365 if (!(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT))
Matt Carlsone7126992009-08-25 10:08:16 +00007366 val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007367 pci_write_config_word(tp->pdev,
7368 tp->pcie_cap + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007369 val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007370
Matt Carlsoncf790032010-11-24 08:31:48 +00007371 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007372
7373 /* Clear error status */
7374 pci_write_config_word(tp->pdev,
7375 tp->pcie_cap + PCI_EXP_DEVSTA,
7376 PCI_EXP_DEVSTA_CED |
7377 PCI_EXP_DEVSTA_NFED |
7378 PCI_EXP_DEVSTA_FED |
7379 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007380 }
7381
Michael Chanee6a99b2007-07-18 21:49:10 -07007382 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007383
Matt Carlsone64de4e2011-04-13 11:05:05 +00007384 tp->tg3_flags &= ~(TG3_FLAG_CHIP_RESETTING |
7385 TG3_FLAG_ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07007386
Michael Chanee6a99b2007-07-18 21:49:10 -07007387 val = 0;
7388 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
Michael Chan4cf78e42005-07-25 12:29:19 -07007389 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07007390 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007391
7392 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
7393 tg3_stop_fw(tp);
7394 tw32(0x5000, 0x400);
7395 }
7396
7397 tw32(GRC_MODE, tp->grc_mode);
7398
7399 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007400 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007401
7402 tw32(0xc4, val | (1 << 15));
7403 }
7404
7405 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
7406 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
7407 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
7408 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
7409 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
7410 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7411 }
7412
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007413 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
7414 tp->mac_mode = MAC_MODE_APE_TX_EN |
7415 MAC_MODE_APE_RX_EN |
7416 MAC_MODE_TDE_ENABLE;
7417
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007418 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007419 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
7420 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007421 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007422 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
7423 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007424 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007425 val = 0;
7426
7427 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007428 udelay(40);
7429
Matt Carlson77b483f2008-08-15 14:07:24 -07007430 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
7431
Michael Chan7a6f4362006-09-27 16:03:31 -07007432 err = tg3_poll_fw(tp);
7433 if (err)
7434 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007435
Matt Carlson0a9140c2009-08-28 12:27:50 +00007436 tg3_mdio_start(tp);
7437
Linus Torvalds1da177e2005-04-16 15:20:36 -07007438 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007439 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
7440 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlson1407deb2011-04-05 14:22:44 +00007441 !(tp->tg3_flags3 & TG3_FLG3_57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007442 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007443
7444 tw32(0x7c00, val | (1 << 25));
7445 }
7446
Matt Carlsond78b59f2011-04-05 14:22:46 +00007447 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
7448 val = tr32(TG3_CPMU_CLCK_ORIDE);
7449 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
7450 }
7451
Linus Torvalds1da177e2005-04-16 15:20:36 -07007452 /* Reprobe ASF enable state. */
7453 tp->tg3_flags &= ~TG3_FLAG_ENABLE_ASF;
7454 tp->tg3_flags2 &= ~TG3_FLG2_ASF_NEW_HANDSHAKE;
7455 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
7456 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
7457 u32 nic_cfg;
7458
7459 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
7460 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
7461 tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
Matt Carlson4ba526c2008-08-15 14:10:04 -07007462 tp->last_event_jiffies = jiffies;
John W. Linvillecbf46852005-04-21 17:01:29 -07007463 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007464 tp->tg3_flags2 |= TG3_FLG2_ASF_NEW_HANDSHAKE;
7465 }
7466 }
7467
7468 return 0;
7469}
7470
7471/* tp->lock is held. */
7472static void tg3_stop_fw(struct tg3 *tp)
7473{
Matt Carlson0d3031d2007-10-10 18:02:43 -07007474 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
7475 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07007476 /* Wait for RX cpu to ACK the previous event. */
7477 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007478
7479 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007480
7481 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007482
Matt Carlson7c5026a2008-05-02 16:49:29 -07007483 /* Wait for RX cpu to ACK this event. */
7484 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007485 }
7486}
7487
7488/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07007489static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007490{
7491 int err;
7492
7493 tg3_stop_fw(tp);
7494
Michael Chan944d9802005-05-29 14:57:48 -07007495 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007496
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007497 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007498 err = tg3_chip_reset(tp);
7499
Matt Carlsondaba2a62009-04-20 06:58:52 +00007500 __tg3_set_mac_addr(tp, 0);
7501
Michael Chan944d9802005-05-29 14:57:48 -07007502 tg3_write_sig_legacy(tp, kind);
7503 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007504
7505 if (err)
7506 return err;
7507
7508 return 0;
7509}
7510
Linus Torvalds1da177e2005-04-16 15:20:36 -07007511#define RX_CPU_SCRATCH_BASE 0x30000
7512#define RX_CPU_SCRATCH_SIZE 0x04000
7513#define TX_CPU_SCRATCH_BASE 0x34000
7514#define TX_CPU_SCRATCH_SIZE 0x04000
7515
7516/* tp->lock is held. */
7517static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
7518{
7519 int i;
7520
Eric Sesterhenn5d9428d2006-04-02 13:52:48 +02007521 BUG_ON(offset == TX_CPU_BASE &&
7522 (tp->tg3_flags2 & TG3_FLG2_5705_PLUS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007523
Michael Chanb5d37722006-09-27 16:06:21 -07007524 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7525 u32 val = tr32(GRC_VCPU_EXT_CTRL);
7526
7527 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
7528 return 0;
7529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007530 if (offset == RX_CPU_BASE) {
7531 for (i = 0; i < 10000; i++) {
7532 tw32(offset + CPU_STATE, 0xffffffff);
7533 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7534 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7535 break;
7536 }
7537
7538 tw32(offset + CPU_STATE, 0xffffffff);
7539 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
7540 udelay(10);
7541 } else {
7542 for (i = 0; i < 10000; i++) {
7543 tw32(offset + CPU_STATE, 0xffffffff);
7544 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7545 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7546 break;
7547 }
7548 }
7549
7550 if (i >= 10000) {
Joe Perches05dbe002010-02-17 19:44:19 +00007551 netdev_err(tp->dev, "%s timed out, %s CPU\n",
7552 __func__, offset == RX_CPU_BASE ? "RX" : "TX");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007553 return -ENODEV;
7554 }
Michael Chanec41c7d2006-01-17 02:40:55 -08007555
7556 /* Clear firmware's nvram arbitration. */
7557 if (tp->tg3_flags & TG3_FLAG_NVRAM)
7558 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007559 return 0;
7560}
7561
7562struct fw_info {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007563 unsigned int fw_base;
7564 unsigned int fw_len;
7565 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007566};
7567
7568/* tp->lock is held. */
7569static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base, u32 cpu_scratch_base,
7570 int cpu_scratch_size, struct fw_info *info)
7571{
Michael Chanec41c7d2006-01-17 02:40:55 -08007572 int err, lock_err, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007573 void (*write_op)(struct tg3 *, u32, u32);
7574
7575 if (cpu_base == TX_CPU_BASE &&
7576 (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007577 netdev_err(tp->dev,
7578 "%s: Trying to load TX cpu firmware which is 5705\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007579 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007580 return -EINVAL;
7581 }
7582
7583 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
7584 write_op = tg3_write_mem;
7585 else
7586 write_op = tg3_write_indirect_reg32;
7587
Michael Chan1b628152005-05-29 14:59:49 -07007588 /* It is possible that bootcode is still loading at this point.
7589 * Get the nvram lock first before halting the cpu.
7590 */
Michael Chanec41c7d2006-01-17 02:40:55 -08007591 lock_err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007592 err = tg3_halt_cpu(tp, cpu_base);
Michael Chanec41c7d2006-01-17 02:40:55 -08007593 if (!lock_err)
7594 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007595 if (err)
7596 goto out;
7597
7598 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
7599 write_op(tp, cpu_scratch_base + i, 0);
7600 tw32(cpu_base + CPU_STATE, 0xffffffff);
7601 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007602 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007603 write_op(tp, (cpu_scratch_base +
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007604 (info->fw_base & 0xffff) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07007605 (i * sizeof(u32))),
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007606 be32_to_cpu(info->fw_data[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007607
7608 err = 0;
7609
7610out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007611 return err;
7612}
7613
7614/* tp->lock is held. */
7615static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
7616{
7617 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007618 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007619 int err, i;
7620
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007621 fw_data = (void *)tp->fw->data;
7622
7623 /* Firmware blob starts with version numbers, followed by
7624 start address and length. We are setting complete length.
7625 length = end_address_of_bss - start_address_of_text.
7626 Remainder is the blob to be loaded contiguously
7627 from start address. */
7628
7629 info.fw_base = be32_to_cpu(fw_data[1]);
7630 info.fw_len = tp->fw->size - 12;
7631 info.fw_data = &fw_data[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007632
7633 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
7634 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
7635 &info);
7636 if (err)
7637 return err;
7638
7639 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
7640 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
7641 &info);
7642 if (err)
7643 return err;
7644
7645 /* Now startup only the RX cpu. */
7646 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007647 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007648
7649 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007650 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007651 break;
7652 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7653 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007654 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007655 udelay(1000);
7656 }
7657 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007658 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
7659 "should be %08x\n", __func__,
Joe Perches05dbe002010-02-17 19:44:19 +00007660 tr32(RX_CPU_BASE + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007661 return -ENODEV;
7662 }
7663 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7664 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
7665
7666 return 0;
7667}
7668
Linus Torvalds1da177e2005-04-16 15:20:36 -07007669/* 5705 needs a special version of the TSO firmware. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007670
7671/* tp->lock is held. */
7672static int tg3_load_tso_firmware(struct tg3 *tp)
7673{
7674 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007675 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007676 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
7677 int err, i;
7678
7679 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
7680 return 0;
7681
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007682 fw_data = (void *)tp->fw->data;
7683
7684 /* Firmware blob starts with version numbers, followed by
7685 start address and length. We are setting complete length.
7686 length = end_address_of_bss - start_address_of_text.
7687 Remainder is the blob to be loaded contiguously
7688 from start address. */
7689
7690 info.fw_base = be32_to_cpu(fw_data[1]);
7691 cpu_scratch_size = tp->fw_len;
7692 info.fw_len = tp->fw->size - 12;
7693 info.fw_data = &fw_data[3];
7694
Linus Torvalds1da177e2005-04-16 15:20:36 -07007695 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007696 cpu_base = RX_CPU_BASE;
7697 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007698 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007699 cpu_base = TX_CPU_BASE;
7700 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
7701 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
7702 }
7703
7704 err = tg3_load_firmware_cpu(tp, cpu_base,
7705 cpu_scratch_base, cpu_scratch_size,
7706 &info);
7707 if (err)
7708 return err;
7709
7710 /* Now startup the cpu. */
7711 tw32(cpu_base + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007712 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007713
7714 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007715 if (tr32(cpu_base + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007716 break;
7717 tw32(cpu_base + CPU_STATE, 0xffffffff);
7718 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007719 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007720 udelay(1000);
7721 }
7722 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007723 netdev_err(tp->dev,
7724 "%s fails to set CPU PC, is %08x should be %08x\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007725 __func__, tr32(cpu_base + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007726 return -ENODEV;
7727 }
7728 tw32(cpu_base + CPU_STATE, 0xffffffff);
7729 tw32_f(cpu_base + CPU_MODE, 0x00000000);
7730 return 0;
7731}
7732
Linus Torvalds1da177e2005-04-16 15:20:36 -07007733
Linus Torvalds1da177e2005-04-16 15:20:36 -07007734static int tg3_set_mac_addr(struct net_device *dev, void *p)
7735{
7736 struct tg3 *tp = netdev_priv(dev);
7737 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07007738 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007739
Michael Chanf9804dd2005-09-27 12:13:10 -07007740 if (!is_valid_ether_addr(addr->sa_data))
7741 return -EINVAL;
7742
Linus Torvalds1da177e2005-04-16 15:20:36 -07007743 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7744
Michael Chane75f7c92006-03-20 21:33:26 -08007745 if (!netif_running(dev))
7746 return 0;
7747
Michael Chan58712ef2006-04-29 18:58:01 -07007748 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) {
Michael Chan986e0ae2007-05-05 12:10:20 -07007749 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07007750
Michael Chan986e0ae2007-05-05 12:10:20 -07007751 addr0_high = tr32(MAC_ADDR_0_HIGH);
7752 addr0_low = tr32(MAC_ADDR_0_LOW);
7753 addr1_high = tr32(MAC_ADDR_1_HIGH);
7754 addr1_low = tr32(MAC_ADDR_1_LOW);
7755
7756 /* Skip MAC addr 1 if ASF is using it. */
7757 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
7758 !(addr1_high == 0 && addr1_low == 0))
7759 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07007760 }
Michael Chan986e0ae2007-05-05 12:10:20 -07007761 spin_lock_bh(&tp->lock);
7762 __tg3_set_mac_addr(tp, skip_mac_1);
7763 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007764
Michael Chanb9ec6c12006-07-25 16:37:27 -07007765 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007766}
7767
7768/* tp->lock is held. */
7769static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
7770 dma_addr_t mapping, u32 maxlen_flags,
7771 u32 nic_addr)
7772{
7773 tg3_write_mem(tp,
7774 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
7775 ((u64) mapping >> 32));
7776 tg3_write_mem(tp,
7777 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
7778 ((u64) mapping & 0xffffffff));
7779 tg3_write_mem(tp,
7780 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
7781 maxlen_flags);
7782
7783 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7784 tg3_write_mem(tp,
7785 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
7786 nic_addr);
7787}
7788
7789static void __tg3_set_rx_mode(struct net_device *);
Michael Chand244c892005-07-05 14:42:33 -07007790static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07007791{
Matt Carlsonb6080e12009-09-01 13:12:00 +00007792 int i;
7793
Matt Carlson19cfaec2009-12-03 08:36:20 +00007794 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007795 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
7796 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
7797 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007798 } else {
7799 tw32(HOSTCC_TXCOL_TICKS, 0);
7800 tw32(HOSTCC_TXMAX_FRAMES, 0);
7801 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007802 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007803
Matt Carlson20d73752010-07-11 09:31:41 +00007804 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00007805 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
7806 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
7807 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
7808 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007809 tw32(HOSTCC_RXCOL_TICKS, 0);
7810 tw32(HOSTCC_RXMAX_FRAMES, 0);
7811 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07007812 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007813
David S. Miller15f98502005-05-18 22:49:26 -07007814 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7815 u32 val = ec->stats_block_coalesce_usecs;
7816
Matt Carlsonb6080e12009-09-01 13:12:00 +00007817 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
7818 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
7819
David S. Miller15f98502005-05-18 22:49:26 -07007820 if (!netif_carrier_ok(tp->dev))
7821 val = 0;
7822
7823 tw32(HOSTCC_STAT_COAL_TICKS, val);
7824 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007825
7826 for (i = 0; i < tp->irq_cnt - 1; i++) {
7827 u32 reg;
7828
7829 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
7830 tw32(reg, ec->rx_coalesce_usecs);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007831 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
7832 tw32(reg, ec->rx_max_coalesced_frames);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007833 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
7834 tw32(reg, ec->rx_max_coalesced_frames_irq);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007835
7836 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS) {
7837 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
7838 tw32(reg, ec->tx_coalesce_usecs);
7839 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
7840 tw32(reg, ec->tx_max_coalesced_frames);
7841 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
7842 tw32(reg, ec->tx_max_coalesced_frames_irq);
7843 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007844 }
7845
7846 for (; i < tp->irq_max - 1; i++) {
7847 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007848 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007849 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007850
7851 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS) {
7852 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
7853 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
7854 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
7855 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007856 }
David S. Miller15f98502005-05-18 22:49:26 -07007857}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007858
7859/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00007860static void tg3_rings_reset(struct tg3 *tp)
7861{
7862 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007863 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007864 struct tg3_napi *tnapi = &tp->napi[0];
7865
7866 /* Disable all transmit rings but the first. */
7867 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7868 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlson0a58d662011-04-05 14:22:45 +00007869 else if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)
Matt Carlson3d377282010-10-14 10:37:39 +00007870 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
Matt Carlsonb703df62009-12-03 08:36:21 +00007871 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
7872 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007873 else
7874 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7875
7876 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7877 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
7878 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
7879 BDINFO_FLAGS_DISABLED);
7880
7881
7882 /* Disable all receive return rings but the first. */
Matt Carlson0a58d662011-04-05 14:22:45 +00007883 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007884 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
7885 else if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00007886 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00007887 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
7888 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson2d31eca2009-09-01 12:53:31 +00007889 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
7890 else
7891 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7892
7893 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7894 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
7895 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
7896 BDINFO_FLAGS_DISABLED);
7897
7898 /* Disable interrupts */
7899 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
7900
7901 /* Zero mailbox registers. */
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007902 if (tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00007903 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007904 tp->napi[i].tx_prod = 0;
7905 tp->napi[i].tx_cons = 0;
Matt Carlsonc2353a32010-01-20 16:58:08 +00007906 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
7907 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007908 tw32_rx_mbox(tp->napi[i].consmbox, 0);
7909 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
7910 }
Matt Carlsonc2353a32010-01-20 16:58:08 +00007911 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS))
7912 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007913 } else {
7914 tp->napi[0].tx_prod = 0;
7915 tp->napi[0].tx_cons = 0;
7916 tw32_mailbox(tp->napi[0].prodmbox, 0);
7917 tw32_rx_mbox(tp->napi[0].consmbox, 0);
7918 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007919
7920 /* Make sure the NIC-based send BD rings are disabled. */
7921 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7922 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
7923 for (i = 0; i < 16; i++)
7924 tw32_tx_mbox(mbox + i * 8, 0);
7925 }
7926
7927 txrcb = NIC_SRAM_SEND_RCB;
7928 rxrcb = NIC_SRAM_RCV_RET_RCB;
7929
7930 /* Clear status block in ram. */
7931 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7932
7933 /* Set status block DMA address */
7934 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
7935 ((u64) tnapi->status_mapping >> 32));
7936 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
7937 ((u64) tnapi->status_mapping & 0xffffffff));
7938
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007939 if (tnapi->tx_ring) {
7940 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7941 (TG3_TX_RING_SIZE <<
7942 BDINFO_FLAGS_MAXLEN_SHIFT),
7943 NIC_SRAM_TX_BUFFER_DESC);
7944 txrcb += TG3_BDINFO_SIZE;
7945 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007946
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007947 if (tnapi->rx_rcb) {
7948 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00007949 (tp->rx_ret_ring_mask + 1) <<
7950 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007951 rxrcb += TG3_BDINFO_SIZE;
7952 }
7953
7954 stblk = HOSTCC_STATBLCK_RING1;
7955
7956 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
7957 u64 mapping = (u64)tnapi->status_mapping;
7958 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
7959 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
7960
7961 /* Clear status block in ram. */
7962 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7963
Matt Carlson19cfaec2009-12-03 08:36:20 +00007964 if (tnapi->tx_ring) {
7965 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7966 (TG3_TX_RING_SIZE <<
7967 BDINFO_FLAGS_MAXLEN_SHIFT),
7968 NIC_SRAM_TX_BUFFER_DESC);
7969 txrcb += TG3_BDINFO_SIZE;
7970 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007971
7972 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00007973 ((tp->rx_ret_ring_mask + 1) <<
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007974 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
7975
7976 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007977 rxrcb += TG3_BDINFO_SIZE;
7978 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007979}
7980
Matt Carlsoneb07a942011-04-20 07:57:36 +00007981static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
7982{
7983 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
7984
7985 if (!(tp->tg3_flags2 & TG3_FLG2_5750_PLUS) ||
7986 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
7987 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
7988 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
7989 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
7990 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
7991 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787)
7992 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
7993 else
7994 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
7995
7996 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
7997 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
7998
7999 val = min(nic_rep_thresh, host_rep_thresh);
8000 tw32(RCVBDI_STD_THRESH, val);
8001
8002 if (tp->tg3_flags3 & TG3_FLG3_57765_PLUS)
8003 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
8004
8005 if (!(tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) ||
8006 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
8007 return;
8008
8009 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
8010 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
8011 else
8012 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5717;
8013
8014 host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
8015
8016 val = min(bdcache_maxcnt / 2, host_rep_thresh);
8017 tw32(RCVBDI_JUMBO_THRESH, val);
8018
8019 if (tp->tg3_flags3 & TG3_FLG3_57765_PLUS)
8020 tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
8021}
8022
Matt Carlson2d31eca2009-09-01 12:53:31 +00008023/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008024static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008025{
8026 u32 val, rdmac_mode;
8027 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00008028 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008029
8030 tg3_disable_ints(tp);
8031
8032 tg3_stop_fw(tp);
8033
8034 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
8035
Matt Carlson859a588792010-04-05 10:19:28 +00008036 if (tp->tg3_flags & TG3_FLAG_INIT_COMPLETE)
Michael Chane6de8ad2005-05-05 14:42:41 -07008037 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008038
Matt Carlson699c0192010-12-06 08:28:51 +00008039 /* Enable MAC control of LPI */
8040 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
8041 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL,
8042 TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
8043 TG3_CPMU_EEE_LNKIDL_UART_IDL);
8044
8045 tw32_f(TG3_CPMU_EEE_CTRL,
8046 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
8047
Matt Carlsona386b902010-12-06 08:28:53 +00008048 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
8049 TG3_CPMU_EEEMD_LPI_IN_TX |
8050 TG3_CPMU_EEEMD_LPI_IN_RX |
8051 TG3_CPMU_EEEMD_EEE_ENABLE;
8052
8053 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8054 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
8055
8056 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
8057 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
8058
8059 tw32_f(TG3_CPMU_EEE_MODE, val);
8060
8061 tw32_f(TG3_CPMU_EEE_DBTMR1,
8062 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
8063 TG3_CPMU_DBTMR1_LNKIDLE_2047US);
8064
8065 tw32_f(TG3_CPMU_EEE_DBTMR2,
Matt Carlsond7f2ab22011-01-25 15:58:56 +00008066 TG3_CPMU_DBTMR2_APE_TX_2047US |
Matt Carlsona386b902010-12-06 08:28:53 +00008067 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
Matt Carlson699c0192010-12-06 08:28:51 +00008068 }
8069
Matt Carlson603f1172010-02-12 14:47:10 +00008070 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08008071 tg3_phy_reset(tp);
8072
Linus Torvalds1da177e2005-04-16 15:20:36 -07008073 err = tg3_chip_reset(tp);
8074 if (err)
8075 return err;
8076
8077 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
8078
Matt Carlsonbcb37f62008-11-03 16:52:09 -08008079 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008080 val = tr32(TG3_CPMU_CTRL);
8081 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
8082 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08008083
8084 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8085 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8086 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8087 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
8088
8089 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
8090 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
8091 val |= CPMU_LNK_AWARE_MACCLK_6_25;
8092 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
8093
8094 val = tr32(TG3_CPMU_HST_ACC);
8095 val &= ~CPMU_HST_ACC_MACCLK_MASK;
8096 val |= CPMU_HST_ACC_MACCLK_6_25;
8097 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07008098 }
8099
Matt Carlson33466d932009-04-20 06:57:41 +00008100 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
8101 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
8102 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
8103 PCIE_PWR_MGMT_L1_THRESH_4MS;
8104 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00008105
8106 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
8107 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
8108
8109 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d932009-04-20 06:57:41 +00008110
Matt Carlsonf40386c2009-11-02 14:24:02 +00008111 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
8112 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00008113 }
8114
Matt Carlson614b0592010-01-20 16:58:02 +00008115 if (tp->tg3_flags3 & TG3_FLG3_L1PLLPD_EN) {
8116 u32 grc_mode = tr32(GRC_MODE);
8117
8118 /* Access the lower 1K of PL PCIE block registers. */
8119 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8120 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
8121
8122 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
8123 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
8124 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
8125
8126 tw32(GRC_MODE, grc_mode);
8127 }
8128
Matt Carlson5093eed2010-11-24 08:31:45 +00008129 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
8130 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
8131 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00008132
Matt Carlson5093eed2010-11-24 08:31:45 +00008133 /* Access the lower 1K of PL PCIE block registers. */
8134 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8135 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00008136
Matt Carlson5093eed2010-11-24 08:31:45 +00008137 val = tr32(TG3_PCIE_TLDLPL_PORT +
8138 TG3_PCIE_PL_LO_PHYCTL5);
8139 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
8140 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00008141
Matt Carlson5093eed2010-11-24 08:31:45 +00008142 tw32(GRC_MODE, grc_mode);
8143 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00008144
8145 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8146 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8147 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8148 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00008149 }
8150
Linus Torvalds1da177e2005-04-16 15:20:36 -07008151 /* This works around an issue with Athlon chipsets on
8152 * B3 tigon3 silicon. This bit has no effect on any
8153 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07008154 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008155 */
Matt Carlson795d01c2007-10-07 23:28:17 -07008156 if (!(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT)) {
8157 if (!(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
8158 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
8159 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
8160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008161
8162 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
8163 (tp->tg3_flags & TG3_FLAG_PCIX_MODE)) {
8164 val = tr32(TG3PCI_PCISTATE);
8165 val |= PCISTATE_RETRY_SAME_DMA;
8166 tw32(TG3PCI_PCISTATE, val);
8167 }
8168
Matt Carlson0d3031d2007-10-10 18:02:43 -07008169 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
8170 /* Allow reads and writes to the
8171 * APE register and memory space.
8172 */
8173 val = tr32(TG3PCI_PCISTATE);
8174 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00008175 PCISTATE_ALLOW_APE_SHMEM_WR |
8176 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008177 tw32(TG3PCI_PCISTATE, val);
8178 }
8179
Linus Torvalds1da177e2005-04-16 15:20:36 -07008180 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
8181 /* Enable some hw fixes. */
8182 val = tr32(TG3PCI_MSI_DATA);
8183 val |= (1 << 26) | (1 << 28) | (1 << 29);
8184 tw32(TG3PCI_MSI_DATA, val);
8185 }
8186
8187 /* Descriptor ring init may make accesses to the
8188 * NIC SRAM area to setup the TX descriptors, so we
8189 * can only do this after the hardware has been
8190 * successfully reset.
8191 */
Michael Chan32d8c572006-07-25 16:38:29 -07008192 err = tg3_init_rings(tp);
8193 if (err)
8194 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008195
Matt Carlson1407deb2011-04-05 14:22:44 +00008196 if (tp->tg3_flags3 & TG3_FLG3_57765_PLUS) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008197 val = tr32(TG3PCI_DMA_RW_CTRL) &
8198 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Matt Carlson1a319022010-04-12 06:58:25 +00008199 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
8200 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlson0aebff42011-04-25 12:42:45 +00008201 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57765 &&
8202 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8203 val |= DMA_RWCTRL_TAGGED_STAT_WA;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008204 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
8205 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
8206 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008207 /* This value is determined during the probe time DMA
8208 * engine test, tg3_test_dma.
8209 */
8210 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
8211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008212
8213 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
8214 GRC_MODE_4X_NIC_SEND_RINGS |
8215 GRC_MODE_NO_TX_PHDR_CSUM |
8216 GRC_MODE_NO_RX_PHDR_CSUM);
8217 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07008218
8219 /* Pseudo-header checksum is done by hardware logic and not
8220 * the offload processers, so make the chip do the pseudo-
8221 * header checksums on receive. For transmit it is more
8222 * convenient to do the pseudo-header checksum in software
8223 * as Linux does that on transmit for us in all cases.
8224 */
8225 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008226
8227 tw32(GRC_MODE,
8228 tp->grc_mode |
8229 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
8230
8231 /* Setup the timer prescalar register. Clock is always 66Mhz. */
8232 val = tr32(GRC_MISC_CFG);
8233 val &= ~0xff;
8234 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
8235 tw32(GRC_MISC_CFG, val);
8236
8237 /* Initialize MBUF/DESC pool. */
John W. Linvillecbf46852005-04-21 17:01:29 -07008238 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008239 /* Do nothing. */
8240 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
8241 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
8242 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
8243 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
8244 else
8245 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
8246 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
8247 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Matt Carlson859a588792010-04-05 10:19:28 +00008248 } else if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008249 int fw_len;
8250
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08008251 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008252 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
8253 tw32(BUFMGR_MB_POOL_ADDR,
8254 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
8255 tw32(BUFMGR_MB_POOL_SIZE,
8256 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
8257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008258
Michael Chan0f893dc2005-07-25 12:30:38 -07008259 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008260 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8261 tp->bufmgr_config.mbuf_read_dma_low_water);
8262 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8263 tp->bufmgr_config.mbuf_mac_rx_low_water);
8264 tw32(BUFMGR_MB_HIGH_WATER,
8265 tp->bufmgr_config.mbuf_high_water);
8266 } else {
8267 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8268 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
8269 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8270 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
8271 tw32(BUFMGR_MB_HIGH_WATER,
8272 tp->bufmgr_config.mbuf_high_water_jumbo);
8273 }
8274 tw32(BUFMGR_DMA_LOW_WATER,
8275 tp->bufmgr_config.dma_low_water);
8276 tw32(BUFMGR_DMA_HIGH_WATER,
8277 tp->bufmgr_config.dma_high_water);
8278
Matt Carlsond309a462010-09-30 10:34:31 +00008279 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
8280 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
8281 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
Matt Carlson4d958472011-04-20 07:57:35 +00008282 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
8283 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
8284 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0)
8285 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
Matt Carlsond309a462010-09-30 10:34:31 +00008286 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008287 for (i = 0; i < 2000; i++) {
8288 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
8289 break;
8290 udelay(10);
8291 }
8292 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00008293 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008294 return -ENODEV;
8295 }
8296
Matt Carlsoneb07a942011-04-20 07:57:36 +00008297 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
8298 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
Michael Chanb5d37722006-09-27 16:06:21 -07008299
Matt Carlsoneb07a942011-04-20 07:57:36 +00008300 tg3_setup_rxbd_thresholds(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008301
8302 /* Initialize TG3_BDINFO's at:
8303 * RCVDBDI_STD_BD: standard eth size rx ring
8304 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
8305 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
8306 *
8307 * like so:
8308 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
8309 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
8310 * ring attribute flags
8311 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
8312 *
8313 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
8314 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
8315 *
8316 * The size of each ring is fixed in the firmware, but the location is
8317 * configurable.
8318 */
8319 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008320 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008321 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008322 ((u64) tpr->rx_std_mapping & 0xffffffff));
Matt Carlson0a58d662011-04-05 14:22:45 +00008323 if (!(tp->tg3_flags3 & TG3_FLG3_5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +00008324 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
8325 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008326
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008327 /* Disable the mini ring */
8328 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008329 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
8330 BDINFO_FLAGS_DISABLED);
8331
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008332 /* Program the jumbo buffer descriptor ring control
8333 * blocks on those devices that have them.
8334 */
Matt Carlsonbb18bb92011-03-09 16:58:19 +00008335 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlson4d163b72011-01-25 15:58:48 +00008336 ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) &&
8337 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008338
Michael Chan0f893dc2005-07-25 12:30:38 -07008339 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008340 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008341 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008342 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008343 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +00008344 val = TG3_RX_JMB_RING_SIZE(tp) <<
8345 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008346 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +00008347 val | BDINFO_FLAGS_USE_EXT_RECV);
Matt Carlsona50d0792010-06-05 17:24:37 +00008348 if (!(tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) ||
8349 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson87668d32009-11-13 13:03:34 +00008350 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
8351 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008352 } else {
8353 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
8354 BDINFO_FLAGS_DISABLED);
8355 }
8356
Matt Carlson1407deb2011-04-05 14:22:44 +00008357 if (tp->tg3_flags3 & TG3_FLG3_57765_PLUS) {
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008358 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlsonde9f5232011-04-05 14:22:43 +00008359 val = TG3_RX_STD_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008360 else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008361 val = TG3_RX_STD_MAX_SIZE_5717;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008362 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
8363 val |= (TG3_RX_STD_DMA_SZ << 2);
8364 } else
Matt Carlson04380d42010-04-12 06:58:29 +00008365 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008366 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008367 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008368
8369 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008370
Matt Carlson411da642009-11-13 13:03:46 +00008371 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e662009-11-13 13:03:49 +00008372 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008373
Matt Carlson411da642009-11-13 13:03:46 +00008374 tpr->rx_jmb_prod_idx = (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) ?
Matt Carlson21f581a2009-08-28 14:00:25 +00008375 tp->rx_jumbo_pending : 0;
Matt Carlson66711e662009-11-13 13:03:49 +00008376 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008377
Matt Carlson2d31eca2009-09-01 12:53:31 +00008378 tg3_rings_reset(tp);
8379
Linus Torvalds1da177e2005-04-16 15:20:36 -07008380 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07008381 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008382
8383 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00008384 tw32(MAC_RX_MTU_SIZE,
8385 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008386
8387 /* The slot time is changed by tg3_setup_phy if we
8388 * run at gigabit with half duplex.
8389 */
Matt Carlsonf2096f92011-04-05 14:22:48 +00008390 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
8391 (6 << TX_LENGTHS_IPG_SHIFT) |
8392 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
8393
8394 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8395 val |= tr32(MAC_TX_LENGTHS) &
8396 (TX_LENGTHS_JMB_FRM_LEN_MSK |
8397 TX_LENGTHS_CNT_DWN_VAL_MSK);
8398
8399 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008400
8401 /* Receive rules. */
8402 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
8403 tw32(RCVLPC_CONFIG, 0x0181);
8404
8405 /* Calculate RDMAC_MODE setting early, we need it to determine
8406 * the RCVLPC_STATE_ENABLE mask.
8407 */
8408 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
8409 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
8410 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
8411 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
8412 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07008413
Matt Carlsondeabaac2010-11-24 08:31:50 +00008414 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +00008415 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
8416
Matt Carlson57e69832008-05-25 23:48:31 -07008417 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08008418 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8419 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07008420 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
8421 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
8422 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
8423
Matt Carlsonc5908932011-03-09 16:58:25 +00008424 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8425 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008426 if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07008427 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008428 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
8429 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
8430 !(tp->tg3_flags2 & TG3_FLG2_IS_5788)) {
8431 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8432 }
8433 }
8434
Michael Chan85e94ce2005-04-21 17:05:28 -07008435 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)
8436 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8437
Linus Torvalds1da177e2005-04-16 15:20:36 -07008438 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
Matt Carlson027455a2008-12-21 20:19:30 -08008439 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
8440
Matt Carlsone849cdc2009-11-13 13:03:38 +00008441 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
8442 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson027455a2008-12-21 20:19:30 -08008443 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
8444 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008445
Matt Carlsonf2096f92011-04-05 14:22:48 +00008446 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8447 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
8448
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008449 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
8450 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
8451 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8452 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlson1407deb2011-04-05 14:22:44 +00008453 (tp->tg3_flags3 & TG3_FLG3_57765_PLUS)) {
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008454 val = tr32(TG3_RDMA_RSRVCTRL_REG);
Matt Carlsond78b59f2011-04-05 14:22:46 +00008455 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8456 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +00008457 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
8458 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
8459 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
8460 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
8461 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
8462 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +00008463 }
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008464 tw32(TG3_RDMA_RSRVCTRL_REG,
8465 val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
8466 }
8467
Matt Carlsond78b59f2011-04-05 14:22:46 +00008468 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8469 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsond309a462010-09-30 10:34:31 +00008470 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
8471 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val |
8472 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
8473 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
8474 }
8475
Linus Torvalds1da177e2005-04-16 15:20:36 -07008476 /* Receive/send statistics. */
Michael Chan16613942006-06-29 20:15:13 -07008477 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
8478 val = tr32(RCVLPC_STATS_ENABLE);
8479 val &= ~RCVLPC_STATSENAB_DACK_FIX;
8480 tw32(RCVLPC_STATS_ENABLE, val);
8481 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
8482 (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008483 val = tr32(RCVLPC_STATS_ENABLE);
8484 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
8485 tw32(RCVLPC_STATS_ENABLE, val);
8486 } else {
8487 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
8488 }
8489 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
8490 tw32(SNDDATAI_STATSENAB, 0xffffff);
8491 tw32(SNDDATAI_STATSCTRL,
8492 (SNDDATAI_SCTRL_ENABLE |
8493 SNDDATAI_SCTRL_FASTUPD));
8494
8495 /* Setup host coalescing engine. */
8496 tw32(HOSTCC_MODE, 0);
8497 for (i = 0; i < 2000; i++) {
8498 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
8499 break;
8500 udelay(10);
8501 }
8502
Michael Chand244c892005-07-05 14:42:33 -07008503 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008504
Linus Torvalds1da177e2005-04-16 15:20:36 -07008505 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
8506 /* Status/statistics block address. See tg3_timer,
8507 * the tg3_periodic_fetch_stats call there, and
8508 * tg3_get_stats to see how this works for 5705/5750 chips.
8509 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008510 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8511 ((u64) tp->stats_mapping >> 32));
8512 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8513 ((u64) tp->stats_mapping & 0xffffffff));
8514 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008515
Linus Torvalds1da177e2005-04-16 15:20:36 -07008516 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008517
8518 /* Clear statistics and status block memory areas */
8519 for (i = NIC_SRAM_STATS_BLK;
8520 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
8521 i += sizeof(u32)) {
8522 tg3_write_mem(tp, i, 0);
8523 udelay(40);
8524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008525 }
8526
8527 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
8528
8529 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
8530 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
8531 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
8532 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
8533
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008534 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
8535 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -07008536 /* reset to prevent losing 1st rx packet intermittently */
8537 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8538 udelay(10);
8539 }
8540
Matt Carlson3bda1252008-08-15 14:08:22 -07008541 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008542 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -07008543 else
8544 tp->mac_mode = 0;
8545 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Linus Torvalds1da177e2005-04-16 15:20:36 -07008546 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE | MAC_MODE_FHDE_ENABLE;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07008547 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008548 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07008549 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
8550 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008551 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
8552 udelay(40);
8553
Michael Chan314fba32005-04-21 17:07:04 -07008554 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Michael Chan9d26e212006-12-07 00:21:14 -08008555 * If TG3_FLG2_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07008556 * register to preserve the GPIO settings for LOMs. The GPIOs,
8557 * whether used as inputs or outputs, are set by boot code after
8558 * reset.
8559 */
Michael Chan9d26e212006-12-07 00:21:14 -08008560 if (!(tp->tg3_flags2 & TG3_FLG2_IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07008561 u32 gpio_mask;
8562
Michael Chan9d26e212006-12-07 00:21:14 -08008563 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
8564 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
8565 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07008566
8567 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
8568 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
8569 GRC_LCLCTRL_GPIO_OUTPUT3;
8570
Michael Chanaf36e6b2006-03-23 01:28:06 -08008571 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
8572 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
8573
Gary Zambranoaaf84462007-05-05 11:51:45 -07008574 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07008575 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
8576
8577 /* GPIO1 must be driven high for eeprom write protect */
Michael Chan9d26e212006-12-07 00:21:14 -08008578 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT)
8579 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
8580 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07008581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008582 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8583 udelay(100);
8584
Matt Carlson0583d522011-01-25 15:58:50 +00008585 if ((tp->tg3_flags2 & TG3_FLG2_USING_MSIX) &&
8586 tp->irq_cnt > 1) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008587 val = tr32(MSGINT_MODE);
8588 val |= MSGINT_MODE_MULTIVEC_EN | MSGINT_MODE_ENABLE;
8589 tw32(MSGINT_MODE, val);
8590 }
8591
Linus Torvalds1da177e2005-04-16 15:20:36 -07008592 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
8593 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
8594 udelay(40);
8595 }
8596
8597 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
8598 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
8599 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
8600 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
8601 WDMAC_MODE_LNGREAD_ENAB);
8602
Matt Carlsonc5908932011-03-09 16:58:25 +00008603 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8604 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Matt Carlson29ea0952009-08-25 10:07:54 +00008605 if ((tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008606 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
8607 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
8608 /* nothing */
8609 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Matt Carlsonc5908932011-03-09 16:58:25 +00008610 !(tp->tg3_flags2 & TG3_FLG2_IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008611 val |= WDMAC_MODE_RX_ACCEL;
8612 }
8613 }
8614
Michael Chand9ab5ad12006-03-20 22:27:35 -08008615 /* Enable host coalescing bug fix */
Matt Carlson321d32a2008-11-21 17:22:19 -08008616 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Matt Carlsonf51f3562008-05-25 23:45:08 -07008617 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad12006-03-20 22:27:35 -08008618
Matt Carlson788a0352009-11-02 14:26:03 +00008619 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
8620 val |= WDMAC_MODE_BURST_ALL_DATA;
8621
Linus Torvalds1da177e2005-04-16 15:20:36 -07008622 tw32_f(WDMAC_MODE, val);
8623 udelay(40);
8624
Matt Carlson9974a352007-10-07 23:27:28 -07008625 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
8626 u16 pcix_cmd;
8627
8628 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8629 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008630 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07008631 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
8632 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008633 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07008634 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
8635 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008636 }
Matt Carlson9974a352007-10-07 23:27:28 -07008637 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8638 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008639 }
8640
8641 tw32_f(RDMAC_MODE, rdmac_mode);
8642 udelay(40);
8643
8644 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
8645 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
8646 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07008647
8648 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
8649 tw32(SNDDATAC_MODE,
8650 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
8651 else
8652 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
8653
Linus Torvalds1da177e2005-04-16 15:20:36 -07008654 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
8655 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008656 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Matt Carlsonde9f5232011-04-05 14:22:43 +00008657 if (tp->tg3_flags3 & TG3_FLG3_LRG_PROD_RING_CAP)
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008658 val |= RCVDBDI_MODE_LRG_RING_SZ;
8659 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008660 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008661 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
8662 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008663 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Matt Carlson19cfaec2009-12-03 08:36:20 +00008664 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008665 val |= SNDBDI_MODE_MULTI_TXQ_EN;
8666 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008667 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
8668
8669 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
8670 err = tg3_load_5701_a0_firmware_fix(tp);
8671 if (err)
8672 return err;
8673 }
8674
Linus Torvalds1da177e2005-04-16 15:20:36 -07008675 if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) {
8676 err = tg3_load_tso_firmware(tp);
8677 if (err)
8678 return err;
8679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008680
8681 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +00008682
Matt Carlsonb1d05212010-06-05 17:24:31 +00008683 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
8684 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
8685 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +00008686
8687 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
8688 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
8689 tp->tx_mode &= ~val;
8690 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
8691 }
8692
Linus Torvalds1da177e2005-04-16 15:20:36 -07008693 tw32_f(MAC_TX_MODE, tp->tx_mode);
8694 udelay(100);
8695
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008696 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS) {
8697 u32 reg = MAC_RSS_INDIR_TBL_0;
8698 u8 *ent = (u8 *)&val;
8699
8700 /* Setup the indirection table */
8701 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
8702 int idx = i % sizeof(val);
8703
Matt Carlson5efeeea2010-07-11 09:31:40 +00008704 ent[idx] = i % (tp->irq_cnt - 1);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008705 if (idx == sizeof(val) - 1) {
8706 tw32(reg, val);
8707 reg += 4;
8708 }
8709 }
8710
8711 /* Setup the "secret" hash key. */
8712 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
8713 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
8714 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
8715 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
8716 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
8717 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
8718 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
8719 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
8720 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
8721 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
8722 }
8723
Linus Torvalds1da177e2005-04-16 15:20:36 -07008724 tp->rx_mode = RX_MODE_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08008725 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Michael Chanaf36e6b2006-03-23 01:28:06 -08008726 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
8727
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008728 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)
8729 tp->rx_mode |= RX_MODE_RSS_ENABLE |
8730 RX_MODE_RSS_ITBL_HASH_BITS_7 |
8731 RX_MODE_RSS_IPV6_HASH_EN |
8732 RX_MODE_RSS_TCP_IPV6_HASH_EN |
8733 RX_MODE_RSS_IPV4_HASH_EN |
8734 RX_MODE_RSS_TCP_IPV4_HASH_EN;
8735
Linus Torvalds1da177e2005-04-16 15:20:36 -07008736 tw32_f(MAC_RX_MODE, tp->rx_mode);
8737 udelay(10);
8738
Linus Torvalds1da177e2005-04-16 15:20:36 -07008739 tw32(MAC_LED_CTRL, tp->led_ctrl);
8740
8741 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008742 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008743 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8744 udelay(10);
8745 }
8746 tw32_f(MAC_RX_MODE, tp->rx_mode);
8747 udelay(10);
8748
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008749 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008750 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008751 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008752 /* Set drive transmission level to 1.2V */
8753 /* only if the signal pre-emphasis bit is not set */
8754 val = tr32(MAC_SERDES_CFG);
8755 val &= 0xfffff000;
8756 val |= 0x880;
8757 tw32(MAC_SERDES_CFG, val);
8758 }
8759 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
8760 tw32(MAC_SERDES_CFG, 0x616000);
8761 }
8762
8763 /* Prevent chip from dropping frames when flow control
8764 * is enabled.
8765 */
Matt Carlson666bc832010-01-20 16:58:03 +00008766 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8767 val = 1;
8768 else
8769 val = 2;
8770 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008771
8772 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008773 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008774 /* Use hardware link auto-negotiation */
8775 tp->tg3_flags2 |= TG3_FLG2_HW_AUTONEG;
8776 }
8777
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008778 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Michael Chand4d2c552006-03-20 17:47:20 -08008779 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)) {
8780 u32 tmp;
8781
8782 tmp = tr32(SERDES_RX_CTRL);
8783 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
8784 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
8785 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
8786 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8787 }
8788
Matt Carlsondd477002008-05-25 23:45:58 -07008789 if (!(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)) {
Matt Carlson80096062010-08-02 11:26:06 +00008790 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
8791 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07008792 tp->link_config.speed = tp->link_config.orig_speed;
8793 tp->link_config.duplex = tp->link_config.orig_duplex;
8794 tp->link_config.autoneg = tp->link_config.orig_autoneg;
8795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008796
Matt Carlsondd477002008-05-25 23:45:58 -07008797 err = tg3_setup_phy(tp, 0);
8798 if (err)
8799 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008800
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008801 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
8802 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07008803 u32 tmp;
8804
8805 /* Clear CRC stats. */
8806 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
8807 tg3_writephy(tp, MII_TG3_TEST1,
8808 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00008809 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -07008810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008811 }
8812 }
8813
8814 __tg3_set_rx_mode(tp->dev);
8815
8816 /* Initialize receive rules. */
8817 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
8818 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
8819 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
8820 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
8821
Michael Chan4cf78e42005-07-25 12:29:19 -07008822 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Michael Chana4e2b342005-10-26 15:46:52 -07008823 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008824 limit = 8;
8825 else
8826 limit = 16;
8827 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF)
8828 limit -= 4;
8829 switch (limit) {
8830 case 16:
8831 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
8832 case 15:
8833 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
8834 case 14:
8835 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
8836 case 13:
8837 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
8838 case 12:
8839 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
8840 case 11:
8841 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
8842 case 10:
8843 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
8844 case 9:
8845 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
8846 case 8:
8847 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
8848 case 7:
8849 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
8850 case 6:
8851 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
8852 case 5:
8853 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
8854 case 4:
8855 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
8856 case 3:
8857 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
8858 case 2:
8859 case 1:
8860
8861 default:
8862 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07008863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008864
Matt Carlson9ce768e2007-10-11 19:49:11 -07008865 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
8866 /* Write our heartbeat update interval to APE. */
8867 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
8868 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07008869
Linus Torvalds1da177e2005-04-16 15:20:36 -07008870 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
8871
Linus Torvalds1da177e2005-04-16 15:20:36 -07008872 return 0;
8873}
8874
8875/* Called at device open time to get the chip ready for
8876 * packet processing. Invoked with tp->lock held.
8877 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008878static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008879{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008880 tg3_switch_clocks(tp);
8881
8882 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
8883
Matt Carlson2f751b62008-08-04 23:17:34 -07008884 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008885}
8886
8887#define TG3_STAT_ADD32(PSTAT, REG) \
8888do { u32 __val = tr32(REG); \
8889 (PSTAT)->low += __val; \
8890 if ((PSTAT)->low < __val) \
8891 (PSTAT)->high += 1; \
8892} while (0)
8893
8894static void tg3_periodic_fetch_stats(struct tg3 *tp)
8895{
8896 struct tg3_hw_stats *sp = tp->hw_stats;
8897
8898 if (!netif_carrier_ok(tp->dev))
8899 return;
8900
8901 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
8902 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
8903 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
8904 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
8905 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
8906 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
8907 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
8908 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
8909 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
8910 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
8911 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
8912 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
8913 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
8914
8915 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
8916 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
8917 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
8918 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
8919 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
8920 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
8921 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
8922 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
8923 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
8924 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
8925 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
8926 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
8927 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
8928 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07008929
8930 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
Matt Carlson4d958472011-04-20 07:57:35 +00008931 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717) {
8932 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
8933 } else {
8934 u32 val = tr32(HOSTCC_FLOW_ATTN);
8935 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
8936 if (val) {
8937 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
8938 sp->rx_discards.low += val;
8939 if (sp->rx_discards.low < val)
8940 sp->rx_discards.high += 1;
8941 }
8942 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
8943 }
Michael Chan463d3052006-05-22 16:36:27 -07008944 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008945}
8946
8947static void tg3_timer(unsigned long __opaque)
8948{
8949 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008950
Michael Chanf475f162006-03-27 23:20:14 -08008951 if (tp->irq_sync)
8952 goto restart_timer;
8953
David S. Millerf47c11e2005-06-24 20:18:35 -07008954 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008955
David S. Millerfac9b832005-05-18 22:46:34 -07008956 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)) {
8957 /* All of this garbage is because when using non-tagged
8958 * IRQ status the mailbox/status_block protocol the chip
8959 * uses with the cpu is race prone.
8960 */
Matt Carlson898a56f2009-08-28 14:02:40 +00008961 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -07008962 tw32(GRC_LOCAL_CTRL,
8963 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
8964 } else {
8965 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00008966 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -07008967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008968
David S. Millerfac9b832005-05-18 22:46:34 -07008969 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
8970 tp->tg3_flags2 |= TG3_FLG2_RESTART_TIMER;
David S. Millerf47c11e2005-06-24 20:18:35 -07008971 spin_unlock(&tp->lock);
David S. Millerfac9b832005-05-18 22:46:34 -07008972 schedule_work(&tp->reset_task);
8973 return;
8974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008975 }
8976
Linus Torvalds1da177e2005-04-16 15:20:36 -07008977 /* This part only runs once per second. */
8978 if (!--tp->timer_counter) {
David S. Millerfac9b832005-05-18 22:46:34 -07008979 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
8980 tg3_periodic_fetch_stats(tp);
8981
Matt Carlson52b02d02010-10-14 10:37:41 +00008982 if (tp->setlpicnt && !--tp->setlpicnt) {
8983 u32 val = tr32(TG3_CPMU_EEE_MODE);
8984 tw32(TG3_CPMU_EEE_MODE,
8985 val | TG3_CPMU_EEEMD_LPI_ENABLE);
8986 }
8987
Linus Torvalds1da177e2005-04-16 15:20:36 -07008988 if (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) {
8989 u32 mac_stat;
8990 int phy_event;
8991
8992 mac_stat = tr32(MAC_STATUS);
8993
8994 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008995 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008996 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
8997 phy_event = 1;
8998 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
8999 phy_event = 1;
9000
9001 if (phy_event)
9002 tg3_setup_phy(tp, 0);
9003 } else if (tp->tg3_flags & TG3_FLAG_POLL_SERDES) {
9004 u32 mac_stat = tr32(MAC_STATUS);
9005 int need_setup = 0;
9006
9007 if (netif_carrier_ok(tp->dev) &&
9008 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
9009 need_setup = 1;
9010 }
Matt Carlsonbe98da62010-07-11 09:31:46 +00009011 if (!netif_carrier_ok(tp->dev) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009012 (mac_stat & (MAC_STATUS_PCS_SYNCED |
9013 MAC_STATUS_SIGNAL_DET))) {
9014 need_setup = 1;
9015 }
9016 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07009017 if (!tp->serdes_counter) {
9018 tw32_f(MAC_MODE,
9019 (tp->mac_mode &
9020 ~MAC_MODE_PORT_MODE_MASK));
9021 udelay(40);
9022 tw32_f(MAC_MODE, tp->mac_mode);
9023 udelay(40);
9024 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009025 tg3_setup_phy(tp, 0);
9026 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009027 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Matt Carlson2138c002010-07-11 09:31:43 +00009028 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -07009029 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +00009030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009031
9032 tp->timer_counter = tp->timer_multiplier;
9033 }
9034
Michael Chan130b8e42006-09-27 16:00:40 -07009035 /* Heartbeat is only sent once every 2 seconds.
9036 *
9037 * The heartbeat is to tell the ASF firmware that the host
9038 * driver is still alive. In the event that the OS crashes,
9039 * ASF needs to reset the hardware to free up the FIFO space
9040 * that may be filled with rx packets destined for the host.
9041 * If the FIFO is full, ASF will no longer function properly.
9042 *
9043 * Unintended resets have been reported on real time kernels
9044 * where the timer doesn't run on time. Netpoll will also have
9045 * same problem.
9046 *
9047 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
9048 * to check the ring condition when the heartbeat is expiring
9049 * before doing the reset. This will prevent most unintended
9050 * resets.
9051 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009052 if (!--tp->asf_counter) {
Matt Carlsonbc7959b2008-08-15 14:08:55 -07009053 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
9054 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07009055 tg3_wait_for_event_ack(tp);
9056
Michael Chanbbadf502006-04-06 21:46:34 -07009057 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07009058 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07009059 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +00009060 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
9061 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -07009062
9063 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009064 }
9065 tp->asf_counter = tp->asf_multiplier;
9066 }
9067
David S. Millerf47c11e2005-06-24 20:18:35 -07009068 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009069
Michael Chanf475f162006-03-27 23:20:14 -08009070restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07009071 tp->timer.expires = jiffies + tp->timer_offset;
9072 add_timer(&tp->timer);
9073}
9074
Matt Carlson4f125f42009-09-01 12:55:02 +00009075static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -08009076{
David Howells7d12e782006-10-05 14:55:46 +01009077 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009078 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +00009079 char *name;
9080 struct tg3_napi *tnapi = &tp->napi[irq_num];
9081
9082 if (tp->irq_cnt == 1)
9083 name = tp->dev->name;
9084 else {
9085 name = &tnapi->irq_lbl[0];
9086 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
9087 name[IFNAMSIZ-1] = 0;
9088 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009089
Matt Carlson679563f2009-09-01 12:55:46 +00009090 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI_OR_MSIX) {
Michael Chanfcfa0a32006-03-20 22:28:41 -08009091 fn = tg3_msi;
9092 if (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)
9093 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009094 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009095 } else {
9096 fn = tg3_interrupt;
9097 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)
9098 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009099 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009100 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009101
9102 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009103}
9104
Michael Chan79381092005-04-21 17:13:59 -07009105static int tg3_test_interrupt(struct tg3 *tp)
9106{
Matt Carlson09943a12009-08-28 14:01:57 +00009107 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -07009108 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -07009109 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009110 u32 val;
Michael Chan79381092005-04-21 17:13:59 -07009111
Michael Chand4bc3922005-05-29 14:59:20 -07009112 if (!netif_running(dev))
9113 return -ENODEV;
9114
Michael Chan79381092005-04-21 17:13:59 -07009115 tg3_disable_ints(tp);
9116
Matt Carlson4f125f42009-09-01 12:55:02 +00009117 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009118
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009119 /*
9120 * Turn off MSI one shot mode. Otherwise this test has no
9121 * observable way to know whether the interrupt was delivered.
9122 */
Matt Carlson1407deb2011-04-05 14:22:44 +00009123 if ((tp->tg3_flags3 & TG3_FLG3_57765_PLUS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009124 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
9125 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
9126 tw32(MSGINT_MODE, val);
9127 }
9128
Matt Carlson4f125f42009-09-01 12:55:02 +00009129 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Matt Carlson09943a12009-08-28 14:01:57 +00009130 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009131 if (err)
9132 return err;
9133
Matt Carlson898a56f2009-08-28 14:02:40 +00009134 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -07009135 tg3_enable_ints(tp);
9136
9137 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00009138 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -07009139
9140 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -07009141 u32 int_mbox, misc_host_ctrl;
9142
Matt Carlson898a56f2009-08-28 14:02:40 +00009143 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -07009144 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
9145
9146 if ((int_mbox != 0) ||
9147 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
9148 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -07009149 break;
Michael Chanb16250e2006-09-27 16:10:14 -07009150 }
9151
Michael Chan79381092005-04-21 17:13:59 -07009152 msleep(10);
9153 }
9154
9155 tg3_disable_ints(tp);
9156
Matt Carlson4f125f42009-09-01 12:55:02 +00009157 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009158
Matt Carlson4f125f42009-09-01 12:55:02 +00009159 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009160
9161 if (err)
9162 return err;
9163
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009164 if (intr_ok) {
9165 /* Reenable MSI one shot mode. */
Matt Carlson1407deb2011-04-05 14:22:44 +00009166 if ((tp->tg3_flags3 & TG3_FLG3_57765_PLUS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009167 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
9168 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
9169 tw32(MSGINT_MODE, val);
9170 }
Michael Chan79381092005-04-21 17:13:59 -07009171 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009172 }
Michael Chan79381092005-04-21 17:13:59 -07009173
9174 return -EIO;
9175}
9176
9177/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
9178 * successfully restored
9179 */
9180static int tg3_test_msi(struct tg3 *tp)
9181{
Michael Chan79381092005-04-21 17:13:59 -07009182 int err;
9183 u16 pci_cmd;
9184
9185 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSI))
9186 return 0;
9187
9188 /* Turn off SERR reporting in case MSI terminates with Master
9189 * Abort.
9190 */
9191 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
9192 pci_write_config_word(tp->pdev, PCI_COMMAND,
9193 pci_cmd & ~PCI_COMMAND_SERR);
9194
9195 err = tg3_test_interrupt(tp);
9196
9197 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
9198
9199 if (!err)
9200 return 0;
9201
9202 /* other failures */
9203 if (err != -EIO)
9204 return err;
9205
9206 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009207 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
9208 "to INTx mode. Please report this failure to the PCI "
9209 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -07009210
Matt Carlson4f125f42009-09-01 12:55:02 +00009211 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +00009212
Michael Chan79381092005-04-21 17:13:59 -07009213 pci_disable_msi(tp->pdev);
9214
9215 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI;
Andre Detschdc8bf1b2010-04-26 07:27:07 +00009216 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -07009217
Matt Carlson4f125f42009-09-01 12:55:02 +00009218 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009219 if (err)
9220 return err;
9221
9222 /* Need to reset the chip because the MSI cycle may have terminated
9223 * with Master Abort.
9224 */
David S. Millerf47c11e2005-06-24 20:18:35 -07009225 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009226
Michael Chan944d9802005-05-29 14:57:48 -07009227 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009228 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009229
David S. Millerf47c11e2005-06-24 20:18:35 -07009230 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009231
9232 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +00009233 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -07009234
9235 return err;
9236}
9237
Matt Carlson9e9fd122009-01-19 16:57:45 -08009238static int tg3_request_firmware(struct tg3 *tp)
9239{
9240 const __be32 *fw_data;
9241
9242 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009243 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
9244 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009245 return -ENOENT;
9246 }
9247
9248 fw_data = (void *)tp->fw->data;
9249
9250 /* Firmware blob starts with version numbers, followed by
9251 * start address and _full_ length including BSS sections
9252 * (which must be longer than the actual data, of course
9253 */
9254
9255 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
9256 if (tp->fw_len < (tp->fw->size - 12)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009257 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
9258 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009259 release_firmware(tp->fw);
9260 tp->fw = NULL;
9261 return -EINVAL;
9262 }
9263
9264 /* We no longer need firmware; we have it. */
9265 tp->fw_needed = NULL;
9266 return 0;
9267}
9268
Matt Carlson679563f2009-09-01 12:55:46 +00009269static bool tg3_enable_msix(struct tg3 *tp)
9270{
9271 int i, rc, cpus = num_online_cpus();
9272 struct msix_entry msix_ent[tp->irq_max];
9273
9274 if (cpus == 1)
9275 /* Just fallback to the simpler MSI mode. */
9276 return false;
9277
9278 /*
9279 * We want as many rx rings enabled as there are cpus.
9280 * The first MSIX vector only deals with link interrupts, etc,
9281 * so we add one to the number of vectors we are requesting.
9282 */
9283 tp->irq_cnt = min_t(unsigned, cpus + 1, tp->irq_max);
9284
9285 for (i = 0; i < tp->irq_max; i++) {
9286 msix_ent[i].entry = i;
9287 msix_ent[i].vector = 0;
9288 }
9289
9290 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +00009291 if (rc < 0) {
9292 return false;
9293 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +00009294 if (pci_enable_msix(tp->pdev, msix_ent, rc))
9295 return false;
Joe Perches05dbe002010-02-17 19:44:19 +00009296 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
9297 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +00009298 tp->irq_cnt = rc;
9299 }
9300
9301 for (i = 0; i < tp->irq_max; i++)
9302 tp->napi[i].irq_vec = msix_ent[i].vector;
9303
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009304 netif_set_real_num_tx_queues(tp->dev, 1);
9305 rc = tp->irq_cnt > 1 ? tp->irq_cnt - 1 : 1;
9306 if (netif_set_real_num_rx_queues(tp->dev, rc)) {
9307 pci_disable_msix(tp->pdev);
9308 return false;
9309 }
Matt Carlsonb92b9042010-11-24 08:31:51 +00009310
9311 if (tp->irq_cnt > 1) {
Matt Carlson2430b032010-06-05 17:24:34 +00009312 tp->tg3_flags3 |= TG3_FLG3_ENABLE_RSS;
Matt Carlsond78b59f2011-04-05 14:22:46 +00009313
9314 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
9315 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsonb92b9042010-11-24 08:31:51 +00009316 tp->tg3_flags3 |= TG3_FLG3_ENABLE_TSS;
9317 netif_set_real_num_tx_queues(tp->dev, tp->irq_cnt - 1);
9318 }
9319 }
Matt Carlson2430b032010-06-05 17:24:34 +00009320
Matt Carlson679563f2009-09-01 12:55:46 +00009321 return true;
9322}
9323
Matt Carlson07b01732009-08-28 14:01:15 +00009324static void tg3_ints_init(struct tg3 *tp)
9325{
Matt Carlson679563f2009-09-01 12:55:46 +00009326 if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSI_OR_MSIX) &&
9327 !(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +00009328 /* All MSI supporting chips should support tagged
9329 * status. Assert that this is the case.
9330 */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009331 netdev_warn(tp->dev,
9332 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +00009333 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +00009334 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009335
Matt Carlson679563f2009-09-01 12:55:46 +00009336 if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX) && tg3_enable_msix(tp))
9337 tp->tg3_flags2 |= TG3_FLG2_USING_MSIX;
9338 else if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSI) &&
9339 pci_enable_msi(tp->pdev) == 0)
9340 tp->tg3_flags2 |= TG3_FLG2_USING_MSI;
9341
9342 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI_OR_MSIX) {
9343 u32 msi_mode = tr32(MSGINT_MODE);
Matt Carlson0583d522011-01-25 15:58:50 +00009344 if ((tp->tg3_flags2 & TG3_FLG2_USING_MSIX) &&
9345 tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009346 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson679563f2009-09-01 12:55:46 +00009347 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
9348 }
9349defcfg:
9350 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSIX)) {
9351 tp->irq_cnt = 1;
9352 tp->napi[0].irq_vec = tp->pdev->irq;
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009353 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -07009354 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +00009355 }
Matt Carlson07b01732009-08-28 14:01:15 +00009356}
9357
9358static void tg3_ints_fini(struct tg3 *tp)
9359{
Matt Carlson679563f2009-09-01 12:55:46 +00009360 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
9361 pci_disable_msix(tp->pdev);
9362 else if (tp->tg3_flags2 & TG3_FLG2_USING_MSI)
9363 pci_disable_msi(tp->pdev);
9364 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI_OR_MSIX;
Matt Carlson774ee752010-08-02 11:25:56 +00009365 tp->tg3_flags3 &= ~(TG3_FLG3_ENABLE_RSS | TG3_FLG3_ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +00009366}
9367
Linus Torvalds1da177e2005-04-16 15:20:36 -07009368static int tg3_open(struct net_device *dev)
9369{
9370 struct tg3 *tp = netdev_priv(dev);
Matt Carlson4f125f42009-09-01 12:55:02 +00009371 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009372
Matt Carlson9e9fd122009-01-19 16:57:45 -08009373 if (tp->fw_needed) {
9374 err = tg3_request_firmware(tp);
9375 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
9376 if (err)
9377 return err;
9378 } else if (err) {
Joe Perches05dbe002010-02-17 19:44:19 +00009379 netdev_warn(tp->dev, "TSO capability disabled\n");
Matt Carlson9e9fd122009-01-19 16:57:45 -08009380 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
9381 } else if (!(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009382 netdev_notice(tp->dev, "TSO capability restored\n");
Matt Carlson9e9fd122009-01-19 16:57:45 -08009383 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
9384 }
9385 }
9386
Michael Chanc49a1562006-12-17 17:07:29 -08009387 netif_carrier_off(tp->dev);
9388
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009389 err = tg3_power_up(tp);
Matt Carlson2f751b62008-08-04 23:17:34 -07009390 if (err)
Michael Chanbc1c7562006-03-20 17:48:03 -08009391 return err;
Matt Carlson2f751b62008-08-04 23:17:34 -07009392
9393 tg3_full_lock(tp, 0);
Michael Chanbc1c7562006-03-20 17:48:03 -08009394
Linus Torvalds1da177e2005-04-16 15:20:36 -07009395 tg3_disable_ints(tp);
9396 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
9397
David S. Millerf47c11e2005-06-24 20:18:35 -07009398 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009399
Matt Carlson679563f2009-09-01 12:55:46 +00009400 /*
9401 * Setup interrupts first so we know how
9402 * many NAPI resources to allocate
9403 */
9404 tg3_ints_init(tp);
9405
Linus Torvalds1da177e2005-04-16 15:20:36 -07009406 /* The placement of this call is tied
9407 * to the setup and use of Host TX descriptors.
9408 */
9409 err = tg3_alloc_consistent(tp);
9410 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009411 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009412
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009413 tg3_napi_init(tp);
9414
Matt Carlsonfed97812009-09-01 13:10:19 +00009415 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07009416
Matt Carlson4f125f42009-09-01 12:55:02 +00009417 for (i = 0; i < tp->irq_cnt; i++) {
9418 struct tg3_napi *tnapi = &tp->napi[i];
9419 err = tg3_request_irq(tp, i);
9420 if (err) {
9421 for (i--; i >= 0; i--)
9422 free_irq(tnapi->irq_vec, tnapi);
9423 break;
9424 }
9425 }
Matt Carlson07b01732009-08-28 14:01:15 +00009426
9427 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009428 goto err_out2;
Matt Carlson07b01732009-08-28 14:01:15 +00009429
David S. Millerf47c11e2005-06-24 20:18:35 -07009430 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009431
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009432 err = tg3_init_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009433 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -07009434 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009435 tg3_free_rings(tp);
9436 } else {
David S. Millerfac9b832005-05-18 22:46:34 -07009437 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)
9438 tp->timer_offset = HZ;
9439 else
9440 tp->timer_offset = HZ / 10;
9441
9442 BUG_ON(tp->timer_offset > HZ);
9443 tp->timer_counter = tp->timer_multiplier =
9444 (HZ / tp->timer_offset);
9445 tp->asf_counter = tp->asf_multiplier =
Michael Chan28fbef72005-10-26 15:48:35 -07009446 ((HZ / tp->timer_offset) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009447
9448 init_timer(&tp->timer);
9449 tp->timer.expires = jiffies + tp->timer_offset;
9450 tp->timer.data = (unsigned long) tp;
9451 tp->timer.function = tg3_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009452 }
9453
David S. Millerf47c11e2005-06-24 20:18:35 -07009454 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009455
Matt Carlson07b01732009-08-28 14:01:15 +00009456 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009457 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009458
Michael Chan79381092005-04-21 17:13:59 -07009459 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
9460 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -07009461
Michael Chan79381092005-04-21 17:13:59 -07009462 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -07009463 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -07009464 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -07009465 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07009466 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009467
Matt Carlson679563f2009-09-01 12:55:46 +00009468 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -07009469 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009470
Matt Carlson1407deb2011-04-05 14:22:44 +00009471 if (!(tp->tg3_flags3 & TG3_FLG3_57765_PLUS) &&
Matt Carlsonc885e822010-08-02 11:25:57 +00009472 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009473 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009474
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009475 tw32(PCIE_TRANSACTION_CFG,
9476 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009477 }
Michael Chan79381092005-04-21 17:13:59 -07009478 }
9479
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009480 tg3_phy_start(tp);
9481
David S. Millerf47c11e2005-06-24 20:18:35 -07009482 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009483
Michael Chan79381092005-04-21 17:13:59 -07009484 add_timer(&tp->timer);
9485 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009486 tg3_enable_ints(tp);
9487
David S. Millerf47c11e2005-06-24 20:18:35 -07009488 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009489
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009490 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009491
9492 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +00009493
Matt Carlson679563f2009-09-01 12:55:46 +00009494err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +00009495 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9496 struct tg3_napi *tnapi = &tp->napi[i];
9497 free_irq(tnapi->irq_vec, tnapi);
9498 }
Matt Carlson07b01732009-08-28 14:01:15 +00009499
Matt Carlson679563f2009-09-01 12:55:46 +00009500err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +00009501 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009502 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00009503 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +00009504
9505err_out1:
9506 tg3_ints_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00009507 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009508}
9509
Eric Dumazet511d2222010-07-07 20:44:24 +00009510static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *,
9511 struct rtnl_link_stats64 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009512static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *);
9513
9514static int tg3_close(struct net_device *dev)
9515{
Matt Carlson4f125f42009-09-01 12:55:02 +00009516 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009517 struct tg3 *tp = netdev_priv(dev);
9518
Matt Carlsonfed97812009-09-01 13:10:19 +00009519 tg3_napi_disable(tp);
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07009520 cancel_work_sync(&tp->reset_task);
Michael Chan7faa0062006-02-02 17:29:28 -08009521
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009522 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009523
9524 del_timer_sync(&tp->timer);
9525
Matt Carlson24bb4fb2009-10-05 17:55:29 +00009526 tg3_phy_stop(tp);
9527
David S. Millerf47c11e2005-06-24 20:18:35 -07009528 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009529
9530 tg3_disable_ints(tp);
9531
Michael Chan944d9802005-05-29 14:57:48 -07009532 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009533 tg3_free_rings(tp);
Michael Chan5cf64b8a2007-05-05 12:11:21 -07009534 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009535
David S. Millerf47c11e2005-06-24 20:18:35 -07009536 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009537
Matt Carlson4f125f42009-09-01 12:55:02 +00009538 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9539 struct tg3_napi *tnapi = &tp->napi[i];
9540 free_irq(tnapi->irq_vec, tnapi);
9541 }
Matt Carlson07b01732009-08-28 14:01:15 +00009542
9543 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009544
Eric Dumazet511d2222010-07-07 20:44:24 +00009545 tg3_get_stats64(tp->dev, &tp->net_stats_prev);
9546
Linus Torvalds1da177e2005-04-16 15:20:36 -07009547 memcpy(&tp->estats_prev, tg3_get_estats(tp),
9548 sizeof(tp->estats_prev));
9549
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009550 tg3_napi_fini(tp);
9551
Linus Torvalds1da177e2005-04-16 15:20:36 -07009552 tg3_free_consistent(tp);
9553
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009554 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -08009555
9556 netif_carrier_off(tp->dev);
9557
Linus Torvalds1da177e2005-04-16 15:20:36 -07009558 return 0;
9559}
9560
Eric Dumazet511d2222010-07-07 20:44:24 +00009561static inline u64 get_stat64(tg3_stat64_t *val)
Stefan Buehler816f8b82008-08-15 14:10:54 -07009562{
9563 return ((u64)val->high << 32) | ((u64)val->low);
9564}
9565
Eric Dumazet511d2222010-07-07 20:44:24 +00009566static u64 calc_crc_errors(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009567{
9568 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9569
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009570 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009571 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
9572 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009573 u32 val;
9574
David S. Millerf47c11e2005-06-24 20:18:35 -07009575 spin_lock_bh(&tp->lock);
Michael Chan569a5df2007-02-13 12:18:15 -08009576 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
9577 tg3_writephy(tp, MII_TG3_TEST1,
9578 val | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009579 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009580 } else
9581 val = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07009582 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009583
9584 tp->phy_crc_errors += val;
9585
9586 return tp->phy_crc_errors;
9587 }
9588
9589 return get_stat64(&hw_stats->rx_fcs_errors);
9590}
9591
9592#define ESTAT_ADD(member) \
9593 estats->member = old_estats->member + \
Eric Dumazet511d2222010-07-07 20:44:24 +00009594 get_stat64(&hw_stats->member)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009595
9596static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *tp)
9597{
9598 struct tg3_ethtool_stats *estats = &tp->estats;
9599 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
9600 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9601
9602 if (!hw_stats)
9603 return old_estats;
9604
9605 ESTAT_ADD(rx_octets);
9606 ESTAT_ADD(rx_fragments);
9607 ESTAT_ADD(rx_ucast_packets);
9608 ESTAT_ADD(rx_mcast_packets);
9609 ESTAT_ADD(rx_bcast_packets);
9610 ESTAT_ADD(rx_fcs_errors);
9611 ESTAT_ADD(rx_align_errors);
9612 ESTAT_ADD(rx_xon_pause_rcvd);
9613 ESTAT_ADD(rx_xoff_pause_rcvd);
9614 ESTAT_ADD(rx_mac_ctrl_rcvd);
9615 ESTAT_ADD(rx_xoff_entered);
9616 ESTAT_ADD(rx_frame_too_long_errors);
9617 ESTAT_ADD(rx_jabbers);
9618 ESTAT_ADD(rx_undersize_packets);
9619 ESTAT_ADD(rx_in_length_errors);
9620 ESTAT_ADD(rx_out_length_errors);
9621 ESTAT_ADD(rx_64_or_less_octet_packets);
9622 ESTAT_ADD(rx_65_to_127_octet_packets);
9623 ESTAT_ADD(rx_128_to_255_octet_packets);
9624 ESTAT_ADD(rx_256_to_511_octet_packets);
9625 ESTAT_ADD(rx_512_to_1023_octet_packets);
9626 ESTAT_ADD(rx_1024_to_1522_octet_packets);
9627 ESTAT_ADD(rx_1523_to_2047_octet_packets);
9628 ESTAT_ADD(rx_2048_to_4095_octet_packets);
9629 ESTAT_ADD(rx_4096_to_8191_octet_packets);
9630 ESTAT_ADD(rx_8192_to_9022_octet_packets);
9631
9632 ESTAT_ADD(tx_octets);
9633 ESTAT_ADD(tx_collisions);
9634 ESTAT_ADD(tx_xon_sent);
9635 ESTAT_ADD(tx_xoff_sent);
9636 ESTAT_ADD(tx_flow_control);
9637 ESTAT_ADD(tx_mac_errors);
9638 ESTAT_ADD(tx_single_collisions);
9639 ESTAT_ADD(tx_mult_collisions);
9640 ESTAT_ADD(tx_deferred);
9641 ESTAT_ADD(tx_excessive_collisions);
9642 ESTAT_ADD(tx_late_collisions);
9643 ESTAT_ADD(tx_collide_2times);
9644 ESTAT_ADD(tx_collide_3times);
9645 ESTAT_ADD(tx_collide_4times);
9646 ESTAT_ADD(tx_collide_5times);
9647 ESTAT_ADD(tx_collide_6times);
9648 ESTAT_ADD(tx_collide_7times);
9649 ESTAT_ADD(tx_collide_8times);
9650 ESTAT_ADD(tx_collide_9times);
9651 ESTAT_ADD(tx_collide_10times);
9652 ESTAT_ADD(tx_collide_11times);
9653 ESTAT_ADD(tx_collide_12times);
9654 ESTAT_ADD(tx_collide_13times);
9655 ESTAT_ADD(tx_collide_14times);
9656 ESTAT_ADD(tx_collide_15times);
9657 ESTAT_ADD(tx_ucast_packets);
9658 ESTAT_ADD(tx_mcast_packets);
9659 ESTAT_ADD(tx_bcast_packets);
9660 ESTAT_ADD(tx_carrier_sense_errors);
9661 ESTAT_ADD(tx_discards);
9662 ESTAT_ADD(tx_errors);
9663
9664 ESTAT_ADD(dma_writeq_full);
9665 ESTAT_ADD(dma_write_prioq_full);
9666 ESTAT_ADD(rxbds_empty);
9667 ESTAT_ADD(rx_discards);
9668 ESTAT_ADD(rx_errors);
9669 ESTAT_ADD(rx_threshold_hit);
9670
9671 ESTAT_ADD(dma_readq_full);
9672 ESTAT_ADD(dma_read_prioq_full);
9673 ESTAT_ADD(tx_comp_queue_full);
9674
9675 ESTAT_ADD(ring_set_send_prod_index);
9676 ESTAT_ADD(ring_status_update);
9677 ESTAT_ADD(nic_irqs);
9678 ESTAT_ADD(nic_avoided_irqs);
9679 ESTAT_ADD(nic_tx_threshold_hit);
9680
9681 return estats;
9682}
9683
Eric Dumazet511d2222010-07-07 20:44:24 +00009684static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
9685 struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009686{
9687 struct tg3 *tp = netdev_priv(dev);
Eric Dumazet511d2222010-07-07 20:44:24 +00009688 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009689 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9690
9691 if (!hw_stats)
9692 return old_stats;
9693
9694 stats->rx_packets = old_stats->rx_packets +
9695 get_stat64(&hw_stats->rx_ucast_packets) +
9696 get_stat64(&hw_stats->rx_mcast_packets) +
9697 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009698
Linus Torvalds1da177e2005-04-16 15:20:36 -07009699 stats->tx_packets = old_stats->tx_packets +
9700 get_stat64(&hw_stats->tx_ucast_packets) +
9701 get_stat64(&hw_stats->tx_mcast_packets) +
9702 get_stat64(&hw_stats->tx_bcast_packets);
9703
9704 stats->rx_bytes = old_stats->rx_bytes +
9705 get_stat64(&hw_stats->rx_octets);
9706 stats->tx_bytes = old_stats->tx_bytes +
9707 get_stat64(&hw_stats->tx_octets);
9708
9709 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -07009710 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009711 stats->tx_errors = old_stats->tx_errors +
9712 get_stat64(&hw_stats->tx_errors) +
9713 get_stat64(&hw_stats->tx_mac_errors) +
9714 get_stat64(&hw_stats->tx_carrier_sense_errors) +
9715 get_stat64(&hw_stats->tx_discards);
9716
9717 stats->multicast = old_stats->multicast +
9718 get_stat64(&hw_stats->rx_mcast_packets);
9719 stats->collisions = old_stats->collisions +
9720 get_stat64(&hw_stats->tx_collisions);
9721
9722 stats->rx_length_errors = old_stats->rx_length_errors +
9723 get_stat64(&hw_stats->rx_frame_too_long_errors) +
9724 get_stat64(&hw_stats->rx_undersize_packets);
9725
9726 stats->rx_over_errors = old_stats->rx_over_errors +
9727 get_stat64(&hw_stats->rxbds_empty);
9728 stats->rx_frame_errors = old_stats->rx_frame_errors +
9729 get_stat64(&hw_stats->rx_align_errors);
9730 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
9731 get_stat64(&hw_stats->tx_discards);
9732 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
9733 get_stat64(&hw_stats->tx_carrier_sense_errors);
9734
9735 stats->rx_crc_errors = old_stats->rx_crc_errors +
9736 calc_crc_errors(tp);
9737
John W. Linville4f63b872005-09-12 14:43:18 -07009738 stats->rx_missed_errors = old_stats->rx_missed_errors +
9739 get_stat64(&hw_stats->rx_discards);
9740
Eric Dumazetb0057c52010-10-10 19:55:52 +00009741 stats->rx_dropped = tp->rx_dropped;
9742
Linus Torvalds1da177e2005-04-16 15:20:36 -07009743 return stats;
9744}
9745
9746static inline u32 calc_crc(unsigned char *buf, int len)
9747{
9748 u32 reg;
9749 u32 tmp;
9750 int j, k;
9751
9752 reg = 0xffffffff;
9753
9754 for (j = 0; j < len; j++) {
9755 reg ^= buf[j];
9756
9757 for (k = 0; k < 8; k++) {
9758 tmp = reg & 0x01;
9759
9760 reg >>= 1;
9761
Matt Carlson859a588792010-04-05 10:19:28 +00009762 if (tmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009763 reg ^= 0xedb88320;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009764 }
9765 }
9766
9767 return ~reg;
9768}
9769
9770static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
9771{
9772 /* accept or reject all multicast frames */
9773 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
9774 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
9775 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
9776 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
9777}
9778
9779static void __tg3_set_rx_mode(struct net_device *dev)
9780{
9781 struct tg3 *tp = netdev_priv(dev);
9782 u32 rx_mode;
9783
9784 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
9785 RX_MODE_KEEP_VLAN_TAG);
9786
Matt Carlsonbf933c82011-01-25 15:58:49 +00009787#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009788 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
9789 * flag clear.
9790 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009791 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
9792 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9793#endif
9794
9795 if (dev->flags & IFF_PROMISC) {
9796 /* Promiscuous mode. */
9797 rx_mode |= RX_MODE_PROMISC;
9798 } else if (dev->flags & IFF_ALLMULTI) {
9799 /* Accept all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +00009800 tg3_set_multi(tp, 1);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00009801 } else if (netdev_mc_empty(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009802 /* Reject all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +00009803 tg3_set_multi(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009804 } else {
9805 /* Accept one or more multicast(s). */
Jiri Pirko22bedad32010-04-01 21:22:57 +00009806 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009807 u32 mc_filter[4] = { 0, };
9808 u32 regidx;
9809 u32 bit;
9810 u32 crc;
9811
Jiri Pirko22bedad32010-04-01 21:22:57 +00009812 netdev_for_each_mc_addr(ha, dev) {
9813 crc = calc_crc(ha->addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009814 bit = ~crc & 0x7f;
9815 regidx = (bit & 0x60) >> 5;
9816 bit &= 0x1f;
9817 mc_filter[regidx] |= (1 << bit);
9818 }
9819
9820 tw32(MAC_HASH_REG_0, mc_filter[0]);
9821 tw32(MAC_HASH_REG_1, mc_filter[1]);
9822 tw32(MAC_HASH_REG_2, mc_filter[2]);
9823 tw32(MAC_HASH_REG_3, mc_filter[3]);
9824 }
9825
9826 if (rx_mode != tp->rx_mode) {
9827 tp->rx_mode = rx_mode;
9828 tw32_f(MAC_RX_MODE, rx_mode);
9829 udelay(10);
9830 }
9831}
9832
9833static void tg3_set_rx_mode(struct net_device *dev)
9834{
9835 struct tg3 *tp = netdev_priv(dev);
9836
Michael Chane75f7c92006-03-20 21:33:26 -08009837 if (!netif_running(dev))
9838 return;
9839
David S. Millerf47c11e2005-06-24 20:18:35 -07009840 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009841 __tg3_set_rx_mode(dev);
David S. Millerf47c11e2005-06-24 20:18:35 -07009842 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009843}
9844
Linus Torvalds1da177e2005-04-16 15:20:36 -07009845static int tg3_get_regs_len(struct net_device *dev)
9846{
Matt Carlson97bd8e42011-04-13 11:05:04 +00009847 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009848}
9849
9850static void tg3_get_regs(struct net_device *dev,
9851 struct ethtool_regs *regs, void *_p)
9852{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009853 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009854
9855 regs->version = 0;
9856
Matt Carlson97bd8e42011-04-13 11:05:04 +00009857 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009858
Matt Carlson80096062010-08-02 11:26:06 +00009859 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -08009860 return;
9861
David S. Millerf47c11e2005-06-24 20:18:35 -07009862 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009863
Matt Carlson97bd8e42011-04-13 11:05:04 +00009864 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009865
David S. Millerf47c11e2005-06-24 20:18:35 -07009866 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009867}
9868
9869static int tg3_get_eeprom_len(struct net_device *dev)
9870{
9871 struct tg3 *tp = netdev_priv(dev);
9872
9873 return tp->nvram_size;
9874}
9875
Linus Torvalds1da177e2005-04-16 15:20:36 -07009876static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9877{
9878 struct tg3 *tp = netdev_priv(dev);
9879 int ret;
9880 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -08009881 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009882 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009883
Matt Carlsondf259d82009-04-20 06:57:14 +00009884 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM)
9885 return -EINVAL;
9886
Matt Carlson80096062010-08-02 11:26:06 +00009887 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -08009888 return -EAGAIN;
9889
Linus Torvalds1da177e2005-04-16 15:20:36 -07009890 offset = eeprom->offset;
9891 len = eeprom->len;
9892 eeprom->len = 0;
9893
9894 eeprom->magic = TG3_EEPROM_MAGIC;
9895
9896 if (offset & 3) {
9897 /* adjustments to start on required 4 byte boundary */
9898 b_offset = offset & 3;
9899 b_count = 4 - b_offset;
9900 if (b_count > len) {
9901 /* i.e. offset=1 len=2 */
9902 b_count = len;
9903 }
Matt Carlsona9dc5292009-02-25 14:25:30 +00009904 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009905 if (ret)
9906 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +00009907 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009908 len -= b_count;
9909 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00009910 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009911 }
9912
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009913 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009914 pd = &data[eeprom->len];
9915 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +00009916 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009917 if (ret) {
9918 eeprom->len += i;
9919 return ret;
9920 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009921 memcpy(pd + i, &val, 4);
9922 }
9923 eeprom->len += i;
9924
9925 if (len & 3) {
9926 /* read last bytes not ending on 4 byte boundary */
9927 pd = &data[eeprom->len];
9928 b_count = len & 3;
9929 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009930 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009931 if (ret)
9932 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009933 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009934 eeprom->len += b_count;
9935 }
9936 return 0;
9937}
9938
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009939static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009940
9941static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9942{
9943 struct tg3 *tp = netdev_priv(dev);
9944 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009945 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009946 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009947 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009948
Matt Carlson80096062010-08-02 11:26:06 +00009949 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -08009950 return -EAGAIN;
9951
Matt Carlsondf259d82009-04-20 06:57:14 +00009952 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
9953 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009954 return -EINVAL;
9955
9956 offset = eeprom->offset;
9957 len = eeprom->len;
9958
9959 if ((b_offset = (offset & 3))) {
9960 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +00009961 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009962 if (ret)
9963 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009964 len += b_offset;
9965 offset &= ~3;
Michael Chan1c8594b42005-04-21 17:12:46 -07009966 if (len < 4)
9967 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009968 }
9969
9970 odd_len = 0;
Michael Chan1c8594b42005-04-21 17:12:46 -07009971 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009972 /* adjustments to end on required 4 byte boundary */
9973 odd_len = 1;
9974 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009975 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009976 if (ret)
9977 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009978 }
9979
9980 buf = data;
9981 if (b_offset || odd_len) {
9982 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009983 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009984 return -ENOMEM;
9985 if (b_offset)
9986 memcpy(buf, &start, 4);
9987 if (odd_len)
9988 memcpy(buf+len-4, &end, 4);
9989 memcpy(buf + b_offset, data, eeprom->len);
9990 }
9991
9992 ret = tg3_nvram_write_block(tp, offset, len, buf);
9993
9994 if (buf != data)
9995 kfree(buf);
9996
9997 return ret;
9998}
9999
10000static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
10001{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010002 struct tg3 *tp = netdev_priv(dev);
10003
10004 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010005 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010006 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010007 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010008 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10009 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010010 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010011
Linus Torvalds1da177e2005-04-16 15:20:36 -070010012 cmd->supported = (SUPPORTED_Autoneg);
10013
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010014 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010015 cmd->supported |= (SUPPORTED_1000baseT_Half |
10016 SUPPORTED_1000baseT_Full);
10017
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010018 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010019 cmd->supported |= (SUPPORTED_100baseT_Half |
10020 SUPPORTED_100baseT_Full |
10021 SUPPORTED_10baseT_Half |
10022 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -080010023 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -070010024 cmd->port = PORT_TP;
10025 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010026 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -070010027 cmd->port = PORT_FIBRE;
10028 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010029
Linus Torvalds1da177e2005-04-16 15:20:36 -070010030 cmd->advertising = tp->link_config.advertising;
10031 if (netif_running(dev)) {
10032 cmd->speed = tp->link_config.active_speed;
10033 cmd->duplex = tp->link_config.active_duplex;
Matt Carlson64c22182010-10-14 10:37:44 +000010034 } else {
10035 cmd->speed = SPEED_INVALID;
10036 cmd->duplex = DUPLEX_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010037 }
Matt Carlson882e9792009-09-01 13:21:36 +000010038 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +000010039 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010040 cmd->autoneg = tp->link_config.autoneg;
10041 cmd->maxtxpkt = 0;
10042 cmd->maxrxpkt = 0;
10043 return 0;
10044}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010045
Linus Torvalds1da177e2005-04-16 15:20:36 -070010046static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
10047{
10048 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010049
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010050 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010051 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010052 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010053 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010054 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10055 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010056 }
10057
Matt Carlson7e5856b2009-02-25 14:23:01 +000010058 if (cmd->autoneg != AUTONEG_ENABLE &&
10059 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -070010060 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +000010061
10062 if (cmd->autoneg == AUTONEG_DISABLE &&
10063 cmd->duplex != DUPLEX_FULL &&
10064 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -070010065 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010066
Matt Carlson7e5856b2009-02-25 14:23:01 +000010067 if (cmd->autoneg == AUTONEG_ENABLE) {
10068 u32 mask = ADVERTISED_Autoneg |
10069 ADVERTISED_Pause |
10070 ADVERTISED_Asym_Pause;
10071
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010072 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010073 mask |= ADVERTISED_1000baseT_Half |
10074 ADVERTISED_1000baseT_Full;
10075
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010076 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010077 mask |= ADVERTISED_100baseT_Half |
10078 ADVERTISED_100baseT_Full |
10079 ADVERTISED_10baseT_Half |
10080 ADVERTISED_10baseT_Full |
10081 ADVERTISED_TP;
10082 else
10083 mask |= ADVERTISED_FIBRE;
10084
10085 if (cmd->advertising & ~mask)
10086 return -EINVAL;
10087
10088 mask &= (ADVERTISED_1000baseT_Half |
10089 ADVERTISED_1000baseT_Full |
10090 ADVERTISED_100baseT_Half |
10091 ADVERTISED_100baseT_Full |
10092 ADVERTISED_10baseT_Half |
10093 ADVERTISED_10baseT_Full);
10094
10095 cmd->advertising &= mask;
10096 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010097 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
Matt Carlson7e5856b2009-02-25 14:23:01 +000010098 if (cmd->speed != SPEED_1000)
10099 return -EINVAL;
10100
10101 if (cmd->duplex != DUPLEX_FULL)
10102 return -EINVAL;
10103 } else {
10104 if (cmd->speed != SPEED_100 &&
10105 cmd->speed != SPEED_10)
10106 return -EINVAL;
10107 }
10108 }
10109
David S. Millerf47c11e2005-06-24 20:18:35 -070010110 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010111
10112 tp->link_config.autoneg = cmd->autoneg;
10113 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -070010114 tp->link_config.advertising = (cmd->advertising |
10115 ADVERTISED_Autoneg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010116 tp->link_config.speed = SPEED_INVALID;
10117 tp->link_config.duplex = DUPLEX_INVALID;
10118 } else {
10119 tp->link_config.advertising = 0;
10120 tp->link_config.speed = cmd->speed;
10121 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010122 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010123
Michael Chan24fcad62006-12-17 17:06:46 -080010124 tp->link_config.orig_speed = tp->link_config.speed;
10125 tp->link_config.orig_duplex = tp->link_config.duplex;
10126 tp->link_config.orig_autoneg = tp->link_config.autoneg;
10127
Linus Torvalds1da177e2005-04-16 15:20:36 -070010128 if (netif_running(dev))
10129 tg3_setup_phy(tp, 1);
10130
David S. Millerf47c11e2005-06-24 20:18:35 -070010131 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010132
Linus Torvalds1da177e2005-04-16 15:20:36 -070010133 return 0;
10134}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010135
Linus Torvalds1da177e2005-04-16 15:20:36 -070010136static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
10137{
10138 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010139
Linus Torvalds1da177e2005-04-16 15:20:36 -070010140 strcpy(info->driver, DRV_MODULE_NAME);
10141 strcpy(info->version, DRV_MODULE_VERSION);
Michael Chanc4e65752006-03-20 22:29:32 -080010142 strcpy(info->fw_version, tp->fw_ver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010143 strcpy(info->bus_info, pci_name(tp->pdev));
10144}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010145
Linus Torvalds1da177e2005-04-16 15:20:36 -070010146static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10147{
10148 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010149
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070010150 if ((tp->tg3_flags & TG3_FLAG_WOL_CAP) &&
10151 device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070010152 wol->supported = WAKE_MAGIC;
10153 else
10154 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010155 wol->wolopts = 0;
Matt Carlson05ac4cb2008-11-03 16:53:46 -080010156 if ((tp->tg3_flags & TG3_FLAG_WOL_ENABLE) &&
10157 device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010158 wol->wolopts = WAKE_MAGIC;
10159 memset(&wol->sopass, 0, sizeof(wol->sopass));
10160}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010161
Linus Torvalds1da177e2005-04-16 15:20:36 -070010162static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10163{
10164 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070010165 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010166
Linus Torvalds1da177e2005-04-16 15:20:36 -070010167 if (wol->wolopts & ~WAKE_MAGIC)
10168 return -EINVAL;
10169 if ((wol->wolopts & WAKE_MAGIC) &&
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070010170 !((tp->tg3_flags & TG3_FLAG_WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010171 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010172
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010173 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
10174
David S. Millerf47c11e2005-06-24 20:18:35 -070010175 spin_lock_bh(&tp->lock);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010176 if (device_may_wakeup(dp))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010177 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010178 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070010179 tp->tg3_flags &= ~TG3_FLAG_WOL_ENABLE;
David S. Millerf47c11e2005-06-24 20:18:35 -070010180 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010181
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010182
Linus Torvalds1da177e2005-04-16 15:20:36 -070010183 return 0;
10184}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010185
Linus Torvalds1da177e2005-04-16 15:20:36 -070010186static u32 tg3_get_msglevel(struct net_device *dev)
10187{
10188 struct tg3 *tp = netdev_priv(dev);
10189 return tp->msg_enable;
10190}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010191
Linus Torvalds1da177e2005-04-16 15:20:36 -070010192static void tg3_set_msglevel(struct net_device *dev, u32 value)
10193{
10194 struct tg3 *tp = netdev_priv(dev);
10195 tp->msg_enable = value;
10196}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010197
Linus Torvalds1da177e2005-04-16 15:20:36 -070010198static int tg3_nway_reset(struct net_device *dev)
10199{
10200 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010201 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010202
Linus Torvalds1da177e2005-04-16 15:20:36 -070010203 if (!netif_running(dev))
10204 return -EAGAIN;
10205
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010206 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070010207 return -EINVAL;
10208
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010209 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010210 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010211 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010212 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010213 } else {
10214 u32 bmcr;
10215
10216 spin_lock_bh(&tp->lock);
10217 r = -EINVAL;
10218 tg3_readphy(tp, MII_BMCR, &bmcr);
10219 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
10220 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010221 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010222 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
10223 BMCR_ANENABLE);
10224 r = 0;
10225 }
10226 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010227 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010228
Linus Torvalds1da177e2005-04-16 15:20:36 -070010229 return r;
10230}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010231
Linus Torvalds1da177e2005-04-16 15:20:36 -070010232static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10233{
10234 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010235
Matt Carlson2c49a442010-09-30 10:34:35 +000010236 ering->rx_max_pending = tp->rx_std_ring_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010237 ering->rx_mini_max_pending = 0;
Michael Chan4f81c322006-03-20 21:33:42 -080010238 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE)
Matt Carlson2c49a442010-09-30 10:34:35 +000010239 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080010240 else
10241 ering->rx_jumbo_max_pending = 0;
10242
10243 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010244
10245 ering->rx_pending = tp->rx_pending;
10246 ering->rx_mini_pending = 0;
Michael Chan4f81c322006-03-20 21:33:42 -080010247 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE)
10248 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
10249 else
10250 ering->rx_jumbo_pending = 0;
10251
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010252 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010253}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010254
Linus Torvalds1da177e2005-04-16 15:20:36 -070010255static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10256{
10257 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000010258 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010259
Matt Carlson2c49a442010-09-30 10:34:35 +000010260 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
10261 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070010262 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
10263 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Michael Chan7f62ad52007-02-20 23:25:40 -080010264 ((tp->tg3_flags2 & TG3_FLG2_TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070010265 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010266 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010267
Michael Chanbbe832c2005-06-24 20:20:04 -070010268 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010269 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010270 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010271 irq_sync = 1;
10272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010273
Michael Chanbbe832c2005-06-24 20:20:04 -070010274 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010275
Linus Torvalds1da177e2005-04-16 15:20:36 -070010276 tp->rx_pending = ering->rx_pending;
10277
10278 if ((tp->tg3_flags2 & TG3_FLG2_MAX_RXPEND_64) &&
10279 tp->rx_pending > 63)
10280 tp->rx_pending = 63;
10281 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000010282
Matt Carlson6fd45cb2010-09-15 08:59:57 +000010283 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000010284 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010285
10286 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070010287 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -070010288 err = tg3_restart_hw(tp, 1);
10289 if (!err)
10290 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010291 }
10292
David S. Millerf47c11e2005-06-24 20:18:35 -070010293 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010294
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010295 if (irq_sync && !err)
10296 tg3_phy_start(tp);
10297
Michael Chanb9ec6c12006-07-25 16:37:27 -070010298 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010299}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010300
Linus Torvalds1da177e2005-04-16 15:20:36 -070010301static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10302{
10303 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010304
Linus Torvalds1da177e2005-04-16 15:20:36 -070010305 epause->autoneg = (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG) != 0;
Matt Carlson8d018622007-12-20 20:05:44 -080010306
Steve Glendinninge18ce342008-12-16 02:00:00 -080010307 if (tp->link_config.active_flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080010308 epause->rx_pause = 1;
10309 else
10310 epause->rx_pause = 0;
10311
Steve Glendinninge18ce342008-12-16 02:00:00 -080010312 if (tp->link_config.active_flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080010313 epause->tx_pause = 1;
10314 else
10315 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010316}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010317
Linus Torvalds1da177e2005-04-16 15:20:36 -070010318static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10319{
10320 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010321 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010322
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010323 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson27121682010-02-17 15:16:57 +000010324 u32 newadv;
10325 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010326
Matt Carlson27121682010-02-17 15:16:57 +000010327 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010328
Matt Carlson27121682010-02-17 15:16:57 +000010329 if (!(phydev->supported & SUPPORTED_Pause) ||
10330 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000010331 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000010332 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010333
Matt Carlson27121682010-02-17 15:16:57 +000010334 tp->link_config.flowctrl = 0;
10335 if (epause->rx_pause) {
10336 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010337
Matt Carlson27121682010-02-17 15:16:57 +000010338 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080010339 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000010340 newadv = ADVERTISED_Pause;
10341 } else
10342 newadv = ADVERTISED_Pause |
10343 ADVERTISED_Asym_Pause;
10344 } else if (epause->tx_pause) {
10345 tp->link_config.flowctrl |= FLOW_CTRL_TX;
10346 newadv = ADVERTISED_Asym_Pause;
10347 } else
10348 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010349
Matt Carlson27121682010-02-17 15:16:57 +000010350 if (epause->autoneg)
10351 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
10352 else
10353 tp->tg3_flags &= ~TG3_FLAG_PAUSE_AUTONEG;
10354
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010355 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000010356 u32 oldadv = phydev->advertising &
10357 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
10358 if (oldadv != newadv) {
10359 phydev->advertising &=
10360 ~(ADVERTISED_Pause |
10361 ADVERTISED_Asym_Pause);
10362 phydev->advertising |= newadv;
10363 if (phydev->autoneg) {
10364 /*
10365 * Always renegotiate the link to
10366 * inform our link partner of our
10367 * flow control settings, even if the
10368 * flow control is forced. Let
10369 * tg3_adjust_link() do the final
10370 * flow control setup.
10371 */
10372 return phy_start_aneg(phydev);
10373 }
10374 }
10375
10376 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010377 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000010378 } else {
10379 tp->link_config.orig_advertising &=
10380 ~(ADVERTISED_Pause |
10381 ADVERTISED_Asym_Pause);
10382 tp->link_config.orig_advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010383 }
10384 } else {
10385 int irq_sync = 0;
10386
10387 if (netif_running(dev)) {
10388 tg3_netif_stop(tp);
10389 irq_sync = 1;
10390 }
10391
10392 tg3_full_lock(tp, irq_sync);
10393
10394 if (epause->autoneg)
10395 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
10396 else
10397 tp->tg3_flags &= ~TG3_FLAG_PAUSE_AUTONEG;
10398 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010399 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010400 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010401 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010402 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010403 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010404 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010405 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010406
10407 if (netif_running(dev)) {
10408 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10409 err = tg3_restart_hw(tp, 1);
10410 if (!err)
10411 tg3_netif_start(tp);
10412 }
10413
10414 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010416
Michael Chanb9ec6c12006-07-25 16:37:27 -070010417 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010418}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010419
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010420static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010421{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010422 switch (sset) {
10423 case ETH_SS_TEST:
10424 return TG3_NUM_TEST;
10425 case ETH_SS_STATS:
10426 return TG3_NUM_STATS;
10427 default:
10428 return -EOPNOTSUPP;
10429 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070010430}
10431
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010432static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010433{
10434 switch (stringset) {
10435 case ETH_SS_STATS:
10436 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
10437 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070010438 case ETH_SS_TEST:
10439 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
10440 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010441 default:
10442 WARN_ON(1); /* we need a WARN() */
10443 break;
10444 }
10445}
10446
stephen hemminger81b87092011-04-04 08:43:50 +000010447static int tg3_set_phys_id(struct net_device *dev,
10448 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070010449{
10450 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070010451
10452 if (!netif_running(tp->dev))
10453 return -EAGAIN;
10454
stephen hemminger81b87092011-04-04 08:43:50 +000010455 switch (state) {
10456 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +000010457 return 1; /* cycle on/off once per second */
Michael Chan4009a932005-09-05 17:52:54 -070010458
stephen hemminger81b87092011-04-04 08:43:50 +000010459 case ETHTOOL_ID_ON:
10460 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10461 LED_CTRL_1000MBPS_ON |
10462 LED_CTRL_100MBPS_ON |
10463 LED_CTRL_10MBPS_ON |
10464 LED_CTRL_TRAFFIC_OVERRIDE |
10465 LED_CTRL_TRAFFIC_BLINK |
10466 LED_CTRL_TRAFFIC_LED);
10467 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010468
stephen hemminger81b87092011-04-04 08:43:50 +000010469 case ETHTOOL_ID_OFF:
10470 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10471 LED_CTRL_TRAFFIC_OVERRIDE);
10472 break;
Michael Chan4009a932005-09-05 17:52:54 -070010473
stephen hemminger81b87092011-04-04 08:43:50 +000010474 case ETHTOOL_ID_INACTIVE:
10475 tw32(MAC_LED_CTRL, tp->led_ctrl);
10476 break;
Michael Chan4009a932005-09-05 17:52:54 -070010477 }
stephen hemminger81b87092011-04-04 08:43:50 +000010478
Michael Chan4009a932005-09-05 17:52:54 -070010479 return 0;
10480}
10481
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010482static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010483 struct ethtool_stats *estats, u64 *tmp_stats)
10484{
10485 struct tg3 *tp = netdev_priv(dev);
10486 memcpy(tmp_stats, tg3_get_estats(tp), sizeof(tp->estats));
10487}
10488
Matt Carlsonc3e94502011-04-13 11:05:08 +000010489static __be32 * tg3_vpd_readblock(struct tg3 *tp)
10490{
10491 int i;
10492 __be32 *buf;
10493 u32 offset = 0, len = 0;
10494 u32 magic, val;
10495
10496 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
10497 tg3_nvram_read(tp, 0, &magic))
10498 return NULL;
10499
10500 if (magic == TG3_EEPROM_MAGIC) {
10501 for (offset = TG3_NVM_DIR_START;
10502 offset < TG3_NVM_DIR_END;
10503 offset += TG3_NVM_DIRENT_SIZE) {
10504 if (tg3_nvram_read(tp, offset, &val))
10505 return NULL;
10506
10507 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
10508 TG3_NVM_DIRTYPE_EXTVPD)
10509 break;
10510 }
10511
10512 if (offset != TG3_NVM_DIR_END) {
10513 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
10514 if (tg3_nvram_read(tp, offset + 4, &offset))
10515 return NULL;
10516
10517 offset = tg3_nvram_logical_addr(tp, offset);
10518 }
10519 }
10520
10521 if (!offset || !len) {
10522 offset = TG3_NVM_VPD_OFF;
10523 len = TG3_NVM_VPD_LEN;
10524 }
10525
10526 buf = kmalloc(len, GFP_KERNEL);
10527 if (buf == NULL)
10528 return NULL;
10529
10530 if (magic == TG3_EEPROM_MAGIC) {
10531 for (i = 0; i < len; i += 4) {
10532 /* The data is in little-endian format in NVRAM.
10533 * Use the big-endian read routines to preserve
10534 * the byte order as it exists in NVRAM.
10535 */
10536 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
10537 goto error;
10538 }
10539 } else {
10540 u8 *ptr;
10541 ssize_t cnt;
10542 unsigned int pos = 0;
10543
10544 ptr = (u8 *)&buf[0];
10545 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
10546 cnt = pci_read_vpd(tp->pdev, pos,
10547 len - pos, ptr);
10548 if (cnt == -ETIMEDOUT || cnt == -EINTR)
10549 cnt = 0;
10550 else if (cnt < 0)
10551 goto error;
10552 }
10553 if (pos != len)
10554 goto error;
10555 }
10556
10557 return buf;
10558
10559error:
10560 kfree(buf);
10561 return NULL;
10562}
10563
Michael Chan566f86a2005-05-29 14:56:58 -070010564#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080010565#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
10566#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
10567#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Michael Chanb16250e2006-09-27 16:10:14 -070010568#define NVRAM_SELFBOOT_HW_SIZE 0x20
10569#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070010570
10571static int tg3_test_nvram(struct tg3 *tp)
10572{
Al Virob9fc7dc2007-12-17 22:59:57 -080010573 u32 csum, magic;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010574 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010575 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070010576
Matt Carlsondf259d82009-04-20 06:57:14 +000010577 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM)
10578 return 0;
10579
Matt Carlsone4f34112009-02-25 14:25:00 +000010580 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080010581 return -EIO;
10582
Michael Chan1b277772006-03-20 22:27:48 -080010583 if (magic == TG3_EEPROM_MAGIC)
10584 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070010585 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080010586 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
10587 TG3_EEPROM_SB_FORMAT_1) {
10588 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
10589 case TG3_EEPROM_SB_REVISION_0:
10590 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
10591 break;
10592 case TG3_EEPROM_SB_REVISION_2:
10593 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
10594 break;
10595 case TG3_EEPROM_SB_REVISION_3:
10596 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
10597 break;
10598 default:
10599 return 0;
10600 }
10601 } else
Michael Chan1b277772006-03-20 22:27:48 -080010602 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070010603 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
10604 size = NVRAM_SELFBOOT_HW_SIZE;
10605 else
Michael Chan1b277772006-03-20 22:27:48 -080010606 return -EIO;
10607
10608 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070010609 if (buf == NULL)
10610 return -ENOMEM;
10611
Michael Chan1b277772006-03-20 22:27:48 -080010612 err = -EIO;
10613 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010614 err = tg3_nvram_read_be32(tp, i, &buf[j]);
10615 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070010616 break;
Michael Chan566f86a2005-05-29 14:56:58 -070010617 }
Michael Chan1b277772006-03-20 22:27:48 -080010618 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070010619 goto out;
10620
Michael Chan1b277772006-03-20 22:27:48 -080010621 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010622 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080010623 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010624 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080010625 u8 *buf8 = (u8 *) buf, csum8 = 0;
10626
Al Virob9fc7dc2007-12-17 22:59:57 -080010627 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080010628 TG3_EEPROM_SB_REVISION_2) {
10629 /* For rev 2, the csum doesn't include the MBA. */
10630 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
10631 csum8 += buf8[i];
10632 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
10633 csum8 += buf8[i];
10634 } else {
10635 for (i = 0; i < size; i++)
10636 csum8 += buf8[i];
10637 }
Michael Chan1b277772006-03-20 22:27:48 -080010638
Adrian Bunkad96b482006-04-05 22:21:04 -070010639 if (csum8 == 0) {
10640 err = 0;
10641 goto out;
10642 }
10643
10644 err = -EIO;
10645 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080010646 }
Michael Chan566f86a2005-05-29 14:56:58 -070010647
Al Virob9fc7dc2007-12-17 22:59:57 -080010648 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010649 TG3_EEPROM_MAGIC_HW) {
10650 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000010651 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070010652 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070010653
10654 /* Separate the parity bits and the data bytes. */
10655 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
10656 if ((i == 0) || (i == 8)) {
10657 int l;
10658 u8 msk;
10659
10660 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
10661 parity[k++] = buf8[i] & msk;
10662 i++;
Matt Carlson859a588792010-04-05 10:19:28 +000010663 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070010664 int l;
10665 u8 msk;
10666
10667 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
10668 parity[k++] = buf8[i] & msk;
10669 i++;
10670
10671 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
10672 parity[k++] = buf8[i] & msk;
10673 i++;
10674 }
10675 data[j++] = buf8[i];
10676 }
10677
10678 err = -EIO;
10679 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
10680 u8 hw8 = hweight8(data[i]);
10681
10682 if ((hw8 & 0x1) && parity[i])
10683 goto out;
10684 else if (!(hw8 & 0x1) && !parity[i])
10685 goto out;
10686 }
10687 err = 0;
10688 goto out;
10689 }
10690
Matt Carlson01c3a392011-03-09 16:58:20 +000010691 err = -EIO;
10692
Michael Chan566f86a2005-05-29 14:56:58 -070010693 /* Bootstrap checksum at offset 0x10 */
10694 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000010695 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070010696 goto out;
10697
10698 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
10699 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000010700 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000010701 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070010702
Matt Carlsonc3e94502011-04-13 11:05:08 +000010703 kfree(buf);
10704
10705 buf = tg3_vpd_readblock(tp);
10706 if (!buf)
10707 return -ENOMEM;
Matt Carlsond4894f32011-03-09 16:58:21 +000010708
10709 i = pci_vpd_find_tag((u8 *)buf, 0, TG3_NVM_VPD_LEN,
10710 PCI_VPD_LRDT_RO_DATA);
10711 if (i > 0) {
10712 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
10713 if (j < 0)
10714 goto out;
10715
10716 if (i + PCI_VPD_LRDT_TAG_SIZE + j > TG3_NVM_VPD_LEN)
10717 goto out;
10718
10719 i += PCI_VPD_LRDT_TAG_SIZE;
10720 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
10721 PCI_VPD_RO_KEYWORD_CHKSUM);
10722 if (j > 0) {
10723 u8 csum8 = 0;
10724
10725 j += PCI_VPD_INFO_FLD_HDR_SIZE;
10726
10727 for (i = 0; i <= j; i++)
10728 csum8 += ((u8 *)buf)[i];
10729
10730 if (csum8)
10731 goto out;
10732 }
10733 }
10734
Michael Chan566f86a2005-05-29 14:56:58 -070010735 err = 0;
10736
10737out:
10738 kfree(buf);
10739 return err;
10740}
10741
Michael Chanca430072005-05-29 14:57:23 -070010742#define TG3_SERDES_TIMEOUT_SEC 2
10743#define TG3_COPPER_TIMEOUT_SEC 6
10744
10745static int tg3_test_link(struct tg3 *tp)
10746{
10747 int i, max;
10748
10749 if (!netif_running(tp->dev))
10750 return -ENODEV;
10751
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010752 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070010753 max = TG3_SERDES_TIMEOUT_SEC;
10754 else
10755 max = TG3_COPPER_TIMEOUT_SEC;
10756
10757 for (i = 0; i < max; i++) {
10758 if (netif_carrier_ok(tp->dev))
10759 return 0;
10760
10761 if (msleep_interruptible(1000))
10762 break;
10763 }
10764
10765 return -EIO;
10766}
10767
Michael Chana71116d2005-05-29 14:58:11 -070010768/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080010769static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070010770{
Michael Chanb16250e2006-09-27 16:10:14 -070010771 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070010772 u32 offset, read_mask, write_mask, val, save_val, read_val;
10773 static struct {
10774 u16 offset;
10775 u16 flags;
10776#define TG3_FL_5705 0x1
10777#define TG3_FL_NOT_5705 0x2
10778#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070010779#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070010780 u32 read_mask;
10781 u32 write_mask;
10782 } reg_tbl[] = {
10783 /* MAC Control Registers */
10784 { MAC_MODE, TG3_FL_NOT_5705,
10785 0x00000000, 0x00ef6f8c },
10786 { MAC_MODE, TG3_FL_5705,
10787 0x00000000, 0x01ef6b8c },
10788 { MAC_STATUS, TG3_FL_NOT_5705,
10789 0x03800107, 0x00000000 },
10790 { MAC_STATUS, TG3_FL_5705,
10791 0x03800100, 0x00000000 },
10792 { MAC_ADDR_0_HIGH, 0x0000,
10793 0x00000000, 0x0000ffff },
10794 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010795 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070010796 { MAC_RX_MTU_SIZE, 0x0000,
10797 0x00000000, 0x0000ffff },
10798 { MAC_TX_MODE, 0x0000,
10799 0x00000000, 0x00000070 },
10800 { MAC_TX_LENGTHS, 0x0000,
10801 0x00000000, 0x00003fff },
10802 { MAC_RX_MODE, TG3_FL_NOT_5705,
10803 0x00000000, 0x000007fc },
10804 { MAC_RX_MODE, TG3_FL_5705,
10805 0x00000000, 0x000007dc },
10806 { MAC_HASH_REG_0, 0x0000,
10807 0x00000000, 0xffffffff },
10808 { MAC_HASH_REG_1, 0x0000,
10809 0x00000000, 0xffffffff },
10810 { MAC_HASH_REG_2, 0x0000,
10811 0x00000000, 0xffffffff },
10812 { MAC_HASH_REG_3, 0x0000,
10813 0x00000000, 0xffffffff },
10814
10815 /* Receive Data and Receive BD Initiator Control Registers. */
10816 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
10817 0x00000000, 0xffffffff },
10818 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
10819 0x00000000, 0xffffffff },
10820 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
10821 0x00000000, 0x00000003 },
10822 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
10823 0x00000000, 0xffffffff },
10824 { RCVDBDI_STD_BD+0, 0x0000,
10825 0x00000000, 0xffffffff },
10826 { RCVDBDI_STD_BD+4, 0x0000,
10827 0x00000000, 0xffffffff },
10828 { RCVDBDI_STD_BD+8, 0x0000,
10829 0x00000000, 0xffff0002 },
10830 { RCVDBDI_STD_BD+0xc, 0x0000,
10831 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010832
Michael Chana71116d2005-05-29 14:58:11 -070010833 /* Receive BD Initiator Control Registers. */
10834 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
10835 0x00000000, 0xffffffff },
10836 { RCVBDI_STD_THRESH, TG3_FL_5705,
10837 0x00000000, 0x000003ff },
10838 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
10839 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010840
Michael Chana71116d2005-05-29 14:58:11 -070010841 /* Host Coalescing Control Registers. */
10842 { HOSTCC_MODE, TG3_FL_NOT_5705,
10843 0x00000000, 0x00000004 },
10844 { HOSTCC_MODE, TG3_FL_5705,
10845 0x00000000, 0x000000f6 },
10846 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
10847 0x00000000, 0xffffffff },
10848 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
10849 0x00000000, 0x000003ff },
10850 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
10851 0x00000000, 0xffffffff },
10852 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
10853 0x00000000, 0x000003ff },
10854 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
10855 0x00000000, 0xffffffff },
10856 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10857 0x00000000, 0x000000ff },
10858 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
10859 0x00000000, 0xffffffff },
10860 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10861 0x00000000, 0x000000ff },
10862 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
10863 0x00000000, 0xffffffff },
10864 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
10865 0x00000000, 0xffffffff },
10866 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10867 0x00000000, 0xffffffff },
10868 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10869 0x00000000, 0x000000ff },
10870 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10871 0x00000000, 0xffffffff },
10872 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10873 0x00000000, 0x000000ff },
10874 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
10875 0x00000000, 0xffffffff },
10876 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
10877 0x00000000, 0xffffffff },
10878 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
10879 0x00000000, 0xffffffff },
10880 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
10881 0x00000000, 0xffffffff },
10882 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
10883 0x00000000, 0xffffffff },
10884 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
10885 0xffffffff, 0x00000000 },
10886 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
10887 0xffffffff, 0x00000000 },
10888
10889 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070010890 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010891 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070010892 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010893 0x00000000, 0x007fffff },
10894 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
10895 0x00000000, 0x0000003f },
10896 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
10897 0x00000000, 0x000001ff },
10898 { BUFMGR_MB_HIGH_WATER, 0x0000,
10899 0x00000000, 0x000001ff },
10900 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
10901 0xffffffff, 0x00000000 },
10902 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
10903 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010904
Michael Chana71116d2005-05-29 14:58:11 -070010905 /* Mailbox Registers */
10906 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
10907 0x00000000, 0x000001ff },
10908 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
10909 0x00000000, 0x000001ff },
10910 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
10911 0x00000000, 0x000007ff },
10912 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
10913 0x00000000, 0x000001ff },
10914
10915 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
10916 };
10917
Michael Chanb16250e2006-09-27 16:10:14 -070010918 is_5705 = is_5750 = 0;
10919 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
Michael Chana71116d2005-05-29 14:58:11 -070010920 is_5705 = 1;
Michael Chanb16250e2006-09-27 16:10:14 -070010921 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
10922 is_5750 = 1;
10923 }
Michael Chana71116d2005-05-29 14:58:11 -070010924
10925 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
10926 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
10927 continue;
10928
10929 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
10930 continue;
10931
10932 if ((tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
10933 (reg_tbl[i].flags & TG3_FL_NOT_5788))
10934 continue;
10935
Michael Chanb16250e2006-09-27 16:10:14 -070010936 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
10937 continue;
10938
Michael Chana71116d2005-05-29 14:58:11 -070010939 offset = (u32) reg_tbl[i].offset;
10940 read_mask = reg_tbl[i].read_mask;
10941 write_mask = reg_tbl[i].write_mask;
10942
10943 /* Save the original register content */
10944 save_val = tr32(offset);
10945
10946 /* Determine the read-only value. */
10947 read_val = save_val & read_mask;
10948
10949 /* Write zero to the register, then make sure the read-only bits
10950 * are not changed and the read/write bits are all zeros.
10951 */
10952 tw32(offset, 0);
10953
10954 val = tr32(offset);
10955
10956 /* Test the read-only and read/write bits. */
10957 if (((val & read_mask) != read_val) || (val & write_mask))
10958 goto out;
10959
10960 /* Write ones to all the bits defined by RdMask and WrMask, then
10961 * make sure the read-only bits are not changed and the
10962 * read/write bits are all ones.
10963 */
10964 tw32(offset, read_mask | write_mask);
10965
10966 val = tr32(offset);
10967
10968 /* Test the read-only bits. */
10969 if ((val & read_mask) != read_val)
10970 goto out;
10971
10972 /* Test the read/write bits. */
10973 if ((val & write_mask) != write_mask)
10974 goto out;
10975
10976 tw32(offset, save_val);
10977 }
10978
10979 return 0;
10980
10981out:
Michael Chan9f88f292006-12-07 00:22:54 -080010982 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000010983 netdev_err(tp->dev,
10984 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070010985 tw32(offset, save_val);
10986 return -EIO;
10987}
10988
Michael Chan7942e1d2005-05-29 14:58:36 -070010989static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
10990{
Arjan van de Venf71e1302006-03-03 21:33:57 -050010991 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070010992 int i;
10993 u32 j;
10994
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020010995 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070010996 for (j = 0; j < len; j += 4) {
10997 u32 val;
10998
10999 tg3_write_mem(tp, offset + j, test_pattern[i]);
11000 tg3_read_mem(tp, offset + j, &val);
11001 if (val != test_pattern[i])
11002 return -EIO;
11003 }
11004 }
11005 return 0;
11006}
11007
11008static int tg3_test_memory(struct tg3 *tp)
11009{
11010 static struct mem_entry {
11011 u32 offset;
11012 u32 len;
11013 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080011014 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070011015 { 0x00002000, 0x1c000},
11016 { 0xffffffff, 0x00000}
11017 }, mem_tbl_5705[] = {
11018 { 0x00000100, 0x0000c},
11019 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070011020 { 0x00004000, 0x00800},
11021 { 0x00006000, 0x01000},
11022 { 0x00008000, 0x02000},
11023 { 0x00010000, 0x0e000},
11024 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080011025 }, mem_tbl_5755[] = {
11026 { 0x00000200, 0x00008},
11027 { 0x00004000, 0x00800},
11028 { 0x00006000, 0x00800},
11029 { 0x00008000, 0x02000},
11030 { 0x00010000, 0x0c000},
11031 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070011032 }, mem_tbl_5906[] = {
11033 { 0x00000200, 0x00008},
11034 { 0x00004000, 0x00400},
11035 { 0x00006000, 0x00400},
11036 { 0x00008000, 0x01000},
11037 { 0x00010000, 0x01000},
11038 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011039 }, mem_tbl_5717[] = {
11040 { 0x00000200, 0x00008},
11041 { 0x00010000, 0x0a000},
11042 { 0x00020000, 0x13c00},
11043 { 0xffffffff, 0x00000}
11044 }, mem_tbl_57765[] = {
11045 { 0x00000200, 0x00008},
11046 { 0x00004000, 0x00800},
11047 { 0x00006000, 0x09800},
11048 { 0x00010000, 0x0a000},
11049 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070011050 };
11051 struct mem_entry *mem_tbl;
11052 int err = 0;
11053 int i;
11054
Matt Carlson0a58d662011-04-05 14:22:45 +000011055 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011056 mem_tbl = mem_tbl_5717;
11057 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
11058 mem_tbl = mem_tbl_57765;
11059 else if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Matt Carlson321d32a2008-11-21 17:22:19 -080011060 mem_tbl = mem_tbl_5755;
11061 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
11062 mem_tbl = mem_tbl_5906;
11063 else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
11064 mem_tbl = mem_tbl_5705;
11065 else
Michael Chan7942e1d2005-05-29 14:58:36 -070011066 mem_tbl = mem_tbl_570x;
11067
11068 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000011069 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
11070 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070011071 break;
11072 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011073
Michael Chan7942e1d2005-05-29 14:58:36 -070011074 return err;
11075}
11076
Michael Chan9f40dea2005-09-05 17:53:06 -070011077#define TG3_MAC_LOOPBACK 0
11078#define TG3_PHY_LOOPBACK 1
11079
Matt Carlson4852a862011-04-13 11:05:07 +000011080static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, int loopback_mode)
Michael Chanc76949a2005-05-29 14:58:59 -070011081{
Michael Chan9f40dea2005-09-05 17:53:06 -070011082 u32 mac_mode, rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011083 u32 desc_idx, coal_now;
Michael Chanc76949a2005-05-29 14:58:59 -070011084 struct sk_buff *skb, *rx_skb;
11085 u8 *tx_data;
11086 dma_addr_t map;
11087 int num_pkts, tx_len, rx_len, i, err;
11088 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000011089 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000011090 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070011091
Matt Carlsonc8873402010-02-12 14:47:11 +000011092 tnapi = &tp->napi[0];
11093 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011094 if (tp->irq_cnt > 1) {
Matt Carlson1da85aa2010-09-30 10:34:34 +000011095 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)
11096 rnapi = &tp->napi[1];
Matt Carlsonc8873402010-02-12 14:47:11 +000011097 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
11098 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011099 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011100 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000011101
Michael Chan9f40dea2005-09-05 17:53:06 -070011102 if (loopback_mode == TG3_MAC_LOOPBACK) {
Michael Chanc94e3942005-09-27 12:12:42 -070011103 /* HW errata - mac loopback fails in some cases on 5780.
11104 * Normal traffic and PHY loopback are not affected by
Matt Carlsonaba49f22011-01-25 15:58:53 +000011105 * errata. Also, the MAC loopback test is deprecated for
11106 * all newer ASIC revisions.
Michael Chanc94e3942005-09-27 12:12:42 -070011107 */
Matt Carlsonaba49f22011-01-25 15:58:53 +000011108 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
11109 (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT))
Michael Chanc94e3942005-09-27 12:12:42 -070011110 return 0;
11111
Matt Carlson49692ca2011-01-25 15:58:52 +000011112 mac_mode = tp->mac_mode &
11113 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
11114 mac_mode |= MAC_MODE_PORT_INT_LPBACK;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070011115 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
11116 mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011117 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
Michael Chan3f7045c2006-09-27 16:02:29 -070011118 mac_mode |= MAC_MODE_PORT_MODE_MII;
11119 else
11120 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chan9f40dea2005-09-05 17:53:06 -070011121 tw32(MAC_MODE, mac_mode);
11122 } else if (loopback_mode == TG3_PHY_LOOPBACK) {
Michael Chan3f7045c2006-09-27 16:02:29 -070011123 u32 val;
11124
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011125 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +000011126 tg3_phy_fet_toggle_apd(tp, false);
Michael Chan5d64ad32006-12-07 00:19:40 -080011127 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED100;
11128 } else
11129 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED1000;
Michael Chan3f7045c2006-09-27 16:02:29 -070011130
Matt Carlson9ef8ca92007-07-11 19:48:29 -070011131 tg3_phy_toggle_automdix(tp, 0);
11132
Michael Chan3f7045c2006-09-27 16:02:29 -070011133 tg3_writephy(tp, MII_BMCR, val);
Michael Chanc94e3942005-09-27 12:12:42 -070011134 udelay(40);
Michael Chan5d64ad32006-12-07 00:19:40 -080011135
Matt Carlson49692ca2011-01-25 15:58:52 +000011136 mac_mode = tp->mac_mode &
11137 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011138 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson1061b7c2010-02-12 14:47:12 +000011139 tg3_writephy(tp, MII_TG3_FET_PTEST,
11140 MII_TG3_FET_PTEST_FRC_TX_LINK |
11141 MII_TG3_FET_PTEST_FRC_TX_LOCK);
11142 /* The write needs to be flushed for the AC131 */
11143 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
11144 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
Michael Chan5d64ad32006-12-07 00:19:40 -080011145 mac_mode |= MAC_MODE_PORT_MODE_MII;
11146 } else
11147 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chanb16250e2006-09-27 16:10:14 -070011148
Michael Chanc94e3942005-09-27 12:12:42 -070011149 /* reset to prevent losing 1st rx packet intermittently */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011150 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Michael Chanc94e3942005-09-27 12:12:42 -070011151 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
11152 udelay(10);
11153 tw32_f(MAC_RX_MODE, tp->rx_mode);
11154 }
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070011155 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlson79eb6902010-02-17 15:17:03 +000011156 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
11157 if (masked_phy_id == TG3_PHY_ID_BCM5401)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070011158 mac_mode &= ~MAC_MODE_LINK_POLARITY;
Matt Carlson79eb6902010-02-17 15:17:03 +000011159 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070011160 mac_mode |= MAC_MODE_LINK_POLARITY;
Michael Chanff18ff02006-03-27 23:17:27 -080011161 tg3_writephy(tp, MII_TG3_EXT_CTRL,
11162 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
11163 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011164 tw32(MAC_MODE, mac_mode);
Matt Carlson49692ca2011-01-25 15:58:52 +000011165
11166 /* Wait for link */
11167 for (i = 0; i < 100; i++) {
11168 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
11169 break;
11170 mdelay(1);
11171 }
Matt Carlson859a588792010-04-05 10:19:28 +000011172 } else {
Michael Chan9f40dea2005-09-05 17:53:06 -070011173 return -EINVAL;
Matt Carlson859a588792010-04-05 10:19:28 +000011174 }
Michael Chanc76949a2005-05-29 14:58:59 -070011175
11176 err = -EIO;
11177
Matt Carlson4852a862011-04-13 11:05:07 +000011178 tx_len = pktsz;
David S. Millera20e9c62006-07-31 22:38:16 -070011179 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070011180 if (!skb)
11181 return -ENOMEM;
11182
Michael Chanc76949a2005-05-29 14:58:59 -070011183 tx_data = skb_put(skb, tx_len);
11184 memcpy(tx_data, tp->dev->dev_addr, 6);
11185 memset(tx_data + 6, 0x0, 8);
11186
Matt Carlson4852a862011-04-13 11:05:07 +000011187 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
Michael Chanc76949a2005-05-29 14:58:59 -070011188
11189 for (i = 14; i < tx_len; i++)
11190 tx_data[i] = (u8) (i & 0xff);
11191
Alexander Duyckf4188d82009-12-02 16:48:38 +000011192 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
11193 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000011194 dev_kfree_skb(skb);
11195 return -EIO;
11196 }
Michael Chanc76949a2005-05-29 14:58:59 -070011197
11198 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011199 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011200
11201 udelay(10);
11202
Matt Carlson898a56f2009-08-28 14:02:40 +000011203 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070011204
Michael Chanc76949a2005-05-29 14:58:59 -070011205 num_pkts = 0;
11206
Alexander Duyckf4188d82009-12-02 16:48:38 +000011207 tg3_set_txd(tnapi, tnapi->tx_prod, map, tx_len, 0, 1);
Michael Chanc76949a2005-05-29 14:58:59 -070011208
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011209 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070011210 num_pkts++;
11211
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011212 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
11213 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070011214
11215 udelay(10);
11216
Matt Carlson303fc922009-11-02 14:27:34 +000011217 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
11218 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070011219 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011220 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011221
11222 udelay(10);
11223
Matt Carlson898a56f2009-08-28 14:02:40 +000011224 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
11225 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011226 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070011227 (rx_idx == (rx_start_idx + num_pkts)))
11228 break;
11229 }
11230
Alexander Duyckf4188d82009-12-02 16:48:38 +000011231 pci_unmap_single(tp->pdev, map, tx_len, PCI_DMA_TODEVICE);
Michael Chanc76949a2005-05-29 14:58:59 -070011232 dev_kfree_skb(skb);
11233
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011234 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070011235 goto out;
11236
11237 if (rx_idx != rx_start_idx + num_pkts)
11238 goto out;
11239
Matt Carlson72334482009-08-28 14:03:01 +000011240 desc = &rnapi->rx_rcb[rx_start_idx];
Michael Chanc76949a2005-05-29 14:58:59 -070011241 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
11242 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
Michael Chanc76949a2005-05-29 14:58:59 -070011243
11244 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
11245 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
11246 goto out;
11247
11248 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) - 4;
11249 if (rx_len != tx_len)
11250 goto out;
11251
Matt Carlson4852a862011-04-13 11:05:07 +000011252 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
11253 if (opaque_key != RXD_OPAQUE_RING_STD)
11254 goto out;
Michael Chanc76949a2005-05-29 14:58:59 -070011255
Matt Carlson4852a862011-04-13 11:05:07 +000011256 rx_skb = tpr->rx_std_buffers[desc_idx].skb;
11257 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx], mapping);
11258 } else {
11259 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
11260 goto out;
11261
11262 rx_skb = tpr->rx_jmb_buffers[desc_idx].skb;
11263 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx], mapping);
11264 }
11265
Michael Chanc76949a2005-05-29 14:58:59 -070011266 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len, PCI_DMA_FROMDEVICE);
11267
11268 for (i = 14; i < tx_len; i++) {
11269 if (*(rx_skb->data + i) != (u8) (i & 0xff))
11270 goto out;
11271 }
11272 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011273
Michael Chanc76949a2005-05-29 14:58:59 -070011274 /* tg3_free_rings will unmap and free the rx_skb */
11275out:
11276 return err;
11277}
11278
Michael Chan9f40dea2005-09-05 17:53:06 -070011279#define TG3_MAC_LOOPBACK_FAILED 1
11280#define TG3_PHY_LOOPBACK_FAILED 2
11281#define TG3_LOOPBACK_FAILED (TG3_MAC_LOOPBACK_FAILED | \
11282 TG3_PHY_LOOPBACK_FAILED)
11283
11284static int tg3_test_loopback(struct tg3 *tp)
11285{
11286 int err = 0;
Matt Carlsonab789042011-01-25 15:58:54 +000011287 u32 eee_cap, cpmuctrl = 0;
Michael Chan9f40dea2005-09-05 17:53:06 -070011288
11289 if (!netif_running(tp->dev))
11290 return TG3_LOOPBACK_FAILED;
11291
Matt Carlsonab789042011-01-25 15:58:54 +000011292 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
11293 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
11294
Michael Chanb9ec6c12006-07-25 16:37:27 -070011295 err = tg3_reset_hw(tp, 1);
Matt Carlsonab789042011-01-25 15:58:54 +000011296 if (err) {
11297 err = TG3_LOOPBACK_FAILED;
11298 goto done;
11299 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011300
Matt Carlson4a85f092011-04-20 07:57:37 +000011301 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS) {
11302 int i;
11303
11304 /* Reroute all rx packets to the 1st queue */
11305 for (i = MAC_RSS_INDIR_TBL_0;
11306 i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
11307 tw32(i, 0x0);
11308 }
11309
Matt Carlson6833c042008-11-21 17:18:59 -080011310 /* Turn off gphy autopowerdown. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011311 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -080011312 tg3_phy_toggle_apd(tp, false);
11313
Matt Carlson321d32a2008-11-21 17:22:19 -080011314 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070011315 int i;
11316 u32 status;
11317
11318 tw32(TG3_CPMU_MUTEX_REQ, CPMU_MUTEX_REQ_DRIVER);
11319
11320 /* Wait for up to 40 microseconds to acquire lock. */
11321 for (i = 0; i < 4; i++) {
11322 status = tr32(TG3_CPMU_MUTEX_GNT);
11323 if (status == CPMU_MUTEX_GNT_DRIVER)
11324 break;
11325 udelay(10);
11326 }
11327
Matt Carlsonab789042011-01-25 15:58:54 +000011328 if (status != CPMU_MUTEX_GNT_DRIVER) {
11329 err = TG3_LOOPBACK_FAILED;
11330 goto done;
11331 }
Matt Carlson9936bcf2007-10-10 18:03:07 -070011332
Matt Carlsonb2a5c192008-04-03 21:44:44 -070011333 /* Turn off link-based power management. */
Matt Carlsone8750932007-11-12 21:11:51 -080011334 cpmuctrl = tr32(TG3_CPMU_CTRL);
Matt Carlson109115e2008-05-02 16:48:59 -070011335 tw32(TG3_CPMU_CTRL,
11336 cpmuctrl & ~(CPMU_CTRL_LINK_SPEED_MODE |
11337 CPMU_CTRL_LINK_AWARE_MODE));
Matt Carlson9936bcf2007-10-10 18:03:07 -070011338 }
11339
Matt Carlson4852a862011-04-13 11:05:07 +000011340 if (tg3_run_loopback(tp, ETH_FRAME_LEN, TG3_MAC_LOOPBACK))
Michael Chan9f40dea2005-09-05 17:53:06 -070011341 err |= TG3_MAC_LOOPBACK_FAILED;
Matt Carlson9936bcf2007-10-10 18:03:07 -070011342
Matt Carlson4852a862011-04-13 11:05:07 +000011343 if ((tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) &&
11344 tg3_run_loopback(tp, 9000 + ETH_HLEN, TG3_MAC_LOOPBACK))
11345 err |= (TG3_MAC_LOOPBACK_FAILED << 2);
11346
Matt Carlson321d32a2008-11-21 17:22:19 -080011347 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070011348 tw32(TG3_CPMU_CTRL, cpmuctrl);
11349
11350 /* Release the mutex */
11351 tw32(TG3_CPMU_MUTEX_GNT, CPMU_MUTEX_GNT_DRIVER);
11352 }
11353
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011354 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Matt Carlsondd477002008-05-25 23:45:58 -070011355 !(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)) {
Matt Carlson4852a862011-04-13 11:05:07 +000011356 if (tg3_run_loopback(tp, ETH_FRAME_LEN, TG3_PHY_LOOPBACK))
Michael Chan9f40dea2005-09-05 17:53:06 -070011357 err |= TG3_PHY_LOOPBACK_FAILED;
Matt Carlson4852a862011-04-13 11:05:07 +000011358 if ((tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) &&
11359 tg3_run_loopback(tp, 9000 + ETH_HLEN, TG3_PHY_LOOPBACK))
11360 err |= (TG3_PHY_LOOPBACK_FAILED << 2);
Michael Chan9f40dea2005-09-05 17:53:06 -070011361 }
11362
Matt Carlson6833c042008-11-21 17:18:59 -080011363 /* Re-enable gphy autopowerdown. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011364 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -080011365 tg3_phy_toggle_apd(tp, true);
11366
Matt Carlsonab789042011-01-25 15:58:54 +000011367done:
11368 tp->phy_flags |= eee_cap;
11369
Michael Chan9f40dea2005-09-05 17:53:06 -070011370 return err;
11371}
11372
Michael Chan4cafd3f2005-05-29 14:56:34 -070011373static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
11374 u64 *data)
11375{
Michael Chan566f86a2005-05-29 14:56:58 -070011376 struct tg3 *tp = netdev_priv(dev);
11377
Matt Carlson80096062010-08-02 11:26:06 +000011378 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000011379 tg3_power_up(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080011380
Michael Chan566f86a2005-05-29 14:56:58 -070011381 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
11382
11383 if (tg3_test_nvram(tp) != 0) {
11384 etest->flags |= ETH_TEST_FL_FAILED;
11385 data[0] = 1;
11386 }
Michael Chanca430072005-05-29 14:57:23 -070011387 if (tg3_test_link(tp) != 0) {
11388 etest->flags |= ETH_TEST_FL_FAILED;
11389 data[1] = 1;
11390 }
Michael Chana71116d2005-05-29 14:58:11 -070011391 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011392 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070011393
Michael Chanbbe832c2005-06-24 20:20:04 -070011394 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011395 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070011396 tg3_netif_stop(tp);
11397 irq_sync = 1;
11398 }
11399
11400 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070011401
11402 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080011403 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011404 tg3_halt_cpu(tp, RX_CPU_BASE);
11405 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
11406 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080011407 if (!err)
11408 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011409
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011410 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad12006-03-20 22:27:35 -080011411 tg3_phy_reset(tp);
11412
Michael Chana71116d2005-05-29 14:58:11 -070011413 if (tg3_test_registers(tp) != 0) {
11414 etest->flags |= ETH_TEST_FL_FAILED;
11415 data[2] = 1;
11416 }
Michael Chan7942e1d2005-05-29 14:58:36 -070011417 if (tg3_test_memory(tp) != 0) {
11418 etest->flags |= ETH_TEST_FL_FAILED;
11419 data[3] = 1;
11420 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011421 if ((data[4] = tg3_test_loopback(tp)) != 0)
Michael Chanc76949a2005-05-29 14:58:59 -070011422 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070011423
David S. Millerf47c11e2005-06-24 20:18:35 -070011424 tg3_full_unlock(tp);
11425
Michael Chand4bc3922005-05-29 14:59:20 -070011426 if (tg3_test_interrupt(tp) != 0) {
11427 etest->flags |= ETH_TEST_FL_FAILED;
11428 data[5] = 1;
11429 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011430
11431 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070011432
Michael Chana71116d2005-05-29 14:58:11 -070011433 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
11434 if (netif_running(dev)) {
11435 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011436 err2 = tg3_restart_hw(tp, 1);
11437 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070011438 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011439 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011440
11441 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011442
11443 if (irq_sync && !err2)
11444 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011445 }
Matt Carlson80096062010-08-02 11:26:06 +000011446 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000011447 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080011448
Michael Chan4cafd3f2005-05-29 14:56:34 -070011449}
11450
Linus Torvalds1da177e2005-04-16 15:20:36 -070011451static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
11452{
11453 struct mii_ioctl_data *data = if_mii(ifr);
11454 struct tg3 *tp = netdev_priv(dev);
11455 int err;
11456
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011457 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011458 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011459 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011460 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011461 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000011462 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011463 }
11464
Matt Carlson33f401a2010-04-05 10:19:27 +000011465 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011466 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000011467 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011468
11469 /* fallthru */
11470 case SIOCGMIIREG: {
11471 u32 mii_regval;
11472
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011473 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011474 break; /* We have no PHY */
11475
Matt Carlson34eea5a2011-04-20 07:57:38 +000011476 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080011477 return -EAGAIN;
11478
David S. Millerf47c11e2005-06-24 20:18:35 -070011479 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011480 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070011481 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011482
11483 data->val_out = mii_regval;
11484
11485 return err;
11486 }
11487
11488 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011489 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011490 break; /* We have no PHY */
11491
Matt Carlson34eea5a2011-04-20 07:57:38 +000011492 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080011493 return -EAGAIN;
11494
David S. Millerf47c11e2005-06-24 20:18:35 -070011495 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011496 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070011497 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011498
11499 return err;
11500
11501 default:
11502 /* do nothing */
11503 break;
11504 }
11505 return -EOPNOTSUPP;
11506}
11507
David S. Miller15f98502005-05-18 22:49:26 -070011508static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11509{
11510 struct tg3 *tp = netdev_priv(dev);
11511
11512 memcpy(ec, &tp->coal, sizeof(*ec));
11513 return 0;
11514}
11515
Michael Chand244c892005-07-05 14:42:33 -070011516static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11517{
11518 struct tg3 *tp = netdev_priv(dev);
11519 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
11520 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
11521
11522 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
11523 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
11524 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
11525 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
11526 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
11527 }
11528
11529 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
11530 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
11531 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
11532 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
11533 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
11534 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
11535 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
11536 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
11537 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
11538 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
11539 return -EINVAL;
11540
11541 /* No rx interrupts will be generated if both are zero */
11542 if ((ec->rx_coalesce_usecs == 0) &&
11543 (ec->rx_max_coalesced_frames == 0))
11544 return -EINVAL;
11545
11546 /* No tx interrupts will be generated if both are zero */
11547 if ((ec->tx_coalesce_usecs == 0) &&
11548 (ec->tx_max_coalesced_frames == 0))
11549 return -EINVAL;
11550
11551 /* Only copy relevant parameters, ignore all others. */
11552 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
11553 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
11554 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
11555 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
11556 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
11557 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
11558 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
11559 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
11560 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
11561
11562 if (netif_running(dev)) {
11563 tg3_full_lock(tp, 0);
11564 __tg3_set_coalesce(tp, &tp->coal);
11565 tg3_full_unlock(tp);
11566 }
11567 return 0;
11568}
11569
Jeff Garzik7282d492006-09-13 14:30:00 -040011570static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011571 .get_settings = tg3_get_settings,
11572 .set_settings = tg3_set_settings,
11573 .get_drvinfo = tg3_get_drvinfo,
11574 .get_regs_len = tg3_get_regs_len,
11575 .get_regs = tg3_get_regs,
11576 .get_wol = tg3_get_wol,
11577 .set_wol = tg3_set_wol,
11578 .get_msglevel = tg3_get_msglevel,
11579 .set_msglevel = tg3_set_msglevel,
11580 .nway_reset = tg3_nway_reset,
11581 .get_link = ethtool_op_get_link,
11582 .get_eeprom_len = tg3_get_eeprom_len,
11583 .get_eeprom = tg3_get_eeprom,
11584 .set_eeprom = tg3_set_eeprom,
11585 .get_ringparam = tg3_get_ringparam,
11586 .set_ringparam = tg3_set_ringparam,
11587 .get_pauseparam = tg3_get_pauseparam,
11588 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070011589 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011590 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000011591 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011592 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070011593 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070011594 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070011595 .get_sset_count = tg3_get_sset_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011596};
11597
11598static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
11599{
Michael Chan1b277772006-03-20 22:27:48 -080011600 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011601
11602 tp->nvram_size = EEPROM_CHIP_SIZE;
11603
Matt Carlsone4f34112009-02-25 14:25:00 +000011604 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011605 return;
11606
Michael Chanb16250e2006-09-27 16:10:14 -070011607 if ((magic != TG3_EEPROM_MAGIC) &&
11608 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
11609 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011610 return;
11611
11612 /*
11613 * Size the chip by reading offsets at increasing powers of two.
11614 * When we encounter our validation signature, we know the addressing
11615 * has wrapped around, and thus have our chip size.
11616 */
Michael Chan1b277772006-03-20 22:27:48 -080011617 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011618
11619 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000011620 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011621 return;
11622
Michael Chan18201802006-03-20 22:29:15 -080011623 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011624 break;
11625
11626 cursize <<= 1;
11627 }
11628
11629 tp->nvram_size = cursize;
11630}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011631
Linus Torvalds1da177e2005-04-16 15:20:36 -070011632static void __devinit tg3_get_nvram_size(struct tg3 *tp)
11633{
11634 u32 val;
11635
Matt Carlsondf259d82009-04-20 06:57:14 +000011636 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
11637 tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080011638 return;
11639
11640 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080011641 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080011642 tg3_get_eeprom_size(tp);
11643 return;
11644 }
11645
Matt Carlson6d348f22009-02-25 14:25:52 +000011646 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011647 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000011648 /* This is confusing. We want to operate on the
11649 * 16-bit value at offset 0xf2. The tg3_nvram_read()
11650 * call will read from NVRAM and byteswap the data
11651 * according to the byteswapping settings for all
11652 * other register accesses. This ensures the data we
11653 * want will always reside in the lower 16-bits.
11654 * However, the data in NVRAM is in LE format, which
11655 * means the data from the NVRAM read will always be
11656 * opposite the endianness of the CPU. The 16-bit
11657 * byteswap then brings the data to CPU endianness.
11658 */
11659 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011660 return;
11661 }
11662 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070011663 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011664}
11665
11666static void __devinit tg3_get_nvram_info(struct tg3 *tp)
11667{
11668 u32 nvcfg1;
11669
11670 nvcfg1 = tr32(NVRAM_CFG1);
11671 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
11672 tp->tg3_flags2 |= TG3_FLG2_FLASH;
Matt Carlson8590a602009-08-28 12:29:16 +000011673 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011674 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11675 tw32(NVRAM_CFG1, nvcfg1);
11676 }
11677
Michael Chan4c987482005-09-05 17:52:38 -070011678 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750) ||
Michael Chana4e2b342005-10-26 15:46:52 -070011679 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011680 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011681 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
11682 tp->nvram_jedecnum = JEDEC_ATMEL;
11683 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
11684 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11685 break;
11686 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
11687 tp->nvram_jedecnum = JEDEC_ATMEL;
11688 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
11689 break;
11690 case FLASH_VENDOR_ATMEL_EEPROM:
11691 tp->nvram_jedecnum = JEDEC_ATMEL;
11692 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11693 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11694 break;
11695 case FLASH_VENDOR_ST:
11696 tp->nvram_jedecnum = JEDEC_ST;
11697 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
11698 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11699 break;
11700 case FLASH_VENDOR_SAIFUN:
11701 tp->nvram_jedecnum = JEDEC_SAIFUN;
11702 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
11703 break;
11704 case FLASH_VENDOR_SST_SMALL:
11705 case FLASH_VENDOR_SST_LARGE:
11706 tp->nvram_jedecnum = JEDEC_SST;
11707 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
11708 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011709 }
Matt Carlson8590a602009-08-28 12:29:16 +000011710 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011711 tp->nvram_jedecnum = JEDEC_ATMEL;
11712 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
11713 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11714 }
11715}
11716
Matt Carlsona1b950d2009-09-01 13:20:17 +000011717static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
11718{
11719 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
11720 case FLASH_5752PAGE_SIZE_256:
11721 tp->nvram_pagesize = 256;
11722 break;
11723 case FLASH_5752PAGE_SIZE_512:
11724 tp->nvram_pagesize = 512;
11725 break;
11726 case FLASH_5752PAGE_SIZE_1K:
11727 tp->nvram_pagesize = 1024;
11728 break;
11729 case FLASH_5752PAGE_SIZE_2K:
11730 tp->nvram_pagesize = 2048;
11731 break;
11732 case FLASH_5752PAGE_SIZE_4K:
11733 tp->nvram_pagesize = 4096;
11734 break;
11735 case FLASH_5752PAGE_SIZE_264:
11736 tp->nvram_pagesize = 264;
11737 break;
11738 case FLASH_5752PAGE_SIZE_528:
11739 tp->nvram_pagesize = 528;
11740 break;
11741 }
11742}
11743
Michael Chan361b4ac2005-04-21 17:11:21 -070011744static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
11745{
11746 u32 nvcfg1;
11747
11748 nvcfg1 = tr32(NVRAM_CFG1);
11749
Michael Chane6af3012005-04-21 17:12:05 -070011750 /* NVRAM protection for TPM */
11751 if (nvcfg1 & (1 << 27))
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011752 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Michael Chane6af3012005-04-21 17:12:05 -070011753
Michael Chan361b4ac2005-04-21 17:11:21 -070011754 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011755 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
11756 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
11757 tp->nvram_jedecnum = JEDEC_ATMEL;
11758 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11759 break;
11760 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11761 tp->nvram_jedecnum = JEDEC_ATMEL;
11762 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11763 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11764 break;
11765 case FLASH_5752VENDOR_ST_M45PE10:
11766 case FLASH_5752VENDOR_ST_M45PE20:
11767 case FLASH_5752VENDOR_ST_M45PE40:
11768 tp->nvram_jedecnum = JEDEC_ST;
11769 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11770 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11771 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070011772 }
11773
11774 if (tp->tg3_flags2 & TG3_FLG2_FLASH) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000011775 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000011776 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070011777 /* For eeprom, set pagesize to maximum eeprom size */
11778 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11779
11780 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11781 tw32(NVRAM_CFG1, nvcfg1);
11782 }
11783}
11784
Michael Chand3c7b882006-03-23 01:28:25 -080011785static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
11786{
Matt Carlson989a9d22007-05-05 11:51:05 -070011787 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080011788
11789 nvcfg1 = tr32(NVRAM_CFG1);
11790
11791 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070011792 if (nvcfg1 & (1 << 27)) {
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011793 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Matt Carlson989a9d22007-05-05 11:51:05 -070011794 protect = 1;
11795 }
Michael Chand3c7b882006-03-23 01:28:25 -080011796
Matt Carlson989a9d22007-05-05 11:51:05 -070011797 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11798 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011799 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11800 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11801 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11802 case FLASH_5755VENDOR_ATMEL_FLASH_5:
11803 tp->nvram_jedecnum = JEDEC_ATMEL;
11804 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11805 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11806 tp->nvram_pagesize = 264;
11807 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
11808 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
11809 tp->nvram_size = (protect ? 0x3e200 :
11810 TG3_NVRAM_SIZE_512KB);
11811 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
11812 tp->nvram_size = (protect ? 0x1f200 :
11813 TG3_NVRAM_SIZE_256KB);
11814 else
11815 tp->nvram_size = (protect ? 0x1f200 :
11816 TG3_NVRAM_SIZE_128KB);
11817 break;
11818 case FLASH_5752VENDOR_ST_M45PE10:
11819 case FLASH_5752VENDOR_ST_M45PE20:
11820 case FLASH_5752VENDOR_ST_M45PE40:
11821 tp->nvram_jedecnum = JEDEC_ST;
11822 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11823 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11824 tp->nvram_pagesize = 256;
11825 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
11826 tp->nvram_size = (protect ?
11827 TG3_NVRAM_SIZE_64KB :
11828 TG3_NVRAM_SIZE_128KB);
11829 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
11830 tp->nvram_size = (protect ?
11831 TG3_NVRAM_SIZE_64KB :
11832 TG3_NVRAM_SIZE_256KB);
11833 else
11834 tp->nvram_size = (protect ?
11835 TG3_NVRAM_SIZE_128KB :
11836 TG3_NVRAM_SIZE_512KB);
11837 break;
Michael Chand3c7b882006-03-23 01:28:25 -080011838 }
11839}
11840
Michael Chan1b277772006-03-20 22:27:48 -080011841static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
11842{
11843 u32 nvcfg1;
11844
11845 nvcfg1 = tr32(NVRAM_CFG1);
11846
11847 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011848 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
11849 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11850 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
11851 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11852 tp->nvram_jedecnum = JEDEC_ATMEL;
11853 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11854 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080011855
Matt Carlson8590a602009-08-28 12:29:16 +000011856 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11857 tw32(NVRAM_CFG1, nvcfg1);
11858 break;
11859 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11860 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11861 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11862 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11863 tp->nvram_jedecnum = JEDEC_ATMEL;
11864 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11865 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11866 tp->nvram_pagesize = 264;
11867 break;
11868 case FLASH_5752VENDOR_ST_M45PE10:
11869 case FLASH_5752VENDOR_ST_M45PE20:
11870 case FLASH_5752VENDOR_ST_M45PE40:
11871 tp->nvram_jedecnum = JEDEC_ST;
11872 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11873 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11874 tp->nvram_pagesize = 256;
11875 break;
Michael Chan1b277772006-03-20 22:27:48 -080011876 }
11877}
11878
Matt Carlson6b91fa02007-10-10 18:01:09 -070011879static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
11880{
11881 u32 nvcfg1, protect = 0;
11882
11883 nvcfg1 = tr32(NVRAM_CFG1);
11884
11885 /* NVRAM protection for TPM */
11886 if (nvcfg1 & (1 << 27)) {
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011887 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011888 protect = 1;
11889 }
11890
11891 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11892 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011893 case FLASH_5761VENDOR_ATMEL_ADB021D:
11894 case FLASH_5761VENDOR_ATMEL_ADB041D:
11895 case FLASH_5761VENDOR_ATMEL_ADB081D:
11896 case FLASH_5761VENDOR_ATMEL_ADB161D:
11897 case FLASH_5761VENDOR_ATMEL_MDB021D:
11898 case FLASH_5761VENDOR_ATMEL_MDB041D:
11899 case FLASH_5761VENDOR_ATMEL_MDB081D:
11900 case FLASH_5761VENDOR_ATMEL_MDB161D:
11901 tp->nvram_jedecnum = JEDEC_ATMEL;
11902 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11903 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11904 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
11905 tp->nvram_pagesize = 256;
11906 break;
11907 case FLASH_5761VENDOR_ST_A_M45PE20:
11908 case FLASH_5761VENDOR_ST_A_M45PE40:
11909 case FLASH_5761VENDOR_ST_A_M45PE80:
11910 case FLASH_5761VENDOR_ST_A_M45PE16:
11911 case FLASH_5761VENDOR_ST_M_M45PE20:
11912 case FLASH_5761VENDOR_ST_M_M45PE40:
11913 case FLASH_5761VENDOR_ST_M_M45PE80:
11914 case FLASH_5761VENDOR_ST_M_M45PE16:
11915 tp->nvram_jedecnum = JEDEC_ST;
11916 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11917 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11918 tp->nvram_pagesize = 256;
11919 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011920 }
11921
11922 if (protect) {
11923 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
11924 } else {
11925 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011926 case FLASH_5761VENDOR_ATMEL_ADB161D:
11927 case FLASH_5761VENDOR_ATMEL_MDB161D:
11928 case FLASH_5761VENDOR_ST_A_M45PE16:
11929 case FLASH_5761VENDOR_ST_M_M45PE16:
11930 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
11931 break;
11932 case FLASH_5761VENDOR_ATMEL_ADB081D:
11933 case FLASH_5761VENDOR_ATMEL_MDB081D:
11934 case FLASH_5761VENDOR_ST_A_M45PE80:
11935 case FLASH_5761VENDOR_ST_M_M45PE80:
11936 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
11937 break;
11938 case FLASH_5761VENDOR_ATMEL_ADB041D:
11939 case FLASH_5761VENDOR_ATMEL_MDB041D:
11940 case FLASH_5761VENDOR_ST_A_M45PE40:
11941 case FLASH_5761VENDOR_ST_M_M45PE40:
11942 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11943 break;
11944 case FLASH_5761VENDOR_ATMEL_ADB021D:
11945 case FLASH_5761VENDOR_ATMEL_MDB021D:
11946 case FLASH_5761VENDOR_ST_A_M45PE20:
11947 case FLASH_5761VENDOR_ST_M_M45PE20:
11948 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11949 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011950 }
11951 }
11952}
11953
Michael Chanb5d37722006-09-27 16:06:21 -070011954static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
11955{
11956 tp->nvram_jedecnum = JEDEC_ATMEL;
11957 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11958 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11959}
11960
Matt Carlson321d32a2008-11-21 17:22:19 -080011961static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
11962{
11963 u32 nvcfg1;
11964
11965 nvcfg1 = tr32(NVRAM_CFG1);
11966
11967 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11968 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11969 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11970 tp->nvram_jedecnum = JEDEC_ATMEL;
11971 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11972 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11973
11974 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11975 tw32(NVRAM_CFG1, nvcfg1);
11976 return;
11977 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11978 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11979 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11980 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11981 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11982 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11983 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
11984 tp->nvram_jedecnum = JEDEC_ATMEL;
11985 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11986 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11987
11988 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11989 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11990 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11991 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11992 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11993 break;
11994 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11995 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11996 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11997 break;
11998 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11999 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
12000 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12001 break;
12002 }
12003 break;
12004 case FLASH_5752VENDOR_ST_M45PE10:
12005 case FLASH_5752VENDOR_ST_M45PE20:
12006 case FLASH_5752VENDOR_ST_M45PE40:
12007 tp->nvram_jedecnum = JEDEC_ST;
12008 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
12009 tp->tg3_flags2 |= TG3_FLG2_FLASH;
12010
12011 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12012 case FLASH_5752VENDOR_ST_M45PE10:
12013 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12014 break;
12015 case FLASH_5752VENDOR_ST_M45PE20:
12016 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12017 break;
12018 case FLASH_5752VENDOR_ST_M45PE40:
12019 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12020 break;
12021 }
12022 break;
12023 default:
Matt Carlsondf259d82009-04-20 06:57:14 +000012024 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
Matt Carlson321d32a2008-11-21 17:22:19 -080012025 return;
12026 }
12027
Matt Carlsona1b950d2009-09-01 13:20:17 +000012028 tg3_nvram_get_pagesize(tp, nvcfg1);
12029 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Matt Carlson321d32a2008-11-21 17:22:19 -080012030 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012031}
12032
12033
12034static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
12035{
12036 u32 nvcfg1;
12037
12038 nvcfg1 = tr32(NVRAM_CFG1);
12039
12040 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12041 case FLASH_5717VENDOR_ATMEL_EEPROM:
12042 case FLASH_5717VENDOR_MICRO_EEPROM:
12043 tp->nvram_jedecnum = JEDEC_ATMEL;
12044 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
12045 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12046
12047 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12048 tw32(NVRAM_CFG1, nvcfg1);
12049 return;
12050 case FLASH_5717VENDOR_ATMEL_MDB011D:
12051 case FLASH_5717VENDOR_ATMEL_ADB011B:
12052 case FLASH_5717VENDOR_ATMEL_ADB011D:
12053 case FLASH_5717VENDOR_ATMEL_MDB021D:
12054 case FLASH_5717VENDOR_ATMEL_ADB021B:
12055 case FLASH_5717VENDOR_ATMEL_ADB021D:
12056 case FLASH_5717VENDOR_ATMEL_45USPT:
12057 tp->nvram_jedecnum = JEDEC_ATMEL;
12058 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
12059 tp->tg3_flags2 |= TG3_FLG2_FLASH;
12060
12061 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12062 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012063 /* Detect size with tg3_nvram_get_size() */
12064 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012065 case FLASH_5717VENDOR_ATMEL_ADB021B:
12066 case FLASH_5717VENDOR_ATMEL_ADB021D:
12067 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12068 break;
12069 default:
12070 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12071 break;
12072 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012073 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012074 case FLASH_5717VENDOR_ST_M_M25PE10:
12075 case FLASH_5717VENDOR_ST_A_M25PE10:
12076 case FLASH_5717VENDOR_ST_M_M45PE10:
12077 case FLASH_5717VENDOR_ST_A_M45PE10:
12078 case FLASH_5717VENDOR_ST_M_M25PE20:
12079 case FLASH_5717VENDOR_ST_A_M25PE20:
12080 case FLASH_5717VENDOR_ST_M_M45PE20:
12081 case FLASH_5717VENDOR_ST_A_M45PE20:
12082 case FLASH_5717VENDOR_ST_25USPT:
12083 case FLASH_5717VENDOR_ST_45USPT:
12084 tp->nvram_jedecnum = JEDEC_ST;
12085 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
12086 tp->tg3_flags2 |= TG3_FLG2_FLASH;
12087
12088 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12089 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012090 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012091 /* Detect size with tg3_nvram_get_size() */
12092 break;
12093 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012094 case FLASH_5717VENDOR_ST_A_M45PE20:
12095 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12096 break;
12097 default:
12098 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12099 break;
12100 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012101 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012102 default:
12103 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
12104 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080012105 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000012106
12107 tg3_nvram_get_pagesize(tp, nvcfg1);
12108 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
12109 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
Matt Carlson321d32a2008-11-21 17:22:19 -080012110}
12111
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012112static void __devinit tg3_get_5720_nvram_info(struct tg3 *tp)
12113{
12114 u32 nvcfg1, nvmpinstrp;
12115
12116 nvcfg1 = tr32(NVRAM_CFG1);
12117 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
12118
12119 switch (nvmpinstrp) {
12120 case FLASH_5720_EEPROM_HD:
12121 case FLASH_5720_EEPROM_LD:
12122 tp->nvram_jedecnum = JEDEC_ATMEL;
12123 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
12124
12125 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12126 tw32(NVRAM_CFG1, nvcfg1);
12127 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
12128 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12129 else
12130 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
12131 return;
12132 case FLASH_5720VENDOR_M_ATMEL_DB011D:
12133 case FLASH_5720VENDOR_A_ATMEL_DB011B:
12134 case FLASH_5720VENDOR_A_ATMEL_DB011D:
12135 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12136 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12137 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12138 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12139 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12140 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12141 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12142 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12143 case FLASH_5720VENDOR_ATMEL_45USPT:
12144 tp->nvram_jedecnum = JEDEC_ATMEL;
12145 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
12146 tp->tg3_flags2 |= TG3_FLG2_FLASH;
12147
12148 switch (nvmpinstrp) {
12149 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12150 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12151 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12152 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12153 break;
12154 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12155 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12156 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12157 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12158 break;
12159 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12160 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12161 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12162 break;
12163 default:
12164 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12165 break;
12166 }
12167 break;
12168 case FLASH_5720VENDOR_M_ST_M25PE10:
12169 case FLASH_5720VENDOR_M_ST_M45PE10:
12170 case FLASH_5720VENDOR_A_ST_M25PE10:
12171 case FLASH_5720VENDOR_A_ST_M45PE10:
12172 case FLASH_5720VENDOR_M_ST_M25PE20:
12173 case FLASH_5720VENDOR_M_ST_M45PE20:
12174 case FLASH_5720VENDOR_A_ST_M25PE20:
12175 case FLASH_5720VENDOR_A_ST_M45PE20:
12176 case FLASH_5720VENDOR_M_ST_M25PE40:
12177 case FLASH_5720VENDOR_M_ST_M45PE40:
12178 case FLASH_5720VENDOR_A_ST_M25PE40:
12179 case FLASH_5720VENDOR_A_ST_M45PE40:
12180 case FLASH_5720VENDOR_M_ST_M25PE80:
12181 case FLASH_5720VENDOR_M_ST_M45PE80:
12182 case FLASH_5720VENDOR_A_ST_M25PE80:
12183 case FLASH_5720VENDOR_A_ST_M45PE80:
12184 case FLASH_5720VENDOR_ST_25USPT:
12185 case FLASH_5720VENDOR_ST_45USPT:
12186 tp->nvram_jedecnum = JEDEC_ST;
12187 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
12188 tp->tg3_flags2 |= TG3_FLG2_FLASH;
12189
12190 switch (nvmpinstrp) {
12191 case FLASH_5720VENDOR_M_ST_M25PE20:
12192 case FLASH_5720VENDOR_M_ST_M45PE20:
12193 case FLASH_5720VENDOR_A_ST_M25PE20:
12194 case FLASH_5720VENDOR_A_ST_M45PE20:
12195 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12196 break;
12197 case FLASH_5720VENDOR_M_ST_M25PE40:
12198 case FLASH_5720VENDOR_M_ST_M45PE40:
12199 case FLASH_5720VENDOR_A_ST_M25PE40:
12200 case FLASH_5720VENDOR_A_ST_M45PE40:
12201 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12202 break;
12203 case FLASH_5720VENDOR_M_ST_M25PE80:
12204 case FLASH_5720VENDOR_M_ST_M45PE80:
12205 case FLASH_5720VENDOR_A_ST_M25PE80:
12206 case FLASH_5720VENDOR_A_ST_M45PE80:
12207 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12208 break;
12209 default:
12210 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12211 break;
12212 }
12213 break;
12214 default:
12215 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
12216 return;
12217 }
12218
12219 tg3_nvram_get_pagesize(tp, nvcfg1);
12220 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
12221 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
12222}
12223
Linus Torvalds1da177e2005-04-16 15:20:36 -070012224/* Chips other than 5700/5701 use the NVRAM for fetching info. */
12225static void __devinit tg3_nvram_init(struct tg3 *tp)
12226{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012227 tw32_f(GRC_EEPROM_ADDR,
12228 (EEPROM_ADDR_FSM_RESET |
12229 (EEPROM_DEFAULT_CLOCK_PERIOD <<
12230 EEPROM_ADDR_CLKPERD_SHIFT)));
12231
Michael Chan9d57f012006-12-07 00:23:25 -080012232 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012233
12234 /* Enable seeprom accesses. */
12235 tw32_f(GRC_LOCAL_CTRL,
12236 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
12237 udelay(100);
12238
12239 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
12240 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
12241 tp->tg3_flags |= TG3_FLAG_NVRAM;
12242
Michael Chanec41c7d2006-01-17 02:40:55 -080012243 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000012244 netdev_warn(tp->dev,
12245 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000012246 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080012247 return;
12248 }
Michael Chane6af3012005-04-21 17:12:05 -070012249 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012250
Matt Carlson989a9d22007-05-05 11:51:05 -070012251 tp->nvram_size = 0;
12252
Michael Chan361b4ac2005-04-21 17:11:21 -070012253 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
12254 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080012255 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
12256 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070012257 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070012258 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
12259 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080012260 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070012261 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
12262 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070012263 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12264 tg3_get_5906_nvram_info(tp);
Matt Carlsonb703df62009-12-03 08:36:21 +000012265 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
12266 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson321d32a2008-11-21 17:22:19 -080012267 tg3_get_57780_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012268 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
12269 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000012270 tg3_get_5717_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012271 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
12272 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070012273 else
12274 tg3_get_nvram_info(tp);
12275
Matt Carlson989a9d22007-05-05 11:51:05 -070012276 if (tp->nvram_size == 0)
12277 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012278
Michael Chane6af3012005-04-21 17:12:05 -070012279 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080012280 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012281
12282 } else {
12283 tp->tg3_flags &= ~(TG3_FLAG_NVRAM | TG3_FLAG_NVRAM_BUFFERED);
12284
12285 tg3_get_eeprom_size(tp);
12286 }
12287}
12288
Linus Torvalds1da177e2005-04-16 15:20:36 -070012289static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
12290 u32 offset, u32 len, u8 *buf)
12291{
12292 int i, j, rc = 0;
12293 u32 val;
12294
12295 for (i = 0; i < len; i += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012296 u32 addr;
Matt Carlsona9dc5292009-02-25 14:25:30 +000012297 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012298
12299 addr = offset + i;
12300
12301 memcpy(&data, buf + i, 4);
12302
Matt Carlson62cedd12009-04-20 14:52:29 -070012303 /*
12304 * The SEEPROM interface expects the data to always be opposite
12305 * the native endian format. We accomplish this by reversing
12306 * all the operations that would have been performed on the
12307 * data from a call to tg3_nvram_read_be32().
12308 */
12309 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012310
12311 val = tr32(GRC_EEPROM_ADDR);
12312 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
12313
12314 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
12315 EEPROM_ADDR_READ);
12316 tw32(GRC_EEPROM_ADDR, val |
12317 (0 << EEPROM_ADDR_DEVID_SHIFT) |
12318 (addr & EEPROM_ADDR_ADDR_MASK) |
12319 EEPROM_ADDR_START |
12320 EEPROM_ADDR_WRITE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012321
Michael Chan9d57f012006-12-07 00:23:25 -080012322 for (j = 0; j < 1000; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012323 val = tr32(GRC_EEPROM_ADDR);
12324
12325 if (val & EEPROM_ADDR_COMPLETE)
12326 break;
Michael Chan9d57f012006-12-07 00:23:25 -080012327 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012328 }
12329 if (!(val & EEPROM_ADDR_COMPLETE)) {
12330 rc = -EBUSY;
12331 break;
12332 }
12333 }
12334
12335 return rc;
12336}
12337
12338/* offset and length are dword aligned */
12339static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
12340 u8 *buf)
12341{
12342 int ret = 0;
12343 u32 pagesize = tp->nvram_pagesize;
12344 u32 pagemask = pagesize - 1;
12345 u32 nvram_cmd;
12346 u8 *tmp;
12347
12348 tmp = kmalloc(pagesize, GFP_KERNEL);
12349 if (tmp == NULL)
12350 return -ENOMEM;
12351
12352 while (len) {
12353 int j;
Michael Chane6af3012005-04-21 17:12:05 -070012354 u32 phy_addr, page_off, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012355
12356 phy_addr = offset & ~pagemask;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012357
Linus Torvalds1da177e2005-04-16 15:20:36 -070012358 for (j = 0; j < pagesize; j += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012359 ret = tg3_nvram_read_be32(tp, phy_addr + j,
12360 (__be32 *) (tmp + j));
12361 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012362 break;
12363 }
12364 if (ret)
12365 break;
12366
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012367 page_off = offset & pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012368 size = pagesize;
12369 if (len < size)
12370 size = len;
12371
12372 len -= size;
12373
12374 memcpy(tmp + page_off, buf, size);
12375
12376 offset = offset + (pagesize - page_off);
12377
Michael Chane6af3012005-04-21 17:12:05 -070012378 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012379
12380 /*
12381 * Before we can erase the flash page, we need
12382 * to issue a special "write enable" command.
12383 */
12384 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12385
12386 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12387 break;
12388
12389 /* Erase the target page */
12390 tw32(NVRAM_ADDR, phy_addr);
12391
12392 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
12393 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
12394
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012395 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012396 break;
12397
12398 /* Issue another write enable to start the write. */
12399 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12400
12401 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12402 break;
12403
12404 for (j = 0; j < pagesize; j += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012405 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012406
Al Virob9fc7dc2007-12-17 22:59:57 -080012407 data = *((__be32 *) (tmp + j));
Matt Carlsona9dc5292009-02-25 14:25:30 +000012408
Al Virob9fc7dc2007-12-17 22:59:57 -080012409 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012410
12411 tw32(NVRAM_ADDR, phy_addr + j);
12412
12413 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
12414 NVRAM_CMD_WR;
12415
12416 if (j == 0)
12417 nvram_cmd |= NVRAM_CMD_FIRST;
12418 else if (j == (pagesize - 4))
12419 nvram_cmd |= NVRAM_CMD_LAST;
12420
12421 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12422 break;
12423 }
12424 if (ret)
12425 break;
12426 }
12427
12428 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12429 tg3_nvram_exec_cmd(tp, nvram_cmd);
12430
12431 kfree(tmp);
12432
12433 return ret;
12434}
12435
12436/* offset and length are dword aligned */
12437static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
12438 u8 *buf)
12439{
12440 int i, ret = 0;
12441
12442 for (i = 0; i < len; i += 4, offset += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012443 u32 page_off, phy_addr, nvram_cmd;
12444 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012445
12446 memcpy(&data, buf + i, 4);
Al Virob9fc7dc2007-12-17 22:59:57 -080012447 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012448
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012449 page_off = offset % tp->nvram_pagesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012450
Michael Chan18201802006-03-20 22:29:15 -080012451 phy_addr = tg3_nvram_phys_addr(tp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012452
12453 tw32(NVRAM_ADDR, phy_addr);
12454
12455 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
12456
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012457 if (page_off == 0 || i == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012458 nvram_cmd |= NVRAM_CMD_FIRST;
Michael Chanf6d9a252006-04-29 19:00:24 -070012459 if (page_off == (tp->nvram_pagesize - 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012460 nvram_cmd |= NVRAM_CMD_LAST;
12461
12462 if (i == (len - 4))
12463 nvram_cmd |= NVRAM_CMD_LAST;
12464
Matt Carlson321d32a2008-11-21 17:22:19 -080012465 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
12466 !(tp->tg3_flags3 & TG3_FLG3_5755_PLUS) &&
Michael Chan4c987482005-09-05 17:52:38 -070012467 (tp->nvram_jedecnum == JEDEC_ST) &&
12468 (nvram_cmd & NVRAM_CMD_FIRST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012469
12470 if ((ret = tg3_nvram_exec_cmd(tp,
12471 NVRAM_CMD_WREN | NVRAM_CMD_GO |
12472 NVRAM_CMD_DONE)))
12473
12474 break;
12475 }
12476 if (!(tp->tg3_flags2 & TG3_FLG2_FLASH)) {
12477 /* We always do complete word writes to eeprom. */
12478 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
12479 }
12480
12481 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12482 break;
12483 }
12484 return ret;
12485}
12486
12487/* offset and length are dword aligned */
12488static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
12489{
12490 int ret;
12491
Linus Torvalds1da177e2005-04-16 15:20:36 -070012492 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT) {
Michael Chan314fba32005-04-21 17:07:04 -070012493 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
12494 ~GRC_LCLCTRL_GPIO_OUTPUT1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012495 udelay(40);
12496 }
12497
12498 if (!(tp->tg3_flags & TG3_FLAG_NVRAM)) {
12499 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
Matt Carlson859a588792010-04-05 10:19:28 +000012500 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012501 u32 grc_mode;
12502
Michael Chanec41c7d2006-01-17 02:40:55 -080012503 ret = tg3_nvram_lock(tp);
12504 if (ret)
12505 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012506
Michael Chane6af3012005-04-21 17:12:05 -070012507 tg3_enable_nvram_access(tp);
12508 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Matt Carlsonf66a29b2009-11-13 13:03:36 +000012509 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012510 tw32(NVRAM_WRITE1, 0x406);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012511
12512 grc_mode = tr32(GRC_MODE);
12513 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
12514
12515 if ((tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) ||
12516 !(tp->tg3_flags2 & TG3_FLG2_FLASH)) {
12517
12518 ret = tg3_nvram_write_block_buffered(tp, offset, len,
12519 buf);
Matt Carlson859a588792010-04-05 10:19:28 +000012520 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012521 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
12522 buf);
12523 }
12524
12525 grc_mode = tr32(GRC_MODE);
12526 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
12527
Michael Chane6af3012005-04-21 17:12:05 -070012528 tg3_disable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012529 tg3_nvram_unlock(tp);
12530 }
12531
12532 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT) {
Michael Chan314fba32005-04-21 17:07:04 -070012533 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012534 udelay(40);
12535 }
12536
12537 return ret;
12538}
12539
12540struct subsys_tbl_ent {
12541 u16 subsys_vendor, subsys_devid;
12542 u32 phy_id;
12543};
12544
Matt Carlson24daf2b2010-02-17 15:17:02 +000012545static struct subsys_tbl_ent subsys_id_to_phy_id[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012546 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012547 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012548 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012549 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012550 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012551 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012552 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012553 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12554 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
12555 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012556 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012557 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012558 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012559 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12560 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
12561 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012562 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012563 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012564 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012565 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012566 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012567 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012568 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012569
12570 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012571 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012572 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012573 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012574 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012575 { TG3PCI_SUBVENDOR_ID_3COM,
12576 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
12577 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012578 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012579 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012580 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012581
12582 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012583 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012584 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012585 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012586 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012587 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012588 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012589 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012590 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012591
12592 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012593 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012594 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012595 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012596 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012597 { TG3PCI_SUBVENDOR_ID_COMPAQ,
12598 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
12599 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012600 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012601 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012602 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012603
12604 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012605 { TG3PCI_SUBVENDOR_ID_IBM,
12606 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012607};
12608
Matt Carlson24daf2b2010-02-17 15:17:02 +000012609static struct subsys_tbl_ent * __devinit tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012610{
12611 int i;
12612
12613 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
12614 if ((subsys_id_to_phy_id[i].subsys_vendor ==
12615 tp->pdev->subsystem_vendor) &&
12616 (subsys_id_to_phy_id[i].subsys_devid ==
12617 tp->pdev->subsystem_device))
12618 return &subsys_id_to_phy_id[i];
12619 }
12620 return NULL;
12621}
12622
Michael Chan7d0c41e2005-04-21 17:06:20 -070012623static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012624{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012625 u32 val;
Michael Chancaf636c72006-03-22 01:05:31 -080012626 u16 pmcsr;
12627
12628 /* On some early chips the SRAM cannot be accessed in D3hot state,
12629 * so need make sure we're in D0.
12630 */
12631 pci_read_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, &pmcsr);
12632 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
12633 pci_write_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, pmcsr);
12634 msleep(1);
Michael Chan7d0c41e2005-04-21 17:06:20 -070012635
12636 /* Make sure register accesses (indirect or otherwise)
12637 * will function correctly.
12638 */
12639 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
12640 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012641
David S. Millerf49639e2006-06-09 11:58:36 -070012642 /* The memory arbiter has to be enabled in order for SRAM accesses
12643 * to succeed. Normally on powerup the tg3 chip firmware will make
12644 * sure it is enabled, but other entities such as system netboot
12645 * code might disable it.
12646 */
12647 val = tr32(MEMARB_MODE);
12648 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
12649
Matt Carlson79eb6902010-02-17 15:17:03 +000012650 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012651 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12652
Gary Zambranoa85feb82007-05-05 11:52:19 -070012653 /* Assume an onboard device and WOL capable by default. */
12654 tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT | TG3_FLAG_WOL_CAP;
David S. Miller72b845e2006-03-14 14:11:48 -080012655
Michael Chanb5d37722006-09-27 16:06:21 -070012656 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080012657 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Michael Chanb5d37722006-09-27 16:06:21 -070012658 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080012659 tp->tg3_flags2 |= TG3_FLG2_IS_NIC;
12660 }
Matt Carlson0527ba32007-10-10 18:03:30 -070012661 val = tr32(VCPU_CFGSHDW);
12662 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Matt Carlson8ed5d972007-05-07 00:25:49 -070012663 tp->tg3_flags |= TG3_FLAG_ASPM_WORKAROUND;
Matt Carlson0527ba32007-10-10 18:03:30 -070012664 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Matt Carlson20232762008-12-21 20:18:56 -080012665 (val & VCPU_CFGSHDW_WOL_MAGPKT))
Matt Carlson0527ba32007-10-10 18:03:30 -070012666 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012667 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070012668 }
12669
Linus Torvalds1da177e2005-04-16 15:20:36 -070012670 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
12671 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
12672 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070012673 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012674 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012675
12676 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
12677 tp->nic_sram_data_cfg = nic_cfg;
12678
12679 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
12680 ver >>= NIC_SRAM_DATA_VER_SHIFT;
12681 if ((GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700) &&
12682 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) &&
12683 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703) &&
12684 (ver > 0) && (ver < 0x100))
12685 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
12686
Matt Carlsona9daf362008-05-25 23:49:44 -070012687 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
12688 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
12689
Linus Torvalds1da177e2005-04-16 15:20:36 -070012690 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
12691 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
12692 eeprom_phy_serdes = 1;
12693
12694 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
12695 if (nic_phy_id != 0) {
12696 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
12697 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
12698
12699 eeprom_phy_id = (id1 >> 16) << 10;
12700 eeprom_phy_id |= (id2 & 0xfc00) << 16;
12701 eeprom_phy_id |= (id2 & 0x03ff) << 0;
12702 } else
12703 eeprom_phy_id = 0;
12704
Michael Chan7d0c41e2005-04-21 17:06:20 -070012705 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070012706 if (eeprom_phy_serdes) {
Matt Carlsona50d0792010-06-05 17:24:37 +000012707 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012708 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000012709 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012710 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070012711 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070012712
John W. Linvillecbf46852005-04-21 17:01:29 -070012713 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012714 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
12715 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070012716 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070012717 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
12718
12719 switch (led_cfg) {
12720 default:
12721 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
12722 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12723 break;
12724
12725 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
12726 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12727 break;
12728
12729 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
12730 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070012731
12732 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
12733 * read on some older 5700/5701 bootcode.
12734 */
12735 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
12736 ASIC_REV_5700 ||
12737 GET_ASIC_REV(tp->pci_chip_rev_id) ==
12738 ASIC_REV_5701)
12739 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12740
Linus Torvalds1da177e2005-04-16 15:20:36 -070012741 break;
12742
12743 case SHASTA_EXT_LED_SHARED:
12744 tp->led_ctrl = LED_CTRL_MODE_SHARED;
12745 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
12746 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
12747 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12748 LED_CTRL_MODE_PHY_2);
12749 break;
12750
12751 case SHASTA_EXT_LED_MAC:
12752 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
12753 break;
12754
12755 case SHASTA_EXT_LED_COMBO:
12756 tp->led_ctrl = LED_CTRL_MODE_COMBO;
12757 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
12758 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12759 LED_CTRL_MODE_PHY_2);
12760 break;
12761
Stephen Hemminger855e1112008-04-16 16:37:28 -070012762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012763
12764 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
12765 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
12766 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
12767 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12768
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012769 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
12770 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080012771
Michael Chan9d26e212006-12-07 00:21:14 -080012772 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012773 tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080012774 if ((tp->pdev->subsystem_vendor ==
12775 PCI_VENDOR_ID_ARIMA) &&
12776 (tp->pdev->subsystem_device == 0x205a ||
12777 tp->pdev->subsystem_device == 0x2063))
12778 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
12779 } else {
David S. Millerf49639e2006-06-09 11:58:36 -070012780 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080012781 tp->tg3_flags2 |= TG3_FLG2_IS_NIC;
12782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012783
12784 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
12785 tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
John W. Linvillecbf46852005-04-21 17:01:29 -070012786 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012787 tp->tg3_flags2 |= TG3_FLG2_ASF_NEW_HANDSHAKE;
12788 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012789
12790 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
12791 (tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
Matt Carlson0d3031d2007-10-10 18:02:43 -070012792 tp->tg3_flags3 |= TG3_FLG3_ENABLE_APE;
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012793
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012794 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070012795 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
12796 tp->tg3_flags &= ~TG3_FLAG_WOL_CAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012797
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070012798 if ((tp->tg3_flags & TG3_FLAG_WOL_CAP) &&
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012799 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE))
Matt Carlson0527ba32007-10-10 18:03:30 -070012800 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
12801
Linus Torvalds1da177e2005-04-16 15:20:36 -070012802 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012803 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012804
12805 /* serdes signal pre-emphasis in register 0x590 set by */
12806 /* bootcode if bit 18 is set */
12807 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012808 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070012809
Matt Carlson1407deb2011-04-05 14:22:44 +000012810 if (((tp->tg3_flags3 & TG3_FLG3_57765_PLUS) ||
Matt Carlson2e1e3292010-11-24 08:31:53 +000012811 ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
12812 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX))) &&
Matt Carlson6833c042008-11-21 17:18:59 -080012813 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012814 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080012815
Matt Carlson8c69b1e2010-08-02 11:26:00 +000012816 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
12817 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlson1407deb2011-04-05 14:22:44 +000012818 !(tp->tg3_flags3 & TG3_FLG3_57765_PLUS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070012819 u32 cfg3;
12820
12821 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
12822 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
12823 tp->tg3_flags |= TG3_FLAG_ASPM_WORKAROUND;
12824 }
Matt Carlsona9daf362008-05-25 23:49:44 -070012825
Matt Carlson14417062010-02-17 15:16:59 +000012826 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
12827 tp->tg3_flags3 |= TG3_FLG3_RGMII_INBAND_DISABLE;
Matt Carlsona9daf362008-05-25 23:49:44 -070012828 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
12829 tp->tg3_flags3 |= TG3_FLG3_RGMII_EXT_IBND_RX_EN;
12830 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
12831 tp->tg3_flags3 |= TG3_FLG3_RGMII_EXT_IBND_TX_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012832 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012833done:
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000012834 if (tp->tg3_flags & TG3_FLAG_WOL_CAP)
12835 device_set_wakeup_enable(&tp->pdev->dev,
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012836 tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000012837 else
12838 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070012839}
12840
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012841static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
12842{
12843 int i;
12844 u32 val;
12845
12846 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
12847 tw32(OTP_CTRL, cmd);
12848
12849 /* Wait for up to 1 ms for command to execute. */
12850 for (i = 0; i < 100; i++) {
12851 val = tr32(OTP_STATUS);
12852 if (val & OTP_STATUS_CMD_DONE)
12853 break;
12854 udelay(10);
12855 }
12856
12857 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
12858}
12859
12860/* Read the gphy configuration from the OTP region of the chip. The gphy
12861 * configuration is a 32-bit value that straddles the alignment boundary.
12862 * We do two 32-bit reads and then shift and merge the results.
12863 */
12864static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
12865{
12866 u32 bhalf_otp, thalf_otp;
12867
12868 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
12869
12870 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
12871 return 0;
12872
12873 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
12874
12875 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12876 return 0;
12877
12878 thalf_otp = tr32(OTP_READ_DATA);
12879
12880 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
12881
12882 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12883 return 0;
12884
12885 bhalf_otp = tr32(OTP_READ_DATA);
12886
12887 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
12888}
12889
Matt Carlsone256f8a2011-03-09 16:58:24 +000012890static void __devinit tg3_phy_init_link_config(struct tg3 *tp)
12891{
12892 u32 adv = ADVERTISED_Autoneg |
12893 ADVERTISED_Pause;
12894
12895 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
12896 adv |= ADVERTISED_1000baseT_Half |
12897 ADVERTISED_1000baseT_Full;
12898
12899 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
12900 adv |= ADVERTISED_100baseT_Half |
12901 ADVERTISED_100baseT_Full |
12902 ADVERTISED_10baseT_Half |
12903 ADVERTISED_10baseT_Full |
12904 ADVERTISED_TP;
12905 else
12906 adv |= ADVERTISED_FIBRE;
12907
12908 tp->link_config.advertising = adv;
12909 tp->link_config.speed = SPEED_INVALID;
12910 tp->link_config.duplex = DUPLEX_INVALID;
12911 tp->link_config.autoneg = AUTONEG_ENABLE;
12912 tp->link_config.active_speed = SPEED_INVALID;
12913 tp->link_config.active_duplex = DUPLEX_INVALID;
12914 tp->link_config.orig_speed = SPEED_INVALID;
12915 tp->link_config.orig_duplex = DUPLEX_INVALID;
12916 tp->link_config.orig_autoneg = AUTONEG_INVALID;
12917}
12918
Michael Chan7d0c41e2005-04-21 17:06:20 -070012919static int __devinit tg3_phy_probe(struct tg3 *tp)
12920{
12921 u32 hw_phy_id_1, hw_phy_id_2;
12922 u32 hw_phy_id, hw_phy_id_masked;
12923 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012924
Matt Carlsone256f8a2011-03-09 16:58:24 +000012925 /* flow control autonegotiation is default behavior */
12926 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
12927 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
12928
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012929 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)
12930 return tg3_phy_init(tp);
12931
Linus Torvalds1da177e2005-04-16 15:20:36 -070012932 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010012933 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012934 */
12935 err = 0;
Matt Carlson0d3031d2007-10-10 18:02:43 -070012936 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
12937 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000012938 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012939 } else {
12940 /* Now read the physical PHY_ID from the chip and verify
12941 * that it is sane. If it doesn't look good, we fall back
12942 * to either the hard-coded table based PHY_ID and failing
12943 * that the value found in the eeprom area.
12944 */
12945 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
12946 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
12947
12948 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
12949 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
12950 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
12951
Matt Carlson79eb6902010-02-17 15:17:03 +000012952 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012953 }
12954
Matt Carlson79eb6902010-02-17 15:17:03 +000012955 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012956 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000012957 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012958 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070012959 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012960 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012961 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000012962 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070012963 /* Do nothing, phy ID already set up in
12964 * tg3_get_eeprom_hw_cfg().
12965 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070012966 } else {
12967 struct subsys_tbl_ent *p;
12968
12969 /* No eeprom signature? Try the hardcoded
12970 * subsys device table.
12971 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012972 p = tg3_lookup_by_subsys(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012973 if (!p)
12974 return -ENODEV;
12975
12976 tp->phy_id = p->phy_id;
12977 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000012978 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012979 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012980 }
12981 }
12982
Matt Carlsona6b68da2010-12-06 08:28:52 +000012983 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
12984 ((tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 &&
12985 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0) ||
12986 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12987 tp->pci_chip_rev_id != CHIPREV_ID_57765_A0)))
Matt Carlson52b02d02010-10-14 10:37:41 +000012988 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
12989
Matt Carlsone256f8a2011-03-09 16:58:24 +000012990 tg3_phy_init_link_config(tp);
12991
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012992 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Matt Carlson0d3031d2007-10-10 18:02:43 -070012993 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070012994 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chan3600d912006-12-07 00:21:48 -080012995 u32 bmsr, adv_reg, tg3_ctrl, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012996
12997 tg3_readphy(tp, MII_BMSR, &bmsr);
12998 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
12999 (bmsr & BMSR_LSTATUS))
13000 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013001
Linus Torvalds1da177e2005-04-16 15:20:36 -070013002 err = tg3_phy_reset(tp);
13003 if (err)
13004 return err;
13005
13006 adv_reg = (ADVERTISE_10HALF | ADVERTISE_10FULL |
13007 ADVERTISE_100HALF | ADVERTISE_100FULL |
13008 ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
13009 tg3_ctrl = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013010 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013011 tg3_ctrl = (MII_TG3_CTRL_ADV_1000_HALF |
13012 MII_TG3_CTRL_ADV_1000_FULL);
13013 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
13014 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
13015 tg3_ctrl |= (MII_TG3_CTRL_AS_MASTER |
13016 MII_TG3_CTRL_ENABLE_AS_MASTER);
13017 }
13018
Michael Chan3600d912006-12-07 00:21:48 -080013019 mask = (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
13020 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
13021 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full);
13022 if (!tg3_copper_is_advertising_all(tp, mask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013023 tg3_writephy(tp, MII_ADVERTISE, adv_reg);
13024
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013025 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013026 tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
13027
13028 tg3_writephy(tp, MII_BMCR,
13029 BMCR_ANENABLE | BMCR_ANRESTART);
13030 }
13031 tg3_phy_set_wirespeed(tp);
13032
13033 tg3_writephy(tp, MII_ADVERTISE, adv_reg);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013034 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013035 tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
13036 }
13037
13038skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000013039 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013040 err = tg3_init_5401phy_dsp(tp);
13041 if (err)
13042 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013043
Linus Torvalds1da177e2005-04-16 15:20:36 -070013044 err = tg3_init_5401phy_dsp(tp);
13045 }
13046
Linus Torvalds1da177e2005-04-16 15:20:36 -070013047 return err;
13048}
13049
Matt Carlson184b8902010-04-05 10:19:25 +000013050static void __devinit tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013051{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013052 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013053 unsigned int block_end, rosize, len;
Matt Carlson184b8902010-04-05 10:19:25 +000013054 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013055
Matt Carlsonc3e94502011-04-13 11:05:08 +000013056 vpd_data = (u8 *)tg3_vpd_readblock(tp);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013057 if (!vpd_data)
13058 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013059
Matt Carlson4181b2c2010-02-26 14:04:45 +000013060 i = pci_vpd_find_tag(vpd_data, 0, TG3_NVM_VPD_LEN,
13061 PCI_VPD_LRDT_RO_DATA);
13062 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013063 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013064
13065 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
13066 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
13067 i += PCI_VPD_LRDT_TAG_SIZE;
13068
13069 if (block_end > TG3_NVM_VPD_LEN)
13070 goto out_not_found;
13071
Matt Carlson184b8902010-04-05 10:19:25 +000013072 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13073 PCI_VPD_RO_KEYWORD_MFR_ID);
13074 if (j > 0) {
13075 len = pci_vpd_info_field_size(&vpd_data[j]);
13076
13077 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13078 if (j + len > block_end || len != 4 ||
13079 memcmp(&vpd_data[j], "1028", 4))
13080 goto partno;
13081
13082 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13083 PCI_VPD_RO_KEYWORD_VENDOR0);
13084 if (j < 0)
13085 goto partno;
13086
13087 len = pci_vpd_info_field_size(&vpd_data[j]);
13088
13089 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13090 if (j + len > block_end)
13091 goto partno;
13092
13093 memcpy(tp->fw_ver, &vpd_data[j], len);
13094 strncat(tp->fw_ver, " bc ", TG3_NVM_VPD_LEN - len - 1);
13095 }
13096
13097partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000013098 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13099 PCI_VPD_RO_KEYWORD_PARTNO);
13100 if (i < 0)
13101 goto out_not_found;
13102
13103 len = pci_vpd_info_field_size(&vpd_data[i]);
13104
13105 i += PCI_VPD_INFO_FLD_HDR_SIZE;
13106 if (len > TG3_BPN_SIZE ||
13107 (len + i) > TG3_NVM_VPD_LEN)
13108 goto out_not_found;
13109
13110 memcpy(tp->board_part_number, &vpd_data[i], len);
13111
Linus Torvalds1da177e2005-04-16 15:20:36 -070013112out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013113 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000013114 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013115 return;
13116
13117out_no_vpd:
Matt Carlson37a949c2010-09-30 10:34:33 +000013118 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
13119 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717)
13120 strcpy(tp->board_part_number, "BCM5717");
13121 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
13122 strcpy(tp->board_part_number, "BCM5718");
13123 else
13124 goto nomatch;
13125 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
13126 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
13127 strcpy(tp->board_part_number, "BCM57780");
13128 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
13129 strcpy(tp->board_part_number, "BCM57760");
13130 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
13131 strcpy(tp->board_part_number, "BCM57790");
13132 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
13133 strcpy(tp->board_part_number, "BCM57788");
13134 else
13135 goto nomatch;
13136 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
13137 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
13138 strcpy(tp->board_part_number, "BCM57761");
13139 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
13140 strcpy(tp->board_part_number, "BCM57765");
13141 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
13142 strcpy(tp->board_part_number, "BCM57781");
13143 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
13144 strcpy(tp->board_part_number, "BCM57785");
13145 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
13146 strcpy(tp->board_part_number, "BCM57791");
13147 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13148 strcpy(tp->board_part_number, "BCM57795");
13149 else
13150 goto nomatch;
13151 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070013152 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000013153 } else {
13154nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070013155 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000013156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013157}
13158
Matt Carlson9c8a6202007-10-21 16:16:08 -070013159static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
13160{
13161 u32 val;
13162
Matt Carlsone4f34112009-02-25 14:25:00 +000013163 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013164 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013165 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013166 val != 0)
13167 return 0;
13168
13169 return 1;
13170}
13171
Matt Carlsonacd9c112009-02-25 14:26:33 +000013172static void __devinit tg3_read_bc_ver(struct tg3 *tp)
13173{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013174 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000013175 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013176 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013177
13178 if (tg3_nvram_read(tp, 0xc, &offset) ||
13179 tg3_nvram_read(tp, 0x4, &start))
13180 return;
13181
13182 offset = tg3_nvram_logical_addr(tp, offset);
13183
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013184 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013185 return;
13186
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013187 if ((val & 0xfc000000) == 0x0c000000) {
13188 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013189 return;
13190
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013191 if (val == 0)
13192 newver = true;
13193 }
13194
Matt Carlson75f99362010-04-05 10:19:24 +000013195 dst_off = strlen(tp->fw_ver);
13196
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013197 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000013198 if (TG3_VER_SIZE - dst_off < 16 ||
13199 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013200 return;
13201
13202 offset = offset + ver_offset - start;
13203 for (i = 0; i < 16; i += 4) {
13204 __be32 v;
13205 if (tg3_nvram_read_be32(tp, offset + i, &v))
13206 return;
13207
Matt Carlson75f99362010-04-05 10:19:24 +000013208 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013209 }
13210 } else {
13211 u32 major, minor;
13212
13213 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
13214 return;
13215
13216 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
13217 TG3_NVM_BCVER_MAJSFT;
13218 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000013219 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
13220 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013221 }
13222}
13223
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013224static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
13225{
13226 u32 val, major, minor;
13227
13228 /* Use native endian representation */
13229 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
13230 return;
13231
13232 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
13233 TG3_NVM_HWSB_CFG1_MAJSFT;
13234 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
13235 TG3_NVM_HWSB_CFG1_MINSFT;
13236
13237 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
13238}
13239
Matt Carlsondfe00d72008-11-21 17:19:41 -080013240static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
13241{
13242 u32 offset, major, minor, build;
13243
Matt Carlson75f99362010-04-05 10:19:24 +000013244 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013245
13246 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
13247 return;
13248
13249 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
13250 case TG3_EEPROM_SB_REVISION_0:
13251 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
13252 break;
13253 case TG3_EEPROM_SB_REVISION_2:
13254 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
13255 break;
13256 case TG3_EEPROM_SB_REVISION_3:
13257 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
13258 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000013259 case TG3_EEPROM_SB_REVISION_4:
13260 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
13261 break;
13262 case TG3_EEPROM_SB_REVISION_5:
13263 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
13264 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000013265 case TG3_EEPROM_SB_REVISION_6:
13266 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
13267 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013268 default:
13269 return;
13270 }
13271
Matt Carlsone4f34112009-02-25 14:25:00 +000013272 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080013273 return;
13274
13275 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
13276 TG3_EEPROM_SB_EDH_BLD_SHFT;
13277 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
13278 TG3_EEPROM_SB_EDH_MAJ_SHFT;
13279 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
13280
13281 if (minor > 99 || build > 26)
13282 return;
13283
Matt Carlson75f99362010-04-05 10:19:24 +000013284 offset = strlen(tp->fw_ver);
13285 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
13286 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013287
13288 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000013289 offset = strlen(tp->fw_ver);
13290 if (offset < TG3_VER_SIZE - 1)
13291 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013292 }
13293}
13294
Matt Carlsonacd9c112009-02-25 14:26:33 +000013295static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080013296{
13297 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013298 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070013299
13300 for (offset = TG3_NVM_DIR_START;
13301 offset < TG3_NVM_DIR_END;
13302 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000013303 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013304 return;
13305
13306 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
13307 break;
13308 }
13309
13310 if (offset == TG3_NVM_DIR_END)
13311 return;
13312
13313 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
13314 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000013315 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013316 return;
13317
Matt Carlsone4f34112009-02-25 14:25:00 +000013318 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013319 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013320 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013321 return;
13322
13323 offset += val - start;
13324
Matt Carlsonacd9c112009-02-25 14:26:33 +000013325 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013326
Matt Carlsonacd9c112009-02-25 14:26:33 +000013327 tp->fw_ver[vlen++] = ',';
13328 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070013329
13330 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000013331 __be32 v;
13332 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013333 return;
13334
Al Virob9fc7dc2007-12-17 22:59:57 -080013335 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013336
Matt Carlsonacd9c112009-02-25 14:26:33 +000013337 if (vlen > TG3_VER_SIZE - sizeof(v)) {
13338 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013339 break;
13340 }
13341
Matt Carlsonacd9c112009-02-25 14:26:33 +000013342 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
13343 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013344 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000013345}
13346
Matt Carlson7fd76442009-02-25 14:27:20 +000013347static void __devinit tg3_read_dash_ver(struct tg3 *tp)
13348{
13349 int vlen;
13350 u32 apedata;
Matt Carlsonecc79642010-08-02 11:26:01 +000013351 char *fwtype;
Matt Carlson7fd76442009-02-25 14:27:20 +000013352
13353 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) ||
13354 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
13355 return;
13356
13357 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
13358 if (apedata != APE_SEG_SIG_MAGIC)
13359 return;
13360
13361 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
13362 if (!(apedata & APE_FW_STATUS_READY))
13363 return;
13364
13365 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
13366
Matt Carlsondc6d0742010-09-15 08:59:55 +000013367 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI) {
13368 tp->tg3_flags3 |= TG3_FLG3_APE_HAS_NCSI;
Matt Carlsonecc79642010-08-02 11:26:01 +000013369 fwtype = "NCSI";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013370 } else {
Matt Carlsonecc79642010-08-02 11:26:01 +000013371 fwtype = "DASH";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013372 }
Matt Carlsonecc79642010-08-02 11:26:01 +000013373
Matt Carlson7fd76442009-02-25 14:27:20 +000013374 vlen = strlen(tp->fw_ver);
13375
Matt Carlsonecc79642010-08-02 11:26:01 +000013376 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
13377 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000013378 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
13379 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
13380 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
13381 (apedata & APE_FW_VERSION_BLDMSK));
13382}
13383
Matt Carlsonacd9c112009-02-25 14:26:33 +000013384static void __devinit tg3_read_fw_ver(struct tg3 *tp)
13385{
13386 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000013387 bool vpd_vers = false;
13388
13389 if (tp->fw_ver[0] != 0)
13390 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013391
Matt Carlsondf259d82009-04-20 06:57:14 +000013392 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) {
Matt Carlson75f99362010-04-05 10:19:24 +000013393 strcat(tp->fw_ver, "sb");
Matt Carlsondf259d82009-04-20 06:57:14 +000013394 return;
13395 }
13396
Matt Carlsonacd9c112009-02-25 14:26:33 +000013397 if (tg3_nvram_read(tp, 0, &val))
13398 return;
13399
13400 if (val == TG3_EEPROM_MAGIC)
13401 tg3_read_bc_ver(tp);
13402 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
13403 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013404 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
13405 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013406 else
13407 return;
13408
13409 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
Matt Carlson75f99362010-04-05 10:19:24 +000013410 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) || vpd_vers)
13411 goto done;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013412
13413 tg3_read_mgmtfw_ver(tp);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013414
Matt Carlson75f99362010-04-05 10:19:24 +000013415done:
Matt Carlson9c8a6202007-10-21 16:16:08 -070013416 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080013417}
13418
Michael Chan7544b092007-05-05 13:08:32 -070013419static struct pci_dev * __devinit tg3_find_peer(struct tg3 *);
13420
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013421static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
13422{
Matt Carlsonde9f5232011-04-05 14:22:43 +000013423 if (tp->tg3_flags3 & TG3_FLG3_LRG_PROD_RING_CAP)
13424 return TG3_RX_RET_MAX_SIZE_5717;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013425 else if ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) &&
13426 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013427 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013428 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000013429 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013430}
13431
Matt Carlson41434702011-03-09 16:58:22 +000013432static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080013433 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
13434 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
13435 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
13436 { },
13437};
13438
Linus Torvalds1da177e2005-04-16 15:20:36 -070013439static int __devinit tg3_get_invariants(struct tg3 *tp)
13440{
Linus Torvalds1da177e2005-04-16 15:20:36 -070013441 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013442 u32 pci_state_reg, grc_misc_cfg;
13443 u32 val;
13444 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013445 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013446
Linus Torvalds1da177e2005-04-16 15:20:36 -070013447 /* Force memory write invalidate off. If we leave it on,
13448 * then on 5700_BX chips we have to enable a workaround.
13449 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
13450 * to match the cacheline size. The Broadcom driver have this
13451 * workaround but turns MWI off all the times so never uses
13452 * it. This seems to suggest that the workaround is insufficient.
13453 */
13454 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13455 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
13456 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13457
13458 /* It is absolutely critical that TG3PCI_MISC_HOST_CTRL
13459 * has the register indirect write enable bit set before
13460 * we try to access any of the MMIO registers. It is also
13461 * critical that the PCI-X hw workaround situation is decided
13462 * before that as well.
13463 */
13464 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13465 &misc_ctrl_reg);
13466
13467 tp->pci_chip_rev_id = (misc_ctrl_reg >>
13468 MISC_HOST_CTRL_CHIPREV_SHIFT);
Matt Carlson795d01c2007-10-07 23:28:17 -070013469 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
13470 u32 prod_id_asic_rev;
13471
Matt Carlson5001e2f2009-11-13 13:03:51 +000013472 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
13473 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013474 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
13475 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013476 pci_read_config_dword(tp->pdev,
13477 TG3PCI_GEN2_PRODID_ASICREV,
13478 &prod_id_asic_rev);
Matt Carlsonb703df62009-12-03 08:36:21 +000013479 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
13480 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
13481 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
13482 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
13483 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
13484 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13485 pci_read_config_dword(tp->pdev,
13486 TG3PCI_GEN15_PRODID_ASICREV,
13487 &prod_id_asic_rev);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013488 else
13489 pci_read_config_dword(tp->pdev, TG3PCI_PRODID_ASICREV,
13490 &prod_id_asic_rev);
13491
Matt Carlson321d32a2008-11-21 17:22:19 -080013492 tp->pci_chip_rev_id = prod_id_asic_rev;
Matt Carlson795d01c2007-10-07 23:28:17 -070013493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013494
Michael Chanff645be2005-04-21 17:09:53 -070013495 /* Wrong chip ID in 5752 A0. This code can be removed later
13496 * as A0 is not in production.
13497 */
13498 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
13499 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
13500
Michael Chan68929142005-08-09 20:17:14 -070013501 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
13502 * we need to disable memory and use config. cycles
13503 * only to access all registers. The 5702/03 chips
13504 * can mistakenly decode the special cycles from the
13505 * ICH chipsets as memory write cycles, causing corruption
13506 * of register and memory space. Only certain ICH bridges
13507 * will drive special cycles with non-zero data during the
13508 * address phase which can fall within the 5703's address
13509 * range. This is not an ICH bug as the PCI spec allows
13510 * non-zero address during special cycles. However, only
13511 * these ICH bridges are known to drive non-zero addresses
13512 * during special cycles.
13513 *
13514 * Since special cycles do not cross PCI bridges, we only
13515 * enable this workaround if the 5703 is on the secondary
13516 * bus of these ICH bridges.
13517 */
13518 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
13519 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
13520 static struct tg3_dev_id {
13521 u32 vendor;
13522 u32 device;
13523 u32 rev;
13524 } ich_chipsets[] = {
13525 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
13526 PCI_ANY_ID },
13527 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
13528 PCI_ANY_ID },
13529 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
13530 0xa },
13531 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
13532 PCI_ANY_ID },
13533 { },
13534 };
13535 struct tg3_dev_id *pci_id = &ich_chipsets[0];
13536 struct pci_dev *bridge = NULL;
13537
13538 while (pci_id->vendor != 0) {
13539 bridge = pci_get_device(pci_id->vendor, pci_id->device,
13540 bridge);
13541 if (!bridge) {
13542 pci_id++;
13543 continue;
13544 }
13545 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070013546 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070013547 continue;
13548 }
13549 if (bridge->subordinate &&
13550 (bridge->subordinate->number ==
13551 tp->pdev->bus->number)) {
13552
13553 tp->tg3_flags2 |= TG3_FLG2_ICH_WORKAROUND;
13554 pci_dev_put(bridge);
13555 break;
13556 }
13557 }
13558 }
13559
Matt Carlson41588ba2008-04-19 18:12:33 -070013560 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
13561 static struct tg3_dev_id {
13562 u32 vendor;
13563 u32 device;
13564 } bridge_chipsets[] = {
13565 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
13566 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
13567 { },
13568 };
13569 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
13570 struct pci_dev *bridge = NULL;
13571
13572 while (pci_id->vendor != 0) {
13573 bridge = pci_get_device(pci_id->vendor,
13574 pci_id->device,
13575 bridge);
13576 if (!bridge) {
13577 pci_id++;
13578 continue;
13579 }
13580 if (bridge->subordinate &&
13581 (bridge->subordinate->number <=
13582 tp->pdev->bus->number) &&
13583 (bridge->subordinate->subordinate >=
13584 tp->pdev->bus->number)) {
13585 tp->tg3_flags3 |= TG3_FLG3_5701_DMA_BUG;
13586 pci_dev_put(bridge);
13587 break;
13588 }
13589 }
13590 }
13591
Michael Chan4a29cc22006-03-19 13:21:12 -080013592 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
13593 * DMA addresses > 40-bit. This bridge may have other additional
13594 * 57xx devices behind it in some 4-port NIC designs for example.
13595 * Any tg3 device found behind the bridge will also need the 40-bit
13596 * DMA workaround.
13597 */
Michael Chana4e2b342005-10-26 15:46:52 -070013598 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
13599 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
13600 tp->tg3_flags2 |= TG3_FLG2_5780_CLASS;
Michael Chan4a29cc22006-03-19 13:21:12 -080013601 tp->tg3_flags |= TG3_FLAG_40BIT_DMA_BUG;
Michael Chan4cf78e42005-07-25 12:29:19 -070013602 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a588792010-04-05 10:19:28 +000013603 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080013604 struct pci_dev *bridge = NULL;
13605
13606 do {
13607 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
13608 PCI_DEVICE_ID_SERVERWORKS_EPB,
13609 bridge);
13610 if (bridge && bridge->subordinate &&
13611 (bridge->subordinate->number <=
13612 tp->pdev->bus->number) &&
13613 (bridge->subordinate->subordinate >=
13614 tp->pdev->bus->number)) {
13615 tp->tg3_flags |= TG3_FLAG_40BIT_DMA_BUG;
13616 pci_dev_put(bridge);
13617 break;
13618 }
13619 } while (bridge);
13620 }
Michael Chan4cf78e42005-07-25 12:29:19 -070013621
Linus Torvalds1da177e2005-04-16 15:20:36 -070013622 /* Initialize misc host control in PCI block. */
13623 tp->misc_host_ctrl |= (misc_ctrl_reg &
13624 MISC_HOST_CTRL_CHIPREV);
13625 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13626 tp->misc_host_ctrl);
13627
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013628 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
13629 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013630 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13631 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Michael Chan7544b092007-05-05 13:08:32 -070013632 tp->pdev_peer = tg3_find_peer(tp);
13633
Matt Carlsonc885e822010-08-02 11:25:57 +000013634 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013635 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13636 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Matt Carlson0a58d662011-04-05 14:22:45 +000013637 tp->tg3_flags3 |= TG3_FLG3_5717_PLUS;
13638
13639 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 ||
13640 (tp->tg3_flags3 & TG3_FLG3_5717_PLUS))
Matt Carlson1407deb2011-04-05 14:22:44 +000013641 tp->tg3_flags3 |= TG3_FLG3_57765_PLUS;
Matt Carlsonc885e822010-08-02 11:25:57 +000013642
Matt Carlson321d32a2008-11-21 17:22:19 -080013643 /* Intentionally exclude ASIC_REV_5906 */
13644 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Michael Chand9ab5ad12006-03-20 22:27:35 -080013645 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013646 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013647 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013648 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013649 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlson1407deb2011-04-05 14:22:44 +000013650 (tp->tg3_flags3 & TG3_FLG3_57765_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080013651 tp->tg3_flags3 |= TG3_FLG3_5755_PLUS;
13652
13653 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13654 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Michael Chanb5d37722006-09-27 16:06:21 -070013655 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013656 (tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
Michael Chana4e2b342005-10-26 15:46:52 -070013657 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
John W. Linville6708e5c2005-04-21 17:00:52 -070013658 tp->tg3_flags2 |= TG3_FLG2_5750_PLUS;
13659
Matt Carlsoneb07a942011-04-20 07:57:36 +000013660
John W. Linville1b440c562005-04-21 17:03:18 -070013661 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) ||
13662 (tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
13663 tp->tg3_flags2 |= TG3_FLG2_5705_PLUS;
13664
Matt Carlson027455a2008-12-21 20:19:30 -080013665 /* 5700 B0 chips do not support checksumming correctly due
13666 * to hardware bugs.
13667 */
Michał Mirosławdc668912011-04-07 03:35:07 +000013668 if (tp->pci_chip_rev_id != CHIPREV_ID_5700_B0) {
13669 u32 features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
Eric Dumazet7fe876a2010-07-08 06:14:55 +000013670
Matt Carlson027455a2008-12-21 20:19:30 -080013671 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Eric Dumazet7fe876a2010-07-08 06:14:55 +000013672 features |= NETIF_F_IPV6_CSUM;
13673 tp->dev->features |= features;
Michał Mirosławdc668912011-04-07 03:35:07 +000013674 tp->dev->hw_features |= features;
13675 tp->dev->vlan_features |= features;
Matt Carlson027455a2008-12-21 20:19:30 -080013676 }
13677
Matt Carlson507399f2009-11-13 13:03:37 +000013678 /* Determine TSO capabilities */
Matt Carlson2866d952011-02-10 20:06:46 -080013679 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlson4d163b72011-01-25 15:58:48 +000013680 ; /* Do nothing. HW bug. */
Matt Carlson1407deb2011-04-05 14:22:44 +000013681 else if (tp->tg3_flags3 & TG3_FLG3_57765_PLUS)
Matt Carlsone849cdc2009-11-13 13:03:38 +000013682 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_3;
13683 else if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
13684 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlson507399f2009-11-13 13:03:37 +000013685 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_2;
13686 else if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
13687 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_1 | TG3_FLG2_TSO_BUG;
13688 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
13689 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
13690 tp->tg3_flags2 &= ~TG3_FLG2_TSO_BUG;
13691 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13692 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13693 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
13694 tp->tg3_flags2 |= TG3_FLG2_TSO_BUG;
13695 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
13696 tp->fw_needed = FIRMWARE_TG3TSO5;
13697 else
13698 tp->fw_needed = FIRMWARE_TG3TSO;
13699 }
13700
13701 tp->irq_max = 1;
13702
Michael Chan5a6f3072006-03-20 22:28:05 -080013703 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
Michael Chan7544b092007-05-05 13:08:32 -070013704 tp->tg3_flags |= TG3_FLAG_SUPPORT_MSI;
13705 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
13706 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
13707 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
13708 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
13709 tp->pdev_peer == tp->pdev))
13710 tp->tg3_flags &= ~TG3_FLAG_SUPPORT_MSI;
13711
Matt Carlson321d32a2008-11-21 17:22:19 -080013712 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070013713 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanfcfa0a32006-03-20 22:28:41 -080013714 tp->tg3_flags2 |= TG3_FLG2_1SHOT_MSI;
Michael Chan52c0fd82006-06-29 20:15:54 -070013715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013716
Matt Carlson1407deb2011-04-05 14:22:44 +000013717 if (tp->tg3_flags3 & TG3_FLG3_57765_PLUS) {
Matt Carlson507399f2009-11-13 13:03:37 +000013718 tp->tg3_flags |= TG3_FLAG_SUPPORT_MSIX;
13719 tp->irq_max = TG3_IRQ_MAX_VECS;
13720 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013721 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000013722
Matt Carlson615774f2009-11-13 13:03:39 +000013723 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsona50d0792010-06-05 17:24:37 +000013724 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlson615774f2009-11-13 13:03:39 +000013725 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
13726 tp->tg3_flags3 |= TG3_FLG3_SHORT_DMA_BUG;
13727 else if (!(tp->tg3_flags3 & TG3_FLG3_5755_PLUS)) {
13728 tp->tg3_flags3 |= TG3_FLG3_4G_DMA_BNDRY_BUG;
13729 tp->tg3_flags3 |= TG3_FLG3_40BIT_DMA_LIMIT_BUG;
Matt Carlson0e1406d2009-11-02 12:33:33 +000013730 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013731
Matt Carlson0a58d662011-04-05 14:22:45 +000013732 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)
Matt Carlsonde9f5232011-04-05 14:22:43 +000013733 tp->tg3_flags3 |= TG3_FLG3_LRG_PROD_RING_CAP;
13734
Matt Carlson1407deb2011-04-05 14:22:44 +000013735 if ((tp->tg3_flags3 & TG3_FLG3_57765_PLUS) &&
Matt Carlson2866d952011-02-10 20:06:46 -080013736 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5719)
Matt Carlsonb703df62009-12-03 08:36:21 +000013737 tp->tg3_flags3 |= TG3_FLG3_USE_JUMBO_BDFLAG;
13738
Matt Carlsonf51f3562008-05-25 23:45:08 -070013739 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
Matt Carlsonc6cdf432010-04-05 10:19:26 +000013740 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
13741 (tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG))
Matt Carlson8f666b02009-08-28 13:58:24 +000013742 tp->tg3_flags |= TG3_FLAG_JUMBO_CAPABLE;
Michael Chan0f893dc2005-07-25 12:30:38 -070013743
Matt Carlson52f44902008-11-21 17:17:04 -080013744 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
13745 &pci_state_reg);
13746
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013747 tp->pcie_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_EXP);
13748 if (tp->pcie_cap != 0) {
13749 u16 lnkctl;
13750
Linus Torvalds1da177e2005-04-16 15:20:36 -070013751 tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS;
Matt Carlson5f5c51e2007-11-12 21:19:37 -080013752
Matt Carlsoncf790032010-11-24 08:31:48 +000013753 tp->pcie_readrq = 4096;
Matt Carlsond78b59f2011-04-05 14:22:46 +000013754 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13755 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Matt Carlsonb4495ed2011-01-25 15:58:47 +000013756 tp->pcie_readrq = 2048;
Matt Carlsoncf790032010-11-24 08:31:48 +000013757
13758 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080013759
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013760 pci_read_config_word(tp->pdev,
13761 tp->pcie_cap + PCI_EXP_LNKCTL,
13762 &lnkctl);
13763 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
13764 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanc7835a72006-11-15 21:14:42 -080013765 tp->tg3_flags2 &= ~TG3_FLG2_HW_TSO_2;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013766 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013767 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000013768 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
13769 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013770 tp->tg3_flags3 |= TG3_FLG3_CLKREQ_BUG;
Matt Carlson614b0592010-01-20 16:58:02 +000013771 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
13772 tp->tg3_flags3 |= TG3_FLG3_L1PLLPD_EN;
Michael Chanc7835a72006-11-15 21:14:42 -080013773 }
Matt Carlson52f44902008-11-21 17:17:04 -080013774 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Matt Carlsonfcb389d2008-11-03 16:55:44 -080013775 tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS;
Matt Carlson52f44902008-11-21 17:17:04 -080013776 } else if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
13777 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
13778 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
13779 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000013780 dev_err(&tp->pdev->dev,
13781 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080013782 return -EIO;
13783 }
13784
13785 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
13786 tp->tg3_flags |= TG3_FLAG_PCIX_MODE;
13787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013788
Michael Chan399de502005-10-03 14:02:39 -070013789 /* If we have an AMD 762 or VIA K8T800 chipset, write
13790 * reordering to the mailbox registers done by the host
13791 * controller can cause major troubles. We read back from
13792 * every mailbox register write to force the writes to be
13793 * posted to the chip in order.
13794 */
Matt Carlson41434702011-03-09 16:58:22 +000013795 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Michael Chan399de502005-10-03 14:02:39 -070013796 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
13797 tp->tg3_flags |= TG3_FLAG_MBOX_WRITE_REORDER;
13798
Matt Carlson69fc4052008-12-21 20:19:57 -080013799 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
13800 &tp->pci_cacheline_sz);
13801 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13802 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013803 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
13804 tp->pci_lat_timer < 64) {
13805 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080013806 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13807 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013808 }
13809
Matt Carlson52f44902008-11-21 17:17:04 -080013810 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
13811 /* 5700 BX chips need to have their TX producer index
13812 * mailboxes written twice to workaround a bug.
13813 */
13814 tp->tg3_flags |= TG3_FLAG_TXD_MBOX_HWBUG;
Matt Carlson9974a352007-10-07 23:27:28 -070013815
Matt Carlson52f44902008-11-21 17:17:04 -080013816 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013817 *
13818 * The workaround is to use indirect register accesses
13819 * for all chip writes not to mailbox registers.
13820 */
Matt Carlson52f44902008-11-21 17:17:04 -080013821 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013822 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013823
13824 tp->tg3_flags |= TG3_FLAG_PCIX_TARGET_HWBUG;
13825
13826 /* The chip can have it's power management PCI config
13827 * space registers clobbered due to this bug.
13828 * So explicitly force the chip into D0 here.
13829 */
Matt Carlson9974a352007-10-07 23:27:28 -070013830 pci_read_config_dword(tp->pdev,
13831 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013832 &pm_reg);
13833 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
13834 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070013835 pci_write_config_dword(tp->pdev,
13836 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013837 pm_reg);
13838
13839 /* Also, force SERR#/PERR# in PCI command. */
13840 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13841 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
13842 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13843 }
13844 }
13845
Linus Torvalds1da177e2005-04-16 15:20:36 -070013846 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
13847 tp->tg3_flags |= TG3_FLAG_PCI_HIGH_SPEED;
13848 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
13849 tp->tg3_flags |= TG3_FLAG_PCI_32BIT;
13850
13851 /* Chip-specific fixup from Broadcom driver */
13852 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
13853 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
13854 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
13855 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
13856 }
13857
Michael Chan1ee582d2005-08-09 20:16:46 -070013858 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070013859 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013860 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070013861 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070013862 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013863 tp->write32_tx_mbox = tg3_write32;
13864 tp->write32_rx_mbox = tg3_write32;
13865
13866 /* Various workaround register access methods */
13867 if (tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG)
13868 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013869 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
13870 ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
13871 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
13872 /*
13873 * Back to back register writes can cause problems on these
13874 * chips, the workaround is to read back all reg writes
13875 * except those to mailbox regs.
13876 *
13877 * See tg3_write_indirect_reg32().
13878 */
Michael Chan1ee582d2005-08-09 20:16:46 -070013879 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013880 }
13881
Michael Chan1ee582d2005-08-09 20:16:46 -070013882 if ((tp->tg3_flags & TG3_FLAG_TXD_MBOX_HWBUG) ||
13883 (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)) {
13884 tp->write32_tx_mbox = tg3_write32_tx_mbox;
13885 if (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)
13886 tp->write32_rx_mbox = tg3_write_flush_reg32;
13887 }
Michael Chan20094932005-08-09 20:16:32 -070013888
Michael Chan68929142005-08-09 20:17:14 -070013889 if (tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND) {
13890 tp->read32 = tg3_read_indirect_reg32;
13891 tp->write32 = tg3_write_indirect_reg32;
13892 tp->read32_mbox = tg3_read_indirect_mbox;
13893 tp->write32_mbox = tg3_write_indirect_mbox;
13894 tp->write32_tx_mbox = tg3_write_indirect_mbox;
13895 tp->write32_rx_mbox = tg3_write_indirect_mbox;
13896
13897 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070013898 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070013899
13900 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13901 pci_cmd &= ~PCI_COMMAND_MEMORY;
13902 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13903 }
Michael Chanb5d37722006-09-27 16:06:21 -070013904 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
13905 tp->read32_mbox = tg3_read32_mbox_5906;
13906 tp->write32_mbox = tg3_write32_mbox_5906;
13907 tp->write32_tx_mbox = tg3_write32_mbox_5906;
13908 tp->write32_rx_mbox = tg3_write32_mbox_5906;
13909 }
Michael Chan68929142005-08-09 20:17:14 -070013910
Michael Chanbbadf502006-04-06 21:46:34 -070013911 if (tp->write32 == tg3_write_indirect_reg32 ||
13912 ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) &&
13913 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070013914 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Michael Chanbbadf502006-04-06 21:46:34 -070013915 tp->tg3_flags |= TG3_FLAG_SRAM_USE_CONFIG;
13916
Michael Chan7d0c41e2005-04-21 17:06:20 -070013917 /* Get eeprom hw config before calling tg3_set_power_state().
Michael Chan9d26e212006-12-07 00:21:14 -080013918 * In particular, the TG3_FLG2_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070013919 * determined before calling tg3_set_power_state() so that
13920 * we know whether or not to switch out of Vaux power.
13921 * When the flag is set, it means that GPIO1 is used for eeprom
13922 * write protect and also implies that it is a LOM where GPIOs
13923 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013924 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070013925 tg3_get_eeprom_hw_cfg(tp);
13926
Matt Carlson0d3031d2007-10-10 18:02:43 -070013927 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
13928 /* Allow reads and writes to the
13929 * APE register and memory space.
13930 */
13931 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +000013932 PCISTATE_ALLOW_APE_SHMEM_WR |
13933 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070013934 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
13935 pci_state_reg);
13936 }
13937
Matt Carlson9936bcf2007-10-10 18:03:07 -070013938 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013939 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013940 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013941 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlson1407deb2011-04-05 14:22:44 +000013942 (tp->tg3_flags3 & TG3_FLG3_57765_PLUS))
Matt Carlsond30cdd22007-10-07 23:28:35 -070013943 tp->tg3_flags |= TG3_FLAG_CPMU_PRESENT;
13944
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000013945 /* Set up tp->grc_local_ctrl before calling tg_power_up().
Michael Chan314fba32005-04-21 17:07:04 -070013946 * GPIO1 driven high will bring 5700's external PHY out of reset.
13947 * It is also used as eeprom write protect on LOMs.
13948 */
13949 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
13950 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
13951 (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT))
13952 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
13953 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070013954 /* Unused GPIO3 must be driven as output on 5752 because there
13955 * are no pull-up resistors on unused GPIO pins.
13956 */
13957 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
13958 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070013959
Matt Carlson321d32a2008-11-21 17:22:19 -080013960 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsoncb4ed1f2010-01-20 16:58:09 +000013961 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
13962 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Michael Chanaf36e6b2006-03-23 01:28:06 -080013963 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
13964
Matt Carlson8d519ab2009-04-20 06:58:01 +000013965 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
13966 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070013967 /* Turn off the debug UART. */
13968 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
13969 if (tp->tg3_flags2 & TG3_FLG2_IS_NIC)
13970 /* Keep VMain power. */
13971 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
13972 GRC_LCLCTRL_GPIO_OUTPUT0;
13973 }
13974
Linus Torvalds1da177e2005-04-16 15:20:36 -070013975 /* Force the chip into D0. */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000013976 err = tg3_power_up(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013977 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000013978 dev_err(&tp->pdev->dev, "Transition to D0 failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070013979 return err;
13980 }
13981
Linus Torvalds1da177e2005-04-16 15:20:36 -070013982 /* Derive initial jumbo mode from MTU assigned in
13983 * ether_setup() via the alloc_etherdev() call
13984 */
Michael Chan0f893dc2005-07-25 12:30:38 -070013985 if (tp->dev->mtu > ETH_DATA_LEN &&
Michael Chana4e2b342005-10-26 15:46:52 -070013986 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Michael Chan0f893dc2005-07-25 12:30:38 -070013987 tp->tg3_flags |= TG3_FLAG_JUMBO_RING_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013988
13989 /* Determine WakeOnLan speed to use. */
13990 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13991 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
13992 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
13993 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
13994 tp->tg3_flags &= ~(TG3_FLAG_WOL_SPEED_100MB);
13995 } else {
13996 tp->tg3_flags |= TG3_FLAG_WOL_SPEED_100MB;
13997 }
13998
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013999 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014000 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000014001
Linus Torvalds1da177e2005-04-16 15:20:36 -070014002 /* A few boards don't want Ethernet@WireSpeed phy feature */
14003 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
14004 ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
14005 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070014006 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014007 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
14008 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
14009 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014010
14011 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
14012 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014013 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014014 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014015 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014016
Matt Carlson321d32a2008-11-21 17:22:19 -080014017 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014018 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080014019 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014020 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
Matt Carlson1407deb2011-04-05 14:22:44 +000014021 !(tp->tg3_flags3 & TG3_FLG3_57765_PLUS)) {
Michael Chanc424cb22006-04-29 18:56:34 -070014022 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070014023 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070014024 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
14025 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080014026 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
14027 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014028 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080014029 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014030 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080014031 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014032 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070014033 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014034
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014035 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
14036 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
14037 tp->phy_otp = tg3_read_otp_phycfg(tp);
14038 if (tp->phy_otp == 0)
14039 tp->phy_otp = TG3_OTP_DEFAULT;
14040 }
14041
Matt Carlsonf51f3562008-05-25 23:45:08 -070014042 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT)
Matt Carlson8ef21422008-05-02 16:47:53 -070014043 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
14044 else
14045 tp->mi_mode = MAC_MI_MODE_BASE;
14046
Linus Torvalds1da177e2005-04-16 15:20:36 -070014047 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014048 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
14049 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
14050 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
14051
Matt Carlson4d958472011-04-20 07:57:35 +000014052 /* Set these bits to enable statistics workaround. */
14053 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14054 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
14055 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0) {
14056 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
14057 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
14058 }
14059
Matt Carlson321d32a2008-11-21 17:22:19 -080014060 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
14061 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson57e69832008-05-25 23:48:31 -070014062 tp->tg3_flags3 |= TG3_FLG3_USE_PHYLIB;
14063
Matt Carlson158d7ab2008-05-29 01:37:54 -070014064 err = tg3_mdio_init(tp);
14065 if (err)
14066 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014067
14068 /* Initialize data/descriptor byte/word swapping. */
14069 val = tr32(GRC_MODE);
Matt Carlsonf2096f92011-04-05 14:22:48 +000014070 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
14071 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
14072 GRC_MODE_WORD_SWAP_B2HRX_DATA |
14073 GRC_MODE_B2HRX_ENABLE |
14074 GRC_MODE_HTX2B_ENABLE |
14075 GRC_MODE_HOST_STACKUP);
14076 else
14077 val &= GRC_MODE_HOST_STACKUP;
14078
Linus Torvalds1da177e2005-04-16 15:20:36 -070014079 tw32(GRC_MODE, val | tp->grc_mode);
14080
14081 tg3_switch_clocks(tp);
14082
14083 /* Clear this out for sanity. */
14084 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
14085
14086 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14087 &pci_state_reg);
14088 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
14089 (tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG) == 0) {
14090 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
14091
14092 if (chiprevid == CHIPREV_ID_5701_A0 ||
14093 chiprevid == CHIPREV_ID_5701_B0 ||
14094 chiprevid == CHIPREV_ID_5701_B2 ||
14095 chiprevid == CHIPREV_ID_5701_B5) {
14096 void __iomem *sram_base;
14097
14098 /* Write some dummy words into the SRAM status block
14099 * area, see if it reads back correctly. If the return
14100 * value is bad, force enable the PCIX workaround.
14101 */
14102 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
14103
14104 writel(0x00000000, sram_base);
14105 writel(0x00000000, sram_base + 4);
14106 writel(0xffffffff, sram_base + 4);
14107 if (readl(sram_base) != 0x00000000)
14108 tp->tg3_flags |= TG3_FLAG_PCIX_TARGET_HWBUG;
14109 }
14110 }
14111
14112 udelay(50);
14113 tg3_nvram_init(tp);
14114
14115 grc_misc_cfg = tr32(GRC_MISC_CFG);
14116 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
14117
Linus Torvalds1da177e2005-04-16 15:20:36 -070014118 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14119 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
14120 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
14121 tp->tg3_flags2 |= TG3_FLG2_IS_5788;
14122
David S. Millerfac9b832005-05-18 22:46:34 -070014123 if (!(tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
14124 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700))
14125 tp->tg3_flags |= TG3_FLAG_TAGGED_STATUS;
14126 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) {
14127 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
14128 HOSTCC_MODE_CLRTICK_TXBD);
14129
14130 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
14131 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
14132 tp->misc_host_ctrl);
14133 }
14134
Matt Carlson3bda1252008-08-15 14:08:22 -070014135 /* Preserve the APE MAC_MODE bits */
14136 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
Matt Carlsond2394e6b2010-11-24 08:31:47 +000014137 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070014138 else
14139 tp->mac_mode = TG3_DEF_MAC_MODE;
14140
Linus Torvalds1da177e2005-04-16 15:20:36 -070014141 /* these are limited to 10/100 only */
14142 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14143 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
14144 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14145 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14146 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
14147 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
14148 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
14149 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14150 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
Michael Chan676917d2006-12-07 00:20:22 -080014151 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
14152 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014153 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
Matt Carlsond1101142010-02-17 15:16:55 +000014154 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
14155 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014156 (tp->phy_flags & TG3_PHYFLG_IS_FET))
14157 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014158
14159 err = tg3_phy_probe(tp);
14160 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014161 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014162 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014163 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014164 }
14165
Matt Carlson184b8902010-04-05 10:19:25 +000014166 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080014167 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014168
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014169 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
14170 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014171 } else {
14172 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014173 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014174 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014175 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014176 }
14177
14178 /* 5700 {AX,BX} chips have a broken status block link
14179 * change bit implementation, so we must use the
14180 * status register in those cases.
14181 */
14182 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
14183 tp->tg3_flags |= TG3_FLAG_USE_LINKCHG_REG;
14184 else
14185 tp->tg3_flags &= ~TG3_FLAG_USE_LINKCHG_REG;
14186
14187 /* The led_ctrl is set during tg3_phy_probe, here we might
14188 * have to force the link status polling mechanism based
14189 * upon subsystem IDs.
14190 */
14191 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070014192 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014193 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
14194 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
14195 tp->tg3_flags |= TG3_FLAG_USE_LINKCHG_REG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014196 }
14197
14198 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014199 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014200 tp->tg3_flags |= TG3_FLAG_POLL_SERDES;
14201 else
14202 tp->tg3_flags &= ~TG3_FLAG_POLL_SERDES;
14203
Matt Carlsonbf933c82011-01-25 15:58:49 +000014204 tp->rx_offset = NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014205 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014206 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsond2757fc2010-04-12 06:58:27 +000014207 (tp->tg3_flags & TG3_FLAG_PCIX_MODE) != 0) {
Matt Carlsonbf933c82011-01-25 15:58:49 +000014208 tp->rx_offset = 0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014209#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000014210 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014211#endif
14212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014213
Matt Carlson2c49a442010-09-30 10:34:35 +000014214 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
14215 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000014216 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
14217
Matt Carlson2c49a442010-09-30 10:34:35 +000014218 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070014219
14220 /* Increment the rx prod index on the rx std ring by at most
14221 * 8 for these chips to workaround hw errata.
14222 */
14223 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
14224 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
14225 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
14226 tp->rx_std_max_post = 8;
14227
Matt Carlson8ed5d972007-05-07 00:25:49 -070014228 if (tp->tg3_flags & TG3_FLAG_ASPM_WORKAROUND)
14229 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
14230 PCIE_PWR_MGMT_L1_THRESH_MSK;
14231
Linus Torvalds1da177e2005-04-16 15:20:36 -070014232 return err;
14233}
14234
David S. Miller49b6e95f2007-03-29 01:38:42 -070014235#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014236static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
14237{
14238 struct net_device *dev = tp->dev;
14239 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014240 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070014241 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014242 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014243
David S. Miller49b6e95f2007-03-29 01:38:42 -070014244 addr = of_get_property(dp, "local-mac-address", &len);
14245 if (addr && len == 6) {
14246 memcpy(dev->dev_addr, addr, 6);
14247 memcpy(dev->perm_addr, dev->dev_addr, 6);
14248 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014249 }
14250 return -ENODEV;
14251}
14252
14253static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
14254{
14255 struct net_device *dev = tp->dev;
14256
14257 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070014258 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014259 return 0;
14260}
14261#endif
14262
14263static int __devinit tg3_get_device_address(struct tg3 *tp)
14264{
14265 struct net_device *dev = tp->dev;
14266 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080014267 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014268
David S. Miller49b6e95f2007-03-29 01:38:42 -070014269#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014270 if (!tg3_get_macaddr_sparc(tp))
14271 return 0;
14272#endif
14273
14274 mac_offset = 0x7c;
David S. Millerf49639e2006-06-09 11:58:36 -070014275 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) ||
Michael Chana4e2b342005-10-26 15:46:52 -070014276 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014277 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
14278 mac_offset = 0xcc;
14279 if (tg3_nvram_lock(tp))
14280 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
14281 else
14282 tg3_nvram_unlock(tp);
Matt Carlson0a58d662011-04-05 14:22:45 +000014283 } else if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
Matt Carlsona50d0792010-06-05 17:24:37 +000014284 if (PCI_FUNC(tp->pdev->devfn) & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014285 mac_offset = 0xcc;
Matt Carlsona50d0792010-06-05 17:24:37 +000014286 if (PCI_FUNC(tp->pdev->devfn) > 1)
14287 mac_offset += 0x18c;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014288 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070014289 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014290
14291 /* First try to get it from MAC address mailbox. */
14292 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
14293 if ((hi >> 16) == 0x484b) {
14294 dev->dev_addr[0] = (hi >> 8) & 0xff;
14295 dev->dev_addr[1] = (hi >> 0) & 0xff;
14296
14297 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
14298 dev->dev_addr[2] = (lo >> 24) & 0xff;
14299 dev->dev_addr[3] = (lo >> 16) & 0xff;
14300 dev->dev_addr[4] = (lo >> 8) & 0xff;
14301 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014302
Michael Chan008652b2006-03-27 23:14:53 -080014303 /* Some old bootcode may report a 0 MAC address in SRAM */
14304 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
14305 }
14306 if (!addr_ok) {
14307 /* Next, try NVRAM. */
Matt Carlsondf259d82009-04-20 06:57:14 +000014308 if (!(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) &&
14309 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000014310 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070014311 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
14312 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080014313 }
14314 /* Finally just fetch it out of the MAC control regs. */
14315 else {
14316 hi = tr32(MAC_ADDR_0_HIGH);
14317 lo = tr32(MAC_ADDR_0_LOW);
14318
14319 dev->dev_addr[5] = lo & 0xff;
14320 dev->dev_addr[4] = (lo >> 8) & 0xff;
14321 dev->dev_addr[3] = (lo >> 16) & 0xff;
14322 dev->dev_addr[2] = (lo >> 24) & 0xff;
14323 dev->dev_addr[1] = hi & 0xff;
14324 dev->dev_addr[0] = (hi >> 8) & 0xff;
14325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014326 }
14327
14328 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070014329#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014330 if (!tg3_get_default_macaddr_sparc(tp))
14331 return 0;
14332#endif
14333 return -EINVAL;
14334 }
John W. Linville2ff43692005-09-12 14:44:20 -070014335 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014336 return 0;
14337}
14338
David S. Miller59e6b432005-05-18 22:50:10 -070014339#define BOUNDARY_SINGLE_CACHELINE 1
14340#define BOUNDARY_MULTI_CACHELINE 2
14341
14342static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
14343{
14344 int cacheline_size;
14345 u8 byte;
14346 int goal;
14347
14348 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
14349 if (byte == 0)
14350 cacheline_size = 1024;
14351 else
14352 cacheline_size = (int) byte * 4;
14353
14354 /* On 5703 and later chips, the boundary bits have no
14355 * effect.
14356 */
14357 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14358 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
14359 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
14360 goto out;
14361
14362#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
14363 goal = BOUNDARY_MULTI_CACHELINE;
14364#else
14365#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
14366 goal = BOUNDARY_SINGLE_CACHELINE;
14367#else
14368 goal = 0;
14369#endif
14370#endif
14371
Matt Carlson1407deb2011-04-05 14:22:44 +000014372 if (tp->tg3_flags3 & TG3_FLG3_57765_PLUS) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014373 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
14374 goto out;
14375 }
14376
David S. Miller59e6b432005-05-18 22:50:10 -070014377 if (!goal)
14378 goto out;
14379
14380 /* PCI controllers on most RISC systems tend to disconnect
14381 * when a device tries to burst across a cache-line boundary.
14382 * Therefore, letting tg3 do so just wastes PCI bandwidth.
14383 *
14384 * Unfortunately, for PCI-E there are only limited
14385 * write-side controls for this, and thus for reads
14386 * we will still get the disconnects. We'll also waste
14387 * these PCI cycles for both read and write for chips
14388 * other than 5700 and 5701 which do not implement the
14389 * boundary bits.
14390 */
14391 if ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) &&
14392 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)) {
14393 switch (cacheline_size) {
14394 case 16:
14395 case 32:
14396 case 64:
14397 case 128:
14398 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14399 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
14400 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
14401 } else {
14402 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14403 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14404 }
14405 break;
14406
14407 case 256:
14408 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
14409 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
14410 break;
14411
14412 default:
14413 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14414 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14415 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014416 }
David S. Miller59e6b432005-05-18 22:50:10 -070014417 } else if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
14418 switch (cacheline_size) {
14419 case 16:
14420 case 32:
14421 case 64:
14422 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14423 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14424 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
14425 break;
14426 }
14427 /* fallthrough */
14428 case 128:
14429 default:
14430 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14431 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
14432 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014433 }
David S. Miller59e6b432005-05-18 22:50:10 -070014434 } else {
14435 switch (cacheline_size) {
14436 case 16:
14437 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14438 val |= (DMA_RWCTRL_READ_BNDRY_16 |
14439 DMA_RWCTRL_WRITE_BNDRY_16);
14440 break;
14441 }
14442 /* fallthrough */
14443 case 32:
14444 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14445 val |= (DMA_RWCTRL_READ_BNDRY_32 |
14446 DMA_RWCTRL_WRITE_BNDRY_32);
14447 break;
14448 }
14449 /* fallthrough */
14450 case 64:
14451 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14452 val |= (DMA_RWCTRL_READ_BNDRY_64 |
14453 DMA_RWCTRL_WRITE_BNDRY_64);
14454 break;
14455 }
14456 /* fallthrough */
14457 case 128:
14458 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14459 val |= (DMA_RWCTRL_READ_BNDRY_128 |
14460 DMA_RWCTRL_WRITE_BNDRY_128);
14461 break;
14462 }
14463 /* fallthrough */
14464 case 256:
14465 val |= (DMA_RWCTRL_READ_BNDRY_256 |
14466 DMA_RWCTRL_WRITE_BNDRY_256);
14467 break;
14468 case 512:
14469 val |= (DMA_RWCTRL_READ_BNDRY_512 |
14470 DMA_RWCTRL_WRITE_BNDRY_512);
14471 break;
14472 case 1024:
14473 default:
14474 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
14475 DMA_RWCTRL_WRITE_BNDRY_1024);
14476 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014477 }
David S. Miller59e6b432005-05-18 22:50:10 -070014478 }
14479
14480out:
14481 return val;
14482}
14483
Linus Torvalds1da177e2005-04-16 15:20:36 -070014484static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
14485{
14486 struct tg3_internal_buffer_desc test_desc;
14487 u32 sram_dma_descs;
14488 int i, ret;
14489
14490 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
14491
14492 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
14493 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
14494 tw32(RDMAC_STATUS, 0);
14495 tw32(WDMAC_STATUS, 0);
14496
14497 tw32(BUFMGR_MODE, 0);
14498 tw32(FTQ_RESET, 0);
14499
14500 test_desc.addr_hi = ((u64) buf_dma) >> 32;
14501 test_desc.addr_lo = buf_dma & 0xffffffff;
14502 test_desc.nic_mbuf = 0x00002100;
14503 test_desc.len = size;
14504
14505 /*
14506 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
14507 * the *second* time the tg3 driver was getting loaded after an
14508 * initial scan.
14509 *
14510 * Broadcom tells me:
14511 * ...the DMA engine is connected to the GRC block and a DMA
14512 * reset may affect the GRC block in some unpredictable way...
14513 * The behavior of resets to individual blocks has not been tested.
14514 *
14515 * Broadcom noted the GRC reset will also reset all sub-components.
14516 */
14517 if (to_device) {
14518 test_desc.cqid_sqid = (13 << 8) | 2;
14519
14520 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
14521 udelay(40);
14522 } else {
14523 test_desc.cqid_sqid = (16 << 8) | 7;
14524
14525 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
14526 udelay(40);
14527 }
14528 test_desc.flags = 0x00000005;
14529
14530 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
14531 u32 val;
14532
14533 val = *(((u32 *)&test_desc) + i);
14534 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
14535 sram_dma_descs + (i * sizeof(u32)));
14536 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
14537 }
14538 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
14539
Matt Carlson859a588792010-04-05 10:19:28 +000014540 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014541 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a588792010-04-05 10:19:28 +000014542 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070014543 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014544
14545 ret = -ENODEV;
14546 for (i = 0; i < 40; i++) {
14547 u32 val;
14548
14549 if (to_device)
14550 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
14551 else
14552 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
14553 if ((val & 0xffff) == sram_dma_descs) {
14554 ret = 0;
14555 break;
14556 }
14557
14558 udelay(100);
14559 }
14560
14561 return ret;
14562}
14563
David S. Millerded73402005-05-23 13:59:47 -070014564#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070014565
Matt Carlson41434702011-03-09 16:58:22 +000014566static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080014567 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
14568 { },
14569};
14570
Linus Torvalds1da177e2005-04-16 15:20:36 -070014571static int __devinit tg3_test_dma(struct tg3 *tp)
14572{
14573 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070014574 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014575 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014576
Matt Carlson4bae65c2010-11-24 08:31:52 +000014577 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
14578 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014579 if (!buf) {
14580 ret = -ENOMEM;
14581 goto out_nofree;
14582 }
14583
14584 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
14585 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
14586
David S. Miller59e6b432005-05-18 22:50:10 -070014587 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014588
Matt Carlson1407deb2011-04-05 14:22:44 +000014589 if (tp->tg3_flags3 & TG3_FLG3_57765_PLUS)
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014590 goto out;
14591
Linus Torvalds1da177e2005-04-16 15:20:36 -070014592 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
14593 /* DMA read watermark not used on PCIE */
14594 tp->dma_rwctrl |= 0x00180000;
14595 } else if (!(tp->tg3_flags & TG3_FLAG_PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070014596 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
14597 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014598 tp->dma_rwctrl |= 0x003f0000;
14599 else
14600 tp->dma_rwctrl |= 0x003f000f;
14601 } else {
14602 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14603 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
14604 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080014605 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014606
Michael Chan4a29cc22006-03-19 13:21:12 -080014607 /* If the 5704 is behind the EPB bridge, we can
14608 * do the less restrictive ONE_DMA workaround for
14609 * better performance.
14610 */
14611 if ((tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG) &&
14612 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14613 tp->dma_rwctrl |= 0x8000;
14614 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014615 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
14616
Michael Chan49afdeb2007-02-13 12:17:03 -080014617 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
14618 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070014619 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080014620 tp->dma_rwctrl |=
14621 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
14622 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
14623 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070014624 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
14625 /* 5780 always in PCIX mode */
14626 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070014627 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
14628 /* 5714 always in PCIX mode */
14629 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014630 } else {
14631 tp->dma_rwctrl |= 0x001b000f;
14632 }
14633 }
14634
14635 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14636 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14637 tp->dma_rwctrl &= 0xfffffff0;
14638
14639 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14640 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
14641 /* Remove this if it causes problems for some boards. */
14642 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
14643
14644 /* On 5700/5701 chips, we need to set this bit.
14645 * Otherwise the chip will issue cacheline transactions
14646 * to streamable DMA memory with not all the byte
14647 * enables turned on. This is an error on several
14648 * RISC PCI controllers, in particular sparc64.
14649 *
14650 * On 5703/5704 chips, this bit has been reassigned
14651 * a different meaning. In particular, it is used
14652 * on those chips to enable a PCI-X workaround.
14653 */
14654 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
14655 }
14656
14657 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14658
14659#if 0
14660 /* Unneeded, already done by tg3_get_invariants. */
14661 tg3_switch_clocks(tp);
14662#endif
14663
Linus Torvalds1da177e2005-04-16 15:20:36 -070014664 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14665 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
14666 goto out;
14667
David S. Miller59e6b432005-05-18 22:50:10 -070014668 /* It is best to perform DMA test with maximum write burst size
14669 * to expose the 5700/5701 write DMA bug.
14670 */
14671 saved_dma_rwctrl = tp->dma_rwctrl;
14672 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14673 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14674
Linus Torvalds1da177e2005-04-16 15:20:36 -070014675 while (1) {
14676 u32 *p = buf, i;
14677
14678 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
14679 p[i] = i;
14680
14681 /* Send the buffer to the chip. */
14682 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
14683 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000014684 dev_err(&tp->pdev->dev,
14685 "%s: Buffer write failed. err = %d\n",
14686 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014687 break;
14688 }
14689
14690#if 0
14691 /* validate data reached card RAM correctly. */
14692 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14693 u32 val;
14694 tg3_read_mem(tp, 0x2100 + (i*4), &val);
14695 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000014696 dev_err(&tp->pdev->dev,
14697 "%s: Buffer corrupted on device! "
14698 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014699 /* ret = -ENODEV here? */
14700 }
14701 p[i] = 0;
14702 }
14703#endif
14704 /* Now read it back. */
14705 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
14706 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000014707 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
14708 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014709 break;
14710 }
14711
14712 /* Verify it. */
14713 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14714 if (p[i] == i)
14715 continue;
14716
David S. Miller59e6b432005-05-18 22:50:10 -070014717 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14718 DMA_RWCTRL_WRITE_BNDRY_16) {
14719 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014720 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
14721 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14722 break;
14723 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000014724 dev_err(&tp->pdev->dev,
14725 "%s: Buffer corrupted on read back! "
14726 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014727 ret = -ENODEV;
14728 goto out;
14729 }
14730 }
14731
14732 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
14733 /* Success. */
14734 ret = 0;
14735 break;
14736 }
14737 }
David S. Miller59e6b432005-05-18 22:50:10 -070014738 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14739 DMA_RWCTRL_WRITE_BNDRY_16) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014740
David S. Miller59e6b432005-05-18 22:50:10 -070014741 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070014742 * now look for chipsets that are known to expose the
14743 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070014744 */
Matt Carlson41434702011-03-09 16:58:22 +000014745 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014746 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14747 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a588792010-04-05 10:19:28 +000014748 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014749 /* Safe to use the calculated DMA boundary. */
14750 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a588792010-04-05 10:19:28 +000014751 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070014752
David S. Miller59e6b432005-05-18 22:50:10 -070014753 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014755
14756out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000014757 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014758out_nofree:
14759 return ret;
14760}
14761
Linus Torvalds1da177e2005-04-16 15:20:36 -070014762static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
14763{
Matt Carlson1407deb2011-04-05 14:22:44 +000014764 if (tp->tg3_flags3 & TG3_FLG3_57765_PLUS) {
Matt Carlson666bc832010-01-20 16:58:03 +000014765 tp->bufmgr_config.mbuf_read_dma_low_water =
14766 DEFAULT_MB_RDMA_LOW_WATER_5705;
14767 tp->bufmgr_config.mbuf_mac_rx_low_water =
14768 DEFAULT_MB_MACRX_LOW_WATER_57765;
14769 tp->bufmgr_config.mbuf_high_water =
14770 DEFAULT_MB_HIGH_WATER_57765;
14771
14772 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14773 DEFAULT_MB_RDMA_LOW_WATER_5705;
14774 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14775 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
14776 tp->bufmgr_config.mbuf_high_water_jumbo =
14777 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
14778 } else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
Michael Chanfdfec1722005-07-25 12:31:48 -070014779 tp->bufmgr_config.mbuf_read_dma_low_water =
14780 DEFAULT_MB_RDMA_LOW_WATER_5705;
14781 tp->bufmgr_config.mbuf_mac_rx_low_water =
14782 DEFAULT_MB_MACRX_LOW_WATER_5705;
14783 tp->bufmgr_config.mbuf_high_water =
14784 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070014785 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
14786 tp->bufmgr_config.mbuf_mac_rx_low_water =
14787 DEFAULT_MB_MACRX_LOW_WATER_5906;
14788 tp->bufmgr_config.mbuf_high_water =
14789 DEFAULT_MB_HIGH_WATER_5906;
14790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014791
Michael Chanfdfec1722005-07-25 12:31:48 -070014792 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14793 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
14794 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14795 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
14796 tp->bufmgr_config.mbuf_high_water_jumbo =
14797 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
14798 } else {
14799 tp->bufmgr_config.mbuf_read_dma_low_water =
14800 DEFAULT_MB_RDMA_LOW_WATER;
14801 tp->bufmgr_config.mbuf_mac_rx_low_water =
14802 DEFAULT_MB_MACRX_LOW_WATER;
14803 tp->bufmgr_config.mbuf_high_water =
14804 DEFAULT_MB_HIGH_WATER;
14805
14806 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14807 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
14808 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14809 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
14810 tp->bufmgr_config.mbuf_high_water_jumbo =
14811 DEFAULT_MB_HIGH_WATER_JUMBO;
14812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014813
14814 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
14815 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
14816}
14817
14818static char * __devinit tg3_phy_string(struct tg3 *tp)
14819{
Matt Carlson79eb6902010-02-17 15:17:03 +000014820 switch (tp->phy_id & TG3_PHY_ID_MASK) {
14821 case TG3_PHY_ID_BCM5400: return "5400";
14822 case TG3_PHY_ID_BCM5401: return "5401";
14823 case TG3_PHY_ID_BCM5411: return "5411";
14824 case TG3_PHY_ID_BCM5701: return "5701";
14825 case TG3_PHY_ID_BCM5703: return "5703";
14826 case TG3_PHY_ID_BCM5704: return "5704";
14827 case TG3_PHY_ID_BCM5705: return "5705";
14828 case TG3_PHY_ID_BCM5750: return "5750";
14829 case TG3_PHY_ID_BCM5752: return "5752";
14830 case TG3_PHY_ID_BCM5714: return "5714";
14831 case TG3_PHY_ID_BCM5780: return "5780";
14832 case TG3_PHY_ID_BCM5755: return "5755";
14833 case TG3_PHY_ID_BCM5787: return "5787";
14834 case TG3_PHY_ID_BCM5784: return "5784";
14835 case TG3_PHY_ID_BCM5756: return "5722/5756";
14836 case TG3_PHY_ID_BCM5906: return "5906";
14837 case TG3_PHY_ID_BCM5761: return "5761";
14838 case TG3_PHY_ID_BCM5718C: return "5718C";
14839 case TG3_PHY_ID_BCM5718S: return "5718S";
14840 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000014841 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000014842 case TG3_PHY_ID_BCM5720C: return "5720C";
Matt Carlson79eb6902010-02-17 15:17:03 +000014843 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070014844 case 0: return "serdes";
14845 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070014846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014847}
14848
Michael Chanf9804dd2005-09-27 12:13:10 -070014849static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
14850{
14851 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
14852 strcpy(str, "PCI Express");
14853 return str;
14854 } else if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
14855 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
14856
14857 strcpy(str, "PCIX:");
14858
14859 if ((clock_ctrl == 7) ||
14860 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
14861 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
14862 strcat(str, "133MHz");
14863 else if (clock_ctrl == 0)
14864 strcat(str, "33MHz");
14865 else if (clock_ctrl == 2)
14866 strcat(str, "50MHz");
14867 else if (clock_ctrl == 4)
14868 strcat(str, "66MHz");
14869 else if (clock_ctrl == 6)
14870 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070014871 } else {
14872 strcpy(str, "PCI:");
14873 if (tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED)
14874 strcat(str, "66MHz");
14875 else
14876 strcat(str, "33MHz");
14877 }
14878 if (tp->tg3_flags & TG3_FLAG_PCI_32BIT)
14879 strcat(str, ":32-bit");
14880 else
14881 strcat(str, ":64-bit");
14882 return str;
14883}
14884
Michael Chan8c2dc7e2005-12-19 16:26:02 -080014885static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014886{
14887 struct pci_dev *peer;
14888 unsigned int func, devnr = tp->pdev->devfn & ~7;
14889
14890 for (func = 0; func < 8; func++) {
14891 peer = pci_get_slot(tp->pdev->bus, devnr | func);
14892 if (peer && peer != tp->pdev)
14893 break;
14894 pci_dev_put(peer);
14895 }
Michael Chan16fe9d72005-12-13 21:09:54 -080014896 /* 5704 can be configured in single-port mode, set peer to
14897 * tp->pdev in that case.
14898 */
14899 if (!peer) {
14900 peer = tp->pdev;
14901 return peer;
14902 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014903
14904 /*
14905 * We don't need to keep the refcount elevated; there's no way
14906 * to remove one half of this device without removing the other
14907 */
14908 pci_dev_put(peer);
14909
14910 return peer;
14911}
14912
David S. Miller15f98502005-05-18 22:49:26 -070014913static void __devinit tg3_init_coal(struct tg3 *tp)
14914{
14915 struct ethtool_coalesce *ec = &tp->coal;
14916
14917 memset(ec, 0, sizeof(*ec));
14918 ec->cmd = ETHTOOL_GCOALESCE;
14919 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
14920 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
14921 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
14922 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
14923 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
14924 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
14925 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
14926 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
14927 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
14928
14929 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
14930 HOSTCC_MODE_CLRTICK_TXBD)) {
14931 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
14932 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
14933 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
14934 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
14935 }
Michael Chand244c892005-07-05 14:42:33 -070014936
14937 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
14938 ec->rx_coalesce_usecs_irq = 0;
14939 ec->tx_coalesce_usecs_irq = 0;
14940 ec->stats_block_coalesce_usecs = 0;
14941 }
David S. Miller15f98502005-05-18 22:49:26 -070014942}
14943
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080014944static const struct net_device_ops tg3_netdev_ops = {
14945 .ndo_open = tg3_open,
14946 .ndo_stop = tg3_close,
Stephen Hemminger00829822008-11-20 20:14:53 -080014947 .ndo_start_xmit = tg3_start_xmit,
Eric Dumazet511d2222010-07-07 20:44:24 +000014948 .ndo_get_stats64 = tg3_get_stats64,
Stephen Hemminger00829822008-11-20 20:14:53 -080014949 .ndo_validate_addr = eth_validate_addr,
14950 .ndo_set_multicast_list = tg3_set_rx_mode,
14951 .ndo_set_mac_address = tg3_set_mac_addr,
14952 .ndo_do_ioctl = tg3_ioctl,
14953 .ndo_tx_timeout = tg3_tx_timeout,
14954 .ndo_change_mtu = tg3_change_mtu,
Michał Mirosławdc668912011-04-07 03:35:07 +000014955 .ndo_fix_features = tg3_fix_features,
Stephen Hemminger00829822008-11-20 20:14:53 -080014956#ifdef CONFIG_NET_POLL_CONTROLLER
14957 .ndo_poll_controller = tg3_poll_controller,
14958#endif
14959};
14960
14961static const struct net_device_ops tg3_netdev_ops_dma_bug = {
14962 .ndo_open = tg3_open,
14963 .ndo_stop = tg3_close,
14964 .ndo_start_xmit = tg3_start_xmit_dma_bug,
Eric Dumazet511d2222010-07-07 20:44:24 +000014965 .ndo_get_stats64 = tg3_get_stats64,
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080014966 .ndo_validate_addr = eth_validate_addr,
14967 .ndo_set_multicast_list = tg3_set_rx_mode,
14968 .ndo_set_mac_address = tg3_set_mac_addr,
14969 .ndo_do_ioctl = tg3_ioctl,
14970 .ndo_tx_timeout = tg3_tx_timeout,
14971 .ndo_change_mtu = tg3_change_mtu,
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080014972#ifdef CONFIG_NET_POLL_CONTROLLER
14973 .ndo_poll_controller = tg3_poll_controller,
14974#endif
14975};
14976
Linus Torvalds1da177e2005-04-16 15:20:36 -070014977static int __devinit tg3_init_one(struct pci_dev *pdev,
14978 const struct pci_device_id *ent)
14979{
Linus Torvalds1da177e2005-04-16 15:20:36 -070014980 struct net_device *dev;
14981 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000014982 int i, err, pm_cap;
14983 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070014984 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080014985 u64 dma_mask, persist_dma_mask;
Michał Mirosławdc668912011-04-07 03:35:07 +000014986 u32 hw_features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014987
Joe Perches05dbe002010-02-17 19:44:19 +000014988 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014989
14990 err = pci_enable_device(pdev);
14991 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014992 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014993 return err;
14994 }
14995
Linus Torvalds1da177e2005-04-16 15:20:36 -070014996 err = pci_request_regions(pdev, DRV_MODULE_NAME);
14997 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014998 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014999 goto err_out_disable_pdev;
15000 }
15001
15002 pci_set_master(pdev);
15003
15004 /* Find power-management capability. */
15005 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
15006 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000015007 dev_err(&pdev->dev,
15008 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015009 err = -EIO;
15010 goto err_out_free_res;
15011 }
15012
Matt Carlsonfe5f5782009-09-01 13:09:39 +000015013 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015014 if (!dev) {
Matt Carlson2445e462010-04-05 10:19:21 +000015015 dev_err(&pdev->dev, "Etherdev alloc failed, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015016 err = -ENOMEM;
15017 goto err_out_free_res;
15018 }
15019
Linus Torvalds1da177e2005-04-16 15:20:36 -070015020 SET_NETDEV_DEV(dev, &pdev->dev);
15021
Linus Torvalds1da177e2005-04-16 15:20:36 -070015022 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015023
15024 tp = netdev_priv(dev);
15025 tp->pdev = pdev;
15026 tp->dev = dev;
15027 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015028 tp->rx_mode = TG3_DEF_RX_MODE;
15029 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070015030
Linus Torvalds1da177e2005-04-16 15:20:36 -070015031 if (tg3_debug > 0)
15032 tp->msg_enable = tg3_debug;
15033 else
15034 tp->msg_enable = TG3_DEF_MSG_ENABLE;
15035
15036 /* The word/byte swap controls here control register access byte
15037 * swapping. DMA data byte swapping is controlled in the GRC_MODE
15038 * setting below.
15039 */
15040 tp->misc_host_ctrl =
15041 MISC_HOST_CTRL_MASK_PCI_INT |
15042 MISC_HOST_CTRL_WORD_SWAP |
15043 MISC_HOST_CTRL_INDIR_ACCESS |
15044 MISC_HOST_CTRL_PCISTATE_RW;
15045
15046 /* The NONFRM (non-frame) byte/word swap controls take effect
15047 * on descriptor entries, anything which isn't packet data.
15048 *
15049 * The StrongARM chips on the board (one for tx, one for rx)
15050 * are running in big-endian mode.
15051 */
15052 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
15053 GRC_MODE_WSWAP_NONFRM_DATA);
15054#ifdef __BIG_ENDIAN
15055 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
15056#endif
15057 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015058 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000015059 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015060
Matt Carlsond5fe4882008-11-21 17:20:32 -080015061 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010015062 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015063 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015064 err = -ENOMEM;
15065 goto err_out_free_dev;
15066 }
15067
Linus Torvalds1da177e2005-04-16 15:20:36 -070015068 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
15069 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015070
Linus Torvalds1da177e2005-04-16 15:20:36 -070015071 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015072 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015073 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015074
15075 err = tg3_get_invariants(tp);
15076 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015077 dev_err(&pdev->dev,
15078 "Problem fetching invariants of chip, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015079 goto err_out_iounmap;
15080 }
15081
Matt Carlson615774f2009-11-13 13:03:39 +000015082 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) &&
Matt Carlson0a58d662011-04-05 14:22:45 +000015083 !(tp->tg3_flags3 & TG3_FLG3_5717_PLUS))
Stephen Hemminger00829822008-11-20 20:14:53 -080015084 dev->netdev_ops = &tg3_netdev_ops;
15085 else
15086 dev->netdev_ops = &tg3_netdev_ops_dma_bug;
15087
15088
Michael Chan4a29cc22006-03-19 13:21:12 -080015089 /* The EPB bridge inside 5714, 5715, and 5780 and any
15090 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080015091 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
15092 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
15093 * do DMA address check in tg3_start_xmit().
15094 */
Michael Chan4a29cc22006-03-19 13:21:12 -080015095 if (tp->tg3_flags2 & TG3_FLG2_IS_5788)
Yang Hongyang284901a2009-04-06 19:01:15 -070015096 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Michael Chan4a29cc22006-03-19 13:21:12 -080015097 else if (tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070015098 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080015099#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070015100 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015101#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080015102 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070015103 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015104
15105 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070015106 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080015107 err = pci_set_dma_mask(pdev, dma_mask);
15108 if (!err) {
15109 dev->features |= NETIF_F_HIGHDMA;
15110 err = pci_set_consistent_dma_mask(pdev,
15111 persist_dma_mask);
15112 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015113 dev_err(&pdev->dev, "Unable to obtain 64 bit "
15114 "DMA for consistent allocations\n");
Michael Chan72f2afb2006-03-06 19:28:35 -080015115 goto err_out_iounmap;
15116 }
15117 }
15118 }
Yang Hongyang284901a2009-04-06 19:01:15 -070015119 if (err || dma_mask == DMA_BIT_MASK(32)) {
15120 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080015121 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015122 dev_err(&pdev->dev,
15123 "No usable DMA configuration, aborting\n");
Michael Chan72f2afb2006-03-06 19:28:35 -080015124 goto err_out_iounmap;
15125 }
15126 }
15127
Michael Chanfdfec1722005-07-25 12:31:48 -070015128 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015129
Matt Carlson507399f2009-11-13 13:03:37 +000015130 /* Selectively allow TSO based on operating conditions */
15131 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO) ||
15132 (tp->fw_needed && !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)))
15133 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
15134 else {
15135 tp->tg3_flags2 &= ~(TG3_FLG2_TSO_CAPABLE | TG3_FLG2_TSO_BUG);
15136 tp->fw_needed = NULL;
15137 }
15138
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080015139 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
Matt Carlson9e9fd122009-01-19 16:57:45 -080015140 tp->fw_needed = FIRMWARE_TG3;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080015141
Michael Chan4e3a7aa2006-03-20 17:47:44 -080015142 /* TSO is on by default on chips that support hardware TSO.
15143 * Firmware TSO on older chips gives lower performance, so it
15144 * is off by default, but can be enabled using ethtool.
15145 */
Matt Carlsone849cdc2009-11-13 13:03:38 +000015146 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO) &&
Michał Mirosławdc668912011-04-07 03:35:07 +000015147 (dev->features & NETIF_F_IP_CSUM))
15148 hw_features |= NETIF_F_TSO;
Matt Carlsone849cdc2009-11-13 13:03:38 +000015149 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_2) ||
15150 (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3)) {
Michał Mirosławdc668912011-04-07 03:35:07 +000015151 if (dev->features & NETIF_F_IPV6_CSUM)
15152 hw_features |= NETIF_F_TSO6;
Matt Carlsone849cdc2009-11-13 13:03:38 +000015153 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
15154 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070015155 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
15156 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080015157 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Michał Mirosławdc668912011-04-07 03:35:07 +000015158 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
15159 hw_features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070015160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015161
Michał Mirosławdc668912011-04-07 03:35:07 +000015162 dev->hw_features |= hw_features;
15163 dev->features |= hw_features;
15164 dev->vlan_features |= hw_features;
15165
Linus Torvalds1da177e2005-04-16 15:20:36 -070015166 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
15167 !(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) &&
15168 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
15169 tp->tg3_flags2 |= TG3_FLG2_MAX_RXPEND_64;
15170 tp->rx_pending = 63;
15171 }
15172
Linus Torvalds1da177e2005-04-16 15:20:36 -070015173 err = tg3_get_device_address(tp);
15174 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015175 dev_err(&pdev->dev,
15176 "Could not obtain valid ethernet address, aborting\n");
Matt Carlson026a6c22009-12-03 08:36:24 +000015177 goto err_out_iounmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015178 }
15179
Matt Carlson0d3031d2007-10-10 18:02:43 -070015180 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
Matt Carlson63532392008-11-03 16:49:57 -080015181 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
Al Viro79ea13c2008-01-24 02:06:46 -080015182 if (!tp->aperegs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015183 dev_err(&pdev->dev,
15184 "Cannot map APE registers, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070015185 err = -ENOMEM;
Matt Carlson026a6c22009-12-03 08:36:24 +000015186 goto err_out_iounmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070015187 }
15188
15189 tg3_ape_lock_init(tp);
Matt Carlson7fd76442009-02-25 14:27:20 +000015190
15191 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF)
15192 tg3_read_dash_ver(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015193 }
15194
Matt Carlsonc88864d2007-11-12 21:07:01 -080015195 /*
15196 * Reset chip in case UNDI or EFI driver did not shutdown
15197 * DMA self test will enable WDMAC and we'll see (spurious)
15198 * pending DMA on the PCI bus at that point.
15199 */
15200 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
15201 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
15202 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
15203 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
15204 }
15205
15206 err = tg3_test_dma(tp);
15207 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015208 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080015209 goto err_out_apeunmap;
15210 }
15211
Matt Carlson78f90dc2009-11-13 13:03:42 +000015212 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
15213 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
15214 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000015215 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000015216 struct tg3_napi *tnapi = &tp->napi[i];
15217
15218 tnapi->tp = tp;
15219 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
15220
15221 tnapi->int_mbox = intmbx;
15222 if (i < 4)
15223 intmbx += 0x8;
15224 else
15225 intmbx += 0x4;
15226
15227 tnapi->consmbox = rcvmbx;
15228 tnapi->prodmbox = sndmbx;
15229
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015230 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000015231 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015232 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000015233 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000015234
15235 if (!(tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX))
15236 break;
15237
15238 /*
15239 * If we support MSIX, we'll be using RSS. If we're using
15240 * RSS, the first vector only handles link interrupts and the
15241 * remaining vectors handle rx and tx interrupts. Reuse the
15242 * mailbox values for the next iteration. The values we setup
15243 * above are still useful for the single vectored mode.
15244 */
15245 if (!i)
15246 continue;
15247
15248 rcvmbx += 0x8;
15249
15250 if (sndmbx & 0x4)
15251 sndmbx -= 0x4;
15252 else
15253 sndmbx += 0xc;
15254 }
15255
Matt Carlsonc88864d2007-11-12 21:07:01 -080015256 tg3_init_coal(tp);
15257
Michael Chanc49a1562006-12-17 17:07:29 -080015258 pci_set_drvdata(pdev, dev);
15259
Linus Torvalds1da177e2005-04-16 15:20:36 -070015260 err = register_netdev(dev);
15261 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015262 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070015263 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015264 }
15265
Joe Perches05dbe002010-02-17 19:44:19 +000015266 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
15267 tp->board_part_number,
15268 tp->pci_chip_rev_id,
15269 tg3_bus_string(tp, str),
15270 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015271
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015272 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000015273 struct phy_device *phydev;
15274 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000015275 netdev_info(dev,
15276 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000015277 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015278 } else {
15279 char *ethtype;
15280
15281 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
15282 ethtype = "10/100Base-TX";
15283 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
15284 ethtype = "1000Base-SX";
15285 else
15286 ethtype = "10/100/1000Base-T";
15287
Matt Carlson5129c3a2010-04-05 10:19:23 +000015288 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlson47007832011-04-20 07:57:43 +000015289 "(WireSpeed[%d], EEE[%d])\n",
15290 tg3_phy_string(tp), ethtype,
15291 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
15292 (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015293 }
Matt Carlsondf59c942008-11-03 16:52:56 -080015294
Joe Perches05dbe002010-02-17 19:44:19 +000015295 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000015296 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches05dbe002010-02-17 19:44:19 +000015297 (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015298 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches05dbe002010-02-17 19:44:19 +000015299 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0,
15300 (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) != 0);
15301 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
15302 tp->dma_rwctrl,
15303 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
15304 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015305
15306 return 0;
15307
Matt Carlson0d3031d2007-10-10 18:02:43 -070015308err_out_apeunmap:
15309 if (tp->aperegs) {
15310 iounmap(tp->aperegs);
15311 tp->aperegs = NULL;
15312 }
15313
Linus Torvalds1da177e2005-04-16 15:20:36 -070015314err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070015315 if (tp->regs) {
15316 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015317 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015319
15320err_out_free_dev:
15321 free_netdev(dev);
15322
15323err_out_free_res:
15324 pci_release_regions(pdev);
15325
15326err_out_disable_pdev:
15327 pci_disable_device(pdev);
15328 pci_set_drvdata(pdev, NULL);
15329 return err;
15330}
15331
15332static void __devexit tg3_remove_one(struct pci_dev *pdev)
15333{
15334 struct net_device *dev = pci_get_drvdata(pdev);
15335
15336 if (dev) {
15337 struct tg3 *tp = netdev_priv(dev);
15338
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080015339 if (tp->fw)
15340 release_firmware(tp->fw);
15341
Tejun Heo23f333a2010-12-12 16:45:14 +010015342 cancel_work_sync(&tp->reset_task);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015343
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015344 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
15345 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015346 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015347 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070015348
Linus Torvalds1da177e2005-04-16 15:20:36 -070015349 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015350 if (tp->aperegs) {
15351 iounmap(tp->aperegs);
15352 tp->aperegs = NULL;
15353 }
Michael Chan68929142005-08-09 20:17:14 -070015354 if (tp->regs) {
15355 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015356 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015358 free_netdev(dev);
15359 pci_release_regions(pdev);
15360 pci_disable_device(pdev);
15361 pci_set_drvdata(pdev, NULL);
15362 }
15363}
15364
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015365#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015366static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015367{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015368 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015369 struct net_device *dev = pci_get_drvdata(pdev);
15370 struct tg3 *tp = netdev_priv(dev);
15371 int err;
15372
15373 if (!netif_running(dev))
15374 return 0;
15375
Tejun Heo23f333a2010-12-12 16:45:14 +010015376 flush_work_sync(&tp->reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015377 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015378 tg3_netif_stop(tp);
15379
15380 del_timer_sync(&tp->timer);
15381
David S. Millerf47c11e2005-06-24 20:18:35 -070015382 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015383 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070015384 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015385
15386 netif_device_detach(dev);
15387
David S. Millerf47c11e2005-06-24 20:18:35 -070015388 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070015389 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan6a9eba12005-12-13 21:08:58 -080015390 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
David S. Millerf47c11e2005-06-24 20:18:35 -070015391 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015392
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015393 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015394 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015395 int err2;
15396
David S. Millerf47c11e2005-06-24 20:18:35 -070015397 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015398
Michael Chan6a9eba12005-12-13 21:08:58 -080015399 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015400 err2 = tg3_restart_hw(tp, 1);
15401 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070015402 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015403
15404 tp->timer.expires = jiffies + tp->timer_offset;
15405 add_timer(&tp->timer);
15406
15407 netif_device_attach(dev);
15408 tg3_netif_start(tp);
15409
Michael Chanb9ec6c12006-07-25 16:37:27 -070015410out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015411 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015412
15413 if (!err2)
15414 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015415 }
15416
15417 return err;
15418}
15419
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015420static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015421{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015422 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015423 struct net_device *dev = pci_get_drvdata(pdev);
15424 struct tg3 *tp = netdev_priv(dev);
15425 int err;
15426
15427 if (!netif_running(dev))
15428 return 0;
15429
Linus Torvalds1da177e2005-04-16 15:20:36 -070015430 netif_device_attach(dev);
15431
David S. Millerf47c11e2005-06-24 20:18:35 -070015432 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015433
Michael Chan6a9eba12005-12-13 21:08:58 -080015434 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Michael Chanb9ec6c12006-07-25 16:37:27 -070015435 err = tg3_restart_hw(tp, 1);
15436 if (err)
15437 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015438
15439 tp->timer.expires = jiffies + tp->timer_offset;
15440 add_timer(&tp->timer);
15441
Linus Torvalds1da177e2005-04-16 15:20:36 -070015442 tg3_netif_start(tp);
15443
Michael Chanb9ec6c12006-07-25 16:37:27 -070015444out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015445 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015446
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015447 if (!err)
15448 tg3_phy_start(tp);
15449
Michael Chanb9ec6c12006-07-25 16:37:27 -070015450 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015451}
15452
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015453static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015454#define TG3_PM_OPS (&tg3_pm_ops)
15455
15456#else
15457
15458#define TG3_PM_OPS NULL
15459
15460#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015461
Linus Torvalds1da177e2005-04-16 15:20:36 -070015462static struct pci_driver tg3_driver = {
15463 .name = DRV_MODULE_NAME,
15464 .id_table = tg3_pci_tbl,
15465 .probe = tg3_init_one,
15466 .remove = __devexit_p(tg3_remove_one),
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015467 .driver.pm = TG3_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070015468};
15469
15470static int __init tg3_init(void)
15471{
Jeff Garzik29917622006-08-19 17:48:59 -040015472 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015473}
15474
15475static void __exit tg3_cleanup(void)
15476{
15477 pci_unregister_driver(&tg3_driver);
15478}
15479
15480module_init(tg3_init);
15481module_exit(tg3_cleanup);