blob: 90ef94292a9cb8ec5be89266bd575ad1b9f6560d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * tg3.c: Broadcom Tigon3 ethernet driver.
3 *
4 * Copyright (C) 2001, 2002, 2003, 2004 David S. Miller (davem@redhat.com)
5 * Copyright (C) 2001, 2002, 2003 Jeff Garzik (jgarzik@pobox.com)
6 * Copyright (C) 2004 Sun Microsystems Inc.
Matt Carlsonb86fb2c2011-01-25 15:58:57 +00007 * Copyright (C) 2005-2011 Broadcom Corporation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * Firmware is:
Michael Chan49cabf42005-06-06 15:15:17 -070010 * Derived from proprietary unpublished source code,
11 * Copyright (C) 2000-2003 Broadcom Corporation.
12 *
13 * Permission is hereby granted for the distribution of this firmware
14 * data in hexadecimal or equivalent format, provided this copyright
15 * notice is accompanying it.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 */
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#include <linux/module.h>
20#include <linux/moduleparam.h>
Matt Carlson6867c842010-07-11 09:31:44 +000021#include <linux/stringify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/kernel.h>
23#include <linux/types.h>
24#include <linux/compiler.h>
25#include <linux/slab.h>
26#include <linux/delay.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020027#include <linux/in.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/init.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000029#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/ioport.h>
31#include <linux/pci.h>
32#include <linux/netdevice.h>
33#include <linux/etherdevice.h>
34#include <linux/skbuff.h>
35#include <linux/ethtool.h>
Matt Carlson3110f5f52010-12-06 08:28:50 +000036#include <linux/mdio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/mii.h>
Matt Carlson158d7ab2008-05-29 01:37:54 -070038#include <linux/phy.h>
Matt Carlsona9daf362008-05-25 23:49:44 -070039#include <linux/brcmphy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/if_vlan.h>
41#include <linux/ip.h>
42#include <linux/tcp.h>
43#include <linux/workqueue.h>
Michael Chan61487482005-09-05 17:53:19 -070044#include <linux/prefetch.h>
Tobias Klauserf9a5f7d2005-10-29 15:09:26 +020045#include <linux/dma-mapping.h>
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080046#include <linux/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#include <net/checksum.h>
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030049#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51#include <asm/system.h>
Javier Martinez Canillas27fd9de2011-03-26 16:42:31 +000052#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <asm/byteorder.h>
Javier Martinez Canillas27fd9de2011-03-26 16:42:31 +000054#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
David S. Miller49b6e95f2007-03-29 01:38:42 -070056#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <asm/idprom.h>
David S. Miller49b6e95f2007-03-29 01:38:42 -070058#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#endif
60
Matt Carlson63532392008-11-03 16:49:57 -080061#define BAR_0 0
62#define BAR_2 2
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include "tg3.h"
65
Joe Perches63c3a662011-04-26 08:12:10 +000066/* Functions & macros to verify TG3_FLAGS types */
67
68static inline int _tg3_flag(enum TG3_FLAGS flag, unsigned long *bits)
69{
70 return test_bit(flag, bits);
71}
72
73static inline void _tg3_flag_set(enum TG3_FLAGS flag, unsigned long *bits)
74{
75 set_bit(flag, bits);
76}
77
78static inline void _tg3_flag_clear(enum TG3_FLAGS flag, unsigned long *bits)
79{
80 clear_bit(flag, bits);
81}
82
83#define tg3_flag(tp, flag) \
84 _tg3_flag(TG3_FLAG_##flag, (tp)->tg3_flags)
85#define tg3_flag_set(tp, flag) \
86 _tg3_flag_set(TG3_FLAG_##flag, (tp)->tg3_flags)
87#define tg3_flag_clear(tp, flag) \
88 _tg3_flag_clear(TG3_FLAG_##flag, (tp)->tg3_flags)
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#define DRV_MODULE_NAME "tg3"
Matt Carlson6867c842010-07-11 09:31:44 +000091#define TG3_MAJ_NUM 3
Matt Carlsonefab79c2011-12-08 14:40:18 +000092#define TG3_MIN_NUM 122
Matt Carlson6867c842010-07-11 09:31:44 +000093#define DRV_MODULE_VERSION \
94 __stringify(TG3_MAJ_NUM) "." __stringify(TG3_MIN_NUM)
Matt Carlsonefab79c2011-12-08 14:40:18 +000095#define DRV_MODULE_RELDATE "December 7, 2011"
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Matt Carlsonfd6d3f02011-08-31 11:44:52 +000097#define RESET_KIND_SHUTDOWN 0
98#define RESET_KIND_INIT 1
99#define RESET_KIND_SUSPEND 2
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101#define TG3_DEF_RX_MODE 0
102#define TG3_DEF_TX_MODE 0
103#define TG3_DEF_MSG_ENABLE \
104 (NETIF_MSG_DRV | \
105 NETIF_MSG_PROBE | \
106 NETIF_MSG_LINK | \
107 NETIF_MSG_TIMER | \
108 NETIF_MSG_IFDOWN | \
109 NETIF_MSG_IFUP | \
110 NETIF_MSG_RX_ERR | \
111 NETIF_MSG_TX_ERR)
112
Matt Carlson520b2752011-06-13 13:39:02 +0000113#define TG3_GRC_LCLCTL_PWRSW_DELAY 100
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115/* length of time before we decide the hardware is borked,
116 * and dev->tx_timeout() should be called to fix the problem
117 */
Joe Perches63c3a662011-04-26 08:12:10 +0000118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119#define TG3_TX_TIMEOUT (5 * HZ)
120
121/* hardware minimum and maximum for a single frame's data payload */
122#define TG3_MIN_MTU 60
123#define TG3_MAX_MTU(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000124 (tg3_flag(tp, JUMBO_CAPABLE) ? 9000 : 1500)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126/* These numbers seem to be hard coded in the NIC firmware somehow.
127 * You can't change the ring sizes, but you can change where you place
128 * them in the NIC onboard memory.
129 */
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000130#define TG3_RX_STD_RING_SIZE(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000131 (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000132 TG3_RX_STD_MAX_SIZE_5717 : TG3_RX_STD_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133#define TG3_DEF_RX_RING_PENDING 200
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000134#define TG3_RX_JMB_RING_SIZE(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000135 (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000136 TG3_RX_JMB_MAX_SIZE_5717 : TG3_RX_JMB_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137#define TG3_DEF_RX_JUMBO_RING_PENDING 100
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000138#define TG3_RSS_INDIR_TBL_SIZE 128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140/* Do not place this n-ring entries value into the tp struct itself,
141 * we really want to expose these constants to GCC so that modulo et
142 * al. operations are done with shifts and masks instead of with
143 * hw multiply/modulo instructions. Another solution would be to
144 * replace things like '% foo' with '& (foo - 1)'.
145 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147#define TG3_TX_RING_SIZE 512
148#define TG3_DEF_TX_RING_PENDING (TG3_TX_RING_SIZE - 1)
149
Matt Carlson2c49a442010-09-30 10:34:35 +0000150#define TG3_RX_STD_RING_BYTES(tp) \
151 (sizeof(struct tg3_rx_buffer_desc) * TG3_RX_STD_RING_SIZE(tp))
152#define TG3_RX_JMB_RING_BYTES(tp) \
153 (sizeof(struct tg3_ext_rx_buffer_desc) * TG3_RX_JMB_RING_SIZE(tp))
154#define TG3_RX_RCB_RING_BYTES(tp) \
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000155 (sizeof(struct tg3_rx_buffer_desc) * (tp->rx_ret_ring_mask + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156#define TG3_TX_RING_BYTES (sizeof(struct tg3_tx_buffer_desc) * \
157 TG3_TX_RING_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158#define NEXT_TX(N) (((N) + 1) & (TG3_TX_RING_SIZE - 1))
159
Matt Carlson287be122009-08-28 13:58:46 +0000160#define TG3_DMA_BYTE_ENAB 64
161
162#define TG3_RX_STD_DMA_SZ 1536
163#define TG3_RX_JMB_DMA_SZ 9046
164
165#define TG3_RX_DMA_TO_MAP_SZ(x) ((x) + TG3_DMA_BYTE_ENAB)
166
167#define TG3_RX_STD_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_STD_DMA_SZ)
168#define TG3_RX_JMB_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_JMB_DMA_SZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Matt Carlson2c49a442010-09-30 10:34:35 +0000170#define TG3_RX_STD_BUFF_RING_SIZE(tp) \
171 (sizeof(struct ring_info) * TG3_RX_STD_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000172
Matt Carlson2c49a442010-09-30 10:34:35 +0000173#define TG3_RX_JMB_BUFF_RING_SIZE(tp) \
174 (sizeof(struct ring_info) * TG3_RX_JMB_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000175
Matt Carlsond2757fc2010-04-12 06:58:27 +0000176/* Due to a hardware bug, the 5701 can only DMA to memory addresses
177 * that are at least dword aligned when used in PCIX mode. The driver
178 * works around this bug by double copying the packet. This workaround
179 * is built into the normal double copy length check for efficiency.
180 *
181 * However, the double copy is only necessary on those architectures
182 * where unaligned memory accesses are inefficient. For those architectures
183 * where unaligned memory accesses incur little penalty, we can reintegrate
184 * the 5701 in the normal rx path. Doing so saves a device structure
185 * dereference by hardcoding the double copy threshold in place.
186 */
187#define TG3_RX_COPY_THRESHOLD 256
188#if NET_IP_ALIGN == 0 || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
189 #define TG3_RX_COPY_THRESH(tp) TG3_RX_COPY_THRESHOLD
190#else
191 #define TG3_RX_COPY_THRESH(tp) ((tp)->rx_copy_thresh)
192#endif
193
Matt Carlson81389f52011-08-31 11:44:49 +0000194#if (NET_IP_ALIGN != 0)
195#define TG3_RX_OFFSET(tp) ((tp)->rx_offset)
196#else
Eric Dumazet9205fd92011-11-18 06:47:01 +0000197#define TG3_RX_OFFSET(tp) (NET_SKB_PAD)
Matt Carlson81389f52011-08-31 11:44:49 +0000198#endif
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200/* minimum number of free TX descriptors required to wake up TX process */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000201#define TG3_TX_WAKEUP_THRESH(tnapi) ((tnapi)->tx_pending / 4)
Matt Carlson55086ad2011-12-14 11:09:59 +0000202#define TG3_TX_BD_DMA_MAX_2K 2048
Matt Carlsona4cb4282011-12-14 11:09:58 +0000203#define TG3_TX_BD_DMA_MAX_4K 4096
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Matt Carlsonad829262008-11-21 17:16:16 -0800205#define TG3_RAW_IP_ALIGN 2
206
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000207#define TG3_FW_UPDATE_TIMEOUT_SEC 5
208
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800209#define FIRMWARE_TG3 "tigon/tg3.bin"
210#define FIRMWARE_TG3TSO "tigon/tg3_tso.bin"
211#define FIRMWARE_TG3TSO5 "tigon/tg3_tso5.bin"
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213static char version[] __devinitdata =
Joe Perches05dbe002010-02-17 19:44:19 +0000214 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216MODULE_AUTHOR("David S. Miller (davem@redhat.com) and Jeff Garzik (jgarzik@pobox.com)");
217MODULE_DESCRIPTION("Broadcom Tigon3 ethernet driver");
218MODULE_LICENSE("GPL");
219MODULE_VERSION(DRV_MODULE_VERSION);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800220MODULE_FIRMWARE(FIRMWARE_TG3);
221MODULE_FIRMWARE(FIRMWARE_TG3TSO);
222MODULE_FIRMWARE(FIRMWARE_TG3TSO5);
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224static int tg3_debug = -1; /* -1 == use TG3_DEF_MSG_ENABLE as value */
225module_param(tg3_debug, int, 0);
226MODULE_PARM_DESC(tg3_debug, "Tigon3 bitmapped debugging message enable value");
227
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000228static DEFINE_PCI_DEVICE_TABLE(tg3_pci_tbl) = {
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700229 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5700)},
230 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5701)},
231 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702)},
232 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703)},
233 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704)},
234 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702FE)},
235 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705)},
236 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705_2)},
237 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M)},
238 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M_2)},
239 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702X)},
240 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703X)},
241 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S)},
242 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702A3)},
243 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703A3)},
244 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5782)},
245 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5788)},
246 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5789)},
247 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901)},
248 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901_2)},
249 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S_2)},
250 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705F)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700251 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5721)},
Michael Chan126a3362006-09-27 16:03:07 -0700252 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5722)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700253 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700254 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751M)},
255 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751F)},
256 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752)},
257 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752M)},
258 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753)},
259 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753M)},
260 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753F)},
261 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754)},
262 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754M)},
263 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755)},
264 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755M)},
Michael Chan126a3362006-09-27 16:03:07 -0700265 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5756)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700266 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5786)},
267 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787)},
268 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787M)},
Michael Chan676917d2006-12-07 00:20:22 -0800269 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787F)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700270 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714)},
271 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714S)},
272 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715)},
273 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715S)},
274 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780)},
275 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780S)},
276 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5781)},
Michael Chanb5d37722006-09-27 16:06:21 -0700277 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906)},
278 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906M)},
Matt Carlsond30cdd22007-10-07 23:28:35 -0700279 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5784)},
280 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5764)},
Matt Carlson6c7af272007-10-21 16:12:02 -0700281 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5723)},
Matt Carlson9936bcf2007-10-10 18:03:07 -0700282 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761)},
283 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761E)},
Matt Carlsonc88e6682008-11-03 16:49:18 -0800284 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761S)},
285 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761SE)},
Matt Carlson2befdce2009-08-28 12:28:45 +0000286 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_G)},
287 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_F)},
Matt Carlson321d32a2008-11-21 17:22:19 -0800288 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780)},
289 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57760)},
290 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57790)},
Matt Carlson5e7ccf22009-08-25 10:08:42 +0000291 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57788)},
Matt Carlson5001e2f2009-11-13 13:03:51 +0000292 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717)},
293 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5718)},
Matt Carlsonb0f75222010-01-20 16:58:11 +0000294 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57781)},
295 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57785)},
296 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57761)},
297 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57765)},
298 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57791)},
299 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57795)},
Matt Carlson302b5002010-06-05 17:24:38 +0000300 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5719)},
Matt Carlsonba1f3c72011-04-05 14:22:50 +0000301 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5720)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700302 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9DXX)},
303 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9MXX)},
304 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1000)},
305 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1001)},
306 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1003)},
307 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC9100)},
308 {PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_TIGON3)},
Meelis Roos1dcb14d2011-05-25 05:43:47 +0000309 {PCI_DEVICE(0x10cf, 0x11a2)}, /* Fujitsu 1000base-SX with BCM5703SKHB */
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700310 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311};
312
313MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
314
Andreas Mohr50da8592006-08-14 23:54:30 -0700315static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000317} ethtool_stats_keys[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 { "rx_octets" },
319 { "rx_fragments" },
320 { "rx_ucast_packets" },
321 { "rx_mcast_packets" },
322 { "rx_bcast_packets" },
323 { "rx_fcs_errors" },
324 { "rx_align_errors" },
325 { "rx_xon_pause_rcvd" },
326 { "rx_xoff_pause_rcvd" },
327 { "rx_mac_ctrl_rcvd" },
328 { "rx_xoff_entered" },
329 { "rx_frame_too_long_errors" },
330 { "rx_jabbers" },
331 { "rx_undersize_packets" },
332 { "rx_in_length_errors" },
333 { "rx_out_length_errors" },
334 { "rx_64_or_less_octet_packets" },
335 { "rx_65_to_127_octet_packets" },
336 { "rx_128_to_255_octet_packets" },
337 { "rx_256_to_511_octet_packets" },
338 { "rx_512_to_1023_octet_packets" },
339 { "rx_1024_to_1522_octet_packets" },
340 { "rx_1523_to_2047_octet_packets" },
341 { "rx_2048_to_4095_octet_packets" },
342 { "rx_4096_to_8191_octet_packets" },
343 { "rx_8192_to_9022_octet_packets" },
344
345 { "tx_octets" },
346 { "tx_collisions" },
347
348 { "tx_xon_sent" },
349 { "tx_xoff_sent" },
350 { "tx_flow_control" },
351 { "tx_mac_errors" },
352 { "tx_single_collisions" },
353 { "tx_mult_collisions" },
354 { "tx_deferred" },
355 { "tx_excessive_collisions" },
356 { "tx_late_collisions" },
357 { "tx_collide_2times" },
358 { "tx_collide_3times" },
359 { "tx_collide_4times" },
360 { "tx_collide_5times" },
361 { "tx_collide_6times" },
362 { "tx_collide_7times" },
363 { "tx_collide_8times" },
364 { "tx_collide_9times" },
365 { "tx_collide_10times" },
366 { "tx_collide_11times" },
367 { "tx_collide_12times" },
368 { "tx_collide_13times" },
369 { "tx_collide_14times" },
370 { "tx_collide_15times" },
371 { "tx_ucast_packets" },
372 { "tx_mcast_packets" },
373 { "tx_bcast_packets" },
374 { "tx_carrier_sense_errors" },
375 { "tx_discards" },
376 { "tx_errors" },
377
378 { "dma_writeq_full" },
379 { "dma_write_prioq_full" },
380 { "rxbds_empty" },
381 { "rx_discards" },
382 { "rx_errors" },
383 { "rx_threshold_hit" },
384
385 { "dma_readq_full" },
386 { "dma_read_prioq_full" },
387 { "tx_comp_queue_full" },
388
389 { "ring_set_send_prod_index" },
390 { "ring_status_update" },
391 { "nic_irqs" },
392 { "nic_avoided_irqs" },
Matt Carlson4452d092011-05-19 12:12:51 +0000393 { "nic_tx_threshold_hit" },
394
395 { "mbuf_lwm_thresh_hit" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396};
397
Matt Carlson48fa55a2011-04-13 11:05:06 +0000398#define TG3_NUM_STATS ARRAY_SIZE(ethtool_stats_keys)
399
400
Andreas Mohr50da8592006-08-14 23:54:30 -0700401static const struct {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700402 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000403} ethtool_test_keys[] = {
Matt Carlson28a45952011-08-19 13:58:22 +0000404 { "nvram test (online) " },
405 { "link test (online) " },
406 { "register test (offline)" },
407 { "memory test (offline)" },
408 { "mac loopback test (offline)" },
409 { "phy loopback test (offline)" },
Matt Carlson941ec902011-08-19 13:58:23 +0000410 { "ext loopback test (offline)" },
Matt Carlson28a45952011-08-19 13:58:22 +0000411 { "interrupt test (offline)" },
Michael Chan4cafd3f2005-05-29 14:56:34 -0700412};
413
Matt Carlson48fa55a2011-04-13 11:05:06 +0000414#define TG3_NUM_TEST ARRAY_SIZE(ethtool_test_keys)
415
416
Michael Chanb401e9e2005-12-19 16:27:04 -0800417static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
418{
419 writel(val, tp->regs + off);
420}
421
422static u32 tg3_read32(struct tg3 *tp, u32 off)
423{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000424 return readl(tp->regs + off);
Michael Chanb401e9e2005-12-19 16:27:04 -0800425}
426
Matt Carlson0d3031d2007-10-10 18:02:43 -0700427static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
428{
429 writel(val, tp->aperegs + off);
430}
431
432static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
433{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000434 return readl(tp->aperegs + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700435}
436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
438{
Michael Chan68929142005-08-09 20:17:14 -0700439 unsigned long flags;
440
441 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700442 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
443 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
Michael Chan68929142005-08-09 20:17:14 -0700444 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700445}
446
447static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
448{
449 writel(val, tp->regs + off);
450 readl(tp->regs + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451}
452
Michael Chan68929142005-08-09 20:17:14 -0700453static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
454{
455 unsigned long flags;
456 u32 val;
457
458 spin_lock_irqsave(&tp->indirect_lock, flags);
459 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
460 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
461 spin_unlock_irqrestore(&tp->indirect_lock, flags);
462 return val;
463}
464
465static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
466{
467 unsigned long flags;
468
469 if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
470 pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
471 TG3_64BIT_REG_LOW, val);
472 return;
473 }
Matt Carlson66711e62009-11-13 13:03:49 +0000474 if (off == TG3_RX_STD_PROD_IDX_REG) {
Michael Chan68929142005-08-09 20:17:14 -0700475 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
476 TG3_64BIT_REG_LOW, val);
477 return;
478 }
479
480 spin_lock_irqsave(&tp->indirect_lock, flags);
481 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
482 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
483 spin_unlock_irqrestore(&tp->indirect_lock, flags);
484
485 /* In indirect mode when disabling interrupts, we also need
486 * to clear the interrupt bit in the GRC local ctrl register.
487 */
488 if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
489 (val == 0x1)) {
490 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
491 tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
492 }
493}
494
495static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
496{
497 unsigned long flags;
498 u32 val;
499
500 spin_lock_irqsave(&tp->indirect_lock, flags);
501 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
502 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
503 spin_unlock_irqrestore(&tp->indirect_lock, flags);
504 return val;
505}
506
Michael Chanb401e9e2005-12-19 16:27:04 -0800507/* usec_wait specifies the wait time in usec when writing to certain registers
508 * where it is unsafe to read back the register without some delay.
509 * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
510 * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
511 */
512static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
Joe Perches63c3a662011-04-26 08:12:10 +0000514 if (tg3_flag(tp, PCIX_TARGET_HWBUG) || tg3_flag(tp, ICH_WORKAROUND))
Michael Chanb401e9e2005-12-19 16:27:04 -0800515 /* Non-posted methods */
516 tp->write32(tp, off, val);
517 else {
518 /* Posted method */
519 tg3_write32(tp, off, val);
520 if (usec_wait)
521 udelay(usec_wait);
522 tp->read32(tp, off);
523 }
524 /* Wait again after the read for the posted method to guarantee that
525 * the wait time is met.
526 */
527 if (usec_wait)
528 udelay(usec_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529}
530
Michael Chan09ee9292005-08-09 20:17:00 -0700531static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
532{
533 tp->write32_mbox(tp, off, val);
Joe Perches63c3a662011-04-26 08:12:10 +0000534 if (!tg3_flag(tp, MBOX_WRITE_REORDER) && !tg3_flag(tp, ICH_WORKAROUND))
Michael Chan68929142005-08-09 20:17:14 -0700535 tp->read32_mbox(tp, off);
Michael Chan09ee9292005-08-09 20:17:00 -0700536}
537
Michael Chan20094932005-08-09 20:16:32 -0700538static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
540 void __iomem *mbox = tp->regs + off;
541 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000542 if (tg3_flag(tp, TXD_MBOX_HWBUG))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000544 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 readl(mbox);
546}
547
Michael Chanb5d37722006-09-27 16:06:21 -0700548static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
549{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000550 return readl(tp->regs + off + GRCMBOX_BASE);
Michael Chanb5d37722006-09-27 16:06:21 -0700551}
552
553static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
554{
555 writel(val, tp->regs + off + GRCMBOX_BASE);
556}
557
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000558#define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700559#define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000560#define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
561#define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
562#define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
Michael Chan20094932005-08-09 20:16:32 -0700563
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000564#define tw32(reg, val) tp->write32(tp, reg, val)
565#define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
566#define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
567#define tr32(reg) tp->read32(tp, reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
570{
Michael Chan68929142005-08-09 20:17:14 -0700571 unsigned long flags;
572
Matt Carlson6ff6f812011-05-19 12:12:54 +0000573 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700574 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
575 return;
576
Michael Chan68929142005-08-09 20:17:14 -0700577 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000578 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700579 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
580 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Michael Chanbbadf502006-04-06 21:46:34 -0700582 /* Always leave this as zero. */
583 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
584 } else {
585 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
586 tw32_f(TG3PCI_MEM_WIN_DATA, val);
587
588 /* Always leave this as zero. */
589 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
590 }
Michael Chan68929142005-08-09 20:17:14 -0700591 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592}
593
594static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
595{
Michael Chan68929142005-08-09 20:17:14 -0700596 unsigned long flags;
597
Matt Carlson6ff6f812011-05-19 12:12:54 +0000598 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700599 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
600 *val = 0;
601 return;
602 }
603
Michael Chan68929142005-08-09 20:17:14 -0700604 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000605 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700606 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
607 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Michael Chanbbadf502006-04-06 21:46:34 -0700609 /* Always leave this as zero. */
610 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
611 } else {
612 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
613 *val = tr32(TG3PCI_MEM_WIN_DATA);
614
615 /* Always leave this as zero. */
616 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
617 }
Michael Chan68929142005-08-09 20:17:14 -0700618 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619}
620
Matt Carlson0d3031d2007-10-10 18:02:43 -0700621static void tg3_ape_lock_init(struct tg3 *tp)
622{
623 int i;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000624 u32 regbase, bit;
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000625
626 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
627 regbase = TG3_APE_LOCK_GRANT;
628 else
629 regbase = TG3_APE_PER_LOCK_GRANT;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700630
631 /* Make sure the driver hasn't any stale locks. */
Matt Carlson78f94dc2011-11-04 09:14:58 +0000632 for (i = TG3_APE_LOCK_PHY0; i <= TG3_APE_LOCK_GPIO; i++) {
633 switch (i) {
634 case TG3_APE_LOCK_PHY0:
635 case TG3_APE_LOCK_PHY1:
636 case TG3_APE_LOCK_PHY2:
637 case TG3_APE_LOCK_PHY3:
638 bit = APE_LOCK_GRANT_DRIVER;
639 break;
640 default:
641 if (!tp->pci_fn)
642 bit = APE_LOCK_GRANT_DRIVER;
643 else
644 bit = 1 << tp->pci_fn;
645 }
646 tg3_ape_write32(tp, regbase + 4 * i, bit);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000647 }
648
Matt Carlson0d3031d2007-10-10 18:02:43 -0700649}
650
651static int tg3_ape_lock(struct tg3 *tp, int locknum)
652{
653 int i, off;
654 int ret = 0;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000655 u32 status, req, gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700656
Joe Perches63c3a662011-04-26 08:12:10 +0000657 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700658 return 0;
659
660 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000661 case TG3_APE_LOCK_GPIO:
662 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
663 return 0;
Matt Carlson33f401a2010-04-05 10:19:27 +0000664 case TG3_APE_LOCK_GRC:
665 case TG3_APE_LOCK_MEM:
Matt Carlson78f94dc2011-11-04 09:14:58 +0000666 if (!tp->pci_fn)
667 bit = APE_LOCK_REQ_DRIVER;
668 else
669 bit = 1 << tp->pci_fn;
Matt Carlson33f401a2010-04-05 10:19:27 +0000670 break;
671 default:
672 return -EINVAL;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700673 }
674
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000675 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
676 req = TG3_APE_LOCK_REQ;
677 gnt = TG3_APE_LOCK_GRANT;
678 } else {
679 req = TG3_APE_PER_LOCK_REQ;
680 gnt = TG3_APE_PER_LOCK_GRANT;
681 }
682
Matt Carlson0d3031d2007-10-10 18:02:43 -0700683 off = 4 * locknum;
684
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000685 tg3_ape_write32(tp, req + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700686
687 /* Wait for up to 1 millisecond to acquire lock. */
688 for (i = 0; i < 100; i++) {
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000689 status = tg3_ape_read32(tp, gnt + off);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000690 if (status == bit)
Matt Carlson0d3031d2007-10-10 18:02:43 -0700691 break;
692 udelay(10);
693 }
694
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000695 if (status != bit) {
Matt Carlson0d3031d2007-10-10 18:02:43 -0700696 /* Revoke the lock request. */
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000697 tg3_ape_write32(tp, gnt + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700698 ret = -EBUSY;
699 }
700
701 return ret;
702}
703
704static void tg3_ape_unlock(struct tg3 *tp, int locknum)
705{
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000706 u32 gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700707
Joe Perches63c3a662011-04-26 08:12:10 +0000708 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700709 return;
710
711 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000712 case TG3_APE_LOCK_GPIO:
713 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
714 return;
Matt Carlson33f401a2010-04-05 10:19:27 +0000715 case TG3_APE_LOCK_GRC:
716 case TG3_APE_LOCK_MEM:
Matt Carlson78f94dc2011-11-04 09:14:58 +0000717 if (!tp->pci_fn)
718 bit = APE_LOCK_GRANT_DRIVER;
719 else
720 bit = 1 << tp->pci_fn;
Matt Carlson33f401a2010-04-05 10:19:27 +0000721 break;
722 default:
723 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700724 }
725
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000726 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
727 gnt = TG3_APE_LOCK_GRANT;
728 else
729 gnt = TG3_APE_PER_LOCK_GRANT;
730
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000731 tg3_ape_write32(tp, gnt + 4 * locknum, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700732}
733
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000734static void tg3_ape_send_event(struct tg3 *tp, u32 event)
735{
736 int i;
737 u32 apedata;
738
739 /* NCSI does not support APE events */
740 if (tg3_flag(tp, APE_HAS_NCSI))
741 return;
742
743 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
744 if (apedata != APE_SEG_SIG_MAGIC)
745 return;
746
747 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
748 if (!(apedata & APE_FW_STATUS_READY))
749 return;
750
751 /* Wait for up to 1 millisecond for APE to service previous event. */
752 for (i = 0; i < 10; i++) {
753 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
754 return;
755
756 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
757
758 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
759 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
760 event | APE_EVENT_STATUS_EVENT_PENDING);
761
762 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
763
764 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
765 break;
766
767 udelay(100);
768 }
769
770 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
771 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
772}
773
774static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
775{
776 u32 event;
777 u32 apedata;
778
779 if (!tg3_flag(tp, ENABLE_APE))
780 return;
781
782 switch (kind) {
783 case RESET_KIND_INIT:
784 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
785 APE_HOST_SEG_SIG_MAGIC);
786 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
787 APE_HOST_SEG_LEN_MAGIC);
788 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
789 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
790 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
791 APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
792 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
793 APE_HOST_BEHAV_NO_PHYLOCK);
794 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
795 TG3_APE_HOST_DRVR_STATE_START);
796
797 event = APE_EVENT_STATUS_STATE_START;
798 break;
799 case RESET_KIND_SHUTDOWN:
800 /* With the interface we are currently using,
801 * APE does not track driver state. Wiping
802 * out the HOST SEGMENT SIGNATURE forces
803 * the APE to assume OS absent status.
804 */
805 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
806
807 if (device_may_wakeup(&tp->pdev->dev) &&
808 tg3_flag(tp, WOL_ENABLE)) {
809 tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
810 TG3_APE_HOST_WOL_SPEED_AUTO);
811 apedata = TG3_APE_HOST_DRVR_STATE_WOL;
812 } else
813 apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
814
815 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
816
817 event = APE_EVENT_STATUS_STATE_UNLOAD;
818 break;
819 case RESET_KIND_SUSPEND:
820 event = APE_EVENT_STATUS_STATE_SUSPEND;
821 break;
822 default:
823 return;
824 }
825
826 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
827
828 tg3_ape_send_event(tp, event);
829}
830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831static void tg3_disable_ints(struct tg3 *tp)
832{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000833 int i;
834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 tw32(TG3PCI_MISC_HOST_CTRL,
836 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000837 for (i = 0; i < tp->irq_max; i++)
838 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839}
840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841static void tg3_enable_ints(struct tg3 *tp)
842{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000843 int i;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000844
Michael Chanbbe832c2005-06-24 20:20:04 -0700845 tp->irq_sync = 0;
846 wmb();
847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 tw32(TG3PCI_MISC_HOST_CTRL,
849 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000850
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000851 tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000852 for (i = 0; i < tp->irq_cnt; i++) {
853 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000854
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000855 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
Joe Perches63c3a662011-04-26 08:12:10 +0000856 if (tg3_flag(tp, 1SHOT_MSI))
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000857 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
858
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000859 tp->coal_now |= tnapi->coal_now;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000860 }
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000861
862 /* Force an initial interrupt */
Joe Perches63c3a662011-04-26 08:12:10 +0000863 if (!tg3_flag(tp, TAGGED_STATUS) &&
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000864 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
865 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
866 else
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000867 tw32(HOSTCC_MODE, tp->coal_now);
868
869 tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870}
871
Matt Carlson17375d22009-08-28 14:02:18 +0000872static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
Michael Chan04237dd2005-04-25 15:17:17 -0700873{
Matt Carlson17375d22009-08-28 14:02:18 +0000874 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +0000875 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan04237dd2005-04-25 15:17:17 -0700876 unsigned int work_exists = 0;
877
878 /* check for phy events */
Joe Perches63c3a662011-04-26 08:12:10 +0000879 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Michael Chan04237dd2005-04-25 15:17:17 -0700880 if (sblk->status & SD_STATUS_LINK_CHG)
881 work_exists = 1;
882 }
883 /* check for RX/TX work to do */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000884 if (sblk->idx[0].tx_consumer != tnapi->tx_cons ||
Matt Carlson8d9d7cf2009-09-01 13:19:05 +0000885 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -0700886 work_exists = 1;
887
888 return work_exists;
889}
890
Matt Carlson17375d22009-08-28 14:02:18 +0000891/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -0700892 * similar to tg3_enable_ints, but it accurately determines whether there
893 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400894 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 */
Matt Carlson17375d22009-08-28 14:02:18 +0000896static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
Matt Carlson17375d22009-08-28 14:02:18 +0000898 struct tg3 *tp = tnapi->tp;
899
Matt Carlson898a56f2009-08-28 14:02:40 +0000900 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 mmiowb();
902
David S. Millerfac9b832005-05-18 22:46:34 -0700903 /* When doing tagged status, this work check is unnecessary.
904 * The last_tag we write above tells the chip which piece of
905 * work we've completed.
906 */
Joe Perches63c3a662011-04-26 08:12:10 +0000907 if (!tg3_flag(tp, TAGGED_STATUS) && tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -0700908 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +0000909 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910}
911
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912static void tg3_switch_clocks(struct tg3 *tp)
913{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000914 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 u32 orig_clock_ctrl;
916
Joe Perches63c3a662011-04-26 08:12:10 +0000917 if (tg3_flag(tp, CPMU_PRESENT) || tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -0700918 return;
919
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000920 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
921
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 orig_clock_ctrl = clock_ctrl;
923 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
924 CLOCK_CTRL_CLKRUN_OENABLE |
925 0x1f);
926 tp->pci_clock_ctrl = clock_ctrl;
927
Joe Perches63c3a662011-04-26 08:12:10 +0000928 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800930 tw32_wait_f(TG3PCI_CLOCK_CTRL,
931 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 }
933 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800934 tw32_wait_f(TG3PCI_CLOCK_CTRL,
935 clock_ctrl |
936 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
937 40);
938 tw32_wait_f(TG3PCI_CLOCK_CTRL,
939 clock_ctrl | (CLOCK_CTRL_ALTCLK),
940 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 }
Michael Chanb401e9e2005-12-19 16:27:04 -0800942 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943}
944
945#define PHY_BUSY_LOOPS 5000
946
947static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
948{
949 u32 frame_val;
950 unsigned int loops;
951 int ret;
952
953 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
954 tw32_f(MAC_MI_MODE,
955 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
956 udelay(80);
957 }
958
959 *val = 0x0;
960
Matt Carlson882e9792009-09-01 13:21:36 +0000961 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 MI_COM_PHY_ADDR_MASK);
963 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
964 MI_COM_REG_ADDR_MASK);
965 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 tw32_f(MAC_MI_COM, frame_val);
968
969 loops = PHY_BUSY_LOOPS;
970 while (loops != 0) {
971 udelay(10);
972 frame_val = tr32(MAC_MI_COM);
973
974 if ((frame_val & MI_COM_BUSY) == 0) {
975 udelay(5);
976 frame_val = tr32(MAC_MI_COM);
977 break;
978 }
979 loops -= 1;
980 }
981
982 ret = -EBUSY;
983 if (loops != 0) {
984 *val = frame_val & MI_COM_DATA_MASK;
985 ret = 0;
986 }
987
988 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
989 tw32_f(MAC_MI_MODE, tp->mi_mode);
990 udelay(80);
991 }
992
993 return ret;
994}
995
996static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
997{
998 u32 frame_val;
999 unsigned int loops;
1000 int ret;
1001
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001002 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson221c5632011-06-13 13:39:01 +00001003 (reg == MII_CTRL1000 || reg == MII_TG3_AUX_CTRL))
Michael Chanb5d37722006-09-27 16:06:21 -07001004 return 0;
1005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1007 tw32_f(MAC_MI_MODE,
1008 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
1009 udelay(80);
1010 }
1011
Matt Carlson882e9792009-09-01 13:21:36 +00001012 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 MI_COM_PHY_ADDR_MASK);
1014 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
1015 MI_COM_REG_ADDR_MASK);
1016 frame_val |= (val & MI_COM_DATA_MASK);
1017 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 tw32_f(MAC_MI_COM, frame_val);
1020
1021 loops = PHY_BUSY_LOOPS;
1022 while (loops != 0) {
1023 udelay(10);
1024 frame_val = tr32(MAC_MI_COM);
1025 if ((frame_val & MI_COM_BUSY) == 0) {
1026 udelay(5);
1027 frame_val = tr32(MAC_MI_COM);
1028 break;
1029 }
1030 loops -= 1;
1031 }
1032
1033 ret = -EBUSY;
1034 if (loops != 0)
1035 ret = 0;
1036
1037 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1038 tw32_f(MAC_MI_MODE, tp->mi_mode);
1039 udelay(80);
1040 }
1041
1042 return ret;
1043}
1044
Matt Carlsonb0988c12011-04-20 07:57:39 +00001045static int tg3_phy_cl45_write(struct tg3 *tp, u32 devad, u32 addr, u32 val)
1046{
1047 int err;
1048
1049 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1050 if (err)
1051 goto done;
1052
1053 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1054 if (err)
1055 goto done;
1056
1057 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1058 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1059 if (err)
1060 goto done;
1061
1062 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, val);
1063
1064done:
1065 return err;
1066}
1067
1068static int tg3_phy_cl45_read(struct tg3 *tp, u32 devad, u32 addr, u32 *val)
1069{
1070 int err;
1071
1072 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1073 if (err)
1074 goto done;
1075
1076 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1077 if (err)
1078 goto done;
1079
1080 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1081 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1082 if (err)
1083 goto done;
1084
1085 err = tg3_readphy(tp, MII_TG3_MMD_ADDRESS, val);
1086
1087done:
1088 return err;
1089}
1090
1091static int tg3_phydsp_read(struct tg3 *tp, u32 reg, u32 *val)
1092{
1093 int err;
1094
1095 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1096 if (!err)
1097 err = tg3_readphy(tp, MII_TG3_DSP_RW_PORT, val);
1098
1099 return err;
1100}
1101
1102static int tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
1103{
1104 int err;
1105
1106 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1107 if (!err)
1108 err = tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
1109
1110 return err;
1111}
1112
Matt Carlson15ee95c2011-04-20 07:57:40 +00001113static int tg3_phy_auxctl_read(struct tg3 *tp, int reg, u32 *val)
1114{
1115 int err;
1116
1117 err = tg3_writephy(tp, MII_TG3_AUX_CTRL,
1118 (reg << MII_TG3_AUXCTL_MISC_RDSEL_SHIFT) |
1119 MII_TG3_AUXCTL_SHDWSEL_MISC);
1120 if (!err)
1121 err = tg3_readphy(tp, MII_TG3_AUX_CTRL, val);
1122
1123 return err;
1124}
1125
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001126static int tg3_phy_auxctl_write(struct tg3 *tp, int reg, u32 set)
1127{
1128 if (reg == MII_TG3_AUXCTL_SHDWSEL_MISC)
1129 set |= MII_TG3_AUXCTL_MISC_WREN;
1130
1131 return tg3_writephy(tp, MII_TG3_AUX_CTRL, set | reg);
1132}
1133
Matt Carlson1d36ba42011-04-20 07:57:42 +00001134#define TG3_PHY_AUXCTL_SMDSP_ENABLE(tp) \
1135 tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
1136 MII_TG3_AUXCTL_ACTL_SMDSP_ENA | \
1137 MII_TG3_AUXCTL_ACTL_TX_6DB)
1138
1139#define TG3_PHY_AUXCTL_SMDSP_DISABLE(tp) \
1140 tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
1141 MII_TG3_AUXCTL_ACTL_TX_6DB);
1142
Matt Carlson95e28692008-05-25 23:44:14 -07001143static int tg3_bmcr_reset(struct tg3 *tp)
1144{
1145 u32 phy_control;
1146 int limit, err;
1147
1148 /* OK, reset it, and poll the BMCR_RESET bit until it
1149 * clears or we time out.
1150 */
1151 phy_control = BMCR_RESET;
1152 err = tg3_writephy(tp, MII_BMCR, phy_control);
1153 if (err != 0)
1154 return -EBUSY;
1155
1156 limit = 5000;
1157 while (limit--) {
1158 err = tg3_readphy(tp, MII_BMCR, &phy_control);
1159 if (err != 0)
1160 return -EBUSY;
1161
1162 if ((phy_control & BMCR_RESET) == 0) {
1163 udelay(40);
1164 break;
1165 }
1166 udelay(10);
1167 }
Roel Kluind4675b52009-02-12 16:33:27 -08001168 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -07001169 return -EBUSY;
1170
1171 return 0;
1172}
1173
Matt Carlson158d7ab2008-05-29 01:37:54 -07001174static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
1175{
Francois Romieu3d165432009-01-19 16:56:50 -08001176 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001177 u32 val;
1178
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001179 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001180
1181 if (tg3_readphy(tp, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001182 val = -EIO;
1183
1184 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001185
1186 return val;
1187}
1188
1189static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
1190{
Francois Romieu3d165432009-01-19 16:56:50 -08001191 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001192 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001193
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001194 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001195
1196 if (tg3_writephy(tp, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001197 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001198
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001199 spin_unlock_bh(&tp->lock);
1200
1201 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001202}
1203
1204static int tg3_mdio_reset(struct mii_bus *bp)
1205{
1206 return 0;
1207}
1208
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001209static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -07001210{
1211 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001212 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -07001213
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001214 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001215 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001216 case PHY_ID_BCM50610:
1217 case PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001218 val = MAC_PHYCFG2_50610_LED_MODES;
1219 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001220 case PHY_ID_BCMAC131:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001221 val = MAC_PHYCFG2_AC131_LED_MODES;
1222 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001223 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001224 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
1225 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001226 case PHY_ID_RTL8201E:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001227 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
1228 break;
1229 default:
Matt Carlsona9daf362008-05-25 23:49:44 -07001230 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001231 }
1232
1233 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
1234 tw32(MAC_PHYCFG2, val);
1235
1236 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001237 val &= ~(MAC_PHYCFG1_RGMII_INT |
1238 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
1239 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001240 tw32(MAC_PHYCFG1, val);
1241
1242 return;
1243 }
1244
Joe Perches63c3a662011-04-26 08:12:10 +00001245 if (!tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001246 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
1247 MAC_PHYCFG2_FMODE_MASK_MASK |
1248 MAC_PHYCFG2_GMODE_MASK_MASK |
1249 MAC_PHYCFG2_ACT_MASK_MASK |
1250 MAC_PHYCFG2_QUAL_MASK_MASK |
1251 MAC_PHYCFG2_INBAND_ENABLE;
1252
1253 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001254
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001255 val = tr32(MAC_PHYCFG1);
1256 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
1257 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
Joe Perches63c3a662011-04-26 08:12:10 +00001258 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1259 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001260 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
Joe Perches63c3a662011-04-26 08:12:10 +00001261 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001262 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1263 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001264 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1265 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1266 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001267
Matt Carlsona9daf362008-05-25 23:49:44 -07001268 val = tr32(MAC_EXT_RGMII_MODE);
1269 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1270 MAC_RGMII_MODE_RX_QUALITY |
1271 MAC_RGMII_MODE_RX_ACTIVITY |
1272 MAC_RGMII_MODE_RX_ENG_DET |
1273 MAC_RGMII_MODE_TX_ENABLE |
1274 MAC_RGMII_MODE_TX_LOWPWR |
1275 MAC_RGMII_MODE_TX_RESET);
Joe Perches63c3a662011-04-26 08:12:10 +00001276 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1277 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001278 val |= MAC_RGMII_MODE_RX_INT_B |
1279 MAC_RGMII_MODE_RX_QUALITY |
1280 MAC_RGMII_MODE_RX_ACTIVITY |
1281 MAC_RGMII_MODE_RX_ENG_DET;
Joe Perches63c3a662011-04-26 08:12:10 +00001282 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001283 val |= MAC_RGMII_MODE_TX_ENABLE |
1284 MAC_RGMII_MODE_TX_LOWPWR |
1285 MAC_RGMII_MODE_TX_RESET;
1286 }
1287 tw32(MAC_EXT_RGMII_MODE, val);
1288}
1289
Matt Carlson158d7ab2008-05-29 01:37:54 -07001290static void tg3_mdio_start(struct tg3 *tp)
1291{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001292 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1293 tw32_f(MAC_MI_MODE, tp->mi_mode);
1294 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001295
Joe Perches63c3a662011-04-26 08:12:10 +00001296 if (tg3_flag(tp, MDIOBUS_INITED) &&
Matt Carlson9ea48182010-02-17 15:17:01 +00001297 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1298 tg3_mdio_config_5785(tp);
1299}
1300
1301static int tg3_mdio_init(struct tg3 *tp)
1302{
1303 int i;
1304 u32 reg;
1305 struct phy_device *phydev;
1306
Joe Perches63c3a662011-04-26 08:12:10 +00001307 if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson9c7df912010-06-05 17:24:36 +00001308 u32 is_serdes;
Matt Carlson882e9792009-09-01 13:21:36 +00001309
Matt Carlson69f11c92011-07-13 09:27:30 +00001310 tp->phy_addr = tp->pci_fn + 1;
Matt Carlson882e9792009-09-01 13:21:36 +00001311
Matt Carlsond1ec96a2010-01-12 10:11:38 +00001312 if (tp->pci_chip_rev_id != CHIPREV_ID_5717_A0)
1313 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1314 else
1315 is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1316 TG3_CPMU_PHY_STRAP_IS_SERDES;
Matt Carlson882e9792009-09-01 13:21:36 +00001317 if (is_serdes)
1318 tp->phy_addr += 7;
1319 } else
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001320 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001321
Matt Carlson158d7ab2008-05-29 01:37:54 -07001322 tg3_mdio_start(tp);
1323
Joe Perches63c3a662011-04-26 08:12:10 +00001324 if (!tg3_flag(tp, USE_PHYLIB) || tg3_flag(tp, MDIOBUS_INITED))
Matt Carlson158d7ab2008-05-29 01:37:54 -07001325 return 0;
1326
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001327 tp->mdio_bus = mdiobus_alloc();
1328 if (tp->mdio_bus == NULL)
1329 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001330
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001331 tp->mdio_bus->name = "tg3 mdio bus";
1332 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001333 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001334 tp->mdio_bus->priv = tp;
1335 tp->mdio_bus->parent = &tp->pdev->dev;
1336 tp->mdio_bus->read = &tg3_mdio_read;
1337 tp->mdio_bus->write = &tg3_mdio_write;
1338 tp->mdio_bus->reset = &tg3_mdio_reset;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001339 tp->mdio_bus->phy_mask = ~(1 << TG3_PHY_MII_ADDR);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001340 tp->mdio_bus->irq = &tp->mdio_irq[0];
Matt Carlson158d7ab2008-05-29 01:37:54 -07001341
1342 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001343 tp->mdio_bus->irq[i] = PHY_POLL;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001344
1345 /* The bus registration will look for all the PHYs on the mdio bus.
1346 * Unfortunately, it does not ensure the PHY is powered up before
1347 * accessing the PHY ID registers. A chip reset is the
1348 * quickest way to bring the device back to an operational state..
1349 */
1350 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1351 tg3_bmcr_reset(tp);
1352
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001353 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001354 if (i) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001355 dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001356 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001357 return i;
1358 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001359
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001360 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsona9daf362008-05-25 23:49:44 -07001361
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001362 if (!phydev || !phydev->drv) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001363 dev_warn(&tp->pdev->dev, "No PHY devices\n");
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001364 mdiobus_unregister(tp->mdio_bus);
1365 mdiobus_free(tp->mdio_bus);
1366 return -ENODEV;
1367 }
1368
1369 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001370 case PHY_ID_BCM57780:
Matt Carlson321d32a2008-11-21 17:22:19 -08001371 phydev->interface = PHY_INTERFACE_MODE_GMII;
Matt Carlsonc704dc22009-11-02 14:32:12 +00001372 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08001373 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001374 case PHY_ID_BCM50610:
1375 case PHY_ID_BCM50610M:
Matt Carlson32e5a8d2009-11-02 14:31:39 +00001376 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001377 PHY_BRCM_RX_REFCLK_UNUSED |
Matt Carlson52fae082009-11-02 14:32:38 +00001378 PHY_BRCM_DIS_TXCRXC_NOENRGY |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001379 PHY_BRCM_AUTO_PWRDWN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001380 if (tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsona9daf362008-05-25 23:49:44 -07001381 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001382 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001383 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001384 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001385 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001386 /* fallthru */
Matt Carlson6a443a02010-02-17 15:17:04 +00001387 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001388 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001389 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001390 case PHY_ID_RTL8201E:
1391 case PHY_ID_BCMAC131:
Matt Carlsona9daf362008-05-25 23:49:44 -07001392 phydev->interface = PHY_INTERFACE_MODE_MII;
Matt Carlsoncdd4e09d2009-11-02 14:31:11 +00001393 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001394 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlsona9daf362008-05-25 23:49:44 -07001395 break;
1396 }
1397
Joe Perches63c3a662011-04-26 08:12:10 +00001398 tg3_flag_set(tp, MDIOBUS_INITED);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001399
1400 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1401 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001402
1403 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001404}
1405
1406static void tg3_mdio_fini(struct tg3 *tp)
1407{
Joe Perches63c3a662011-04-26 08:12:10 +00001408 if (tg3_flag(tp, MDIOBUS_INITED)) {
1409 tg3_flag_clear(tp, MDIOBUS_INITED);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001410 mdiobus_unregister(tp->mdio_bus);
1411 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001412 }
1413}
1414
Matt Carlson95e28692008-05-25 23:44:14 -07001415/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001416static inline void tg3_generate_fw_event(struct tg3 *tp)
1417{
1418 u32 val;
1419
1420 val = tr32(GRC_RX_CPU_EVENT);
1421 val |= GRC_RX_CPU_DRIVER_EVENT;
1422 tw32_f(GRC_RX_CPU_EVENT, val);
1423
1424 tp->last_event_jiffies = jiffies;
1425}
1426
1427#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1428
1429/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001430static void tg3_wait_for_event_ack(struct tg3 *tp)
1431{
1432 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001433 unsigned int delay_cnt;
1434 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001435
Matt Carlson4ba526c2008-08-15 14:10:04 -07001436 /* If enough time has passed, no wait is necessary. */
1437 time_remain = (long)(tp->last_event_jiffies + 1 +
1438 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1439 (long)jiffies;
1440 if (time_remain < 0)
1441 return;
1442
1443 /* Check if we can shorten the wait time. */
1444 delay_cnt = jiffies_to_usecs(time_remain);
1445 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1446 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1447 delay_cnt = (delay_cnt >> 3) + 1;
1448
1449 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001450 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1451 break;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001452 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001453 }
1454}
1455
1456/* tp->lock is held. */
1457static void tg3_ump_link_report(struct tg3 *tp)
1458{
1459 u32 reg;
1460 u32 val;
1461
Joe Perches63c3a662011-04-26 08:12:10 +00001462 if (!tg3_flag(tp, 5780_CLASS) || !tg3_flag(tp, ENABLE_ASF))
Matt Carlson95e28692008-05-25 23:44:14 -07001463 return;
1464
1465 tg3_wait_for_event_ack(tp);
1466
1467 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1468
1469 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1470
1471 val = 0;
1472 if (!tg3_readphy(tp, MII_BMCR, &reg))
1473 val = reg << 16;
1474 if (!tg3_readphy(tp, MII_BMSR, &reg))
1475 val |= (reg & 0xffff);
1476 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX, val);
1477
1478 val = 0;
1479 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1480 val = reg << 16;
1481 if (!tg3_readphy(tp, MII_LPA, &reg))
1482 val |= (reg & 0xffff);
1483 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 4, val);
1484
1485 val = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001486 if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
Matt Carlson95e28692008-05-25 23:44:14 -07001487 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1488 val = reg << 16;
1489 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1490 val |= (reg & 0xffff);
1491 }
1492 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 8, val);
1493
1494 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1495 val = reg << 16;
1496 else
1497 val = 0;
1498 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 12, val);
1499
Matt Carlson4ba526c2008-08-15 14:10:04 -07001500 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001501}
1502
Matt Carlson8d5a89b2011-08-31 11:44:51 +00001503/* tp->lock is held. */
1504static void tg3_stop_fw(struct tg3 *tp)
1505{
1506 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
1507 /* Wait for RX cpu to ACK the previous event. */
1508 tg3_wait_for_event_ack(tp);
1509
1510 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
1511
1512 tg3_generate_fw_event(tp);
1513
1514 /* Wait for RX cpu to ACK this event. */
1515 tg3_wait_for_event_ack(tp);
1516 }
1517}
1518
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001519/* tp->lock is held. */
1520static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
1521{
1522 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
1523 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
1524
1525 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1526 switch (kind) {
1527 case RESET_KIND_INIT:
1528 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1529 DRV_STATE_START);
1530 break;
1531
1532 case RESET_KIND_SHUTDOWN:
1533 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1534 DRV_STATE_UNLOAD);
1535 break;
1536
1537 case RESET_KIND_SUSPEND:
1538 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1539 DRV_STATE_SUSPEND);
1540 break;
1541
1542 default:
1543 break;
1544 }
1545 }
1546
1547 if (kind == RESET_KIND_INIT ||
1548 kind == RESET_KIND_SUSPEND)
1549 tg3_ape_driver_state_change(tp, kind);
1550}
1551
1552/* tp->lock is held. */
1553static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
1554{
1555 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1556 switch (kind) {
1557 case RESET_KIND_INIT:
1558 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1559 DRV_STATE_START_DONE);
1560 break;
1561
1562 case RESET_KIND_SHUTDOWN:
1563 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1564 DRV_STATE_UNLOAD_DONE);
1565 break;
1566
1567 default:
1568 break;
1569 }
1570 }
1571
1572 if (kind == RESET_KIND_SHUTDOWN)
1573 tg3_ape_driver_state_change(tp, kind);
1574}
1575
1576/* tp->lock is held. */
1577static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
1578{
1579 if (tg3_flag(tp, ENABLE_ASF)) {
1580 switch (kind) {
1581 case RESET_KIND_INIT:
1582 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1583 DRV_STATE_START);
1584 break;
1585
1586 case RESET_KIND_SHUTDOWN:
1587 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1588 DRV_STATE_UNLOAD);
1589 break;
1590
1591 case RESET_KIND_SUSPEND:
1592 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1593 DRV_STATE_SUSPEND);
1594 break;
1595
1596 default:
1597 break;
1598 }
1599 }
1600}
1601
1602static int tg3_poll_fw(struct tg3 *tp)
1603{
1604 int i;
1605 u32 val;
1606
1607 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
1608 /* Wait up to 20ms for init done. */
1609 for (i = 0; i < 200; i++) {
1610 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
1611 return 0;
1612 udelay(100);
1613 }
1614 return -ENODEV;
1615 }
1616
1617 /* Wait for firmware initialization to complete. */
1618 for (i = 0; i < 100000; i++) {
1619 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
1620 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
1621 break;
1622 udelay(10);
1623 }
1624
1625 /* Chip might not be fitted with firmware. Some Sun onboard
1626 * parts are configured like that. So don't signal the timeout
1627 * of the above loop as an error, but do report the lack of
1628 * running firmware once.
1629 */
1630 if (i >= 100000 && !tg3_flag(tp, NO_FWARE_REPORTED)) {
1631 tg3_flag_set(tp, NO_FWARE_REPORTED);
1632
1633 netdev_info(tp->dev, "No firmware running\n");
1634 }
1635
1636 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
1637 /* The 57765 A0 needs a little more
1638 * time to do some important work.
1639 */
1640 mdelay(10);
1641 }
1642
1643 return 0;
1644}
1645
Matt Carlson95e28692008-05-25 23:44:14 -07001646static void tg3_link_report(struct tg3 *tp)
1647{
1648 if (!netif_carrier_ok(tp->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001649 netif_info(tp, link, tp->dev, "Link is down\n");
Matt Carlson95e28692008-05-25 23:44:14 -07001650 tg3_ump_link_report(tp);
1651 } else if (netif_msg_link(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001652 netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
1653 (tp->link_config.active_speed == SPEED_1000 ?
1654 1000 :
1655 (tp->link_config.active_speed == SPEED_100 ?
1656 100 : 10)),
1657 (tp->link_config.active_duplex == DUPLEX_FULL ?
1658 "full" : "half"));
Matt Carlson95e28692008-05-25 23:44:14 -07001659
Joe Perches05dbe002010-02-17 19:44:19 +00001660 netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
1661 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
1662 "on" : "off",
1663 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
1664 "on" : "off");
Matt Carlson47007832011-04-20 07:57:43 +00001665
1666 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
1667 netdev_info(tp->dev, "EEE is %s\n",
1668 tp->setlpicnt ? "enabled" : "disabled");
1669
Matt Carlson95e28692008-05-25 23:44:14 -07001670 tg3_ump_link_report(tp);
1671 }
1672}
1673
1674static u16 tg3_advert_flowctrl_1000T(u8 flow_ctrl)
1675{
1676 u16 miireg;
1677
Steve Glendinninge18ce342008-12-16 02:00:00 -08001678 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001679 miireg = ADVERTISE_PAUSE_CAP;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001680 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001681 miireg = ADVERTISE_PAUSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001682 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001683 miireg = ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1684 else
1685 miireg = 0;
1686
1687 return miireg;
1688}
1689
1690static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1691{
1692 u16 miireg;
1693
Steve Glendinninge18ce342008-12-16 02:00:00 -08001694 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001695 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001696 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001697 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001698 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001699 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1700 else
1701 miireg = 0;
1702
1703 return miireg;
1704}
1705
Matt Carlson95e28692008-05-25 23:44:14 -07001706static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1707{
1708 u8 cap = 0;
1709
Matt Carlsonf3791cd2011-11-21 15:01:17 +00001710 if (lcladv & rmtadv & ADVERTISE_1000XPAUSE) {
1711 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
1712 } else if (lcladv & rmtadv & ADVERTISE_1000XPSE_ASYM) {
1713 if (lcladv & ADVERTISE_1000XPAUSE)
1714 cap = FLOW_CTRL_RX;
1715 if (rmtadv & ADVERTISE_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001716 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001717 }
1718
1719 return cap;
1720}
1721
Matt Carlsonf51f3562008-05-25 23:45:08 -07001722static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001723{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001724 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001725 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001726 u32 old_rx_mode = tp->rx_mode;
1727 u32 old_tx_mode = tp->tx_mode;
1728
Joe Perches63c3a662011-04-26 08:12:10 +00001729 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001730 autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001731 else
1732 autoneg = tp->link_config.autoneg;
1733
Joe Perches63c3a662011-04-26 08:12:10 +00001734 if (autoneg == AUTONEG_ENABLE && tg3_flag(tp, PAUSE_AUTONEG)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001735 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001736 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001737 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001738 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001739 } else
1740 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001741
Matt Carlsonf51f3562008-05-25 23:45:08 -07001742 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001743
Steve Glendinninge18ce342008-12-16 02:00:00 -08001744 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001745 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1746 else
1747 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1748
Matt Carlsonf51f3562008-05-25 23:45:08 -07001749 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001750 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001751
Steve Glendinninge18ce342008-12-16 02:00:00 -08001752 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001753 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1754 else
1755 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1756
Matt Carlsonf51f3562008-05-25 23:45:08 -07001757 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001758 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001759}
1760
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001761static void tg3_adjust_link(struct net_device *dev)
1762{
1763 u8 oldflowctrl, linkmesg = 0;
1764 u32 mac_mode, lcl_adv, rmt_adv;
1765 struct tg3 *tp = netdev_priv(dev);
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001766 struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001767
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001768 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001769
1770 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
1771 MAC_MODE_HALF_DUPLEX);
1772
1773 oldflowctrl = tp->link_config.active_flowctrl;
1774
1775 if (phydev->link) {
1776 lcl_adv = 0;
1777 rmt_adv = 0;
1778
1779 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
1780 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001781 else if (phydev->speed == SPEED_1000 ||
1782 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001783 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001784 else
1785 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001786
1787 if (phydev->duplex == DUPLEX_HALF)
1788 mac_mode |= MAC_MODE_HALF_DUPLEX;
1789 else {
1790 lcl_adv = tg3_advert_flowctrl_1000T(
1791 tp->link_config.flowctrl);
1792
1793 if (phydev->pause)
1794 rmt_adv = LPA_PAUSE_CAP;
1795 if (phydev->asym_pause)
1796 rmt_adv |= LPA_PAUSE_ASYM;
1797 }
1798
1799 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
1800 } else
1801 mac_mode |= MAC_MODE_PORT_MODE_GMII;
1802
1803 if (mac_mode != tp->mac_mode) {
1804 tp->mac_mode = mac_mode;
1805 tw32_f(MAC_MODE, tp->mac_mode);
1806 udelay(40);
1807 }
1808
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001809 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
1810 if (phydev->speed == SPEED_10)
1811 tw32(MAC_MI_STAT,
1812 MAC_MI_STAT_10MBPS_MODE |
1813 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1814 else
1815 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1816 }
1817
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001818 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
1819 tw32(MAC_TX_LENGTHS,
1820 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1821 (6 << TX_LENGTHS_IPG_SHIFT) |
1822 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
1823 else
1824 tw32(MAC_TX_LENGTHS,
1825 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1826 (6 << TX_LENGTHS_IPG_SHIFT) |
1827 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
1828
1829 if ((phydev->link && tp->link_config.active_speed == SPEED_INVALID) ||
1830 (!phydev->link && tp->link_config.active_speed != SPEED_INVALID) ||
1831 phydev->speed != tp->link_config.active_speed ||
1832 phydev->duplex != tp->link_config.active_duplex ||
1833 oldflowctrl != tp->link_config.active_flowctrl)
Matt Carlsonc6cdf432010-04-05 10:19:26 +00001834 linkmesg = 1;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001835
1836 tp->link_config.active_speed = phydev->speed;
1837 tp->link_config.active_duplex = phydev->duplex;
1838
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001839 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001840
1841 if (linkmesg)
1842 tg3_link_report(tp);
1843}
1844
1845static int tg3_phy_init(struct tg3 *tp)
1846{
1847 struct phy_device *phydev;
1848
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001849 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001850 return 0;
1851
1852 /* Bring the PHY back to a known state. */
1853 tg3_bmcr_reset(tp);
1854
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001855 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001856
1857 /* Attach the MAC to the PHY. */
Kay Sieversfb28ad32008-11-10 13:55:14 -08001858 phydev = phy_connect(tp->dev, dev_name(&phydev->dev), tg3_adjust_link,
Matt Carlsona9daf362008-05-25 23:49:44 -07001859 phydev->dev_flags, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001860 if (IS_ERR(phydev)) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001861 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001862 return PTR_ERR(phydev);
1863 }
1864
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001865 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001866 switch (phydev->interface) {
1867 case PHY_INTERFACE_MODE_GMII:
1868 case PHY_INTERFACE_MODE_RGMII:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001869 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Matt Carlson321d32a2008-11-21 17:22:19 -08001870 phydev->supported &= (PHY_GBIT_FEATURES |
1871 SUPPORTED_Pause |
1872 SUPPORTED_Asym_Pause);
1873 break;
1874 }
1875 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001876 case PHY_INTERFACE_MODE_MII:
1877 phydev->supported &= (PHY_BASIC_FEATURES |
1878 SUPPORTED_Pause |
1879 SUPPORTED_Asym_Pause);
1880 break;
1881 default:
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001882 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001883 return -EINVAL;
1884 }
1885
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001886 tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001887
1888 phydev->advertising = phydev->supported;
1889
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001890 return 0;
1891}
1892
1893static void tg3_phy_start(struct tg3 *tp)
1894{
1895 struct phy_device *phydev;
1896
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001897 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001898 return;
1899
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001900 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001901
Matt Carlson80096062010-08-02 11:26:06 +00001902 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
1903 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001904 phydev->speed = tp->link_config.orig_speed;
1905 phydev->duplex = tp->link_config.orig_duplex;
1906 phydev->autoneg = tp->link_config.orig_autoneg;
1907 phydev->advertising = tp->link_config.orig_advertising;
1908 }
1909
1910 phy_start(phydev);
1911
1912 phy_start_aneg(phydev);
1913}
1914
1915static void tg3_phy_stop(struct tg3 *tp)
1916{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001917 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001918 return;
1919
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001920 phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001921}
1922
1923static void tg3_phy_fini(struct tg3 *tp)
1924{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001925 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001926 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001927 tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001928 }
1929}
1930
Matt Carlson941ec902011-08-19 13:58:23 +00001931static int tg3_phy_set_extloopbk(struct tg3 *tp)
1932{
1933 int err;
1934 u32 val;
1935
1936 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
1937 return 0;
1938
1939 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
1940 /* Cannot do read-modify-write on 5401 */
1941 err = tg3_phy_auxctl_write(tp,
1942 MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
1943 MII_TG3_AUXCTL_ACTL_EXTLOOPBK |
1944 0x4c20);
1945 goto done;
1946 }
1947
1948 err = tg3_phy_auxctl_read(tp,
1949 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
1950 if (err)
1951 return err;
1952
1953 val |= MII_TG3_AUXCTL_ACTL_EXTLOOPBK;
1954 err = tg3_phy_auxctl_write(tp,
1955 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, val);
1956
1957done:
1958 return err;
1959}
1960
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001961static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
1962{
1963 u32 phytest;
1964
1965 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
1966 u32 phy;
1967
1968 tg3_writephy(tp, MII_TG3_FET_TEST,
1969 phytest | MII_TG3_FET_SHADOW_EN);
1970 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
1971 if (enable)
1972 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
1973 else
1974 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
1975 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
1976 }
1977 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
1978 }
1979}
1980
Matt Carlson6833c042008-11-21 17:18:59 -08001981static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
1982{
1983 u32 reg;
1984
Joe Perches63c3a662011-04-26 08:12:10 +00001985 if (!tg3_flag(tp, 5705_PLUS) ||
1986 (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001987 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08001988 return;
1989
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001990 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001991 tg3_phy_fet_toggle_apd(tp, enable);
1992 return;
1993 }
1994
Matt Carlson6833c042008-11-21 17:18:59 -08001995 reg = MII_TG3_MISC_SHDW_WREN |
1996 MII_TG3_MISC_SHDW_SCR5_SEL |
1997 MII_TG3_MISC_SHDW_SCR5_LPED |
1998 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
1999 MII_TG3_MISC_SHDW_SCR5_SDTL |
2000 MII_TG3_MISC_SHDW_SCR5_C125OE;
2001 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 || !enable)
2002 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
2003
2004 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
2005
2006
2007 reg = MII_TG3_MISC_SHDW_WREN |
2008 MII_TG3_MISC_SHDW_APD_SEL |
2009 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
2010 if (enable)
2011 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
2012
2013 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
2014}
2015
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002016static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
2017{
2018 u32 phy;
2019
Joe Perches63c3a662011-04-26 08:12:10 +00002020 if (!tg3_flag(tp, 5705_PLUS) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002021 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002022 return;
2023
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002024 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002025 u32 ephy;
2026
Matt Carlson535ef6e2009-08-25 10:09:36 +00002027 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
2028 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
2029
2030 tg3_writephy(tp, MII_TG3_FET_TEST,
2031 ephy | MII_TG3_FET_SHADOW_EN);
2032 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002033 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00002034 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002035 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00002036 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
2037 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002038 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00002039 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002040 }
2041 } else {
Matt Carlson15ee95c2011-04-20 07:57:40 +00002042 int ret;
2043
2044 ret = tg3_phy_auxctl_read(tp,
2045 MII_TG3_AUXCTL_SHDWSEL_MISC, &phy);
2046 if (!ret) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002047 if (enable)
2048 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
2049 else
2050 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002051 tg3_phy_auxctl_write(tp,
2052 MII_TG3_AUXCTL_SHDWSEL_MISC, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002053 }
2054 }
2055}
2056
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057static void tg3_phy_set_wirespeed(struct tg3 *tp)
2058{
Matt Carlson15ee95c2011-04-20 07:57:40 +00002059 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 u32 val;
2061
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002062 if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 return;
2064
Matt Carlson15ee95c2011-04-20 07:57:40 +00002065 ret = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_MISC, &val);
2066 if (!ret)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002067 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_MISC,
2068 val | MII_TG3_AUXCTL_MISC_WIRESPD_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069}
2070
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002071static void tg3_phy_apply_otp(struct tg3 *tp)
2072{
2073 u32 otp, phy;
2074
2075 if (!tp->phy_otp)
2076 return;
2077
2078 otp = tp->phy_otp;
2079
Matt Carlson1d36ba42011-04-20 07:57:42 +00002080 if (TG3_PHY_AUXCTL_SMDSP_ENABLE(tp))
2081 return;
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002082
2083 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
2084 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
2085 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
2086
2087 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
2088 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
2089 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
2090
2091 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
2092 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
2093 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
2094
2095 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
2096 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
2097
2098 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
2099 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
2100
2101 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
2102 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
2103 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
2104
Matt Carlson1d36ba42011-04-20 07:57:42 +00002105 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002106}
2107
Matt Carlson52b02d02010-10-14 10:37:41 +00002108static void tg3_phy_eee_adjust(struct tg3 *tp, u32 current_link_up)
2109{
2110 u32 val;
2111
2112 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
2113 return;
2114
2115 tp->setlpicnt = 0;
2116
2117 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
2118 current_link_up == 1 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +00002119 tp->link_config.active_duplex == DUPLEX_FULL &&
2120 (tp->link_config.active_speed == SPEED_100 ||
2121 tp->link_config.active_speed == SPEED_1000)) {
Matt Carlson52b02d02010-10-14 10:37:41 +00002122 u32 eeectl;
2123
2124 if (tp->link_config.active_speed == SPEED_1000)
2125 eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
2126 else
2127 eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
2128
2129 tw32(TG3_CPMU_EEE_CTRL, eeectl);
2130
Matt Carlson3110f5f52010-12-06 08:28:50 +00002131 tg3_phy_cl45_read(tp, MDIO_MMD_AN,
2132 TG3_CL45_D7_EEERES_STAT, &val);
Matt Carlson52b02d02010-10-14 10:37:41 +00002133
Matt Carlsonb0c59432011-05-19 12:12:48 +00002134 if (val == TG3_CL45_D7_EEERES_STAT_LP_1000T ||
2135 val == TG3_CL45_D7_EEERES_STAT_LP_100TX)
Matt Carlson52b02d02010-10-14 10:37:41 +00002136 tp->setlpicnt = 2;
2137 }
2138
2139 if (!tp->setlpicnt) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002140 if (current_link_up == 1 &&
2141 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2142 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0000);
2143 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2144 }
2145
Matt Carlson52b02d02010-10-14 10:37:41 +00002146 val = tr32(TG3_CPMU_EEE_MODE);
2147 tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
2148 }
2149}
2150
Matt Carlsonb0c59432011-05-19 12:12:48 +00002151static void tg3_phy_eee_enable(struct tg3 *tp)
2152{
2153 u32 val;
2154
2155 if (tp->link_config.active_speed == SPEED_1000 &&
2156 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2157 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00002158 tg3_flag(tp, 57765_CLASS)) &&
Matt Carlsonb0c59432011-05-19 12:12:48 +00002159 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002160 val = MII_TG3_DSP_TAP26_ALNOKO |
2161 MII_TG3_DSP_TAP26_RMRXSTO;
2162 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
Matt Carlsonb0c59432011-05-19 12:12:48 +00002163 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2164 }
2165
2166 val = tr32(TG3_CPMU_EEE_MODE);
2167 tw32(TG3_CPMU_EEE_MODE, val | TG3_CPMU_EEEMD_LPI_ENABLE);
2168}
2169
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170static int tg3_wait_macro_done(struct tg3 *tp)
2171{
2172 int limit = 100;
2173
2174 while (limit--) {
2175 u32 tmp32;
2176
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002177 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 if ((tmp32 & 0x1000) == 0)
2179 break;
2180 }
2181 }
Roel Kluind4675b52009-02-12 16:33:27 -08002182 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 return -EBUSY;
2184
2185 return 0;
2186}
2187
2188static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
2189{
2190 static const u32 test_pat[4][6] = {
2191 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
2192 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
2193 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
2194 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
2195 };
2196 int chan;
2197
2198 for (chan = 0; chan < 4; chan++) {
2199 int i;
2200
2201 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2202 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002203 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
2205 for (i = 0; i < 6; i++)
2206 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
2207 test_pat[chan][i]);
2208
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002209 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 if (tg3_wait_macro_done(tp)) {
2211 *resetp = 1;
2212 return -EBUSY;
2213 }
2214
2215 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2216 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002217 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 if (tg3_wait_macro_done(tp)) {
2219 *resetp = 1;
2220 return -EBUSY;
2221 }
2222
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002223 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 if (tg3_wait_macro_done(tp)) {
2225 *resetp = 1;
2226 return -EBUSY;
2227 }
2228
2229 for (i = 0; i < 6; i += 2) {
2230 u32 low, high;
2231
2232 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
2233 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
2234 tg3_wait_macro_done(tp)) {
2235 *resetp = 1;
2236 return -EBUSY;
2237 }
2238 low &= 0x7fff;
2239 high &= 0x000f;
2240 if (low != test_pat[chan][i] ||
2241 high != test_pat[chan][i+1]) {
2242 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
2243 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
2244 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
2245
2246 return -EBUSY;
2247 }
2248 }
2249 }
2250
2251 return 0;
2252}
2253
2254static int tg3_phy_reset_chanpat(struct tg3 *tp)
2255{
2256 int chan;
2257
2258 for (chan = 0; chan < 4; chan++) {
2259 int i;
2260
2261 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2262 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002263 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 for (i = 0; i < 6; i++)
2265 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002266 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 if (tg3_wait_macro_done(tp))
2268 return -EBUSY;
2269 }
2270
2271 return 0;
2272}
2273
2274static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
2275{
2276 u32 reg32, phy9_orig;
2277 int retries, do_phy_reset, err;
2278
2279 retries = 10;
2280 do_phy_reset = 1;
2281 do {
2282 if (do_phy_reset) {
2283 err = tg3_bmcr_reset(tp);
2284 if (err)
2285 return err;
2286 do_phy_reset = 0;
2287 }
2288
2289 /* Disable transmitter and interrupt. */
2290 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
2291 continue;
2292
2293 reg32 |= 0x3000;
2294 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2295
2296 /* Set full-duplex, 1000 mbps. */
2297 tg3_writephy(tp, MII_BMCR,
Matt Carlson221c5632011-06-13 13:39:01 +00002298 BMCR_FULLDPLX | BMCR_SPEED1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
2300 /* Set to master mode. */
Matt Carlson221c5632011-06-13 13:39:01 +00002301 if (tg3_readphy(tp, MII_CTRL1000, &phy9_orig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 continue;
2303
Matt Carlson221c5632011-06-13 13:39:01 +00002304 tg3_writephy(tp, MII_CTRL1000,
2305 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
Matt Carlson1d36ba42011-04-20 07:57:42 +00002307 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
2308 if (err)
2309 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
2311 /* Block the PHY control access. */
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002312 tg3_phydsp_write(tp, 0x8005, 0x0800);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
2314 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
2315 if (!err)
2316 break;
2317 } while (--retries);
2318
2319 err = tg3_phy_reset_chanpat(tp);
2320 if (err)
2321 return err;
2322
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002323 tg3_phydsp_write(tp, 0x8005, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
2325 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002326 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
Matt Carlson1d36ba42011-04-20 07:57:42 +00002328 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
Matt Carlson221c5632011-06-13 13:39:01 +00002330 tg3_writephy(tp, MII_CTRL1000, phy9_orig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
2332 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
2333 reg32 &= ~0x3000;
2334 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2335 } else if (!err)
2336 err = -EBUSY;
2337
2338 return err;
2339}
2340
2341/* This will reset the tigon3 PHY if there is no valid
2342 * link unless the FORCE argument is non-zero.
2343 */
2344static int tg3_phy_reset(struct tg3 *tp)
2345{
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002346 u32 val, cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 int err;
2348
Michael Chan60189dd2006-12-17 17:08:07 -08002349 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002350 val = tr32(GRC_MISC_CFG);
2351 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
2352 udelay(40);
2353 }
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002354 err = tg3_readphy(tp, MII_BMSR, &val);
2355 err |= tg3_readphy(tp, MII_BMSR, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 if (err != 0)
2357 return -EBUSY;
2358
Michael Chanc8e1e822006-04-29 18:55:17 -07002359 if (netif_running(tp->dev) && netif_carrier_ok(tp->dev)) {
2360 netif_carrier_off(tp->dev);
2361 tg3_link_report(tp);
2362 }
2363
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2365 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2366 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
2367 err = tg3_phy_reset_5703_4_5(tp);
2368 if (err)
2369 return err;
2370 goto out;
2371 }
2372
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002373 cpmuctrl = 0;
2374 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
2375 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
2376 cpmuctrl = tr32(TG3_CPMU_CTRL);
2377 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
2378 tw32(TG3_CPMU_CTRL,
2379 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
2380 }
2381
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 err = tg3_bmcr_reset(tp);
2383 if (err)
2384 return err;
2385
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002386 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002387 val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
2388 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002389
2390 tw32(TG3_CPMU_CTRL, cpmuctrl);
2391 }
2392
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002393 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2394 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002395 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2396 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
2397 CPMU_LSPD_1000MB_MACCLK_12_5) {
2398 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2399 udelay(40);
2400 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2401 }
2402 }
2403
Joe Perches63c3a662011-04-26 08:12:10 +00002404 if (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002405 (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
Matt Carlsonecf14102010-01-20 16:58:05 +00002406 return 0;
2407
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002408 tg3_phy_apply_otp(tp);
2409
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002410 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -08002411 tg3_phy_toggle_apd(tp, true);
2412 else
2413 tg3_phy_toggle_apd(tp, false);
2414
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415out:
Matt Carlson1d36ba42011-04-20 07:57:42 +00002416 if ((tp->phy_flags & TG3_PHYFLG_ADC_BUG) &&
2417 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002418 tg3_phydsp_write(tp, 0x201f, 0x2aaa);
2419 tg3_phydsp_write(tp, 0x000a, 0x0323);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002420 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002422
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002423 if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002424 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2425 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002427
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002428 if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002429 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2430 tg3_phydsp_write(tp, 0x000a, 0x310b);
2431 tg3_phydsp_write(tp, 0x201f, 0x9506);
2432 tg3_phydsp_write(tp, 0x401f, 0x14e2);
2433 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2434 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002435 } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002436 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2437 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
2438 if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
2439 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2440 tg3_writephy(tp, MII_TG3_TEST1,
2441 MII_TG3_TEST1_TRIM_EN | 0x4);
2442 } else
2443 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
2444
2445 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2446 }
Michael Chanc424cb22006-04-29 18:56:34 -07002447 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002448
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 /* Set Extended packet length bit (bit 14) on all chips that */
2450 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002451 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 /* Cannot do read-modify-write on 5401 */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002453 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Joe Perches63c3a662011-04-26 08:12:10 +00002454 } else if (tg3_flag(tp, JUMBO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 /* Set bit 14 with read-modify-write to preserve other bits */
Matt Carlson15ee95c2011-04-20 07:57:40 +00002456 err = tg3_phy_auxctl_read(tp,
2457 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2458 if (!err)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002459 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2460 val | MII_TG3_AUXCTL_ACTL_EXTPKTLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 }
2462
2463 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2464 * jumbo frames transmission.
2465 */
Joe Perches63c3a662011-04-26 08:12:10 +00002466 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002467 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002468 tg3_writephy(tp, MII_TG3_EXT_CTRL,
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002469 val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 }
2471
Michael Chan715116a2006-09-27 16:09:25 -07002472 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002473 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002474 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002475 }
2476
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002477 tg3_phy_toggle_automdix(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 tg3_phy_set_wirespeed(tp);
2479 return 0;
2480}
2481
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002482#define TG3_GPIO_MSG_DRVR_PRES 0x00000001
2483#define TG3_GPIO_MSG_NEED_VAUX 0x00000002
2484#define TG3_GPIO_MSG_MASK (TG3_GPIO_MSG_DRVR_PRES | \
2485 TG3_GPIO_MSG_NEED_VAUX)
2486#define TG3_GPIO_MSG_ALL_DRVR_PRES_MASK \
2487 ((TG3_GPIO_MSG_DRVR_PRES << 0) | \
2488 (TG3_GPIO_MSG_DRVR_PRES << 4) | \
2489 (TG3_GPIO_MSG_DRVR_PRES << 8) | \
2490 (TG3_GPIO_MSG_DRVR_PRES << 12))
2491
2492#define TG3_GPIO_MSG_ALL_NEED_VAUX_MASK \
2493 ((TG3_GPIO_MSG_NEED_VAUX << 0) | \
2494 (TG3_GPIO_MSG_NEED_VAUX << 4) | \
2495 (TG3_GPIO_MSG_NEED_VAUX << 8) | \
2496 (TG3_GPIO_MSG_NEED_VAUX << 12))
2497
2498static inline u32 tg3_set_function_status(struct tg3 *tp, u32 newstat)
2499{
2500 u32 status, shift;
2501
2502 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2503 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
2504 status = tg3_ape_read32(tp, TG3_APE_GPIO_MSG);
2505 else
2506 status = tr32(TG3_CPMU_DRV_STATUS);
2507
2508 shift = TG3_APE_GPIO_MSG_SHIFT + 4 * tp->pci_fn;
2509 status &= ~(TG3_GPIO_MSG_MASK << shift);
2510 status |= (newstat << shift);
2511
2512 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2513 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
2514 tg3_ape_write32(tp, TG3_APE_GPIO_MSG, status);
2515 else
2516 tw32(TG3_CPMU_DRV_STATUS, status);
2517
2518 return status >> TG3_APE_GPIO_MSG_SHIFT;
2519}
2520
Matt Carlson520b2752011-06-13 13:39:02 +00002521static inline int tg3_pwrsrc_switch_to_vmain(struct tg3 *tp)
2522{
2523 if (!tg3_flag(tp, IS_NIC))
2524 return 0;
2525
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002526 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2527 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
2528 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
2529 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2530 return -EIO;
Matt Carlson520b2752011-06-13 13:39:02 +00002531
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002532 tg3_set_function_status(tp, TG3_GPIO_MSG_DRVR_PRES);
2533
2534 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2535 TG3_GRC_LCLCTL_PWRSW_DELAY);
2536
2537 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
2538 } else {
2539 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2540 TG3_GRC_LCLCTL_PWRSW_DELAY);
2541 }
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002542
Matt Carlson520b2752011-06-13 13:39:02 +00002543 return 0;
2544}
2545
2546static void tg3_pwrsrc_die_with_vmain(struct tg3 *tp)
2547{
2548 u32 grc_local_ctrl;
2549
2550 if (!tg3_flag(tp, IS_NIC) ||
2551 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2552 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)
2553 return;
2554
2555 grc_local_ctrl = tp->grc_local_ctrl | GRC_LCLCTRL_GPIO_OE1;
2556
2557 tw32_wait_f(GRC_LOCAL_CTRL,
2558 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2559 TG3_GRC_LCLCTL_PWRSW_DELAY);
2560
2561 tw32_wait_f(GRC_LOCAL_CTRL,
2562 grc_local_ctrl,
2563 TG3_GRC_LCLCTL_PWRSW_DELAY);
2564
2565 tw32_wait_f(GRC_LOCAL_CTRL,
2566 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2567 TG3_GRC_LCLCTL_PWRSW_DELAY);
2568}
2569
2570static void tg3_pwrsrc_switch_to_vaux(struct tg3 *tp)
2571{
2572 if (!tg3_flag(tp, IS_NIC))
2573 return;
2574
2575 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2576 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2577 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2578 (GRC_LCLCTRL_GPIO_OE0 |
2579 GRC_LCLCTRL_GPIO_OE1 |
2580 GRC_LCLCTRL_GPIO_OE2 |
2581 GRC_LCLCTRL_GPIO_OUTPUT0 |
2582 GRC_LCLCTRL_GPIO_OUTPUT1),
2583 TG3_GRC_LCLCTL_PWRSW_DELAY);
2584 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2585 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
2586 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2587 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2588 GRC_LCLCTRL_GPIO_OE1 |
2589 GRC_LCLCTRL_GPIO_OE2 |
2590 GRC_LCLCTRL_GPIO_OUTPUT0 |
2591 GRC_LCLCTRL_GPIO_OUTPUT1 |
2592 tp->grc_local_ctrl;
2593 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2594 TG3_GRC_LCLCTL_PWRSW_DELAY);
2595
2596 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2597 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2598 TG3_GRC_LCLCTL_PWRSW_DELAY);
2599
2600 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2601 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2602 TG3_GRC_LCLCTL_PWRSW_DELAY);
2603 } else {
2604 u32 no_gpio2;
2605 u32 grc_local_ctrl = 0;
2606
2607 /* Workaround to prevent overdrawing Amps. */
2608 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
2609 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
2610 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2611 grc_local_ctrl,
2612 TG3_GRC_LCLCTL_PWRSW_DELAY);
2613 }
2614
2615 /* On 5753 and variants, GPIO2 cannot be used. */
2616 no_gpio2 = tp->nic_sram_data_cfg &
2617 NIC_SRAM_DATA_CFG_NO_GPIO2;
2618
2619 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
2620 GRC_LCLCTRL_GPIO_OE1 |
2621 GRC_LCLCTRL_GPIO_OE2 |
2622 GRC_LCLCTRL_GPIO_OUTPUT1 |
2623 GRC_LCLCTRL_GPIO_OUTPUT2;
2624 if (no_gpio2) {
2625 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2626 GRC_LCLCTRL_GPIO_OUTPUT2);
2627 }
2628 tw32_wait_f(GRC_LOCAL_CTRL,
2629 tp->grc_local_ctrl | grc_local_ctrl,
2630 TG3_GRC_LCLCTL_PWRSW_DELAY);
2631
2632 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2633
2634 tw32_wait_f(GRC_LOCAL_CTRL,
2635 tp->grc_local_ctrl | grc_local_ctrl,
2636 TG3_GRC_LCLCTL_PWRSW_DELAY);
2637
2638 if (!no_gpio2) {
2639 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
2640 tw32_wait_f(GRC_LOCAL_CTRL,
2641 tp->grc_local_ctrl | grc_local_ctrl,
2642 TG3_GRC_LCLCTL_PWRSW_DELAY);
2643 }
2644 }
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002645}
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002646
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002647static void tg3_frob_aux_power_5717(struct tg3 *tp, bool wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002648{
2649 u32 msg = 0;
2650
2651 /* Serialize power state transitions */
2652 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2653 return;
2654
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002655 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE) || wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002656 msg = TG3_GPIO_MSG_NEED_VAUX;
2657
2658 msg = tg3_set_function_status(tp, msg);
2659
2660 if (msg & TG3_GPIO_MSG_ALL_DRVR_PRES_MASK)
2661 goto done;
2662
2663 if (msg & TG3_GPIO_MSG_ALL_NEED_VAUX_MASK)
2664 tg3_pwrsrc_switch_to_vaux(tp);
2665 else
2666 tg3_pwrsrc_die_with_vmain(tp);
2667
2668done:
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002669 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
Matt Carlson520b2752011-06-13 13:39:02 +00002670}
2671
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002672static void tg3_frob_aux_power(struct tg3 *tp, bool include_wol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673{
Matt Carlson683644b2011-03-09 16:58:23 +00002674 bool need_vaux = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675
Matt Carlson334355a2010-01-20 16:58:10 +00002676 /* The GPIOs do something completely different on 57765. */
Matt Carlson55086ad2011-12-14 11:09:59 +00002677 if (!tg3_flag(tp, IS_NIC) || tg3_flag(tp, 57765_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 return;
2679
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002680 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2681 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
2682 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002683 tg3_frob_aux_power_5717(tp, include_wol ?
2684 tg3_flag(tp, WOL_ENABLE) != 0 : 0);
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002685 return;
2686 }
2687
2688 if (tp->pdev_peer && tp->pdev_peer != tp->pdev) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002689 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002691 dev_peer = pci_get_drvdata(tp->pdev_peer);
Matt Carlson683644b2011-03-09 16:58:23 +00002692
Michael Chanbc1c7562006-03-20 17:48:03 -08002693 /* remove_one() may have been run on the peer. */
Matt Carlson683644b2011-03-09 16:58:23 +00002694 if (dev_peer) {
2695 struct tg3 *tp_peer = netdev_priv(dev_peer);
2696
Joe Perches63c3a662011-04-26 08:12:10 +00002697 if (tg3_flag(tp_peer, INIT_COMPLETE))
Matt Carlson683644b2011-03-09 16:58:23 +00002698 return;
2699
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002700 if ((include_wol && tg3_flag(tp_peer, WOL_ENABLE)) ||
Joe Perches63c3a662011-04-26 08:12:10 +00002701 tg3_flag(tp_peer, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002702 need_vaux = true;
2703 }
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002706 if ((include_wol && tg3_flag(tp, WOL_ENABLE)) ||
2707 tg3_flag(tp, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002708 need_vaux = true;
2709
Matt Carlson520b2752011-06-13 13:39:02 +00002710 if (need_vaux)
2711 tg3_pwrsrc_switch_to_vaux(tp);
2712 else
2713 tg3_pwrsrc_die_with_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714}
2715
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002716static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2717{
2718 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2719 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00002720 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002721 if (speed != SPEED_10)
2722 return 1;
2723 } else if (speed == SPEED_10)
2724 return 1;
2725
2726 return 0;
2727}
2728
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729static int tg3_setup_phy(struct tg3 *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730static int tg3_halt_cpu(struct tg3 *, u32);
2731
Matt Carlson0a459aa2008-11-03 16:54:15 -08002732static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002733{
Matt Carlsonce057f02007-11-12 21:08:03 -08002734 u32 val;
2735
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002736 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Michael Chan51297242007-02-13 12:17:57 -08002737 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2738 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2739 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2740
2741 sg_dig_ctrl |=
2742 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2743 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2744 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2745 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002746 return;
Michael Chan51297242007-02-13 12:17:57 -08002747 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002748
Michael Chan60189dd2006-12-17 17:08:07 -08002749 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002750 tg3_bmcr_reset(tp);
2751 val = tr32(GRC_MISC_CFG);
2752 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2753 udelay(40);
2754 return;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002755 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson0e5f7842009-11-02 14:26:38 +00002756 u32 phytest;
2757 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2758 u32 phy;
2759
2760 tg3_writephy(tp, MII_ADVERTISE, 0);
2761 tg3_writephy(tp, MII_BMCR,
2762 BMCR_ANENABLE | BMCR_ANRESTART);
2763
2764 tg3_writephy(tp, MII_TG3_FET_TEST,
2765 phytest | MII_TG3_FET_SHADOW_EN);
2766 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
2767 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
2768 tg3_writephy(tp,
2769 MII_TG3_FET_SHDW_AUXMODE4,
2770 phy);
2771 }
2772 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2773 }
2774 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002775 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07002776 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2777 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002778
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002779 val = MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2780 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2781 MII_TG3_AUXCTL_PCTL_VREG_11V;
2782 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, val);
Michael Chan715116a2006-09-27 16:09:25 -07002783 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002784
Michael Chan15c3b692006-03-22 01:06:52 -08002785 /* The PHY should not be powered down on some chips because
2786 * of bugs.
2787 */
2788 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2789 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2790 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002791 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Michael Chan15c3b692006-03-22 01:06:52 -08002792 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08002793
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002794 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2795 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002796 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2797 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2798 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2799 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2800 }
2801
Michael Chan15c3b692006-03-22 01:06:52 -08002802 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2803}
2804
Matt Carlson3f007892008-11-03 16:51:36 -08002805/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002806static int tg3_nvram_lock(struct tg3 *tp)
2807{
Joe Perches63c3a662011-04-26 08:12:10 +00002808 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002809 int i;
2810
2811 if (tp->nvram_lock_cnt == 0) {
2812 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2813 for (i = 0; i < 8000; i++) {
2814 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2815 break;
2816 udelay(20);
2817 }
2818 if (i == 8000) {
2819 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2820 return -ENODEV;
2821 }
2822 }
2823 tp->nvram_lock_cnt++;
2824 }
2825 return 0;
2826}
2827
2828/* tp->lock is held. */
2829static void tg3_nvram_unlock(struct tg3 *tp)
2830{
Joe Perches63c3a662011-04-26 08:12:10 +00002831 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002832 if (tp->nvram_lock_cnt > 0)
2833 tp->nvram_lock_cnt--;
2834 if (tp->nvram_lock_cnt == 0)
2835 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
2836 }
2837}
2838
2839/* tp->lock is held. */
2840static void tg3_enable_nvram_access(struct tg3 *tp)
2841{
Joe Perches63c3a662011-04-26 08:12:10 +00002842 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002843 u32 nvaccess = tr32(NVRAM_ACCESS);
2844
2845 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
2846 }
2847}
2848
2849/* tp->lock is held. */
2850static void tg3_disable_nvram_access(struct tg3 *tp)
2851{
Joe Perches63c3a662011-04-26 08:12:10 +00002852 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002853 u32 nvaccess = tr32(NVRAM_ACCESS);
2854
2855 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
2856 }
2857}
2858
2859static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
2860 u32 offset, u32 *val)
2861{
2862 u32 tmp;
2863 int i;
2864
2865 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
2866 return -EINVAL;
2867
2868 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
2869 EEPROM_ADDR_DEVID_MASK |
2870 EEPROM_ADDR_READ);
2871 tw32(GRC_EEPROM_ADDR,
2872 tmp |
2873 (0 << EEPROM_ADDR_DEVID_SHIFT) |
2874 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
2875 EEPROM_ADDR_ADDR_MASK) |
2876 EEPROM_ADDR_READ | EEPROM_ADDR_START);
2877
2878 for (i = 0; i < 1000; i++) {
2879 tmp = tr32(GRC_EEPROM_ADDR);
2880
2881 if (tmp & EEPROM_ADDR_COMPLETE)
2882 break;
2883 msleep(1);
2884 }
2885 if (!(tmp & EEPROM_ADDR_COMPLETE))
2886 return -EBUSY;
2887
Matt Carlson62cedd12009-04-20 14:52:29 -07002888 tmp = tr32(GRC_EEPROM_DATA);
2889
2890 /*
2891 * The data will always be opposite the native endian
2892 * format. Perform a blind byteswap to compensate.
2893 */
2894 *val = swab32(tmp);
2895
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002896 return 0;
2897}
2898
2899#define NVRAM_CMD_TIMEOUT 10000
2900
2901static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
2902{
2903 int i;
2904
2905 tw32(NVRAM_CMD, nvram_cmd);
2906 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
2907 udelay(10);
2908 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
2909 udelay(10);
2910 break;
2911 }
2912 }
2913
2914 if (i == NVRAM_CMD_TIMEOUT)
2915 return -EBUSY;
2916
2917 return 0;
2918}
2919
2920static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
2921{
Joe Perches63c3a662011-04-26 08:12:10 +00002922 if (tg3_flag(tp, NVRAM) &&
2923 tg3_flag(tp, NVRAM_BUFFERED) &&
2924 tg3_flag(tp, FLASH) &&
2925 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002926 (tp->nvram_jedecnum == JEDEC_ATMEL))
2927
2928 addr = ((addr / tp->nvram_pagesize) <<
2929 ATMEL_AT45DB0X1B_PAGE_POS) +
2930 (addr % tp->nvram_pagesize);
2931
2932 return addr;
2933}
2934
2935static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
2936{
Joe Perches63c3a662011-04-26 08:12:10 +00002937 if (tg3_flag(tp, NVRAM) &&
2938 tg3_flag(tp, NVRAM_BUFFERED) &&
2939 tg3_flag(tp, FLASH) &&
2940 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002941 (tp->nvram_jedecnum == JEDEC_ATMEL))
2942
2943 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
2944 tp->nvram_pagesize) +
2945 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
2946
2947 return addr;
2948}
2949
Matt Carlsone4f34112009-02-25 14:25:00 +00002950/* NOTE: Data read in from NVRAM is byteswapped according to
2951 * the byteswapping settings for all other register accesses.
2952 * tg3 devices are BE devices, so on a BE machine, the data
2953 * returned will be exactly as it is seen in NVRAM. On a LE
2954 * machine, the 32-bit value will be byteswapped.
2955 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002956static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
2957{
2958 int ret;
2959
Joe Perches63c3a662011-04-26 08:12:10 +00002960 if (!tg3_flag(tp, NVRAM))
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002961 return tg3_nvram_read_using_eeprom(tp, offset, val);
2962
2963 offset = tg3_nvram_phys_addr(tp, offset);
2964
2965 if (offset > NVRAM_ADDR_MSK)
2966 return -EINVAL;
2967
2968 ret = tg3_nvram_lock(tp);
2969 if (ret)
2970 return ret;
2971
2972 tg3_enable_nvram_access(tp);
2973
2974 tw32(NVRAM_ADDR, offset);
2975 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
2976 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
2977
2978 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00002979 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002980
2981 tg3_disable_nvram_access(tp);
2982
2983 tg3_nvram_unlock(tp);
2984
2985 return ret;
2986}
2987
Matt Carlsona9dc5292009-02-25 14:25:30 +00002988/* Ensures NVRAM data is in bytestream format. */
2989static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002990{
2991 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00002992 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002993 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00002994 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002995 return res;
2996}
2997
Matt Carlson997b4f12011-08-31 11:44:53 +00002998#define RX_CPU_SCRATCH_BASE 0x30000
2999#define RX_CPU_SCRATCH_SIZE 0x04000
3000#define TX_CPU_SCRATCH_BASE 0x34000
3001#define TX_CPU_SCRATCH_SIZE 0x04000
3002
3003/* tp->lock is held. */
3004static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
3005{
3006 int i;
3007
3008 BUG_ON(offset == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS));
3009
3010 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
3011 u32 val = tr32(GRC_VCPU_EXT_CTRL);
3012
3013 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
3014 return 0;
3015 }
3016 if (offset == RX_CPU_BASE) {
3017 for (i = 0; i < 10000; i++) {
3018 tw32(offset + CPU_STATE, 0xffffffff);
3019 tw32(offset + CPU_MODE, CPU_MODE_HALT);
3020 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
3021 break;
3022 }
3023
3024 tw32(offset + CPU_STATE, 0xffffffff);
3025 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
3026 udelay(10);
3027 } else {
3028 for (i = 0; i < 10000; i++) {
3029 tw32(offset + CPU_STATE, 0xffffffff);
3030 tw32(offset + CPU_MODE, CPU_MODE_HALT);
3031 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
3032 break;
3033 }
3034 }
3035
3036 if (i >= 10000) {
3037 netdev_err(tp->dev, "%s timed out, %s CPU\n",
3038 __func__, offset == RX_CPU_BASE ? "RX" : "TX");
3039 return -ENODEV;
3040 }
3041
3042 /* Clear firmware's nvram arbitration. */
3043 if (tg3_flag(tp, NVRAM))
3044 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
3045 return 0;
3046}
3047
3048struct fw_info {
3049 unsigned int fw_base;
3050 unsigned int fw_len;
3051 const __be32 *fw_data;
3052};
3053
3054/* tp->lock is held. */
3055static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base,
3056 u32 cpu_scratch_base, int cpu_scratch_size,
3057 struct fw_info *info)
3058{
3059 int err, lock_err, i;
3060 void (*write_op)(struct tg3 *, u32, u32);
3061
3062 if (cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS)) {
3063 netdev_err(tp->dev,
3064 "%s: Trying to load TX cpu firmware which is 5705\n",
3065 __func__);
3066 return -EINVAL;
3067 }
3068
3069 if (tg3_flag(tp, 5705_PLUS))
3070 write_op = tg3_write_mem;
3071 else
3072 write_op = tg3_write_indirect_reg32;
3073
3074 /* It is possible that bootcode is still loading at this point.
3075 * Get the nvram lock first before halting the cpu.
3076 */
3077 lock_err = tg3_nvram_lock(tp);
3078 err = tg3_halt_cpu(tp, cpu_base);
3079 if (!lock_err)
3080 tg3_nvram_unlock(tp);
3081 if (err)
3082 goto out;
3083
3084 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
3085 write_op(tp, cpu_scratch_base + i, 0);
3086 tw32(cpu_base + CPU_STATE, 0xffffffff);
3087 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
3088 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
3089 write_op(tp, (cpu_scratch_base +
3090 (info->fw_base & 0xffff) +
3091 (i * sizeof(u32))),
3092 be32_to_cpu(info->fw_data[i]));
3093
3094 err = 0;
3095
3096out:
3097 return err;
3098}
3099
3100/* tp->lock is held. */
3101static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
3102{
3103 struct fw_info info;
3104 const __be32 *fw_data;
3105 int err, i;
3106
3107 fw_data = (void *)tp->fw->data;
3108
3109 /* Firmware blob starts with version numbers, followed by
3110 start address and length. We are setting complete length.
3111 length = end_address_of_bss - start_address_of_text.
3112 Remainder is the blob to be loaded contiguously
3113 from start address. */
3114
3115 info.fw_base = be32_to_cpu(fw_data[1]);
3116 info.fw_len = tp->fw->size - 12;
3117 info.fw_data = &fw_data[3];
3118
3119 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
3120 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
3121 &info);
3122 if (err)
3123 return err;
3124
3125 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
3126 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
3127 &info);
3128 if (err)
3129 return err;
3130
3131 /* Now startup only the RX cpu. */
3132 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3133 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
3134
3135 for (i = 0; i < 5; i++) {
3136 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
3137 break;
3138 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3139 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
3140 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
3141 udelay(1000);
3142 }
3143 if (i >= 5) {
3144 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
3145 "should be %08x\n", __func__,
3146 tr32(RX_CPU_BASE + CPU_PC), info.fw_base);
3147 return -ENODEV;
3148 }
3149 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3150 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
3151
3152 return 0;
3153}
3154
3155/* tp->lock is held. */
3156static int tg3_load_tso_firmware(struct tg3 *tp)
3157{
3158 struct fw_info info;
3159 const __be32 *fw_data;
3160 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
3161 int err, i;
3162
3163 if (tg3_flag(tp, HW_TSO_1) ||
3164 tg3_flag(tp, HW_TSO_2) ||
3165 tg3_flag(tp, HW_TSO_3))
3166 return 0;
3167
3168 fw_data = (void *)tp->fw->data;
3169
3170 /* Firmware blob starts with version numbers, followed by
3171 start address and length. We are setting complete length.
3172 length = end_address_of_bss - start_address_of_text.
3173 Remainder is the blob to be loaded contiguously
3174 from start address. */
3175
3176 info.fw_base = be32_to_cpu(fw_data[1]);
3177 cpu_scratch_size = tp->fw_len;
3178 info.fw_len = tp->fw->size - 12;
3179 info.fw_data = &fw_data[3];
3180
3181 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
3182 cpu_base = RX_CPU_BASE;
3183 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
3184 } else {
3185 cpu_base = TX_CPU_BASE;
3186 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
3187 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
3188 }
3189
3190 err = tg3_load_firmware_cpu(tp, cpu_base,
3191 cpu_scratch_base, cpu_scratch_size,
3192 &info);
3193 if (err)
3194 return err;
3195
3196 /* Now startup the cpu. */
3197 tw32(cpu_base + CPU_STATE, 0xffffffff);
3198 tw32_f(cpu_base + CPU_PC, info.fw_base);
3199
3200 for (i = 0; i < 5; i++) {
3201 if (tr32(cpu_base + CPU_PC) == info.fw_base)
3202 break;
3203 tw32(cpu_base + CPU_STATE, 0xffffffff);
3204 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
3205 tw32_f(cpu_base + CPU_PC, info.fw_base);
3206 udelay(1000);
3207 }
3208 if (i >= 5) {
3209 netdev_err(tp->dev,
3210 "%s fails to set CPU PC, is %08x should be %08x\n",
3211 __func__, tr32(cpu_base + CPU_PC), info.fw_base);
3212 return -ENODEV;
3213 }
3214 tw32(cpu_base + CPU_STATE, 0xffffffff);
3215 tw32_f(cpu_base + CPU_MODE, 0x00000000);
3216 return 0;
3217}
3218
3219
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003220/* tp->lock is held. */
Matt Carlson3f007892008-11-03 16:51:36 -08003221static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
3222{
3223 u32 addr_high, addr_low;
3224 int i;
3225
3226 addr_high = ((tp->dev->dev_addr[0] << 8) |
3227 tp->dev->dev_addr[1]);
3228 addr_low = ((tp->dev->dev_addr[2] << 24) |
3229 (tp->dev->dev_addr[3] << 16) |
3230 (tp->dev->dev_addr[4] << 8) |
3231 (tp->dev->dev_addr[5] << 0));
3232 for (i = 0; i < 4; i++) {
3233 if (i == 1 && skip_mac_1)
3234 continue;
3235 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
3236 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
3237 }
3238
3239 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3240 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
3241 for (i = 0; i < 12; i++) {
3242 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
3243 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
3244 }
3245 }
3246
3247 addr_high = (tp->dev->dev_addr[0] +
3248 tp->dev->dev_addr[1] +
3249 tp->dev->dev_addr[2] +
3250 tp->dev->dev_addr[3] +
3251 tp->dev->dev_addr[4] +
3252 tp->dev->dev_addr[5]) &
3253 TX_BACKOFF_SEED_MASK;
3254 tw32(MAC_TX_BACKOFF_SEED, addr_high);
3255}
3256
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003257static void tg3_enable_register_access(struct tg3 *tp)
3258{
3259 /*
3260 * Make sure register accesses (indirect or otherwise) will function
3261 * correctly.
3262 */
3263 pci_write_config_dword(tp->pdev,
3264 TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
3265}
3266
3267static int tg3_power_up(struct tg3 *tp)
3268{
Matt Carlsonbed98292011-07-13 09:27:29 +00003269 int err;
3270
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003271 tg3_enable_register_access(tp);
3272
Matt Carlsonbed98292011-07-13 09:27:29 +00003273 err = pci_set_power_state(tp->pdev, PCI_D0);
3274 if (!err) {
3275 /* Switch out of Vaux if it is a NIC */
3276 tg3_pwrsrc_switch_to_vmain(tp);
3277 } else {
3278 netdev_err(tp->dev, "Transition to D0 failed\n");
3279 }
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003280
Matt Carlsonbed98292011-07-13 09:27:29 +00003281 return err;
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003282}
3283
3284static int tg3_power_down_prepare(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285{
3286 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003287 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003289 tg3_enable_register_access(tp);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003290
3291 /* Restore the CLKREQ setting. */
Joe Perches63c3a662011-04-26 08:12:10 +00003292 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003293 u16 lnkctl;
3294
3295 pci_read_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00003296 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003297 &lnkctl);
3298 lnkctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
3299 pci_write_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00003300 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003301 lnkctl);
3302 }
3303
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
3305 tw32(TG3PCI_MISC_HOST_CTRL,
3306 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
3307
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003308 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00003309 tg3_flag(tp, WOL_ENABLE);
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003310
Joe Perches63c3a662011-04-26 08:12:10 +00003311 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08003312 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003313 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson80096062010-08-02 11:26:06 +00003314 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003315 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003316 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003317
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00003318 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003319
Matt Carlson80096062010-08-02 11:26:06 +00003320 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003321
3322 tp->link_config.orig_speed = phydev->speed;
3323 tp->link_config.orig_duplex = phydev->duplex;
3324 tp->link_config.orig_autoneg = phydev->autoneg;
3325 tp->link_config.orig_advertising = phydev->advertising;
3326
3327 advertising = ADVERTISED_TP |
3328 ADVERTISED_Pause |
3329 ADVERTISED_Autoneg |
3330 ADVERTISED_10baseT_Half;
3331
Joe Perches63c3a662011-04-26 08:12:10 +00003332 if (tg3_flag(tp, ENABLE_ASF) || device_should_wake) {
3333 if (tg3_flag(tp, WOL_SPEED_100MB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003334 advertising |=
3335 ADVERTISED_100baseT_Half |
3336 ADVERTISED_100baseT_Full |
3337 ADVERTISED_10baseT_Full;
3338 else
3339 advertising |= ADVERTISED_10baseT_Full;
3340 }
3341
3342 phydev->advertising = advertising;
3343
3344 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08003345
3346 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00003347 if (phyid != PHY_ID_BCMAC131) {
3348 phyid &= PHY_BCM_OUI_MASK;
3349 if (phyid == PHY_BCM_OUI_1 ||
3350 phyid == PHY_BCM_OUI_2 ||
3351 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08003352 do_low_power = true;
3353 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003354 }
Matt Carlsondd477002008-05-25 23:45:58 -07003355 } else {
Matt Carlson20232762008-12-21 20:18:56 -08003356 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003357
Matt Carlson80096062010-08-02 11:26:06 +00003358 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
3359 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07003360 tp->link_config.orig_speed = tp->link_config.speed;
3361 tp->link_config.orig_duplex = tp->link_config.duplex;
3362 tp->link_config.orig_autoneg = tp->link_config.autoneg;
3363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003365 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Matt Carlsondd477002008-05-25 23:45:58 -07003366 tp->link_config.speed = SPEED_10;
3367 tp->link_config.duplex = DUPLEX_HALF;
3368 tp->link_config.autoneg = AUTONEG_ENABLE;
3369 tg3_setup_phy(tp, 0);
3370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 }
3372
Michael Chanb5d37722006-09-27 16:06:21 -07003373 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
3374 u32 val;
3375
3376 val = tr32(GRC_VCPU_EXT_CTRL);
3377 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
Joe Perches63c3a662011-04-26 08:12:10 +00003378 } else if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08003379 int i;
3380 u32 val;
3381
3382 for (i = 0; i < 200; i++) {
3383 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
3384 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
3385 break;
3386 msleep(1);
3387 }
3388 }
Joe Perches63c3a662011-04-26 08:12:10 +00003389 if (tg3_flag(tp, WOL_CAP))
Gary Zambranoa85feb82007-05-05 11:52:19 -07003390 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
3391 WOL_DRV_STATE_SHUTDOWN |
3392 WOL_DRV_WOL |
3393 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08003394
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003395 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 u32 mac_mode;
3397
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003398 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003399 if (do_low_power &&
3400 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
3401 tg3_phy_auxctl_write(tp,
3402 MII_TG3_AUXCTL_SHDWSEL_PWRCTL,
3403 MII_TG3_AUXCTL_PCTL_WOL_EN |
3404 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
3405 MII_TG3_AUXCTL_PCTL_CL_AB_TXDAC);
Matt Carlsondd477002008-05-25 23:45:58 -07003406 udelay(40);
3407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003409 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07003410 mac_mode = MAC_MODE_PORT_MODE_GMII;
3411 else
3412 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003414 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
3415 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
3416 ASIC_REV_5700) {
Joe Perches63c3a662011-04-26 08:12:10 +00003417 u32 speed = tg3_flag(tp, WOL_SPEED_100MB) ?
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003418 SPEED_100 : SPEED_10;
3419 if (tg3_5700_link_polarity(tp, speed))
3420 mac_mode |= MAC_MODE_LINK_POLARITY;
3421 else
3422 mac_mode &= ~MAC_MODE_LINK_POLARITY;
3423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 } else {
3425 mac_mode = MAC_MODE_PORT_MODE_TBI;
3426 }
3427
Joe Perches63c3a662011-04-26 08:12:10 +00003428 if (!tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 tw32(MAC_LED_CTRL, tp->led_ctrl);
3430
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003431 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00003432 if ((tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS)) &&
3433 (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)))
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003434 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435
Joe Perches63c3a662011-04-26 08:12:10 +00003436 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00003437 mac_mode |= MAC_MODE_APE_TX_EN |
3438 MAC_MODE_APE_RX_EN |
3439 MAC_MODE_TDE_ENABLE;
Matt Carlson3bda1252008-08-15 14:08:22 -07003440
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 tw32_f(MAC_MODE, mac_mode);
3442 udelay(100);
3443
3444 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
3445 udelay(10);
3446 }
3447
Joe Perches63c3a662011-04-26 08:12:10 +00003448 if (!tg3_flag(tp, WOL_SPEED_100MB) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3450 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
3451 u32 base_val;
3452
3453 base_val = tp->pci_clock_ctrl;
3454 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
3455 CLOCK_CTRL_TXCLK_DISABLE);
3456
Michael Chanb401e9e2005-12-19 16:27:04 -08003457 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
3458 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Joe Perches63c3a662011-04-26 08:12:10 +00003459 } else if (tg3_flag(tp, 5780_CLASS) ||
3460 tg3_flag(tp, CPMU_PRESENT) ||
Matt Carlson6ff6f812011-05-19 12:12:54 +00003461 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan4cf78e42005-07-25 12:29:19 -07003462 /* do nothing */
Joe Perches63c3a662011-04-26 08:12:10 +00003463 } else if (!(tg3_flag(tp, 5750_PLUS) && tg3_flag(tp, ENABLE_ASF))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 u32 newbits1, newbits2;
3465
3466 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3467 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3468 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
3469 CLOCK_CTRL_TXCLK_DISABLE |
3470 CLOCK_CTRL_ALTCLK);
3471 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
Joe Perches63c3a662011-04-26 08:12:10 +00003472 } else if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 newbits1 = CLOCK_CTRL_625_CORE;
3474 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
3475 } else {
3476 newbits1 = CLOCK_CTRL_ALTCLK;
3477 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
3478 }
3479
Michael Chanb401e9e2005-12-19 16:27:04 -08003480 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
3481 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482
Michael Chanb401e9e2005-12-19 16:27:04 -08003483 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
3484 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485
Joe Perches63c3a662011-04-26 08:12:10 +00003486 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 u32 newbits3;
3488
3489 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3490 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3491 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
3492 CLOCK_CTRL_TXCLK_DISABLE |
3493 CLOCK_CTRL_44MHZ_CORE);
3494 } else {
3495 newbits3 = CLOCK_CTRL_44MHZ_CORE;
3496 }
3497
Michael Chanb401e9e2005-12-19 16:27:04 -08003498 tw32_wait_f(TG3PCI_CLOCK_CTRL,
3499 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 }
3501 }
3502
Joe Perches63c3a662011-04-26 08:12:10 +00003503 if (!(device_should_wake) && !tg3_flag(tp, ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08003504 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08003505
Matt Carlsoncd0d7222011-07-13 09:27:33 +00003506 tg3_frob_aux_power(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507
3508 /* Workaround for unstable PLL clock */
3509 if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
3510 (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
3511 u32 val = tr32(0x7d00);
3512
3513 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
3514 tw32(0x7d00, val);
Joe Perches63c3a662011-04-26 08:12:10 +00003515 if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08003516 int err;
3517
3518 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08003520 if (!err)
3521 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08003522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 }
3524
Michael Chanbbadf502006-04-06 21:46:34 -07003525 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
3526
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 return 0;
3528}
3529
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003530static void tg3_power_down(struct tg3 *tp)
3531{
3532 tg3_power_down_prepare(tp);
3533
Joe Perches63c3a662011-04-26 08:12:10 +00003534 pci_wake_from_d3(tp->pdev, tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003535 pci_set_power_state(tp->pdev, PCI_D3hot);
3536}
3537
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
3539{
3540 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
3541 case MII_TG3_AUX_STAT_10HALF:
3542 *speed = SPEED_10;
3543 *duplex = DUPLEX_HALF;
3544 break;
3545
3546 case MII_TG3_AUX_STAT_10FULL:
3547 *speed = SPEED_10;
3548 *duplex = DUPLEX_FULL;
3549 break;
3550
3551 case MII_TG3_AUX_STAT_100HALF:
3552 *speed = SPEED_100;
3553 *duplex = DUPLEX_HALF;
3554 break;
3555
3556 case MII_TG3_AUX_STAT_100FULL:
3557 *speed = SPEED_100;
3558 *duplex = DUPLEX_FULL;
3559 break;
3560
3561 case MII_TG3_AUX_STAT_1000HALF:
3562 *speed = SPEED_1000;
3563 *duplex = DUPLEX_HALF;
3564 break;
3565
3566 case MII_TG3_AUX_STAT_1000FULL:
3567 *speed = SPEED_1000;
3568 *duplex = DUPLEX_FULL;
3569 break;
3570
3571 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003572 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07003573 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
3574 SPEED_10;
3575 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
3576 DUPLEX_HALF;
3577 break;
3578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 *speed = SPEED_INVALID;
3580 *duplex = DUPLEX_INVALID;
3581 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583}
3584
Matt Carlson42b64a42011-05-19 12:12:49 +00003585static int tg3_phy_autoneg_cfg(struct tg3 *tp, u32 advertise, u32 flowctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586{
Matt Carlson42b64a42011-05-19 12:12:49 +00003587 int err = 0;
3588 u32 val, new_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589
Matt Carlson42b64a42011-05-19 12:12:49 +00003590 new_adv = ADVERTISE_CSMA;
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +00003591 new_adv |= ethtool_adv_to_mii_adv_t(advertise) & ADVERTISE_ALL;
Matt Carlson42b64a42011-05-19 12:12:49 +00003592 new_adv |= tg3_advert_flowctrl_1000T(flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593
Matt Carlson42b64a42011-05-19 12:12:49 +00003594 err = tg3_writephy(tp, MII_ADVERTISE, new_adv);
3595 if (err)
3596 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597
Matt Carlson4f272092011-12-14 11:09:57 +00003598 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
3599 new_adv = ethtool_adv_to_mii_ctrl1000_t(advertise);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003600
Matt Carlson4f272092011-12-14 11:09:57 +00003601 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3602 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
3603 new_adv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003604
Matt Carlson4f272092011-12-14 11:09:57 +00003605 err = tg3_writephy(tp, MII_CTRL1000, new_adv);
3606 if (err)
3607 goto done;
3608 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003609
Matt Carlson42b64a42011-05-19 12:12:49 +00003610 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
3611 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612
Matt Carlson42b64a42011-05-19 12:12:49 +00003613 tw32(TG3_CPMU_EEE_MODE,
3614 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003615
Matt Carlson42b64a42011-05-19 12:12:49 +00003616 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
3617 if (!err) {
3618 u32 err2;
Matt Carlson52b02d02010-10-14 10:37:41 +00003619
Matt Carlsona6b68da2010-12-06 08:28:52 +00003620 val = 0;
Matt Carlson42b64a42011-05-19 12:12:49 +00003621 /* Advertise 100-BaseTX EEE ability */
3622 if (advertise & ADVERTISED_100baseT_Full)
3623 val |= MDIO_AN_EEE_ADV_100TX;
3624 /* Advertise 1000-BaseT EEE ability */
3625 if (advertise & ADVERTISED_1000baseT_Full)
3626 val |= MDIO_AN_EEE_ADV_1000T;
3627 err = tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlsonb715ce92011-07-20 10:20:52 +00003628 if (err)
3629 val = 0;
3630
3631 switch (GET_ASIC_REV(tp->pci_chip_rev_id)) {
3632 case ASIC_REV_5717:
3633 case ASIC_REV_57765:
Matt Carlson55086ad2011-12-14 11:09:59 +00003634 case ASIC_REV_57766:
Matt Carlsonb715ce92011-07-20 10:20:52 +00003635 case ASIC_REV_5719:
3636 /* If we advertised any eee advertisements above... */
3637 if (val)
3638 val = MII_TG3_DSP_TAP26_ALNOKO |
3639 MII_TG3_DSP_TAP26_RMRXSTO |
3640 MII_TG3_DSP_TAP26_OPCSINPT;
3641 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
3642 /* Fall through */
3643 case ASIC_REV_5720:
3644 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
3645 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
3646 MII_TG3_DSP_CH34TP2_HIBW01);
3647 }
Matt Carlson52b02d02010-10-14 10:37:41 +00003648
Matt Carlson42b64a42011-05-19 12:12:49 +00003649 err2 = TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
3650 if (!err)
3651 err = err2;
3652 }
3653
3654done:
3655 return err;
3656}
3657
3658static void tg3_phy_copper_begin(struct tg3 *tp)
3659{
3660 u32 new_adv;
3661 int i;
3662
3663 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
3664 new_adv = ADVERTISED_10baseT_Half |
3665 ADVERTISED_10baseT_Full;
3666 if (tg3_flag(tp, WOL_SPEED_100MB))
3667 new_adv |= ADVERTISED_100baseT_Half |
3668 ADVERTISED_100baseT_Full;
3669
3670 tg3_phy_autoneg_cfg(tp, new_adv,
3671 FLOW_CTRL_TX | FLOW_CTRL_RX);
3672 } else if (tp->link_config.speed == SPEED_INVALID) {
3673 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
3674 tp->link_config.advertising &=
3675 ~(ADVERTISED_1000baseT_Half |
3676 ADVERTISED_1000baseT_Full);
3677
3678 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
3679 tp->link_config.flowctrl);
3680 } else {
3681 /* Asking for a specific link mode. */
3682 if (tp->link_config.speed == SPEED_1000) {
3683 if (tp->link_config.duplex == DUPLEX_FULL)
3684 new_adv = ADVERTISED_1000baseT_Full;
3685 else
3686 new_adv = ADVERTISED_1000baseT_Half;
3687 } else if (tp->link_config.speed == SPEED_100) {
3688 if (tp->link_config.duplex == DUPLEX_FULL)
3689 new_adv = ADVERTISED_100baseT_Full;
3690 else
3691 new_adv = ADVERTISED_100baseT_Half;
3692 } else {
3693 if (tp->link_config.duplex == DUPLEX_FULL)
3694 new_adv = ADVERTISED_10baseT_Full;
3695 else
3696 new_adv = ADVERTISED_10baseT_Half;
3697 }
3698
3699 tg3_phy_autoneg_cfg(tp, new_adv,
3700 tp->link_config.flowctrl);
Matt Carlson52b02d02010-10-14 10:37:41 +00003701 }
3702
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 if (tp->link_config.autoneg == AUTONEG_DISABLE &&
3704 tp->link_config.speed != SPEED_INVALID) {
3705 u32 bmcr, orig_bmcr;
3706
3707 tp->link_config.active_speed = tp->link_config.speed;
3708 tp->link_config.active_duplex = tp->link_config.duplex;
3709
3710 bmcr = 0;
3711 switch (tp->link_config.speed) {
3712 default:
3713 case SPEED_10:
3714 break;
3715
3716 case SPEED_100:
3717 bmcr |= BMCR_SPEED100;
3718 break;
3719
3720 case SPEED_1000:
Matt Carlson221c5632011-06-13 13:39:01 +00003721 bmcr |= BMCR_SPEED1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724
3725 if (tp->link_config.duplex == DUPLEX_FULL)
3726 bmcr |= BMCR_FULLDPLX;
3727
3728 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
3729 (bmcr != orig_bmcr)) {
3730 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
3731 for (i = 0; i < 1500; i++) {
3732 u32 tmp;
3733
3734 udelay(10);
3735 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
3736 tg3_readphy(tp, MII_BMSR, &tmp))
3737 continue;
3738 if (!(tmp & BMSR_LSTATUS)) {
3739 udelay(40);
3740 break;
3741 }
3742 }
3743 tg3_writephy(tp, MII_BMCR, bmcr);
3744 udelay(40);
3745 }
3746 } else {
3747 tg3_writephy(tp, MII_BMCR,
3748 BMCR_ANENABLE | BMCR_ANRESTART);
3749 }
3750}
3751
3752static int tg3_init_5401phy_dsp(struct tg3 *tp)
3753{
3754 int err;
3755
3756 /* Turn off tap power management. */
3757 /* Set Extended packet length bit */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003758 err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00003760 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
3761 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
3762 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
3763 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
3764 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765
3766 udelay(40);
3767
3768 return err;
3769}
3770
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003771static bool tg3_phy_copper_an_config_ok(struct tg3 *tp, u32 *lcladv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772{
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003773 u32 advmsk, tgtadv, advertising;
Michael Chan3600d912006-12-07 00:21:48 -08003774
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003775 advertising = tp->link_config.advertising;
3776 tgtadv = ethtool_adv_to_mii_adv_t(advertising) & ADVERTISE_ALL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003778 advmsk = ADVERTISE_ALL;
3779 if (tp->link_config.active_duplex == DUPLEX_FULL) {
3780 tgtadv |= tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
3781 advmsk |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
3782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003784 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
3785 return false;
3786
3787 if ((*lcladv & advmsk) != tgtadv)
3788 return false;
Matt Carlsonb99d2a52011-08-31 11:44:47 +00003789
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003790 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 u32 tg3_ctrl;
3792
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003793 tgtadv = ethtool_adv_to_mii_ctrl1000_t(advertising);
Michael Chan3600d912006-12-07 00:21:48 -08003794
Matt Carlson221c5632011-06-13 13:39:01 +00003795 if (tg3_readphy(tp, MII_CTRL1000, &tg3_ctrl))
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003796 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797
Matt Carlsonb99d2a52011-08-31 11:44:47 +00003798 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL);
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003799 if (tg3_ctrl != tgtadv)
3800 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801 }
Matt Carlson93a700a2011-08-31 11:44:54 +00003802
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003803 return true;
Matt Carlsonef167e22007-12-20 20:10:01 -08003804}
3805
Matt Carlson859edb22011-12-08 14:40:16 +00003806static bool tg3_phy_copper_fetch_rmtadv(struct tg3 *tp, u32 *rmtadv)
3807{
3808 u32 lpeth = 0;
3809
3810 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
3811 u32 val;
3812
3813 if (tg3_readphy(tp, MII_STAT1000, &val))
3814 return false;
3815
3816 lpeth = mii_stat1000_to_ethtool_lpa_t(val);
3817 }
3818
3819 if (tg3_readphy(tp, MII_LPA, rmtadv))
3820 return false;
3821
3822 lpeth |= mii_lpa_to_ethtool_lpa_t(*rmtadv);
3823 tp->link_config.rmt_adv = lpeth;
3824
3825 return true;
3826}
3827
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
3829{
3830 int current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003831 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08003832 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 u16 current_speed;
3834 u8 current_duplex;
3835 int i, err;
3836
3837 tw32(MAC_EVENT, 0);
3838
3839 tw32_f(MAC_STATUS,
3840 (MAC_STATUS_SYNC_CHANGED |
3841 MAC_STATUS_CFG_CHANGED |
3842 MAC_STATUS_MI_COMPLETION |
3843 MAC_STATUS_LNKSTATE_CHANGED));
3844 udelay(40);
3845
Matt Carlson8ef21422008-05-02 16:47:53 -07003846 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
3847 tw32_f(MAC_MI_MODE,
3848 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
3849 udelay(80);
3850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003852 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853
3854 /* Some third-party PHYs need to be reset on link going
3855 * down.
3856 */
3857 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3858 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
3859 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
3860 netif_carrier_ok(tp->dev)) {
3861 tg3_readphy(tp, MII_BMSR, &bmsr);
3862 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3863 !(bmsr & BMSR_LSTATUS))
3864 force_reset = 1;
3865 }
3866 if (force_reset)
3867 tg3_phy_reset(tp);
3868
Matt Carlson79eb6902010-02-17 15:17:03 +00003869 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 tg3_readphy(tp, MII_BMSR, &bmsr);
3871 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
Joe Perches63c3a662011-04-26 08:12:10 +00003872 !tg3_flag(tp, INIT_COMPLETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 bmsr = 0;
3874
3875 if (!(bmsr & BMSR_LSTATUS)) {
3876 err = tg3_init_5401phy_dsp(tp);
3877 if (err)
3878 return err;
3879
3880 tg3_readphy(tp, MII_BMSR, &bmsr);
3881 for (i = 0; i < 1000; i++) {
3882 udelay(10);
3883 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3884 (bmsr & BMSR_LSTATUS)) {
3885 udelay(40);
3886 break;
3887 }
3888 }
3889
Matt Carlson79eb6902010-02-17 15:17:03 +00003890 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
3891 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 !(bmsr & BMSR_LSTATUS) &&
3893 tp->link_config.active_speed == SPEED_1000) {
3894 err = tg3_phy_reset(tp);
3895 if (!err)
3896 err = tg3_init_5401phy_dsp(tp);
3897 if (err)
3898 return err;
3899 }
3900 }
3901 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3902 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
3903 /* 5701 {A0,B0} CRC bug workaround */
3904 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00003905 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
3906 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
3907 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 }
3909
3910 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003911 tg3_readphy(tp, MII_TG3_ISTAT, &val);
3912 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003914 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003916 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 tg3_writephy(tp, MII_TG3_IMASK, ~0);
3918
3919 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3920 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3921 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
3922 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3923 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
3924 else
3925 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
3926 }
3927
3928 current_link_up = 0;
3929 current_speed = SPEED_INVALID;
3930 current_duplex = DUPLEX_INVALID;
Matt Carlsone348c5e2011-11-21 15:01:20 +00003931 tp->phy_flags &= ~TG3_PHYFLG_MDIX_STATE;
Matt Carlson859edb22011-12-08 14:40:16 +00003932 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003934 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Matt Carlson15ee95c2011-04-20 07:57:40 +00003935 err = tg3_phy_auxctl_read(tp,
3936 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
3937 &val);
3938 if (!err && !(val & (1 << 10))) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003939 tg3_phy_auxctl_write(tp,
3940 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
3941 val | (1 << 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 goto relink;
3943 }
3944 }
3945
3946 bmsr = 0;
3947 for (i = 0; i < 100; i++) {
3948 tg3_readphy(tp, MII_BMSR, &bmsr);
3949 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3950 (bmsr & BMSR_LSTATUS))
3951 break;
3952 udelay(40);
3953 }
3954
3955 if (bmsr & BMSR_LSTATUS) {
3956 u32 aux_stat, bmcr;
3957
3958 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
3959 for (i = 0; i < 2000; i++) {
3960 udelay(10);
3961 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
3962 aux_stat)
3963 break;
3964 }
3965
3966 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
3967 &current_speed,
3968 &current_duplex);
3969
3970 bmcr = 0;
3971 for (i = 0; i < 200; i++) {
3972 tg3_readphy(tp, MII_BMCR, &bmcr);
3973 if (tg3_readphy(tp, MII_BMCR, &bmcr))
3974 continue;
3975 if (bmcr && bmcr != 0x7fff)
3976 break;
3977 udelay(10);
3978 }
3979
Matt Carlsonef167e22007-12-20 20:10:01 -08003980 lcl_adv = 0;
3981 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982
Matt Carlsonef167e22007-12-20 20:10:01 -08003983 tp->link_config.active_speed = current_speed;
3984 tp->link_config.active_duplex = current_duplex;
3985
3986 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3987 if ((bmcr & BMCR_ANENABLE) &&
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003988 tg3_phy_copper_an_config_ok(tp, &lcl_adv) &&
Matt Carlson859edb22011-12-08 14:40:16 +00003989 tg3_phy_copper_fetch_rmtadv(tp, &rmt_adv))
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003990 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 } else {
3992 if (!(bmcr & BMCR_ANENABLE) &&
3993 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08003994 tp->link_config.duplex == current_duplex &&
3995 tp->link_config.flowctrl ==
3996 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 }
3999 }
4000
Matt Carlsonef167e22007-12-20 20:10:01 -08004001 if (current_link_up == 1 &&
Matt Carlsone348c5e2011-11-21 15:01:20 +00004002 tp->link_config.active_duplex == DUPLEX_FULL) {
4003 u32 reg, bit;
4004
4005 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
4006 reg = MII_TG3_FET_GEN_STAT;
4007 bit = MII_TG3_FET_GEN_STAT_MDIXSTAT;
4008 } else {
4009 reg = MII_TG3_EXT_STAT;
4010 bit = MII_TG3_EXT_STAT_MDIX;
4011 }
4012
4013 if (!tg3_readphy(tp, reg, &val) && (val & bit))
4014 tp->phy_flags |= TG3_PHYFLG_MDIX_STATE;
4015
Matt Carlsonef167e22007-12-20 20:10:01 -08004016 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Matt Carlsone348c5e2011-11-21 15:01:20 +00004017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 }
4019
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020relink:
Matt Carlson80096062010-08-02 11:26:06 +00004021 if (current_link_up == 0 || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022 tg3_phy_copper_begin(tp);
4023
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004024 tg3_readphy(tp, MII_BMSR, &bmsr);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00004025 if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
4026 (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 current_link_up = 1;
4028 }
4029
4030 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
4031 if (current_link_up == 1) {
4032 if (tp->link_config.active_speed == SPEED_100 ||
4033 tp->link_config.active_speed == SPEED_10)
4034 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4035 else
4036 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004037 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00004038 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4039 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4041
4042 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4043 if (tp->link_config.active_duplex == DUPLEX_HALF)
4044 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4045
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004047 if (current_link_up == 1 &&
4048 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004050 else
4051 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 }
4053
4054 /* ??? Without this setting Netgear GA302T PHY does not
4055 * ??? send/receive packets...
4056 */
Matt Carlson79eb6902010-02-17 15:17:03 +00004057 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
4059 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
4060 tw32_f(MAC_MI_MODE, tp->mi_mode);
4061 udelay(80);
4062 }
4063
4064 tw32_f(MAC_MODE, tp->mac_mode);
4065 udelay(40);
4066
Matt Carlson52b02d02010-10-14 10:37:41 +00004067 tg3_phy_eee_adjust(tp, current_link_up);
4068
Joe Perches63c3a662011-04-26 08:12:10 +00004069 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 /* Polled via timer. */
4071 tw32_f(MAC_EVENT, 0);
4072 } else {
4073 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4074 }
4075 udelay(40);
4076
4077 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
4078 current_link_up == 1 &&
4079 tp->link_config.active_speed == SPEED_1000 &&
Joe Perches63c3a662011-04-26 08:12:10 +00004080 (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081 udelay(120);
4082 tw32_f(MAC_STATUS,
4083 (MAC_STATUS_SYNC_CHANGED |
4084 MAC_STATUS_CFG_CHANGED));
4085 udelay(40);
4086 tg3_write_mem(tp,
4087 NIC_SRAM_FIRMWARE_MBOX,
4088 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
4089 }
4090
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004091 /* Prevent send BD corruption. */
Joe Perches63c3a662011-04-26 08:12:10 +00004092 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004093 u16 oldlnkctl, newlnkctl;
4094
4095 pci_read_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00004096 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004097 &oldlnkctl);
4098 if (tp->link_config.active_speed == SPEED_100 ||
4099 tp->link_config.active_speed == SPEED_10)
4100 newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
4101 else
4102 newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
4103 if (newlnkctl != oldlnkctl)
4104 pci_write_config_word(tp->pdev,
Matt Carlson93a700a2011-08-31 11:44:54 +00004105 pci_pcie_cap(tp->pdev) +
4106 PCI_EXP_LNKCTL, newlnkctl);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004107 }
4108
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109 if (current_link_up != netif_carrier_ok(tp->dev)) {
4110 if (current_link_up)
4111 netif_carrier_on(tp->dev);
4112 else
4113 netif_carrier_off(tp->dev);
4114 tg3_link_report(tp);
4115 }
4116
4117 return 0;
4118}
4119
4120struct tg3_fiber_aneginfo {
4121 int state;
4122#define ANEG_STATE_UNKNOWN 0
4123#define ANEG_STATE_AN_ENABLE 1
4124#define ANEG_STATE_RESTART_INIT 2
4125#define ANEG_STATE_RESTART 3
4126#define ANEG_STATE_DISABLE_LINK_OK 4
4127#define ANEG_STATE_ABILITY_DETECT_INIT 5
4128#define ANEG_STATE_ABILITY_DETECT 6
4129#define ANEG_STATE_ACK_DETECT_INIT 7
4130#define ANEG_STATE_ACK_DETECT 8
4131#define ANEG_STATE_COMPLETE_ACK_INIT 9
4132#define ANEG_STATE_COMPLETE_ACK 10
4133#define ANEG_STATE_IDLE_DETECT_INIT 11
4134#define ANEG_STATE_IDLE_DETECT 12
4135#define ANEG_STATE_LINK_OK 13
4136#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
4137#define ANEG_STATE_NEXT_PAGE_WAIT 15
4138
4139 u32 flags;
4140#define MR_AN_ENABLE 0x00000001
4141#define MR_RESTART_AN 0x00000002
4142#define MR_AN_COMPLETE 0x00000004
4143#define MR_PAGE_RX 0x00000008
4144#define MR_NP_LOADED 0x00000010
4145#define MR_TOGGLE_TX 0x00000020
4146#define MR_LP_ADV_FULL_DUPLEX 0x00000040
4147#define MR_LP_ADV_HALF_DUPLEX 0x00000080
4148#define MR_LP_ADV_SYM_PAUSE 0x00000100
4149#define MR_LP_ADV_ASYM_PAUSE 0x00000200
4150#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
4151#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
4152#define MR_LP_ADV_NEXT_PAGE 0x00001000
4153#define MR_TOGGLE_RX 0x00002000
4154#define MR_NP_RX 0x00004000
4155
4156#define MR_LINK_OK 0x80000000
4157
4158 unsigned long link_time, cur_time;
4159
4160 u32 ability_match_cfg;
4161 int ability_match_count;
4162
4163 char ability_match, idle_match, ack_match;
4164
4165 u32 txconfig, rxconfig;
4166#define ANEG_CFG_NP 0x00000080
4167#define ANEG_CFG_ACK 0x00000040
4168#define ANEG_CFG_RF2 0x00000020
4169#define ANEG_CFG_RF1 0x00000010
4170#define ANEG_CFG_PS2 0x00000001
4171#define ANEG_CFG_PS1 0x00008000
4172#define ANEG_CFG_HD 0x00004000
4173#define ANEG_CFG_FD 0x00002000
4174#define ANEG_CFG_INVAL 0x00001f06
4175
4176};
4177#define ANEG_OK 0
4178#define ANEG_DONE 1
4179#define ANEG_TIMER_ENAB 2
4180#define ANEG_FAILED -1
4181
4182#define ANEG_STATE_SETTLE_TIME 10000
4183
4184static int tg3_fiber_aneg_smachine(struct tg3 *tp,
4185 struct tg3_fiber_aneginfo *ap)
4186{
Matt Carlson5be73b42007-12-20 20:09:29 -08004187 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 unsigned long delta;
4189 u32 rx_cfg_reg;
4190 int ret;
4191
4192 if (ap->state == ANEG_STATE_UNKNOWN) {
4193 ap->rxconfig = 0;
4194 ap->link_time = 0;
4195 ap->cur_time = 0;
4196 ap->ability_match_cfg = 0;
4197 ap->ability_match_count = 0;
4198 ap->ability_match = 0;
4199 ap->idle_match = 0;
4200 ap->ack_match = 0;
4201 }
4202 ap->cur_time++;
4203
4204 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
4205 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
4206
4207 if (rx_cfg_reg != ap->ability_match_cfg) {
4208 ap->ability_match_cfg = rx_cfg_reg;
4209 ap->ability_match = 0;
4210 ap->ability_match_count = 0;
4211 } else {
4212 if (++ap->ability_match_count > 1) {
4213 ap->ability_match = 1;
4214 ap->ability_match_cfg = rx_cfg_reg;
4215 }
4216 }
4217 if (rx_cfg_reg & ANEG_CFG_ACK)
4218 ap->ack_match = 1;
4219 else
4220 ap->ack_match = 0;
4221
4222 ap->idle_match = 0;
4223 } else {
4224 ap->idle_match = 1;
4225 ap->ability_match_cfg = 0;
4226 ap->ability_match_count = 0;
4227 ap->ability_match = 0;
4228 ap->ack_match = 0;
4229
4230 rx_cfg_reg = 0;
4231 }
4232
4233 ap->rxconfig = rx_cfg_reg;
4234 ret = ANEG_OK;
4235
Matt Carlson33f401a2010-04-05 10:19:27 +00004236 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 case ANEG_STATE_UNKNOWN:
4238 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
4239 ap->state = ANEG_STATE_AN_ENABLE;
4240
4241 /* fallthru */
4242 case ANEG_STATE_AN_ENABLE:
4243 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
4244 if (ap->flags & MR_AN_ENABLE) {
4245 ap->link_time = 0;
4246 ap->cur_time = 0;
4247 ap->ability_match_cfg = 0;
4248 ap->ability_match_count = 0;
4249 ap->ability_match = 0;
4250 ap->idle_match = 0;
4251 ap->ack_match = 0;
4252
4253 ap->state = ANEG_STATE_RESTART_INIT;
4254 } else {
4255 ap->state = ANEG_STATE_DISABLE_LINK_OK;
4256 }
4257 break;
4258
4259 case ANEG_STATE_RESTART_INIT:
4260 ap->link_time = ap->cur_time;
4261 ap->flags &= ~(MR_NP_LOADED);
4262 ap->txconfig = 0;
4263 tw32(MAC_TX_AUTO_NEG, 0);
4264 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4265 tw32_f(MAC_MODE, tp->mac_mode);
4266 udelay(40);
4267
4268 ret = ANEG_TIMER_ENAB;
4269 ap->state = ANEG_STATE_RESTART;
4270
4271 /* fallthru */
4272 case ANEG_STATE_RESTART:
4273 delta = ap->cur_time - ap->link_time;
Matt Carlson859a5882010-04-05 10:19:28 +00004274 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a5882010-04-05 10:19:28 +00004276 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 break;
4279
4280 case ANEG_STATE_DISABLE_LINK_OK:
4281 ret = ANEG_DONE;
4282 break;
4283
4284 case ANEG_STATE_ABILITY_DETECT_INIT:
4285 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08004286 ap->txconfig = ANEG_CFG_FD;
4287 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
4288 if (flowctrl & ADVERTISE_1000XPAUSE)
4289 ap->txconfig |= ANEG_CFG_PS1;
4290 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
4291 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
4293 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4294 tw32_f(MAC_MODE, tp->mac_mode);
4295 udelay(40);
4296
4297 ap->state = ANEG_STATE_ABILITY_DETECT;
4298 break;
4299
4300 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a5882010-04-05 10:19:28 +00004301 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 break;
4304
4305 case ANEG_STATE_ACK_DETECT_INIT:
4306 ap->txconfig |= ANEG_CFG_ACK;
4307 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
4308 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4309 tw32_f(MAC_MODE, tp->mac_mode);
4310 udelay(40);
4311
4312 ap->state = ANEG_STATE_ACK_DETECT;
4313
4314 /* fallthru */
4315 case ANEG_STATE_ACK_DETECT:
4316 if (ap->ack_match != 0) {
4317 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
4318 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
4319 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
4320 } else {
4321 ap->state = ANEG_STATE_AN_ENABLE;
4322 }
4323 } else if (ap->ability_match != 0 &&
4324 ap->rxconfig == 0) {
4325 ap->state = ANEG_STATE_AN_ENABLE;
4326 }
4327 break;
4328
4329 case ANEG_STATE_COMPLETE_ACK_INIT:
4330 if (ap->rxconfig & ANEG_CFG_INVAL) {
4331 ret = ANEG_FAILED;
4332 break;
4333 }
4334 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
4335 MR_LP_ADV_HALF_DUPLEX |
4336 MR_LP_ADV_SYM_PAUSE |
4337 MR_LP_ADV_ASYM_PAUSE |
4338 MR_LP_ADV_REMOTE_FAULT1 |
4339 MR_LP_ADV_REMOTE_FAULT2 |
4340 MR_LP_ADV_NEXT_PAGE |
4341 MR_TOGGLE_RX |
4342 MR_NP_RX);
4343 if (ap->rxconfig & ANEG_CFG_FD)
4344 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
4345 if (ap->rxconfig & ANEG_CFG_HD)
4346 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
4347 if (ap->rxconfig & ANEG_CFG_PS1)
4348 ap->flags |= MR_LP_ADV_SYM_PAUSE;
4349 if (ap->rxconfig & ANEG_CFG_PS2)
4350 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
4351 if (ap->rxconfig & ANEG_CFG_RF1)
4352 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
4353 if (ap->rxconfig & ANEG_CFG_RF2)
4354 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
4355 if (ap->rxconfig & ANEG_CFG_NP)
4356 ap->flags |= MR_LP_ADV_NEXT_PAGE;
4357
4358 ap->link_time = ap->cur_time;
4359
4360 ap->flags ^= (MR_TOGGLE_TX);
4361 if (ap->rxconfig & 0x0008)
4362 ap->flags |= MR_TOGGLE_RX;
4363 if (ap->rxconfig & ANEG_CFG_NP)
4364 ap->flags |= MR_NP_RX;
4365 ap->flags |= MR_PAGE_RX;
4366
4367 ap->state = ANEG_STATE_COMPLETE_ACK;
4368 ret = ANEG_TIMER_ENAB;
4369 break;
4370
4371 case ANEG_STATE_COMPLETE_ACK:
4372 if (ap->ability_match != 0 &&
4373 ap->rxconfig == 0) {
4374 ap->state = ANEG_STATE_AN_ENABLE;
4375 break;
4376 }
4377 delta = ap->cur_time - ap->link_time;
4378 if (delta > ANEG_STATE_SETTLE_TIME) {
4379 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
4380 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
4381 } else {
4382 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
4383 !(ap->flags & MR_NP_RX)) {
4384 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
4385 } else {
4386 ret = ANEG_FAILED;
4387 }
4388 }
4389 }
4390 break;
4391
4392 case ANEG_STATE_IDLE_DETECT_INIT:
4393 ap->link_time = ap->cur_time;
4394 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
4395 tw32_f(MAC_MODE, tp->mac_mode);
4396 udelay(40);
4397
4398 ap->state = ANEG_STATE_IDLE_DETECT;
4399 ret = ANEG_TIMER_ENAB;
4400 break;
4401
4402 case ANEG_STATE_IDLE_DETECT:
4403 if (ap->ability_match != 0 &&
4404 ap->rxconfig == 0) {
4405 ap->state = ANEG_STATE_AN_ENABLE;
4406 break;
4407 }
4408 delta = ap->cur_time - ap->link_time;
4409 if (delta > ANEG_STATE_SETTLE_TIME) {
4410 /* XXX another gem from the Broadcom driver :( */
4411 ap->state = ANEG_STATE_LINK_OK;
4412 }
4413 break;
4414
4415 case ANEG_STATE_LINK_OK:
4416 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
4417 ret = ANEG_DONE;
4418 break;
4419
4420 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
4421 /* ??? unimplemented */
4422 break;
4423
4424 case ANEG_STATE_NEXT_PAGE_WAIT:
4425 /* ??? unimplemented */
4426 break;
4427
4428 default:
4429 ret = ANEG_FAILED;
4430 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432
4433 return ret;
4434}
4435
Matt Carlson5be73b42007-12-20 20:09:29 -08004436static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437{
4438 int res = 0;
4439 struct tg3_fiber_aneginfo aninfo;
4440 int status = ANEG_FAILED;
4441 unsigned int tick;
4442 u32 tmp;
4443
4444 tw32_f(MAC_TX_AUTO_NEG, 0);
4445
4446 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
4447 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
4448 udelay(40);
4449
4450 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
4451 udelay(40);
4452
4453 memset(&aninfo, 0, sizeof(aninfo));
4454 aninfo.flags |= MR_AN_ENABLE;
4455 aninfo.state = ANEG_STATE_UNKNOWN;
4456 aninfo.cur_time = 0;
4457 tick = 0;
4458 while (++tick < 195000) {
4459 status = tg3_fiber_aneg_smachine(tp, &aninfo);
4460 if (status == ANEG_DONE || status == ANEG_FAILED)
4461 break;
4462
4463 udelay(1);
4464 }
4465
4466 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
4467 tw32_f(MAC_MODE, tp->mac_mode);
4468 udelay(40);
4469
Matt Carlson5be73b42007-12-20 20:09:29 -08004470 *txflags = aninfo.txconfig;
4471 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472
4473 if (status == ANEG_DONE &&
4474 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
4475 MR_LP_ADV_FULL_DUPLEX)))
4476 res = 1;
4477
4478 return res;
4479}
4480
4481static void tg3_init_bcm8002(struct tg3 *tp)
4482{
4483 u32 mac_status = tr32(MAC_STATUS);
4484 int i;
4485
4486 /* Reset when initting first time or we have a link. */
Joe Perches63c3a662011-04-26 08:12:10 +00004487 if (tg3_flag(tp, INIT_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488 !(mac_status & MAC_STATUS_PCS_SYNCED))
4489 return;
4490
4491 /* Set PLL lock range. */
4492 tg3_writephy(tp, 0x16, 0x8007);
4493
4494 /* SW reset */
4495 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
4496
4497 /* Wait for reset to complete. */
4498 /* XXX schedule_timeout() ... */
4499 for (i = 0; i < 500; i++)
4500 udelay(10);
4501
4502 /* Config mode; select PMA/Ch 1 regs. */
4503 tg3_writephy(tp, 0x10, 0x8411);
4504
4505 /* Enable auto-lock and comdet, select txclk for tx. */
4506 tg3_writephy(tp, 0x11, 0x0a10);
4507
4508 tg3_writephy(tp, 0x18, 0x00a0);
4509 tg3_writephy(tp, 0x16, 0x41ff);
4510
4511 /* Assert and deassert POR. */
4512 tg3_writephy(tp, 0x13, 0x0400);
4513 udelay(40);
4514 tg3_writephy(tp, 0x13, 0x0000);
4515
4516 tg3_writephy(tp, 0x11, 0x0a50);
4517 udelay(40);
4518 tg3_writephy(tp, 0x11, 0x0a10);
4519
4520 /* Wait for signal to stabilize */
4521 /* XXX schedule_timeout() ... */
4522 for (i = 0; i < 15000; i++)
4523 udelay(10);
4524
4525 /* Deselect the channel register so we can read the PHYID
4526 * later.
4527 */
4528 tg3_writephy(tp, 0x10, 0x8011);
4529}
4530
4531static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
4532{
Matt Carlson82cd3d12007-12-20 20:09:00 -08004533 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 u32 sg_dig_ctrl, sg_dig_status;
4535 u32 serdes_cfg, expected_sg_dig_ctrl;
4536 int workaround, port_a;
4537 int current_link_up;
4538
4539 serdes_cfg = 0;
4540 expected_sg_dig_ctrl = 0;
4541 workaround = 0;
4542 port_a = 1;
4543 current_link_up = 0;
4544
4545 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
4546 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
4547 workaround = 1;
4548 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
4549 port_a = 0;
4550
4551 /* preserve bits 0-11,13,14 for signal pre-emphasis */
4552 /* preserve bits 20-23 for voltage regulator */
4553 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
4554 }
4555
4556 sg_dig_ctrl = tr32(SG_DIG_CTRL);
4557
4558 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004559 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 if (workaround) {
4561 u32 val = serdes_cfg;
4562
4563 if (port_a)
4564 val |= 0xc010000;
4565 else
4566 val |= 0x4010000;
4567 tw32_f(MAC_SERDES_CFG, val);
4568 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004569
4570 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571 }
4572 if (mac_status & MAC_STATUS_PCS_SYNCED) {
4573 tg3_setup_flow_control(tp, 0, 0);
4574 current_link_up = 1;
4575 }
4576 goto out;
4577 }
4578
4579 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004580 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581
Matt Carlson82cd3d12007-12-20 20:09:00 -08004582 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
4583 if (flowctrl & ADVERTISE_1000XPAUSE)
4584 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
4585 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
4586 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587
4588 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004589 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07004590 tp->serdes_counter &&
4591 ((mac_status & (MAC_STATUS_PCS_SYNCED |
4592 MAC_STATUS_RCVD_CFG)) ==
4593 MAC_STATUS_PCS_SYNCED)) {
4594 tp->serdes_counter--;
4595 current_link_up = 1;
4596 goto out;
4597 }
4598restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599 if (workaround)
4600 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004601 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602 udelay(5);
4603 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
4604
Michael Chan3d3ebe72006-09-27 15:59:15 -07004605 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004606 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
4608 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07004609 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610 mac_status = tr32(MAC_STATUS);
4611
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004612 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08004614 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615
Matt Carlson82cd3d12007-12-20 20:09:00 -08004616 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
4617 local_adv |= ADVERTISE_1000XPAUSE;
4618 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
4619 local_adv |= ADVERTISE_1000XPSE_ASYM;
4620
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004621 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08004622 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004623 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08004624 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625
Matt Carlson859edb22011-12-08 14:40:16 +00004626 tp->link_config.rmt_adv =
4627 mii_adv_to_ethtool_adv_x(remote_adv);
4628
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629 tg3_setup_flow_control(tp, local_adv, remote_adv);
4630 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004631 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004632 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004633 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07004634 if (tp->serdes_counter)
4635 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636 else {
4637 if (workaround) {
4638 u32 val = serdes_cfg;
4639
4640 if (port_a)
4641 val |= 0xc010000;
4642 else
4643 val |= 0x4010000;
4644
4645 tw32_f(MAC_SERDES_CFG, val);
4646 }
4647
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004648 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 udelay(40);
4650
4651 /* Link parallel detection - link is up */
4652 /* only if we have PCS_SYNC and not */
4653 /* receiving config code words */
4654 mac_status = tr32(MAC_STATUS);
4655 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
4656 !(mac_status & MAC_STATUS_RCVD_CFG)) {
4657 tg3_setup_flow_control(tp, 0, 0);
4658 current_link_up = 1;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004659 tp->phy_flags |=
4660 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004661 tp->serdes_counter =
4662 SERDES_PARALLEL_DET_TIMEOUT;
4663 } else
4664 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 }
4666 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07004667 } else {
4668 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004669 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670 }
4671
4672out:
4673 return current_link_up;
4674}
4675
4676static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
4677{
4678 int current_link_up = 0;
4679
Michael Chan5cf64b82007-05-05 12:11:21 -07004680 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682
4683 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08004684 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004686
Matt Carlson5be73b42007-12-20 20:09:29 -08004687 if (fiber_autoneg(tp, &txflags, &rxflags)) {
4688 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689
Matt Carlson5be73b42007-12-20 20:09:29 -08004690 if (txflags & ANEG_CFG_PS1)
4691 local_adv |= ADVERTISE_1000XPAUSE;
4692 if (txflags & ANEG_CFG_PS2)
4693 local_adv |= ADVERTISE_1000XPSE_ASYM;
4694
4695 if (rxflags & MR_LP_ADV_SYM_PAUSE)
4696 remote_adv |= LPA_1000XPAUSE;
4697 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
4698 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699
Matt Carlson859edb22011-12-08 14:40:16 +00004700 tp->link_config.rmt_adv =
4701 mii_adv_to_ethtool_adv_x(remote_adv);
4702
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 tg3_setup_flow_control(tp, local_adv, remote_adv);
4704
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705 current_link_up = 1;
4706 }
4707 for (i = 0; i < 30; i++) {
4708 udelay(20);
4709 tw32_f(MAC_STATUS,
4710 (MAC_STATUS_SYNC_CHANGED |
4711 MAC_STATUS_CFG_CHANGED));
4712 udelay(40);
4713 if ((tr32(MAC_STATUS) &
4714 (MAC_STATUS_SYNC_CHANGED |
4715 MAC_STATUS_CFG_CHANGED)) == 0)
4716 break;
4717 }
4718
4719 mac_status = tr32(MAC_STATUS);
4720 if (current_link_up == 0 &&
4721 (mac_status & MAC_STATUS_PCS_SYNCED) &&
4722 !(mac_status & MAC_STATUS_RCVD_CFG))
4723 current_link_up = 1;
4724 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08004725 tg3_setup_flow_control(tp, 0, 0);
4726
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727 /* Forcing 1000FD link up. */
4728 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729
4730 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
4731 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004732
4733 tw32_f(MAC_MODE, tp->mac_mode);
4734 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735 }
4736
4737out:
4738 return current_link_up;
4739}
4740
4741static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
4742{
4743 u32 orig_pause_cfg;
4744 u16 orig_active_speed;
4745 u8 orig_active_duplex;
4746 u32 mac_status;
4747 int current_link_up;
4748 int i;
4749
Matt Carlson8d018622007-12-20 20:05:44 -08004750 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751 orig_active_speed = tp->link_config.active_speed;
4752 orig_active_duplex = tp->link_config.active_duplex;
4753
Joe Perches63c3a662011-04-26 08:12:10 +00004754 if (!tg3_flag(tp, HW_AUTONEG) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 netif_carrier_ok(tp->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00004756 tg3_flag(tp, INIT_COMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757 mac_status = tr32(MAC_STATUS);
4758 mac_status &= (MAC_STATUS_PCS_SYNCED |
4759 MAC_STATUS_SIGNAL_DET |
4760 MAC_STATUS_CFG_CHANGED |
4761 MAC_STATUS_RCVD_CFG);
4762 if (mac_status == (MAC_STATUS_PCS_SYNCED |
4763 MAC_STATUS_SIGNAL_DET)) {
4764 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4765 MAC_STATUS_CFG_CHANGED));
4766 return 0;
4767 }
4768 }
4769
4770 tw32_f(MAC_TX_AUTO_NEG, 0);
4771
4772 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
4773 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
4774 tw32_f(MAC_MODE, tp->mac_mode);
4775 udelay(40);
4776
Matt Carlson79eb6902010-02-17 15:17:03 +00004777 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778 tg3_init_bcm8002(tp);
4779
4780 /* Enable link change event even when serdes polling. */
4781 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4782 udelay(40);
4783
4784 current_link_up = 0;
Matt Carlson859edb22011-12-08 14:40:16 +00004785 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786 mac_status = tr32(MAC_STATUS);
4787
Joe Perches63c3a662011-04-26 08:12:10 +00004788 if (tg3_flag(tp, HW_AUTONEG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
4790 else
4791 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
4792
Matt Carlson898a56f2009-08-28 14:02:40 +00004793 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00004795 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796
4797 for (i = 0; i < 100; i++) {
4798 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4799 MAC_STATUS_CFG_CHANGED));
4800 udelay(5);
4801 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07004802 MAC_STATUS_CFG_CHANGED |
4803 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 break;
4805 }
4806
4807 mac_status = tr32(MAC_STATUS);
4808 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
4809 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004810 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
4811 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812 tw32_f(MAC_MODE, (tp->mac_mode |
4813 MAC_MODE_SEND_CONFIGS));
4814 udelay(1);
4815 tw32_f(MAC_MODE, tp->mac_mode);
4816 }
4817 }
4818
4819 if (current_link_up == 1) {
4820 tp->link_config.active_speed = SPEED_1000;
4821 tp->link_config.active_duplex = DUPLEX_FULL;
4822 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4823 LED_CTRL_LNKLED_OVERRIDE |
4824 LED_CTRL_1000MBPS_ON));
4825 } else {
4826 tp->link_config.active_speed = SPEED_INVALID;
4827 tp->link_config.active_duplex = DUPLEX_INVALID;
4828 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4829 LED_CTRL_LNKLED_OVERRIDE |
4830 LED_CTRL_TRAFFIC_OVERRIDE));
4831 }
4832
4833 if (current_link_up != netif_carrier_ok(tp->dev)) {
4834 if (current_link_up)
4835 netif_carrier_on(tp->dev);
4836 else
4837 netif_carrier_off(tp->dev);
4838 tg3_link_report(tp);
4839 } else {
Matt Carlson8d018622007-12-20 20:05:44 -08004840 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841 if (orig_pause_cfg != now_pause_cfg ||
4842 orig_active_speed != tp->link_config.active_speed ||
4843 orig_active_duplex != tp->link_config.active_duplex)
4844 tg3_link_report(tp);
4845 }
4846
4847 return 0;
4848}
4849
Michael Chan747e8f82005-07-25 12:33:22 -07004850static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
4851{
4852 int current_link_up, err = 0;
4853 u32 bmsr, bmcr;
4854 u16 current_speed;
4855 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08004856 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07004857
4858 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4859 tw32_f(MAC_MODE, tp->mac_mode);
4860 udelay(40);
4861
4862 tw32(MAC_EVENT, 0);
4863
4864 tw32_f(MAC_STATUS,
4865 (MAC_STATUS_SYNC_CHANGED |
4866 MAC_STATUS_CFG_CHANGED |
4867 MAC_STATUS_MI_COMPLETION |
4868 MAC_STATUS_LNKSTATE_CHANGED));
4869 udelay(40);
4870
4871 if (force_reset)
4872 tg3_phy_reset(tp);
4873
4874 current_link_up = 0;
4875 current_speed = SPEED_INVALID;
4876 current_duplex = DUPLEX_INVALID;
Matt Carlson859edb22011-12-08 14:40:16 +00004877 tp->link_config.rmt_adv = 0;
Michael Chan747e8f82005-07-25 12:33:22 -07004878
4879 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4880 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004881 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
4882 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4883 bmsr |= BMSR_LSTATUS;
4884 else
4885 bmsr &= ~BMSR_LSTATUS;
4886 }
Michael Chan747e8f82005-07-25 12:33:22 -07004887
4888 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
4889
4890 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004891 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004892 /* do nothing, just check for link up at the end */
4893 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson28011cf2011-11-16 18:36:59 -05004894 u32 adv, newadv;
Michael Chan747e8f82005-07-25 12:33:22 -07004895
4896 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
Matt Carlson28011cf2011-11-16 18:36:59 -05004897 newadv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
4898 ADVERTISE_1000XPAUSE |
4899 ADVERTISE_1000XPSE_ASYM |
4900 ADVERTISE_SLCT);
Michael Chan747e8f82005-07-25 12:33:22 -07004901
Matt Carlson28011cf2011-11-16 18:36:59 -05004902 newadv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Matt Carlson37f07022011-11-17 14:30:55 +00004903 newadv |= ethtool_adv_to_mii_adv_x(tp->link_config.advertising);
Michael Chan747e8f82005-07-25 12:33:22 -07004904
Matt Carlson28011cf2011-11-16 18:36:59 -05004905 if ((newadv != adv) || !(bmcr & BMCR_ANENABLE)) {
4906 tg3_writephy(tp, MII_ADVERTISE, newadv);
Michael Chan747e8f82005-07-25 12:33:22 -07004907 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
4908 tg3_writephy(tp, MII_BMCR, bmcr);
4909
4910 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07004911 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004912 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004913
4914 return err;
4915 }
4916 } else {
4917 u32 new_bmcr;
4918
4919 bmcr &= ~BMCR_SPEED1000;
4920 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
4921
4922 if (tp->link_config.duplex == DUPLEX_FULL)
4923 new_bmcr |= BMCR_FULLDPLX;
4924
4925 if (new_bmcr != bmcr) {
4926 /* BMCR_SPEED1000 is a reserved bit that needs
4927 * to be set on write.
4928 */
4929 new_bmcr |= BMCR_SPEED1000;
4930
4931 /* Force a linkdown */
4932 if (netif_carrier_ok(tp->dev)) {
4933 u32 adv;
4934
4935 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4936 adv &= ~(ADVERTISE_1000XFULL |
4937 ADVERTISE_1000XHALF |
4938 ADVERTISE_SLCT);
4939 tg3_writephy(tp, MII_ADVERTISE, adv);
4940 tg3_writephy(tp, MII_BMCR, bmcr |
4941 BMCR_ANRESTART |
4942 BMCR_ANENABLE);
4943 udelay(10);
4944 netif_carrier_off(tp->dev);
4945 }
4946 tg3_writephy(tp, MII_BMCR, new_bmcr);
4947 bmcr = new_bmcr;
4948 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4949 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004950 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
4951 ASIC_REV_5714) {
4952 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4953 bmsr |= BMSR_LSTATUS;
4954 else
4955 bmsr &= ~BMSR_LSTATUS;
4956 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004957 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004958 }
4959 }
4960
4961 if (bmsr & BMSR_LSTATUS) {
4962 current_speed = SPEED_1000;
4963 current_link_up = 1;
4964 if (bmcr & BMCR_FULLDPLX)
4965 current_duplex = DUPLEX_FULL;
4966 else
4967 current_duplex = DUPLEX_HALF;
4968
Matt Carlsonef167e22007-12-20 20:10:01 -08004969 local_adv = 0;
4970 remote_adv = 0;
4971
Michael Chan747e8f82005-07-25 12:33:22 -07004972 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08004973 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07004974
4975 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
4976 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
4977 common = local_adv & remote_adv;
4978 if (common & (ADVERTISE_1000XHALF |
4979 ADVERTISE_1000XFULL)) {
4980 if (common & ADVERTISE_1000XFULL)
4981 current_duplex = DUPLEX_FULL;
4982 else
4983 current_duplex = DUPLEX_HALF;
Matt Carlson859edb22011-12-08 14:40:16 +00004984
4985 tp->link_config.rmt_adv =
4986 mii_adv_to_ethtool_adv_x(remote_adv);
Joe Perches63c3a662011-04-26 08:12:10 +00004987 } else if (!tg3_flag(tp, 5780_CLASS)) {
Matt Carlson57d8b882010-06-05 17:24:35 +00004988 /* Link is up via parallel detect */
Matt Carlson859a5882010-04-05 10:19:28 +00004989 } else {
Michael Chan747e8f82005-07-25 12:33:22 -07004990 current_link_up = 0;
Matt Carlson859a5882010-04-05 10:19:28 +00004991 }
Michael Chan747e8f82005-07-25 12:33:22 -07004992 }
4993 }
4994
Matt Carlsonef167e22007-12-20 20:10:01 -08004995 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
4996 tg3_setup_flow_control(tp, local_adv, remote_adv);
4997
Michael Chan747e8f82005-07-25 12:33:22 -07004998 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4999 if (tp->link_config.active_duplex == DUPLEX_HALF)
5000 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
5001
5002 tw32_f(MAC_MODE, tp->mac_mode);
5003 udelay(40);
5004
5005 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5006
5007 tp->link_config.active_speed = current_speed;
5008 tp->link_config.active_duplex = current_duplex;
5009
5010 if (current_link_up != netif_carrier_ok(tp->dev)) {
5011 if (current_link_up)
5012 netif_carrier_on(tp->dev);
5013 else {
5014 netif_carrier_off(tp->dev);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005015 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005016 }
5017 tg3_link_report(tp);
5018 }
5019 return err;
5020}
5021
5022static void tg3_serdes_parallel_detect(struct tg3 *tp)
5023{
Michael Chan3d3ebe72006-09-27 15:59:15 -07005024 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07005025 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07005026 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07005027 return;
5028 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005029
Michael Chan747e8f82005-07-25 12:33:22 -07005030 if (!netif_carrier_ok(tp->dev) &&
5031 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
5032 u32 bmcr;
5033
5034 tg3_readphy(tp, MII_BMCR, &bmcr);
5035 if (bmcr & BMCR_ANENABLE) {
5036 u32 phy1, phy2;
5037
5038 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005039 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
5040 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07005041
5042 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005043 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5044 MII_TG3_DSP_EXP1_INT_STAT);
5045 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
5046 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005047
5048 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
5049 /* We have signal detect and not receiving
5050 * config code words, link is up by parallel
5051 * detection.
5052 */
5053
5054 bmcr &= ~BMCR_ANENABLE;
5055 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
5056 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005057 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005058 }
5059 }
Matt Carlson859a5882010-04-05 10:19:28 +00005060 } else if (netif_carrier_ok(tp->dev) &&
5061 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005062 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005063 u32 phy2;
5064
5065 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005066 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5067 MII_TG3_DSP_EXP1_INT_STAT);
5068 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005069 if (phy2 & 0x20) {
5070 u32 bmcr;
5071
5072 /* Config code words received, turn on autoneg. */
5073 tg3_readphy(tp, MII_BMCR, &bmcr);
5074 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
5075
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005076 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005077
5078 }
5079 }
5080}
5081
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082static int tg3_setup_phy(struct tg3 *tp, int force_reset)
5083{
Matt Carlsonf2096f92011-04-05 14:22:48 +00005084 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085 int err;
5086
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005087 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005089 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07005090 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a5882010-04-05 10:19:28 +00005091 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093
Matt Carlsonbcb37f62008-11-03 16:52:09 -08005094 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00005095 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08005096
5097 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
5098 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
5099 scale = 65;
5100 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
5101 scale = 6;
5102 else
5103 scale = 12;
5104
5105 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
5106 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
5107 tw32(GRC_MISC_CFG, val);
5108 }
5109
Matt Carlsonf2096f92011-04-05 14:22:48 +00005110 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
5111 (6 << TX_LENGTHS_IPG_SHIFT);
5112 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
5113 val |= tr32(MAC_TX_LENGTHS) &
5114 (TX_LENGTHS_JMB_FRM_LEN_MSK |
5115 TX_LENGTHS_CNT_DWN_VAL_MSK);
5116
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117 if (tp->link_config.active_speed == SPEED_1000 &&
5118 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00005119 tw32(MAC_TX_LENGTHS, val |
5120 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00005122 tw32(MAC_TX_LENGTHS, val |
5123 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124
Joe Perches63c3a662011-04-26 08:12:10 +00005125 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126 if (netif_carrier_ok(tp->dev)) {
5127 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07005128 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129 } else {
5130 tw32(HOSTCC_STAT_COAL_TICKS, 0);
5131 }
5132 }
5133
Joe Perches63c3a662011-04-26 08:12:10 +00005134 if (tg3_flag(tp, ASPM_WORKAROUND)) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00005135 val = tr32(PCIE_PWR_MGMT_THRESH);
Matt Carlson8ed5d972007-05-07 00:25:49 -07005136 if (!netif_carrier_ok(tp->dev))
5137 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
5138 tp->pwrmgmt_thresh;
5139 else
5140 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
5141 tw32(PCIE_PWR_MGMT_THRESH, val);
5142 }
5143
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144 return err;
5145}
5146
Matt Carlson66cfd1b2010-09-30 10:34:30 +00005147static inline int tg3_irq_sync(struct tg3 *tp)
5148{
5149 return tp->irq_sync;
5150}
5151
Matt Carlson97bd8e42011-04-13 11:05:04 +00005152static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
5153{
5154 int i;
5155
5156 dst = (u32 *)((u8 *)dst + off);
5157 for (i = 0; i < len; i += sizeof(u32))
5158 *dst++ = tr32(off + i);
5159}
5160
5161static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
5162{
5163 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
5164 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
5165 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
5166 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
5167 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
5168 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
5169 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
5170 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
5171 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
5172 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
5173 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
5174 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
5175 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
5176 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
5177 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
5178 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
5179 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
5180 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
5181 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
5182
Joe Perches63c3a662011-04-26 08:12:10 +00005183 if (tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson97bd8e42011-04-13 11:05:04 +00005184 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
5185
5186 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
5187 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
5188 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
5189 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
5190 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
5191 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
5192 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
5193 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
5194
Joe Perches63c3a662011-04-26 08:12:10 +00005195 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00005196 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
5197 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
5198 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
5199 }
5200
5201 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
5202 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
5203 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
5204 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
5205 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
5206
Joe Perches63c3a662011-04-26 08:12:10 +00005207 if (tg3_flag(tp, NVRAM))
Matt Carlson97bd8e42011-04-13 11:05:04 +00005208 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
5209}
5210
5211static void tg3_dump_state(struct tg3 *tp)
5212{
5213 int i;
5214 u32 *regs;
5215
5216 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
5217 if (!regs) {
5218 netdev_err(tp->dev, "Failed allocating register dump buffer\n");
5219 return;
5220 }
5221
Joe Perches63c3a662011-04-26 08:12:10 +00005222 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00005223 /* Read up to but not including private PCI registers */
5224 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
5225 regs[i / sizeof(u32)] = tr32(i);
5226 } else
5227 tg3_dump_legacy_regs(tp, regs);
5228
5229 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
5230 if (!regs[i + 0] && !regs[i + 1] &&
5231 !regs[i + 2] && !regs[i + 3])
5232 continue;
5233
5234 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
5235 i * 4,
5236 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
5237 }
5238
5239 kfree(regs);
5240
5241 for (i = 0; i < tp->irq_cnt; i++) {
5242 struct tg3_napi *tnapi = &tp->napi[i];
5243
5244 /* SW status block */
5245 netdev_err(tp->dev,
5246 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
5247 i,
5248 tnapi->hw_status->status,
5249 tnapi->hw_status->status_tag,
5250 tnapi->hw_status->rx_jumbo_consumer,
5251 tnapi->hw_status->rx_consumer,
5252 tnapi->hw_status->rx_mini_consumer,
5253 tnapi->hw_status->idx[0].rx_producer,
5254 tnapi->hw_status->idx[0].tx_consumer);
5255
5256 netdev_err(tp->dev,
5257 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
5258 i,
5259 tnapi->last_tag, tnapi->last_irq_tag,
5260 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
5261 tnapi->rx_rcb_ptr,
5262 tnapi->prodring.rx_std_prod_idx,
5263 tnapi->prodring.rx_std_cons_idx,
5264 tnapi->prodring.rx_jmb_prod_idx,
5265 tnapi->prodring.rx_jmb_cons_idx);
5266 }
5267}
5268
Michael Chandf3e6542006-05-26 17:48:07 -07005269/* This is called whenever we suspect that the system chipset is re-
5270 * ordering the sequence of MMIO to the tx send mailbox. The symptom
5271 * is bogus tx completions. We try to recover by setting the
5272 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
5273 * in the workqueue.
5274 */
5275static void tg3_tx_recover(struct tg3 *tp)
5276{
Joe Perches63c3a662011-04-26 08:12:10 +00005277 BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
Michael Chandf3e6542006-05-26 17:48:07 -07005278 tp->write32_tx_mbox == tg3_write_indirect_mbox);
5279
Matt Carlson5129c3a2010-04-05 10:19:23 +00005280 netdev_warn(tp->dev,
5281 "The system may be re-ordering memory-mapped I/O "
5282 "cycles to the network device, attempting to recover. "
5283 "Please report the problem to the driver maintainer "
5284 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07005285
5286 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00005287 tg3_flag_set(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07005288 spin_unlock(&tp->lock);
5289}
5290
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005291static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07005292{
Matt Carlsonf65aac12010-08-02 11:26:03 +00005293 /* Tell compiler to fetch tx indices from memory. */
5294 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005295 return tnapi->tx_pending -
5296 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07005297}
5298
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299/* Tigon3 never reports partial packet sends. So we do not
5300 * need special logic to handle SKBs that have not had all
5301 * of their frags sent yet, like SunGEM does.
5302 */
Matt Carlson17375d22009-08-28 14:02:18 +00005303static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304{
Matt Carlson17375d22009-08-28 14:02:18 +00005305 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005306 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005307 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005308 struct netdev_queue *txq;
5309 int index = tnapi - tp->napi;
Tom Herbert298376d2011-11-28 16:33:30 +00005310 unsigned int pkts_compl = 0, bytes_compl = 0;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005311
Joe Perches63c3a662011-04-26 08:12:10 +00005312 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005313 index--;
5314
5315 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316
5317 while (sw_idx != hw_idx) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00005318 struct tg3_tx_ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07005320 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321
Michael Chandf3e6542006-05-26 17:48:07 -07005322 if (unlikely(skb == NULL)) {
5323 tg3_tx_recover(tp);
5324 return;
5325 }
5326
Alexander Duyckf4188d82009-12-02 16:48:38 +00005327 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005328 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005329 skb_headlen(skb),
5330 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331
5332 ri->skb = NULL;
5333
Matt Carlsone01ee142011-07-27 14:20:50 +00005334 while (ri->fragmented) {
5335 ri->fragmented = false;
5336 sw_idx = NEXT_TX(sw_idx);
5337 ri = &tnapi->tx_buffers[sw_idx];
5338 }
5339
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340 sw_idx = NEXT_TX(sw_idx);
5341
5342 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005343 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07005344 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
5345 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005346
5347 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005348 dma_unmap_addr(ri, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00005349 skb_frag_size(&skb_shinfo(skb)->frags[i]),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005350 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00005351
5352 while (ri->fragmented) {
5353 ri->fragmented = false;
5354 sw_idx = NEXT_TX(sw_idx);
5355 ri = &tnapi->tx_buffers[sw_idx];
5356 }
5357
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358 sw_idx = NEXT_TX(sw_idx);
5359 }
5360
Tom Herbert298376d2011-11-28 16:33:30 +00005361 pkts_compl++;
5362 bytes_compl += skb->len;
5363
David S. Millerf47c11e2005-06-24 20:18:35 -07005364 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07005365
5366 if (unlikely(tx_bug)) {
5367 tg3_tx_recover(tp);
5368 return;
5369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370 }
5371
Tom Herbert298376d2011-11-28 16:33:30 +00005372 netdev_completed_queue(tp->dev, pkts_compl, bytes_compl);
5373
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005374 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375
Michael Chan1b2a7202006-08-07 21:46:02 -07005376 /* Need to make the tx_cons update visible to tg3_start_xmit()
5377 * before checking for netif_queue_stopped(). Without the
5378 * memory barrier, there is a small possibility that tg3_start_xmit()
5379 * will miss it and cause the queue to be stopped forever.
5380 */
5381 smp_mb();
5382
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005383 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005384 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005385 __netif_tx_lock(txq, smp_processor_id());
5386 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005387 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005388 netif_tx_wake_queue(txq);
5389 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07005390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391}
5392
Eric Dumazet9205fd92011-11-18 06:47:01 +00005393static void tg3_rx_data_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005394{
Eric Dumazet9205fd92011-11-18 06:47:01 +00005395 if (!ri->data)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005396 return;
5397
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005398 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005399 map_sz, PCI_DMA_FROMDEVICE);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005400 kfree(ri->data);
5401 ri->data = NULL;
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005402}
5403
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404/* Returns size of skb allocated or < 0 on error.
5405 *
5406 * We only need to fill in the address because the other members
5407 * of the RX descriptor are invariant, see tg3_init_rings.
5408 *
5409 * Note the purposeful assymetry of cpu vs. chip accesses. For
5410 * posting buffers we only dirty the first cache line of the RX
5411 * descriptor (containing the address). Whereas for the RX status
5412 * buffers the cpu only reads the last cacheline of the RX descriptor
5413 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
5414 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00005415static int tg3_alloc_rx_data(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Matt Carlsona3896162009-11-13 13:03:44 +00005416 u32 opaque_key, u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417{
5418 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00005419 struct ring_info *map;
Eric Dumazet9205fd92011-11-18 06:47:01 +00005420 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421 dma_addr_t mapping;
Eric Dumazet9205fd92011-11-18 06:47:01 +00005422 int skb_size, data_size, dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424 switch (opaque_key) {
5425 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00005426 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00005427 desc = &tpr->rx_std[dest_idx];
5428 map = &tpr->rx_std_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00005429 data_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430 break;
5431
5432 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00005433 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00005434 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00005435 map = &tpr->rx_jmb_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00005436 data_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437 break;
5438
5439 default:
5440 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442
5443 /* Do not overwrite any of the map or rp information
5444 * until we are sure we can commit to a new buffer.
5445 *
5446 * Callers depend upon this behavior and assume that
5447 * we leave everything unchanged if we fail.
5448 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00005449 skb_size = SKB_DATA_ALIGN(data_size + TG3_RX_OFFSET(tp)) +
5450 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
5451 data = kmalloc(skb_size, GFP_ATOMIC);
5452 if (!data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453 return -ENOMEM;
5454
Eric Dumazet9205fd92011-11-18 06:47:01 +00005455 mapping = pci_map_single(tp->pdev,
5456 data + TG3_RX_OFFSET(tp),
5457 data_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458 PCI_DMA_FROMDEVICE);
Matt Carlsona21771d2009-11-02 14:25:31 +00005459 if (pci_dma_mapping_error(tp->pdev, mapping)) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00005460 kfree(data);
Matt Carlsona21771d2009-11-02 14:25:31 +00005461 return -EIO;
5462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463
Eric Dumazet9205fd92011-11-18 06:47:01 +00005464 map->data = data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005465 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467 desc->addr_hi = ((u64)mapping >> 32);
5468 desc->addr_lo = ((u64)mapping & 0xffffffff);
5469
Eric Dumazet9205fd92011-11-18 06:47:01 +00005470 return data_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005471}
5472
5473/* We only need to move over in the address because the other
5474 * members of the RX descriptor are invariant. See notes above
Eric Dumazet9205fd92011-11-18 06:47:01 +00005475 * tg3_alloc_rx_data for full details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476 */
Matt Carlsona3896162009-11-13 13:03:44 +00005477static void tg3_recycle_rx(struct tg3_napi *tnapi,
5478 struct tg3_rx_prodring_set *dpr,
5479 u32 opaque_key, int src_idx,
5480 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481{
Matt Carlson17375d22009-08-28 14:02:18 +00005482 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
5484 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00005485 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005486 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487
5488 switch (opaque_key) {
5489 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00005490 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00005491 dest_desc = &dpr->rx_std[dest_idx];
5492 dest_map = &dpr->rx_std_buffers[dest_idx];
5493 src_desc = &spr->rx_std[src_idx];
5494 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495 break;
5496
5497 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00005498 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00005499 dest_desc = &dpr->rx_jmb[dest_idx].std;
5500 dest_map = &dpr->rx_jmb_buffers[dest_idx];
5501 src_desc = &spr->rx_jmb[src_idx].std;
5502 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503 break;
5504
5505 default:
5506 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508
Eric Dumazet9205fd92011-11-18 06:47:01 +00005509 dest_map->data = src_map->data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005510 dma_unmap_addr_set(dest_map, mapping,
5511 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512 dest_desc->addr_hi = src_desc->addr_hi;
5513 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00005514
5515 /* Ensure that the update to the skb happens after the physical
5516 * addresses have been transferred to the new BD location.
5517 */
5518 smp_wmb();
5519
Eric Dumazet9205fd92011-11-18 06:47:01 +00005520 src_map->data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521}
5522
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523/* The RX ring scheme is composed of multiple rings which post fresh
5524 * buffers to the chip, and one special ring the chip uses to report
5525 * status back to the host.
5526 *
5527 * The special ring reports the status of received packets to the
5528 * host. The chip does not write into the original descriptor the
5529 * RX buffer was obtained from. The chip simply takes the original
5530 * descriptor as provided by the host, updates the status and length
5531 * field, then writes this into the next status ring entry.
5532 *
5533 * Each ring the host uses to post buffers to the chip is described
5534 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
5535 * it is first placed into the on-chip ram. When the packet's length
5536 * is known, it walks down the TG3_BDINFO entries to select the ring.
5537 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
5538 * which is within the range of the new packet's length is chosen.
5539 *
5540 * The "separate ring for rx status" scheme may sound queer, but it makes
5541 * sense from a cache coherency perspective. If only the host writes
5542 * to the buffer post rings, and only the chip writes to the rx status
5543 * rings, then cache lines never move beyond shared-modified state.
5544 * If both the host and chip were to write into the same ring, cache line
5545 * eviction could occur since both entities want it in an exclusive state.
5546 */
Matt Carlson17375d22009-08-28 14:02:18 +00005547static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548{
Matt Carlson17375d22009-08-28 14:02:18 +00005549 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07005550 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00005551 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00005552 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07005553 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00005555 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005557 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558 /*
5559 * We need to order the read of hw_idx and the read of
5560 * the opaque cookie.
5561 */
5562 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563 work_mask = 0;
5564 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00005565 std_prod_idx = tpr->rx_std_prod_idx;
5566 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00005568 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00005569 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570 unsigned int len;
5571 struct sk_buff *skb;
5572 dma_addr_t dma_addr;
5573 u32 opaque_key, desc_idx, *post_ptr;
Eric Dumazet9205fd92011-11-18 06:47:01 +00005574 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575
5576 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
5577 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
5578 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005579 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005580 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005581 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00005582 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07005583 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005585 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005586 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005587 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00005588 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00005589 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591
5592 work_mask |= opaque_key;
5593
5594 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
5595 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
5596 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00005597 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598 desc_idx, *post_ptr);
5599 drop_it_no_recycle:
5600 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00005601 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602 goto next_pkt;
5603 }
5604
Eric Dumazet9205fd92011-11-18 06:47:01 +00005605 prefetch(data + TG3_RX_OFFSET(tp));
Matt Carlsonad829262008-11-21 17:16:16 -08005606 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
5607 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608
Matt Carlsond2757fc2010-04-12 06:58:27 +00005609 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610 int skb_size;
5611
Eric Dumazet9205fd92011-11-18 06:47:01 +00005612 skb_size = tg3_alloc_rx_data(tp, tpr, opaque_key,
Matt Carlsonafc081f2009-11-13 13:03:43 +00005613 *post_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614 if (skb_size < 0)
5615 goto drop_it;
5616
Matt Carlson287be122009-08-28 13:58:46 +00005617 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618 PCI_DMA_FROMDEVICE);
5619
Eric Dumazet9205fd92011-11-18 06:47:01 +00005620 skb = build_skb(data);
5621 if (!skb) {
5622 kfree(data);
5623 goto drop_it_no_recycle;
5624 }
5625 skb_reserve(skb, TG3_RX_OFFSET(tp));
5626 /* Ensure that the update to the data happens
Matt Carlson61e800c2010-02-17 15:16:54 +00005627 * after the usage of the old DMA mapping.
5628 */
5629 smp_wmb();
5630
Eric Dumazet9205fd92011-11-18 06:47:01 +00005631 ri->data = NULL;
Matt Carlson61e800c2010-02-17 15:16:54 +00005632
Linus Torvalds1da177e2005-04-16 15:20:36 -07005633 } else {
Matt Carlsona3896162009-11-13 13:03:44 +00005634 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635 desc_idx, *post_ptr);
5636
Eric Dumazet9205fd92011-11-18 06:47:01 +00005637 skb = netdev_alloc_skb(tp->dev,
5638 len + TG3_RAW_IP_ALIGN);
5639 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640 goto drop_it_no_recycle;
5641
Eric Dumazet9205fd92011-11-18 06:47:01 +00005642 skb_reserve(skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005644 memcpy(skb->data,
5645 data + TG3_RX_OFFSET(tp),
5646 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648 }
5649
Eric Dumazet9205fd92011-11-18 06:47:01 +00005650 skb_put(skb, len);
Michał Mirosławdc668912011-04-07 03:35:07 +00005651 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
5653 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
5654 >> RXD_TCPCSUM_SHIFT) == 0xffff))
5655 skb->ip_summed = CHECKSUM_UNNECESSARY;
5656 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005657 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658
5659 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00005660
5661 if (len > (tp->dev->mtu + ETH_HLEN) &&
5662 skb->protocol != htons(ETH_P_8021Q)) {
5663 dev_kfree_skb(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00005664 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00005665 }
5666
Matt Carlson9dc7a112010-04-12 06:58:28 +00005667 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00005668 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
5669 __vlan_hwaccel_put_tag(skb,
5670 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00005671
Matt Carlsonbf933c82011-01-25 15:58:49 +00005672 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674 received++;
5675 budget--;
5676
5677next_pkt:
5678 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07005679
5680 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005681 tpr->rx_std_prod_idx = std_prod_idx &
5682 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00005683 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5684 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07005685 work_mask &= ~RXD_OPAQUE_RING_STD;
5686 rx_std_posted = 0;
5687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07005689 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00005690 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07005691
5692 /* Refresh hw_idx to see if there is new work */
5693 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005694 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07005695 rmb();
5696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697 }
5698
5699 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00005700 tnapi->rx_rcb_ptr = sw_idx;
5701 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702
5703 /* Refill RX ring(s). */
Joe Perches63c3a662011-04-26 08:12:10 +00005704 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005705 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005706 tpr->rx_std_prod_idx = std_prod_idx &
5707 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005708 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5709 tpr->rx_std_prod_idx);
5710 }
5711 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005712 tpr->rx_jmb_prod_idx = jmb_prod_idx &
5713 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005714 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5715 tpr->rx_jmb_prod_idx);
5716 }
5717 mmiowb();
5718 } else if (work_mask) {
5719 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
5720 * updated before the producer indices can be updated.
5721 */
5722 smp_wmb();
5723
Matt Carlson2c49a442010-09-30 10:34:35 +00005724 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
5725 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005726
Matt Carlsone4af1af2010-02-12 14:47:05 +00005727 if (tnapi != &tp->napi[1])
5728 napi_schedule(&tp->napi[1].napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730
5731 return received;
5732}
5733
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005734static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736 /* handle link change and other phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00005737 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005738 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
5739
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740 if (sblk->status & SD_STATUS_LINK_CHG) {
5741 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005742 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07005743 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00005744 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsondd477002008-05-25 23:45:58 -07005745 tw32_f(MAC_STATUS,
5746 (MAC_STATUS_SYNC_CHANGED |
5747 MAC_STATUS_CFG_CHANGED |
5748 MAC_STATUS_MI_COMPLETION |
5749 MAC_STATUS_LNKSTATE_CHANGED));
5750 udelay(40);
5751 } else
5752 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07005753 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005754 }
5755 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005756}
5757
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005758static int tg3_rx_prodring_xfer(struct tg3 *tp,
5759 struct tg3_rx_prodring_set *dpr,
5760 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005761{
5762 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005763 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005764
5765 while (1) {
5766 src_prod_idx = spr->rx_std_prod_idx;
5767
5768 /* Make sure updates to the rx_std_buffers[] entries and the
5769 * standard producer index are seen in the correct order.
5770 */
5771 smp_rmb();
5772
5773 if (spr->rx_std_cons_idx == src_prod_idx)
5774 break;
5775
5776 if (spr->rx_std_cons_idx < src_prod_idx)
5777 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
5778 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005779 cpycnt = tp->rx_std_ring_mask + 1 -
5780 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005781
Matt Carlson2c49a442010-09-30 10:34:35 +00005782 cpycnt = min(cpycnt,
5783 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005784
5785 si = spr->rx_std_cons_idx;
5786 di = dpr->rx_std_prod_idx;
5787
Matt Carlsone92967b2010-02-12 14:47:06 +00005788 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00005789 if (dpr->rx_std_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00005790 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005791 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005792 break;
5793 }
5794 }
5795
5796 if (!cpycnt)
5797 break;
5798
5799 /* Ensure that updates to the rx_std_buffers ring and the
5800 * shadowed hardware producer ring from tg3_recycle_skb() are
5801 * ordered correctly WRT the skb check above.
5802 */
5803 smp_rmb();
5804
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005805 memcpy(&dpr->rx_std_buffers[di],
5806 &spr->rx_std_buffers[si],
5807 cpycnt * sizeof(struct ring_info));
5808
5809 for (i = 0; i < cpycnt; i++, di++, si++) {
5810 struct tg3_rx_buffer_desc *sbd, *dbd;
5811 sbd = &spr->rx_std[si];
5812 dbd = &dpr->rx_std[di];
5813 dbd->addr_hi = sbd->addr_hi;
5814 dbd->addr_lo = sbd->addr_lo;
5815 }
5816
Matt Carlson2c49a442010-09-30 10:34:35 +00005817 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
5818 tp->rx_std_ring_mask;
5819 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
5820 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005821 }
5822
5823 while (1) {
5824 src_prod_idx = spr->rx_jmb_prod_idx;
5825
5826 /* Make sure updates to the rx_jmb_buffers[] entries and
5827 * the jumbo producer index are seen in the correct order.
5828 */
5829 smp_rmb();
5830
5831 if (spr->rx_jmb_cons_idx == src_prod_idx)
5832 break;
5833
5834 if (spr->rx_jmb_cons_idx < src_prod_idx)
5835 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
5836 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005837 cpycnt = tp->rx_jmb_ring_mask + 1 -
5838 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005839
5840 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00005841 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005842
5843 si = spr->rx_jmb_cons_idx;
5844 di = dpr->rx_jmb_prod_idx;
5845
Matt Carlsone92967b2010-02-12 14:47:06 +00005846 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00005847 if (dpr->rx_jmb_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00005848 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005849 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005850 break;
5851 }
5852 }
5853
5854 if (!cpycnt)
5855 break;
5856
5857 /* Ensure that updates to the rx_jmb_buffers ring and the
5858 * shadowed hardware producer ring from tg3_recycle_skb() are
5859 * ordered correctly WRT the skb check above.
5860 */
5861 smp_rmb();
5862
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005863 memcpy(&dpr->rx_jmb_buffers[di],
5864 &spr->rx_jmb_buffers[si],
5865 cpycnt * sizeof(struct ring_info));
5866
5867 for (i = 0; i < cpycnt; i++, di++, si++) {
5868 struct tg3_rx_buffer_desc *sbd, *dbd;
5869 sbd = &spr->rx_jmb[si].std;
5870 dbd = &dpr->rx_jmb[di].std;
5871 dbd->addr_hi = sbd->addr_hi;
5872 dbd->addr_lo = sbd->addr_lo;
5873 }
5874
Matt Carlson2c49a442010-09-30 10:34:35 +00005875 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
5876 tp->rx_jmb_ring_mask;
5877 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
5878 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005879 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005880
5881 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005882}
5883
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005884static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
5885{
5886 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005887
5888 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005889 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00005890 tg3_tx(tnapi);
Joe Perches63c3a662011-04-26 08:12:10 +00005891 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Michael Chan4fd7ab52007-10-12 01:39:50 -07005892 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005893 }
5894
Linus Torvalds1da177e2005-04-16 15:20:36 -07005895 /* run RX thread, within the bounds set by NAPI.
5896 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005897 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005899 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00005900 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005901
Joe Perches63c3a662011-04-26 08:12:10 +00005902 if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005903 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005904 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00005905 u32 std_prod_idx = dpr->rx_std_prod_idx;
5906 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005907
Matt Carlsone4af1af2010-02-12 14:47:05 +00005908 for (i = 1; i < tp->irq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005909 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00005910 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005911
5912 wmb();
5913
Matt Carlsone4af1af2010-02-12 14:47:05 +00005914 if (std_prod_idx != dpr->rx_std_prod_idx)
5915 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5916 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005917
Matt Carlsone4af1af2010-02-12 14:47:05 +00005918 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
5919 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5920 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005921
5922 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005923
5924 if (err)
5925 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005926 }
5927
David S. Miller6f535762007-10-11 18:08:29 -07005928 return work_done;
5929}
David S. Millerf7383c22005-05-18 22:50:53 -07005930
Matt Carlsondb219972011-11-04 09:15:03 +00005931static inline void tg3_reset_task_schedule(struct tg3 *tp)
5932{
5933 if (!test_and_set_bit(TG3_FLAG_RESET_TASK_PENDING, tp->tg3_flags))
5934 schedule_work(&tp->reset_task);
5935}
5936
5937static inline void tg3_reset_task_cancel(struct tg3 *tp)
5938{
5939 cancel_work_sync(&tp->reset_task);
5940 tg3_flag_clear(tp, RESET_TASK_PENDING);
5941}
5942
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005943static int tg3_poll_msix(struct napi_struct *napi, int budget)
5944{
5945 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5946 struct tg3 *tp = tnapi->tp;
5947 int work_done = 0;
5948 struct tg3_hw_status *sblk = tnapi->hw_status;
5949
5950 while (1) {
5951 work_done = tg3_poll_work(tnapi, work_done, budget);
5952
Joe Perches63c3a662011-04-26 08:12:10 +00005953 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005954 goto tx_recovery;
5955
5956 if (unlikely(work_done >= budget))
5957 break;
5958
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005959 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005960 * to tell the hw how much work has been processed,
5961 * so we must read it before checking for more work.
5962 */
5963 tnapi->last_tag = sblk->status_tag;
5964 tnapi->last_irq_tag = tnapi->last_tag;
5965 rmb();
5966
5967 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00005968 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
5969 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005970 napi_complete(napi);
5971 /* Reenable interrupts. */
5972 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
5973 mmiowb();
5974 break;
5975 }
5976 }
5977
5978 return work_done;
5979
5980tx_recovery:
5981 /* work_done is guaranteed to be less than budget. */
5982 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00005983 tg3_reset_task_schedule(tp);
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005984 return work_done;
5985}
5986
Matt Carlsone64de4e2011-04-13 11:05:05 +00005987static void tg3_process_error(struct tg3 *tp)
5988{
5989 u32 val;
5990 bool real_error = false;
5991
Joe Perches63c3a662011-04-26 08:12:10 +00005992 if (tg3_flag(tp, ERROR_PROCESSED))
Matt Carlsone64de4e2011-04-13 11:05:05 +00005993 return;
5994
5995 /* Check Flow Attention register */
5996 val = tr32(HOSTCC_FLOW_ATTN);
5997 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
5998 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
5999 real_error = true;
6000 }
6001
6002 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
6003 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
6004 real_error = true;
6005 }
6006
6007 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
6008 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
6009 real_error = true;
6010 }
6011
6012 if (!real_error)
6013 return;
6014
6015 tg3_dump_state(tp);
6016
Joe Perches63c3a662011-04-26 08:12:10 +00006017 tg3_flag_set(tp, ERROR_PROCESSED);
Matt Carlsondb219972011-11-04 09:15:03 +00006018 tg3_reset_task_schedule(tp);
Matt Carlsone64de4e2011-04-13 11:05:05 +00006019}
6020
David S. Miller6f535762007-10-11 18:08:29 -07006021static int tg3_poll(struct napi_struct *napi, int budget)
6022{
Matt Carlson8ef04422009-08-28 14:01:37 +00006023 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
6024 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07006025 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00006026 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07006027
6028 while (1) {
Matt Carlsone64de4e2011-04-13 11:05:05 +00006029 if (sblk->status & SD_STATUS_ERROR)
6030 tg3_process_error(tp);
6031
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006032 tg3_poll_link(tp);
6033
Matt Carlson17375d22009-08-28 14:02:18 +00006034 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07006035
Joe Perches63c3a662011-04-26 08:12:10 +00006036 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
David S. Miller6f535762007-10-11 18:08:29 -07006037 goto tx_recovery;
6038
6039 if (unlikely(work_done >= budget))
6040 break;
6041
Joe Perches63c3a662011-04-26 08:12:10 +00006042 if (tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson17375d22009-08-28 14:02:18 +00006043 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07006044 * to tell the hw how much work has been processed,
6045 * so we must read it before checking for more work.
6046 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006047 tnapi->last_tag = sblk->status_tag;
6048 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07006049 rmb();
6050 } else
6051 sblk->status &= ~SD_STATUS_UPDATED;
6052
Matt Carlson17375d22009-08-28 14:02:18 +00006053 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08006054 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00006055 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07006056 break;
6057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058 }
6059
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006060 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07006061
6062tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07006063 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08006064 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00006065 tg3_reset_task_schedule(tp);
Michael Chan4fd7ab52007-10-12 01:39:50 -07006066 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067}
6068
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006069static void tg3_napi_disable(struct tg3 *tp)
6070{
6071 int i;
6072
6073 for (i = tp->irq_cnt - 1; i >= 0; i--)
6074 napi_disable(&tp->napi[i].napi);
6075}
6076
6077static void tg3_napi_enable(struct tg3 *tp)
6078{
6079 int i;
6080
6081 for (i = 0; i < tp->irq_cnt; i++)
6082 napi_enable(&tp->napi[i].napi);
6083}
6084
6085static void tg3_napi_init(struct tg3 *tp)
6086{
6087 int i;
6088
6089 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
6090 for (i = 1; i < tp->irq_cnt; i++)
6091 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
6092}
6093
6094static void tg3_napi_fini(struct tg3 *tp)
6095{
6096 int i;
6097
6098 for (i = 0; i < tp->irq_cnt; i++)
6099 netif_napi_del(&tp->napi[i].napi);
6100}
6101
6102static inline void tg3_netif_stop(struct tg3 *tp)
6103{
6104 tp->dev->trans_start = jiffies; /* prevent tx timeout */
6105 tg3_napi_disable(tp);
6106 netif_tx_disable(tp->dev);
6107}
6108
6109static inline void tg3_netif_start(struct tg3 *tp)
6110{
6111 /* NOTE: unconditional netif_tx_wake_all_queues is only
6112 * appropriate so long as all callers are assured to
6113 * have free tx slots (such as after tg3_init_hw)
6114 */
6115 netif_tx_wake_all_queues(tp->dev);
6116
6117 tg3_napi_enable(tp);
6118 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
6119 tg3_enable_ints(tp);
6120}
6121
David S. Millerf47c11e2005-06-24 20:18:35 -07006122static void tg3_irq_quiesce(struct tg3 *tp)
6123{
Matt Carlson4f125f42009-09-01 12:55:02 +00006124 int i;
6125
David S. Millerf47c11e2005-06-24 20:18:35 -07006126 BUG_ON(tp->irq_sync);
6127
6128 tp->irq_sync = 1;
6129 smp_mb();
6130
Matt Carlson4f125f42009-09-01 12:55:02 +00006131 for (i = 0; i < tp->irq_cnt; i++)
6132 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07006133}
6134
David S. Millerf47c11e2005-06-24 20:18:35 -07006135/* Fully shutdown all tg3 driver activity elsewhere in the system.
6136 * If irq_sync is non-zero, then the IRQ handler must be synchronized
6137 * with as well. Most of the time, this is not necessary except when
6138 * shutting down the device.
6139 */
6140static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
6141{
Michael Chan46966542007-07-11 19:47:19 -07006142 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07006143 if (irq_sync)
6144 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07006145}
6146
6147static inline void tg3_full_unlock(struct tg3 *tp)
6148{
David S. Millerf47c11e2005-06-24 20:18:35 -07006149 spin_unlock_bh(&tp->lock);
6150}
6151
Michael Chanfcfa0a32006-03-20 22:28:41 -08006152/* One-shot MSI handler - Chip automatically disables interrupt
6153 * after sending MSI so driver doesn't have to do it.
6154 */
David Howells7d12e782006-10-05 14:55:46 +01006155static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08006156{
Matt Carlson09943a12009-08-28 14:01:57 +00006157 struct tg3_napi *tnapi = dev_id;
6158 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08006159
Matt Carlson898a56f2009-08-28 14:02:40 +00006160 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006161 if (tnapi->rx_rcb)
6162 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08006163
6164 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00006165 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08006166
6167 return IRQ_HANDLED;
6168}
6169
Michael Chan88b06bc2005-04-21 17:13:25 -07006170/* MSI ISR - No need to check for interrupt sharing and no need to
6171 * flush status block and interrupt mailbox. PCI ordering rules
6172 * guarantee that MSI will arrive after the status block.
6173 */
David Howells7d12e782006-10-05 14:55:46 +01006174static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc2005-04-21 17:13:25 -07006175{
Matt Carlson09943a12009-08-28 14:01:57 +00006176 struct tg3_napi *tnapi = dev_id;
6177 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc2005-04-21 17:13:25 -07006178
Matt Carlson898a56f2009-08-28 14:02:40 +00006179 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006180 if (tnapi->rx_rcb)
6181 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc2005-04-21 17:13:25 -07006182 /*
David S. Millerfac9b832005-05-18 22:46:34 -07006183 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc2005-04-21 17:13:25 -07006184 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07006185 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc2005-04-21 17:13:25 -07006186 * NIC to stop sending us irqs, engaging "in-intr-handler"
6187 * event coalescing.
6188 */
Matt Carlson5b39de92011-08-31 11:44:50 +00006189 tw32_mailbox(tnapi->int_mbox, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07006190 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00006191 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07006192
Michael Chan88b06bc2005-04-21 17:13:25 -07006193 return IRQ_RETVAL(1);
6194}
6195
David Howells7d12e782006-10-05 14:55:46 +01006196static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006197{
Matt Carlson09943a12009-08-28 14:01:57 +00006198 struct tg3_napi *tnapi = dev_id;
6199 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006200 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006201 unsigned int handled = 1;
6202
Linus Torvalds1da177e2005-04-16 15:20:36 -07006203 /* In INTx mode, it is possible for the interrupt to arrive at
6204 * the CPU before the status block posted prior to the interrupt.
6205 * Reading the PCI State register will confirm whether the
6206 * interrupt is ours and will flush the status block.
6207 */
Michael Chand18edcb2007-03-24 20:57:11 -07006208 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
Joe Perches63c3a662011-04-26 08:12:10 +00006209 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07006210 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
6211 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07006212 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07006213 }
Michael Chand18edcb2007-03-24 20:57:11 -07006214 }
6215
6216 /*
6217 * Writing any value to intr-mbox-0 clears PCI INTA# and
6218 * chip-internal interrupt pending events.
6219 * Writing non-zero to intr-mbox-0 additional tells the
6220 * NIC to stop sending us irqs, engaging "in-intr-handler"
6221 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07006222 *
6223 * Flush the mailbox to de-assert the IRQ immediately to prevent
6224 * spurious interrupts. The flush impacts performance but
6225 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07006226 */
Michael Chanc04cb342007-05-07 00:26:15 -07006227 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07006228 if (tg3_irq_sync(tp))
6229 goto out;
6230 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00006231 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00006232 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00006233 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07006234 } else {
6235 /* No work, shared interrupt perhaps? re-enable
6236 * interrupts, and flush that PCI write
6237 */
6238 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
6239 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07006240 }
David S. Millerf47c11e2005-06-24 20:18:35 -07006241out:
David S. Millerfac9b832005-05-18 22:46:34 -07006242 return IRQ_RETVAL(handled);
6243}
6244
David Howells7d12e782006-10-05 14:55:46 +01006245static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07006246{
Matt Carlson09943a12009-08-28 14:01:57 +00006247 struct tg3_napi *tnapi = dev_id;
6248 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006249 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07006250 unsigned int handled = 1;
6251
David S. Millerfac9b832005-05-18 22:46:34 -07006252 /* In INTx mode, it is possible for the interrupt to arrive at
6253 * the CPU before the status block posted prior to the interrupt.
6254 * Reading the PCI State register will confirm whether the
6255 * interrupt is ours and will flush the status block.
6256 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006257 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Joe Perches63c3a662011-04-26 08:12:10 +00006258 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07006259 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
6260 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07006261 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006262 }
Michael Chand18edcb2007-03-24 20:57:11 -07006263 }
6264
6265 /*
6266 * writing any value to intr-mbox-0 clears PCI INTA# and
6267 * chip-internal interrupt pending events.
6268 * writing non-zero to intr-mbox-0 additional tells the
6269 * NIC to stop sending us irqs, engaging "in-intr-handler"
6270 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07006271 *
6272 * Flush the mailbox to de-assert the IRQ immediately to prevent
6273 * spurious interrupts. The flush impacts performance but
6274 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07006275 */
Michael Chanc04cb342007-05-07 00:26:15 -07006276 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00006277
6278 /*
6279 * In a shared interrupt configuration, sometimes other devices'
6280 * interrupts will scream. We record the current status tag here
6281 * so that the above check can report that the screaming interrupts
6282 * are unhandled. Eventually they will be silenced.
6283 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006284 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00006285
Michael Chand18edcb2007-03-24 20:57:11 -07006286 if (tg3_irq_sync(tp))
6287 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00006288
Matt Carlson72334482009-08-28 14:03:01 +00006289 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00006290
Matt Carlson09943a12009-08-28 14:01:57 +00006291 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00006292
David S. Millerf47c11e2005-06-24 20:18:35 -07006293out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006294 return IRQ_RETVAL(handled);
6295}
6296
Michael Chan79381092005-04-21 17:13:59 -07006297/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01006298static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07006299{
Matt Carlson09943a12009-08-28 14:01:57 +00006300 struct tg3_napi *tnapi = dev_id;
6301 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006302 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07006303
Michael Chanf9804dd2005-09-27 12:13:10 -07006304 if ((sblk->status & SD_STATUS_UPDATED) ||
6305 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07006306 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07006307 return IRQ_RETVAL(1);
6308 }
6309 return IRQ_RETVAL(0);
6310}
6311
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07006312static int tg3_init_hw(struct tg3 *, int);
Michael Chan944d9802005-05-29 14:57:48 -07006313static int tg3_halt(struct tg3 *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006314
Michael Chanb9ec6c12006-07-25 16:37:27 -07006315/* Restart hardware after configuration changes, self-test, etc.
6316 * Invoked with tp->lock held.
6317 */
6318static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
Eric Dumazet78c61462008-04-24 23:33:06 -07006319 __releases(tp->lock)
6320 __acquires(tp->lock)
Michael Chanb9ec6c12006-07-25 16:37:27 -07006321{
6322 int err;
6323
6324 err = tg3_init_hw(tp, reset_phy);
6325 if (err) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006326 netdev_err(tp->dev,
6327 "Failed to re-initialize device, aborting\n");
Michael Chanb9ec6c12006-07-25 16:37:27 -07006328 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
6329 tg3_full_unlock(tp);
6330 del_timer_sync(&tp->timer);
6331 tp->irq_sync = 0;
Matt Carlsonfed97812009-09-01 13:10:19 +00006332 tg3_napi_enable(tp);
Michael Chanb9ec6c12006-07-25 16:37:27 -07006333 dev_close(tp->dev);
6334 tg3_full_lock(tp, 0);
6335 }
6336 return err;
6337}
6338
Linus Torvalds1da177e2005-04-16 15:20:36 -07006339#ifdef CONFIG_NET_POLL_CONTROLLER
6340static void tg3_poll_controller(struct net_device *dev)
6341{
Matt Carlson4f125f42009-09-01 12:55:02 +00006342 int i;
Michael Chan88b06bc2005-04-21 17:13:25 -07006343 struct tg3 *tp = netdev_priv(dev);
6344
Matt Carlson4f125f42009-09-01 12:55:02 +00006345 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00006346 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006347}
6348#endif
6349
David Howellsc4028952006-11-22 14:57:56 +00006350static void tg3_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006351{
David Howellsc4028952006-11-22 14:57:56 +00006352 struct tg3 *tp = container_of(work, struct tg3, reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006353 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006354
Michael Chan7faa0062006-02-02 17:29:28 -08006355 tg3_full_lock(tp, 0);
Michael Chan7faa0062006-02-02 17:29:28 -08006356
6357 if (!netif_running(tp->dev)) {
Matt Carlsondb219972011-11-04 09:15:03 +00006358 tg3_flag_clear(tp, RESET_TASK_PENDING);
Michael Chan7faa0062006-02-02 17:29:28 -08006359 tg3_full_unlock(tp);
6360 return;
6361 }
6362
6363 tg3_full_unlock(tp);
6364
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006365 tg3_phy_stop(tp);
6366
Linus Torvalds1da177e2005-04-16 15:20:36 -07006367 tg3_netif_stop(tp);
6368
David S. Millerf47c11e2005-06-24 20:18:35 -07006369 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006370
Joe Perches63c3a662011-04-26 08:12:10 +00006371 if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
Michael Chandf3e6542006-05-26 17:48:07 -07006372 tp->write32_tx_mbox = tg3_write32_tx_mbox;
6373 tp->write32_rx_mbox = tg3_write_flush_reg32;
Joe Perches63c3a662011-04-26 08:12:10 +00006374 tg3_flag_set(tp, MBOX_WRITE_REORDER);
6375 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07006376 }
6377
Michael Chan944d9802005-05-29 14:57:48 -07006378 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006379 err = tg3_init_hw(tp, 1);
6380 if (err)
Michael Chanb9ec6c12006-07-25 16:37:27 -07006381 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006382
6383 tg3_netif_start(tp);
6384
Michael Chanb9ec6c12006-07-25 16:37:27 -07006385out:
Michael Chan7faa0062006-02-02 17:29:28 -08006386 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006387
6388 if (!err)
6389 tg3_phy_start(tp);
Matt Carlsondb219972011-11-04 09:15:03 +00006390
6391 tg3_flag_clear(tp, RESET_TASK_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006392}
6393
6394static void tg3_tx_timeout(struct net_device *dev)
6395{
6396 struct tg3 *tp = netdev_priv(dev);
6397
Michael Chanb0408752007-02-13 12:18:30 -08006398 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00006399 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00006400 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08006401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006402
Matt Carlsondb219972011-11-04 09:15:03 +00006403 tg3_reset_task_schedule(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006404}
6405
Michael Chanc58ec932005-09-17 00:46:27 -07006406/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
6407static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
6408{
6409 u32 base = (u32) mapping & 0xffffffff;
6410
Eric Dumazet807540b2010-09-23 05:40:09 +00006411 return (base > 0xffffdcc0) && (base + len + 8 < base);
Michael Chanc58ec932005-09-17 00:46:27 -07006412}
6413
Michael Chan72f2afb2006-03-06 19:28:35 -08006414/* Test for DMA addresses > 40-bit */
6415static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
6416 int len)
6417{
6418#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Joe Perches63c3a662011-04-26 08:12:10 +00006419 if (tg3_flag(tp, 40BIT_DMA_BUG))
Eric Dumazet807540b2010-09-23 05:40:09 +00006420 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08006421 return 0;
6422#else
6423 return 0;
6424#endif
6425}
6426
Matt Carlsond1a3b732011-07-27 14:20:51 +00006427static inline void tg3_tx_set_bd(struct tg3_tx_buffer_desc *txbd,
Matt Carlson92cd3a12011-07-27 14:20:47 +00006428 dma_addr_t mapping, u32 len, u32 flags,
6429 u32 mss, u32 vlan)
Matt Carlson2ffcc982011-05-19 12:12:44 +00006430{
Matt Carlson92cd3a12011-07-27 14:20:47 +00006431 txbd->addr_hi = ((u64) mapping >> 32);
6432 txbd->addr_lo = ((u64) mapping & 0xffffffff);
6433 txbd->len_flags = (len << TXD_LEN_SHIFT) | (flags & 0x0000ffff);
6434 txbd->vlan_tag = (mss << TXD_MSS_SHIFT) | (vlan << TXD_VLAN_TAG_SHIFT);
Matt Carlson2ffcc982011-05-19 12:12:44 +00006435}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006436
Matt Carlson84b67b22011-07-27 14:20:52 +00006437static bool tg3_tx_frag_set(struct tg3_napi *tnapi, u32 *entry, u32 *budget,
Matt Carlsond1a3b732011-07-27 14:20:51 +00006438 dma_addr_t map, u32 len, u32 flags,
6439 u32 mss, u32 vlan)
6440{
6441 struct tg3 *tp = tnapi->tp;
6442 bool hwbug = false;
6443
6444 if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
6445 hwbug = 1;
6446
6447 if (tg3_4g_overflow_test(map, len))
6448 hwbug = 1;
6449
6450 if (tg3_40bit_overflow_test(tp, map, len))
6451 hwbug = 1;
6452
Matt Carlsona4cb4282011-12-14 11:09:58 +00006453 if (tp->dma_limit) {
Matt Carlsonb9e45482011-11-04 09:14:59 +00006454 u32 prvidx = *entry;
Matt Carlsone31aa982011-07-27 14:20:53 +00006455 u32 tmp_flag = flags & ~TXD_FLAG_END;
Matt Carlsona4cb4282011-12-14 11:09:58 +00006456 while (len > tp->dma_limit && *budget) {
6457 u32 frag_len = tp->dma_limit;
6458 len -= tp->dma_limit;
Matt Carlsone31aa982011-07-27 14:20:53 +00006459
Matt Carlsonb9e45482011-11-04 09:14:59 +00006460 /* Avoid the 8byte DMA problem */
6461 if (len <= 8) {
Matt Carlsona4cb4282011-12-14 11:09:58 +00006462 len += tp->dma_limit / 2;
6463 frag_len = tp->dma_limit / 2;
Matt Carlsone31aa982011-07-27 14:20:53 +00006464 }
6465
Matt Carlsonb9e45482011-11-04 09:14:59 +00006466 tnapi->tx_buffers[*entry].fragmented = true;
6467
6468 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6469 frag_len, tmp_flag, mss, vlan);
6470 *budget -= 1;
6471 prvidx = *entry;
6472 *entry = NEXT_TX(*entry);
6473
Matt Carlsone31aa982011-07-27 14:20:53 +00006474 map += frag_len;
6475 }
6476
6477 if (len) {
6478 if (*budget) {
6479 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6480 len, flags, mss, vlan);
Matt Carlsonb9e45482011-11-04 09:14:59 +00006481 *budget -= 1;
Matt Carlsone31aa982011-07-27 14:20:53 +00006482 *entry = NEXT_TX(*entry);
6483 } else {
6484 hwbug = 1;
Matt Carlsonb9e45482011-11-04 09:14:59 +00006485 tnapi->tx_buffers[prvidx].fragmented = false;
Matt Carlsone31aa982011-07-27 14:20:53 +00006486 }
6487 }
6488 } else {
Matt Carlson84b67b22011-07-27 14:20:52 +00006489 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6490 len, flags, mss, vlan);
Matt Carlsone31aa982011-07-27 14:20:53 +00006491 *entry = NEXT_TX(*entry);
6492 }
Matt Carlsond1a3b732011-07-27 14:20:51 +00006493
6494 return hwbug;
6495}
6496
Matt Carlson0d681b22011-07-27 14:20:49 +00006497static void tg3_tx_skb_unmap(struct tg3_napi *tnapi, u32 entry, int last)
Matt Carlson432aa7e2011-05-19 12:12:45 +00006498{
6499 int i;
Matt Carlson0d681b22011-07-27 14:20:49 +00006500 struct sk_buff *skb;
Matt Carlsondf8944c2011-07-27 14:20:46 +00006501 struct tg3_tx_ring_info *txb = &tnapi->tx_buffers[entry];
Matt Carlson432aa7e2011-05-19 12:12:45 +00006502
Matt Carlson0d681b22011-07-27 14:20:49 +00006503 skb = txb->skb;
6504 txb->skb = NULL;
6505
Matt Carlson432aa7e2011-05-19 12:12:45 +00006506 pci_unmap_single(tnapi->tp->pdev,
6507 dma_unmap_addr(txb, mapping),
6508 skb_headlen(skb),
6509 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00006510
6511 while (txb->fragmented) {
6512 txb->fragmented = false;
6513 entry = NEXT_TX(entry);
6514 txb = &tnapi->tx_buffers[entry];
6515 }
6516
Matt Carlsonba1142e2011-11-04 09:15:00 +00006517 for (i = 0; i <= last; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00006518 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Matt Carlson432aa7e2011-05-19 12:12:45 +00006519
6520 entry = NEXT_TX(entry);
6521 txb = &tnapi->tx_buffers[entry];
6522
6523 pci_unmap_page(tnapi->tp->pdev,
6524 dma_unmap_addr(txb, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00006525 skb_frag_size(frag), PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00006526
6527 while (txb->fragmented) {
6528 txb->fragmented = false;
6529 entry = NEXT_TX(entry);
6530 txb = &tnapi->tx_buffers[entry];
6531 }
Matt Carlson432aa7e2011-05-19 12:12:45 +00006532 }
6533}
6534
Michael Chan72f2afb2006-03-06 19:28:35 -08006535/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006536static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
David S. Miller1805b2f2011-10-24 18:18:09 -04006537 struct sk_buff **pskb,
Matt Carlson84b67b22011-07-27 14:20:52 +00006538 u32 *entry, u32 *budget,
Matt Carlson92cd3a12011-07-27 14:20:47 +00006539 u32 base_flags, u32 mss, u32 vlan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006540{
Matt Carlson24f4efd2009-11-13 13:03:35 +00006541 struct tg3 *tp = tnapi->tp;
David S. Miller1805b2f2011-10-24 18:18:09 -04006542 struct sk_buff *new_skb, *skb = *pskb;
Michael Chanc58ec932005-09-17 00:46:27 -07006543 dma_addr_t new_addr = 0;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006544 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006545
Matt Carlson41588ba2008-04-19 18:12:33 -07006546 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
6547 new_skb = skb_copy(skb, GFP_ATOMIC);
6548 else {
6549 int more_headroom = 4 - ((unsigned long)skb->data & 3);
6550
6551 new_skb = skb_copy_expand(skb,
6552 skb_headroom(skb) + more_headroom,
6553 skb_tailroom(skb), GFP_ATOMIC);
6554 }
6555
Linus Torvalds1da177e2005-04-16 15:20:36 -07006556 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07006557 ret = -1;
6558 } else {
6559 /* New SKB is guaranteed to be linear. */
Alexander Duyckf4188d82009-12-02 16:48:38 +00006560 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
6561 PCI_DMA_TODEVICE);
6562 /* Make sure the mapping succeeded */
6563 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00006564 dev_kfree_skb(new_skb);
Michael Chanc58ec932005-09-17 00:46:27 -07006565 ret = -1;
Michael Chanc58ec932005-09-17 00:46:27 -07006566 } else {
Matt Carlsonb9e45482011-11-04 09:14:59 +00006567 u32 save_entry = *entry;
6568
Matt Carlson92cd3a12011-07-27 14:20:47 +00006569 base_flags |= TXD_FLAG_END;
6570
Matt Carlson84b67b22011-07-27 14:20:52 +00006571 tnapi->tx_buffers[*entry].skb = new_skb;
6572 dma_unmap_addr_set(&tnapi->tx_buffers[*entry],
Matt Carlson432aa7e2011-05-19 12:12:45 +00006573 mapping, new_addr);
6574
Matt Carlson84b67b22011-07-27 14:20:52 +00006575 if (tg3_tx_frag_set(tnapi, entry, budget, new_addr,
Matt Carlsond1a3b732011-07-27 14:20:51 +00006576 new_skb->len, base_flags,
6577 mss, vlan)) {
Matt Carlsonba1142e2011-11-04 09:15:00 +00006578 tg3_tx_skb_unmap(tnapi, save_entry, -1);
Matt Carlsond1a3b732011-07-27 14:20:51 +00006579 dev_kfree_skb(new_skb);
6580 ret = -1;
6581 }
Michael Chanc58ec932005-09-17 00:46:27 -07006582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006583 }
6584
Linus Torvalds1da177e2005-04-16 15:20:36 -07006585 dev_kfree_skb(skb);
David S. Miller1805b2f2011-10-24 18:18:09 -04006586 *pskb = new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07006587 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006588}
6589
Matt Carlson2ffcc982011-05-19 12:12:44 +00006590static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07006591
6592/* Use GSO to workaround a rare TSO bug that may be triggered when the
6593 * TSO header is greater than 80 bytes.
6594 */
6595static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
6596{
6597 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006598 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07006599
6600 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006601 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07006602 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006603
6604 /* netif_tx_stop_queue() must be done before checking
6605 * checking tx index in tg3_tx_avail() below, because in
6606 * tg3_tx(), we update tx index before checking for
6607 * netif_tx_queue_stopped().
6608 */
6609 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006610 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08006611 return NETDEV_TX_BUSY;
6612
6613 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07006614 }
6615
6616 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07006617 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07006618 goto tg3_tso_bug_end;
6619
6620 do {
6621 nskb = segs;
6622 segs = segs->next;
6623 nskb->next = NULL;
Matt Carlson2ffcc982011-05-19 12:12:44 +00006624 tg3_start_xmit(nskb, tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07006625 } while (segs);
6626
6627tg3_tso_bug_end:
6628 dev_kfree_skb(skb);
6629
6630 return NETDEV_TX_OK;
6631}
Michael Chan52c0fd82006-06-29 20:15:54 -07006632
Michael Chan5a6f3072006-03-20 22:28:05 -08006633/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
Joe Perches63c3a662011-04-26 08:12:10 +00006634 * support TG3_FLAG_HW_TSO_1 or firmware TSO only.
Michael Chan5a6f3072006-03-20 22:28:05 -08006635 */
Matt Carlson2ffcc982011-05-19 12:12:44 +00006636static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08006637{
6638 struct tg3 *tp = netdev_priv(dev);
Matt Carlson92cd3a12011-07-27 14:20:47 +00006639 u32 len, entry, base_flags, mss, vlan = 0;
Matt Carlson84b67b22011-07-27 14:20:52 +00006640 u32 budget;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006641 int i = -1, would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07006642 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00006643 struct tg3_napi *tnapi;
6644 struct netdev_queue *txq;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006645 unsigned int last;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006646
Matt Carlson24f4efd2009-11-13 13:03:35 +00006647 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
6648 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Joe Perches63c3a662011-04-26 08:12:10 +00006649 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006650 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006651
Matt Carlson84b67b22011-07-27 14:20:52 +00006652 budget = tg3_tx_avail(tnapi);
6653
Michael Chan00b70502006-06-17 21:58:45 -07006654 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006655 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07006656 * interrupt. Furthermore, IRQ processing runs lockless so we have
6657 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07006658 */
Matt Carlson84b67b22011-07-27 14:20:52 +00006659 if (unlikely(budget <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006660 if (!netif_tx_queue_stopped(txq)) {
6661 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08006662
6663 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00006664 netdev_err(dev,
6665 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08006666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006667 return NETDEV_TX_BUSY;
6668 }
6669
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006670 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006671 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07006672 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006673 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00006674
Matt Carlsonbe98da62010-07-11 09:31:46 +00006675 mss = skb_shinfo(skb)->gso_size;
6676 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006677 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00006678 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006679
6680 if (skb_header_cloned(skb) &&
Eric Dumazet48855432011-10-24 07:53:03 +00006681 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
6682 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006683
Matt Carlson34195c32010-07-11 09:31:42 +00006684 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07006685 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006686
Matt Carlson02e96082010-09-15 08:59:59 +00006687 if (skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00006688 hdr_len = skb_headlen(skb) - ETH_HLEN;
6689 } else {
6690 u32 ip_tcp_len;
6691
6692 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
6693 hdr_len = ip_tcp_len + tcp_opt_len;
6694
6695 iph->check = 0;
6696 iph->tot_len = htons(mss + hdr_len);
6697 }
6698
Michael Chan52c0fd82006-06-29 20:15:54 -07006699 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Joe Perches63c3a662011-04-26 08:12:10 +00006700 tg3_flag(tp, TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00006701 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07006702
Linus Torvalds1da177e2005-04-16 15:20:36 -07006703 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
6704 TXD_FLAG_CPU_POST_DMA);
6705
Joe Perches63c3a662011-04-26 08:12:10 +00006706 if (tg3_flag(tp, HW_TSO_1) ||
6707 tg3_flag(tp, HW_TSO_2) ||
6708 tg3_flag(tp, HW_TSO_3)) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006709 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006710 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006711 } else
6712 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
6713 iph->daddr, 0,
6714 IPPROTO_TCP,
6715 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006716
Joe Perches63c3a662011-04-26 08:12:10 +00006717 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlson615774f2009-11-13 13:03:39 +00006718 mss |= (hdr_len & 0xc) << 12;
6719 if (hdr_len & 0x10)
6720 base_flags |= 0x00000010;
6721 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +00006722 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006723 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +00006724 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006725 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006726 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006727 int tsflags;
6728
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006729 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006730 mss |= (tsflags << 11);
6731 }
6732 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006733 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006734 int tsflags;
6735
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006736 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006737 base_flags |= tsflags << 12;
6738 }
6739 }
6740 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00006741
Matt Carlson93a700a2011-08-31 11:44:54 +00006742 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
6743 !mss && skb->len > VLAN_ETH_FRAME_LEN)
6744 base_flags |= TXD_FLAG_JMB_PKT;
6745
Matt Carlson92cd3a12011-07-27 14:20:47 +00006746 if (vlan_tx_tag_present(skb)) {
6747 base_flags |= TXD_FLAG_VLAN;
6748 vlan = vlan_tx_tag_get(skb);
6749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006750
Alexander Duyckf4188d82009-12-02 16:48:38 +00006751 len = skb_headlen(skb);
6752
6753 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
Eric Dumazet48855432011-10-24 07:53:03 +00006754 if (pci_dma_mapping_error(tp->pdev, mapping))
6755 goto drop;
6756
David S. Miller90079ce2008-09-11 04:52:51 -07006757
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006758 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006759 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006760
6761 would_hit_hwbug = 0;
6762
Joe Perches63c3a662011-04-26 08:12:10 +00006763 if (tg3_flag(tp, 5701_DMA_BUG))
Michael Chanc58ec932005-09-17 00:46:27 -07006764 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006765
Matt Carlson84b67b22011-07-27 14:20:52 +00006766 if (tg3_tx_frag_set(tnapi, &entry, &budget, mapping, len, base_flags |
Matt Carlsond1a3b732011-07-27 14:20:51 +00006767 ((skb_shinfo(skb)->nr_frags == 0) ? TXD_FLAG_END : 0),
Matt Carlsonba1142e2011-11-04 09:15:00 +00006768 mss, vlan)) {
Matt Carlsond1a3b732011-07-27 14:20:51 +00006769 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006770 /* Now loop through additional data fragments, and queue them. */
Matt Carlsonba1142e2011-11-04 09:15:00 +00006771 } else if (skb_shinfo(skb)->nr_frags > 0) {
Matt Carlson92cd3a12011-07-27 14:20:47 +00006772 u32 tmp_mss = mss;
6773
6774 if (!tg3_flag(tp, HW_TSO_1) &&
6775 !tg3_flag(tp, HW_TSO_2) &&
6776 !tg3_flag(tp, HW_TSO_3))
6777 tmp_mss = 0;
6778
Linus Torvalds1da177e2005-04-16 15:20:36 -07006779 last = skb_shinfo(skb)->nr_frags - 1;
6780 for (i = 0; i <= last; i++) {
6781 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6782
Eric Dumazet9e903e02011-10-18 21:00:24 +00006783 len = skb_frag_size(frag);
Ian Campbelldc234d02011-08-24 22:28:11 +00006784 mapping = skb_frag_dma_map(&tp->pdev->dev, frag, 0,
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01006785 len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006786
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006787 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006788 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00006789 mapping);
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01006790 if (dma_mapping_error(&tp->pdev->dev, mapping))
Alexander Duyckf4188d82009-12-02 16:48:38 +00006791 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006792
Matt Carlsonb9e45482011-11-04 09:14:59 +00006793 if (!budget ||
6794 tg3_tx_frag_set(tnapi, &entry, &budget, mapping,
Matt Carlson84b67b22011-07-27 14:20:52 +00006795 len, base_flags |
6796 ((i == last) ? TXD_FLAG_END : 0),
Matt Carlsonb9e45482011-11-04 09:14:59 +00006797 tmp_mss, vlan)) {
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006798 would_hit_hwbug = 1;
Matt Carlsonb9e45482011-11-04 09:14:59 +00006799 break;
6800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006801 }
6802 }
6803
6804 if (would_hit_hwbug) {
Matt Carlson0d681b22011-07-27 14:20:49 +00006805 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006806
6807 /* If the workaround fails due to memory/mapping
6808 * failure, silently drop this packet.
6809 */
Matt Carlson84b67b22011-07-27 14:20:52 +00006810 entry = tnapi->tx_prod;
6811 budget = tg3_tx_avail(tnapi);
David S. Miller1805b2f2011-10-24 18:18:09 -04006812 if (tigon3_dma_hwbug_workaround(tnapi, &skb, &entry, &budget,
Matt Carlson84b67b22011-07-27 14:20:52 +00006813 base_flags, mss, vlan))
Eric Dumazet48855432011-10-24 07:53:03 +00006814 goto drop_nofree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006815 }
6816
Richard Cochrand515b452011-06-19 03:31:41 +00006817 skb_tx_timestamp(skb);
Tom Herbert298376d2011-11-28 16:33:30 +00006818 netdev_sent_queue(tp->dev, skb->len);
Richard Cochrand515b452011-06-19 03:31:41 +00006819
Linus Torvalds1da177e2005-04-16 15:20:36 -07006820 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006821 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006822
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006823 tnapi->tx_prod = entry;
6824 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006825 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006826
6827 /* netif_tx_stop_queue() must be done before checking
6828 * checking tx index in tg3_tx_avail() below, because in
6829 * tg3_tx(), we update tx index before checking for
6830 * netif_tx_queue_stopped().
6831 */
6832 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006833 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006834 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07006835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006836
Eric Dumazetcdd0db02009-05-28 00:00:41 +00006837 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006838 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006839
6840dma_error:
Matt Carlsonba1142e2011-11-04 09:15:00 +00006841 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, --i);
Matt Carlson432aa7e2011-05-19 12:12:45 +00006842 tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
Eric Dumazet48855432011-10-24 07:53:03 +00006843drop:
6844 dev_kfree_skb(skb);
6845drop_nofree:
6846 tp->tx_dropped++;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006847 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006848}
6849
Matt Carlson6e01b202011-08-19 13:58:20 +00006850static void tg3_mac_loopback(struct tg3 *tp, bool enable)
6851{
6852 if (enable) {
6853 tp->mac_mode &= ~(MAC_MODE_HALF_DUPLEX |
6854 MAC_MODE_PORT_MODE_MASK);
6855
6856 tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
6857
6858 if (!tg3_flag(tp, 5705_PLUS))
6859 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
6860
6861 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
6862 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
6863 else
6864 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
6865 } else {
6866 tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
6867
6868 if (tg3_flag(tp, 5705_PLUS) ||
6869 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) ||
6870 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
6871 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
6872 }
6873
6874 tw32(MAC_MODE, tp->mac_mode);
6875 udelay(40);
6876}
6877
Matt Carlson941ec902011-08-19 13:58:23 +00006878static int tg3_phy_lpbk_set(struct tg3 *tp, u32 speed, bool extlpbk)
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006879{
Matt Carlson941ec902011-08-19 13:58:23 +00006880 u32 val, bmcr, mac_mode, ptest = 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006881
6882 tg3_phy_toggle_apd(tp, false);
6883 tg3_phy_toggle_automdix(tp, 0);
6884
Matt Carlson941ec902011-08-19 13:58:23 +00006885 if (extlpbk && tg3_phy_set_extloopbk(tp))
6886 return -EIO;
6887
6888 bmcr = BMCR_FULLDPLX;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006889 switch (speed) {
6890 case SPEED_10:
6891 break;
6892 case SPEED_100:
6893 bmcr |= BMCR_SPEED100;
6894 break;
6895 case SPEED_1000:
6896 default:
6897 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
6898 speed = SPEED_100;
6899 bmcr |= BMCR_SPEED100;
6900 } else {
6901 speed = SPEED_1000;
6902 bmcr |= BMCR_SPEED1000;
6903 }
6904 }
6905
Matt Carlson941ec902011-08-19 13:58:23 +00006906 if (extlpbk) {
6907 if (!(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
6908 tg3_readphy(tp, MII_CTRL1000, &val);
6909 val |= CTL1000_AS_MASTER |
6910 CTL1000_ENABLE_MASTER;
6911 tg3_writephy(tp, MII_CTRL1000, val);
6912 } else {
6913 ptest = MII_TG3_FET_PTEST_TRIM_SEL |
6914 MII_TG3_FET_PTEST_TRIM_2;
6915 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest);
6916 }
6917 } else
6918 bmcr |= BMCR_LOOPBACK;
6919
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006920 tg3_writephy(tp, MII_BMCR, bmcr);
6921
6922 /* The write needs to be flushed for the FETs */
6923 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
6924 tg3_readphy(tp, MII_BMCR, &bmcr);
6925
6926 udelay(40);
6927
6928 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
6929 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Matt Carlson941ec902011-08-19 13:58:23 +00006930 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest |
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006931 MII_TG3_FET_PTEST_FRC_TX_LINK |
6932 MII_TG3_FET_PTEST_FRC_TX_LOCK);
6933
6934 /* The write needs to be flushed for the AC131 */
6935 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
6936 }
6937
6938 /* Reset to prevent losing 1st rx packet intermittently */
6939 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
6940 tg3_flag(tp, 5780_CLASS)) {
6941 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
6942 udelay(10);
6943 tw32_f(MAC_RX_MODE, tp->rx_mode);
6944 }
6945
6946 mac_mode = tp->mac_mode &
6947 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
6948 if (speed == SPEED_1000)
6949 mac_mode |= MAC_MODE_PORT_MODE_GMII;
6950 else
6951 mac_mode |= MAC_MODE_PORT_MODE_MII;
6952
6953 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
6954 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
6955
6956 if (masked_phy_id == TG3_PHY_ID_BCM5401)
6957 mac_mode &= ~MAC_MODE_LINK_POLARITY;
6958 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
6959 mac_mode |= MAC_MODE_LINK_POLARITY;
6960
6961 tg3_writephy(tp, MII_TG3_EXT_CTRL,
6962 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
6963 }
6964
6965 tw32(MAC_MODE, mac_mode);
6966 udelay(40);
Matt Carlson941ec902011-08-19 13:58:23 +00006967
6968 return 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006969}
6970
Michał Mirosławc8f44af2011-11-15 15:29:55 +00006971static void tg3_set_loopback(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006972{
6973 struct tg3 *tp = netdev_priv(dev);
6974
6975 if (features & NETIF_F_LOOPBACK) {
6976 if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
6977 return;
6978
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006979 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00006980 tg3_mac_loopback(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006981 netif_carrier_on(tp->dev);
6982 spin_unlock_bh(&tp->lock);
6983 netdev_info(dev, "Internal MAC loopback mode enabled.\n");
6984 } else {
6985 if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
6986 return;
6987
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006988 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00006989 tg3_mac_loopback(tp, false);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006990 /* Force link status check */
6991 tg3_setup_phy(tp, 1);
6992 spin_unlock_bh(&tp->lock);
6993 netdev_info(dev, "Internal MAC loopback mode disabled.\n");
6994 }
6995}
6996
Michał Mirosławc8f44af2011-11-15 15:29:55 +00006997static netdev_features_t tg3_fix_features(struct net_device *dev,
6998 netdev_features_t features)
Michał Mirosławdc668912011-04-07 03:35:07 +00006999{
7000 struct tg3 *tp = netdev_priv(dev);
7001
Joe Perches63c3a662011-04-26 08:12:10 +00007002 if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
Michał Mirosławdc668912011-04-07 03:35:07 +00007003 features &= ~NETIF_F_ALL_TSO;
7004
7005 return features;
7006}
7007
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007008static int tg3_set_features(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007009{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007010 netdev_features_t changed = dev->features ^ features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007011
7012 if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
7013 tg3_set_loopback(dev, features);
7014
7015 return 0;
7016}
7017
Linus Torvalds1da177e2005-04-16 15:20:36 -07007018static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
7019 int new_mtu)
7020{
7021 dev->mtu = new_mtu;
7022
Michael Chanef7f5ec2005-07-25 12:32:25 -07007023 if (new_mtu > ETH_DATA_LEN) {
Joe Perches63c3a662011-04-26 08:12:10 +00007024 if (tg3_flag(tp, 5780_CLASS)) {
Michał Mirosławdc668912011-04-07 03:35:07 +00007025 netdev_update_features(dev);
Joe Perches63c3a662011-04-26 08:12:10 +00007026 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson859a5882010-04-05 10:19:28 +00007027 } else {
Joe Perches63c3a662011-04-26 08:12:10 +00007028 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Matt Carlson859a5882010-04-05 10:19:28 +00007029 }
Michael Chanef7f5ec2005-07-25 12:32:25 -07007030 } else {
Joe Perches63c3a662011-04-26 08:12:10 +00007031 if (tg3_flag(tp, 5780_CLASS)) {
7032 tg3_flag_set(tp, TSO_CAPABLE);
Michał Mirosławdc668912011-04-07 03:35:07 +00007033 netdev_update_features(dev);
7034 }
Joe Perches63c3a662011-04-26 08:12:10 +00007035 tg3_flag_clear(tp, JUMBO_RING_ENABLE);
Michael Chanef7f5ec2005-07-25 12:32:25 -07007036 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007037}
7038
7039static int tg3_change_mtu(struct net_device *dev, int new_mtu)
7040{
7041 struct tg3 *tp = netdev_priv(dev);
Michael Chanb9ec6c12006-07-25 16:37:27 -07007042 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007043
7044 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
7045 return -EINVAL;
7046
7047 if (!netif_running(dev)) {
7048 /* We'll just catch it later when the
7049 * device is up'd.
7050 */
7051 tg3_set_mtu(dev, tp, new_mtu);
7052 return 0;
7053 }
7054
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07007055 tg3_phy_stop(tp);
7056
Linus Torvalds1da177e2005-04-16 15:20:36 -07007057 tg3_netif_stop(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07007058
7059 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007060
Michael Chan944d9802005-05-29 14:57:48 -07007061 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007062
7063 tg3_set_mtu(dev, tp, new_mtu);
7064
Michael Chanb9ec6c12006-07-25 16:37:27 -07007065 err = tg3_restart_hw(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007066
Michael Chanb9ec6c12006-07-25 16:37:27 -07007067 if (!err)
7068 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007069
David S. Millerf47c11e2005-06-24 20:18:35 -07007070 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007071
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07007072 if (!err)
7073 tg3_phy_start(tp);
7074
Michael Chanb9ec6c12006-07-25 16:37:27 -07007075 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007076}
7077
Matt Carlson21f581a2009-08-28 14:00:25 +00007078static void tg3_rx_prodring_free(struct tg3 *tp,
7079 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007080{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007081 int i;
7082
Matt Carlson8fea32b2010-09-15 08:59:58 +00007083 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007084 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00007085 i = (i + 1) & tp->rx_std_ring_mask)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007086 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007087 tp->rx_pkt_map_sz);
7088
Joe Perches63c3a662011-04-26 08:12:10 +00007089 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007090 for (i = tpr->rx_jmb_cons_idx;
7091 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00007092 i = (i + 1) & tp->rx_jmb_ring_mask) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007093 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007094 TG3_RX_JMB_MAP_SZ);
7095 }
7096 }
7097
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007098 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007099 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007100
Matt Carlson2c49a442010-09-30 10:34:35 +00007101 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007102 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007103 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007104
Joe Perches63c3a662011-04-26 08:12:10 +00007105 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007106 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007107 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007108 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007109 }
7110}
7111
Matt Carlsonc6cdf432010-04-05 10:19:26 +00007112/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007113 *
7114 * The chip has been shut down and the driver detached from
7115 * the networking, so no interrupts or new tx packets will
7116 * end up in the driver. tp->{tx,}lock are held and thus
7117 * we may not sleep.
7118 */
Matt Carlson21f581a2009-08-28 14:00:25 +00007119static int tg3_rx_prodring_alloc(struct tg3 *tp,
7120 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007121{
Matt Carlson287be122009-08-28 13:58:46 +00007122 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007123
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007124 tpr->rx_std_cons_idx = 0;
7125 tpr->rx_std_prod_idx = 0;
7126 tpr->rx_jmb_cons_idx = 0;
7127 tpr->rx_jmb_prod_idx = 0;
7128
Matt Carlson8fea32b2010-09-15 08:59:58 +00007129 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007130 memset(&tpr->rx_std_buffers[0], 0,
7131 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00007132 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007133 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00007134 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007135 goto done;
7136 }
7137
Linus Torvalds1da177e2005-04-16 15:20:36 -07007138 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00007139 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007140
Matt Carlson287be122009-08-28 13:58:46 +00007141 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00007142 if (tg3_flag(tp, 5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00007143 tp->dev->mtu > ETH_DATA_LEN)
7144 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
7145 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07007146
Linus Torvalds1da177e2005-04-16 15:20:36 -07007147 /* Initialize invariants of the rings, we only set this
7148 * stuff once. This works because the card does not
7149 * write into the rx buffer posting rings.
7150 */
Matt Carlson2c49a442010-09-30 10:34:35 +00007151 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007152 struct tg3_rx_buffer_desc *rxd;
7153
Matt Carlson21f581a2009-08-28 14:00:25 +00007154 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00007155 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007156 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
7157 rxd->opaque = (RXD_OPAQUE_RING_STD |
7158 (i << RXD_OPAQUE_INDEX_SHIFT));
7159 }
7160
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007161 /* Now allocate fresh SKBs for each rx ring. */
7162 for (i = 0; i < tp->rx_pending; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007163 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_STD, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007164 netdev_warn(tp->dev,
7165 "Using a smaller RX standard ring. Only "
7166 "%d out of %d buffers were allocated "
7167 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007168 if (i == 0)
7169 goto initfail;
7170 tp->rx_pending = i;
7171 break;
7172 }
7173 }
7174
Joe Perches63c3a662011-04-26 08:12:10 +00007175 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007176 goto done;
7177
Matt Carlson2c49a442010-09-30 10:34:35 +00007178 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007179
Joe Perches63c3a662011-04-26 08:12:10 +00007180 if (!tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson0d86df82010-02-17 15:17:00 +00007181 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007182
Matt Carlson2c49a442010-09-30 10:34:35 +00007183 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00007184 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007185
Matt Carlson0d86df82010-02-17 15:17:00 +00007186 rxd = &tpr->rx_jmb[i].std;
7187 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
7188 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
7189 RXD_FLAG_JUMBO;
7190 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
7191 (i << RXD_OPAQUE_INDEX_SHIFT));
7192 }
7193
7194 for (i = 0; i < tp->rx_jumbo_pending; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007195 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_JUMBO, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007196 netdev_warn(tp->dev,
7197 "Using a smaller RX jumbo ring. Only %d "
7198 "out of %d buffers were allocated "
7199 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00007200 if (i == 0)
7201 goto initfail;
7202 tp->rx_jumbo_pending = i;
7203 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007204 }
7205 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007206
7207done:
Michael Chan32d8c572006-07-25 16:38:29 -07007208 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007209
7210initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00007211 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007212 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007213}
7214
Matt Carlson21f581a2009-08-28 14:00:25 +00007215static void tg3_rx_prodring_fini(struct tg3 *tp,
7216 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007217{
Matt Carlson21f581a2009-08-28 14:00:25 +00007218 kfree(tpr->rx_std_buffers);
7219 tpr->rx_std_buffers = NULL;
7220 kfree(tpr->rx_jmb_buffers);
7221 tpr->rx_jmb_buffers = NULL;
7222 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007223 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
7224 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00007225 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007226 }
Matt Carlson21f581a2009-08-28 14:00:25 +00007227 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007228 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
7229 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00007230 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007231 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007232}
7233
Matt Carlson21f581a2009-08-28 14:00:25 +00007234static int tg3_rx_prodring_init(struct tg3 *tp,
7235 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007236{
Matt Carlson2c49a442010-09-30 10:34:35 +00007237 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
7238 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007239 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007240 return -ENOMEM;
7241
Matt Carlson4bae65c2010-11-24 08:31:52 +00007242 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
7243 TG3_RX_STD_RING_BYTES(tp),
7244 &tpr->rx_std_mapping,
7245 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007246 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007247 goto err_out;
7248
Joe Perches63c3a662011-04-26 08:12:10 +00007249 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007250 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00007251 GFP_KERNEL);
7252 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007253 goto err_out;
7254
Matt Carlson4bae65c2010-11-24 08:31:52 +00007255 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
7256 TG3_RX_JMB_RING_BYTES(tp),
7257 &tpr->rx_jmb_mapping,
7258 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007259 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007260 goto err_out;
7261 }
7262
7263 return 0;
7264
7265err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00007266 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007267 return -ENOMEM;
7268}
7269
7270/* Free up pending packets in all rx/tx rings.
7271 *
7272 * The chip has been shut down and the driver detached from
7273 * the networking, so no interrupts or new tx packets will
7274 * end up in the driver. tp->{tx,}lock is not held and we are not
7275 * in an interrupt context and thus may sleep.
7276 */
7277static void tg3_free_rings(struct tg3 *tp)
7278{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007279 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007280
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007281 for (j = 0; j < tp->irq_cnt; j++) {
7282 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007283
Matt Carlson8fea32b2010-09-15 08:59:58 +00007284 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00007285
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007286 if (!tnapi->tx_buffers)
7287 continue;
7288
Matt Carlson0d681b22011-07-27 14:20:49 +00007289 for (i = 0; i < TG3_TX_RING_SIZE; i++) {
7290 struct sk_buff *skb = tnapi->tx_buffers[i].skb;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007291
Matt Carlson0d681b22011-07-27 14:20:49 +00007292 if (!skb)
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007293 continue;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007294
Matt Carlsonba1142e2011-11-04 09:15:00 +00007295 tg3_tx_skb_unmap(tnapi, i,
7296 skb_shinfo(skb)->nr_frags - 1);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007297
7298 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007299 }
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007300 }
Tom Herbert298376d2011-11-28 16:33:30 +00007301 netdev_reset_queue(tp->dev);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007302}
7303
7304/* Initialize tx/rx rings for packet processing.
7305 *
7306 * The chip has been shut down and the driver detached from
7307 * the networking, so no interrupts or new tx packets will
7308 * end up in the driver. tp->{tx,}lock are held and thus
7309 * we may not sleep.
7310 */
7311static int tg3_init_rings(struct tg3 *tp)
7312{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007313 int i;
Matt Carlson72334482009-08-28 14:03:01 +00007314
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007315 /* Free up all the SKBs. */
7316 tg3_free_rings(tp);
7317
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007318 for (i = 0; i < tp->irq_cnt; i++) {
7319 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007320
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007321 tnapi->last_tag = 0;
7322 tnapi->last_irq_tag = 0;
7323 tnapi->hw_status->status = 0;
7324 tnapi->hw_status->status_tag = 0;
7325 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7326
7327 tnapi->tx_prod = 0;
7328 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007329 if (tnapi->tx_ring)
7330 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007331
7332 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007333 if (tnapi->rx_rcb)
7334 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007335
Matt Carlson8fea32b2010-09-15 08:59:58 +00007336 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00007337 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007338 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00007339 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007340 }
Matt Carlson72334482009-08-28 14:03:01 +00007341
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007342 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007343}
7344
7345/*
7346 * Must not be invoked with interrupt sources disabled and
7347 * the hardware shutdown down.
7348 */
7349static void tg3_free_consistent(struct tg3 *tp)
7350{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007351 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00007352
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007353 for (i = 0; i < tp->irq_cnt; i++) {
7354 struct tg3_napi *tnapi = &tp->napi[i];
7355
7356 if (tnapi->tx_ring) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007357 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007358 tnapi->tx_ring, tnapi->tx_desc_mapping);
7359 tnapi->tx_ring = NULL;
7360 }
7361
7362 kfree(tnapi->tx_buffers);
7363 tnapi->tx_buffers = NULL;
7364
7365 if (tnapi->rx_rcb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007366 dma_free_coherent(&tp->pdev->dev,
7367 TG3_RX_RCB_RING_BYTES(tp),
7368 tnapi->rx_rcb,
7369 tnapi->rx_rcb_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007370 tnapi->rx_rcb = NULL;
7371 }
7372
Matt Carlson8fea32b2010-09-15 08:59:58 +00007373 tg3_rx_prodring_fini(tp, &tnapi->prodring);
7374
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007375 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007376 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
7377 tnapi->hw_status,
7378 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007379 tnapi->hw_status = NULL;
7380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007381 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007382
Linus Torvalds1da177e2005-04-16 15:20:36 -07007383 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007384 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
7385 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007386 tp->hw_stats = NULL;
7387 }
7388}
7389
7390/*
7391 * Must not be invoked with interrupt sources disabled and
7392 * the hardware shutdown down. Can sleep.
7393 */
7394static int tg3_alloc_consistent(struct tg3 *tp)
7395{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007396 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00007397
Matt Carlson4bae65c2010-11-24 08:31:52 +00007398 tp->hw_stats = dma_alloc_coherent(&tp->pdev->dev,
7399 sizeof(struct tg3_hw_stats),
7400 &tp->stats_mapping,
7401 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007402 if (!tp->hw_stats)
7403 goto err_out;
7404
Linus Torvalds1da177e2005-04-16 15:20:36 -07007405 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
7406
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007407 for (i = 0; i < tp->irq_cnt; i++) {
7408 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007409 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007410
Matt Carlson4bae65c2010-11-24 08:31:52 +00007411 tnapi->hw_status = dma_alloc_coherent(&tp->pdev->dev,
7412 TG3_HW_STATUS_SIZE,
7413 &tnapi->status_mapping,
7414 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007415 if (!tnapi->hw_status)
7416 goto err_out;
7417
7418 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007419 sblk = tnapi->hw_status;
7420
Matt Carlson8fea32b2010-09-15 08:59:58 +00007421 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
7422 goto err_out;
7423
Matt Carlson19cfaec2009-12-03 08:36:20 +00007424 /* If multivector TSS is enabled, vector 0 does not handle
7425 * tx interrupts. Don't allocate any resources for it.
7426 */
Joe Perches63c3a662011-04-26 08:12:10 +00007427 if ((!i && !tg3_flag(tp, ENABLE_TSS)) ||
7428 (i && tg3_flag(tp, ENABLE_TSS))) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00007429 tnapi->tx_buffers = kzalloc(
7430 sizeof(struct tg3_tx_ring_info) *
7431 TG3_TX_RING_SIZE, GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007432 if (!tnapi->tx_buffers)
7433 goto err_out;
7434
Matt Carlson4bae65c2010-11-24 08:31:52 +00007435 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
7436 TG3_TX_RING_BYTES,
7437 &tnapi->tx_desc_mapping,
7438 GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007439 if (!tnapi->tx_ring)
7440 goto err_out;
7441 }
7442
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007443 /*
7444 * When RSS is enabled, the status block format changes
7445 * slightly. The "rx_jumbo_consumer", "reserved",
7446 * and "rx_mini_consumer" members get mapped to the
7447 * other three rx return ring producer indexes.
7448 */
7449 switch (i) {
7450 default:
7451 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
7452 break;
7453 case 2:
7454 tnapi->rx_rcb_prod_idx = &sblk->rx_jumbo_consumer;
7455 break;
7456 case 3:
7457 tnapi->rx_rcb_prod_idx = &sblk->reserved;
7458 break;
7459 case 4:
7460 tnapi->rx_rcb_prod_idx = &sblk->rx_mini_consumer;
7461 break;
7462 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007463
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007464 /*
7465 * If multivector RSS is enabled, vector 0 does not handle
7466 * rx or tx interrupts. Don't allocate any resources for it.
7467 */
Joe Perches63c3a662011-04-26 08:12:10 +00007468 if (!i && tg3_flag(tp, ENABLE_RSS))
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007469 continue;
7470
Matt Carlson4bae65c2010-11-24 08:31:52 +00007471 tnapi->rx_rcb = dma_alloc_coherent(&tp->pdev->dev,
7472 TG3_RX_RCB_RING_BYTES(tp),
7473 &tnapi->rx_rcb_mapping,
7474 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007475 if (!tnapi->rx_rcb)
7476 goto err_out;
7477
7478 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007479 }
7480
Linus Torvalds1da177e2005-04-16 15:20:36 -07007481 return 0;
7482
7483err_out:
7484 tg3_free_consistent(tp);
7485 return -ENOMEM;
7486}
7487
7488#define MAX_WAIT_CNT 1000
7489
7490/* To stop a block, clear the enable bit and poll till it
7491 * clears. tp->lock is held.
7492 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007493static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007494{
7495 unsigned int i;
7496 u32 val;
7497
Joe Perches63c3a662011-04-26 08:12:10 +00007498 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007499 switch (ofs) {
7500 case RCVLSC_MODE:
7501 case DMAC_MODE:
7502 case MBFREE_MODE:
7503 case BUFMGR_MODE:
7504 case MEMARB_MODE:
7505 /* We can't enable/disable these bits of the
7506 * 5705/5750, just say success.
7507 */
7508 return 0;
7509
7510 default:
7511 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007513 }
7514
7515 val = tr32(ofs);
7516 val &= ~enable_bit;
7517 tw32_f(ofs, val);
7518
7519 for (i = 0; i < MAX_WAIT_CNT; i++) {
7520 udelay(100);
7521 val = tr32(ofs);
7522 if ((val & enable_bit) == 0)
7523 break;
7524 }
7525
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007526 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00007527 dev_err(&tp->pdev->dev,
7528 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
7529 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007530 return -ENODEV;
7531 }
7532
7533 return 0;
7534}
7535
7536/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007537static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007538{
7539 int i, err;
7540
7541 tg3_disable_ints(tp);
7542
7543 tp->rx_mode &= ~RX_MODE_ENABLE;
7544 tw32_f(MAC_RX_MODE, tp->rx_mode);
7545 udelay(10);
7546
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007547 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
7548 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
7549 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
7550 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
7551 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
7552 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007553
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007554 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
7555 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
7556 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
7557 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
7558 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
7559 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
7560 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007561
7562 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
7563 tw32_f(MAC_MODE, tp->mac_mode);
7564 udelay(40);
7565
7566 tp->tx_mode &= ~TX_MODE_ENABLE;
7567 tw32_f(MAC_TX_MODE, tp->tx_mode);
7568
7569 for (i = 0; i < MAX_WAIT_CNT; i++) {
7570 udelay(100);
7571 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
7572 break;
7573 }
7574 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00007575 dev_err(&tp->pdev->dev,
7576 "%s timed out, TX_MODE_ENABLE will not clear "
7577 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07007578 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007579 }
7580
Michael Chane6de8ad2005-05-05 14:42:41 -07007581 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007582 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
7583 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007584
7585 tw32(FTQ_RESET, 0xffffffff);
7586 tw32(FTQ_RESET, 0x00000000);
7587
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007588 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
7589 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007590
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007591 for (i = 0; i < tp->irq_cnt; i++) {
7592 struct tg3_napi *tnapi = &tp->napi[i];
7593 if (tnapi->hw_status)
7594 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007596
Linus Torvalds1da177e2005-04-16 15:20:36 -07007597 return err;
7598}
7599
Michael Chanee6a99b2007-07-18 21:49:10 -07007600/* Save PCI command register before chip reset */
7601static void tg3_save_pci_state(struct tg3 *tp)
7602{
Matt Carlson8a6eac92007-10-21 16:17:55 -07007603 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007604}
7605
7606/* Restore PCI state after chip reset */
7607static void tg3_restore_pci_state(struct tg3 *tp)
7608{
7609 u32 val;
7610
7611 /* Re-enable indirect register accesses. */
7612 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
7613 tp->misc_host_ctrl);
7614
7615 /* Set MAX PCI retry to zero. */
7616 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
7617 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007618 tg3_flag(tp, PCIX_MODE))
Michael Chanee6a99b2007-07-18 21:49:10 -07007619 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07007620 /* Allow reads and writes to the APE register and memory space. */
Joe Perches63c3a662011-04-26 08:12:10 +00007621 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07007622 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00007623 PCISTATE_ALLOW_APE_SHMEM_WR |
7624 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07007625 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
7626
Matt Carlson8a6eac92007-10-21 16:17:55 -07007627 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007628
Matt Carlson2c55a3d2011-11-28 09:41:04 +00007629 if (!tg3_flag(tp, PCI_EXPRESS)) {
7630 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
7631 tp->pci_cacheline_sz);
7632 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
7633 tp->pci_lat_timer);
Michael Chan114342f2007-10-15 02:12:26 -07007634 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08007635
Michael Chanee6a99b2007-07-18 21:49:10 -07007636 /* Make sure PCI-X relaxed ordering bit is clear. */
Joe Perches63c3a662011-04-26 08:12:10 +00007637 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07007638 u16 pcix_cmd;
7639
7640 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7641 &pcix_cmd);
7642 pcix_cmd &= ~PCI_X_CMD_ERO;
7643 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7644 pcix_cmd);
7645 }
Michael Chanee6a99b2007-07-18 21:49:10 -07007646
Joe Perches63c3a662011-04-26 08:12:10 +00007647 if (tg3_flag(tp, 5780_CLASS)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007648
7649 /* Chip reset on 5780 will reset MSI enable bit,
7650 * so need to restore it.
7651 */
Joe Perches63c3a662011-04-26 08:12:10 +00007652 if (tg3_flag(tp, USING_MSI)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007653 u16 ctrl;
7654
7655 pci_read_config_word(tp->pdev,
7656 tp->msi_cap + PCI_MSI_FLAGS,
7657 &ctrl);
7658 pci_write_config_word(tp->pdev,
7659 tp->msi_cap + PCI_MSI_FLAGS,
7660 ctrl | PCI_MSI_FLAGS_ENABLE);
7661 val = tr32(MSGINT_MODE);
7662 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
7663 }
7664 }
7665}
7666
Linus Torvalds1da177e2005-04-16 15:20:36 -07007667/* tp->lock is held. */
7668static int tg3_chip_reset(struct tg3 *tp)
7669{
7670 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07007671 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00007672 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007673
David S. Millerf49639e2006-06-09 11:58:36 -07007674 tg3_nvram_lock(tp);
7675
Matt Carlson77b483f2008-08-15 14:07:24 -07007676 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
7677
David S. Millerf49639e2006-06-09 11:58:36 -07007678 /* No matching tg3_nvram_unlock() after this because
7679 * chip reset below will undo the nvram lock.
7680 */
7681 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007682
Michael Chanee6a99b2007-07-18 21:49:10 -07007683 /* GRC_MISC_CFG core clock reset will clear the memory
7684 * enable bit in PCI register 4 and the MSI enable bit
7685 * on some chips, so we save relevant registers here.
7686 */
7687 tg3_save_pci_state(tp);
7688
Michael Chand9ab5ad2006-03-20 22:27:35 -08007689 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Joe Perches63c3a662011-04-26 08:12:10 +00007690 tg3_flag(tp, 5755_PLUS))
Michael Chand9ab5ad2006-03-20 22:27:35 -08007691 tw32(GRC_FASTBOOT_PC, 0);
7692
Linus Torvalds1da177e2005-04-16 15:20:36 -07007693 /*
7694 * We must avoid the readl() that normally takes place.
7695 * It locks machines, causes machine checks, and other
7696 * fun things. So, temporarily disable the 5701
7697 * hardware workaround, while we do the reset.
7698 */
Michael Chan1ee582d2005-08-09 20:16:46 -07007699 write_op = tp->write32;
7700 if (write_op == tg3_write_flush_reg32)
7701 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007702
Michael Chand18edcb2007-03-24 20:57:11 -07007703 /* Prevent the irq handler from reading or writing PCI registers
7704 * during chip reset when the memory enable bit in the PCI command
7705 * register may be cleared. The chip does not generate interrupt
7706 * at this time, but the irq handler may still be called due to irq
7707 * sharing or irqpoll.
7708 */
Joe Perches63c3a662011-04-26 08:12:10 +00007709 tg3_flag_set(tp, CHIP_RESETTING);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007710 for (i = 0; i < tp->irq_cnt; i++) {
7711 struct tg3_napi *tnapi = &tp->napi[i];
7712 if (tnapi->hw_status) {
7713 tnapi->hw_status->status = 0;
7714 tnapi->hw_status->status_tag = 0;
7715 }
7716 tnapi->last_tag = 0;
7717 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07007718 }
Michael Chand18edcb2007-03-24 20:57:11 -07007719 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00007720
7721 for (i = 0; i < tp->irq_cnt; i++)
7722 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07007723
Matt Carlson255ca312009-08-25 10:07:27 +00007724 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7725 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7726 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
7727 }
7728
Linus Torvalds1da177e2005-04-16 15:20:36 -07007729 /* do the reset */
7730 val = GRC_MISC_CFG_CORECLK_RESET;
7731
Joe Perches63c3a662011-04-26 08:12:10 +00007732 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson88075d92010-08-02 11:25:58 +00007733 /* Force PCIe 1.0a mode */
7734 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007735 !tg3_flag(tp, 57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00007736 tr32(TG3_PCIE_PHY_TSTCTL) ==
7737 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
7738 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
7739
Linus Torvalds1da177e2005-04-16 15:20:36 -07007740 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
7741 tw32(GRC_MISC_CFG, (1 << 29));
7742 val |= (1 << 29);
7743 }
7744 }
7745
Michael Chanb5d37722006-09-27 16:06:21 -07007746 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7747 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
7748 tw32(GRC_VCPU_EXT_CTRL,
7749 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
7750 }
7751
Matt Carlsonf37500d2010-08-02 11:25:59 +00007752 /* Manage gphy power for all CPMU absent PCIe devices. */
Joe Perches63c3a662011-04-26 08:12:10 +00007753 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007754 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00007755
Linus Torvalds1da177e2005-04-16 15:20:36 -07007756 tw32(GRC_MISC_CFG, val);
7757
Michael Chan1ee582d2005-08-09 20:16:46 -07007758 /* restore 5701 hardware bug workaround write method */
7759 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007760
7761 /* Unfortunately, we have to delay before the PCI read back.
7762 * Some 575X chips even will not respond to a PCI cfg access
7763 * when the reset command is given to the chip.
7764 *
7765 * How do these hardware designers expect things to work
7766 * properly if the PCI write is posted for a long period
7767 * of time? It is always necessary to have some method by
7768 * which a register read back can occur to push the write
7769 * out which does the reset.
7770 *
7771 * For most tg3 variants the trick below was working.
7772 * Ho hum...
7773 */
7774 udelay(120);
7775
7776 /* Flush PCI posted writes. The normal MMIO registers
7777 * are inaccessible at this time so this is the only
7778 * way to make this reliably (actually, this is no longer
7779 * the case, see above). I tried to use indirect
7780 * register read/write but this upset some 5701 variants.
7781 */
7782 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
7783
7784 udelay(120);
7785
Jon Mason708ebb32011-06-27 12:56:50 +00007786 if (tg3_flag(tp, PCI_EXPRESS) && pci_pcie_cap(tp->pdev)) {
Matt Carlsone7126992009-08-25 10:08:16 +00007787 u16 val16;
7788
Linus Torvalds1da177e2005-04-16 15:20:36 -07007789 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
7790 int i;
7791 u32 cfg_val;
7792
7793 /* Wait for link training to complete. */
7794 for (i = 0; i < 5000; i++)
7795 udelay(100);
7796
7797 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
7798 pci_write_config_dword(tp->pdev, 0xc4,
7799 cfg_val | (1 << 15));
7800 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007801
Matt Carlsone7126992009-08-25 10:08:16 +00007802 /* Clear the "no snoop" and "relaxed ordering" bits. */
7803 pci_read_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00007804 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007805 &val16);
7806 val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
7807 PCI_EXP_DEVCTL_NOSNOOP_EN);
7808 /*
7809 * Older PCIe devices only support the 128 byte
7810 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007811 */
Joe Perches63c3a662011-04-26 08:12:10 +00007812 if (!tg3_flag(tp, CPMU_PRESENT))
Matt Carlsone7126992009-08-25 10:08:16 +00007813 val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007814 pci_write_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00007815 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007816 val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007817
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007818 /* Clear error status */
7819 pci_write_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00007820 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVSTA,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007821 PCI_EXP_DEVSTA_CED |
7822 PCI_EXP_DEVSTA_NFED |
7823 PCI_EXP_DEVSTA_FED |
7824 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007825 }
7826
Michael Chanee6a99b2007-07-18 21:49:10 -07007827 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007828
Joe Perches63c3a662011-04-26 08:12:10 +00007829 tg3_flag_clear(tp, CHIP_RESETTING);
7830 tg3_flag_clear(tp, ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07007831
Michael Chanee6a99b2007-07-18 21:49:10 -07007832 val = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00007833 if (tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07007834 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07007835 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007836
7837 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
7838 tg3_stop_fw(tp);
7839 tw32(0x5000, 0x400);
7840 }
7841
7842 tw32(GRC_MODE, tp->grc_mode);
7843
7844 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007845 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007846
7847 tw32(0xc4, val | (1 << 15));
7848 }
7849
7850 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
7851 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
7852 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
7853 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
7854 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
7855 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7856 }
7857
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007858 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00007859 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007860 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007861 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00007862 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007863 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007864 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007865 val = 0;
7866
7867 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007868 udelay(40);
7869
Matt Carlson77b483f2008-08-15 14:07:24 -07007870 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
7871
Michael Chan7a6f4362006-09-27 16:03:31 -07007872 err = tg3_poll_fw(tp);
7873 if (err)
7874 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007875
Matt Carlson0a9140c2009-08-28 12:27:50 +00007876 tg3_mdio_start(tp);
7877
Joe Perches63c3a662011-04-26 08:12:10 +00007878 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007879 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
7880 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007881 !tg3_flag(tp, 57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007882 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007883
7884 tw32(0x7c00, val | (1 << 25));
7885 }
7886
Matt Carlsond78b59f2011-04-05 14:22:46 +00007887 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
7888 val = tr32(TG3_CPMU_CLCK_ORIDE);
7889 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
7890 }
7891
Linus Torvalds1da177e2005-04-16 15:20:36 -07007892 /* Reprobe ASF enable state. */
Joe Perches63c3a662011-04-26 08:12:10 +00007893 tg3_flag_clear(tp, ENABLE_ASF);
7894 tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007895 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
7896 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
7897 u32 nic_cfg;
7898
7899 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
7900 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +00007901 tg3_flag_set(tp, ENABLE_ASF);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007902 tp->last_event_jiffies = jiffies;
Joe Perches63c3a662011-04-26 08:12:10 +00007903 if (tg3_flag(tp, 5750_PLUS))
7904 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007905 }
7906 }
7907
7908 return 0;
7909}
7910
Matt Carlson92feeab2011-12-08 14:40:14 +00007911static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *,
7912 struct rtnl_link_stats64 *);
7913static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *,
7914 struct tg3_ethtool_stats *);
7915
Linus Torvalds1da177e2005-04-16 15:20:36 -07007916/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07007917static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007918{
7919 int err;
7920
7921 tg3_stop_fw(tp);
7922
Michael Chan944d9802005-05-29 14:57:48 -07007923 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007924
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007925 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007926 err = tg3_chip_reset(tp);
7927
Matt Carlsondaba2a62009-04-20 06:58:52 +00007928 __tg3_set_mac_addr(tp, 0);
7929
Michael Chan944d9802005-05-29 14:57:48 -07007930 tg3_write_sig_legacy(tp, kind);
7931 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007932
Matt Carlson92feeab2011-12-08 14:40:14 +00007933 if (tp->hw_stats) {
7934 /* Save the stats across chip resets... */
7935 tg3_get_stats64(tp->dev, &tp->net_stats_prev),
7936 tg3_get_estats(tp, &tp->estats_prev);
7937
7938 /* And make sure the next sample is new data */
7939 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
7940 }
7941
Linus Torvalds1da177e2005-04-16 15:20:36 -07007942 if (err)
7943 return err;
7944
7945 return 0;
7946}
7947
Linus Torvalds1da177e2005-04-16 15:20:36 -07007948static int tg3_set_mac_addr(struct net_device *dev, void *p)
7949{
7950 struct tg3 *tp = netdev_priv(dev);
7951 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07007952 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007953
Michael Chanf9804dd2005-09-27 12:13:10 -07007954 if (!is_valid_ether_addr(addr->sa_data))
7955 return -EINVAL;
7956
Linus Torvalds1da177e2005-04-16 15:20:36 -07007957 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7958
Michael Chane75f7c92006-03-20 21:33:26 -08007959 if (!netif_running(dev))
7960 return 0;
7961
Joe Perches63c3a662011-04-26 08:12:10 +00007962 if (tg3_flag(tp, ENABLE_ASF)) {
Michael Chan986e0ae2007-05-05 12:10:20 -07007963 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07007964
Michael Chan986e0ae2007-05-05 12:10:20 -07007965 addr0_high = tr32(MAC_ADDR_0_HIGH);
7966 addr0_low = tr32(MAC_ADDR_0_LOW);
7967 addr1_high = tr32(MAC_ADDR_1_HIGH);
7968 addr1_low = tr32(MAC_ADDR_1_LOW);
7969
7970 /* Skip MAC addr 1 if ASF is using it. */
7971 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
7972 !(addr1_high == 0 && addr1_low == 0))
7973 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07007974 }
Michael Chan986e0ae2007-05-05 12:10:20 -07007975 spin_lock_bh(&tp->lock);
7976 __tg3_set_mac_addr(tp, skip_mac_1);
7977 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007978
Michael Chanb9ec6c12006-07-25 16:37:27 -07007979 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007980}
7981
7982/* tp->lock is held. */
7983static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
7984 dma_addr_t mapping, u32 maxlen_flags,
7985 u32 nic_addr)
7986{
7987 tg3_write_mem(tp,
7988 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
7989 ((u64) mapping >> 32));
7990 tg3_write_mem(tp,
7991 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
7992 ((u64) mapping & 0xffffffff));
7993 tg3_write_mem(tp,
7994 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
7995 maxlen_flags);
7996
Joe Perches63c3a662011-04-26 08:12:10 +00007997 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007998 tg3_write_mem(tp,
7999 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
8000 nic_addr);
8001}
8002
8003static void __tg3_set_rx_mode(struct net_device *);
Michael Chand244c892005-07-05 14:42:33 -07008004static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07008005{
Matt Carlsonb6080e12009-09-01 13:12:00 +00008006 int i;
8007
Joe Perches63c3a662011-04-26 08:12:10 +00008008 if (!tg3_flag(tp, ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00008009 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
8010 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
8011 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008012 } else {
8013 tw32(HOSTCC_TXCOL_TICKS, 0);
8014 tw32(HOSTCC_TXMAX_FRAMES, 0);
8015 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00008016 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008017
Joe Perches63c3a662011-04-26 08:12:10 +00008018 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008019 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
8020 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
8021 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
8022 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00008023 tw32(HOSTCC_RXCOL_TICKS, 0);
8024 tw32(HOSTCC_RXMAX_FRAMES, 0);
8025 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07008026 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008027
Joe Perches63c3a662011-04-26 08:12:10 +00008028 if (!tg3_flag(tp, 5705_PLUS)) {
David S. Miller15f98502005-05-18 22:49:26 -07008029 u32 val = ec->stats_block_coalesce_usecs;
8030
Matt Carlsonb6080e12009-09-01 13:12:00 +00008031 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
8032 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
8033
David S. Miller15f98502005-05-18 22:49:26 -07008034 if (!netif_carrier_ok(tp->dev))
8035 val = 0;
8036
8037 tw32(HOSTCC_STAT_COAL_TICKS, val);
8038 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008039
8040 for (i = 0; i < tp->irq_cnt - 1; i++) {
8041 u32 reg;
8042
8043 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
8044 tw32(reg, ec->rx_coalesce_usecs);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008045 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
8046 tw32(reg, ec->rx_max_coalesced_frames);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008047 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
8048 tw32(reg, ec->rx_max_coalesced_frames_irq);
Matt Carlson19cfaec2009-12-03 08:36:20 +00008049
Joe Perches63c3a662011-04-26 08:12:10 +00008050 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008051 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
8052 tw32(reg, ec->tx_coalesce_usecs);
8053 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
8054 tw32(reg, ec->tx_max_coalesced_frames);
8055 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
8056 tw32(reg, ec->tx_max_coalesced_frames_irq);
8057 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008058 }
8059
8060 for (; i < tp->irq_max - 1; i++) {
8061 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008062 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008063 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00008064
Joe Perches63c3a662011-04-26 08:12:10 +00008065 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008066 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
8067 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
8068 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
8069 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008070 }
David S. Miller15f98502005-05-18 22:49:26 -07008071}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008072
8073/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00008074static void tg3_rings_reset(struct tg3 *tp)
8075{
8076 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008077 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008078 struct tg3_napi *tnapi = &tp->napi[0];
8079
8080 /* Disable all transmit rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008081 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008082 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Joe Perches63c3a662011-04-26 08:12:10 +00008083 else if (tg3_flag(tp, 5717_PLUS))
Matt Carlson3d377282010-10-14 10:37:39 +00008084 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
Matt Carlson55086ad2011-12-14 11:09:59 +00008085 else if (tg3_flag(tp, 57765_CLASS))
Matt Carlsonb703df62009-12-03 08:36:21 +00008086 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008087 else
8088 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8089
8090 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8091 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
8092 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
8093 BDINFO_FLAGS_DISABLED);
8094
8095
8096 /* Disable all receive return rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008097 if (tg3_flag(tp, 5717_PLUS))
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008098 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
Joe Perches63c3a662011-04-26 08:12:10 +00008099 else if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008100 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00008101 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00008102 tg3_flag(tp, 57765_CLASS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008103 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
8104 else
8105 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8106
8107 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8108 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
8109 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
8110 BDINFO_FLAGS_DISABLED);
8111
8112 /* Disable interrupts */
8113 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008114 tp->napi[0].chk_msi_cnt = 0;
8115 tp->napi[0].last_rx_cons = 0;
8116 tp->napi[0].last_tx_cons = 0;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008117
8118 /* Zero mailbox registers. */
Joe Perches63c3a662011-04-26 08:12:10 +00008119 if (tg3_flag(tp, SUPPORT_MSIX)) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00008120 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008121 tp->napi[i].tx_prod = 0;
8122 tp->napi[i].tx_cons = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00008123 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008124 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008125 tw32_rx_mbox(tp->napi[i].consmbox, 0);
8126 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
Matt Carlson7f230732011-08-31 11:44:48 +00008127 tp->napi[i].chk_msi_cnt = 0;
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008128 tp->napi[i].last_rx_cons = 0;
8129 tp->napi[i].last_tx_cons = 0;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008130 }
Joe Perches63c3a662011-04-26 08:12:10 +00008131 if (!tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008132 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008133 } else {
8134 tp->napi[0].tx_prod = 0;
8135 tp->napi[0].tx_cons = 0;
8136 tw32_mailbox(tp->napi[0].prodmbox, 0);
8137 tw32_rx_mbox(tp->napi[0].consmbox, 0);
8138 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008139
8140 /* Make sure the NIC-based send BD rings are disabled. */
Joe Perches63c3a662011-04-26 08:12:10 +00008141 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson2d31eca2009-09-01 12:53:31 +00008142 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
8143 for (i = 0; i < 16; i++)
8144 tw32_tx_mbox(mbox + i * 8, 0);
8145 }
8146
8147 txrcb = NIC_SRAM_SEND_RCB;
8148 rxrcb = NIC_SRAM_RCV_RET_RCB;
8149
8150 /* Clear status block in ram. */
8151 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8152
8153 /* Set status block DMA address */
8154 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8155 ((u64) tnapi->status_mapping >> 32));
8156 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8157 ((u64) tnapi->status_mapping & 0xffffffff));
8158
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008159 if (tnapi->tx_ring) {
8160 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8161 (TG3_TX_RING_SIZE <<
8162 BDINFO_FLAGS_MAXLEN_SHIFT),
8163 NIC_SRAM_TX_BUFFER_DESC);
8164 txrcb += TG3_BDINFO_SIZE;
8165 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008166
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008167 if (tnapi->rx_rcb) {
8168 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008169 (tp->rx_ret_ring_mask + 1) <<
8170 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008171 rxrcb += TG3_BDINFO_SIZE;
8172 }
8173
8174 stblk = HOSTCC_STATBLCK_RING1;
8175
8176 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
8177 u64 mapping = (u64)tnapi->status_mapping;
8178 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
8179 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
8180
8181 /* Clear status block in ram. */
8182 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8183
Matt Carlson19cfaec2009-12-03 08:36:20 +00008184 if (tnapi->tx_ring) {
8185 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8186 (TG3_TX_RING_SIZE <<
8187 BDINFO_FLAGS_MAXLEN_SHIFT),
8188 NIC_SRAM_TX_BUFFER_DESC);
8189 txrcb += TG3_BDINFO_SIZE;
8190 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008191
8192 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008193 ((tp->rx_ret_ring_mask + 1) <<
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008194 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
8195
8196 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008197 rxrcb += TG3_BDINFO_SIZE;
8198 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008199}
8200
Matt Carlsoneb07a942011-04-20 07:57:36 +00008201static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
8202{
8203 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
8204
Joe Perches63c3a662011-04-26 08:12:10 +00008205 if (!tg3_flag(tp, 5750_PLUS) ||
8206 tg3_flag(tp, 5780_CLASS) ||
Matt Carlsoneb07a942011-04-20 07:57:36 +00008207 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
Matt Carlson513aa6e2011-11-21 15:01:18 +00008208 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
8209 tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008210 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
8211 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
8212 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787)
8213 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
8214 else
8215 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
8216
8217 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
8218 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
8219
8220 val = min(nic_rep_thresh, host_rep_thresh);
8221 tw32(RCVBDI_STD_THRESH, val);
8222
Joe Perches63c3a662011-04-26 08:12:10 +00008223 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008224 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
8225
Joe Perches63c3a662011-04-26 08:12:10 +00008226 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008227 return;
8228
Matt Carlson513aa6e2011-11-21 15:01:18 +00008229 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
Matt Carlsoneb07a942011-04-20 07:57:36 +00008230
8231 host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
8232
8233 val = min(bdcache_maxcnt / 2, host_rep_thresh);
8234 tw32(RCVBDI_JUMBO_THRESH, val);
8235
Joe Perches63c3a662011-04-26 08:12:10 +00008236 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008237 tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
8238}
8239
Matt Carlson2d31eca2009-09-01 12:53:31 +00008240/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008241static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008242{
8243 u32 val, rdmac_mode;
8244 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00008245 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008246
8247 tg3_disable_ints(tp);
8248
8249 tg3_stop_fw(tp);
8250
8251 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
8252
Joe Perches63c3a662011-04-26 08:12:10 +00008253 if (tg3_flag(tp, INIT_COMPLETE))
Michael Chane6de8ad2005-05-05 14:42:41 -07008254 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008255
Matt Carlson699c0192010-12-06 08:28:51 +00008256 /* Enable MAC control of LPI */
8257 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
8258 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL,
8259 TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
8260 TG3_CPMU_EEE_LNKIDL_UART_IDL);
8261
8262 tw32_f(TG3_CPMU_EEE_CTRL,
8263 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
8264
Matt Carlsona386b902010-12-06 08:28:53 +00008265 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
8266 TG3_CPMU_EEEMD_LPI_IN_TX |
8267 TG3_CPMU_EEEMD_LPI_IN_RX |
8268 TG3_CPMU_EEEMD_EEE_ENABLE;
8269
8270 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8271 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
8272
Joe Perches63c3a662011-04-26 08:12:10 +00008273 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsona386b902010-12-06 08:28:53 +00008274 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
8275
8276 tw32_f(TG3_CPMU_EEE_MODE, val);
8277
8278 tw32_f(TG3_CPMU_EEE_DBTMR1,
8279 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
8280 TG3_CPMU_DBTMR1_LNKIDLE_2047US);
8281
8282 tw32_f(TG3_CPMU_EEE_DBTMR2,
Matt Carlsond7f2ab22011-01-25 15:58:56 +00008283 TG3_CPMU_DBTMR2_APE_TX_2047US |
Matt Carlsona386b902010-12-06 08:28:53 +00008284 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
Matt Carlson699c0192010-12-06 08:28:51 +00008285 }
8286
Matt Carlson603f1172010-02-12 14:47:10 +00008287 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08008288 tg3_phy_reset(tp);
8289
Linus Torvalds1da177e2005-04-16 15:20:36 -07008290 err = tg3_chip_reset(tp);
8291 if (err)
8292 return err;
8293
8294 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
8295
Matt Carlsonbcb37f62008-11-03 16:52:09 -08008296 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008297 val = tr32(TG3_CPMU_CTRL);
8298 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
8299 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08008300
8301 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8302 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8303 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8304 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
8305
8306 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
8307 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
8308 val |= CPMU_LNK_AWARE_MACCLK_6_25;
8309 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
8310
8311 val = tr32(TG3_CPMU_HST_ACC);
8312 val &= ~CPMU_HST_ACC_MACCLK_MASK;
8313 val |= CPMU_HST_ACC_MACCLK_6_25;
8314 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07008315 }
8316
Matt Carlson33466d92009-04-20 06:57:41 +00008317 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
8318 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
8319 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
8320 PCIE_PWR_MGMT_L1_THRESH_4MS;
8321 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00008322
8323 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
8324 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
8325
8326 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00008327
Matt Carlsonf40386c2009-11-02 14:24:02 +00008328 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
8329 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00008330 }
8331
Joe Perches63c3a662011-04-26 08:12:10 +00008332 if (tg3_flag(tp, L1PLLPD_EN)) {
Matt Carlson614b0592010-01-20 16:58:02 +00008333 u32 grc_mode = tr32(GRC_MODE);
8334
8335 /* Access the lower 1K of PL PCIE block registers. */
8336 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8337 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
8338
8339 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
8340 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
8341 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
8342
8343 tw32(GRC_MODE, grc_mode);
8344 }
8345
Matt Carlson55086ad2011-12-14 11:09:59 +00008346 if (tg3_flag(tp, 57765_CLASS)) {
Matt Carlson5093eed2010-11-24 08:31:45 +00008347 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
8348 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00008349
Matt Carlson5093eed2010-11-24 08:31:45 +00008350 /* Access the lower 1K of PL PCIE block registers. */
8351 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8352 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00008353
Matt Carlson5093eed2010-11-24 08:31:45 +00008354 val = tr32(TG3_PCIE_TLDLPL_PORT +
8355 TG3_PCIE_PL_LO_PHYCTL5);
8356 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
8357 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00008358
Matt Carlson5093eed2010-11-24 08:31:45 +00008359 tw32(GRC_MODE, grc_mode);
8360 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00008361
Matt Carlson1ff30a52011-05-19 12:12:46 +00008362 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_57765_AX) {
8363 u32 grc_mode = tr32(GRC_MODE);
8364
8365 /* Access the lower 1K of DL PCIE block registers. */
8366 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8367 tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
8368
8369 val = tr32(TG3_PCIE_TLDLPL_PORT +
8370 TG3_PCIE_DL_LO_FTSMAX);
8371 val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
8372 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
8373 val | TG3_PCIE_DL_LO_FTSMAX_VAL);
8374
8375 tw32(GRC_MODE, grc_mode);
8376 }
8377
Matt Carlsona977dbe2010-04-12 06:58:26 +00008378 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8379 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8380 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8381 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00008382 }
8383
Linus Torvalds1da177e2005-04-16 15:20:36 -07008384 /* This works around an issue with Athlon chipsets on
8385 * B3 tigon3 silicon. This bit has no effect on any
8386 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07008387 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008388 */
Joe Perches63c3a662011-04-26 08:12:10 +00008389 if (!tg3_flag(tp, CPMU_PRESENT)) {
8390 if (!tg3_flag(tp, PCI_EXPRESS))
Matt Carlson795d01c2007-10-07 23:28:17 -07008391 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
8392 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
8393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008394
8395 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008396 tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008397 val = tr32(TG3PCI_PCISTATE);
8398 val |= PCISTATE_RETRY_SAME_DMA;
8399 tw32(TG3PCI_PCISTATE, val);
8400 }
8401
Joe Perches63c3a662011-04-26 08:12:10 +00008402 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -07008403 /* Allow reads and writes to the
8404 * APE register and memory space.
8405 */
8406 val = tr32(TG3PCI_PCISTATE);
8407 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00008408 PCISTATE_ALLOW_APE_SHMEM_WR |
8409 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008410 tw32(TG3PCI_PCISTATE, val);
8411 }
8412
Linus Torvalds1da177e2005-04-16 15:20:36 -07008413 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
8414 /* Enable some hw fixes. */
8415 val = tr32(TG3PCI_MSI_DATA);
8416 val |= (1 << 26) | (1 << 28) | (1 << 29);
8417 tw32(TG3PCI_MSI_DATA, val);
8418 }
8419
8420 /* Descriptor ring init may make accesses to the
8421 * NIC SRAM area to setup the TX descriptors, so we
8422 * can only do this after the hardware has been
8423 * successfully reset.
8424 */
Michael Chan32d8c572006-07-25 16:38:29 -07008425 err = tg3_init_rings(tp);
8426 if (err)
8427 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008428
Joe Perches63c3a662011-04-26 08:12:10 +00008429 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008430 val = tr32(TG3PCI_DMA_RW_CTRL) &
8431 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Matt Carlson1a319022010-04-12 06:58:25 +00008432 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
8433 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlson55086ad2011-12-14 11:09:59 +00008434 if (!tg3_flag(tp, 57765_CLASS) &&
Matt Carlson0aebff42011-04-25 12:42:45 +00008435 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8436 val |= DMA_RWCTRL_TAGGED_STAT_WA;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008437 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
8438 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
8439 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008440 /* This value is determined during the probe time DMA
8441 * engine test, tg3_test_dma.
8442 */
8443 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
8444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008445
8446 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
8447 GRC_MODE_4X_NIC_SEND_RINGS |
8448 GRC_MODE_NO_TX_PHDR_CSUM |
8449 GRC_MODE_NO_RX_PHDR_CSUM);
8450 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07008451
8452 /* Pseudo-header checksum is done by hardware logic and not
8453 * the offload processers, so make the chip do the pseudo-
8454 * header checksums on receive. For transmit it is more
8455 * convenient to do the pseudo-header checksum in software
8456 * as Linux does that on transmit for us in all cases.
8457 */
8458 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008459
8460 tw32(GRC_MODE,
8461 tp->grc_mode |
8462 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
8463
8464 /* Setup the timer prescalar register. Clock is always 66Mhz. */
8465 val = tr32(GRC_MISC_CFG);
8466 val &= ~0xff;
8467 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
8468 tw32(GRC_MISC_CFG, val);
8469
8470 /* Initialize MBUF/DESC pool. */
Joe Perches63c3a662011-04-26 08:12:10 +00008471 if (tg3_flag(tp, 5750_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008472 /* Do nothing. */
8473 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
8474 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
8475 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
8476 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
8477 else
8478 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
8479 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
8480 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Joe Perches63c3a662011-04-26 08:12:10 +00008481 } else if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008482 int fw_len;
8483
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08008484 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008485 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
8486 tw32(BUFMGR_MB_POOL_ADDR,
8487 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
8488 tw32(BUFMGR_MB_POOL_SIZE,
8489 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
8490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008491
Michael Chan0f893dc2005-07-25 12:30:38 -07008492 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008493 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8494 tp->bufmgr_config.mbuf_read_dma_low_water);
8495 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8496 tp->bufmgr_config.mbuf_mac_rx_low_water);
8497 tw32(BUFMGR_MB_HIGH_WATER,
8498 tp->bufmgr_config.mbuf_high_water);
8499 } else {
8500 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8501 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
8502 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8503 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
8504 tw32(BUFMGR_MB_HIGH_WATER,
8505 tp->bufmgr_config.mbuf_high_water_jumbo);
8506 }
8507 tw32(BUFMGR_DMA_LOW_WATER,
8508 tp->bufmgr_config.dma_low_water);
8509 tw32(BUFMGR_DMA_HIGH_WATER,
8510 tp->bufmgr_config.dma_high_water);
8511
Matt Carlsond309a462010-09-30 10:34:31 +00008512 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
8513 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
8514 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
Matt Carlson4d958472011-04-20 07:57:35 +00008515 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
8516 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
8517 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0)
8518 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
Matt Carlsond309a462010-09-30 10:34:31 +00008519 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008520 for (i = 0; i < 2000; i++) {
8521 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
8522 break;
8523 udelay(10);
8524 }
8525 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00008526 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008527 return -ENODEV;
8528 }
8529
Matt Carlsoneb07a942011-04-20 07:57:36 +00008530 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
8531 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
Michael Chanb5d37722006-09-27 16:06:21 -07008532
Matt Carlsoneb07a942011-04-20 07:57:36 +00008533 tg3_setup_rxbd_thresholds(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008534
8535 /* Initialize TG3_BDINFO's at:
8536 * RCVDBDI_STD_BD: standard eth size rx ring
8537 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
8538 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
8539 *
8540 * like so:
8541 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
8542 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
8543 * ring attribute flags
8544 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
8545 *
8546 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
8547 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
8548 *
8549 * The size of each ring is fixed in the firmware, but the location is
8550 * configurable.
8551 */
8552 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008553 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008554 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008555 ((u64) tpr->rx_std_mapping & 0xffffffff));
Joe Perches63c3a662011-04-26 08:12:10 +00008556 if (!tg3_flag(tp, 5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +00008557 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
8558 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008559
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008560 /* Disable the mini ring */
Joe Perches63c3a662011-04-26 08:12:10 +00008561 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008562 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
8563 BDINFO_FLAGS_DISABLED);
8564
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008565 /* Program the jumbo buffer descriptor ring control
8566 * blocks on those devices that have them.
8567 */
Matt Carlsona0512942011-07-27 14:20:54 +00008568 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008569 (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008570
Joe Perches63c3a662011-04-26 08:12:10 +00008571 if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008572 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008573 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008574 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008575 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +00008576 val = TG3_RX_JMB_RING_SIZE(tp) <<
8577 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008578 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +00008579 val | BDINFO_FLAGS_USE_EXT_RECV);
Joe Perches63c3a662011-04-26 08:12:10 +00008580 if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
Matt Carlson55086ad2011-12-14 11:09:59 +00008581 tg3_flag(tp, 57765_CLASS))
Matt Carlson87668d32009-11-13 13:03:34 +00008582 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
8583 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008584 } else {
8585 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
8586 BDINFO_FLAGS_DISABLED);
8587 }
8588
Joe Perches63c3a662011-04-26 08:12:10 +00008589 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonfa6b2aa2011-11-21 15:01:19 +00008590 val = TG3_RX_STD_RING_SIZE(tp);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008591 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
8592 val |= (TG3_RX_STD_DMA_SZ << 2);
8593 } else
Matt Carlson04380d42010-04-12 06:58:29 +00008594 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008595 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008596 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008597
8598 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008599
Matt Carlson411da642009-11-13 13:03:46 +00008600 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e62009-11-13 13:03:49 +00008601 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008602
Joe Perches63c3a662011-04-26 08:12:10 +00008603 tpr->rx_jmb_prod_idx =
8604 tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
Matt Carlson66711e62009-11-13 13:03:49 +00008605 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008606
Matt Carlson2d31eca2009-09-01 12:53:31 +00008607 tg3_rings_reset(tp);
8608
Linus Torvalds1da177e2005-04-16 15:20:36 -07008609 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07008610 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008611
8612 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00008613 tw32(MAC_RX_MTU_SIZE,
8614 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008615
8616 /* The slot time is changed by tg3_setup_phy if we
8617 * run at gigabit with half duplex.
8618 */
Matt Carlsonf2096f92011-04-05 14:22:48 +00008619 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
8620 (6 << TX_LENGTHS_IPG_SHIFT) |
8621 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
8622
8623 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8624 val |= tr32(MAC_TX_LENGTHS) &
8625 (TX_LENGTHS_JMB_FRM_LEN_MSK |
8626 TX_LENGTHS_CNT_DWN_VAL_MSK);
8627
8628 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008629
8630 /* Receive rules. */
8631 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
8632 tw32(RCVLPC_CONFIG, 0x0181);
8633
8634 /* Calculate RDMAC_MODE setting early, we need it to determine
8635 * the RCVLPC_STATE_ENABLE mask.
8636 */
8637 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
8638 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
8639 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
8640 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
8641 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07008642
Matt Carlsondeabaac2010-11-24 08:31:50 +00008643 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +00008644 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
8645
Matt Carlson57e69832008-05-25 23:48:31 -07008646 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08008647 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8648 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07008649 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
8650 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
8651 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
8652
Matt Carlsonc5908932011-03-09 16:58:25 +00008653 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8654 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00008655 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07008656 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008657 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
8658 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008659 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008660 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8661 }
8662 }
8663
Joe Perches63c3a662011-04-26 08:12:10 +00008664 if (tg3_flag(tp, PCI_EXPRESS))
Michael Chan85e94ce2005-04-21 17:05:28 -07008665 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8666
Matt Carlson55086ad2011-12-14 11:09:59 +00008667 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57766)
8668 rdmac_mode |= RDMAC_MODE_JMB_2K_MMRR;
8669
Joe Perches63c3a662011-04-26 08:12:10 +00008670 if (tg3_flag(tp, HW_TSO_1) ||
8671 tg3_flag(tp, HW_TSO_2) ||
8672 tg3_flag(tp, HW_TSO_3))
Matt Carlson027455a2008-12-21 20:19:30 -08008673 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
8674
Matt Carlson108a6c12011-05-19 12:12:47 +00008675 if (tg3_flag(tp, 57765_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +00008676 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson027455a2008-12-21 20:19:30 -08008677 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
8678 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008679
Matt Carlsonf2096f92011-04-05 14:22:48 +00008680 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8681 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
8682
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008683 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
8684 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
8685 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8686 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008687 tg3_flag(tp, 57765_PLUS)) {
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008688 val = tr32(TG3_RDMA_RSRVCTRL_REG);
Matt Carlsond78b59f2011-04-05 14:22:46 +00008689 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8690 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +00008691 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
8692 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
8693 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
8694 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
8695 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
8696 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +00008697 }
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008698 tw32(TG3_RDMA_RSRVCTRL_REG,
8699 val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
8700 }
8701
Matt Carlsond78b59f2011-04-05 14:22:46 +00008702 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8703 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsond309a462010-09-30 10:34:31 +00008704 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
8705 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val |
8706 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
8707 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
8708 }
8709
Linus Torvalds1da177e2005-04-16 15:20:36 -07008710 /* Receive/send statistics. */
Joe Perches63c3a662011-04-26 08:12:10 +00008711 if (tg3_flag(tp, 5750_PLUS)) {
Michael Chan16613942006-06-29 20:15:13 -07008712 val = tr32(RCVLPC_STATS_ENABLE);
8713 val &= ~RCVLPC_STATSENAB_DACK_FIX;
8714 tw32(RCVLPC_STATS_ENABLE, val);
8715 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008716 tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008717 val = tr32(RCVLPC_STATS_ENABLE);
8718 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
8719 tw32(RCVLPC_STATS_ENABLE, val);
8720 } else {
8721 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
8722 }
8723 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
8724 tw32(SNDDATAI_STATSENAB, 0xffffff);
8725 tw32(SNDDATAI_STATSCTRL,
8726 (SNDDATAI_SCTRL_ENABLE |
8727 SNDDATAI_SCTRL_FASTUPD));
8728
8729 /* Setup host coalescing engine. */
8730 tw32(HOSTCC_MODE, 0);
8731 for (i = 0; i < 2000; i++) {
8732 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
8733 break;
8734 udelay(10);
8735 }
8736
Michael Chand244c892005-07-05 14:42:33 -07008737 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008738
Joe Perches63c3a662011-04-26 08:12:10 +00008739 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008740 /* Status/statistics block address. See tg3_timer,
8741 * the tg3_periodic_fetch_stats call there, and
8742 * tg3_get_stats to see how this works for 5705/5750 chips.
8743 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008744 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8745 ((u64) tp->stats_mapping >> 32));
8746 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8747 ((u64) tp->stats_mapping & 0xffffffff));
8748 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008749
Linus Torvalds1da177e2005-04-16 15:20:36 -07008750 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008751
8752 /* Clear statistics and status block memory areas */
8753 for (i = NIC_SRAM_STATS_BLK;
8754 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
8755 i += sizeof(u32)) {
8756 tg3_write_mem(tp, i, 0);
8757 udelay(40);
8758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008759 }
8760
8761 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
8762
8763 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
8764 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008765 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008766 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
8767
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008768 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
8769 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -07008770 /* reset to prevent losing 1st rx packet intermittently */
8771 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8772 udelay(10);
8773 }
8774
Matt Carlson3bda1252008-08-15 14:08:22 -07008775 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Matt Carlson9e975cc2011-07-20 10:20:50 +00008776 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE |
8777 MAC_MODE_FHDE_ENABLE;
8778 if (tg3_flag(tp, ENABLE_APE))
8779 tp->mac_mode |= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Joe Perches63c3a662011-04-26 08:12:10 +00008780 if (!tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008781 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07008782 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
8783 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008784 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
8785 udelay(40);
8786
Michael Chan314fba32005-04-21 17:07:04 -07008787 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Joe Perches63c3a662011-04-26 08:12:10 +00008788 * If TG3_FLAG_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07008789 * register to preserve the GPIO settings for LOMs. The GPIOs,
8790 * whether used as inputs or outputs, are set by boot code after
8791 * reset.
8792 */
Joe Perches63c3a662011-04-26 08:12:10 +00008793 if (!tg3_flag(tp, IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07008794 u32 gpio_mask;
8795
Michael Chan9d26e212006-12-07 00:21:14 -08008796 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
8797 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
8798 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07008799
8800 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
8801 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
8802 GRC_LCLCTRL_GPIO_OUTPUT3;
8803
Michael Chanaf36e6b2006-03-23 01:28:06 -08008804 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
8805 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
8806
Gary Zambranoaaf84462007-05-05 11:51:45 -07008807 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07008808 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
8809
8810 /* GPIO1 must be driven high for eeprom write protect */
Joe Perches63c3a662011-04-26 08:12:10 +00008811 if (tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan9d26e212006-12-07 00:21:14 -08008812 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
8813 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07008814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008815 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8816 udelay(100);
8817
Joe Perches63c3a662011-04-26 08:12:10 +00008818 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008819 val = tr32(MSGINT_MODE);
8820 val |= MSGINT_MODE_MULTIVEC_EN | MSGINT_MODE_ENABLE;
Matt Carlson5b39de92011-08-31 11:44:50 +00008821 if (!tg3_flag(tp, 1SHOT_MSI))
8822 val |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008823 tw32(MSGINT_MODE, val);
8824 }
8825
Joe Perches63c3a662011-04-26 08:12:10 +00008826 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008827 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
8828 udelay(40);
8829 }
8830
8831 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
8832 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
8833 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
8834 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
8835 WDMAC_MODE_LNGREAD_ENAB);
8836
Matt Carlsonc5908932011-03-09 16:58:25 +00008837 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8838 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00008839 if (tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008840 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
8841 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
8842 /* nothing */
8843 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008844 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008845 val |= WDMAC_MODE_RX_ACCEL;
8846 }
8847 }
8848
Michael Chand9ab5ad2006-03-20 22:27:35 -08008849 /* Enable host coalescing bug fix */
Joe Perches63c3a662011-04-26 08:12:10 +00008850 if (tg3_flag(tp, 5755_PLUS))
Matt Carlsonf51f3562008-05-25 23:45:08 -07008851 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad2006-03-20 22:27:35 -08008852
Matt Carlson788a0352009-11-02 14:26:03 +00008853 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
8854 val |= WDMAC_MODE_BURST_ALL_DATA;
8855
Linus Torvalds1da177e2005-04-16 15:20:36 -07008856 tw32_f(WDMAC_MODE, val);
8857 udelay(40);
8858
Joe Perches63c3a662011-04-26 08:12:10 +00008859 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07008860 u16 pcix_cmd;
8861
8862 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8863 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008864 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07008865 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
8866 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008867 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07008868 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
8869 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008870 }
Matt Carlson9974a352007-10-07 23:27:28 -07008871 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8872 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008873 }
8874
8875 tw32_f(RDMAC_MODE, rdmac_mode);
8876 udelay(40);
8877
8878 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008879 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008880 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07008881
8882 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
8883 tw32(SNDDATAC_MODE,
8884 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
8885 else
8886 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
8887
Linus Torvalds1da177e2005-04-16 15:20:36 -07008888 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
8889 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008890 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00008891 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008892 val |= RCVDBDI_MODE_LRG_RING_SZ;
8893 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008894 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008895 if (tg3_flag(tp, HW_TSO_1) ||
8896 tg3_flag(tp, HW_TSO_2) ||
8897 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008898 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008899 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00008900 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008901 val |= SNDBDI_MODE_MULTI_TXQ_EN;
8902 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008903 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
8904
8905 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
8906 err = tg3_load_5701_a0_firmware_fix(tp);
8907 if (err)
8908 return err;
8909 }
8910
Joe Perches63c3a662011-04-26 08:12:10 +00008911 if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008912 err = tg3_load_tso_firmware(tp);
8913 if (err)
8914 return err;
8915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008916
8917 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +00008918
Joe Perches63c3a662011-04-26 08:12:10 +00008919 if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsonb1d05212010-06-05 17:24:31 +00008920 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
8921 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +00008922
8923 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
8924 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
8925 tp->tx_mode &= ~val;
8926 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
8927 }
8928
Linus Torvalds1da177e2005-04-16 15:20:36 -07008929 tw32_f(MAC_TX_MODE, tp->tx_mode);
8930 udelay(100);
8931
Joe Perches63c3a662011-04-26 08:12:10 +00008932 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson9d53fa12011-07-20 10:20:54 +00008933 int i = 0;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008934 u32 reg = MAC_RSS_INDIR_TBL_0;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008935
Matt Carlson9d53fa12011-07-20 10:20:54 +00008936 if (tp->irq_cnt == 2) {
8937 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i += 8) {
8938 tw32(reg, 0x0);
8939 reg += 4;
8940 }
8941 } else {
8942 u32 val;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008943
Matt Carlson9d53fa12011-07-20 10:20:54 +00008944 while (i < TG3_RSS_INDIR_TBL_SIZE) {
8945 val = i % (tp->irq_cnt - 1);
8946 i++;
8947 for (; i % 8; i++) {
8948 val <<= 4;
8949 val |= (i % (tp->irq_cnt - 1));
8950 }
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008951 tw32(reg, val);
8952 reg += 4;
8953 }
8954 }
8955
8956 /* Setup the "secret" hash key. */
8957 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
8958 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
8959 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
8960 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
8961 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
8962 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
8963 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
8964 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
8965 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
8966 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
8967 }
8968
Linus Torvalds1da177e2005-04-16 15:20:36 -07008969 tp->rx_mode = RX_MODE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00008970 if (tg3_flag(tp, 5755_PLUS))
Michael Chanaf36e6b2006-03-23 01:28:06 -08008971 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
8972
Joe Perches63c3a662011-04-26 08:12:10 +00008973 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008974 tp->rx_mode |= RX_MODE_RSS_ENABLE |
8975 RX_MODE_RSS_ITBL_HASH_BITS_7 |
8976 RX_MODE_RSS_IPV6_HASH_EN |
8977 RX_MODE_RSS_TCP_IPV6_HASH_EN |
8978 RX_MODE_RSS_IPV4_HASH_EN |
8979 RX_MODE_RSS_TCP_IPV4_HASH_EN;
8980
Linus Torvalds1da177e2005-04-16 15:20:36 -07008981 tw32_f(MAC_RX_MODE, tp->rx_mode);
8982 udelay(10);
8983
Linus Torvalds1da177e2005-04-16 15:20:36 -07008984 tw32(MAC_LED_CTRL, tp->led_ctrl);
8985
8986 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008987 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008988 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8989 udelay(10);
8990 }
8991 tw32_f(MAC_RX_MODE, tp->rx_mode);
8992 udelay(10);
8993
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008994 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008995 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008996 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008997 /* Set drive transmission level to 1.2V */
8998 /* only if the signal pre-emphasis bit is not set */
8999 val = tr32(MAC_SERDES_CFG);
9000 val &= 0xfffff000;
9001 val |= 0x880;
9002 tw32(MAC_SERDES_CFG, val);
9003 }
9004 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
9005 tw32(MAC_SERDES_CFG, 0x616000);
9006 }
9007
9008 /* Prevent chip from dropping frames when flow control
9009 * is enabled.
9010 */
Matt Carlson55086ad2011-12-14 11:09:59 +00009011 if (tg3_flag(tp, 57765_CLASS))
Matt Carlson666bc832010-01-20 16:58:03 +00009012 val = 1;
9013 else
9014 val = 2;
9015 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009016
9017 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009018 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009019 /* Use hardware link auto-negotiation */
Joe Perches63c3a662011-04-26 08:12:10 +00009020 tg3_flag_set(tp, HW_AUTONEG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009021 }
9022
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009023 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +00009024 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08009025 u32 tmp;
9026
9027 tmp = tr32(SERDES_RX_CTRL);
9028 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
9029 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
9030 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
9031 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
9032 }
9033
Joe Perches63c3a662011-04-26 08:12:10 +00009034 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson80096062010-08-02 11:26:06 +00009035 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
9036 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07009037 tp->link_config.speed = tp->link_config.orig_speed;
9038 tp->link_config.duplex = tp->link_config.orig_duplex;
9039 tp->link_config.autoneg = tp->link_config.orig_autoneg;
9040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009041
Matt Carlsondd477002008-05-25 23:45:58 -07009042 err = tg3_setup_phy(tp, 0);
9043 if (err)
9044 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009045
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009046 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
9047 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07009048 u32 tmp;
9049
9050 /* Clear CRC stats. */
9051 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
9052 tg3_writephy(tp, MII_TG3_TEST1,
9053 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009054 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -07009055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009056 }
9057 }
9058
9059 __tg3_set_rx_mode(tp->dev);
9060
9061 /* Initialize receive rules. */
9062 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
9063 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
9064 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
9065 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
9066
Joe Perches63c3a662011-04-26 08:12:10 +00009067 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009068 limit = 8;
9069 else
9070 limit = 16;
Joe Perches63c3a662011-04-26 08:12:10 +00009071 if (tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009072 limit -= 4;
9073 switch (limit) {
9074 case 16:
9075 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
9076 case 15:
9077 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
9078 case 14:
9079 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
9080 case 13:
9081 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
9082 case 12:
9083 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
9084 case 11:
9085 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
9086 case 10:
9087 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
9088 case 9:
9089 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
9090 case 8:
9091 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
9092 case 7:
9093 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
9094 case 6:
9095 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
9096 case 5:
9097 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
9098 case 4:
9099 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
9100 case 3:
9101 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
9102 case 2:
9103 case 1:
9104
9105 default:
9106 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07009107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009108
Joe Perches63c3a662011-04-26 08:12:10 +00009109 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson9ce768e2007-10-11 19:49:11 -07009110 /* Write our heartbeat update interval to APE. */
9111 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
9112 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07009113
Linus Torvalds1da177e2005-04-16 15:20:36 -07009114 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
9115
Linus Torvalds1da177e2005-04-16 15:20:36 -07009116 return 0;
9117}
9118
9119/* Called at device open time to get the chip ready for
9120 * packet processing. Invoked with tp->lock held.
9121 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009122static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009123{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009124 tg3_switch_clocks(tp);
9125
9126 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
9127
Matt Carlson2f751b62008-08-04 23:17:34 -07009128 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009129}
9130
9131#define TG3_STAT_ADD32(PSTAT, REG) \
9132do { u32 __val = tr32(REG); \
9133 (PSTAT)->low += __val; \
9134 if ((PSTAT)->low < __val) \
9135 (PSTAT)->high += 1; \
9136} while (0)
9137
9138static void tg3_periodic_fetch_stats(struct tg3 *tp)
9139{
9140 struct tg3_hw_stats *sp = tp->hw_stats;
9141
9142 if (!netif_carrier_ok(tp->dev))
9143 return;
9144
9145 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
9146 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
9147 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
9148 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
9149 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
9150 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
9151 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
9152 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
9153 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
9154 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
9155 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
9156 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
9157 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
9158
9159 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
9160 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
9161 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
9162 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
9163 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
9164 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
9165 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
9166 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
9167 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
9168 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
9169 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
9170 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
9171 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
9172 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07009173
9174 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
Matt Carlson310050f2011-05-19 12:12:55 +00009175 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
9176 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0 &&
9177 tp->pci_chip_rev_id != CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +00009178 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
9179 } else {
9180 u32 val = tr32(HOSTCC_FLOW_ATTN);
9181 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
9182 if (val) {
9183 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
9184 sp->rx_discards.low += val;
9185 if (sp->rx_discards.low < val)
9186 sp->rx_discards.high += 1;
9187 }
9188 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
9189 }
Michael Chan463d3052006-05-22 16:36:27 -07009190 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009191}
9192
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009193static void tg3_chk_missed_msi(struct tg3 *tp)
9194{
9195 u32 i;
9196
9197 for (i = 0; i < tp->irq_cnt; i++) {
9198 struct tg3_napi *tnapi = &tp->napi[i];
9199
9200 if (tg3_has_work(tnapi)) {
9201 if (tnapi->last_rx_cons == tnapi->rx_rcb_ptr &&
9202 tnapi->last_tx_cons == tnapi->tx_cons) {
9203 if (tnapi->chk_msi_cnt < 1) {
9204 tnapi->chk_msi_cnt++;
9205 return;
9206 }
Matt Carlson7f230732011-08-31 11:44:48 +00009207 tg3_msi(0, tnapi);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009208 }
9209 }
9210 tnapi->chk_msi_cnt = 0;
9211 tnapi->last_rx_cons = tnapi->rx_rcb_ptr;
9212 tnapi->last_tx_cons = tnapi->tx_cons;
9213 }
9214}
9215
Linus Torvalds1da177e2005-04-16 15:20:36 -07009216static void tg3_timer(unsigned long __opaque)
9217{
9218 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009219
Matt Carlson5b190622011-11-04 09:15:04 +00009220 if (tp->irq_sync || tg3_flag(tp, RESET_TASK_PENDING))
Michael Chanf475f162006-03-27 23:20:14 -08009221 goto restart_timer;
9222
David S. Millerf47c11e2005-06-24 20:18:35 -07009223 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009224
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009225 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00009226 tg3_flag(tp, 57765_CLASS))
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009227 tg3_chk_missed_msi(tp);
9228
Joe Perches63c3a662011-04-26 08:12:10 +00009229 if (!tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -07009230 /* All of this garbage is because when using non-tagged
9231 * IRQ status the mailbox/status_block protocol the chip
9232 * uses with the cpu is race prone.
9233 */
Matt Carlson898a56f2009-08-28 14:02:40 +00009234 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -07009235 tw32(GRC_LOCAL_CTRL,
9236 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
9237 } else {
9238 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00009239 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -07009240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009241
David S. Millerfac9b832005-05-18 22:46:34 -07009242 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
David S. Millerf47c11e2005-06-24 20:18:35 -07009243 spin_unlock(&tp->lock);
Matt Carlsondb219972011-11-04 09:15:03 +00009244 tg3_reset_task_schedule(tp);
Matt Carlson5b190622011-11-04 09:15:04 +00009245 goto restart_timer;
David S. Millerfac9b832005-05-18 22:46:34 -07009246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009247 }
9248
Linus Torvalds1da177e2005-04-16 15:20:36 -07009249 /* This part only runs once per second. */
9250 if (!--tp->timer_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00009251 if (tg3_flag(tp, 5705_PLUS))
David S. Millerfac9b832005-05-18 22:46:34 -07009252 tg3_periodic_fetch_stats(tp);
9253
Matt Carlsonb0c59432011-05-19 12:12:48 +00009254 if (tp->setlpicnt && !--tp->setlpicnt)
9255 tg3_phy_eee_enable(tp);
Matt Carlson52b02d02010-10-14 10:37:41 +00009256
Joe Perches63c3a662011-04-26 08:12:10 +00009257 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009258 u32 mac_stat;
9259 int phy_event;
9260
9261 mac_stat = tr32(MAC_STATUS);
9262
9263 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009264 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009265 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
9266 phy_event = 1;
9267 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
9268 phy_event = 1;
9269
9270 if (phy_event)
9271 tg3_setup_phy(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +00009272 } else if (tg3_flag(tp, POLL_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009273 u32 mac_stat = tr32(MAC_STATUS);
9274 int need_setup = 0;
9275
9276 if (netif_carrier_ok(tp->dev) &&
9277 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
9278 need_setup = 1;
9279 }
Matt Carlsonbe98da62010-07-11 09:31:46 +00009280 if (!netif_carrier_ok(tp->dev) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009281 (mac_stat & (MAC_STATUS_PCS_SYNCED |
9282 MAC_STATUS_SIGNAL_DET))) {
9283 need_setup = 1;
9284 }
9285 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07009286 if (!tp->serdes_counter) {
9287 tw32_f(MAC_MODE,
9288 (tp->mac_mode &
9289 ~MAC_MODE_PORT_MODE_MASK));
9290 udelay(40);
9291 tw32_f(MAC_MODE, tp->mac_mode);
9292 udelay(40);
9293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009294 tg3_setup_phy(tp, 0);
9295 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009296 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009297 tg3_flag(tp, 5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -07009298 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +00009299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009300
9301 tp->timer_counter = tp->timer_multiplier;
9302 }
9303
Michael Chan130b8e42006-09-27 16:00:40 -07009304 /* Heartbeat is only sent once every 2 seconds.
9305 *
9306 * The heartbeat is to tell the ASF firmware that the host
9307 * driver is still alive. In the event that the OS crashes,
9308 * ASF needs to reset the hardware to free up the FIFO space
9309 * that may be filled with rx packets destined for the host.
9310 * If the FIFO is full, ASF will no longer function properly.
9311 *
9312 * Unintended resets have been reported on real time kernels
9313 * where the timer doesn't run on time. Netpoll will also have
9314 * same problem.
9315 *
9316 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
9317 * to check the ring condition when the heartbeat is expiring
9318 * before doing the reset. This will prevent most unintended
9319 * resets.
9320 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009321 if (!--tp->asf_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00009322 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07009323 tg3_wait_for_event_ack(tp);
9324
Michael Chanbbadf502006-04-06 21:46:34 -07009325 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07009326 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07009327 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +00009328 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
9329 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -07009330
9331 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009332 }
9333 tp->asf_counter = tp->asf_multiplier;
9334 }
9335
David S. Millerf47c11e2005-06-24 20:18:35 -07009336 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009337
Michael Chanf475f162006-03-27 23:20:14 -08009338restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07009339 tp->timer.expires = jiffies + tp->timer_offset;
9340 add_timer(&tp->timer);
9341}
9342
Matt Carlson4f125f42009-09-01 12:55:02 +00009343static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -08009344{
David Howells7d12e782006-10-05 14:55:46 +01009345 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009346 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +00009347 char *name;
9348 struct tg3_napi *tnapi = &tp->napi[irq_num];
9349
9350 if (tp->irq_cnt == 1)
9351 name = tp->dev->name;
9352 else {
9353 name = &tnapi->irq_lbl[0];
9354 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
9355 name[IFNAMSIZ-1] = 0;
9356 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009357
Joe Perches63c3a662011-04-26 08:12:10 +00009358 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Michael Chanfcfa0a32006-03-20 22:28:41 -08009359 fn = tg3_msi;
Joe Perches63c3a662011-04-26 08:12:10 +00009360 if (tg3_flag(tp, 1SHOT_MSI))
Michael Chanfcfa0a32006-03-20 22:28:41 -08009361 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009362 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009363 } else {
9364 fn = tg3_interrupt;
Joe Perches63c3a662011-04-26 08:12:10 +00009365 if (tg3_flag(tp, TAGGED_STATUS))
Michael Chanfcfa0a32006-03-20 22:28:41 -08009366 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009367 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009368 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009369
9370 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009371}
9372
Michael Chan79381092005-04-21 17:13:59 -07009373static int tg3_test_interrupt(struct tg3 *tp)
9374{
Matt Carlson09943a12009-08-28 14:01:57 +00009375 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -07009376 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -07009377 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009378 u32 val;
Michael Chan79381092005-04-21 17:13:59 -07009379
Michael Chand4bc3922005-05-29 14:59:20 -07009380 if (!netif_running(dev))
9381 return -ENODEV;
9382
Michael Chan79381092005-04-21 17:13:59 -07009383 tg3_disable_ints(tp);
9384
Matt Carlson4f125f42009-09-01 12:55:02 +00009385 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009386
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009387 /*
9388 * Turn off MSI one shot mode. Otherwise this test has no
9389 * observable way to know whether the interrupt was delivered.
9390 */
Matt Carlson3aa1cdf2011-07-20 10:20:55 +00009391 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009392 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
9393 tw32(MSGINT_MODE, val);
9394 }
9395
Matt Carlson4f125f42009-09-01 12:55:02 +00009396 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Matt Carlson09943a12009-08-28 14:01:57 +00009397 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009398 if (err)
9399 return err;
9400
Matt Carlson898a56f2009-08-28 14:02:40 +00009401 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -07009402 tg3_enable_ints(tp);
9403
9404 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00009405 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -07009406
9407 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -07009408 u32 int_mbox, misc_host_ctrl;
9409
Matt Carlson898a56f2009-08-28 14:02:40 +00009410 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -07009411 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
9412
9413 if ((int_mbox != 0) ||
9414 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
9415 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -07009416 break;
Michael Chanb16250e2006-09-27 16:10:14 -07009417 }
9418
Matt Carlson3aa1cdf2011-07-20 10:20:55 +00009419 if (tg3_flag(tp, 57765_PLUS) &&
9420 tnapi->hw_status->status_tag != tnapi->last_tag)
9421 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
9422
Michael Chan79381092005-04-21 17:13:59 -07009423 msleep(10);
9424 }
9425
9426 tg3_disable_ints(tp);
9427
Matt Carlson4f125f42009-09-01 12:55:02 +00009428 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009429
Matt Carlson4f125f42009-09-01 12:55:02 +00009430 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009431
9432 if (err)
9433 return err;
9434
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009435 if (intr_ok) {
9436 /* Reenable MSI one shot mode. */
Matt Carlson5b39de92011-08-31 11:44:50 +00009437 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, 1SHOT_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009438 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
9439 tw32(MSGINT_MODE, val);
9440 }
Michael Chan79381092005-04-21 17:13:59 -07009441 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009442 }
Michael Chan79381092005-04-21 17:13:59 -07009443
9444 return -EIO;
9445}
9446
9447/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
9448 * successfully restored
9449 */
9450static int tg3_test_msi(struct tg3 *tp)
9451{
Michael Chan79381092005-04-21 17:13:59 -07009452 int err;
9453 u16 pci_cmd;
9454
Joe Perches63c3a662011-04-26 08:12:10 +00009455 if (!tg3_flag(tp, USING_MSI))
Michael Chan79381092005-04-21 17:13:59 -07009456 return 0;
9457
9458 /* Turn off SERR reporting in case MSI terminates with Master
9459 * Abort.
9460 */
9461 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
9462 pci_write_config_word(tp->pdev, PCI_COMMAND,
9463 pci_cmd & ~PCI_COMMAND_SERR);
9464
9465 err = tg3_test_interrupt(tp);
9466
9467 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
9468
9469 if (!err)
9470 return 0;
9471
9472 /* other failures */
9473 if (err != -EIO)
9474 return err;
9475
9476 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009477 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
9478 "to INTx mode. Please report this failure to the PCI "
9479 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -07009480
Matt Carlson4f125f42009-09-01 12:55:02 +00009481 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +00009482
Michael Chan79381092005-04-21 17:13:59 -07009483 pci_disable_msi(tp->pdev);
9484
Joe Perches63c3a662011-04-26 08:12:10 +00009485 tg3_flag_clear(tp, USING_MSI);
Andre Detschdc8bf1b2010-04-26 07:27:07 +00009486 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -07009487
Matt Carlson4f125f42009-09-01 12:55:02 +00009488 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009489 if (err)
9490 return err;
9491
9492 /* Need to reset the chip because the MSI cycle may have terminated
9493 * with Master Abort.
9494 */
David S. Millerf47c11e2005-06-24 20:18:35 -07009495 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009496
Michael Chan944d9802005-05-29 14:57:48 -07009497 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009498 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009499
David S. Millerf47c11e2005-06-24 20:18:35 -07009500 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009501
9502 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +00009503 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -07009504
9505 return err;
9506}
9507
Matt Carlson9e9fd122009-01-19 16:57:45 -08009508static int tg3_request_firmware(struct tg3 *tp)
9509{
9510 const __be32 *fw_data;
9511
9512 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009513 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
9514 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009515 return -ENOENT;
9516 }
9517
9518 fw_data = (void *)tp->fw->data;
9519
9520 /* Firmware blob starts with version numbers, followed by
9521 * start address and _full_ length including BSS sections
9522 * (which must be longer than the actual data, of course
9523 */
9524
9525 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
9526 if (tp->fw_len < (tp->fw->size - 12)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009527 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
9528 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009529 release_firmware(tp->fw);
9530 tp->fw = NULL;
9531 return -EINVAL;
9532 }
9533
9534 /* We no longer need firmware; we have it. */
9535 tp->fw_needed = NULL;
9536 return 0;
9537}
9538
Matt Carlson679563f2009-09-01 12:55:46 +00009539static bool tg3_enable_msix(struct tg3 *tp)
9540{
9541 int i, rc, cpus = num_online_cpus();
9542 struct msix_entry msix_ent[tp->irq_max];
9543
9544 if (cpus == 1)
9545 /* Just fallback to the simpler MSI mode. */
9546 return false;
9547
9548 /*
9549 * We want as many rx rings enabled as there are cpus.
9550 * The first MSIX vector only deals with link interrupts, etc,
9551 * so we add one to the number of vectors we are requesting.
9552 */
9553 tp->irq_cnt = min_t(unsigned, cpus + 1, tp->irq_max);
9554
9555 for (i = 0; i < tp->irq_max; i++) {
9556 msix_ent[i].entry = i;
9557 msix_ent[i].vector = 0;
9558 }
9559
9560 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +00009561 if (rc < 0) {
9562 return false;
9563 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +00009564 if (pci_enable_msix(tp->pdev, msix_ent, rc))
9565 return false;
Joe Perches05dbe002010-02-17 19:44:19 +00009566 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
9567 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +00009568 tp->irq_cnt = rc;
9569 }
9570
9571 for (i = 0; i < tp->irq_max; i++)
9572 tp->napi[i].irq_vec = msix_ent[i].vector;
9573
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009574 netif_set_real_num_tx_queues(tp->dev, 1);
9575 rc = tp->irq_cnt > 1 ? tp->irq_cnt - 1 : 1;
9576 if (netif_set_real_num_rx_queues(tp->dev, rc)) {
9577 pci_disable_msix(tp->pdev);
9578 return false;
9579 }
Matt Carlsonb92b9042010-11-24 08:31:51 +00009580
9581 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +00009582 tg3_flag_set(tp, ENABLE_RSS);
Matt Carlsond78b59f2011-04-05 14:22:46 +00009583
9584 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
9585 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Joe Perches63c3a662011-04-26 08:12:10 +00009586 tg3_flag_set(tp, ENABLE_TSS);
Matt Carlsonb92b9042010-11-24 08:31:51 +00009587 netif_set_real_num_tx_queues(tp->dev, tp->irq_cnt - 1);
9588 }
9589 }
Matt Carlson2430b032010-06-05 17:24:34 +00009590
Matt Carlson679563f2009-09-01 12:55:46 +00009591 return true;
9592}
9593
Matt Carlson07b01732009-08-28 14:01:15 +00009594static void tg3_ints_init(struct tg3 *tp)
9595{
Joe Perches63c3a662011-04-26 08:12:10 +00009596 if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
9597 !tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +00009598 /* All MSI supporting chips should support tagged
9599 * status. Assert that this is the case.
9600 */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009601 netdev_warn(tp->dev,
9602 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +00009603 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +00009604 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009605
Joe Perches63c3a662011-04-26 08:12:10 +00009606 if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
9607 tg3_flag_set(tp, USING_MSIX);
9608 else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
9609 tg3_flag_set(tp, USING_MSI);
Matt Carlson679563f2009-09-01 12:55:46 +00009610
Joe Perches63c3a662011-04-26 08:12:10 +00009611 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009612 u32 msi_mode = tr32(MSGINT_MODE);
Joe Perches63c3a662011-04-26 08:12:10 +00009613 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009614 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +00009615 if (!tg3_flag(tp, 1SHOT_MSI))
9616 msi_mode |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlson679563f2009-09-01 12:55:46 +00009617 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
9618 }
9619defcfg:
Joe Perches63c3a662011-04-26 08:12:10 +00009620 if (!tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009621 tp->irq_cnt = 1;
9622 tp->napi[0].irq_vec = tp->pdev->irq;
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009623 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -07009624 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +00009625 }
Matt Carlson07b01732009-08-28 14:01:15 +00009626}
9627
9628static void tg3_ints_fini(struct tg3 *tp)
9629{
Joe Perches63c3a662011-04-26 08:12:10 +00009630 if (tg3_flag(tp, USING_MSIX))
Matt Carlson679563f2009-09-01 12:55:46 +00009631 pci_disable_msix(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009632 else if (tg3_flag(tp, USING_MSI))
Matt Carlson679563f2009-09-01 12:55:46 +00009633 pci_disable_msi(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009634 tg3_flag_clear(tp, USING_MSI);
9635 tg3_flag_clear(tp, USING_MSIX);
9636 tg3_flag_clear(tp, ENABLE_RSS);
9637 tg3_flag_clear(tp, ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +00009638}
9639
Linus Torvalds1da177e2005-04-16 15:20:36 -07009640static int tg3_open(struct net_device *dev)
9641{
9642 struct tg3 *tp = netdev_priv(dev);
Matt Carlson4f125f42009-09-01 12:55:02 +00009643 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009644
Matt Carlson9e9fd122009-01-19 16:57:45 -08009645 if (tp->fw_needed) {
9646 err = tg3_request_firmware(tp);
9647 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
9648 if (err)
9649 return err;
9650 } else if (err) {
Joe Perches05dbe002010-02-17 19:44:19 +00009651 netdev_warn(tp->dev, "TSO capability disabled\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009652 tg3_flag_clear(tp, TSO_CAPABLE);
9653 } else if (!tg3_flag(tp, TSO_CAPABLE)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009654 netdev_notice(tp->dev, "TSO capability restored\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009655 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009656 }
9657 }
9658
Michael Chanc49a1562006-12-17 17:07:29 -08009659 netif_carrier_off(tp->dev);
9660
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009661 err = tg3_power_up(tp);
Matt Carlson2f751b62008-08-04 23:17:34 -07009662 if (err)
Michael Chanbc1c7562006-03-20 17:48:03 -08009663 return err;
Matt Carlson2f751b62008-08-04 23:17:34 -07009664
9665 tg3_full_lock(tp, 0);
Michael Chanbc1c7562006-03-20 17:48:03 -08009666
Linus Torvalds1da177e2005-04-16 15:20:36 -07009667 tg3_disable_ints(tp);
Joe Perches63c3a662011-04-26 08:12:10 +00009668 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009669
David S. Millerf47c11e2005-06-24 20:18:35 -07009670 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009671
Matt Carlson679563f2009-09-01 12:55:46 +00009672 /*
9673 * Setup interrupts first so we know how
9674 * many NAPI resources to allocate
9675 */
9676 tg3_ints_init(tp);
9677
Linus Torvalds1da177e2005-04-16 15:20:36 -07009678 /* The placement of this call is tied
9679 * to the setup and use of Host TX descriptors.
9680 */
9681 err = tg3_alloc_consistent(tp);
9682 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009683 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009684
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009685 tg3_napi_init(tp);
9686
Matt Carlsonfed97812009-09-01 13:10:19 +00009687 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07009688
Matt Carlson4f125f42009-09-01 12:55:02 +00009689 for (i = 0; i < tp->irq_cnt; i++) {
9690 struct tg3_napi *tnapi = &tp->napi[i];
9691 err = tg3_request_irq(tp, i);
9692 if (err) {
Matt Carlson5bc09182011-11-04 09:15:01 +00009693 for (i--; i >= 0; i--) {
9694 tnapi = &tp->napi[i];
Matt Carlson4f125f42009-09-01 12:55:02 +00009695 free_irq(tnapi->irq_vec, tnapi);
Matt Carlson5bc09182011-11-04 09:15:01 +00009696 }
9697 goto err_out2;
Matt Carlson4f125f42009-09-01 12:55:02 +00009698 }
9699 }
Matt Carlson07b01732009-08-28 14:01:15 +00009700
David S. Millerf47c11e2005-06-24 20:18:35 -07009701 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009702
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009703 err = tg3_init_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009704 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -07009705 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009706 tg3_free_rings(tp);
9707 } else {
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009708 if (tg3_flag(tp, TAGGED_STATUS) &&
Matt Carlson55086ad2011-12-14 11:09:59 +00009709 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
9710 !tg3_flag(tp, 57765_CLASS))
David S. Millerfac9b832005-05-18 22:46:34 -07009711 tp->timer_offset = HZ;
9712 else
9713 tp->timer_offset = HZ / 10;
9714
9715 BUG_ON(tp->timer_offset > HZ);
9716 tp->timer_counter = tp->timer_multiplier =
9717 (HZ / tp->timer_offset);
9718 tp->asf_counter = tp->asf_multiplier =
Michael Chan28fbef72005-10-26 15:48:35 -07009719 ((HZ / tp->timer_offset) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009720
9721 init_timer(&tp->timer);
9722 tp->timer.expires = jiffies + tp->timer_offset;
9723 tp->timer.data = (unsigned long) tp;
9724 tp->timer.function = tg3_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009725 }
9726
David S. Millerf47c11e2005-06-24 20:18:35 -07009727 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009728
Matt Carlson07b01732009-08-28 14:01:15 +00009729 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009730 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009731
Joe Perches63c3a662011-04-26 08:12:10 +00009732 if (tg3_flag(tp, USING_MSI)) {
Michael Chan79381092005-04-21 17:13:59 -07009733 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -07009734
Michael Chan79381092005-04-21 17:13:59 -07009735 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -07009736 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -07009737 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -07009738 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07009739 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009740
Matt Carlson679563f2009-09-01 12:55:46 +00009741 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -07009742 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009743
Joe Perches63c3a662011-04-26 08:12:10 +00009744 if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009745 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009746
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009747 tw32(PCIE_TRANSACTION_CFG,
9748 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009749 }
Michael Chan79381092005-04-21 17:13:59 -07009750 }
9751
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009752 tg3_phy_start(tp);
9753
David S. Millerf47c11e2005-06-24 20:18:35 -07009754 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009755
Michael Chan79381092005-04-21 17:13:59 -07009756 add_timer(&tp->timer);
Joe Perches63c3a662011-04-26 08:12:10 +00009757 tg3_flag_set(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009758 tg3_enable_ints(tp);
9759
David S. Millerf47c11e2005-06-24 20:18:35 -07009760 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009761
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009762 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009763
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00009764 /*
9765 * Reset loopback feature if it was turned on while the device was down
9766 * make sure that it's installed properly now.
9767 */
9768 if (dev->features & NETIF_F_LOOPBACK)
9769 tg3_set_loopback(dev, dev->features);
9770
Linus Torvalds1da177e2005-04-16 15:20:36 -07009771 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +00009772
Matt Carlson679563f2009-09-01 12:55:46 +00009773err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +00009774 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9775 struct tg3_napi *tnapi = &tp->napi[i];
9776 free_irq(tnapi->irq_vec, tnapi);
9777 }
Matt Carlson07b01732009-08-28 14:01:15 +00009778
Matt Carlson679563f2009-09-01 12:55:46 +00009779err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +00009780 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009781 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00009782 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +00009783
9784err_out1:
9785 tg3_ints_fini(tp);
Matt Carlsoncd0d7222011-07-13 09:27:33 +00009786 tg3_frob_aux_power(tp, false);
9787 pci_set_power_state(tp->pdev, PCI_D3hot);
Matt Carlson07b01732009-08-28 14:01:15 +00009788 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009789}
9790
Linus Torvalds1da177e2005-04-16 15:20:36 -07009791static int tg3_close(struct net_device *dev)
9792{
Matt Carlson4f125f42009-09-01 12:55:02 +00009793 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009794 struct tg3 *tp = netdev_priv(dev);
9795
Matt Carlsonfed97812009-09-01 13:10:19 +00009796 tg3_napi_disable(tp);
Matt Carlsondb219972011-11-04 09:15:03 +00009797 tg3_reset_task_cancel(tp);
Michael Chan7faa0062006-02-02 17:29:28 -08009798
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009799 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009800
9801 del_timer_sync(&tp->timer);
9802
Matt Carlson24bb4fb2009-10-05 17:55:29 +00009803 tg3_phy_stop(tp);
9804
David S. Millerf47c11e2005-06-24 20:18:35 -07009805 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009806
9807 tg3_disable_ints(tp);
9808
Michael Chan944d9802005-05-29 14:57:48 -07009809 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009810 tg3_free_rings(tp);
Joe Perches63c3a662011-04-26 08:12:10 +00009811 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009812
David S. Millerf47c11e2005-06-24 20:18:35 -07009813 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009814
Matt Carlson4f125f42009-09-01 12:55:02 +00009815 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9816 struct tg3_napi *tnapi = &tp->napi[i];
9817 free_irq(tnapi->irq_vec, tnapi);
9818 }
Matt Carlson07b01732009-08-28 14:01:15 +00009819
9820 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009821
Matt Carlson92feeab2011-12-08 14:40:14 +00009822 /* Clear stats across close / open calls */
9823 memset(&tp->net_stats_prev, 0, sizeof(tp->net_stats_prev));
9824 memset(&tp->estats_prev, 0, sizeof(tp->estats_prev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07009825
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009826 tg3_napi_fini(tp);
9827
Linus Torvalds1da177e2005-04-16 15:20:36 -07009828 tg3_free_consistent(tp);
9829
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009830 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -08009831
9832 netif_carrier_off(tp->dev);
9833
Linus Torvalds1da177e2005-04-16 15:20:36 -07009834 return 0;
9835}
9836
Eric Dumazet511d2222010-07-07 20:44:24 +00009837static inline u64 get_stat64(tg3_stat64_t *val)
Stefan Buehler816f8b82008-08-15 14:10:54 -07009838{
9839 return ((u64)val->high << 32) | ((u64)val->low);
9840}
9841
Eric Dumazet511d2222010-07-07 20:44:24 +00009842static u64 calc_crc_errors(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009843{
9844 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9845
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009846 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009847 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
9848 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009849 u32 val;
9850
David S. Millerf47c11e2005-06-24 20:18:35 -07009851 spin_lock_bh(&tp->lock);
Michael Chan569a5df2007-02-13 12:18:15 -08009852 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
9853 tg3_writephy(tp, MII_TG3_TEST1,
9854 val | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009855 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009856 } else
9857 val = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07009858 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009859
9860 tp->phy_crc_errors += val;
9861
9862 return tp->phy_crc_errors;
9863 }
9864
9865 return get_stat64(&hw_stats->rx_fcs_errors);
9866}
9867
9868#define ESTAT_ADD(member) \
9869 estats->member = old_estats->member + \
Eric Dumazet511d2222010-07-07 20:44:24 +00009870 get_stat64(&hw_stats->member)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009871
Matt Carlson0e6c9da2011-12-08 14:40:13 +00009872static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *tp,
9873 struct tg3_ethtool_stats *estats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009874{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009875 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
9876 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9877
9878 if (!hw_stats)
9879 return old_estats;
9880
9881 ESTAT_ADD(rx_octets);
9882 ESTAT_ADD(rx_fragments);
9883 ESTAT_ADD(rx_ucast_packets);
9884 ESTAT_ADD(rx_mcast_packets);
9885 ESTAT_ADD(rx_bcast_packets);
9886 ESTAT_ADD(rx_fcs_errors);
9887 ESTAT_ADD(rx_align_errors);
9888 ESTAT_ADD(rx_xon_pause_rcvd);
9889 ESTAT_ADD(rx_xoff_pause_rcvd);
9890 ESTAT_ADD(rx_mac_ctrl_rcvd);
9891 ESTAT_ADD(rx_xoff_entered);
9892 ESTAT_ADD(rx_frame_too_long_errors);
9893 ESTAT_ADD(rx_jabbers);
9894 ESTAT_ADD(rx_undersize_packets);
9895 ESTAT_ADD(rx_in_length_errors);
9896 ESTAT_ADD(rx_out_length_errors);
9897 ESTAT_ADD(rx_64_or_less_octet_packets);
9898 ESTAT_ADD(rx_65_to_127_octet_packets);
9899 ESTAT_ADD(rx_128_to_255_octet_packets);
9900 ESTAT_ADD(rx_256_to_511_octet_packets);
9901 ESTAT_ADD(rx_512_to_1023_octet_packets);
9902 ESTAT_ADD(rx_1024_to_1522_octet_packets);
9903 ESTAT_ADD(rx_1523_to_2047_octet_packets);
9904 ESTAT_ADD(rx_2048_to_4095_octet_packets);
9905 ESTAT_ADD(rx_4096_to_8191_octet_packets);
9906 ESTAT_ADD(rx_8192_to_9022_octet_packets);
9907
9908 ESTAT_ADD(tx_octets);
9909 ESTAT_ADD(tx_collisions);
9910 ESTAT_ADD(tx_xon_sent);
9911 ESTAT_ADD(tx_xoff_sent);
9912 ESTAT_ADD(tx_flow_control);
9913 ESTAT_ADD(tx_mac_errors);
9914 ESTAT_ADD(tx_single_collisions);
9915 ESTAT_ADD(tx_mult_collisions);
9916 ESTAT_ADD(tx_deferred);
9917 ESTAT_ADD(tx_excessive_collisions);
9918 ESTAT_ADD(tx_late_collisions);
9919 ESTAT_ADD(tx_collide_2times);
9920 ESTAT_ADD(tx_collide_3times);
9921 ESTAT_ADD(tx_collide_4times);
9922 ESTAT_ADD(tx_collide_5times);
9923 ESTAT_ADD(tx_collide_6times);
9924 ESTAT_ADD(tx_collide_7times);
9925 ESTAT_ADD(tx_collide_8times);
9926 ESTAT_ADD(tx_collide_9times);
9927 ESTAT_ADD(tx_collide_10times);
9928 ESTAT_ADD(tx_collide_11times);
9929 ESTAT_ADD(tx_collide_12times);
9930 ESTAT_ADD(tx_collide_13times);
9931 ESTAT_ADD(tx_collide_14times);
9932 ESTAT_ADD(tx_collide_15times);
9933 ESTAT_ADD(tx_ucast_packets);
9934 ESTAT_ADD(tx_mcast_packets);
9935 ESTAT_ADD(tx_bcast_packets);
9936 ESTAT_ADD(tx_carrier_sense_errors);
9937 ESTAT_ADD(tx_discards);
9938 ESTAT_ADD(tx_errors);
9939
9940 ESTAT_ADD(dma_writeq_full);
9941 ESTAT_ADD(dma_write_prioq_full);
9942 ESTAT_ADD(rxbds_empty);
9943 ESTAT_ADD(rx_discards);
9944 ESTAT_ADD(rx_errors);
9945 ESTAT_ADD(rx_threshold_hit);
9946
9947 ESTAT_ADD(dma_readq_full);
9948 ESTAT_ADD(dma_read_prioq_full);
9949 ESTAT_ADD(tx_comp_queue_full);
9950
9951 ESTAT_ADD(ring_set_send_prod_index);
9952 ESTAT_ADD(ring_status_update);
9953 ESTAT_ADD(nic_irqs);
9954 ESTAT_ADD(nic_avoided_irqs);
9955 ESTAT_ADD(nic_tx_threshold_hit);
9956
Matt Carlson4452d092011-05-19 12:12:51 +00009957 ESTAT_ADD(mbuf_lwm_thresh_hit);
9958
Linus Torvalds1da177e2005-04-16 15:20:36 -07009959 return estats;
9960}
9961
Eric Dumazet511d2222010-07-07 20:44:24 +00009962static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
9963 struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009964{
9965 struct tg3 *tp = netdev_priv(dev);
Eric Dumazet511d2222010-07-07 20:44:24 +00009966 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009967 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9968
9969 if (!hw_stats)
9970 return old_stats;
9971
9972 stats->rx_packets = old_stats->rx_packets +
9973 get_stat64(&hw_stats->rx_ucast_packets) +
9974 get_stat64(&hw_stats->rx_mcast_packets) +
9975 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009976
Linus Torvalds1da177e2005-04-16 15:20:36 -07009977 stats->tx_packets = old_stats->tx_packets +
9978 get_stat64(&hw_stats->tx_ucast_packets) +
9979 get_stat64(&hw_stats->tx_mcast_packets) +
9980 get_stat64(&hw_stats->tx_bcast_packets);
9981
9982 stats->rx_bytes = old_stats->rx_bytes +
9983 get_stat64(&hw_stats->rx_octets);
9984 stats->tx_bytes = old_stats->tx_bytes +
9985 get_stat64(&hw_stats->tx_octets);
9986
9987 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -07009988 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009989 stats->tx_errors = old_stats->tx_errors +
9990 get_stat64(&hw_stats->tx_errors) +
9991 get_stat64(&hw_stats->tx_mac_errors) +
9992 get_stat64(&hw_stats->tx_carrier_sense_errors) +
9993 get_stat64(&hw_stats->tx_discards);
9994
9995 stats->multicast = old_stats->multicast +
9996 get_stat64(&hw_stats->rx_mcast_packets);
9997 stats->collisions = old_stats->collisions +
9998 get_stat64(&hw_stats->tx_collisions);
9999
10000 stats->rx_length_errors = old_stats->rx_length_errors +
10001 get_stat64(&hw_stats->rx_frame_too_long_errors) +
10002 get_stat64(&hw_stats->rx_undersize_packets);
10003
10004 stats->rx_over_errors = old_stats->rx_over_errors +
10005 get_stat64(&hw_stats->rxbds_empty);
10006 stats->rx_frame_errors = old_stats->rx_frame_errors +
10007 get_stat64(&hw_stats->rx_align_errors);
10008 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
10009 get_stat64(&hw_stats->tx_discards);
10010 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
10011 get_stat64(&hw_stats->tx_carrier_sense_errors);
10012
10013 stats->rx_crc_errors = old_stats->rx_crc_errors +
10014 calc_crc_errors(tp);
10015
John W. Linville4f63b872005-09-12 14:43:18 -070010016 stats->rx_missed_errors = old_stats->rx_missed_errors +
10017 get_stat64(&hw_stats->rx_discards);
10018
Eric Dumazetb0057c52010-10-10 19:55:52 +000010019 stats->rx_dropped = tp->rx_dropped;
Eric Dumazet48855432011-10-24 07:53:03 +000010020 stats->tx_dropped = tp->tx_dropped;
Eric Dumazetb0057c52010-10-10 19:55:52 +000010021
Linus Torvalds1da177e2005-04-16 15:20:36 -070010022 return stats;
10023}
10024
10025static inline u32 calc_crc(unsigned char *buf, int len)
10026{
10027 u32 reg;
10028 u32 tmp;
10029 int j, k;
10030
10031 reg = 0xffffffff;
10032
10033 for (j = 0; j < len; j++) {
10034 reg ^= buf[j];
10035
10036 for (k = 0; k < 8; k++) {
10037 tmp = reg & 0x01;
10038
10039 reg >>= 1;
10040
Matt Carlson859a5882010-04-05 10:19:28 +000010041 if (tmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010042 reg ^= 0xedb88320;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010043 }
10044 }
10045
10046 return ~reg;
10047}
10048
10049static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
10050{
10051 /* accept or reject all multicast frames */
10052 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
10053 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
10054 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
10055 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
10056}
10057
10058static void __tg3_set_rx_mode(struct net_device *dev)
10059{
10060 struct tg3 *tp = netdev_priv(dev);
10061 u32 rx_mode;
10062
10063 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
10064 RX_MODE_KEEP_VLAN_TAG);
10065
Matt Carlsonbf933c82011-01-25 15:58:49 +000010066#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010067 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
10068 * flag clear.
10069 */
Joe Perches63c3a662011-04-26 08:12:10 +000010070 if (!tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010071 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
10072#endif
10073
10074 if (dev->flags & IFF_PROMISC) {
10075 /* Promiscuous mode. */
10076 rx_mode |= RX_MODE_PROMISC;
10077 } else if (dev->flags & IFF_ALLMULTI) {
10078 /* Accept all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010079 tg3_set_multi(tp, 1);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +000010080 } else if (netdev_mc_empty(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010081 /* Reject all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010082 tg3_set_multi(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010083 } else {
10084 /* Accept one or more multicast(s). */
Jiri Pirko22bedad2010-04-01 21:22:57 +000010085 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010086 u32 mc_filter[4] = { 0, };
10087 u32 regidx;
10088 u32 bit;
10089 u32 crc;
10090
Jiri Pirko22bedad2010-04-01 21:22:57 +000010091 netdev_for_each_mc_addr(ha, dev) {
10092 crc = calc_crc(ha->addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010093 bit = ~crc & 0x7f;
10094 regidx = (bit & 0x60) >> 5;
10095 bit &= 0x1f;
10096 mc_filter[regidx] |= (1 << bit);
10097 }
10098
10099 tw32(MAC_HASH_REG_0, mc_filter[0]);
10100 tw32(MAC_HASH_REG_1, mc_filter[1]);
10101 tw32(MAC_HASH_REG_2, mc_filter[2]);
10102 tw32(MAC_HASH_REG_3, mc_filter[3]);
10103 }
10104
10105 if (rx_mode != tp->rx_mode) {
10106 tp->rx_mode = rx_mode;
10107 tw32_f(MAC_RX_MODE, rx_mode);
10108 udelay(10);
10109 }
10110}
10111
10112static void tg3_set_rx_mode(struct net_device *dev)
10113{
10114 struct tg3 *tp = netdev_priv(dev);
10115
Michael Chane75f7c92006-03-20 21:33:26 -080010116 if (!netif_running(dev))
10117 return;
10118
David S. Millerf47c11e2005-06-24 20:18:35 -070010119 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010120 __tg3_set_rx_mode(dev);
David S. Millerf47c11e2005-06-24 20:18:35 -070010121 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010122}
10123
Linus Torvalds1da177e2005-04-16 15:20:36 -070010124static int tg3_get_regs_len(struct net_device *dev)
10125{
Matt Carlson97bd8e42011-04-13 11:05:04 +000010126 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010127}
10128
10129static void tg3_get_regs(struct net_device *dev,
10130 struct ethtool_regs *regs, void *_p)
10131{
Linus Torvalds1da177e2005-04-16 15:20:36 -070010132 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010133
10134 regs->version = 0;
10135
Matt Carlson97bd8e42011-04-13 11:05:04 +000010136 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010137
Matt Carlson80096062010-08-02 11:26:06 +000010138 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010139 return;
10140
David S. Millerf47c11e2005-06-24 20:18:35 -070010141 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010142
Matt Carlson97bd8e42011-04-13 11:05:04 +000010143 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010144
David S. Millerf47c11e2005-06-24 20:18:35 -070010145 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010146}
10147
10148static int tg3_get_eeprom_len(struct net_device *dev)
10149{
10150 struct tg3 *tp = netdev_priv(dev);
10151
10152 return tp->nvram_size;
10153}
10154
Linus Torvalds1da177e2005-04-16 15:20:36 -070010155static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
10156{
10157 struct tg3 *tp = netdev_priv(dev);
10158 int ret;
10159 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -080010160 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010161 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010162
Joe Perches63c3a662011-04-26 08:12:10 +000010163 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000010164 return -EINVAL;
10165
Matt Carlson80096062010-08-02 11:26:06 +000010166 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010167 return -EAGAIN;
10168
Linus Torvalds1da177e2005-04-16 15:20:36 -070010169 offset = eeprom->offset;
10170 len = eeprom->len;
10171 eeprom->len = 0;
10172
10173 eeprom->magic = TG3_EEPROM_MAGIC;
10174
10175 if (offset & 3) {
10176 /* adjustments to start on required 4 byte boundary */
10177 b_offset = offset & 3;
10178 b_count = 4 - b_offset;
10179 if (b_count > len) {
10180 /* i.e. offset=1 len=2 */
10181 b_count = len;
10182 }
Matt Carlsona9dc5292009-02-25 14:25:30 +000010183 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010184 if (ret)
10185 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +000010186 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010187 len -= b_count;
10188 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010189 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010190 }
10191
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010192 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010193 pd = &data[eeprom->len];
10194 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010195 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010196 if (ret) {
10197 eeprom->len += i;
10198 return ret;
10199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010200 memcpy(pd + i, &val, 4);
10201 }
10202 eeprom->len += i;
10203
10204 if (len & 3) {
10205 /* read last bytes not ending on 4 byte boundary */
10206 pd = &data[eeprom->len];
10207 b_count = len & 3;
10208 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010209 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010210 if (ret)
10211 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080010212 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010213 eeprom->len += b_count;
10214 }
10215 return 0;
10216}
10217
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010218static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010219
10220static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
10221{
10222 struct tg3 *tp = netdev_priv(dev);
10223 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080010224 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010225 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010226 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010227
Matt Carlson80096062010-08-02 11:26:06 +000010228 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010229 return -EAGAIN;
10230
Joe Perches63c3a662011-04-26 08:12:10 +000010231 if (tg3_flag(tp, NO_NVRAM) ||
Matt Carlsondf259d82009-04-20 06:57:14 +000010232 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010233 return -EINVAL;
10234
10235 offset = eeprom->offset;
10236 len = eeprom->len;
10237
10238 if ((b_offset = (offset & 3))) {
10239 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010240 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010241 if (ret)
10242 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010243 len += b_offset;
10244 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -070010245 if (len < 4)
10246 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010247 }
10248
10249 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -070010250 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010251 /* adjustments to end on required 4 byte boundary */
10252 odd_len = 1;
10253 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010254 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010255 if (ret)
10256 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010257 }
10258
10259 buf = data;
10260 if (b_offset || odd_len) {
10261 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010262 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010263 return -ENOMEM;
10264 if (b_offset)
10265 memcpy(buf, &start, 4);
10266 if (odd_len)
10267 memcpy(buf+len-4, &end, 4);
10268 memcpy(buf + b_offset, data, eeprom->len);
10269 }
10270
10271 ret = tg3_nvram_write_block(tp, offset, len, buf);
10272
10273 if (buf != data)
10274 kfree(buf);
10275
10276 return ret;
10277}
10278
10279static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
10280{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010281 struct tg3 *tp = netdev_priv(dev);
10282
Joe Perches63c3a662011-04-26 08:12:10 +000010283 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010284 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010285 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010286 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010287 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10288 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010289 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010290
Linus Torvalds1da177e2005-04-16 15:20:36 -070010291 cmd->supported = (SUPPORTED_Autoneg);
10292
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010293 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010294 cmd->supported |= (SUPPORTED_1000baseT_Half |
10295 SUPPORTED_1000baseT_Full);
10296
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010297 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010298 cmd->supported |= (SUPPORTED_100baseT_Half |
10299 SUPPORTED_100baseT_Full |
10300 SUPPORTED_10baseT_Half |
10301 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -080010302 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -070010303 cmd->port = PORT_TP;
10304 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010305 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -070010306 cmd->port = PORT_FIBRE;
10307 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010308
Linus Torvalds1da177e2005-04-16 15:20:36 -070010309 cmd->advertising = tp->link_config.advertising;
Matt Carlson5bb09772011-06-13 13:39:00 +000010310 if (tg3_flag(tp, PAUSE_AUTONEG)) {
10311 if (tp->link_config.flowctrl & FLOW_CTRL_RX) {
10312 if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
10313 cmd->advertising |= ADVERTISED_Pause;
10314 } else {
10315 cmd->advertising |= ADVERTISED_Pause |
10316 ADVERTISED_Asym_Pause;
10317 }
10318 } else if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
10319 cmd->advertising |= ADVERTISED_Asym_Pause;
10320 }
10321 }
Matt Carlson859edb22011-12-08 14:40:16 +000010322 if (netif_running(dev) && netif_carrier_ok(dev)) {
David Decotigny70739492011-04-27 18:32:40 +000010323 ethtool_cmd_speed_set(cmd, tp->link_config.active_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010324 cmd->duplex = tp->link_config.active_duplex;
Matt Carlson859edb22011-12-08 14:40:16 +000010325 cmd->lp_advertising = tp->link_config.rmt_adv;
Matt Carlsone348c5e2011-11-21 15:01:20 +000010326 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
10327 if (tp->phy_flags & TG3_PHYFLG_MDIX_STATE)
10328 cmd->eth_tp_mdix = ETH_TP_MDI_X;
10329 else
10330 cmd->eth_tp_mdix = ETH_TP_MDI;
10331 }
Matt Carlson64c22182010-10-14 10:37:44 +000010332 } else {
David Decotigny70739492011-04-27 18:32:40 +000010333 ethtool_cmd_speed_set(cmd, SPEED_INVALID);
Matt Carlson64c22182010-10-14 10:37:44 +000010334 cmd->duplex = DUPLEX_INVALID;
Matt Carlsone348c5e2011-11-21 15:01:20 +000010335 cmd->eth_tp_mdix = ETH_TP_MDI_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010336 }
Matt Carlson882e9792009-09-01 13:21:36 +000010337 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +000010338 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010339 cmd->autoneg = tp->link_config.autoneg;
10340 cmd->maxtxpkt = 0;
10341 cmd->maxrxpkt = 0;
10342 return 0;
10343}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010344
Linus Torvalds1da177e2005-04-16 15:20:36 -070010345static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
10346{
10347 struct tg3 *tp = netdev_priv(dev);
David Decotigny25db0332011-04-27 18:32:39 +000010348 u32 speed = ethtool_cmd_speed(cmd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010349
Joe Perches63c3a662011-04-26 08:12:10 +000010350 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010351 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010352 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010353 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010354 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10355 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010356 }
10357
Matt Carlson7e5856b2009-02-25 14:23:01 +000010358 if (cmd->autoneg != AUTONEG_ENABLE &&
10359 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -070010360 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +000010361
10362 if (cmd->autoneg == AUTONEG_DISABLE &&
10363 cmd->duplex != DUPLEX_FULL &&
10364 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -070010365 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010366
Matt Carlson7e5856b2009-02-25 14:23:01 +000010367 if (cmd->autoneg == AUTONEG_ENABLE) {
10368 u32 mask = ADVERTISED_Autoneg |
10369 ADVERTISED_Pause |
10370 ADVERTISED_Asym_Pause;
10371
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010372 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010373 mask |= ADVERTISED_1000baseT_Half |
10374 ADVERTISED_1000baseT_Full;
10375
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010376 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010377 mask |= ADVERTISED_100baseT_Half |
10378 ADVERTISED_100baseT_Full |
10379 ADVERTISED_10baseT_Half |
10380 ADVERTISED_10baseT_Full |
10381 ADVERTISED_TP;
10382 else
10383 mask |= ADVERTISED_FIBRE;
10384
10385 if (cmd->advertising & ~mask)
10386 return -EINVAL;
10387
10388 mask &= (ADVERTISED_1000baseT_Half |
10389 ADVERTISED_1000baseT_Full |
10390 ADVERTISED_100baseT_Half |
10391 ADVERTISED_100baseT_Full |
10392 ADVERTISED_10baseT_Half |
10393 ADVERTISED_10baseT_Full);
10394
10395 cmd->advertising &= mask;
10396 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010397 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
David Decotigny25db0332011-04-27 18:32:39 +000010398 if (speed != SPEED_1000)
Matt Carlson7e5856b2009-02-25 14:23:01 +000010399 return -EINVAL;
10400
10401 if (cmd->duplex != DUPLEX_FULL)
10402 return -EINVAL;
10403 } else {
David Decotigny25db0332011-04-27 18:32:39 +000010404 if (speed != SPEED_100 &&
10405 speed != SPEED_10)
Matt Carlson7e5856b2009-02-25 14:23:01 +000010406 return -EINVAL;
10407 }
10408 }
10409
David S. Millerf47c11e2005-06-24 20:18:35 -070010410 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010411
10412 tp->link_config.autoneg = cmd->autoneg;
10413 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -070010414 tp->link_config.advertising = (cmd->advertising |
10415 ADVERTISED_Autoneg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010416 tp->link_config.speed = SPEED_INVALID;
10417 tp->link_config.duplex = DUPLEX_INVALID;
10418 } else {
10419 tp->link_config.advertising = 0;
David Decotigny25db0332011-04-27 18:32:39 +000010420 tp->link_config.speed = speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010421 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010422 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010423
Michael Chan24fcad62006-12-17 17:06:46 -080010424 tp->link_config.orig_speed = tp->link_config.speed;
10425 tp->link_config.orig_duplex = tp->link_config.duplex;
10426 tp->link_config.orig_autoneg = tp->link_config.autoneg;
10427
Linus Torvalds1da177e2005-04-16 15:20:36 -070010428 if (netif_running(dev))
10429 tg3_setup_phy(tp, 1);
10430
David S. Millerf47c11e2005-06-24 20:18:35 -070010431 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010432
Linus Torvalds1da177e2005-04-16 15:20:36 -070010433 return 0;
10434}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010435
Linus Torvalds1da177e2005-04-16 15:20:36 -070010436static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
10437{
10438 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010439
Rick Jones68aad782011-11-07 13:29:27 +000010440 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
10441 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
10442 strlcpy(info->fw_version, tp->fw_ver, sizeof(info->fw_version));
10443 strlcpy(info->bus_info, pci_name(tp->pdev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010444}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010445
Linus Torvalds1da177e2005-04-16 15:20:36 -070010446static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10447{
10448 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010449
Joe Perches63c3a662011-04-26 08:12:10 +000010450 if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070010451 wol->supported = WAKE_MAGIC;
10452 else
10453 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010454 wol->wolopts = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010455 if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010456 wol->wolopts = WAKE_MAGIC;
10457 memset(&wol->sopass, 0, sizeof(wol->sopass));
10458}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010459
Linus Torvalds1da177e2005-04-16 15:20:36 -070010460static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10461{
10462 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070010463 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010464
Linus Torvalds1da177e2005-04-16 15:20:36 -070010465 if (wol->wolopts & ~WAKE_MAGIC)
10466 return -EINVAL;
10467 if ((wol->wolopts & WAKE_MAGIC) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010468 !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010469 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010470
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010471 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
10472
David S. Millerf47c11e2005-06-24 20:18:35 -070010473 spin_lock_bh(&tp->lock);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010474 if (device_may_wakeup(dp))
Joe Perches63c3a662011-04-26 08:12:10 +000010475 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010476 else
Joe Perches63c3a662011-04-26 08:12:10 +000010477 tg3_flag_clear(tp, WOL_ENABLE);
David S. Millerf47c11e2005-06-24 20:18:35 -070010478 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010479
Linus Torvalds1da177e2005-04-16 15:20:36 -070010480 return 0;
10481}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010482
Linus Torvalds1da177e2005-04-16 15:20:36 -070010483static u32 tg3_get_msglevel(struct net_device *dev)
10484{
10485 struct tg3 *tp = netdev_priv(dev);
10486 return tp->msg_enable;
10487}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010488
Linus Torvalds1da177e2005-04-16 15:20:36 -070010489static void tg3_set_msglevel(struct net_device *dev, u32 value)
10490{
10491 struct tg3 *tp = netdev_priv(dev);
10492 tp->msg_enable = value;
10493}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010494
Linus Torvalds1da177e2005-04-16 15:20:36 -070010495static int tg3_nway_reset(struct net_device *dev)
10496{
10497 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010498 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010499
Linus Torvalds1da177e2005-04-16 15:20:36 -070010500 if (!netif_running(dev))
10501 return -EAGAIN;
10502
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010503 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070010504 return -EINVAL;
10505
Joe Perches63c3a662011-04-26 08:12:10 +000010506 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010507 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010508 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010509 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010510 } else {
10511 u32 bmcr;
10512
10513 spin_lock_bh(&tp->lock);
10514 r = -EINVAL;
10515 tg3_readphy(tp, MII_BMCR, &bmcr);
10516 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
10517 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010518 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010519 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
10520 BMCR_ANENABLE);
10521 r = 0;
10522 }
10523 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010524 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010525
Linus Torvalds1da177e2005-04-16 15:20:36 -070010526 return r;
10527}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010528
Linus Torvalds1da177e2005-04-16 15:20:36 -070010529static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10530{
10531 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010532
Matt Carlson2c49a442010-09-30 10:34:35 +000010533 ering->rx_max_pending = tp->rx_std_ring_mask;
Joe Perches63c3a662011-04-26 08:12:10 +000010534 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson2c49a442010-09-30 10:34:35 +000010535 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080010536 else
10537 ering->rx_jumbo_max_pending = 0;
10538
10539 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010540
10541 ering->rx_pending = tp->rx_pending;
Joe Perches63c3a662011-04-26 08:12:10 +000010542 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Michael Chan4f81c322006-03-20 21:33:42 -080010543 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
10544 else
10545 ering->rx_jumbo_pending = 0;
10546
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010547 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010548}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010549
Linus Torvalds1da177e2005-04-16 15:20:36 -070010550static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10551{
10552 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000010553 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010554
Matt Carlson2c49a442010-09-30 10:34:35 +000010555 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
10556 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070010557 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
10558 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Joe Perches63c3a662011-04-26 08:12:10 +000010559 (tg3_flag(tp, TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070010560 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010561 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010562
Michael Chanbbe832c2005-06-24 20:20:04 -070010563 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010564 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010565 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010566 irq_sync = 1;
10567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010568
Michael Chanbbe832c2005-06-24 20:20:04 -070010569 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010570
Linus Torvalds1da177e2005-04-16 15:20:36 -070010571 tp->rx_pending = ering->rx_pending;
10572
Joe Perches63c3a662011-04-26 08:12:10 +000010573 if (tg3_flag(tp, MAX_RXPEND_64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010574 tp->rx_pending > 63)
10575 tp->rx_pending = 63;
10576 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000010577
Matt Carlson6fd45cb2010-09-15 08:59:57 +000010578 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000010579 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010580
10581 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070010582 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -070010583 err = tg3_restart_hw(tp, 1);
10584 if (!err)
10585 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010586 }
10587
David S. Millerf47c11e2005-06-24 20:18:35 -070010588 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010589
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010590 if (irq_sync && !err)
10591 tg3_phy_start(tp);
10592
Michael Chanb9ec6c12006-07-25 16:37:27 -070010593 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010594}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010595
Linus Torvalds1da177e2005-04-16 15:20:36 -070010596static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10597{
10598 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010599
Joe Perches63c3a662011-04-26 08:12:10 +000010600 epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
Matt Carlson8d018622007-12-20 20:05:44 -080010601
Matt Carlson4a2db502011-12-08 14:40:17 +000010602 if (tp->link_config.flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080010603 epause->rx_pause = 1;
10604 else
10605 epause->rx_pause = 0;
10606
Matt Carlson4a2db502011-12-08 14:40:17 +000010607 if (tp->link_config.flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080010608 epause->tx_pause = 1;
10609 else
10610 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010611}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010612
Linus Torvalds1da177e2005-04-16 15:20:36 -070010613static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10614{
10615 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010616 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010617
Joe Perches63c3a662011-04-26 08:12:10 +000010618 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson27121682010-02-17 15:16:57 +000010619 u32 newadv;
10620 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010621
Matt Carlson27121682010-02-17 15:16:57 +000010622 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010623
Matt Carlson27121682010-02-17 15:16:57 +000010624 if (!(phydev->supported & SUPPORTED_Pause) ||
10625 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000010626 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000010627 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010628
Matt Carlson27121682010-02-17 15:16:57 +000010629 tp->link_config.flowctrl = 0;
10630 if (epause->rx_pause) {
10631 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010632
Matt Carlson27121682010-02-17 15:16:57 +000010633 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080010634 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000010635 newadv = ADVERTISED_Pause;
10636 } else
10637 newadv = ADVERTISED_Pause |
10638 ADVERTISED_Asym_Pause;
10639 } else if (epause->tx_pause) {
10640 tp->link_config.flowctrl |= FLOW_CTRL_TX;
10641 newadv = ADVERTISED_Asym_Pause;
10642 } else
10643 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010644
Matt Carlson27121682010-02-17 15:16:57 +000010645 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010646 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010647 else
Joe Perches63c3a662011-04-26 08:12:10 +000010648 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010649
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010650 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000010651 u32 oldadv = phydev->advertising &
10652 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
10653 if (oldadv != newadv) {
10654 phydev->advertising &=
10655 ~(ADVERTISED_Pause |
10656 ADVERTISED_Asym_Pause);
10657 phydev->advertising |= newadv;
10658 if (phydev->autoneg) {
10659 /*
10660 * Always renegotiate the link to
10661 * inform our link partner of our
10662 * flow control settings, even if the
10663 * flow control is forced. Let
10664 * tg3_adjust_link() do the final
10665 * flow control setup.
10666 */
10667 return phy_start_aneg(phydev);
10668 }
10669 }
10670
10671 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010672 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000010673 } else {
10674 tp->link_config.orig_advertising &=
10675 ~(ADVERTISED_Pause |
10676 ADVERTISED_Asym_Pause);
10677 tp->link_config.orig_advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010678 }
10679 } else {
10680 int irq_sync = 0;
10681
10682 if (netif_running(dev)) {
10683 tg3_netif_stop(tp);
10684 irq_sync = 1;
10685 }
10686
10687 tg3_full_lock(tp, irq_sync);
10688
10689 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010690 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010691 else
Joe Perches63c3a662011-04-26 08:12:10 +000010692 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010693 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010694 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010695 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010696 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010697 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010698 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010699 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010700 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010701
10702 if (netif_running(dev)) {
10703 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10704 err = tg3_restart_hw(tp, 1);
10705 if (!err)
10706 tg3_netif_start(tp);
10707 }
10708
10709 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010711
Michael Chanb9ec6c12006-07-25 16:37:27 -070010712 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010713}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010714
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010715static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010716{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010717 switch (sset) {
10718 case ETH_SS_TEST:
10719 return TG3_NUM_TEST;
10720 case ETH_SS_STATS:
10721 return TG3_NUM_STATS;
10722 default:
10723 return -EOPNOTSUPP;
10724 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070010725}
10726
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010727static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010728{
10729 switch (stringset) {
10730 case ETH_SS_STATS:
10731 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
10732 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070010733 case ETH_SS_TEST:
10734 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
10735 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010736 default:
10737 WARN_ON(1); /* we need a WARN() */
10738 break;
10739 }
10740}
10741
stephen hemminger81b87092011-04-04 08:43:50 +000010742static int tg3_set_phys_id(struct net_device *dev,
10743 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070010744{
10745 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070010746
10747 if (!netif_running(tp->dev))
10748 return -EAGAIN;
10749
stephen hemminger81b87092011-04-04 08:43:50 +000010750 switch (state) {
10751 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +000010752 return 1; /* cycle on/off once per second */
Michael Chan4009a932005-09-05 17:52:54 -070010753
stephen hemminger81b87092011-04-04 08:43:50 +000010754 case ETHTOOL_ID_ON:
10755 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10756 LED_CTRL_1000MBPS_ON |
10757 LED_CTRL_100MBPS_ON |
10758 LED_CTRL_10MBPS_ON |
10759 LED_CTRL_TRAFFIC_OVERRIDE |
10760 LED_CTRL_TRAFFIC_BLINK |
10761 LED_CTRL_TRAFFIC_LED);
10762 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010763
stephen hemminger81b87092011-04-04 08:43:50 +000010764 case ETHTOOL_ID_OFF:
10765 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10766 LED_CTRL_TRAFFIC_OVERRIDE);
10767 break;
Michael Chan4009a932005-09-05 17:52:54 -070010768
stephen hemminger81b87092011-04-04 08:43:50 +000010769 case ETHTOOL_ID_INACTIVE:
10770 tw32(MAC_LED_CTRL, tp->led_ctrl);
10771 break;
Michael Chan4009a932005-09-05 17:52:54 -070010772 }
stephen hemminger81b87092011-04-04 08:43:50 +000010773
Michael Chan4009a932005-09-05 17:52:54 -070010774 return 0;
10775}
10776
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010777static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010778 struct ethtool_stats *estats, u64 *tmp_stats)
10779{
10780 struct tg3 *tp = netdev_priv(dev);
Matt Carlson0e6c9da2011-12-08 14:40:13 +000010781
10782 tg3_get_estats(tp, (struct tg3_ethtool_stats *)tmp_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010783}
10784
Matt Carlson535a4902011-07-20 10:20:56 +000010785static __be32 *tg3_vpd_readblock(struct tg3 *tp, u32 *vpdlen)
Matt Carlsonc3e94502011-04-13 11:05:08 +000010786{
10787 int i;
10788 __be32 *buf;
10789 u32 offset = 0, len = 0;
10790 u32 magic, val;
10791
Joe Perches63c3a662011-04-26 08:12:10 +000010792 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
Matt Carlsonc3e94502011-04-13 11:05:08 +000010793 return NULL;
10794
10795 if (magic == TG3_EEPROM_MAGIC) {
10796 for (offset = TG3_NVM_DIR_START;
10797 offset < TG3_NVM_DIR_END;
10798 offset += TG3_NVM_DIRENT_SIZE) {
10799 if (tg3_nvram_read(tp, offset, &val))
10800 return NULL;
10801
10802 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
10803 TG3_NVM_DIRTYPE_EXTVPD)
10804 break;
10805 }
10806
10807 if (offset != TG3_NVM_DIR_END) {
10808 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
10809 if (tg3_nvram_read(tp, offset + 4, &offset))
10810 return NULL;
10811
10812 offset = tg3_nvram_logical_addr(tp, offset);
10813 }
10814 }
10815
10816 if (!offset || !len) {
10817 offset = TG3_NVM_VPD_OFF;
10818 len = TG3_NVM_VPD_LEN;
10819 }
10820
10821 buf = kmalloc(len, GFP_KERNEL);
10822 if (buf == NULL)
10823 return NULL;
10824
10825 if (magic == TG3_EEPROM_MAGIC) {
10826 for (i = 0; i < len; i += 4) {
10827 /* The data is in little-endian format in NVRAM.
10828 * Use the big-endian read routines to preserve
10829 * the byte order as it exists in NVRAM.
10830 */
10831 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
10832 goto error;
10833 }
10834 } else {
10835 u8 *ptr;
10836 ssize_t cnt;
10837 unsigned int pos = 0;
10838
10839 ptr = (u8 *)&buf[0];
10840 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
10841 cnt = pci_read_vpd(tp->pdev, pos,
10842 len - pos, ptr);
10843 if (cnt == -ETIMEDOUT || cnt == -EINTR)
10844 cnt = 0;
10845 else if (cnt < 0)
10846 goto error;
10847 }
10848 if (pos != len)
10849 goto error;
10850 }
10851
Matt Carlson535a4902011-07-20 10:20:56 +000010852 *vpdlen = len;
10853
Matt Carlsonc3e94502011-04-13 11:05:08 +000010854 return buf;
10855
10856error:
10857 kfree(buf);
10858 return NULL;
10859}
10860
Michael Chan566f86a2005-05-29 14:56:58 -070010861#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080010862#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
10863#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
10864#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Matt Carlson727a6d92011-06-13 13:38:58 +000010865#define NVRAM_SELFBOOT_FORMAT1_4_SIZE 0x20
10866#define NVRAM_SELFBOOT_FORMAT1_5_SIZE 0x24
Matt Carlsonbda18fa2011-07-20 10:20:57 +000010867#define NVRAM_SELFBOOT_FORMAT1_6_SIZE 0x50
Michael Chanb16250e2006-09-27 16:10:14 -070010868#define NVRAM_SELFBOOT_HW_SIZE 0x20
10869#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070010870
10871static int tg3_test_nvram(struct tg3 *tp)
10872{
Matt Carlson535a4902011-07-20 10:20:56 +000010873 u32 csum, magic, len;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010874 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010875 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070010876
Joe Perches63c3a662011-04-26 08:12:10 +000010877 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000010878 return 0;
10879
Matt Carlsone4f34112009-02-25 14:25:00 +000010880 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080010881 return -EIO;
10882
Michael Chan1b277772006-03-20 22:27:48 -080010883 if (magic == TG3_EEPROM_MAGIC)
10884 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070010885 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080010886 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
10887 TG3_EEPROM_SB_FORMAT_1) {
10888 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
10889 case TG3_EEPROM_SB_REVISION_0:
10890 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
10891 break;
10892 case TG3_EEPROM_SB_REVISION_2:
10893 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
10894 break;
10895 case TG3_EEPROM_SB_REVISION_3:
10896 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
10897 break;
Matt Carlson727a6d92011-06-13 13:38:58 +000010898 case TG3_EEPROM_SB_REVISION_4:
10899 size = NVRAM_SELFBOOT_FORMAT1_4_SIZE;
10900 break;
10901 case TG3_EEPROM_SB_REVISION_5:
10902 size = NVRAM_SELFBOOT_FORMAT1_5_SIZE;
10903 break;
10904 case TG3_EEPROM_SB_REVISION_6:
10905 size = NVRAM_SELFBOOT_FORMAT1_6_SIZE;
10906 break;
Matt Carlsona5767de2007-11-12 21:10:58 -080010907 default:
Matt Carlson727a6d92011-06-13 13:38:58 +000010908 return -EIO;
Matt Carlsona5767de2007-11-12 21:10:58 -080010909 }
10910 } else
Michael Chan1b277772006-03-20 22:27:48 -080010911 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070010912 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
10913 size = NVRAM_SELFBOOT_HW_SIZE;
10914 else
Michael Chan1b277772006-03-20 22:27:48 -080010915 return -EIO;
10916
10917 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070010918 if (buf == NULL)
10919 return -ENOMEM;
10920
Michael Chan1b277772006-03-20 22:27:48 -080010921 err = -EIO;
10922 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010923 err = tg3_nvram_read_be32(tp, i, &buf[j]);
10924 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070010925 break;
Michael Chan566f86a2005-05-29 14:56:58 -070010926 }
Michael Chan1b277772006-03-20 22:27:48 -080010927 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070010928 goto out;
10929
Michael Chan1b277772006-03-20 22:27:48 -080010930 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010931 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080010932 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010933 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080010934 u8 *buf8 = (u8 *) buf, csum8 = 0;
10935
Al Virob9fc7dc2007-12-17 22:59:57 -080010936 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080010937 TG3_EEPROM_SB_REVISION_2) {
10938 /* For rev 2, the csum doesn't include the MBA. */
10939 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
10940 csum8 += buf8[i];
10941 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
10942 csum8 += buf8[i];
10943 } else {
10944 for (i = 0; i < size; i++)
10945 csum8 += buf8[i];
10946 }
Michael Chan1b277772006-03-20 22:27:48 -080010947
Adrian Bunkad96b482006-04-05 22:21:04 -070010948 if (csum8 == 0) {
10949 err = 0;
10950 goto out;
10951 }
10952
10953 err = -EIO;
10954 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080010955 }
Michael Chan566f86a2005-05-29 14:56:58 -070010956
Al Virob9fc7dc2007-12-17 22:59:57 -080010957 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010958 TG3_EEPROM_MAGIC_HW) {
10959 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000010960 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070010961 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070010962
10963 /* Separate the parity bits and the data bytes. */
10964 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
10965 if ((i == 0) || (i == 8)) {
10966 int l;
10967 u8 msk;
10968
10969 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
10970 parity[k++] = buf8[i] & msk;
10971 i++;
Matt Carlson859a5882010-04-05 10:19:28 +000010972 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070010973 int l;
10974 u8 msk;
10975
10976 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
10977 parity[k++] = buf8[i] & msk;
10978 i++;
10979
10980 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
10981 parity[k++] = buf8[i] & msk;
10982 i++;
10983 }
10984 data[j++] = buf8[i];
10985 }
10986
10987 err = -EIO;
10988 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
10989 u8 hw8 = hweight8(data[i]);
10990
10991 if ((hw8 & 0x1) && parity[i])
10992 goto out;
10993 else if (!(hw8 & 0x1) && !parity[i])
10994 goto out;
10995 }
10996 err = 0;
10997 goto out;
10998 }
10999
Matt Carlson01c3a392011-03-09 16:58:20 +000011000 err = -EIO;
11001
Michael Chan566f86a2005-05-29 14:56:58 -070011002 /* Bootstrap checksum at offset 0x10 */
11003 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000011004 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070011005 goto out;
11006
11007 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
11008 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000011009 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000011010 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070011011
Matt Carlsonc3e94502011-04-13 11:05:08 +000011012 kfree(buf);
11013
Matt Carlson535a4902011-07-20 10:20:56 +000011014 buf = tg3_vpd_readblock(tp, &len);
Matt Carlsonc3e94502011-04-13 11:05:08 +000011015 if (!buf)
11016 return -ENOMEM;
Matt Carlsond4894f32011-03-09 16:58:21 +000011017
Matt Carlson535a4902011-07-20 10:20:56 +000011018 i = pci_vpd_find_tag((u8 *)buf, 0, len, PCI_VPD_LRDT_RO_DATA);
Matt Carlsond4894f32011-03-09 16:58:21 +000011019 if (i > 0) {
11020 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
11021 if (j < 0)
11022 goto out;
11023
Matt Carlson535a4902011-07-20 10:20:56 +000011024 if (i + PCI_VPD_LRDT_TAG_SIZE + j > len)
Matt Carlsond4894f32011-03-09 16:58:21 +000011025 goto out;
11026
11027 i += PCI_VPD_LRDT_TAG_SIZE;
11028 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
11029 PCI_VPD_RO_KEYWORD_CHKSUM);
11030 if (j > 0) {
11031 u8 csum8 = 0;
11032
11033 j += PCI_VPD_INFO_FLD_HDR_SIZE;
11034
11035 for (i = 0; i <= j; i++)
11036 csum8 += ((u8 *)buf)[i];
11037
11038 if (csum8)
11039 goto out;
11040 }
11041 }
11042
Michael Chan566f86a2005-05-29 14:56:58 -070011043 err = 0;
11044
11045out:
11046 kfree(buf);
11047 return err;
11048}
11049
Michael Chanca430072005-05-29 14:57:23 -070011050#define TG3_SERDES_TIMEOUT_SEC 2
11051#define TG3_COPPER_TIMEOUT_SEC 6
11052
11053static int tg3_test_link(struct tg3 *tp)
11054{
11055 int i, max;
11056
11057 if (!netif_running(tp->dev))
11058 return -ENODEV;
11059
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011060 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070011061 max = TG3_SERDES_TIMEOUT_SEC;
11062 else
11063 max = TG3_COPPER_TIMEOUT_SEC;
11064
11065 for (i = 0; i < max; i++) {
11066 if (netif_carrier_ok(tp->dev))
11067 return 0;
11068
11069 if (msleep_interruptible(1000))
11070 break;
11071 }
11072
11073 return -EIO;
11074}
11075
Michael Chana71116d2005-05-29 14:58:11 -070011076/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080011077static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070011078{
Michael Chanb16250e2006-09-27 16:10:14 -070011079 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070011080 u32 offset, read_mask, write_mask, val, save_val, read_val;
11081 static struct {
11082 u16 offset;
11083 u16 flags;
11084#define TG3_FL_5705 0x1
11085#define TG3_FL_NOT_5705 0x2
11086#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070011087#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070011088 u32 read_mask;
11089 u32 write_mask;
11090 } reg_tbl[] = {
11091 /* MAC Control Registers */
11092 { MAC_MODE, TG3_FL_NOT_5705,
11093 0x00000000, 0x00ef6f8c },
11094 { MAC_MODE, TG3_FL_5705,
11095 0x00000000, 0x01ef6b8c },
11096 { MAC_STATUS, TG3_FL_NOT_5705,
11097 0x03800107, 0x00000000 },
11098 { MAC_STATUS, TG3_FL_5705,
11099 0x03800100, 0x00000000 },
11100 { MAC_ADDR_0_HIGH, 0x0000,
11101 0x00000000, 0x0000ffff },
11102 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011103 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070011104 { MAC_RX_MTU_SIZE, 0x0000,
11105 0x00000000, 0x0000ffff },
11106 { MAC_TX_MODE, 0x0000,
11107 0x00000000, 0x00000070 },
11108 { MAC_TX_LENGTHS, 0x0000,
11109 0x00000000, 0x00003fff },
11110 { MAC_RX_MODE, TG3_FL_NOT_5705,
11111 0x00000000, 0x000007fc },
11112 { MAC_RX_MODE, TG3_FL_5705,
11113 0x00000000, 0x000007dc },
11114 { MAC_HASH_REG_0, 0x0000,
11115 0x00000000, 0xffffffff },
11116 { MAC_HASH_REG_1, 0x0000,
11117 0x00000000, 0xffffffff },
11118 { MAC_HASH_REG_2, 0x0000,
11119 0x00000000, 0xffffffff },
11120 { MAC_HASH_REG_3, 0x0000,
11121 0x00000000, 0xffffffff },
11122
11123 /* Receive Data and Receive BD Initiator Control Registers. */
11124 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
11125 0x00000000, 0xffffffff },
11126 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
11127 0x00000000, 0xffffffff },
11128 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
11129 0x00000000, 0x00000003 },
11130 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
11131 0x00000000, 0xffffffff },
11132 { RCVDBDI_STD_BD+0, 0x0000,
11133 0x00000000, 0xffffffff },
11134 { RCVDBDI_STD_BD+4, 0x0000,
11135 0x00000000, 0xffffffff },
11136 { RCVDBDI_STD_BD+8, 0x0000,
11137 0x00000000, 0xffff0002 },
11138 { RCVDBDI_STD_BD+0xc, 0x0000,
11139 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011140
Michael Chana71116d2005-05-29 14:58:11 -070011141 /* Receive BD Initiator Control Registers. */
11142 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
11143 0x00000000, 0xffffffff },
11144 { RCVBDI_STD_THRESH, TG3_FL_5705,
11145 0x00000000, 0x000003ff },
11146 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
11147 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011148
Michael Chana71116d2005-05-29 14:58:11 -070011149 /* Host Coalescing Control Registers. */
11150 { HOSTCC_MODE, TG3_FL_NOT_5705,
11151 0x00000000, 0x00000004 },
11152 { HOSTCC_MODE, TG3_FL_5705,
11153 0x00000000, 0x000000f6 },
11154 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
11155 0x00000000, 0xffffffff },
11156 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
11157 0x00000000, 0x000003ff },
11158 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
11159 0x00000000, 0xffffffff },
11160 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
11161 0x00000000, 0x000003ff },
11162 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
11163 0x00000000, 0xffffffff },
11164 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
11165 0x00000000, 0x000000ff },
11166 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
11167 0x00000000, 0xffffffff },
11168 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
11169 0x00000000, 0x000000ff },
11170 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
11171 0x00000000, 0xffffffff },
11172 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
11173 0x00000000, 0xffffffff },
11174 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
11175 0x00000000, 0xffffffff },
11176 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
11177 0x00000000, 0x000000ff },
11178 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
11179 0x00000000, 0xffffffff },
11180 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
11181 0x00000000, 0x000000ff },
11182 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
11183 0x00000000, 0xffffffff },
11184 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
11185 0x00000000, 0xffffffff },
11186 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
11187 0x00000000, 0xffffffff },
11188 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
11189 0x00000000, 0xffffffff },
11190 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
11191 0x00000000, 0xffffffff },
11192 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
11193 0xffffffff, 0x00000000 },
11194 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
11195 0xffffffff, 0x00000000 },
11196
11197 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070011198 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070011199 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070011200 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070011201 0x00000000, 0x007fffff },
11202 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
11203 0x00000000, 0x0000003f },
11204 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
11205 0x00000000, 0x000001ff },
11206 { BUFMGR_MB_HIGH_WATER, 0x0000,
11207 0x00000000, 0x000001ff },
11208 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
11209 0xffffffff, 0x00000000 },
11210 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
11211 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011212
Michael Chana71116d2005-05-29 14:58:11 -070011213 /* Mailbox Registers */
11214 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
11215 0x00000000, 0x000001ff },
11216 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
11217 0x00000000, 0x000001ff },
11218 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
11219 0x00000000, 0x000007ff },
11220 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
11221 0x00000000, 0x000001ff },
11222
11223 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
11224 };
11225
Michael Chanb16250e2006-09-27 16:10:14 -070011226 is_5705 = is_5750 = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000011227 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chana71116d2005-05-29 14:58:11 -070011228 is_5705 = 1;
Joe Perches63c3a662011-04-26 08:12:10 +000011229 if (tg3_flag(tp, 5750_PLUS))
Michael Chanb16250e2006-09-27 16:10:14 -070011230 is_5750 = 1;
11231 }
Michael Chana71116d2005-05-29 14:58:11 -070011232
11233 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
11234 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
11235 continue;
11236
11237 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
11238 continue;
11239
Joe Perches63c3a662011-04-26 08:12:10 +000011240 if (tg3_flag(tp, IS_5788) &&
Michael Chana71116d2005-05-29 14:58:11 -070011241 (reg_tbl[i].flags & TG3_FL_NOT_5788))
11242 continue;
11243
Michael Chanb16250e2006-09-27 16:10:14 -070011244 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
11245 continue;
11246
Michael Chana71116d2005-05-29 14:58:11 -070011247 offset = (u32) reg_tbl[i].offset;
11248 read_mask = reg_tbl[i].read_mask;
11249 write_mask = reg_tbl[i].write_mask;
11250
11251 /* Save the original register content */
11252 save_val = tr32(offset);
11253
11254 /* Determine the read-only value. */
11255 read_val = save_val & read_mask;
11256
11257 /* Write zero to the register, then make sure the read-only bits
11258 * are not changed and the read/write bits are all zeros.
11259 */
11260 tw32(offset, 0);
11261
11262 val = tr32(offset);
11263
11264 /* Test the read-only and read/write bits. */
11265 if (((val & read_mask) != read_val) || (val & write_mask))
11266 goto out;
11267
11268 /* Write ones to all the bits defined by RdMask and WrMask, then
11269 * make sure the read-only bits are not changed and the
11270 * read/write bits are all ones.
11271 */
11272 tw32(offset, read_mask | write_mask);
11273
11274 val = tr32(offset);
11275
11276 /* Test the read-only bits. */
11277 if ((val & read_mask) != read_val)
11278 goto out;
11279
11280 /* Test the read/write bits. */
11281 if ((val & write_mask) != write_mask)
11282 goto out;
11283
11284 tw32(offset, save_val);
11285 }
11286
11287 return 0;
11288
11289out:
Michael Chan9f88f292006-12-07 00:22:54 -080011290 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000011291 netdev_err(tp->dev,
11292 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070011293 tw32(offset, save_val);
11294 return -EIO;
11295}
11296
Michael Chan7942e1d2005-05-29 14:58:36 -070011297static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
11298{
Arjan van de Venf71e1302006-03-03 21:33:57 -050011299 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070011300 int i;
11301 u32 j;
11302
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020011303 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070011304 for (j = 0; j < len; j += 4) {
11305 u32 val;
11306
11307 tg3_write_mem(tp, offset + j, test_pattern[i]);
11308 tg3_read_mem(tp, offset + j, &val);
11309 if (val != test_pattern[i])
11310 return -EIO;
11311 }
11312 }
11313 return 0;
11314}
11315
11316static int tg3_test_memory(struct tg3 *tp)
11317{
11318 static struct mem_entry {
11319 u32 offset;
11320 u32 len;
11321 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080011322 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070011323 { 0x00002000, 0x1c000},
11324 { 0xffffffff, 0x00000}
11325 }, mem_tbl_5705[] = {
11326 { 0x00000100, 0x0000c},
11327 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070011328 { 0x00004000, 0x00800},
11329 { 0x00006000, 0x01000},
11330 { 0x00008000, 0x02000},
11331 { 0x00010000, 0x0e000},
11332 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080011333 }, mem_tbl_5755[] = {
11334 { 0x00000200, 0x00008},
11335 { 0x00004000, 0x00800},
11336 { 0x00006000, 0x00800},
11337 { 0x00008000, 0x02000},
11338 { 0x00010000, 0x0c000},
11339 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070011340 }, mem_tbl_5906[] = {
11341 { 0x00000200, 0x00008},
11342 { 0x00004000, 0x00400},
11343 { 0x00006000, 0x00400},
11344 { 0x00008000, 0x01000},
11345 { 0x00010000, 0x01000},
11346 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011347 }, mem_tbl_5717[] = {
11348 { 0x00000200, 0x00008},
11349 { 0x00010000, 0x0a000},
11350 { 0x00020000, 0x13c00},
11351 { 0xffffffff, 0x00000}
11352 }, mem_tbl_57765[] = {
11353 { 0x00000200, 0x00008},
11354 { 0x00004000, 0x00800},
11355 { 0x00006000, 0x09800},
11356 { 0x00010000, 0x0a000},
11357 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070011358 };
11359 struct mem_entry *mem_tbl;
11360 int err = 0;
11361 int i;
11362
Joe Perches63c3a662011-04-26 08:12:10 +000011363 if (tg3_flag(tp, 5717_PLUS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011364 mem_tbl = mem_tbl_5717;
Matt Carlson55086ad2011-12-14 11:09:59 +000011365 else if (tg3_flag(tp, 57765_CLASS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011366 mem_tbl = mem_tbl_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000011367 else if (tg3_flag(tp, 5755_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080011368 mem_tbl = mem_tbl_5755;
11369 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
11370 mem_tbl = mem_tbl_5906;
Joe Perches63c3a662011-04-26 08:12:10 +000011371 else if (tg3_flag(tp, 5705_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080011372 mem_tbl = mem_tbl_5705;
11373 else
Michael Chan7942e1d2005-05-29 14:58:36 -070011374 mem_tbl = mem_tbl_570x;
11375
11376 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000011377 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
11378 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070011379 break;
11380 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011381
Michael Chan7942e1d2005-05-29 14:58:36 -070011382 return err;
11383}
11384
Matt Carlsonbb158d62011-04-25 12:42:47 +000011385#define TG3_TSO_MSS 500
11386
11387#define TG3_TSO_IP_HDR_LEN 20
11388#define TG3_TSO_TCP_HDR_LEN 20
11389#define TG3_TSO_TCP_OPT_LEN 12
11390
11391static const u8 tg3_tso_header[] = {
113920x08, 0x00,
113930x45, 0x00, 0x00, 0x00,
113940x00, 0x00, 0x40, 0x00,
113950x40, 0x06, 0x00, 0x00,
113960x0a, 0x00, 0x00, 0x01,
113970x0a, 0x00, 0x00, 0x02,
113980x0d, 0x00, 0xe0, 0x00,
113990x00, 0x00, 0x01, 0x00,
114000x00, 0x00, 0x02, 0x00,
114010x80, 0x10, 0x10, 0x00,
114020x14, 0x09, 0x00, 0x00,
114030x01, 0x01, 0x08, 0x0a,
114040x11, 0x11, 0x11, 0x11,
114050x11, 0x11, 0x11, 0x11,
11406};
Michael Chan9f40dea2005-09-05 17:53:06 -070011407
Matt Carlson28a45952011-08-19 13:58:22 +000011408static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, bool tso_loopback)
Michael Chanc76949a2005-05-29 14:58:59 -070011409{
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011410 u32 rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011411 u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
Matt Carlson84b67b22011-07-27 14:20:52 +000011412 u32 budget;
Eric Dumazet9205fd92011-11-18 06:47:01 +000011413 struct sk_buff *skb;
11414 u8 *tx_data, *rx_data;
Michael Chanc76949a2005-05-29 14:58:59 -070011415 dma_addr_t map;
11416 int num_pkts, tx_len, rx_len, i, err;
11417 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000011418 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000011419 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070011420
Matt Carlsonc8873402010-02-12 14:47:11 +000011421 tnapi = &tp->napi[0];
11422 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011423 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +000011424 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlson1da85aa2010-09-30 10:34:34 +000011425 rnapi = &tp->napi[1];
Joe Perches63c3a662011-04-26 08:12:10 +000011426 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc8873402010-02-12 14:47:11 +000011427 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011428 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011429 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000011430
Michael Chanc76949a2005-05-29 14:58:59 -070011431 err = -EIO;
11432
Matt Carlson4852a862011-04-13 11:05:07 +000011433 tx_len = pktsz;
David S. Millera20e9c62006-07-31 22:38:16 -070011434 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070011435 if (!skb)
11436 return -ENOMEM;
11437
Michael Chanc76949a2005-05-29 14:58:59 -070011438 tx_data = skb_put(skb, tx_len);
11439 memcpy(tx_data, tp->dev->dev_addr, 6);
11440 memset(tx_data + 6, 0x0, 8);
11441
Matt Carlson4852a862011-04-13 11:05:07 +000011442 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
Michael Chanc76949a2005-05-29 14:58:59 -070011443
Matt Carlson28a45952011-08-19 13:58:22 +000011444 if (tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011445 struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
11446
11447 u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
11448 TG3_TSO_TCP_OPT_LEN;
11449
11450 memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
11451 sizeof(tg3_tso_header));
11452 mss = TG3_TSO_MSS;
11453
11454 val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
11455 num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
11456
11457 /* Set the total length field in the IP header */
11458 iph->tot_len = htons((u16)(mss + hdr_len));
11459
11460 base_flags = (TXD_FLAG_CPU_PRE_DMA |
11461 TXD_FLAG_CPU_POST_DMA);
11462
Joe Perches63c3a662011-04-26 08:12:10 +000011463 if (tg3_flag(tp, HW_TSO_1) ||
11464 tg3_flag(tp, HW_TSO_2) ||
11465 tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011466 struct tcphdr *th;
11467 val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
11468 th = (struct tcphdr *)&tx_data[val];
11469 th->check = 0;
11470 } else
11471 base_flags |= TXD_FLAG_TCPUDP_CSUM;
11472
Joe Perches63c3a662011-04-26 08:12:10 +000011473 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011474 mss |= (hdr_len & 0xc) << 12;
11475 if (hdr_len & 0x10)
11476 base_flags |= 0x00000010;
11477 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +000011478 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlsonbb158d62011-04-25 12:42:47 +000011479 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +000011480 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlsonbb158d62011-04-25 12:42:47 +000011481 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
11482 mss |= (TG3_TSO_TCP_OPT_LEN << 9);
11483 } else {
11484 base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
11485 }
11486
11487 data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
11488 } else {
11489 num_pkts = 1;
11490 data_off = ETH_HLEN;
11491 }
11492
11493 for (i = data_off; i < tx_len; i++)
Michael Chanc76949a2005-05-29 14:58:59 -070011494 tx_data[i] = (u8) (i & 0xff);
11495
Alexander Duyckf4188d82009-12-02 16:48:38 +000011496 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
11497 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000011498 dev_kfree_skb(skb);
11499 return -EIO;
11500 }
Michael Chanc76949a2005-05-29 14:58:59 -070011501
Matt Carlson0d681b22011-07-27 14:20:49 +000011502 val = tnapi->tx_prod;
11503 tnapi->tx_buffers[val].skb = skb;
11504 dma_unmap_addr_set(&tnapi->tx_buffers[val], mapping, map);
11505
Michael Chanc76949a2005-05-29 14:58:59 -070011506 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011507 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011508
11509 udelay(10);
11510
Matt Carlson898a56f2009-08-28 14:02:40 +000011511 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070011512
Matt Carlson84b67b22011-07-27 14:20:52 +000011513 budget = tg3_tx_avail(tnapi);
11514 if (tg3_tx_frag_set(tnapi, &val, &budget, map, tx_len,
Matt Carlsond1a3b732011-07-27 14:20:51 +000011515 base_flags | TXD_FLAG_END, mss, 0)) {
11516 tnapi->tx_buffers[val].skb = NULL;
11517 dev_kfree_skb(skb);
11518 return -EIO;
11519 }
Michael Chanc76949a2005-05-29 14:58:59 -070011520
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011521 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070011522
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011523 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
11524 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070011525
11526 udelay(10);
11527
Matt Carlson303fc922009-11-02 14:27:34 +000011528 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
11529 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070011530 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011531 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011532
11533 udelay(10);
11534
Matt Carlson898a56f2009-08-28 14:02:40 +000011535 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
11536 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011537 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070011538 (rx_idx == (rx_start_idx + num_pkts)))
11539 break;
11540 }
11541
Matt Carlsonba1142e2011-11-04 09:15:00 +000011542 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod - 1, -1);
Michael Chanc76949a2005-05-29 14:58:59 -070011543 dev_kfree_skb(skb);
11544
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011545 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070011546 goto out;
11547
11548 if (rx_idx != rx_start_idx + num_pkts)
11549 goto out;
11550
Matt Carlsonbb158d62011-04-25 12:42:47 +000011551 val = data_off;
11552 while (rx_idx != rx_start_idx) {
11553 desc = &rnapi->rx_rcb[rx_start_idx++];
11554 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
11555 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
Michael Chanc76949a2005-05-29 14:58:59 -070011556
Matt Carlsonbb158d62011-04-25 12:42:47 +000011557 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
11558 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
Matt Carlson4852a862011-04-13 11:05:07 +000011559 goto out;
Michael Chanc76949a2005-05-29 14:58:59 -070011560
Matt Carlsonbb158d62011-04-25 12:42:47 +000011561 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
11562 - ETH_FCS_LEN;
11563
Matt Carlson28a45952011-08-19 13:58:22 +000011564 if (!tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011565 if (rx_len != tx_len)
11566 goto out;
11567
11568 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
11569 if (opaque_key != RXD_OPAQUE_RING_STD)
11570 goto out;
11571 } else {
11572 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
11573 goto out;
11574 }
11575 } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
11576 (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
Matt Carlson54e0a672011-05-19 12:12:50 +000011577 >> RXD_TCPCSUM_SHIFT != 0xffff) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011578 goto out;
11579 }
11580
11581 if (opaque_key == RXD_OPAQUE_RING_STD) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000011582 rx_data = tpr->rx_std_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011583 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
11584 mapping);
11585 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000011586 rx_data = tpr->rx_jmb_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011587 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
11588 mapping);
11589 } else
Matt Carlson4852a862011-04-13 11:05:07 +000011590 goto out;
11591
Matt Carlsonbb158d62011-04-25 12:42:47 +000011592 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
11593 PCI_DMA_FROMDEVICE);
11594
Eric Dumazet9205fd92011-11-18 06:47:01 +000011595 rx_data += TG3_RX_OFFSET(tp);
Matt Carlsonbb158d62011-04-25 12:42:47 +000011596 for (i = data_off; i < rx_len; i++, val++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000011597 if (*(rx_data + i) != (u8) (val & 0xff))
Matt Carlsonbb158d62011-04-25 12:42:47 +000011598 goto out;
11599 }
Matt Carlson4852a862011-04-13 11:05:07 +000011600 }
11601
Michael Chanc76949a2005-05-29 14:58:59 -070011602 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011603
Eric Dumazet9205fd92011-11-18 06:47:01 +000011604 /* tg3_free_rings will unmap and free the rx_data */
Michael Chanc76949a2005-05-29 14:58:59 -070011605out:
11606 return err;
11607}
11608
Matt Carlson00c266b2011-04-25 12:42:46 +000011609#define TG3_STD_LOOPBACK_FAILED 1
11610#define TG3_JMB_LOOPBACK_FAILED 2
Matt Carlsonbb158d62011-04-25 12:42:47 +000011611#define TG3_TSO_LOOPBACK_FAILED 4
Matt Carlson28a45952011-08-19 13:58:22 +000011612#define TG3_LOOPBACK_FAILED \
11613 (TG3_STD_LOOPBACK_FAILED | \
11614 TG3_JMB_LOOPBACK_FAILED | \
11615 TG3_TSO_LOOPBACK_FAILED)
Matt Carlson00c266b2011-04-25 12:42:46 +000011616
Matt Carlson941ec902011-08-19 13:58:23 +000011617static int tg3_test_loopback(struct tg3 *tp, u64 *data, bool do_extlpbk)
Michael Chan9f40dea2005-09-05 17:53:06 -070011618{
Matt Carlson28a45952011-08-19 13:58:22 +000011619 int err = -EIO;
Matt Carlson2215e242011-08-19 13:58:19 +000011620 u32 eee_cap;
Michael Chan9f40dea2005-09-05 17:53:06 -070011621
Matt Carlsonab789042011-01-25 15:58:54 +000011622 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
11623 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
11624
Matt Carlson28a45952011-08-19 13:58:22 +000011625 if (!netif_running(tp->dev)) {
11626 data[0] = TG3_LOOPBACK_FAILED;
11627 data[1] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000011628 if (do_extlpbk)
11629 data[2] = TG3_LOOPBACK_FAILED;
Matt Carlson28a45952011-08-19 13:58:22 +000011630 goto done;
11631 }
11632
Michael Chanb9ec6c12006-07-25 16:37:27 -070011633 err = tg3_reset_hw(tp, 1);
Matt Carlsonab789042011-01-25 15:58:54 +000011634 if (err) {
Matt Carlson28a45952011-08-19 13:58:22 +000011635 data[0] = TG3_LOOPBACK_FAILED;
11636 data[1] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000011637 if (do_extlpbk)
11638 data[2] = TG3_LOOPBACK_FAILED;
Matt Carlsonab789042011-01-25 15:58:54 +000011639 goto done;
11640 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011641
Joe Perches63c3a662011-04-26 08:12:10 +000011642 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson4a85f092011-04-20 07:57:37 +000011643 int i;
11644
11645 /* Reroute all rx packets to the 1st queue */
11646 for (i = MAC_RSS_INDIR_TBL_0;
11647 i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
11648 tw32(i, 0x0);
11649 }
11650
Matt Carlson6e01b202011-08-19 13:58:20 +000011651 /* HW errata - mac loopback fails in some cases on 5780.
11652 * Normal traffic and PHY loopback are not affected by
11653 * errata. Also, the MAC loopback test is deprecated for
11654 * all newer ASIC revisions.
11655 */
11656 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
11657 !tg3_flag(tp, CPMU_PRESENT)) {
11658 tg3_mac_loopback(tp, true);
Matt Carlson9936bcf2007-10-10 18:03:07 -070011659
Matt Carlson28a45952011-08-19 13:58:22 +000011660 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
11661 data[0] |= TG3_STD_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000011662
11663 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000011664 tg3_run_loopback(tp, 9000 + ETH_HLEN, false))
11665 data[0] |= TG3_JMB_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000011666
11667 tg3_mac_loopback(tp, false);
11668 }
Matt Carlson4852a862011-04-13 11:05:07 +000011669
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011670 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000011671 !tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011672 int i;
11673
Matt Carlson941ec902011-08-19 13:58:23 +000011674 tg3_phy_lpbk_set(tp, 0, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011675
11676 /* Wait for link */
11677 for (i = 0; i < 100; i++) {
11678 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
11679 break;
11680 mdelay(1);
11681 }
11682
Matt Carlson28a45952011-08-19 13:58:22 +000011683 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
11684 data[1] |= TG3_STD_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000011685 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000011686 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
11687 data[1] |= TG3_TSO_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000011688 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000011689 tg3_run_loopback(tp, 9000 + ETH_HLEN, false))
11690 data[1] |= TG3_JMB_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070011691
Matt Carlson941ec902011-08-19 13:58:23 +000011692 if (do_extlpbk) {
11693 tg3_phy_lpbk_set(tp, 0, true);
11694
11695 /* All link indications report up, but the hardware
11696 * isn't really ready for about 20 msec. Double it
11697 * to be sure.
11698 */
11699 mdelay(40);
11700
11701 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
11702 data[2] |= TG3_STD_LOOPBACK_FAILED;
11703 if (tg3_flag(tp, TSO_CAPABLE) &&
11704 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
11705 data[2] |= TG3_TSO_LOOPBACK_FAILED;
11706 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
11707 tg3_run_loopback(tp, 9000 + ETH_HLEN, false))
11708 data[2] |= TG3_JMB_LOOPBACK_FAILED;
11709 }
11710
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011711 /* Re-enable gphy autopowerdown. */
11712 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
11713 tg3_phy_toggle_apd(tp, true);
11714 }
Matt Carlson6833c042008-11-21 17:18:59 -080011715
Matt Carlson941ec902011-08-19 13:58:23 +000011716 err = (data[0] | data[1] | data[2]) ? -EIO : 0;
Matt Carlson28a45952011-08-19 13:58:22 +000011717
Matt Carlsonab789042011-01-25 15:58:54 +000011718done:
11719 tp->phy_flags |= eee_cap;
11720
Michael Chan9f40dea2005-09-05 17:53:06 -070011721 return err;
11722}
11723
Michael Chan4cafd3f2005-05-29 14:56:34 -070011724static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
11725 u64 *data)
11726{
Michael Chan566f86a2005-05-29 14:56:58 -070011727 struct tg3 *tp = netdev_priv(dev);
Matt Carlson941ec902011-08-19 13:58:23 +000011728 bool doextlpbk = etest->flags & ETH_TEST_FL_EXTERNAL_LB;
Michael Chan566f86a2005-05-29 14:56:58 -070011729
Matt Carlsonbed98292011-07-13 09:27:29 +000011730 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
11731 tg3_power_up(tp)) {
11732 etest->flags |= ETH_TEST_FL_FAILED;
11733 memset(data, 1, sizeof(u64) * TG3_NUM_TEST);
11734 return;
11735 }
Michael Chanbc1c7562006-03-20 17:48:03 -080011736
Michael Chan566f86a2005-05-29 14:56:58 -070011737 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
11738
11739 if (tg3_test_nvram(tp) != 0) {
11740 etest->flags |= ETH_TEST_FL_FAILED;
11741 data[0] = 1;
11742 }
Matt Carlson941ec902011-08-19 13:58:23 +000011743 if (!doextlpbk && tg3_test_link(tp)) {
Michael Chanca430072005-05-29 14:57:23 -070011744 etest->flags |= ETH_TEST_FL_FAILED;
11745 data[1] = 1;
11746 }
Michael Chana71116d2005-05-29 14:58:11 -070011747 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011748 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070011749
Michael Chanbbe832c2005-06-24 20:20:04 -070011750 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011751 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070011752 tg3_netif_stop(tp);
11753 irq_sync = 1;
11754 }
11755
11756 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070011757
11758 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080011759 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011760 tg3_halt_cpu(tp, RX_CPU_BASE);
Joe Perches63c3a662011-04-26 08:12:10 +000011761 if (!tg3_flag(tp, 5705_PLUS))
Michael Chana71116d2005-05-29 14:58:11 -070011762 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080011763 if (!err)
11764 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011765
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011766 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad2006-03-20 22:27:35 -080011767 tg3_phy_reset(tp);
11768
Michael Chana71116d2005-05-29 14:58:11 -070011769 if (tg3_test_registers(tp) != 0) {
11770 etest->flags |= ETH_TEST_FL_FAILED;
11771 data[2] = 1;
11772 }
Matt Carlson28a45952011-08-19 13:58:22 +000011773
Michael Chan7942e1d2005-05-29 14:58:36 -070011774 if (tg3_test_memory(tp) != 0) {
11775 etest->flags |= ETH_TEST_FL_FAILED;
11776 data[3] = 1;
11777 }
Matt Carlson28a45952011-08-19 13:58:22 +000011778
Matt Carlson941ec902011-08-19 13:58:23 +000011779 if (doextlpbk)
11780 etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
11781
11782 if (tg3_test_loopback(tp, &data[4], doextlpbk))
Michael Chanc76949a2005-05-29 14:58:59 -070011783 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070011784
David S. Millerf47c11e2005-06-24 20:18:35 -070011785 tg3_full_unlock(tp);
11786
Michael Chand4bc3922005-05-29 14:59:20 -070011787 if (tg3_test_interrupt(tp) != 0) {
11788 etest->flags |= ETH_TEST_FL_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000011789 data[7] = 1;
Michael Chand4bc3922005-05-29 14:59:20 -070011790 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011791
11792 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070011793
Michael Chana71116d2005-05-29 14:58:11 -070011794 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
11795 if (netif_running(dev)) {
Joe Perches63c3a662011-04-26 08:12:10 +000011796 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011797 err2 = tg3_restart_hw(tp, 1);
11798 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070011799 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011800 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011801
11802 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011803
11804 if (irq_sync && !err2)
11805 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011806 }
Matt Carlson80096062010-08-02 11:26:06 +000011807 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000011808 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080011809
Michael Chan4cafd3f2005-05-29 14:56:34 -070011810}
11811
Linus Torvalds1da177e2005-04-16 15:20:36 -070011812static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
11813{
11814 struct mii_ioctl_data *data = if_mii(ifr);
11815 struct tg3 *tp = netdev_priv(dev);
11816 int err;
11817
Joe Perches63c3a662011-04-26 08:12:10 +000011818 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011819 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011820 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011821 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011822 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000011823 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011824 }
11825
Matt Carlson33f401a2010-04-05 10:19:27 +000011826 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011827 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000011828 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011829
11830 /* fallthru */
11831 case SIOCGMIIREG: {
11832 u32 mii_regval;
11833
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011834 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011835 break; /* We have no PHY */
11836
Matt Carlson34eea5a2011-04-20 07:57:38 +000011837 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080011838 return -EAGAIN;
11839
David S. Millerf47c11e2005-06-24 20:18:35 -070011840 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011841 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070011842 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011843
11844 data->val_out = mii_regval;
11845
11846 return err;
11847 }
11848
11849 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011850 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011851 break; /* We have no PHY */
11852
Matt Carlson34eea5a2011-04-20 07:57:38 +000011853 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080011854 return -EAGAIN;
11855
David S. Millerf47c11e2005-06-24 20:18:35 -070011856 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011857 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070011858 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011859
11860 return err;
11861
11862 default:
11863 /* do nothing */
11864 break;
11865 }
11866 return -EOPNOTSUPP;
11867}
11868
David S. Miller15f98502005-05-18 22:49:26 -070011869static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11870{
11871 struct tg3 *tp = netdev_priv(dev);
11872
11873 memcpy(ec, &tp->coal, sizeof(*ec));
11874 return 0;
11875}
11876
Michael Chand244c892005-07-05 14:42:33 -070011877static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11878{
11879 struct tg3 *tp = netdev_priv(dev);
11880 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
11881 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
11882
Joe Perches63c3a662011-04-26 08:12:10 +000011883 if (!tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070011884 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
11885 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
11886 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
11887 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
11888 }
11889
11890 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
11891 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
11892 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
11893 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
11894 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
11895 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
11896 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
11897 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
11898 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
11899 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
11900 return -EINVAL;
11901
11902 /* No rx interrupts will be generated if both are zero */
11903 if ((ec->rx_coalesce_usecs == 0) &&
11904 (ec->rx_max_coalesced_frames == 0))
11905 return -EINVAL;
11906
11907 /* No tx interrupts will be generated if both are zero */
11908 if ((ec->tx_coalesce_usecs == 0) &&
11909 (ec->tx_max_coalesced_frames == 0))
11910 return -EINVAL;
11911
11912 /* Only copy relevant parameters, ignore all others. */
11913 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
11914 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
11915 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
11916 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
11917 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
11918 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
11919 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
11920 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
11921 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
11922
11923 if (netif_running(dev)) {
11924 tg3_full_lock(tp, 0);
11925 __tg3_set_coalesce(tp, &tp->coal);
11926 tg3_full_unlock(tp);
11927 }
11928 return 0;
11929}
11930
Jeff Garzik7282d492006-09-13 14:30:00 -040011931static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011932 .get_settings = tg3_get_settings,
11933 .set_settings = tg3_set_settings,
11934 .get_drvinfo = tg3_get_drvinfo,
11935 .get_regs_len = tg3_get_regs_len,
11936 .get_regs = tg3_get_regs,
11937 .get_wol = tg3_get_wol,
11938 .set_wol = tg3_set_wol,
11939 .get_msglevel = tg3_get_msglevel,
11940 .set_msglevel = tg3_set_msglevel,
11941 .nway_reset = tg3_nway_reset,
11942 .get_link = ethtool_op_get_link,
11943 .get_eeprom_len = tg3_get_eeprom_len,
11944 .get_eeprom = tg3_get_eeprom,
11945 .set_eeprom = tg3_set_eeprom,
11946 .get_ringparam = tg3_get_ringparam,
11947 .set_ringparam = tg3_set_ringparam,
11948 .get_pauseparam = tg3_get_pauseparam,
11949 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070011950 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011951 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000011952 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011953 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070011954 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070011955 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070011956 .get_sset_count = tg3_get_sset_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011957};
11958
11959static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
11960{
Michael Chan1b277772006-03-20 22:27:48 -080011961 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011962
11963 tp->nvram_size = EEPROM_CHIP_SIZE;
11964
Matt Carlsone4f34112009-02-25 14:25:00 +000011965 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011966 return;
11967
Michael Chanb16250e2006-09-27 16:10:14 -070011968 if ((magic != TG3_EEPROM_MAGIC) &&
11969 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
11970 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011971 return;
11972
11973 /*
11974 * Size the chip by reading offsets at increasing powers of two.
11975 * When we encounter our validation signature, we know the addressing
11976 * has wrapped around, and thus have our chip size.
11977 */
Michael Chan1b277772006-03-20 22:27:48 -080011978 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011979
11980 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000011981 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011982 return;
11983
Michael Chan18201802006-03-20 22:29:15 -080011984 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011985 break;
11986
11987 cursize <<= 1;
11988 }
11989
11990 tp->nvram_size = cursize;
11991}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011992
Linus Torvalds1da177e2005-04-16 15:20:36 -070011993static void __devinit tg3_get_nvram_size(struct tg3 *tp)
11994{
11995 u32 val;
11996
Joe Perches63c3a662011-04-26 08:12:10 +000011997 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080011998 return;
11999
12000 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080012001 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080012002 tg3_get_eeprom_size(tp);
12003 return;
12004 }
12005
Matt Carlson6d348f22009-02-25 14:25:52 +000012006 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012007 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000012008 /* This is confusing. We want to operate on the
12009 * 16-bit value at offset 0xf2. The tg3_nvram_read()
12010 * call will read from NVRAM and byteswap the data
12011 * according to the byteswapping settings for all
12012 * other register accesses. This ensures the data we
12013 * want will always reside in the lower 16-bits.
12014 * However, the data in NVRAM is in LE format, which
12015 * means the data from the NVRAM read will always be
12016 * opposite the endianness of the CPU. The 16-bit
12017 * byteswap then brings the data to CPU endianness.
12018 */
12019 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012020 return;
12021 }
12022 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070012023 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012024}
12025
12026static void __devinit tg3_get_nvram_info(struct tg3 *tp)
12027{
12028 u32 nvcfg1;
12029
12030 nvcfg1 = tr32(NVRAM_CFG1);
12031 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
Joe Perches63c3a662011-04-26 08:12:10 +000012032 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012033 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012034 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12035 tw32(NVRAM_CFG1, nvcfg1);
12036 }
12037
Matt Carlson6ff6f812011-05-19 12:12:54 +000012038 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
Joe Perches63c3a662011-04-26 08:12:10 +000012039 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012040 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012041 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
12042 tp->nvram_jedecnum = JEDEC_ATMEL;
12043 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012044 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012045 break;
12046 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
12047 tp->nvram_jedecnum = JEDEC_ATMEL;
12048 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
12049 break;
12050 case FLASH_VENDOR_ATMEL_EEPROM:
12051 tp->nvram_jedecnum = JEDEC_ATMEL;
12052 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012053 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012054 break;
12055 case FLASH_VENDOR_ST:
12056 tp->nvram_jedecnum = JEDEC_ST;
12057 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012058 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012059 break;
12060 case FLASH_VENDOR_SAIFUN:
12061 tp->nvram_jedecnum = JEDEC_SAIFUN;
12062 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
12063 break;
12064 case FLASH_VENDOR_SST_SMALL:
12065 case FLASH_VENDOR_SST_LARGE:
12066 tp->nvram_jedecnum = JEDEC_SST;
12067 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
12068 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012069 }
Matt Carlson8590a602009-08-28 12:29:16 +000012070 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012071 tp->nvram_jedecnum = JEDEC_ATMEL;
12072 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012073 tg3_flag_set(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012074 }
12075}
12076
Matt Carlsona1b950d2009-09-01 13:20:17 +000012077static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
12078{
12079 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
12080 case FLASH_5752PAGE_SIZE_256:
12081 tp->nvram_pagesize = 256;
12082 break;
12083 case FLASH_5752PAGE_SIZE_512:
12084 tp->nvram_pagesize = 512;
12085 break;
12086 case FLASH_5752PAGE_SIZE_1K:
12087 tp->nvram_pagesize = 1024;
12088 break;
12089 case FLASH_5752PAGE_SIZE_2K:
12090 tp->nvram_pagesize = 2048;
12091 break;
12092 case FLASH_5752PAGE_SIZE_4K:
12093 tp->nvram_pagesize = 4096;
12094 break;
12095 case FLASH_5752PAGE_SIZE_264:
12096 tp->nvram_pagesize = 264;
12097 break;
12098 case FLASH_5752PAGE_SIZE_528:
12099 tp->nvram_pagesize = 528;
12100 break;
12101 }
12102}
12103
Michael Chan361b4ac2005-04-21 17:11:21 -070012104static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
12105{
12106 u32 nvcfg1;
12107
12108 nvcfg1 = tr32(NVRAM_CFG1);
12109
Michael Chane6af3012005-04-21 17:12:05 -070012110 /* NVRAM protection for TPM */
12111 if (nvcfg1 & (1 << 27))
Joe Perches63c3a662011-04-26 08:12:10 +000012112 tg3_flag_set(tp, PROTECTED_NVRAM);
Michael Chane6af3012005-04-21 17:12:05 -070012113
Michael Chan361b4ac2005-04-21 17:11:21 -070012114 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012115 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
12116 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
12117 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012118 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012119 break;
12120 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12121 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012122 tg3_flag_set(tp, NVRAM_BUFFERED);
12123 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012124 break;
12125 case FLASH_5752VENDOR_ST_M45PE10:
12126 case FLASH_5752VENDOR_ST_M45PE20:
12127 case FLASH_5752VENDOR_ST_M45PE40:
12128 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012129 tg3_flag_set(tp, NVRAM_BUFFERED);
12130 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012131 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070012132 }
12133
Joe Perches63c3a662011-04-26 08:12:10 +000012134 if (tg3_flag(tp, FLASH)) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000012135 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000012136 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070012137 /* For eeprom, set pagesize to maximum eeprom size */
12138 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12139
12140 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12141 tw32(NVRAM_CFG1, nvcfg1);
12142 }
12143}
12144
Michael Chand3c7b882006-03-23 01:28:25 -080012145static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
12146{
Matt Carlson989a9d22007-05-05 11:51:05 -070012147 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080012148
12149 nvcfg1 = tr32(NVRAM_CFG1);
12150
12151 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070012152 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012153 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson989a9d22007-05-05 11:51:05 -070012154 protect = 1;
12155 }
Michael Chand3c7b882006-03-23 01:28:25 -080012156
Matt Carlson989a9d22007-05-05 11:51:05 -070012157 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
12158 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012159 case FLASH_5755VENDOR_ATMEL_FLASH_1:
12160 case FLASH_5755VENDOR_ATMEL_FLASH_2:
12161 case FLASH_5755VENDOR_ATMEL_FLASH_3:
12162 case FLASH_5755VENDOR_ATMEL_FLASH_5:
12163 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012164 tg3_flag_set(tp, NVRAM_BUFFERED);
12165 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012166 tp->nvram_pagesize = 264;
12167 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
12168 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
12169 tp->nvram_size = (protect ? 0x3e200 :
12170 TG3_NVRAM_SIZE_512KB);
12171 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
12172 tp->nvram_size = (protect ? 0x1f200 :
12173 TG3_NVRAM_SIZE_256KB);
12174 else
12175 tp->nvram_size = (protect ? 0x1f200 :
12176 TG3_NVRAM_SIZE_128KB);
12177 break;
12178 case FLASH_5752VENDOR_ST_M45PE10:
12179 case FLASH_5752VENDOR_ST_M45PE20:
12180 case FLASH_5752VENDOR_ST_M45PE40:
12181 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012182 tg3_flag_set(tp, NVRAM_BUFFERED);
12183 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012184 tp->nvram_pagesize = 256;
12185 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
12186 tp->nvram_size = (protect ?
12187 TG3_NVRAM_SIZE_64KB :
12188 TG3_NVRAM_SIZE_128KB);
12189 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
12190 tp->nvram_size = (protect ?
12191 TG3_NVRAM_SIZE_64KB :
12192 TG3_NVRAM_SIZE_256KB);
12193 else
12194 tp->nvram_size = (protect ?
12195 TG3_NVRAM_SIZE_128KB :
12196 TG3_NVRAM_SIZE_512KB);
12197 break;
Michael Chand3c7b882006-03-23 01:28:25 -080012198 }
12199}
12200
Michael Chan1b277772006-03-20 22:27:48 -080012201static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
12202{
12203 u32 nvcfg1;
12204
12205 nvcfg1 = tr32(NVRAM_CFG1);
12206
12207 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012208 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
12209 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
12210 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
12211 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
12212 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012213 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012214 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080012215
Matt Carlson8590a602009-08-28 12:29:16 +000012216 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12217 tw32(NVRAM_CFG1, nvcfg1);
12218 break;
12219 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12220 case FLASH_5755VENDOR_ATMEL_FLASH_1:
12221 case FLASH_5755VENDOR_ATMEL_FLASH_2:
12222 case FLASH_5755VENDOR_ATMEL_FLASH_3:
12223 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012224 tg3_flag_set(tp, NVRAM_BUFFERED);
12225 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012226 tp->nvram_pagesize = 264;
12227 break;
12228 case FLASH_5752VENDOR_ST_M45PE10:
12229 case FLASH_5752VENDOR_ST_M45PE20:
12230 case FLASH_5752VENDOR_ST_M45PE40:
12231 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012232 tg3_flag_set(tp, NVRAM_BUFFERED);
12233 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012234 tp->nvram_pagesize = 256;
12235 break;
Michael Chan1b277772006-03-20 22:27:48 -080012236 }
12237}
12238
Matt Carlson6b91fa02007-10-10 18:01:09 -070012239static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
12240{
12241 u32 nvcfg1, protect = 0;
12242
12243 nvcfg1 = tr32(NVRAM_CFG1);
12244
12245 /* NVRAM protection for TPM */
12246 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012247 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson6b91fa02007-10-10 18:01:09 -070012248 protect = 1;
12249 }
12250
12251 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
12252 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012253 case FLASH_5761VENDOR_ATMEL_ADB021D:
12254 case FLASH_5761VENDOR_ATMEL_ADB041D:
12255 case FLASH_5761VENDOR_ATMEL_ADB081D:
12256 case FLASH_5761VENDOR_ATMEL_ADB161D:
12257 case FLASH_5761VENDOR_ATMEL_MDB021D:
12258 case FLASH_5761VENDOR_ATMEL_MDB041D:
12259 case FLASH_5761VENDOR_ATMEL_MDB081D:
12260 case FLASH_5761VENDOR_ATMEL_MDB161D:
12261 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012262 tg3_flag_set(tp, NVRAM_BUFFERED);
12263 tg3_flag_set(tp, FLASH);
12264 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson8590a602009-08-28 12:29:16 +000012265 tp->nvram_pagesize = 256;
12266 break;
12267 case FLASH_5761VENDOR_ST_A_M45PE20:
12268 case FLASH_5761VENDOR_ST_A_M45PE40:
12269 case FLASH_5761VENDOR_ST_A_M45PE80:
12270 case FLASH_5761VENDOR_ST_A_M45PE16:
12271 case FLASH_5761VENDOR_ST_M_M45PE20:
12272 case FLASH_5761VENDOR_ST_M_M45PE40:
12273 case FLASH_5761VENDOR_ST_M_M45PE80:
12274 case FLASH_5761VENDOR_ST_M_M45PE16:
12275 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012276 tg3_flag_set(tp, NVRAM_BUFFERED);
12277 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012278 tp->nvram_pagesize = 256;
12279 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070012280 }
12281
12282 if (protect) {
12283 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
12284 } else {
12285 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012286 case FLASH_5761VENDOR_ATMEL_ADB161D:
12287 case FLASH_5761VENDOR_ATMEL_MDB161D:
12288 case FLASH_5761VENDOR_ST_A_M45PE16:
12289 case FLASH_5761VENDOR_ST_M_M45PE16:
12290 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
12291 break;
12292 case FLASH_5761VENDOR_ATMEL_ADB081D:
12293 case FLASH_5761VENDOR_ATMEL_MDB081D:
12294 case FLASH_5761VENDOR_ST_A_M45PE80:
12295 case FLASH_5761VENDOR_ST_M_M45PE80:
12296 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12297 break;
12298 case FLASH_5761VENDOR_ATMEL_ADB041D:
12299 case FLASH_5761VENDOR_ATMEL_MDB041D:
12300 case FLASH_5761VENDOR_ST_A_M45PE40:
12301 case FLASH_5761VENDOR_ST_M_M45PE40:
12302 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12303 break;
12304 case FLASH_5761VENDOR_ATMEL_ADB021D:
12305 case FLASH_5761VENDOR_ATMEL_MDB021D:
12306 case FLASH_5761VENDOR_ST_A_M45PE20:
12307 case FLASH_5761VENDOR_ST_M_M45PE20:
12308 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12309 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070012310 }
12311 }
12312}
12313
Michael Chanb5d37722006-09-27 16:06:21 -070012314static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
12315{
12316 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012317 tg3_flag_set(tp, NVRAM_BUFFERED);
Michael Chanb5d37722006-09-27 16:06:21 -070012318 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12319}
12320
Matt Carlson321d32a2008-11-21 17:22:19 -080012321static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
12322{
12323 u32 nvcfg1;
12324
12325 nvcfg1 = tr32(NVRAM_CFG1);
12326
12327 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12328 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
12329 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
12330 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012331 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson321d32a2008-11-21 17:22:19 -080012332 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12333
12334 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12335 tw32(NVRAM_CFG1, nvcfg1);
12336 return;
12337 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12338 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
12339 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
12340 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
12341 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
12342 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
12343 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
12344 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012345 tg3_flag_set(tp, NVRAM_BUFFERED);
12346 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080012347
12348 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12349 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12350 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
12351 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
12352 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12353 break;
12354 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
12355 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
12356 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12357 break;
12358 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
12359 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
12360 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12361 break;
12362 }
12363 break;
12364 case FLASH_5752VENDOR_ST_M45PE10:
12365 case FLASH_5752VENDOR_ST_M45PE20:
12366 case FLASH_5752VENDOR_ST_M45PE40:
12367 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012368 tg3_flag_set(tp, NVRAM_BUFFERED);
12369 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080012370
12371 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12372 case FLASH_5752VENDOR_ST_M45PE10:
12373 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12374 break;
12375 case FLASH_5752VENDOR_ST_M45PE20:
12376 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12377 break;
12378 case FLASH_5752VENDOR_ST_M45PE40:
12379 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12380 break;
12381 }
12382 break;
12383 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012384 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson321d32a2008-11-21 17:22:19 -080012385 return;
12386 }
12387
Matt Carlsona1b950d2009-09-01 13:20:17 +000012388 tg3_nvram_get_pagesize(tp, nvcfg1);
12389 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012390 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012391}
12392
12393
12394static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
12395{
12396 u32 nvcfg1;
12397
12398 nvcfg1 = tr32(NVRAM_CFG1);
12399
12400 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12401 case FLASH_5717VENDOR_ATMEL_EEPROM:
12402 case FLASH_5717VENDOR_MICRO_EEPROM:
12403 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012404 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012405 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12406
12407 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12408 tw32(NVRAM_CFG1, nvcfg1);
12409 return;
12410 case FLASH_5717VENDOR_ATMEL_MDB011D:
12411 case FLASH_5717VENDOR_ATMEL_ADB011B:
12412 case FLASH_5717VENDOR_ATMEL_ADB011D:
12413 case FLASH_5717VENDOR_ATMEL_MDB021D:
12414 case FLASH_5717VENDOR_ATMEL_ADB021B:
12415 case FLASH_5717VENDOR_ATMEL_ADB021D:
12416 case FLASH_5717VENDOR_ATMEL_45USPT:
12417 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012418 tg3_flag_set(tp, NVRAM_BUFFERED);
12419 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012420
12421 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12422 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012423 /* Detect size with tg3_nvram_get_size() */
12424 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012425 case FLASH_5717VENDOR_ATMEL_ADB021B:
12426 case FLASH_5717VENDOR_ATMEL_ADB021D:
12427 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12428 break;
12429 default:
12430 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12431 break;
12432 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012433 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012434 case FLASH_5717VENDOR_ST_M_M25PE10:
12435 case FLASH_5717VENDOR_ST_A_M25PE10:
12436 case FLASH_5717VENDOR_ST_M_M45PE10:
12437 case FLASH_5717VENDOR_ST_A_M45PE10:
12438 case FLASH_5717VENDOR_ST_M_M25PE20:
12439 case FLASH_5717VENDOR_ST_A_M25PE20:
12440 case FLASH_5717VENDOR_ST_M_M45PE20:
12441 case FLASH_5717VENDOR_ST_A_M45PE20:
12442 case FLASH_5717VENDOR_ST_25USPT:
12443 case FLASH_5717VENDOR_ST_45USPT:
12444 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012445 tg3_flag_set(tp, NVRAM_BUFFERED);
12446 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012447
12448 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12449 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012450 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012451 /* Detect size with tg3_nvram_get_size() */
12452 break;
12453 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012454 case FLASH_5717VENDOR_ST_A_M45PE20:
12455 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12456 break;
12457 default:
12458 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12459 break;
12460 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012461 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012462 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012463 tg3_flag_set(tp, NO_NVRAM);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012464 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080012465 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000012466
12467 tg3_nvram_get_pagesize(tp, nvcfg1);
12468 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012469 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson321d32a2008-11-21 17:22:19 -080012470}
12471
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012472static void __devinit tg3_get_5720_nvram_info(struct tg3 *tp)
12473{
12474 u32 nvcfg1, nvmpinstrp;
12475
12476 nvcfg1 = tr32(NVRAM_CFG1);
12477 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
12478
12479 switch (nvmpinstrp) {
12480 case FLASH_5720_EEPROM_HD:
12481 case FLASH_5720_EEPROM_LD:
12482 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012483 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012484
12485 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12486 tw32(NVRAM_CFG1, nvcfg1);
12487 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
12488 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12489 else
12490 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
12491 return;
12492 case FLASH_5720VENDOR_M_ATMEL_DB011D:
12493 case FLASH_5720VENDOR_A_ATMEL_DB011B:
12494 case FLASH_5720VENDOR_A_ATMEL_DB011D:
12495 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12496 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12497 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12498 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12499 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12500 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12501 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12502 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12503 case FLASH_5720VENDOR_ATMEL_45USPT:
12504 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012505 tg3_flag_set(tp, NVRAM_BUFFERED);
12506 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012507
12508 switch (nvmpinstrp) {
12509 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12510 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12511 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12512 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12513 break;
12514 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12515 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12516 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12517 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12518 break;
12519 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12520 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12521 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12522 break;
12523 default:
12524 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12525 break;
12526 }
12527 break;
12528 case FLASH_5720VENDOR_M_ST_M25PE10:
12529 case FLASH_5720VENDOR_M_ST_M45PE10:
12530 case FLASH_5720VENDOR_A_ST_M25PE10:
12531 case FLASH_5720VENDOR_A_ST_M45PE10:
12532 case FLASH_5720VENDOR_M_ST_M25PE20:
12533 case FLASH_5720VENDOR_M_ST_M45PE20:
12534 case FLASH_5720VENDOR_A_ST_M25PE20:
12535 case FLASH_5720VENDOR_A_ST_M45PE20:
12536 case FLASH_5720VENDOR_M_ST_M25PE40:
12537 case FLASH_5720VENDOR_M_ST_M45PE40:
12538 case FLASH_5720VENDOR_A_ST_M25PE40:
12539 case FLASH_5720VENDOR_A_ST_M45PE40:
12540 case FLASH_5720VENDOR_M_ST_M25PE80:
12541 case FLASH_5720VENDOR_M_ST_M45PE80:
12542 case FLASH_5720VENDOR_A_ST_M25PE80:
12543 case FLASH_5720VENDOR_A_ST_M45PE80:
12544 case FLASH_5720VENDOR_ST_25USPT:
12545 case FLASH_5720VENDOR_ST_45USPT:
12546 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012547 tg3_flag_set(tp, NVRAM_BUFFERED);
12548 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012549
12550 switch (nvmpinstrp) {
12551 case FLASH_5720VENDOR_M_ST_M25PE20:
12552 case FLASH_5720VENDOR_M_ST_M45PE20:
12553 case FLASH_5720VENDOR_A_ST_M25PE20:
12554 case FLASH_5720VENDOR_A_ST_M45PE20:
12555 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12556 break;
12557 case FLASH_5720VENDOR_M_ST_M25PE40:
12558 case FLASH_5720VENDOR_M_ST_M45PE40:
12559 case FLASH_5720VENDOR_A_ST_M25PE40:
12560 case FLASH_5720VENDOR_A_ST_M45PE40:
12561 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12562 break;
12563 case FLASH_5720VENDOR_M_ST_M25PE80:
12564 case FLASH_5720VENDOR_M_ST_M45PE80:
12565 case FLASH_5720VENDOR_A_ST_M25PE80:
12566 case FLASH_5720VENDOR_A_ST_M45PE80:
12567 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12568 break;
12569 default:
12570 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12571 break;
12572 }
12573 break;
12574 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012575 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012576 return;
12577 }
12578
12579 tg3_nvram_get_pagesize(tp, nvcfg1);
12580 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012581 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012582}
12583
Linus Torvalds1da177e2005-04-16 15:20:36 -070012584/* Chips other than 5700/5701 use the NVRAM for fetching info. */
12585static void __devinit tg3_nvram_init(struct tg3 *tp)
12586{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012587 tw32_f(GRC_EEPROM_ADDR,
12588 (EEPROM_ADDR_FSM_RESET |
12589 (EEPROM_DEFAULT_CLOCK_PERIOD <<
12590 EEPROM_ADDR_CLKPERD_SHIFT)));
12591
Michael Chan9d57f012006-12-07 00:23:25 -080012592 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012593
12594 /* Enable seeprom accesses. */
12595 tw32_f(GRC_LOCAL_CTRL,
12596 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
12597 udelay(100);
12598
12599 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
12600 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
Joe Perches63c3a662011-04-26 08:12:10 +000012601 tg3_flag_set(tp, NVRAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012602
Michael Chanec41c7d2006-01-17 02:40:55 -080012603 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000012604 netdev_warn(tp->dev,
12605 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000012606 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080012607 return;
12608 }
Michael Chane6af3012005-04-21 17:12:05 -070012609 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012610
Matt Carlson989a9d22007-05-05 11:51:05 -070012611 tp->nvram_size = 0;
12612
Michael Chan361b4ac2005-04-21 17:11:21 -070012613 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
12614 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080012615 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
12616 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070012617 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070012618 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
12619 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080012620 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070012621 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
12622 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070012623 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12624 tg3_get_5906_nvram_info(tp);
Matt Carlsonb703df62009-12-03 08:36:21 +000012625 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000012626 tg3_flag(tp, 57765_CLASS))
Matt Carlson321d32a2008-11-21 17:22:19 -080012627 tg3_get_57780_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012628 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
12629 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000012630 tg3_get_5717_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012631 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
12632 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070012633 else
12634 tg3_get_nvram_info(tp);
12635
Matt Carlson989a9d22007-05-05 11:51:05 -070012636 if (tp->nvram_size == 0)
12637 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012638
Michael Chane6af3012005-04-21 17:12:05 -070012639 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080012640 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012641
12642 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000012643 tg3_flag_clear(tp, NVRAM);
12644 tg3_flag_clear(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012645
12646 tg3_get_eeprom_size(tp);
12647 }
12648}
12649
Linus Torvalds1da177e2005-04-16 15:20:36 -070012650static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
12651 u32 offset, u32 len, u8 *buf)
12652{
12653 int i, j, rc = 0;
12654 u32 val;
12655
12656 for (i = 0; i < len; i += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012657 u32 addr;
Matt Carlsona9dc5292009-02-25 14:25:30 +000012658 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012659
12660 addr = offset + i;
12661
12662 memcpy(&data, buf + i, 4);
12663
Matt Carlson62cedd12009-04-20 14:52:29 -070012664 /*
12665 * The SEEPROM interface expects the data to always be opposite
12666 * the native endian format. We accomplish this by reversing
12667 * all the operations that would have been performed on the
12668 * data from a call to tg3_nvram_read_be32().
12669 */
12670 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012671
12672 val = tr32(GRC_EEPROM_ADDR);
12673 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
12674
12675 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
12676 EEPROM_ADDR_READ);
12677 tw32(GRC_EEPROM_ADDR, val |
12678 (0 << EEPROM_ADDR_DEVID_SHIFT) |
12679 (addr & EEPROM_ADDR_ADDR_MASK) |
12680 EEPROM_ADDR_START |
12681 EEPROM_ADDR_WRITE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012682
Michael Chan9d57f012006-12-07 00:23:25 -080012683 for (j = 0; j < 1000; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012684 val = tr32(GRC_EEPROM_ADDR);
12685
12686 if (val & EEPROM_ADDR_COMPLETE)
12687 break;
Michael Chan9d57f012006-12-07 00:23:25 -080012688 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012689 }
12690 if (!(val & EEPROM_ADDR_COMPLETE)) {
12691 rc = -EBUSY;
12692 break;
12693 }
12694 }
12695
12696 return rc;
12697}
12698
12699/* offset and length are dword aligned */
12700static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
12701 u8 *buf)
12702{
12703 int ret = 0;
12704 u32 pagesize = tp->nvram_pagesize;
12705 u32 pagemask = pagesize - 1;
12706 u32 nvram_cmd;
12707 u8 *tmp;
12708
12709 tmp = kmalloc(pagesize, GFP_KERNEL);
12710 if (tmp == NULL)
12711 return -ENOMEM;
12712
12713 while (len) {
12714 int j;
Michael Chane6af3012005-04-21 17:12:05 -070012715 u32 phy_addr, page_off, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012716
12717 phy_addr = offset & ~pagemask;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012718
Linus Torvalds1da177e2005-04-16 15:20:36 -070012719 for (j = 0; j < pagesize; j += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012720 ret = tg3_nvram_read_be32(tp, phy_addr + j,
12721 (__be32 *) (tmp + j));
12722 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012723 break;
12724 }
12725 if (ret)
12726 break;
12727
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012728 page_off = offset & pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012729 size = pagesize;
12730 if (len < size)
12731 size = len;
12732
12733 len -= size;
12734
12735 memcpy(tmp + page_off, buf, size);
12736
12737 offset = offset + (pagesize - page_off);
12738
Michael Chane6af3012005-04-21 17:12:05 -070012739 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012740
12741 /*
12742 * Before we can erase the flash page, we need
12743 * to issue a special "write enable" command.
12744 */
12745 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12746
12747 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12748 break;
12749
12750 /* Erase the target page */
12751 tw32(NVRAM_ADDR, phy_addr);
12752
12753 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
12754 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
12755
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012756 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012757 break;
12758
12759 /* Issue another write enable to start the write. */
12760 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12761
12762 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12763 break;
12764
12765 for (j = 0; j < pagesize; j += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012766 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012767
Al Virob9fc7dc2007-12-17 22:59:57 -080012768 data = *((__be32 *) (tmp + j));
Matt Carlsona9dc5292009-02-25 14:25:30 +000012769
Al Virob9fc7dc2007-12-17 22:59:57 -080012770 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012771
12772 tw32(NVRAM_ADDR, phy_addr + j);
12773
12774 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
12775 NVRAM_CMD_WR;
12776
12777 if (j == 0)
12778 nvram_cmd |= NVRAM_CMD_FIRST;
12779 else if (j == (pagesize - 4))
12780 nvram_cmd |= NVRAM_CMD_LAST;
12781
12782 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12783 break;
12784 }
12785 if (ret)
12786 break;
12787 }
12788
12789 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12790 tg3_nvram_exec_cmd(tp, nvram_cmd);
12791
12792 kfree(tmp);
12793
12794 return ret;
12795}
12796
12797/* offset and length are dword aligned */
12798static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
12799 u8 *buf)
12800{
12801 int i, ret = 0;
12802
12803 for (i = 0; i < len; i += 4, offset += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012804 u32 page_off, phy_addr, nvram_cmd;
12805 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012806
12807 memcpy(&data, buf + i, 4);
Al Virob9fc7dc2007-12-17 22:59:57 -080012808 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012809
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012810 page_off = offset % tp->nvram_pagesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012811
Michael Chan18201802006-03-20 22:29:15 -080012812 phy_addr = tg3_nvram_phys_addr(tp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012813
12814 tw32(NVRAM_ADDR, phy_addr);
12815
12816 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
12817
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012818 if (page_off == 0 || i == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012819 nvram_cmd |= NVRAM_CMD_FIRST;
Michael Chanf6d9a252006-04-29 19:00:24 -070012820 if (page_off == (tp->nvram_pagesize - 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012821 nvram_cmd |= NVRAM_CMD_LAST;
12822
12823 if (i == (len - 4))
12824 nvram_cmd |= NVRAM_CMD_LAST;
12825
Matt Carlson321d32a2008-11-21 17:22:19 -080012826 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
Joe Perches63c3a662011-04-26 08:12:10 +000012827 !tg3_flag(tp, 5755_PLUS) &&
Michael Chan4c987482005-09-05 17:52:38 -070012828 (tp->nvram_jedecnum == JEDEC_ST) &&
12829 (nvram_cmd & NVRAM_CMD_FIRST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012830
12831 if ((ret = tg3_nvram_exec_cmd(tp,
12832 NVRAM_CMD_WREN | NVRAM_CMD_GO |
12833 NVRAM_CMD_DONE)))
12834
12835 break;
12836 }
Joe Perches63c3a662011-04-26 08:12:10 +000012837 if (!tg3_flag(tp, FLASH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012838 /* We always do complete word writes to eeprom. */
12839 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
12840 }
12841
12842 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12843 break;
12844 }
12845 return ret;
12846}
12847
12848/* offset and length are dword aligned */
12849static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
12850{
12851 int ret;
12852
Joe Perches63c3a662011-04-26 08:12:10 +000012853 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
Michael Chan314fba32005-04-21 17:07:04 -070012854 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
12855 ~GRC_LCLCTRL_GPIO_OUTPUT1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012856 udelay(40);
12857 }
12858
Joe Perches63c3a662011-04-26 08:12:10 +000012859 if (!tg3_flag(tp, NVRAM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012860 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
Matt Carlson859a5882010-04-05 10:19:28 +000012861 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012862 u32 grc_mode;
12863
Michael Chanec41c7d2006-01-17 02:40:55 -080012864 ret = tg3_nvram_lock(tp);
12865 if (ret)
12866 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012867
Michael Chane6af3012005-04-21 17:12:05 -070012868 tg3_enable_nvram_access(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000012869 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012870 tw32(NVRAM_WRITE1, 0x406);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012871
12872 grc_mode = tr32(GRC_MODE);
12873 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
12874
Joe Perches63c3a662011-04-26 08:12:10 +000012875 if (tg3_flag(tp, NVRAM_BUFFERED) || !tg3_flag(tp, FLASH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012876 ret = tg3_nvram_write_block_buffered(tp, offset, len,
12877 buf);
Matt Carlson859a5882010-04-05 10:19:28 +000012878 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012879 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
12880 buf);
12881 }
12882
12883 grc_mode = tr32(GRC_MODE);
12884 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
12885
Michael Chane6af3012005-04-21 17:12:05 -070012886 tg3_disable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012887 tg3_nvram_unlock(tp);
12888 }
12889
Joe Perches63c3a662011-04-26 08:12:10 +000012890 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
Michael Chan314fba32005-04-21 17:07:04 -070012891 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012892 udelay(40);
12893 }
12894
12895 return ret;
12896}
12897
12898struct subsys_tbl_ent {
12899 u16 subsys_vendor, subsys_devid;
12900 u32 phy_id;
12901};
12902
Matt Carlson24daf2b2010-02-17 15:17:02 +000012903static struct subsys_tbl_ent subsys_id_to_phy_id[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012904 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012905 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012906 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012907 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012908 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012909 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012910 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012911 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12912 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
12913 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012914 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012915 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012916 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012917 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12918 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
12919 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012920 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012921 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012922 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012923 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012924 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012925 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012926 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012927
12928 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012929 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012930 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012931 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012932 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012933 { TG3PCI_SUBVENDOR_ID_3COM,
12934 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
12935 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012936 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012937 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012938 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012939
12940 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012941 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012942 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012943 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012944 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012945 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012946 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012947 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012948 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012949
12950 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012951 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012952 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012953 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012954 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012955 { TG3PCI_SUBVENDOR_ID_COMPAQ,
12956 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
12957 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012958 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012959 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012960 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012961
12962 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012963 { TG3PCI_SUBVENDOR_ID_IBM,
12964 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012965};
12966
Matt Carlson24daf2b2010-02-17 15:17:02 +000012967static struct subsys_tbl_ent * __devinit tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012968{
12969 int i;
12970
12971 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
12972 if ((subsys_id_to_phy_id[i].subsys_vendor ==
12973 tp->pdev->subsystem_vendor) &&
12974 (subsys_id_to_phy_id[i].subsys_devid ==
12975 tp->pdev->subsystem_device))
12976 return &subsys_id_to_phy_id[i];
12977 }
12978 return NULL;
12979}
12980
Michael Chan7d0c41e2005-04-21 17:06:20 -070012981static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012982{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012983 u32 val;
David S. Millerf49639e2006-06-09 11:58:36 -070012984
Matt Carlson79eb6902010-02-17 15:17:03 +000012985 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012986 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12987
Gary Zambranoa85feb82007-05-05 11:52:19 -070012988 /* Assume an onboard device and WOL capable by default. */
Joe Perches63c3a662011-04-26 08:12:10 +000012989 tg3_flag_set(tp, EEPROM_WRITE_PROT);
12990 tg3_flag_set(tp, WOL_CAP);
David S. Miller72b845e2006-03-14 14:11:48 -080012991
Michael Chanb5d37722006-09-27 16:06:21 -070012992 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080012993 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012994 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
12995 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080012996 }
Matt Carlson0527ba32007-10-10 18:03:30 -070012997 val = tr32(VCPU_CFGSHDW);
12998 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Joe Perches63c3a662011-04-26 08:12:10 +000012999 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson0527ba32007-10-10 18:03:30 -070013000 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013001 (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013002 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013003 device_set_wakeup_enable(&tp->pdev->dev, true);
13004 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080013005 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070013006 }
13007
Linus Torvalds1da177e2005-04-16 15:20:36 -070013008 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
13009 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
13010 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070013011 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070013012 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013013
13014 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
13015 tp->nic_sram_data_cfg = nic_cfg;
13016
13017 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
13018 ver >>= NIC_SRAM_DATA_VER_SHIFT;
Matt Carlson6ff6f812011-05-19 12:12:54 +000013019 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13020 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13021 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070013022 (ver > 0) && (ver < 0x100))
13023 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
13024
Matt Carlsona9daf362008-05-25 23:49:44 -070013025 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
13026 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
13027
Linus Torvalds1da177e2005-04-16 15:20:36 -070013028 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
13029 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
13030 eeprom_phy_serdes = 1;
13031
13032 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
13033 if (nic_phy_id != 0) {
13034 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
13035 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
13036
13037 eeprom_phy_id = (id1 >> 16) << 10;
13038 eeprom_phy_id |= (id2 & 0xfc00) << 16;
13039 eeprom_phy_id |= (id2 & 0x03ff) << 0;
13040 } else
13041 eeprom_phy_id = 0;
13042
Michael Chan7d0c41e2005-04-21 17:06:20 -070013043 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070013044 if (eeprom_phy_serdes) {
Joe Perches63c3a662011-04-26 08:12:10 +000013045 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013046 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000013047 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013048 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070013049 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070013050
Joe Perches63c3a662011-04-26 08:12:10 +000013051 if (tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013052 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
13053 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070013054 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070013055 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
13056
13057 switch (led_cfg) {
13058 default:
13059 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
13060 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13061 break;
13062
13063 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
13064 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
13065 break;
13066
13067 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
13068 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070013069
13070 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
13071 * read on some older 5700/5701 bootcode.
13072 */
13073 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
13074 ASIC_REV_5700 ||
13075 GET_ASIC_REV(tp->pci_chip_rev_id) ==
13076 ASIC_REV_5701)
13077 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13078
Linus Torvalds1da177e2005-04-16 15:20:36 -070013079 break;
13080
13081 case SHASTA_EXT_LED_SHARED:
13082 tp->led_ctrl = LED_CTRL_MODE_SHARED;
13083 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
13084 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
13085 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
13086 LED_CTRL_MODE_PHY_2);
13087 break;
13088
13089 case SHASTA_EXT_LED_MAC:
13090 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
13091 break;
13092
13093 case SHASTA_EXT_LED_COMBO:
13094 tp->led_ctrl = LED_CTRL_MODE_COMBO;
13095 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
13096 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
13097 LED_CTRL_MODE_PHY_2);
13098 break;
13099
Stephen Hemminger855e1112008-04-16 16:37:28 -070013100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013101
13102 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13103 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
13104 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
13105 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
13106
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013107 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
13108 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080013109
Michael Chan9d26e212006-12-07 00:21:14 -080013110 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Joe Perches63c3a662011-04-26 08:12:10 +000013111 tg3_flag_set(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080013112 if ((tp->pdev->subsystem_vendor ==
13113 PCI_VENDOR_ID_ARIMA) &&
13114 (tp->pdev->subsystem_device == 0x205a ||
13115 tp->pdev->subsystem_device == 0x2063))
Joe Perches63c3a662011-04-26 08:12:10 +000013116 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080013117 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000013118 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
13119 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080013120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013121
13122 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +000013123 tg3_flag_set(tp, ENABLE_ASF);
13124 if (tg3_flag(tp, 5750_PLUS))
13125 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013126 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080013127
13128 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013129 tg3_flag(tp, 5750_PLUS))
13130 tg3_flag_set(tp, ENABLE_APE);
Matt Carlsonb2b98d42008-11-03 16:52:32 -080013131
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013132 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070013133 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
Joe Perches63c3a662011-04-26 08:12:10 +000013134 tg3_flag_clear(tp, WOL_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013135
Joe Perches63c3a662011-04-26 08:12:10 +000013136 if (tg3_flag(tp, WOL_CAP) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013137 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013138 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013139 device_set_wakeup_enable(&tp->pdev->dev, true);
13140 }
Matt Carlson0527ba32007-10-10 18:03:30 -070013141
Linus Torvalds1da177e2005-04-16 15:20:36 -070013142 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013143 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013144
13145 /* serdes signal pre-emphasis in register 0x590 set by */
13146 /* bootcode if bit 18 is set */
13147 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013148 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070013149
Joe Perches63c3a662011-04-26 08:12:10 +000013150 if ((tg3_flag(tp, 57765_PLUS) ||
13151 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
13152 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080013153 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013154 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080013155
Joe Perches63c3a662011-04-26 08:12:10 +000013156 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson8c69b1e2010-08-02 11:26:00 +000013157 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +000013158 !tg3_flag(tp, 57765_PLUS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070013159 u32 cfg3;
13160
13161 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
13162 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
Joe Perches63c3a662011-04-26 08:12:10 +000013163 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson8ed5d972007-05-07 00:25:49 -070013164 }
Matt Carlsona9daf362008-05-25 23:49:44 -070013165
Matt Carlson14417062010-02-17 15:16:59 +000013166 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
Joe Perches63c3a662011-04-26 08:12:10 +000013167 tg3_flag_set(tp, RGMII_INBAND_DISABLE);
Matt Carlsona9daf362008-05-25 23:49:44 -070013168 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000013169 tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
Matt Carlsona9daf362008-05-25 23:49:44 -070013170 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000013171 tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013172 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080013173done:
Joe Perches63c3a662011-04-26 08:12:10 +000013174 if (tg3_flag(tp, WOL_CAP))
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000013175 device_set_wakeup_enable(&tp->pdev->dev,
Joe Perches63c3a662011-04-26 08:12:10 +000013176 tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000013177 else
13178 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070013179}
13180
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013181static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
13182{
13183 int i;
13184 u32 val;
13185
13186 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
13187 tw32(OTP_CTRL, cmd);
13188
13189 /* Wait for up to 1 ms for command to execute. */
13190 for (i = 0; i < 100; i++) {
13191 val = tr32(OTP_STATUS);
13192 if (val & OTP_STATUS_CMD_DONE)
13193 break;
13194 udelay(10);
13195 }
13196
13197 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
13198}
13199
13200/* Read the gphy configuration from the OTP region of the chip. The gphy
13201 * configuration is a 32-bit value that straddles the alignment boundary.
13202 * We do two 32-bit reads and then shift and merge the results.
13203 */
13204static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
13205{
13206 u32 bhalf_otp, thalf_otp;
13207
13208 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
13209
13210 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
13211 return 0;
13212
13213 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
13214
13215 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
13216 return 0;
13217
13218 thalf_otp = tr32(OTP_READ_DATA);
13219
13220 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
13221
13222 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
13223 return 0;
13224
13225 bhalf_otp = tr32(OTP_READ_DATA);
13226
13227 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
13228}
13229
Matt Carlsone256f8a2011-03-09 16:58:24 +000013230static void __devinit tg3_phy_init_link_config(struct tg3 *tp)
13231{
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +000013232 u32 adv = ADVERTISED_Autoneg;
Matt Carlsone256f8a2011-03-09 16:58:24 +000013233
13234 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
13235 adv |= ADVERTISED_1000baseT_Half |
13236 ADVERTISED_1000baseT_Full;
13237
13238 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
13239 adv |= ADVERTISED_100baseT_Half |
13240 ADVERTISED_100baseT_Full |
13241 ADVERTISED_10baseT_Half |
13242 ADVERTISED_10baseT_Full |
13243 ADVERTISED_TP;
13244 else
13245 adv |= ADVERTISED_FIBRE;
13246
13247 tp->link_config.advertising = adv;
13248 tp->link_config.speed = SPEED_INVALID;
13249 tp->link_config.duplex = DUPLEX_INVALID;
13250 tp->link_config.autoneg = AUTONEG_ENABLE;
13251 tp->link_config.active_speed = SPEED_INVALID;
13252 tp->link_config.active_duplex = DUPLEX_INVALID;
13253 tp->link_config.orig_speed = SPEED_INVALID;
13254 tp->link_config.orig_duplex = DUPLEX_INVALID;
13255 tp->link_config.orig_autoneg = AUTONEG_INVALID;
13256}
13257
Michael Chan7d0c41e2005-04-21 17:06:20 -070013258static int __devinit tg3_phy_probe(struct tg3 *tp)
13259{
13260 u32 hw_phy_id_1, hw_phy_id_2;
13261 u32 hw_phy_id, hw_phy_id_masked;
13262 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013263
Matt Carlsone256f8a2011-03-09 16:58:24 +000013264 /* flow control autonegotiation is default behavior */
Joe Perches63c3a662011-04-26 08:12:10 +000013265 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsone256f8a2011-03-09 16:58:24 +000013266 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
13267
Joe Perches63c3a662011-04-26 08:12:10 +000013268 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013269 return tg3_phy_init(tp);
13270
Linus Torvalds1da177e2005-04-16 15:20:36 -070013271 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010013272 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013273 */
13274 err = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000013275 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000013276 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013277 } else {
13278 /* Now read the physical PHY_ID from the chip and verify
13279 * that it is sane. If it doesn't look good, we fall back
13280 * to either the hard-coded table based PHY_ID and failing
13281 * that the value found in the eeprom area.
13282 */
13283 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
13284 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
13285
13286 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
13287 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
13288 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
13289
Matt Carlson79eb6902010-02-17 15:17:03 +000013290 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013291 }
13292
Matt Carlson79eb6902010-02-17 15:17:03 +000013293 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013294 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000013295 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013296 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070013297 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013298 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013299 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000013300 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070013301 /* Do nothing, phy ID already set up in
13302 * tg3_get_eeprom_hw_cfg().
13303 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070013304 } else {
13305 struct subsys_tbl_ent *p;
13306
13307 /* No eeprom signature? Try the hardcoded
13308 * subsys device table.
13309 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013310 p = tg3_lookup_by_subsys(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013311 if (!p)
13312 return -ENODEV;
13313
13314 tp->phy_id = p->phy_id;
13315 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000013316 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013317 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013318 }
13319 }
13320
Matt Carlsona6b68da2010-12-06 08:28:52 +000013321 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Matt Carlson5baa5e92011-07-20 10:20:53 +000013322 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13323 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720 ||
13324 (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +000013325 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0) ||
13326 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
13327 tp->pci_chip_rev_id != CHIPREV_ID_57765_A0)))
Matt Carlson52b02d02010-10-14 10:37:41 +000013328 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
13329
Matt Carlsone256f8a2011-03-09 16:58:24 +000013330 tg3_phy_init_link_config(tp);
13331
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013332 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013333 !tg3_flag(tp, ENABLE_APE) &&
13334 !tg3_flag(tp, ENABLE_ASF)) {
Matt Carlsone2bf73e2011-12-08 14:40:15 +000013335 u32 bmsr, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013336
13337 tg3_readphy(tp, MII_BMSR, &bmsr);
13338 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
13339 (bmsr & BMSR_LSTATUS))
13340 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013341
Linus Torvalds1da177e2005-04-16 15:20:36 -070013342 err = tg3_phy_reset(tp);
13343 if (err)
13344 return err;
13345
Matt Carlson42b64a42011-05-19 12:12:49 +000013346 tg3_phy_set_wirespeed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013347
Matt Carlsone2bf73e2011-12-08 14:40:15 +000013348 if (!tg3_phy_copper_an_config_ok(tp, &dummy)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000013349 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
13350 tp->link_config.flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013351
13352 tg3_writephy(tp, MII_BMCR,
13353 BMCR_ANENABLE | BMCR_ANRESTART);
13354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013355 }
13356
13357skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000013358 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013359 err = tg3_init_5401phy_dsp(tp);
13360 if (err)
13361 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013362
Linus Torvalds1da177e2005-04-16 15:20:36 -070013363 err = tg3_init_5401phy_dsp(tp);
13364 }
13365
Linus Torvalds1da177e2005-04-16 15:20:36 -070013366 return err;
13367}
13368
Matt Carlson184b8902010-04-05 10:19:25 +000013369static void __devinit tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013370{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013371 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013372 unsigned int block_end, rosize, len;
Matt Carlson535a4902011-07-20 10:20:56 +000013373 u32 vpdlen;
Matt Carlson184b8902010-04-05 10:19:25 +000013374 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013375
Matt Carlson535a4902011-07-20 10:20:56 +000013376 vpd_data = (u8 *)tg3_vpd_readblock(tp, &vpdlen);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013377 if (!vpd_data)
13378 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013379
Matt Carlson535a4902011-07-20 10:20:56 +000013380 i = pci_vpd_find_tag(vpd_data, 0, vpdlen, PCI_VPD_LRDT_RO_DATA);
Matt Carlson4181b2c2010-02-26 14:04:45 +000013381 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013382 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013383
13384 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
13385 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
13386 i += PCI_VPD_LRDT_TAG_SIZE;
13387
Matt Carlson535a4902011-07-20 10:20:56 +000013388 if (block_end > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000013389 goto out_not_found;
13390
Matt Carlson184b8902010-04-05 10:19:25 +000013391 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13392 PCI_VPD_RO_KEYWORD_MFR_ID);
13393 if (j > 0) {
13394 len = pci_vpd_info_field_size(&vpd_data[j]);
13395
13396 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13397 if (j + len > block_end || len != 4 ||
13398 memcmp(&vpd_data[j], "1028", 4))
13399 goto partno;
13400
13401 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13402 PCI_VPD_RO_KEYWORD_VENDOR0);
13403 if (j < 0)
13404 goto partno;
13405
13406 len = pci_vpd_info_field_size(&vpd_data[j]);
13407
13408 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13409 if (j + len > block_end)
13410 goto partno;
13411
13412 memcpy(tp->fw_ver, &vpd_data[j], len);
Matt Carlson535a4902011-07-20 10:20:56 +000013413 strncat(tp->fw_ver, " bc ", vpdlen - len - 1);
Matt Carlson184b8902010-04-05 10:19:25 +000013414 }
13415
13416partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000013417 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13418 PCI_VPD_RO_KEYWORD_PARTNO);
13419 if (i < 0)
13420 goto out_not_found;
13421
13422 len = pci_vpd_info_field_size(&vpd_data[i]);
13423
13424 i += PCI_VPD_INFO_FLD_HDR_SIZE;
13425 if (len > TG3_BPN_SIZE ||
Matt Carlson535a4902011-07-20 10:20:56 +000013426 (len + i) > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000013427 goto out_not_found;
13428
13429 memcpy(tp->board_part_number, &vpd_data[i], len);
13430
Linus Torvalds1da177e2005-04-16 15:20:36 -070013431out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013432 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000013433 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013434 return;
13435
13436out_no_vpd:
Matt Carlson37a949c2010-09-30 10:34:33 +000013437 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
13438 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717)
13439 strcpy(tp->board_part_number, "BCM5717");
13440 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
13441 strcpy(tp->board_part_number, "BCM5718");
13442 else
13443 goto nomatch;
13444 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
13445 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
13446 strcpy(tp->board_part_number, "BCM57780");
13447 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
13448 strcpy(tp->board_part_number, "BCM57760");
13449 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
13450 strcpy(tp->board_part_number, "BCM57790");
13451 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
13452 strcpy(tp->board_part_number, "BCM57788");
13453 else
13454 goto nomatch;
13455 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
13456 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
13457 strcpy(tp->board_part_number, "BCM57761");
13458 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
13459 strcpy(tp->board_part_number, "BCM57765");
13460 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
13461 strcpy(tp->board_part_number, "BCM57781");
13462 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
13463 strcpy(tp->board_part_number, "BCM57785");
13464 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
13465 strcpy(tp->board_part_number, "BCM57791");
13466 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13467 strcpy(tp->board_part_number, "BCM57795");
13468 else
13469 goto nomatch;
Matt Carlson55086ad2011-12-14 11:09:59 +000013470 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57766) {
13471 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762)
13472 strcpy(tp->board_part_number, "BCM57762");
13473 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766)
13474 strcpy(tp->board_part_number, "BCM57766");
13475 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782)
13476 strcpy(tp->board_part_number, "BCM57782");
13477 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
13478 strcpy(tp->board_part_number, "BCM57786");
13479 else
13480 goto nomatch;
Matt Carlson37a949c2010-09-30 10:34:33 +000013481 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070013482 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000013483 } else {
13484nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070013485 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000013486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013487}
13488
Matt Carlson9c8a6202007-10-21 16:16:08 -070013489static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
13490{
13491 u32 val;
13492
Matt Carlsone4f34112009-02-25 14:25:00 +000013493 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013494 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013495 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013496 val != 0)
13497 return 0;
13498
13499 return 1;
13500}
13501
Matt Carlsonacd9c112009-02-25 14:26:33 +000013502static void __devinit tg3_read_bc_ver(struct tg3 *tp)
13503{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013504 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000013505 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013506 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013507
13508 if (tg3_nvram_read(tp, 0xc, &offset) ||
13509 tg3_nvram_read(tp, 0x4, &start))
13510 return;
13511
13512 offset = tg3_nvram_logical_addr(tp, offset);
13513
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013514 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013515 return;
13516
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013517 if ((val & 0xfc000000) == 0x0c000000) {
13518 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013519 return;
13520
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013521 if (val == 0)
13522 newver = true;
13523 }
13524
Matt Carlson75f99362010-04-05 10:19:24 +000013525 dst_off = strlen(tp->fw_ver);
13526
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013527 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000013528 if (TG3_VER_SIZE - dst_off < 16 ||
13529 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013530 return;
13531
13532 offset = offset + ver_offset - start;
13533 for (i = 0; i < 16; i += 4) {
13534 __be32 v;
13535 if (tg3_nvram_read_be32(tp, offset + i, &v))
13536 return;
13537
Matt Carlson75f99362010-04-05 10:19:24 +000013538 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013539 }
13540 } else {
13541 u32 major, minor;
13542
13543 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
13544 return;
13545
13546 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
13547 TG3_NVM_BCVER_MAJSFT;
13548 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000013549 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
13550 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013551 }
13552}
13553
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013554static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
13555{
13556 u32 val, major, minor;
13557
13558 /* Use native endian representation */
13559 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
13560 return;
13561
13562 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
13563 TG3_NVM_HWSB_CFG1_MAJSFT;
13564 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
13565 TG3_NVM_HWSB_CFG1_MINSFT;
13566
13567 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
13568}
13569
Matt Carlsondfe00d72008-11-21 17:19:41 -080013570static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
13571{
13572 u32 offset, major, minor, build;
13573
Matt Carlson75f99362010-04-05 10:19:24 +000013574 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013575
13576 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
13577 return;
13578
13579 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
13580 case TG3_EEPROM_SB_REVISION_0:
13581 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
13582 break;
13583 case TG3_EEPROM_SB_REVISION_2:
13584 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
13585 break;
13586 case TG3_EEPROM_SB_REVISION_3:
13587 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
13588 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000013589 case TG3_EEPROM_SB_REVISION_4:
13590 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
13591 break;
13592 case TG3_EEPROM_SB_REVISION_5:
13593 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
13594 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000013595 case TG3_EEPROM_SB_REVISION_6:
13596 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
13597 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013598 default:
13599 return;
13600 }
13601
Matt Carlsone4f34112009-02-25 14:25:00 +000013602 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080013603 return;
13604
13605 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
13606 TG3_EEPROM_SB_EDH_BLD_SHFT;
13607 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
13608 TG3_EEPROM_SB_EDH_MAJ_SHFT;
13609 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
13610
13611 if (minor > 99 || build > 26)
13612 return;
13613
Matt Carlson75f99362010-04-05 10:19:24 +000013614 offset = strlen(tp->fw_ver);
13615 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
13616 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013617
13618 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000013619 offset = strlen(tp->fw_ver);
13620 if (offset < TG3_VER_SIZE - 1)
13621 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013622 }
13623}
13624
Matt Carlsonacd9c112009-02-25 14:26:33 +000013625static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080013626{
13627 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013628 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070013629
13630 for (offset = TG3_NVM_DIR_START;
13631 offset < TG3_NVM_DIR_END;
13632 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000013633 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013634 return;
13635
13636 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
13637 break;
13638 }
13639
13640 if (offset == TG3_NVM_DIR_END)
13641 return;
13642
Joe Perches63c3a662011-04-26 08:12:10 +000013643 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013644 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000013645 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013646 return;
13647
Matt Carlsone4f34112009-02-25 14:25:00 +000013648 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013649 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013650 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013651 return;
13652
13653 offset += val - start;
13654
Matt Carlsonacd9c112009-02-25 14:26:33 +000013655 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013656
Matt Carlsonacd9c112009-02-25 14:26:33 +000013657 tp->fw_ver[vlen++] = ',';
13658 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070013659
13660 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000013661 __be32 v;
13662 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013663 return;
13664
Al Virob9fc7dc2007-12-17 22:59:57 -080013665 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013666
Matt Carlsonacd9c112009-02-25 14:26:33 +000013667 if (vlen > TG3_VER_SIZE - sizeof(v)) {
13668 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013669 break;
13670 }
13671
Matt Carlsonacd9c112009-02-25 14:26:33 +000013672 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
13673 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013674 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000013675}
13676
Matt Carlson7fd76442009-02-25 14:27:20 +000013677static void __devinit tg3_read_dash_ver(struct tg3 *tp)
13678{
13679 int vlen;
13680 u32 apedata;
Matt Carlsonecc79642010-08-02 11:26:01 +000013681 char *fwtype;
Matt Carlson7fd76442009-02-25 14:27:20 +000013682
Joe Perches63c3a662011-04-26 08:12:10 +000013683 if (!tg3_flag(tp, ENABLE_APE) || !tg3_flag(tp, ENABLE_ASF))
Matt Carlson7fd76442009-02-25 14:27:20 +000013684 return;
13685
13686 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
13687 if (apedata != APE_SEG_SIG_MAGIC)
13688 return;
13689
13690 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
13691 if (!(apedata & APE_FW_STATUS_READY))
13692 return;
13693
13694 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
13695
Matt Carlsondc6d0742010-09-15 08:59:55 +000013696 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI) {
Joe Perches63c3a662011-04-26 08:12:10 +000013697 tg3_flag_set(tp, APE_HAS_NCSI);
Matt Carlsonecc79642010-08-02 11:26:01 +000013698 fwtype = "NCSI";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013699 } else {
Matt Carlsonecc79642010-08-02 11:26:01 +000013700 fwtype = "DASH";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013701 }
Matt Carlsonecc79642010-08-02 11:26:01 +000013702
Matt Carlson7fd76442009-02-25 14:27:20 +000013703 vlen = strlen(tp->fw_ver);
13704
Matt Carlsonecc79642010-08-02 11:26:01 +000013705 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
13706 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000013707 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
13708 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
13709 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
13710 (apedata & APE_FW_VERSION_BLDMSK));
13711}
13712
Matt Carlsonacd9c112009-02-25 14:26:33 +000013713static void __devinit tg3_read_fw_ver(struct tg3 *tp)
13714{
13715 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000013716 bool vpd_vers = false;
13717
13718 if (tp->fw_ver[0] != 0)
13719 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013720
Joe Perches63c3a662011-04-26 08:12:10 +000013721 if (tg3_flag(tp, NO_NVRAM)) {
Matt Carlson75f99362010-04-05 10:19:24 +000013722 strcat(tp->fw_ver, "sb");
Matt Carlsondf259d82009-04-20 06:57:14 +000013723 return;
13724 }
13725
Matt Carlsonacd9c112009-02-25 14:26:33 +000013726 if (tg3_nvram_read(tp, 0, &val))
13727 return;
13728
13729 if (val == TG3_EEPROM_MAGIC)
13730 tg3_read_bc_ver(tp);
13731 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
13732 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013733 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
13734 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013735 else
13736 return;
13737
Matt Carlsonc9cab242011-07-13 09:27:27 +000013738 if (vpd_vers)
Matt Carlson75f99362010-04-05 10:19:24 +000013739 goto done;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013740
Matt Carlsonc9cab242011-07-13 09:27:27 +000013741 if (tg3_flag(tp, ENABLE_APE)) {
13742 if (tg3_flag(tp, ENABLE_ASF))
13743 tg3_read_dash_ver(tp);
13744 } else if (tg3_flag(tp, ENABLE_ASF)) {
13745 tg3_read_mgmtfw_ver(tp);
13746 }
Matt Carlson9c8a6202007-10-21 16:16:08 -070013747
Matt Carlson75f99362010-04-05 10:19:24 +000013748done:
Matt Carlson9c8a6202007-10-21 16:16:08 -070013749 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080013750}
13751
Michael Chan7544b092007-05-05 13:08:32 -070013752static struct pci_dev * __devinit tg3_find_peer(struct tg3 *);
13753
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013754static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
13755{
Joe Perches63c3a662011-04-26 08:12:10 +000013756 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013757 return TG3_RX_RET_MAX_SIZE_5717;
Joe Perches63c3a662011-04-26 08:12:10 +000013758 else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013759 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013760 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000013761 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013762}
13763
Matt Carlson41434702011-03-09 16:58:22 +000013764static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080013765 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
13766 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
13767 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
13768 { },
13769};
13770
Linus Torvalds1da177e2005-04-16 15:20:36 -070013771static int __devinit tg3_get_invariants(struct tg3 *tp)
13772{
Linus Torvalds1da177e2005-04-16 15:20:36 -070013773 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013774 u32 pci_state_reg, grc_misc_cfg;
13775 u32 val;
13776 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013777 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013778
Linus Torvalds1da177e2005-04-16 15:20:36 -070013779 /* Force memory write invalidate off. If we leave it on,
13780 * then on 5700_BX chips we have to enable a workaround.
13781 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
13782 * to match the cacheline size. The Broadcom driver have this
13783 * workaround but turns MWI off all the times so never uses
13784 * it. This seems to suggest that the workaround is insufficient.
13785 */
13786 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13787 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
13788 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13789
Matt Carlson16821282011-07-13 09:27:28 +000013790 /* Important! -- Make sure register accesses are byteswapped
13791 * correctly. Also, for those chips that require it, make
13792 * sure that indirect register accesses are enabled before
13793 * the first operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013794 */
13795 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13796 &misc_ctrl_reg);
Matt Carlson16821282011-07-13 09:27:28 +000013797 tp->misc_host_ctrl |= (misc_ctrl_reg &
13798 MISC_HOST_CTRL_CHIPREV);
13799 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13800 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013801
13802 tp->pci_chip_rev_id = (misc_ctrl_reg >>
13803 MISC_HOST_CTRL_CHIPREV_SHIFT);
Matt Carlson795d01c2007-10-07 23:28:17 -070013804 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
13805 u32 prod_id_asic_rev;
13806
Matt Carlson5001e2f2009-11-13 13:03:51 +000013807 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
13808 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013809 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
13810 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013811 pci_read_config_dword(tp->pdev,
13812 TG3PCI_GEN2_PRODID_ASICREV,
13813 &prod_id_asic_rev);
Matt Carlsonb703df62009-12-03 08:36:21 +000013814 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
13815 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
13816 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
13817 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
13818 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000013819 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
13820 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762 ||
13821 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766 ||
13822 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782 ||
13823 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
Matt Carlsonb703df62009-12-03 08:36:21 +000013824 pci_read_config_dword(tp->pdev,
13825 TG3PCI_GEN15_PRODID_ASICREV,
13826 &prod_id_asic_rev);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013827 else
13828 pci_read_config_dword(tp->pdev, TG3PCI_PRODID_ASICREV,
13829 &prod_id_asic_rev);
13830
Matt Carlson321d32a2008-11-21 17:22:19 -080013831 tp->pci_chip_rev_id = prod_id_asic_rev;
Matt Carlson795d01c2007-10-07 23:28:17 -070013832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013833
Michael Chanff645be2005-04-21 17:09:53 -070013834 /* Wrong chip ID in 5752 A0. This code can be removed later
13835 * as A0 is not in production.
13836 */
13837 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
13838 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
13839
Michael Chan68929142005-08-09 20:17:14 -070013840 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
13841 * we need to disable memory and use config. cycles
13842 * only to access all registers. The 5702/03 chips
13843 * can mistakenly decode the special cycles from the
13844 * ICH chipsets as memory write cycles, causing corruption
13845 * of register and memory space. Only certain ICH bridges
13846 * will drive special cycles with non-zero data during the
13847 * address phase which can fall within the 5703's address
13848 * range. This is not an ICH bug as the PCI spec allows
13849 * non-zero address during special cycles. However, only
13850 * these ICH bridges are known to drive non-zero addresses
13851 * during special cycles.
13852 *
13853 * Since special cycles do not cross PCI bridges, we only
13854 * enable this workaround if the 5703 is on the secondary
13855 * bus of these ICH bridges.
13856 */
13857 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
13858 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
13859 static struct tg3_dev_id {
13860 u32 vendor;
13861 u32 device;
13862 u32 rev;
13863 } ich_chipsets[] = {
13864 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
13865 PCI_ANY_ID },
13866 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
13867 PCI_ANY_ID },
13868 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
13869 0xa },
13870 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
13871 PCI_ANY_ID },
13872 { },
13873 };
13874 struct tg3_dev_id *pci_id = &ich_chipsets[0];
13875 struct pci_dev *bridge = NULL;
13876
13877 while (pci_id->vendor != 0) {
13878 bridge = pci_get_device(pci_id->vendor, pci_id->device,
13879 bridge);
13880 if (!bridge) {
13881 pci_id++;
13882 continue;
13883 }
13884 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070013885 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070013886 continue;
13887 }
13888 if (bridge->subordinate &&
13889 (bridge->subordinate->number ==
13890 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013891 tg3_flag_set(tp, ICH_WORKAROUND);
Michael Chan68929142005-08-09 20:17:14 -070013892 pci_dev_put(bridge);
13893 break;
13894 }
13895 }
13896 }
13897
Matt Carlson6ff6f812011-05-19 12:12:54 +000013898 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Matt Carlson41588ba2008-04-19 18:12:33 -070013899 static struct tg3_dev_id {
13900 u32 vendor;
13901 u32 device;
13902 } bridge_chipsets[] = {
13903 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
13904 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
13905 { },
13906 };
13907 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
13908 struct pci_dev *bridge = NULL;
13909
13910 while (pci_id->vendor != 0) {
13911 bridge = pci_get_device(pci_id->vendor,
13912 pci_id->device,
13913 bridge);
13914 if (!bridge) {
13915 pci_id++;
13916 continue;
13917 }
13918 if (bridge->subordinate &&
13919 (bridge->subordinate->number <=
13920 tp->pdev->bus->number) &&
13921 (bridge->subordinate->subordinate >=
13922 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013923 tg3_flag_set(tp, 5701_DMA_BUG);
Matt Carlson41588ba2008-04-19 18:12:33 -070013924 pci_dev_put(bridge);
13925 break;
13926 }
13927 }
13928 }
13929
Michael Chan4a29cc22006-03-19 13:21:12 -080013930 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
13931 * DMA addresses > 40-bit. This bridge may have other additional
13932 * 57xx devices behind it in some 4-port NIC designs for example.
13933 * Any tg3 device found behind the bridge will also need the 40-bit
13934 * DMA workaround.
13935 */
Michael Chana4e2b342005-10-26 15:46:52 -070013936 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
13937 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Joe Perches63c3a662011-04-26 08:12:10 +000013938 tg3_flag_set(tp, 5780_CLASS);
13939 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4cf78e42005-07-25 12:29:19 -070013940 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a5882010-04-05 10:19:28 +000013941 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080013942 struct pci_dev *bridge = NULL;
13943
13944 do {
13945 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
13946 PCI_DEVICE_ID_SERVERWORKS_EPB,
13947 bridge);
13948 if (bridge && bridge->subordinate &&
13949 (bridge->subordinate->number <=
13950 tp->pdev->bus->number) &&
13951 (bridge->subordinate->subordinate >=
13952 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013953 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4a29cc22006-03-19 13:21:12 -080013954 pci_dev_put(bridge);
13955 break;
13956 }
13957 } while (bridge);
13958 }
Michael Chan4cf78e42005-07-25 12:29:19 -070013959
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013960 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Matt Carlson3a1e19d2011-07-13 09:27:32 +000013961 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)
Michael Chan7544b092007-05-05 13:08:32 -070013962 tp->pdev_peer = tg3_find_peer(tp);
13963
Matt Carlsonc885e822010-08-02 11:25:57 +000013964 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013965 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13966 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Joe Perches63c3a662011-04-26 08:12:10 +000013967 tg3_flag_set(tp, 5717_PLUS);
Matt Carlson0a58d662011-04-05 14:22:45 +000013968
13969 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000013970 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57766)
13971 tg3_flag_set(tp, 57765_CLASS);
13972
13973 if (tg3_flag(tp, 57765_CLASS) || tg3_flag(tp, 5717_PLUS))
Joe Perches63c3a662011-04-26 08:12:10 +000013974 tg3_flag_set(tp, 57765_PLUS);
Matt Carlsonc885e822010-08-02 11:25:57 +000013975
Matt Carlson321d32a2008-11-21 17:22:19 -080013976 /* Intentionally exclude ASIC_REV_5906 */
13977 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Michael Chand9ab5ad2006-03-20 22:27:35 -080013978 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013979 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013980 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013981 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013982 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013983 tg3_flag(tp, 57765_PLUS))
13984 tg3_flag_set(tp, 5755_PLUS);
Matt Carlson321d32a2008-11-21 17:22:19 -080013985
13986 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13987 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Michael Chanb5d37722006-09-27 16:06:21 -070013988 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013989 tg3_flag(tp, 5755_PLUS) ||
13990 tg3_flag(tp, 5780_CLASS))
13991 tg3_flag_set(tp, 5750_PLUS);
John W. Linville6708e5c2005-04-21 17:00:52 -070013992
Matt Carlson6ff6f812011-05-19 12:12:54 +000013993 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013994 tg3_flag(tp, 5750_PLUS))
13995 tg3_flag_set(tp, 5705_PLUS);
John W. Linville1b440c562005-04-21 17:03:18 -070013996
Matt Carlson507399f2009-11-13 13:03:37 +000013997 /* Determine TSO capabilities */
Matt Carlsona0512942011-07-27 14:20:54 +000013998 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0)
Matt Carlson4d163b72011-01-25 15:58:48 +000013999 ; /* Do nothing. HW bug. */
Joe Perches63c3a662011-04-26 08:12:10 +000014000 else if (tg3_flag(tp, 57765_PLUS))
14001 tg3_flag_set(tp, HW_TSO_3);
14002 else if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000014003 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000014004 tg3_flag_set(tp, HW_TSO_2);
14005 else if (tg3_flag(tp, 5750_PLUS)) {
14006 tg3_flag_set(tp, HW_TSO_1);
14007 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000014008 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
14009 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
Joe Perches63c3a662011-04-26 08:12:10 +000014010 tg3_flag_clear(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000014011 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14012 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
14013 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000014014 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000014015 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
14016 tp->fw_needed = FIRMWARE_TG3TSO5;
14017 else
14018 tp->fw_needed = FIRMWARE_TG3TSO;
14019 }
14020
Matt Carlsondabc5c62011-05-19 12:12:52 +000014021 /* Selectively allow TSO based on operating conditions */
Matt Carlson6ff6f812011-05-19 12:12:54 +000014022 if (tg3_flag(tp, HW_TSO_1) ||
14023 tg3_flag(tp, HW_TSO_2) ||
14024 tg3_flag(tp, HW_TSO_3) ||
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000014025 tp->fw_needed) {
14026 /* For firmware TSO, assume ASF is disabled.
14027 * We'll disable TSO later if we discover ASF
14028 * is enabled in tg3_get_eeprom_hw_cfg().
14029 */
Matt Carlsondabc5c62011-05-19 12:12:52 +000014030 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000014031 } else {
Matt Carlsondabc5c62011-05-19 12:12:52 +000014032 tg3_flag_clear(tp, TSO_CAPABLE);
14033 tg3_flag_clear(tp, TSO_BUG);
14034 tp->fw_needed = NULL;
14035 }
14036
14037 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
14038 tp->fw_needed = FIRMWARE_TG3;
14039
Matt Carlson507399f2009-11-13 13:03:37 +000014040 tp->irq_max = 1;
14041
Joe Perches63c3a662011-04-26 08:12:10 +000014042 if (tg3_flag(tp, 5750_PLUS)) {
14043 tg3_flag_set(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070014044 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
14045 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
14046 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
14047 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
14048 tp->pdev_peer == tp->pdev))
Joe Perches63c3a662011-04-26 08:12:10 +000014049 tg3_flag_clear(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070014050
Joe Perches63c3a662011-04-26 08:12:10 +000014051 if (tg3_flag(tp, 5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070014052 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000014053 tg3_flag_set(tp, 1SHOT_MSI);
Michael Chan52c0fd82006-06-29 20:15:54 -070014054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014055
Joe Perches63c3a662011-04-26 08:12:10 +000014056 if (tg3_flag(tp, 57765_PLUS)) {
14057 tg3_flag_set(tp, SUPPORT_MSIX);
Matt Carlson507399f2009-11-13 13:03:37 +000014058 tp->irq_max = TG3_IRQ_MAX_VECS;
14059 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014060 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000014061
Matt Carlson2ffcc982011-05-19 12:12:44 +000014062 if (tg3_flag(tp, 5755_PLUS))
Joe Perches63c3a662011-04-26 08:12:10 +000014063 tg3_flag_set(tp, SHORT_DMA_BUG);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014064
Matt Carlsone31aa982011-07-27 14:20:53 +000014065 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona4cb4282011-12-14 11:09:58 +000014066 tp->dma_limit = TG3_TX_BD_DMA_MAX_4K;
Matt Carlson55086ad2011-12-14 11:09:59 +000014067 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57766)
14068 tp->dma_limit = TG3_TX_BD_DMA_MAX_2K;
Matt Carlsone31aa982011-07-27 14:20:53 +000014069
Matt Carlsonfa6b2aa2011-11-21 15:01:19 +000014070 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14071 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
14072 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Joe Perches63c3a662011-04-26 08:12:10 +000014073 tg3_flag_set(tp, LRG_PROD_RING_CAP);
Matt Carlsonde9f5232011-04-05 14:22:43 +000014074
Joe Perches63c3a662011-04-26 08:12:10 +000014075 if (tg3_flag(tp, 57765_PLUS) &&
Matt Carlsona0512942011-07-27 14:20:54 +000014076 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0)
Joe Perches63c3a662011-04-26 08:12:10 +000014077 tg3_flag_set(tp, USE_JUMBO_BDFLAG);
Matt Carlsonb703df62009-12-03 08:36:21 +000014078
Joe Perches63c3a662011-04-26 08:12:10 +000014079 if (!tg3_flag(tp, 5705_PLUS) ||
14080 tg3_flag(tp, 5780_CLASS) ||
14081 tg3_flag(tp, USE_JUMBO_BDFLAG))
14082 tg3_flag_set(tp, JUMBO_CAPABLE);
Michael Chan0f893dc2005-07-25 12:30:38 -070014083
Matt Carlson52f44902008-11-21 17:17:04 -080014084 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14085 &pci_state_reg);
14086
Jon Mason708ebb32011-06-27 12:56:50 +000014087 if (pci_is_pcie(tp->pdev)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014088 u16 lnkctl;
14089
Joe Perches63c3a662011-04-26 08:12:10 +000014090 tg3_flag_set(tp, PCI_EXPRESS);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080014091
Matt Carlson2c55a3d2011-11-28 09:41:04 +000014092 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0) {
14093 int readrq = pcie_get_readrq(tp->pdev);
14094 if (readrq > 2048)
14095 pcie_set_readrq(tp->pdev, 2048);
14096 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -080014097
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014098 pci_read_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +000014099 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014100 &lnkctl);
14101 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
Matt Carlson7196cd62011-05-19 16:02:44 +000014102 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
14103 ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000014104 tg3_flag_clear(tp, HW_TSO_2);
Matt Carlsondabc5c62011-05-19 12:12:52 +000014105 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson7196cd62011-05-19 16:02:44 +000014106 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014107 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014108 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000014109 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
14110 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Joe Perches63c3a662011-04-26 08:12:10 +000014111 tg3_flag_set(tp, CLKREQ_BUG);
Matt Carlson614b0592010-01-20 16:58:02 +000014112 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000014113 tg3_flag_set(tp, L1PLLPD_EN);
Michael Chanc7835a72006-11-15 21:14:42 -080014114 }
Matt Carlson52f44902008-11-21 17:17:04 -080014115 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Jon Mason708ebb32011-06-27 12:56:50 +000014116 /* BCM5785 devices are effectively PCIe devices, and should
14117 * follow PCIe codepaths, but do not have a PCIe capabilities
14118 * section.
Matt Carlson93a700a2011-08-31 11:44:54 +000014119 */
Joe Perches63c3a662011-04-26 08:12:10 +000014120 tg3_flag_set(tp, PCI_EXPRESS);
14121 } else if (!tg3_flag(tp, 5705_PLUS) ||
14122 tg3_flag(tp, 5780_CLASS)) {
Matt Carlson52f44902008-11-21 17:17:04 -080014123 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
14124 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000014125 dev_err(&tp->pdev->dev,
14126 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080014127 return -EIO;
14128 }
14129
14130 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
Joe Perches63c3a662011-04-26 08:12:10 +000014131 tg3_flag_set(tp, PCIX_MODE);
Matt Carlson52f44902008-11-21 17:17:04 -080014132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014133
Michael Chan399de502005-10-03 14:02:39 -070014134 /* If we have an AMD 762 or VIA K8T800 chipset, write
14135 * reordering to the mailbox registers done by the host
14136 * controller can cause major troubles. We read back from
14137 * every mailbox register write to force the writes to be
14138 * posted to the chip in order.
14139 */
Matt Carlson41434702011-03-09 16:58:22 +000014140 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Joe Perches63c3a662011-04-26 08:12:10 +000014141 !tg3_flag(tp, PCI_EXPRESS))
14142 tg3_flag_set(tp, MBOX_WRITE_REORDER);
Michael Chan399de502005-10-03 14:02:39 -070014143
Matt Carlson69fc4052008-12-21 20:19:57 -080014144 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
14145 &tp->pci_cacheline_sz);
14146 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
14147 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014148 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14149 tp->pci_lat_timer < 64) {
14150 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080014151 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
14152 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014153 }
14154
Matt Carlson16821282011-07-13 09:27:28 +000014155 /* Important! -- It is critical that the PCI-X hw workaround
14156 * situation is decided before the first MMIO register access.
14157 */
Matt Carlson52f44902008-11-21 17:17:04 -080014158 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
14159 /* 5700 BX chips need to have their TX producer index
14160 * mailboxes written twice to workaround a bug.
14161 */
Joe Perches63c3a662011-04-26 08:12:10 +000014162 tg3_flag_set(tp, TXD_MBOX_HWBUG);
Matt Carlson9974a352007-10-07 23:27:28 -070014163
Matt Carlson52f44902008-11-21 17:17:04 -080014164 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014165 *
14166 * The workaround is to use indirect register accesses
14167 * for all chip writes not to mailbox registers.
14168 */
Joe Perches63c3a662011-04-26 08:12:10 +000014169 if (tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014170 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014171
Joe Perches63c3a662011-04-26 08:12:10 +000014172 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014173
14174 /* The chip can have it's power management PCI config
14175 * space registers clobbered due to this bug.
14176 * So explicitly force the chip into D0 here.
14177 */
Matt Carlson9974a352007-10-07 23:27:28 -070014178 pci_read_config_dword(tp->pdev,
14179 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014180 &pm_reg);
14181 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
14182 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070014183 pci_write_config_dword(tp->pdev,
14184 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014185 pm_reg);
14186
14187 /* Also, force SERR#/PERR# in PCI command. */
14188 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
14189 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
14190 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
14191 }
14192 }
14193
Linus Torvalds1da177e2005-04-16 15:20:36 -070014194 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000014195 tg3_flag_set(tp, PCI_HIGH_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014196 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000014197 tg3_flag_set(tp, PCI_32BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014198
14199 /* Chip-specific fixup from Broadcom driver */
14200 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
14201 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
14202 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
14203 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
14204 }
14205
Michael Chan1ee582d2005-08-09 20:16:46 -070014206 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070014207 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070014208 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070014209 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070014210 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070014211 tp->write32_tx_mbox = tg3_write32;
14212 tp->write32_rx_mbox = tg3_write32;
14213
14214 /* Various workaround register access methods */
Joe Perches63c3a662011-04-26 08:12:10 +000014215 if (tg3_flag(tp, PCIX_TARGET_HWBUG))
Michael Chan1ee582d2005-08-09 20:16:46 -070014216 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070014217 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014218 (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson98efd8a2007-05-05 12:47:25 -070014219 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
14220 /*
14221 * Back to back register writes can cause problems on these
14222 * chips, the workaround is to read back all reg writes
14223 * except those to mailbox regs.
14224 *
14225 * See tg3_write_indirect_reg32().
14226 */
Michael Chan1ee582d2005-08-09 20:16:46 -070014227 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070014228 }
14229
Joe Perches63c3a662011-04-26 08:12:10 +000014230 if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
Michael Chan1ee582d2005-08-09 20:16:46 -070014231 tp->write32_tx_mbox = tg3_write32_tx_mbox;
Joe Perches63c3a662011-04-26 08:12:10 +000014232 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Michael Chan1ee582d2005-08-09 20:16:46 -070014233 tp->write32_rx_mbox = tg3_write_flush_reg32;
14234 }
Michael Chan20094932005-08-09 20:16:32 -070014235
Joe Perches63c3a662011-04-26 08:12:10 +000014236 if (tg3_flag(tp, ICH_WORKAROUND)) {
Michael Chan68929142005-08-09 20:17:14 -070014237 tp->read32 = tg3_read_indirect_reg32;
14238 tp->write32 = tg3_write_indirect_reg32;
14239 tp->read32_mbox = tg3_read_indirect_mbox;
14240 tp->write32_mbox = tg3_write_indirect_mbox;
14241 tp->write32_tx_mbox = tg3_write_indirect_mbox;
14242 tp->write32_rx_mbox = tg3_write_indirect_mbox;
14243
14244 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070014245 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070014246
14247 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
14248 pci_cmd &= ~PCI_COMMAND_MEMORY;
14249 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
14250 }
Michael Chanb5d37722006-09-27 16:06:21 -070014251 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
14252 tp->read32_mbox = tg3_read32_mbox_5906;
14253 tp->write32_mbox = tg3_write32_mbox_5906;
14254 tp->write32_tx_mbox = tg3_write32_mbox_5906;
14255 tp->write32_rx_mbox = tg3_write32_mbox_5906;
14256 }
Michael Chan68929142005-08-09 20:17:14 -070014257
Michael Chanbbadf502006-04-06 21:46:34 -070014258 if (tp->write32 == tg3_write_indirect_reg32 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014259 (tg3_flag(tp, PCIX_MODE) &&
Michael Chanbbadf502006-04-06 21:46:34 -070014260 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070014261 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Joe Perches63c3a662011-04-26 08:12:10 +000014262 tg3_flag_set(tp, SRAM_USE_CONFIG);
Michael Chanbbadf502006-04-06 21:46:34 -070014263
Matt Carlson16821282011-07-13 09:27:28 +000014264 /* The memory arbiter has to be enabled in order for SRAM accesses
14265 * to succeed. Normally on powerup the tg3 chip firmware will make
14266 * sure it is enabled, but other entities such as system netboot
14267 * code might disable it.
14268 */
14269 val = tr32(MEMARB_MODE);
14270 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
14271
Matt Carlson9dc5e342011-11-04 09:15:02 +000014272 tp->pci_fn = PCI_FUNC(tp->pdev->devfn) & 3;
14273 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
14274 tg3_flag(tp, 5780_CLASS)) {
14275 if (tg3_flag(tp, PCIX_MODE)) {
14276 pci_read_config_dword(tp->pdev,
14277 tp->pcix_cap + PCI_X_STATUS,
14278 &val);
14279 tp->pci_fn = val & 0x7;
14280 }
14281 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
14282 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
14283 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) ==
14284 NIC_SRAM_CPMUSTAT_SIG) {
14285 tp->pci_fn = val & TG3_CPMU_STATUS_FMSK_5717;
14286 tp->pci_fn = tp->pci_fn ? 1 : 0;
14287 }
14288 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
14289 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
14290 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
14291 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) ==
14292 NIC_SRAM_CPMUSTAT_SIG) {
14293 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5719) >>
14294 TG3_CPMU_STATUS_FSHFT_5719;
14295 }
Matt Carlson69f11c92011-07-13 09:27:30 +000014296 }
14297
Michael Chan7d0c41e2005-04-21 17:06:20 -070014298 /* Get eeprom hw config before calling tg3_set_power_state().
Joe Perches63c3a662011-04-26 08:12:10 +000014299 * In particular, the TG3_FLAG_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070014300 * determined before calling tg3_set_power_state() so that
14301 * we know whether or not to switch out of Vaux power.
14302 * When the flag is set, it means that GPIO1 is used for eeprom
14303 * write protect and also implies that it is a LOM where GPIOs
14304 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040014305 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070014306 tg3_get_eeprom_hw_cfg(tp);
14307
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000014308 if (tp->fw_needed && tg3_flag(tp, ENABLE_ASF)) {
14309 tg3_flag_clear(tp, TSO_CAPABLE);
14310 tg3_flag_clear(tp, TSO_BUG);
14311 tp->fw_needed = NULL;
14312 }
14313
Joe Perches63c3a662011-04-26 08:12:10 +000014314 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070014315 /* Allow reads and writes to the
14316 * APE register and memory space.
14317 */
14318 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +000014319 PCISTATE_ALLOW_APE_SHMEM_WR |
14320 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070014321 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
14322 pci_state_reg);
Matt Carlsonc9cab242011-07-13 09:27:27 +000014323
14324 tg3_ape_lock_init(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070014325 }
14326
Matt Carlson9936bcf2007-10-10 18:03:07 -070014327 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson57e69832008-05-25 23:48:31 -070014328 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014329 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014330 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014331 tg3_flag(tp, 57765_PLUS))
14332 tg3_flag_set(tp, CPMU_PRESENT);
Matt Carlsond30cdd22007-10-07 23:28:35 -070014333
Matt Carlson16821282011-07-13 09:27:28 +000014334 /* Set up tp->grc_local_ctrl before calling
14335 * tg3_pwrsrc_switch_to_vmain(). GPIO1 driven high
14336 * will bring 5700's external PHY out of reset.
Michael Chan314fba32005-04-21 17:07:04 -070014337 * It is also used as eeprom write protect on LOMs.
14338 */
14339 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
Matt Carlson6ff6f812011-05-19 12:12:54 +000014340 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014341 tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan314fba32005-04-21 17:07:04 -070014342 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
14343 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070014344 /* Unused GPIO3 must be driven as output on 5752 because there
14345 * are no pull-up resistors on unused GPIO pins.
14346 */
14347 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
14348 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070014349
Matt Carlson321d32a2008-11-21 17:22:19 -080014350 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsoncb4ed1f2010-01-20 16:58:09 +000014351 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000014352 tg3_flag(tp, 57765_CLASS))
Michael Chanaf36e6b2006-03-23 01:28:06 -080014353 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
14354
Matt Carlson8d519ab2009-04-20 06:58:01 +000014355 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
14356 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070014357 /* Turn off the debug UART. */
14358 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014359 if (tg3_flag(tp, IS_NIC))
Matt Carlson5f0c4a32008-06-09 15:41:12 -070014360 /* Keep VMain power. */
14361 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
14362 GRC_LCLCTRL_GPIO_OUTPUT0;
14363 }
14364
Matt Carlson16821282011-07-13 09:27:28 +000014365 /* Switch out of Vaux if it is a NIC */
14366 tg3_pwrsrc_switch_to_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014367
Linus Torvalds1da177e2005-04-16 15:20:36 -070014368 /* Derive initial jumbo mode from MTU assigned in
14369 * ether_setup() via the alloc_etherdev() call
14370 */
Joe Perches63c3a662011-04-26 08:12:10 +000014371 if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
14372 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014373
14374 /* Determine WakeOnLan speed to use. */
14375 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14376 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
14377 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
14378 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
Joe Perches63c3a662011-04-26 08:12:10 +000014379 tg3_flag_clear(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014380 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000014381 tg3_flag_set(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014382 }
14383
Matt Carlson7f97a4b2009-08-25 10:10:03 +000014384 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014385 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000014386
Linus Torvalds1da177e2005-04-16 15:20:36 -070014387 /* A few boards don't want Ethernet@WireSpeed phy feature */
Matt Carlson6ff6f812011-05-19 12:12:54 +000014388 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14389 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070014390 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070014391 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014392 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
14393 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
14394 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014395
14396 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
14397 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014398 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014399 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014400 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014401
Joe Perches63c3a662011-04-26 08:12:10 +000014402 if (tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014403 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080014404 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014405 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014406 !tg3_flag(tp, 57765_PLUS)) {
Michael Chanc424cb22006-04-29 18:56:34 -070014407 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070014408 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070014409 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
14410 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080014411 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
14412 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014413 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080014414 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014415 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080014416 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014417 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070014418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014419
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014420 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
14421 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
14422 tp->phy_otp = tg3_read_otp_phycfg(tp);
14423 if (tp->phy_otp == 0)
14424 tp->phy_otp = TG3_OTP_DEFAULT;
14425 }
14426
Joe Perches63c3a662011-04-26 08:12:10 +000014427 if (tg3_flag(tp, CPMU_PRESENT))
Matt Carlson8ef21422008-05-02 16:47:53 -070014428 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
14429 else
14430 tp->mi_mode = MAC_MI_MODE_BASE;
14431
Linus Torvalds1da177e2005-04-16 15:20:36 -070014432 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014433 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
14434 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
14435 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
14436
Matt Carlson4d958472011-04-20 07:57:35 +000014437 /* Set these bits to enable statistics workaround. */
14438 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14439 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
14440 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0) {
14441 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
14442 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
14443 }
14444
Matt Carlson321d32a2008-11-21 17:22:19 -080014445 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
14446 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Joe Perches63c3a662011-04-26 08:12:10 +000014447 tg3_flag_set(tp, USE_PHYLIB);
Matt Carlson57e69832008-05-25 23:48:31 -070014448
Matt Carlson158d7ab2008-05-29 01:37:54 -070014449 err = tg3_mdio_init(tp);
14450 if (err)
14451 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014452
14453 /* Initialize data/descriptor byte/word swapping. */
14454 val = tr32(GRC_MODE);
Matt Carlsonf2096f92011-04-05 14:22:48 +000014455 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
14456 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
14457 GRC_MODE_WORD_SWAP_B2HRX_DATA |
14458 GRC_MODE_B2HRX_ENABLE |
14459 GRC_MODE_HTX2B_ENABLE |
14460 GRC_MODE_HOST_STACKUP);
14461 else
14462 val &= GRC_MODE_HOST_STACKUP;
14463
Linus Torvalds1da177e2005-04-16 15:20:36 -070014464 tw32(GRC_MODE, val | tp->grc_mode);
14465
14466 tg3_switch_clocks(tp);
14467
14468 /* Clear this out for sanity. */
14469 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
14470
14471 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14472 &pci_state_reg);
14473 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014474 !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014475 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
14476
14477 if (chiprevid == CHIPREV_ID_5701_A0 ||
14478 chiprevid == CHIPREV_ID_5701_B0 ||
14479 chiprevid == CHIPREV_ID_5701_B2 ||
14480 chiprevid == CHIPREV_ID_5701_B5) {
14481 void __iomem *sram_base;
14482
14483 /* Write some dummy words into the SRAM status block
14484 * area, see if it reads back correctly. If the return
14485 * value is bad, force enable the PCIX workaround.
14486 */
14487 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
14488
14489 writel(0x00000000, sram_base);
14490 writel(0x00000000, sram_base + 4);
14491 writel(0xffffffff, sram_base + 4);
14492 if (readl(sram_base) != 0x00000000)
Joe Perches63c3a662011-04-26 08:12:10 +000014493 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014494 }
14495 }
14496
14497 udelay(50);
14498 tg3_nvram_init(tp);
14499
14500 grc_misc_cfg = tr32(GRC_MISC_CFG);
14501 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
14502
Linus Torvalds1da177e2005-04-16 15:20:36 -070014503 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14504 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
14505 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
Joe Perches63c3a662011-04-26 08:12:10 +000014506 tg3_flag_set(tp, IS_5788);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014507
Joe Perches63c3a662011-04-26 08:12:10 +000014508 if (!tg3_flag(tp, IS_5788) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +000014509 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014510 tg3_flag_set(tp, TAGGED_STATUS);
14511 if (tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070014512 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
14513 HOSTCC_MODE_CLRTICK_TXBD);
14514
14515 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
14516 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
14517 tp->misc_host_ctrl);
14518 }
14519
Matt Carlson3bda1252008-08-15 14:08:22 -070014520 /* Preserve the APE MAC_MODE bits */
Joe Perches63c3a662011-04-26 08:12:10 +000014521 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +000014522 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070014523 else
Matt Carlson6e01b202011-08-19 13:58:20 +000014524 tp->mac_mode = 0;
Matt Carlson3bda1252008-08-15 14:08:22 -070014525
Linus Torvalds1da177e2005-04-16 15:20:36 -070014526 /* these are limited to 10/100 only */
14527 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14528 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
14529 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14530 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14531 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
14532 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
14533 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
14534 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14535 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
Michael Chan676917d2006-12-07 00:20:22 -080014536 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
14537 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014538 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
Matt Carlsond1101142010-02-17 15:16:55 +000014539 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
14540 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014541 (tp->phy_flags & TG3_PHYFLG_IS_FET))
14542 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014543
14544 err = tg3_phy_probe(tp);
14545 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014546 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014547 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014548 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014549 }
14550
Matt Carlson184b8902010-04-05 10:19:25 +000014551 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080014552 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014553
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014554 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
14555 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014556 } else {
14557 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014558 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014559 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014560 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014561 }
14562
14563 /* 5700 {AX,BX} chips have a broken status block link
14564 * change bit implementation, so we must use the
14565 * status register in those cases.
14566 */
14567 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014568 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014569 else
Joe Perches63c3a662011-04-26 08:12:10 +000014570 tg3_flag_clear(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014571
14572 /* The led_ctrl is set during tg3_phy_probe, here we might
14573 * have to force the link status polling mechanism based
14574 * upon subsystem IDs.
14575 */
14576 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070014577 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014578 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
14579 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Joe Perches63c3a662011-04-26 08:12:10 +000014580 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014581 }
14582
14583 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014584 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Joe Perches63c3a662011-04-26 08:12:10 +000014585 tg3_flag_set(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014586 else
Joe Perches63c3a662011-04-26 08:12:10 +000014587 tg3_flag_clear(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014588
Eric Dumazet9205fd92011-11-18 06:47:01 +000014589 tp->rx_offset = NET_SKB_PAD + NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014590 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014591 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014592 tg3_flag(tp, PCIX_MODE)) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000014593 tp->rx_offset = NET_SKB_PAD;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014594#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000014595 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014596#endif
14597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014598
Matt Carlson2c49a442010-09-30 10:34:35 +000014599 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
14600 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000014601 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
14602
Matt Carlson2c49a442010-09-30 10:34:35 +000014603 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070014604
14605 /* Increment the rx prod index on the rx std ring by at most
14606 * 8 for these chips to workaround hw errata.
14607 */
14608 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
14609 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
14610 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
14611 tp->rx_std_max_post = 8;
14612
Joe Perches63c3a662011-04-26 08:12:10 +000014613 if (tg3_flag(tp, ASPM_WORKAROUND))
Matt Carlson8ed5d972007-05-07 00:25:49 -070014614 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
14615 PCIE_PWR_MGMT_L1_THRESH_MSK;
14616
Linus Torvalds1da177e2005-04-16 15:20:36 -070014617 return err;
14618}
14619
David S. Miller49b6e95f2007-03-29 01:38:42 -070014620#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014621static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
14622{
14623 struct net_device *dev = tp->dev;
14624 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014625 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070014626 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014627 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014628
David S. Miller49b6e95f2007-03-29 01:38:42 -070014629 addr = of_get_property(dp, "local-mac-address", &len);
14630 if (addr && len == 6) {
14631 memcpy(dev->dev_addr, addr, 6);
14632 memcpy(dev->perm_addr, dev->dev_addr, 6);
14633 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014634 }
14635 return -ENODEV;
14636}
14637
14638static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
14639{
14640 struct net_device *dev = tp->dev;
14641
14642 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070014643 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014644 return 0;
14645}
14646#endif
14647
14648static int __devinit tg3_get_device_address(struct tg3 *tp)
14649{
14650 struct net_device *dev = tp->dev;
14651 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080014652 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014653
David S. Miller49b6e95f2007-03-29 01:38:42 -070014654#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014655 if (!tg3_get_macaddr_sparc(tp))
14656 return 0;
14657#endif
14658
14659 mac_offset = 0x7c;
Matt Carlson6ff6f812011-05-19 12:12:54 +000014660 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014661 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014662 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
14663 mac_offset = 0xcc;
14664 if (tg3_nvram_lock(tp))
14665 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
14666 else
14667 tg3_nvram_unlock(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000014668 } else if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson69f11c92011-07-13 09:27:30 +000014669 if (tp->pci_fn & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014670 mac_offset = 0xcc;
Matt Carlson69f11c92011-07-13 09:27:30 +000014671 if (tp->pci_fn > 1)
Matt Carlsona50d0792010-06-05 17:24:37 +000014672 mac_offset += 0x18c;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014673 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070014674 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014675
14676 /* First try to get it from MAC address mailbox. */
14677 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
14678 if ((hi >> 16) == 0x484b) {
14679 dev->dev_addr[0] = (hi >> 8) & 0xff;
14680 dev->dev_addr[1] = (hi >> 0) & 0xff;
14681
14682 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
14683 dev->dev_addr[2] = (lo >> 24) & 0xff;
14684 dev->dev_addr[3] = (lo >> 16) & 0xff;
14685 dev->dev_addr[4] = (lo >> 8) & 0xff;
14686 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014687
Michael Chan008652b2006-03-27 23:14:53 -080014688 /* Some old bootcode may report a 0 MAC address in SRAM */
14689 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
14690 }
14691 if (!addr_ok) {
14692 /* Next, try NVRAM. */
Joe Perches63c3a662011-04-26 08:12:10 +000014693 if (!tg3_flag(tp, NO_NVRAM) &&
Matt Carlsondf259d82009-04-20 06:57:14 +000014694 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000014695 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070014696 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
14697 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080014698 }
14699 /* Finally just fetch it out of the MAC control regs. */
14700 else {
14701 hi = tr32(MAC_ADDR_0_HIGH);
14702 lo = tr32(MAC_ADDR_0_LOW);
14703
14704 dev->dev_addr[5] = lo & 0xff;
14705 dev->dev_addr[4] = (lo >> 8) & 0xff;
14706 dev->dev_addr[3] = (lo >> 16) & 0xff;
14707 dev->dev_addr[2] = (lo >> 24) & 0xff;
14708 dev->dev_addr[1] = hi & 0xff;
14709 dev->dev_addr[0] = (hi >> 8) & 0xff;
14710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014711 }
14712
14713 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070014714#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014715 if (!tg3_get_default_macaddr_sparc(tp))
14716 return 0;
14717#endif
14718 return -EINVAL;
14719 }
John W. Linville2ff43692005-09-12 14:44:20 -070014720 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014721 return 0;
14722}
14723
David S. Miller59e6b432005-05-18 22:50:10 -070014724#define BOUNDARY_SINGLE_CACHELINE 1
14725#define BOUNDARY_MULTI_CACHELINE 2
14726
14727static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
14728{
14729 int cacheline_size;
14730 u8 byte;
14731 int goal;
14732
14733 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
14734 if (byte == 0)
14735 cacheline_size = 1024;
14736 else
14737 cacheline_size = (int) byte * 4;
14738
14739 /* On 5703 and later chips, the boundary bits have no
14740 * effect.
14741 */
14742 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14743 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014744 !tg3_flag(tp, PCI_EXPRESS))
David S. Miller59e6b432005-05-18 22:50:10 -070014745 goto out;
14746
14747#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
14748 goal = BOUNDARY_MULTI_CACHELINE;
14749#else
14750#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
14751 goal = BOUNDARY_SINGLE_CACHELINE;
14752#else
14753 goal = 0;
14754#endif
14755#endif
14756
Joe Perches63c3a662011-04-26 08:12:10 +000014757 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014758 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
14759 goto out;
14760 }
14761
David S. Miller59e6b432005-05-18 22:50:10 -070014762 if (!goal)
14763 goto out;
14764
14765 /* PCI controllers on most RISC systems tend to disconnect
14766 * when a device tries to burst across a cache-line boundary.
14767 * Therefore, letting tg3 do so just wastes PCI bandwidth.
14768 *
14769 * Unfortunately, for PCI-E there are only limited
14770 * write-side controls for this, and thus for reads
14771 * we will still get the disconnects. We'll also waste
14772 * these PCI cycles for both read and write for chips
14773 * other than 5700 and 5701 which do not implement the
14774 * boundary bits.
14775 */
Joe Perches63c3a662011-04-26 08:12:10 +000014776 if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014777 switch (cacheline_size) {
14778 case 16:
14779 case 32:
14780 case 64:
14781 case 128:
14782 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14783 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
14784 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
14785 } else {
14786 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14787 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14788 }
14789 break;
14790
14791 case 256:
14792 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
14793 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
14794 break;
14795
14796 default:
14797 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14798 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14799 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014800 }
Joe Perches63c3a662011-04-26 08:12:10 +000014801 } else if (tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014802 switch (cacheline_size) {
14803 case 16:
14804 case 32:
14805 case 64:
14806 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14807 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14808 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
14809 break;
14810 }
14811 /* fallthrough */
14812 case 128:
14813 default:
14814 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14815 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
14816 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014817 }
David S. Miller59e6b432005-05-18 22:50:10 -070014818 } else {
14819 switch (cacheline_size) {
14820 case 16:
14821 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14822 val |= (DMA_RWCTRL_READ_BNDRY_16 |
14823 DMA_RWCTRL_WRITE_BNDRY_16);
14824 break;
14825 }
14826 /* fallthrough */
14827 case 32:
14828 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14829 val |= (DMA_RWCTRL_READ_BNDRY_32 |
14830 DMA_RWCTRL_WRITE_BNDRY_32);
14831 break;
14832 }
14833 /* fallthrough */
14834 case 64:
14835 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14836 val |= (DMA_RWCTRL_READ_BNDRY_64 |
14837 DMA_RWCTRL_WRITE_BNDRY_64);
14838 break;
14839 }
14840 /* fallthrough */
14841 case 128:
14842 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14843 val |= (DMA_RWCTRL_READ_BNDRY_128 |
14844 DMA_RWCTRL_WRITE_BNDRY_128);
14845 break;
14846 }
14847 /* fallthrough */
14848 case 256:
14849 val |= (DMA_RWCTRL_READ_BNDRY_256 |
14850 DMA_RWCTRL_WRITE_BNDRY_256);
14851 break;
14852 case 512:
14853 val |= (DMA_RWCTRL_READ_BNDRY_512 |
14854 DMA_RWCTRL_WRITE_BNDRY_512);
14855 break;
14856 case 1024:
14857 default:
14858 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
14859 DMA_RWCTRL_WRITE_BNDRY_1024);
14860 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014861 }
David S. Miller59e6b432005-05-18 22:50:10 -070014862 }
14863
14864out:
14865 return val;
14866}
14867
Linus Torvalds1da177e2005-04-16 15:20:36 -070014868static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
14869{
14870 struct tg3_internal_buffer_desc test_desc;
14871 u32 sram_dma_descs;
14872 int i, ret;
14873
14874 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
14875
14876 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
14877 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
14878 tw32(RDMAC_STATUS, 0);
14879 tw32(WDMAC_STATUS, 0);
14880
14881 tw32(BUFMGR_MODE, 0);
14882 tw32(FTQ_RESET, 0);
14883
14884 test_desc.addr_hi = ((u64) buf_dma) >> 32;
14885 test_desc.addr_lo = buf_dma & 0xffffffff;
14886 test_desc.nic_mbuf = 0x00002100;
14887 test_desc.len = size;
14888
14889 /*
14890 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
14891 * the *second* time the tg3 driver was getting loaded after an
14892 * initial scan.
14893 *
14894 * Broadcom tells me:
14895 * ...the DMA engine is connected to the GRC block and a DMA
14896 * reset may affect the GRC block in some unpredictable way...
14897 * The behavior of resets to individual blocks has not been tested.
14898 *
14899 * Broadcom noted the GRC reset will also reset all sub-components.
14900 */
14901 if (to_device) {
14902 test_desc.cqid_sqid = (13 << 8) | 2;
14903
14904 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
14905 udelay(40);
14906 } else {
14907 test_desc.cqid_sqid = (16 << 8) | 7;
14908
14909 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
14910 udelay(40);
14911 }
14912 test_desc.flags = 0x00000005;
14913
14914 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
14915 u32 val;
14916
14917 val = *(((u32 *)&test_desc) + i);
14918 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
14919 sram_dma_descs + (i * sizeof(u32)));
14920 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
14921 }
14922 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
14923
Matt Carlson859a5882010-04-05 10:19:28 +000014924 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014925 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a5882010-04-05 10:19:28 +000014926 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070014927 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014928
14929 ret = -ENODEV;
14930 for (i = 0; i < 40; i++) {
14931 u32 val;
14932
14933 if (to_device)
14934 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
14935 else
14936 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
14937 if ((val & 0xffff) == sram_dma_descs) {
14938 ret = 0;
14939 break;
14940 }
14941
14942 udelay(100);
14943 }
14944
14945 return ret;
14946}
14947
David S. Millerded73402005-05-23 13:59:47 -070014948#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070014949
Matt Carlson41434702011-03-09 16:58:22 +000014950static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080014951 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
14952 { },
14953};
14954
Linus Torvalds1da177e2005-04-16 15:20:36 -070014955static int __devinit tg3_test_dma(struct tg3 *tp)
14956{
14957 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070014958 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014959 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014960
Matt Carlson4bae65c2010-11-24 08:31:52 +000014961 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
14962 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014963 if (!buf) {
14964 ret = -ENOMEM;
14965 goto out_nofree;
14966 }
14967
14968 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
14969 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
14970
David S. Miller59e6b432005-05-18 22:50:10 -070014971 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014972
Joe Perches63c3a662011-04-26 08:12:10 +000014973 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014974 goto out;
14975
Joe Perches63c3a662011-04-26 08:12:10 +000014976 if (tg3_flag(tp, PCI_EXPRESS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014977 /* DMA read watermark not used on PCIE */
14978 tp->dma_rwctrl |= 0x00180000;
Joe Perches63c3a662011-04-26 08:12:10 +000014979 } else if (!tg3_flag(tp, PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070014980 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
14981 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014982 tp->dma_rwctrl |= 0x003f0000;
14983 else
14984 tp->dma_rwctrl |= 0x003f000f;
14985 } else {
14986 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14987 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
14988 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080014989 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014990
Michael Chan4a29cc22006-03-19 13:21:12 -080014991 /* If the 5704 is behind the EPB bridge, we can
14992 * do the less restrictive ONE_DMA workaround for
14993 * better performance.
14994 */
Joe Perches63c3a662011-04-26 08:12:10 +000014995 if (tg3_flag(tp, 40BIT_DMA_BUG) &&
Michael Chan4a29cc22006-03-19 13:21:12 -080014996 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14997 tp->dma_rwctrl |= 0x8000;
14998 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014999 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
15000
Michael Chan49afdeb2007-02-13 12:17:03 -080015001 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
15002 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070015003 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080015004 tp->dma_rwctrl |=
15005 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
15006 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
15007 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070015008 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
15009 /* 5780 always in PCIX mode */
15010 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070015011 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
15012 /* 5714 always in PCIX mode */
15013 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015014 } else {
15015 tp->dma_rwctrl |= 0x001b000f;
15016 }
15017 }
15018
15019 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
15020 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
15021 tp->dma_rwctrl &= 0xfffffff0;
15022
15023 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
15024 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
15025 /* Remove this if it causes problems for some boards. */
15026 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
15027
15028 /* On 5700/5701 chips, we need to set this bit.
15029 * Otherwise the chip will issue cacheline transactions
15030 * to streamable DMA memory with not all the byte
15031 * enables turned on. This is an error on several
15032 * RISC PCI controllers, in particular sparc64.
15033 *
15034 * On 5703/5704 chips, this bit has been reassigned
15035 * a different meaning. In particular, it is used
15036 * on those chips to enable a PCI-X workaround.
15037 */
15038 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
15039 }
15040
15041 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15042
15043#if 0
15044 /* Unneeded, already done by tg3_get_invariants. */
15045 tg3_switch_clocks(tp);
15046#endif
15047
Linus Torvalds1da177e2005-04-16 15:20:36 -070015048 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
15049 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
15050 goto out;
15051
David S. Miller59e6b432005-05-18 22:50:10 -070015052 /* It is best to perform DMA test with maximum write burst size
15053 * to expose the 5700/5701 write DMA bug.
15054 */
15055 saved_dma_rwctrl = tp->dma_rwctrl;
15056 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
15057 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15058
Linus Torvalds1da177e2005-04-16 15:20:36 -070015059 while (1) {
15060 u32 *p = buf, i;
15061
15062 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
15063 p[i] = i;
15064
15065 /* Send the buffer to the chip. */
15066 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
15067 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000015068 dev_err(&tp->pdev->dev,
15069 "%s: Buffer write failed. err = %d\n",
15070 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015071 break;
15072 }
15073
15074#if 0
15075 /* validate data reached card RAM correctly. */
15076 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
15077 u32 val;
15078 tg3_read_mem(tp, 0x2100 + (i*4), &val);
15079 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000015080 dev_err(&tp->pdev->dev,
15081 "%s: Buffer corrupted on device! "
15082 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015083 /* ret = -ENODEV here? */
15084 }
15085 p[i] = 0;
15086 }
15087#endif
15088 /* Now read it back. */
15089 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
15090 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000015091 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
15092 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015093 break;
15094 }
15095
15096 /* Verify it. */
15097 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
15098 if (p[i] == i)
15099 continue;
15100
David S. Miller59e6b432005-05-18 22:50:10 -070015101 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
15102 DMA_RWCTRL_WRITE_BNDRY_16) {
15103 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015104 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
15105 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15106 break;
15107 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000015108 dev_err(&tp->pdev->dev,
15109 "%s: Buffer corrupted on read back! "
15110 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015111 ret = -ENODEV;
15112 goto out;
15113 }
15114 }
15115
15116 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
15117 /* Success. */
15118 ret = 0;
15119 break;
15120 }
15121 }
David S. Miller59e6b432005-05-18 22:50:10 -070015122 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
15123 DMA_RWCTRL_WRITE_BNDRY_16) {
15124 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070015125 * now look for chipsets that are known to expose the
15126 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070015127 */
Matt Carlson41434702011-03-09 16:58:22 +000015128 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070015129 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
15130 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a5882010-04-05 10:19:28 +000015131 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070015132 /* Safe to use the calculated DMA boundary. */
15133 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a5882010-04-05 10:19:28 +000015134 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070015135
David S. Miller59e6b432005-05-18 22:50:10 -070015136 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015138
15139out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000015140 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015141out_nofree:
15142 return ret;
15143}
15144
Linus Torvalds1da177e2005-04-16 15:20:36 -070015145static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
15146{
Joe Perches63c3a662011-04-26 08:12:10 +000015147 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson666bc832010-01-20 16:58:03 +000015148 tp->bufmgr_config.mbuf_read_dma_low_water =
15149 DEFAULT_MB_RDMA_LOW_WATER_5705;
15150 tp->bufmgr_config.mbuf_mac_rx_low_water =
15151 DEFAULT_MB_MACRX_LOW_WATER_57765;
15152 tp->bufmgr_config.mbuf_high_water =
15153 DEFAULT_MB_HIGH_WATER_57765;
15154
15155 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15156 DEFAULT_MB_RDMA_LOW_WATER_5705;
15157 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15158 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
15159 tp->bufmgr_config.mbuf_high_water_jumbo =
15160 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000015161 } else if (tg3_flag(tp, 5705_PLUS)) {
Michael Chanfdfec172005-07-25 12:31:48 -070015162 tp->bufmgr_config.mbuf_read_dma_low_water =
15163 DEFAULT_MB_RDMA_LOW_WATER_5705;
15164 tp->bufmgr_config.mbuf_mac_rx_low_water =
15165 DEFAULT_MB_MACRX_LOW_WATER_5705;
15166 tp->bufmgr_config.mbuf_high_water =
15167 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070015168 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
15169 tp->bufmgr_config.mbuf_mac_rx_low_water =
15170 DEFAULT_MB_MACRX_LOW_WATER_5906;
15171 tp->bufmgr_config.mbuf_high_water =
15172 DEFAULT_MB_HIGH_WATER_5906;
15173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015174
Michael Chanfdfec172005-07-25 12:31:48 -070015175 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15176 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
15177 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15178 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
15179 tp->bufmgr_config.mbuf_high_water_jumbo =
15180 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
15181 } else {
15182 tp->bufmgr_config.mbuf_read_dma_low_water =
15183 DEFAULT_MB_RDMA_LOW_WATER;
15184 tp->bufmgr_config.mbuf_mac_rx_low_water =
15185 DEFAULT_MB_MACRX_LOW_WATER;
15186 tp->bufmgr_config.mbuf_high_water =
15187 DEFAULT_MB_HIGH_WATER;
15188
15189 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15190 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
15191 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15192 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
15193 tp->bufmgr_config.mbuf_high_water_jumbo =
15194 DEFAULT_MB_HIGH_WATER_JUMBO;
15195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015196
15197 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
15198 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
15199}
15200
15201static char * __devinit tg3_phy_string(struct tg3 *tp)
15202{
Matt Carlson79eb6902010-02-17 15:17:03 +000015203 switch (tp->phy_id & TG3_PHY_ID_MASK) {
15204 case TG3_PHY_ID_BCM5400: return "5400";
15205 case TG3_PHY_ID_BCM5401: return "5401";
15206 case TG3_PHY_ID_BCM5411: return "5411";
15207 case TG3_PHY_ID_BCM5701: return "5701";
15208 case TG3_PHY_ID_BCM5703: return "5703";
15209 case TG3_PHY_ID_BCM5704: return "5704";
15210 case TG3_PHY_ID_BCM5705: return "5705";
15211 case TG3_PHY_ID_BCM5750: return "5750";
15212 case TG3_PHY_ID_BCM5752: return "5752";
15213 case TG3_PHY_ID_BCM5714: return "5714";
15214 case TG3_PHY_ID_BCM5780: return "5780";
15215 case TG3_PHY_ID_BCM5755: return "5755";
15216 case TG3_PHY_ID_BCM5787: return "5787";
15217 case TG3_PHY_ID_BCM5784: return "5784";
15218 case TG3_PHY_ID_BCM5756: return "5722/5756";
15219 case TG3_PHY_ID_BCM5906: return "5906";
15220 case TG3_PHY_ID_BCM5761: return "5761";
15221 case TG3_PHY_ID_BCM5718C: return "5718C";
15222 case TG3_PHY_ID_BCM5718S: return "5718S";
15223 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000015224 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000015225 case TG3_PHY_ID_BCM5720C: return "5720C";
Matt Carlson79eb6902010-02-17 15:17:03 +000015226 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070015227 case 0: return "serdes";
15228 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070015229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015230}
15231
Michael Chanf9804dd2005-09-27 12:13:10 -070015232static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
15233{
Joe Perches63c3a662011-04-26 08:12:10 +000015234 if (tg3_flag(tp, PCI_EXPRESS)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070015235 strcpy(str, "PCI Express");
15236 return str;
Joe Perches63c3a662011-04-26 08:12:10 +000015237 } else if (tg3_flag(tp, PCIX_MODE)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070015238 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
15239
15240 strcpy(str, "PCIX:");
15241
15242 if ((clock_ctrl == 7) ||
15243 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
15244 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
15245 strcat(str, "133MHz");
15246 else if (clock_ctrl == 0)
15247 strcat(str, "33MHz");
15248 else if (clock_ctrl == 2)
15249 strcat(str, "50MHz");
15250 else if (clock_ctrl == 4)
15251 strcat(str, "66MHz");
15252 else if (clock_ctrl == 6)
15253 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070015254 } else {
15255 strcpy(str, "PCI:");
Joe Perches63c3a662011-04-26 08:12:10 +000015256 if (tg3_flag(tp, PCI_HIGH_SPEED))
Michael Chanf9804dd2005-09-27 12:13:10 -070015257 strcat(str, "66MHz");
15258 else
15259 strcat(str, "33MHz");
15260 }
Joe Perches63c3a662011-04-26 08:12:10 +000015261 if (tg3_flag(tp, PCI_32BIT))
Michael Chanf9804dd2005-09-27 12:13:10 -070015262 strcat(str, ":32-bit");
15263 else
15264 strcat(str, ":64-bit");
15265 return str;
15266}
15267
Michael Chan8c2dc7e2005-12-19 16:26:02 -080015268static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015269{
15270 struct pci_dev *peer;
15271 unsigned int func, devnr = tp->pdev->devfn & ~7;
15272
15273 for (func = 0; func < 8; func++) {
15274 peer = pci_get_slot(tp->pdev->bus, devnr | func);
15275 if (peer && peer != tp->pdev)
15276 break;
15277 pci_dev_put(peer);
15278 }
Michael Chan16fe9d72005-12-13 21:09:54 -080015279 /* 5704 can be configured in single-port mode, set peer to
15280 * tp->pdev in that case.
15281 */
15282 if (!peer) {
15283 peer = tp->pdev;
15284 return peer;
15285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015286
15287 /*
15288 * We don't need to keep the refcount elevated; there's no way
15289 * to remove one half of this device without removing the other
15290 */
15291 pci_dev_put(peer);
15292
15293 return peer;
15294}
15295
David S. Miller15f98502005-05-18 22:49:26 -070015296static void __devinit tg3_init_coal(struct tg3 *tp)
15297{
15298 struct ethtool_coalesce *ec = &tp->coal;
15299
15300 memset(ec, 0, sizeof(*ec));
15301 ec->cmd = ETHTOOL_GCOALESCE;
15302 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
15303 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
15304 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
15305 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
15306 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
15307 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
15308 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
15309 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
15310 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
15311
15312 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
15313 HOSTCC_MODE_CLRTICK_TXBD)) {
15314 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
15315 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
15316 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
15317 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
15318 }
Michael Chand244c892005-07-05 14:42:33 -070015319
Joe Perches63c3a662011-04-26 08:12:10 +000015320 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070015321 ec->rx_coalesce_usecs_irq = 0;
15322 ec->tx_coalesce_usecs_irq = 0;
15323 ec->stats_block_coalesce_usecs = 0;
15324 }
David S. Miller15f98502005-05-18 22:49:26 -070015325}
15326
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080015327static const struct net_device_ops tg3_netdev_ops = {
15328 .ndo_open = tg3_open,
15329 .ndo_stop = tg3_close,
Stephen Hemminger00829822008-11-20 20:14:53 -080015330 .ndo_start_xmit = tg3_start_xmit,
Eric Dumazet511d2222010-07-07 20:44:24 +000015331 .ndo_get_stats64 = tg3_get_stats64,
Stephen Hemminger00829822008-11-20 20:14:53 -080015332 .ndo_validate_addr = eth_validate_addr,
Jiri Pirkoafc4b132011-08-16 06:29:01 +000015333 .ndo_set_rx_mode = tg3_set_rx_mode,
Stephen Hemminger00829822008-11-20 20:14:53 -080015334 .ndo_set_mac_address = tg3_set_mac_addr,
15335 .ndo_do_ioctl = tg3_ioctl,
15336 .ndo_tx_timeout = tg3_tx_timeout,
15337 .ndo_change_mtu = tg3_change_mtu,
Michał Mirosławdc668912011-04-07 03:35:07 +000015338 .ndo_fix_features = tg3_fix_features,
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015339 .ndo_set_features = tg3_set_features,
Stephen Hemminger00829822008-11-20 20:14:53 -080015340#ifdef CONFIG_NET_POLL_CONTROLLER
15341 .ndo_poll_controller = tg3_poll_controller,
15342#endif
15343};
15344
Linus Torvalds1da177e2005-04-16 15:20:36 -070015345static int __devinit tg3_init_one(struct pci_dev *pdev,
15346 const struct pci_device_id *ent)
15347{
Linus Torvalds1da177e2005-04-16 15:20:36 -070015348 struct net_device *dev;
15349 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000015350 int i, err, pm_cap;
15351 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070015352 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080015353 u64 dma_mask, persist_dma_mask;
Michał Mirosławc8f44af2011-11-15 15:29:55 +000015354 netdev_features_t features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015355
Joe Perches05dbe002010-02-17 19:44:19 +000015356 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015357
15358 err = pci_enable_device(pdev);
15359 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015360 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015361 return err;
15362 }
15363
Linus Torvalds1da177e2005-04-16 15:20:36 -070015364 err = pci_request_regions(pdev, DRV_MODULE_NAME);
15365 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015366 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015367 goto err_out_disable_pdev;
15368 }
15369
15370 pci_set_master(pdev);
15371
15372 /* Find power-management capability. */
15373 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
15374 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000015375 dev_err(&pdev->dev,
15376 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015377 err = -EIO;
15378 goto err_out_free_res;
15379 }
15380
Matt Carlson16821282011-07-13 09:27:28 +000015381 err = pci_set_power_state(pdev, PCI_D0);
15382 if (err) {
15383 dev_err(&pdev->dev, "Transition to D0 failed, aborting\n");
15384 goto err_out_free_res;
15385 }
15386
Matt Carlsonfe5f5782009-09-01 13:09:39 +000015387 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015388 if (!dev) {
Matt Carlson2445e462010-04-05 10:19:21 +000015389 dev_err(&pdev->dev, "Etherdev alloc failed, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015390 err = -ENOMEM;
Matt Carlson16821282011-07-13 09:27:28 +000015391 goto err_out_power_down;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015392 }
15393
Linus Torvalds1da177e2005-04-16 15:20:36 -070015394 SET_NETDEV_DEV(dev, &pdev->dev);
15395
Linus Torvalds1da177e2005-04-16 15:20:36 -070015396 tp = netdev_priv(dev);
15397 tp->pdev = pdev;
15398 tp->dev = dev;
15399 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015400 tp->rx_mode = TG3_DEF_RX_MODE;
15401 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070015402
Linus Torvalds1da177e2005-04-16 15:20:36 -070015403 if (tg3_debug > 0)
15404 tp->msg_enable = tg3_debug;
15405 else
15406 tp->msg_enable = TG3_DEF_MSG_ENABLE;
15407
15408 /* The word/byte swap controls here control register access byte
15409 * swapping. DMA data byte swapping is controlled in the GRC_MODE
15410 * setting below.
15411 */
15412 tp->misc_host_ctrl =
15413 MISC_HOST_CTRL_MASK_PCI_INT |
15414 MISC_HOST_CTRL_WORD_SWAP |
15415 MISC_HOST_CTRL_INDIR_ACCESS |
15416 MISC_HOST_CTRL_PCISTATE_RW;
15417
15418 /* The NONFRM (non-frame) byte/word swap controls take effect
15419 * on descriptor entries, anything which isn't packet data.
15420 *
15421 * The StrongARM chips on the board (one for tx, one for rx)
15422 * are running in big-endian mode.
15423 */
15424 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
15425 GRC_MODE_WSWAP_NONFRM_DATA);
15426#ifdef __BIG_ENDIAN
15427 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
15428#endif
15429 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015430 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000015431 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015432
Matt Carlsond5fe4882008-11-21 17:20:32 -080015433 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010015434 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015435 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015436 err = -ENOMEM;
15437 goto err_out_free_dev;
15438 }
15439
Matt Carlsonc9cab242011-07-13 09:27:27 +000015440 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
15441 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761E ||
15442 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S ||
15443 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761SE ||
15444 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
15445 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
15446 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
15447 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720) {
15448 tg3_flag_set(tp, ENABLE_APE);
15449 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
15450 if (!tp->aperegs) {
15451 dev_err(&pdev->dev,
15452 "Cannot map APE registers, aborting\n");
15453 err = -ENOMEM;
15454 goto err_out_iounmap;
15455 }
15456 }
15457
Linus Torvalds1da177e2005-04-16 15:20:36 -070015458 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
15459 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015460
Linus Torvalds1da177e2005-04-16 15:20:36 -070015461 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015462 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Matt Carlson2ffcc982011-05-19 12:12:44 +000015463 dev->netdev_ops = &tg3_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015464 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015465
15466 err = tg3_get_invariants(tp);
15467 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015468 dev_err(&pdev->dev,
15469 "Problem fetching invariants of chip, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015470 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015471 }
15472
Michael Chan4a29cc22006-03-19 13:21:12 -080015473 /* The EPB bridge inside 5714, 5715, and 5780 and any
15474 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080015475 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
15476 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
15477 * do DMA address check in tg3_start_xmit().
15478 */
Joe Perches63c3a662011-04-26 08:12:10 +000015479 if (tg3_flag(tp, IS_5788))
Yang Hongyang284901a2009-04-06 19:01:15 -070015480 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Joe Perches63c3a662011-04-26 08:12:10 +000015481 else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070015482 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080015483#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070015484 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015485#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080015486 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070015487 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015488
15489 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070015490 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080015491 err = pci_set_dma_mask(pdev, dma_mask);
15492 if (!err) {
Matt Carlson0da06062011-05-19 12:12:53 +000015493 features |= NETIF_F_HIGHDMA;
Michael Chan72f2afb2006-03-06 19:28:35 -080015494 err = pci_set_consistent_dma_mask(pdev,
15495 persist_dma_mask);
15496 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015497 dev_err(&pdev->dev, "Unable to obtain 64 bit "
15498 "DMA for consistent allocations\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015499 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080015500 }
15501 }
15502 }
Yang Hongyang284901a2009-04-06 19:01:15 -070015503 if (err || dma_mask == DMA_BIT_MASK(32)) {
15504 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080015505 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015506 dev_err(&pdev->dev,
15507 "No usable DMA configuration, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015508 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080015509 }
15510 }
15511
Michael Chanfdfec172005-07-25 12:31:48 -070015512 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015513
Matt Carlson0da06062011-05-19 12:12:53 +000015514 features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
15515
15516 /* 5700 B0 chips do not support checksumming correctly due
15517 * to hardware bugs.
15518 */
15519 if (tp->pci_chip_rev_id != CHIPREV_ID_5700_B0) {
15520 features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
15521
15522 if (tg3_flag(tp, 5755_PLUS))
15523 features |= NETIF_F_IPV6_CSUM;
15524 }
15525
Michael Chan4e3a7aa2006-03-20 17:47:44 -080015526 /* TSO is on by default on chips that support hardware TSO.
15527 * Firmware TSO on older chips gives lower performance, so it
15528 * is off by default, but can be enabled using ethtool.
15529 */
Joe Perches63c3a662011-04-26 08:12:10 +000015530 if ((tg3_flag(tp, HW_TSO_1) ||
15531 tg3_flag(tp, HW_TSO_2) ||
15532 tg3_flag(tp, HW_TSO_3)) &&
Matt Carlson0da06062011-05-19 12:12:53 +000015533 (features & NETIF_F_IP_CSUM))
15534 features |= NETIF_F_TSO;
Joe Perches63c3a662011-04-26 08:12:10 +000015535 if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
Matt Carlson0da06062011-05-19 12:12:53 +000015536 if (features & NETIF_F_IPV6_CSUM)
15537 features |= NETIF_F_TSO6;
Joe Perches63c3a662011-04-26 08:12:10 +000015538 if (tg3_flag(tp, HW_TSO_3) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000015539 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070015540 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
15541 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Joe Perches63c3a662011-04-26 08:12:10 +000015542 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Michał Mirosławdc668912011-04-07 03:35:07 +000015543 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson0da06062011-05-19 12:12:53 +000015544 features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070015545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015546
Matt Carlsond542fe22011-05-19 16:02:43 +000015547 dev->features |= features;
15548 dev->vlan_features |= features;
15549
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015550 /*
15551 * Add loopback capability only for a subset of devices that support
15552 * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
15553 * loopback for the remaining devices.
15554 */
15555 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
15556 !tg3_flag(tp, CPMU_PRESENT))
15557 /* Add the loopback capability */
Matt Carlson0da06062011-05-19 12:12:53 +000015558 features |= NETIF_F_LOOPBACK;
15559
Matt Carlson0da06062011-05-19 12:12:53 +000015560 dev->hw_features |= features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015561
Linus Torvalds1da177e2005-04-16 15:20:36 -070015562 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015563 !tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015564 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015565 tg3_flag_set(tp, MAX_RXPEND_64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015566 tp->rx_pending = 63;
15567 }
15568
Linus Torvalds1da177e2005-04-16 15:20:36 -070015569 err = tg3_get_device_address(tp);
15570 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015571 dev_err(&pdev->dev,
15572 "Could not obtain valid ethernet address, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015573 goto err_out_apeunmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070015574 }
15575
Matt Carlsonc88864d2007-11-12 21:07:01 -080015576 /*
15577 * Reset chip in case UNDI or EFI driver did not shutdown
15578 * DMA self test will enable WDMAC and we'll see (spurious)
15579 * pending DMA on the PCI bus at that point.
15580 */
15581 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
15582 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
15583 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
15584 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
15585 }
15586
15587 err = tg3_test_dma(tp);
15588 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015589 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080015590 goto err_out_apeunmap;
15591 }
15592
Matt Carlson78f90dc2009-11-13 13:03:42 +000015593 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
15594 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
15595 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000015596 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000015597 struct tg3_napi *tnapi = &tp->napi[i];
15598
15599 tnapi->tp = tp;
15600 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
15601
15602 tnapi->int_mbox = intmbx;
Matt Carlson93a700a2011-08-31 11:44:54 +000015603 if (i <= 4)
Matt Carlson78f90dc2009-11-13 13:03:42 +000015604 intmbx += 0x8;
15605 else
15606 intmbx += 0x4;
15607
15608 tnapi->consmbox = rcvmbx;
15609 tnapi->prodmbox = sndmbx;
15610
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015611 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000015612 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015613 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000015614 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000015615
Joe Perches63c3a662011-04-26 08:12:10 +000015616 if (!tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson78f90dc2009-11-13 13:03:42 +000015617 break;
15618
15619 /*
15620 * If we support MSIX, we'll be using RSS. If we're using
15621 * RSS, the first vector only handles link interrupts and the
15622 * remaining vectors handle rx and tx interrupts. Reuse the
15623 * mailbox values for the next iteration. The values we setup
15624 * above are still useful for the single vectored mode.
15625 */
15626 if (!i)
15627 continue;
15628
15629 rcvmbx += 0x8;
15630
15631 if (sndmbx & 0x4)
15632 sndmbx -= 0x4;
15633 else
15634 sndmbx += 0xc;
15635 }
15636
Matt Carlsonc88864d2007-11-12 21:07:01 -080015637 tg3_init_coal(tp);
15638
Michael Chanc49a1562006-12-17 17:07:29 -080015639 pci_set_drvdata(pdev, dev);
15640
Matt Carlsoncd0d7222011-07-13 09:27:33 +000015641 if (tg3_flag(tp, 5717_PLUS)) {
15642 /* Resume a low-power mode */
15643 tg3_frob_aux_power(tp, false);
15644 }
15645
Linus Torvalds1da177e2005-04-16 15:20:36 -070015646 err = register_netdev(dev);
15647 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015648 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070015649 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015650 }
15651
Joe Perches05dbe002010-02-17 19:44:19 +000015652 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
15653 tp->board_part_number,
15654 tp->pci_chip_rev_id,
15655 tg3_bus_string(tp, str),
15656 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015657
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015658 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000015659 struct phy_device *phydev;
15660 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000015661 netdev_info(dev,
15662 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000015663 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015664 } else {
15665 char *ethtype;
15666
15667 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
15668 ethtype = "10/100Base-TX";
15669 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
15670 ethtype = "1000Base-SX";
15671 else
15672 ethtype = "10/100/1000Base-T";
15673
Matt Carlson5129c3a2010-04-05 10:19:23 +000015674 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlson47007832011-04-20 07:57:43 +000015675 "(WireSpeed[%d], EEE[%d])\n",
15676 tg3_phy_string(tp), ethtype,
15677 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
15678 (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015679 }
Matt Carlsondf59c942008-11-03 16:52:56 -080015680
Joe Perches05dbe002010-02-17 19:44:19 +000015681 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000015682 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015683 tg3_flag(tp, USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015684 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015685 tg3_flag(tp, ENABLE_ASF) != 0,
15686 tg3_flag(tp, TSO_CAPABLE) != 0);
Joe Perches05dbe002010-02-17 19:44:19 +000015687 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
15688 tp->dma_rwctrl,
15689 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
15690 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015691
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015692 pci_save_state(pdev);
15693
Linus Torvalds1da177e2005-04-16 15:20:36 -070015694 return 0;
15695
Matt Carlson0d3031d2007-10-10 18:02:43 -070015696err_out_apeunmap:
15697 if (tp->aperegs) {
15698 iounmap(tp->aperegs);
15699 tp->aperegs = NULL;
15700 }
15701
Linus Torvalds1da177e2005-04-16 15:20:36 -070015702err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070015703 if (tp->regs) {
15704 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015705 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015707
15708err_out_free_dev:
15709 free_netdev(dev);
15710
Matt Carlson16821282011-07-13 09:27:28 +000015711err_out_power_down:
15712 pci_set_power_state(pdev, PCI_D3hot);
15713
Linus Torvalds1da177e2005-04-16 15:20:36 -070015714err_out_free_res:
15715 pci_release_regions(pdev);
15716
15717err_out_disable_pdev:
15718 pci_disable_device(pdev);
15719 pci_set_drvdata(pdev, NULL);
15720 return err;
15721}
15722
15723static void __devexit tg3_remove_one(struct pci_dev *pdev)
15724{
15725 struct net_device *dev = pci_get_drvdata(pdev);
15726
15727 if (dev) {
15728 struct tg3 *tp = netdev_priv(dev);
15729
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080015730 if (tp->fw)
15731 release_firmware(tp->fw);
15732
Matt Carlsondb219972011-11-04 09:15:03 +000015733 tg3_reset_task_cancel(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015734
David S. Miller1805b2f2011-10-24 18:18:09 -040015735 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015736 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015737 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015738 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070015739
Linus Torvalds1da177e2005-04-16 15:20:36 -070015740 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015741 if (tp->aperegs) {
15742 iounmap(tp->aperegs);
15743 tp->aperegs = NULL;
15744 }
Michael Chan68929142005-08-09 20:17:14 -070015745 if (tp->regs) {
15746 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015747 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015749 free_netdev(dev);
15750 pci_release_regions(pdev);
15751 pci_disable_device(pdev);
15752 pci_set_drvdata(pdev, NULL);
15753 }
15754}
15755
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015756#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015757static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015758{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015759 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015760 struct net_device *dev = pci_get_drvdata(pdev);
15761 struct tg3 *tp = netdev_priv(dev);
15762 int err;
15763
15764 if (!netif_running(dev))
15765 return 0;
15766
Matt Carlsondb219972011-11-04 09:15:03 +000015767 tg3_reset_task_cancel(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015768 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015769 tg3_netif_stop(tp);
15770
15771 del_timer_sync(&tp->timer);
15772
David S. Millerf47c11e2005-06-24 20:18:35 -070015773 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015774 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070015775 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015776
15777 netif_device_detach(dev);
15778
David S. Millerf47c11e2005-06-24 20:18:35 -070015779 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070015780 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches63c3a662011-04-26 08:12:10 +000015781 tg3_flag_clear(tp, INIT_COMPLETE);
David S. Millerf47c11e2005-06-24 20:18:35 -070015782 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015783
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015784 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015785 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015786 int err2;
15787
David S. Millerf47c11e2005-06-24 20:18:35 -070015788 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015789
Joe Perches63c3a662011-04-26 08:12:10 +000015790 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015791 err2 = tg3_restart_hw(tp, 1);
15792 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070015793 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015794
15795 tp->timer.expires = jiffies + tp->timer_offset;
15796 add_timer(&tp->timer);
15797
15798 netif_device_attach(dev);
15799 tg3_netif_start(tp);
15800
Michael Chanb9ec6c12006-07-25 16:37:27 -070015801out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015802 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015803
15804 if (!err2)
15805 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015806 }
15807
15808 return err;
15809}
15810
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015811static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015812{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015813 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015814 struct net_device *dev = pci_get_drvdata(pdev);
15815 struct tg3 *tp = netdev_priv(dev);
15816 int err;
15817
15818 if (!netif_running(dev))
15819 return 0;
15820
Linus Torvalds1da177e2005-04-16 15:20:36 -070015821 netif_device_attach(dev);
15822
David S. Millerf47c11e2005-06-24 20:18:35 -070015823 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015824
Joe Perches63c3a662011-04-26 08:12:10 +000015825 tg3_flag_set(tp, INIT_COMPLETE);
Michael Chanb9ec6c12006-07-25 16:37:27 -070015826 err = tg3_restart_hw(tp, 1);
15827 if (err)
15828 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015829
15830 tp->timer.expires = jiffies + tp->timer_offset;
15831 add_timer(&tp->timer);
15832
Linus Torvalds1da177e2005-04-16 15:20:36 -070015833 tg3_netif_start(tp);
15834
Michael Chanb9ec6c12006-07-25 16:37:27 -070015835out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015836 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015837
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015838 if (!err)
15839 tg3_phy_start(tp);
15840
Michael Chanb9ec6c12006-07-25 16:37:27 -070015841 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015842}
15843
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015844static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015845#define TG3_PM_OPS (&tg3_pm_ops)
15846
15847#else
15848
15849#define TG3_PM_OPS NULL
15850
15851#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015852
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015853/**
15854 * tg3_io_error_detected - called when PCI error is detected
15855 * @pdev: Pointer to PCI device
15856 * @state: The current pci connection state
15857 *
15858 * This function is called after a PCI bus error affecting
15859 * this device has been detected.
15860 */
15861static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
15862 pci_channel_state_t state)
15863{
15864 struct net_device *netdev = pci_get_drvdata(pdev);
15865 struct tg3 *tp = netdev_priv(netdev);
15866 pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
15867
15868 netdev_info(netdev, "PCI I/O error detected\n");
15869
15870 rtnl_lock();
15871
15872 if (!netif_running(netdev))
15873 goto done;
15874
15875 tg3_phy_stop(tp);
15876
15877 tg3_netif_stop(tp);
15878
15879 del_timer_sync(&tp->timer);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015880
15881 /* Want to make sure that the reset task doesn't run */
Matt Carlsondb219972011-11-04 09:15:03 +000015882 tg3_reset_task_cancel(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000015883 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015884
15885 netif_device_detach(netdev);
15886
15887 /* Clean up software state, even if MMIO is blocked */
15888 tg3_full_lock(tp, 0);
15889 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
15890 tg3_full_unlock(tp);
15891
15892done:
15893 if (state == pci_channel_io_perm_failure)
15894 err = PCI_ERS_RESULT_DISCONNECT;
15895 else
15896 pci_disable_device(pdev);
15897
15898 rtnl_unlock();
15899
15900 return err;
15901}
15902
15903/**
15904 * tg3_io_slot_reset - called after the pci bus has been reset.
15905 * @pdev: Pointer to PCI device
15906 *
15907 * Restart the card from scratch, as if from a cold-boot.
15908 * At this point, the card has exprienced a hard reset,
15909 * followed by fixups by BIOS, and has its config space
15910 * set up identically to what it was at cold boot.
15911 */
15912static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
15913{
15914 struct net_device *netdev = pci_get_drvdata(pdev);
15915 struct tg3 *tp = netdev_priv(netdev);
15916 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
15917 int err;
15918
15919 rtnl_lock();
15920
15921 if (pci_enable_device(pdev)) {
15922 netdev_err(netdev, "Cannot re-enable PCI device after reset.\n");
15923 goto done;
15924 }
15925
15926 pci_set_master(pdev);
15927 pci_restore_state(pdev);
15928 pci_save_state(pdev);
15929
15930 if (!netif_running(netdev)) {
15931 rc = PCI_ERS_RESULT_RECOVERED;
15932 goto done;
15933 }
15934
15935 err = tg3_power_up(tp);
Matt Carlsonbed98292011-07-13 09:27:29 +000015936 if (err)
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015937 goto done;
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015938
15939 rc = PCI_ERS_RESULT_RECOVERED;
15940
15941done:
15942 rtnl_unlock();
15943
15944 return rc;
15945}
15946
15947/**
15948 * tg3_io_resume - called when traffic can start flowing again.
15949 * @pdev: Pointer to PCI device
15950 *
15951 * This callback is called when the error recovery driver tells
15952 * us that its OK to resume normal operation.
15953 */
15954static void tg3_io_resume(struct pci_dev *pdev)
15955{
15956 struct net_device *netdev = pci_get_drvdata(pdev);
15957 struct tg3 *tp = netdev_priv(netdev);
15958 int err;
15959
15960 rtnl_lock();
15961
15962 if (!netif_running(netdev))
15963 goto done;
15964
15965 tg3_full_lock(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +000015966 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015967 err = tg3_restart_hw(tp, 1);
15968 tg3_full_unlock(tp);
15969 if (err) {
15970 netdev_err(netdev, "Cannot restart hardware after reset.\n");
15971 goto done;
15972 }
15973
15974 netif_device_attach(netdev);
15975
15976 tp->timer.expires = jiffies + tp->timer_offset;
15977 add_timer(&tp->timer);
15978
15979 tg3_netif_start(tp);
15980
15981 tg3_phy_start(tp);
15982
15983done:
15984 rtnl_unlock();
15985}
15986
15987static struct pci_error_handlers tg3_err_handler = {
15988 .error_detected = tg3_io_error_detected,
15989 .slot_reset = tg3_io_slot_reset,
15990 .resume = tg3_io_resume
15991};
15992
Linus Torvalds1da177e2005-04-16 15:20:36 -070015993static struct pci_driver tg3_driver = {
15994 .name = DRV_MODULE_NAME,
15995 .id_table = tg3_pci_tbl,
15996 .probe = tg3_init_one,
15997 .remove = __devexit_p(tg3_remove_one),
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015998 .err_handler = &tg3_err_handler,
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015999 .driver.pm = TG3_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070016000};
16001
16002static int __init tg3_init(void)
16003{
Jeff Garzik29917622006-08-19 17:48:59 -040016004 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016005}
16006
16007static void __exit tg3_cleanup(void)
16008{
16009 pci_unregister_driver(&tg3_driver);
16010}
16011
16012module_init(tg3_init);
16013module_exit(tg3_cleanup);