blob: 7be10cfb0a5f785d2e72f223ce9a23fe6461e7d0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * tg3.c: Broadcom Tigon3 ethernet driver.
3 *
4 * Copyright (C) 2001, 2002, 2003, 2004 David S. Miller (davem@redhat.com)
5 * Copyright (C) 2001, 2002, 2003 Jeff Garzik (jgarzik@pobox.com)
6 * Copyright (C) 2004 Sun Microsystems Inc.
Matt Carlsonb86fb2c2011-01-25 15:58:57 +00007 * Copyright (C) 2005-2011 Broadcom Corporation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * Firmware is:
Michael Chan49cabf42005-06-06 15:15:17 -070010 * Derived from proprietary unpublished source code,
11 * Copyright (C) 2000-2003 Broadcom Corporation.
12 *
13 * Permission is hereby granted for the distribution of this firmware
14 * data in hexadecimal or equivalent format, provided this copyright
15 * notice is accompanying it.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 */
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#include <linux/module.h>
20#include <linux/moduleparam.h>
Matt Carlson6867c842010-07-11 09:31:44 +000021#include <linux/stringify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/kernel.h>
23#include <linux/types.h>
24#include <linux/compiler.h>
25#include <linux/slab.h>
26#include <linux/delay.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020027#include <linux/in.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/init.h>
29#include <linux/ioport.h>
30#include <linux/pci.h>
31#include <linux/netdevice.h>
32#include <linux/etherdevice.h>
33#include <linux/skbuff.h>
34#include <linux/ethtool.h>
Matt Carlson3110f5f52010-12-06 08:28:50 +000035#include <linux/mdio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/mii.h>
Matt Carlson158d7ab2008-05-29 01:37:54 -070037#include <linux/phy.h>
Matt Carlsona9daf362008-05-25 23:49:44 -070038#include <linux/brcmphy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/if_vlan.h>
40#include <linux/ip.h>
41#include <linux/tcp.h>
42#include <linux/workqueue.h>
Michael Chan61487482005-09-05 17:53:19 -070043#include <linux/prefetch.h>
Tobias Klauserf9a5f7d2005-10-29 15:09:26 +020044#include <linux/dma-mapping.h>
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080045#include <linux/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#include <net/checksum.h>
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030048#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50#include <asm/system.h>
Javier Martinez Canillas27fd9de2011-03-26 16:42:31 +000051#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <asm/byteorder.h>
Javier Martinez Canillas27fd9de2011-03-26 16:42:31 +000053#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
David S. Miller49b6e95f2007-03-29 01:38:42 -070055#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <asm/idprom.h>
David S. Miller49b6e95f2007-03-29 01:38:42 -070057#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#endif
59
Matt Carlson63532392008-11-03 16:49:57 -080060#define BAR_0 0
61#define BAR_2 2
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include "tg3.h"
64
65#define DRV_MODULE_NAME "tg3"
Matt Carlson6867c842010-07-11 09:31:44 +000066#define TG3_MAJ_NUM 3
Matt Carlsonb86fb2c2011-01-25 15:58:57 +000067#define TG3_MIN_NUM 117
Matt Carlson6867c842010-07-11 09:31:44 +000068#define DRV_MODULE_VERSION \
69 __stringify(TG3_MAJ_NUM) "." __stringify(TG3_MIN_NUM)
Matt Carlsonb86fb2c2011-01-25 15:58:57 +000070#define DRV_MODULE_RELDATE "January 25, 2011"
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72#define TG3_DEF_MAC_MODE 0
73#define TG3_DEF_RX_MODE 0
74#define TG3_DEF_TX_MODE 0
75#define TG3_DEF_MSG_ENABLE \
76 (NETIF_MSG_DRV | \
77 NETIF_MSG_PROBE | \
78 NETIF_MSG_LINK | \
79 NETIF_MSG_TIMER | \
80 NETIF_MSG_IFDOWN | \
81 NETIF_MSG_IFUP | \
82 NETIF_MSG_RX_ERR | \
83 NETIF_MSG_TX_ERR)
84
85/* length of time before we decide the hardware is borked,
86 * and dev->tx_timeout() should be called to fix the problem
87 */
88#define TG3_TX_TIMEOUT (5 * HZ)
89
90/* hardware minimum and maximum for a single frame's data payload */
91#define TG3_MIN_MTU 60
92#define TG3_MAX_MTU(tp) \
Matt Carlson8f666b02009-08-28 13:58:24 +000093 ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) ? 9000 : 1500)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95/* These numbers seem to be hard coded in the NIC firmware somehow.
96 * You can't change the ring sizes, but you can change where you place
97 * them in the NIC onboard memory.
98 */
Matt Carlson7cb32cf2010-09-30 10:34:36 +000099#define TG3_RX_STD_RING_SIZE(tp) \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000100 ((tp->tg3_flags3 & TG3_FLG3_LRG_PROD_RING_CAP) ? \
101 TG3_RX_STD_MAX_SIZE_5717 : TG3_RX_STD_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102#define TG3_DEF_RX_RING_PENDING 200
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000103#define TG3_RX_JMB_RING_SIZE(tp) \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000104 ((tp->tg3_flags3 & TG3_FLG3_LRG_PROD_RING_CAP) ? \
105 TG3_RX_JMB_MAX_SIZE_5717 : TG3_RX_JMB_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106#define TG3_DEF_RX_JUMBO_RING_PENDING 100
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000107#define TG3_RSS_INDIR_TBL_SIZE 128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109/* Do not place this n-ring entries value into the tp struct itself,
110 * we really want to expose these constants to GCC so that modulo et
111 * al. operations are done with shifts and masks instead of with
112 * hw multiply/modulo instructions. Another solution would be to
113 * replace things like '% foo' with '& (foo - 1)'.
114 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116#define TG3_TX_RING_SIZE 512
117#define TG3_DEF_TX_RING_PENDING (TG3_TX_RING_SIZE - 1)
118
Matt Carlson2c49a442010-09-30 10:34:35 +0000119#define TG3_RX_STD_RING_BYTES(tp) \
120 (sizeof(struct tg3_rx_buffer_desc) * TG3_RX_STD_RING_SIZE(tp))
121#define TG3_RX_JMB_RING_BYTES(tp) \
122 (sizeof(struct tg3_ext_rx_buffer_desc) * TG3_RX_JMB_RING_SIZE(tp))
123#define TG3_RX_RCB_RING_BYTES(tp) \
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000124 (sizeof(struct tg3_rx_buffer_desc) * (tp->rx_ret_ring_mask + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125#define TG3_TX_RING_BYTES (sizeof(struct tg3_tx_buffer_desc) * \
126 TG3_TX_RING_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127#define NEXT_TX(N) (((N) + 1) & (TG3_TX_RING_SIZE - 1))
128
Matt Carlson287be122009-08-28 13:58:46 +0000129#define TG3_DMA_BYTE_ENAB 64
130
131#define TG3_RX_STD_DMA_SZ 1536
132#define TG3_RX_JMB_DMA_SZ 9046
133
134#define TG3_RX_DMA_TO_MAP_SZ(x) ((x) + TG3_DMA_BYTE_ENAB)
135
136#define TG3_RX_STD_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_STD_DMA_SZ)
137#define TG3_RX_JMB_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_JMB_DMA_SZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Matt Carlson2c49a442010-09-30 10:34:35 +0000139#define TG3_RX_STD_BUFF_RING_SIZE(tp) \
140 (sizeof(struct ring_info) * TG3_RX_STD_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000141
Matt Carlson2c49a442010-09-30 10:34:35 +0000142#define TG3_RX_JMB_BUFF_RING_SIZE(tp) \
143 (sizeof(struct ring_info) * TG3_RX_JMB_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000144
Matt Carlsond2757fc2010-04-12 06:58:27 +0000145/* Due to a hardware bug, the 5701 can only DMA to memory addresses
146 * that are at least dword aligned when used in PCIX mode. The driver
147 * works around this bug by double copying the packet. This workaround
148 * is built into the normal double copy length check for efficiency.
149 *
150 * However, the double copy is only necessary on those architectures
151 * where unaligned memory accesses are inefficient. For those architectures
152 * where unaligned memory accesses incur little penalty, we can reintegrate
153 * the 5701 in the normal rx path. Doing so saves a device structure
154 * dereference by hardcoding the double copy threshold in place.
155 */
156#define TG3_RX_COPY_THRESHOLD 256
157#if NET_IP_ALIGN == 0 || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
158 #define TG3_RX_COPY_THRESH(tp) TG3_RX_COPY_THRESHOLD
159#else
160 #define TG3_RX_COPY_THRESH(tp) ((tp)->rx_copy_thresh)
161#endif
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163/* minimum number of free TX descriptors required to wake up TX process */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000164#define TG3_TX_WAKEUP_THRESH(tnapi) ((tnapi)->tx_pending / 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Matt Carlsonad829262008-11-21 17:16:16 -0800166#define TG3_RAW_IP_ALIGN 2
167
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000168#define TG3_FW_UPDATE_TIMEOUT_SEC 5
169
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800170#define FIRMWARE_TG3 "tigon/tg3.bin"
171#define FIRMWARE_TG3TSO "tigon/tg3_tso.bin"
172#define FIRMWARE_TG3TSO5 "tigon/tg3_tso5.bin"
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174static char version[] __devinitdata =
Joe Perches05dbe002010-02-17 19:44:19 +0000175 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177MODULE_AUTHOR("David S. Miller (davem@redhat.com) and Jeff Garzik (jgarzik@pobox.com)");
178MODULE_DESCRIPTION("Broadcom Tigon3 ethernet driver");
179MODULE_LICENSE("GPL");
180MODULE_VERSION(DRV_MODULE_VERSION);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800181MODULE_FIRMWARE(FIRMWARE_TG3);
182MODULE_FIRMWARE(FIRMWARE_TG3TSO);
183MODULE_FIRMWARE(FIRMWARE_TG3TSO5);
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185static int tg3_debug = -1; /* -1 == use TG3_DEF_MSG_ENABLE as value */
186module_param(tg3_debug, int, 0);
187MODULE_PARM_DESC(tg3_debug, "Tigon3 bitmapped debugging message enable value");
188
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000189static DEFINE_PCI_DEVICE_TABLE(tg3_pci_tbl) = {
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700190 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5700)},
191 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5701)},
192 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702)},
193 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703)},
194 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704)},
195 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702FE)},
196 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705)},
197 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705_2)},
198 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M)},
199 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M_2)},
200 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702X)},
201 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703X)},
202 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S)},
203 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702A3)},
204 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703A3)},
205 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5782)},
206 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5788)},
207 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5789)},
208 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901)},
209 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901_2)},
210 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S_2)},
211 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705F)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700212 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5721)},
Michael Chan126a3362006-09-27 16:03:07 -0700213 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5722)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700214 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700215 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751M)},
216 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751F)},
217 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752)},
218 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752M)},
219 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753)},
220 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753M)},
221 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753F)},
222 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754)},
223 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754M)},
224 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755)},
225 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755M)},
Michael Chan126a3362006-09-27 16:03:07 -0700226 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5756)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700227 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5786)},
228 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787)},
229 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787M)},
Michael Chan676917d2006-12-07 00:20:22 -0800230 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787F)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700231 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714)},
232 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714S)},
233 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715)},
234 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715S)},
235 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780)},
236 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780S)},
237 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5781)},
Michael Chanb5d37722006-09-27 16:06:21 -0700238 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906)},
239 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906M)},
Matt Carlsond30cdd22007-10-07 23:28:35 -0700240 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5784)},
241 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5764)},
Matt Carlson6c7af272007-10-21 16:12:02 -0700242 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5723)},
Matt Carlson9936bcf2007-10-10 18:03:07 -0700243 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761)},
244 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761E)},
Matt Carlsonc88e6682008-11-03 16:49:18 -0800245 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761S)},
246 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761SE)},
Matt Carlson2befdce2009-08-28 12:28:45 +0000247 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_G)},
248 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_F)},
Matt Carlson321d32a2008-11-21 17:22:19 -0800249 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780)},
250 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57760)},
251 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57790)},
Matt Carlson5e7ccf22009-08-25 10:08:42 +0000252 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57788)},
Matt Carlson5001e2f2009-11-13 13:03:51 +0000253 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717)},
254 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5718)},
Matt Carlsonb0f75222010-01-20 16:58:11 +0000255 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57781)},
256 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57785)},
257 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57761)},
258 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57765)},
259 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57791)},
260 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57795)},
Matt Carlson302b5002010-06-05 17:24:38 +0000261 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5719)},
Matt Carlsonba1f3c72011-04-05 14:22:50 +0000262 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5720)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700263 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9DXX)},
264 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9MXX)},
265 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1000)},
266 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1001)},
267 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1003)},
268 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC9100)},
269 {PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_TIGON3)},
270 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271};
272
273MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
274
Andreas Mohr50da8592006-08-14 23:54:30 -0700275static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000277} ethtool_stats_keys[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 { "rx_octets" },
279 { "rx_fragments" },
280 { "rx_ucast_packets" },
281 { "rx_mcast_packets" },
282 { "rx_bcast_packets" },
283 { "rx_fcs_errors" },
284 { "rx_align_errors" },
285 { "rx_xon_pause_rcvd" },
286 { "rx_xoff_pause_rcvd" },
287 { "rx_mac_ctrl_rcvd" },
288 { "rx_xoff_entered" },
289 { "rx_frame_too_long_errors" },
290 { "rx_jabbers" },
291 { "rx_undersize_packets" },
292 { "rx_in_length_errors" },
293 { "rx_out_length_errors" },
294 { "rx_64_or_less_octet_packets" },
295 { "rx_65_to_127_octet_packets" },
296 { "rx_128_to_255_octet_packets" },
297 { "rx_256_to_511_octet_packets" },
298 { "rx_512_to_1023_octet_packets" },
299 { "rx_1024_to_1522_octet_packets" },
300 { "rx_1523_to_2047_octet_packets" },
301 { "rx_2048_to_4095_octet_packets" },
302 { "rx_4096_to_8191_octet_packets" },
303 { "rx_8192_to_9022_octet_packets" },
304
305 { "tx_octets" },
306 { "tx_collisions" },
307
308 { "tx_xon_sent" },
309 { "tx_xoff_sent" },
310 { "tx_flow_control" },
311 { "tx_mac_errors" },
312 { "tx_single_collisions" },
313 { "tx_mult_collisions" },
314 { "tx_deferred" },
315 { "tx_excessive_collisions" },
316 { "tx_late_collisions" },
317 { "tx_collide_2times" },
318 { "tx_collide_3times" },
319 { "tx_collide_4times" },
320 { "tx_collide_5times" },
321 { "tx_collide_6times" },
322 { "tx_collide_7times" },
323 { "tx_collide_8times" },
324 { "tx_collide_9times" },
325 { "tx_collide_10times" },
326 { "tx_collide_11times" },
327 { "tx_collide_12times" },
328 { "tx_collide_13times" },
329 { "tx_collide_14times" },
330 { "tx_collide_15times" },
331 { "tx_ucast_packets" },
332 { "tx_mcast_packets" },
333 { "tx_bcast_packets" },
334 { "tx_carrier_sense_errors" },
335 { "tx_discards" },
336 { "tx_errors" },
337
338 { "dma_writeq_full" },
339 { "dma_write_prioq_full" },
340 { "rxbds_empty" },
341 { "rx_discards" },
Matt Carlson4d958472011-04-20 07:57:35 +0000342 { "mbuf_lwm_thresh_hit" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 { "rx_errors" },
344 { "rx_threshold_hit" },
345
346 { "dma_readq_full" },
347 { "dma_read_prioq_full" },
348 { "tx_comp_queue_full" },
349
350 { "ring_set_send_prod_index" },
351 { "ring_status_update" },
352 { "nic_irqs" },
353 { "nic_avoided_irqs" },
354 { "nic_tx_threshold_hit" }
355};
356
Matt Carlson48fa55a2011-04-13 11:05:06 +0000357#define TG3_NUM_STATS ARRAY_SIZE(ethtool_stats_keys)
358
359
Andreas Mohr50da8592006-08-14 23:54:30 -0700360static const struct {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700361 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000362} ethtool_test_keys[] = {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700363 { "nvram test (online) " },
364 { "link test (online) " },
365 { "register test (offline)" },
366 { "memory test (offline)" },
367 { "loopback test (offline)" },
368 { "interrupt test (offline)" },
369};
370
Matt Carlson48fa55a2011-04-13 11:05:06 +0000371#define TG3_NUM_TEST ARRAY_SIZE(ethtool_test_keys)
372
373
Michael Chanb401e9e2005-12-19 16:27:04 -0800374static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
375{
376 writel(val, tp->regs + off);
377}
378
379static u32 tg3_read32(struct tg3 *tp, u32 off)
380{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000381 return readl(tp->regs + off);
Michael Chanb401e9e2005-12-19 16:27:04 -0800382}
383
Matt Carlson0d3031d2007-10-10 18:02:43 -0700384static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
385{
386 writel(val, tp->aperegs + off);
387}
388
389static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
390{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000391 return readl(tp->aperegs + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700392}
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
395{
Michael Chan68929142005-08-09 20:17:14 -0700396 unsigned long flags;
397
398 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700399 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
400 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
Michael Chan68929142005-08-09 20:17:14 -0700401 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700402}
403
404static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
405{
406 writel(val, tp->regs + off);
407 readl(tp->regs + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408}
409
Michael Chan68929142005-08-09 20:17:14 -0700410static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
411{
412 unsigned long flags;
413 u32 val;
414
415 spin_lock_irqsave(&tp->indirect_lock, flags);
416 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
417 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
418 spin_unlock_irqrestore(&tp->indirect_lock, flags);
419 return val;
420}
421
422static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
423{
424 unsigned long flags;
425
426 if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
427 pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
428 TG3_64BIT_REG_LOW, val);
429 return;
430 }
Matt Carlson66711e62009-11-13 13:03:49 +0000431 if (off == TG3_RX_STD_PROD_IDX_REG) {
Michael Chan68929142005-08-09 20:17:14 -0700432 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
433 TG3_64BIT_REG_LOW, val);
434 return;
435 }
436
437 spin_lock_irqsave(&tp->indirect_lock, flags);
438 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
439 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
440 spin_unlock_irqrestore(&tp->indirect_lock, flags);
441
442 /* In indirect mode when disabling interrupts, we also need
443 * to clear the interrupt bit in the GRC local ctrl register.
444 */
445 if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
446 (val == 0x1)) {
447 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
448 tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
449 }
450}
451
452static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
453{
454 unsigned long flags;
455 u32 val;
456
457 spin_lock_irqsave(&tp->indirect_lock, flags);
458 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
459 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
460 spin_unlock_irqrestore(&tp->indirect_lock, flags);
461 return val;
462}
463
Michael Chanb401e9e2005-12-19 16:27:04 -0800464/* usec_wait specifies the wait time in usec when writing to certain registers
465 * where it is unsafe to read back the register without some delay.
466 * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
467 * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
468 */
469static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
Michael Chanb401e9e2005-12-19 16:27:04 -0800471 if ((tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG) ||
472 (tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND))
473 /* Non-posted methods */
474 tp->write32(tp, off, val);
475 else {
476 /* Posted method */
477 tg3_write32(tp, off, val);
478 if (usec_wait)
479 udelay(usec_wait);
480 tp->read32(tp, off);
481 }
482 /* Wait again after the read for the posted method to guarantee that
483 * the wait time is met.
484 */
485 if (usec_wait)
486 udelay(usec_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487}
488
Michael Chan09ee9292005-08-09 20:17:00 -0700489static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
490{
491 tp->write32_mbox(tp, off, val);
Michael Chan68929142005-08-09 20:17:14 -0700492 if (!(tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER) &&
493 !(tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND))
494 tp->read32_mbox(tp, off);
Michael Chan09ee9292005-08-09 20:17:00 -0700495}
496
Michael Chan20094932005-08-09 20:16:32 -0700497static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
499 void __iomem *mbox = tp->regs + off;
500 writel(val, mbox);
501 if (tp->tg3_flags & TG3_FLAG_TXD_MBOX_HWBUG)
502 writel(val, mbox);
503 if (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)
504 readl(mbox);
505}
506
Michael Chanb5d37722006-09-27 16:06:21 -0700507static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
508{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000509 return readl(tp->regs + off + GRCMBOX_BASE);
Michael Chanb5d37722006-09-27 16:06:21 -0700510}
511
512static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
513{
514 writel(val, tp->regs + off + GRCMBOX_BASE);
515}
516
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000517#define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700518#define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000519#define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
520#define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
521#define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
Michael Chan20094932005-08-09 20:16:32 -0700522
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000523#define tw32(reg, val) tp->write32(tp, reg, val)
524#define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
525#define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
526#define tr32(reg) tp->read32(tp, reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
529{
Michael Chan68929142005-08-09 20:17:14 -0700530 unsigned long flags;
531
Michael Chanb5d37722006-09-27 16:06:21 -0700532 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) &&
533 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
534 return;
535
Michael Chan68929142005-08-09 20:17:14 -0700536 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chanbbadf502006-04-06 21:46:34 -0700537 if (tp->tg3_flags & TG3_FLAG_SRAM_USE_CONFIG) {
538 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
539 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Michael Chanbbadf502006-04-06 21:46:34 -0700541 /* Always leave this as zero. */
542 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
543 } else {
544 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
545 tw32_f(TG3PCI_MEM_WIN_DATA, val);
546
547 /* Always leave this as zero. */
548 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
549 }
Michael Chan68929142005-08-09 20:17:14 -0700550 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551}
552
553static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
554{
Michael Chan68929142005-08-09 20:17:14 -0700555 unsigned long flags;
556
Michael Chanb5d37722006-09-27 16:06:21 -0700557 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) &&
558 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
559 *val = 0;
560 return;
561 }
562
Michael Chan68929142005-08-09 20:17:14 -0700563 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chanbbadf502006-04-06 21:46:34 -0700564 if (tp->tg3_flags & TG3_FLAG_SRAM_USE_CONFIG) {
565 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
566 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Michael Chanbbadf502006-04-06 21:46:34 -0700568 /* Always leave this as zero. */
569 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
570 } else {
571 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
572 *val = tr32(TG3PCI_MEM_WIN_DATA);
573
574 /* Always leave this as zero. */
575 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
576 }
Michael Chan68929142005-08-09 20:17:14 -0700577 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578}
579
Matt Carlson0d3031d2007-10-10 18:02:43 -0700580static void tg3_ape_lock_init(struct tg3 *tp)
581{
582 int i;
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000583 u32 regbase;
584
585 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
586 regbase = TG3_APE_LOCK_GRANT;
587 else
588 regbase = TG3_APE_PER_LOCK_GRANT;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700589
590 /* Make sure the driver hasn't any stale locks. */
591 for (i = 0; i < 8; i++)
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000592 tg3_ape_write32(tp, regbase + 4 * i, APE_LOCK_GRANT_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700593}
594
595static int tg3_ape_lock(struct tg3 *tp, int locknum)
596{
597 int i, off;
598 int ret = 0;
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000599 u32 status, req, gnt;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700600
601 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
602 return 0;
603
604 switch (locknum) {
Matt Carlson33f401a2010-04-05 10:19:27 +0000605 case TG3_APE_LOCK_GRC:
606 case TG3_APE_LOCK_MEM:
607 break;
608 default:
609 return -EINVAL;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700610 }
611
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000612 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
613 req = TG3_APE_LOCK_REQ;
614 gnt = TG3_APE_LOCK_GRANT;
615 } else {
616 req = TG3_APE_PER_LOCK_REQ;
617 gnt = TG3_APE_PER_LOCK_GRANT;
618 }
619
Matt Carlson0d3031d2007-10-10 18:02:43 -0700620 off = 4 * locknum;
621
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000622 tg3_ape_write32(tp, req + off, APE_LOCK_REQ_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700623
624 /* Wait for up to 1 millisecond to acquire lock. */
625 for (i = 0; i < 100; i++) {
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000626 status = tg3_ape_read32(tp, gnt + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700627 if (status == APE_LOCK_GRANT_DRIVER)
628 break;
629 udelay(10);
630 }
631
632 if (status != APE_LOCK_GRANT_DRIVER) {
633 /* Revoke the lock request. */
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000634 tg3_ape_write32(tp, gnt + off,
Matt Carlson0d3031d2007-10-10 18:02:43 -0700635 APE_LOCK_GRANT_DRIVER);
636
637 ret = -EBUSY;
638 }
639
640 return ret;
641}
642
643static void tg3_ape_unlock(struct tg3 *tp, int locknum)
644{
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000645 u32 gnt;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700646
647 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
648 return;
649
650 switch (locknum) {
Matt Carlson33f401a2010-04-05 10:19:27 +0000651 case TG3_APE_LOCK_GRC:
652 case TG3_APE_LOCK_MEM:
653 break;
654 default:
655 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700656 }
657
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000658 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
659 gnt = TG3_APE_LOCK_GRANT;
660 else
661 gnt = TG3_APE_PER_LOCK_GRANT;
662
663 tg3_ape_write32(tp, gnt + 4 * locknum, APE_LOCK_GRANT_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700664}
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666static void tg3_disable_ints(struct tg3 *tp)
667{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000668 int i;
669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 tw32(TG3PCI_MISC_HOST_CTRL,
671 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000672 for (i = 0; i < tp->irq_max; i++)
673 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674}
675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676static void tg3_enable_ints(struct tg3 *tp)
677{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000678 int i;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000679
Michael Chanbbe832c2005-06-24 20:20:04 -0700680 tp->irq_sync = 0;
681 wmb();
682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 tw32(TG3PCI_MISC_HOST_CTRL,
684 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000685
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000686 tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000687 for (i = 0; i < tp->irq_cnt; i++) {
688 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000689
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000690 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
691 if (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)
692 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
693
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000694 tp->coal_now |= tnapi->coal_now;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000695 }
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000696
697 /* Force an initial interrupt */
698 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) &&
699 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
700 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
701 else
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000702 tw32(HOSTCC_MODE, tp->coal_now);
703
704 tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705}
706
Matt Carlson17375d22009-08-28 14:02:18 +0000707static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
Michael Chan04237dd2005-04-25 15:17:17 -0700708{
Matt Carlson17375d22009-08-28 14:02:18 +0000709 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +0000710 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan04237dd2005-04-25 15:17:17 -0700711 unsigned int work_exists = 0;
712
713 /* check for phy events */
714 if (!(tp->tg3_flags &
715 (TG3_FLAG_USE_LINKCHG_REG |
716 TG3_FLAG_POLL_SERDES))) {
717 if (sblk->status & SD_STATUS_LINK_CHG)
718 work_exists = 1;
719 }
720 /* check for RX/TX work to do */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000721 if (sblk->idx[0].tx_consumer != tnapi->tx_cons ||
Matt Carlson8d9d7cf2009-09-01 13:19:05 +0000722 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -0700723 work_exists = 1;
724
725 return work_exists;
726}
727
Matt Carlson17375d22009-08-28 14:02:18 +0000728/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -0700729 * similar to tg3_enable_ints, but it accurately determines whether there
730 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400731 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 */
Matt Carlson17375d22009-08-28 14:02:18 +0000733static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
Matt Carlson17375d22009-08-28 14:02:18 +0000735 struct tg3 *tp = tnapi->tp;
736
Matt Carlson898a56f2009-08-28 14:02:40 +0000737 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 mmiowb();
739
David S. Millerfac9b832005-05-18 22:46:34 -0700740 /* When doing tagged status, this work check is unnecessary.
741 * The last_tag we write above tells the chip which piece of
742 * work we've completed.
743 */
744 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) &&
Matt Carlson17375d22009-08-28 14:02:18 +0000745 tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -0700746 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +0000747 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748}
749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750static void tg3_switch_clocks(struct tg3 *tp)
751{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000752 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 u32 orig_clock_ctrl;
754
Matt Carlson795d01c2007-10-07 23:28:17 -0700755 if ((tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
756 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -0700757 return;
758
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000759 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 orig_clock_ctrl = clock_ctrl;
762 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
763 CLOCK_CTRL_CLKRUN_OENABLE |
764 0x1f);
765 tp->pci_clock_ctrl = clock_ctrl;
766
767 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
768 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800769 tw32_wait_f(TG3PCI_CLOCK_CTRL,
770 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
772 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800773 tw32_wait_f(TG3PCI_CLOCK_CTRL,
774 clock_ctrl |
775 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
776 40);
777 tw32_wait_f(TG3PCI_CLOCK_CTRL,
778 clock_ctrl | (CLOCK_CTRL_ALTCLK),
779 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 }
Michael Chanb401e9e2005-12-19 16:27:04 -0800781 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782}
783
784#define PHY_BUSY_LOOPS 5000
785
786static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
787{
788 u32 frame_val;
789 unsigned int loops;
790 int ret;
791
792 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
793 tw32_f(MAC_MI_MODE,
794 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
795 udelay(80);
796 }
797
798 *val = 0x0;
799
Matt Carlson882e9792009-09-01 13:21:36 +0000800 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 MI_COM_PHY_ADDR_MASK);
802 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
803 MI_COM_REG_ADDR_MASK);
804 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 tw32_f(MAC_MI_COM, frame_val);
807
808 loops = PHY_BUSY_LOOPS;
809 while (loops != 0) {
810 udelay(10);
811 frame_val = tr32(MAC_MI_COM);
812
813 if ((frame_val & MI_COM_BUSY) == 0) {
814 udelay(5);
815 frame_val = tr32(MAC_MI_COM);
816 break;
817 }
818 loops -= 1;
819 }
820
821 ret = -EBUSY;
822 if (loops != 0) {
823 *val = frame_val & MI_COM_DATA_MASK;
824 ret = 0;
825 }
826
827 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
828 tw32_f(MAC_MI_MODE, tp->mi_mode);
829 udelay(80);
830 }
831
832 return ret;
833}
834
835static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
836{
837 u32 frame_val;
838 unsigned int loops;
839 int ret;
840
Matt Carlsonf07e9af2010-08-02 11:26:07 +0000841 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Michael Chanb5d37722006-09-27 16:06:21 -0700842 (reg == MII_TG3_CTRL || reg == MII_TG3_AUX_CTRL))
843 return 0;
844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
846 tw32_f(MAC_MI_MODE,
847 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
848 udelay(80);
849 }
850
Matt Carlson882e9792009-09-01 13:21:36 +0000851 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 MI_COM_PHY_ADDR_MASK);
853 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
854 MI_COM_REG_ADDR_MASK);
855 frame_val |= (val & MI_COM_DATA_MASK);
856 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 tw32_f(MAC_MI_COM, frame_val);
859
860 loops = PHY_BUSY_LOOPS;
861 while (loops != 0) {
862 udelay(10);
863 frame_val = tr32(MAC_MI_COM);
864 if ((frame_val & MI_COM_BUSY) == 0) {
865 udelay(5);
866 frame_val = tr32(MAC_MI_COM);
867 break;
868 }
869 loops -= 1;
870 }
871
872 ret = -EBUSY;
873 if (loops != 0)
874 ret = 0;
875
876 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
877 tw32_f(MAC_MI_MODE, tp->mi_mode);
878 udelay(80);
879 }
880
881 return ret;
882}
883
Matt 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 Carlson95e28692008-05-25 23:44:14 -0700965static int tg3_bmcr_reset(struct tg3 *tp)
966{
967 u32 phy_control;
968 int limit, err;
969
970 /* OK, reset it, and poll the BMCR_RESET bit until it
971 * clears or we time out.
972 */
973 phy_control = BMCR_RESET;
974 err = tg3_writephy(tp, MII_BMCR, phy_control);
975 if (err != 0)
976 return -EBUSY;
977
978 limit = 5000;
979 while (limit--) {
980 err = tg3_readphy(tp, MII_BMCR, &phy_control);
981 if (err != 0)
982 return -EBUSY;
983
984 if ((phy_control & BMCR_RESET) == 0) {
985 udelay(40);
986 break;
987 }
988 udelay(10);
989 }
Roel Kluind4675b52009-02-12 16:33:27 -0800990 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -0700991 return -EBUSY;
992
993 return 0;
994}
995
Matt Carlson158d7ab2008-05-29 01:37:54 -0700996static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
997{
Francois Romieu3d165432009-01-19 16:56:50 -0800998 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700999 u32 val;
1000
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001001 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001002
1003 if (tg3_readphy(tp, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001004 val = -EIO;
1005
1006 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001007
1008 return val;
1009}
1010
1011static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
1012{
Francois Romieu3d165432009-01-19 16:56:50 -08001013 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001014 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001015
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001016 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001017
1018 if (tg3_writephy(tp, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001019 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001020
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001021 spin_unlock_bh(&tp->lock);
1022
1023 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001024}
1025
1026static int tg3_mdio_reset(struct mii_bus *bp)
1027{
1028 return 0;
1029}
1030
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001031static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -07001032{
1033 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001034 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -07001035
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001036 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001037 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001038 case PHY_ID_BCM50610:
1039 case PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001040 val = MAC_PHYCFG2_50610_LED_MODES;
1041 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001042 case PHY_ID_BCMAC131:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001043 val = MAC_PHYCFG2_AC131_LED_MODES;
1044 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001045 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001046 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
1047 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001048 case PHY_ID_RTL8201E:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001049 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
1050 break;
1051 default:
Matt Carlsona9daf362008-05-25 23:49:44 -07001052 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001053 }
1054
1055 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
1056 tw32(MAC_PHYCFG2, val);
1057
1058 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001059 val &= ~(MAC_PHYCFG1_RGMII_INT |
1060 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
1061 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001062 tw32(MAC_PHYCFG1, val);
1063
1064 return;
1065 }
1066
Matt Carlson14417062010-02-17 15:16:59 +00001067 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE))
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001068 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
1069 MAC_PHYCFG2_FMODE_MASK_MASK |
1070 MAC_PHYCFG2_GMODE_MASK_MASK |
1071 MAC_PHYCFG2_ACT_MASK_MASK |
1072 MAC_PHYCFG2_QUAL_MASK_MASK |
1073 MAC_PHYCFG2_INBAND_ENABLE;
1074
1075 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001076
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001077 val = tr32(MAC_PHYCFG1);
1078 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
1079 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
Matt Carlson14417062010-02-17 15:16:59 +00001080 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE)) {
Matt Carlsona9daf362008-05-25 23:49:44 -07001081 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
1082 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
1083 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
1084 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1085 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001086 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1087 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1088 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001089
Matt Carlsona9daf362008-05-25 23:49:44 -07001090 val = tr32(MAC_EXT_RGMII_MODE);
1091 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1092 MAC_RGMII_MODE_RX_QUALITY |
1093 MAC_RGMII_MODE_RX_ACTIVITY |
1094 MAC_RGMII_MODE_RX_ENG_DET |
1095 MAC_RGMII_MODE_TX_ENABLE |
1096 MAC_RGMII_MODE_TX_LOWPWR |
1097 MAC_RGMII_MODE_TX_RESET);
Matt Carlson14417062010-02-17 15:16:59 +00001098 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE)) {
Matt Carlsona9daf362008-05-25 23:49:44 -07001099 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
1100 val |= MAC_RGMII_MODE_RX_INT_B |
1101 MAC_RGMII_MODE_RX_QUALITY |
1102 MAC_RGMII_MODE_RX_ACTIVITY |
1103 MAC_RGMII_MODE_RX_ENG_DET;
1104 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
1105 val |= MAC_RGMII_MODE_TX_ENABLE |
1106 MAC_RGMII_MODE_TX_LOWPWR |
1107 MAC_RGMII_MODE_TX_RESET;
1108 }
1109 tw32(MAC_EXT_RGMII_MODE, val);
1110}
1111
Matt Carlson158d7ab2008-05-29 01:37:54 -07001112static void tg3_mdio_start(struct tg3 *tp)
1113{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001114 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1115 tw32_f(MAC_MI_MODE, tp->mi_mode);
1116 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001117
Matt Carlson9ea48182010-02-17 15:17:01 +00001118 if ((tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED) &&
1119 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1120 tg3_mdio_config_5785(tp);
1121}
1122
1123static int tg3_mdio_init(struct tg3 *tp)
1124{
1125 int i;
1126 u32 reg;
1127 struct phy_device *phydev;
1128
Matt Carlson0a58d662011-04-05 14:22:45 +00001129 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
Matt Carlson9c7df912010-06-05 17:24:36 +00001130 u32 is_serdes;
Matt Carlson882e9792009-09-01 13:21:36 +00001131
Matt Carlson9c7df912010-06-05 17:24:36 +00001132 tp->phy_addr = PCI_FUNC(tp->pdev->devfn) + 1;
Matt Carlson882e9792009-09-01 13:21:36 +00001133
Matt Carlsond1ec96a2010-01-12 10:11:38 +00001134 if (tp->pci_chip_rev_id != CHIPREV_ID_5717_A0)
1135 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1136 else
1137 is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1138 TG3_CPMU_PHY_STRAP_IS_SERDES;
Matt Carlson882e9792009-09-01 13:21:36 +00001139 if (is_serdes)
1140 tp->phy_addr += 7;
1141 } else
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001142 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001143
Matt Carlson158d7ab2008-05-29 01:37:54 -07001144 tg3_mdio_start(tp);
1145
1146 if (!(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) ||
1147 (tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED))
1148 return 0;
1149
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001150 tp->mdio_bus = mdiobus_alloc();
1151 if (tp->mdio_bus == NULL)
1152 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001153
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001154 tp->mdio_bus->name = "tg3 mdio bus";
1155 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001156 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001157 tp->mdio_bus->priv = tp;
1158 tp->mdio_bus->parent = &tp->pdev->dev;
1159 tp->mdio_bus->read = &tg3_mdio_read;
1160 tp->mdio_bus->write = &tg3_mdio_write;
1161 tp->mdio_bus->reset = &tg3_mdio_reset;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001162 tp->mdio_bus->phy_mask = ~(1 << TG3_PHY_MII_ADDR);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001163 tp->mdio_bus->irq = &tp->mdio_irq[0];
Matt Carlson158d7ab2008-05-29 01:37:54 -07001164
1165 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001166 tp->mdio_bus->irq[i] = PHY_POLL;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001167
1168 /* The bus registration will look for all the PHYs on the mdio bus.
1169 * Unfortunately, it does not ensure the PHY is powered up before
1170 * accessing the PHY ID registers. A chip reset is the
1171 * quickest way to bring the device back to an operational state..
1172 */
1173 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1174 tg3_bmcr_reset(tp);
1175
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001176 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001177 if (i) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001178 dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001179 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001180 return i;
1181 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001182
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001183 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsona9daf362008-05-25 23:49:44 -07001184
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001185 if (!phydev || !phydev->drv) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001186 dev_warn(&tp->pdev->dev, "No PHY devices\n");
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001187 mdiobus_unregister(tp->mdio_bus);
1188 mdiobus_free(tp->mdio_bus);
1189 return -ENODEV;
1190 }
1191
1192 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001193 case PHY_ID_BCM57780:
Matt Carlson321d32a2008-11-21 17:22:19 -08001194 phydev->interface = PHY_INTERFACE_MODE_GMII;
Matt Carlsonc704dc22009-11-02 14:32:12 +00001195 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08001196 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001197 case PHY_ID_BCM50610:
1198 case PHY_ID_BCM50610M:
Matt Carlson32e5a8d2009-11-02 14:31:39 +00001199 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001200 PHY_BRCM_RX_REFCLK_UNUSED |
Matt Carlson52fae082009-11-02 14:32:38 +00001201 PHY_BRCM_DIS_TXCRXC_NOENRGY |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001202 PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson14417062010-02-17 15:16:59 +00001203 if (tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE)
Matt Carlsona9daf362008-05-25 23:49:44 -07001204 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
1205 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
1206 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
1207 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
1208 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001209 /* fallthru */
Matt Carlson6a443a02010-02-17 15:17:04 +00001210 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001211 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001212 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001213 case PHY_ID_RTL8201E:
1214 case PHY_ID_BCMAC131:
Matt Carlsona9daf362008-05-25 23:49:44 -07001215 phydev->interface = PHY_INTERFACE_MODE_MII;
Matt Carlsoncdd4e092009-11-02 14:31:11 +00001216 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001217 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlsona9daf362008-05-25 23:49:44 -07001218 break;
1219 }
1220
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001221 tp->tg3_flags3 |= TG3_FLG3_MDIOBUS_INITED;
1222
1223 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1224 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001225
1226 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001227}
1228
1229static void tg3_mdio_fini(struct tg3 *tp)
1230{
1231 if (tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED) {
1232 tp->tg3_flags3 &= ~TG3_FLG3_MDIOBUS_INITED;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001233 mdiobus_unregister(tp->mdio_bus);
1234 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001235 }
1236}
1237
Matt Carlson95e28692008-05-25 23:44:14 -07001238/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001239static inline void tg3_generate_fw_event(struct tg3 *tp)
1240{
1241 u32 val;
1242
1243 val = tr32(GRC_RX_CPU_EVENT);
1244 val |= GRC_RX_CPU_DRIVER_EVENT;
1245 tw32_f(GRC_RX_CPU_EVENT, val);
1246
1247 tp->last_event_jiffies = jiffies;
1248}
1249
1250#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1251
1252/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001253static void tg3_wait_for_event_ack(struct tg3 *tp)
1254{
1255 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001256 unsigned int delay_cnt;
1257 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001258
Matt Carlson4ba526c2008-08-15 14:10:04 -07001259 /* If enough time has passed, no wait is necessary. */
1260 time_remain = (long)(tp->last_event_jiffies + 1 +
1261 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1262 (long)jiffies;
1263 if (time_remain < 0)
1264 return;
1265
1266 /* Check if we can shorten the wait time. */
1267 delay_cnt = jiffies_to_usecs(time_remain);
1268 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1269 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1270 delay_cnt = (delay_cnt >> 3) + 1;
1271
1272 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001273 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1274 break;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001275 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001276 }
1277}
1278
1279/* tp->lock is held. */
1280static void tg3_ump_link_report(struct tg3 *tp)
1281{
1282 u32 reg;
1283 u32 val;
1284
1285 if (!(tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
1286 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
1287 return;
1288
1289 tg3_wait_for_event_ack(tp);
1290
1291 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1292
1293 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1294
1295 val = 0;
1296 if (!tg3_readphy(tp, MII_BMCR, &reg))
1297 val = reg << 16;
1298 if (!tg3_readphy(tp, MII_BMSR, &reg))
1299 val |= (reg & 0xffff);
1300 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX, val);
1301
1302 val = 0;
1303 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1304 val = reg << 16;
1305 if (!tg3_readphy(tp, MII_LPA, &reg))
1306 val |= (reg & 0xffff);
1307 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 4, val);
1308
1309 val = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001310 if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
Matt Carlson95e28692008-05-25 23:44:14 -07001311 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1312 val = reg << 16;
1313 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1314 val |= (reg & 0xffff);
1315 }
1316 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 8, val);
1317
1318 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1319 val = reg << 16;
1320 else
1321 val = 0;
1322 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 12, val);
1323
Matt Carlson4ba526c2008-08-15 14:10:04 -07001324 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001325}
1326
1327static void tg3_link_report(struct tg3 *tp)
1328{
1329 if (!netif_carrier_ok(tp->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001330 netif_info(tp, link, tp->dev, "Link is down\n");
Matt Carlson95e28692008-05-25 23:44:14 -07001331 tg3_ump_link_report(tp);
1332 } else if (netif_msg_link(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001333 netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
1334 (tp->link_config.active_speed == SPEED_1000 ?
1335 1000 :
1336 (tp->link_config.active_speed == SPEED_100 ?
1337 100 : 10)),
1338 (tp->link_config.active_duplex == DUPLEX_FULL ?
1339 "full" : "half"));
Matt Carlson95e28692008-05-25 23:44:14 -07001340
Joe Perches05dbe002010-02-17 19:44:19 +00001341 netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
1342 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
1343 "on" : "off",
1344 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
1345 "on" : "off");
Matt Carlson95e28692008-05-25 23:44:14 -07001346 tg3_ump_link_report(tp);
1347 }
1348}
1349
1350static u16 tg3_advert_flowctrl_1000T(u8 flow_ctrl)
1351{
1352 u16 miireg;
1353
Steve Glendinninge18ce342008-12-16 02:00:00 -08001354 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001355 miireg = ADVERTISE_PAUSE_CAP;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001356 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001357 miireg = ADVERTISE_PAUSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001358 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001359 miireg = ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1360 else
1361 miireg = 0;
1362
1363 return miireg;
1364}
1365
1366static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1367{
1368 u16 miireg;
1369
Steve Glendinninge18ce342008-12-16 02:00:00 -08001370 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001371 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001372 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001373 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001374 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001375 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1376 else
1377 miireg = 0;
1378
1379 return miireg;
1380}
1381
Matt Carlson95e28692008-05-25 23:44:14 -07001382static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1383{
1384 u8 cap = 0;
1385
1386 if (lcladv & ADVERTISE_1000XPAUSE) {
1387 if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1388 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001389 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001390 else if (rmtadv & LPA_1000XPAUSE_ASYM)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001391 cap = FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001392 } else {
1393 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001394 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001395 }
1396 } else if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1397 if ((rmtadv & LPA_1000XPAUSE) && (rmtadv & LPA_1000XPAUSE_ASYM))
Steve Glendinninge18ce342008-12-16 02:00:00 -08001398 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001399 }
1400
1401 return cap;
1402}
1403
Matt Carlsonf51f3562008-05-25 23:45:08 -07001404static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001405{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001406 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001407 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001408 u32 old_rx_mode = tp->rx_mode;
1409 u32 old_tx_mode = tp->tx_mode;
1410
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001411 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001412 autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001413 else
1414 autoneg = tp->link_config.autoneg;
1415
1416 if (autoneg == AUTONEG_ENABLE &&
Matt Carlson95e28692008-05-25 23:44:14 -07001417 (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001418 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001419 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001420 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001421 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001422 } else
1423 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001424
Matt Carlsonf51f3562008-05-25 23:45:08 -07001425 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001426
Steve Glendinninge18ce342008-12-16 02:00:00 -08001427 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001428 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1429 else
1430 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1431
Matt Carlsonf51f3562008-05-25 23:45:08 -07001432 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001433 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001434
Steve Glendinninge18ce342008-12-16 02:00:00 -08001435 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001436 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1437 else
1438 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1439
Matt Carlsonf51f3562008-05-25 23:45:08 -07001440 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001441 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001442}
1443
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001444static void tg3_adjust_link(struct net_device *dev)
1445{
1446 u8 oldflowctrl, linkmesg = 0;
1447 u32 mac_mode, lcl_adv, rmt_adv;
1448 struct tg3 *tp = netdev_priv(dev);
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001449 struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001450
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001451 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001452
1453 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
1454 MAC_MODE_HALF_DUPLEX);
1455
1456 oldflowctrl = tp->link_config.active_flowctrl;
1457
1458 if (phydev->link) {
1459 lcl_adv = 0;
1460 rmt_adv = 0;
1461
1462 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
1463 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001464 else if (phydev->speed == SPEED_1000 ||
1465 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001466 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001467 else
1468 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001469
1470 if (phydev->duplex == DUPLEX_HALF)
1471 mac_mode |= MAC_MODE_HALF_DUPLEX;
1472 else {
1473 lcl_adv = tg3_advert_flowctrl_1000T(
1474 tp->link_config.flowctrl);
1475
1476 if (phydev->pause)
1477 rmt_adv = LPA_PAUSE_CAP;
1478 if (phydev->asym_pause)
1479 rmt_adv |= LPA_PAUSE_ASYM;
1480 }
1481
1482 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
1483 } else
1484 mac_mode |= MAC_MODE_PORT_MODE_GMII;
1485
1486 if (mac_mode != tp->mac_mode) {
1487 tp->mac_mode = mac_mode;
1488 tw32_f(MAC_MODE, tp->mac_mode);
1489 udelay(40);
1490 }
1491
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001492 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
1493 if (phydev->speed == SPEED_10)
1494 tw32(MAC_MI_STAT,
1495 MAC_MI_STAT_10MBPS_MODE |
1496 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1497 else
1498 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1499 }
1500
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001501 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
1502 tw32(MAC_TX_LENGTHS,
1503 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1504 (6 << TX_LENGTHS_IPG_SHIFT) |
1505 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
1506 else
1507 tw32(MAC_TX_LENGTHS,
1508 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1509 (6 << TX_LENGTHS_IPG_SHIFT) |
1510 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
1511
1512 if ((phydev->link && tp->link_config.active_speed == SPEED_INVALID) ||
1513 (!phydev->link && tp->link_config.active_speed != SPEED_INVALID) ||
1514 phydev->speed != tp->link_config.active_speed ||
1515 phydev->duplex != tp->link_config.active_duplex ||
1516 oldflowctrl != tp->link_config.active_flowctrl)
Matt Carlsonc6cdf432010-04-05 10:19:26 +00001517 linkmesg = 1;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001518
1519 tp->link_config.active_speed = phydev->speed;
1520 tp->link_config.active_duplex = phydev->duplex;
1521
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001522 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001523
1524 if (linkmesg)
1525 tg3_link_report(tp);
1526}
1527
1528static int tg3_phy_init(struct tg3 *tp)
1529{
1530 struct phy_device *phydev;
1531
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001532 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001533 return 0;
1534
1535 /* Bring the PHY back to a known state. */
1536 tg3_bmcr_reset(tp);
1537
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001538 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001539
1540 /* Attach the MAC to the PHY. */
Kay Sieversfb28ad32008-11-10 13:55:14 -08001541 phydev = phy_connect(tp->dev, dev_name(&phydev->dev), tg3_adjust_link,
Matt Carlsona9daf362008-05-25 23:49:44 -07001542 phydev->dev_flags, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001543 if (IS_ERR(phydev)) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001544 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001545 return PTR_ERR(phydev);
1546 }
1547
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001548 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001549 switch (phydev->interface) {
1550 case PHY_INTERFACE_MODE_GMII:
1551 case PHY_INTERFACE_MODE_RGMII:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001552 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Matt Carlson321d32a2008-11-21 17:22:19 -08001553 phydev->supported &= (PHY_GBIT_FEATURES |
1554 SUPPORTED_Pause |
1555 SUPPORTED_Asym_Pause);
1556 break;
1557 }
1558 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001559 case PHY_INTERFACE_MODE_MII:
1560 phydev->supported &= (PHY_BASIC_FEATURES |
1561 SUPPORTED_Pause |
1562 SUPPORTED_Asym_Pause);
1563 break;
1564 default:
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001565 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001566 return -EINVAL;
1567 }
1568
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001569 tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001570
1571 phydev->advertising = phydev->supported;
1572
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001573 return 0;
1574}
1575
1576static void tg3_phy_start(struct tg3 *tp)
1577{
1578 struct phy_device *phydev;
1579
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001580 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001581 return;
1582
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001583 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001584
Matt Carlson80096062010-08-02 11:26:06 +00001585 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
1586 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001587 phydev->speed = tp->link_config.orig_speed;
1588 phydev->duplex = tp->link_config.orig_duplex;
1589 phydev->autoneg = tp->link_config.orig_autoneg;
1590 phydev->advertising = tp->link_config.orig_advertising;
1591 }
1592
1593 phy_start(phydev);
1594
1595 phy_start_aneg(phydev);
1596}
1597
1598static void tg3_phy_stop(struct tg3 *tp)
1599{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001600 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001601 return;
1602
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001603 phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001604}
1605
1606static void tg3_phy_fini(struct tg3 *tp)
1607{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001608 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001609 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001610 tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001611 }
1612}
1613
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001614static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
1615{
1616 u32 phytest;
1617
1618 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
1619 u32 phy;
1620
1621 tg3_writephy(tp, MII_TG3_FET_TEST,
1622 phytest | MII_TG3_FET_SHADOW_EN);
1623 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
1624 if (enable)
1625 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
1626 else
1627 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
1628 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
1629 }
1630 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
1631 }
1632}
1633
Matt Carlson6833c042008-11-21 17:18:59 -08001634static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
1635{
1636 u32 reg;
1637
Matt Carlsonecf14102010-01-20 16:58:05 +00001638 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
Matt Carlson0a58d662011-04-05 14:22:45 +00001639 ((tp->tg3_flags3 & TG3_FLG3_5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001640 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08001641 return;
1642
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001643 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001644 tg3_phy_fet_toggle_apd(tp, enable);
1645 return;
1646 }
1647
Matt Carlson6833c042008-11-21 17:18:59 -08001648 reg = MII_TG3_MISC_SHDW_WREN |
1649 MII_TG3_MISC_SHDW_SCR5_SEL |
1650 MII_TG3_MISC_SHDW_SCR5_LPED |
1651 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
1652 MII_TG3_MISC_SHDW_SCR5_SDTL |
1653 MII_TG3_MISC_SHDW_SCR5_C125OE;
1654 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 || !enable)
1655 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
1656
1657 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1658
1659
1660 reg = MII_TG3_MISC_SHDW_WREN |
1661 MII_TG3_MISC_SHDW_APD_SEL |
1662 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
1663 if (enable)
1664 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
1665
1666 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1667}
1668
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001669static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
1670{
1671 u32 phy;
1672
1673 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001674 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001675 return;
1676
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001677 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001678 u32 ephy;
1679
Matt Carlson535ef6e2009-08-25 10:09:36 +00001680 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
1681 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
1682
1683 tg3_writephy(tp, MII_TG3_FET_TEST,
1684 ephy | MII_TG3_FET_SHADOW_EN);
1685 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001686 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00001687 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001688 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00001689 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
1690 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001691 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00001692 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001693 }
1694 } else {
Matt Carlson15ee95c2011-04-20 07:57:40 +00001695 int ret;
1696
1697 ret = tg3_phy_auxctl_read(tp,
1698 MII_TG3_AUXCTL_SHDWSEL_MISC, &phy);
1699 if (!ret) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001700 if (enable)
1701 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1702 else
1703 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1704 phy |= MII_TG3_AUXCTL_MISC_WREN;
1705 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1706 }
1707 }
1708}
1709
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710static void tg3_phy_set_wirespeed(struct tg3 *tp)
1711{
Matt Carlson15ee95c2011-04-20 07:57:40 +00001712 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 u32 val;
1714
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001715 if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 return;
1717
Matt Carlson15ee95c2011-04-20 07:57:40 +00001718 ret = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_MISC, &val);
1719 if (!ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 tg3_writephy(tp, MII_TG3_AUX_CTRL,
1721 (val | (1 << 15) | (1 << 4)));
1722}
1723
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001724static void tg3_phy_apply_otp(struct tg3 *tp)
1725{
1726 u32 otp, phy;
1727
1728 if (!tp->phy_otp)
1729 return;
1730
1731 otp = tp->phy_otp;
1732
1733 /* Enable SM_DSP clock and tx 6dB coding. */
1734 phy = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1735 MII_TG3_AUXCTL_ACTL_SMDSP_ENA |
1736 MII_TG3_AUXCTL_ACTL_TX_6DB;
1737 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1738
1739 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
1740 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
1741 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
1742
1743 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
1744 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
1745 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
1746
1747 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
1748 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
1749 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
1750
1751 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
1752 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
1753
1754 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
1755 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
1756
1757 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
1758 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
1759 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
1760
1761 /* Turn off SM_DSP clock. */
1762 phy = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1763 MII_TG3_AUXCTL_ACTL_TX_6DB;
1764 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1765}
1766
Matt Carlson52b02d02010-10-14 10:37:41 +00001767static void tg3_phy_eee_adjust(struct tg3 *tp, u32 current_link_up)
1768{
1769 u32 val;
1770
1771 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
1772 return;
1773
1774 tp->setlpicnt = 0;
1775
1776 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
1777 current_link_up == 1 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +00001778 tp->link_config.active_duplex == DUPLEX_FULL &&
1779 (tp->link_config.active_speed == SPEED_100 ||
1780 tp->link_config.active_speed == SPEED_1000)) {
Matt Carlson52b02d02010-10-14 10:37:41 +00001781 u32 eeectl;
1782
1783 if (tp->link_config.active_speed == SPEED_1000)
1784 eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
1785 else
1786 eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
1787
1788 tw32(TG3_CPMU_EEE_CTRL, eeectl);
1789
Matt Carlson3110f5f52010-12-06 08:28:50 +00001790 tg3_phy_cl45_read(tp, MDIO_MMD_AN,
1791 TG3_CL45_D7_EEERES_STAT, &val);
Matt Carlson52b02d02010-10-14 10:37:41 +00001792
Matt Carlson21a00ab2011-01-25 15:58:55 +00001793 switch (val) {
1794 case TG3_CL45_D7_EEERES_STAT_LP_1000T:
1795 switch (GET_ASIC_REV(tp->pci_chip_rev_id)) {
1796 case ASIC_REV_5717:
1797 case ASIC_REV_5719:
1798 case ASIC_REV_57765:
1799 /* Enable SM_DSP clock and tx 6dB coding. */
1800 val = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1801 MII_TG3_AUXCTL_ACTL_SMDSP_ENA |
1802 MII_TG3_AUXCTL_ACTL_TX_6DB;
1803 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
1804
1805 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0000);
1806
1807 /* Turn off SM_DSP clock. */
1808 val = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1809 MII_TG3_AUXCTL_ACTL_TX_6DB;
1810 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
1811 }
1812 /* Fallthrough */
1813 case TG3_CL45_D7_EEERES_STAT_LP_100TX:
Matt Carlson52b02d02010-10-14 10:37:41 +00001814 tp->setlpicnt = 2;
Matt Carlson21a00ab2011-01-25 15:58:55 +00001815 }
Matt Carlson52b02d02010-10-14 10:37:41 +00001816 }
1817
1818 if (!tp->setlpicnt) {
1819 val = tr32(TG3_CPMU_EEE_MODE);
1820 tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
1821 }
1822}
1823
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824static int tg3_wait_macro_done(struct tg3 *tp)
1825{
1826 int limit = 100;
1827
1828 while (limit--) {
1829 u32 tmp32;
1830
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001831 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 if ((tmp32 & 0x1000) == 0)
1833 break;
1834 }
1835 }
Roel Kluind4675b52009-02-12 16:33:27 -08001836 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 return -EBUSY;
1838
1839 return 0;
1840}
1841
1842static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
1843{
1844 static const u32 test_pat[4][6] = {
1845 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
1846 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
1847 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
1848 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
1849 };
1850 int chan;
1851
1852 for (chan = 0; chan < 4; chan++) {
1853 int i;
1854
1855 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1856 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001857 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
1859 for (i = 0; i < 6; i++)
1860 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
1861 test_pat[chan][i]);
1862
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001863 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 if (tg3_wait_macro_done(tp)) {
1865 *resetp = 1;
1866 return -EBUSY;
1867 }
1868
1869 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1870 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001871 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 if (tg3_wait_macro_done(tp)) {
1873 *resetp = 1;
1874 return -EBUSY;
1875 }
1876
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001877 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 if (tg3_wait_macro_done(tp)) {
1879 *resetp = 1;
1880 return -EBUSY;
1881 }
1882
1883 for (i = 0; i < 6; i += 2) {
1884 u32 low, high;
1885
1886 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
1887 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
1888 tg3_wait_macro_done(tp)) {
1889 *resetp = 1;
1890 return -EBUSY;
1891 }
1892 low &= 0x7fff;
1893 high &= 0x000f;
1894 if (low != test_pat[chan][i] ||
1895 high != test_pat[chan][i+1]) {
1896 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
1897 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
1898 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
1899
1900 return -EBUSY;
1901 }
1902 }
1903 }
1904
1905 return 0;
1906}
1907
1908static int tg3_phy_reset_chanpat(struct tg3 *tp)
1909{
1910 int chan;
1911
1912 for (chan = 0; chan < 4; chan++) {
1913 int i;
1914
1915 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1916 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001917 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 for (i = 0; i < 6; i++)
1919 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001920 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 if (tg3_wait_macro_done(tp))
1922 return -EBUSY;
1923 }
1924
1925 return 0;
1926}
1927
1928static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
1929{
1930 u32 reg32, phy9_orig;
1931 int retries, do_phy_reset, err;
1932
1933 retries = 10;
1934 do_phy_reset = 1;
1935 do {
1936 if (do_phy_reset) {
1937 err = tg3_bmcr_reset(tp);
1938 if (err)
1939 return err;
1940 do_phy_reset = 0;
1941 }
1942
1943 /* Disable transmitter and interrupt. */
1944 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
1945 continue;
1946
1947 reg32 |= 0x3000;
1948 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
1949
1950 /* Set full-duplex, 1000 mbps. */
1951 tg3_writephy(tp, MII_BMCR,
1952 BMCR_FULLDPLX | TG3_BMCR_SPEED1000);
1953
1954 /* Set to master mode. */
1955 if (tg3_readphy(tp, MII_TG3_CTRL, &phy9_orig))
1956 continue;
1957
1958 tg3_writephy(tp, MII_TG3_CTRL,
1959 (MII_TG3_CTRL_AS_MASTER |
1960 MII_TG3_CTRL_ENABLE_AS_MASTER));
1961
1962 /* Enable SM_DSP_CLOCK and 6dB. */
1963 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
1964
1965 /* Block the PHY control access. */
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00001966 tg3_phydsp_write(tp, 0x8005, 0x0800);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
1968 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
1969 if (!err)
1970 break;
1971 } while (--retries);
1972
1973 err = tg3_phy_reset_chanpat(tp);
1974 if (err)
1975 return err;
1976
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00001977 tg3_phydsp_write(tp, 0x8005, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
1979 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001980 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
1982 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
1983 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
1984 /* Set Extended packet length bit for jumbo frames */
1985 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4400);
Matt Carlson859a5882010-04-05 10:19:28 +00001986 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
1988 }
1989
1990 tg3_writephy(tp, MII_TG3_CTRL, phy9_orig);
1991
1992 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
1993 reg32 &= ~0x3000;
1994 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
1995 } else if (!err)
1996 err = -EBUSY;
1997
1998 return err;
1999}
2000
2001/* This will reset the tigon3 PHY if there is no valid
2002 * link unless the FORCE argument is non-zero.
2003 */
2004static int tg3_phy_reset(struct tg3 *tp)
2005{
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002006 u32 val, cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 int err;
2008
Michael Chan60189dd2006-12-17 17:08:07 -08002009 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002010 val = tr32(GRC_MISC_CFG);
2011 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
2012 udelay(40);
2013 }
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002014 err = tg3_readphy(tp, MII_BMSR, &val);
2015 err |= tg3_readphy(tp, MII_BMSR, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 if (err != 0)
2017 return -EBUSY;
2018
Michael Chanc8e1e822006-04-29 18:55:17 -07002019 if (netif_running(tp->dev) && netif_carrier_ok(tp->dev)) {
2020 netif_carrier_off(tp->dev);
2021 tg3_link_report(tp);
2022 }
2023
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2025 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2026 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
2027 err = tg3_phy_reset_5703_4_5(tp);
2028 if (err)
2029 return err;
2030 goto out;
2031 }
2032
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002033 cpmuctrl = 0;
2034 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
2035 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
2036 cpmuctrl = tr32(TG3_CPMU_CTRL);
2037 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
2038 tw32(TG3_CPMU_CTRL,
2039 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
2040 }
2041
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 err = tg3_bmcr_reset(tp);
2043 if (err)
2044 return err;
2045
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002046 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002047 val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
2048 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002049
2050 tw32(TG3_CPMU_CTRL, cpmuctrl);
2051 }
2052
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002053 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2054 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002055 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2056 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
2057 CPMU_LSPD_1000MB_MACCLK_12_5) {
2058 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2059 udelay(40);
2060 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2061 }
2062 }
2063
Matt Carlson0a58d662011-04-05 14:22:45 +00002064 if ((tp->tg3_flags3 & TG3_FLG3_5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002065 (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
Matt Carlsonecf14102010-01-20 16:58:05 +00002066 return 0;
2067
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002068 tg3_phy_apply_otp(tp);
2069
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002070 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -08002071 tg3_phy_toggle_apd(tp, true);
2072 else
2073 tg3_phy_toggle_apd(tp, false);
2074
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075out:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002076 if (tp->phy_flags & TG3_PHYFLG_ADC_BUG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002078 tg3_phydsp_write(tp, 0x201f, 0x2aaa);
2079 tg3_phydsp_write(tp, 0x000a, 0x0323);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
2081 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002082 if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002083 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2084 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002086 if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002088 tg3_phydsp_write(tp, 0x000a, 0x310b);
2089 tg3_phydsp_write(tp, 0x201f, 0x9506);
2090 tg3_phydsp_write(tp, 0x401f, 0x14e2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002092 } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
Michael Chanc424cb22006-04-29 18:56:34 -07002093 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
2094 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002095 if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
Michael Chanc1d2a192007-01-08 19:57:20 -08002096 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2097 tg3_writephy(tp, MII_TG3_TEST1,
2098 MII_TG3_TEST1_TRIM_EN | 0x4);
2099 } else
2100 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
Michael Chanc424cb22006-04-29 18:56:34 -07002101 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
2102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 /* Set Extended packet length bit (bit 14) on all chips that */
2104 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002105 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 /* Cannot do read-modify-write on 5401 */
2107 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4c20);
Matt Carlson8f666b02009-08-28 13:58:24 +00002108 } else if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 /* Set bit 14 with read-modify-write to preserve other bits */
Matt Carlson15ee95c2011-04-20 07:57:40 +00002110 err = tg3_phy_auxctl_read(tp,
2111 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2112 if (!err)
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002113 tg3_writephy(tp, MII_TG3_AUX_CTRL, val | 0x4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 }
2115
2116 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2117 * jumbo frames transmission.
2118 */
Matt Carlson8f666b02009-08-28 13:58:24 +00002119 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002120 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002121 tg3_writephy(tp, MII_TG3_EXT_CTRL,
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002122 val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 }
2124
Michael Chan715116a2006-09-27 16:09:25 -07002125 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002126 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002127 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002128 }
2129
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002130 tg3_phy_toggle_automdix(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 tg3_phy_set_wirespeed(tp);
2132 return 0;
2133}
2134
2135static void tg3_frob_aux_power(struct tg3 *tp)
2136{
Matt Carlson683644b2011-03-09 16:58:23 +00002137 bool need_vaux = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138
Matt Carlson334355a2010-01-20 16:58:10 +00002139 /* The GPIOs do something completely different on 57765. */
2140 if ((tp->tg3_flags2 & TG3_FLG2_IS_NIC) == 0 ||
Matt Carlsona50d0792010-06-05 17:24:37 +00002141 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlson334355a2010-01-20 16:58:10 +00002142 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 return;
2144
Matt Carlson683644b2011-03-09 16:58:23 +00002145 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2146 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +00002147 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2148 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) &&
Matt Carlson683644b2011-03-09 16:58:23 +00002149 tp->pdev_peer != tp->pdev) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002150 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002152 dev_peer = pci_get_drvdata(tp->pdev_peer);
Matt Carlson683644b2011-03-09 16:58:23 +00002153
Michael Chanbc1c7562006-03-20 17:48:03 -08002154 /* remove_one() may have been run on the peer. */
Matt Carlson683644b2011-03-09 16:58:23 +00002155 if (dev_peer) {
2156 struct tg3 *tp_peer = netdev_priv(dev_peer);
2157
2158 if (tp_peer->tg3_flags & TG3_FLAG_INIT_COMPLETE)
2159 return;
2160
2161 if ((tp_peer->tg3_flags & TG3_FLAG_WOL_ENABLE) ||
2162 (tp_peer->tg3_flags & TG3_FLAG_ENABLE_ASF))
2163 need_vaux = true;
2164 }
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
Matt Carlson683644b2011-03-09 16:58:23 +00002167 if ((tp->tg3_flags & TG3_FLAG_WOL_ENABLE) ||
2168 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
2169 need_vaux = true;
2170
2171 if (need_vaux) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2173 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Michael Chanb401e9e2005-12-19 16:27:04 -08002174 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2175 (GRC_LCLCTRL_GPIO_OE0 |
2176 GRC_LCLCTRL_GPIO_OE1 |
2177 GRC_LCLCTRL_GPIO_OE2 |
2178 GRC_LCLCTRL_GPIO_OUTPUT0 |
2179 GRC_LCLCTRL_GPIO_OUTPUT1),
2180 100);
Matt Carlson8d519ab2009-04-20 06:58:01 +00002181 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2182 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -07002183 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2184 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2185 GRC_LCLCTRL_GPIO_OE1 |
2186 GRC_LCLCTRL_GPIO_OE2 |
2187 GRC_LCLCTRL_GPIO_OUTPUT0 |
2188 GRC_LCLCTRL_GPIO_OUTPUT1 |
2189 tp->grc_local_ctrl;
2190 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2191
2192 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2193 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2194
2195 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2196 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 } else {
2198 u32 no_gpio2;
Michael Chandc56b7d2005-12-19 16:26:28 -08002199 u32 grc_local_ctrl = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
Michael Chandc56b7d2005-12-19 16:26:28 -08002201 /* Workaround to prevent overdrawing Amps. */
2202 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2203 ASIC_REV_5714) {
2204 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chanb401e9e2005-12-19 16:27:04 -08002205 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2206 grc_local_ctrl, 100);
Michael Chandc56b7d2005-12-19 16:26:28 -08002207 }
2208
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 /* On 5753 and variants, GPIO2 cannot be used. */
2210 no_gpio2 = tp->nic_sram_data_cfg &
2211 NIC_SRAM_DATA_CFG_NO_GPIO2;
2212
Michael Chandc56b7d2005-12-19 16:26:28 -08002213 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 GRC_LCLCTRL_GPIO_OE1 |
2215 GRC_LCLCTRL_GPIO_OE2 |
2216 GRC_LCLCTRL_GPIO_OUTPUT1 |
2217 GRC_LCLCTRL_GPIO_OUTPUT2;
2218 if (no_gpio2) {
2219 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2220 GRC_LCLCTRL_GPIO_OUTPUT2);
2221 }
Michael Chanb401e9e2005-12-19 16:27:04 -08002222 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2223 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224
2225 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2226
Michael Chanb401e9e2005-12-19 16:27:04 -08002227 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2228 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
2230 if (!no_gpio2) {
2231 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chanb401e9e2005-12-19 16:27:04 -08002232 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2233 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 }
2235 }
2236 } else {
2237 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
2238 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
Michael Chanb401e9e2005-12-19 16:27:04 -08002239 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2240 (GRC_LCLCTRL_GPIO_OE1 |
2241 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242
Michael Chanb401e9e2005-12-19 16:27:04 -08002243 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2244 GRC_LCLCTRL_GPIO_OE1, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245
Michael Chanb401e9e2005-12-19 16:27:04 -08002246 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2247 (GRC_LCLCTRL_GPIO_OE1 |
2248 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 }
2250 }
2251}
2252
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002253static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2254{
2255 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2256 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00002257 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002258 if (speed != SPEED_10)
2259 return 1;
2260 } else if (speed == SPEED_10)
2261 return 1;
2262
2263 return 0;
2264}
2265
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266static int tg3_setup_phy(struct tg3 *, int);
2267
2268#define RESET_KIND_SHUTDOWN 0
2269#define RESET_KIND_INIT 1
2270#define RESET_KIND_SUSPEND 2
2271
2272static void tg3_write_sig_post_reset(struct tg3 *, int);
2273static int tg3_halt_cpu(struct tg3 *, u32);
2274
Matt Carlson0a459aa2008-11-03 16:54:15 -08002275static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002276{
Matt Carlsonce057f02007-11-12 21:08:03 -08002277 u32 val;
2278
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002279 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Michael Chan51297242007-02-13 12:17:57 -08002280 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2281 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2282 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2283
2284 sg_dig_ctrl |=
2285 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2286 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2287 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2288 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002289 return;
Michael Chan51297242007-02-13 12:17:57 -08002290 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002291
Michael Chan60189dd2006-12-17 17:08:07 -08002292 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002293 tg3_bmcr_reset(tp);
2294 val = tr32(GRC_MISC_CFG);
2295 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2296 udelay(40);
2297 return;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002298 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson0e5f7842009-11-02 14:26:38 +00002299 u32 phytest;
2300 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2301 u32 phy;
2302
2303 tg3_writephy(tp, MII_ADVERTISE, 0);
2304 tg3_writephy(tp, MII_BMCR,
2305 BMCR_ANENABLE | BMCR_ANRESTART);
2306
2307 tg3_writephy(tp, MII_TG3_FET_TEST,
2308 phytest | MII_TG3_FET_SHADOW_EN);
2309 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
2310 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
2311 tg3_writephy(tp,
2312 MII_TG3_FET_SHDW_AUXMODE4,
2313 phy);
2314 }
2315 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2316 }
2317 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002318 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07002319 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2320 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002321
2322 tg3_writephy(tp, MII_TG3_AUX_CTRL,
2323 MII_TG3_AUXCTL_SHDWSEL_PWRCTL |
2324 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2325 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2326 MII_TG3_AUXCTL_PCTL_VREG_11V);
Michael Chan715116a2006-09-27 16:09:25 -07002327 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002328
Michael Chan15c3b692006-03-22 01:06:52 -08002329 /* The PHY should not be powered down on some chips because
2330 * of bugs.
2331 */
2332 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2333 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2334 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002335 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Michael Chan15c3b692006-03-22 01:06:52 -08002336 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08002337
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002338 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2339 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002340 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2341 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2342 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2343 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2344 }
2345
Michael Chan15c3b692006-03-22 01:06:52 -08002346 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2347}
2348
Matt Carlson3f007892008-11-03 16:51:36 -08002349/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002350static int tg3_nvram_lock(struct tg3 *tp)
2351{
2352 if (tp->tg3_flags & TG3_FLAG_NVRAM) {
2353 int i;
2354
2355 if (tp->nvram_lock_cnt == 0) {
2356 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2357 for (i = 0; i < 8000; i++) {
2358 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2359 break;
2360 udelay(20);
2361 }
2362 if (i == 8000) {
2363 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2364 return -ENODEV;
2365 }
2366 }
2367 tp->nvram_lock_cnt++;
2368 }
2369 return 0;
2370}
2371
2372/* tp->lock is held. */
2373static void tg3_nvram_unlock(struct tg3 *tp)
2374{
2375 if (tp->tg3_flags & TG3_FLAG_NVRAM) {
2376 if (tp->nvram_lock_cnt > 0)
2377 tp->nvram_lock_cnt--;
2378 if (tp->nvram_lock_cnt == 0)
2379 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
2380 }
2381}
2382
2383/* tp->lock is held. */
2384static void tg3_enable_nvram_access(struct tg3 *tp)
2385{
2386 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Matt Carlsonf66a29b2009-11-13 13:03:36 +00002387 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002388 u32 nvaccess = tr32(NVRAM_ACCESS);
2389
2390 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
2391 }
2392}
2393
2394/* tp->lock is held. */
2395static void tg3_disable_nvram_access(struct tg3 *tp)
2396{
2397 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Matt Carlsonf66a29b2009-11-13 13:03:36 +00002398 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002399 u32 nvaccess = tr32(NVRAM_ACCESS);
2400
2401 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
2402 }
2403}
2404
2405static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
2406 u32 offset, u32 *val)
2407{
2408 u32 tmp;
2409 int i;
2410
2411 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
2412 return -EINVAL;
2413
2414 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
2415 EEPROM_ADDR_DEVID_MASK |
2416 EEPROM_ADDR_READ);
2417 tw32(GRC_EEPROM_ADDR,
2418 tmp |
2419 (0 << EEPROM_ADDR_DEVID_SHIFT) |
2420 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
2421 EEPROM_ADDR_ADDR_MASK) |
2422 EEPROM_ADDR_READ | EEPROM_ADDR_START);
2423
2424 for (i = 0; i < 1000; i++) {
2425 tmp = tr32(GRC_EEPROM_ADDR);
2426
2427 if (tmp & EEPROM_ADDR_COMPLETE)
2428 break;
2429 msleep(1);
2430 }
2431 if (!(tmp & EEPROM_ADDR_COMPLETE))
2432 return -EBUSY;
2433
Matt Carlson62cedd12009-04-20 14:52:29 -07002434 tmp = tr32(GRC_EEPROM_DATA);
2435
2436 /*
2437 * The data will always be opposite the native endian
2438 * format. Perform a blind byteswap to compensate.
2439 */
2440 *val = swab32(tmp);
2441
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002442 return 0;
2443}
2444
2445#define NVRAM_CMD_TIMEOUT 10000
2446
2447static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
2448{
2449 int i;
2450
2451 tw32(NVRAM_CMD, nvram_cmd);
2452 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
2453 udelay(10);
2454 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
2455 udelay(10);
2456 break;
2457 }
2458 }
2459
2460 if (i == NVRAM_CMD_TIMEOUT)
2461 return -EBUSY;
2462
2463 return 0;
2464}
2465
2466static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
2467{
2468 if ((tp->tg3_flags & TG3_FLAG_NVRAM) &&
2469 (tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) &&
2470 (tp->tg3_flags2 & TG3_FLG2_FLASH) &&
2471 !(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM_ADDR_TRANS) &&
2472 (tp->nvram_jedecnum == JEDEC_ATMEL))
2473
2474 addr = ((addr / tp->nvram_pagesize) <<
2475 ATMEL_AT45DB0X1B_PAGE_POS) +
2476 (addr % tp->nvram_pagesize);
2477
2478 return addr;
2479}
2480
2481static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
2482{
2483 if ((tp->tg3_flags & TG3_FLAG_NVRAM) &&
2484 (tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) &&
2485 (tp->tg3_flags2 & TG3_FLG2_FLASH) &&
2486 !(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM_ADDR_TRANS) &&
2487 (tp->nvram_jedecnum == JEDEC_ATMEL))
2488
2489 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
2490 tp->nvram_pagesize) +
2491 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
2492
2493 return addr;
2494}
2495
Matt Carlsone4f34112009-02-25 14:25:00 +00002496/* NOTE: Data read in from NVRAM is byteswapped according to
2497 * the byteswapping settings for all other register accesses.
2498 * tg3 devices are BE devices, so on a BE machine, the data
2499 * returned will be exactly as it is seen in NVRAM. On a LE
2500 * machine, the 32-bit value will be byteswapped.
2501 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002502static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
2503{
2504 int ret;
2505
2506 if (!(tp->tg3_flags & TG3_FLAG_NVRAM))
2507 return tg3_nvram_read_using_eeprom(tp, offset, val);
2508
2509 offset = tg3_nvram_phys_addr(tp, offset);
2510
2511 if (offset > NVRAM_ADDR_MSK)
2512 return -EINVAL;
2513
2514 ret = tg3_nvram_lock(tp);
2515 if (ret)
2516 return ret;
2517
2518 tg3_enable_nvram_access(tp);
2519
2520 tw32(NVRAM_ADDR, offset);
2521 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
2522 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
2523
2524 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00002525 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002526
2527 tg3_disable_nvram_access(tp);
2528
2529 tg3_nvram_unlock(tp);
2530
2531 return ret;
2532}
2533
Matt Carlsona9dc5292009-02-25 14:25:30 +00002534/* Ensures NVRAM data is in bytestream format. */
2535static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002536{
2537 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00002538 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002539 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00002540 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002541 return res;
2542}
2543
2544/* tp->lock is held. */
Matt Carlson3f007892008-11-03 16:51:36 -08002545static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
2546{
2547 u32 addr_high, addr_low;
2548 int i;
2549
2550 addr_high = ((tp->dev->dev_addr[0] << 8) |
2551 tp->dev->dev_addr[1]);
2552 addr_low = ((tp->dev->dev_addr[2] << 24) |
2553 (tp->dev->dev_addr[3] << 16) |
2554 (tp->dev->dev_addr[4] << 8) |
2555 (tp->dev->dev_addr[5] << 0));
2556 for (i = 0; i < 4; i++) {
2557 if (i == 1 && skip_mac_1)
2558 continue;
2559 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
2560 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
2561 }
2562
2563 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2564 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2565 for (i = 0; i < 12; i++) {
2566 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
2567 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
2568 }
2569 }
2570
2571 addr_high = (tp->dev->dev_addr[0] +
2572 tp->dev->dev_addr[1] +
2573 tp->dev->dev_addr[2] +
2574 tp->dev->dev_addr[3] +
2575 tp->dev->dev_addr[4] +
2576 tp->dev->dev_addr[5]) &
2577 TX_BACKOFF_SEED_MASK;
2578 tw32(MAC_TX_BACKOFF_SEED, addr_high);
2579}
2580
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002581static void tg3_enable_register_access(struct tg3 *tp)
2582{
2583 /*
2584 * Make sure register accesses (indirect or otherwise) will function
2585 * correctly.
2586 */
2587 pci_write_config_dword(tp->pdev,
2588 TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
2589}
2590
2591static int tg3_power_up(struct tg3 *tp)
2592{
2593 tg3_enable_register_access(tp);
2594
2595 pci_set_power_state(tp->pdev, PCI_D0);
2596
2597 /* Switch out of Vaux if it is a NIC */
2598 if (tp->tg3_flags2 & TG3_FLG2_IS_NIC)
2599 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl, 100);
2600
2601 return 0;
2602}
2603
2604static int tg3_power_down_prepare(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605{
2606 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002607 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002609 tg3_enable_register_access(tp);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002610
2611 /* Restore the CLKREQ setting. */
2612 if (tp->tg3_flags3 & TG3_FLG3_CLKREQ_BUG) {
2613 u16 lnkctl;
2614
2615 pci_read_config_word(tp->pdev,
2616 tp->pcie_cap + PCI_EXP_LNKCTL,
2617 &lnkctl);
2618 lnkctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
2619 pci_write_config_word(tp->pdev,
2620 tp->pcie_cap + PCI_EXP_LNKCTL,
2621 lnkctl);
2622 }
2623
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
2625 tw32(TG3PCI_MISC_HOST_CTRL,
2626 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
2627
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002628 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002629 (tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
2630
Matt Carlsondd477002008-05-25 23:45:58 -07002631 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08002632 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002633 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson80096062010-08-02 11:26:06 +00002634 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002635 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002636 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002637
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002638 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002639
Matt Carlson80096062010-08-02 11:26:06 +00002640 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002641
2642 tp->link_config.orig_speed = phydev->speed;
2643 tp->link_config.orig_duplex = phydev->duplex;
2644 tp->link_config.orig_autoneg = phydev->autoneg;
2645 tp->link_config.orig_advertising = phydev->advertising;
2646
2647 advertising = ADVERTISED_TP |
2648 ADVERTISED_Pause |
2649 ADVERTISED_Autoneg |
2650 ADVERTISED_10baseT_Half;
2651
2652 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002653 device_should_wake) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002654 if (tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB)
2655 advertising |=
2656 ADVERTISED_100baseT_Half |
2657 ADVERTISED_100baseT_Full |
2658 ADVERTISED_10baseT_Full;
2659 else
2660 advertising |= ADVERTISED_10baseT_Full;
2661 }
2662
2663 phydev->advertising = advertising;
2664
2665 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002666
2667 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00002668 if (phyid != PHY_ID_BCMAC131) {
2669 phyid &= PHY_BCM_OUI_MASK;
2670 if (phyid == PHY_BCM_OUI_1 ||
2671 phyid == PHY_BCM_OUI_2 ||
2672 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08002673 do_low_power = true;
2674 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002675 }
Matt Carlsondd477002008-05-25 23:45:58 -07002676 } else {
Matt Carlson20232762008-12-21 20:18:56 -08002677 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002678
Matt Carlson80096062010-08-02 11:26:06 +00002679 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
2680 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07002681 tp->link_config.orig_speed = tp->link_config.speed;
2682 tp->link_config.orig_duplex = tp->link_config.duplex;
2683 tp->link_config.orig_autoneg = tp->link_config.autoneg;
2684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002686 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Matt Carlsondd477002008-05-25 23:45:58 -07002687 tp->link_config.speed = SPEED_10;
2688 tp->link_config.duplex = DUPLEX_HALF;
2689 tp->link_config.autoneg = AUTONEG_ENABLE;
2690 tg3_setup_phy(tp, 0);
2691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 }
2693
Michael Chanb5d37722006-09-27 16:06:21 -07002694 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
2695 u32 val;
2696
2697 val = tr32(GRC_VCPU_EXT_CTRL);
2698 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
2699 } else if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08002700 int i;
2701 u32 val;
2702
2703 for (i = 0; i < 200; i++) {
2704 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
2705 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
2706 break;
2707 msleep(1);
2708 }
2709 }
Gary Zambranoa85feb82007-05-05 11:52:19 -07002710 if (tp->tg3_flags & TG3_FLAG_WOL_CAP)
2711 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
2712 WOL_DRV_STATE_SHUTDOWN |
2713 WOL_DRV_WOL |
2714 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08002715
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002716 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 u32 mac_mode;
2718
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002719 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08002720 if (do_low_power) {
Matt Carlsondd477002008-05-25 23:45:58 -07002721 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x5a);
2722 udelay(40);
2723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002725 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07002726 mac_mode = MAC_MODE_PORT_MODE_GMII;
2727 else
2728 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002730 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
2731 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2732 ASIC_REV_5700) {
2733 u32 speed = (tp->tg3_flags &
2734 TG3_FLAG_WOL_SPEED_100MB) ?
2735 SPEED_100 : SPEED_10;
2736 if (tg3_5700_link_polarity(tp, speed))
2737 mac_mode |= MAC_MODE_LINK_POLARITY;
2738 else
2739 mac_mode &= ~MAC_MODE_LINK_POLARITY;
2740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 } else {
2742 mac_mode = MAC_MODE_PORT_MODE_TBI;
2743 }
2744
John W. Linvillecbf46852005-04-21 17:01:29 -07002745 if (!(tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 tw32(MAC_LED_CTRL, tp->led_ctrl);
2747
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002748 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
2749 if (((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
2750 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) &&
2751 ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
2752 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)))
2753 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754
Matt Carlsond2394e6b2010-11-24 08:31:47 +00002755 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
2756 mac_mode |= MAC_MODE_APE_TX_EN |
2757 MAC_MODE_APE_RX_EN |
2758 MAC_MODE_TDE_ENABLE;
Matt Carlson3bda1252008-08-15 14:08:22 -07002759
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 tw32_f(MAC_MODE, mac_mode);
2761 udelay(100);
2762
2763 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
2764 udelay(10);
2765 }
2766
2767 if (!(tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB) &&
2768 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2769 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
2770 u32 base_val;
2771
2772 base_val = tp->pci_clock_ctrl;
2773 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
2774 CLOCK_CTRL_TXCLK_DISABLE);
2775
Michael Chanb401e9e2005-12-19 16:27:04 -08002776 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
2777 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Michael Chand7b0a852007-02-13 12:17:38 -08002778 } else if ((tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
Matt Carlson795d01c2007-10-07 23:28:17 -07002779 (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
Michael Chand7b0a852007-02-13 12:17:38 -08002780 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)) {
Michael Chan4cf78e42005-07-25 12:29:19 -07002781 /* do nothing */
Michael Chan85e94ce2005-04-21 17:05:28 -07002782 } else if (!((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF))) {
2784 u32 newbits1, newbits2;
2785
2786 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2787 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2788 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
2789 CLOCK_CTRL_TXCLK_DISABLE |
2790 CLOCK_CTRL_ALTCLK);
2791 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2792 } else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
2793 newbits1 = CLOCK_CTRL_625_CORE;
2794 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
2795 } else {
2796 newbits1 = CLOCK_CTRL_ALTCLK;
2797 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2798 }
2799
Michael Chanb401e9e2005-12-19 16:27:04 -08002800 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
2801 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802
Michael Chanb401e9e2005-12-19 16:27:04 -08002803 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
2804 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805
2806 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
2807 u32 newbits3;
2808
2809 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2810 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2811 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
2812 CLOCK_CTRL_TXCLK_DISABLE |
2813 CLOCK_CTRL_44MHZ_CORE);
2814 } else {
2815 newbits3 = CLOCK_CTRL_44MHZ_CORE;
2816 }
2817
Michael Chanb401e9e2005-12-19 16:27:04 -08002818 tw32_wait_f(TG3PCI_CLOCK_CTRL,
2819 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 }
2821 }
2822
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002823 if (!(device_should_wake) &&
Matt Carlson22435842008-11-21 17:21:13 -08002824 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08002825 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08002826
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 tg3_frob_aux_power(tp);
2828
2829 /* Workaround for unstable PLL clock */
2830 if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
2831 (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
2832 u32 val = tr32(0x7d00);
2833
2834 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
2835 tw32(0x7d00, val);
Michael Chan6921d202005-12-13 21:15:53 -08002836 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08002837 int err;
2838
2839 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08002841 if (!err)
2842 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08002843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 }
2845
Michael Chanbbadf502006-04-06 21:46:34 -07002846 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
2847
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 return 0;
2849}
2850
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002851static void tg3_power_down(struct tg3 *tp)
2852{
2853 tg3_power_down_prepare(tp);
2854
2855 pci_wake_from_d3(tp->pdev, tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
2856 pci_set_power_state(tp->pdev, PCI_D3hot);
2857}
2858
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
2860{
2861 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
2862 case MII_TG3_AUX_STAT_10HALF:
2863 *speed = SPEED_10;
2864 *duplex = DUPLEX_HALF;
2865 break;
2866
2867 case MII_TG3_AUX_STAT_10FULL:
2868 *speed = SPEED_10;
2869 *duplex = DUPLEX_FULL;
2870 break;
2871
2872 case MII_TG3_AUX_STAT_100HALF:
2873 *speed = SPEED_100;
2874 *duplex = DUPLEX_HALF;
2875 break;
2876
2877 case MII_TG3_AUX_STAT_100FULL:
2878 *speed = SPEED_100;
2879 *duplex = DUPLEX_FULL;
2880 break;
2881
2882 case MII_TG3_AUX_STAT_1000HALF:
2883 *speed = SPEED_1000;
2884 *duplex = DUPLEX_HALF;
2885 break;
2886
2887 case MII_TG3_AUX_STAT_1000FULL:
2888 *speed = SPEED_1000;
2889 *duplex = DUPLEX_FULL;
2890 break;
2891
2892 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002893 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07002894 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
2895 SPEED_10;
2896 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
2897 DUPLEX_HALF;
2898 break;
2899 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 *speed = SPEED_INVALID;
2901 *duplex = DUPLEX_INVALID;
2902 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904}
2905
2906static void tg3_phy_copper_begin(struct tg3 *tp)
2907{
2908 u32 new_adv;
2909 int i;
2910
Matt Carlson80096062010-08-02 11:26:06 +00002911 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 /* Entering low power mode. Disable gigabit and
2913 * 100baseT advertisements.
2914 */
2915 tg3_writephy(tp, MII_TG3_CTRL, 0);
2916
2917 new_adv = (ADVERTISE_10HALF | ADVERTISE_10FULL |
2918 ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
2919 if (tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB)
2920 new_adv |= (ADVERTISE_100HALF | ADVERTISE_100FULL);
2921
2922 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2923 } else if (tp->link_config.speed == SPEED_INVALID) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002924 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 tp->link_config.advertising &=
2926 ~(ADVERTISED_1000baseT_Half |
2927 ADVERTISED_1000baseT_Full);
2928
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002929 new_adv = ADVERTISE_CSMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 if (tp->link_config.advertising & ADVERTISED_10baseT_Half)
2931 new_adv |= ADVERTISE_10HALF;
2932 if (tp->link_config.advertising & ADVERTISED_10baseT_Full)
2933 new_adv |= ADVERTISE_10FULL;
2934 if (tp->link_config.advertising & ADVERTISED_100baseT_Half)
2935 new_adv |= ADVERTISE_100HALF;
2936 if (tp->link_config.advertising & ADVERTISED_100baseT_Full)
2937 new_adv |= ADVERTISE_100FULL;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002938
2939 new_adv |= tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2940
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2942
2943 if (tp->link_config.advertising &
2944 (ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full)) {
2945 new_adv = 0;
2946 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
2947 new_adv |= MII_TG3_CTRL_ADV_1000_HALF;
2948 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
2949 new_adv |= MII_TG3_CTRL_ADV_1000_FULL;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002950 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2952 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0))
2953 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2954 MII_TG3_CTRL_ENABLE_AS_MASTER);
2955 tg3_writephy(tp, MII_TG3_CTRL, new_adv);
2956 } else {
2957 tg3_writephy(tp, MII_TG3_CTRL, 0);
2958 }
2959 } else {
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002960 new_adv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2961 new_adv |= ADVERTISE_CSMA;
2962
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 /* Asking for a specific link mode. */
2964 if (tp->link_config.speed == SPEED_1000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2966
2967 if (tp->link_config.duplex == DUPLEX_FULL)
2968 new_adv = MII_TG3_CTRL_ADV_1000_FULL;
2969 else
2970 new_adv = MII_TG3_CTRL_ADV_1000_HALF;
2971 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2972 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
2973 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2974 MII_TG3_CTRL_ENABLE_AS_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 if (tp->link_config.speed == SPEED_100) {
2977 if (tp->link_config.duplex == DUPLEX_FULL)
2978 new_adv |= ADVERTISE_100FULL;
2979 else
2980 new_adv |= ADVERTISE_100HALF;
2981 } else {
2982 if (tp->link_config.duplex == DUPLEX_FULL)
2983 new_adv |= ADVERTISE_10FULL;
2984 else
2985 new_adv |= ADVERTISE_10HALF;
2986 }
2987 tg3_writephy(tp, MII_ADVERTISE, new_adv);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002988
2989 new_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002991
2992 tg3_writephy(tp, MII_TG3_CTRL, new_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 }
2994
Matt Carlson52b02d02010-10-14 10:37:41 +00002995 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
Matt Carlsona6b68da2010-12-06 08:28:52 +00002996 u32 val;
Matt Carlson52b02d02010-10-14 10:37:41 +00002997
2998 tw32(TG3_CPMU_EEE_MODE,
2999 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
3000
3001 /* Enable SM_DSP clock and tx 6dB coding. */
3002 val = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
3003 MII_TG3_AUXCTL_ACTL_SMDSP_ENA |
3004 MII_TG3_AUXCTL_ACTL_TX_6DB;
3005 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
3006
Matt Carlson21a00ab2011-01-25 15:58:55 +00003007 switch (GET_ASIC_REV(tp->pci_chip_rev_id)) {
3008 case ASIC_REV_5717:
3009 case ASIC_REV_57765:
3010 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
3011 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
3012 MII_TG3_DSP_CH34TP2_HIBW01);
3013 /* Fall through */
3014 case ASIC_REV_5719:
3015 val = MII_TG3_DSP_TAP26_ALNOKO |
3016 MII_TG3_DSP_TAP26_RMRXSTO |
3017 MII_TG3_DSP_TAP26_OPCSINPT;
3018 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
3019 }
Matt Carlson52b02d02010-10-14 10:37:41 +00003020
Matt Carlsona6b68da2010-12-06 08:28:52 +00003021 val = 0;
Matt Carlson52b02d02010-10-14 10:37:41 +00003022 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3023 /* Advertise 100-BaseTX EEE ability */
3024 if (tp->link_config.advertising &
Matt Carlson3110f5f52010-12-06 08:28:50 +00003025 ADVERTISED_100baseT_Full)
3026 val |= MDIO_AN_EEE_ADV_100TX;
Matt Carlson52b02d02010-10-14 10:37:41 +00003027 /* Advertise 1000-BaseT EEE ability */
3028 if (tp->link_config.advertising &
Matt Carlson3110f5f52010-12-06 08:28:50 +00003029 ADVERTISED_1000baseT_Full)
3030 val |= MDIO_AN_EEE_ADV_1000T;
Matt Carlson52b02d02010-10-14 10:37:41 +00003031 }
Matt Carlson3110f5f52010-12-06 08:28:50 +00003032 tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlson52b02d02010-10-14 10:37:41 +00003033
3034 /* Turn off SM_DSP clock. */
3035 val = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
3036 MII_TG3_AUXCTL_ACTL_TX_6DB;
3037 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
3038 }
3039
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 if (tp->link_config.autoneg == AUTONEG_DISABLE &&
3041 tp->link_config.speed != SPEED_INVALID) {
3042 u32 bmcr, orig_bmcr;
3043
3044 tp->link_config.active_speed = tp->link_config.speed;
3045 tp->link_config.active_duplex = tp->link_config.duplex;
3046
3047 bmcr = 0;
3048 switch (tp->link_config.speed) {
3049 default:
3050 case SPEED_10:
3051 break;
3052
3053 case SPEED_100:
3054 bmcr |= BMCR_SPEED100;
3055 break;
3056
3057 case SPEED_1000:
3058 bmcr |= TG3_BMCR_SPEED1000;
3059 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061
3062 if (tp->link_config.duplex == DUPLEX_FULL)
3063 bmcr |= BMCR_FULLDPLX;
3064
3065 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
3066 (bmcr != orig_bmcr)) {
3067 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
3068 for (i = 0; i < 1500; i++) {
3069 u32 tmp;
3070
3071 udelay(10);
3072 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
3073 tg3_readphy(tp, MII_BMSR, &tmp))
3074 continue;
3075 if (!(tmp & BMSR_LSTATUS)) {
3076 udelay(40);
3077 break;
3078 }
3079 }
3080 tg3_writephy(tp, MII_BMCR, bmcr);
3081 udelay(40);
3082 }
3083 } else {
3084 tg3_writephy(tp, MII_BMCR,
3085 BMCR_ANENABLE | BMCR_ANRESTART);
3086 }
3087}
3088
3089static int tg3_init_5401phy_dsp(struct tg3 *tp)
3090{
3091 int err;
3092
3093 /* Turn off tap power management. */
3094 /* Set Extended packet length bit */
3095 err = tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4c20);
3096
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00003097 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
3098 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
3099 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
3100 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
3101 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102
3103 udelay(40);
3104
3105 return err;
3106}
3107
Michael Chan3600d912006-12-07 00:21:48 -08003108static int tg3_copper_is_advertising_all(struct tg3 *tp, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109{
Michael Chan3600d912006-12-07 00:21:48 -08003110 u32 adv_reg, all_mask = 0;
3111
3112 if (mask & ADVERTISED_10baseT_Half)
3113 all_mask |= ADVERTISE_10HALF;
3114 if (mask & ADVERTISED_10baseT_Full)
3115 all_mask |= ADVERTISE_10FULL;
3116 if (mask & ADVERTISED_100baseT_Half)
3117 all_mask |= ADVERTISE_100HALF;
3118 if (mask & ADVERTISED_100baseT_Full)
3119 all_mask |= ADVERTISE_100FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120
3121 if (tg3_readphy(tp, MII_ADVERTISE, &adv_reg))
3122 return 0;
3123
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 if ((adv_reg & all_mask) != all_mask)
3125 return 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003126 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 u32 tg3_ctrl;
3128
Michael Chan3600d912006-12-07 00:21:48 -08003129 all_mask = 0;
3130 if (mask & ADVERTISED_1000baseT_Half)
3131 all_mask |= ADVERTISE_1000HALF;
3132 if (mask & ADVERTISED_1000baseT_Full)
3133 all_mask |= ADVERTISE_1000FULL;
3134
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 if (tg3_readphy(tp, MII_TG3_CTRL, &tg3_ctrl))
3136 return 0;
3137
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 if ((tg3_ctrl & all_mask) != all_mask)
3139 return 0;
3140 }
3141 return 1;
3142}
3143
Matt Carlsonef167e22007-12-20 20:10:01 -08003144static int tg3_adv_1000T_flowctrl_ok(struct tg3 *tp, u32 *lcladv, u32 *rmtadv)
3145{
3146 u32 curadv, reqadv;
3147
3148 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
3149 return 1;
3150
3151 curadv = *lcladv & (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
3152 reqadv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
3153
3154 if (tp->link_config.active_duplex == DUPLEX_FULL) {
3155 if (curadv != reqadv)
3156 return 0;
3157
3158 if (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG)
3159 tg3_readphy(tp, MII_LPA, rmtadv);
3160 } else {
3161 /* Reprogram the advertisement register, even if it
3162 * does not affect the current link. If the link
3163 * gets renegotiated in the future, we can save an
3164 * additional renegotiation cycle by advertising
3165 * it correctly in the first place.
3166 */
3167 if (curadv != reqadv) {
3168 *lcladv &= ~(ADVERTISE_PAUSE_CAP |
3169 ADVERTISE_PAUSE_ASYM);
3170 tg3_writephy(tp, MII_ADVERTISE, *lcladv | reqadv);
3171 }
3172 }
3173
3174 return 1;
3175}
3176
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
3178{
3179 int current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003180 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08003181 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 u16 current_speed;
3183 u8 current_duplex;
3184 int i, err;
3185
3186 tw32(MAC_EVENT, 0);
3187
3188 tw32_f(MAC_STATUS,
3189 (MAC_STATUS_SYNC_CHANGED |
3190 MAC_STATUS_CFG_CHANGED |
3191 MAC_STATUS_MI_COMPLETION |
3192 MAC_STATUS_LNKSTATE_CHANGED));
3193 udelay(40);
3194
Matt Carlson8ef21422008-05-02 16:47:53 -07003195 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
3196 tw32_f(MAC_MI_MODE,
3197 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
3198 udelay(80);
3199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200
3201 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x02);
3202
3203 /* Some third-party PHYs need to be reset on link going
3204 * down.
3205 */
3206 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3207 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
3208 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
3209 netif_carrier_ok(tp->dev)) {
3210 tg3_readphy(tp, MII_BMSR, &bmsr);
3211 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3212 !(bmsr & BMSR_LSTATUS))
3213 force_reset = 1;
3214 }
3215 if (force_reset)
3216 tg3_phy_reset(tp);
3217
Matt Carlson79eb6902010-02-17 15:17:03 +00003218 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 tg3_readphy(tp, MII_BMSR, &bmsr);
3220 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
3221 !(tp->tg3_flags & TG3_FLAG_INIT_COMPLETE))
3222 bmsr = 0;
3223
3224 if (!(bmsr & BMSR_LSTATUS)) {
3225 err = tg3_init_5401phy_dsp(tp);
3226 if (err)
3227 return err;
3228
3229 tg3_readphy(tp, MII_BMSR, &bmsr);
3230 for (i = 0; i < 1000; i++) {
3231 udelay(10);
3232 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3233 (bmsr & BMSR_LSTATUS)) {
3234 udelay(40);
3235 break;
3236 }
3237 }
3238
Matt Carlson79eb6902010-02-17 15:17:03 +00003239 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
3240 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 !(bmsr & BMSR_LSTATUS) &&
3242 tp->link_config.active_speed == SPEED_1000) {
3243 err = tg3_phy_reset(tp);
3244 if (!err)
3245 err = tg3_init_5401phy_dsp(tp);
3246 if (err)
3247 return err;
3248 }
3249 }
3250 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3251 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
3252 /* 5701 {A0,B0} CRC bug workaround */
3253 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00003254 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
3255 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
3256 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 }
3258
3259 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003260 tg3_readphy(tp, MII_TG3_ISTAT, &val);
3261 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003263 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003265 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 tg3_writephy(tp, MII_TG3_IMASK, ~0);
3267
3268 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3269 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3270 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
3271 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3272 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
3273 else
3274 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
3275 }
3276
3277 current_link_up = 0;
3278 current_speed = SPEED_INVALID;
3279 current_duplex = DUPLEX_INVALID;
3280
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003281 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Matt Carlson15ee95c2011-04-20 07:57:40 +00003282 err = tg3_phy_auxctl_read(tp,
3283 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
3284 &val);
3285 if (!err && !(val & (1 << 10))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 val |= (1 << 10);
3287 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
3288 goto relink;
3289 }
3290 }
3291
3292 bmsr = 0;
3293 for (i = 0; i < 100; i++) {
3294 tg3_readphy(tp, MII_BMSR, &bmsr);
3295 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3296 (bmsr & BMSR_LSTATUS))
3297 break;
3298 udelay(40);
3299 }
3300
3301 if (bmsr & BMSR_LSTATUS) {
3302 u32 aux_stat, bmcr;
3303
3304 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
3305 for (i = 0; i < 2000; i++) {
3306 udelay(10);
3307 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
3308 aux_stat)
3309 break;
3310 }
3311
3312 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
3313 &current_speed,
3314 &current_duplex);
3315
3316 bmcr = 0;
3317 for (i = 0; i < 200; i++) {
3318 tg3_readphy(tp, MII_BMCR, &bmcr);
3319 if (tg3_readphy(tp, MII_BMCR, &bmcr))
3320 continue;
3321 if (bmcr && bmcr != 0x7fff)
3322 break;
3323 udelay(10);
3324 }
3325
Matt Carlsonef167e22007-12-20 20:10:01 -08003326 lcl_adv = 0;
3327 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328
Matt Carlsonef167e22007-12-20 20:10:01 -08003329 tp->link_config.active_speed = current_speed;
3330 tp->link_config.active_duplex = current_duplex;
3331
3332 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3333 if ((bmcr & BMCR_ANENABLE) &&
3334 tg3_copper_is_advertising_all(tp,
3335 tp->link_config.advertising)) {
3336 if (tg3_adv_1000T_flowctrl_ok(tp, &lcl_adv,
3337 &rmt_adv))
3338 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 }
3340 } else {
3341 if (!(bmcr & BMCR_ANENABLE) &&
3342 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08003343 tp->link_config.duplex == current_duplex &&
3344 tp->link_config.flowctrl ==
3345 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 }
3348 }
3349
Matt Carlsonef167e22007-12-20 20:10:01 -08003350 if (current_link_up == 1 &&
3351 tp->link_config.active_duplex == DUPLEX_FULL)
3352 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 }
3354
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355relink:
Matt Carlson80096062010-08-02 11:26:06 +00003356 if (current_link_up == 0 || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 tg3_phy_copper_begin(tp);
3358
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003359 tg3_readphy(tp, MII_BMSR, &bmsr);
3360 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3361 (bmsr & BMSR_LSTATUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 current_link_up = 1;
3363 }
3364
3365 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
3366 if (current_link_up == 1) {
3367 if (tp->link_config.active_speed == SPEED_100 ||
3368 tp->link_config.active_speed == SPEED_10)
3369 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3370 else
3371 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003372 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00003373 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3374 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
3376
3377 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
3378 if (tp->link_config.active_duplex == DUPLEX_HALF)
3379 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
3380
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003382 if (current_link_up == 1 &&
3383 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003385 else
3386 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 }
3388
3389 /* ??? Without this setting Netgear GA302T PHY does not
3390 * ??? send/receive packets...
3391 */
Matt Carlson79eb6902010-02-17 15:17:03 +00003392 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
3394 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
3395 tw32_f(MAC_MI_MODE, tp->mi_mode);
3396 udelay(80);
3397 }
3398
3399 tw32_f(MAC_MODE, tp->mac_mode);
3400 udelay(40);
3401
Matt Carlson52b02d02010-10-14 10:37:41 +00003402 tg3_phy_eee_adjust(tp, current_link_up);
3403
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 if (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) {
3405 /* Polled via timer. */
3406 tw32_f(MAC_EVENT, 0);
3407 } else {
3408 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
3409 }
3410 udelay(40);
3411
3412 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
3413 current_link_up == 1 &&
3414 tp->link_config.active_speed == SPEED_1000 &&
3415 ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) ||
3416 (tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED))) {
3417 udelay(120);
3418 tw32_f(MAC_STATUS,
3419 (MAC_STATUS_SYNC_CHANGED |
3420 MAC_STATUS_CFG_CHANGED));
3421 udelay(40);
3422 tg3_write_mem(tp,
3423 NIC_SRAM_FIRMWARE_MBOX,
3424 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
3425 }
3426
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003427 /* Prevent send BD corruption. */
3428 if (tp->tg3_flags3 & TG3_FLG3_CLKREQ_BUG) {
3429 u16 oldlnkctl, newlnkctl;
3430
3431 pci_read_config_word(tp->pdev,
3432 tp->pcie_cap + PCI_EXP_LNKCTL,
3433 &oldlnkctl);
3434 if (tp->link_config.active_speed == SPEED_100 ||
3435 tp->link_config.active_speed == SPEED_10)
3436 newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
3437 else
3438 newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
3439 if (newlnkctl != oldlnkctl)
3440 pci_write_config_word(tp->pdev,
3441 tp->pcie_cap + PCI_EXP_LNKCTL,
3442 newlnkctl);
3443 }
3444
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 if (current_link_up != netif_carrier_ok(tp->dev)) {
3446 if (current_link_up)
3447 netif_carrier_on(tp->dev);
3448 else
3449 netif_carrier_off(tp->dev);
3450 tg3_link_report(tp);
3451 }
3452
3453 return 0;
3454}
3455
3456struct tg3_fiber_aneginfo {
3457 int state;
3458#define ANEG_STATE_UNKNOWN 0
3459#define ANEG_STATE_AN_ENABLE 1
3460#define ANEG_STATE_RESTART_INIT 2
3461#define ANEG_STATE_RESTART 3
3462#define ANEG_STATE_DISABLE_LINK_OK 4
3463#define ANEG_STATE_ABILITY_DETECT_INIT 5
3464#define ANEG_STATE_ABILITY_DETECT 6
3465#define ANEG_STATE_ACK_DETECT_INIT 7
3466#define ANEG_STATE_ACK_DETECT 8
3467#define ANEG_STATE_COMPLETE_ACK_INIT 9
3468#define ANEG_STATE_COMPLETE_ACK 10
3469#define ANEG_STATE_IDLE_DETECT_INIT 11
3470#define ANEG_STATE_IDLE_DETECT 12
3471#define ANEG_STATE_LINK_OK 13
3472#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
3473#define ANEG_STATE_NEXT_PAGE_WAIT 15
3474
3475 u32 flags;
3476#define MR_AN_ENABLE 0x00000001
3477#define MR_RESTART_AN 0x00000002
3478#define MR_AN_COMPLETE 0x00000004
3479#define MR_PAGE_RX 0x00000008
3480#define MR_NP_LOADED 0x00000010
3481#define MR_TOGGLE_TX 0x00000020
3482#define MR_LP_ADV_FULL_DUPLEX 0x00000040
3483#define MR_LP_ADV_HALF_DUPLEX 0x00000080
3484#define MR_LP_ADV_SYM_PAUSE 0x00000100
3485#define MR_LP_ADV_ASYM_PAUSE 0x00000200
3486#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
3487#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
3488#define MR_LP_ADV_NEXT_PAGE 0x00001000
3489#define MR_TOGGLE_RX 0x00002000
3490#define MR_NP_RX 0x00004000
3491
3492#define MR_LINK_OK 0x80000000
3493
3494 unsigned long link_time, cur_time;
3495
3496 u32 ability_match_cfg;
3497 int ability_match_count;
3498
3499 char ability_match, idle_match, ack_match;
3500
3501 u32 txconfig, rxconfig;
3502#define ANEG_CFG_NP 0x00000080
3503#define ANEG_CFG_ACK 0x00000040
3504#define ANEG_CFG_RF2 0x00000020
3505#define ANEG_CFG_RF1 0x00000010
3506#define ANEG_CFG_PS2 0x00000001
3507#define ANEG_CFG_PS1 0x00008000
3508#define ANEG_CFG_HD 0x00004000
3509#define ANEG_CFG_FD 0x00002000
3510#define ANEG_CFG_INVAL 0x00001f06
3511
3512};
3513#define ANEG_OK 0
3514#define ANEG_DONE 1
3515#define ANEG_TIMER_ENAB 2
3516#define ANEG_FAILED -1
3517
3518#define ANEG_STATE_SETTLE_TIME 10000
3519
3520static int tg3_fiber_aneg_smachine(struct tg3 *tp,
3521 struct tg3_fiber_aneginfo *ap)
3522{
Matt Carlson5be73b42007-12-20 20:09:29 -08003523 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 unsigned long delta;
3525 u32 rx_cfg_reg;
3526 int ret;
3527
3528 if (ap->state == ANEG_STATE_UNKNOWN) {
3529 ap->rxconfig = 0;
3530 ap->link_time = 0;
3531 ap->cur_time = 0;
3532 ap->ability_match_cfg = 0;
3533 ap->ability_match_count = 0;
3534 ap->ability_match = 0;
3535 ap->idle_match = 0;
3536 ap->ack_match = 0;
3537 }
3538 ap->cur_time++;
3539
3540 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
3541 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
3542
3543 if (rx_cfg_reg != ap->ability_match_cfg) {
3544 ap->ability_match_cfg = rx_cfg_reg;
3545 ap->ability_match = 0;
3546 ap->ability_match_count = 0;
3547 } else {
3548 if (++ap->ability_match_count > 1) {
3549 ap->ability_match = 1;
3550 ap->ability_match_cfg = rx_cfg_reg;
3551 }
3552 }
3553 if (rx_cfg_reg & ANEG_CFG_ACK)
3554 ap->ack_match = 1;
3555 else
3556 ap->ack_match = 0;
3557
3558 ap->idle_match = 0;
3559 } else {
3560 ap->idle_match = 1;
3561 ap->ability_match_cfg = 0;
3562 ap->ability_match_count = 0;
3563 ap->ability_match = 0;
3564 ap->ack_match = 0;
3565
3566 rx_cfg_reg = 0;
3567 }
3568
3569 ap->rxconfig = rx_cfg_reg;
3570 ret = ANEG_OK;
3571
Matt Carlson33f401a2010-04-05 10:19:27 +00003572 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 case ANEG_STATE_UNKNOWN:
3574 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
3575 ap->state = ANEG_STATE_AN_ENABLE;
3576
3577 /* fallthru */
3578 case ANEG_STATE_AN_ENABLE:
3579 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
3580 if (ap->flags & MR_AN_ENABLE) {
3581 ap->link_time = 0;
3582 ap->cur_time = 0;
3583 ap->ability_match_cfg = 0;
3584 ap->ability_match_count = 0;
3585 ap->ability_match = 0;
3586 ap->idle_match = 0;
3587 ap->ack_match = 0;
3588
3589 ap->state = ANEG_STATE_RESTART_INIT;
3590 } else {
3591 ap->state = ANEG_STATE_DISABLE_LINK_OK;
3592 }
3593 break;
3594
3595 case ANEG_STATE_RESTART_INIT:
3596 ap->link_time = ap->cur_time;
3597 ap->flags &= ~(MR_NP_LOADED);
3598 ap->txconfig = 0;
3599 tw32(MAC_TX_AUTO_NEG, 0);
3600 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3601 tw32_f(MAC_MODE, tp->mac_mode);
3602 udelay(40);
3603
3604 ret = ANEG_TIMER_ENAB;
3605 ap->state = ANEG_STATE_RESTART;
3606
3607 /* fallthru */
3608 case ANEG_STATE_RESTART:
3609 delta = ap->cur_time - ap->link_time;
Matt Carlson859a5882010-04-05 10:19:28 +00003610 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a5882010-04-05 10:19:28 +00003612 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 break;
3615
3616 case ANEG_STATE_DISABLE_LINK_OK:
3617 ret = ANEG_DONE;
3618 break;
3619
3620 case ANEG_STATE_ABILITY_DETECT_INIT:
3621 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08003622 ap->txconfig = ANEG_CFG_FD;
3623 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3624 if (flowctrl & ADVERTISE_1000XPAUSE)
3625 ap->txconfig |= ANEG_CFG_PS1;
3626 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3627 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3629 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3630 tw32_f(MAC_MODE, tp->mac_mode);
3631 udelay(40);
3632
3633 ap->state = ANEG_STATE_ABILITY_DETECT;
3634 break;
3635
3636 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a5882010-04-05 10:19:28 +00003637 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 break;
3640
3641 case ANEG_STATE_ACK_DETECT_INIT:
3642 ap->txconfig |= ANEG_CFG_ACK;
3643 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3644 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3645 tw32_f(MAC_MODE, tp->mac_mode);
3646 udelay(40);
3647
3648 ap->state = ANEG_STATE_ACK_DETECT;
3649
3650 /* fallthru */
3651 case ANEG_STATE_ACK_DETECT:
3652 if (ap->ack_match != 0) {
3653 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
3654 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
3655 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
3656 } else {
3657 ap->state = ANEG_STATE_AN_ENABLE;
3658 }
3659 } else if (ap->ability_match != 0 &&
3660 ap->rxconfig == 0) {
3661 ap->state = ANEG_STATE_AN_ENABLE;
3662 }
3663 break;
3664
3665 case ANEG_STATE_COMPLETE_ACK_INIT:
3666 if (ap->rxconfig & ANEG_CFG_INVAL) {
3667 ret = ANEG_FAILED;
3668 break;
3669 }
3670 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
3671 MR_LP_ADV_HALF_DUPLEX |
3672 MR_LP_ADV_SYM_PAUSE |
3673 MR_LP_ADV_ASYM_PAUSE |
3674 MR_LP_ADV_REMOTE_FAULT1 |
3675 MR_LP_ADV_REMOTE_FAULT2 |
3676 MR_LP_ADV_NEXT_PAGE |
3677 MR_TOGGLE_RX |
3678 MR_NP_RX);
3679 if (ap->rxconfig & ANEG_CFG_FD)
3680 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
3681 if (ap->rxconfig & ANEG_CFG_HD)
3682 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
3683 if (ap->rxconfig & ANEG_CFG_PS1)
3684 ap->flags |= MR_LP_ADV_SYM_PAUSE;
3685 if (ap->rxconfig & ANEG_CFG_PS2)
3686 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
3687 if (ap->rxconfig & ANEG_CFG_RF1)
3688 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
3689 if (ap->rxconfig & ANEG_CFG_RF2)
3690 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
3691 if (ap->rxconfig & ANEG_CFG_NP)
3692 ap->flags |= MR_LP_ADV_NEXT_PAGE;
3693
3694 ap->link_time = ap->cur_time;
3695
3696 ap->flags ^= (MR_TOGGLE_TX);
3697 if (ap->rxconfig & 0x0008)
3698 ap->flags |= MR_TOGGLE_RX;
3699 if (ap->rxconfig & ANEG_CFG_NP)
3700 ap->flags |= MR_NP_RX;
3701 ap->flags |= MR_PAGE_RX;
3702
3703 ap->state = ANEG_STATE_COMPLETE_ACK;
3704 ret = ANEG_TIMER_ENAB;
3705 break;
3706
3707 case ANEG_STATE_COMPLETE_ACK:
3708 if (ap->ability_match != 0 &&
3709 ap->rxconfig == 0) {
3710 ap->state = ANEG_STATE_AN_ENABLE;
3711 break;
3712 }
3713 delta = ap->cur_time - ap->link_time;
3714 if (delta > ANEG_STATE_SETTLE_TIME) {
3715 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
3716 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3717 } else {
3718 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
3719 !(ap->flags & MR_NP_RX)) {
3720 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3721 } else {
3722 ret = ANEG_FAILED;
3723 }
3724 }
3725 }
3726 break;
3727
3728 case ANEG_STATE_IDLE_DETECT_INIT:
3729 ap->link_time = ap->cur_time;
3730 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3731 tw32_f(MAC_MODE, tp->mac_mode);
3732 udelay(40);
3733
3734 ap->state = ANEG_STATE_IDLE_DETECT;
3735 ret = ANEG_TIMER_ENAB;
3736 break;
3737
3738 case ANEG_STATE_IDLE_DETECT:
3739 if (ap->ability_match != 0 &&
3740 ap->rxconfig == 0) {
3741 ap->state = ANEG_STATE_AN_ENABLE;
3742 break;
3743 }
3744 delta = ap->cur_time - ap->link_time;
3745 if (delta > ANEG_STATE_SETTLE_TIME) {
3746 /* XXX another gem from the Broadcom driver :( */
3747 ap->state = ANEG_STATE_LINK_OK;
3748 }
3749 break;
3750
3751 case ANEG_STATE_LINK_OK:
3752 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
3753 ret = ANEG_DONE;
3754 break;
3755
3756 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
3757 /* ??? unimplemented */
3758 break;
3759
3760 case ANEG_STATE_NEXT_PAGE_WAIT:
3761 /* ??? unimplemented */
3762 break;
3763
3764 default:
3765 ret = ANEG_FAILED;
3766 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768
3769 return ret;
3770}
3771
Matt Carlson5be73b42007-12-20 20:09:29 -08003772static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773{
3774 int res = 0;
3775 struct tg3_fiber_aneginfo aninfo;
3776 int status = ANEG_FAILED;
3777 unsigned int tick;
3778 u32 tmp;
3779
3780 tw32_f(MAC_TX_AUTO_NEG, 0);
3781
3782 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
3783 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
3784 udelay(40);
3785
3786 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
3787 udelay(40);
3788
3789 memset(&aninfo, 0, sizeof(aninfo));
3790 aninfo.flags |= MR_AN_ENABLE;
3791 aninfo.state = ANEG_STATE_UNKNOWN;
3792 aninfo.cur_time = 0;
3793 tick = 0;
3794 while (++tick < 195000) {
3795 status = tg3_fiber_aneg_smachine(tp, &aninfo);
3796 if (status == ANEG_DONE || status == ANEG_FAILED)
3797 break;
3798
3799 udelay(1);
3800 }
3801
3802 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3803 tw32_f(MAC_MODE, tp->mac_mode);
3804 udelay(40);
3805
Matt Carlson5be73b42007-12-20 20:09:29 -08003806 *txflags = aninfo.txconfig;
3807 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808
3809 if (status == ANEG_DONE &&
3810 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
3811 MR_LP_ADV_FULL_DUPLEX)))
3812 res = 1;
3813
3814 return res;
3815}
3816
3817static void tg3_init_bcm8002(struct tg3 *tp)
3818{
3819 u32 mac_status = tr32(MAC_STATUS);
3820 int i;
3821
3822 /* Reset when initting first time or we have a link. */
3823 if ((tp->tg3_flags & TG3_FLAG_INIT_COMPLETE) &&
3824 !(mac_status & MAC_STATUS_PCS_SYNCED))
3825 return;
3826
3827 /* Set PLL lock range. */
3828 tg3_writephy(tp, 0x16, 0x8007);
3829
3830 /* SW reset */
3831 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
3832
3833 /* Wait for reset to complete. */
3834 /* XXX schedule_timeout() ... */
3835 for (i = 0; i < 500; i++)
3836 udelay(10);
3837
3838 /* Config mode; select PMA/Ch 1 regs. */
3839 tg3_writephy(tp, 0x10, 0x8411);
3840
3841 /* Enable auto-lock and comdet, select txclk for tx. */
3842 tg3_writephy(tp, 0x11, 0x0a10);
3843
3844 tg3_writephy(tp, 0x18, 0x00a0);
3845 tg3_writephy(tp, 0x16, 0x41ff);
3846
3847 /* Assert and deassert POR. */
3848 tg3_writephy(tp, 0x13, 0x0400);
3849 udelay(40);
3850 tg3_writephy(tp, 0x13, 0x0000);
3851
3852 tg3_writephy(tp, 0x11, 0x0a50);
3853 udelay(40);
3854 tg3_writephy(tp, 0x11, 0x0a10);
3855
3856 /* Wait for signal to stabilize */
3857 /* XXX schedule_timeout() ... */
3858 for (i = 0; i < 15000; i++)
3859 udelay(10);
3860
3861 /* Deselect the channel register so we can read the PHYID
3862 * later.
3863 */
3864 tg3_writephy(tp, 0x10, 0x8011);
3865}
3866
3867static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
3868{
Matt Carlson82cd3d12007-12-20 20:09:00 -08003869 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 u32 sg_dig_ctrl, sg_dig_status;
3871 u32 serdes_cfg, expected_sg_dig_ctrl;
3872 int workaround, port_a;
3873 int current_link_up;
3874
3875 serdes_cfg = 0;
3876 expected_sg_dig_ctrl = 0;
3877 workaround = 0;
3878 port_a = 1;
3879 current_link_up = 0;
3880
3881 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
3882 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
3883 workaround = 1;
3884 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
3885 port_a = 0;
3886
3887 /* preserve bits 0-11,13,14 for signal pre-emphasis */
3888 /* preserve bits 20-23 for voltage regulator */
3889 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
3890 }
3891
3892 sg_dig_ctrl = tr32(SG_DIG_CTRL);
3893
3894 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003895 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 if (workaround) {
3897 u32 val = serdes_cfg;
3898
3899 if (port_a)
3900 val |= 0xc010000;
3901 else
3902 val |= 0x4010000;
3903 tw32_f(MAC_SERDES_CFG, val);
3904 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003905
3906 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 }
3908 if (mac_status & MAC_STATUS_PCS_SYNCED) {
3909 tg3_setup_flow_control(tp, 0, 0);
3910 current_link_up = 1;
3911 }
3912 goto out;
3913 }
3914
3915 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003916 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917
Matt Carlson82cd3d12007-12-20 20:09:00 -08003918 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3919 if (flowctrl & ADVERTISE_1000XPAUSE)
3920 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
3921 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3922 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923
3924 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003925 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07003926 tp->serdes_counter &&
3927 ((mac_status & (MAC_STATUS_PCS_SYNCED |
3928 MAC_STATUS_RCVD_CFG)) ==
3929 MAC_STATUS_PCS_SYNCED)) {
3930 tp->serdes_counter--;
3931 current_link_up = 1;
3932 goto out;
3933 }
3934restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 if (workaround)
3936 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003937 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 udelay(5);
3939 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
3940
Michael Chan3d3ebe72006-09-27 15:59:15 -07003941 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003942 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
3944 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003945 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 mac_status = tr32(MAC_STATUS);
3947
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003948 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08003950 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951
Matt Carlson82cd3d12007-12-20 20:09:00 -08003952 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
3953 local_adv |= ADVERTISE_1000XPAUSE;
3954 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
3955 local_adv |= ADVERTISE_1000XPSE_ASYM;
3956
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003957 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003958 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003959 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003960 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961
3962 tg3_setup_flow_control(tp, local_adv, remote_adv);
3963 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003964 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003965 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003966 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003967 if (tp->serdes_counter)
3968 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 else {
3970 if (workaround) {
3971 u32 val = serdes_cfg;
3972
3973 if (port_a)
3974 val |= 0xc010000;
3975 else
3976 val |= 0x4010000;
3977
3978 tw32_f(MAC_SERDES_CFG, val);
3979 }
3980
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003981 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 udelay(40);
3983
3984 /* Link parallel detection - link is up */
3985 /* only if we have PCS_SYNC and not */
3986 /* receiving config code words */
3987 mac_status = tr32(MAC_STATUS);
3988 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
3989 !(mac_status & MAC_STATUS_RCVD_CFG)) {
3990 tg3_setup_flow_control(tp, 0, 0);
3991 current_link_up = 1;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003992 tp->phy_flags |=
3993 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003994 tp->serdes_counter =
3995 SERDES_PARALLEL_DET_TIMEOUT;
3996 } else
3997 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 }
3999 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07004000 } else {
4001 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004002 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 }
4004
4005out:
4006 return current_link_up;
4007}
4008
4009static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
4010{
4011 int current_link_up = 0;
4012
Michael Chan5cf64b82007-05-05 12:11:21 -07004013 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015
4016 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08004017 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004019
Matt Carlson5be73b42007-12-20 20:09:29 -08004020 if (fiber_autoneg(tp, &txflags, &rxflags)) {
4021 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022
Matt Carlson5be73b42007-12-20 20:09:29 -08004023 if (txflags & ANEG_CFG_PS1)
4024 local_adv |= ADVERTISE_1000XPAUSE;
4025 if (txflags & ANEG_CFG_PS2)
4026 local_adv |= ADVERTISE_1000XPSE_ASYM;
4027
4028 if (rxflags & MR_LP_ADV_SYM_PAUSE)
4029 remote_adv |= LPA_1000XPAUSE;
4030 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
4031 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032
4033 tg3_setup_flow_control(tp, local_adv, remote_adv);
4034
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 current_link_up = 1;
4036 }
4037 for (i = 0; i < 30; i++) {
4038 udelay(20);
4039 tw32_f(MAC_STATUS,
4040 (MAC_STATUS_SYNC_CHANGED |
4041 MAC_STATUS_CFG_CHANGED));
4042 udelay(40);
4043 if ((tr32(MAC_STATUS) &
4044 (MAC_STATUS_SYNC_CHANGED |
4045 MAC_STATUS_CFG_CHANGED)) == 0)
4046 break;
4047 }
4048
4049 mac_status = tr32(MAC_STATUS);
4050 if (current_link_up == 0 &&
4051 (mac_status & MAC_STATUS_PCS_SYNCED) &&
4052 !(mac_status & MAC_STATUS_RCVD_CFG))
4053 current_link_up = 1;
4054 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08004055 tg3_setup_flow_control(tp, 0, 0);
4056
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057 /* Forcing 1000FD link up. */
4058 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059
4060 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
4061 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004062
4063 tw32_f(MAC_MODE, tp->mac_mode);
4064 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 }
4066
4067out:
4068 return current_link_up;
4069}
4070
4071static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
4072{
4073 u32 orig_pause_cfg;
4074 u16 orig_active_speed;
4075 u8 orig_active_duplex;
4076 u32 mac_status;
4077 int current_link_up;
4078 int i;
4079
Matt Carlson8d018622007-12-20 20:05:44 -08004080 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081 orig_active_speed = tp->link_config.active_speed;
4082 orig_active_duplex = tp->link_config.active_duplex;
4083
4084 if (!(tp->tg3_flags2 & TG3_FLG2_HW_AUTONEG) &&
4085 netif_carrier_ok(tp->dev) &&
4086 (tp->tg3_flags & TG3_FLAG_INIT_COMPLETE)) {
4087 mac_status = tr32(MAC_STATUS);
4088 mac_status &= (MAC_STATUS_PCS_SYNCED |
4089 MAC_STATUS_SIGNAL_DET |
4090 MAC_STATUS_CFG_CHANGED |
4091 MAC_STATUS_RCVD_CFG);
4092 if (mac_status == (MAC_STATUS_PCS_SYNCED |
4093 MAC_STATUS_SIGNAL_DET)) {
4094 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4095 MAC_STATUS_CFG_CHANGED));
4096 return 0;
4097 }
4098 }
4099
4100 tw32_f(MAC_TX_AUTO_NEG, 0);
4101
4102 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
4103 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
4104 tw32_f(MAC_MODE, tp->mac_mode);
4105 udelay(40);
4106
Matt Carlson79eb6902010-02-17 15:17:03 +00004107 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 tg3_init_bcm8002(tp);
4109
4110 /* Enable link change event even when serdes polling. */
4111 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4112 udelay(40);
4113
4114 current_link_up = 0;
4115 mac_status = tr32(MAC_STATUS);
4116
4117 if (tp->tg3_flags2 & TG3_FLG2_HW_AUTONEG)
4118 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
4119 else
4120 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
4121
Matt Carlson898a56f2009-08-28 14:02:40 +00004122 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00004124 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125
4126 for (i = 0; i < 100; i++) {
4127 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4128 MAC_STATUS_CFG_CHANGED));
4129 udelay(5);
4130 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07004131 MAC_STATUS_CFG_CHANGED |
4132 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 break;
4134 }
4135
4136 mac_status = tr32(MAC_STATUS);
4137 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
4138 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004139 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
4140 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 tw32_f(MAC_MODE, (tp->mac_mode |
4142 MAC_MODE_SEND_CONFIGS));
4143 udelay(1);
4144 tw32_f(MAC_MODE, tp->mac_mode);
4145 }
4146 }
4147
4148 if (current_link_up == 1) {
4149 tp->link_config.active_speed = SPEED_1000;
4150 tp->link_config.active_duplex = DUPLEX_FULL;
4151 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4152 LED_CTRL_LNKLED_OVERRIDE |
4153 LED_CTRL_1000MBPS_ON));
4154 } else {
4155 tp->link_config.active_speed = SPEED_INVALID;
4156 tp->link_config.active_duplex = DUPLEX_INVALID;
4157 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4158 LED_CTRL_LNKLED_OVERRIDE |
4159 LED_CTRL_TRAFFIC_OVERRIDE));
4160 }
4161
4162 if (current_link_up != netif_carrier_ok(tp->dev)) {
4163 if (current_link_up)
4164 netif_carrier_on(tp->dev);
4165 else
4166 netif_carrier_off(tp->dev);
4167 tg3_link_report(tp);
4168 } else {
Matt Carlson8d018622007-12-20 20:05:44 -08004169 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 if (orig_pause_cfg != now_pause_cfg ||
4171 orig_active_speed != tp->link_config.active_speed ||
4172 orig_active_duplex != tp->link_config.active_duplex)
4173 tg3_link_report(tp);
4174 }
4175
4176 return 0;
4177}
4178
Michael Chan747e8f82005-07-25 12:33:22 -07004179static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
4180{
4181 int current_link_up, err = 0;
4182 u32 bmsr, bmcr;
4183 u16 current_speed;
4184 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08004185 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07004186
4187 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4188 tw32_f(MAC_MODE, tp->mac_mode);
4189 udelay(40);
4190
4191 tw32(MAC_EVENT, 0);
4192
4193 tw32_f(MAC_STATUS,
4194 (MAC_STATUS_SYNC_CHANGED |
4195 MAC_STATUS_CFG_CHANGED |
4196 MAC_STATUS_MI_COMPLETION |
4197 MAC_STATUS_LNKSTATE_CHANGED));
4198 udelay(40);
4199
4200 if (force_reset)
4201 tg3_phy_reset(tp);
4202
4203 current_link_up = 0;
4204 current_speed = SPEED_INVALID;
4205 current_duplex = DUPLEX_INVALID;
4206
4207 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4208 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004209 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
4210 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4211 bmsr |= BMSR_LSTATUS;
4212 else
4213 bmsr &= ~BMSR_LSTATUS;
4214 }
Michael Chan747e8f82005-07-25 12:33:22 -07004215
4216 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
4217
4218 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004219 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004220 /* do nothing, just check for link up at the end */
4221 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4222 u32 adv, new_adv;
4223
4224 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4225 new_adv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
4226 ADVERTISE_1000XPAUSE |
4227 ADVERTISE_1000XPSE_ASYM |
4228 ADVERTISE_SLCT);
4229
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004230 new_adv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Michael Chan747e8f82005-07-25 12:33:22 -07004231
4232 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
4233 new_adv |= ADVERTISE_1000XHALF;
4234 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
4235 new_adv |= ADVERTISE_1000XFULL;
4236
4237 if ((new_adv != adv) || !(bmcr & BMCR_ANENABLE)) {
4238 tg3_writephy(tp, MII_ADVERTISE, new_adv);
4239 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
4240 tg3_writephy(tp, MII_BMCR, bmcr);
4241
4242 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07004243 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004244 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004245
4246 return err;
4247 }
4248 } else {
4249 u32 new_bmcr;
4250
4251 bmcr &= ~BMCR_SPEED1000;
4252 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
4253
4254 if (tp->link_config.duplex == DUPLEX_FULL)
4255 new_bmcr |= BMCR_FULLDPLX;
4256
4257 if (new_bmcr != bmcr) {
4258 /* BMCR_SPEED1000 is a reserved bit that needs
4259 * to be set on write.
4260 */
4261 new_bmcr |= BMCR_SPEED1000;
4262
4263 /* Force a linkdown */
4264 if (netif_carrier_ok(tp->dev)) {
4265 u32 adv;
4266
4267 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4268 adv &= ~(ADVERTISE_1000XFULL |
4269 ADVERTISE_1000XHALF |
4270 ADVERTISE_SLCT);
4271 tg3_writephy(tp, MII_ADVERTISE, adv);
4272 tg3_writephy(tp, MII_BMCR, bmcr |
4273 BMCR_ANRESTART |
4274 BMCR_ANENABLE);
4275 udelay(10);
4276 netif_carrier_off(tp->dev);
4277 }
4278 tg3_writephy(tp, MII_BMCR, new_bmcr);
4279 bmcr = new_bmcr;
4280 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4281 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004282 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
4283 ASIC_REV_5714) {
4284 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4285 bmsr |= BMSR_LSTATUS;
4286 else
4287 bmsr &= ~BMSR_LSTATUS;
4288 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004289 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004290 }
4291 }
4292
4293 if (bmsr & BMSR_LSTATUS) {
4294 current_speed = SPEED_1000;
4295 current_link_up = 1;
4296 if (bmcr & BMCR_FULLDPLX)
4297 current_duplex = DUPLEX_FULL;
4298 else
4299 current_duplex = DUPLEX_HALF;
4300
Matt Carlsonef167e22007-12-20 20:10:01 -08004301 local_adv = 0;
4302 remote_adv = 0;
4303
Michael Chan747e8f82005-07-25 12:33:22 -07004304 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08004305 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07004306
4307 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
4308 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
4309 common = local_adv & remote_adv;
4310 if (common & (ADVERTISE_1000XHALF |
4311 ADVERTISE_1000XFULL)) {
4312 if (common & ADVERTISE_1000XFULL)
4313 current_duplex = DUPLEX_FULL;
4314 else
4315 current_duplex = DUPLEX_HALF;
Matt Carlson57d8b882010-06-05 17:24:35 +00004316 } else if (!(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
4317 /* Link is up via parallel detect */
Matt Carlson859a5882010-04-05 10:19:28 +00004318 } else {
Michael Chan747e8f82005-07-25 12:33:22 -07004319 current_link_up = 0;
Matt Carlson859a5882010-04-05 10:19:28 +00004320 }
Michael Chan747e8f82005-07-25 12:33:22 -07004321 }
4322 }
4323
Matt Carlsonef167e22007-12-20 20:10:01 -08004324 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
4325 tg3_setup_flow_control(tp, local_adv, remote_adv);
4326
Michael Chan747e8f82005-07-25 12:33:22 -07004327 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4328 if (tp->link_config.active_duplex == DUPLEX_HALF)
4329 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4330
4331 tw32_f(MAC_MODE, tp->mac_mode);
4332 udelay(40);
4333
4334 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4335
4336 tp->link_config.active_speed = current_speed;
4337 tp->link_config.active_duplex = current_duplex;
4338
4339 if (current_link_up != netif_carrier_ok(tp->dev)) {
4340 if (current_link_up)
4341 netif_carrier_on(tp->dev);
4342 else {
4343 netif_carrier_off(tp->dev);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004344 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004345 }
4346 tg3_link_report(tp);
4347 }
4348 return err;
4349}
4350
4351static void tg3_serdes_parallel_detect(struct tg3 *tp)
4352{
Michael Chan3d3ebe72006-09-27 15:59:15 -07004353 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07004354 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07004355 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07004356 return;
4357 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00004358
Michael Chan747e8f82005-07-25 12:33:22 -07004359 if (!netif_carrier_ok(tp->dev) &&
4360 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
4361 u32 bmcr;
4362
4363 tg3_readphy(tp, MII_BMCR, &bmcr);
4364 if (bmcr & BMCR_ANENABLE) {
4365 u32 phy1, phy2;
4366
4367 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004368 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
4369 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07004370
4371 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004372 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
4373 MII_TG3_DSP_EXP1_INT_STAT);
4374 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
4375 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07004376
4377 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
4378 /* We have signal detect and not receiving
4379 * config code words, link is up by parallel
4380 * detection.
4381 */
4382
4383 bmcr &= ~BMCR_ANENABLE;
4384 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
4385 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004386 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004387 }
4388 }
Matt Carlson859a5882010-04-05 10:19:28 +00004389 } else if (netif_carrier_ok(tp->dev) &&
4390 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004391 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004392 u32 phy2;
4393
4394 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004395 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
4396 MII_TG3_DSP_EXP1_INT_STAT);
4397 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07004398 if (phy2 & 0x20) {
4399 u32 bmcr;
4400
4401 /* Config code words received, turn on autoneg. */
4402 tg3_readphy(tp, MII_BMCR, &bmcr);
4403 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
4404
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004405 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004406
4407 }
4408 }
4409}
4410
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411static int tg3_setup_phy(struct tg3 *tp, int force_reset)
4412{
Matt Carlsonf2096f92011-04-05 14:22:48 +00004413 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 int err;
4415
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004416 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004418 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07004419 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a5882010-04-05 10:19:28 +00004420 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422
Matt Carlsonbcb37f62008-11-03 16:52:09 -08004423 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00004424 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08004425
4426 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
4427 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
4428 scale = 65;
4429 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
4430 scale = 6;
4431 else
4432 scale = 12;
4433
4434 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
4435 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
4436 tw32(GRC_MISC_CFG, val);
4437 }
4438
Matt Carlsonf2096f92011-04-05 14:22:48 +00004439 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4440 (6 << TX_LENGTHS_IPG_SHIFT);
4441 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
4442 val |= tr32(MAC_TX_LENGTHS) &
4443 (TX_LENGTHS_JMB_FRM_LEN_MSK |
4444 TX_LENGTHS_CNT_DWN_VAL_MSK);
4445
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446 if (tp->link_config.active_speed == SPEED_1000 &&
4447 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00004448 tw32(MAC_TX_LENGTHS, val |
4449 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00004451 tw32(MAC_TX_LENGTHS, val |
4452 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453
4454 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
4455 if (netif_carrier_ok(tp->dev)) {
4456 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07004457 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 } else {
4459 tw32(HOSTCC_STAT_COAL_TICKS, 0);
4460 }
4461 }
4462
Matt Carlson8ed5d972007-05-07 00:25:49 -07004463 if (tp->tg3_flags & TG3_FLAG_ASPM_WORKAROUND) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00004464 val = tr32(PCIE_PWR_MGMT_THRESH);
Matt Carlson8ed5d972007-05-07 00:25:49 -07004465 if (!netif_carrier_ok(tp->dev))
4466 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
4467 tp->pwrmgmt_thresh;
4468 else
4469 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
4470 tw32(PCIE_PWR_MGMT_THRESH, val);
4471 }
4472
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 return err;
4474}
4475
Matt Carlson66cfd1b2010-09-30 10:34:30 +00004476static inline int tg3_irq_sync(struct tg3 *tp)
4477{
4478 return tp->irq_sync;
4479}
4480
Matt Carlson97bd8e42011-04-13 11:05:04 +00004481static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
4482{
4483 int i;
4484
4485 dst = (u32 *)((u8 *)dst + off);
4486 for (i = 0; i < len; i += sizeof(u32))
4487 *dst++ = tr32(off + i);
4488}
4489
4490static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
4491{
4492 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
4493 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
4494 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
4495 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
4496 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
4497 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
4498 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
4499 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
4500 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
4501 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
4502 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
4503 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
4504 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
4505 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
4506 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
4507 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
4508 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
4509 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
4510 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
4511
4512 if (tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX)
4513 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
4514
4515 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
4516 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
4517 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
4518 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
4519 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
4520 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
4521 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
4522 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
4523
4524 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
4525 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
4526 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
4527 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
4528 }
4529
4530 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
4531 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
4532 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
4533 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
4534 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
4535
4536 if (tp->tg3_flags & TG3_FLAG_NVRAM)
4537 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
4538}
4539
4540static void tg3_dump_state(struct tg3 *tp)
4541{
4542 int i;
4543 u32 *regs;
4544
4545 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
4546 if (!regs) {
4547 netdev_err(tp->dev, "Failed allocating register dump buffer\n");
4548 return;
4549 }
4550
4551 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
4552 /* Read up to but not including private PCI registers */
4553 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
4554 regs[i / sizeof(u32)] = tr32(i);
4555 } else
4556 tg3_dump_legacy_regs(tp, regs);
4557
4558 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
4559 if (!regs[i + 0] && !regs[i + 1] &&
4560 !regs[i + 2] && !regs[i + 3])
4561 continue;
4562
4563 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
4564 i * 4,
4565 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
4566 }
4567
4568 kfree(regs);
4569
4570 for (i = 0; i < tp->irq_cnt; i++) {
4571 struct tg3_napi *tnapi = &tp->napi[i];
4572
4573 /* SW status block */
4574 netdev_err(tp->dev,
4575 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
4576 i,
4577 tnapi->hw_status->status,
4578 tnapi->hw_status->status_tag,
4579 tnapi->hw_status->rx_jumbo_consumer,
4580 tnapi->hw_status->rx_consumer,
4581 tnapi->hw_status->rx_mini_consumer,
4582 tnapi->hw_status->idx[0].rx_producer,
4583 tnapi->hw_status->idx[0].tx_consumer);
4584
4585 netdev_err(tp->dev,
4586 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
4587 i,
4588 tnapi->last_tag, tnapi->last_irq_tag,
4589 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
4590 tnapi->rx_rcb_ptr,
4591 tnapi->prodring.rx_std_prod_idx,
4592 tnapi->prodring.rx_std_cons_idx,
4593 tnapi->prodring.rx_jmb_prod_idx,
4594 tnapi->prodring.rx_jmb_cons_idx);
4595 }
4596}
4597
Michael Chandf3e6542006-05-26 17:48:07 -07004598/* This is called whenever we suspect that the system chipset is re-
4599 * ordering the sequence of MMIO to the tx send mailbox. The symptom
4600 * is bogus tx completions. We try to recover by setting the
4601 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
4602 * in the workqueue.
4603 */
4604static void tg3_tx_recover(struct tg3 *tp)
4605{
4606 BUG_ON((tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER) ||
4607 tp->write32_tx_mbox == tg3_write_indirect_mbox);
4608
Matt Carlson5129c3a2010-04-05 10:19:23 +00004609 netdev_warn(tp->dev,
4610 "The system may be re-ordering memory-mapped I/O "
4611 "cycles to the network device, attempting to recover. "
4612 "Please report the problem to the driver maintainer "
4613 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07004614
4615 spin_lock(&tp->lock);
Michael Chandf3e6542006-05-26 17:48:07 -07004616 tp->tg3_flags |= TG3_FLAG_TX_RECOVERY_PENDING;
Michael Chandf3e6542006-05-26 17:48:07 -07004617 spin_unlock(&tp->lock);
4618}
4619
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004620static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07004621{
Matt Carlsonf65aac12010-08-02 11:26:03 +00004622 /* Tell compiler to fetch tx indices from memory. */
4623 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004624 return tnapi->tx_pending -
4625 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07004626}
4627
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628/* Tigon3 never reports partial packet sends. So we do not
4629 * need special logic to handle SKBs that have not had all
4630 * of their frags sent yet, like SunGEM does.
4631 */
Matt Carlson17375d22009-08-28 14:02:18 +00004632static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633{
Matt Carlson17375d22009-08-28 14:02:18 +00004634 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00004635 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004636 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004637 struct netdev_queue *txq;
4638 int index = tnapi - tp->napi;
4639
Matt Carlson19cfaec2009-12-03 08:36:20 +00004640 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004641 index--;
4642
4643 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644
4645 while (sw_idx != hw_idx) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00004646 struct ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07004648 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649
Michael Chandf3e6542006-05-26 17:48:07 -07004650 if (unlikely(skb == NULL)) {
4651 tg3_tx_recover(tp);
4652 return;
4653 }
4654
Alexander Duyckf4188d82009-12-02 16:48:38 +00004655 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004656 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00004657 skb_headlen(skb),
4658 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659
4660 ri->skb = NULL;
4661
4662 sw_idx = NEXT_TX(sw_idx);
4663
4664 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004665 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07004666 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
4667 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00004668
4669 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004670 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00004671 skb_shinfo(skb)->frags[i].size,
4672 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 sw_idx = NEXT_TX(sw_idx);
4674 }
4675
David S. Millerf47c11e2005-06-24 20:18:35 -07004676 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07004677
4678 if (unlikely(tx_bug)) {
4679 tg3_tx_recover(tp);
4680 return;
4681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 }
4683
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004684 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685
Michael Chan1b2a7202006-08-07 21:46:02 -07004686 /* Need to make the tx_cons update visible to tg3_start_xmit()
4687 * before checking for netif_queue_stopped(). Without the
4688 * memory barrier, there is a small possibility that tg3_start_xmit()
4689 * will miss it and cause the queue to be stopped forever.
4690 */
4691 smp_mb();
4692
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004693 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004694 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004695 __netif_tx_lock(txq, smp_processor_id());
4696 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004697 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004698 netif_tx_wake_queue(txq);
4699 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07004700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701}
4702
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004703static void tg3_rx_skb_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
4704{
4705 if (!ri->skb)
4706 return;
4707
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004708 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004709 map_sz, PCI_DMA_FROMDEVICE);
4710 dev_kfree_skb_any(ri->skb);
4711 ri->skb = NULL;
4712}
4713
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714/* Returns size of skb allocated or < 0 on error.
4715 *
4716 * We only need to fill in the address because the other members
4717 * of the RX descriptor are invariant, see tg3_init_rings.
4718 *
4719 * Note the purposeful assymetry of cpu vs. chip accesses. For
4720 * posting buffers we only dirty the first cache line of the RX
4721 * descriptor (containing the address). Whereas for the RX status
4722 * buffers the cpu only reads the last cacheline of the RX descriptor
4723 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
4724 */
Matt Carlson86b21e52009-11-13 13:03:45 +00004725static int tg3_alloc_rx_skb(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Matt Carlsona3896162009-11-13 13:03:44 +00004726 u32 opaque_key, u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727{
4728 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00004729 struct ring_info *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730 struct sk_buff *skb;
4731 dma_addr_t mapping;
4732 int skb_size, dest_idx;
4733
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734 switch (opaque_key) {
4735 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00004736 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00004737 desc = &tpr->rx_std[dest_idx];
4738 map = &tpr->rx_std_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004739 skb_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 break;
4741
4742 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00004743 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00004744 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00004745 map = &tpr->rx_jmb_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004746 skb_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747 break;
4748
4749 default:
4750 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752
4753 /* Do not overwrite any of the map or rp information
4754 * until we are sure we can commit to a new buffer.
4755 *
4756 * Callers depend upon this behavior and assume that
4757 * we leave everything unchanged if we fail.
4758 */
Matt Carlson287be122009-08-28 13:58:46 +00004759 skb = netdev_alloc_skb(tp->dev, skb_size + tp->rx_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 if (skb == NULL)
4761 return -ENOMEM;
4762
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763 skb_reserve(skb, tp->rx_offset);
4764
Matt Carlson287be122009-08-28 13:58:46 +00004765 mapping = pci_map_single(tp->pdev, skb->data, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 PCI_DMA_FROMDEVICE);
Matt Carlsona21771d2009-11-02 14:25:31 +00004767 if (pci_dma_mapping_error(tp->pdev, mapping)) {
4768 dev_kfree_skb(skb);
4769 return -EIO;
4770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771
4772 map->skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004773 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 desc->addr_hi = ((u64)mapping >> 32);
4776 desc->addr_lo = ((u64)mapping & 0xffffffff);
4777
4778 return skb_size;
4779}
4780
4781/* We only need to move over in the address because the other
4782 * members of the RX descriptor are invariant. See notes above
4783 * tg3_alloc_rx_skb for full details.
4784 */
Matt Carlsona3896162009-11-13 13:03:44 +00004785static void tg3_recycle_rx(struct tg3_napi *tnapi,
4786 struct tg3_rx_prodring_set *dpr,
4787 u32 opaque_key, int src_idx,
4788 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789{
Matt Carlson17375d22009-08-28 14:02:18 +00004790 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
4792 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00004793 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00004794 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795
4796 switch (opaque_key) {
4797 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00004798 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00004799 dest_desc = &dpr->rx_std[dest_idx];
4800 dest_map = &dpr->rx_std_buffers[dest_idx];
4801 src_desc = &spr->rx_std[src_idx];
4802 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803 break;
4804
4805 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00004806 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00004807 dest_desc = &dpr->rx_jmb[dest_idx].std;
4808 dest_map = &dpr->rx_jmb_buffers[dest_idx];
4809 src_desc = &spr->rx_jmb[src_idx].std;
4810 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811 break;
4812
4813 default:
4814 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816
4817 dest_map->skb = src_map->skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004818 dma_unmap_addr_set(dest_map, mapping,
4819 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820 dest_desc->addr_hi = src_desc->addr_hi;
4821 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00004822
4823 /* Ensure that the update to the skb happens after the physical
4824 * addresses have been transferred to the new BD location.
4825 */
4826 smp_wmb();
4827
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828 src_map->skb = NULL;
4829}
4830
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831/* The RX ring scheme is composed of multiple rings which post fresh
4832 * buffers to the chip, and one special ring the chip uses to report
4833 * status back to the host.
4834 *
4835 * The special ring reports the status of received packets to the
4836 * host. The chip does not write into the original descriptor the
4837 * RX buffer was obtained from. The chip simply takes the original
4838 * descriptor as provided by the host, updates the status and length
4839 * field, then writes this into the next status ring entry.
4840 *
4841 * Each ring the host uses to post buffers to the chip is described
4842 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
4843 * it is first placed into the on-chip ram. When the packet's length
4844 * is known, it walks down the TG3_BDINFO entries to select the ring.
4845 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
4846 * which is within the range of the new packet's length is chosen.
4847 *
4848 * The "separate ring for rx status" scheme may sound queer, but it makes
4849 * sense from a cache coherency perspective. If only the host writes
4850 * to the buffer post rings, and only the chip writes to the rx status
4851 * rings, then cache lines never move beyond shared-modified state.
4852 * If both the host and chip were to write into the same ring, cache line
4853 * eviction could occur since both entities want it in an exclusive state.
4854 */
Matt Carlson17375d22009-08-28 14:02:18 +00004855static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856{
Matt Carlson17375d22009-08-28 14:02:18 +00004857 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07004858 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00004859 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00004860 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07004861 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00004863 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004865 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866 /*
4867 * We need to order the read of hw_idx and the read of
4868 * the opaque cookie.
4869 */
4870 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871 work_mask = 0;
4872 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00004873 std_prod_idx = tpr->rx_std_prod_idx;
4874 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00004876 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00004877 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 unsigned int len;
4879 struct sk_buff *skb;
4880 dma_addr_t dma_addr;
4881 u32 opaque_key, desc_idx, *post_ptr;
4882
4883 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
4884 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
4885 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00004886 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004887 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00004888 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00004889 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07004890 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00004892 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004893 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00004894 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00004895 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00004896 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898
4899 work_mask |= opaque_key;
4900
4901 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
4902 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
4903 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00004904 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905 desc_idx, *post_ptr);
4906 drop_it_no_recycle:
4907 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00004908 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909 goto next_pkt;
4910 }
4911
Matt Carlsonad829262008-11-21 17:16:16 -08004912 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
4913 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914
Matt Carlsond2757fc2010-04-12 06:58:27 +00004915 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 int skb_size;
4917
Matt Carlson86b21e52009-11-13 13:03:45 +00004918 skb_size = tg3_alloc_rx_skb(tp, tpr, opaque_key,
Matt Carlsonafc081f2009-11-13 13:03:43 +00004919 *post_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920 if (skb_size < 0)
4921 goto drop_it;
4922
Matt Carlson287be122009-08-28 13:58:46 +00004923 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924 PCI_DMA_FROMDEVICE);
4925
Matt Carlson61e800c2010-02-17 15:16:54 +00004926 /* Ensure that the update to the skb happens
4927 * after the usage of the old DMA mapping.
4928 */
4929 smp_wmb();
4930
4931 ri->skb = NULL;
4932
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933 skb_put(skb, len);
4934 } else {
4935 struct sk_buff *copy_skb;
4936
Matt Carlsona3896162009-11-13 13:03:44 +00004937 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938 desc_idx, *post_ptr);
4939
Matt Carlsonbf933c82011-01-25 15:58:49 +00004940 copy_skb = netdev_alloc_skb(tp->dev, len +
Matt Carlson9dc7a112010-04-12 06:58:28 +00004941 TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 if (copy_skb == NULL)
4943 goto drop_it_no_recycle;
4944
Matt Carlsonbf933c82011-01-25 15:58:49 +00004945 skb_reserve(copy_skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 skb_put(copy_skb, len);
4947 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03004948 skb_copy_from_linear_data(skb, copy_skb->data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
4950
4951 /* We'll reuse the original ring buffer. */
4952 skb = copy_skb;
4953 }
4954
Michał Mirosławdc668912011-04-07 03:35:07 +00004955 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
4957 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
4958 >> RXD_TCPCSUM_SHIFT) == 0xffff))
4959 skb->ip_summed = CHECKSUM_UNNECESSARY;
4960 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07004961 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962
4963 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00004964
4965 if (len > (tp->dev->mtu + ETH_HLEN) &&
4966 skb->protocol != htons(ETH_P_8021Q)) {
4967 dev_kfree_skb(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00004968 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00004969 }
4970
Matt Carlson9dc7a112010-04-12 06:58:28 +00004971 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00004972 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
4973 __vlan_hwaccel_put_tag(skb,
4974 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00004975
Matt Carlsonbf933c82011-01-25 15:58:49 +00004976 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978 received++;
4979 budget--;
4980
4981next_pkt:
4982 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07004983
4984 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00004985 tpr->rx_std_prod_idx = std_prod_idx &
4986 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00004987 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
4988 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07004989 work_mask &= ~RXD_OPAQUE_RING_STD;
4990 rx_std_posted = 0;
4991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07004993 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00004994 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07004995
4996 /* Refresh hw_idx to see if there is new work */
4997 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004998 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07004999 rmb();
5000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001 }
5002
5003 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00005004 tnapi->rx_rcb_ptr = sw_idx;
5005 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006
5007 /* Refill RX ring(s). */
Matt Carlsone4af1af2010-02-12 14:47:05 +00005008 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005009 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005010 tpr->rx_std_prod_idx = std_prod_idx &
5011 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005012 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5013 tpr->rx_std_prod_idx);
5014 }
5015 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005016 tpr->rx_jmb_prod_idx = jmb_prod_idx &
5017 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005018 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5019 tpr->rx_jmb_prod_idx);
5020 }
5021 mmiowb();
5022 } else if (work_mask) {
5023 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
5024 * updated before the producer indices can be updated.
5025 */
5026 smp_wmb();
5027
Matt Carlson2c49a442010-09-30 10:34:35 +00005028 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
5029 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005030
Matt Carlsone4af1af2010-02-12 14:47:05 +00005031 if (tnapi != &tp->napi[1])
5032 napi_schedule(&tp->napi[1].napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034
5035 return received;
5036}
5037
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005038static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040 /* handle link change and other phy events */
5041 if (!(tp->tg3_flags &
5042 (TG3_FLAG_USE_LINKCHG_REG |
5043 TG3_FLAG_POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005044 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
5045
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046 if (sblk->status & SD_STATUS_LINK_CHG) {
5047 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005048 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07005049 spin_lock(&tp->lock);
Matt Carlsondd477002008-05-25 23:45:58 -07005050 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
5051 tw32_f(MAC_STATUS,
5052 (MAC_STATUS_SYNC_CHANGED |
5053 MAC_STATUS_CFG_CHANGED |
5054 MAC_STATUS_MI_COMPLETION |
5055 MAC_STATUS_LNKSTATE_CHANGED));
5056 udelay(40);
5057 } else
5058 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07005059 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060 }
5061 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005062}
5063
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005064static int tg3_rx_prodring_xfer(struct tg3 *tp,
5065 struct tg3_rx_prodring_set *dpr,
5066 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005067{
5068 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005069 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005070
5071 while (1) {
5072 src_prod_idx = spr->rx_std_prod_idx;
5073
5074 /* Make sure updates to the rx_std_buffers[] entries and the
5075 * standard producer index are seen in the correct order.
5076 */
5077 smp_rmb();
5078
5079 if (spr->rx_std_cons_idx == src_prod_idx)
5080 break;
5081
5082 if (spr->rx_std_cons_idx < src_prod_idx)
5083 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
5084 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005085 cpycnt = tp->rx_std_ring_mask + 1 -
5086 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005087
Matt Carlson2c49a442010-09-30 10:34:35 +00005088 cpycnt = min(cpycnt,
5089 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005090
5091 si = spr->rx_std_cons_idx;
5092 di = dpr->rx_std_prod_idx;
5093
Matt Carlsone92967b2010-02-12 14:47:06 +00005094 for (i = di; i < di + cpycnt; i++) {
5095 if (dpr->rx_std_buffers[i].skb) {
5096 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005097 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005098 break;
5099 }
5100 }
5101
5102 if (!cpycnt)
5103 break;
5104
5105 /* Ensure that updates to the rx_std_buffers ring and the
5106 * shadowed hardware producer ring from tg3_recycle_skb() are
5107 * ordered correctly WRT the skb check above.
5108 */
5109 smp_rmb();
5110
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005111 memcpy(&dpr->rx_std_buffers[di],
5112 &spr->rx_std_buffers[si],
5113 cpycnt * sizeof(struct ring_info));
5114
5115 for (i = 0; i < cpycnt; i++, di++, si++) {
5116 struct tg3_rx_buffer_desc *sbd, *dbd;
5117 sbd = &spr->rx_std[si];
5118 dbd = &dpr->rx_std[di];
5119 dbd->addr_hi = sbd->addr_hi;
5120 dbd->addr_lo = sbd->addr_lo;
5121 }
5122
Matt Carlson2c49a442010-09-30 10:34:35 +00005123 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
5124 tp->rx_std_ring_mask;
5125 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
5126 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005127 }
5128
5129 while (1) {
5130 src_prod_idx = spr->rx_jmb_prod_idx;
5131
5132 /* Make sure updates to the rx_jmb_buffers[] entries and
5133 * the jumbo producer index are seen in the correct order.
5134 */
5135 smp_rmb();
5136
5137 if (spr->rx_jmb_cons_idx == src_prod_idx)
5138 break;
5139
5140 if (spr->rx_jmb_cons_idx < src_prod_idx)
5141 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
5142 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005143 cpycnt = tp->rx_jmb_ring_mask + 1 -
5144 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005145
5146 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00005147 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005148
5149 si = spr->rx_jmb_cons_idx;
5150 di = dpr->rx_jmb_prod_idx;
5151
Matt Carlsone92967b2010-02-12 14:47:06 +00005152 for (i = di; i < di + cpycnt; i++) {
5153 if (dpr->rx_jmb_buffers[i].skb) {
5154 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005155 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005156 break;
5157 }
5158 }
5159
5160 if (!cpycnt)
5161 break;
5162
5163 /* Ensure that updates to the rx_jmb_buffers ring and the
5164 * shadowed hardware producer ring from tg3_recycle_skb() are
5165 * ordered correctly WRT the skb check above.
5166 */
5167 smp_rmb();
5168
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005169 memcpy(&dpr->rx_jmb_buffers[di],
5170 &spr->rx_jmb_buffers[si],
5171 cpycnt * sizeof(struct ring_info));
5172
5173 for (i = 0; i < cpycnt; i++, di++, si++) {
5174 struct tg3_rx_buffer_desc *sbd, *dbd;
5175 sbd = &spr->rx_jmb[si].std;
5176 dbd = &dpr->rx_jmb[di].std;
5177 dbd->addr_hi = sbd->addr_hi;
5178 dbd->addr_lo = sbd->addr_lo;
5179 }
5180
Matt Carlson2c49a442010-09-30 10:34:35 +00005181 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
5182 tp->rx_jmb_ring_mask;
5183 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
5184 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005185 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005186
5187 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005188}
5189
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005190static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
5191{
5192 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193
5194 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005195 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00005196 tg3_tx(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07005197 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
Michael Chan4fd7ab52007-10-12 01:39:50 -07005198 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199 }
5200
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201 /* run RX thread, within the bounds set by NAPI.
5202 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005203 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005205 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00005206 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005208 if ((tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005209 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005210 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00005211 u32 std_prod_idx = dpr->rx_std_prod_idx;
5212 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005213
Matt Carlsone4af1af2010-02-12 14:47:05 +00005214 for (i = 1; i < tp->irq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005215 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00005216 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005217
5218 wmb();
5219
Matt Carlsone4af1af2010-02-12 14:47:05 +00005220 if (std_prod_idx != dpr->rx_std_prod_idx)
5221 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5222 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005223
Matt Carlsone4af1af2010-02-12 14:47:05 +00005224 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
5225 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5226 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005227
5228 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005229
5230 if (err)
5231 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005232 }
5233
David S. Miller6f535762007-10-11 18:08:29 -07005234 return work_done;
5235}
David S. Millerf7383c22005-05-18 22:50:53 -07005236
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005237static int tg3_poll_msix(struct napi_struct *napi, int budget)
5238{
5239 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5240 struct tg3 *tp = tnapi->tp;
5241 int work_done = 0;
5242 struct tg3_hw_status *sblk = tnapi->hw_status;
5243
5244 while (1) {
5245 work_done = tg3_poll_work(tnapi, work_done, budget);
5246
5247 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
5248 goto tx_recovery;
5249
5250 if (unlikely(work_done >= budget))
5251 break;
5252
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005253 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005254 * to tell the hw how much work has been processed,
5255 * so we must read it before checking for more work.
5256 */
5257 tnapi->last_tag = sblk->status_tag;
5258 tnapi->last_irq_tag = tnapi->last_tag;
5259 rmb();
5260
5261 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00005262 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
5263 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005264 napi_complete(napi);
5265 /* Reenable interrupts. */
5266 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
5267 mmiowb();
5268 break;
5269 }
5270 }
5271
5272 return work_done;
5273
5274tx_recovery:
5275 /* work_done is guaranteed to be less than budget. */
5276 napi_complete(napi);
5277 schedule_work(&tp->reset_task);
5278 return work_done;
5279}
5280
Matt Carlsone64de4e2011-04-13 11:05:05 +00005281static void tg3_process_error(struct tg3 *tp)
5282{
5283 u32 val;
5284 bool real_error = false;
5285
5286 if (tp->tg3_flags & TG3_FLAG_ERROR_PROCESSED)
5287 return;
5288
5289 /* Check Flow Attention register */
5290 val = tr32(HOSTCC_FLOW_ATTN);
5291 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
5292 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
5293 real_error = true;
5294 }
5295
5296 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
5297 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
5298 real_error = true;
5299 }
5300
5301 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
5302 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
5303 real_error = true;
5304 }
5305
5306 if (!real_error)
5307 return;
5308
5309 tg3_dump_state(tp);
5310
5311 tp->tg3_flags |= TG3_FLAG_ERROR_PROCESSED;
5312 schedule_work(&tp->reset_task);
5313}
5314
David S. Miller6f535762007-10-11 18:08:29 -07005315static int tg3_poll(struct napi_struct *napi, int budget)
5316{
Matt Carlson8ef04422009-08-28 14:01:37 +00005317 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5318 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07005319 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00005320 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07005321
5322 while (1) {
Matt Carlsone64de4e2011-04-13 11:05:05 +00005323 if (sblk->status & SD_STATUS_ERROR)
5324 tg3_process_error(tp);
5325
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005326 tg3_poll_link(tp);
5327
Matt Carlson17375d22009-08-28 14:02:18 +00005328 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07005329
5330 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
5331 goto tx_recovery;
5332
5333 if (unlikely(work_done >= budget))
5334 break;
5335
Michael Chan4fd7ab52007-10-12 01:39:50 -07005336 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) {
Matt Carlson17375d22009-08-28 14:02:18 +00005337 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07005338 * to tell the hw how much work has been processed,
5339 * so we must read it before checking for more work.
5340 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005341 tnapi->last_tag = sblk->status_tag;
5342 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07005343 rmb();
5344 } else
5345 sblk->status &= ~SD_STATUS_UPDATED;
5346
Matt Carlson17375d22009-08-28 14:02:18 +00005347 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005348 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00005349 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07005350 break;
5351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352 }
5353
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005354 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07005355
5356tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07005357 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08005358 napi_complete(napi);
David S. Miller6f535762007-10-11 18:08:29 -07005359 schedule_work(&tp->reset_task);
Michael Chan4fd7ab52007-10-12 01:39:50 -07005360 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361}
5362
Matt Carlson66cfd1b2010-09-30 10:34:30 +00005363static void tg3_napi_disable(struct tg3 *tp)
5364{
5365 int i;
5366
5367 for (i = tp->irq_cnt - 1; i >= 0; i--)
5368 napi_disable(&tp->napi[i].napi);
5369}
5370
5371static void tg3_napi_enable(struct tg3 *tp)
5372{
5373 int i;
5374
5375 for (i = 0; i < tp->irq_cnt; i++)
5376 napi_enable(&tp->napi[i].napi);
5377}
5378
5379static void tg3_napi_init(struct tg3 *tp)
5380{
5381 int i;
5382
5383 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
5384 for (i = 1; i < tp->irq_cnt; i++)
5385 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
5386}
5387
5388static void tg3_napi_fini(struct tg3 *tp)
5389{
5390 int i;
5391
5392 for (i = 0; i < tp->irq_cnt; i++)
5393 netif_napi_del(&tp->napi[i].napi);
5394}
5395
5396static inline void tg3_netif_stop(struct tg3 *tp)
5397{
5398 tp->dev->trans_start = jiffies; /* prevent tx timeout */
5399 tg3_napi_disable(tp);
5400 netif_tx_disable(tp->dev);
5401}
5402
5403static inline void tg3_netif_start(struct tg3 *tp)
5404{
5405 /* NOTE: unconditional netif_tx_wake_all_queues is only
5406 * appropriate so long as all callers are assured to
5407 * have free tx slots (such as after tg3_init_hw)
5408 */
5409 netif_tx_wake_all_queues(tp->dev);
5410
5411 tg3_napi_enable(tp);
5412 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
5413 tg3_enable_ints(tp);
5414}
5415
David S. Millerf47c11e2005-06-24 20:18:35 -07005416static void tg3_irq_quiesce(struct tg3 *tp)
5417{
Matt Carlson4f125f42009-09-01 12:55:02 +00005418 int i;
5419
David S. Millerf47c11e2005-06-24 20:18:35 -07005420 BUG_ON(tp->irq_sync);
5421
5422 tp->irq_sync = 1;
5423 smp_mb();
5424
Matt Carlson4f125f42009-09-01 12:55:02 +00005425 for (i = 0; i < tp->irq_cnt; i++)
5426 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07005427}
5428
David S. Millerf47c11e2005-06-24 20:18:35 -07005429/* Fully shutdown all tg3 driver activity elsewhere in the system.
5430 * If irq_sync is non-zero, then the IRQ handler must be synchronized
5431 * with as well. Most of the time, this is not necessary except when
5432 * shutting down the device.
5433 */
5434static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
5435{
Michael Chan46966542007-07-11 19:47:19 -07005436 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07005437 if (irq_sync)
5438 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07005439}
5440
5441static inline void tg3_full_unlock(struct tg3 *tp)
5442{
David S. Millerf47c11e2005-06-24 20:18:35 -07005443 spin_unlock_bh(&tp->lock);
5444}
5445
Michael Chanfcfa0a32006-03-20 22:28:41 -08005446/* One-shot MSI handler - Chip automatically disables interrupt
5447 * after sending MSI so driver doesn't have to do it.
5448 */
David Howells7d12e782006-10-05 14:55:46 +01005449static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08005450{
Matt Carlson09943a12009-08-28 14:01:57 +00005451 struct tg3_napi *tnapi = dev_id;
5452 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08005453
Matt Carlson898a56f2009-08-28 14:02:40 +00005454 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005455 if (tnapi->rx_rcb)
5456 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005457
5458 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005459 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005460
5461 return IRQ_HANDLED;
5462}
5463
Michael Chan88b06bc2005-04-21 17:13:25 -07005464/* MSI ISR - No need to check for interrupt sharing and no need to
5465 * flush status block and interrupt mailbox. PCI ordering rules
5466 * guarantee that MSI will arrive after the status block.
5467 */
David Howells7d12e782006-10-05 14:55:46 +01005468static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc2005-04-21 17:13:25 -07005469{
Matt Carlson09943a12009-08-28 14:01:57 +00005470 struct tg3_napi *tnapi = dev_id;
5471 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc2005-04-21 17:13:25 -07005472
Matt Carlson898a56f2009-08-28 14:02:40 +00005473 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005474 if (tnapi->rx_rcb)
5475 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc2005-04-21 17:13:25 -07005476 /*
David S. Millerfac9b832005-05-18 22:46:34 -07005477 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc2005-04-21 17:13:25 -07005478 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07005479 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc2005-04-21 17:13:25 -07005480 * NIC to stop sending us irqs, engaging "in-intr-handler"
5481 * event coalescing.
5482 */
5483 tw32_mailbox(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07005484 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005485 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07005486
Michael Chan88b06bc2005-04-21 17:13:25 -07005487 return IRQ_RETVAL(1);
5488}
5489
David Howells7d12e782006-10-05 14:55:46 +01005490static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491{
Matt Carlson09943a12009-08-28 14:01:57 +00005492 struct tg3_napi *tnapi = dev_id;
5493 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005494 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495 unsigned int handled = 1;
5496
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497 /* In INTx mode, it is possible for the interrupt to arrive at
5498 * the CPU before the status block posted prior to the interrupt.
5499 * Reading the PCI State register will confirm whether the
5500 * interrupt is ours and will flush the status block.
5501 */
Michael Chand18edcb2007-03-24 20:57:11 -07005502 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
5503 if ((tp->tg3_flags & TG3_FLAG_CHIP_RESETTING) ||
5504 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5505 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005506 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07005507 }
Michael Chand18edcb2007-03-24 20:57:11 -07005508 }
5509
5510 /*
5511 * Writing any value to intr-mbox-0 clears PCI INTA# and
5512 * chip-internal interrupt pending events.
5513 * Writing non-zero to intr-mbox-0 additional tells the
5514 * NIC to stop sending us irqs, engaging "in-intr-handler"
5515 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005516 *
5517 * Flush the mailbox to de-assert the IRQ immediately to prevent
5518 * spurious interrupts. The flush impacts performance but
5519 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005520 */
Michael Chanc04cb342007-05-07 00:26:15 -07005521 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07005522 if (tg3_irq_sync(tp))
5523 goto out;
5524 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00005525 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00005526 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00005527 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07005528 } else {
5529 /* No work, shared interrupt perhaps? re-enable
5530 * interrupts, and flush that PCI write
5531 */
5532 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
5533 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07005534 }
David S. Millerf47c11e2005-06-24 20:18:35 -07005535out:
David S. Millerfac9b832005-05-18 22:46:34 -07005536 return IRQ_RETVAL(handled);
5537}
5538
David Howells7d12e782006-10-05 14:55:46 +01005539static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07005540{
Matt Carlson09943a12009-08-28 14:01:57 +00005541 struct tg3_napi *tnapi = dev_id;
5542 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005543 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07005544 unsigned int handled = 1;
5545
David S. Millerfac9b832005-05-18 22:46:34 -07005546 /* In INTx mode, it is possible for the interrupt to arrive at
5547 * the CPU before the status block posted prior to the interrupt.
5548 * Reading the PCI State register will confirm whether the
5549 * interrupt is ours and will flush the status block.
5550 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005551 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Michael Chand18edcb2007-03-24 20:57:11 -07005552 if ((tp->tg3_flags & TG3_FLAG_CHIP_RESETTING) ||
5553 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5554 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005555 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556 }
Michael Chand18edcb2007-03-24 20:57:11 -07005557 }
5558
5559 /*
5560 * writing any value to intr-mbox-0 clears PCI INTA# and
5561 * chip-internal interrupt pending events.
5562 * writing non-zero to intr-mbox-0 additional tells the
5563 * NIC to stop sending us irqs, engaging "in-intr-handler"
5564 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005565 *
5566 * Flush the mailbox to de-assert the IRQ immediately to prevent
5567 * spurious interrupts. The flush impacts performance but
5568 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005569 */
Michael Chanc04cb342007-05-07 00:26:15 -07005570 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00005571
5572 /*
5573 * In a shared interrupt configuration, sometimes other devices'
5574 * interrupts will scream. We record the current status tag here
5575 * so that the above check can report that the screaming interrupts
5576 * are unhandled. Eventually they will be silenced.
5577 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005578 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00005579
Michael Chand18edcb2007-03-24 20:57:11 -07005580 if (tg3_irq_sync(tp))
5581 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00005582
Matt Carlson72334482009-08-28 14:03:01 +00005583 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00005584
Matt Carlson09943a12009-08-28 14:01:57 +00005585 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00005586
David S. Millerf47c11e2005-06-24 20:18:35 -07005587out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588 return IRQ_RETVAL(handled);
5589}
5590
Michael Chan79381092005-04-21 17:13:59 -07005591/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01005592static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07005593{
Matt Carlson09943a12009-08-28 14:01:57 +00005594 struct tg3_napi *tnapi = dev_id;
5595 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005596 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07005597
Michael Chanf9804dd2005-09-27 12:13:10 -07005598 if ((sblk->status & SD_STATUS_UPDATED) ||
5599 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07005600 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07005601 return IRQ_RETVAL(1);
5602 }
5603 return IRQ_RETVAL(0);
5604}
5605
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07005606static int tg3_init_hw(struct tg3 *, int);
Michael Chan944d9802005-05-29 14:57:48 -07005607static int tg3_halt(struct tg3 *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608
Michael Chanb9ec6c12006-07-25 16:37:27 -07005609/* Restart hardware after configuration changes, self-test, etc.
5610 * Invoked with tp->lock held.
5611 */
5612static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
Eric Dumazet78c61462008-04-24 23:33:06 -07005613 __releases(tp->lock)
5614 __acquires(tp->lock)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005615{
5616 int err;
5617
5618 err = tg3_init_hw(tp, reset_phy);
5619 if (err) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00005620 netdev_err(tp->dev,
5621 "Failed to re-initialize device, aborting\n");
Michael Chanb9ec6c12006-07-25 16:37:27 -07005622 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
5623 tg3_full_unlock(tp);
5624 del_timer_sync(&tp->timer);
5625 tp->irq_sync = 0;
Matt Carlsonfed97812009-09-01 13:10:19 +00005626 tg3_napi_enable(tp);
Michael Chanb9ec6c12006-07-25 16:37:27 -07005627 dev_close(tp->dev);
5628 tg3_full_lock(tp, 0);
5629 }
5630 return err;
5631}
5632
Linus Torvalds1da177e2005-04-16 15:20:36 -07005633#ifdef CONFIG_NET_POLL_CONTROLLER
5634static void tg3_poll_controller(struct net_device *dev)
5635{
Matt Carlson4f125f42009-09-01 12:55:02 +00005636 int i;
Michael Chan88b06bc2005-04-21 17:13:25 -07005637 struct tg3 *tp = netdev_priv(dev);
5638
Matt Carlson4f125f42009-09-01 12:55:02 +00005639 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00005640 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641}
5642#endif
5643
David Howellsc4028952006-11-22 14:57:56 +00005644static void tg3_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645{
David Howellsc4028952006-11-22 14:57:56 +00005646 struct tg3 *tp = container_of(work, struct tg3, reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005647 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648 unsigned int restart_timer;
5649
Michael Chan7faa0062006-02-02 17:29:28 -08005650 tg3_full_lock(tp, 0);
Michael Chan7faa0062006-02-02 17:29:28 -08005651
5652 if (!netif_running(tp->dev)) {
Michael Chan7faa0062006-02-02 17:29:28 -08005653 tg3_full_unlock(tp);
5654 return;
5655 }
5656
5657 tg3_full_unlock(tp);
5658
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005659 tg3_phy_stop(tp);
5660
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661 tg3_netif_stop(tp);
5662
David S. Millerf47c11e2005-06-24 20:18:35 -07005663 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664
5665 restart_timer = tp->tg3_flags2 & TG3_FLG2_RESTART_TIMER;
5666 tp->tg3_flags2 &= ~TG3_FLG2_RESTART_TIMER;
5667
Michael Chandf3e6542006-05-26 17:48:07 -07005668 if (tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING) {
5669 tp->write32_tx_mbox = tg3_write32_tx_mbox;
5670 tp->write32_rx_mbox = tg3_write_flush_reg32;
5671 tp->tg3_flags |= TG3_FLAG_MBOX_WRITE_REORDER;
5672 tp->tg3_flags &= ~TG3_FLAG_TX_RECOVERY_PENDING;
5673 }
5674
Michael Chan944d9802005-05-29 14:57:48 -07005675 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005676 err = tg3_init_hw(tp, 1);
5677 if (err)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005678 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679
5680 tg3_netif_start(tp);
5681
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682 if (restart_timer)
5683 mod_timer(&tp->timer, jiffies + 1);
Michael Chan7faa0062006-02-02 17:29:28 -08005684
Michael Chanb9ec6c12006-07-25 16:37:27 -07005685out:
Michael Chan7faa0062006-02-02 17:29:28 -08005686 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005687
5688 if (!err)
5689 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005690}
5691
5692static void tg3_tx_timeout(struct net_device *dev)
5693{
5694 struct tg3 *tp = netdev_priv(dev);
5695
Michael Chanb0408752007-02-13 12:18:30 -08005696 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00005697 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00005698 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08005699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700
5701 schedule_work(&tp->reset_task);
5702}
5703
Michael Chanc58ec932005-09-17 00:46:27 -07005704/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
5705static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
5706{
5707 u32 base = (u32) mapping & 0xffffffff;
5708
Eric Dumazet807540b2010-09-23 05:40:09 +00005709 return (base > 0xffffdcc0) && (base + len + 8 < base);
Michael Chanc58ec932005-09-17 00:46:27 -07005710}
5711
Michael Chan72f2afb2006-03-06 19:28:35 -08005712/* Test for DMA addresses > 40-bit */
5713static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
5714 int len)
5715{
5716#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Michael Chan6728a8e2006-03-27 23:16:49 -08005717 if (tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG)
Eric Dumazet807540b2010-09-23 05:40:09 +00005718 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08005719 return 0;
5720#else
5721 return 0;
5722#endif
5723}
5724
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005725static void tg3_set_txd(struct tg3_napi *, int, dma_addr_t, int, u32, u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005726
Michael Chan72f2afb2006-03-06 19:28:35 -08005727/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00005728static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
5729 struct sk_buff *skb, u32 last_plus_one,
5730 u32 *start, u32 base_flags, u32 mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731{
Matt Carlson24f4efd2009-11-13 13:03:35 +00005732 struct tg3 *tp = tnapi->tp;
Matt Carlson41588ba2008-04-19 18:12:33 -07005733 struct sk_buff *new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07005734 dma_addr_t new_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735 u32 entry = *start;
Michael Chanc58ec932005-09-17 00:46:27 -07005736 int i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737
Matt Carlson41588ba2008-04-19 18:12:33 -07005738 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
5739 new_skb = skb_copy(skb, GFP_ATOMIC);
5740 else {
5741 int more_headroom = 4 - ((unsigned long)skb->data & 3);
5742
5743 new_skb = skb_copy_expand(skb,
5744 skb_headroom(skb) + more_headroom,
5745 skb_tailroom(skb), GFP_ATOMIC);
5746 }
5747
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07005749 ret = -1;
5750 } else {
5751 /* New SKB is guaranteed to be linear. */
5752 entry = *start;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005753 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
5754 PCI_DMA_TODEVICE);
5755 /* Make sure the mapping succeeded */
5756 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
5757 ret = -1;
5758 dev_kfree_skb(new_skb);
5759 new_skb = NULL;
David S. Miller90079ce2008-09-11 04:52:51 -07005760
Michael Chanc58ec932005-09-17 00:46:27 -07005761 /* Make sure new skb does not cross any 4G boundaries.
5762 * Drop the packet if it does.
5763 */
Alexander Duyckf4188d82009-12-02 16:48:38 +00005764 } else if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
5765 tg3_4g_overflow_test(new_addr, new_skb->len)) {
5766 pci_unmap_single(tp->pdev, new_addr, new_skb->len,
5767 PCI_DMA_TODEVICE);
Michael Chanc58ec932005-09-17 00:46:27 -07005768 ret = -1;
5769 dev_kfree_skb(new_skb);
5770 new_skb = NULL;
5771 } else {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005772 tg3_set_txd(tnapi, entry, new_addr, new_skb->len,
Michael Chanc58ec932005-09-17 00:46:27 -07005773 base_flags, 1 | (mss << 1));
5774 *start = NEXT_TX(entry);
5775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005776 }
5777
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778 /* Now clean up the sw ring entries. */
5779 i = 0;
5780 while (entry != last_plus_one) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00005781 int len;
5782
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005783 if (i == 0)
Alexander Duyckf4188d82009-12-02 16:48:38 +00005784 len = skb_headlen(skb);
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005785 else
Alexander Duyckf4188d82009-12-02 16:48:38 +00005786 len = skb_shinfo(skb)->frags[i-1].size;
5787
5788 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005789 dma_unmap_addr(&tnapi->tx_buffers[entry],
Alexander Duyckf4188d82009-12-02 16:48:38 +00005790 mapping),
5791 len, PCI_DMA_TODEVICE);
5792 if (i == 0) {
5793 tnapi->tx_buffers[entry].skb = new_skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005794 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00005795 new_addr);
5796 } else {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005797 tnapi->tx_buffers[entry].skb = NULL;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799 entry = NEXT_TX(entry);
5800 i++;
5801 }
5802
5803 dev_kfree_skb(skb);
5804
Michael Chanc58ec932005-09-17 00:46:27 -07005805 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806}
5807
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005808static void tg3_set_txd(struct tg3_napi *tnapi, int entry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809 dma_addr_t mapping, int len, u32 flags,
5810 u32 mss_and_is_end)
5811{
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005812 struct tg3_tx_buffer_desc *txd = &tnapi->tx_ring[entry];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005813 int is_end = (mss_and_is_end & 0x1);
5814 u32 mss = (mss_and_is_end >> 1);
5815 u32 vlan_tag = 0;
5816
5817 if (is_end)
5818 flags |= TXD_FLAG_END;
5819 if (flags & TXD_FLAG_VLAN) {
5820 vlan_tag = flags >> 16;
5821 flags &= 0xffff;
5822 }
5823 vlan_tag |= (mss << TXD_MSS_SHIFT);
5824
5825 txd->addr_hi = ((u64) mapping >> 32);
5826 txd->addr_lo = ((u64) mapping & 0xffffffff);
5827 txd->len_flags = (len << TXD_LEN_SHIFT) | flags;
5828 txd->vlan_tag = vlan_tag << TXD_VLAN_TAG_SHIFT;
5829}
5830
Michael Chan5a6f3072006-03-20 22:28:05 -08005831/* hard_start_xmit for devices that don't have any bugs and
Matt Carlsone849cdc2009-11-13 13:03:38 +00005832 * support TG3_FLG2_HW_TSO_2 and TG3_FLG2_HW_TSO_3 only.
Michael Chan5a6f3072006-03-20 22:28:05 -08005833 */
Stephen Hemminger613573252009-08-31 19:50:58 +00005834static netdev_tx_t tg3_start_xmit(struct sk_buff *skb,
5835 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836{
5837 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005838 u32 len, entry, base_flags, mss;
David S. Miller90079ce2008-09-11 04:52:51 -07005839 dma_addr_t mapping;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005840 struct tg3_napi *tnapi;
5841 struct netdev_queue *txq;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005842 unsigned int i, last;
5843
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005844 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
5845 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Matt Carlson19cfaec2009-12-03 08:36:20 +00005846 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005847 tnapi++;
Michael Chan5a6f3072006-03-20 22:28:05 -08005848
Michael Chan00b70502006-06-17 21:58:45 -07005849 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005850 * and TX reclaim runs via tp->napi.poll inside of a software
Michael Chan5a6f3072006-03-20 22:28:05 -08005851 * interrupt. Furthermore, IRQ processing runs lockless so we have
5852 * no IRQ context deadlocks to worry about either. Rejoice!
5853 */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005854 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005855 if (!netif_tx_queue_stopped(txq)) {
5856 netif_tx_stop_queue(txq);
Michael Chan5a6f3072006-03-20 22:28:05 -08005857
5858 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00005859 netdev_err(dev,
5860 "BUG! Tx Ring full when queue awake!\n");
Michael Chan5a6f3072006-03-20 22:28:05 -08005861 }
Michael Chan5a6f3072006-03-20 22:28:05 -08005862 return NETDEV_TX_BUSY;
5863 }
5864
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005865 entry = tnapi->tx_prod;
Michael Chan5a6f3072006-03-20 22:28:05 -08005866 base_flags = 0;
Matt Carlsonbe98da62010-07-11 09:31:46 +00005867 mss = skb_shinfo(skb)->gso_size;
5868 if (mss) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005869 int tcp_opt_len, ip_tcp_len;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005870 u32 hdrlen;
Michael Chan5a6f3072006-03-20 22:28:05 -08005871
5872 if (skb_header_cloned(skb) &&
5873 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
5874 dev_kfree_skb(skb);
5875 goto out_unlock;
5876 }
5877
Matt Carlson02e96082010-09-15 08:59:59 +00005878 if (skb_is_gso_v6(skb)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005879 hdrlen = skb_headlen(skb) - ETH_HLEN;
Matt Carlson02e96082010-09-15 08:59:59 +00005880 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005881 struct iphdr *iph = ip_hdr(skb);
5882
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07005883 tcp_opt_len = tcp_optlen(skb);
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -03005884 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
Michael Chanb0026622006-07-03 19:42:14 -07005885
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005886 iph->check = 0;
5887 iph->tot_len = htons(mss + ip_tcp_len + tcp_opt_len);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005888 hdrlen = ip_tcp_len + tcp_opt_len;
Michael Chanb0026622006-07-03 19:42:14 -07005889 }
Michael Chan5a6f3072006-03-20 22:28:05 -08005890
Matt Carlsone849cdc2009-11-13 13:03:38 +00005891 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005892 mss |= (hdrlen & 0xc) << 12;
5893 if (hdrlen & 0x10)
5894 base_flags |= 0x00000010;
5895 base_flags |= (hdrlen & 0x3e0) << 5;
5896 } else
5897 mss |= hdrlen << 9;
5898
Michael Chan5a6f3072006-03-20 22:28:05 -08005899 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
5900 TXD_FLAG_CPU_POST_DMA);
5901
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005902 tcp_hdr(skb)->check = 0;
Michael Chan5a6f3072006-03-20 22:28:05 -08005903
Matt Carlson859a5882010-04-05 10:19:28 +00005904 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005905 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson859a5882010-04-05 10:19:28 +00005906 }
5907
Jesse Grosseab6d182010-10-20 13:56:03 +00005908 if (vlan_tx_tag_present(skb))
Michael Chan5a6f3072006-03-20 22:28:05 -08005909 base_flags |= (TXD_FLAG_VLAN |
5910 (vlan_tx_tag_get(skb) << 16));
Michael Chan5a6f3072006-03-20 22:28:05 -08005911
Alexander Duyckf4188d82009-12-02 16:48:38 +00005912 len = skb_headlen(skb);
5913
5914 /* Queue skb data, a.k.a. the main skb fragment. */
5915 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
5916 if (pci_dma_mapping_error(tp->pdev, mapping)) {
David S. Miller90079ce2008-09-11 04:52:51 -07005917 dev_kfree_skb(skb);
5918 goto out_unlock;
5919 }
5920
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005921 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005922 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005923
Matt Carlsonb703df62009-12-03 08:36:21 +00005924 if ((tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) &&
Matt Carlson8fc2f992010-12-06 08:28:49 +00005925 !mss && skb->len > VLAN_ETH_FRAME_LEN)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005926 base_flags |= TXD_FLAG_JMB_PKT;
5927
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005928 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
Michael Chan5a6f3072006-03-20 22:28:05 -08005929 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
5930
5931 entry = NEXT_TX(entry);
5932
5933 /* Now loop through additional data fragments, and queue them. */
5934 if (skb_shinfo(skb)->nr_frags > 0) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005935 last = skb_shinfo(skb)->nr_frags - 1;
5936 for (i = 0; i <= last; i++) {
5937 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5938
5939 len = frag->size;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005940 mapping = pci_map_page(tp->pdev,
5941 frag->page,
5942 frag->page_offset,
5943 len, PCI_DMA_TODEVICE);
5944 if (pci_dma_mapping_error(tp->pdev, mapping))
5945 goto dma_error;
5946
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005947 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005948 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00005949 mapping);
Michael Chan5a6f3072006-03-20 22:28:05 -08005950
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005951 tg3_set_txd(tnapi, entry, mapping, len,
Michael Chan5a6f3072006-03-20 22:28:05 -08005952 base_flags, (i == last) | (mss << 1));
5953
5954 entry = NEXT_TX(entry);
5955 }
5956 }
5957
5958 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005959 tw32_tx_mbox(tnapi->prodmbox, entry);
Michael Chan5a6f3072006-03-20 22:28:05 -08005960
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005961 tnapi->tx_prod = entry;
5962 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005963 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00005964
5965 /* netif_tx_stop_queue() must be done before checking
5966 * checking tx index in tg3_tx_avail() below, because in
5967 * tg3_tx(), we update tx index before checking for
5968 * netif_tx_queue_stopped().
5969 */
5970 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005971 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005972 netif_tx_wake_queue(txq);
Michael Chan5a6f3072006-03-20 22:28:05 -08005973 }
5974
5975out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00005976 mmiowb();
Michael Chan5a6f3072006-03-20 22:28:05 -08005977
5978 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005979
5980dma_error:
5981 last = i;
5982 entry = tnapi->tx_prod;
5983 tnapi->tx_buffers[entry].skb = NULL;
5984 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005985 dma_unmap_addr(&tnapi->tx_buffers[entry], mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005986 skb_headlen(skb),
5987 PCI_DMA_TODEVICE);
5988 for (i = 0; i <= last; i++) {
5989 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5990 entry = NEXT_TX(entry);
5991
5992 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005993 dma_unmap_addr(&tnapi->tx_buffers[entry],
Alexander Duyckf4188d82009-12-02 16:48:38 +00005994 mapping),
5995 frag->size, PCI_DMA_TODEVICE);
5996 }
5997
5998 dev_kfree_skb(skb);
5999 return NETDEV_TX_OK;
Michael Chan5a6f3072006-03-20 22:28:05 -08006000}
6001
Stephen Hemminger613573252009-08-31 19:50:58 +00006002static netdev_tx_t tg3_start_xmit_dma_bug(struct sk_buff *,
6003 struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07006004
6005/* Use GSO to workaround a rare TSO bug that may be triggered when the
6006 * TSO header is greater than 80 bytes.
6007 */
6008static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
6009{
6010 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006011 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07006012
6013 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006014 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07006015 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006016
6017 /* netif_tx_stop_queue() must be done before checking
6018 * checking tx index in tg3_tx_avail() below, because in
6019 * tg3_tx(), we update tx index before checking for
6020 * netif_tx_queue_stopped().
6021 */
6022 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006023 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08006024 return NETDEV_TX_BUSY;
6025
6026 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07006027 }
6028
6029 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07006030 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07006031 goto tg3_tso_bug_end;
6032
6033 do {
6034 nskb = segs;
6035 segs = segs->next;
6036 nskb->next = NULL;
6037 tg3_start_xmit_dma_bug(nskb, tp->dev);
6038 } while (segs);
6039
6040tg3_tso_bug_end:
6041 dev_kfree_skb(skb);
6042
6043 return NETDEV_TX_OK;
6044}
Michael Chan52c0fd82006-06-29 20:15:54 -07006045
Michael Chan5a6f3072006-03-20 22:28:05 -08006046/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
6047 * support TG3_FLG2_HW_TSO_1 or firmware TSO only.
6048 */
Stephen Hemminger613573252009-08-31 19:50:58 +00006049static netdev_tx_t tg3_start_xmit_dma_bug(struct sk_buff *skb,
6050 struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08006051{
6052 struct tg3 *tp = netdev_priv(dev);
Michael Chan5a6f3072006-03-20 22:28:05 -08006053 u32 len, entry, base_flags, mss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006054 int would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07006055 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00006056 struct tg3_napi *tnapi;
6057 struct netdev_queue *txq;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006058 unsigned int i, last;
6059
Matt Carlson24f4efd2009-11-13 13:03:35 +00006060 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
6061 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Matt Carlson19cfaec2009-12-03 08:36:20 +00006062 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlson24f4efd2009-11-13 13:03:35 +00006063 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006064
Michael Chan00b70502006-06-17 21:58:45 -07006065 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006066 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07006067 * interrupt. Furthermore, IRQ processing runs lockless so we have
6068 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069 */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006070 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006071 if (!netif_tx_queue_stopped(txq)) {
6072 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08006073
6074 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00006075 netdev_err(dev,
6076 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08006077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006078 return NETDEV_TX_BUSY;
6079 }
6080
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006081 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07006083 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00006085
Matt Carlsonbe98da62010-07-11 09:31:46 +00006086 mss = skb_shinfo(skb)->gso_size;
6087 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006088 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00006089 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006090
6091 if (skb_header_cloned(skb) &&
6092 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
6093 dev_kfree_skb(skb);
6094 goto out_unlock;
6095 }
6096
Matt Carlson34195c32010-07-11 09:31:42 +00006097 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07006098 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006099
Matt Carlson02e96082010-09-15 08:59:59 +00006100 if (skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00006101 hdr_len = skb_headlen(skb) - ETH_HLEN;
6102 } else {
6103 u32 ip_tcp_len;
6104
6105 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
6106 hdr_len = ip_tcp_len + tcp_opt_len;
6107
6108 iph->check = 0;
6109 iph->tot_len = htons(mss + hdr_len);
6110 }
6111
Michael Chan52c0fd82006-06-29 20:15:54 -07006112 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Michael Chan7f62ad52007-02-20 23:25:40 -08006113 (tp->tg3_flags2 & TG3_FLG2_TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00006114 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07006115
Linus Torvalds1da177e2005-04-16 15:20:36 -07006116 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
6117 TXD_FLAG_CPU_POST_DMA);
6118
Linus Torvalds1da177e2005-04-16 15:20:36 -07006119 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006120 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006121 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006122 } else
6123 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
6124 iph->daddr, 0,
6125 IPPROTO_TCP,
6126 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006127
Matt Carlson615774f2009-11-13 13:03:39 +00006128 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) {
6129 mss |= (hdr_len & 0xc) << 12;
6130 if (hdr_len & 0x10)
6131 base_flags |= 0x00000010;
6132 base_flags |= (hdr_len & 0x3e0) << 5;
6133 } else if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_2)
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006134 mss |= hdr_len << 9;
6135 else if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_1) ||
6136 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006137 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006138 int tsflags;
6139
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006140 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006141 mss |= (tsflags << 11);
6142 }
6143 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006144 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006145 int tsflags;
6146
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006147 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006148 base_flags |= tsflags << 12;
6149 }
6150 }
6151 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00006152
Jesse Grosseab6d182010-10-20 13:56:03 +00006153 if (vlan_tx_tag_present(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006154 base_flags |= (TXD_FLAG_VLAN |
6155 (vlan_tx_tag_get(skb) << 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006156
Matt Carlsonb703df62009-12-03 08:36:21 +00006157 if ((tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) &&
Matt Carlson8fc2f992010-12-06 08:28:49 +00006158 !mss && skb->len > VLAN_ETH_FRAME_LEN)
Matt Carlson615774f2009-11-13 13:03:39 +00006159 base_flags |= TXD_FLAG_JMB_PKT;
6160
Alexander Duyckf4188d82009-12-02 16:48:38 +00006161 len = skb_headlen(skb);
6162
6163 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
6164 if (pci_dma_mapping_error(tp->pdev, mapping)) {
David S. Miller90079ce2008-09-11 04:52:51 -07006165 dev_kfree_skb(skb);
6166 goto out_unlock;
6167 }
6168
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006169 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006170 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006171
6172 would_hit_hwbug = 0;
6173
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006174 if ((tp->tg3_flags3 & TG3_FLG3_SHORT_DMA_BUG) && len <= 8)
6175 would_hit_hwbug = 1;
6176
Matt Carlson0e1406d2009-11-02 12:33:33 +00006177 if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
6178 tg3_4g_overflow_test(mapping, len))
Matt Carlson41588ba2008-04-19 18:12:33 -07006179 would_hit_hwbug = 1;
Matt Carlson0e1406d2009-11-02 12:33:33 +00006180
6181 if ((tp->tg3_flags3 & TG3_FLG3_40BIT_DMA_LIMIT_BUG) &&
6182 tg3_40bit_overflow_test(tp, mapping, len))
6183 would_hit_hwbug = 1;
6184
6185 if (tp->tg3_flags3 & TG3_FLG3_5701_DMA_BUG)
Michael Chanc58ec932005-09-17 00:46:27 -07006186 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006187
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006188 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006189 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
6190
6191 entry = NEXT_TX(entry);
6192
6193 /* Now loop through additional data fragments, and queue them. */
6194 if (skb_shinfo(skb)->nr_frags > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006195 last = skb_shinfo(skb)->nr_frags - 1;
6196 for (i = 0; i <= last; i++) {
6197 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6198
6199 len = frag->size;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006200 mapping = pci_map_page(tp->pdev,
6201 frag->page,
6202 frag->page_offset,
6203 len, PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006204
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006205 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006206 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00006207 mapping);
6208 if (pci_dma_mapping_error(tp->pdev, mapping))
6209 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006210
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006211 if ((tp->tg3_flags3 & TG3_FLG3_SHORT_DMA_BUG) &&
6212 len <= 8)
6213 would_hit_hwbug = 1;
6214
Matt Carlson0e1406d2009-11-02 12:33:33 +00006215 if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
6216 tg3_4g_overflow_test(mapping, len))
Michael Chanc58ec932005-09-17 00:46:27 -07006217 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218
Matt Carlson0e1406d2009-11-02 12:33:33 +00006219 if ((tp->tg3_flags3 & TG3_FLG3_40BIT_DMA_LIMIT_BUG) &&
6220 tg3_40bit_overflow_test(tp, mapping, len))
Michael Chan72f2afb2006-03-06 19:28:35 -08006221 would_hit_hwbug = 1;
6222
Linus Torvalds1da177e2005-04-16 15:20:36 -07006223 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006224 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006225 base_flags, (i == last)|(mss << 1));
6226 else
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006227 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006228 base_flags, (i == last));
6229
6230 entry = NEXT_TX(entry);
6231 }
6232 }
6233
6234 if (would_hit_hwbug) {
6235 u32 last_plus_one = entry;
6236 u32 start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006237
Michael Chanc58ec932005-09-17 00:46:27 -07006238 start = entry - 1 - skb_shinfo(skb)->nr_frags;
6239 start &= (TG3_TX_RING_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006240
6241 /* If the workaround fails due to memory/mapping
6242 * failure, silently drop this packet.
6243 */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006244 if (tigon3_dma_hwbug_workaround(tnapi, skb, last_plus_one,
Michael Chanc58ec932005-09-17 00:46:27 -07006245 &start, base_flags, mss))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246 goto out_unlock;
6247
6248 entry = start;
6249 }
6250
6251 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006252 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006253
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006254 tnapi->tx_prod = entry;
6255 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006256 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006257
6258 /* netif_tx_stop_queue() must be done before checking
6259 * checking tx index in tg3_tx_avail() below, because in
6260 * tg3_tx(), we update tx index before checking for
6261 * netif_tx_queue_stopped().
6262 */
6263 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006264 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006265 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07006266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006267
6268out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00006269 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006270
6271 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006272
6273dma_error:
6274 last = i;
6275 entry = tnapi->tx_prod;
6276 tnapi->tx_buffers[entry].skb = NULL;
6277 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006278 dma_unmap_addr(&tnapi->tx_buffers[entry], mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006279 skb_headlen(skb),
6280 PCI_DMA_TODEVICE);
6281 for (i = 0; i <= last; i++) {
6282 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6283 entry = NEXT_TX(entry);
6284
6285 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006286 dma_unmap_addr(&tnapi->tx_buffers[entry],
Alexander Duyckf4188d82009-12-02 16:48:38 +00006287 mapping),
6288 frag->size, PCI_DMA_TODEVICE);
6289 }
6290
6291 dev_kfree_skb(skb);
6292 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006293}
6294
Michał Mirosławdc668912011-04-07 03:35:07 +00006295static u32 tg3_fix_features(struct net_device *dev, u32 features)
6296{
6297 struct tg3 *tp = netdev_priv(dev);
6298
6299 if (dev->mtu > ETH_DATA_LEN && (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
6300 features &= ~NETIF_F_ALL_TSO;
6301
6302 return features;
6303}
6304
Linus Torvalds1da177e2005-04-16 15:20:36 -07006305static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
6306 int new_mtu)
6307{
6308 dev->mtu = new_mtu;
6309
Michael Chanef7f5ec2005-07-25 12:32:25 -07006310 if (new_mtu > ETH_DATA_LEN) {
Michael Chana4e2b342005-10-26 15:46:52 -07006311 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
Michał Mirosławdc668912011-04-07 03:35:07 +00006312 netdev_update_features(dev);
Michael Chanef7f5ec2005-07-25 12:32:25 -07006313 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
Matt Carlson859a5882010-04-05 10:19:28 +00006314 } else {
Michael Chanef7f5ec2005-07-25 12:32:25 -07006315 tp->tg3_flags |= TG3_FLAG_JUMBO_RING_ENABLE;
Matt Carlson859a5882010-04-05 10:19:28 +00006316 }
Michael Chanef7f5ec2005-07-25 12:32:25 -07006317 } else {
Michał Mirosławdc668912011-04-07 03:35:07 +00006318 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
Michael Chanef7f5ec2005-07-25 12:32:25 -07006319 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
Michał Mirosławdc668912011-04-07 03:35:07 +00006320 netdev_update_features(dev);
6321 }
Michael Chan0f893dc2005-07-25 12:30:38 -07006322 tp->tg3_flags &= ~TG3_FLAG_JUMBO_RING_ENABLE;
Michael Chanef7f5ec2005-07-25 12:32:25 -07006323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006324}
6325
6326static int tg3_change_mtu(struct net_device *dev, int new_mtu)
6327{
6328 struct tg3 *tp = netdev_priv(dev);
Michael Chanb9ec6c12006-07-25 16:37:27 -07006329 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330
6331 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
6332 return -EINVAL;
6333
6334 if (!netif_running(dev)) {
6335 /* We'll just catch it later when the
6336 * device is up'd.
6337 */
6338 tg3_set_mtu(dev, tp, new_mtu);
6339 return 0;
6340 }
6341
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006342 tg3_phy_stop(tp);
6343
Linus Torvalds1da177e2005-04-16 15:20:36 -07006344 tg3_netif_stop(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07006345
6346 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006347
Michael Chan944d9802005-05-29 14:57:48 -07006348 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006349
6350 tg3_set_mtu(dev, tp, new_mtu);
6351
Michael Chanb9ec6c12006-07-25 16:37:27 -07006352 err = tg3_restart_hw(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006353
Michael Chanb9ec6c12006-07-25 16:37:27 -07006354 if (!err)
6355 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006356
David S. Millerf47c11e2005-06-24 20:18:35 -07006357 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006358
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006359 if (!err)
6360 tg3_phy_start(tp);
6361
Michael Chanb9ec6c12006-07-25 16:37:27 -07006362 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006363}
6364
Matt Carlson21f581a2009-08-28 14:00:25 +00006365static void tg3_rx_prodring_free(struct tg3 *tp,
6366 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006367{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006368 int i;
6369
Matt Carlson8fea32b2010-09-15 08:59:58 +00006370 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006371 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00006372 i = (i + 1) & tp->rx_std_ring_mask)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006373 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6374 tp->rx_pkt_map_sz);
6375
6376 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
6377 for (i = tpr->rx_jmb_cons_idx;
6378 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00006379 i = (i + 1) & tp->rx_jmb_ring_mask) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006380 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6381 TG3_RX_JMB_MAP_SZ);
6382 }
6383 }
6384
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006385 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006387
Matt Carlson2c49a442010-09-30 10:34:35 +00006388 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006389 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6390 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006391
Matt Carlson48035722010-10-14 10:37:43 +00006392 if ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) &&
6393 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006394 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006395 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6396 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006397 }
6398}
6399
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006400/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006401 *
6402 * The chip has been shut down and the driver detached from
6403 * the networking, so no interrupts or new tx packets will
6404 * end up in the driver. tp->{tx,}lock are held and thus
6405 * we may not sleep.
6406 */
Matt Carlson21f581a2009-08-28 14:00:25 +00006407static int tg3_rx_prodring_alloc(struct tg3 *tp,
6408 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006409{
Matt Carlson287be122009-08-28 13:58:46 +00006410 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006411
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006412 tpr->rx_std_cons_idx = 0;
6413 tpr->rx_std_prod_idx = 0;
6414 tpr->rx_jmb_cons_idx = 0;
6415 tpr->rx_jmb_prod_idx = 0;
6416
Matt Carlson8fea32b2010-09-15 08:59:58 +00006417 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006418 memset(&tpr->rx_std_buffers[0], 0,
6419 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00006420 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006421 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00006422 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006423 goto done;
6424 }
6425
Linus Torvalds1da177e2005-04-16 15:20:36 -07006426 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00006427 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006428
Matt Carlson287be122009-08-28 13:58:46 +00006429 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Michael Chana4e2b342005-10-26 15:46:52 -07006430 if ((tp->tg3_flags2 & TG3_FLG2_5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00006431 tp->dev->mtu > ETH_DATA_LEN)
6432 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
6433 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07006434
Linus Torvalds1da177e2005-04-16 15:20:36 -07006435 /* Initialize invariants of the rings, we only set this
6436 * stuff once. This works because the card does not
6437 * write into the rx buffer posting rings.
6438 */
Matt Carlson2c49a442010-09-30 10:34:35 +00006439 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006440 struct tg3_rx_buffer_desc *rxd;
6441
Matt Carlson21f581a2009-08-28 14:00:25 +00006442 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00006443 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006444 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
6445 rxd->opaque = (RXD_OPAQUE_RING_STD |
6446 (i << RXD_OPAQUE_INDEX_SHIFT));
6447 }
6448
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006449 /* Now allocate fresh SKBs for each rx ring. */
6450 for (i = 0; i < tp->rx_pending; i++) {
Matt Carlson86b21e52009-11-13 13:03:45 +00006451 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_STD, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006452 netdev_warn(tp->dev,
6453 "Using a smaller RX standard ring. Only "
6454 "%d out of %d buffers were allocated "
6455 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006456 if (i == 0)
6457 goto initfail;
6458 tp->rx_pending = i;
6459 break;
6460 }
6461 }
6462
Matt Carlson48035722010-10-14 10:37:43 +00006463 if (!(tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) ||
6464 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006465 goto done;
6466
Matt Carlson2c49a442010-09-30 10:34:35 +00006467 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006468
Matt Carlson0d86df82010-02-17 15:17:00 +00006469 if (!(tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE))
6470 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006471
Matt Carlson2c49a442010-09-30 10:34:35 +00006472 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00006473 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006474
Matt Carlson0d86df82010-02-17 15:17:00 +00006475 rxd = &tpr->rx_jmb[i].std;
6476 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
6477 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
6478 RXD_FLAG_JUMBO;
6479 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
6480 (i << RXD_OPAQUE_INDEX_SHIFT));
6481 }
6482
6483 for (i = 0; i < tp->rx_jumbo_pending; i++) {
6484 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_JUMBO, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006485 netdev_warn(tp->dev,
6486 "Using a smaller RX jumbo ring. Only %d "
6487 "out of %d buffers were allocated "
6488 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00006489 if (i == 0)
6490 goto initfail;
6491 tp->rx_jumbo_pending = i;
6492 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006493 }
6494 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006495
6496done:
Michael Chan32d8c572006-07-25 16:38:29 -07006497 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006498
6499initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00006500 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006501 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006502}
6503
Matt Carlson21f581a2009-08-28 14:00:25 +00006504static void tg3_rx_prodring_fini(struct tg3 *tp,
6505 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006506{
Matt Carlson21f581a2009-08-28 14:00:25 +00006507 kfree(tpr->rx_std_buffers);
6508 tpr->rx_std_buffers = NULL;
6509 kfree(tpr->rx_jmb_buffers);
6510 tpr->rx_jmb_buffers = NULL;
6511 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006512 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
6513 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00006514 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006515 }
Matt Carlson21f581a2009-08-28 14:00:25 +00006516 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006517 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
6518 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00006519 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006520 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006521}
6522
Matt Carlson21f581a2009-08-28 14:00:25 +00006523static int tg3_rx_prodring_init(struct tg3 *tp,
6524 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006525{
Matt Carlson2c49a442010-09-30 10:34:35 +00006526 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
6527 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006528 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006529 return -ENOMEM;
6530
Matt Carlson4bae65c2010-11-24 08:31:52 +00006531 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
6532 TG3_RX_STD_RING_BYTES(tp),
6533 &tpr->rx_std_mapping,
6534 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006535 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006536 goto err_out;
6537
Matt Carlson48035722010-10-14 10:37:43 +00006538 if ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) &&
6539 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006540 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00006541 GFP_KERNEL);
6542 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006543 goto err_out;
6544
Matt Carlson4bae65c2010-11-24 08:31:52 +00006545 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
6546 TG3_RX_JMB_RING_BYTES(tp),
6547 &tpr->rx_jmb_mapping,
6548 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006549 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006550 goto err_out;
6551 }
6552
6553 return 0;
6554
6555err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00006556 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006557 return -ENOMEM;
6558}
6559
6560/* Free up pending packets in all rx/tx rings.
6561 *
6562 * The chip has been shut down and the driver detached from
6563 * the networking, so no interrupts or new tx packets will
6564 * end up in the driver. tp->{tx,}lock is not held and we are not
6565 * in an interrupt context and thus may sleep.
6566 */
6567static void tg3_free_rings(struct tg3 *tp)
6568{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006569 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006570
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006571 for (j = 0; j < tp->irq_cnt; j++) {
6572 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006573
Matt Carlson8fea32b2010-09-15 08:59:58 +00006574 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00006575
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006576 if (!tnapi->tx_buffers)
6577 continue;
6578
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006579 for (i = 0; i < TG3_TX_RING_SIZE; ) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00006580 struct ring_info *txp;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006581 struct sk_buff *skb;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006582 unsigned int k;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006583
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006584 txp = &tnapi->tx_buffers[i];
6585 skb = txp->skb;
6586
6587 if (skb == NULL) {
6588 i++;
6589 continue;
6590 }
6591
Alexander Duyckf4188d82009-12-02 16:48:38 +00006592 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006593 dma_unmap_addr(txp, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006594 skb_headlen(skb),
6595 PCI_DMA_TODEVICE);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006596 txp->skb = NULL;
6597
Alexander Duyckf4188d82009-12-02 16:48:38 +00006598 i++;
6599
6600 for (k = 0; k < skb_shinfo(skb)->nr_frags; k++) {
6601 txp = &tnapi->tx_buffers[i & (TG3_TX_RING_SIZE - 1)];
6602 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006603 dma_unmap_addr(txp, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006604 skb_shinfo(skb)->frags[k].size,
6605 PCI_DMA_TODEVICE);
6606 i++;
6607 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006608
6609 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006610 }
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006611 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006612}
6613
6614/* Initialize tx/rx rings for packet processing.
6615 *
6616 * The chip has been shut down and the driver detached from
6617 * the networking, so no interrupts or new tx packets will
6618 * end up in the driver. tp->{tx,}lock are held and thus
6619 * we may not sleep.
6620 */
6621static int tg3_init_rings(struct tg3 *tp)
6622{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006623 int i;
Matt Carlson72334482009-08-28 14:03:01 +00006624
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006625 /* Free up all the SKBs. */
6626 tg3_free_rings(tp);
6627
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006628 for (i = 0; i < tp->irq_cnt; i++) {
6629 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006630
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006631 tnapi->last_tag = 0;
6632 tnapi->last_irq_tag = 0;
6633 tnapi->hw_status->status = 0;
6634 tnapi->hw_status->status_tag = 0;
6635 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6636
6637 tnapi->tx_prod = 0;
6638 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006639 if (tnapi->tx_ring)
6640 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006641
6642 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006643 if (tnapi->rx_rcb)
6644 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006645
Matt Carlson8fea32b2010-09-15 08:59:58 +00006646 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00006647 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006648 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006649 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006650 }
Matt Carlson72334482009-08-28 14:03:01 +00006651
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006652 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006653}
6654
6655/*
6656 * Must not be invoked with interrupt sources disabled and
6657 * the hardware shutdown down.
6658 */
6659static void tg3_free_consistent(struct tg3 *tp)
6660{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006661 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006662
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006663 for (i = 0; i < tp->irq_cnt; i++) {
6664 struct tg3_napi *tnapi = &tp->napi[i];
6665
6666 if (tnapi->tx_ring) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006667 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006668 tnapi->tx_ring, tnapi->tx_desc_mapping);
6669 tnapi->tx_ring = NULL;
6670 }
6671
6672 kfree(tnapi->tx_buffers);
6673 tnapi->tx_buffers = NULL;
6674
6675 if (tnapi->rx_rcb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006676 dma_free_coherent(&tp->pdev->dev,
6677 TG3_RX_RCB_RING_BYTES(tp),
6678 tnapi->rx_rcb,
6679 tnapi->rx_rcb_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006680 tnapi->rx_rcb = NULL;
6681 }
6682
Matt Carlson8fea32b2010-09-15 08:59:58 +00006683 tg3_rx_prodring_fini(tp, &tnapi->prodring);
6684
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006685 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006686 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
6687 tnapi->hw_status,
6688 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006689 tnapi->hw_status = NULL;
6690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006691 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006692
Linus Torvalds1da177e2005-04-16 15:20:36 -07006693 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006694 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
6695 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006696 tp->hw_stats = NULL;
6697 }
6698}
6699
6700/*
6701 * Must not be invoked with interrupt sources disabled and
6702 * the hardware shutdown down. Can sleep.
6703 */
6704static int tg3_alloc_consistent(struct tg3 *tp)
6705{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006706 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006707
Matt Carlson4bae65c2010-11-24 08:31:52 +00006708 tp->hw_stats = dma_alloc_coherent(&tp->pdev->dev,
6709 sizeof(struct tg3_hw_stats),
6710 &tp->stats_mapping,
6711 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006712 if (!tp->hw_stats)
6713 goto err_out;
6714
Linus Torvalds1da177e2005-04-16 15:20:36 -07006715 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6716
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006717 for (i = 0; i < tp->irq_cnt; i++) {
6718 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006719 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006720
Matt Carlson4bae65c2010-11-24 08:31:52 +00006721 tnapi->hw_status = dma_alloc_coherent(&tp->pdev->dev,
6722 TG3_HW_STATUS_SIZE,
6723 &tnapi->status_mapping,
6724 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006725 if (!tnapi->hw_status)
6726 goto err_out;
6727
6728 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006729 sblk = tnapi->hw_status;
6730
Matt Carlson8fea32b2010-09-15 08:59:58 +00006731 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
6732 goto err_out;
6733
Matt Carlson19cfaec2009-12-03 08:36:20 +00006734 /* If multivector TSS is enabled, vector 0 does not handle
6735 * tx interrupts. Don't allocate any resources for it.
6736 */
6737 if ((!i && !(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)) ||
6738 (i && (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS))) {
6739 tnapi->tx_buffers = kzalloc(sizeof(struct ring_info) *
6740 TG3_TX_RING_SIZE,
6741 GFP_KERNEL);
6742 if (!tnapi->tx_buffers)
6743 goto err_out;
6744
Matt Carlson4bae65c2010-11-24 08:31:52 +00006745 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
6746 TG3_TX_RING_BYTES,
6747 &tnapi->tx_desc_mapping,
6748 GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00006749 if (!tnapi->tx_ring)
6750 goto err_out;
6751 }
6752
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006753 /*
6754 * When RSS is enabled, the status block format changes
6755 * slightly. The "rx_jumbo_consumer", "reserved",
6756 * and "rx_mini_consumer" members get mapped to the
6757 * other three rx return ring producer indexes.
6758 */
6759 switch (i) {
6760 default:
6761 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
6762 break;
6763 case 2:
6764 tnapi->rx_rcb_prod_idx = &sblk->rx_jumbo_consumer;
6765 break;
6766 case 3:
6767 tnapi->rx_rcb_prod_idx = &sblk->reserved;
6768 break;
6769 case 4:
6770 tnapi->rx_rcb_prod_idx = &sblk->rx_mini_consumer;
6771 break;
6772 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006773
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006774 /*
6775 * If multivector RSS is enabled, vector 0 does not handle
6776 * rx or tx interrupts. Don't allocate any resources for it.
6777 */
6778 if (!i && (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS))
6779 continue;
6780
Matt Carlson4bae65c2010-11-24 08:31:52 +00006781 tnapi->rx_rcb = dma_alloc_coherent(&tp->pdev->dev,
6782 TG3_RX_RCB_RING_BYTES(tp),
6783 &tnapi->rx_rcb_mapping,
6784 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006785 if (!tnapi->rx_rcb)
6786 goto err_out;
6787
6788 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006789 }
6790
Linus Torvalds1da177e2005-04-16 15:20:36 -07006791 return 0;
6792
6793err_out:
6794 tg3_free_consistent(tp);
6795 return -ENOMEM;
6796}
6797
6798#define MAX_WAIT_CNT 1000
6799
6800/* To stop a block, clear the enable bit and poll till it
6801 * clears. tp->lock is held.
6802 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006803static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006804{
6805 unsigned int i;
6806 u32 val;
6807
6808 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
6809 switch (ofs) {
6810 case RCVLSC_MODE:
6811 case DMAC_MODE:
6812 case MBFREE_MODE:
6813 case BUFMGR_MODE:
6814 case MEMARB_MODE:
6815 /* We can't enable/disable these bits of the
6816 * 5705/5750, just say success.
6817 */
6818 return 0;
6819
6820 default:
6821 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006823 }
6824
6825 val = tr32(ofs);
6826 val &= ~enable_bit;
6827 tw32_f(ofs, val);
6828
6829 for (i = 0; i < MAX_WAIT_CNT; i++) {
6830 udelay(100);
6831 val = tr32(ofs);
6832 if ((val & enable_bit) == 0)
6833 break;
6834 }
6835
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006836 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00006837 dev_err(&tp->pdev->dev,
6838 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
6839 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006840 return -ENODEV;
6841 }
6842
6843 return 0;
6844}
6845
6846/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006847static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006848{
6849 int i, err;
6850
6851 tg3_disable_ints(tp);
6852
6853 tp->rx_mode &= ~RX_MODE_ENABLE;
6854 tw32_f(MAC_RX_MODE, tp->rx_mode);
6855 udelay(10);
6856
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006857 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
6858 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
6859 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
6860 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
6861 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
6862 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006863
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006864 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
6865 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
6866 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
6867 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
6868 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
6869 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
6870 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006871
6872 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
6873 tw32_f(MAC_MODE, tp->mac_mode);
6874 udelay(40);
6875
6876 tp->tx_mode &= ~TX_MODE_ENABLE;
6877 tw32_f(MAC_TX_MODE, tp->tx_mode);
6878
6879 for (i = 0; i < MAX_WAIT_CNT; i++) {
6880 udelay(100);
6881 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
6882 break;
6883 }
6884 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00006885 dev_err(&tp->pdev->dev,
6886 "%s timed out, TX_MODE_ENABLE will not clear "
6887 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07006888 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006889 }
6890
Michael Chane6de8ad2005-05-05 14:42:41 -07006891 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006892 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
6893 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006894
6895 tw32(FTQ_RESET, 0xffffffff);
6896 tw32(FTQ_RESET, 0x00000000);
6897
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006898 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
6899 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006900
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006901 for (i = 0; i < tp->irq_cnt; i++) {
6902 struct tg3_napi *tnapi = &tp->napi[i];
6903 if (tnapi->hw_status)
6904 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6905 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006906 if (tp->hw_stats)
6907 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6908
Linus Torvalds1da177e2005-04-16 15:20:36 -07006909 return err;
6910}
6911
Matt Carlson0d3031d2007-10-10 18:02:43 -07006912static void tg3_ape_send_event(struct tg3 *tp, u32 event)
6913{
6914 int i;
6915 u32 apedata;
6916
Matt Carlsondc6d0742010-09-15 08:59:55 +00006917 /* NCSI does not support APE events */
6918 if (tp->tg3_flags3 & TG3_FLG3_APE_HAS_NCSI)
6919 return;
6920
Matt Carlson0d3031d2007-10-10 18:02:43 -07006921 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
6922 if (apedata != APE_SEG_SIG_MAGIC)
6923 return;
6924
6925 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
Matt Carlson731fd792008-08-15 14:07:51 -07006926 if (!(apedata & APE_FW_STATUS_READY))
Matt Carlson0d3031d2007-10-10 18:02:43 -07006927 return;
6928
6929 /* Wait for up to 1 millisecond for APE to service previous event. */
6930 for (i = 0; i < 10; i++) {
6931 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
6932 return;
6933
6934 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
6935
6936 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6937 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
6938 event | APE_EVENT_STATUS_EVENT_PENDING);
6939
6940 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
6941
6942 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6943 break;
6944
6945 udelay(100);
6946 }
6947
6948 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6949 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
6950}
6951
6952static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
6953{
6954 u32 event;
6955 u32 apedata;
6956
6957 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
6958 return;
6959
6960 switch (kind) {
Matt Carlson33f401a2010-04-05 10:19:27 +00006961 case RESET_KIND_INIT:
6962 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
6963 APE_HOST_SEG_SIG_MAGIC);
6964 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
6965 APE_HOST_SEG_LEN_MAGIC);
6966 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
6967 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
6968 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
Matt Carlson6867c842010-07-11 09:31:44 +00006969 APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
Matt Carlson33f401a2010-04-05 10:19:27 +00006970 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
6971 APE_HOST_BEHAV_NO_PHYLOCK);
Matt Carlsondc6d0742010-09-15 08:59:55 +00006972 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
6973 TG3_APE_HOST_DRVR_STATE_START);
Matt Carlson0d3031d2007-10-10 18:02:43 -07006974
Matt Carlson33f401a2010-04-05 10:19:27 +00006975 event = APE_EVENT_STATUS_STATE_START;
6976 break;
6977 case RESET_KIND_SHUTDOWN:
6978 /* With the interface we are currently using,
6979 * APE does not track driver state. Wiping
6980 * out the HOST SEGMENT SIGNATURE forces
6981 * the APE to assume OS absent status.
6982 */
6983 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
Matt Carlsonb2aee152008-11-03 16:51:11 -08006984
Matt Carlsondc6d0742010-09-15 08:59:55 +00006985 if (device_may_wakeup(&tp->pdev->dev) &&
6986 (tp->tg3_flags & TG3_FLAG_WOL_ENABLE)) {
6987 tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
6988 TG3_APE_HOST_WOL_SPEED_AUTO);
6989 apedata = TG3_APE_HOST_DRVR_STATE_WOL;
6990 } else
6991 apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
6992
6993 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
6994
Matt Carlson33f401a2010-04-05 10:19:27 +00006995 event = APE_EVENT_STATUS_STATE_UNLOAD;
6996 break;
6997 case RESET_KIND_SUSPEND:
6998 event = APE_EVENT_STATUS_STATE_SUSPEND;
6999 break;
7000 default:
7001 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -07007002 }
7003
7004 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
7005
7006 tg3_ape_send_event(tp, event);
7007}
7008
Michael Chane6af3012005-04-21 17:12:05 -07007009/* tp->lock is held. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007010static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
7011{
David S. Millerf49639e2006-06-09 11:58:36 -07007012 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
7013 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007014
7015 if (tp->tg3_flags2 & TG3_FLG2_ASF_NEW_HANDSHAKE) {
7016 switch (kind) {
7017 case RESET_KIND_INIT:
7018 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7019 DRV_STATE_START);
7020 break;
7021
7022 case RESET_KIND_SHUTDOWN:
7023 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7024 DRV_STATE_UNLOAD);
7025 break;
7026
7027 case RESET_KIND_SUSPEND:
7028 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7029 DRV_STATE_SUSPEND);
7030 break;
7031
7032 default:
7033 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007035 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07007036
7037 if (kind == RESET_KIND_INIT ||
7038 kind == RESET_KIND_SUSPEND)
7039 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007040}
7041
7042/* tp->lock is held. */
7043static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
7044{
7045 if (tp->tg3_flags2 & TG3_FLG2_ASF_NEW_HANDSHAKE) {
7046 switch (kind) {
7047 case RESET_KIND_INIT:
7048 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7049 DRV_STATE_START_DONE);
7050 break;
7051
7052 case RESET_KIND_SHUTDOWN:
7053 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7054 DRV_STATE_UNLOAD_DONE);
7055 break;
7056
7057 default:
7058 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007059 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007060 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07007061
7062 if (kind == RESET_KIND_SHUTDOWN)
7063 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007064}
7065
7066/* tp->lock is held. */
7067static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
7068{
7069 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) {
7070 switch (kind) {
7071 case RESET_KIND_INIT:
7072 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7073 DRV_STATE_START);
7074 break;
7075
7076 case RESET_KIND_SHUTDOWN:
7077 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7078 DRV_STATE_UNLOAD);
7079 break;
7080
7081 case RESET_KIND_SUSPEND:
7082 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7083 DRV_STATE_SUSPEND);
7084 break;
7085
7086 default:
7087 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007089 }
7090}
7091
Michael Chan7a6f4362006-09-27 16:03:31 -07007092static int tg3_poll_fw(struct tg3 *tp)
7093{
7094 int i;
7095 u32 val;
7096
Michael Chanb5d37722006-09-27 16:06:21 -07007097 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Gary Zambrano0ccead12006-11-14 16:34:00 -08007098 /* Wait up to 20ms for init done. */
7099 for (i = 0; i < 200; i++) {
Michael Chanb5d37722006-09-27 16:06:21 -07007100 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
7101 return 0;
Gary Zambrano0ccead12006-11-14 16:34:00 -08007102 udelay(100);
Michael Chanb5d37722006-09-27 16:06:21 -07007103 }
7104 return -ENODEV;
7105 }
7106
Michael Chan7a6f4362006-09-27 16:03:31 -07007107 /* Wait for firmware initialization to complete. */
7108 for (i = 0; i < 100000; i++) {
7109 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
7110 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
7111 break;
7112 udelay(10);
7113 }
7114
7115 /* Chip might not be fitted with firmware. Some Sun onboard
7116 * parts are configured like that. So don't signal the timeout
7117 * of the above loop as an error, but do report the lack of
7118 * running firmware once.
7119 */
7120 if (i >= 100000 &&
7121 !(tp->tg3_flags2 & TG3_FLG2_NO_FWARE_REPORTED)) {
7122 tp->tg3_flags2 |= TG3_FLG2_NO_FWARE_REPORTED;
7123
Joe Perches05dbe002010-02-17 19:44:19 +00007124 netdev_info(tp->dev, "No firmware running\n");
Michael Chan7a6f4362006-09-27 16:03:31 -07007125 }
7126
Matt Carlson6b10c162010-02-12 14:47:08 +00007127 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
7128 /* The 57765 A0 needs a little more
7129 * time to do some important work.
7130 */
7131 mdelay(10);
7132 }
7133
Michael Chan7a6f4362006-09-27 16:03:31 -07007134 return 0;
7135}
7136
Michael Chanee6a99b2007-07-18 21:49:10 -07007137/* Save PCI command register before chip reset */
7138static void tg3_save_pci_state(struct tg3 *tp)
7139{
Matt Carlson8a6eac92007-10-21 16:17:55 -07007140 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007141}
7142
7143/* Restore PCI state after chip reset */
7144static void tg3_restore_pci_state(struct tg3 *tp)
7145{
7146 u32 val;
7147
7148 /* Re-enable indirect register accesses. */
7149 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
7150 tp->misc_host_ctrl);
7151
7152 /* Set MAX PCI retry to zero. */
7153 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
7154 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
7155 (tp->tg3_flags & TG3_FLAG_PCIX_MODE))
7156 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07007157 /* Allow reads and writes to the APE register and memory space. */
7158 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
7159 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc2010-06-05 17:24:30 +00007160 PCISTATE_ALLOW_APE_SHMEM_WR |
7161 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07007162 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
7163
Matt Carlson8a6eac92007-10-21 16:17:55 -07007164 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007165
Matt Carlsonfcb389d2008-11-03 16:55:44 -08007166 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785) {
7167 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)
Matt Carlsoncf790032010-11-24 08:31:48 +00007168 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlsonfcb389d2008-11-03 16:55:44 -08007169 else {
7170 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
7171 tp->pci_cacheline_sz);
7172 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
7173 tp->pci_lat_timer);
7174 }
Michael Chan114342f2007-10-15 02:12:26 -07007175 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08007176
Michael Chanee6a99b2007-07-18 21:49:10 -07007177 /* Make sure PCI-X relaxed ordering bit is clear. */
Matt Carlson52f44902008-11-21 17:17:04 -08007178 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
Matt Carlson9974a352007-10-07 23:27:28 -07007179 u16 pcix_cmd;
7180
7181 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7182 &pcix_cmd);
7183 pcix_cmd &= ~PCI_X_CMD_ERO;
7184 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7185 pcix_cmd);
7186 }
Michael Chanee6a99b2007-07-18 21:49:10 -07007187
7188 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007189
7190 /* Chip reset on 5780 will reset MSI enable bit,
7191 * so need to restore it.
7192 */
7193 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
7194 u16 ctrl;
7195
7196 pci_read_config_word(tp->pdev,
7197 tp->msi_cap + PCI_MSI_FLAGS,
7198 &ctrl);
7199 pci_write_config_word(tp->pdev,
7200 tp->msi_cap + PCI_MSI_FLAGS,
7201 ctrl | PCI_MSI_FLAGS_ENABLE);
7202 val = tr32(MSGINT_MODE);
7203 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
7204 }
7205 }
7206}
7207
Linus Torvalds1da177e2005-04-16 15:20:36 -07007208static void tg3_stop_fw(struct tg3 *);
7209
7210/* tp->lock is held. */
7211static int tg3_chip_reset(struct tg3 *tp)
7212{
7213 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07007214 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00007215 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007216
David S. Millerf49639e2006-06-09 11:58:36 -07007217 tg3_nvram_lock(tp);
7218
Matt Carlson77b483f2008-08-15 14:07:24 -07007219 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
7220
David S. Millerf49639e2006-06-09 11:58:36 -07007221 /* No matching tg3_nvram_unlock() after this because
7222 * chip reset below will undo the nvram lock.
7223 */
7224 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007225
Michael Chanee6a99b2007-07-18 21:49:10 -07007226 /* GRC_MISC_CFG core clock reset will clear the memory
7227 * enable bit in PCI register 4 and the MSI enable bit
7228 * on some chips, so we save relevant registers here.
7229 */
7230 tg3_save_pci_state(tp);
7231
Michael Chand9ab5ad2006-03-20 22:27:35 -08007232 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08007233 (tp->tg3_flags3 & TG3_FLG3_5755_PLUS))
Michael Chand9ab5ad2006-03-20 22:27:35 -08007234 tw32(GRC_FASTBOOT_PC, 0);
7235
Linus Torvalds1da177e2005-04-16 15:20:36 -07007236 /*
7237 * We must avoid the readl() that normally takes place.
7238 * It locks machines, causes machine checks, and other
7239 * fun things. So, temporarily disable the 5701
7240 * hardware workaround, while we do the reset.
7241 */
Michael Chan1ee582d2005-08-09 20:16:46 -07007242 write_op = tp->write32;
7243 if (write_op == tg3_write_flush_reg32)
7244 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007245
Michael Chand18edcb2007-03-24 20:57:11 -07007246 /* Prevent the irq handler from reading or writing PCI registers
7247 * during chip reset when the memory enable bit in the PCI command
7248 * register may be cleared. The chip does not generate interrupt
7249 * at this time, but the irq handler may still be called due to irq
7250 * sharing or irqpoll.
7251 */
7252 tp->tg3_flags |= TG3_FLAG_CHIP_RESETTING;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007253 for (i = 0; i < tp->irq_cnt; i++) {
7254 struct tg3_napi *tnapi = &tp->napi[i];
7255 if (tnapi->hw_status) {
7256 tnapi->hw_status->status = 0;
7257 tnapi->hw_status->status_tag = 0;
7258 }
7259 tnapi->last_tag = 0;
7260 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07007261 }
Michael Chand18edcb2007-03-24 20:57:11 -07007262 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00007263
7264 for (i = 0; i < tp->irq_cnt; i++)
7265 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07007266
Matt Carlson255ca312009-08-25 10:07:27 +00007267 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7268 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7269 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
7270 }
7271
Linus Torvalds1da177e2005-04-16 15:20:36 -07007272 /* do the reset */
7273 val = GRC_MISC_CFG_CORECLK_RESET;
7274
7275 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
Matt Carlson88075d92010-08-02 11:25:58 +00007276 /* Force PCIe 1.0a mode */
7277 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlson1407deb2011-04-05 14:22:44 +00007278 !(tp->tg3_flags3 & TG3_FLG3_57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00007279 tr32(TG3_PCIE_PHY_TSTCTL) ==
7280 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
7281 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
7282
Linus Torvalds1da177e2005-04-16 15:20:36 -07007283 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
7284 tw32(GRC_MISC_CFG, (1 << 29));
7285 val |= (1 << 29);
7286 }
7287 }
7288
Michael Chanb5d37722006-09-27 16:06:21 -07007289 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7290 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
7291 tw32(GRC_VCPU_EXT_CTRL,
7292 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
7293 }
7294
Matt Carlsonf37500d2010-08-02 11:25:59 +00007295 /* Manage gphy power for all CPMU absent PCIe devices. */
7296 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
7297 !(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007298 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00007299
Linus Torvalds1da177e2005-04-16 15:20:36 -07007300 tw32(GRC_MISC_CFG, val);
7301
Michael Chan1ee582d2005-08-09 20:16:46 -07007302 /* restore 5701 hardware bug workaround write method */
7303 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007304
7305 /* Unfortunately, we have to delay before the PCI read back.
7306 * Some 575X chips even will not respond to a PCI cfg access
7307 * when the reset command is given to the chip.
7308 *
7309 * How do these hardware designers expect things to work
7310 * properly if the PCI write is posted for a long period
7311 * of time? It is always necessary to have some method by
7312 * which a register read back can occur to push the write
7313 * out which does the reset.
7314 *
7315 * For most tg3 variants the trick below was working.
7316 * Ho hum...
7317 */
7318 udelay(120);
7319
7320 /* Flush PCI posted writes. The normal MMIO registers
7321 * are inaccessible at this time so this is the only
7322 * way to make this reliably (actually, this is no longer
7323 * the case, see above). I tried to use indirect
7324 * register read/write but this upset some 5701 variants.
7325 */
7326 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
7327
7328 udelay(120);
7329
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007330 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) && tp->pcie_cap) {
Matt Carlsone7126992009-08-25 10:08:16 +00007331 u16 val16;
7332
Linus Torvalds1da177e2005-04-16 15:20:36 -07007333 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
7334 int i;
7335 u32 cfg_val;
7336
7337 /* Wait for link training to complete. */
7338 for (i = 0; i < 5000; i++)
7339 udelay(100);
7340
7341 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
7342 pci_write_config_dword(tp->pdev, 0xc4,
7343 cfg_val | (1 << 15));
7344 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007345
Matt Carlsone7126992009-08-25 10:08:16 +00007346 /* Clear the "no snoop" and "relaxed ordering" bits. */
7347 pci_read_config_word(tp->pdev,
7348 tp->pcie_cap + PCI_EXP_DEVCTL,
7349 &val16);
7350 val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
7351 PCI_EXP_DEVCTL_NOSNOOP_EN);
7352 /*
7353 * Older PCIe devices only support the 128 byte
7354 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007355 */
Matt Carlson6de34cb2010-08-02 11:25:55 +00007356 if (!(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT))
Matt Carlsone7126992009-08-25 10:08:16 +00007357 val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007358 pci_write_config_word(tp->pdev,
7359 tp->pcie_cap + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007360 val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007361
Matt Carlsoncf790032010-11-24 08:31:48 +00007362 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007363
7364 /* Clear error status */
7365 pci_write_config_word(tp->pdev,
7366 tp->pcie_cap + PCI_EXP_DEVSTA,
7367 PCI_EXP_DEVSTA_CED |
7368 PCI_EXP_DEVSTA_NFED |
7369 PCI_EXP_DEVSTA_FED |
7370 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007371 }
7372
Michael Chanee6a99b2007-07-18 21:49:10 -07007373 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007374
Matt Carlsone64de4e2011-04-13 11:05:05 +00007375 tp->tg3_flags &= ~(TG3_FLAG_CHIP_RESETTING |
7376 TG3_FLAG_ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07007377
Michael Chanee6a99b2007-07-18 21:49:10 -07007378 val = 0;
7379 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
Michael Chan4cf78e42005-07-25 12:29:19 -07007380 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07007381 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007382
7383 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
7384 tg3_stop_fw(tp);
7385 tw32(0x5000, 0x400);
7386 }
7387
7388 tw32(GRC_MODE, tp->grc_mode);
7389
7390 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007391 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007392
7393 tw32(0xc4, val | (1 << 15));
7394 }
7395
7396 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
7397 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
7398 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
7399 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
7400 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
7401 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7402 }
7403
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007404 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
7405 tp->mac_mode = MAC_MODE_APE_TX_EN |
7406 MAC_MODE_APE_RX_EN |
7407 MAC_MODE_TDE_ENABLE;
7408
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007409 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007410 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
7411 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007412 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007413 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
7414 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007415 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007416 val = 0;
7417
7418 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007419 udelay(40);
7420
Matt Carlson77b483f2008-08-15 14:07:24 -07007421 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
7422
Michael Chan7a6f4362006-09-27 16:03:31 -07007423 err = tg3_poll_fw(tp);
7424 if (err)
7425 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007426
Matt Carlson0a9140c2009-08-28 12:27:50 +00007427 tg3_mdio_start(tp);
7428
Linus Torvalds1da177e2005-04-16 15:20:36 -07007429 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007430 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
7431 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlson1407deb2011-04-05 14:22:44 +00007432 !(tp->tg3_flags3 & TG3_FLG3_57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007433 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007434
7435 tw32(0x7c00, val | (1 << 25));
7436 }
7437
Matt Carlsond78b59f2011-04-05 14:22:46 +00007438 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
7439 val = tr32(TG3_CPMU_CLCK_ORIDE);
7440 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
7441 }
7442
Linus Torvalds1da177e2005-04-16 15:20:36 -07007443 /* Reprobe ASF enable state. */
7444 tp->tg3_flags &= ~TG3_FLAG_ENABLE_ASF;
7445 tp->tg3_flags2 &= ~TG3_FLG2_ASF_NEW_HANDSHAKE;
7446 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
7447 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
7448 u32 nic_cfg;
7449
7450 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
7451 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
7452 tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
Matt Carlson4ba526c2008-08-15 14:10:04 -07007453 tp->last_event_jiffies = jiffies;
John W. Linvillecbf46852005-04-21 17:01:29 -07007454 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007455 tp->tg3_flags2 |= TG3_FLG2_ASF_NEW_HANDSHAKE;
7456 }
7457 }
7458
7459 return 0;
7460}
7461
7462/* tp->lock is held. */
7463static void tg3_stop_fw(struct tg3 *tp)
7464{
Matt Carlson0d3031d2007-10-10 18:02:43 -07007465 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
7466 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07007467 /* Wait for RX cpu to ACK the previous event. */
7468 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007469
7470 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007471
7472 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007473
Matt Carlson7c5026a2008-05-02 16:49:29 -07007474 /* Wait for RX cpu to ACK this event. */
7475 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007476 }
7477}
7478
7479/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07007480static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007481{
7482 int err;
7483
7484 tg3_stop_fw(tp);
7485
Michael Chan944d9802005-05-29 14:57:48 -07007486 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007487
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007488 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007489 err = tg3_chip_reset(tp);
7490
Matt Carlsondaba2a62009-04-20 06:58:52 +00007491 __tg3_set_mac_addr(tp, 0);
7492
Michael Chan944d9802005-05-29 14:57:48 -07007493 tg3_write_sig_legacy(tp, kind);
7494 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007495
7496 if (err)
7497 return err;
7498
7499 return 0;
7500}
7501
Linus Torvalds1da177e2005-04-16 15:20:36 -07007502#define RX_CPU_SCRATCH_BASE 0x30000
7503#define RX_CPU_SCRATCH_SIZE 0x04000
7504#define TX_CPU_SCRATCH_BASE 0x34000
7505#define TX_CPU_SCRATCH_SIZE 0x04000
7506
7507/* tp->lock is held. */
7508static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
7509{
7510 int i;
7511
Eric Sesterhenn5d9428d2006-04-02 13:52:48 +02007512 BUG_ON(offset == TX_CPU_BASE &&
7513 (tp->tg3_flags2 & TG3_FLG2_5705_PLUS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007514
Michael Chanb5d37722006-09-27 16:06:21 -07007515 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7516 u32 val = tr32(GRC_VCPU_EXT_CTRL);
7517
7518 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
7519 return 0;
7520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007521 if (offset == RX_CPU_BASE) {
7522 for (i = 0; i < 10000; i++) {
7523 tw32(offset + CPU_STATE, 0xffffffff);
7524 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7525 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7526 break;
7527 }
7528
7529 tw32(offset + CPU_STATE, 0xffffffff);
7530 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
7531 udelay(10);
7532 } else {
7533 for (i = 0; i < 10000; i++) {
7534 tw32(offset + CPU_STATE, 0xffffffff);
7535 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7536 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7537 break;
7538 }
7539 }
7540
7541 if (i >= 10000) {
Joe Perches05dbe002010-02-17 19:44:19 +00007542 netdev_err(tp->dev, "%s timed out, %s CPU\n",
7543 __func__, offset == RX_CPU_BASE ? "RX" : "TX");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007544 return -ENODEV;
7545 }
Michael Chanec41c7d2006-01-17 02:40:55 -08007546
7547 /* Clear firmware's nvram arbitration. */
7548 if (tp->tg3_flags & TG3_FLAG_NVRAM)
7549 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007550 return 0;
7551}
7552
7553struct fw_info {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007554 unsigned int fw_base;
7555 unsigned int fw_len;
7556 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007557};
7558
7559/* tp->lock is held. */
7560static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base, u32 cpu_scratch_base,
7561 int cpu_scratch_size, struct fw_info *info)
7562{
Michael Chanec41c7d2006-01-17 02:40:55 -08007563 int err, lock_err, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007564 void (*write_op)(struct tg3 *, u32, u32);
7565
7566 if (cpu_base == TX_CPU_BASE &&
7567 (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007568 netdev_err(tp->dev,
7569 "%s: Trying to load TX cpu firmware which is 5705\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007570 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007571 return -EINVAL;
7572 }
7573
7574 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
7575 write_op = tg3_write_mem;
7576 else
7577 write_op = tg3_write_indirect_reg32;
7578
Michael Chan1b628152005-05-29 14:59:49 -07007579 /* It is possible that bootcode is still loading at this point.
7580 * Get the nvram lock first before halting the cpu.
7581 */
Michael Chanec41c7d2006-01-17 02:40:55 -08007582 lock_err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007583 err = tg3_halt_cpu(tp, cpu_base);
Michael Chanec41c7d2006-01-17 02:40:55 -08007584 if (!lock_err)
7585 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007586 if (err)
7587 goto out;
7588
7589 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
7590 write_op(tp, cpu_scratch_base + i, 0);
7591 tw32(cpu_base + CPU_STATE, 0xffffffff);
7592 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007593 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007594 write_op(tp, (cpu_scratch_base +
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007595 (info->fw_base & 0xffff) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07007596 (i * sizeof(u32))),
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007597 be32_to_cpu(info->fw_data[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007598
7599 err = 0;
7600
7601out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007602 return err;
7603}
7604
7605/* tp->lock is held. */
7606static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
7607{
7608 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007609 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007610 int err, i;
7611
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007612 fw_data = (void *)tp->fw->data;
7613
7614 /* Firmware blob starts with version numbers, followed by
7615 start address and length. We are setting complete length.
7616 length = end_address_of_bss - start_address_of_text.
7617 Remainder is the blob to be loaded contiguously
7618 from start address. */
7619
7620 info.fw_base = be32_to_cpu(fw_data[1]);
7621 info.fw_len = tp->fw->size - 12;
7622 info.fw_data = &fw_data[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007623
7624 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
7625 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
7626 &info);
7627 if (err)
7628 return err;
7629
7630 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
7631 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
7632 &info);
7633 if (err)
7634 return err;
7635
7636 /* Now startup only the RX cpu. */
7637 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007638 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007639
7640 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007641 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007642 break;
7643 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7644 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007645 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007646 udelay(1000);
7647 }
7648 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007649 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
7650 "should be %08x\n", __func__,
Joe Perches05dbe002010-02-17 19:44:19 +00007651 tr32(RX_CPU_BASE + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007652 return -ENODEV;
7653 }
7654 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7655 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
7656
7657 return 0;
7658}
7659
Linus Torvalds1da177e2005-04-16 15:20:36 -07007660/* 5705 needs a special version of the TSO firmware. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007661
7662/* tp->lock is held. */
7663static int tg3_load_tso_firmware(struct tg3 *tp)
7664{
7665 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007666 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007667 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
7668 int err, i;
7669
7670 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
7671 return 0;
7672
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007673 fw_data = (void *)tp->fw->data;
7674
7675 /* Firmware blob starts with version numbers, followed by
7676 start address and length. We are setting complete length.
7677 length = end_address_of_bss - start_address_of_text.
7678 Remainder is the blob to be loaded contiguously
7679 from start address. */
7680
7681 info.fw_base = be32_to_cpu(fw_data[1]);
7682 cpu_scratch_size = tp->fw_len;
7683 info.fw_len = tp->fw->size - 12;
7684 info.fw_data = &fw_data[3];
7685
Linus Torvalds1da177e2005-04-16 15:20:36 -07007686 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007687 cpu_base = RX_CPU_BASE;
7688 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007689 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007690 cpu_base = TX_CPU_BASE;
7691 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
7692 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
7693 }
7694
7695 err = tg3_load_firmware_cpu(tp, cpu_base,
7696 cpu_scratch_base, cpu_scratch_size,
7697 &info);
7698 if (err)
7699 return err;
7700
7701 /* Now startup the cpu. */
7702 tw32(cpu_base + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007703 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007704
7705 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007706 if (tr32(cpu_base + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007707 break;
7708 tw32(cpu_base + CPU_STATE, 0xffffffff);
7709 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007710 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007711 udelay(1000);
7712 }
7713 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007714 netdev_err(tp->dev,
7715 "%s fails to set CPU PC, is %08x should be %08x\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007716 __func__, tr32(cpu_base + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007717 return -ENODEV;
7718 }
7719 tw32(cpu_base + CPU_STATE, 0xffffffff);
7720 tw32_f(cpu_base + CPU_MODE, 0x00000000);
7721 return 0;
7722}
7723
Linus Torvalds1da177e2005-04-16 15:20:36 -07007724
Linus Torvalds1da177e2005-04-16 15:20:36 -07007725static int tg3_set_mac_addr(struct net_device *dev, void *p)
7726{
7727 struct tg3 *tp = netdev_priv(dev);
7728 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07007729 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007730
Michael Chanf9804dd2005-09-27 12:13:10 -07007731 if (!is_valid_ether_addr(addr->sa_data))
7732 return -EINVAL;
7733
Linus Torvalds1da177e2005-04-16 15:20:36 -07007734 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7735
Michael Chane75f7c92006-03-20 21:33:26 -08007736 if (!netif_running(dev))
7737 return 0;
7738
Michael Chan58712ef2006-04-29 18:58:01 -07007739 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) {
Michael Chan986e0ae2007-05-05 12:10:20 -07007740 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07007741
Michael Chan986e0ae2007-05-05 12:10:20 -07007742 addr0_high = tr32(MAC_ADDR_0_HIGH);
7743 addr0_low = tr32(MAC_ADDR_0_LOW);
7744 addr1_high = tr32(MAC_ADDR_1_HIGH);
7745 addr1_low = tr32(MAC_ADDR_1_LOW);
7746
7747 /* Skip MAC addr 1 if ASF is using it. */
7748 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
7749 !(addr1_high == 0 && addr1_low == 0))
7750 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07007751 }
Michael Chan986e0ae2007-05-05 12:10:20 -07007752 spin_lock_bh(&tp->lock);
7753 __tg3_set_mac_addr(tp, skip_mac_1);
7754 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007755
Michael Chanb9ec6c12006-07-25 16:37:27 -07007756 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007757}
7758
7759/* tp->lock is held. */
7760static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
7761 dma_addr_t mapping, u32 maxlen_flags,
7762 u32 nic_addr)
7763{
7764 tg3_write_mem(tp,
7765 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
7766 ((u64) mapping >> 32));
7767 tg3_write_mem(tp,
7768 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
7769 ((u64) mapping & 0xffffffff));
7770 tg3_write_mem(tp,
7771 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
7772 maxlen_flags);
7773
7774 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7775 tg3_write_mem(tp,
7776 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
7777 nic_addr);
7778}
7779
7780static void __tg3_set_rx_mode(struct net_device *);
Michael Chand244c892005-07-05 14:42:33 -07007781static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07007782{
Matt Carlsonb6080e12009-09-01 13:12:00 +00007783 int i;
7784
Matt Carlson19cfaec2009-12-03 08:36:20 +00007785 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007786 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
7787 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
7788 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007789 } else {
7790 tw32(HOSTCC_TXCOL_TICKS, 0);
7791 tw32(HOSTCC_TXMAX_FRAMES, 0);
7792 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007793 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007794
Matt Carlson20d73752010-07-11 09:31:41 +00007795 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00007796 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
7797 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
7798 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
7799 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007800 tw32(HOSTCC_RXCOL_TICKS, 0);
7801 tw32(HOSTCC_RXMAX_FRAMES, 0);
7802 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07007803 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007804
David S. Miller15f98502005-05-18 22:49:26 -07007805 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7806 u32 val = ec->stats_block_coalesce_usecs;
7807
Matt Carlsonb6080e12009-09-01 13:12:00 +00007808 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
7809 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
7810
David S. Miller15f98502005-05-18 22:49:26 -07007811 if (!netif_carrier_ok(tp->dev))
7812 val = 0;
7813
7814 tw32(HOSTCC_STAT_COAL_TICKS, val);
7815 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007816
7817 for (i = 0; i < tp->irq_cnt - 1; i++) {
7818 u32 reg;
7819
7820 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
7821 tw32(reg, ec->rx_coalesce_usecs);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007822 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
7823 tw32(reg, ec->rx_max_coalesced_frames);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007824 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
7825 tw32(reg, ec->rx_max_coalesced_frames_irq);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007826
7827 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS) {
7828 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
7829 tw32(reg, ec->tx_coalesce_usecs);
7830 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
7831 tw32(reg, ec->tx_max_coalesced_frames);
7832 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
7833 tw32(reg, ec->tx_max_coalesced_frames_irq);
7834 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007835 }
7836
7837 for (; i < tp->irq_max - 1; i++) {
7838 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007839 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007840 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007841
7842 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS) {
7843 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
7844 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
7845 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
7846 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007847 }
David S. Miller15f98502005-05-18 22:49:26 -07007848}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007849
7850/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00007851static void tg3_rings_reset(struct tg3 *tp)
7852{
7853 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007854 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007855 struct tg3_napi *tnapi = &tp->napi[0];
7856
7857 /* Disable all transmit rings but the first. */
7858 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7859 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlson0a58d662011-04-05 14:22:45 +00007860 else if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)
Matt Carlson3d377282010-10-14 10:37:39 +00007861 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
Matt Carlsonb703df62009-12-03 08:36:21 +00007862 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
7863 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007864 else
7865 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7866
7867 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7868 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
7869 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
7870 BDINFO_FLAGS_DISABLED);
7871
7872
7873 /* Disable all receive return rings but the first. */
Matt Carlson0a58d662011-04-05 14:22:45 +00007874 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007875 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
7876 else if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00007877 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00007878 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
7879 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson2d31eca2009-09-01 12:53:31 +00007880 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
7881 else
7882 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7883
7884 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7885 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
7886 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
7887 BDINFO_FLAGS_DISABLED);
7888
7889 /* Disable interrupts */
7890 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
7891
7892 /* Zero mailbox registers. */
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007893 if (tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00007894 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007895 tp->napi[i].tx_prod = 0;
7896 tp->napi[i].tx_cons = 0;
Matt Carlsonc2353a32010-01-20 16:58:08 +00007897 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
7898 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007899 tw32_rx_mbox(tp->napi[i].consmbox, 0);
7900 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
7901 }
Matt Carlsonc2353a32010-01-20 16:58:08 +00007902 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS))
7903 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007904 } else {
7905 tp->napi[0].tx_prod = 0;
7906 tp->napi[0].tx_cons = 0;
7907 tw32_mailbox(tp->napi[0].prodmbox, 0);
7908 tw32_rx_mbox(tp->napi[0].consmbox, 0);
7909 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007910
7911 /* Make sure the NIC-based send BD rings are disabled. */
7912 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7913 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
7914 for (i = 0; i < 16; i++)
7915 tw32_tx_mbox(mbox + i * 8, 0);
7916 }
7917
7918 txrcb = NIC_SRAM_SEND_RCB;
7919 rxrcb = NIC_SRAM_RCV_RET_RCB;
7920
7921 /* Clear status block in ram. */
7922 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7923
7924 /* Set status block DMA address */
7925 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
7926 ((u64) tnapi->status_mapping >> 32));
7927 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
7928 ((u64) tnapi->status_mapping & 0xffffffff));
7929
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007930 if (tnapi->tx_ring) {
7931 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7932 (TG3_TX_RING_SIZE <<
7933 BDINFO_FLAGS_MAXLEN_SHIFT),
7934 NIC_SRAM_TX_BUFFER_DESC);
7935 txrcb += TG3_BDINFO_SIZE;
7936 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007937
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007938 if (tnapi->rx_rcb) {
7939 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00007940 (tp->rx_ret_ring_mask + 1) <<
7941 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007942 rxrcb += TG3_BDINFO_SIZE;
7943 }
7944
7945 stblk = HOSTCC_STATBLCK_RING1;
7946
7947 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
7948 u64 mapping = (u64)tnapi->status_mapping;
7949 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
7950 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
7951
7952 /* Clear status block in ram. */
7953 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7954
Matt Carlson19cfaec2009-12-03 08:36:20 +00007955 if (tnapi->tx_ring) {
7956 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7957 (TG3_TX_RING_SIZE <<
7958 BDINFO_FLAGS_MAXLEN_SHIFT),
7959 NIC_SRAM_TX_BUFFER_DESC);
7960 txrcb += TG3_BDINFO_SIZE;
7961 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007962
7963 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00007964 ((tp->rx_ret_ring_mask + 1) <<
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007965 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
7966
7967 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007968 rxrcb += TG3_BDINFO_SIZE;
7969 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007970}
7971
Matt Carlsoneb07a942011-04-20 07:57:36 +00007972static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
7973{
7974 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
7975
7976 if (!(tp->tg3_flags2 & TG3_FLG2_5750_PLUS) ||
7977 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
7978 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
7979 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
7980 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
7981 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
7982 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787)
7983 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
7984 else
7985 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
7986
7987 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
7988 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
7989
7990 val = min(nic_rep_thresh, host_rep_thresh);
7991 tw32(RCVBDI_STD_THRESH, val);
7992
7993 if (tp->tg3_flags3 & TG3_FLG3_57765_PLUS)
7994 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
7995
7996 if (!(tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) ||
7997 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
7998 return;
7999
8000 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
8001 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
8002 else
8003 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5717;
8004
8005 host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
8006
8007 val = min(bdcache_maxcnt / 2, host_rep_thresh);
8008 tw32(RCVBDI_JUMBO_THRESH, val);
8009
8010 if (tp->tg3_flags3 & TG3_FLG3_57765_PLUS)
8011 tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
8012}
8013
Matt Carlson2d31eca2009-09-01 12:53:31 +00008014/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008015static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008016{
8017 u32 val, rdmac_mode;
8018 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00008019 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008020
8021 tg3_disable_ints(tp);
8022
8023 tg3_stop_fw(tp);
8024
8025 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
8026
Matt Carlson859a5882010-04-05 10:19:28 +00008027 if (tp->tg3_flags & TG3_FLAG_INIT_COMPLETE)
Michael Chane6de8ad2005-05-05 14:42:41 -07008028 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008029
Matt Carlson699c0192010-12-06 08:28:51 +00008030 /* Enable MAC control of LPI */
8031 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
8032 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL,
8033 TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
8034 TG3_CPMU_EEE_LNKIDL_UART_IDL);
8035
8036 tw32_f(TG3_CPMU_EEE_CTRL,
8037 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
8038
Matt Carlsona386b902010-12-06 08:28:53 +00008039 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
8040 TG3_CPMU_EEEMD_LPI_IN_TX |
8041 TG3_CPMU_EEEMD_LPI_IN_RX |
8042 TG3_CPMU_EEEMD_EEE_ENABLE;
8043
8044 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8045 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
8046
8047 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
8048 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
8049
8050 tw32_f(TG3_CPMU_EEE_MODE, val);
8051
8052 tw32_f(TG3_CPMU_EEE_DBTMR1,
8053 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
8054 TG3_CPMU_DBTMR1_LNKIDLE_2047US);
8055
8056 tw32_f(TG3_CPMU_EEE_DBTMR2,
Matt Carlsond7f2ab22011-01-25 15:58:56 +00008057 TG3_CPMU_DBTMR2_APE_TX_2047US |
Matt Carlsona386b902010-12-06 08:28:53 +00008058 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
Matt Carlson699c0192010-12-06 08:28:51 +00008059 }
8060
Matt Carlson603f1172010-02-12 14:47:10 +00008061 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08008062 tg3_phy_reset(tp);
8063
Linus Torvalds1da177e2005-04-16 15:20:36 -07008064 err = tg3_chip_reset(tp);
8065 if (err)
8066 return err;
8067
8068 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
8069
Matt Carlsonbcb37f62008-11-03 16:52:09 -08008070 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008071 val = tr32(TG3_CPMU_CTRL);
8072 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
8073 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08008074
8075 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8076 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8077 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8078 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
8079
8080 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
8081 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
8082 val |= CPMU_LNK_AWARE_MACCLK_6_25;
8083 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
8084
8085 val = tr32(TG3_CPMU_HST_ACC);
8086 val &= ~CPMU_HST_ACC_MACCLK_MASK;
8087 val |= CPMU_HST_ACC_MACCLK_6_25;
8088 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07008089 }
8090
Matt Carlson33466d92009-04-20 06:57:41 +00008091 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
8092 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
8093 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
8094 PCIE_PWR_MGMT_L1_THRESH_4MS;
8095 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00008096
8097 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
8098 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
8099
8100 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00008101
Matt Carlsonf40386c2009-11-02 14:24:02 +00008102 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
8103 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00008104 }
8105
Matt Carlson614b05902010-01-20 16:58:02 +00008106 if (tp->tg3_flags3 & TG3_FLG3_L1PLLPD_EN) {
8107 u32 grc_mode = tr32(GRC_MODE);
8108
8109 /* Access the lower 1K of PL PCIE block registers. */
8110 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8111 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
8112
8113 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
8114 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
8115 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
8116
8117 tw32(GRC_MODE, grc_mode);
8118 }
8119
Matt Carlson5093eed2010-11-24 08:31:45 +00008120 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
8121 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
8122 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00008123
Matt Carlson5093eed2010-11-24 08:31:45 +00008124 /* Access the lower 1K of PL PCIE block registers. */
8125 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8126 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00008127
Matt Carlson5093eed2010-11-24 08:31:45 +00008128 val = tr32(TG3_PCIE_TLDLPL_PORT +
8129 TG3_PCIE_PL_LO_PHYCTL5);
8130 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
8131 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00008132
Matt Carlson5093eed2010-11-24 08:31:45 +00008133 tw32(GRC_MODE, grc_mode);
8134 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00008135
8136 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8137 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8138 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8139 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00008140 }
8141
Linus Torvalds1da177e2005-04-16 15:20:36 -07008142 /* This works around an issue with Athlon chipsets on
8143 * B3 tigon3 silicon. This bit has no effect on any
8144 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07008145 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008146 */
Matt Carlson795d01c2007-10-07 23:28:17 -07008147 if (!(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT)) {
8148 if (!(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
8149 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
8150 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
8151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008152
8153 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
8154 (tp->tg3_flags & TG3_FLAG_PCIX_MODE)) {
8155 val = tr32(TG3PCI_PCISTATE);
8156 val |= PCISTATE_RETRY_SAME_DMA;
8157 tw32(TG3PCI_PCISTATE, val);
8158 }
8159
Matt Carlson0d3031d2007-10-10 18:02:43 -07008160 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
8161 /* Allow reads and writes to the
8162 * APE register and memory space.
8163 */
8164 val = tr32(TG3PCI_PCISTATE);
8165 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc2010-06-05 17:24:30 +00008166 PCISTATE_ALLOW_APE_SHMEM_WR |
8167 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008168 tw32(TG3PCI_PCISTATE, val);
8169 }
8170
Linus Torvalds1da177e2005-04-16 15:20:36 -07008171 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
8172 /* Enable some hw fixes. */
8173 val = tr32(TG3PCI_MSI_DATA);
8174 val |= (1 << 26) | (1 << 28) | (1 << 29);
8175 tw32(TG3PCI_MSI_DATA, val);
8176 }
8177
8178 /* Descriptor ring init may make accesses to the
8179 * NIC SRAM area to setup the TX descriptors, so we
8180 * can only do this after the hardware has been
8181 * successfully reset.
8182 */
Michael Chan32d8c572006-07-25 16:38:29 -07008183 err = tg3_init_rings(tp);
8184 if (err)
8185 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008186
Matt Carlson1407deb2011-04-05 14:22:44 +00008187 if (tp->tg3_flags3 & TG3_FLG3_57765_PLUS) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008188 val = tr32(TG3PCI_DMA_RW_CTRL) &
8189 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Matt Carlson1a319022010-04-12 06:58:25 +00008190 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
8191 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008192 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
8193 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
8194 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008195 /* This value is determined during the probe time DMA
8196 * engine test, tg3_test_dma.
8197 */
8198 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
8199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008200
8201 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
8202 GRC_MODE_4X_NIC_SEND_RINGS |
8203 GRC_MODE_NO_TX_PHDR_CSUM |
8204 GRC_MODE_NO_RX_PHDR_CSUM);
8205 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07008206
8207 /* Pseudo-header checksum is done by hardware logic and not
8208 * the offload processers, so make the chip do the pseudo-
8209 * header checksums on receive. For transmit it is more
8210 * convenient to do the pseudo-header checksum in software
8211 * as Linux does that on transmit for us in all cases.
8212 */
8213 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008214
8215 tw32(GRC_MODE,
8216 tp->grc_mode |
8217 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
8218
8219 /* Setup the timer prescalar register. Clock is always 66Mhz. */
8220 val = tr32(GRC_MISC_CFG);
8221 val &= ~0xff;
8222 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
8223 tw32(GRC_MISC_CFG, val);
8224
8225 /* Initialize MBUF/DESC pool. */
John W. Linvillecbf46852005-04-21 17:01:29 -07008226 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008227 /* Do nothing. */
8228 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
8229 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
8230 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
8231 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
8232 else
8233 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
8234 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
8235 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Matt Carlson859a5882010-04-05 10:19:28 +00008236 } else if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008237 int fw_len;
8238
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08008239 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008240 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
8241 tw32(BUFMGR_MB_POOL_ADDR,
8242 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
8243 tw32(BUFMGR_MB_POOL_SIZE,
8244 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
8245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008246
Michael Chan0f893dc2005-07-25 12:30:38 -07008247 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008248 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8249 tp->bufmgr_config.mbuf_read_dma_low_water);
8250 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8251 tp->bufmgr_config.mbuf_mac_rx_low_water);
8252 tw32(BUFMGR_MB_HIGH_WATER,
8253 tp->bufmgr_config.mbuf_high_water);
8254 } else {
8255 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8256 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
8257 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8258 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
8259 tw32(BUFMGR_MB_HIGH_WATER,
8260 tp->bufmgr_config.mbuf_high_water_jumbo);
8261 }
8262 tw32(BUFMGR_DMA_LOW_WATER,
8263 tp->bufmgr_config.dma_low_water);
8264 tw32(BUFMGR_DMA_HIGH_WATER,
8265 tp->bufmgr_config.dma_high_water);
8266
Matt Carlsond309a462010-09-30 10:34:31 +00008267 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
8268 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
8269 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
Matt Carlson4d958472011-04-20 07:57:35 +00008270 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
8271 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
8272 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0)
8273 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
Matt Carlsond309a462010-09-30 10:34:31 +00008274 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008275 for (i = 0; i < 2000; i++) {
8276 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
8277 break;
8278 udelay(10);
8279 }
8280 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00008281 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008282 return -ENODEV;
8283 }
8284
Matt Carlsoneb07a942011-04-20 07:57:36 +00008285 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
8286 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
Michael Chanb5d37722006-09-27 16:06:21 -07008287
Matt Carlsoneb07a942011-04-20 07:57:36 +00008288 tg3_setup_rxbd_thresholds(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008289
8290 /* Initialize TG3_BDINFO's at:
8291 * RCVDBDI_STD_BD: standard eth size rx ring
8292 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
8293 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
8294 *
8295 * like so:
8296 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
8297 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
8298 * ring attribute flags
8299 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
8300 *
8301 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
8302 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
8303 *
8304 * The size of each ring is fixed in the firmware, but the location is
8305 * configurable.
8306 */
8307 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008308 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008309 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008310 ((u64) tpr->rx_std_mapping & 0xffffffff));
Matt Carlson0a58d662011-04-05 14:22:45 +00008311 if (!(tp->tg3_flags3 & TG3_FLG3_5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +00008312 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
8313 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008314
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008315 /* Disable the mini ring */
8316 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008317 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
8318 BDINFO_FLAGS_DISABLED);
8319
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008320 /* Program the jumbo buffer descriptor ring control
8321 * blocks on those devices that have them.
8322 */
Matt Carlsonbb18bb92011-03-09 16:58:19 +00008323 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlson4d163b72011-01-25 15:58:48 +00008324 ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) &&
8325 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008326
Michael Chan0f893dc2005-07-25 12:30:38 -07008327 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008328 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008329 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008330 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008331 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +00008332 val = TG3_RX_JMB_RING_SIZE(tp) <<
8333 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008334 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +00008335 val | BDINFO_FLAGS_USE_EXT_RECV);
Matt Carlsona50d0792010-06-05 17:24:37 +00008336 if (!(tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) ||
8337 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson87668d32009-11-13 13:03:34 +00008338 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
8339 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008340 } else {
8341 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
8342 BDINFO_FLAGS_DISABLED);
8343 }
8344
Matt Carlson1407deb2011-04-05 14:22:44 +00008345 if (tp->tg3_flags3 & TG3_FLG3_57765_PLUS) {
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008346 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlsonde9f5232011-04-05 14:22:43 +00008347 val = TG3_RX_STD_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008348 else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008349 val = TG3_RX_STD_MAX_SIZE_5717;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008350 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
8351 val |= (TG3_RX_STD_DMA_SZ << 2);
8352 } else
Matt Carlson04380d42010-04-12 06:58:29 +00008353 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008354 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008355 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008356
8357 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008358
Matt Carlson411da642009-11-13 13:03:46 +00008359 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e62009-11-13 13:03:49 +00008360 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008361
Matt Carlson411da642009-11-13 13:03:46 +00008362 tpr->rx_jmb_prod_idx = (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) ?
Matt Carlson21f581a2009-08-28 14:00:25 +00008363 tp->rx_jumbo_pending : 0;
Matt Carlson66711e62009-11-13 13:03:49 +00008364 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008365
Matt Carlson2d31eca2009-09-01 12:53:31 +00008366 tg3_rings_reset(tp);
8367
Linus Torvalds1da177e2005-04-16 15:20:36 -07008368 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07008369 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008370
8371 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00008372 tw32(MAC_RX_MTU_SIZE,
8373 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008374
8375 /* The slot time is changed by tg3_setup_phy if we
8376 * run at gigabit with half duplex.
8377 */
Matt Carlsonf2096f92011-04-05 14:22:48 +00008378 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
8379 (6 << TX_LENGTHS_IPG_SHIFT) |
8380 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
8381
8382 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8383 val |= tr32(MAC_TX_LENGTHS) &
8384 (TX_LENGTHS_JMB_FRM_LEN_MSK |
8385 TX_LENGTHS_CNT_DWN_VAL_MSK);
8386
8387 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008388
8389 /* Receive rules. */
8390 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
8391 tw32(RCVLPC_CONFIG, 0x0181);
8392
8393 /* Calculate RDMAC_MODE setting early, we need it to determine
8394 * the RCVLPC_STATE_ENABLE mask.
8395 */
8396 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
8397 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
8398 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
8399 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
8400 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07008401
Matt Carlsondeabaac2010-11-24 08:31:50 +00008402 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +00008403 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
8404
Matt Carlson57e69832008-05-25 23:48:31 -07008405 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08008406 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8407 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07008408 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
8409 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
8410 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
8411
Matt Carlsonc5908932011-03-09 16:58:25 +00008412 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8413 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008414 if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07008415 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008416 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
8417 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
8418 !(tp->tg3_flags2 & TG3_FLG2_IS_5788)) {
8419 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8420 }
8421 }
8422
Michael Chan85e94ce2005-04-21 17:05:28 -07008423 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)
8424 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8425
Linus Torvalds1da177e2005-04-16 15:20:36 -07008426 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
Matt Carlson027455a2008-12-21 20:19:30 -08008427 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
8428
Matt Carlsone849cdc2009-11-13 13:03:38 +00008429 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
8430 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson027455a2008-12-21 20:19:30 -08008431 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
8432 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008433
Matt Carlsonf2096f92011-04-05 14:22:48 +00008434 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8435 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
8436
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008437 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
8438 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
8439 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8440 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlson1407deb2011-04-05 14:22:44 +00008441 (tp->tg3_flags3 & TG3_FLG3_57765_PLUS)) {
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008442 val = tr32(TG3_RDMA_RSRVCTRL_REG);
Matt Carlsond78b59f2011-04-05 14:22:46 +00008443 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8444 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +00008445 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
8446 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
8447 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
8448 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
8449 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
8450 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +00008451 }
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008452 tw32(TG3_RDMA_RSRVCTRL_REG,
8453 val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
8454 }
8455
Matt Carlsond78b59f2011-04-05 14:22:46 +00008456 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8457 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsond309a462010-09-30 10:34:31 +00008458 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
8459 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val |
8460 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
8461 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
8462 }
8463
Linus Torvalds1da177e2005-04-16 15:20:36 -07008464 /* Receive/send statistics. */
Michael Chan16613942006-06-29 20:15:13 -07008465 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
8466 val = tr32(RCVLPC_STATS_ENABLE);
8467 val &= ~RCVLPC_STATSENAB_DACK_FIX;
8468 tw32(RCVLPC_STATS_ENABLE, val);
8469 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
8470 (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008471 val = tr32(RCVLPC_STATS_ENABLE);
8472 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
8473 tw32(RCVLPC_STATS_ENABLE, val);
8474 } else {
8475 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
8476 }
8477 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
8478 tw32(SNDDATAI_STATSENAB, 0xffffff);
8479 tw32(SNDDATAI_STATSCTRL,
8480 (SNDDATAI_SCTRL_ENABLE |
8481 SNDDATAI_SCTRL_FASTUPD));
8482
8483 /* Setup host coalescing engine. */
8484 tw32(HOSTCC_MODE, 0);
8485 for (i = 0; i < 2000; i++) {
8486 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
8487 break;
8488 udelay(10);
8489 }
8490
Michael Chand244c892005-07-05 14:42:33 -07008491 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008492
Linus Torvalds1da177e2005-04-16 15:20:36 -07008493 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
8494 /* Status/statistics block address. See tg3_timer,
8495 * the tg3_periodic_fetch_stats call there, and
8496 * tg3_get_stats to see how this works for 5705/5750 chips.
8497 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008498 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8499 ((u64) tp->stats_mapping >> 32));
8500 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8501 ((u64) tp->stats_mapping & 0xffffffff));
8502 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008503
Linus Torvalds1da177e2005-04-16 15:20:36 -07008504 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008505
8506 /* Clear statistics and status block memory areas */
8507 for (i = NIC_SRAM_STATS_BLK;
8508 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
8509 i += sizeof(u32)) {
8510 tg3_write_mem(tp, i, 0);
8511 udelay(40);
8512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008513 }
8514
8515 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
8516
8517 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
8518 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
8519 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
8520 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
8521
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008522 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
8523 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -07008524 /* reset to prevent losing 1st rx packet intermittently */
8525 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8526 udelay(10);
8527 }
8528
Matt Carlson3bda1252008-08-15 14:08:22 -07008529 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008530 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -07008531 else
8532 tp->mac_mode = 0;
8533 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Linus Torvalds1da177e2005-04-16 15:20:36 -07008534 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE | MAC_MODE_FHDE_ENABLE;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07008535 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008536 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07008537 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
8538 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008539 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
8540 udelay(40);
8541
Michael Chan314fba32005-04-21 17:07:04 -07008542 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Michael Chan9d26e212006-12-07 00:21:14 -08008543 * If TG3_FLG2_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07008544 * register to preserve the GPIO settings for LOMs. The GPIOs,
8545 * whether used as inputs or outputs, are set by boot code after
8546 * reset.
8547 */
Michael Chan9d26e212006-12-07 00:21:14 -08008548 if (!(tp->tg3_flags2 & TG3_FLG2_IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07008549 u32 gpio_mask;
8550
Michael Chan9d26e212006-12-07 00:21:14 -08008551 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
8552 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
8553 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07008554
8555 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
8556 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
8557 GRC_LCLCTRL_GPIO_OUTPUT3;
8558
Michael Chanaf36e6b2006-03-23 01:28:06 -08008559 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
8560 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
8561
Gary Zambranoaaf84462007-05-05 11:51:45 -07008562 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07008563 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
8564
8565 /* GPIO1 must be driven high for eeprom write protect */
Michael Chan9d26e212006-12-07 00:21:14 -08008566 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT)
8567 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
8568 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07008569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008570 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8571 udelay(100);
8572
Matt Carlson0583d522011-01-25 15:58:50 +00008573 if ((tp->tg3_flags2 & TG3_FLG2_USING_MSIX) &&
8574 tp->irq_cnt > 1) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008575 val = tr32(MSGINT_MODE);
8576 val |= MSGINT_MODE_MULTIVEC_EN | MSGINT_MODE_ENABLE;
8577 tw32(MSGINT_MODE, val);
8578 }
8579
Linus Torvalds1da177e2005-04-16 15:20:36 -07008580 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
8581 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
8582 udelay(40);
8583 }
8584
8585 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
8586 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
8587 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
8588 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
8589 WDMAC_MODE_LNGREAD_ENAB);
8590
Matt Carlsonc5908932011-03-09 16:58:25 +00008591 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8592 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Matt Carlson29ea0952009-08-25 10:07:54 +00008593 if ((tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008594 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
8595 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
8596 /* nothing */
8597 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Matt Carlsonc5908932011-03-09 16:58:25 +00008598 !(tp->tg3_flags2 & TG3_FLG2_IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008599 val |= WDMAC_MODE_RX_ACCEL;
8600 }
8601 }
8602
Michael Chand9ab5ad2006-03-20 22:27:35 -08008603 /* Enable host coalescing bug fix */
Matt Carlson321d32a2008-11-21 17:22:19 -08008604 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Matt Carlsonf51f3562008-05-25 23:45:08 -07008605 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad2006-03-20 22:27:35 -08008606
Matt Carlson788a0352009-11-02 14:26:03 +00008607 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
8608 val |= WDMAC_MODE_BURST_ALL_DATA;
8609
Linus Torvalds1da177e2005-04-16 15:20:36 -07008610 tw32_f(WDMAC_MODE, val);
8611 udelay(40);
8612
Matt Carlson9974a352007-10-07 23:27:28 -07008613 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
8614 u16 pcix_cmd;
8615
8616 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8617 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008618 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07008619 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
8620 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008621 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07008622 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
8623 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008624 }
Matt Carlson9974a352007-10-07 23:27:28 -07008625 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8626 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008627 }
8628
8629 tw32_f(RDMAC_MODE, rdmac_mode);
8630 udelay(40);
8631
8632 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
8633 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
8634 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07008635
8636 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
8637 tw32(SNDDATAC_MODE,
8638 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
8639 else
8640 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
8641
Linus Torvalds1da177e2005-04-16 15:20:36 -07008642 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
8643 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008644 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Matt Carlsonde9f5232011-04-05 14:22:43 +00008645 if (tp->tg3_flags3 & TG3_FLG3_LRG_PROD_RING_CAP)
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008646 val |= RCVDBDI_MODE_LRG_RING_SZ;
8647 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008648 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008649 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
8650 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008651 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Matt Carlson19cfaec2009-12-03 08:36:20 +00008652 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008653 val |= SNDBDI_MODE_MULTI_TXQ_EN;
8654 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008655 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
8656
8657 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
8658 err = tg3_load_5701_a0_firmware_fix(tp);
8659 if (err)
8660 return err;
8661 }
8662
Linus Torvalds1da177e2005-04-16 15:20:36 -07008663 if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) {
8664 err = tg3_load_tso_firmware(tp);
8665 if (err)
8666 return err;
8667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008668
8669 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +00008670
Matt Carlsonb1d05212010-06-05 17:24:31 +00008671 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
8672 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
8673 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +00008674
8675 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
8676 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
8677 tp->tx_mode &= ~val;
8678 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
8679 }
8680
Linus Torvalds1da177e2005-04-16 15:20:36 -07008681 tw32_f(MAC_TX_MODE, tp->tx_mode);
8682 udelay(100);
8683
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008684 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS) {
8685 u32 reg = MAC_RSS_INDIR_TBL_0;
8686 u8 *ent = (u8 *)&val;
8687
8688 /* Setup the indirection table */
8689 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
8690 int idx = i % sizeof(val);
8691
Matt Carlson5efeeea2010-07-11 09:31:40 +00008692 ent[idx] = i % (tp->irq_cnt - 1);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008693 if (idx == sizeof(val) - 1) {
8694 tw32(reg, val);
8695 reg += 4;
8696 }
8697 }
8698
8699 /* Setup the "secret" hash key. */
8700 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
8701 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
8702 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
8703 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
8704 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
8705 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
8706 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
8707 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
8708 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
8709 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
8710 }
8711
Linus Torvalds1da177e2005-04-16 15:20:36 -07008712 tp->rx_mode = RX_MODE_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08008713 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Michael Chanaf36e6b2006-03-23 01:28:06 -08008714 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
8715
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008716 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)
8717 tp->rx_mode |= RX_MODE_RSS_ENABLE |
8718 RX_MODE_RSS_ITBL_HASH_BITS_7 |
8719 RX_MODE_RSS_IPV6_HASH_EN |
8720 RX_MODE_RSS_TCP_IPV6_HASH_EN |
8721 RX_MODE_RSS_IPV4_HASH_EN |
8722 RX_MODE_RSS_TCP_IPV4_HASH_EN;
8723
Linus Torvalds1da177e2005-04-16 15:20:36 -07008724 tw32_f(MAC_RX_MODE, tp->rx_mode);
8725 udelay(10);
8726
Linus Torvalds1da177e2005-04-16 15:20:36 -07008727 tw32(MAC_LED_CTRL, tp->led_ctrl);
8728
8729 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008730 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008731 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8732 udelay(10);
8733 }
8734 tw32_f(MAC_RX_MODE, tp->rx_mode);
8735 udelay(10);
8736
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008737 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008738 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008739 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008740 /* Set drive transmission level to 1.2V */
8741 /* only if the signal pre-emphasis bit is not set */
8742 val = tr32(MAC_SERDES_CFG);
8743 val &= 0xfffff000;
8744 val |= 0x880;
8745 tw32(MAC_SERDES_CFG, val);
8746 }
8747 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
8748 tw32(MAC_SERDES_CFG, 0x616000);
8749 }
8750
8751 /* Prevent chip from dropping frames when flow control
8752 * is enabled.
8753 */
Matt Carlson666bc832010-01-20 16:58:03 +00008754 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8755 val = 1;
8756 else
8757 val = 2;
8758 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008759
8760 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008761 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008762 /* Use hardware link auto-negotiation */
8763 tp->tg3_flags2 |= TG3_FLG2_HW_AUTONEG;
8764 }
8765
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008766 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Michael Chand4d2c552006-03-20 17:47:20 -08008767 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)) {
8768 u32 tmp;
8769
8770 tmp = tr32(SERDES_RX_CTRL);
8771 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
8772 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
8773 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
8774 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8775 }
8776
Matt Carlsondd477002008-05-25 23:45:58 -07008777 if (!(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)) {
Matt Carlson80096062010-08-02 11:26:06 +00008778 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
8779 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07008780 tp->link_config.speed = tp->link_config.orig_speed;
8781 tp->link_config.duplex = tp->link_config.orig_duplex;
8782 tp->link_config.autoneg = tp->link_config.orig_autoneg;
8783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008784
Matt Carlsondd477002008-05-25 23:45:58 -07008785 err = tg3_setup_phy(tp, 0);
8786 if (err)
8787 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008788
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008789 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
8790 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07008791 u32 tmp;
8792
8793 /* Clear CRC stats. */
8794 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
8795 tg3_writephy(tp, MII_TG3_TEST1,
8796 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00008797 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -07008798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008799 }
8800 }
8801
8802 __tg3_set_rx_mode(tp->dev);
8803
8804 /* Initialize receive rules. */
8805 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
8806 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
8807 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
8808 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
8809
Michael Chan4cf78e42005-07-25 12:29:19 -07008810 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Michael Chana4e2b342005-10-26 15:46:52 -07008811 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008812 limit = 8;
8813 else
8814 limit = 16;
8815 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF)
8816 limit -= 4;
8817 switch (limit) {
8818 case 16:
8819 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
8820 case 15:
8821 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
8822 case 14:
8823 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
8824 case 13:
8825 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
8826 case 12:
8827 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
8828 case 11:
8829 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
8830 case 10:
8831 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
8832 case 9:
8833 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
8834 case 8:
8835 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
8836 case 7:
8837 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
8838 case 6:
8839 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
8840 case 5:
8841 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
8842 case 4:
8843 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
8844 case 3:
8845 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
8846 case 2:
8847 case 1:
8848
8849 default:
8850 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07008851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008852
Matt Carlson9ce768e2007-10-11 19:49:11 -07008853 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
8854 /* Write our heartbeat update interval to APE. */
8855 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
8856 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07008857
Linus Torvalds1da177e2005-04-16 15:20:36 -07008858 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
8859
Linus Torvalds1da177e2005-04-16 15:20:36 -07008860 return 0;
8861}
8862
8863/* Called at device open time to get the chip ready for
8864 * packet processing. Invoked with tp->lock held.
8865 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008866static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008867{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008868 tg3_switch_clocks(tp);
8869
8870 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
8871
Matt Carlson2f751b62008-08-04 23:17:34 -07008872 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008873}
8874
8875#define TG3_STAT_ADD32(PSTAT, REG) \
8876do { u32 __val = tr32(REG); \
8877 (PSTAT)->low += __val; \
8878 if ((PSTAT)->low < __val) \
8879 (PSTAT)->high += 1; \
8880} while (0)
8881
8882static void tg3_periodic_fetch_stats(struct tg3 *tp)
8883{
8884 struct tg3_hw_stats *sp = tp->hw_stats;
8885
8886 if (!netif_carrier_ok(tp->dev))
8887 return;
8888
8889 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
8890 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
8891 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
8892 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
8893 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
8894 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
8895 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
8896 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
8897 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
8898 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
8899 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
8900 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
8901 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
8902
8903 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
8904 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
8905 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
8906 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
8907 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
8908 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
8909 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
8910 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
8911 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
8912 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
8913 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
8914 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
8915 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
8916 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07008917
8918 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
Matt Carlson4d958472011-04-20 07:57:35 +00008919 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717) {
8920 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
8921 } else {
8922 u32 val = tr32(HOSTCC_FLOW_ATTN);
8923 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
8924 if (val) {
8925 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
8926 sp->rx_discards.low += val;
8927 if (sp->rx_discards.low < val)
8928 sp->rx_discards.high += 1;
8929 }
8930 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
8931 }
Michael Chan463d3052006-05-22 16:36:27 -07008932 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008933}
8934
8935static void tg3_timer(unsigned long __opaque)
8936{
8937 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008938
Michael Chanf475f162006-03-27 23:20:14 -08008939 if (tp->irq_sync)
8940 goto restart_timer;
8941
David S. Millerf47c11e2005-06-24 20:18:35 -07008942 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008943
David S. Millerfac9b832005-05-18 22:46:34 -07008944 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)) {
8945 /* All of this garbage is because when using non-tagged
8946 * IRQ status the mailbox/status_block protocol the chip
8947 * uses with the cpu is race prone.
8948 */
Matt Carlson898a56f2009-08-28 14:02:40 +00008949 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -07008950 tw32(GRC_LOCAL_CTRL,
8951 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
8952 } else {
8953 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00008954 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -07008955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008956
David S. Millerfac9b832005-05-18 22:46:34 -07008957 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
8958 tp->tg3_flags2 |= TG3_FLG2_RESTART_TIMER;
David S. Millerf47c11e2005-06-24 20:18:35 -07008959 spin_unlock(&tp->lock);
David S. Millerfac9b832005-05-18 22:46:34 -07008960 schedule_work(&tp->reset_task);
8961 return;
8962 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008963 }
8964
Linus Torvalds1da177e2005-04-16 15:20:36 -07008965 /* This part only runs once per second. */
8966 if (!--tp->timer_counter) {
David S. Millerfac9b832005-05-18 22:46:34 -07008967 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
8968 tg3_periodic_fetch_stats(tp);
8969
Matt Carlson52b02d02010-10-14 10:37:41 +00008970 if (tp->setlpicnt && !--tp->setlpicnt) {
8971 u32 val = tr32(TG3_CPMU_EEE_MODE);
8972 tw32(TG3_CPMU_EEE_MODE,
8973 val | TG3_CPMU_EEEMD_LPI_ENABLE);
8974 }
8975
Linus Torvalds1da177e2005-04-16 15:20:36 -07008976 if (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) {
8977 u32 mac_stat;
8978 int phy_event;
8979
8980 mac_stat = tr32(MAC_STATUS);
8981
8982 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008983 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008984 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
8985 phy_event = 1;
8986 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
8987 phy_event = 1;
8988
8989 if (phy_event)
8990 tg3_setup_phy(tp, 0);
8991 } else if (tp->tg3_flags & TG3_FLAG_POLL_SERDES) {
8992 u32 mac_stat = tr32(MAC_STATUS);
8993 int need_setup = 0;
8994
8995 if (netif_carrier_ok(tp->dev) &&
8996 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
8997 need_setup = 1;
8998 }
Matt Carlsonbe98da62010-07-11 09:31:46 +00008999 if (!netif_carrier_ok(tp->dev) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009000 (mac_stat & (MAC_STATUS_PCS_SYNCED |
9001 MAC_STATUS_SIGNAL_DET))) {
9002 need_setup = 1;
9003 }
9004 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07009005 if (!tp->serdes_counter) {
9006 tw32_f(MAC_MODE,
9007 (tp->mac_mode &
9008 ~MAC_MODE_PORT_MODE_MASK));
9009 udelay(40);
9010 tw32_f(MAC_MODE, tp->mac_mode);
9011 udelay(40);
9012 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009013 tg3_setup_phy(tp, 0);
9014 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009015 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Matt Carlson2138c002010-07-11 09:31:43 +00009016 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -07009017 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +00009018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009019
9020 tp->timer_counter = tp->timer_multiplier;
9021 }
9022
Michael Chan130b8e42006-09-27 16:00:40 -07009023 /* Heartbeat is only sent once every 2 seconds.
9024 *
9025 * The heartbeat is to tell the ASF firmware that the host
9026 * driver is still alive. In the event that the OS crashes,
9027 * ASF needs to reset the hardware to free up the FIFO space
9028 * that may be filled with rx packets destined for the host.
9029 * If the FIFO is full, ASF will no longer function properly.
9030 *
9031 * Unintended resets have been reported on real time kernels
9032 * where the timer doesn't run on time. Netpoll will also have
9033 * same problem.
9034 *
9035 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
9036 * to check the ring condition when the heartbeat is expiring
9037 * before doing the reset. This will prevent most unintended
9038 * resets.
9039 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009040 if (!--tp->asf_counter) {
Matt Carlsonbc7959b2008-08-15 14:08:55 -07009041 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
9042 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07009043 tg3_wait_for_event_ack(tp);
9044
Michael Chanbbadf502006-04-06 21:46:34 -07009045 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07009046 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07009047 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +00009048 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
9049 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -07009050
9051 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009052 }
9053 tp->asf_counter = tp->asf_multiplier;
9054 }
9055
David S. Millerf47c11e2005-06-24 20:18:35 -07009056 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009057
Michael Chanf475f162006-03-27 23:20:14 -08009058restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07009059 tp->timer.expires = jiffies + tp->timer_offset;
9060 add_timer(&tp->timer);
9061}
9062
Matt Carlson4f125f42009-09-01 12:55:02 +00009063static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -08009064{
David Howells7d12e782006-10-05 14:55:46 +01009065 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009066 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +00009067 char *name;
9068 struct tg3_napi *tnapi = &tp->napi[irq_num];
9069
9070 if (tp->irq_cnt == 1)
9071 name = tp->dev->name;
9072 else {
9073 name = &tnapi->irq_lbl[0];
9074 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
9075 name[IFNAMSIZ-1] = 0;
9076 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009077
Matt Carlson679563f2009-09-01 12:55:46 +00009078 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI_OR_MSIX) {
Michael Chanfcfa0a32006-03-20 22:28:41 -08009079 fn = tg3_msi;
9080 if (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)
9081 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009082 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009083 } else {
9084 fn = tg3_interrupt;
9085 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)
9086 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009087 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009088 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009089
9090 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009091}
9092
Michael Chan79381092005-04-21 17:13:59 -07009093static int tg3_test_interrupt(struct tg3 *tp)
9094{
Matt Carlson09943a12009-08-28 14:01:57 +00009095 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -07009096 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -07009097 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009098 u32 val;
Michael Chan79381092005-04-21 17:13:59 -07009099
Michael Chand4bc3922005-05-29 14:59:20 -07009100 if (!netif_running(dev))
9101 return -ENODEV;
9102
Michael Chan79381092005-04-21 17:13:59 -07009103 tg3_disable_ints(tp);
9104
Matt Carlson4f125f42009-09-01 12:55:02 +00009105 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009106
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009107 /*
9108 * Turn off MSI one shot mode. Otherwise this test has no
9109 * observable way to know whether the interrupt was delivered.
9110 */
Matt Carlson1407deb2011-04-05 14:22:44 +00009111 if ((tp->tg3_flags3 & TG3_FLG3_57765_PLUS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009112 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
9113 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
9114 tw32(MSGINT_MODE, val);
9115 }
9116
Matt Carlson4f125f42009-09-01 12:55:02 +00009117 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Matt Carlson09943a12009-08-28 14:01:57 +00009118 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009119 if (err)
9120 return err;
9121
Matt Carlson898a56f2009-08-28 14:02:40 +00009122 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -07009123 tg3_enable_ints(tp);
9124
9125 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00009126 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -07009127
9128 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -07009129 u32 int_mbox, misc_host_ctrl;
9130
Matt Carlson898a56f2009-08-28 14:02:40 +00009131 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -07009132 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
9133
9134 if ((int_mbox != 0) ||
9135 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
9136 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -07009137 break;
Michael Chanb16250e2006-09-27 16:10:14 -07009138 }
9139
Michael Chan79381092005-04-21 17:13:59 -07009140 msleep(10);
9141 }
9142
9143 tg3_disable_ints(tp);
9144
Matt Carlson4f125f42009-09-01 12:55:02 +00009145 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009146
Matt Carlson4f125f42009-09-01 12:55:02 +00009147 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009148
9149 if (err)
9150 return err;
9151
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009152 if (intr_ok) {
9153 /* Reenable MSI one shot mode. */
Matt Carlson1407deb2011-04-05 14:22:44 +00009154 if ((tp->tg3_flags3 & TG3_FLG3_57765_PLUS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009155 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
9156 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
9157 tw32(MSGINT_MODE, val);
9158 }
Michael Chan79381092005-04-21 17:13:59 -07009159 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009160 }
Michael Chan79381092005-04-21 17:13:59 -07009161
9162 return -EIO;
9163}
9164
9165/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
9166 * successfully restored
9167 */
9168static int tg3_test_msi(struct tg3 *tp)
9169{
Michael Chan79381092005-04-21 17:13:59 -07009170 int err;
9171 u16 pci_cmd;
9172
9173 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSI))
9174 return 0;
9175
9176 /* Turn off SERR reporting in case MSI terminates with Master
9177 * Abort.
9178 */
9179 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
9180 pci_write_config_word(tp->pdev, PCI_COMMAND,
9181 pci_cmd & ~PCI_COMMAND_SERR);
9182
9183 err = tg3_test_interrupt(tp);
9184
9185 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
9186
9187 if (!err)
9188 return 0;
9189
9190 /* other failures */
9191 if (err != -EIO)
9192 return err;
9193
9194 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009195 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
9196 "to INTx mode. Please report this failure to the PCI "
9197 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -07009198
Matt Carlson4f125f42009-09-01 12:55:02 +00009199 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +00009200
Michael Chan79381092005-04-21 17:13:59 -07009201 pci_disable_msi(tp->pdev);
9202
9203 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI;
Andre Detschdc8bf1b2010-04-26 07:27:07 +00009204 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -07009205
Matt Carlson4f125f42009-09-01 12:55:02 +00009206 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009207 if (err)
9208 return err;
9209
9210 /* Need to reset the chip because the MSI cycle may have terminated
9211 * with Master Abort.
9212 */
David S. Millerf47c11e2005-06-24 20:18:35 -07009213 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009214
Michael Chan944d9802005-05-29 14:57:48 -07009215 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009216 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009217
David S. Millerf47c11e2005-06-24 20:18:35 -07009218 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009219
9220 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +00009221 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -07009222
9223 return err;
9224}
9225
Matt Carlson9e9fd122009-01-19 16:57:45 -08009226static int tg3_request_firmware(struct tg3 *tp)
9227{
9228 const __be32 *fw_data;
9229
9230 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009231 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
9232 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009233 return -ENOENT;
9234 }
9235
9236 fw_data = (void *)tp->fw->data;
9237
9238 /* Firmware blob starts with version numbers, followed by
9239 * start address and _full_ length including BSS sections
9240 * (which must be longer than the actual data, of course
9241 */
9242
9243 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
9244 if (tp->fw_len < (tp->fw->size - 12)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009245 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
9246 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009247 release_firmware(tp->fw);
9248 tp->fw = NULL;
9249 return -EINVAL;
9250 }
9251
9252 /* We no longer need firmware; we have it. */
9253 tp->fw_needed = NULL;
9254 return 0;
9255}
9256
Matt Carlson679563f2009-09-01 12:55:46 +00009257static bool tg3_enable_msix(struct tg3 *tp)
9258{
9259 int i, rc, cpus = num_online_cpus();
9260 struct msix_entry msix_ent[tp->irq_max];
9261
9262 if (cpus == 1)
9263 /* Just fallback to the simpler MSI mode. */
9264 return false;
9265
9266 /*
9267 * We want as many rx rings enabled as there are cpus.
9268 * The first MSIX vector only deals with link interrupts, etc,
9269 * so we add one to the number of vectors we are requesting.
9270 */
9271 tp->irq_cnt = min_t(unsigned, cpus + 1, tp->irq_max);
9272
9273 for (i = 0; i < tp->irq_max; i++) {
9274 msix_ent[i].entry = i;
9275 msix_ent[i].vector = 0;
9276 }
9277
9278 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +00009279 if (rc < 0) {
9280 return false;
9281 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +00009282 if (pci_enable_msix(tp->pdev, msix_ent, rc))
9283 return false;
Joe Perches05dbe002010-02-17 19:44:19 +00009284 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
9285 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +00009286 tp->irq_cnt = rc;
9287 }
9288
9289 for (i = 0; i < tp->irq_max; i++)
9290 tp->napi[i].irq_vec = msix_ent[i].vector;
9291
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009292 netif_set_real_num_tx_queues(tp->dev, 1);
9293 rc = tp->irq_cnt > 1 ? tp->irq_cnt - 1 : 1;
9294 if (netif_set_real_num_rx_queues(tp->dev, rc)) {
9295 pci_disable_msix(tp->pdev);
9296 return false;
9297 }
Matt Carlsonb92b9042010-11-24 08:31:51 +00009298
9299 if (tp->irq_cnt > 1) {
Matt Carlson2430b032010-06-05 17:24:34 +00009300 tp->tg3_flags3 |= TG3_FLG3_ENABLE_RSS;
Matt Carlsond78b59f2011-04-05 14:22:46 +00009301
9302 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
9303 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsonb92b9042010-11-24 08:31:51 +00009304 tp->tg3_flags3 |= TG3_FLG3_ENABLE_TSS;
9305 netif_set_real_num_tx_queues(tp->dev, tp->irq_cnt - 1);
9306 }
9307 }
Matt Carlson2430b032010-06-05 17:24:34 +00009308
Matt Carlson679563f2009-09-01 12:55:46 +00009309 return true;
9310}
9311
Matt Carlson07b01732009-08-28 14:01:15 +00009312static void tg3_ints_init(struct tg3 *tp)
9313{
Matt Carlson679563f2009-09-01 12:55:46 +00009314 if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSI_OR_MSIX) &&
9315 !(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +00009316 /* All MSI supporting chips should support tagged
9317 * status. Assert that this is the case.
9318 */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009319 netdev_warn(tp->dev,
9320 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +00009321 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +00009322 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009323
Matt Carlson679563f2009-09-01 12:55:46 +00009324 if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX) && tg3_enable_msix(tp))
9325 tp->tg3_flags2 |= TG3_FLG2_USING_MSIX;
9326 else if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSI) &&
9327 pci_enable_msi(tp->pdev) == 0)
9328 tp->tg3_flags2 |= TG3_FLG2_USING_MSI;
9329
9330 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI_OR_MSIX) {
9331 u32 msi_mode = tr32(MSGINT_MODE);
Matt Carlson0583d522011-01-25 15:58:50 +00009332 if ((tp->tg3_flags2 & TG3_FLG2_USING_MSIX) &&
9333 tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009334 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson679563f2009-09-01 12:55:46 +00009335 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
9336 }
9337defcfg:
9338 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSIX)) {
9339 tp->irq_cnt = 1;
9340 tp->napi[0].irq_vec = tp->pdev->irq;
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009341 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -07009342 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +00009343 }
Matt Carlson07b01732009-08-28 14:01:15 +00009344}
9345
9346static void tg3_ints_fini(struct tg3 *tp)
9347{
Matt Carlson679563f2009-09-01 12:55:46 +00009348 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
9349 pci_disable_msix(tp->pdev);
9350 else if (tp->tg3_flags2 & TG3_FLG2_USING_MSI)
9351 pci_disable_msi(tp->pdev);
9352 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI_OR_MSIX;
Matt Carlson774ee752010-08-02 11:25:56 +00009353 tp->tg3_flags3 &= ~(TG3_FLG3_ENABLE_RSS | TG3_FLG3_ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +00009354}
9355
Linus Torvalds1da177e2005-04-16 15:20:36 -07009356static int tg3_open(struct net_device *dev)
9357{
9358 struct tg3 *tp = netdev_priv(dev);
Matt Carlson4f125f42009-09-01 12:55:02 +00009359 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009360
Matt Carlson9e9fd122009-01-19 16:57:45 -08009361 if (tp->fw_needed) {
9362 err = tg3_request_firmware(tp);
9363 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
9364 if (err)
9365 return err;
9366 } else if (err) {
Joe Perches05dbe002010-02-17 19:44:19 +00009367 netdev_warn(tp->dev, "TSO capability disabled\n");
Matt Carlson9e9fd122009-01-19 16:57:45 -08009368 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
9369 } else if (!(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009370 netdev_notice(tp->dev, "TSO capability restored\n");
Matt Carlson9e9fd122009-01-19 16:57:45 -08009371 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
9372 }
9373 }
9374
Michael Chanc49a1562006-12-17 17:07:29 -08009375 netif_carrier_off(tp->dev);
9376
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009377 err = tg3_power_up(tp);
Matt Carlson2f751b62008-08-04 23:17:34 -07009378 if (err)
Michael Chanbc1c7562006-03-20 17:48:03 -08009379 return err;
Matt Carlson2f751b62008-08-04 23:17:34 -07009380
9381 tg3_full_lock(tp, 0);
Michael Chanbc1c7562006-03-20 17:48:03 -08009382
Linus Torvalds1da177e2005-04-16 15:20:36 -07009383 tg3_disable_ints(tp);
9384 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
9385
David S. Millerf47c11e2005-06-24 20:18:35 -07009386 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009387
Matt Carlson679563f2009-09-01 12:55:46 +00009388 /*
9389 * Setup interrupts first so we know how
9390 * many NAPI resources to allocate
9391 */
9392 tg3_ints_init(tp);
9393
Linus Torvalds1da177e2005-04-16 15:20:36 -07009394 /* The placement of this call is tied
9395 * to the setup and use of Host TX descriptors.
9396 */
9397 err = tg3_alloc_consistent(tp);
9398 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009399 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009400
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009401 tg3_napi_init(tp);
9402
Matt Carlsonfed97812009-09-01 13:10:19 +00009403 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07009404
Matt Carlson4f125f42009-09-01 12:55:02 +00009405 for (i = 0; i < tp->irq_cnt; i++) {
9406 struct tg3_napi *tnapi = &tp->napi[i];
9407 err = tg3_request_irq(tp, i);
9408 if (err) {
9409 for (i--; i >= 0; i--)
9410 free_irq(tnapi->irq_vec, tnapi);
9411 break;
9412 }
9413 }
Matt Carlson07b01732009-08-28 14:01:15 +00009414
9415 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009416 goto err_out2;
Matt Carlson07b01732009-08-28 14:01:15 +00009417
David S. Millerf47c11e2005-06-24 20:18:35 -07009418 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009419
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009420 err = tg3_init_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009421 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -07009422 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009423 tg3_free_rings(tp);
9424 } else {
David S. Millerfac9b832005-05-18 22:46:34 -07009425 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)
9426 tp->timer_offset = HZ;
9427 else
9428 tp->timer_offset = HZ / 10;
9429
9430 BUG_ON(tp->timer_offset > HZ);
9431 tp->timer_counter = tp->timer_multiplier =
9432 (HZ / tp->timer_offset);
9433 tp->asf_counter = tp->asf_multiplier =
Michael Chan28fbef72005-10-26 15:48:35 -07009434 ((HZ / tp->timer_offset) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009435
9436 init_timer(&tp->timer);
9437 tp->timer.expires = jiffies + tp->timer_offset;
9438 tp->timer.data = (unsigned long) tp;
9439 tp->timer.function = tg3_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009440 }
9441
David S. Millerf47c11e2005-06-24 20:18:35 -07009442 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009443
Matt Carlson07b01732009-08-28 14:01:15 +00009444 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009445 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009446
Michael Chan79381092005-04-21 17:13:59 -07009447 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
9448 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -07009449
Michael Chan79381092005-04-21 17:13:59 -07009450 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -07009451 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -07009452 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -07009453 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07009454 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009455
Matt Carlson679563f2009-09-01 12:55:46 +00009456 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -07009457 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009458
Matt Carlson1407deb2011-04-05 14:22:44 +00009459 if (!(tp->tg3_flags3 & TG3_FLG3_57765_PLUS) &&
Matt Carlsonc885e822010-08-02 11:25:57 +00009460 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009461 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009462
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009463 tw32(PCIE_TRANSACTION_CFG,
9464 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009465 }
Michael Chan79381092005-04-21 17:13:59 -07009466 }
9467
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009468 tg3_phy_start(tp);
9469
David S. Millerf47c11e2005-06-24 20:18:35 -07009470 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009471
Michael Chan79381092005-04-21 17:13:59 -07009472 add_timer(&tp->timer);
9473 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009474 tg3_enable_ints(tp);
9475
David S. Millerf47c11e2005-06-24 20:18:35 -07009476 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009477
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009478 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009479
9480 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +00009481
Matt Carlson679563f2009-09-01 12:55:46 +00009482err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +00009483 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9484 struct tg3_napi *tnapi = &tp->napi[i];
9485 free_irq(tnapi->irq_vec, tnapi);
9486 }
Matt Carlson07b01732009-08-28 14:01:15 +00009487
Matt Carlson679563f2009-09-01 12:55:46 +00009488err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +00009489 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009490 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00009491 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +00009492
9493err_out1:
9494 tg3_ints_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00009495 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009496}
9497
Eric Dumazet511d2222010-07-07 20:44:24 +00009498static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *,
9499 struct rtnl_link_stats64 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009500static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *);
9501
9502static int tg3_close(struct net_device *dev)
9503{
Matt Carlson4f125f42009-09-01 12:55:02 +00009504 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009505 struct tg3 *tp = netdev_priv(dev);
9506
Matt Carlsonfed97812009-09-01 13:10:19 +00009507 tg3_napi_disable(tp);
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07009508 cancel_work_sync(&tp->reset_task);
Michael Chan7faa0062006-02-02 17:29:28 -08009509
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009510 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009511
9512 del_timer_sync(&tp->timer);
9513
Matt Carlson24bb4fb2009-10-05 17:55:29 +00009514 tg3_phy_stop(tp);
9515
David S. Millerf47c11e2005-06-24 20:18:35 -07009516 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009517
9518 tg3_disable_ints(tp);
9519
Michael Chan944d9802005-05-29 14:57:48 -07009520 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009521 tg3_free_rings(tp);
Michael Chan5cf64b82007-05-05 12:11:21 -07009522 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009523
David S. Millerf47c11e2005-06-24 20:18:35 -07009524 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009525
Matt Carlson4f125f42009-09-01 12:55:02 +00009526 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9527 struct tg3_napi *tnapi = &tp->napi[i];
9528 free_irq(tnapi->irq_vec, tnapi);
9529 }
Matt Carlson07b01732009-08-28 14:01:15 +00009530
9531 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009532
Eric Dumazet511d2222010-07-07 20:44:24 +00009533 tg3_get_stats64(tp->dev, &tp->net_stats_prev);
9534
Linus Torvalds1da177e2005-04-16 15:20:36 -07009535 memcpy(&tp->estats_prev, tg3_get_estats(tp),
9536 sizeof(tp->estats_prev));
9537
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009538 tg3_napi_fini(tp);
9539
Linus Torvalds1da177e2005-04-16 15:20:36 -07009540 tg3_free_consistent(tp);
9541
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009542 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -08009543
9544 netif_carrier_off(tp->dev);
9545
Linus Torvalds1da177e2005-04-16 15:20:36 -07009546 return 0;
9547}
9548
Eric Dumazet511d2222010-07-07 20:44:24 +00009549static inline u64 get_stat64(tg3_stat64_t *val)
Stefan Buehler816f8b82008-08-15 14:10:54 -07009550{
9551 return ((u64)val->high << 32) | ((u64)val->low);
9552}
9553
Eric Dumazet511d2222010-07-07 20:44:24 +00009554static u64 calc_crc_errors(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009555{
9556 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9557
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009558 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009559 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
9560 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009561 u32 val;
9562
David S. Millerf47c11e2005-06-24 20:18:35 -07009563 spin_lock_bh(&tp->lock);
Michael Chan569a5df2007-02-13 12:18:15 -08009564 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
9565 tg3_writephy(tp, MII_TG3_TEST1,
9566 val | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009567 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009568 } else
9569 val = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07009570 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009571
9572 tp->phy_crc_errors += val;
9573
9574 return tp->phy_crc_errors;
9575 }
9576
9577 return get_stat64(&hw_stats->rx_fcs_errors);
9578}
9579
9580#define ESTAT_ADD(member) \
9581 estats->member = old_estats->member + \
Eric Dumazet511d2222010-07-07 20:44:24 +00009582 get_stat64(&hw_stats->member)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009583
9584static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *tp)
9585{
9586 struct tg3_ethtool_stats *estats = &tp->estats;
9587 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
9588 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9589
9590 if (!hw_stats)
9591 return old_estats;
9592
9593 ESTAT_ADD(rx_octets);
9594 ESTAT_ADD(rx_fragments);
9595 ESTAT_ADD(rx_ucast_packets);
9596 ESTAT_ADD(rx_mcast_packets);
9597 ESTAT_ADD(rx_bcast_packets);
9598 ESTAT_ADD(rx_fcs_errors);
9599 ESTAT_ADD(rx_align_errors);
9600 ESTAT_ADD(rx_xon_pause_rcvd);
9601 ESTAT_ADD(rx_xoff_pause_rcvd);
9602 ESTAT_ADD(rx_mac_ctrl_rcvd);
9603 ESTAT_ADD(rx_xoff_entered);
9604 ESTAT_ADD(rx_frame_too_long_errors);
9605 ESTAT_ADD(rx_jabbers);
9606 ESTAT_ADD(rx_undersize_packets);
9607 ESTAT_ADD(rx_in_length_errors);
9608 ESTAT_ADD(rx_out_length_errors);
9609 ESTAT_ADD(rx_64_or_less_octet_packets);
9610 ESTAT_ADD(rx_65_to_127_octet_packets);
9611 ESTAT_ADD(rx_128_to_255_octet_packets);
9612 ESTAT_ADD(rx_256_to_511_octet_packets);
9613 ESTAT_ADD(rx_512_to_1023_octet_packets);
9614 ESTAT_ADD(rx_1024_to_1522_octet_packets);
9615 ESTAT_ADD(rx_1523_to_2047_octet_packets);
9616 ESTAT_ADD(rx_2048_to_4095_octet_packets);
9617 ESTAT_ADD(rx_4096_to_8191_octet_packets);
9618 ESTAT_ADD(rx_8192_to_9022_octet_packets);
9619
9620 ESTAT_ADD(tx_octets);
9621 ESTAT_ADD(tx_collisions);
9622 ESTAT_ADD(tx_xon_sent);
9623 ESTAT_ADD(tx_xoff_sent);
9624 ESTAT_ADD(tx_flow_control);
9625 ESTAT_ADD(tx_mac_errors);
9626 ESTAT_ADD(tx_single_collisions);
9627 ESTAT_ADD(tx_mult_collisions);
9628 ESTAT_ADD(tx_deferred);
9629 ESTAT_ADD(tx_excessive_collisions);
9630 ESTAT_ADD(tx_late_collisions);
9631 ESTAT_ADD(tx_collide_2times);
9632 ESTAT_ADD(tx_collide_3times);
9633 ESTAT_ADD(tx_collide_4times);
9634 ESTAT_ADD(tx_collide_5times);
9635 ESTAT_ADD(tx_collide_6times);
9636 ESTAT_ADD(tx_collide_7times);
9637 ESTAT_ADD(tx_collide_8times);
9638 ESTAT_ADD(tx_collide_9times);
9639 ESTAT_ADD(tx_collide_10times);
9640 ESTAT_ADD(tx_collide_11times);
9641 ESTAT_ADD(tx_collide_12times);
9642 ESTAT_ADD(tx_collide_13times);
9643 ESTAT_ADD(tx_collide_14times);
9644 ESTAT_ADD(tx_collide_15times);
9645 ESTAT_ADD(tx_ucast_packets);
9646 ESTAT_ADD(tx_mcast_packets);
9647 ESTAT_ADD(tx_bcast_packets);
9648 ESTAT_ADD(tx_carrier_sense_errors);
9649 ESTAT_ADD(tx_discards);
9650 ESTAT_ADD(tx_errors);
9651
9652 ESTAT_ADD(dma_writeq_full);
9653 ESTAT_ADD(dma_write_prioq_full);
9654 ESTAT_ADD(rxbds_empty);
9655 ESTAT_ADD(rx_discards);
9656 ESTAT_ADD(rx_errors);
9657 ESTAT_ADD(rx_threshold_hit);
9658
9659 ESTAT_ADD(dma_readq_full);
9660 ESTAT_ADD(dma_read_prioq_full);
9661 ESTAT_ADD(tx_comp_queue_full);
9662
9663 ESTAT_ADD(ring_set_send_prod_index);
9664 ESTAT_ADD(ring_status_update);
9665 ESTAT_ADD(nic_irqs);
9666 ESTAT_ADD(nic_avoided_irqs);
9667 ESTAT_ADD(nic_tx_threshold_hit);
9668
9669 return estats;
9670}
9671
Eric Dumazet511d2222010-07-07 20:44:24 +00009672static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
9673 struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009674{
9675 struct tg3 *tp = netdev_priv(dev);
Eric Dumazet511d2222010-07-07 20:44:24 +00009676 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009677 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9678
9679 if (!hw_stats)
9680 return old_stats;
9681
9682 stats->rx_packets = old_stats->rx_packets +
9683 get_stat64(&hw_stats->rx_ucast_packets) +
9684 get_stat64(&hw_stats->rx_mcast_packets) +
9685 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009686
Linus Torvalds1da177e2005-04-16 15:20:36 -07009687 stats->tx_packets = old_stats->tx_packets +
9688 get_stat64(&hw_stats->tx_ucast_packets) +
9689 get_stat64(&hw_stats->tx_mcast_packets) +
9690 get_stat64(&hw_stats->tx_bcast_packets);
9691
9692 stats->rx_bytes = old_stats->rx_bytes +
9693 get_stat64(&hw_stats->rx_octets);
9694 stats->tx_bytes = old_stats->tx_bytes +
9695 get_stat64(&hw_stats->tx_octets);
9696
9697 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -07009698 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009699 stats->tx_errors = old_stats->tx_errors +
9700 get_stat64(&hw_stats->tx_errors) +
9701 get_stat64(&hw_stats->tx_mac_errors) +
9702 get_stat64(&hw_stats->tx_carrier_sense_errors) +
9703 get_stat64(&hw_stats->tx_discards);
9704
9705 stats->multicast = old_stats->multicast +
9706 get_stat64(&hw_stats->rx_mcast_packets);
9707 stats->collisions = old_stats->collisions +
9708 get_stat64(&hw_stats->tx_collisions);
9709
9710 stats->rx_length_errors = old_stats->rx_length_errors +
9711 get_stat64(&hw_stats->rx_frame_too_long_errors) +
9712 get_stat64(&hw_stats->rx_undersize_packets);
9713
9714 stats->rx_over_errors = old_stats->rx_over_errors +
9715 get_stat64(&hw_stats->rxbds_empty);
9716 stats->rx_frame_errors = old_stats->rx_frame_errors +
9717 get_stat64(&hw_stats->rx_align_errors);
9718 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
9719 get_stat64(&hw_stats->tx_discards);
9720 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
9721 get_stat64(&hw_stats->tx_carrier_sense_errors);
9722
9723 stats->rx_crc_errors = old_stats->rx_crc_errors +
9724 calc_crc_errors(tp);
9725
John W. Linville4f63b872005-09-12 14:43:18 -07009726 stats->rx_missed_errors = old_stats->rx_missed_errors +
9727 get_stat64(&hw_stats->rx_discards);
9728
Eric Dumazetb0057c52010-10-10 19:55:52 +00009729 stats->rx_dropped = tp->rx_dropped;
9730
Linus Torvalds1da177e2005-04-16 15:20:36 -07009731 return stats;
9732}
9733
9734static inline u32 calc_crc(unsigned char *buf, int len)
9735{
9736 u32 reg;
9737 u32 tmp;
9738 int j, k;
9739
9740 reg = 0xffffffff;
9741
9742 for (j = 0; j < len; j++) {
9743 reg ^= buf[j];
9744
9745 for (k = 0; k < 8; k++) {
9746 tmp = reg & 0x01;
9747
9748 reg >>= 1;
9749
Matt Carlson859a5882010-04-05 10:19:28 +00009750 if (tmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009751 reg ^= 0xedb88320;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009752 }
9753 }
9754
9755 return ~reg;
9756}
9757
9758static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
9759{
9760 /* accept or reject all multicast frames */
9761 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
9762 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
9763 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
9764 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
9765}
9766
9767static void __tg3_set_rx_mode(struct net_device *dev)
9768{
9769 struct tg3 *tp = netdev_priv(dev);
9770 u32 rx_mode;
9771
9772 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
9773 RX_MODE_KEEP_VLAN_TAG);
9774
Matt Carlsonbf933c82011-01-25 15:58:49 +00009775#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009776 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
9777 * flag clear.
9778 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009779 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
9780 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9781#endif
9782
9783 if (dev->flags & IFF_PROMISC) {
9784 /* Promiscuous mode. */
9785 rx_mode |= RX_MODE_PROMISC;
9786 } else if (dev->flags & IFF_ALLMULTI) {
9787 /* Accept all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +00009788 tg3_set_multi(tp, 1);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00009789 } else if (netdev_mc_empty(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009790 /* Reject all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +00009791 tg3_set_multi(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009792 } else {
9793 /* Accept one or more multicast(s). */
Jiri Pirko22bedad2010-04-01 21:22:57 +00009794 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009795 u32 mc_filter[4] = { 0, };
9796 u32 regidx;
9797 u32 bit;
9798 u32 crc;
9799
Jiri Pirko22bedad2010-04-01 21:22:57 +00009800 netdev_for_each_mc_addr(ha, dev) {
9801 crc = calc_crc(ha->addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009802 bit = ~crc & 0x7f;
9803 regidx = (bit & 0x60) >> 5;
9804 bit &= 0x1f;
9805 mc_filter[regidx] |= (1 << bit);
9806 }
9807
9808 tw32(MAC_HASH_REG_0, mc_filter[0]);
9809 tw32(MAC_HASH_REG_1, mc_filter[1]);
9810 tw32(MAC_HASH_REG_2, mc_filter[2]);
9811 tw32(MAC_HASH_REG_3, mc_filter[3]);
9812 }
9813
9814 if (rx_mode != tp->rx_mode) {
9815 tp->rx_mode = rx_mode;
9816 tw32_f(MAC_RX_MODE, rx_mode);
9817 udelay(10);
9818 }
9819}
9820
9821static void tg3_set_rx_mode(struct net_device *dev)
9822{
9823 struct tg3 *tp = netdev_priv(dev);
9824
Michael Chane75f7c92006-03-20 21:33:26 -08009825 if (!netif_running(dev))
9826 return;
9827
David S. Millerf47c11e2005-06-24 20:18:35 -07009828 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009829 __tg3_set_rx_mode(dev);
David S. Millerf47c11e2005-06-24 20:18:35 -07009830 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009831}
9832
Linus Torvalds1da177e2005-04-16 15:20:36 -07009833static int tg3_get_regs_len(struct net_device *dev)
9834{
Matt Carlson97bd8e42011-04-13 11:05:04 +00009835 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009836}
9837
9838static void tg3_get_regs(struct net_device *dev,
9839 struct ethtool_regs *regs, void *_p)
9840{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009841 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009842
9843 regs->version = 0;
9844
Matt Carlson97bd8e42011-04-13 11:05:04 +00009845 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009846
Matt Carlson80096062010-08-02 11:26:06 +00009847 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -08009848 return;
9849
David S. Millerf47c11e2005-06-24 20:18:35 -07009850 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009851
Matt Carlson97bd8e42011-04-13 11:05:04 +00009852 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009853
David S. Millerf47c11e2005-06-24 20:18:35 -07009854 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009855}
9856
9857static int tg3_get_eeprom_len(struct net_device *dev)
9858{
9859 struct tg3 *tp = netdev_priv(dev);
9860
9861 return tp->nvram_size;
9862}
9863
Linus Torvalds1da177e2005-04-16 15:20:36 -07009864static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9865{
9866 struct tg3 *tp = netdev_priv(dev);
9867 int ret;
9868 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -08009869 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009870 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009871
Matt Carlsondf259d82009-04-20 06:57:14 +00009872 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM)
9873 return -EINVAL;
9874
Matt Carlson80096062010-08-02 11:26:06 +00009875 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -08009876 return -EAGAIN;
9877
Linus Torvalds1da177e2005-04-16 15:20:36 -07009878 offset = eeprom->offset;
9879 len = eeprom->len;
9880 eeprom->len = 0;
9881
9882 eeprom->magic = TG3_EEPROM_MAGIC;
9883
9884 if (offset & 3) {
9885 /* adjustments to start on required 4 byte boundary */
9886 b_offset = offset & 3;
9887 b_count = 4 - b_offset;
9888 if (b_count > len) {
9889 /* i.e. offset=1 len=2 */
9890 b_count = len;
9891 }
Matt Carlsona9dc5292009-02-25 14:25:30 +00009892 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009893 if (ret)
9894 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +00009895 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009896 len -= b_count;
9897 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00009898 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009899 }
9900
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009901 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009902 pd = &data[eeprom->len];
9903 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +00009904 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009905 if (ret) {
9906 eeprom->len += i;
9907 return ret;
9908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009909 memcpy(pd + i, &val, 4);
9910 }
9911 eeprom->len += i;
9912
9913 if (len & 3) {
9914 /* read last bytes not ending on 4 byte boundary */
9915 pd = &data[eeprom->len];
9916 b_count = len & 3;
9917 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009918 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009919 if (ret)
9920 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009921 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009922 eeprom->len += b_count;
9923 }
9924 return 0;
9925}
9926
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009927static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009928
9929static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9930{
9931 struct tg3 *tp = netdev_priv(dev);
9932 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009933 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009934 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009935 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009936
Matt Carlson80096062010-08-02 11:26:06 +00009937 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -08009938 return -EAGAIN;
9939
Matt Carlsondf259d82009-04-20 06:57:14 +00009940 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
9941 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009942 return -EINVAL;
9943
9944 offset = eeprom->offset;
9945 len = eeprom->len;
9946
9947 if ((b_offset = (offset & 3))) {
9948 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +00009949 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009950 if (ret)
9951 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009952 len += b_offset;
9953 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -07009954 if (len < 4)
9955 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009956 }
9957
9958 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -07009959 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009960 /* adjustments to end on required 4 byte boundary */
9961 odd_len = 1;
9962 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009963 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009964 if (ret)
9965 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009966 }
9967
9968 buf = data;
9969 if (b_offset || odd_len) {
9970 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009971 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009972 return -ENOMEM;
9973 if (b_offset)
9974 memcpy(buf, &start, 4);
9975 if (odd_len)
9976 memcpy(buf+len-4, &end, 4);
9977 memcpy(buf + b_offset, data, eeprom->len);
9978 }
9979
9980 ret = tg3_nvram_write_block(tp, offset, len, buf);
9981
9982 if (buf != data)
9983 kfree(buf);
9984
9985 return ret;
9986}
9987
9988static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9989{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009990 struct tg3 *tp = netdev_priv(dev);
9991
9992 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009993 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009994 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009995 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009996 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
9997 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009998 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009999
Linus Torvalds1da177e2005-04-16 15:20:36 -070010000 cmd->supported = (SUPPORTED_Autoneg);
10001
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010002 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010003 cmd->supported |= (SUPPORTED_1000baseT_Half |
10004 SUPPORTED_1000baseT_Full);
10005
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010006 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010007 cmd->supported |= (SUPPORTED_100baseT_Half |
10008 SUPPORTED_100baseT_Full |
10009 SUPPORTED_10baseT_Half |
10010 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -080010011 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -070010012 cmd->port = PORT_TP;
10013 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010014 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -070010015 cmd->port = PORT_FIBRE;
10016 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010017
Linus Torvalds1da177e2005-04-16 15:20:36 -070010018 cmd->advertising = tp->link_config.advertising;
10019 if (netif_running(dev)) {
10020 cmd->speed = tp->link_config.active_speed;
10021 cmd->duplex = tp->link_config.active_duplex;
Matt Carlson64c22182010-10-14 10:37:44 +000010022 } else {
10023 cmd->speed = SPEED_INVALID;
10024 cmd->duplex = DUPLEX_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010025 }
Matt Carlson882e9792009-09-01 13:21:36 +000010026 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +000010027 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010028 cmd->autoneg = tp->link_config.autoneg;
10029 cmd->maxtxpkt = 0;
10030 cmd->maxrxpkt = 0;
10031 return 0;
10032}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010033
Linus Torvalds1da177e2005-04-16 15:20:36 -070010034static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
10035{
10036 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010037
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010038 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010039 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010040 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010041 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010042 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10043 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010044 }
10045
Matt Carlson7e5856b2009-02-25 14:23:01 +000010046 if (cmd->autoneg != AUTONEG_ENABLE &&
10047 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -070010048 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +000010049
10050 if (cmd->autoneg == AUTONEG_DISABLE &&
10051 cmd->duplex != DUPLEX_FULL &&
10052 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -070010053 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010054
Matt Carlson7e5856b2009-02-25 14:23:01 +000010055 if (cmd->autoneg == AUTONEG_ENABLE) {
10056 u32 mask = ADVERTISED_Autoneg |
10057 ADVERTISED_Pause |
10058 ADVERTISED_Asym_Pause;
10059
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010060 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010061 mask |= ADVERTISED_1000baseT_Half |
10062 ADVERTISED_1000baseT_Full;
10063
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010064 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010065 mask |= ADVERTISED_100baseT_Half |
10066 ADVERTISED_100baseT_Full |
10067 ADVERTISED_10baseT_Half |
10068 ADVERTISED_10baseT_Full |
10069 ADVERTISED_TP;
10070 else
10071 mask |= ADVERTISED_FIBRE;
10072
10073 if (cmd->advertising & ~mask)
10074 return -EINVAL;
10075
10076 mask &= (ADVERTISED_1000baseT_Half |
10077 ADVERTISED_1000baseT_Full |
10078 ADVERTISED_100baseT_Half |
10079 ADVERTISED_100baseT_Full |
10080 ADVERTISED_10baseT_Half |
10081 ADVERTISED_10baseT_Full);
10082
10083 cmd->advertising &= mask;
10084 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010085 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
Matt Carlson7e5856b2009-02-25 14:23:01 +000010086 if (cmd->speed != SPEED_1000)
10087 return -EINVAL;
10088
10089 if (cmd->duplex != DUPLEX_FULL)
10090 return -EINVAL;
10091 } else {
10092 if (cmd->speed != SPEED_100 &&
10093 cmd->speed != SPEED_10)
10094 return -EINVAL;
10095 }
10096 }
10097
David S. Millerf47c11e2005-06-24 20:18:35 -070010098 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010099
10100 tp->link_config.autoneg = cmd->autoneg;
10101 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -070010102 tp->link_config.advertising = (cmd->advertising |
10103 ADVERTISED_Autoneg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010104 tp->link_config.speed = SPEED_INVALID;
10105 tp->link_config.duplex = DUPLEX_INVALID;
10106 } else {
10107 tp->link_config.advertising = 0;
10108 tp->link_config.speed = cmd->speed;
10109 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010110 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010111
Michael Chan24fcad62006-12-17 17:06:46 -080010112 tp->link_config.orig_speed = tp->link_config.speed;
10113 tp->link_config.orig_duplex = tp->link_config.duplex;
10114 tp->link_config.orig_autoneg = tp->link_config.autoneg;
10115
Linus Torvalds1da177e2005-04-16 15:20:36 -070010116 if (netif_running(dev))
10117 tg3_setup_phy(tp, 1);
10118
David S. Millerf47c11e2005-06-24 20:18:35 -070010119 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010120
Linus Torvalds1da177e2005-04-16 15:20:36 -070010121 return 0;
10122}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010123
Linus Torvalds1da177e2005-04-16 15:20:36 -070010124static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
10125{
10126 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010127
Linus Torvalds1da177e2005-04-16 15:20:36 -070010128 strcpy(info->driver, DRV_MODULE_NAME);
10129 strcpy(info->version, DRV_MODULE_VERSION);
Michael Chanc4e65752006-03-20 22:29:32 -080010130 strcpy(info->fw_version, tp->fw_ver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010131 strcpy(info->bus_info, pci_name(tp->pdev));
10132}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010133
Linus Torvalds1da177e2005-04-16 15:20:36 -070010134static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10135{
10136 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010137
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070010138 if ((tp->tg3_flags & TG3_FLAG_WOL_CAP) &&
10139 device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070010140 wol->supported = WAKE_MAGIC;
10141 else
10142 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010143 wol->wolopts = 0;
Matt Carlson05ac4cb2008-11-03 16:53:46 -080010144 if ((tp->tg3_flags & TG3_FLAG_WOL_ENABLE) &&
10145 device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010146 wol->wolopts = WAKE_MAGIC;
10147 memset(&wol->sopass, 0, sizeof(wol->sopass));
10148}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010149
Linus Torvalds1da177e2005-04-16 15:20:36 -070010150static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10151{
10152 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070010153 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010154
Linus Torvalds1da177e2005-04-16 15:20:36 -070010155 if (wol->wolopts & ~WAKE_MAGIC)
10156 return -EINVAL;
10157 if ((wol->wolopts & WAKE_MAGIC) &&
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070010158 !((tp->tg3_flags & TG3_FLAG_WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010159 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010160
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010161 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
10162
David S. Millerf47c11e2005-06-24 20:18:35 -070010163 spin_lock_bh(&tp->lock);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010164 if (device_may_wakeup(dp))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010165 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010166 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070010167 tp->tg3_flags &= ~TG3_FLAG_WOL_ENABLE;
David S. Millerf47c11e2005-06-24 20:18:35 -070010168 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010169
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010170
Linus Torvalds1da177e2005-04-16 15:20:36 -070010171 return 0;
10172}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010173
Linus Torvalds1da177e2005-04-16 15:20:36 -070010174static u32 tg3_get_msglevel(struct net_device *dev)
10175{
10176 struct tg3 *tp = netdev_priv(dev);
10177 return tp->msg_enable;
10178}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010179
Linus Torvalds1da177e2005-04-16 15:20:36 -070010180static void tg3_set_msglevel(struct net_device *dev, u32 value)
10181{
10182 struct tg3 *tp = netdev_priv(dev);
10183 tp->msg_enable = value;
10184}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010185
Linus Torvalds1da177e2005-04-16 15:20:36 -070010186static int tg3_nway_reset(struct net_device *dev)
10187{
10188 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010189 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010190
Linus Torvalds1da177e2005-04-16 15:20:36 -070010191 if (!netif_running(dev))
10192 return -EAGAIN;
10193
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010194 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070010195 return -EINVAL;
10196
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010197 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010198 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010199 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010200 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010201 } else {
10202 u32 bmcr;
10203
10204 spin_lock_bh(&tp->lock);
10205 r = -EINVAL;
10206 tg3_readphy(tp, MII_BMCR, &bmcr);
10207 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
10208 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010209 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010210 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
10211 BMCR_ANENABLE);
10212 r = 0;
10213 }
10214 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010215 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010216
Linus Torvalds1da177e2005-04-16 15:20:36 -070010217 return r;
10218}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010219
Linus Torvalds1da177e2005-04-16 15:20:36 -070010220static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10221{
10222 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010223
Matt Carlson2c49a442010-09-30 10:34:35 +000010224 ering->rx_max_pending = tp->rx_std_ring_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010225 ering->rx_mini_max_pending = 0;
Michael Chan4f81c322006-03-20 21:33:42 -080010226 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE)
Matt Carlson2c49a442010-09-30 10:34:35 +000010227 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080010228 else
10229 ering->rx_jumbo_max_pending = 0;
10230
10231 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010232
10233 ering->rx_pending = tp->rx_pending;
10234 ering->rx_mini_pending = 0;
Michael Chan4f81c322006-03-20 21:33:42 -080010235 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE)
10236 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
10237 else
10238 ering->rx_jumbo_pending = 0;
10239
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010240 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010241}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010242
Linus Torvalds1da177e2005-04-16 15:20:36 -070010243static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10244{
10245 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000010246 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010247
Matt Carlson2c49a442010-09-30 10:34:35 +000010248 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
10249 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070010250 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
10251 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Michael Chan7f62ad52007-02-20 23:25:40 -080010252 ((tp->tg3_flags2 & TG3_FLG2_TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070010253 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010254 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010255
Michael Chanbbe832c2005-06-24 20:20:04 -070010256 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010257 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010258 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010259 irq_sync = 1;
10260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010261
Michael Chanbbe832c2005-06-24 20:20:04 -070010262 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010263
Linus Torvalds1da177e2005-04-16 15:20:36 -070010264 tp->rx_pending = ering->rx_pending;
10265
10266 if ((tp->tg3_flags2 & TG3_FLG2_MAX_RXPEND_64) &&
10267 tp->rx_pending > 63)
10268 tp->rx_pending = 63;
10269 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000010270
Matt Carlson6fd45cb2010-09-15 08:59:57 +000010271 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000010272 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010273
10274 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070010275 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -070010276 err = tg3_restart_hw(tp, 1);
10277 if (!err)
10278 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010279 }
10280
David S. Millerf47c11e2005-06-24 20:18:35 -070010281 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010282
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010283 if (irq_sync && !err)
10284 tg3_phy_start(tp);
10285
Michael Chanb9ec6c12006-07-25 16:37:27 -070010286 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010287}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010288
Linus Torvalds1da177e2005-04-16 15:20:36 -070010289static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10290{
10291 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010292
Linus Torvalds1da177e2005-04-16 15:20:36 -070010293 epause->autoneg = (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG) != 0;
Matt Carlson8d018622007-12-20 20:05:44 -080010294
Steve Glendinninge18ce342008-12-16 02:00:00 -080010295 if (tp->link_config.active_flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080010296 epause->rx_pause = 1;
10297 else
10298 epause->rx_pause = 0;
10299
Steve Glendinninge18ce342008-12-16 02:00:00 -080010300 if (tp->link_config.active_flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080010301 epause->tx_pause = 1;
10302 else
10303 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010304}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010305
Linus Torvalds1da177e2005-04-16 15:20:36 -070010306static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10307{
10308 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010309 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010310
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010311 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson27121682010-02-17 15:16:57 +000010312 u32 newadv;
10313 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010314
Matt Carlson27121682010-02-17 15:16:57 +000010315 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010316
Matt Carlson27121682010-02-17 15:16:57 +000010317 if (!(phydev->supported & SUPPORTED_Pause) ||
10318 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000010319 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000010320 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010321
Matt Carlson27121682010-02-17 15:16:57 +000010322 tp->link_config.flowctrl = 0;
10323 if (epause->rx_pause) {
10324 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010325
Matt Carlson27121682010-02-17 15:16:57 +000010326 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080010327 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000010328 newadv = ADVERTISED_Pause;
10329 } else
10330 newadv = ADVERTISED_Pause |
10331 ADVERTISED_Asym_Pause;
10332 } else if (epause->tx_pause) {
10333 tp->link_config.flowctrl |= FLOW_CTRL_TX;
10334 newadv = ADVERTISED_Asym_Pause;
10335 } else
10336 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010337
Matt Carlson27121682010-02-17 15:16:57 +000010338 if (epause->autoneg)
10339 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
10340 else
10341 tp->tg3_flags &= ~TG3_FLAG_PAUSE_AUTONEG;
10342
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010343 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000010344 u32 oldadv = phydev->advertising &
10345 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
10346 if (oldadv != newadv) {
10347 phydev->advertising &=
10348 ~(ADVERTISED_Pause |
10349 ADVERTISED_Asym_Pause);
10350 phydev->advertising |= newadv;
10351 if (phydev->autoneg) {
10352 /*
10353 * Always renegotiate the link to
10354 * inform our link partner of our
10355 * flow control settings, even if the
10356 * flow control is forced. Let
10357 * tg3_adjust_link() do the final
10358 * flow control setup.
10359 */
10360 return phy_start_aneg(phydev);
10361 }
10362 }
10363
10364 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010365 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000010366 } else {
10367 tp->link_config.orig_advertising &=
10368 ~(ADVERTISED_Pause |
10369 ADVERTISED_Asym_Pause);
10370 tp->link_config.orig_advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010371 }
10372 } else {
10373 int irq_sync = 0;
10374
10375 if (netif_running(dev)) {
10376 tg3_netif_stop(tp);
10377 irq_sync = 1;
10378 }
10379
10380 tg3_full_lock(tp, irq_sync);
10381
10382 if (epause->autoneg)
10383 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
10384 else
10385 tp->tg3_flags &= ~TG3_FLAG_PAUSE_AUTONEG;
10386 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010387 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010388 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010389 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010390 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010391 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010392 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010393 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010394
10395 if (netif_running(dev)) {
10396 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10397 err = tg3_restart_hw(tp, 1);
10398 if (!err)
10399 tg3_netif_start(tp);
10400 }
10401
10402 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010404
Michael Chanb9ec6c12006-07-25 16:37:27 -070010405 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010406}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010407
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010408static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010409{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010410 switch (sset) {
10411 case ETH_SS_TEST:
10412 return TG3_NUM_TEST;
10413 case ETH_SS_STATS:
10414 return TG3_NUM_STATS;
10415 default:
10416 return -EOPNOTSUPP;
10417 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070010418}
10419
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010420static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010421{
10422 switch (stringset) {
10423 case ETH_SS_STATS:
10424 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
10425 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070010426 case ETH_SS_TEST:
10427 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
10428 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010429 default:
10430 WARN_ON(1); /* we need a WARN() */
10431 break;
10432 }
10433}
10434
stephen hemminger81b87092011-04-04 08:43:50 +000010435static int tg3_set_phys_id(struct net_device *dev,
10436 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070010437{
10438 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070010439
10440 if (!netif_running(tp->dev))
10441 return -EAGAIN;
10442
stephen hemminger81b87092011-04-04 08:43:50 +000010443 switch (state) {
10444 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +000010445 return 1; /* cycle on/off once per second */
Michael Chan4009a932005-09-05 17:52:54 -070010446
stephen hemminger81b87092011-04-04 08:43:50 +000010447 case ETHTOOL_ID_ON:
10448 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10449 LED_CTRL_1000MBPS_ON |
10450 LED_CTRL_100MBPS_ON |
10451 LED_CTRL_10MBPS_ON |
10452 LED_CTRL_TRAFFIC_OVERRIDE |
10453 LED_CTRL_TRAFFIC_BLINK |
10454 LED_CTRL_TRAFFIC_LED);
10455 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010456
stephen hemminger81b87092011-04-04 08:43:50 +000010457 case ETHTOOL_ID_OFF:
10458 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10459 LED_CTRL_TRAFFIC_OVERRIDE);
10460 break;
Michael Chan4009a932005-09-05 17:52:54 -070010461
stephen hemminger81b87092011-04-04 08:43:50 +000010462 case ETHTOOL_ID_INACTIVE:
10463 tw32(MAC_LED_CTRL, tp->led_ctrl);
10464 break;
Michael Chan4009a932005-09-05 17:52:54 -070010465 }
stephen hemminger81b87092011-04-04 08:43:50 +000010466
Michael Chan4009a932005-09-05 17:52:54 -070010467 return 0;
10468}
10469
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010470static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010471 struct ethtool_stats *estats, u64 *tmp_stats)
10472{
10473 struct tg3 *tp = netdev_priv(dev);
10474 memcpy(tmp_stats, tg3_get_estats(tp), sizeof(tp->estats));
10475}
10476
Matt Carlsonc3e94502011-04-13 11:05:08 +000010477static __be32 * tg3_vpd_readblock(struct tg3 *tp)
10478{
10479 int i;
10480 __be32 *buf;
10481 u32 offset = 0, len = 0;
10482 u32 magic, val;
10483
10484 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
10485 tg3_nvram_read(tp, 0, &magic))
10486 return NULL;
10487
10488 if (magic == TG3_EEPROM_MAGIC) {
10489 for (offset = TG3_NVM_DIR_START;
10490 offset < TG3_NVM_DIR_END;
10491 offset += TG3_NVM_DIRENT_SIZE) {
10492 if (tg3_nvram_read(tp, offset, &val))
10493 return NULL;
10494
10495 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
10496 TG3_NVM_DIRTYPE_EXTVPD)
10497 break;
10498 }
10499
10500 if (offset != TG3_NVM_DIR_END) {
10501 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
10502 if (tg3_nvram_read(tp, offset + 4, &offset))
10503 return NULL;
10504
10505 offset = tg3_nvram_logical_addr(tp, offset);
10506 }
10507 }
10508
10509 if (!offset || !len) {
10510 offset = TG3_NVM_VPD_OFF;
10511 len = TG3_NVM_VPD_LEN;
10512 }
10513
10514 buf = kmalloc(len, GFP_KERNEL);
10515 if (buf == NULL)
10516 return NULL;
10517
10518 if (magic == TG3_EEPROM_MAGIC) {
10519 for (i = 0; i < len; i += 4) {
10520 /* The data is in little-endian format in NVRAM.
10521 * Use the big-endian read routines to preserve
10522 * the byte order as it exists in NVRAM.
10523 */
10524 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
10525 goto error;
10526 }
10527 } else {
10528 u8 *ptr;
10529 ssize_t cnt;
10530 unsigned int pos = 0;
10531
10532 ptr = (u8 *)&buf[0];
10533 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
10534 cnt = pci_read_vpd(tp->pdev, pos,
10535 len - pos, ptr);
10536 if (cnt == -ETIMEDOUT || cnt == -EINTR)
10537 cnt = 0;
10538 else if (cnt < 0)
10539 goto error;
10540 }
10541 if (pos != len)
10542 goto error;
10543 }
10544
10545 return buf;
10546
10547error:
10548 kfree(buf);
10549 return NULL;
10550}
10551
Michael Chan566f86a2005-05-29 14:56:58 -070010552#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080010553#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
10554#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
10555#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Michael Chanb16250e2006-09-27 16:10:14 -070010556#define NVRAM_SELFBOOT_HW_SIZE 0x20
10557#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070010558
10559static int tg3_test_nvram(struct tg3 *tp)
10560{
Al Virob9fc7dc2007-12-17 22:59:57 -080010561 u32 csum, magic;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010562 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010563 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070010564
Matt Carlsondf259d82009-04-20 06:57:14 +000010565 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM)
10566 return 0;
10567
Matt Carlsone4f34112009-02-25 14:25:00 +000010568 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080010569 return -EIO;
10570
Michael Chan1b277772006-03-20 22:27:48 -080010571 if (magic == TG3_EEPROM_MAGIC)
10572 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070010573 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080010574 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
10575 TG3_EEPROM_SB_FORMAT_1) {
10576 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
10577 case TG3_EEPROM_SB_REVISION_0:
10578 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
10579 break;
10580 case TG3_EEPROM_SB_REVISION_2:
10581 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
10582 break;
10583 case TG3_EEPROM_SB_REVISION_3:
10584 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
10585 break;
10586 default:
10587 return 0;
10588 }
10589 } else
Michael Chan1b277772006-03-20 22:27:48 -080010590 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070010591 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
10592 size = NVRAM_SELFBOOT_HW_SIZE;
10593 else
Michael Chan1b277772006-03-20 22:27:48 -080010594 return -EIO;
10595
10596 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070010597 if (buf == NULL)
10598 return -ENOMEM;
10599
Michael Chan1b277772006-03-20 22:27:48 -080010600 err = -EIO;
10601 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010602 err = tg3_nvram_read_be32(tp, i, &buf[j]);
10603 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070010604 break;
Michael Chan566f86a2005-05-29 14:56:58 -070010605 }
Michael Chan1b277772006-03-20 22:27:48 -080010606 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070010607 goto out;
10608
Michael Chan1b277772006-03-20 22:27:48 -080010609 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010610 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080010611 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010612 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080010613 u8 *buf8 = (u8 *) buf, csum8 = 0;
10614
Al Virob9fc7dc2007-12-17 22:59:57 -080010615 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080010616 TG3_EEPROM_SB_REVISION_2) {
10617 /* For rev 2, the csum doesn't include the MBA. */
10618 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
10619 csum8 += buf8[i];
10620 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
10621 csum8 += buf8[i];
10622 } else {
10623 for (i = 0; i < size; i++)
10624 csum8 += buf8[i];
10625 }
Michael Chan1b277772006-03-20 22:27:48 -080010626
Adrian Bunkad96b482006-04-05 22:21:04 -070010627 if (csum8 == 0) {
10628 err = 0;
10629 goto out;
10630 }
10631
10632 err = -EIO;
10633 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080010634 }
Michael Chan566f86a2005-05-29 14:56:58 -070010635
Al Virob9fc7dc2007-12-17 22:59:57 -080010636 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010637 TG3_EEPROM_MAGIC_HW) {
10638 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000010639 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070010640 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070010641
10642 /* Separate the parity bits and the data bytes. */
10643 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
10644 if ((i == 0) || (i == 8)) {
10645 int l;
10646 u8 msk;
10647
10648 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
10649 parity[k++] = buf8[i] & msk;
10650 i++;
Matt Carlson859a5882010-04-05 10:19:28 +000010651 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070010652 int l;
10653 u8 msk;
10654
10655 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
10656 parity[k++] = buf8[i] & msk;
10657 i++;
10658
10659 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
10660 parity[k++] = buf8[i] & msk;
10661 i++;
10662 }
10663 data[j++] = buf8[i];
10664 }
10665
10666 err = -EIO;
10667 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
10668 u8 hw8 = hweight8(data[i]);
10669
10670 if ((hw8 & 0x1) && parity[i])
10671 goto out;
10672 else if (!(hw8 & 0x1) && !parity[i])
10673 goto out;
10674 }
10675 err = 0;
10676 goto out;
10677 }
10678
Matt Carlson01c3a392011-03-09 16:58:20 +000010679 err = -EIO;
10680
Michael Chan566f86a2005-05-29 14:56:58 -070010681 /* Bootstrap checksum at offset 0x10 */
10682 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000010683 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070010684 goto out;
10685
10686 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
10687 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000010688 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000010689 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070010690
Matt Carlsonc3e94502011-04-13 11:05:08 +000010691 kfree(buf);
10692
10693 buf = tg3_vpd_readblock(tp);
10694 if (!buf)
10695 return -ENOMEM;
Matt Carlsond4894f32011-03-09 16:58:21 +000010696
10697 i = pci_vpd_find_tag((u8 *)buf, 0, TG3_NVM_VPD_LEN,
10698 PCI_VPD_LRDT_RO_DATA);
10699 if (i > 0) {
10700 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
10701 if (j < 0)
10702 goto out;
10703
10704 if (i + PCI_VPD_LRDT_TAG_SIZE + j > TG3_NVM_VPD_LEN)
10705 goto out;
10706
10707 i += PCI_VPD_LRDT_TAG_SIZE;
10708 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
10709 PCI_VPD_RO_KEYWORD_CHKSUM);
10710 if (j > 0) {
10711 u8 csum8 = 0;
10712
10713 j += PCI_VPD_INFO_FLD_HDR_SIZE;
10714
10715 for (i = 0; i <= j; i++)
10716 csum8 += ((u8 *)buf)[i];
10717
10718 if (csum8)
10719 goto out;
10720 }
10721 }
10722
Michael Chan566f86a2005-05-29 14:56:58 -070010723 err = 0;
10724
10725out:
10726 kfree(buf);
10727 return err;
10728}
10729
Michael Chanca430072005-05-29 14:57:23 -070010730#define TG3_SERDES_TIMEOUT_SEC 2
10731#define TG3_COPPER_TIMEOUT_SEC 6
10732
10733static int tg3_test_link(struct tg3 *tp)
10734{
10735 int i, max;
10736
10737 if (!netif_running(tp->dev))
10738 return -ENODEV;
10739
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010740 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070010741 max = TG3_SERDES_TIMEOUT_SEC;
10742 else
10743 max = TG3_COPPER_TIMEOUT_SEC;
10744
10745 for (i = 0; i < max; i++) {
10746 if (netif_carrier_ok(tp->dev))
10747 return 0;
10748
10749 if (msleep_interruptible(1000))
10750 break;
10751 }
10752
10753 return -EIO;
10754}
10755
Michael Chana71116d2005-05-29 14:58:11 -070010756/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080010757static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070010758{
Michael Chanb16250e2006-09-27 16:10:14 -070010759 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070010760 u32 offset, read_mask, write_mask, val, save_val, read_val;
10761 static struct {
10762 u16 offset;
10763 u16 flags;
10764#define TG3_FL_5705 0x1
10765#define TG3_FL_NOT_5705 0x2
10766#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070010767#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070010768 u32 read_mask;
10769 u32 write_mask;
10770 } reg_tbl[] = {
10771 /* MAC Control Registers */
10772 { MAC_MODE, TG3_FL_NOT_5705,
10773 0x00000000, 0x00ef6f8c },
10774 { MAC_MODE, TG3_FL_5705,
10775 0x00000000, 0x01ef6b8c },
10776 { MAC_STATUS, TG3_FL_NOT_5705,
10777 0x03800107, 0x00000000 },
10778 { MAC_STATUS, TG3_FL_5705,
10779 0x03800100, 0x00000000 },
10780 { MAC_ADDR_0_HIGH, 0x0000,
10781 0x00000000, 0x0000ffff },
10782 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010783 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070010784 { MAC_RX_MTU_SIZE, 0x0000,
10785 0x00000000, 0x0000ffff },
10786 { MAC_TX_MODE, 0x0000,
10787 0x00000000, 0x00000070 },
10788 { MAC_TX_LENGTHS, 0x0000,
10789 0x00000000, 0x00003fff },
10790 { MAC_RX_MODE, TG3_FL_NOT_5705,
10791 0x00000000, 0x000007fc },
10792 { MAC_RX_MODE, TG3_FL_5705,
10793 0x00000000, 0x000007dc },
10794 { MAC_HASH_REG_0, 0x0000,
10795 0x00000000, 0xffffffff },
10796 { MAC_HASH_REG_1, 0x0000,
10797 0x00000000, 0xffffffff },
10798 { MAC_HASH_REG_2, 0x0000,
10799 0x00000000, 0xffffffff },
10800 { MAC_HASH_REG_3, 0x0000,
10801 0x00000000, 0xffffffff },
10802
10803 /* Receive Data and Receive BD Initiator Control Registers. */
10804 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
10805 0x00000000, 0xffffffff },
10806 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
10807 0x00000000, 0xffffffff },
10808 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
10809 0x00000000, 0x00000003 },
10810 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
10811 0x00000000, 0xffffffff },
10812 { RCVDBDI_STD_BD+0, 0x0000,
10813 0x00000000, 0xffffffff },
10814 { RCVDBDI_STD_BD+4, 0x0000,
10815 0x00000000, 0xffffffff },
10816 { RCVDBDI_STD_BD+8, 0x0000,
10817 0x00000000, 0xffff0002 },
10818 { RCVDBDI_STD_BD+0xc, 0x0000,
10819 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010820
Michael Chana71116d2005-05-29 14:58:11 -070010821 /* Receive BD Initiator Control Registers. */
10822 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
10823 0x00000000, 0xffffffff },
10824 { RCVBDI_STD_THRESH, TG3_FL_5705,
10825 0x00000000, 0x000003ff },
10826 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
10827 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010828
Michael Chana71116d2005-05-29 14:58:11 -070010829 /* Host Coalescing Control Registers. */
10830 { HOSTCC_MODE, TG3_FL_NOT_5705,
10831 0x00000000, 0x00000004 },
10832 { HOSTCC_MODE, TG3_FL_5705,
10833 0x00000000, 0x000000f6 },
10834 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
10835 0x00000000, 0xffffffff },
10836 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
10837 0x00000000, 0x000003ff },
10838 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
10839 0x00000000, 0xffffffff },
10840 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
10841 0x00000000, 0x000003ff },
10842 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
10843 0x00000000, 0xffffffff },
10844 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10845 0x00000000, 0x000000ff },
10846 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
10847 0x00000000, 0xffffffff },
10848 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10849 0x00000000, 0x000000ff },
10850 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
10851 0x00000000, 0xffffffff },
10852 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
10853 0x00000000, 0xffffffff },
10854 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10855 0x00000000, 0xffffffff },
10856 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10857 0x00000000, 0x000000ff },
10858 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10859 0x00000000, 0xffffffff },
10860 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10861 0x00000000, 0x000000ff },
10862 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
10863 0x00000000, 0xffffffff },
10864 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
10865 0x00000000, 0xffffffff },
10866 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
10867 0x00000000, 0xffffffff },
10868 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
10869 0x00000000, 0xffffffff },
10870 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
10871 0x00000000, 0xffffffff },
10872 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
10873 0xffffffff, 0x00000000 },
10874 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
10875 0xffffffff, 0x00000000 },
10876
10877 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070010878 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010879 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070010880 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010881 0x00000000, 0x007fffff },
10882 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
10883 0x00000000, 0x0000003f },
10884 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
10885 0x00000000, 0x000001ff },
10886 { BUFMGR_MB_HIGH_WATER, 0x0000,
10887 0x00000000, 0x000001ff },
10888 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
10889 0xffffffff, 0x00000000 },
10890 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
10891 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010892
Michael Chana71116d2005-05-29 14:58:11 -070010893 /* Mailbox Registers */
10894 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
10895 0x00000000, 0x000001ff },
10896 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
10897 0x00000000, 0x000001ff },
10898 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
10899 0x00000000, 0x000007ff },
10900 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
10901 0x00000000, 0x000001ff },
10902
10903 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
10904 };
10905
Michael Chanb16250e2006-09-27 16:10:14 -070010906 is_5705 = is_5750 = 0;
10907 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
Michael Chana71116d2005-05-29 14:58:11 -070010908 is_5705 = 1;
Michael Chanb16250e2006-09-27 16:10:14 -070010909 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
10910 is_5750 = 1;
10911 }
Michael Chana71116d2005-05-29 14:58:11 -070010912
10913 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
10914 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
10915 continue;
10916
10917 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
10918 continue;
10919
10920 if ((tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
10921 (reg_tbl[i].flags & TG3_FL_NOT_5788))
10922 continue;
10923
Michael Chanb16250e2006-09-27 16:10:14 -070010924 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
10925 continue;
10926
Michael Chana71116d2005-05-29 14:58:11 -070010927 offset = (u32) reg_tbl[i].offset;
10928 read_mask = reg_tbl[i].read_mask;
10929 write_mask = reg_tbl[i].write_mask;
10930
10931 /* Save the original register content */
10932 save_val = tr32(offset);
10933
10934 /* Determine the read-only value. */
10935 read_val = save_val & read_mask;
10936
10937 /* Write zero to the register, then make sure the read-only bits
10938 * are not changed and the read/write bits are all zeros.
10939 */
10940 tw32(offset, 0);
10941
10942 val = tr32(offset);
10943
10944 /* Test the read-only and read/write bits. */
10945 if (((val & read_mask) != read_val) || (val & write_mask))
10946 goto out;
10947
10948 /* Write ones to all the bits defined by RdMask and WrMask, then
10949 * make sure the read-only bits are not changed and the
10950 * read/write bits are all ones.
10951 */
10952 tw32(offset, read_mask | write_mask);
10953
10954 val = tr32(offset);
10955
10956 /* Test the read-only bits. */
10957 if ((val & read_mask) != read_val)
10958 goto out;
10959
10960 /* Test the read/write bits. */
10961 if ((val & write_mask) != write_mask)
10962 goto out;
10963
10964 tw32(offset, save_val);
10965 }
10966
10967 return 0;
10968
10969out:
Michael Chan9f88f292006-12-07 00:22:54 -080010970 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000010971 netdev_err(tp->dev,
10972 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070010973 tw32(offset, save_val);
10974 return -EIO;
10975}
10976
Michael Chan7942e1d2005-05-29 14:58:36 -070010977static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
10978{
Arjan van de Venf71e1302006-03-03 21:33:57 -050010979 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070010980 int i;
10981 u32 j;
10982
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020010983 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070010984 for (j = 0; j < len; j += 4) {
10985 u32 val;
10986
10987 tg3_write_mem(tp, offset + j, test_pattern[i]);
10988 tg3_read_mem(tp, offset + j, &val);
10989 if (val != test_pattern[i])
10990 return -EIO;
10991 }
10992 }
10993 return 0;
10994}
10995
10996static int tg3_test_memory(struct tg3 *tp)
10997{
10998 static struct mem_entry {
10999 u32 offset;
11000 u32 len;
11001 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080011002 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070011003 { 0x00002000, 0x1c000},
11004 { 0xffffffff, 0x00000}
11005 }, mem_tbl_5705[] = {
11006 { 0x00000100, 0x0000c},
11007 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070011008 { 0x00004000, 0x00800},
11009 { 0x00006000, 0x01000},
11010 { 0x00008000, 0x02000},
11011 { 0x00010000, 0x0e000},
11012 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080011013 }, mem_tbl_5755[] = {
11014 { 0x00000200, 0x00008},
11015 { 0x00004000, 0x00800},
11016 { 0x00006000, 0x00800},
11017 { 0x00008000, 0x02000},
11018 { 0x00010000, 0x0c000},
11019 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070011020 }, mem_tbl_5906[] = {
11021 { 0x00000200, 0x00008},
11022 { 0x00004000, 0x00400},
11023 { 0x00006000, 0x00400},
11024 { 0x00008000, 0x01000},
11025 { 0x00010000, 0x01000},
11026 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011027 }, mem_tbl_5717[] = {
11028 { 0x00000200, 0x00008},
11029 { 0x00010000, 0x0a000},
11030 { 0x00020000, 0x13c00},
11031 { 0xffffffff, 0x00000}
11032 }, mem_tbl_57765[] = {
11033 { 0x00000200, 0x00008},
11034 { 0x00004000, 0x00800},
11035 { 0x00006000, 0x09800},
11036 { 0x00010000, 0x0a000},
11037 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070011038 };
11039 struct mem_entry *mem_tbl;
11040 int err = 0;
11041 int i;
11042
Matt Carlson0a58d662011-04-05 14:22:45 +000011043 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011044 mem_tbl = mem_tbl_5717;
11045 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
11046 mem_tbl = mem_tbl_57765;
11047 else if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Matt Carlson321d32a2008-11-21 17:22:19 -080011048 mem_tbl = mem_tbl_5755;
11049 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
11050 mem_tbl = mem_tbl_5906;
11051 else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
11052 mem_tbl = mem_tbl_5705;
11053 else
Michael Chan7942e1d2005-05-29 14:58:36 -070011054 mem_tbl = mem_tbl_570x;
11055
11056 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000011057 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
11058 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070011059 break;
11060 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011061
Michael Chan7942e1d2005-05-29 14:58:36 -070011062 return err;
11063}
11064
Michael Chan9f40dea2005-09-05 17:53:06 -070011065#define TG3_MAC_LOOPBACK 0
11066#define TG3_PHY_LOOPBACK 1
11067
Matt Carlson4852a862011-04-13 11:05:07 +000011068static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, int loopback_mode)
Michael Chanc76949a2005-05-29 14:58:59 -070011069{
Michael Chan9f40dea2005-09-05 17:53:06 -070011070 u32 mac_mode, rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011071 u32 desc_idx, coal_now;
Michael Chanc76949a2005-05-29 14:58:59 -070011072 struct sk_buff *skb, *rx_skb;
11073 u8 *tx_data;
11074 dma_addr_t map;
11075 int num_pkts, tx_len, rx_len, i, err;
11076 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000011077 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000011078 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070011079
Matt Carlsonc8873402010-02-12 14:47:11 +000011080 tnapi = &tp->napi[0];
11081 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011082 if (tp->irq_cnt > 1) {
Matt Carlson1da85aa2010-09-30 10:34:34 +000011083 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)
11084 rnapi = &tp->napi[1];
Matt Carlsonc8873402010-02-12 14:47:11 +000011085 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
11086 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011087 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011088 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000011089
Michael Chan9f40dea2005-09-05 17:53:06 -070011090 if (loopback_mode == TG3_MAC_LOOPBACK) {
Michael Chanc94e3942005-09-27 12:12:42 -070011091 /* HW errata - mac loopback fails in some cases on 5780.
11092 * Normal traffic and PHY loopback are not affected by
Matt Carlsonaba49f22011-01-25 15:58:53 +000011093 * errata. Also, the MAC loopback test is deprecated for
11094 * all newer ASIC revisions.
Michael Chanc94e3942005-09-27 12:12:42 -070011095 */
Matt Carlsonaba49f22011-01-25 15:58:53 +000011096 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
11097 (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT))
Michael Chanc94e3942005-09-27 12:12:42 -070011098 return 0;
11099
Matt Carlson49692ca2011-01-25 15:58:52 +000011100 mac_mode = tp->mac_mode &
11101 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
11102 mac_mode |= MAC_MODE_PORT_INT_LPBACK;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070011103 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
11104 mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011105 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
Michael Chan3f7045c2006-09-27 16:02:29 -070011106 mac_mode |= MAC_MODE_PORT_MODE_MII;
11107 else
11108 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chan9f40dea2005-09-05 17:53:06 -070011109 tw32(MAC_MODE, mac_mode);
11110 } else if (loopback_mode == TG3_PHY_LOOPBACK) {
Michael Chan3f7045c2006-09-27 16:02:29 -070011111 u32 val;
11112
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011113 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +000011114 tg3_phy_fet_toggle_apd(tp, false);
Michael Chan5d64ad32006-12-07 00:19:40 -080011115 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED100;
11116 } else
11117 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED1000;
Michael Chan3f7045c2006-09-27 16:02:29 -070011118
Matt Carlson9ef8ca92007-07-11 19:48:29 -070011119 tg3_phy_toggle_automdix(tp, 0);
11120
Michael Chan3f7045c2006-09-27 16:02:29 -070011121 tg3_writephy(tp, MII_BMCR, val);
Michael Chanc94e3942005-09-27 12:12:42 -070011122 udelay(40);
Michael Chan5d64ad32006-12-07 00:19:40 -080011123
Matt Carlson49692ca2011-01-25 15:58:52 +000011124 mac_mode = tp->mac_mode &
11125 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011126 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson1061b7c2010-02-12 14:47:12 +000011127 tg3_writephy(tp, MII_TG3_FET_PTEST,
11128 MII_TG3_FET_PTEST_FRC_TX_LINK |
11129 MII_TG3_FET_PTEST_FRC_TX_LOCK);
11130 /* The write needs to be flushed for the AC131 */
11131 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
11132 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
Michael Chan5d64ad32006-12-07 00:19:40 -080011133 mac_mode |= MAC_MODE_PORT_MODE_MII;
11134 } else
11135 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chanb16250e2006-09-27 16:10:14 -070011136
Michael Chanc94e3942005-09-27 12:12:42 -070011137 /* reset to prevent losing 1st rx packet intermittently */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011138 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Michael Chanc94e3942005-09-27 12:12:42 -070011139 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
11140 udelay(10);
11141 tw32_f(MAC_RX_MODE, tp->rx_mode);
11142 }
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070011143 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlson79eb6902010-02-17 15:17:03 +000011144 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
11145 if (masked_phy_id == TG3_PHY_ID_BCM5401)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070011146 mac_mode &= ~MAC_MODE_LINK_POLARITY;
Matt Carlson79eb6902010-02-17 15:17:03 +000011147 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070011148 mac_mode |= MAC_MODE_LINK_POLARITY;
Michael Chanff18ff02006-03-27 23:17:27 -080011149 tg3_writephy(tp, MII_TG3_EXT_CTRL,
11150 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
11151 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011152 tw32(MAC_MODE, mac_mode);
Matt Carlson49692ca2011-01-25 15:58:52 +000011153
11154 /* Wait for link */
11155 for (i = 0; i < 100; i++) {
11156 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
11157 break;
11158 mdelay(1);
11159 }
Matt Carlson859a5882010-04-05 10:19:28 +000011160 } else {
Michael Chan9f40dea2005-09-05 17:53:06 -070011161 return -EINVAL;
Matt Carlson859a5882010-04-05 10:19:28 +000011162 }
Michael Chanc76949a2005-05-29 14:58:59 -070011163
11164 err = -EIO;
11165
Matt Carlson4852a862011-04-13 11:05:07 +000011166 tx_len = pktsz;
David S. Millera20e9c62006-07-31 22:38:16 -070011167 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070011168 if (!skb)
11169 return -ENOMEM;
11170
Michael Chanc76949a2005-05-29 14:58:59 -070011171 tx_data = skb_put(skb, tx_len);
11172 memcpy(tx_data, tp->dev->dev_addr, 6);
11173 memset(tx_data + 6, 0x0, 8);
11174
Matt Carlson4852a862011-04-13 11:05:07 +000011175 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
Michael Chanc76949a2005-05-29 14:58:59 -070011176
11177 for (i = 14; i < tx_len; i++)
11178 tx_data[i] = (u8) (i & 0xff);
11179
Alexander Duyckf4188d82009-12-02 16:48:38 +000011180 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
11181 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000011182 dev_kfree_skb(skb);
11183 return -EIO;
11184 }
Michael Chanc76949a2005-05-29 14:58:59 -070011185
11186 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011187 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011188
11189 udelay(10);
11190
Matt Carlson898a56f2009-08-28 14:02:40 +000011191 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070011192
Michael Chanc76949a2005-05-29 14:58:59 -070011193 num_pkts = 0;
11194
Alexander Duyckf4188d82009-12-02 16:48:38 +000011195 tg3_set_txd(tnapi, tnapi->tx_prod, map, tx_len, 0, 1);
Michael Chanc76949a2005-05-29 14:58:59 -070011196
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011197 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070011198 num_pkts++;
11199
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011200 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
11201 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070011202
11203 udelay(10);
11204
Matt Carlson303fc922009-11-02 14:27:34 +000011205 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
11206 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070011207 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011208 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011209
11210 udelay(10);
11211
Matt Carlson898a56f2009-08-28 14:02:40 +000011212 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
11213 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011214 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070011215 (rx_idx == (rx_start_idx + num_pkts)))
11216 break;
11217 }
11218
Alexander Duyckf4188d82009-12-02 16:48:38 +000011219 pci_unmap_single(tp->pdev, map, tx_len, PCI_DMA_TODEVICE);
Michael Chanc76949a2005-05-29 14:58:59 -070011220 dev_kfree_skb(skb);
11221
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011222 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070011223 goto out;
11224
11225 if (rx_idx != rx_start_idx + num_pkts)
11226 goto out;
11227
Matt Carlson72334482009-08-28 14:03:01 +000011228 desc = &rnapi->rx_rcb[rx_start_idx];
Michael Chanc76949a2005-05-29 14:58:59 -070011229 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
11230 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
Michael Chanc76949a2005-05-29 14:58:59 -070011231
11232 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
11233 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
11234 goto out;
11235
11236 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) - 4;
11237 if (rx_len != tx_len)
11238 goto out;
11239
Matt Carlson4852a862011-04-13 11:05:07 +000011240 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
11241 if (opaque_key != RXD_OPAQUE_RING_STD)
11242 goto out;
Michael Chanc76949a2005-05-29 14:58:59 -070011243
Matt Carlson4852a862011-04-13 11:05:07 +000011244 rx_skb = tpr->rx_std_buffers[desc_idx].skb;
11245 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx], mapping);
11246 } else {
11247 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
11248 goto out;
11249
11250 rx_skb = tpr->rx_jmb_buffers[desc_idx].skb;
11251 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx], mapping);
11252 }
11253
Michael Chanc76949a2005-05-29 14:58:59 -070011254 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len, PCI_DMA_FROMDEVICE);
11255
11256 for (i = 14; i < tx_len; i++) {
11257 if (*(rx_skb->data + i) != (u8) (i & 0xff))
11258 goto out;
11259 }
11260 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011261
Michael Chanc76949a2005-05-29 14:58:59 -070011262 /* tg3_free_rings will unmap and free the rx_skb */
11263out:
11264 return err;
11265}
11266
Michael Chan9f40dea2005-09-05 17:53:06 -070011267#define TG3_MAC_LOOPBACK_FAILED 1
11268#define TG3_PHY_LOOPBACK_FAILED 2
11269#define TG3_LOOPBACK_FAILED (TG3_MAC_LOOPBACK_FAILED | \
11270 TG3_PHY_LOOPBACK_FAILED)
11271
11272static int tg3_test_loopback(struct tg3 *tp)
11273{
11274 int err = 0;
Matt Carlsonab789042011-01-25 15:58:54 +000011275 u32 eee_cap, cpmuctrl = 0;
Michael Chan9f40dea2005-09-05 17:53:06 -070011276
11277 if (!netif_running(tp->dev))
11278 return TG3_LOOPBACK_FAILED;
11279
Matt Carlsonab789042011-01-25 15:58:54 +000011280 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
11281 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
11282
Michael Chanb9ec6c12006-07-25 16:37:27 -070011283 err = tg3_reset_hw(tp, 1);
Matt Carlsonab789042011-01-25 15:58:54 +000011284 if (err) {
11285 err = TG3_LOOPBACK_FAILED;
11286 goto done;
11287 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011288
Matt Carlson4a85f092011-04-20 07:57:37 +000011289 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS) {
11290 int i;
11291
11292 /* Reroute all rx packets to the 1st queue */
11293 for (i = MAC_RSS_INDIR_TBL_0;
11294 i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
11295 tw32(i, 0x0);
11296 }
11297
Matt Carlson6833c042008-11-21 17:18:59 -080011298 /* Turn off gphy autopowerdown. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011299 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -080011300 tg3_phy_toggle_apd(tp, false);
11301
Matt Carlson321d32a2008-11-21 17:22:19 -080011302 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070011303 int i;
11304 u32 status;
11305
11306 tw32(TG3_CPMU_MUTEX_REQ, CPMU_MUTEX_REQ_DRIVER);
11307
11308 /* Wait for up to 40 microseconds to acquire lock. */
11309 for (i = 0; i < 4; i++) {
11310 status = tr32(TG3_CPMU_MUTEX_GNT);
11311 if (status == CPMU_MUTEX_GNT_DRIVER)
11312 break;
11313 udelay(10);
11314 }
11315
Matt Carlsonab789042011-01-25 15:58:54 +000011316 if (status != CPMU_MUTEX_GNT_DRIVER) {
11317 err = TG3_LOOPBACK_FAILED;
11318 goto done;
11319 }
Matt Carlson9936bcf2007-10-10 18:03:07 -070011320
Matt Carlsonb2a5c192008-04-03 21:44:44 -070011321 /* Turn off link-based power management. */
Matt Carlsone8750932007-11-12 21:11:51 -080011322 cpmuctrl = tr32(TG3_CPMU_CTRL);
Matt Carlson109115e2008-05-02 16:48:59 -070011323 tw32(TG3_CPMU_CTRL,
11324 cpmuctrl & ~(CPMU_CTRL_LINK_SPEED_MODE |
11325 CPMU_CTRL_LINK_AWARE_MODE));
Matt Carlson9936bcf2007-10-10 18:03:07 -070011326 }
11327
Matt Carlson4852a862011-04-13 11:05:07 +000011328 if (tg3_run_loopback(tp, ETH_FRAME_LEN, TG3_MAC_LOOPBACK))
Michael Chan9f40dea2005-09-05 17:53:06 -070011329 err |= TG3_MAC_LOOPBACK_FAILED;
Matt Carlson9936bcf2007-10-10 18:03:07 -070011330
Matt Carlson4852a862011-04-13 11:05:07 +000011331 if ((tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) &&
11332 tg3_run_loopback(tp, 9000 + ETH_HLEN, TG3_MAC_LOOPBACK))
11333 err |= (TG3_MAC_LOOPBACK_FAILED << 2);
11334
Matt Carlson321d32a2008-11-21 17:22:19 -080011335 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070011336 tw32(TG3_CPMU_CTRL, cpmuctrl);
11337
11338 /* Release the mutex */
11339 tw32(TG3_CPMU_MUTEX_GNT, CPMU_MUTEX_GNT_DRIVER);
11340 }
11341
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011342 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Matt Carlsondd477002008-05-25 23:45:58 -070011343 !(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)) {
Matt Carlson4852a862011-04-13 11:05:07 +000011344 if (tg3_run_loopback(tp, ETH_FRAME_LEN, TG3_PHY_LOOPBACK))
Michael Chan9f40dea2005-09-05 17:53:06 -070011345 err |= TG3_PHY_LOOPBACK_FAILED;
Matt Carlson4852a862011-04-13 11:05:07 +000011346 if ((tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) &&
11347 tg3_run_loopback(tp, 9000 + ETH_HLEN, TG3_PHY_LOOPBACK))
11348 err |= (TG3_PHY_LOOPBACK_FAILED << 2);
Michael Chan9f40dea2005-09-05 17:53:06 -070011349 }
11350
Matt Carlson6833c042008-11-21 17:18:59 -080011351 /* Re-enable gphy autopowerdown. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011352 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -080011353 tg3_phy_toggle_apd(tp, true);
11354
Matt Carlsonab789042011-01-25 15:58:54 +000011355done:
11356 tp->phy_flags |= eee_cap;
11357
Michael Chan9f40dea2005-09-05 17:53:06 -070011358 return err;
11359}
11360
Michael Chan4cafd3f2005-05-29 14:56:34 -070011361static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
11362 u64 *data)
11363{
Michael Chan566f86a2005-05-29 14:56:58 -070011364 struct tg3 *tp = netdev_priv(dev);
11365
Matt Carlson80096062010-08-02 11:26:06 +000011366 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000011367 tg3_power_up(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080011368
Michael Chan566f86a2005-05-29 14:56:58 -070011369 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
11370
11371 if (tg3_test_nvram(tp) != 0) {
11372 etest->flags |= ETH_TEST_FL_FAILED;
11373 data[0] = 1;
11374 }
Michael Chanca430072005-05-29 14:57:23 -070011375 if (tg3_test_link(tp) != 0) {
11376 etest->flags |= ETH_TEST_FL_FAILED;
11377 data[1] = 1;
11378 }
Michael Chana71116d2005-05-29 14:58:11 -070011379 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011380 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070011381
Michael Chanbbe832c2005-06-24 20:20:04 -070011382 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011383 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070011384 tg3_netif_stop(tp);
11385 irq_sync = 1;
11386 }
11387
11388 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070011389
11390 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080011391 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011392 tg3_halt_cpu(tp, RX_CPU_BASE);
11393 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
11394 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080011395 if (!err)
11396 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011397
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011398 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad2006-03-20 22:27:35 -080011399 tg3_phy_reset(tp);
11400
Michael Chana71116d2005-05-29 14:58:11 -070011401 if (tg3_test_registers(tp) != 0) {
11402 etest->flags |= ETH_TEST_FL_FAILED;
11403 data[2] = 1;
11404 }
Michael Chan7942e1d2005-05-29 14:58:36 -070011405 if (tg3_test_memory(tp) != 0) {
11406 etest->flags |= ETH_TEST_FL_FAILED;
11407 data[3] = 1;
11408 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011409 if ((data[4] = tg3_test_loopback(tp)) != 0)
Michael Chanc76949a2005-05-29 14:58:59 -070011410 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070011411
David S. Millerf47c11e2005-06-24 20:18:35 -070011412 tg3_full_unlock(tp);
11413
Michael Chand4bc3922005-05-29 14:59:20 -070011414 if (tg3_test_interrupt(tp) != 0) {
11415 etest->flags |= ETH_TEST_FL_FAILED;
11416 data[5] = 1;
11417 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011418
11419 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070011420
Michael Chana71116d2005-05-29 14:58:11 -070011421 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
11422 if (netif_running(dev)) {
11423 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011424 err2 = tg3_restart_hw(tp, 1);
11425 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070011426 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011427 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011428
11429 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011430
11431 if (irq_sync && !err2)
11432 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011433 }
Matt Carlson80096062010-08-02 11:26:06 +000011434 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000011435 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080011436
Michael Chan4cafd3f2005-05-29 14:56:34 -070011437}
11438
Linus Torvalds1da177e2005-04-16 15:20:36 -070011439static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
11440{
11441 struct mii_ioctl_data *data = if_mii(ifr);
11442 struct tg3 *tp = netdev_priv(dev);
11443 int err;
11444
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011445 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011446 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011447 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011448 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011449 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000011450 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011451 }
11452
Matt Carlson33f401a2010-04-05 10:19:27 +000011453 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011454 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000011455 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011456
11457 /* fallthru */
11458 case SIOCGMIIREG: {
11459 u32 mii_regval;
11460
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011461 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011462 break; /* We have no PHY */
11463
Matt Carlson34eea5a2011-04-20 07:57:38 +000011464 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080011465 return -EAGAIN;
11466
David S. Millerf47c11e2005-06-24 20:18:35 -070011467 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011468 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070011469 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011470
11471 data->val_out = mii_regval;
11472
11473 return err;
11474 }
11475
11476 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011477 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011478 break; /* We have no PHY */
11479
Matt Carlson34eea5a2011-04-20 07:57:38 +000011480 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080011481 return -EAGAIN;
11482
David S. Millerf47c11e2005-06-24 20:18:35 -070011483 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011484 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070011485 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011486
11487 return err;
11488
11489 default:
11490 /* do nothing */
11491 break;
11492 }
11493 return -EOPNOTSUPP;
11494}
11495
David S. Miller15f98502005-05-18 22:49:26 -070011496static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11497{
11498 struct tg3 *tp = netdev_priv(dev);
11499
11500 memcpy(ec, &tp->coal, sizeof(*ec));
11501 return 0;
11502}
11503
Michael Chand244c892005-07-05 14:42:33 -070011504static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11505{
11506 struct tg3 *tp = netdev_priv(dev);
11507 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
11508 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
11509
11510 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
11511 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
11512 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
11513 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
11514 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
11515 }
11516
11517 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
11518 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
11519 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
11520 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
11521 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
11522 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
11523 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
11524 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
11525 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
11526 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
11527 return -EINVAL;
11528
11529 /* No rx interrupts will be generated if both are zero */
11530 if ((ec->rx_coalesce_usecs == 0) &&
11531 (ec->rx_max_coalesced_frames == 0))
11532 return -EINVAL;
11533
11534 /* No tx interrupts will be generated if both are zero */
11535 if ((ec->tx_coalesce_usecs == 0) &&
11536 (ec->tx_max_coalesced_frames == 0))
11537 return -EINVAL;
11538
11539 /* Only copy relevant parameters, ignore all others. */
11540 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
11541 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
11542 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
11543 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
11544 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
11545 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
11546 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
11547 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
11548 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
11549
11550 if (netif_running(dev)) {
11551 tg3_full_lock(tp, 0);
11552 __tg3_set_coalesce(tp, &tp->coal);
11553 tg3_full_unlock(tp);
11554 }
11555 return 0;
11556}
11557
Jeff Garzik7282d492006-09-13 14:30:00 -040011558static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011559 .get_settings = tg3_get_settings,
11560 .set_settings = tg3_set_settings,
11561 .get_drvinfo = tg3_get_drvinfo,
11562 .get_regs_len = tg3_get_regs_len,
11563 .get_regs = tg3_get_regs,
11564 .get_wol = tg3_get_wol,
11565 .set_wol = tg3_set_wol,
11566 .get_msglevel = tg3_get_msglevel,
11567 .set_msglevel = tg3_set_msglevel,
11568 .nway_reset = tg3_nway_reset,
11569 .get_link = ethtool_op_get_link,
11570 .get_eeprom_len = tg3_get_eeprom_len,
11571 .get_eeprom = tg3_get_eeprom,
11572 .set_eeprom = tg3_set_eeprom,
11573 .get_ringparam = tg3_get_ringparam,
11574 .set_ringparam = tg3_set_ringparam,
11575 .get_pauseparam = tg3_get_pauseparam,
11576 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070011577 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011578 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000011579 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011580 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070011581 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070011582 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070011583 .get_sset_count = tg3_get_sset_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011584};
11585
11586static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
11587{
Michael Chan1b277772006-03-20 22:27:48 -080011588 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011589
11590 tp->nvram_size = EEPROM_CHIP_SIZE;
11591
Matt Carlsone4f34112009-02-25 14:25:00 +000011592 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011593 return;
11594
Michael Chanb16250e2006-09-27 16:10:14 -070011595 if ((magic != TG3_EEPROM_MAGIC) &&
11596 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
11597 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011598 return;
11599
11600 /*
11601 * Size the chip by reading offsets at increasing powers of two.
11602 * When we encounter our validation signature, we know the addressing
11603 * has wrapped around, and thus have our chip size.
11604 */
Michael Chan1b277772006-03-20 22:27:48 -080011605 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011606
11607 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000011608 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011609 return;
11610
Michael Chan18201802006-03-20 22:29:15 -080011611 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011612 break;
11613
11614 cursize <<= 1;
11615 }
11616
11617 tp->nvram_size = cursize;
11618}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011619
Linus Torvalds1da177e2005-04-16 15:20:36 -070011620static void __devinit tg3_get_nvram_size(struct tg3 *tp)
11621{
11622 u32 val;
11623
Matt Carlsondf259d82009-04-20 06:57:14 +000011624 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
11625 tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080011626 return;
11627
11628 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080011629 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080011630 tg3_get_eeprom_size(tp);
11631 return;
11632 }
11633
Matt Carlson6d348f22009-02-25 14:25:52 +000011634 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011635 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000011636 /* This is confusing. We want to operate on the
11637 * 16-bit value at offset 0xf2. The tg3_nvram_read()
11638 * call will read from NVRAM and byteswap the data
11639 * according to the byteswapping settings for all
11640 * other register accesses. This ensures the data we
11641 * want will always reside in the lower 16-bits.
11642 * However, the data in NVRAM is in LE format, which
11643 * means the data from the NVRAM read will always be
11644 * opposite the endianness of the CPU. The 16-bit
11645 * byteswap then brings the data to CPU endianness.
11646 */
11647 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011648 return;
11649 }
11650 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070011651 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011652}
11653
11654static void __devinit tg3_get_nvram_info(struct tg3 *tp)
11655{
11656 u32 nvcfg1;
11657
11658 nvcfg1 = tr32(NVRAM_CFG1);
11659 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
11660 tp->tg3_flags2 |= TG3_FLG2_FLASH;
Matt Carlson8590a602009-08-28 12:29:16 +000011661 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011662 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11663 tw32(NVRAM_CFG1, nvcfg1);
11664 }
11665
Michael Chan4c987482005-09-05 17:52:38 -070011666 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750) ||
Michael Chana4e2b342005-10-26 15:46:52 -070011667 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011668 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011669 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
11670 tp->nvram_jedecnum = JEDEC_ATMEL;
11671 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
11672 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11673 break;
11674 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
11675 tp->nvram_jedecnum = JEDEC_ATMEL;
11676 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
11677 break;
11678 case FLASH_VENDOR_ATMEL_EEPROM:
11679 tp->nvram_jedecnum = JEDEC_ATMEL;
11680 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11681 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11682 break;
11683 case FLASH_VENDOR_ST:
11684 tp->nvram_jedecnum = JEDEC_ST;
11685 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
11686 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11687 break;
11688 case FLASH_VENDOR_SAIFUN:
11689 tp->nvram_jedecnum = JEDEC_SAIFUN;
11690 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
11691 break;
11692 case FLASH_VENDOR_SST_SMALL:
11693 case FLASH_VENDOR_SST_LARGE:
11694 tp->nvram_jedecnum = JEDEC_SST;
11695 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
11696 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011697 }
Matt Carlson8590a602009-08-28 12:29:16 +000011698 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011699 tp->nvram_jedecnum = JEDEC_ATMEL;
11700 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
11701 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11702 }
11703}
11704
Matt Carlsona1b950d2009-09-01 13:20:17 +000011705static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
11706{
11707 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
11708 case FLASH_5752PAGE_SIZE_256:
11709 tp->nvram_pagesize = 256;
11710 break;
11711 case FLASH_5752PAGE_SIZE_512:
11712 tp->nvram_pagesize = 512;
11713 break;
11714 case FLASH_5752PAGE_SIZE_1K:
11715 tp->nvram_pagesize = 1024;
11716 break;
11717 case FLASH_5752PAGE_SIZE_2K:
11718 tp->nvram_pagesize = 2048;
11719 break;
11720 case FLASH_5752PAGE_SIZE_4K:
11721 tp->nvram_pagesize = 4096;
11722 break;
11723 case FLASH_5752PAGE_SIZE_264:
11724 tp->nvram_pagesize = 264;
11725 break;
11726 case FLASH_5752PAGE_SIZE_528:
11727 tp->nvram_pagesize = 528;
11728 break;
11729 }
11730}
11731
Michael Chan361b4ac2005-04-21 17:11:21 -070011732static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
11733{
11734 u32 nvcfg1;
11735
11736 nvcfg1 = tr32(NVRAM_CFG1);
11737
Michael Chane6af3012005-04-21 17:12:05 -070011738 /* NVRAM protection for TPM */
11739 if (nvcfg1 & (1 << 27))
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011740 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Michael Chane6af3012005-04-21 17:12:05 -070011741
Michael Chan361b4ac2005-04-21 17:11:21 -070011742 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011743 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
11744 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
11745 tp->nvram_jedecnum = JEDEC_ATMEL;
11746 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11747 break;
11748 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11749 tp->nvram_jedecnum = JEDEC_ATMEL;
11750 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11751 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11752 break;
11753 case FLASH_5752VENDOR_ST_M45PE10:
11754 case FLASH_5752VENDOR_ST_M45PE20:
11755 case FLASH_5752VENDOR_ST_M45PE40:
11756 tp->nvram_jedecnum = JEDEC_ST;
11757 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11758 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11759 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070011760 }
11761
11762 if (tp->tg3_flags2 & TG3_FLG2_FLASH) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000011763 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000011764 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070011765 /* For eeprom, set pagesize to maximum eeprom size */
11766 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11767
11768 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11769 tw32(NVRAM_CFG1, nvcfg1);
11770 }
11771}
11772
Michael Chand3c7b882006-03-23 01:28:25 -080011773static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
11774{
Matt Carlson989a9d22007-05-05 11:51:05 -070011775 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080011776
11777 nvcfg1 = tr32(NVRAM_CFG1);
11778
11779 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070011780 if (nvcfg1 & (1 << 27)) {
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011781 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Matt Carlson989a9d22007-05-05 11:51:05 -070011782 protect = 1;
11783 }
Michael Chand3c7b882006-03-23 01:28:25 -080011784
Matt Carlson989a9d22007-05-05 11:51:05 -070011785 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11786 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011787 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11788 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11789 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11790 case FLASH_5755VENDOR_ATMEL_FLASH_5:
11791 tp->nvram_jedecnum = JEDEC_ATMEL;
11792 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11793 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11794 tp->nvram_pagesize = 264;
11795 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
11796 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
11797 tp->nvram_size = (protect ? 0x3e200 :
11798 TG3_NVRAM_SIZE_512KB);
11799 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
11800 tp->nvram_size = (protect ? 0x1f200 :
11801 TG3_NVRAM_SIZE_256KB);
11802 else
11803 tp->nvram_size = (protect ? 0x1f200 :
11804 TG3_NVRAM_SIZE_128KB);
11805 break;
11806 case FLASH_5752VENDOR_ST_M45PE10:
11807 case FLASH_5752VENDOR_ST_M45PE20:
11808 case FLASH_5752VENDOR_ST_M45PE40:
11809 tp->nvram_jedecnum = JEDEC_ST;
11810 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11811 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11812 tp->nvram_pagesize = 256;
11813 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
11814 tp->nvram_size = (protect ?
11815 TG3_NVRAM_SIZE_64KB :
11816 TG3_NVRAM_SIZE_128KB);
11817 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
11818 tp->nvram_size = (protect ?
11819 TG3_NVRAM_SIZE_64KB :
11820 TG3_NVRAM_SIZE_256KB);
11821 else
11822 tp->nvram_size = (protect ?
11823 TG3_NVRAM_SIZE_128KB :
11824 TG3_NVRAM_SIZE_512KB);
11825 break;
Michael Chand3c7b882006-03-23 01:28:25 -080011826 }
11827}
11828
Michael Chan1b277772006-03-20 22:27:48 -080011829static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
11830{
11831 u32 nvcfg1;
11832
11833 nvcfg1 = tr32(NVRAM_CFG1);
11834
11835 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011836 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
11837 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11838 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
11839 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11840 tp->nvram_jedecnum = JEDEC_ATMEL;
11841 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11842 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080011843
Matt Carlson8590a602009-08-28 12:29:16 +000011844 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11845 tw32(NVRAM_CFG1, nvcfg1);
11846 break;
11847 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11848 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11849 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11850 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11851 tp->nvram_jedecnum = JEDEC_ATMEL;
11852 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11853 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11854 tp->nvram_pagesize = 264;
11855 break;
11856 case FLASH_5752VENDOR_ST_M45PE10:
11857 case FLASH_5752VENDOR_ST_M45PE20:
11858 case FLASH_5752VENDOR_ST_M45PE40:
11859 tp->nvram_jedecnum = JEDEC_ST;
11860 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11861 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11862 tp->nvram_pagesize = 256;
11863 break;
Michael Chan1b277772006-03-20 22:27:48 -080011864 }
11865}
11866
Matt Carlson6b91fa02007-10-10 18:01:09 -070011867static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
11868{
11869 u32 nvcfg1, protect = 0;
11870
11871 nvcfg1 = tr32(NVRAM_CFG1);
11872
11873 /* NVRAM protection for TPM */
11874 if (nvcfg1 & (1 << 27)) {
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011875 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011876 protect = 1;
11877 }
11878
11879 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11880 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011881 case FLASH_5761VENDOR_ATMEL_ADB021D:
11882 case FLASH_5761VENDOR_ATMEL_ADB041D:
11883 case FLASH_5761VENDOR_ATMEL_ADB081D:
11884 case FLASH_5761VENDOR_ATMEL_ADB161D:
11885 case FLASH_5761VENDOR_ATMEL_MDB021D:
11886 case FLASH_5761VENDOR_ATMEL_MDB041D:
11887 case FLASH_5761VENDOR_ATMEL_MDB081D:
11888 case FLASH_5761VENDOR_ATMEL_MDB161D:
11889 tp->nvram_jedecnum = JEDEC_ATMEL;
11890 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11891 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11892 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
11893 tp->nvram_pagesize = 256;
11894 break;
11895 case FLASH_5761VENDOR_ST_A_M45PE20:
11896 case FLASH_5761VENDOR_ST_A_M45PE40:
11897 case FLASH_5761VENDOR_ST_A_M45PE80:
11898 case FLASH_5761VENDOR_ST_A_M45PE16:
11899 case FLASH_5761VENDOR_ST_M_M45PE20:
11900 case FLASH_5761VENDOR_ST_M_M45PE40:
11901 case FLASH_5761VENDOR_ST_M_M45PE80:
11902 case FLASH_5761VENDOR_ST_M_M45PE16:
11903 tp->nvram_jedecnum = JEDEC_ST;
11904 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11905 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11906 tp->nvram_pagesize = 256;
11907 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011908 }
11909
11910 if (protect) {
11911 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
11912 } else {
11913 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011914 case FLASH_5761VENDOR_ATMEL_ADB161D:
11915 case FLASH_5761VENDOR_ATMEL_MDB161D:
11916 case FLASH_5761VENDOR_ST_A_M45PE16:
11917 case FLASH_5761VENDOR_ST_M_M45PE16:
11918 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
11919 break;
11920 case FLASH_5761VENDOR_ATMEL_ADB081D:
11921 case FLASH_5761VENDOR_ATMEL_MDB081D:
11922 case FLASH_5761VENDOR_ST_A_M45PE80:
11923 case FLASH_5761VENDOR_ST_M_M45PE80:
11924 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
11925 break;
11926 case FLASH_5761VENDOR_ATMEL_ADB041D:
11927 case FLASH_5761VENDOR_ATMEL_MDB041D:
11928 case FLASH_5761VENDOR_ST_A_M45PE40:
11929 case FLASH_5761VENDOR_ST_M_M45PE40:
11930 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11931 break;
11932 case FLASH_5761VENDOR_ATMEL_ADB021D:
11933 case FLASH_5761VENDOR_ATMEL_MDB021D:
11934 case FLASH_5761VENDOR_ST_A_M45PE20:
11935 case FLASH_5761VENDOR_ST_M_M45PE20:
11936 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11937 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011938 }
11939 }
11940}
11941
Michael Chanb5d37722006-09-27 16:06:21 -070011942static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
11943{
11944 tp->nvram_jedecnum = JEDEC_ATMEL;
11945 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11946 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11947}
11948
Matt Carlson321d32a2008-11-21 17:22:19 -080011949static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
11950{
11951 u32 nvcfg1;
11952
11953 nvcfg1 = tr32(NVRAM_CFG1);
11954
11955 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11956 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11957 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11958 tp->nvram_jedecnum = JEDEC_ATMEL;
11959 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11960 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11961
11962 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11963 tw32(NVRAM_CFG1, nvcfg1);
11964 return;
11965 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11966 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11967 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11968 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11969 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11970 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11971 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
11972 tp->nvram_jedecnum = JEDEC_ATMEL;
11973 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11974 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11975
11976 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11977 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11978 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11979 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11980 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11981 break;
11982 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11983 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11984 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11985 break;
11986 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11987 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
11988 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11989 break;
11990 }
11991 break;
11992 case FLASH_5752VENDOR_ST_M45PE10:
11993 case FLASH_5752VENDOR_ST_M45PE20:
11994 case FLASH_5752VENDOR_ST_M45PE40:
11995 tp->nvram_jedecnum = JEDEC_ST;
11996 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11997 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11998
11999 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12000 case FLASH_5752VENDOR_ST_M45PE10:
12001 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12002 break;
12003 case FLASH_5752VENDOR_ST_M45PE20:
12004 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12005 break;
12006 case FLASH_5752VENDOR_ST_M45PE40:
12007 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12008 break;
12009 }
12010 break;
12011 default:
Matt Carlsondf259d82009-04-20 06:57:14 +000012012 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
Matt Carlson321d32a2008-11-21 17:22:19 -080012013 return;
12014 }
12015
Matt Carlsona1b950d2009-09-01 13:20:17 +000012016 tg3_nvram_get_pagesize(tp, nvcfg1);
12017 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Matt Carlson321d32a2008-11-21 17:22:19 -080012018 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012019}
12020
12021
12022static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
12023{
12024 u32 nvcfg1;
12025
12026 nvcfg1 = tr32(NVRAM_CFG1);
12027
12028 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12029 case FLASH_5717VENDOR_ATMEL_EEPROM:
12030 case FLASH_5717VENDOR_MICRO_EEPROM:
12031 tp->nvram_jedecnum = JEDEC_ATMEL;
12032 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
12033 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12034
12035 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12036 tw32(NVRAM_CFG1, nvcfg1);
12037 return;
12038 case FLASH_5717VENDOR_ATMEL_MDB011D:
12039 case FLASH_5717VENDOR_ATMEL_ADB011B:
12040 case FLASH_5717VENDOR_ATMEL_ADB011D:
12041 case FLASH_5717VENDOR_ATMEL_MDB021D:
12042 case FLASH_5717VENDOR_ATMEL_ADB021B:
12043 case FLASH_5717VENDOR_ATMEL_ADB021D:
12044 case FLASH_5717VENDOR_ATMEL_45USPT:
12045 tp->nvram_jedecnum = JEDEC_ATMEL;
12046 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
12047 tp->tg3_flags2 |= TG3_FLG2_FLASH;
12048
12049 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12050 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012051 /* Detect size with tg3_nvram_get_size() */
12052 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012053 case FLASH_5717VENDOR_ATMEL_ADB021B:
12054 case FLASH_5717VENDOR_ATMEL_ADB021D:
12055 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12056 break;
12057 default:
12058 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12059 break;
12060 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012061 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012062 case FLASH_5717VENDOR_ST_M_M25PE10:
12063 case FLASH_5717VENDOR_ST_A_M25PE10:
12064 case FLASH_5717VENDOR_ST_M_M45PE10:
12065 case FLASH_5717VENDOR_ST_A_M45PE10:
12066 case FLASH_5717VENDOR_ST_M_M25PE20:
12067 case FLASH_5717VENDOR_ST_A_M25PE20:
12068 case FLASH_5717VENDOR_ST_M_M45PE20:
12069 case FLASH_5717VENDOR_ST_A_M45PE20:
12070 case FLASH_5717VENDOR_ST_25USPT:
12071 case FLASH_5717VENDOR_ST_45USPT:
12072 tp->nvram_jedecnum = JEDEC_ST;
12073 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
12074 tp->tg3_flags2 |= TG3_FLG2_FLASH;
12075
12076 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12077 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012078 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012079 /* Detect size with tg3_nvram_get_size() */
12080 break;
12081 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012082 case FLASH_5717VENDOR_ST_A_M45PE20:
12083 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12084 break;
12085 default:
12086 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12087 break;
12088 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012089 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012090 default:
12091 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
12092 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080012093 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000012094
12095 tg3_nvram_get_pagesize(tp, nvcfg1);
12096 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
12097 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
Matt Carlson321d32a2008-11-21 17:22:19 -080012098}
12099
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012100static void __devinit tg3_get_5720_nvram_info(struct tg3 *tp)
12101{
12102 u32 nvcfg1, nvmpinstrp;
12103
12104 nvcfg1 = tr32(NVRAM_CFG1);
12105 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
12106
12107 switch (nvmpinstrp) {
12108 case FLASH_5720_EEPROM_HD:
12109 case FLASH_5720_EEPROM_LD:
12110 tp->nvram_jedecnum = JEDEC_ATMEL;
12111 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
12112
12113 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12114 tw32(NVRAM_CFG1, nvcfg1);
12115 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
12116 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12117 else
12118 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
12119 return;
12120 case FLASH_5720VENDOR_M_ATMEL_DB011D:
12121 case FLASH_5720VENDOR_A_ATMEL_DB011B:
12122 case FLASH_5720VENDOR_A_ATMEL_DB011D:
12123 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12124 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12125 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12126 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12127 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12128 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12129 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12130 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12131 case FLASH_5720VENDOR_ATMEL_45USPT:
12132 tp->nvram_jedecnum = JEDEC_ATMEL;
12133 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
12134 tp->tg3_flags2 |= TG3_FLG2_FLASH;
12135
12136 switch (nvmpinstrp) {
12137 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12138 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12139 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12140 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12141 break;
12142 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12143 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12144 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12145 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12146 break;
12147 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12148 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12149 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12150 break;
12151 default:
12152 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12153 break;
12154 }
12155 break;
12156 case FLASH_5720VENDOR_M_ST_M25PE10:
12157 case FLASH_5720VENDOR_M_ST_M45PE10:
12158 case FLASH_5720VENDOR_A_ST_M25PE10:
12159 case FLASH_5720VENDOR_A_ST_M45PE10:
12160 case FLASH_5720VENDOR_M_ST_M25PE20:
12161 case FLASH_5720VENDOR_M_ST_M45PE20:
12162 case FLASH_5720VENDOR_A_ST_M25PE20:
12163 case FLASH_5720VENDOR_A_ST_M45PE20:
12164 case FLASH_5720VENDOR_M_ST_M25PE40:
12165 case FLASH_5720VENDOR_M_ST_M45PE40:
12166 case FLASH_5720VENDOR_A_ST_M25PE40:
12167 case FLASH_5720VENDOR_A_ST_M45PE40:
12168 case FLASH_5720VENDOR_M_ST_M25PE80:
12169 case FLASH_5720VENDOR_M_ST_M45PE80:
12170 case FLASH_5720VENDOR_A_ST_M25PE80:
12171 case FLASH_5720VENDOR_A_ST_M45PE80:
12172 case FLASH_5720VENDOR_ST_25USPT:
12173 case FLASH_5720VENDOR_ST_45USPT:
12174 tp->nvram_jedecnum = JEDEC_ST;
12175 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
12176 tp->tg3_flags2 |= TG3_FLG2_FLASH;
12177
12178 switch (nvmpinstrp) {
12179 case FLASH_5720VENDOR_M_ST_M25PE20:
12180 case FLASH_5720VENDOR_M_ST_M45PE20:
12181 case FLASH_5720VENDOR_A_ST_M25PE20:
12182 case FLASH_5720VENDOR_A_ST_M45PE20:
12183 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12184 break;
12185 case FLASH_5720VENDOR_M_ST_M25PE40:
12186 case FLASH_5720VENDOR_M_ST_M45PE40:
12187 case FLASH_5720VENDOR_A_ST_M25PE40:
12188 case FLASH_5720VENDOR_A_ST_M45PE40:
12189 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12190 break;
12191 case FLASH_5720VENDOR_M_ST_M25PE80:
12192 case FLASH_5720VENDOR_M_ST_M45PE80:
12193 case FLASH_5720VENDOR_A_ST_M25PE80:
12194 case FLASH_5720VENDOR_A_ST_M45PE80:
12195 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12196 break;
12197 default:
12198 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12199 break;
12200 }
12201 break;
12202 default:
12203 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
12204 return;
12205 }
12206
12207 tg3_nvram_get_pagesize(tp, nvcfg1);
12208 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
12209 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
12210}
12211
Linus Torvalds1da177e2005-04-16 15:20:36 -070012212/* Chips other than 5700/5701 use the NVRAM for fetching info. */
12213static void __devinit tg3_nvram_init(struct tg3 *tp)
12214{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012215 tw32_f(GRC_EEPROM_ADDR,
12216 (EEPROM_ADDR_FSM_RESET |
12217 (EEPROM_DEFAULT_CLOCK_PERIOD <<
12218 EEPROM_ADDR_CLKPERD_SHIFT)));
12219
Michael Chan9d57f012006-12-07 00:23:25 -080012220 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012221
12222 /* Enable seeprom accesses. */
12223 tw32_f(GRC_LOCAL_CTRL,
12224 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
12225 udelay(100);
12226
12227 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
12228 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
12229 tp->tg3_flags |= TG3_FLAG_NVRAM;
12230
Michael Chanec41c7d2006-01-17 02:40:55 -080012231 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000012232 netdev_warn(tp->dev,
12233 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000012234 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080012235 return;
12236 }
Michael Chane6af3012005-04-21 17:12:05 -070012237 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012238
Matt Carlson989a9d22007-05-05 11:51:05 -070012239 tp->nvram_size = 0;
12240
Michael Chan361b4ac2005-04-21 17:11:21 -070012241 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
12242 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080012243 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
12244 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070012245 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070012246 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
12247 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080012248 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070012249 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
12250 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070012251 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12252 tg3_get_5906_nvram_info(tp);
Matt Carlsonb703df62009-12-03 08:36:21 +000012253 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
12254 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson321d32a2008-11-21 17:22:19 -080012255 tg3_get_57780_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012256 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
12257 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000012258 tg3_get_5717_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012259 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
12260 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070012261 else
12262 tg3_get_nvram_info(tp);
12263
Matt Carlson989a9d22007-05-05 11:51:05 -070012264 if (tp->nvram_size == 0)
12265 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012266
Michael Chane6af3012005-04-21 17:12:05 -070012267 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080012268 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012269
12270 } else {
12271 tp->tg3_flags &= ~(TG3_FLAG_NVRAM | TG3_FLAG_NVRAM_BUFFERED);
12272
12273 tg3_get_eeprom_size(tp);
12274 }
12275}
12276
Linus Torvalds1da177e2005-04-16 15:20:36 -070012277static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
12278 u32 offset, u32 len, u8 *buf)
12279{
12280 int i, j, rc = 0;
12281 u32 val;
12282
12283 for (i = 0; i < len; i += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012284 u32 addr;
Matt Carlsona9dc5292009-02-25 14:25:30 +000012285 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012286
12287 addr = offset + i;
12288
12289 memcpy(&data, buf + i, 4);
12290
Matt Carlson62cedd12009-04-20 14:52:29 -070012291 /*
12292 * The SEEPROM interface expects the data to always be opposite
12293 * the native endian format. We accomplish this by reversing
12294 * all the operations that would have been performed on the
12295 * data from a call to tg3_nvram_read_be32().
12296 */
12297 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012298
12299 val = tr32(GRC_EEPROM_ADDR);
12300 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
12301
12302 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
12303 EEPROM_ADDR_READ);
12304 tw32(GRC_EEPROM_ADDR, val |
12305 (0 << EEPROM_ADDR_DEVID_SHIFT) |
12306 (addr & EEPROM_ADDR_ADDR_MASK) |
12307 EEPROM_ADDR_START |
12308 EEPROM_ADDR_WRITE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012309
Michael Chan9d57f012006-12-07 00:23:25 -080012310 for (j = 0; j < 1000; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012311 val = tr32(GRC_EEPROM_ADDR);
12312
12313 if (val & EEPROM_ADDR_COMPLETE)
12314 break;
Michael Chan9d57f012006-12-07 00:23:25 -080012315 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012316 }
12317 if (!(val & EEPROM_ADDR_COMPLETE)) {
12318 rc = -EBUSY;
12319 break;
12320 }
12321 }
12322
12323 return rc;
12324}
12325
12326/* offset and length are dword aligned */
12327static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
12328 u8 *buf)
12329{
12330 int ret = 0;
12331 u32 pagesize = tp->nvram_pagesize;
12332 u32 pagemask = pagesize - 1;
12333 u32 nvram_cmd;
12334 u8 *tmp;
12335
12336 tmp = kmalloc(pagesize, GFP_KERNEL);
12337 if (tmp == NULL)
12338 return -ENOMEM;
12339
12340 while (len) {
12341 int j;
Michael Chane6af3012005-04-21 17:12:05 -070012342 u32 phy_addr, page_off, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012343
12344 phy_addr = offset & ~pagemask;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012345
Linus Torvalds1da177e2005-04-16 15:20:36 -070012346 for (j = 0; j < pagesize; j += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012347 ret = tg3_nvram_read_be32(tp, phy_addr + j,
12348 (__be32 *) (tmp + j));
12349 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012350 break;
12351 }
12352 if (ret)
12353 break;
12354
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012355 page_off = offset & pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012356 size = pagesize;
12357 if (len < size)
12358 size = len;
12359
12360 len -= size;
12361
12362 memcpy(tmp + page_off, buf, size);
12363
12364 offset = offset + (pagesize - page_off);
12365
Michael Chane6af3012005-04-21 17:12:05 -070012366 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012367
12368 /*
12369 * Before we can erase the flash page, we need
12370 * to issue a special "write enable" command.
12371 */
12372 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12373
12374 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12375 break;
12376
12377 /* Erase the target page */
12378 tw32(NVRAM_ADDR, phy_addr);
12379
12380 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
12381 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
12382
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012383 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012384 break;
12385
12386 /* Issue another write enable to start the write. */
12387 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12388
12389 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12390 break;
12391
12392 for (j = 0; j < pagesize; j += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012393 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012394
Al Virob9fc7dc2007-12-17 22:59:57 -080012395 data = *((__be32 *) (tmp + j));
Matt Carlsona9dc5292009-02-25 14:25:30 +000012396
Al Virob9fc7dc2007-12-17 22:59:57 -080012397 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012398
12399 tw32(NVRAM_ADDR, phy_addr + j);
12400
12401 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
12402 NVRAM_CMD_WR;
12403
12404 if (j == 0)
12405 nvram_cmd |= NVRAM_CMD_FIRST;
12406 else if (j == (pagesize - 4))
12407 nvram_cmd |= NVRAM_CMD_LAST;
12408
12409 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12410 break;
12411 }
12412 if (ret)
12413 break;
12414 }
12415
12416 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12417 tg3_nvram_exec_cmd(tp, nvram_cmd);
12418
12419 kfree(tmp);
12420
12421 return ret;
12422}
12423
12424/* offset and length are dword aligned */
12425static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
12426 u8 *buf)
12427{
12428 int i, ret = 0;
12429
12430 for (i = 0; i < len; i += 4, offset += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012431 u32 page_off, phy_addr, nvram_cmd;
12432 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012433
12434 memcpy(&data, buf + i, 4);
Al Virob9fc7dc2007-12-17 22:59:57 -080012435 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012436
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012437 page_off = offset % tp->nvram_pagesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012438
Michael Chan18201802006-03-20 22:29:15 -080012439 phy_addr = tg3_nvram_phys_addr(tp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012440
12441 tw32(NVRAM_ADDR, phy_addr);
12442
12443 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
12444
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012445 if (page_off == 0 || i == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012446 nvram_cmd |= NVRAM_CMD_FIRST;
Michael Chanf6d9a252006-04-29 19:00:24 -070012447 if (page_off == (tp->nvram_pagesize - 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012448 nvram_cmd |= NVRAM_CMD_LAST;
12449
12450 if (i == (len - 4))
12451 nvram_cmd |= NVRAM_CMD_LAST;
12452
Matt Carlson321d32a2008-11-21 17:22:19 -080012453 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
12454 !(tp->tg3_flags3 & TG3_FLG3_5755_PLUS) &&
Michael Chan4c987482005-09-05 17:52:38 -070012455 (tp->nvram_jedecnum == JEDEC_ST) &&
12456 (nvram_cmd & NVRAM_CMD_FIRST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012457
12458 if ((ret = tg3_nvram_exec_cmd(tp,
12459 NVRAM_CMD_WREN | NVRAM_CMD_GO |
12460 NVRAM_CMD_DONE)))
12461
12462 break;
12463 }
12464 if (!(tp->tg3_flags2 & TG3_FLG2_FLASH)) {
12465 /* We always do complete word writes to eeprom. */
12466 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
12467 }
12468
12469 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12470 break;
12471 }
12472 return ret;
12473}
12474
12475/* offset and length are dword aligned */
12476static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
12477{
12478 int ret;
12479
Linus Torvalds1da177e2005-04-16 15:20:36 -070012480 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT) {
Michael Chan314fba32005-04-21 17:07:04 -070012481 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
12482 ~GRC_LCLCTRL_GPIO_OUTPUT1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012483 udelay(40);
12484 }
12485
12486 if (!(tp->tg3_flags & TG3_FLAG_NVRAM)) {
12487 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
Matt Carlson859a5882010-04-05 10:19:28 +000012488 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012489 u32 grc_mode;
12490
Michael Chanec41c7d2006-01-17 02:40:55 -080012491 ret = tg3_nvram_lock(tp);
12492 if (ret)
12493 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012494
Michael Chane6af3012005-04-21 17:12:05 -070012495 tg3_enable_nvram_access(tp);
12496 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Matt Carlsonf66a29b2009-11-13 13:03:36 +000012497 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012498 tw32(NVRAM_WRITE1, 0x406);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012499
12500 grc_mode = tr32(GRC_MODE);
12501 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
12502
12503 if ((tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) ||
12504 !(tp->tg3_flags2 & TG3_FLG2_FLASH)) {
12505
12506 ret = tg3_nvram_write_block_buffered(tp, offset, len,
12507 buf);
Matt Carlson859a5882010-04-05 10:19:28 +000012508 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012509 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
12510 buf);
12511 }
12512
12513 grc_mode = tr32(GRC_MODE);
12514 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
12515
Michael Chane6af3012005-04-21 17:12:05 -070012516 tg3_disable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012517 tg3_nvram_unlock(tp);
12518 }
12519
12520 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT) {
Michael Chan314fba32005-04-21 17:07:04 -070012521 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012522 udelay(40);
12523 }
12524
12525 return ret;
12526}
12527
12528struct subsys_tbl_ent {
12529 u16 subsys_vendor, subsys_devid;
12530 u32 phy_id;
12531};
12532
Matt Carlson24daf2b2010-02-17 15:17:02 +000012533static struct subsys_tbl_ent subsys_id_to_phy_id[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012534 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012535 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012536 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012537 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012538 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012539 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012540 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012541 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12542 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
12543 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012544 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012545 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012546 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012547 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12548 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
12549 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012550 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, 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_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012553 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012554 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012555 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012556 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012557
12558 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012559 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012560 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012561 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012562 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012563 { TG3PCI_SUBVENDOR_ID_3COM,
12564 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
12565 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012566 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012567 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012568 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012569
12570 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012571 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012572 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012573 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012574 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012575 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012576 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012577 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012578 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012579
12580 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012581 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012582 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012583 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012584 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012585 { TG3PCI_SUBVENDOR_ID_COMPAQ,
12586 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
12587 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012588 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012589 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012590 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012591
12592 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012593 { TG3PCI_SUBVENDOR_ID_IBM,
12594 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012595};
12596
Matt Carlson24daf2b2010-02-17 15:17:02 +000012597static struct subsys_tbl_ent * __devinit tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012598{
12599 int i;
12600
12601 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
12602 if ((subsys_id_to_phy_id[i].subsys_vendor ==
12603 tp->pdev->subsystem_vendor) &&
12604 (subsys_id_to_phy_id[i].subsys_devid ==
12605 tp->pdev->subsystem_device))
12606 return &subsys_id_to_phy_id[i];
12607 }
12608 return NULL;
12609}
12610
Michael Chan7d0c41e2005-04-21 17:06:20 -070012611static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012612{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012613 u32 val;
Michael Chancaf636c72006-03-22 01:05:31 -080012614 u16 pmcsr;
12615
12616 /* On some early chips the SRAM cannot be accessed in D3hot state,
12617 * so need make sure we're in D0.
12618 */
12619 pci_read_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, &pmcsr);
12620 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
12621 pci_write_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, pmcsr);
12622 msleep(1);
Michael Chan7d0c41e2005-04-21 17:06:20 -070012623
12624 /* Make sure register accesses (indirect or otherwise)
12625 * will function correctly.
12626 */
12627 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
12628 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012629
David S. Millerf49639e2006-06-09 11:58:36 -070012630 /* The memory arbiter has to be enabled in order for SRAM accesses
12631 * to succeed. Normally on powerup the tg3 chip firmware will make
12632 * sure it is enabled, but other entities such as system netboot
12633 * code might disable it.
12634 */
12635 val = tr32(MEMARB_MODE);
12636 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
12637
Matt Carlson79eb6902010-02-17 15:17:03 +000012638 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012639 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12640
Gary Zambranoa85feb82007-05-05 11:52:19 -070012641 /* Assume an onboard device and WOL capable by default. */
12642 tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT | TG3_FLAG_WOL_CAP;
David S. Miller72b845e2006-03-14 14:11:48 -080012643
Michael Chanb5d37722006-09-27 16:06:21 -070012644 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080012645 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Michael Chanb5d37722006-09-27 16:06:21 -070012646 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080012647 tp->tg3_flags2 |= TG3_FLG2_IS_NIC;
12648 }
Matt Carlson0527ba32007-10-10 18:03:30 -070012649 val = tr32(VCPU_CFGSHDW);
12650 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Matt Carlson8ed5d972007-05-07 00:25:49 -070012651 tp->tg3_flags |= TG3_FLAG_ASPM_WORKAROUND;
Matt Carlson0527ba32007-10-10 18:03:30 -070012652 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Matt Carlson20232762008-12-21 20:18:56 -080012653 (val & VCPU_CFGSHDW_WOL_MAGPKT))
Matt Carlson0527ba32007-10-10 18:03:30 -070012654 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012655 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070012656 }
12657
Linus Torvalds1da177e2005-04-16 15:20:36 -070012658 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
12659 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
12660 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070012661 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012662 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012663
12664 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
12665 tp->nic_sram_data_cfg = nic_cfg;
12666
12667 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
12668 ver >>= NIC_SRAM_DATA_VER_SHIFT;
12669 if ((GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700) &&
12670 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) &&
12671 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703) &&
12672 (ver > 0) && (ver < 0x100))
12673 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
12674
Matt Carlsona9daf362008-05-25 23:49:44 -070012675 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
12676 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
12677
Linus Torvalds1da177e2005-04-16 15:20:36 -070012678 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
12679 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
12680 eeprom_phy_serdes = 1;
12681
12682 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
12683 if (nic_phy_id != 0) {
12684 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
12685 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
12686
12687 eeprom_phy_id = (id1 >> 16) << 10;
12688 eeprom_phy_id |= (id2 & 0xfc00) << 16;
12689 eeprom_phy_id |= (id2 & 0x03ff) << 0;
12690 } else
12691 eeprom_phy_id = 0;
12692
Michael Chan7d0c41e2005-04-21 17:06:20 -070012693 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070012694 if (eeprom_phy_serdes) {
Matt Carlsona50d0792010-06-05 17:24:37 +000012695 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012696 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000012697 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012698 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070012699 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070012700
John W. Linvillecbf46852005-04-21 17:01:29 -070012701 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012702 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
12703 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070012704 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070012705 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
12706
12707 switch (led_cfg) {
12708 default:
12709 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
12710 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12711 break;
12712
12713 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
12714 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12715 break;
12716
12717 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
12718 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070012719
12720 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
12721 * read on some older 5700/5701 bootcode.
12722 */
12723 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
12724 ASIC_REV_5700 ||
12725 GET_ASIC_REV(tp->pci_chip_rev_id) ==
12726 ASIC_REV_5701)
12727 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12728
Linus Torvalds1da177e2005-04-16 15:20:36 -070012729 break;
12730
12731 case SHASTA_EXT_LED_SHARED:
12732 tp->led_ctrl = LED_CTRL_MODE_SHARED;
12733 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
12734 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
12735 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12736 LED_CTRL_MODE_PHY_2);
12737 break;
12738
12739 case SHASTA_EXT_LED_MAC:
12740 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
12741 break;
12742
12743 case SHASTA_EXT_LED_COMBO:
12744 tp->led_ctrl = LED_CTRL_MODE_COMBO;
12745 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
12746 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12747 LED_CTRL_MODE_PHY_2);
12748 break;
12749
Stephen Hemminger855e1112008-04-16 16:37:28 -070012750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012751
12752 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
12753 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
12754 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
12755 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12756
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012757 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
12758 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080012759
Michael Chan9d26e212006-12-07 00:21:14 -080012760 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012761 tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080012762 if ((tp->pdev->subsystem_vendor ==
12763 PCI_VENDOR_ID_ARIMA) &&
12764 (tp->pdev->subsystem_device == 0x205a ||
12765 tp->pdev->subsystem_device == 0x2063))
12766 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
12767 } else {
David S. Millerf49639e2006-06-09 11:58:36 -070012768 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080012769 tp->tg3_flags2 |= TG3_FLG2_IS_NIC;
12770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012771
12772 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
12773 tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
John W. Linvillecbf46852005-04-21 17:01:29 -070012774 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012775 tp->tg3_flags2 |= TG3_FLG2_ASF_NEW_HANDSHAKE;
12776 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012777
12778 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
12779 (tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
Matt Carlson0d3031d2007-10-10 18:02:43 -070012780 tp->tg3_flags3 |= TG3_FLG3_ENABLE_APE;
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012781
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012782 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070012783 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
12784 tp->tg3_flags &= ~TG3_FLAG_WOL_CAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012785
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070012786 if ((tp->tg3_flags & TG3_FLAG_WOL_CAP) &&
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012787 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE))
Matt Carlson0527ba32007-10-10 18:03:30 -070012788 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
12789
Linus Torvalds1da177e2005-04-16 15:20:36 -070012790 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012791 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012792
12793 /* serdes signal pre-emphasis in register 0x590 set by */
12794 /* bootcode if bit 18 is set */
12795 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012796 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070012797
Matt Carlson1407deb2011-04-05 14:22:44 +000012798 if (((tp->tg3_flags3 & TG3_FLG3_57765_PLUS) ||
Matt Carlson2e1e3292010-11-24 08:31:53 +000012799 ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
12800 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX))) &&
Matt Carlson6833c042008-11-21 17:18:59 -080012801 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012802 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080012803
Matt Carlson8c69b1e2010-08-02 11:26:00 +000012804 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
12805 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlson1407deb2011-04-05 14:22:44 +000012806 !(tp->tg3_flags3 & TG3_FLG3_57765_PLUS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070012807 u32 cfg3;
12808
12809 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
12810 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
12811 tp->tg3_flags |= TG3_FLAG_ASPM_WORKAROUND;
12812 }
Matt Carlsona9daf362008-05-25 23:49:44 -070012813
Matt Carlson14417062010-02-17 15:16:59 +000012814 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
12815 tp->tg3_flags3 |= TG3_FLG3_RGMII_INBAND_DISABLE;
Matt Carlsona9daf362008-05-25 23:49:44 -070012816 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
12817 tp->tg3_flags3 |= TG3_FLG3_RGMII_EXT_IBND_RX_EN;
12818 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
12819 tp->tg3_flags3 |= TG3_FLG3_RGMII_EXT_IBND_TX_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012820 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012821done:
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000012822 if (tp->tg3_flags & TG3_FLAG_WOL_CAP)
12823 device_set_wakeup_enable(&tp->pdev->dev,
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012824 tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000012825 else
12826 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070012827}
12828
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012829static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
12830{
12831 int i;
12832 u32 val;
12833
12834 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
12835 tw32(OTP_CTRL, cmd);
12836
12837 /* Wait for up to 1 ms for command to execute. */
12838 for (i = 0; i < 100; i++) {
12839 val = tr32(OTP_STATUS);
12840 if (val & OTP_STATUS_CMD_DONE)
12841 break;
12842 udelay(10);
12843 }
12844
12845 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
12846}
12847
12848/* Read the gphy configuration from the OTP region of the chip. The gphy
12849 * configuration is a 32-bit value that straddles the alignment boundary.
12850 * We do two 32-bit reads and then shift and merge the results.
12851 */
12852static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
12853{
12854 u32 bhalf_otp, thalf_otp;
12855
12856 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
12857
12858 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
12859 return 0;
12860
12861 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
12862
12863 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12864 return 0;
12865
12866 thalf_otp = tr32(OTP_READ_DATA);
12867
12868 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
12869
12870 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12871 return 0;
12872
12873 bhalf_otp = tr32(OTP_READ_DATA);
12874
12875 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
12876}
12877
Matt Carlsone256f8a2011-03-09 16:58:24 +000012878static void __devinit tg3_phy_init_link_config(struct tg3 *tp)
12879{
12880 u32 adv = ADVERTISED_Autoneg |
12881 ADVERTISED_Pause;
12882
12883 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
12884 adv |= ADVERTISED_1000baseT_Half |
12885 ADVERTISED_1000baseT_Full;
12886
12887 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
12888 adv |= ADVERTISED_100baseT_Half |
12889 ADVERTISED_100baseT_Full |
12890 ADVERTISED_10baseT_Half |
12891 ADVERTISED_10baseT_Full |
12892 ADVERTISED_TP;
12893 else
12894 adv |= ADVERTISED_FIBRE;
12895
12896 tp->link_config.advertising = adv;
12897 tp->link_config.speed = SPEED_INVALID;
12898 tp->link_config.duplex = DUPLEX_INVALID;
12899 tp->link_config.autoneg = AUTONEG_ENABLE;
12900 tp->link_config.active_speed = SPEED_INVALID;
12901 tp->link_config.active_duplex = DUPLEX_INVALID;
12902 tp->link_config.orig_speed = SPEED_INVALID;
12903 tp->link_config.orig_duplex = DUPLEX_INVALID;
12904 tp->link_config.orig_autoneg = AUTONEG_INVALID;
12905}
12906
Michael Chan7d0c41e2005-04-21 17:06:20 -070012907static int __devinit tg3_phy_probe(struct tg3 *tp)
12908{
12909 u32 hw_phy_id_1, hw_phy_id_2;
12910 u32 hw_phy_id, hw_phy_id_masked;
12911 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012912
Matt Carlsone256f8a2011-03-09 16:58:24 +000012913 /* flow control autonegotiation is default behavior */
12914 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
12915 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
12916
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012917 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)
12918 return tg3_phy_init(tp);
12919
Linus Torvalds1da177e2005-04-16 15:20:36 -070012920 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010012921 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012922 */
12923 err = 0;
Matt Carlson0d3031d2007-10-10 18:02:43 -070012924 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
12925 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000012926 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012927 } else {
12928 /* Now read the physical PHY_ID from the chip and verify
12929 * that it is sane. If it doesn't look good, we fall back
12930 * to either the hard-coded table based PHY_ID and failing
12931 * that the value found in the eeprom area.
12932 */
12933 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
12934 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
12935
12936 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
12937 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
12938 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
12939
Matt Carlson79eb6902010-02-17 15:17:03 +000012940 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012941 }
12942
Matt Carlson79eb6902010-02-17 15:17:03 +000012943 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012944 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000012945 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012946 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070012947 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012948 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012949 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000012950 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070012951 /* Do nothing, phy ID already set up in
12952 * tg3_get_eeprom_hw_cfg().
12953 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070012954 } else {
12955 struct subsys_tbl_ent *p;
12956
12957 /* No eeprom signature? Try the hardcoded
12958 * subsys device table.
12959 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012960 p = tg3_lookup_by_subsys(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012961 if (!p)
12962 return -ENODEV;
12963
12964 tp->phy_id = p->phy_id;
12965 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000012966 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012967 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012968 }
12969 }
12970
Matt Carlsona6b68da2010-12-06 08:28:52 +000012971 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
12972 ((tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 &&
12973 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0) ||
12974 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12975 tp->pci_chip_rev_id != CHIPREV_ID_57765_A0)))
Matt Carlson52b02d02010-10-14 10:37:41 +000012976 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
12977
Matt Carlsone256f8a2011-03-09 16:58:24 +000012978 tg3_phy_init_link_config(tp);
12979
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012980 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Matt Carlson0d3031d2007-10-10 18:02:43 -070012981 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070012982 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chan3600d912006-12-07 00:21:48 -080012983 u32 bmsr, adv_reg, tg3_ctrl, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012984
12985 tg3_readphy(tp, MII_BMSR, &bmsr);
12986 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
12987 (bmsr & BMSR_LSTATUS))
12988 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012989
Linus Torvalds1da177e2005-04-16 15:20:36 -070012990 err = tg3_phy_reset(tp);
12991 if (err)
12992 return err;
12993
12994 adv_reg = (ADVERTISE_10HALF | ADVERTISE_10FULL |
12995 ADVERTISE_100HALF | ADVERTISE_100FULL |
12996 ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
12997 tg3_ctrl = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012998 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012999 tg3_ctrl = (MII_TG3_CTRL_ADV_1000_HALF |
13000 MII_TG3_CTRL_ADV_1000_FULL);
13001 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
13002 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
13003 tg3_ctrl |= (MII_TG3_CTRL_AS_MASTER |
13004 MII_TG3_CTRL_ENABLE_AS_MASTER);
13005 }
13006
Michael Chan3600d912006-12-07 00:21:48 -080013007 mask = (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
13008 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
13009 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full);
13010 if (!tg3_copper_is_advertising_all(tp, mask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013011 tg3_writephy(tp, MII_ADVERTISE, adv_reg);
13012
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013013 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013014 tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
13015
13016 tg3_writephy(tp, MII_BMCR,
13017 BMCR_ANENABLE | BMCR_ANRESTART);
13018 }
13019 tg3_phy_set_wirespeed(tp);
13020
13021 tg3_writephy(tp, MII_ADVERTISE, adv_reg);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013022 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013023 tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
13024 }
13025
13026skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000013027 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013028 err = tg3_init_5401phy_dsp(tp);
13029 if (err)
13030 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013031
Linus Torvalds1da177e2005-04-16 15:20:36 -070013032 err = tg3_init_5401phy_dsp(tp);
13033 }
13034
Linus Torvalds1da177e2005-04-16 15:20:36 -070013035 return err;
13036}
13037
Matt Carlson184b8902010-04-05 10:19:25 +000013038static void __devinit tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013039{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013040 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013041 unsigned int block_end, rosize, len;
Matt Carlson184b8902010-04-05 10:19:25 +000013042 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013043
Matt Carlsonc3e94502011-04-13 11:05:08 +000013044 vpd_data = (u8 *)tg3_vpd_readblock(tp);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013045 if (!vpd_data)
13046 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013047
Matt Carlson4181b2c2010-02-26 14:04:45 +000013048 i = pci_vpd_find_tag(vpd_data, 0, TG3_NVM_VPD_LEN,
13049 PCI_VPD_LRDT_RO_DATA);
13050 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013051 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013052
13053 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
13054 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
13055 i += PCI_VPD_LRDT_TAG_SIZE;
13056
13057 if (block_end > TG3_NVM_VPD_LEN)
13058 goto out_not_found;
13059
Matt Carlson184b8902010-04-05 10:19:25 +000013060 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13061 PCI_VPD_RO_KEYWORD_MFR_ID);
13062 if (j > 0) {
13063 len = pci_vpd_info_field_size(&vpd_data[j]);
13064
13065 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13066 if (j + len > block_end || len != 4 ||
13067 memcmp(&vpd_data[j], "1028", 4))
13068 goto partno;
13069
13070 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13071 PCI_VPD_RO_KEYWORD_VENDOR0);
13072 if (j < 0)
13073 goto partno;
13074
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)
13079 goto partno;
13080
13081 memcpy(tp->fw_ver, &vpd_data[j], len);
13082 strncat(tp->fw_ver, " bc ", TG3_NVM_VPD_LEN - len - 1);
13083 }
13084
13085partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000013086 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13087 PCI_VPD_RO_KEYWORD_PARTNO);
13088 if (i < 0)
13089 goto out_not_found;
13090
13091 len = pci_vpd_info_field_size(&vpd_data[i]);
13092
13093 i += PCI_VPD_INFO_FLD_HDR_SIZE;
13094 if (len > TG3_BPN_SIZE ||
13095 (len + i) > TG3_NVM_VPD_LEN)
13096 goto out_not_found;
13097
13098 memcpy(tp->board_part_number, &vpd_data[i], len);
13099
Linus Torvalds1da177e2005-04-16 15:20:36 -070013100out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013101 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000013102 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013103 return;
13104
13105out_no_vpd:
Matt Carlson37a949c2010-09-30 10:34:33 +000013106 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
13107 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717)
13108 strcpy(tp->board_part_number, "BCM5717");
13109 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
13110 strcpy(tp->board_part_number, "BCM5718");
13111 else
13112 goto nomatch;
13113 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
13114 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
13115 strcpy(tp->board_part_number, "BCM57780");
13116 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
13117 strcpy(tp->board_part_number, "BCM57760");
13118 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
13119 strcpy(tp->board_part_number, "BCM57790");
13120 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
13121 strcpy(tp->board_part_number, "BCM57788");
13122 else
13123 goto nomatch;
13124 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
13125 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
13126 strcpy(tp->board_part_number, "BCM57761");
13127 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
13128 strcpy(tp->board_part_number, "BCM57765");
13129 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
13130 strcpy(tp->board_part_number, "BCM57781");
13131 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
13132 strcpy(tp->board_part_number, "BCM57785");
13133 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
13134 strcpy(tp->board_part_number, "BCM57791");
13135 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13136 strcpy(tp->board_part_number, "BCM57795");
13137 else
13138 goto nomatch;
13139 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070013140 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000013141 } else {
13142nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070013143 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000013144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013145}
13146
Matt Carlson9c8a6202007-10-21 16:16:08 -070013147static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
13148{
13149 u32 val;
13150
Matt Carlsone4f34112009-02-25 14:25:00 +000013151 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013152 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013153 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013154 val != 0)
13155 return 0;
13156
13157 return 1;
13158}
13159
Matt Carlsonacd9c112009-02-25 14:26:33 +000013160static void __devinit tg3_read_bc_ver(struct tg3 *tp)
13161{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013162 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000013163 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013164 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013165
13166 if (tg3_nvram_read(tp, 0xc, &offset) ||
13167 tg3_nvram_read(tp, 0x4, &start))
13168 return;
13169
13170 offset = tg3_nvram_logical_addr(tp, offset);
13171
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013172 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013173 return;
13174
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013175 if ((val & 0xfc000000) == 0x0c000000) {
13176 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013177 return;
13178
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013179 if (val == 0)
13180 newver = true;
13181 }
13182
Matt Carlson75f99362010-04-05 10:19:24 +000013183 dst_off = strlen(tp->fw_ver);
13184
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013185 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000013186 if (TG3_VER_SIZE - dst_off < 16 ||
13187 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013188 return;
13189
13190 offset = offset + ver_offset - start;
13191 for (i = 0; i < 16; i += 4) {
13192 __be32 v;
13193 if (tg3_nvram_read_be32(tp, offset + i, &v))
13194 return;
13195
Matt Carlson75f99362010-04-05 10:19:24 +000013196 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013197 }
13198 } else {
13199 u32 major, minor;
13200
13201 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
13202 return;
13203
13204 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
13205 TG3_NVM_BCVER_MAJSFT;
13206 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000013207 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
13208 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013209 }
13210}
13211
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013212static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
13213{
13214 u32 val, major, minor;
13215
13216 /* Use native endian representation */
13217 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
13218 return;
13219
13220 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
13221 TG3_NVM_HWSB_CFG1_MAJSFT;
13222 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
13223 TG3_NVM_HWSB_CFG1_MINSFT;
13224
13225 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
13226}
13227
Matt Carlsondfe00d72008-11-21 17:19:41 -080013228static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
13229{
13230 u32 offset, major, minor, build;
13231
Matt Carlson75f99362010-04-05 10:19:24 +000013232 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013233
13234 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
13235 return;
13236
13237 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
13238 case TG3_EEPROM_SB_REVISION_0:
13239 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
13240 break;
13241 case TG3_EEPROM_SB_REVISION_2:
13242 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
13243 break;
13244 case TG3_EEPROM_SB_REVISION_3:
13245 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
13246 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000013247 case TG3_EEPROM_SB_REVISION_4:
13248 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
13249 break;
13250 case TG3_EEPROM_SB_REVISION_5:
13251 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
13252 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000013253 case TG3_EEPROM_SB_REVISION_6:
13254 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
13255 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013256 default:
13257 return;
13258 }
13259
Matt Carlsone4f34112009-02-25 14:25:00 +000013260 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080013261 return;
13262
13263 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
13264 TG3_EEPROM_SB_EDH_BLD_SHFT;
13265 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
13266 TG3_EEPROM_SB_EDH_MAJ_SHFT;
13267 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
13268
13269 if (minor > 99 || build > 26)
13270 return;
13271
Matt Carlson75f99362010-04-05 10:19:24 +000013272 offset = strlen(tp->fw_ver);
13273 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
13274 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013275
13276 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000013277 offset = strlen(tp->fw_ver);
13278 if (offset < TG3_VER_SIZE - 1)
13279 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013280 }
13281}
13282
Matt Carlsonacd9c112009-02-25 14:26:33 +000013283static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080013284{
13285 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013286 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070013287
13288 for (offset = TG3_NVM_DIR_START;
13289 offset < TG3_NVM_DIR_END;
13290 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000013291 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013292 return;
13293
13294 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
13295 break;
13296 }
13297
13298 if (offset == TG3_NVM_DIR_END)
13299 return;
13300
13301 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
13302 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000013303 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013304 return;
13305
Matt Carlsone4f34112009-02-25 14:25:00 +000013306 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013307 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013308 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013309 return;
13310
13311 offset += val - start;
13312
Matt Carlsonacd9c112009-02-25 14:26:33 +000013313 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013314
Matt Carlsonacd9c112009-02-25 14:26:33 +000013315 tp->fw_ver[vlen++] = ',';
13316 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070013317
13318 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000013319 __be32 v;
13320 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013321 return;
13322
Al Virob9fc7dc2007-12-17 22:59:57 -080013323 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013324
Matt Carlsonacd9c112009-02-25 14:26:33 +000013325 if (vlen > TG3_VER_SIZE - sizeof(v)) {
13326 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013327 break;
13328 }
13329
Matt Carlsonacd9c112009-02-25 14:26:33 +000013330 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
13331 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013332 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000013333}
13334
Matt Carlson7fd76442009-02-25 14:27:20 +000013335static void __devinit tg3_read_dash_ver(struct tg3 *tp)
13336{
13337 int vlen;
13338 u32 apedata;
Matt Carlsonecc79642010-08-02 11:26:01 +000013339 char *fwtype;
Matt Carlson7fd76442009-02-25 14:27:20 +000013340
13341 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) ||
13342 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
13343 return;
13344
13345 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
13346 if (apedata != APE_SEG_SIG_MAGIC)
13347 return;
13348
13349 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
13350 if (!(apedata & APE_FW_STATUS_READY))
13351 return;
13352
13353 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
13354
Matt Carlsondc6d0742010-09-15 08:59:55 +000013355 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI) {
13356 tp->tg3_flags3 |= TG3_FLG3_APE_HAS_NCSI;
Matt Carlsonecc79642010-08-02 11:26:01 +000013357 fwtype = "NCSI";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013358 } else {
Matt Carlsonecc79642010-08-02 11:26:01 +000013359 fwtype = "DASH";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013360 }
Matt Carlsonecc79642010-08-02 11:26:01 +000013361
Matt Carlson7fd76442009-02-25 14:27:20 +000013362 vlen = strlen(tp->fw_ver);
13363
Matt Carlsonecc79642010-08-02 11:26:01 +000013364 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
13365 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000013366 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
13367 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
13368 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
13369 (apedata & APE_FW_VERSION_BLDMSK));
13370}
13371
Matt Carlsonacd9c112009-02-25 14:26:33 +000013372static void __devinit tg3_read_fw_ver(struct tg3 *tp)
13373{
13374 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000013375 bool vpd_vers = false;
13376
13377 if (tp->fw_ver[0] != 0)
13378 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013379
Matt Carlsondf259d82009-04-20 06:57:14 +000013380 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) {
Matt Carlson75f99362010-04-05 10:19:24 +000013381 strcat(tp->fw_ver, "sb");
Matt Carlsondf259d82009-04-20 06:57:14 +000013382 return;
13383 }
13384
Matt Carlsonacd9c112009-02-25 14:26:33 +000013385 if (tg3_nvram_read(tp, 0, &val))
13386 return;
13387
13388 if (val == TG3_EEPROM_MAGIC)
13389 tg3_read_bc_ver(tp);
13390 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
13391 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013392 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
13393 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013394 else
13395 return;
13396
13397 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
Matt Carlson75f99362010-04-05 10:19:24 +000013398 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) || vpd_vers)
13399 goto done;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013400
13401 tg3_read_mgmtfw_ver(tp);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013402
Matt Carlson75f99362010-04-05 10:19:24 +000013403done:
Matt Carlson9c8a6202007-10-21 16:16:08 -070013404 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080013405}
13406
Michael Chan7544b092007-05-05 13:08:32 -070013407static struct pci_dev * __devinit tg3_find_peer(struct tg3 *);
13408
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013409static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
13410{
Matt Carlsonde9f5232011-04-05 14:22:43 +000013411 if (tp->tg3_flags3 & TG3_FLG3_LRG_PROD_RING_CAP)
13412 return TG3_RX_RET_MAX_SIZE_5717;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013413 else if ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) &&
13414 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013415 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013416 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000013417 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013418}
13419
Matt Carlson41434702011-03-09 16:58:22 +000013420static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080013421 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
13422 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
13423 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
13424 { },
13425};
13426
Linus Torvalds1da177e2005-04-16 15:20:36 -070013427static int __devinit tg3_get_invariants(struct tg3 *tp)
13428{
Linus Torvalds1da177e2005-04-16 15:20:36 -070013429 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013430 u32 pci_state_reg, grc_misc_cfg;
13431 u32 val;
13432 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013433 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013434
Linus Torvalds1da177e2005-04-16 15:20:36 -070013435 /* Force memory write invalidate off. If we leave it on,
13436 * then on 5700_BX chips we have to enable a workaround.
13437 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
13438 * to match the cacheline size. The Broadcom driver have this
13439 * workaround but turns MWI off all the times so never uses
13440 * it. This seems to suggest that the workaround is insufficient.
13441 */
13442 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13443 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
13444 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13445
13446 /* It is absolutely critical that TG3PCI_MISC_HOST_CTRL
13447 * has the register indirect write enable bit set before
13448 * we try to access any of the MMIO registers. It is also
13449 * critical that the PCI-X hw workaround situation is decided
13450 * before that as well.
13451 */
13452 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13453 &misc_ctrl_reg);
13454
13455 tp->pci_chip_rev_id = (misc_ctrl_reg >>
13456 MISC_HOST_CTRL_CHIPREV_SHIFT);
Matt Carlson795d01c2007-10-07 23:28:17 -070013457 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
13458 u32 prod_id_asic_rev;
13459
Matt Carlson5001e2f2009-11-13 13:03:51 +000013460 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
13461 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013462 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
13463 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013464 pci_read_config_dword(tp->pdev,
13465 TG3PCI_GEN2_PRODID_ASICREV,
13466 &prod_id_asic_rev);
Matt Carlsonb703df62009-12-03 08:36:21 +000013467 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
13468 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
13469 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
13470 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
13471 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
13472 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13473 pci_read_config_dword(tp->pdev,
13474 TG3PCI_GEN15_PRODID_ASICREV,
13475 &prod_id_asic_rev);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013476 else
13477 pci_read_config_dword(tp->pdev, TG3PCI_PRODID_ASICREV,
13478 &prod_id_asic_rev);
13479
Matt Carlson321d32a2008-11-21 17:22:19 -080013480 tp->pci_chip_rev_id = prod_id_asic_rev;
Matt Carlson795d01c2007-10-07 23:28:17 -070013481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013482
Michael Chanff645be2005-04-21 17:09:53 -070013483 /* Wrong chip ID in 5752 A0. This code can be removed later
13484 * as A0 is not in production.
13485 */
13486 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
13487 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
13488
Michael Chan68929142005-08-09 20:17:14 -070013489 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
13490 * we need to disable memory and use config. cycles
13491 * only to access all registers. The 5702/03 chips
13492 * can mistakenly decode the special cycles from the
13493 * ICH chipsets as memory write cycles, causing corruption
13494 * of register and memory space. Only certain ICH bridges
13495 * will drive special cycles with non-zero data during the
13496 * address phase which can fall within the 5703's address
13497 * range. This is not an ICH bug as the PCI spec allows
13498 * non-zero address during special cycles. However, only
13499 * these ICH bridges are known to drive non-zero addresses
13500 * during special cycles.
13501 *
13502 * Since special cycles do not cross PCI bridges, we only
13503 * enable this workaround if the 5703 is on the secondary
13504 * bus of these ICH bridges.
13505 */
13506 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
13507 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
13508 static struct tg3_dev_id {
13509 u32 vendor;
13510 u32 device;
13511 u32 rev;
13512 } ich_chipsets[] = {
13513 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
13514 PCI_ANY_ID },
13515 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
13516 PCI_ANY_ID },
13517 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
13518 0xa },
13519 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
13520 PCI_ANY_ID },
13521 { },
13522 };
13523 struct tg3_dev_id *pci_id = &ich_chipsets[0];
13524 struct pci_dev *bridge = NULL;
13525
13526 while (pci_id->vendor != 0) {
13527 bridge = pci_get_device(pci_id->vendor, pci_id->device,
13528 bridge);
13529 if (!bridge) {
13530 pci_id++;
13531 continue;
13532 }
13533 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070013534 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070013535 continue;
13536 }
13537 if (bridge->subordinate &&
13538 (bridge->subordinate->number ==
13539 tp->pdev->bus->number)) {
13540
13541 tp->tg3_flags2 |= TG3_FLG2_ICH_WORKAROUND;
13542 pci_dev_put(bridge);
13543 break;
13544 }
13545 }
13546 }
13547
Matt Carlson41588ba2008-04-19 18:12:33 -070013548 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
13549 static struct tg3_dev_id {
13550 u32 vendor;
13551 u32 device;
13552 } bridge_chipsets[] = {
13553 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
13554 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
13555 { },
13556 };
13557 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
13558 struct pci_dev *bridge = NULL;
13559
13560 while (pci_id->vendor != 0) {
13561 bridge = pci_get_device(pci_id->vendor,
13562 pci_id->device,
13563 bridge);
13564 if (!bridge) {
13565 pci_id++;
13566 continue;
13567 }
13568 if (bridge->subordinate &&
13569 (bridge->subordinate->number <=
13570 tp->pdev->bus->number) &&
13571 (bridge->subordinate->subordinate >=
13572 tp->pdev->bus->number)) {
13573 tp->tg3_flags3 |= TG3_FLG3_5701_DMA_BUG;
13574 pci_dev_put(bridge);
13575 break;
13576 }
13577 }
13578 }
13579
Michael Chan4a29cc22006-03-19 13:21:12 -080013580 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
13581 * DMA addresses > 40-bit. This bridge may have other additional
13582 * 57xx devices behind it in some 4-port NIC designs for example.
13583 * Any tg3 device found behind the bridge will also need the 40-bit
13584 * DMA workaround.
13585 */
Michael Chana4e2b342005-10-26 15:46:52 -070013586 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
13587 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
13588 tp->tg3_flags2 |= TG3_FLG2_5780_CLASS;
Michael Chan4a29cc22006-03-19 13:21:12 -080013589 tp->tg3_flags |= TG3_FLAG_40BIT_DMA_BUG;
Michael Chan4cf78e42005-07-25 12:29:19 -070013590 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a5882010-04-05 10:19:28 +000013591 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080013592 struct pci_dev *bridge = NULL;
13593
13594 do {
13595 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
13596 PCI_DEVICE_ID_SERVERWORKS_EPB,
13597 bridge);
13598 if (bridge && bridge->subordinate &&
13599 (bridge->subordinate->number <=
13600 tp->pdev->bus->number) &&
13601 (bridge->subordinate->subordinate >=
13602 tp->pdev->bus->number)) {
13603 tp->tg3_flags |= TG3_FLAG_40BIT_DMA_BUG;
13604 pci_dev_put(bridge);
13605 break;
13606 }
13607 } while (bridge);
13608 }
Michael Chan4cf78e42005-07-25 12:29:19 -070013609
Linus Torvalds1da177e2005-04-16 15:20:36 -070013610 /* Initialize misc host control in PCI block. */
13611 tp->misc_host_ctrl |= (misc_ctrl_reg &
13612 MISC_HOST_CTRL_CHIPREV);
13613 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13614 tp->misc_host_ctrl);
13615
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013616 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
13617 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013618 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13619 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Michael Chan7544b092007-05-05 13:08:32 -070013620 tp->pdev_peer = tg3_find_peer(tp);
13621
Matt Carlsonc885e822010-08-02 11:25:57 +000013622 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013623 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13624 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Matt Carlson0a58d662011-04-05 14:22:45 +000013625 tp->tg3_flags3 |= TG3_FLG3_5717_PLUS;
13626
13627 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 ||
13628 (tp->tg3_flags3 & TG3_FLG3_5717_PLUS))
Matt Carlson1407deb2011-04-05 14:22:44 +000013629 tp->tg3_flags3 |= TG3_FLG3_57765_PLUS;
Matt Carlsonc885e822010-08-02 11:25:57 +000013630
Matt Carlson321d32a2008-11-21 17:22:19 -080013631 /* Intentionally exclude ASIC_REV_5906 */
13632 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Michael Chand9ab5ad2006-03-20 22:27:35 -080013633 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013634 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013635 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013636 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013637 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlson1407deb2011-04-05 14:22:44 +000013638 (tp->tg3_flags3 & TG3_FLG3_57765_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080013639 tp->tg3_flags3 |= TG3_FLG3_5755_PLUS;
13640
13641 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13642 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Michael Chanb5d37722006-09-27 16:06:21 -070013643 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013644 (tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
Michael Chana4e2b342005-10-26 15:46:52 -070013645 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
John W. Linville6708e5c2005-04-21 17:00:52 -070013646 tp->tg3_flags2 |= TG3_FLG2_5750_PLUS;
13647
Matt Carlsoneb07a942011-04-20 07:57:36 +000013648
John W. Linville1b440c562005-04-21 17:03:18 -070013649 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) ||
13650 (tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
13651 tp->tg3_flags2 |= TG3_FLG2_5705_PLUS;
13652
Matt Carlson027455a2008-12-21 20:19:30 -080013653 /* 5700 B0 chips do not support checksumming correctly due
13654 * to hardware bugs.
13655 */
Michał Mirosławdc668912011-04-07 03:35:07 +000013656 if (tp->pci_chip_rev_id != CHIPREV_ID_5700_B0) {
13657 u32 features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
Eric Dumazet7fe876a2010-07-08 06:14:55 +000013658
Matt Carlson027455a2008-12-21 20:19:30 -080013659 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Eric Dumazet7fe876a2010-07-08 06:14:55 +000013660 features |= NETIF_F_IPV6_CSUM;
13661 tp->dev->features |= features;
Michał Mirosławdc668912011-04-07 03:35:07 +000013662 tp->dev->hw_features |= features;
13663 tp->dev->vlan_features |= features;
Matt Carlson027455a2008-12-21 20:19:30 -080013664 }
13665
Matt Carlson507399f2009-11-13 13:03:37 +000013666 /* Determine TSO capabilities */
Matt Carlson2866d952011-02-10 20:06:46 -080013667 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlson4d163b72011-01-25 15:58:48 +000013668 ; /* Do nothing. HW bug. */
Matt Carlson1407deb2011-04-05 14:22:44 +000013669 else if (tp->tg3_flags3 & TG3_FLG3_57765_PLUS)
Matt Carlsone849cdc2009-11-13 13:03:38 +000013670 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_3;
13671 else if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
13672 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlson507399f2009-11-13 13:03:37 +000013673 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_2;
13674 else if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
13675 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_1 | TG3_FLG2_TSO_BUG;
13676 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
13677 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
13678 tp->tg3_flags2 &= ~TG3_FLG2_TSO_BUG;
13679 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13680 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13681 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
13682 tp->tg3_flags2 |= TG3_FLG2_TSO_BUG;
13683 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
13684 tp->fw_needed = FIRMWARE_TG3TSO5;
13685 else
13686 tp->fw_needed = FIRMWARE_TG3TSO;
13687 }
13688
13689 tp->irq_max = 1;
13690
Michael Chan5a6f3072006-03-20 22:28:05 -080013691 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
Michael Chan7544b092007-05-05 13:08:32 -070013692 tp->tg3_flags |= TG3_FLAG_SUPPORT_MSI;
13693 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
13694 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
13695 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
13696 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
13697 tp->pdev_peer == tp->pdev))
13698 tp->tg3_flags &= ~TG3_FLAG_SUPPORT_MSI;
13699
Matt Carlson321d32a2008-11-21 17:22:19 -080013700 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070013701 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanfcfa0a32006-03-20 22:28:41 -080013702 tp->tg3_flags2 |= TG3_FLG2_1SHOT_MSI;
Michael Chan52c0fd82006-06-29 20:15:54 -070013703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013704
Matt Carlson1407deb2011-04-05 14:22:44 +000013705 if (tp->tg3_flags3 & TG3_FLG3_57765_PLUS) {
Matt Carlson507399f2009-11-13 13:03:37 +000013706 tp->tg3_flags |= TG3_FLAG_SUPPORT_MSIX;
13707 tp->irq_max = TG3_IRQ_MAX_VECS;
13708 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013709 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000013710
Matt Carlson615774f2009-11-13 13:03:39 +000013711 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsona50d0792010-06-05 17:24:37 +000013712 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlson615774f2009-11-13 13:03:39 +000013713 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
13714 tp->tg3_flags3 |= TG3_FLG3_SHORT_DMA_BUG;
13715 else if (!(tp->tg3_flags3 & TG3_FLG3_5755_PLUS)) {
13716 tp->tg3_flags3 |= TG3_FLG3_4G_DMA_BNDRY_BUG;
13717 tp->tg3_flags3 |= TG3_FLG3_40BIT_DMA_LIMIT_BUG;
Matt Carlson0e1406d2009-11-02 12:33:33 +000013718 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013719
Matt Carlson0a58d662011-04-05 14:22:45 +000013720 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)
Matt Carlsonde9f5232011-04-05 14:22:43 +000013721 tp->tg3_flags3 |= TG3_FLG3_LRG_PROD_RING_CAP;
13722
Matt Carlson1407deb2011-04-05 14:22:44 +000013723 if ((tp->tg3_flags3 & TG3_FLG3_57765_PLUS) &&
Matt Carlson2866d952011-02-10 20:06:46 -080013724 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5719)
Matt Carlsonb703df62009-12-03 08:36:21 +000013725 tp->tg3_flags3 |= TG3_FLG3_USE_JUMBO_BDFLAG;
13726
Matt Carlsonf51f3562008-05-25 23:45:08 -070013727 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
Matt Carlsonc6cdf432010-04-05 10:19:26 +000013728 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
13729 (tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG))
Matt Carlson8f666b02009-08-28 13:58:24 +000013730 tp->tg3_flags |= TG3_FLAG_JUMBO_CAPABLE;
Michael Chan0f893dc2005-07-25 12:30:38 -070013731
Matt Carlson52f44902008-11-21 17:17:04 -080013732 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
13733 &pci_state_reg);
13734
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013735 tp->pcie_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_EXP);
13736 if (tp->pcie_cap != 0) {
13737 u16 lnkctl;
13738
Linus Torvalds1da177e2005-04-16 15:20:36 -070013739 tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS;
Matt Carlson5f5c51e2007-11-12 21:19:37 -080013740
Matt Carlsoncf790032010-11-24 08:31:48 +000013741 tp->pcie_readrq = 4096;
Matt Carlsond78b59f2011-04-05 14:22:46 +000013742 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13743 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Matt Carlsonb4495ed2011-01-25 15:58:47 +000013744 tp->pcie_readrq = 2048;
Matt Carlsoncf790032010-11-24 08:31:48 +000013745
13746 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080013747
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013748 pci_read_config_word(tp->pdev,
13749 tp->pcie_cap + PCI_EXP_LNKCTL,
13750 &lnkctl);
13751 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
13752 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanc7835a72006-11-15 21:14:42 -080013753 tp->tg3_flags2 &= ~TG3_FLG2_HW_TSO_2;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013754 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013755 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000013756 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
13757 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013758 tp->tg3_flags3 |= TG3_FLG3_CLKREQ_BUG;
Matt Carlson614b05902010-01-20 16:58:02 +000013759 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
13760 tp->tg3_flags3 |= TG3_FLG3_L1PLLPD_EN;
Michael Chanc7835a72006-11-15 21:14:42 -080013761 }
Matt Carlson52f44902008-11-21 17:17:04 -080013762 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Matt Carlsonfcb389d2008-11-03 16:55:44 -080013763 tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS;
Matt Carlson52f44902008-11-21 17:17:04 -080013764 } else if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
13765 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
13766 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
13767 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000013768 dev_err(&tp->pdev->dev,
13769 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080013770 return -EIO;
13771 }
13772
13773 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
13774 tp->tg3_flags |= TG3_FLAG_PCIX_MODE;
13775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013776
Michael Chan399de502005-10-03 14:02:39 -070013777 /* If we have an AMD 762 or VIA K8T800 chipset, write
13778 * reordering to the mailbox registers done by the host
13779 * controller can cause major troubles. We read back from
13780 * every mailbox register write to force the writes to be
13781 * posted to the chip in order.
13782 */
Matt Carlson41434702011-03-09 16:58:22 +000013783 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Michael Chan399de502005-10-03 14:02:39 -070013784 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
13785 tp->tg3_flags |= TG3_FLAG_MBOX_WRITE_REORDER;
13786
Matt Carlson69fc4052008-12-21 20:19:57 -080013787 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
13788 &tp->pci_cacheline_sz);
13789 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13790 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013791 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
13792 tp->pci_lat_timer < 64) {
13793 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080013794 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13795 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013796 }
13797
Matt Carlson52f44902008-11-21 17:17:04 -080013798 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
13799 /* 5700 BX chips need to have their TX producer index
13800 * mailboxes written twice to workaround a bug.
13801 */
13802 tp->tg3_flags |= TG3_FLAG_TXD_MBOX_HWBUG;
Matt Carlson9974a352007-10-07 23:27:28 -070013803
Matt Carlson52f44902008-11-21 17:17:04 -080013804 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013805 *
13806 * The workaround is to use indirect register accesses
13807 * for all chip writes not to mailbox registers.
13808 */
Matt Carlson52f44902008-11-21 17:17:04 -080013809 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013810 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013811
13812 tp->tg3_flags |= TG3_FLAG_PCIX_TARGET_HWBUG;
13813
13814 /* The chip can have it's power management PCI config
13815 * space registers clobbered due to this bug.
13816 * So explicitly force the chip into D0 here.
13817 */
Matt Carlson9974a352007-10-07 23:27:28 -070013818 pci_read_config_dword(tp->pdev,
13819 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013820 &pm_reg);
13821 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
13822 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070013823 pci_write_config_dword(tp->pdev,
13824 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013825 pm_reg);
13826
13827 /* Also, force SERR#/PERR# in PCI command. */
13828 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13829 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
13830 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13831 }
13832 }
13833
Linus Torvalds1da177e2005-04-16 15:20:36 -070013834 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
13835 tp->tg3_flags |= TG3_FLAG_PCI_HIGH_SPEED;
13836 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
13837 tp->tg3_flags |= TG3_FLAG_PCI_32BIT;
13838
13839 /* Chip-specific fixup from Broadcom driver */
13840 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
13841 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
13842 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
13843 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
13844 }
13845
Michael Chan1ee582d2005-08-09 20:16:46 -070013846 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070013847 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013848 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070013849 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070013850 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013851 tp->write32_tx_mbox = tg3_write32;
13852 tp->write32_rx_mbox = tg3_write32;
13853
13854 /* Various workaround register access methods */
13855 if (tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG)
13856 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013857 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
13858 ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
13859 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
13860 /*
13861 * Back to back register writes can cause problems on these
13862 * chips, the workaround is to read back all reg writes
13863 * except those to mailbox regs.
13864 *
13865 * See tg3_write_indirect_reg32().
13866 */
Michael Chan1ee582d2005-08-09 20:16:46 -070013867 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013868 }
13869
Michael Chan1ee582d2005-08-09 20:16:46 -070013870 if ((tp->tg3_flags & TG3_FLAG_TXD_MBOX_HWBUG) ||
13871 (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)) {
13872 tp->write32_tx_mbox = tg3_write32_tx_mbox;
13873 if (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)
13874 tp->write32_rx_mbox = tg3_write_flush_reg32;
13875 }
Michael Chan20094932005-08-09 20:16:32 -070013876
Michael Chan68929142005-08-09 20:17:14 -070013877 if (tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND) {
13878 tp->read32 = tg3_read_indirect_reg32;
13879 tp->write32 = tg3_write_indirect_reg32;
13880 tp->read32_mbox = tg3_read_indirect_mbox;
13881 tp->write32_mbox = tg3_write_indirect_mbox;
13882 tp->write32_tx_mbox = tg3_write_indirect_mbox;
13883 tp->write32_rx_mbox = tg3_write_indirect_mbox;
13884
13885 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070013886 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070013887
13888 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13889 pci_cmd &= ~PCI_COMMAND_MEMORY;
13890 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13891 }
Michael Chanb5d37722006-09-27 16:06:21 -070013892 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
13893 tp->read32_mbox = tg3_read32_mbox_5906;
13894 tp->write32_mbox = tg3_write32_mbox_5906;
13895 tp->write32_tx_mbox = tg3_write32_mbox_5906;
13896 tp->write32_rx_mbox = tg3_write32_mbox_5906;
13897 }
Michael Chan68929142005-08-09 20:17:14 -070013898
Michael Chanbbadf502006-04-06 21:46:34 -070013899 if (tp->write32 == tg3_write_indirect_reg32 ||
13900 ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) &&
13901 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070013902 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Michael Chanbbadf502006-04-06 21:46:34 -070013903 tp->tg3_flags |= TG3_FLAG_SRAM_USE_CONFIG;
13904
Michael Chan7d0c41e2005-04-21 17:06:20 -070013905 /* Get eeprom hw config before calling tg3_set_power_state().
Michael Chan9d26e212006-12-07 00:21:14 -080013906 * In particular, the TG3_FLG2_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070013907 * determined before calling tg3_set_power_state() so that
13908 * we know whether or not to switch out of Vaux power.
13909 * When the flag is set, it means that GPIO1 is used for eeprom
13910 * write protect and also implies that it is a LOM where GPIOs
13911 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013912 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070013913 tg3_get_eeprom_hw_cfg(tp);
13914
Matt Carlson0d3031d2007-10-10 18:02:43 -070013915 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
13916 /* Allow reads and writes to the
13917 * APE register and memory space.
13918 */
13919 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc2010-06-05 17:24:30 +000013920 PCISTATE_ALLOW_APE_SHMEM_WR |
13921 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070013922 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
13923 pci_state_reg);
13924 }
13925
Matt Carlson9936bcf2007-10-10 18:03:07 -070013926 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013927 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013928 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013929 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlson1407deb2011-04-05 14:22:44 +000013930 (tp->tg3_flags3 & TG3_FLG3_57765_PLUS))
Matt Carlsond30cdd22007-10-07 23:28:35 -070013931 tp->tg3_flags |= TG3_FLAG_CPMU_PRESENT;
13932
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000013933 /* Set up tp->grc_local_ctrl before calling tg_power_up().
Michael Chan314fba32005-04-21 17:07:04 -070013934 * GPIO1 driven high will bring 5700's external PHY out of reset.
13935 * It is also used as eeprom write protect on LOMs.
13936 */
13937 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
13938 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
13939 (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT))
13940 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
13941 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070013942 /* Unused GPIO3 must be driven as output on 5752 because there
13943 * are no pull-up resistors on unused GPIO pins.
13944 */
13945 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
13946 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070013947
Matt Carlson321d32a2008-11-21 17:22:19 -080013948 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsoncb4ed1f2010-01-20 16:58:09 +000013949 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
13950 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Michael Chanaf36e6b2006-03-23 01:28:06 -080013951 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
13952
Matt Carlson8d519ab2009-04-20 06:58:01 +000013953 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
13954 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070013955 /* Turn off the debug UART. */
13956 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
13957 if (tp->tg3_flags2 & TG3_FLG2_IS_NIC)
13958 /* Keep VMain power. */
13959 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
13960 GRC_LCLCTRL_GPIO_OUTPUT0;
13961 }
13962
Linus Torvalds1da177e2005-04-16 15:20:36 -070013963 /* Force the chip into D0. */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000013964 err = tg3_power_up(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013965 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000013966 dev_err(&tp->pdev->dev, "Transition to D0 failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070013967 return err;
13968 }
13969
Linus Torvalds1da177e2005-04-16 15:20:36 -070013970 /* Derive initial jumbo mode from MTU assigned in
13971 * ether_setup() via the alloc_etherdev() call
13972 */
Michael Chan0f893dc2005-07-25 12:30:38 -070013973 if (tp->dev->mtu > ETH_DATA_LEN &&
Michael Chana4e2b342005-10-26 15:46:52 -070013974 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Michael Chan0f893dc2005-07-25 12:30:38 -070013975 tp->tg3_flags |= TG3_FLAG_JUMBO_RING_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013976
13977 /* Determine WakeOnLan speed to use. */
13978 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13979 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
13980 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
13981 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
13982 tp->tg3_flags &= ~(TG3_FLAG_WOL_SPEED_100MB);
13983 } else {
13984 tp->tg3_flags |= TG3_FLAG_WOL_SPEED_100MB;
13985 }
13986
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013987 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013988 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013989
Linus Torvalds1da177e2005-04-16 15:20:36 -070013990 /* A few boards don't want Ethernet@WireSpeed phy feature */
13991 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
13992 ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
13993 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070013994 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013995 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
13996 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
13997 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013998
13999 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
14000 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014001 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014002 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014003 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014004
Matt Carlson321d32a2008-11-21 17:22:19 -080014005 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014006 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080014007 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014008 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
Matt Carlson1407deb2011-04-05 14:22:44 +000014009 !(tp->tg3_flags3 & TG3_FLG3_57765_PLUS)) {
Michael Chanc424cb22006-04-29 18:56:34 -070014010 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070014011 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070014012 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
14013 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080014014 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
14015 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014016 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080014017 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014018 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080014019 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014020 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070014021 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014022
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014023 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
14024 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
14025 tp->phy_otp = tg3_read_otp_phycfg(tp);
14026 if (tp->phy_otp == 0)
14027 tp->phy_otp = TG3_OTP_DEFAULT;
14028 }
14029
Matt Carlsonf51f3562008-05-25 23:45:08 -070014030 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT)
Matt Carlson8ef21422008-05-02 16:47:53 -070014031 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
14032 else
14033 tp->mi_mode = MAC_MI_MODE_BASE;
14034
Linus Torvalds1da177e2005-04-16 15:20:36 -070014035 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014036 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
14037 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
14038 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
14039
Matt Carlson4d958472011-04-20 07:57:35 +000014040 /* Set these bits to enable statistics workaround. */
14041 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14042 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
14043 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0) {
14044 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
14045 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
14046 }
14047
Matt Carlson321d32a2008-11-21 17:22:19 -080014048 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
14049 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson57e69832008-05-25 23:48:31 -070014050 tp->tg3_flags3 |= TG3_FLG3_USE_PHYLIB;
14051
Matt Carlson158d7ab2008-05-29 01:37:54 -070014052 err = tg3_mdio_init(tp);
14053 if (err)
14054 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014055
14056 /* Initialize data/descriptor byte/word swapping. */
14057 val = tr32(GRC_MODE);
Matt Carlsonf2096f92011-04-05 14:22:48 +000014058 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
14059 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
14060 GRC_MODE_WORD_SWAP_B2HRX_DATA |
14061 GRC_MODE_B2HRX_ENABLE |
14062 GRC_MODE_HTX2B_ENABLE |
14063 GRC_MODE_HOST_STACKUP);
14064 else
14065 val &= GRC_MODE_HOST_STACKUP;
14066
Linus Torvalds1da177e2005-04-16 15:20:36 -070014067 tw32(GRC_MODE, val | tp->grc_mode);
14068
14069 tg3_switch_clocks(tp);
14070
14071 /* Clear this out for sanity. */
14072 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
14073
14074 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14075 &pci_state_reg);
14076 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
14077 (tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG) == 0) {
14078 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
14079
14080 if (chiprevid == CHIPREV_ID_5701_A0 ||
14081 chiprevid == CHIPREV_ID_5701_B0 ||
14082 chiprevid == CHIPREV_ID_5701_B2 ||
14083 chiprevid == CHIPREV_ID_5701_B5) {
14084 void __iomem *sram_base;
14085
14086 /* Write some dummy words into the SRAM status block
14087 * area, see if it reads back correctly. If the return
14088 * value is bad, force enable the PCIX workaround.
14089 */
14090 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
14091
14092 writel(0x00000000, sram_base);
14093 writel(0x00000000, sram_base + 4);
14094 writel(0xffffffff, sram_base + 4);
14095 if (readl(sram_base) != 0x00000000)
14096 tp->tg3_flags |= TG3_FLAG_PCIX_TARGET_HWBUG;
14097 }
14098 }
14099
14100 udelay(50);
14101 tg3_nvram_init(tp);
14102
14103 grc_misc_cfg = tr32(GRC_MISC_CFG);
14104 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
14105
Linus Torvalds1da177e2005-04-16 15:20:36 -070014106 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14107 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
14108 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
14109 tp->tg3_flags2 |= TG3_FLG2_IS_5788;
14110
David S. Millerfac9b832005-05-18 22:46:34 -070014111 if (!(tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
14112 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700))
14113 tp->tg3_flags |= TG3_FLAG_TAGGED_STATUS;
14114 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) {
14115 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
14116 HOSTCC_MODE_CLRTICK_TXBD);
14117
14118 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
14119 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
14120 tp->misc_host_ctrl);
14121 }
14122
Matt Carlson3bda1252008-08-15 14:08:22 -070014123 /* Preserve the APE MAC_MODE bits */
14124 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
Matt Carlsond2394e6b2010-11-24 08:31:47 +000014125 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070014126 else
14127 tp->mac_mode = TG3_DEF_MAC_MODE;
14128
Linus Torvalds1da177e2005-04-16 15:20:36 -070014129 /* these are limited to 10/100 only */
14130 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14131 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
14132 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14133 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14134 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
14135 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
14136 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
14137 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14138 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
Michael Chan676917d2006-12-07 00:20:22 -080014139 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
14140 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014141 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
Matt Carlsond1101142010-02-17 15:16:55 +000014142 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
14143 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014144 (tp->phy_flags & TG3_PHYFLG_IS_FET))
14145 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014146
14147 err = tg3_phy_probe(tp);
14148 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014149 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014150 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014151 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014152 }
14153
Matt Carlson184b8902010-04-05 10:19:25 +000014154 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080014155 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014156
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014157 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
14158 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014159 } else {
14160 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014161 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014162 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014163 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014164 }
14165
14166 /* 5700 {AX,BX} chips have a broken status block link
14167 * change bit implementation, so we must use the
14168 * status register in those cases.
14169 */
14170 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
14171 tp->tg3_flags |= TG3_FLAG_USE_LINKCHG_REG;
14172 else
14173 tp->tg3_flags &= ~TG3_FLAG_USE_LINKCHG_REG;
14174
14175 /* The led_ctrl is set during tg3_phy_probe, here we might
14176 * have to force the link status polling mechanism based
14177 * upon subsystem IDs.
14178 */
14179 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070014180 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014181 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
14182 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
14183 tp->tg3_flags |= TG3_FLAG_USE_LINKCHG_REG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014184 }
14185
14186 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014187 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014188 tp->tg3_flags |= TG3_FLAG_POLL_SERDES;
14189 else
14190 tp->tg3_flags &= ~TG3_FLAG_POLL_SERDES;
14191
Matt Carlsonbf933c82011-01-25 15:58:49 +000014192 tp->rx_offset = NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014193 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014194 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsond2757fc2010-04-12 06:58:27 +000014195 (tp->tg3_flags & TG3_FLAG_PCIX_MODE) != 0) {
Matt Carlsonbf933c82011-01-25 15:58:49 +000014196 tp->rx_offset = 0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014197#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000014198 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014199#endif
14200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014201
Matt Carlson2c49a442010-09-30 10:34:35 +000014202 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
14203 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000014204 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
14205
Matt Carlson2c49a442010-09-30 10:34:35 +000014206 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070014207
14208 /* Increment the rx prod index on the rx std ring by at most
14209 * 8 for these chips to workaround hw errata.
14210 */
14211 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
14212 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
14213 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
14214 tp->rx_std_max_post = 8;
14215
Matt Carlson8ed5d972007-05-07 00:25:49 -070014216 if (tp->tg3_flags & TG3_FLAG_ASPM_WORKAROUND)
14217 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
14218 PCIE_PWR_MGMT_L1_THRESH_MSK;
14219
Linus Torvalds1da177e2005-04-16 15:20:36 -070014220 return err;
14221}
14222
David S. Miller49b6e95f2007-03-29 01:38:42 -070014223#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014224static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
14225{
14226 struct net_device *dev = tp->dev;
14227 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014228 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070014229 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014230 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014231
David S. Miller49b6e95f2007-03-29 01:38:42 -070014232 addr = of_get_property(dp, "local-mac-address", &len);
14233 if (addr && len == 6) {
14234 memcpy(dev->dev_addr, addr, 6);
14235 memcpy(dev->perm_addr, dev->dev_addr, 6);
14236 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014237 }
14238 return -ENODEV;
14239}
14240
14241static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
14242{
14243 struct net_device *dev = tp->dev;
14244
14245 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070014246 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014247 return 0;
14248}
14249#endif
14250
14251static int __devinit tg3_get_device_address(struct tg3 *tp)
14252{
14253 struct net_device *dev = tp->dev;
14254 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080014255 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014256
David S. Miller49b6e95f2007-03-29 01:38:42 -070014257#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014258 if (!tg3_get_macaddr_sparc(tp))
14259 return 0;
14260#endif
14261
14262 mac_offset = 0x7c;
David S. Millerf49639e2006-06-09 11:58:36 -070014263 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) ||
Michael Chana4e2b342005-10-26 15:46:52 -070014264 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014265 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
14266 mac_offset = 0xcc;
14267 if (tg3_nvram_lock(tp))
14268 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
14269 else
14270 tg3_nvram_unlock(tp);
Matt Carlson0a58d662011-04-05 14:22:45 +000014271 } else if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
Matt Carlsona50d0792010-06-05 17:24:37 +000014272 if (PCI_FUNC(tp->pdev->devfn) & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014273 mac_offset = 0xcc;
Matt Carlsona50d0792010-06-05 17:24:37 +000014274 if (PCI_FUNC(tp->pdev->devfn) > 1)
14275 mac_offset += 0x18c;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014276 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070014277 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014278
14279 /* First try to get it from MAC address mailbox. */
14280 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
14281 if ((hi >> 16) == 0x484b) {
14282 dev->dev_addr[0] = (hi >> 8) & 0xff;
14283 dev->dev_addr[1] = (hi >> 0) & 0xff;
14284
14285 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
14286 dev->dev_addr[2] = (lo >> 24) & 0xff;
14287 dev->dev_addr[3] = (lo >> 16) & 0xff;
14288 dev->dev_addr[4] = (lo >> 8) & 0xff;
14289 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014290
Michael Chan008652b2006-03-27 23:14:53 -080014291 /* Some old bootcode may report a 0 MAC address in SRAM */
14292 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
14293 }
14294 if (!addr_ok) {
14295 /* Next, try NVRAM. */
Matt Carlsondf259d82009-04-20 06:57:14 +000014296 if (!(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) &&
14297 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000014298 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070014299 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
14300 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080014301 }
14302 /* Finally just fetch it out of the MAC control regs. */
14303 else {
14304 hi = tr32(MAC_ADDR_0_HIGH);
14305 lo = tr32(MAC_ADDR_0_LOW);
14306
14307 dev->dev_addr[5] = lo & 0xff;
14308 dev->dev_addr[4] = (lo >> 8) & 0xff;
14309 dev->dev_addr[3] = (lo >> 16) & 0xff;
14310 dev->dev_addr[2] = (lo >> 24) & 0xff;
14311 dev->dev_addr[1] = hi & 0xff;
14312 dev->dev_addr[0] = (hi >> 8) & 0xff;
14313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014314 }
14315
14316 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070014317#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014318 if (!tg3_get_default_macaddr_sparc(tp))
14319 return 0;
14320#endif
14321 return -EINVAL;
14322 }
John W. Linville2ff43692005-09-12 14:44:20 -070014323 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014324 return 0;
14325}
14326
David S. Miller59e6b432005-05-18 22:50:10 -070014327#define BOUNDARY_SINGLE_CACHELINE 1
14328#define BOUNDARY_MULTI_CACHELINE 2
14329
14330static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
14331{
14332 int cacheline_size;
14333 u8 byte;
14334 int goal;
14335
14336 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
14337 if (byte == 0)
14338 cacheline_size = 1024;
14339 else
14340 cacheline_size = (int) byte * 4;
14341
14342 /* On 5703 and later chips, the boundary bits have no
14343 * effect.
14344 */
14345 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14346 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
14347 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
14348 goto out;
14349
14350#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
14351 goal = BOUNDARY_MULTI_CACHELINE;
14352#else
14353#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
14354 goal = BOUNDARY_SINGLE_CACHELINE;
14355#else
14356 goal = 0;
14357#endif
14358#endif
14359
Matt Carlson1407deb2011-04-05 14:22:44 +000014360 if (tp->tg3_flags3 & TG3_FLG3_57765_PLUS) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014361 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
14362 goto out;
14363 }
14364
David S. Miller59e6b432005-05-18 22:50:10 -070014365 if (!goal)
14366 goto out;
14367
14368 /* PCI controllers on most RISC systems tend to disconnect
14369 * when a device tries to burst across a cache-line boundary.
14370 * Therefore, letting tg3 do so just wastes PCI bandwidth.
14371 *
14372 * Unfortunately, for PCI-E there are only limited
14373 * write-side controls for this, and thus for reads
14374 * we will still get the disconnects. We'll also waste
14375 * these PCI cycles for both read and write for chips
14376 * other than 5700 and 5701 which do not implement the
14377 * boundary bits.
14378 */
14379 if ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) &&
14380 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)) {
14381 switch (cacheline_size) {
14382 case 16:
14383 case 32:
14384 case 64:
14385 case 128:
14386 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14387 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
14388 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
14389 } else {
14390 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14391 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14392 }
14393 break;
14394
14395 case 256:
14396 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
14397 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
14398 break;
14399
14400 default:
14401 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14402 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14403 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014404 }
David S. Miller59e6b432005-05-18 22:50:10 -070014405 } else if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
14406 switch (cacheline_size) {
14407 case 16:
14408 case 32:
14409 case 64:
14410 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14411 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14412 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
14413 break;
14414 }
14415 /* fallthrough */
14416 case 128:
14417 default:
14418 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14419 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
14420 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014421 }
David S. Miller59e6b432005-05-18 22:50:10 -070014422 } else {
14423 switch (cacheline_size) {
14424 case 16:
14425 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14426 val |= (DMA_RWCTRL_READ_BNDRY_16 |
14427 DMA_RWCTRL_WRITE_BNDRY_16);
14428 break;
14429 }
14430 /* fallthrough */
14431 case 32:
14432 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14433 val |= (DMA_RWCTRL_READ_BNDRY_32 |
14434 DMA_RWCTRL_WRITE_BNDRY_32);
14435 break;
14436 }
14437 /* fallthrough */
14438 case 64:
14439 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14440 val |= (DMA_RWCTRL_READ_BNDRY_64 |
14441 DMA_RWCTRL_WRITE_BNDRY_64);
14442 break;
14443 }
14444 /* fallthrough */
14445 case 128:
14446 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14447 val |= (DMA_RWCTRL_READ_BNDRY_128 |
14448 DMA_RWCTRL_WRITE_BNDRY_128);
14449 break;
14450 }
14451 /* fallthrough */
14452 case 256:
14453 val |= (DMA_RWCTRL_READ_BNDRY_256 |
14454 DMA_RWCTRL_WRITE_BNDRY_256);
14455 break;
14456 case 512:
14457 val |= (DMA_RWCTRL_READ_BNDRY_512 |
14458 DMA_RWCTRL_WRITE_BNDRY_512);
14459 break;
14460 case 1024:
14461 default:
14462 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
14463 DMA_RWCTRL_WRITE_BNDRY_1024);
14464 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014465 }
David S. Miller59e6b432005-05-18 22:50:10 -070014466 }
14467
14468out:
14469 return val;
14470}
14471
Linus Torvalds1da177e2005-04-16 15:20:36 -070014472static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
14473{
14474 struct tg3_internal_buffer_desc test_desc;
14475 u32 sram_dma_descs;
14476 int i, ret;
14477
14478 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
14479
14480 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
14481 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
14482 tw32(RDMAC_STATUS, 0);
14483 tw32(WDMAC_STATUS, 0);
14484
14485 tw32(BUFMGR_MODE, 0);
14486 tw32(FTQ_RESET, 0);
14487
14488 test_desc.addr_hi = ((u64) buf_dma) >> 32;
14489 test_desc.addr_lo = buf_dma & 0xffffffff;
14490 test_desc.nic_mbuf = 0x00002100;
14491 test_desc.len = size;
14492
14493 /*
14494 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
14495 * the *second* time the tg3 driver was getting loaded after an
14496 * initial scan.
14497 *
14498 * Broadcom tells me:
14499 * ...the DMA engine is connected to the GRC block and a DMA
14500 * reset may affect the GRC block in some unpredictable way...
14501 * The behavior of resets to individual blocks has not been tested.
14502 *
14503 * Broadcom noted the GRC reset will also reset all sub-components.
14504 */
14505 if (to_device) {
14506 test_desc.cqid_sqid = (13 << 8) | 2;
14507
14508 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
14509 udelay(40);
14510 } else {
14511 test_desc.cqid_sqid = (16 << 8) | 7;
14512
14513 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
14514 udelay(40);
14515 }
14516 test_desc.flags = 0x00000005;
14517
14518 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
14519 u32 val;
14520
14521 val = *(((u32 *)&test_desc) + i);
14522 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
14523 sram_dma_descs + (i * sizeof(u32)));
14524 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
14525 }
14526 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
14527
Matt Carlson859a5882010-04-05 10:19:28 +000014528 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014529 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a5882010-04-05 10:19:28 +000014530 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070014531 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014532
14533 ret = -ENODEV;
14534 for (i = 0; i < 40; i++) {
14535 u32 val;
14536
14537 if (to_device)
14538 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
14539 else
14540 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
14541 if ((val & 0xffff) == sram_dma_descs) {
14542 ret = 0;
14543 break;
14544 }
14545
14546 udelay(100);
14547 }
14548
14549 return ret;
14550}
14551
David S. Millerded73402005-05-23 13:59:47 -070014552#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070014553
Matt Carlson41434702011-03-09 16:58:22 +000014554static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080014555 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
14556 { },
14557};
14558
Linus Torvalds1da177e2005-04-16 15:20:36 -070014559static int __devinit tg3_test_dma(struct tg3 *tp)
14560{
14561 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070014562 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014563 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014564
Matt Carlson4bae65c2010-11-24 08:31:52 +000014565 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
14566 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014567 if (!buf) {
14568 ret = -ENOMEM;
14569 goto out_nofree;
14570 }
14571
14572 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
14573 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
14574
David S. Miller59e6b432005-05-18 22:50:10 -070014575 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014576
Matt Carlson1407deb2011-04-05 14:22:44 +000014577 if (tp->tg3_flags3 & TG3_FLG3_57765_PLUS)
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014578 goto out;
14579
Linus Torvalds1da177e2005-04-16 15:20:36 -070014580 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
14581 /* DMA read watermark not used on PCIE */
14582 tp->dma_rwctrl |= 0x00180000;
14583 } else if (!(tp->tg3_flags & TG3_FLAG_PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070014584 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
14585 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014586 tp->dma_rwctrl |= 0x003f0000;
14587 else
14588 tp->dma_rwctrl |= 0x003f000f;
14589 } else {
14590 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14591 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
14592 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080014593 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014594
Michael Chan4a29cc22006-03-19 13:21:12 -080014595 /* If the 5704 is behind the EPB bridge, we can
14596 * do the less restrictive ONE_DMA workaround for
14597 * better performance.
14598 */
14599 if ((tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG) &&
14600 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14601 tp->dma_rwctrl |= 0x8000;
14602 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014603 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
14604
Michael Chan49afdeb2007-02-13 12:17:03 -080014605 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
14606 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070014607 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080014608 tp->dma_rwctrl |=
14609 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
14610 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
14611 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070014612 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
14613 /* 5780 always in PCIX mode */
14614 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070014615 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
14616 /* 5714 always in PCIX mode */
14617 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014618 } else {
14619 tp->dma_rwctrl |= 0x001b000f;
14620 }
14621 }
14622
14623 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14624 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14625 tp->dma_rwctrl &= 0xfffffff0;
14626
14627 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14628 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
14629 /* Remove this if it causes problems for some boards. */
14630 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
14631
14632 /* On 5700/5701 chips, we need to set this bit.
14633 * Otherwise the chip will issue cacheline transactions
14634 * to streamable DMA memory with not all the byte
14635 * enables turned on. This is an error on several
14636 * RISC PCI controllers, in particular sparc64.
14637 *
14638 * On 5703/5704 chips, this bit has been reassigned
14639 * a different meaning. In particular, it is used
14640 * on those chips to enable a PCI-X workaround.
14641 */
14642 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
14643 }
14644
14645 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14646
14647#if 0
14648 /* Unneeded, already done by tg3_get_invariants. */
14649 tg3_switch_clocks(tp);
14650#endif
14651
Linus Torvalds1da177e2005-04-16 15:20:36 -070014652 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14653 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
14654 goto out;
14655
David S. Miller59e6b432005-05-18 22:50:10 -070014656 /* It is best to perform DMA test with maximum write burst size
14657 * to expose the 5700/5701 write DMA bug.
14658 */
14659 saved_dma_rwctrl = tp->dma_rwctrl;
14660 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14661 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14662
Linus Torvalds1da177e2005-04-16 15:20:36 -070014663 while (1) {
14664 u32 *p = buf, i;
14665
14666 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
14667 p[i] = i;
14668
14669 /* Send the buffer to the chip. */
14670 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
14671 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000014672 dev_err(&tp->pdev->dev,
14673 "%s: Buffer write failed. err = %d\n",
14674 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014675 break;
14676 }
14677
14678#if 0
14679 /* validate data reached card RAM correctly. */
14680 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14681 u32 val;
14682 tg3_read_mem(tp, 0x2100 + (i*4), &val);
14683 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000014684 dev_err(&tp->pdev->dev,
14685 "%s: Buffer corrupted on device! "
14686 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014687 /* ret = -ENODEV here? */
14688 }
14689 p[i] = 0;
14690 }
14691#endif
14692 /* Now read it back. */
14693 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
14694 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000014695 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
14696 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014697 break;
14698 }
14699
14700 /* Verify it. */
14701 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14702 if (p[i] == i)
14703 continue;
14704
David S. Miller59e6b432005-05-18 22:50:10 -070014705 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14706 DMA_RWCTRL_WRITE_BNDRY_16) {
14707 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014708 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
14709 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14710 break;
14711 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000014712 dev_err(&tp->pdev->dev,
14713 "%s: Buffer corrupted on read back! "
14714 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014715 ret = -ENODEV;
14716 goto out;
14717 }
14718 }
14719
14720 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
14721 /* Success. */
14722 ret = 0;
14723 break;
14724 }
14725 }
David S. Miller59e6b432005-05-18 22:50:10 -070014726 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14727 DMA_RWCTRL_WRITE_BNDRY_16) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014728
David S. Miller59e6b432005-05-18 22:50:10 -070014729 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070014730 * now look for chipsets that are known to expose the
14731 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070014732 */
Matt Carlson41434702011-03-09 16:58:22 +000014733 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014734 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14735 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a5882010-04-05 10:19:28 +000014736 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014737 /* Safe to use the calculated DMA boundary. */
14738 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a5882010-04-05 10:19:28 +000014739 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070014740
David S. Miller59e6b432005-05-18 22:50:10 -070014741 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014743
14744out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000014745 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014746out_nofree:
14747 return ret;
14748}
14749
Linus Torvalds1da177e2005-04-16 15:20:36 -070014750static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
14751{
Matt Carlson1407deb2011-04-05 14:22:44 +000014752 if (tp->tg3_flags3 & TG3_FLG3_57765_PLUS) {
Matt Carlson666bc832010-01-20 16:58:03 +000014753 tp->bufmgr_config.mbuf_read_dma_low_water =
14754 DEFAULT_MB_RDMA_LOW_WATER_5705;
14755 tp->bufmgr_config.mbuf_mac_rx_low_water =
14756 DEFAULT_MB_MACRX_LOW_WATER_57765;
14757 tp->bufmgr_config.mbuf_high_water =
14758 DEFAULT_MB_HIGH_WATER_57765;
14759
14760 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14761 DEFAULT_MB_RDMA_LOW_WATER_5705;
14762 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14763 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
14764 tp->bufmgr_config.mbuf_high_water_jumbo =
14765 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
14766 } else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
Michael Chanfdfec172005-07-25 12:31:48 -070014767 tp->bufmgr_config.mbuf_read_dma_low_water =
14768 DEFAULT_MB_RDMA_LOW_WATER_5705;
14769 tp->bufmgr_config.mbuf_mac_rx_low_water =
14770 DEFAULT_MB_MACRX_LOW_WATER_5705;
14771 tp->bufmgr_config.mbuf_high_water =
14772 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070014773 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
14774 tp->bufmgr_config.mbuf_mac_rx_low_water =
14775 DEFAULT_MB_MACRX_LOW_WATER_5906;
14776 tp->bufmgr_config.mbuf_high_water =
14777 DEFAULT_MB_HIGH_WATER_5906;
14778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014779
Michael Chanfdfec172005-07-25 12:31:48 -070014780 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14781 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
14782 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14783 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
14784 tp->bufmgr_config.mbuf_high_water_jumbo =
14785 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
14786 } else {
14787 tp->bufmgr_config.mbuf_read_dma_low_water =
14788 DEFAULT_MB_RDMA_LOW_WATER;
14789 tp->bufmgr_config.mbuf_mac_rx_low_water =
14790 DEFAULT_MB_MACRX_LOW_WATER;
14791 tp->bufmgr_config.mbuf_high_water =
14792 DEFAULT_MB_HIGH_WATER;
14793
14794 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14795 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
14796 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14797 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
14798 tp->bufmgr_config.mbuf_high_water_jumbo =
14799 DEFAULT_MB_HIGH_WATER_JUMBO;
14800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014801
14802 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
14803 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
14804}
14805
14806static char * __devinit tg3_phy_string(struct tg3 *tp)
14807{
Matt Carlson79eb6902010-02-17 15:17:03 +000014808 switch (tp->phy_id & TG3_PHY_ID_MASK) {
14809 case TG3_PHY_ID_BCM5400: return "5400";
14810 case TG3_PHY_ID_BCM5401: return "5401";
14811 case TG3_PHY_ID_BCM5411: return "5411";
14812 case TG3_PHY_ID_BCM5701: return "5701";
14813 case TG3_PHY_ID_BCM5703: return "5703";
14814 case TG3_PHY_ID_BCM5704: return "5704";
14815 case TG3_PHY_ID_BCM5705: return "5705";
14816 case TG3_PHY_ID_BCM5750: return "5750";
14817 case TG3_PHY_ID_BCM5752: return "5752";
14818 case TG3_PHY_ID_BCM5714: return "5714";
14819 case TG3_PHY_ID_BCM5780: return "5780";
14820 case TG3_PHY_ID_BCM5755: return "5755";
14821 case TG3_PHY_ID_BCM5787: return "5787";
14822 case TG3_PHY_ID_BCM5784: return "5784";
14823 case TG3_PHY_ID_BCM5756: return "5722/5756";
14824 case TG3_PHY_ID_BCM5906: return "5906";
14825 case TG3_PHY_ID_BCM5761: return "5761";
14826 case TG3_PHY_ID_BCM5718C: return "5718C";
14827 case TG3_PHY_ID_BCM5718S: return "5718S";
14828 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000014829 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000014830 case TG3_PHY_ID_BCM5720C: return "5720C";
Matt Carlson79eb6902010-02-17 15:17:03 +000014831 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070014832 case 0: return "serdes";
14833 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070014834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014835}
14836
Michael Chanf9804dd2005-09-27 12:13:10 -070014837static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
14838{
14839 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
14840 strcpy(str, "PCI Express");
14841 return str;
14842 } else if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
14843 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
14844
14845 strcpy(str, "PCIX:");
14846
14847 if ((clock_ctrl == 7) ||
14848 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
14849 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
14850 strcat(str, "133MHz");
14851 else if (clock_ctrl == 0)
14852 strcat(str, "33MHz");
14853 else if (clock_ctrl == 2)
14854 strcat(str, "50MHz");
14855 else if (clock_ctrl == 4)
14856 strcat(str, "66MHz");
14857 else if (clock_ctrl == 6)
14858 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070014859 } else {
14860 strcpy(str, "PCI:");
14861 if (tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED)
14862 strcat(str, "66MHz");
14863 else
14864 strcat(str, "33MHz");
14865 }
14866 if (tp->tg3_flags & TG3_FLAG_PCI_32BIT)
14867 strcat(str, ":32-bit");
14868 else
14869 strcat(str, ":64-bit");
14870 return str;
14871}
14872
Michael Chan8c2dc7e2005-12-19 16:26:02 -080014873static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014874{
14875 struct pci_dev *peer;
14876 unsigned int func, devnr = tp->pdev->devfn & ~7;
14877
14878 for (func = 0; func < 8; func++) {
14879 peer = pci_get_slot(tp->pdev->bus, devnr | func);
14880 if (peer && peer != tp->pdev)
14881 break;
14882 pci_dev_put(peer);
14883 }
Michael Chan16fe9d72005-12-13 21:09:54 -080014884 /* 5704 can be configured in single-port mode, set peer to
14885 * tp->pdev in that case.
14886 */
14887 if (!peer) {
14888 peer = tp->pdev;
14889 return peer;
14890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014891
14892 /*
14893 * We don't need to keep the refcount elevated; there's no way
14894 * to remove one half of this device without removing the other
14895 */
14896 pci_dev_put(peer);
14897
14898 return peer;
14899}
14900
David S. Miller15f98502005-05-18 22:49:26 -070014901static void __devinit tg3_init_coal(struct tg3 *tp)
14902{
14903 struct ethtool_coalesce *ec = &tp->coal;
14904
14905 memset(ec, 0, sizeof(*ec));
14906 ec->cmd = ETHTOOL_GCOALESCE;
14907 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
14908 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
14909 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
14910 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
14911 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
14912 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
14913 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
14914 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
14915 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
14916
14917 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
14918 HOSTCC_MODE_CLRTICK_TXBD)) {
14919 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
14920 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
14921 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
14922 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
14923 }
Michael Chand244c892005-07-05 14:42:33 -070014924
14925 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
14926 ec->rx_coalesce_usecs_irq = 0;
14927 ec->tx_coalesce_usecs_irq = 0;
14928 ec->stats_block_coalesce_usecs = 0;
14929 }
David S. Miller15f98502005-05-18 22:49:26 -070014930}
14931
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080014932static const struct net_device_ops tg3_netdev_ops = {
14933 .ndo_open = tg3_open,
14934 .ndo_stop = tg3_close,
Stephen Hemminger00829822008-11-20 20:14:53 -080014935 .ndo_start_xmit = tg3_start_xmit,
Eric Dumazet511d2222010-07-07 20:44:24 +000014936 .ndo_get_stats64 = tg3_get_stats64,
Stephen Hemminger00829822008-11-20 20:14:53 -080014937 .ndo_validate_addr = eth_validate_addr,
14938 .ndo_set_multicast_list = tg3_set_rx_mode,
14939 .ndo_set_mac_address = tg3_set_mac_addr,
14940 .ndo_do_ioctl = tg3_ioctl,
14941 .ndo_tx_timeout = tg3_tx_timeout,
14942 .ndo_change_mtu = tg3_change_mtu,
Michał Mirosławdc668912011-04-07 03:35:07 +000014943 .ndo_fix_features = tg3_fix_features,
Stephen Hemminger00829822008-11-20 20:14:53 -080014944#ifdef CONFIG_NET_POLL_CONTROLLER
14945 .ndo_poll_controller = tg3_poll_controller,
14946#endif
14947};
14948
14949static const struct net_device_ops tg3_netdev_ops_dma_bug = {
14950 .ndo_open = tg3_open,
14951 .ndo_stop = tg3_close,
14952 .ndo_start_xmit = tg3_start_xmit_dma_bug,
Eric Dumazet511d2222010-07-07 20:44:24 +000014953 .ndo_get_stats64 = tg3_get_stats64,
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080014954 .ndo_validate_addr = eth_validate_addr,
14955 .ndo_set_multicast_list = tg3_set_rx_mode,
14956 .ndo_set_mac_address = tg3_set_mac_addr,
14957 .ndo_do_ioctl = tg3_ioctl,
14958 .ndo_tx_timeout = tg3_tx_timeout,
14959 .ndo_change_mtu = tg3_change_mtu,
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080014960#ifdef CONFIG_NET_POLL_CONTROLLER
14961 .ndo_poll_controller = tg3_poll_controller,
14962#endif
14963};
14964
Linus Torvalds1da177e2005-04-16 15:20:36 -070014965static int __devinit tg3_init_one(struct pci_dev *pdev,
14966 const struct pci_device_id *ent)
14967{
Linus Torvalds1da177e2005-04-16 15:20:36 -070014968 struct net_device *dev;
14969 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000014970 int i, err, pm_cap;
14971 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070014972 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080014973 u64 dma_mask, persist_dma_mask;
Michał Mirosławdc668912011-04-07 03:35:07 +000014974 u32 hw_features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014975
Joe Perches05dbe002010-02-17 19:44:19 +000014976 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014977
14978 err = pci_enable_device(pdev);
14979 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014980 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014981 return err;
14982 }
14983
Linus Torvalds1da177e2005-04-16 15:20:36 -070014984 err = pci_request_regions(pdev, DRV_MODULE_NAME);
14985 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014986 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014987 goto err_out_disable_pdev;
14988 }
14989
14990 pci_set_master(pdev);
14991
14992 /* Find power-management capability. */
14993 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
14994 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000014995 dev_err(&pdev->dev,
14996 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014997 err = -EIO;
14998 goto err_out_free_res;
14999 }
15000
Matt Carlsonfe5f5782009-09-01 13:09:39 +000015001 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015002 if (!dev) {
Matt Carlson2445e462010-04-05 10:19:21 +000015003 dev_err(&pdev->dev, "Etherdev alloc failed, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015004 err = -ENOMEM;
15005 goto err_out_free_res;
15006 }
15007
Linus Torvalds1da177e2005-04-16 15:20:36 -070015008 SET_NETDEV_DEV(dev, &pdev->dev);
15009
Linus Torvalds1da177e2005-04-16 15:20:36 -070015010 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015011
15012 tp = netdev_priv(dev);
15013 tp->pdev = pdev;
15014 tp->dev = dev;
15015 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015016 tp->rx_mode = TG3_DEF_RX_MODE;
15017 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070015018
Linus Torvalds1da177e2005-04-16 15:20:36 -070015019 if (tg3_debug > 0)
15020 tp->msg_enable = tg3_debug;
15021 else
15022 tp->msg_enable = TG3_DEF_MSG_ENABLE;
15023
15024 /* The word/byte swap controls here control register access byte
15025 * swapping. DMA data byte swapping is controlled in the GRC_MODE
15026 * setting below.
15027 */
15028 tp->misc_host_ctrl =
15029 MISC_HOST_CTRL_MASK_PCI_INT |
15030 MISC_HOST_CTRL_WORD_SWAP |
15031 MISC_HOST_CTRL_INDIR_ACCESS |
15032 MISC_HOST_CTRL_PCISTATE_RW;
15033
15034 /* The NONFRM (non-frame) byte/word swap controls take effect
15035 * on descriptor entries, anything which isn't packet data.
15036 *
15037 * The StrongARM chips on the board (one for tx, one for rx)
15038 * are running in big-endian mode.
15039 */
15040 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
15041 GRC_MODE_WSWAP_NONFRM_DATA);
15042#ifdef __BIG_ENDIAN
15043 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
15044#endif
15045 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015046 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000015047 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015048
Matt Carlsond5fe4882008-11-21 17:20:32 -080015049 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010015050 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015051 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015052 err = -ENOMEM;
15053 goto err_out_free_dev;
15054 }
15055
Linus Torvalds1da177e2005-04-16 15:20:36 -070015056 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
15057 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015058
Linus Torvalds1da177e2005-04-16 15:20:36 -070015059 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015060 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015061 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015062
15063 err = tg3_get_invariants(tp);
15064 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015065 dev_err(&pdev->dev,
15066 "Problem fetching invariants of chip, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015067 goto err_out_iounmap;
15068 }
15069
Matt Carlson615774f2009-11-13 13:03:39 +000015070 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) &&
Matt Carlson0a58d662011-04-05 14:22:45 +000015071 !(tp->tg3_flags3 & TG3_FLG3_5717_PLUS))
Stephen Hemminger00829822008-11-20 20:14:53 -080015072 dev->netdev_ops = &tg3_netdev_ops;
15073 else
15074 dev->netdev_ops = &tg3_netdev_ops_dma_bug;
15075
15076
Michael Chan4a29cc22006-03-19 13:21:12 -080015077 /* The EPB bridge inside 5714, 5715, and 5780 and any
15078 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080015079 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
15080 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
15081 * do DMA address check in tg3_start_xmit().
15082 */
Michael Chan4a29cc22006-03-19 13:21:12 -080015083 if (tp->tg3_flags2 & TG3_FLG2_IS_5788)
Yang Hongyang284901a2009-04-06 19:01:15 -070015084 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Michael Chan4a29cc22006-03-19 13:21:12 -080015085 else if (tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070015086 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080015087#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070015088 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015089#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080015090 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070015091 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015092
15093 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070015094 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080015095 err = pci_set_dma_mask(pdev, dma_mask);
15096 if (!err) {
15097 dev->features |= NETIF_F_HIGHDMA;
15098 err = pci_set_consistent_dma_mask(pdev,
15099 persist_dma_mask);
15100 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015101 dev_err(&pdev->dev, "Unable to obtain 64 bit "
15102 "DMA for consistent allocations\n");
Michael Chan72f2afb2006-03-06 19:28:35 -080015103 goto err_out_iounmap;
15104 }
15105 }
15106 }
Yang Hongyang284901a2009-04-06 19:01:15 -070015107 if (err || dma_mask == DMA_BIT_MASK(32)) {
15108 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080015109 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015110 dev_err(&pdev->dev,
15111 "No usable DMA configuration, aborting\n");
Michael Chan72f2afb2006-03-06 19:28:35 -080015112 goto err_out_iounmap;
15113 }
15114 }
15115
Michael Chanfdfec172005-07-25 12:31:48 -070015116 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015117
Matt Carlson507399f2009-11-13 13:03:37 +000015118 /* Selectively allow TSO based on operating conditions */
15119 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO) ||
15120 (tp->fw_needed && !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)))
15121 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
15122 else {
15123 tp->tg3_flags2 &= ~(TG3_FLG2_TSO_CAPABLE | TG3_FLG2_TSO_BUG);
15124 tp->fw_needed = NULL;
15125 }
15126
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080015127 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
Matt Carlson9e9fd122009-01-19 16:57:45 -080015128 tp->fw_needed = FIRMWARE_TG3;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080015129
Michael Chan4e3a7aa2006-03-20 17:47:44 -080015130 /* TSO is on by default on chips that support hardware TSO.
15131 * Firmware TSO on older chips gives lower performance, so it
15132 * is off by default, but can be enabled using ethtool.
15133 */
Matt Carlsone849cdc2009-11-13 13:03:38 +000015134 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO) &&
Michał Mirosławdc668912011-04-07 03:35:07 +000015135 (dev->features & NETIF_F_IP_CSUM))
15136 hw_features |= NETIF_F_TSO;
Matt Carlsone849cdc2009-11-13 13:03:38 +000015137 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_2) ||
15138 (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3)) {
Michał Mirosławdc668912011-04-07 03:35:07 +000015139 if (dev->features & NETIF_F_IPV6_CSUM)
15140 hw_features |= NETIF_F_TSO6;
Matt Carlsone849cdc2009-11-13 13:03:38 +000015141 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
15142 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070015143 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
15144 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080015145 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Michał Mirosławdc668912011-04-07 03:35:07 +000015146 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
15147 hw_features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070015148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015149
Michał Mirosławdc668912011-04-07 03:35:07 +000015150 dev->hw_features |= hw_features;
15151 dev->features |= hw_features;
15152 dev->vlan_features |= hw_features;
15153
Linus Torvalds1da177e2005-04-16 15:20:36 -070015154 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
15155 !(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) &&
15156 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
15157 tp->tg3_flags2 |= TG3_FLG2_MAX_RXPEND_64;
15158 tp->rx_pending = 63;
15159 }
15160
Linus Torvalds1da177e2005-04-16 15:20:36 -070015161 err = tg3_get_device_address(tp);
15162 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015163 dev_err(&pdev->dev,
15164 "Could not obtain valid ethernet address, aborting\n");
Matt Carlson026a6c22009-12-03 08:36:24 +000015165 goto err_out_iounmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015166 }
15167
Matt Carlson0d3031d2007-10-10 18:02:43 -070015168 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
Matt Carlson63532392008-11-03 16:49:57 -080015169 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
Al Viro79ea13c2008-01-24 02:06:46 -080015170 if (!tp->aperegs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015171 dev_err(&pdev->dev,
15172 "Cannot map APE registers, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070015173 err = -ENOMEM;
Matt Carlson026a6c22009-12-03 08:36:24 +000015174 goto err_out_iounmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070015175 }
15176
15177 tg3_ape_lock_init(tp);
Matt Carlson7fd76442009-02-25 14:27:20 +000015178
15179 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF)
15180 tg3_read_dash_ver(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015181 }
15182
Matt Carlsonc88864d2007-11-12 21:07:01 -080015183 /*
15184 * Reset chip in case UNDI or EFI driver did not shutdown
15185 * DMA self test will enable WDMAC and we'll see (spurious)
15186 * pending DMA on the PCI bus at that point.
15187 */
15188 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
15189 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
15190 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
15191 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
15192 }
15193
15194 err = tg3_test_dma(tp);
15195 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015196 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080015197 goto err_out_apeunmap;
15198 }
15199
Matt Carlson78f90dc2009-11-13 13:03:42 +000015200 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
15201 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
15202 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000015203 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000015204 struct tg3_napi *tnapi = &tp->napi[i];
15205
15206 tnapi->tp = tp;
15207 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
15208
15209 tnapi->int_mbox = intmbx;
15210 if (i < 4)
15211 intmbx += 0x8;
15212 else
15213 intmbx += 0x4;
15214
15215 tnapi->consmbox = rcvmbx;
15216 tnapi->prodmbox = sndmbx;
15217
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015218 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000015219 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015220 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000015221 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000015222
15223 if (!(tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX))
15224 break;
15225
15226 /*
15227 * If we support MSIX, we'll be using RSS. If we're using
15228 * RSS, the first vector only handles link interrupts and the
15229 * remaining vectors handle rx and tx interrupts. Reuse the
15230 * mailbox values for the next iteration. The values we setup
15231 * above are still useful for the single vectored mode.
15232 */
15233 if (!i)
15234 continue;
15235
15236 rcvmbx += 0x8;
15237
15238 if (sndmbx & 0x4)
15239 sndmbx -= 0x4;
15240 else
15241 sndmbx += 0xc;
15242 }
15243
Matt Carlsonc88864d2007-11-12 21:07:01 -080015244 tg3_init_coal(tp);
15245
Michael Chanc49a1562006-12-17 17:07:29 -080015246 pci_set_drvdata(pdev, dev);
15247
Linus Torvalds1da177e2005-04-16 15:20:36 -070015248 err = register_netdev(dev);
15249 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015250 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070015251 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015252 }
15253
Joe Perches05dbe002010-02-17 19:44:19 +000015254 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
15255 tp->board_part_number,
15256 tp->pci_chip_rev_id,
15257 tg3_bus_string(tp, str),
15258 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015259
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015260 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000015261 struct phy_device *phydev;
15262 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000015263 netdev_info(dev,
15264 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000015265 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015266 } else {
15267 char *ethtype;
15268
15269 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
15270 ethtype = "10/100Base-TX";
15271 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
15272 ethtype = "1000Base-SX";
15273 else
15274 ethtype = "10/100/1000Base-T";
15275
Matt Carlson5129c3a2010-04-05 10:19:23 +000015276 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015277 "(WireSpeed[%d])\n", tg3_phy_string(tp), ethtype,
15278 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0);
15279 }
Matt Carlsondf59c942008-11-03 16:52:56 -080015280
Joe Perches05dbe002010-02-17 19:44:19 +000015281 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000015282 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches05dbe002010-02-17 19:44:19 +000015283 (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015284 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches05dbe002010-02-17 19:44:19 +000015285 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0,
15286 (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) != 0);
15287 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
15288 tp->dma_rwctrl,
15289 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
15290 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015291
15292 return 0;
15293
Matt Carlson0d3031d2007-10-10 18:02:43 -070015294err_out_apeunmap:
15295 if (tp->aperegs) {
15296 iounmap(tp->aperegs);
15297 tp->aperegs = NULL;
15298 }
15299
Linus Torvalds1da177e2005-04-16 15:20:36 -070015300err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070015301 if (tp->regs) {
15302 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015303 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015305
15306err_out_free_dev:
15307 free_netdev(dev);
15308
15309err_out_free_res:
15310 pci_release_regions(pdev);
15311
15312err_out_disable_pdev:
15313 pci_disable_device(pdev);
15314 pci_set_drvdata(pdev, NULL);
15315 return err;
15316}
15317
15318static void __devexit tg3_remove_one(struct pci_dev *pdev)
15319{
15320 struct net_device *dev = pci_get_drvdata(pdev);
15321
15322 if (dev) {
15323 struct tg3 *tp = netdev_priv(dev);
15324
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080015325 if (tp->fw)
15326 release_firmware(tp->fw);
15327
Tejun Heo23f333a2010-12-12 16:45:14 +010015328 cancel_work_sync(&tp->reset_task);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015329
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015330 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
15331 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015332 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015333 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070015334
Linus Torvalds1da177e2005-04-16 15:20:36 -070015335 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015336 if (tp->aperegs) {
15337 iounmap(tp->aperegs);
15338 tp->aperegs = NULL;
15339 }
Michael Chan68929142005-08-09 20:17:14 -070015340 if (tp->regs) {
15341 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015342 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015344 free_netdev(dev);
15345 pci_release_regions(pdev);
15346 pci_disable_device(pdev);
15347 pci_set_drvdata(pdev, NULL);
15348 }
15349}
15350
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015351#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015352static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015353{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015354 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015355 struct net_device *dev = pci_get_drvdata(pdev);
15356 struct tg3 *tp = netdev_priv(dev);
15357 int err;
15358
15359 if (!netif_running(dev))
15360 return 0;
15361
Tejun Heo23f333a2010-12-12 16:45:14 +010015362 flush_work_sync(&tp->reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015363 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015364 tg3_netif_stop(tp);
15365
15366 del_timer_sync(&tp->timer);
15367
David S. Millerf47c11e2005-06-24 20:18:35 -070015368 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015369 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070015370 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015371
15372 netif_device_detach(dev);
15373
David S. Millerf47c11e2005-06-24 20:18:35 -070015374 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070015375 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan6a9eba12005-12-13 21:08:58 -080015376 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
David S. Millerf47c11e2005-06-24 20:18:35 -070015377 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015378
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015379 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015380 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015381 int err2;
15382
David S. Millerf47c11e2005-06-24 20:18:35 -070015383 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015384
Michael Chan6a9eba12005-12-13 21:08:58 -080015385 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015386 err2 = tg3_restart_hw(tp, 1);
15387 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070015388 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015389
15390 tp->timer.expires = jiffies + tp->timer_offset;
15391 add_timer(&tp->timer);
15392
15393 netif_device_attach(dev);
15394 tg3_netif_start(tp);
15395
Michael Chanb9ec6c12006-07-25 16:37:27 -070015396out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015397 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015398
15399 if (!err2)
15400 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015401 }
15402
15403 return err;
15404}
15405
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015406static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015407{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015408 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015409 struct net_device *dev = pci_get_drvdata(pdev);
15410 struct tg3 *tp = netdev_priv(dev);
15411 int err;
15412
15413 if (!netif_running(dev))
15414 return 0;
15415
Linus Torvalds1da177e2005-04-16 15:20:36 -070015416 netif_device_attach(dev);
15417
David S. Millerf47c11e2005-06-24 20:18:35 -070015418 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015419
Michael Chan6a9eba12005-12-13 21:08:58 -080015420 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Michael Chanb9ec6c12006-07-25 16:37:27 -070015421 err = tg3_restart_hw(tp, 1);
15422 if (err)
15423 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015424
15425 tp->timer.expires = jiffies + tp->timer_offset;
15426 add_timer(&tp->timer);
15427
Linus Torvalds1da177e2005-04-16 15:20:36 -070015428 tg3_netif_start(tp);
15429
Michael Chanb9ec6c12006-07-25 16:37:27 -070015430out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015431 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015432
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015433 if (!err)
15434 tg3_phy_start(tp);
15435
Michael Chanb9ec6c12006-07-25 16:37:27 -070015436 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015437}
15438
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015439static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015440#define TG3_PM_OPS (&tg3_pm_ops)
15441
15442#else
15443
15444#define TG3_PM_OPS NULL
15445
15446#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015447
Linus Torvalds1da177e2005-04-16 15:20:36 -070015448static struct pci_driver tg3_driver = {
15449 .name = DRV_MODULE_NAME,
15450 .id_table = tg3_pci_tbl,
15451 .probe = tg3_init_one,
15452 .remove = __devexit_p(tg3_remove_one),
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015453 .driver.pm = TG3_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070015454};
15455
15456static int __init tg3_init(void)
15457{
Jeff Garzik29917622006-08-19 17:48:59 -040015458 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015459}
15460
15461static void __exit tg3_cleanup(void)
15462{
15463 pci_unregister_driver(&tg3_driver);
15464}
15465
15466module_init(tg3_init);
15467module_exit(tg3_cleanup);