blob: 0766d032bd9b0c28a8121a2b030f6f4cebd5b8e5 [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 Carlson66711e662009-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
Matt Carlson95e28692008-05-25 23:44:14 -07001674static u16 tg3_advert_flowctrl_1000X(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_1000XPAUSE;
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_1000XPSE_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_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1684 else
1685 miireg = 0;
1686
1687 return miireg;
1688}
1689
Matt Carlson95e28692008-05-25 23:44:14 -07001690static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1691{
1692 u8 cap = 0;
1693
Matt Carlsonf3791cd2011-11-21 15:01:17 +00001694 if (lcladv & rmtadv & ADVERTISE_1000XPAUSE) {
1695 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
1696 } else if (lcladv & rmtadv & ADVERTISE_1000XPSE_ASYM) {
1697 if (lcladv & ADVERTISE_1000XPAUSE)
1698 cap = FLOW_CTRL_RX;
1699 if (rmtadv & ADVERTISE_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001700 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001701 }
1702
1703 return cap;
1704}
1705
Matt Carlsonf51f3562008-05-25 23:45:08 -07001706static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001707{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001708 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001709 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001710 u32 old_rx_mode = tp->rx_mode;
1711 u32 old_tx_mode = tp->tx_mode;
1712
Joe Perches63c3a662011-04-26 08:12:10 +00001713 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001714 autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001715 else
1716 autoneg = tp->link_config.autoneg;
1717
Joe Perches63c3a662011-04-26 08:12:10 +00001718 if (autoneg == AUTONEG_ENABLE && tg3_flag(tp, PAUSE_AUTONEG)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001719 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001720 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001721 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001722 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001723 } else
1724 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001725
Matt Carlsonf51f3562008-05-25 23:45:08 -07001726 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001727
Steve Glendinninge18ce342008-12-16 02:00:00 -08001728 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001729 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1730 else
1731 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1732
Matt Carlsonf51f3562008-05-25 23:45:08 -07001733 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001734 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001735
Steve Glendinninge18ce342008-12-16 02:00:00 -08001736 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001737 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1738 else
1739 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1740
Matt Carlsonf51f3562008-05-25 23:45:08 -07001741 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001742 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001743}
1744
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001745static void tg3_adjust_link(struct net_device *dev)
1746{
1747 u8 oldflowctrl, linkmesg = 0;
1748 u32 mac_mode, lcl_adv, rmt_adv;
1749 struct tg3 *tp = netdev_priv(dev);
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001750 struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001751
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001752 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001753
1754 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
1755 MAC_MODE_HALF_DUPLEX);
1756
1757 oldflowctrl = tp->link_config.active_flowctrl;
1758
1759 if (phydev->link) {
1760 lcl_adv = 0;
1761 rmt_adv = 0;
1762
1763 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
1764 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001765 else if (phydev->speed == SPEED_1000 ||
1766 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001767 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001768 else
1769 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001770
1771 if (phydev->duplex == DUPLEX_HALF)
1772 mac_mode |= MAC_MODE_HALF_DUPLEX;
1773 else {
Matt Carlsonf88788f2011-12-14 11:10:00 +00001774 lcl_adv = mii_advertise_flowctrl(
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001775 tp->link_config.flowctrl);
1776
1777 if (phydev->pause)
1778 rmt_adv = LPA_PAUSE_CAP;
1779 if (phydev->asym_pause)
1780 rmt_adv |= LPA_PAUSE_ASYM;
1781 }
1782
1783 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
1784 } else
1785 mac_mode |= MAC_MODE_PORT_MODE_GMII;
1786
1787 if (mac_mode != tp->mac_mode) {
1788 tp->mac_mode = mac_mode;
1789 tw32_f(MAC_MODE, tp->mac_mode);
1790 udelay(40);
1791 }
1792
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001793 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
1794 if (phydev->speed == SPEED_10)
1795 tw32(MAC_MI_STAT,
1796 MAC_MI_STAT_10MBPS_MODE |
1797 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1798 else
1799 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1800 }
1801
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001802 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
1803 tw32(MAC_TX_LENGTHS,
1804 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1805 (6 << TX_LENGTHS_IPG_SHIFT) |
1806 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
1807 else
1808 tw32(MAC_TX_LENGTHS,
1809 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1810 (6 << TX_LENGTHS_IPG_SHIFT) |
1811 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
1812
1813 if ((phydev->link && tp->link_config.active_speed == SPEED_INVALID) ||
1814 (!phydev->link && tp->link_config.active_speed != SPEED_INVALID) ||
1815 phydev->speed != tp->link_config.active_speed ||
1816 phydev->duplex != tp->link_config.active_duplex ||
1817 oldflowctrl != tp->link_config.active_flowctrl)
Matt Carlsonc6cdf432010-04-05 10:19:26 +00001818 linkmesg = 1;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001819
1820 tp->link_config.active_speed = phydev->speed;
1821 tp->link_config.active_duplex = phydev->duplex;
1822
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001823 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001824
1825 if (linkmesg)
1826 tg3_link_report(tp);
1827}
1828
1829static int tg3_phy_init(struct tg3 *tp)
1830{
1831 struct phy_device *phydev;
1832
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001833 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001834 return 0;
1835
1836 /* Bring the PHY back to a known state. */
1837 tg3_bmcr_reset(tp);
1838
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001839 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001840
1841 /* Attach the MAC to the PHY. */
Kay Sieversfb28ad32008-11-10 13:55:14 -08001842 phydev = phy_connect(tp->dev, dev_name(&phydev->dev), tg3_adjust_link,
Matt Carlsona9daf362008-05-25 23:49:44 -07001843 phydev->dev_flags, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001844 if (IS_ERR(phydev)) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001845 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001846 return PTR_ERR(phydev);
1847 }
1848
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001849 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001850 switch (phydev->interface) {
1851 case PHY_INTERFACE_MODE_GMII:
1852 case PHY_INTERFACE_MODE_RGMII:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001853 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Matt Carlson321d32a2008-11-21 17:22:19 -08001854 phydev->supported &= (PHY_GBIT_FEATURES |
1855 SUPPORTED_Pause |
1856 SUPPORTED_Asym_Pause);
1857 break;
1858 }
1859 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001860 case PHY_INTERFACE_MODE_MII:
1861 phydev->supported &= (PHY_BASIC_FEATURES |
1862 SUPPORTED_Pause |
1863 SUPPORTED_Asym_Pause);
1864 break;
1865 default:
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001866 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001867 return -EINVAL;
1868 }
1869
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001870 tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001871
1872 phydev->advertising = phydev->supported;
1873
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001874 return 0;
1875}
1876
1877static void tg3_phy_start(struct tg3 *tp)
1878{
1879 struct phy_device *phydev;
1880
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001881 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001882 return;
1883
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001884 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001885
Matt Carlson80096062010-08-02 11:26:06 +00001886 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
1887 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001888 phydev->speed = tp->link_config.orig_speed;
1889 phydev->duplex = tp->link_config.orig_duplex;
1890 phydev->autoneg = tp->link_config.orig_autoneg;
1891 phydev->advertising = tp->link_config.orig_advertising;
1892 }
1893
1894 phy_start(phydev);
1895
1896 phy_start_aneg(phydev);
1897}
1898
1899static void tg3_phy_stop(struct tg3 *tp)
1900{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001901 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001902 return;
1903
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001904 phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001905}
1906
1907static void tg3_phy_fini(struct tg3 *tp)
1908{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001909 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001910 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001911 tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001912 }
1913}
1914
Matt Carlson941ec902011-08-19 13:58:23 +00001915static int tg3_phy_set_extloopbk(struct tg3 *tp)
1916{
1917 int err;
1918 u32 val;
1919
1920 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
1921 return 0;
1922
1923 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
1924 /* Cannot do read-modify-write on 5401 */
1925 err = tg3_phy_auxctl_write(tp,
1926 MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
1927 MII_TG3_AUXCTL_ACTL_EXTLOOPBK |
1928 0x4c20);
1929 goto done;
1930 }
1931
1932 err = tg3_phy_auxctl_read(tp,
1933 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
1934 if (err)
1935 return err;
1936
1937 val |= MII_TG3_AUXCTL_ACTL_EXTLOOPBK;
1938 err = tg3_phy_auxctl_write(tp,
1939 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, val);
1940
1941done:
1942 return err;
1943}
1944
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001945static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
1946{
1947 u32 phytest;
1948
1949 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
1950 u32 phy;
1951
1952 tg3_writephy(tp, MII_TG3_FET_TEST,
1953 phytest | MII_TG3_FET_SHADOW_EN);
1954 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
1955 if (enable)
1956 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
1957 else
1958 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
1959 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
1960 }
1961 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
1962 }
1963}
1964
Matt Carlson6833c042008-11-21 17:18:59 -08001965static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
1966{
1967 u32 reg;
1968
Joe Perches63c3a662011-04-26 08:12:10 +00001969 if (!tg3_flag(tp, 5705_PLUS) ||
1970 (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001971 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08001972 return;
1973
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001974 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001975 tg3_phy_fet_toggle_apd(tp, enable);
1976 return;
1977 }
1978
Matt Carlson6833c042008-11-21 17:18:59 -08001979 reg = MII_TG3_MISC_SHDW_WREN |
1980 MII_TG3_MISC_SHDW_SCR5_SEL |
1981 MII_TG3_MISC_SHDW_SCR5_LPED |
1982 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
1983 MII_TG3_MISC_SHDW_SCR5_SDTL |
1984 MII_TG3_MISC_SHDW_SCR5_C125OE;
1985 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 || !enable)
1986 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
1987
1988 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1989
1990
1991 reg = MII_TG3_MISC_SHDW_WREN |
1992 MII_TG3_MISC_SHDW_APD_SEL |
1993 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
1994 if (enable)
1995 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
1996
1997 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1998}
1999
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002000static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
2001{
2002 u32 phy;
2003
Joe Perches63c3a662011-04-26 08:12:10 +00002004 if (!tg3_flag(tp, 5705_PLUS) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002005 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002006 return;
2007
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002008 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002009 u32 ephy;
2010
Matt Carlson535ef6e2009-08-25 10:09:36 +00002011 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
2012 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
2013
2014 tg3_writephy(tp, MII_TG3_FET_TEST,
2015 ephy | MII_TG3_FET_SHADOW_EN);
2016 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002017 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00002018 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002019 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00002020 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
2021 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002022 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00002023 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002024 }
2025 } else {
Matt Carlson15ee95c2011-04-20 07:57:40 +00002026 int ret;
2027
2028 ret = tg3_phy_auxctl_read(tp,
2029 MII_TG3_AUXCTL_SHDWSEL_MISC, &phy);
2030 if (!ret) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002031 if (enable)
2032 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
2033 else
2034 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002035 tg3_phy_auxctl_write(tp,
2036 MII_TG3_AUXCTL_SHDWSEL_MISC, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002037 }
2038 }
2039}
2040
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041static void tg3_phy_set_wirespeed(struct tg3 *tp)
2042{
Matt Carlson15ee95c2011-04-20 07:57:40 +00002043 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 u32 val;
2045
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002046 if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 return;
2048
Matt Carlson15ee95c2011-04-20 07:57:40 +00002049 ret = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_MISC, &val);
2050 if (!ret)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002051 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_MISC,
2052 val | MII_TG3_AUXCTL_MISC_WIRESPD_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053}
2054
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002055static void tg3_phy_apply_otp(struct tg3 *tp)
2056{
2057 u32 otp, phy;
2058
2059 if (!tp->phy_otp)
2060 return;
2061
2062 otp = tp->phy_otp;
2063
Matt Carlson1d36ba42011-04-20 07:57:42 +00002064 if (TG3_PHY_AUXCTL_SMDSP_ENABLE(tp))
2065 return;
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002066
2067 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
2068 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
2069 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
2070
2071 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
2072 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
2073 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
2074
2075 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
2076 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
2077 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
2078
2079 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
2080 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
2081
2082 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
2083 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
2084
2085 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
2086 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
2087 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
2088
Matt Carlson1d36ba42011-04-20 07:57:42 +00002089 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002090}
2091
Matt Carlson52b02d02010-10-14 10:37:41 +00002092static void tg3_phy_eee_adjust(struct tg3 *tp, u32 current_link_up)
2093{
2094 u32 val;
2095
2096 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
2097 return;
2098
2099 tp->setlpicnt = 0;
2100
2101 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
2102 current_link_up == 1 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +00002103 tp->link_config.active_duplex == DUPLEX_FULL &&
2104 (tp->link_config.active_speed == SPEED_100 ||
2105 tp->link_config.active_speed == SPEED_1000)) {
Matt Carlson52b02d02010-10-14 10:37:41 +00002106 u32 eeectl;
2107
2108 if (tp->link_config.active_speed == SPEED_1000)
2109 eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
2110 else
2111 eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
2112
2113 tw32(TG3_CPMU_EEE_CTRL, eeectl);
2114
Matt Carlson3110f5f52010-12-06 08:28:50 +00002115 tg3_phy_cl45_read(tp, MDIO_MMD_AN,
2116 TG3_CL45_D7_EEERES_STAT, &val);
Matt Carlson52b02d02010-10-14 10:37:41 +00002117
Matt Carlsonb0c59432011-05-19 12:12:48 +00002118 if (val == TG3_CL45_D7_EEERES_STAT_LP_1000T ||
2119 val == TG3_CL45_D7_EEERES_STAT_LP_100TX)
Matt Carlson52b02d02010-10-14 10:37:41 +00002120 tp->setlpicnt = 2;
2121 }
2122
2123 if (!tp->setlpicnt) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002124 if (current_link_up == 1 &&
2125 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2126 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0000);
2127 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2128 }
2129
Matt Carlson52b02d02010-10-14 10:37:41 +00002130 val = tr32(TG3_CPMU_EEE_MODE);
2131 tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
2132 }
2133}
2134
Matt Carlsonb0c59432011-05-19 12:12:48 +00002135static void tg3_phy_eee_enable(struct tg3 *tp)
2136{
2137 u32 val;
2138
2139 if (tp->link_config.active_speed == SPEED_1000 &&
2140 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2141 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00002142 tg3_flag(tp, 57765_CLASS)) &&
Matt Carlsonb0c59432011-05-19 12:12:48 +00002143 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002144 val = MII_TG3_DSP_TAP26_ALNOKO |
2145 MII_TG3_DSP_TAP26_RMRXSTO;
2146 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
Matt Carlsonb0c59432011-05-19 12:12:48 +00002147 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2148 }
2149
2150 val = tr32(TG3_CPMU_EEE_MODE);
2151 tw32(TG3_CPMU_EEE_MODE, val | TG3_CPMU_EEEMD_LPI_ENABLE);
2152}
2153
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154static int tg3_wait_macro_done(struct tg3 *tp)
2155{
2156 int limit = 100;
2157
2158 while (limit--) {
2159 u32 tmp32;
2160
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002161 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 if ((tmp32 & 0x1000) == 0)
2163 break;
2164 }
2165 }
Roel Kluind4675b52009-02-12 16:33:27 -08002166 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 return -EBUSY;
2168
2169 return 0;
2170}
2171
2172static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
2173{
2174 static const u32 test_pat[4][6] = {
2175 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
2176 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
2177 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
2178 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
2179 };
2180 int chan;
2181
2182 for (chan = 0; chan < 4; chan++) {
2183 int i;
2184
2185 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2186 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002187 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
2189 for (i = 0; i < 6; i++)
2190 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
2191 test_pat[chan][i]);
2192
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002193 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 if (tg3_wait_macro_done(tp)) {
2195 *resetp = 1;
2196 return -EBUSY;
2197 }
2198
2199 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2200 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002201 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 if (tg3_wait_macro_done(tp)) {
2203 *resetp = 1;
2204 return -EBUSY;
2205 }
2206
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002207 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 if (tg3_wait_macro_done(tp)) {
2209 *resetp = 1;
2210 return -EBUSY;
2211 }
2212
2213 for (i = 0; i < 6; i += 2) {
2214 u32 low, high;
2215
2216 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
2217 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
2218 tg3_wait_macro_done(tp)) {
2219 *resetp = 1;
2220 return -EBUSY;
2221 }
2222 low &= 0x7fff;
2223 high &= 0x000f;
2224 if (low != test_pat[chan][i] ||
2225 high != test_pat[chan][i+1]) {
2226 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
2227 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
2228 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
2229
2230 return -EBUSY;
2231 }
2232 }
2233 }
2234
2235 return 0;
2236}
2237
2238static int tg3_phy_reset_chanpat(struct tg3 *tp)
2239{
2240 int chan;
2241
2242 for (chan = 0; chan < 4; chan++) {
2243 int i;
2244
2245 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2246 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002247 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 for (i = 0; i < 6; i++)
2249 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002250 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 if (tg3_wait_macro_done(tp))
2252 return -EBUSY;
2253 }
2254
2255 return 0;
2256}
2257
2258static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
2259{
2260 u32 reg32, phy9_orig;
2261 int retries, do_phy_reset, err;
2262
2263 retries = 10;
2264 do_phy_reset = 1;
2265 do {
2266 if (do_phy_reset) {
2267 err = tg3_bmcr_reset(tp);
2268 if (err)
2269 return err;
2270 do_phy_reset = 0;
2271 }
2272
2273 /* Disable transmitter and interrupt. */
2274 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
2275 continue;
2276
2277 reg32 |= 0x3000;
2278 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2279
2280 /* Set full-duplex, 1000 mbps. */
2281 tg3_writephy(tp, MII_BMCR,
Matt Carlson221c5632011-06-13 13:39:01 +00002282 BMCR_FULLDPLX | BMCR_SPEED1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
2284 /* Set to master mode. */
Matt Carlson221c5632011-06-13 13:39:01 +00002285 if (tg3_readphy(tp, MII_CTRL1000, &phy9_orig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 continue;
2287
Matt Carlson221c5632011-06-13 13:39:01 +00002288 tg3_writephy(tp, MII_CTRL1000,
2289 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
Matt Carlson1d36ba42011-04-20 07:57:42 +00002291 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
2292 if (err)
2293 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294
2295 /* Block the PHY control access. */
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002296 tg3_phydsp_write(tp, 0x8005, 0x0800);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
2298 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
2299 if (!err)
2300 break;
2301 } while (--retries);
2302
2303 err = tg3_phy_reset_chanpat(tp);
2304 if (err)
2305 return err;
2306
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002307 tg3_phydsp_write(tp, 0x8005, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
2309 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002310 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
Matt Carlson1d36ba42011-04-20 07:57:42 +00002312 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
Matt Carlson221c5632011-06-13 13:39:01 +00002314 tg3_writephy(tp, MII_CTRL1000, phy9_orig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
2316 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
2317 reg32 &= ~0x3000;
2318 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2319 } else if (!err)
2320 err = -EBUSY;
2321
2322 return err;
2323}
2324
2325/* This will reset the tigon3 PHY if there is no valid
2326 * link unless the FORCE argument is non-zero.
2327 */
2328static int tg3_phy_reset(struct tg3 *tp)
2329{
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002330 u32 val, cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 int err;
2332
Michael Chan60189dd2006-12-17 17:08:07 -08002333 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002334 val = tr32(GRC_MISC_CFG);
2335 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
2336 udelay(40);
2337 }
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002338 err = tg3_readphy(tp, MII_BMSR, &val);
2339 err |= tg3_readphy(tp, MII_BMSR, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 if (err != 0)
2341 return -EBUSY;
2342
Michael Chanc8e1e822006-04-29 18:55:17 -07002343 if (netif_running(tp->dev) && netif_carrier_ok(tp->dev)) {
2344 netif_carrier_off(tp->dev);
2345 tg3_link_report(tp);
2346 }
2347
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2349 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2350 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
2351 err = tg3_phy_reset_5703_4_5(tp);
2352 if (err)
2353 return err;
2354 goto out;
2355 }
2356
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002357 cpmuctrl = 0;
2358 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
2359 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
2360 cpmuctrl = tr32(TG3_CPMU_CTRL);
2361 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
2362 tw32(TG3_CPMU_CTRL,
2363 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
2364 }
2365
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 err = tg3_bmcr_reset(tp);
2367 if (err)
2368 return err;
2369
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002370 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002371 val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
2372 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002373
2374 tw32(TG3_CPMU_CTRL, cpmuctrl);
2375 }
2376
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002377 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2378 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002379 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2380 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
2381 CPMU_LSPD_1000MB_MACCLK_12_5) {
2382 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2383 udelay(40);
2384 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2385 }
2386 }
2387
Joe Perches63c3a662011-04-26 08:12:10 +00002388 if (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002389 (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
Matt Carlsonecf14102010-01-20 16:58:05 +00002390 return 0;
2391
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002392 tg3_phy_apply_otp(tp);
2393
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002394 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -08002395 tg3_phy_toggle_apd(tp, true);
2396 else
2397 tg3_phy_toggle_apd(tp, false);
2398
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399out:
Matt Carlson1d36ba42011-04-20 07:57:42 +00002400 if ((tp->phy_flags & TG3_PHYFLG_ADC_BUG) &&
2401 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002402 tg3_phydsp_write(tp, 0x201f, 0x2aaa);
2403 tg3_phydsp_write(tp, 0x000a, 0x0323);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002404 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002406
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002407 if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002408 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2409 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002411
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002412 if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002413 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2414 tg3_phydsp_write(tp, 0x000a, 0x310b);
2415 tg3_phydsp_write(tp, 0x201f, 0x9506);
2416 tg3_phydsp_write(tp, 0x401f, 0x14e2);
2417 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2418 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002419 } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002420 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2421 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
2422 if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
2423 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2424 tg3_writephy(tp, MII_TG3_TEST1,
2425 MII_TG3_TEST1_TRIM_EN | 0x4);
2426 } else
2427 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
2428
2429 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2430 }
Michael Chanc424cb22006-04-29 18:56:34 -07002431 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002432
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 /* Set Extended packet length bit (bit 14) on all chips that */
2434 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002435 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 /* Cannot do read-modify-write on 5401 */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002437 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Joe Perches63c3a662011-04-26 08:12:10 +00002438 } else if (tg3_flag(tp, JUMBO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 /* Set bit 14 with read-modify-write to preserve other bits */
Matt Carlson15ee95c2011-04-20 07:57:40 +00002440 err = tg3_phy_auxctl_read(tp,
2441 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2442 if (!err)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002443 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2444 val | MII_TG3_AUXCTL_ACTL_EXTPKTLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 }
2446
2447 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2448 * jumbo frames transmission.
2449 */
Joe Perches63c3a662011-04-26 08:12:10 +00002450 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002451 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002452 tg3_writephy(tp, MII_TG3_EXT_CTRL,
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002453 val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 }
2455
Michael Chan715116a2006-09-27 16:09:25 -07002456 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002457 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002458 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002459 }
2460
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002461 tg3_phy_toggle_automdix(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 tg3_phy_set_wirespeed(tp);
2463 return 0;
2464}
2465
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002466#define TG3_GPIO_MSG_DRVR_PRES 0x00000001
2467#define TG3_GPIO_MSG_NEED_VAUX 0x00000002
2468#define TG3_GPIO_MSG_MASK (TG3_GPIO_MSG_DRVR_PRES | \
2469 TG3_GPIO_MSG_NEED_VAUX)
2470#define TG3_GPIO_MSG_ALL_DRVR_PRES_MASK \
2471 ((TG3_GPIO_MSG_DRVR_PRES << 0) | \
2472 (TG3_GPIO_MSG_DRVR_PRES << 4) | \
2473 (TG3_GPIO_MSG_DRVR_PRES << 8) | \
2474 (TG3_GPIO_MSG_DRVR_PRES << 12))
2475
2476#define TG3_GPIO_MSG_ALL_NEED_VAUX_MASK \
2477 ((TG3_GPIO_MSG_NEED_VAUX << 0) | \
2478 (TG3_GPIO_MSG_NEED_VAUX << 4) | \
2479 (TG3_GPIO_MSG_NEED_VAUX << 8) | \
2480 (TG3_GPIO_MSG_NEED_VAUX << 12))
2481
2482static inline u32 tg3_set_function_status(struct tg3 *tp, u32 newstat)
2483{
2484 u32 status, shift;
2485
2486 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2487 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
2488 status = tg3_ape_read32(tp, TG3_APE_GPIO_MSG);
2489 else
2490 status = tr32(TG3_CPMU_DRV_STATUS);
2491
2492 shift = TG3_APE_GPIO_MSG_SHIFT + 4 * tp->pci_fn;
2493 status &= ~(TG3_GPIO_MSG_MASK << shift);
2494 status |= (newstat << shift);
2495
2496 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2497 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
2498 tg3_ape_write32(tp, TG3_APE_GPIO_MSG, status);
2499 else
2500 tw32(TG3_CPMU_DRV_STATUS, status);
2501
2502 return status >> TG3_APE_GPIO_MSG_SHIFT;
2503}
2504
Matt Carlson520b2752011-06-13 13:39:02 +00002505static inline int tg3_pwrsrc_switch_to_vmain(struct tg3 *tp)
2506{
2507 if (!tg3_flag(tp, IS_NIC))
2508 return 0;
2509
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002510 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2511 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
2512 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
2513 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2514 return -EIO;
Matt Carlson520b2752011-06-13 13:39:02 +00002515
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002516 tg3_set_function_status(tp, TG3_GPIO_MSG_DRVR_PRES);
2517
2518 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2519 TG3_GRC_LCLCTL_PWRSW_DELAY);
2520
2521 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
2522 } else {
2523 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2524 TG3_GRC_LCLCTL_PWRSW_DELAY);
2525 }
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002526
Matt Carlson520b2752011-06-13 13:39:02 +00002527 return 0;
2528}
2529
2530static void tg3_pwrsrc_die_with_vmain(struct tg3 *tp)
2531{
2532 u32 grc_local_ctrl;
2533
2534 if (!tg3_flag(tp, IS_NIC) ||
2535 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2536 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)
2537 return;
2538
2539 grc_local_ctrl = tp->grc_local_ctrl | GRC_LCLCTRL_GPIO_OE1;
2540
2541 tw32_wait_f(GRC_LOCAL_CTRL,
2542 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2543 TG3_GRC_LCLCTL_PWRSW_DELAY);
2544
2545 tw32_wait_f(GRC_LOCAL_CTRL,
2546 grc_local_ctrl,
2547 TG3_GRC_LCLCTL_PWRSW_DELAY);
2548
2549 tw32_wait_f(GRC_LOCAL_CTRL,
2550 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2551 TG3_GRC_LCLCTL_PWRSW_DELAY);
2552}
2553
2554static void tg3_pwrsrc_switch_to_vaux(struct tg3 *tp)
2555{
2556 if (!tg3_flag(tp, IS_NIC))
2557 return;
2558
2559 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2560 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2561 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2562 (GRC_LCLCTRL_GPIO_OE0 |
2563 GRC_LCLCTRL_GPIO_OE1 |
2564 GRC_LCLCTRL_GPIO_OE2 |
2565 GRC_LCLCTRL_GPIO_OUTPUT0 |
2566 GRC_LCLCTRL_GPIO_OUTPUT1),
2567 TG3_GRC_LCLCTL_PWRSW_DELAY);
2568 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2569 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
2570 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2571 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2572 GRC_LCLCTRL_GPIO_OE1 |
2573 GRC_LCLCTRL_GPIO_OE2 |
2574 GRC_LCLCTRL_GPIO_OUTPUT0 |
2575 GRC_LCLCTRL_GPIO_OUTPUT1 |
2576 tp->grc_local_ctrl;
2577 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2578 TG3_GRC_LCLCTL_PWRSW_DELAY);
2579
2580 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2581 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2582 TG3_GRC_LCLCTL_PWRSW_DELAY);
2583
2584 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2585 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2586 TG3_GRC_LCLCTL_PWRSW_DELAY);
2587 } else {
2588 u32 no_gpio2;
2589 u32 grc_local_ctrl = 0;
2590
2591 /* Workaround to prevent overdrawing Amps. */
2592 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
2593 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
2594 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2595 grc_local_ctrl,
2596 TG3_GRC_LCLCTL_PWRSW_DELAY);
2597 }
2598
2599 /* On 5753 and variants, GPIO2 cannot be used. */
2600 no_gpio2 = tp->nic_sram_data_cfg &
2601 NIC_SRAM_DATA_CFG_NO_GPIO2;
2602
2603 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
2604 GRC_LCLCTRL_GPIO_OE1 |
2605 GRC_LCLCTRL_GPIO_OE2 |
2606 GRC_LCLCTRL_GPIO_OUTPUT1 |
2607 GRC_LCLCTRL_GPIO_OUTPUT2;
2608 if (no_gpio2) {
2609 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2610 GRC_LCLCTRL_GPIO_OUTPUT2);
2611 }
2612 tw32_wait_f(GRC_LOCAL_CTRL,
2613 tp->grc_local_ctrl | grc_local_ctrl,
2614 TG3_GRC_LCLCTL_PWRSW_DELAY);
2615
2616 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2617
2618 tw32_wait_f(GRC_LOCAL_CTRL,
2619 tp->grc_local_ctrl | grc_local_ctrl,
2620 TG3_GRC_LCLCTL_PWRSW_DELAY);
2621
2622 if (!no_gpio2) {
2623 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
2624 tw32_wait_f(GRC_LOCAL_CTRL,
2625 tp->grc_local_ctrl | grc_local_ctrl,
2626 TG3_GRC_LCLCTL_PWRSW_DELAY);
2627 }
2628 }
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002629}
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002630
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002631static void tg3_frob_aux_power_5717(struct tg3 *tp, bool wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002632{
2633 u32 msg = 0;
2634
2635 /* Serialize power state transitions */
2636 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2637 return;
2638
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002639 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE) || wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002640 msg = TG3_GPIO_MSG_NEED_VAUX;
2641
2642 msg = tg3_set_function_status(tp, msg);
2643
2644 if (msg & TG3_GPIO_MSG_ALL_DRVR_PRES_MASK)
2645 goto done;
2646
2647 if (msg & TG3_GPIO_MSG_ALL_NEED_VAUX_MASK)
2648 tg3_pwrsrc_switch_to_vaux(tp);
2649 else
2650 tg3_pwrsrc_die_with_vmain(tp);
2651
2652done:
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002653 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
Matt Carlson520b2752011-06-13 13:39:02 +00002654}
2655
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002656static void tg3_frob_aux_power(struct tg3 *tp, bool include_wol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657{
Matt Carlson683644b2011-03-09 16:58:23 +00002658 bool need_vaux = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659
Matt Carlson334355a2010-01-20 16:58:10 +00002660 /* The GPIOs do something completely different on 57765. */
Matt Carlson55086ad2011-12-14 11:09:59 +00002661 if (!tg3_flag(tp, IS_NIC) || tg3_flag(tp, 57765_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 return;
2663
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002664 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2665 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
2666 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002667 tg3_frob_aux_power_5717(tp, include_wol ?
2668 tg3_flag(tp, WOL_ENABLE) != 0 : 0);
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002669 return;
2670 }
2671
2672 if (tp->pdev_peer && tp->pdev_peer != tp->pdev) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002673 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002675 dev_peer = pci_get_drvdata(tp->pdev_peer);
Matt Carlson683644b2011-03-09 16:58:23 +00002676
Michael Chanbc1c7562006-03-20 17:48:03 -08002677 /* remove_one() may have been run on the peer. */
Matt Carlson683644b2011-03-09 16:58:23 +00002678 if (dev_peer) {
2679 struct tg3 *tp_peer = netdev_priv(dev_peer);
2680
Joe Perches63c3a662011-04-26 08:12:10 +00002681 if (tg3_flag(tp_peer, INIT_COMPLETE))
Matt Carlson683644b2011-03-09 16:58:23 +00002682 return;
2683
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002684 if ((include_wol && tg3_flag(tp_peer, WOL_ENABLE)) ||
Joe Perches63c3a662011-04-26 08:12:10 +00002685 tg3_flag(tp_peer, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002686 need_vaux = true;
2687 }
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002690 if ((include_wol && tg3_flag(tp, WOL_ENABLE)) ||
2691 tg3_flag(tp, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002692 need_vaux = true;
2693
Matt Carlson520b2752011-06-13 13:39:02 +00002694 if (need_vaux)
2695 tg3_pwrsrc_switch_to_vaux(tp);
2696 else
2697 tg3_pwrsrc_die_with_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698}
2699
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002700static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2701{
2702 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2703 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00002704 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002705 if (speed != SPEED_10)
2706 return 1;
2707 } else if (speed == SPEED_10)
2708 return 1;
2709
2710 return 0;
2711}
2712
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713static int tg3_setup_phy(struct tg3 *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714static int tg3_halt_cpu(struct tg3 *, u32);
2715
Matt Carlson0a459aa2008-11-03 16:54:15 -08002716static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002717{
Matt Carlsonce057f02007-11-12 21:08:03 -08002718 u32 val;
2719
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002720 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Michael Chan51297242007-02-13 12:17:57 -08002721 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2722 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2723 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2724
2725 sg_dig_ctrl |=
2726 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2727 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2728 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2729 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002730 return;
Michael Chan51297242007-02-13 12:17:57 -08002731 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002732
Michael Chan60189dd2006-12-17 17:08:07 -08002733 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002734 tg3_bmcr_reset(tp);
2735 val = tr32(GRC_MISC_CFG);
2736 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2737 udelay(40);
2738 return;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002739 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson0e5f7842009-11-02 14:26:38 +00002740 u32 phytest;
2741 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2742 u32 phy;
2743
2744 tg3_writephy(tp, MII_ADVERTISE, 0);
2745 tg3_writephy(tp, MII_BMCR,
2746 BMCR_ANENABLE | BMCR_ANRESTART);
2747
2748 tg3_writephy(tp, MII_TG3_FET_TEST,
2749 phytest | MII_TG3_FET_SHADOW_EN);
2750 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
2751 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
2752 tg3_writephy(tp,
2753 MII_TG3_FET_SHDW_AUXMODE4,
2754 phy);
2755 }
2756 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2757 }
2758 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002759 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07002760 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2761 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002762
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002763 val = MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2764 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2765 MII_TG3_AUXCTL_PCTL_VREG_11V;
2766 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, val);
Michael Chan715116a2006-09-27 16:09:25 -07002767 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002768
Michael Chan15c3b692006-03-22 01:06:52 -08002769 /* The PHY should not be powered down on some chips because
2770 * of bugs.
2771 */
2772 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2773 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2774 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002775 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Michael Chan15c3b692006-03-22 01:06:52 -08002776 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08002777
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002778 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2779 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002780 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2781 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2782 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2783 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2784 }
2785
Michael Chan15c3b692006-03-22 01:06:52 -08002786 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2787}
2788
Matt Carlson3f007892008-11-03 16:51:36 -08002789/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002790static int tg3_nvram_lock(struct tg3 *tp)
2791{
Joe Perches63c3a662011-04-26 08:12:10 +00002792 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002793 int i;
2794
2795 if (tp->nvram_lock_cnt == 0) {
2796 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2797 for (i = 0; i < 8000; i++) {
2798 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2799 break;
2800 udelay(20);
2801 }
2802 if (i == 8000) {
2803 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2804 return -ENODEV;
2805 }
2806 }
2807 tp->nvram_lock_cnt++;
2808 }
2809 return 0;
2810}
2811
2812/* tp->lock is held. */
2813static void tg3_nvram_unlock(struct tg3 *tp)
2814{
Joe Perches63c3a662011-04-26 08:12:10 +00002815 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002816 if (tp->nvram_lock_cnt > 0)
2817 tp->nvram_lock_cnt--;
2818 if (tp->nvram_lock_cnt == 0)
2819 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
2820 }
2821}
2822
2823/* tp->lock is held. */
2824static void tg3_enable_nvram_access(struct tg3 *tp)
2825{
Joe Perches63c3a662011-04-26 08:12:10 +00002826 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002827 u32 nvaccess = tr32(NVRAM_ACCESS);
2828
2829 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
2830 }
2831}
2832
2833/* tp->lock is held. */
2834static void tg3_disable_nvram_access(struct tg3 *tp)
2835{
Joe Perches63c3a662011-04-26 08:12:10 +00002836 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002837 u32 nvaccess = tr32(NVRAM_ACCESS);
2838
2839 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
2840 }
2841}
2842
2843static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
2844 u32 offset, u32 *val)
2845{
2846 u32 tmp;
2847 int i;
2848
2849 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
2850 return -EINVAL;
2851
2852 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
2853 EEPROM_ADDR_DEVID_MASK |
2854 EEPROM_ADDR_READ);
2855 tw32(GRC_EEPROM_ADDR,
2856 tmp |
2857 (0 << EEPROM_ADDR_DEVID_SHIFT) |
2858 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
2859 EEPROM_ADDR_ADDR_MASK) |
2860 EEPROM_ADDR_READ | EEPROM_ADDR_START);
2861
2862 for (i = 0; i < 1000; i++) {
2863 tmp = tr32(GRC_EEPROM_ADDR);
2864
2865 if (tmp & EEPROM_ADDR_COMPLETE)
2866 break;
2867 msleep(1);
2868 }
2869 if (!(tmp & EEPROM_ADDR_COMPLETE))
2870 return -EBUSY;
2871
Matt Carlson62cedd12009-04-20 14:52:29 -07002872 tmp = tr32(GRC_EEPROM_DATA);
2873
2874 /*
2875 * The data will always be opposite the native endian
2876 * format. Perform a blind byteswap to compensate.
2877 */
2878 *val = swab32(tmp);
2879
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002880 return 0;
2881}
2882
2883#define NVRAM_CMD_TIMEOUT 10000
2884
2885static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
2886{
2887 int i;
2888
2889 tw32(NVRAM_CMD, nvram_cmd);
2890 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
2891 udelay(10);
2892 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
2893 udelay(10);
2894 break;
2895 }
2896 }
2897
2898 if (i == NVRAM_CMD_TIMEOUT)
2899 return -EBUSY;
2900
2901 return 0;
2902}
2903
2904static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
2905{
Joe Perches63c3a662011-04-26 08:12:10 +00002906 if (tg3_flag(tp, NVRAM) &&
2907 tg3_flag(tp, NVRAM_BUFFERED) &&
2908 tg3_flag(tp, FLASH) &&
2909 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002910 (tp->nvram_jedecnum == JEDEC_ATMEL))
2911
2912 addr = ((addr / tp->nvram_pagesize) <<
2913 ATMEL_AT45DB0X1B_PAGE_POS) +
2914 (addr % tp->nvram_pagesize);
2915
2916 return addr;
2917}
2918
2919static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
2920{
Joe Perches63c3a662011-04-26 08:12:10 +00002921 if (tg3_flag(tp, NVRAM) &&
2922 tg3_flag(tp, NVRAM_BUFFERED) &&
2923 tg3_flag(tp, FLASH) &&
2924 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002925 (tp->nvram_jedecnum == JEDEC_ATMEL))
2926
2927 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
2928 tp->nvram_pagesize) +
2929 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
2930
2931 return addr;
2932}
2933
Matt Carlsone4f34112009-02-25 14:25:00 +00002934/* NOTE: Data read in from NVRAM is byteswapped according to
2935 * the byteswapping settings for all other register accesses.
2936 * tg3 devices are BE devices, so on a BE machine, the data
2937 * returned will be exactly as it is seen in NVRAM. On a LE
2938 * machine, the 32-bit value will be byteswapped.
2939 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002940static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
2941{
2942 int ret;
2943
Joe Perches63c3a662011-04-26 08:12:10 +00002944 if (!tg3_flag(tp, NVRAM))
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002945 return tg3_nvram_read_using_eeprom(tp, offset, val);
2946
2947 offset = tg3_nvram_phys_addr(tp, offset);
2948
2949 if (offset > NVRAM_ADDR_MSK)
2950 return -EINVAL;
2951
2952 ret = tg3_nvram_lock(tp);
2953 if (ret)
2954 return ret;
2955
2956 tg3_enable_nvram_access(tp);
2957
2958 tw32(NVRAM_ADDR, offset);
2959 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
2960 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
2961
2962 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00002963 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002964
2965 tg3_disable_nvram_access(tp);
2966
2967 tg3_nvram_unlock(tp);
2968
2969 return ret;
2970}
2971
Matt Carlsona9dc5292009-02-25 14:25:30 +00002972/* Ensures NVRAM data is in bytestream format. */
2973static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002974{
2975 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00002976 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002977 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00002978 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002979 return res;
2980}
2981
Matt Carlson997b4f12011-08-31 11:44:53 +00002982#define RX_CPU_SCRATCH_BASE 0x30000
2983#define RX_CPU_SCRATCH_SIZE 0x04000
2984#define TX_CPU_SCRATCH_BASE 0x34000
2985#define TX_CPU_SCRATCH_SIZE 0x04000
2986
2987/* tp->lock is held. */
2988static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
2989{
2990 int i;
2991
2992 BUG_ON(offset == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS));
2993
2994 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
2995 u32 val = tr32(GRC_VCPU_EXT_CTRL);
2996
2997 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
2998 return 0;
2999 }
3000 if (offset == RX_CPU_BASE) {
3001 for (i = 0; i < 10000; i++) {
3002 tw32(offset + CPU_STATE, 0xffffffff);
3003 tw32(offset + CPU_MODE, CPU_MODE_HALT);
3004 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
3005 break;
3006 }
3007
3008 tw32(offset + CPU_STATE, 0xffffffff);
3009 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
3010 udelay(10);
3011 } else {
3012 for (i = 0; i < 10000; i++) {
3013 tw32(offset + CPU_STATE, 0xffffffff);
3014 tw32(offset + CPU_MODE, CPU_MODE_HALT);
3015 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
3016 break;
3017 }
3018 }
3019
3020 if (i >= 10000) {
3021 netdev_err(tp->dev, "%s timed out, %s CPU\n",
3022 __func__, offset == RX_CPU_BASE ? "RX" : "TX");
3023 return -ENODEV;
3024 }
3025
3026 /* Clear firmware's nvram arbitration. */
3027 if (tg3_flag(tp, NVRAM))
3028 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
3029 return 0;
3030}
3031
3032struct fw_info {
3033 unsigned int fw_base;
3034 unsigned int fw_len;
3035 const __be32 *fw_data;
3036};
3037
3038/* tp->lock is held. */
3039static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base,
3040 u32 cpu_scratch_base, int cpu_scratch_size,
3041 struct fw_info *info)
3042{
3043 int err, lock_err, i;
3044 void (*write_op)(struct tg3 *, u32, u32);
3045
3046 if (cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS)) {
3047 netdev_err(tp->dev,
3048 "%s: Trying to load TX cpu firmware which is 5705\n",
3049 __func__);
3050 return -EINVAL;
3051 }
3052
3053 if (tg3_flag(tp, 5705_PLUS))
3054 write_op = tg3_write_mem;
3055 else
3056 write_op = tg3_write_indirect_reg32;
3057
3058 /* It is possible that bootcode is still loading at this point.
3059 * Get the nvram lock first before halting the cpu.
3060 */
3061 lock_err = tg3_nvram_lock(tp);
3062 err = tg3_halt_cpu(tp, cpu_base);
3063 if (!lock_err)
3064 tg3_nvram_unlock(tp);
3065 if (err)
3066 goto out;
3067
3068 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
3069 write_op(tp, cpu_scratch_base + i, 0);
3070 tw32(cpu_base + CPU_STATE, 0xffffffff);
3071 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
3072 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
3073 write_op(tp, (cpu_scratch_base +
3074 (info->fw_base & 0xffff) +
3075 (i * sizeof(u32))),
3076 be32_to_cpu(info->fw_data[i]));
3077
3078 err = 0;
3079
3080out:
3081 return err;
3082}
3083
3084/* tp->lock is held. */
3085static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
3086{
3087 struct fw_info info;
3088 const __be32 *fw_data;
3089 int err, i;
3090
3091 fw_data = (void *)tp->fw->data;
3092
3093 /* Firmware blob starts with version numbers, followed by
3094 start address and length. We are setting complete length.
3095 length = end_address_of_bss - start_address_of_text.
3096 Remainder is the blob to be loaded contiguously
3097 from start address. */
3098
3099 info.fw_base = be32_to_cpu(fw_data[1]);
3100 info.fw_len = tp->fw->size - 12;
3101 info.fw_data = &fw_data[3];
3102
3103 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
3104 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
3105 &info);
3106 if (err)
3107 return err;
3108
3109 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
3110 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
3111 &info);
3112 if (err)
3113 return err;
3114
3115 /* Now startup only the RX cpu. */
3116 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3117 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
3118
3119 for (i = 0; i < 5; i++) {
3120 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
3121 break;
3122 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3123 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
3124 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
3125 udelay(1000);
3126 }
3127 if (i >= 5) {
3128 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
3129 "should be %08x\n", __func__,
3130 tr32(RX_CPU_BASE + CPU_PC), info.fw_base);
3131 return -ENODEV;
3132 }
3133 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3134 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
3135
3136 return 0;
3137}
3138
3139/* tp->lock is held. */
3140static int tg3_load_tso_firmware(struct tg3 *tp)
3141{
3142 struct fw_info info;
3143 const __be32 *fw_data;
3144 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
3145 int err, i;
3146
3147 if (tg3_flag(tp, HW_TSO_1) ||
3148 tg3_flag(tp, HW_TSO_2) ||
3149 tg3_flag(tp, HW_TSO_3))
3150 return 0;
3151
3152 fw_data = (void *)tp->fw->data;
3153
3154 /* Firmware blob starts with version numbers, followed by
3155 start address and length. We are setting complete length.
3156 length = end_address_of_bss - start_address_of_text.
3157 Remainder is the blob to be loaded contiguously
3158 from start address. */
3159
3160 info.fw_base = be32_to_cpu(fw_data[1]);
3161 cpu_scratch_size = tp->fw_len;
3162 info.fw_len = tp->fw->size - 12;
3163 info.fw_data = &fw_data[3];
3164
3165 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
3166 cpu_base = RX_CPU_BASE;
3167 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
3168 } else {
3169 cpu_base = TX_CPU_BASE;
3170 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
3171 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
3172 }
3173
3174 err = tg3_load_firmware_cpu(tp, cpu_base,
3175 cpu_scratch_base, cpu_scratch_size,
3176 &info);
3177 if (err)
3178 return err;
3179
3180 /* Now startup the cpu. */
3181 tw32(cpu_base + CPU_STATE, 0xffffffff);
3182 tw32_f(cpu_base + CPU_PC, info.fw_base);
3183
3184 for (i = 0; i < 5; i++) {
3185 if (tr32(cpu_base + CPU_PC) == info.fw_base)
3186 break;
3187 tw32(cpu_base + CPU_STATE, 0xffffffff);
3188 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
3189 tw32_f(cpu_base + CPU_PC, info.fw_base);
3190 udelay(1000);
3191 }
3192 if (i >= 5) {
3193 netdev_err(tp->dev,
3194 "%s fails to set CPU PC, is %08x should be %08x\n",
3195 __func__, tr32(cpu_base + CPU_PC), info.fw_base);
3196 return -ENODEV;
3197 }
3198 tw32(cpu_base + CPU_STATE, 0xffffffff);
3199 tw32_f(cpu_base + CPU_MODE, 0x00000000);
3200 return 0;
3201}
3202
3203
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003204/* tp->lock is held. */
Matt Carlson3f007892008-11-03 16:51:36 -08003205static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
3206{
3207 u32 addr_high, addr_low;
3208 int i;
3209
3210 addr_high = ((tp->dev->dev_addr[0] << 8) |
3211 tp->dev->dev_addr[1]);
3212 addr_low = ((tp->dev->dev_addr[2] << 24) |
3213 (tp->dev->dev_addr[3] << 16) |
3214 (tp->dev->dev_addr[4] << 8) |
3215 (tp->dev->dev_addr[5] << 0));
3216 for (i = 0; i < 4; i++) {
3217 if (i == 1 && skip_mac_1)
3218 continue;
3219 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
3220 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
3221 }
3222
3223 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3224 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
3225 for (i = 0; i < 12; i++) {
3226 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
3227 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
3228 }
3229 }
3230
3231 addr_high = (tp->dev->dev_addr[0] +
3232 tp->dev->dev_addr[1] +
3233 tp->dev->dev_addr[2] +
3234 tp->dev->dev_addr[3] +
3235 tp->dev->dev_addr[4] +
3236 tp->dev->dev_addr[5]) &
3237 TX_BACKOFF_SEED_MASK;
3238 tw32(MAC_TX_BACKOFF_SEED, addr_high);
3239}
3240
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003241static void tg3_enable_register_access(struct tg3 *tp)
3242{
3243 /*
3244 * Make sure register accesses (indirect or otherwise) will function
3245 * correctly.
3246 */
3247 pci_write_config_dword(tp->pdev,
3248 TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
3249}
3250
3251static int tg3_power_up(struct tg3 *tp)
3252{
Matt Carlsonbed98292011-07-13 09:27:29 +00003253 int err;
3254
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003255 tg3_enable_register_access(tp);
3256
Matt Carlsonbed98292011-07-13 09:27:29 +00003257 err = pci_set_power_state(tp->pdev, PCI_D0);
3258 if (!err) {
3259 /* Switch out of Vaux if it is a NIC */
3260 tg3_pwrsrc_switch_to_vmain(tp);
3261 } else {
3262 netdev_err(tp->dev, "Transition to D0 failed\n");
3263 }
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003264
Matt Carlsonbed98292011-07-13 09:27:29 +00003265 return err;
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003266}
3267
3268static int tg3_power_down_prepare(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269{
3270 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003271 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003273 tg3_enable_register_access(tp);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003274
3275 /* Restore the CLKREQ setting. */
Joe Perches63c3a662011-04-26 08:12:10 +00003276 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003277 u16 lnkctl;
3278
3279 pci_read_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +00003280 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003281 &lnkctl);
3282 lnkctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
3283 pci_write_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +00003284 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003285 lnkctl);
3286 }
3287
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
3289 tw32(TG3PCI_MISC_HOST_CTRL,
3290 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
3291
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003292 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00003293 tg3_flag(tp, WOL_ENABLE);
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003294
Joe Perches63c3a662011-04-26 08:12:10 +00003295 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08003296 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003297 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson80096062010-08-02 11:26:06 +00003298 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003299 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003300 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003301
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00003302 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003303
Matt Carlson80096062010-08-02 11:26:06 +00003304 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003305
3306 tp->link_config.orig_speed = phydev->speed;
3307 tp->link_config.orig_duplex = phydev->duplex;
3308 tp->link_config.orig_autoneg = phydev->autoneg;
3309 tp->link_config.orig_advertising = phydev->advertising;
3310
3311 advertising = ADVERTISED_TP |
3312 ADVERTISED_Pause |
3313 ADVERTISED_Autoneg |
3314 ADVERTISED_10baseT_Half;
3315
Joe Perches63c3a662011-04-26 08:12:10 +00003316 if (tg3_flag(tp, ENABLE_ASF) || device_should_wake) {
3317 if (tg3_flag(tp, WOL_SPEED_100MB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003318 advertising |=
3319 ADVERTISED_100baseT_Half |
3320 ADVERTISED_100baseT_Full |
3321 ADVERTISED_10baseT_Full;
3322 else
3323 advertising |= ADVERTISED_10baseT_Full;
3324 }
3325
3326 phydev->advertising = advertising;
3327
3328 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08003329
3330 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00003331 if (phyid != PHY_ID_BCMAC131) {
3332 phyid &= PHY_BCM_OUI_MASK;
3333 if (phyid == PHY_BCM_OUI_1 ||
3334 phyid == PHY_BCM_OUI_2 ||
3335 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08003336 do_low_power = true;
3337 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003338 }
Matt Carlsondd477002008-05-25 23:45:58 -07003339 } else {
Matt Carlson20232762008-12-21 20:18:56 -08003340 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003341
Matt Carlson80096062010-08-02 11:26:06 +00003342 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
3343 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07003344 tp->link_config.orig_speed = tp->link_config.speed;
3345 tp->link_config.orig_duplex = tp->link_config.duplex;
3346 tp->link_config.orig_autoneg = tp->link_config.autoneg;
3347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003349 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Matt Carlsondd477002008-05-25 23:45:58 -07003350 tp->link_config.speed = SPEED_10;
3351 tp->link_config.duplex = DUPLEX_HALF;
3352 tp->link_config.autoneg = AUTONEG_ENABLE;
3353 tg3_setup_phy(tp, 0);
3354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 }
3356
Michael Chanb5d37722006-09-27 16:06:21 -07003357 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
3358 u32 val;
3359
3360 val = tr32(GRC_VCPU_EXT_CTRL);
3361 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
Joe Perches63c3a662011-04-26 08:12:10 +00003362 } else if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08003363 int i;
3364 u32 val;
3365
3366 for (i = 0; i < 200; i++) {
3367 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
3368 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
3369 break;
3370 msleep(1);
3371 }
3372 }
Joe Perches63c3a662011-04-26 08:12:10 +00003373 if (tg3_flag(tp, WOL_CAP))
Gary Zambranoa85feb82007-05-05 11:52:19 -07003374 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
3375 WOL_DRV_STATE_SHUTDOWN |
3376 WOL_DRV_WOL |
3377 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08003378
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003379 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 u32 mac_mode;
3381
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003382 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003383 if (do_low_power &&
3384 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
3385 tg3_phy_auxctl_write(tp,
3386 MII_TG3_AUXCTL_SHDWSEL_PWRCTL,
3387 MII_TG3_AUXCTL_PCTL_WOL_EN |
3388 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
3389 MII_TG3_AUXCTL_PCTL_CL_AB_TXDAC);
Matt Carlsondd477002008-05-25 23:45:58 -07003390 udelay(40);
3391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003393 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07003394 mac_mode = MAC_MODE_PORT_MODE_GMII;
3395 else
3396 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003398 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
3399 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
3400 ASIC_REV_5700) {
Joe Perches63c3a662011-04-26 08:12:10 +00003401 u32 speed = tg3_flag(tp, WOL_SPEED_100MB) ?
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003402 SPEED_100 : SPEED_10;
3403 if (tg3_5700_link_polarity(tp, speed))
3404 mac_mode |= MAC_MODE_LINK_POLARITY;
3405 else
3406 mac_mode &= ~MAC_MODE_LINK_POLARITY;
3407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 } else {
3409 mac_mode = MAC_MODE_PORT_MODE_TBI;
3410 }
3411
Joe Perches63c3a662011-04-26 08:12:10 +00003412 if (!tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 tw32(MAC_LED_CTRL, tp->led_ctrl);
3414
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003415 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00003416 if ((tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS)) &&
3417 (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)))
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003418 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419
Joe Perches63c3a662011-04-26 08:12:10 +00003420 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00003421 mac_mode |= MAC_MODE_APE_TX_EN |
3422 MAC_MODE_APE_RX_EN |
3423 MAC_MODE_TDE_ENABLE;
Matt Carlson3bda1252008-08-15 14:08:22 -07003424
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 tw32_f(MAC_MODE, mac_mode);
3426 udelay(100);
3427
3428 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
3429 udelay(10);
3430 }
3431
Joe Perches63c3a662011-04-26 08:12:10 +00003432 if (!tg3_flag(tp, WOL_SPEED_100MB) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3434 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
3435 u32 base_val;
3436
3437 base_val = tp->pci_clock_ctrl;
3438 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
3439 CLOCK_CTRL_TXCLK_DISABLE);
3440
Michael Chanb401e9e2005-12-19 16:27:04 -08003441 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
3442 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Joe Perches63c3a662011-04-26 08:12:10 +00003443 } else if (tg3_flag(tp, 5780_CLASS) ||
3444 tg3_flag(tp, CPMU_PRESENT) ||
Matt Carlson6ff6f812011-05-19 12:12:54 +00003445 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan4cf78e42005-07-25 12:29:19 -07003446 /* do nothing */
Joe Perches63c3a662011-04-26 08:12:10 +00003447 } else if (!(tg3_flag(tp, 5750_PLUS) && tg3_flag(tp, ENABLE_ASF))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 u32 newbits1, newbits2;
3449
3450 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3451 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3452 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
3453 CLOCK_CTRL_TXCLK_DISABLE |
3454 CLOCK_CTRL_ALTCLK);
3455 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
Joe Perches63c3a662011-04-26 08:12:10 +00003456 } else if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 newbits1 = CLOCK_CTRL_625_CORE;
3458 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
3459 } else {
3460 newbits1 = CLOCK_CTRL_ALTCLK;
3461 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
3462 }
3463
Michael Chanb401e9e2005-12-19 16:27:04 -08003464 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
3465 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466
Michael Chanb401e9e2005-12-19 16:27:04 -08003467 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
3468 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469
Joe Perches63c3a662011-04-26 08:12:10 +00003470 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 u32 newbits3;
3472
3473 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3474 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3475 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
3476 CLOCK_CTRL_TXCLK_DISABLE |
3477 CLOCK_CTRL_44MHZ_CORE);
3478 } else {
3479 newbits3 = CLOCK_CTRL_44MHZ_CORE;
3480 }
3481
Michael Chanb401e9e2005-12-19 16:27:04 -08003482 tw32_wait_f(TG3PCI_CLOCK_CTRL,
3483 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 }
3485 }
3486
Joe Perches63c3a662011-04-26 08:12:10 +00003487 if (!(device_should_wake) && !tg3_flag(tp, ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08003488 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08003489
Matt Carlsoncd0d7222011-07-13 09:27:33 +00003490 tg3_frob_aux_power(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491
3492 /* Workaround for unstable PLL clock */
3493 if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
3494 (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
3495 u32 val = tr32(0x7d00);
3496
3497 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
3498 tw32(0x7d00, val);
Joe Perches63c3a662011-04-26 08:12:10 +00003499 if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08003500 int err;
3501
3502 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08003504 if (!err)
3505 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08003506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 }
3508
Michael Chanbbadf502006-04-06 21:46:34 -07003509 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
3510
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 return 0;
3512}
3513
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003514static void tg3_power_down(struct tg3 *tp)
3515{
3516 tg3_power_down_prepare(tp);
3517
Joe Perches63c3a662011-04-26 08:12:10 +00003518 pci_wake_from_d3(tp->pdev, tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003519 pci_set_power_state(tp->pdev, PCI_D3hot);
3520}
3521
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
3523{
3524 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
3525 case MII_TG3_AUX_STAT_10HALF:
3526 *speed = SPEED_10;
3527 *duplex = DUPLEX_HALF;
3528 break;
3529
3530 case MII_TG3_AUX_STAT_10FULL:
3531 *speed = SPEED_10;
3532 *duplex = DUPLEX_FULL;
3533 break;
3534
3535 case MII_TG3_AUX_STAT_100HALF:
3536 *speed = SPEED_100;
3537 *duplex = DUPLEX_HALF;
3538 break;
3539
3540 case MII_TG3_AUX_STAT_100FULL:
3541 *speed = SPEED_100;
3542 *duplex = DUPLEX_FULL;
3543 break;
3544
3545 case MII_TG3_AUX_STAT_1000HALF:
3546 *speed = SPEED_1000;
3547 *duplex = DUPLEX_HALF;
3548 break;
3549
3550 case MII_TG3_AUX_STAT_1000FULL:
3551 *speed = SPEED_1000;
3552 *duplex = DUPLEX_FULL;
3553 break;
3554
3555 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003556 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07003557 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
3558 SPEED_10;
3559 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
3560 DUPLEX_HALF;
3561 break;
3562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 *speed = SPEED_INVALID;
3564 *duplex = DUPLEX_INVALID;
3565 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003566 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567}
3568
Matt Carlson42b64a42011-05-19 12:12:49 +00003569static int tg3_phy_autoneg_cfg(struct tg3 *tp, u32 advertise, u32 flowctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570{
Matt Carlson42b64a42011-05-19 12:12:49 +00003571 int err = 0;
3572 u32 val, new_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573
Matt Carlson42b64a42011-05-19 12:12:49 +00003574 new_adv = ADVERTISE_CSMA;
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +00003575 new_adv |= ethtool_adv_to_mii_adv_t(advertise) & ADVERTISE_ALL;
Matt Carlsonf88788f2011-12-14 11:10:00 +00003576 new_adv |= mii_advertise_flowctrl(flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577
Matt Carlson42b64a42011-05-19 12:12:49 +00003578 err = tg3_writephy(tp, MII_ADVERTISE, new_adv);
3579 if (err)
3580 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581
Matt Carlson4f272092011-12-14 11:09:57 +00003582 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
3583 new_adv = ethtool_adv_to_mii_ctrl1000_t(advertise);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003584
Matt Carlson4f272092011-12-14 11:09:57 +00003585 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3586 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
3587 new_adv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003588
Matt Carlson4f272092011-12-14 11:09:57 +00003589 err = tg3_writephy(tp, MII_CTRL1000, new_adv);
3590 if (err)
3591 goto done;
3592 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003593
Matt Carlson42b64a42011-05-19 12:12:49 +00003594 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
3595 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596
Matt Carlson42b64a42011-05-19 12:12:49 +00003597 tw32(TG3_CPMU_EEE_MODE,
3598 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003599
Matt Carlson42b64a42011-05-19 12:12:49 +00003600 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
3601 if (!err) {
3602 u32 err2;
Matt Carlson52b02d02010-10-14 10:37:41 +00003603
Matt Carlsona6b68da2010-12-06 08:28:52 +00003604 val = 0;
Matt Carlson42b64a42011-05-19 12:12:49 +00003605 /* Advertise 100-BaseTX EEE ability */
3606 if (advertise & ADVERTISED_100baseT_Full)
3607 val |= MDIO_AN_EEE_ADV_100TX;
3608 /* Advertise 1000-BaseT EEE ability */
3609 if (advertise & ADVERTISED_1000baseT_Full)
3610 val |= MDIO_AN_EEE_ADV_1000T;
3611 err = tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlsonb715ce92011-07-20 10:20:52 +00003612 if (err)
3613 val = 0;
3614
3615 switch (GET_ASIC_REV(tp->pci_chip_rev_id)) {
3616 case ASIC_REV_5717:
3617 case ASIC_REV_57765:
Matt Carlson55086ad2011-12-14 11:09:59 +00003618 case ASIC_REV_57766:
Matt Carlsonb715ce92011-07-20 10:20:52 +00003619 case ASIC_REV_5719:
3620 /* If we advertised any eee advertisements above... */
3621 if (val)
3622 val = MII_TG3_DSP_TAP26_ALNOKO |
3623 MII_TG3_DSP_TAP26_RMRXSTO |
3624 MII_TG3_DSP_TAP26_OPCSINPT;
3625 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
3626 /* Fall through */
3627 case ASIC_REV_5720:
3628 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
3629 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
3630 MII_TG3_DSP_CH34TP2_HIBW01);
3631 }
Matt Carlson52b02d02010-10-14 10:37:41 +00003632
Matt Carlson42b64a42011-05-19 12:12:49 +00003633 err2 = TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
3634 if (!err)
3635 err = err2;
3636 }
3637
3638done:
3639 return err;
3640}
3641
3642static void tg3_phy_copper_begin(struct tg3 *tp)
3643{
3644 u32 new_adv;
3645 int i;
3646
3647 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
3648 new_adv = ADVERTISED_10baseT_Half |
3649 ADVERTISED_10baseT_Full;
3650 if (tg3_flag(tp, WOL_SPEED_100MB))
3651 new_adv |= ADVERTISED_100baseT_Half |
3652 ADVERTISED_100baseT_Full;
3653
3654 tg3_phy_autoneg_cfg(tp, new_adv,
3655 FLOW_CTRL_TX | FLOW_CTRL_RX);
3656 } else if (tp->link_config.speed == SPEED_INVALID) {
3657 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
3658 tp->link_config.advertising &=
3659 ~(ADVERTISED_1000baseT_Half |
3660 ADVERTISED_1000baseT_Full);
3661
3662 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
3663 tp->link_config.flowctrl);
3664 } else {
3665 /* Asking for a specific link mode. */
3666 if (tp->link_config.speed == SPEED_1000) {
3667 if (tp->link_config.duplex == DUPLEX_FULL)
3668 new_adv = ADVERTISED_1000baseT_Full;
3669 else
3670 new_adv = ADVERTISED_1000baseT_Half;
3671 } else if (tp->link_config.speed == SPEED_100) {
3672 if (tp->link_config.duplex == DUPLEX_FULL)
3673 new_adv = ADVERTISED_100baseT_Full;
3674 else
3675 new_adv = ADVERTISED_100baseT_Half;
3676 } else {
3677 if (tp->link_config.duplex == DUPLEX_FULL)
3678 new_adv = ADVERTISED_10baseT_Full;
3679 else
3680 new_adv = ADVERTISED_10baseT_Half;
3681 }
3682
3683 tg3_phy_autoneg_cfg(tp, new_adv,
3684 tp->link_config.flowctrl);
Matt Carlson52b02d02010-10-14 10:37:41 +00003685 }
3686
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 if (tp->link_config.autoneg == AUTONEG_DISABLE &&
3688 tp->link_config.speed != SPEED_INVALID) {
3689 u32 bmcr, orig_bmcr;
3690
3691 tp->link_config.active_speed = tp->link_config.speed;
3692 tp->link_config.active_duplex = tp->link_config.duplex;
3693
3694 bmcr = 0;
3695 switch (tp->link_config.speed) {
3696 default:
3697 case SPEED_10:
3698 break;
3699
3700 case SPEED_100:
3701 bmcr |= BMCR_SPEED100;
3702 break;
3703
3704 case SPEED_1000:
Matt Carlson221c5632011-06-13 13:39:01 +00003705 bmcr |= BMCR_SPEED1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708
3709 if (tp->link_config.duplex == DUPLEX_FULL)
3710 bmcr |= BMCR_FULLDPLX;
3711
3712 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
3713 (bmcr != orig_bmcr)) {
3714 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
3715 for (i = 0; i < 1500; i++) {
3716 u32 tmp;
3717
3718 udelay(10);
3719 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
3720 tg3_readphy(tp, MII_BMSR, &tmp))
3721 continue;
3722 if (!(tmp & BMSR_LSTATUS)) {
3723 udelay(40);
3724 break;
3725 }
3726 }
3727 tg3_writephy(tp, MII_BMCR, bmcr);
3728 udelay(40);
3729 }
3730 } else {
3731 tg3_writephy(tp, MII_BMCR,
3732 BMCR_ANENABLE | BMCR_ANRESTART);
3733 }
3734}
3735
3736static int tg3_init_5401phy_dsp(struct tg3 *tp)
3737{
3738 int err;
3739
3740 /* Turn off tap power management. */
3741 /* Set Extended packet length bit */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003742 err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00003744 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
3745 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
3746 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
3747 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
3748 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749
3750 udelay(40);
3751
3752 return err;
3753}
3754
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003755static bool tg3_phy_copper_an_config_ok(struct tg3 *tp, u32 *lcladv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756{
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003757 u32 advmsk, tgtadv, advertising;
Michael Chan3600d912006-12-07 00:21:48 -08003758
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003759 advertising = tp->link_config.advertising;
3760 tgtadv = ethtool_adv_to_mii_adv_t(advertising) & ADVERTISE_ALL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003762 advmsk = ADVERTISE_ALL;
3763 if (tp->link_config.active_duplex == DUPLEX_FULL) {
Matt Carlsonf88788f2011-12-14 11:10:00 +00003764 tgtadv |= mii_advertise_flowctrl(tp->link_config.flowctrl);
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003765 advmsk |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
3766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003768 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
3769 return false;
3770
3771 if ((*lcladv & advmsk) != tgtadv)
3772 return false;
Matt Carlsonb99d2a52011-08-31 11:44:47 +00003773
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003774 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 u32 tg3_ctrl;
3776
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003777 tgtadv = ethtool_adv_to_mii_ctrl1000_t(advertising);
Michael Chan3600d912006-12-07 00:21:48 -08003778
Matt Carlson221c5632011-06-13 13:39:01 +00003779 if (tg3_readphy(tp, MII_CTRL1000, &tg3_ctrl))
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003780 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781
Matt Carlsonb99d2a52011-08-31 11:44:47 +00003782 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL);
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003783 if (tg3_ctrl != tgtadv)
3784 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 }
Matt Carlson93a700a2011-08-31 11:44:54 +00003786
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003787 return true;
Matt Carlsonef167e22007-12-20 20:10:01 -08003788}
3789
Matt Carlson859edb22011-12-08 14:40:16 +00003790static bool tg3_phy_copper_fetch_rmtadv(struct tg3 *tp, u32 *rmtadv)
3791{
3792 u32 lpeth = 0;
3793
3794 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
3795 u32 val;
3796
3797 if (tg3_readphy(tp, MII_STAT1000, &val))
3798 return false;
3799
3800 lpeth = mii_stat1000_to_ethtool_lpa_t(val);
3801 }
3802
3803 if (tg3_readphy(tp, MII_LPA, rmtadv))
3804 return false;
3805
3806 lpeth |= mii_lpa_to_ethtool_lpa_t(*rmtadv);
3807 tp->link_config.rmt_adv = lpeth;
3808
3809 return true;
3810}
3811
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
3813{
3814 int current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003815 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08003816 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817 u16 current_speed;
3818 u8 current_duplex;
3819 int i, err;
3820
3821 tw32(MAC_EVENT, 0);
3822
3823 tw32_f(MAC_STATUS,
3824 (MAC_STATUS_SYNC_CHANGED |
3825 MAC_STATUS_CFG_CHANGED |
3826 MAC_STATUS_MI_COMPLETION |
3827 MAC_STATUS_LNKSTATE_CHANGED));
3828 udelay(40);
3829
Matt Carlson8ef21422008-05-02 16:47:53 -07003830 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
3831 tw32_f(MAC_MI_MODE,
3832 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
3833 udelay(80);
3834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003836 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837
3838 /* Some third-party PHYs need to be reset on link going
3839 * down.
3840 */
3841 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3842 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
3843 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
3844 netif_carrier_ok(tp->dev)) {
3845 tg3_readphy(tp, MII_BMSR, &bmsr);
3846 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3847 !(bmsr & BMSR_LSTATUS))
3848 force_reset = 1;
3849 }
3850 if (force_reset)
3851 tg3_phy_reset(tp);
3852
Matt Carlson79eb6902010-02-17 15:17:03 +00003853 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854 tg3_readphy(tp, MII_BMSR, &bmsr);
3855 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
Joe Perches63c3a662011-04-26 08:12:10 +00003856 !tg3_flag(tp, INIT_COMPLETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 bmsr = 0;
3858
3859 if (!(bmsr & BMSR_LSTATUS)) {
3860 err = tg3_init_5401phy_dsp(tp);
3861 if (err)
3862 return err;
3863
3864 tg3_readphy(tp, MII_BMSR, &bmsr);
3865 for (i = 0; i < 1000; i++) {
3866 udelay(10);
3867 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3868 (bmsr & BMSR_LSTATUS)) {
3869 udelay(40);
3870 break;
3871 }
3872 }
3873
Matt Carlson79eb6902010-02-17 15:17:03 +00003874 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
3875 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 !(bmsr & BMSR_LSTATUS) &&
3877 tp->link_config.active_speed == SPEED_1000) {
3878 err = tg3_phy_reset(tp);
3879 if (!err)
3880 err = tg3_init_5401phy_dsp(tp);
3881 if (err)
3882 return err;
3883 }
3884 }
3885 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3886 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
3887 /* 5701 {A0,B0} CRC bug workaround */
3888 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00003889 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
3890 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
3891 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 }
3893
3894 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003895 tg3_readphy(tp, MII_TG3_ISTAT, &val);
3896 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003898 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003900 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 tg3_writephy(tp, MII_TG3_IMASK, ~0);
3902
3903 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3904 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3905 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
3906 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3907 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
3908 else
3909 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
3910 }
3911
3912 current_link_up = 0;
3913 current_speed = SPEED_INVALID;
3914 current_duplex = DUPLEX_INVALID;
Matt Carlsone348c5e2011-11-21 15:01:20 +00003915 tp->phy_flags &= ~TG3_PHYFLG_MDIX_STATE;
Matt Carlson859edb22011-12-08 14:40:16 +00003916 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003918 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Matt Carlson15ee95c2011-04-20 07:57:40 +00003919 err = tg3_phy_auxctl_read(tp,
3920 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
3921 &val);
3922 if (!err && !(val & (1 << 10))) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003923 tg3_phy_auxctl_write(tp,
3924 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
3925 val | (1 << 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 goto relink;
3927 }
3928 }
3929
3930 bmsr = 0;
3931 for (i = 0; i < 100; i++) {
3932 tg3_readphy(tp, MII_BMSR, &bmsr);
3933 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3934 (bmsr & BMSR_LSTATUS))
3935 break;
3936 udelay(40);
3937 }
3938
3939 if (bmsr & BMSR_LSTATUS) {
3940 u32 aux_stat, bmcr;
3941
3942 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
3943 for (i = 0; i < 2000; i++) {
3944 udelay(10);
3945 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
3946 aux_stat)
3947 break;
3948 }
3949
3950 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
3951 &current_speed,
3952 &current_duplex);
3953
3954 bmcr = 0;
3955 for (i = 0; i < 200; i++) {
3956 tg3_readphy(tp, MII_BMCR, &bmcr);
3957 if (tg3_readphy(tp, MII_BMCR, &bmcr))
3958 continue;
3959 if (bmcr && bmcr != 0x7fff)
3960 break;
3961 udelay(10);
3962 }
3963
Matt Carlsonef167e22007-12-20 20:10:01 -08003964 lcl_adv = 0;
3965 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966
Matt Carlsonef167e22007-12-20 20:10:01 -08003967 tp->link_config.active_speed = current_speed;
3968 tp->link_config.active_duplex = current_duplex;
3969
3970 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3971 if ((bmcr & BMCR_ANENABLE) &&
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003972 tg3_phy_copper_an_config_ok(tp, &lcl_adv) &&
Matt Carlson859edb22011-12-08 14:40:16 +00003973 tg3_phy_copper_fetch_rmtadv(tp, &rmt_adv))
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003974 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 } else {
3976 if (!(bmcr & BMCR_ANENABLE) &&
3977 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08003978 tp->link_config.duplex == current_duplex &&
3979 tp->link_config.flowctrl ==
3980 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 }
3983 }
3984
Matt Carlsonef167e22007-12-20 20:10:01 -08003985 if (current_link_up == 1 &&
Matt Carlsone348c5e2011-11-21 15:01:20 +00003986 tp->link_config.active_duplex == DUPLEX_FULL) {
3987 u32 reg, bit;
3988
3989 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
3990 reg = MII_TG3_FET_GEN_STAT;
3991 bit = MII_TG3_FET_GEN_STAT_MDIXSTAT;
3992 } else {
3993 reg = MII_TG3_EXT_STAT;
3994 bit = MII_TG3_EXT_STAT_MDIX;
3995 }
3996
3997 if (!tg3_readphy(tp, reg, &val) && (val & bit))
3998 tp->phy_flags |= TG3_PHYFLG_MDIX_STATE;
3999
Matt Carlsonef167e22007-12-20 20:10:01 -08004000 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Matt Carlsone348c5e2011-11-21 15:01:20 +00004001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 }
4003
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004relink:
Matt Carlson80096062010-08-02 11:26:06 +00004005 if (current_link_up == 0 || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 tg3_phy_copper_begin(tp);
4007
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004008 tg3_readphy(tp, MII_BMSR, &bmsr);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00004009 if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
4010 (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011 current_link_up = 1;
4012 }
4013
4014 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
4015 if (current_link_up == 1) {
4016 if (tp->link_config.active_speed == SPEED_100 ||
4017 tp->link_config.active_speed == SPEED_10)
4018 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4019 else
4020 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004021 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00004022 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4023 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4025
4026 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4027 if (tp->link_config.active_duplex == DUPLEX_HALF)
4028 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4029
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004031 if (current_link_up == 1 &&
4032 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004034 else
4035 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 }
4037
4038 /* ??? Without this setting Netgear GA302T PHY does not
4039 * ??? send/receive packets...
4040 */
Matt Carlson79eb6902010-02-17 15:17:03 +00004041 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
4043 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
4044 tw32_f(MAC_MI_MODE, tp->mi_mode);
4045 udelay(80);
4046 }
4047
4048 tw32_f(MAC_MODE, tp->mac_mode);
4049 udelay(40);
4050
Matt Carlson52b02d02010-10-14 10:37:41 +00004051 tg3_phy_eee_adjust(tp, current_link_up);
4052
Joe Perches63c3a662011-04-26 08:12:10 +00004053 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 /* Polled via timer. */
4055 tw32_f(MAC_EVENT, 0);
4056 } else {
4057 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4058 }
4059 udelay(40);
4060
4061 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
4062 current_link_up == 1 &&
4063 tp->link_config.active_speed == SPEED_1000 &&
Joe Perches63c3a662011-04-26 08:12:10 +00004064 (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 udelay(120);
4066 tw32_f(MAC_STATUS,
4067 (MAC_STATUS_SYNC_CHANGED |
4068 MAC_STATUS_CFG_CHANGED));
4069 udelay(40);
4070 tg3_write_mem(tp,
4071 NIC_SRAM_FIRMWARE_MBOX,
4072 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
4073 }
4074
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004075 /* Prevent send BD corruption. */
Joe Perches63c3a662011-04-26 08:12:10 +00004076 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004077 u16 oldlnkctl, newlnkctl;
4078
4079 pci_read_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +00004080 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004081 &oldlnkctl);
4082 if (tp->link_config.active_speed == SPEED_100 ||
4083 tp->link_config.active_speed == SPEED_10)
4084 newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
4085 else
4086 newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
4087 if (newlnkctl != oldlnkctl)
4088 pci_write_config_word(tp->pdev,
Matt Carlson93a700a2011-08-31 11:44:54 +00004089 pci_pcie_cap(tp->pdev) +
4090 PCI_EXP_LNKCTL, newlnkctl);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004091 }
4092
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093 if (current_link_up != netif_carrier_ok(tp->dev)) {
4094 if (current_link_up)
4095 netif_carrier_on(tp->dev);
4096 else
4097 netif_carrier_off(tp->dev);
4098 tg3_link_report(tp);
4099 }
4100
4101 return 0;
4102}
4103
4104struct tg3_fiber_aneginfo {
4105 int state;
4106#define ANEG_STATE_UNKNOWN 0
4107#define ANEG_STATE_AN_ENABLE 1
4108#define ANEG_STATE_RESTART_INIT 2
4109#define ANEG_STATE_RESTART 3
4110#define ANEG_STATE_DISABLE_LINK_OK 4
4111#define ANEG_STATE_ABILITY_DETECT_INIT 5
4112#define ANEG_STATE_ABILITY_DETECT 6
4113#define ANEG_STATE_ACK_DETECT_INIT 7
4114#define ANEG_STATE_ACK_DETECT 8
4115#define ANEG_STATE_COMPLETE_ACK_INIT 9
4116#define ANEG_STATE_COMPLETE_ACK 10
4117#define ANEG_STATE_IDLE_DETECT_INIT 11
4118#define ANEG_STATE_IDLE_DETECT 12
4119#define ANEG_STATE_LINK_OK 13
4120#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
4121#define ANEG_STATE_NEXT_PAGE_WAIT 15
4122
4123 u32 flags;
4124#define MR_AN_ENABLE 0x00000001
4125#define MR_RESTART_AN 0x00000002
4126#define MR_AN_COMPLETE 0x00000004
4127#define MR_PAGE_RX 0x00000008
4128#define MR_NP_LOADED 0x00000010
4129#define MR_TOGGLE_TX 0x00000020
4130#define MR_LP_ADV_FULL_DUPLEX 0x00000040
4131#define MR_LP_ADV_HALF_DUPLEX 0x00000080
4132#define MR_LP_ADV_SYM_PAUSE 0x00000100
4133#define MR_LP_ADV_ASYM_PAUSE 0x00000200
4134#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
4135#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
4136#define MR_LP_ADV_NEXT_PAGE 0x00001000
4137#define MR_TOGGLE_RX 0x00002000
4138#define MR_NP_RX 0x00004000
4139
4140#define MR_LINK_OK 0x80000000
4141
4142 unsigned long link_time, cur_time;
4143
4144 u32 ability_match_cfg;
4145 int ability_match_count;
4146
4147 char ability_match, idle_match, ack_match;
4148
4149 u32 txconfig, rxconfig;
4150#define ANEG_CFG_NP 0x00000080
4151#define ANEG_CFG_ACK 0x00000040
4152#define ANEG_CFG_RF2 0x00000020
4153#define ANEG_CFG_RF1 0x00000010
4154#define ANEG_CFG_PS2 0x00000001
4155#define ANEG_CFG_PS1 0x00008000
4156#define ANEG_CFG_HD 0x00004000
4157#define ANEG_CFG_FD 0x00002000
4158#define ANEG_CFG_INVAL 0x00001f06
4159
4160};
4161#define ANEG_OK 0
4162#define ANEG_DONE 1
4163#define ANEG_TIMER_ENAB 2
4164#define ANEG_FAILED -1
4165
4166#define ANEG_STATE_SETTLE_TIME 10000
4167
4168static int tg3_fiber_aneg_smachine(struct tg3 *tp,
4169 struct tg3_fiber_aneginfo *ap)
4170{
Matt Carlson5be73b42007-12-20 20:09:29 -08004171 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 unsigned long delta;
4173 u32 rx_cfg_reg;
4174 int ret;
4175
4176 if (ap->state == ANEG_STATE_UNKNOWN) {
4177 ap->rxconfig = 0;
4178 ap->link_time = 0;
4179 ap->cur_time = 0;
4180 ap->ability_match_cfg = 0;
4181 ap->ability_match_count = 0;
4182 ap->ability_match = 0;
4183 ap->idle_match = 0;
4184 ap->ack_match = 0;
4185 }
4186 ap->cur_time++;
4187
4188 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
4189 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
4190
4191 if (rx_cfg_reg != ap->ability_match_cfg) {
4192 ap->ability_match_cfg = rx_cfg_reg;
4193 ap->ability_match = 0;
4194 ap->ability_match_count = 0;
4195 } else {
4196 if (++ap->ability_match_count > 1) {
4197 ap->ability_match = 1;
4198 ap->ability_match_cfg = rx_cfg_reg;
4199 }
4200 }
4201 if (rx_cfg_reg & ANEG_CFG_ACK)
4202 ap->ack_match = 1;
4203 else
4204 ap->ack_match = 0;
4205
4206 ap->idle_match = 0;
4207 } else {
4208 ap->idle_match = 1;
4209 ap->ability_match_cfg = 0;
4210 ap->ability_match_count = 0;
4211 ap->ability_match = 0;
4212 ap->ack_match = 0;
4213
4214 rx_cfg_reg = 0;
4215 }
4216
4217 ap->rxconfig = rx_cfg_reg;
4218 ret = ANEG_OK;
4219
Matt Carlson33f401a2010-04-05 10:19:27 +00004220 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 case ANEG_STATE_UNKNOWN:
4222 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
4223 ap->state = ANEG_STATE_AN_ENABLE;
4224
4225 /* fallthru */
4226 case ANEG_STATE_AN_ENABLE:
4227 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
4228 if (ap->flags & MR_AN_ENABLE) {
4229 ap->link_time = 0;
4230 ap->cur_time = 0;
4231 ap->ability_match_cfg = 0;
4232 ap->ability_match_count = 0;
4233 ap->ability_match = 0;
4234 ap->idle_match = 0;
4235 ap->ack_match = 0;
4236
4237 ap->state = ANEG_STATE_RESTART_INIT;
4238 } else {
4239 ap->state = ANEG_STATE_DISABLE_LINK_OK;
4240 }
4241 break;
4242
4243 case ANEG_STATE_RESTART_INIT:
4244 ap->link_time = ap->cur_time;
4245 ap->flags &= ~(MR_NP_LOADED);
4246 ap->txconfig = 0;
4247 tw32(MAC_TX_AUTO_NEG, 0);
4248 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4249 tw32_f(MAC_MODE, tp->mac_mode);
4250 udelay(40);
4251
4252 ret = ANEG_TIMER_ENAB;
4253 ap->state = ANEG_STATE_RESTART;
4254
4255 /* fallthru */
4256 case ANEG_STATE_RESTART:
4257 delta = ap->cur_time - ap->link_time;
Matt Carlson859a588792010-04-05 10:19:28 +00004258 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a588792010-04-05 10:19:28 +00004260 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 break;
4263
4264 case ANEG_STATE_DISABLE_LINK_OK:
4265 ret = ANEG_DONE;
4266 break;
4267
4268 case ANEG_STATE_ABILITY_DETECT_INIT:
4269 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08004270 ap->txconfig = ANEG_CFG_FD;
4271 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
4272 if (flowctrl & ADVERTISE_1000XPAUSE)
4273 ap->txconfig |= ANEG_CFG_PS1;
4274 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
4275 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
4277 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4278 tw32_f(MAC_MODE, tp->mac_mode);
4279 udelay(40);
4280
4281 ap->state = ANEG_STATE_ABILITY_DETECT;
4282 break;
4283
4284 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a588792010-04-05 10:19:28 +00004285 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 break;
4288
4289 case ANEG_STATE_ACK_DETECT_INIT:
4290 ap->txconfig |= ANEG_CFG_ACK;
4291 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
4292 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4293 tw32_f(MAC_MODE, tp->mac_mode);
4294 udelay(40);
4295
4296 ap->state = ANEG_STATE_ACK_DETECT;
4297
4298 /* fallthru */
4299 case ANEG_STATE_ACK_DETECT:
4300 if (ap->ack_match != 0) {
4301 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
4302 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
4303 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
4304 } else {
4305 ap->state = ANEG_STATE_AN_ENABLE;
4306 }
4307 } else if (ap->ability_match != 0 &&
4308 ap->rxconfig == 0) {
4309 ap->state = ANEG_STATE_AN_ENABLE;
4310 }
4311 break;
4312
4313 case ANEG_STATE_COMPLETE_ACK_INIT:
4314 if (ap->rxconfig & ANEG_CFG_INVAL) {
4315 ret = ANEG_FAILED;
4316 break;
4317 }
4318 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
4319 MR_LP_ADV_HALF_DUPLEX |
4320 MR_LP_ADV_SYM_PAUSE |
4321 MR_LP_ADV_ASYM_PAUSE |
4322 MR_LP_ADV_REMOTE_FAULT1 |
4323 MR_LP_ADV_REMOTE_FAULT2 |
4324 MR_LP_ADV_NEXT_PAGE |
4325 MR_TOGGLE_RX |
4326 MR_NP_RX);
4327 if (ap->rxconfig & ANEG_CFG_FD)
4328 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
4329 if (ap->rxconfig & ANEG_CFG_HD)
4330 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
4331 if (ap->rxconfig & ANEG_CFG_PS1)
4332 ap->flags |= MR_LP_ADV_SYM_PAUSE;
4333 if (ap->rxconfig & ANEG_CFG_PS2)
4334 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
4335 if (ap->rxconfig & ANEG_CFG_RF1)
4336 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
4337 if (ap->rxconfig & ANEG_CFG_RF2)
4338 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
4339 if (ap->rxconfig & ANEG_CFG_NP)
4340 ap->flags |= MR_LP_ADV_NEXT_PAGE;
4341
4342 ap->link_time = ap->cur_time;
4343
4344 ap->flags ^= (MR_TOGGLE_TX);
4345 if (ap->rxconfig & 0x0008)
4346 ap->flags |= MR_TOGGLE_RX;
4347 if (ap->rxconfig & ANEG_CFG_NP)
4348 ap->flags |= MR_NP_RX;
4349 ap->flags |= MR_PAGE_RX;
4350
4351 ap->state = ANEG_STATE_COMPLETE_ACK;
4352 ret = ANEG_TIMER_ENAB;
4353 break;
4354
4355 case ANEG_STATE_COMPLETE_ACK:
4356 if (ap->ability_match != 0 &&
4357 ap->rxconfig == 0) {
4358 ap->state = ANEG_STATE_AN_ENABLE;
4359 break;
4360 }
4361 delta = ap->cur_time - ap->link_time;
4362 if (delta > ANEG_STATE_SETTLE_TIME) {
4363 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
4364 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
4365 } else {
4366 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
4367 !(ap->flags & MR_NP_RX)) {
4368 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
4369 } else {
4370 ret = ANEG_FAILED;
4371 }
4372 }
4373 }
4374 break;
4375
4376 case ANEG_STATE_IDLE_DETECT_INIT:
4377 ap->link_time = ap->cur_time;
4378 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
4379 tw32_f(MAC_MODE, tp->mac_mode);
4380 udelay(40);
4381
4382 ap->state = ANEG_STATE_IDLE_DETECT;
4383 ret = ANEG_TIMER_ENAB;
4384 break;
4385
4386 case ANEG_STATE_IDLE_DETECT:
4387 if (ap->ability_match != 0 &&
4388 ap->rxconfig == 0) {
4389 ap->state = ANEG_STATE_AN_ENABLE;
4390 break;
4391 }
4392 delta = ap->cur_time - ap->link_time;
4393 if (delta > ANEG_STATE_SETTLE_TIME) {
4394 /* XXX another gem from the Broadcom driver :( */
4395 ap->state = ANEG_STATE_LINK_OK;
4396 }
4397 break;
4398
4399 case ANEG_STATE_LINK_OK:
4400 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
4401 ret = ANEG_DONE;
4402 break;
4403
4404 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
4405 /* ??? unimplemented */
4406 break;
4407
4408 case ANEG_STATE_NEXT_PAGE_WAIT:
4409 /* ??? unimplemented */
4410 break;
4411
4412 default:
4413 ret = ANEG_FAILED;
4414 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416
4417 return ret;
4418}
4419
Matt Carlson5be73b42007-12-20 20:09:29 -08004420static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421{
4422 int res = 0;
4423 struct tg3_fiber_aneginfo aninfo;
4424 int status = ANEG_FAILED;
4425 unsigned int tick;
4426 u32 tmp;
4427
4428 tw32_f(MAC_TX_AUTO_NEG, 0);
4429
4430 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
4431 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
4432 udelay(40);
4433
4434 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
4435 udelay(40);
4436
4437 memset(&aninfo, 0, sizeof(aninfo));
4438 aninfo.flags |= MR_AN_ENABLE;
4439 aninfo.state = ANEG_STATE_UNKNOWN;
4440 aninfo.cur_time = 0;
4441 tick = 0;
4442 while (++tick < 195000) {
4443 status = tg3_fiber_aneg_smachine(tp, &aninfo);
4444 if (status == ANEG_DONE || status == ANEG_FAILED)
4445 break;
4446
4447 udelay(1);
4448 }
4449
4450 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
4451 tw32_f(MAC_MODE, tp->mac_mode);
4452 udelay(40);
4453
Matt Carlson5be73b42007-12-20 20:09:29 -08004454 *txflags = aninfo.txconfig;
4455 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456
4457 if (status == ANEG_DONE &&
4458 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
4459 MR_LP_ADV_FULL_DUPLEX)))
4460 res = 1;
4461
4462 return res;
4463}
4464
4465static void tg3_init_bcm8002(struct tg3 *tp)
4466{
4467 u32 mac_status = tr32(MAC_STATUS);
4468 int i;
4469
4470 /* Reset when initting first time or we have a link. */
Joe Perches63c3a662011-04-26 08:12:10 +00004471 if (tg3_flag(tp, INIT_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472 !(mac_status & MAC_STATUS_PCS_SYNCED))
4473 return;
4474
4475 /* Set PLL lock range. */
4476 tg3_writephy(tp, 0x16, 0x8007);
4477
4478 /* SW reset */
4479 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
4480
4481 /* Wait for reset to complete. */
4482 /* XXX schedule_timeout() ... */
4483 for (i = 0; i < 500; i++)
4484 udelay(10);
4485
4486 /* Config mode; select PMA/Ch 1 regs. */
4487 tg3_writephy(tp, 0x10, 0x8411);
4488
4489 /* Enable auto-lock and comdet, select txclk for tx. */
4490 tg3_writephy(tp, 0x11, 0x0a10);
4491
4492 tg3_writephy(tp, 0x18, 0x00a0);
4493 tg3_writephy(tp, 0x16, 0x41ff);
4494
4495 /* Assert and deassert POR. */
4496 tg3_writephy(tp, 0x13, 0x0400);
4497 udelay(40);
4498 tg3_writephy(tp, 0x13, 0x0000);
4499
4500 tg3_writephy(tp, 0x11, 0x0a50);
4501 udelay(40);
4502 tg3_writephy(tp, 0x11, 0x0a10);
4503
4504 /* Wait for signal to stabilize */
4505 /* XXX schedule_timeout() ... */
4506 for (i = 0; i < 15000; i++)
4507 udelay(10);
4508
4509 /* Deselect the channel register so we can read the PHYID
4510 * later.
4511 */
4512 tg3_writephy(tp, 0x10, 0x8011);
4513}
4514
4515static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
4516{
Matt Carlson82cd3d12007-12-20 20:09:00 -08004517 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 u32 sg_dig_ctrl, sg_dig_status;
4519 u32 serdes_cfg, expected_sg_dig_ctrl;
4520 int workaround, port_a;
4521 int current_link_up;
4522
4523 serdes_cfg = 0;
4524 expected_sg_dig_ctrl = 0;
4525 workaround = 0;
4526 port_a = 1;
4527 current_link_up = 0;
4528
4529 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
4530 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
4531 workaround = 1;
4532 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
4533 port_a = 0;
4534
4535 /* preserve bits 0-11,13,14 for signal pre-emphasis */
4536 /* preserve bits 20-23 for voltage regulator */
4537 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
4538 }
4539
4540 sg_dig_ctrl = tr32(SG_DIG_CTRL);
4541
4542 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004543 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544 if (workaround) {
4545 u32 val = serdes_cfg;
4546
4547 if (port_a)
4548 val |= 0xc010000;
4549 else
4550 val |= 0x4010000;
4551 tw32_f(MAC_SERDES_CFG, val);
4552 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004553
4554 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555 }
4556 if (mac_status & MAC_STATUS_PCS_SYNCED) {
4557 tg3_setup_flow_control(tp, 0, 0);
4558 current_link_up = 1;
4559 }
4560 goto out;
4561 }
4562
4563 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004564 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565
Matt Carlson82cd3d12007-12-20 20:09:00 -08004566 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
4567 if (flowctrl & ADVERTISE_1000XPAUSE)
4568 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
4569 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
4570 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571
4572 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004573 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07004574 tp->serdes_counter &&
4575 ((mac_status & (MAC_STATUS_PCS_SYNCED |
4576 MAC_STATUS_RCVD_CFG)) ==
4577 MAC_STATUS_PCS_SYNCED)) {
4578 tp->serdes_counter--;
4579 current_link_up = 1;
4580 goto out;
4581 }
4582restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583 if (workaround)
4584 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004585 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 udelay(5);
4587 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
4588
Michael Chan3d3ebe72006-09-27 15:59:15 -07004589 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004590 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
4592 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07004593 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594 mac_status = tr32(MAC_STATUS);
4595
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004596 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08004598 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599
Matt Carlson82cd3d12007-12-20 20:09:00 -08004600 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
4601 local_adv |= ADVERTISE_1000XPAUSE;
4602 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
4603 local_adv |= ADVERTISE_1000XPSE_ASYM;
4604
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004605 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08004606 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004607 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08004608 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609
Matt Carlson859edb22011-12-08 14:40:16 +00004610 tp->link_config.rmt_adv =
4611 mii_adv_to_ethtool_adv_x(remote_adv);
4612
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 tg3_setup_flow_control(tp, local_adv, remote_adv);
4614 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004615 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004616 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004617 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07004618 if (tp->serdes_counter)
4619 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620 else {
4621 if (workaround) {
4622 u32 val = serdes_cfg;
4623
4624 if (port_a)
4625 val |= 0xc010000;
4626 else
4627 val |= 0x4010000;
4628
4629 tw32_f(MAC_SERDES_CFG, val);
4630 }
4631
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004632 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633 udelay(40);
4634
4635 /* Link parallel detection - link is up */
4636 /* only if we have PCS_SYNC and not */
4637 /* receiving config code words */
4638 mac_status = tr32(MAC_STATUS);
4639 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
4640 !(mac_status & MAC_STATUS_RCVD_CFG)) {
4641 tg3_setup_flow_control(tp, 0, 0);
4642 current_link_up = 1;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004643 tp->phy_flags |=
4644 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004645 tp->serdes_counter =
4646 SERDES_PARALLEL_DET_TIMEOUT;
4647 } else
4648 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 }
4650 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07004651 } else {
4652 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004653 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654 }
4655
4656out:
4657 return current_link_up;
4658}
4659
4660static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
4661{
4662 int current_link_up = 0;
4663
Michael Chan5cf64b8a2007-05-05 12:11:21 -07004664 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666
4667 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08004668 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004670
Matt Carlson5be73b42007-12-20 20:09:29 -08004671 if (fiber_autoneg(tp, &txflags, &rxflags)) {
4672 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673
Matt Carlson5be73b42007-12-20 20:09:29 -08004674 if (txflags & ANEG_CFG_PS1)
4675 local_adv |= ADVERTISE_1000XPAUSE;
4676 if (txflags & ANEG_CFG_PS2)
4677 local_adv |= ADVERTISE_1000XPSE_ASYM;
4678
4679 if (rxflags & MR_LP_ADV_SYM_PAUSE)
4680 remote_adv |= LPA_1000XPAUSE;
4681 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
4682 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683
Matt Carlson859edb22011-12-08 14:40:16 +00004684 tp->link_config.rmt_adv =
4685 mii_adv_to_ethtool_adv_x(remote_adv);
4686
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687 tg3_setup_flow_control(tp, local_adv, remote_adv);
4688
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 current_link_up = 1;
4690 }
4691 for (i = 0; i < 30; i++) {
4692 udelay(20);
4693 tw32_f(MAC_STATUS,
4694 (MAC_STATUS_SYNC_CHANGED |
4695 MAC_STATUS_CFG_CHANGED));
4696 udelay(40);
4697 if ((tr32(MAC_STATUS) &
4698 (MAC_STATUS_SYNC_CHANGED |
4699 MAC_STATUS_CFG_CHANGED)) == 0)
4700 break;
4701 }
4702
4703 mac_status = tr32(MAC_STATUS);
4704 if (current_link_up == 0 &&
4705 (mac_status & MAC_STATUS_PCS_SYNCED) &&
4706 !(mac_status & MAC_STATUS_RCVD_CFG))
4707 current_link_up = 1;
4708 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08004709 tg3_setup_flow_control(tp, 0, 0);
4710
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711 /* Forcing 1000FD link up. */
4712 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713
4714 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
4715 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004716
4717 tw32_f(MAC_MODE, tp->mac_mode);
4718 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 }
4720
4721out:
4722 return current_link_up;
4723}
4724
4725static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
4726{
4727 u32 orig_pause_cfg;
4728 u16 orig_active_speed;
4729 u8 orig_active_duplex;
4730 u32 mac_status;
4731 int current_link_up;
4732 int i;
4733
Matt Carlson8d018622007-12-20 20:05:44 -08004734 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735 orig_active_speed = tp->link_config.active_speed;
4736 orig_active_duplex = tp->link_config.active_duplex;
4737
Joe Perches63c3a662011-04-26 08:12:10 +00004738 if (!tg3_flag(tp, HW_AUTONEG) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739 netif_carrier_ok(tp->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00004740 tg3_flag(tp, INIT_COMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741 mac_status = tr32(MAC_STATUS);
4742 mac_status &= (MAC_STATUS_PCS_SYNCED |
4743 MAC_STATUS_SIGNAL_DET |
4744 MAC_STATUS_CFG_CHANGED |
4745 MAC_STATUS_RCVD_CFG);
4746 if (mac_status == (MAC_STATUS_PCS_SYNCED |
4747 MAC_STATUS_SIGNAL_DET)) {
4748 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4749 MAC_STATUS_CFG_CHANGED));
4750 return 0;
4751 }
4752 }
4753
4754 tw32_f(MAC_TX_AUTO_NEG, 0);
4755
4756 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
4757 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
4758 tw32_f(MAC_MODE, tp->mac_mode);
4759 udelay(40);
4760
Matt Carlson79eb6902010-02-17 15:17:03 +00004761 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762 tg3_init_bcm8002(tp);
4763
4764 /* Enable link change event even when serdes polling. */
4765 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4766 udelay(40);
4767
4768 current_link_up = 0;
Matt Carlson859edb22011-12-08 14:40:16 +00004769 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770 mac_status = tr32(MAC_STATUS);
4771
Joe Perches63c3a662011-04-26 08:12:10 +00004772 if (tg3_flag(tp, HW_AUTONEG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
4774 else
4775 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
4776
Matt Carlson898a56f2009-08-28 14:02:40 +00004777 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00004779 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780
4781 for (i = 0; i < 100; i++) {
4782 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4783 MAC_STATUS_CFG_CHANGED));
4784 udelay(5);
4785 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07004786 MAC_STATUS_CFG_CHANGED |
4787 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788 break;
4789 }
4790
4791 mac_status = tr32(MAC_STATUS);
4792 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
4793 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004794 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
4795 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796 tw32_f(MAC_MODE, (tp->mac_mode |
4797 MAC_MODE_SEND_CONFIGS));
4798 udelay(1);
4799 tw32_f(MAC_MODE, tp->mac_mode);
4800 }
4801 }
4802
4803 if (current_link_up == 1) {
4804 tp->link_config.active_speed = SPEED_1000;
4805 tp->link_config.active_duplex = DUPLEX_FULL;
4806 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4807 LED_CTRL_LNKLED_OVERRIDE |
4808 LED_CTRL_1000MBPS_ON));
4809 } else {
4810 tp->link_config.active_speed = SPEED_INVALID;
4811 tp->link_config.active_duplex = DUPLEX_INVALID;
4812 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4813 LED_CTRL_LNKLED_OVERRIDE |
4814 LED_CTRL_TRAFFIC_OVERRIDE));
4815 }
4816
4817 if (current_link_up != netif_carrier_ok(tp->dev)) {
4818 if (current_link_up)
4819 netif_carrier_on(tp->dev);
4820 else
4821 netif_carrier_off(tp->dev);
4822 tg3_link_report(tp);
4823 } else {
Matt Carlson8d018622007-12-20 20:05:44 -08004824 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825 if (orig_pause_cfg != now_pause_cfg ||
4826 orig_active_speed != tp->link_config.active_speed ||
4827 orig_active_duplex != tp->link_config.active_duplex)
4828 tg3_link_report(tp);
4829 }
4830
4831 return 0;
4832}
4833
Michael Chan747e8f82005-07-25 12:33:22 -07004834static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
4835{
4836 int current_link_up, err = 0;
4837 u32 bmsr, bmcr;
4838 u16 current_speed;
4839 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08004840 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07004841
4842 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4843 tw32_f(MAC_MODE, tp->mac_mode);
4844 udelay(40);
4845
4846 tw32(MAC_EVENT, 0);
4847
4848 tw32_f(MAC_STATUS,
4849 (MAC_STATUS_SYNC_CHANGED |
4850 MAC_STATUS_CFG_CHANGED |
4851 MAC_STATUS_MI_COMPLETION |
4852 MAC_STATUS_LNKSTATE_CHANGED));
4853 udelay(40);
4854
4855 if (force_reset)
4856 tg3_phy_reset(tp);
4857
4858 current_link_up = 0;
4859 current_speed = SPEED_INVALID;
4860 current_duplex = DUPLEX_INVALID;
Matt Carlson859edb22011-12-08 14:40:16 +00004861 tp->link_config.rmt_adv = 0;
Michael Chan747e8f82005-07-25 12:33:22 -07004862
4863 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4864 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004865 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
4866 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4867 bmsr |= BMSR_LSTATUS;
4868 else
4869 bmsr &= ~BMSR_LSTATUS;
4870 }
Michael Chan747e8f82005-07-25 12:33:22 -07004871
4872 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
4873
4874 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004875 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004876 /* do nothing, just check for link up at the end */
4877 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson28011cf2011-11-16 18:36:59 -05004878 u32 adv, newadv;
Michael Chan747e8f82005-07-25 12:33:22 -07004879
4880 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
Matt Carlson28011cf2011-11-16 18:36:59 -05004881 newadv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
4882 ADVERTISE_1000XPAUSE |
4883 ADVERTISE_1000XPSE_ASYM |
4884 ADVERTISE_SLCT);
Michael Chan747e8f82005-07-25 12:33:22 -07004885
Matt Carlson28011cf2011-11-16 18:36:59 -05004886 newadv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Matt Carlson37f07022011-11-17 14:30:55 +00004887 newadv |= ethtool_adv_to_mii_adv_x(tp->link_config.advertising);
Michael Chan747e8f82005-07-25 12:33:22 -07004888
Matt Carlson28011cf2011-11-16 18:36:59 -05004889 if ((newadv != adv) || !(bmcr & BMCR_ANENABLE)) {
4890 tg3_writephy(tp, MII_ADVERTISE, newadv);
Michael Chan747e8f82005-07-25 12:33:22 -07004891 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
4892 tg3_writephy(tp, MII_BMCR, bmcr);
4893
4894 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07004895 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004896 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004897
4898 return err;
4899 }
4900 } else {
4901 u32 new_bmcr;
4902
4903 bmcr &= ~BMCR_SPEED1000;
4904 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
4905
4906 if (tp->link_config.duplex == DUPLEX_FULL)
4907 new_bmcr |= BMCR_FULLDPLX;
4908
4909 if (new_bmcr != bmcr) {
4910 /* BMCR_SPEED1000 is a reserved bit that needs
4911 * to be set on write.
4912 */
4913 new_bmcr |= BMCR_SPEED1000;
4914
4915 /* Force a linkdown */
4916 if (netif_carrier_ok(tp->dev)) {
4917 u32 adv;
4918
4919 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4920 adv &= ~(ADVERTISE_1000XFULL |
4921 ADVERTISE_1000XHALF |
4922 ADVERTISE_SLCT);
4923 tg3_writephy(tp, MII_ADVERTISE, adv);
4924 tg3_writephy(tp, MII_BMCR, bmcr |
4925 BMCR_ANRESTART |
4926 BMCR_ANENABLE);
4927 udelay(10);
4928 netif_carrier_off(tp->dev);
4929 }
4930 tg3_writephy(tp, MII_BMCR, new_bmcr);
4931 bmcr = new_bmcr;
4932 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4933 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004934 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
4935 ASIC_REV_5714) {
4936 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4937 bmsr |= BMSR_LSTATUS;
4938 else
4939 bmsr &= ~BMSR_LSTATUS;
4940 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004941 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004942 }
4943 }
4944
4945 if (bmsr & BMSR_LSTATUS) {
4946 current_speed = SPEED_1000;
4947 current_link_up = 1;
4948 if (bmcr & BMCR_FULLDPLX)
4949 current_duplex = DUPLEX_FULL;
4950 else
4951 current_duplex = DUPLEX_HALF;
4952
Matt Carlsonef167e22007-12-20 20:10:01 -08004953 local_adv = 0;
4954 remote_adv = 0;
4955
Michael Chan747e8f82005-07-25 12:33:22 -07004956 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08004957 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07004958
4959 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
4960 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
4961 common = local_adv & remote_adv;
4962 if (common & (ADVERTISE_1000XHALF |
4963 ADVERTISE_1000XFULL)) {
4964 if (common & ADVERTISE_1000XFULL)
4965 current_duplex = DUPLEX_FULL;
4966 else
4967 current_duplex = DUPLEX_HALF;
Matt Carlson859edb22011-12-08 14:40:16 +00004968
4969 tp->link_config.rmt_adv =
4970 mii_adv_to_ethtool_adv_x(remote_adv);
Joe Perches63c3a662011-04-26 08:12:10 +00004971 } else if (!tg3_flag(tp, 5780_CLASS)) {
Matt Carlson57d8b882010-06-05 17:24:35 +00004972 /* Link is up via parallel detect */
Matt Carlson859a588792010-04-05 10:19:28 +00004973 } else {
Michael Chan747e8f82005-07-25 12:33:22 -07004974 current_link_up = 0;
Matt Carlson859a588792010-04-05 10:19:28 +00004975 }
Michael Chan747e8f82005-07-25 12:33:22 -07004976 }
4977 }
4978
Matt Carlsonef167e22007-12-20 20:10:01 -08004979 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
4980 tg3_setup_flow_control(tp, local_adv, remote_adv);
4981
Michael Chan747e8f82005-07-25 12:33:22 -07004982 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4983 if (tp->link_config.active_duplex == DUPLEX_HALF)
4984 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4985
4986 tw32_f(MAC_MODE, tp->mac_mode);
4987 udelay(40);
4988
4989 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4990
4991 tp->link_config.active_speed = current_speed;
4992 tp->link_config.active_duplex = current_duplex;
4993
4994 if (current_link_up != netif_carrier_ok(tp->dev)) {
4995 if (current_link_up)
4996 netif_carrier_on(tp->dev);
4997 else {
4998 netif_carrier_off(tp->dev);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004999 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005000 }
5001 tg3_link_report(tp);
5002 }
5003 return err;
5004}
5005
5006static void tg3_serdes_parallel_detect(struct tg3 *tp)
5007{
Michael Chan3d3ebe72006-09-27 15:59:15 -07005008 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07005009 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07005010 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07005011 return;
5012 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005013
Michael Chan747e8f82005-07-25 12:33:22 -07005014 if (!netif_carrier_ok(tp->dev) &&
5015 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
5016 u32 bmcr;
5017
5018 tg3_readphy(tp, MII_BMCR, &bmcr);
5019 if (bmcr & BMCR_ANENABLE) {
5020 u32 phy1, phy2;
5021
5022 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005023 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
5024 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07005025
5026 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005027 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5028 MII_TG3_DSP_EXP1_INT_STAT);
5029 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
5030 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005031
5032 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
5033 /* We have signal detect and not receiving
5034 * config code words, link is up by parallel
5035 * detection.
5036 */
5037
5038 bmcr &= ~BMCR_ANENABLE;
5039 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
5040 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005041 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005042 }
5043 }
Matt Carlson859a588792010-04-05 10:19:28 +00005044 } else if (netif_carrier_ok(tp->dev) &&
5045 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005046 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005047 u32 phy2;
5048
5049 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005050 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5051 MII_TG3_DSP_EXP1_INT_STAT);
5052 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005053 if (phy2 & 0x20) {
5054 u32 bmcr;
5055
5056 /* Config code words received, turn on autoneg. */
5057 tg3_readphy(tp, MII_BMCR, &bmcr);
5058 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
5059
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005060 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005061
5062 }
5063 }
5064}
5065
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066static int tg3_setup_phy(struct tg3 *tp, int force_reset)
5067{
Matt Carlsonf2096f92011-04-05 14:22:48 +00005068 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 int err;
5070
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005071 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005073 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07005074 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a588792010-04-05 10:19:28 +00005075 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077
Matt Carlsonbcb37f62008-11-03 16:52:09 -08005078 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00005079 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08005080
5081 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
5082 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
5083 scale = 65;
5084 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
5085 scale = 6;
5086 else
5087 scale = 12;
5088
5089 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
5090 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
5091 tw32(GRC_MISC_CFG, val);
5092 }
5093
Matt Carlsonf2096f92011-04-05 14:22:48 +00005094 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
5095 (6 << TX_LENGTHS_IPG_SHIFT);
5096 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
5097 val |= tr32(MAC_TX_LENGTHS) &
5098 (TX_LENGTHS_JMB_FRM_LEN_MSK |
5099 TX_LENGTHS_CNT_DWN_VAL_MSK);
5100
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101 if (tp->link_config.active_speed == SPEED_1000 &&
5102 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00005103 tw32(MAC_TX_LENGTHS, val |
5104 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00005106 tw32(MAC_TX_LENGTHS, val |
5107 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108
Joe Perches63c3a662011-04-26 08:12:10 +00005109 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110 if (netif_carrier_ok(tp->dev)) {
5111 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07005112 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113 } else {
5114 tw32(HOSTCC_STAT_COAL_TICKS, 0);
5115 }
5116 }
5117
Joe Perches63c3a662011-04-26 08:12:10 +00005118 if (tg3_flag(tp, ASPM_WORKAROUND)) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00005119 val = tr32(PCIE_PWR_MGMT_THRESH);
Matt Carlson8ed5d972007-05-07 00:25:49 -07005120 if (!netif_carrier_ok(tp->dev))
5121 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
5122 tp->pwrmgmt_thresh;
5123 else
5124 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
5125 tw32(PCIE_PWR_MGMT_THRESH, val);
5126 }
5127
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128 return err;
5129}
5130
Matt Carlson66cfd1b2010-09-30 10:34:30 +00005131static inline int tg3_irq_sync(struct tg3 *tp)
5132{
5133 return tp->irq_sync;
5134}
5135
Matt Carlson97bd8e42011-04-13 11:05:04 +00005136static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
5137{
5138 int i;
5139
5140 dst = (u32 *)((u8 *)dst + off);
5141 for (i = 0; i < len; i += sizeof(u32))
5142 *dst++ = tr32(off + i);
5143}
5144
5145static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
5146{
5147 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
5148 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
5149 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
5150 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
5151 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
5152 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
5153 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
5154 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
5155 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
5156 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
5157 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
5158 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
5159 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
5160 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
5161 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
5162 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
5163 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
5164 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
5165 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
5166
Joe Perches63c3a662011-04-26 08:12:10 +00005167 if (tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson97bd8e42011-04-13 11:05:04 +00005168 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
5169
5170 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
5171 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
5172 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
5173 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
5174 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
5175 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
5176 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
5177 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
5178
Joe Perches63c3a662011-04-26 08:12:10 +00005179 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00005180 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
5181 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
5182 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
5183 }
5184
5185 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
5186 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
5187 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
5188 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
5189 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
5190
Joe Perches63c3a662011-04-26 08:12:10 +00005191 if (tg3_flag(tp, NVRAM))
Matt Carlson97bd8e42011-04-13 11:05:04 +00005192 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
5193}
5194
5195static void tg3_dump_state(struct tg3 *tp)
5196{
5197 int i;
5198 u32 *regs;
5199
5200 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
5201 if (!regs) {
5202 netdev_err(tp->dev, "Failed allocating register dump buffer\n");
5203 return;
5204 }
5205
Joe Perches63c3a662011-04-26 08:12:10 +00005206 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00005207 /* Read up to but not including private PCI registers */
5208 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
5209 regs[i / sizeof(u32)] = tr32(i);
5210 } else
5211 tg3_dump_legacy_regs(tp, regs);
5212
5213 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
5214 if (!regs[i + 0] && !regs[i + 1] &&
5215 !regs[i + 2] && !regs[i + 3])
5216 continue;
5217
5218 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
5219 i * 4,
5220 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
5221 }
5222
5223 kfree(regs);
5224
5225 for (i = 0; i < tp->irq_cnt; i++) {
5226 struct tg3_napi *tnapi = &tp->napi[i];
5227
5228 /* SW status block */
5229 netdev_err(tp->dev,
5230 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
5231 i,
5232 tnapi->hw_status->status,
5233 tnapi->hw_status->status_tag,
5234 tnapi->hw_status->rx_jumbo_consumer,
5235 tnapi->hw_status->rx_consumer,
5236 tnapi->hw_status->rx_mini_consumer,
5237 tnapi->hw_status->idx[0].rx_producer,
5238 tnapi->hw_status->idx[0].tx_consumer);
5239
5240 netdev_err(tp->dev,
5241 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
5242 i,
5243 tnapi->last_tag, tnapi->last_irq_tag,
5244 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
5245 tnapi->rx_rcb_ptr,
5246 tnapi->prodring.rx_std_prod_idx,
5247 tnapi->prodring.rx_std_cons_idx,
5248 tnapi->prodring.rx_jmb_prod_idx,
5249 tnapi->prodring.rx_jmb_cons_idx);
5250 }
5251}
5252
Michael Chandf3e6542006-05-26 17:48:07 -07005253/* This is called whenever we suspect that the system chipset is re-
5254 * ordering the sequence of MMIO to the tx send mailbox. The symptom
5255 * is bogus tx completions. We try to recover by setting the
5256 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
5257 * in the workqueue.
5258 */
5259static void tg3_tx_recover(struct tg3 *tp)
5260{
Joe Perches63c3a662011-04-26 08:12:10 +00005261 BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
Michael Chandf3e6542006-05-26 17:48:07 -07005262 tp->write32_tx_mbox == tg3_write_indirect_mbox);
5263
Matt Carlson5129c3a2010-04-05 10:19:23 +00005264 netdev_warn(tp->dev,
5265 "The system may be re-ordering memory-mapped I/O "
5266 "cycles to the network device, attempting to recover. "
5267 "Please report the problem to the driver maintainer "
5268 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07005269
5270 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00005271 tg3_flag_set(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07005272 spin_unlock(&tp->lock);
5273}
5274
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005275static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07005276{
Matt Carlsonf65aac12010-08-02 11:26:03 +00005277 /* Tell compiler to fetch tx indices from memory. */
5278 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005279 return tnapi->tx_pending -
5280 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07005281}
5282
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283/* Tigon3 never reports partial packet sends. So we do not
5284 * need special logic to handle SKBs that have not had all
5285 * of their frags sent yet, like SunGEM does.
5286 */
Matt Carlson17375d22009-08-28 14:02:18 +00005287static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005288{
Matt Carlson17375d22009-08-28 14:02:18 +00005289 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005290 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005291 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005292 struct netdev_queue *txq;
5293 int index = tnapi - tp->napi;
Tom Herbert298376d2011-11-28 16:33:30 +00005294 unsigned int pkts_compl = 0, bytes_compl = 0;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005295
Joe Perches63c3a662011-04-26 08:12:10 +00005296 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005297 index--;
5298
5299 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300
5301 while (sw_idx != hw_idx) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00005302 struct tg3_tx_ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07005304 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305
Michael Chandf3e6542006-05-26 17:48:07 -07005306 if (unlikely(skb == NULL)) {
5307 tg3_tx_recover(tp);
5308 return;
5309 }
5310
Alexander Duyckf4188d82009-12-02 16:48:38 +00005311 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005312 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005313 skb_headlen(skb),
5314 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005315
5316 ri->skb = NULL;
5317
Matt Carlsone01ee142011-07-27 14:20:50 +00005318 while (ri->fragmented) {
5319 ri->fragmented = false;
5320 sw_idx = NEXT_TX(sw_idx);
5321 ri = &tnapi->tx_buffers[sw_idx];
5322 }
5323
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324 sw_idx = NEXT_TX(sw_idx);
5325
5326 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005327 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07005328 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
5329 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005330
5331 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005332 dma_unmap_addr(ri, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00005333 skb_frag_size(&skb_shinfo(skb)->frags[i]),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005334 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00005335
5336 while (ri->fragmented) {
5337 ri->fragmented = false;
5338 sw_idx = NEXT_TX(sw_idx);
5339 ri = &tnapi->tx_buffers[sw_idx];
5340 }
5341
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342 sw_idx = NEXT_TX(sw_idx);
5343 }
5344
Tom Herbert298376d2011-11-28 16:33:30 +00005345 pkts_compl++;
5346 bytes_compl += skb->len;
5347
David S. Millerf47c11e2005-06-24 20:18:35 -07005348 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07005349
5350 if (unlikely(tx_bug)) {
5351 tg3_tx_recover(tp);
5352 return;
5353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354 }
5355
Tom Herbert298376d2011-11-28 16:33:30 +00005356 netdev_completed_queue(tp->dev, pkts_compl, bytes_compl);
5357
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005358 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359
Michael Chan1b2a7202006-08-07 21:46:02 -07005360 /* Need to make the tx_cons update visible to tg3_start_xmit()
5361 * before checking for netif_queue_stopped(). Without the
5362 * memory barrier, there is a small possibility that tg3_start_xmit()
5363 * will miss it and cause the queue to be stopped forever.
5364 */
5365 smp_mb();
5366
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005367 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005368 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005369 __netif_tx_lock(txq, smp_processor_id());
5370 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005371 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005372 netif_tx_wake_queue(txq);
5373 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07005374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375}
5376
Eric Dumazet9205fd92011-11-18 06:47:01 +00005377static void tg3_rx_data_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005378{
Eric Dumazet9205fd92011-11-18 06:47:01 +00005379 if (!ri->data)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005380 return;
5381
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005382 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005383 map_sz, PCI_DMA_FROMDEVICE);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005384 kfree(ri->data);
5385 ri->data = NULL;
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005386}
5387
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388/* Returns size of skb allocated or < 0 on error.
5389 *
5390 * We only need to fill in the address because the other members
5391 * of the RX descriptor are invariant, see tg3_init_rings.
5392 *
5393 * Note the purposeful assymetry of cpu vs. chip accesses. For
5394 * posting buffers we only dirty the first cache line of the RX
5395 * descriptor (containing the address). Whereas for the RX status
5396 * buffers the cpu only reads the last cacheline of the RX descriptor
5397 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
5398 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00005399static int tg3_alloc_rx_data(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Matt Carlsona3896162009-11-13 13:03:44 +00005400 u32 opaque_key, u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401{
5402 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00005403 struct ring_info *map;
Eric Dumazet9205fd92011-11-18 06:47:01 +00005404 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405 dma_addr_t mapping;
Eric Dumazet9205fd92011-11-18 06:47:01 +00005406 int skb_size, data_size, dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408 switch (opaque_key) {
5409 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00005410 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00005411 desc = &tpr->rx_std[dest_idx];
5412 map = &tpr->rx_std_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00005413 data_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414 break;
5415
5416 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00005417 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00005418 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00005419 map = &tpr->rx_jmb_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00005420 data_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421 break;
5422
5423 default:
5424 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426
5427 /* Do not overwrite any of the map or rp information
5428 * until we are sure we can commit to a new buffer.
5429 *
5430 * Callers depend upon this behavior and assume that
5431 * we leave everything unchanged if we fail.
5432 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00005433 skb_size = SKB_DATA_ALIGN(data_size + TG3_RX_OFFSET(tp)) +
5434 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
5435 data = kmalloc(skb_size, GFP_ATOMIC);
5436 if (!data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437 return -ENOMEM;
5438
Eric Dumazet9205fd92011-11-18 06:47:01 +00005439 mapping = pci_map_single(tp->pdev,
5440 data + TG3_RX_OFFSET(tp),
5441 data_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442 PCI_DMA_FROMDEVICE);
Matt Carlsona21771d2009-11-02 14:25:31 +00005443 if (pci_dma_mapping_error(tp->pdev, mapping)) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00005444 kfree(data);
Matt Carlsona21771d2009-11-02 14:25:31 +00005445 return -EIO;
5446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447
Eric Dumazet9205fd92011-11-18 06:47:01 +00005448 map->data = data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005449 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451 desc->addr_hi = ((u64)mapping >> 32);
5452 desc->addr_lo = ((u64)mapping & 0xffffffff);
5453
Eric Dumazet9205fd92011-11-18 06:47:01 +00005454 return data_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455}
5456
5457/* We only need to move over in the address because the other
5458 * members of the RX descriptor are invariant. See notes above
Eric Dumazet9205fd92011-11-18 06:47:01 +00005459 * tg3_alloc_rx_data for full details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460 */
Matt Carlsona3896162009-11-13 13:03:44 +00005461static void tg3_recycle_rx(struct tg3_napi *tnapi,
5462 struct tg3_rx_prodring_set *dpr,
5463 u32 opaque_key, int src_idx,
5464 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465{
Matt Carlson17375d22009-08-28 14:02:18 +00005466 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
5468 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00005469 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005470 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005471
5472 switch (opaque_key) {
5473 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00005474 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00005475 dest_desc = &dpr->rx_std[dest_idx];
5476 dest_map = &dpr->rx_std_buffers[dest_idx];
5477 src_desc = &spr->rx_std[src_idx];
5478 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479 break;
5480
5481 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00005482 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00005483 dest_desc = &dpr->rx_jmb[dest_idx].std;
5484 dest_map = &dpr->rx_jmb_buffers[dest_idx];
5485 src_desc = &spr->rx_jmb[src_idx].std;
5486 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487 break;
5488
5489 default:
5490 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492
Eric Dumazet9205fd92011-11-18 06:47:01 +00005493 dest_map->data = src_map->data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005494 dma_unmap_addr_set(dest_map, mapping,
5495 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496 dest_desc->addr_hi = src_desc->addr_hi;
5497 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00005498
5499 /* Ensure that the update to the skb happens after the physical
5500 * addresses have been transferred to the new BD location.
5501 */
5502 smp_wmb();
5503
Eric Dumazet9205fd92011-11-18 06:47:01 +00005504 src_map->data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505}
5506
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507/* The RX ring scheme is composed of multiple rings which post fresh
5508 * buffers to the chip, and one special ring the chip uses to report
5509 * status back to the host.
5510 *
5511 * The special ring reports the status of received packets to the
5512 * host. The chip does not write into the original descriptor the
5513 * RX buffer was obtained from. The chip simply takes the original
5514 * descriptor as provided by the host, updates the status and length
5515 * field, then writes this into the next status ring entry.
5516 *
5517 * Each ring the host uses to post buffers to the chip is described
5518 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
5519 * it is first placed into the on-chip ram. When the packet's length
5520 * is known, it walks down the TG3_BDINFO entries to select the ring.
5521 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
5522 * which is within the range of the new packet's length is chosen.
5523 *
5524 * The "separate ring for rx status" scheme may sound queer, but it makes
5525 * sense from a cache coherency perspective. If only the host writes
5526 * to the buffer post rings, and only the chip writes to the rx status
5527 * rings, then cache lines never move beyond shared-modified state.
5528 * If both the host and chip were to write into the same ring, cache line
5529 * eviction could occur since both entities want it in an exclusive state.
5530 */
Matt Carlson17375d22009-08-28 14:02:18 +00005531static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532{
Matt Carlson17375d22009-08-28 14:02:18 +00005533 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07005534 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00005535 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00005536 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07005537 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00005539 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005541 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542 /*
5543 * We need to order the read of hw_idx and the read of
5544 * the opaque cookie.
5545 */
5546 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547 work_mask = 0;
5548 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00005549 std_prod_idx = tpr->rx_std_prod_idx;
5550 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00005552 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00005553 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554 unsigned int len;
5555 struct sk_buff *skb;
5556 dma_addr_t dma_addr;
5557 u32 opaque_key, desc_idx, *post_ptr;
Eric Dumazet9205fd92011-11-18 06:47:01 +00005558 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559
5560 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
5561 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
5562 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005563 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005564 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005565 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00005566 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07005567 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005569 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005570 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005571 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00005572 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00005573 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575
5576 work_mask |= opaque_key;
5577
5578 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
5579 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
5580 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00005581 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582 desc_idx, *post_ptr);
5583 drop_it_no_recycle:
5584 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00005585 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586 goto next_pkt;
5587 }
5588
Eric Dumazet9205fd92011-11-18 06:47:01 +00005589 prefetch(data + TG3_RX_OFFSET(tp));
Matt Carlsonad829262008-11-21 17:16:16 -08005590 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
5591 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592
Matt Carlsond2757fc2010-04-12 06:58:27 +00005593 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594 int skb_size;
5595
Eric Dumazet9205fd92011-11-18 06:47:01 +00005596 skb_size = tg3_alloc_rx_data(tp, tpr, opaque_key,
Matt Carlsonafc081f2009-11-13 13:03:43 +00005597 *post_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598 if (skb_size < 0)
5599 goto drop_it;
5600
Matt Carlson287be122009-08-28 13:58:46 +00005601 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602 PCI_DMA_FROMDEVICE);
5603
Eric Dumazet9205fd92011-11-18 06:47:01 +00005604 skb = build_skb(data);
5605 if (!skb) {
5606 kfree(data);
5607 goto drop_it_no_recycle;
5608 }
5609 skb_reserve(skb, TG3_RX_OFFSET(tp));
5610 /* Ensure that the update to the data happens
Matt Carlson61e800c2010-02-17 15:16:54 +00005611 * after the usage of the old DMA mapping.
5612 */
5613 smp_wmb();
5614
Eric Dumazet9205fd92011-11-18 06:47:01 +00005615 ri->data = NULL;
Matt Carlson61e800c2010-02-17 15:16:54 +00005616
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617 } else {
Matt Carlsona3896162009-11-13 13:03:44 +00005618 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619 desc_idx, *post_ptr);
5620
Eric Dumazet9205fd92011-11-18 06:47:01 +00005621 skb = netdev_alloc_skb(tp->dev,
5622 len + TG3_RAW_IP_ALIGN);
5623 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624 goto drop_it_no_recycle;
5625
Eric Dumazet9205fd92011-11-18 06:47:01 +00005626 skb_reserve(skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005628 memcpy(skb->data,
5629 data + TG3_RX_OFFSET(tp),
5630 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632 }
5633
Eric Dumazet9205fd92011-11-18 06:47:01 +00005634 skb_put(skb, len);
Michał Mirosławdc668912011-04-07 03:35:07 +00005635 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
5637 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
5638 >> RXD_TCPCSUM_SHIFT) == 0xffff))
5639 skb->ip_summed = CHECKSUM_UNNECESSARY;
5640 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005641 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642
5643 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00005644
5645 if (len > (tp->dev->mtu + ETH_HLEN) &&
5646 skb->protocol != htons(ETH_P_8021Q)) {
5647 dev_kfree_skb(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00005648 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00005649 }
5650
Matt Carlson9dc7a112010-04-12 06:58:28 +00005651 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00005652 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
5653 __vlan_hwaccel_put_tag(skb,
5654 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00005655
Matt Carlsonbf933c82011-01-25 15:58:49 +00005656 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658 received++;
5659 budget--;
5660
5661next_pkt:
5662 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07005663
5664 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005665 tpr->rx_std_prod_idx = std_prod_idx &
5666 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00005667 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5668 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07005669 work_mask &= ~RXD_OPAQUE_RING_STD;
5670 rx_std_posted = 0;
5671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07005673 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00005674 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07005675
5676 /* Refresh hw_idx to see if there is new work */
5677 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005678 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07005679 rmb();
5680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681 }
5682
5683 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00005684 tnapi->rx_rcb_ptr = sw_idx;
5685 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686
5687 /* Refill RX ring(s). */
Joe Perches63c3a662011-04-26 08:12:10 +00005688 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005689 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005690 tpr->rx_std_prod_idx = std_prod_idx &
5691 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005692 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5693 tpr->rx_std_prod_idx);
5694 }
5695 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005696 tpr->rx_jmb_prod_idx = jmb_prod_idx &
5697 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005698 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5699 tpr->rx_jmb_prod_idx);
5700 }
5701 mmiowb();
5702 } else if (work_mask) {
5703 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
5704 * updated before the producer indices can be updated.
5705 */
5706 smp_wmb();
5707
Matt Carlson2c49a442010-09-30 10:34:35 +00005708 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
5709 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005710
Matt Carlsone4af1af2010-02-12 14:47:05 +00005711 if (tnapi != &tp->napi[1])
5712 napi_schedule(&tp->napi[1].napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714
5715 return received;
5716}
5717
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005718static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720 /* handle link change and other phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00005721 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005722 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
5723
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724 if (sblk->status & SD_STATUS_LINK_CHG) {
5725 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005726 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07005727 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00005728 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsondd477002008-05-25 23:45:58 -07005729 tw32_f(MAC_STATUS,
5730 (MAC_STATUS_SYNC_CHANGED |
5731 MAC_STATUS_CFG_CHANGED |
5732 MAC_STATUS_MI_COMPLETION |
5733 MAC_STATUS_LNKSTATE_CHANGED));
5734 udelay(40);
5735 } else
5736 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07005737 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738 }
5739 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005740}
5741
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005742static int tg3_rx_prodring_xfer(struct tg3 *tp,
5743 struct tg3_rx_prodring_set *dpr,
5744 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005745{
5746 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005747 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005748
5749 while (1) {
5750 src_prod_idx = spr->rx_std_prod_idx;
5751
5752 /* Make sure updates to the rx_std_buffers[] entries and the
5753 * standard producer index are seen in the correct order.
5754 */
5755 smp_rmb();
5756
5757 if (spr->rx_std_cons_idx == src_prod_idx)
5758 break;
5759
5760 if (spr->rx_std_cons_idx < src_prod_idx)
5761 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
5762 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005763 cpycnt = tp->rx_std_ring_mask + 1 -
5764 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005765
Matt Carlson2c49a442010-09-30 10:34:35 +00005766 cpycnt = min(cpycnt,
5767 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005768
5769 si = spr->rx_std_cons_idx;
5770 di = dpr->rx_std_prod_idx;
5771
Matt Carlsone92967b2010-02-12 14:47:06 +00005772 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00005773 if (dpr->rx_std_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00005774 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005775 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005776 break;
5777 }
5778 }
5779
5780 if (!cpycnt)
5781 break;
5782
5783 /* Ensure that updates to the rx_std_buffers ring and the
5784 * shadowed hardware producer ring from tg3_recycle_skb() are
5785 * ordered correctly WRT the skb check above.
5786 */
5787 smp_rmb();
5788
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005789 memcpy(&dpr->rx_std_buffers[di],
5790 &spr->rx_std_buffers[si],
5791 cpycnt * sizeof(struct ring_info));
5792
5793 for (i = 0; i < cpycnt; i++, di++, si++) {
5794 struct tg3_rx_buffer_desc *sbd, *dbd;
5795 sbd = &spr->rx_std[si];
5796 dbd = &dpr->rx_std[di];
5797 dbd->addr_hi = sbd->addr_hi;
5798 dbd->addr_lo = sbd->addr_lo;
5799 }
5800
Matt Carlson2c49a442010-09-30 10:34:35 +00005801 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
5802 tp->rx_std_ring_mask;
5803 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
5804 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005805 }
5806
5807 while (1) {
5808 src_prod_idx = spr->rx_jmb_prod_idx;
5809
5810 /* Make sure updates to the rx_jmb_buffers[] entries and
5811 * the jumbo producer index are seen in the correct order.
5812 */
5813 smp_rmb();
5814
5815 if (spr->rx_jmb_cons_idx == src_prod_idx)
5816 break;
5817
5818 if (spr->rx_jmb_cons_idx < src_prod_idx)
5819 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
5820 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005821 cpycnt = tp->rx_jmb_ring_mask + 1 -
5822 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005823
5824 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00005825 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005826
5827 si = spr->rx_jmb_cons_idx;
5828 di = dpr->rx_jmb_prod_idx;
5829
Matt Carlsone92967b2010-02-12 14:47:06 +00005830 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00005831 if (dpr->rx_jmb_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00005832 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005833 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005834 break;
5835 }
5836 }
5837
5838 if (!cpycnt)
5839 break;
5840
5841 /* Ensure that updates to the rx_jmb_buffers ring and the
5842 * shadowed hardware producer ring from tg3_recycle_skb() are
5843 * ordered correctly WRT the skb check above.
5844 */
5845 smp_rmb();
5846
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005847 memcpy(&dpr->rx_jmb_buffers[di],
5848 &spr->rx_jmb_buffers[si],
5849 cpycnt * sizeof(struct ring_info));
5850
5851 for (i = 0; i < cpycnt; i++, di++, si++) {
5852 struct tg3_rx_buffer_desc *sbd, *dbd;
5853 sbd = &spr->rx_jmb[si].std;
5854 dbd = &dpr->rx_jmb[di].std;
5855 dbd->addr_hi = sbd->addr_hi;
5856 dbd->addr_lo = sbd->addr_lo;
5857 }
5858
Matt Carlson2c49a442010-09-30 10:34:35 +00005859 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
5860 tp->rx_jmb_ring_mask;
5861 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
5862 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005863 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005864
5865 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005866}
5867
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005868static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
5869{
5870 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005871
5872 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005873 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00005874 tg3_tx(tnapi);
Joe Perches63c3a662011-04-26 08:12:10 +00005875 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Michael Chan4fd7ab52007-10-12 01:39:50 -07005876 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005877 }
5878
Linus Torvalds1da177e2005-04-16 15:20:36 -07005879 /* run RX thread, within the bounds set by NAPI.
5880 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005881 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005883 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00005884 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005885
Joe Perches63c3a662011-04-26 08:12:10 +00005886 if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005887 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005888 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00005889 u32 std_prod_idx = dpr->rx_std_prod_idx;
5890 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005891
Matt Carlsone4af1af2010-02-12 14:47:05 +00005892 for (i = 1; i < tp->irq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005893 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00005894 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005895
5896 wmb();
5897
Matt Carlsone4af1af2010-02-12 14:47:05 +00005898 if (std_prod_idx != dpr->rx_std_prod_idx)
5899 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5900 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005901
Matt Carlsone4af1af2010-02-12 14:47:05 +00005902 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
5903 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5904 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005905
5906 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005907
5908 if (err)
5909 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005910 }
5911
David S. Miller6f535762007-10-11 18:08:29 -07005912 return work_done;
5913}
David S. Millerf7383c222005-05-18 22:50:53 -07005914
Matt Carlsondb219972011-11-04 09:15:03 +00005915static inline void tg3_reset_task_schedule(struct tg3 *tp)
5916{
5917 if (!test_and_set_bit(TG3_FLAG_RESET_TASK_PENDING, tp->tg3_flags))
5918 schedule_work(&tp->reset_task);
5919}
5920
5921static inline void tg3_reset_task_cancel(struct tg3 *tp)
5922{
5923 cancel_work_sync(&tp->reset_task);
5924 tg3_flag_clear(tp, RESET_TASK_PENDING);
5925}
5926
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005927static int tg3_poll_msix(struct napi_struct *napi, int budget)
5928{
5929 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5930 struct tg3 *tp = tnapi->tp;
5931 int work_done = 0;
5932 struct tg3_hw_status *sblk = tnapi->hw_status;
5933
5934 while (1) {
5935 work_done = tg3_poll_work(tnapi, work_done, budget);
5936
Joe Perches63c3a662011-04-26 08:12:10 +00005937 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005938 goto tx_recovery;
5939
5940 if (unlikely(work_done >= budget))
5941 break;
5942
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005943 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005944 * to tell the hw how much work has been processed,
5945 * so we must read it before checking for more work.
5946 */
5947 tnapi->last_tag = sblk->status_tag;
5948 tnapi->last_irq_tag = tnapi->last_tag;
5949 rmb();
5950
5951 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00005952 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
5953 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005954 napi_complete(napi);
5955 /* Reenable interrupts. */
5956 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
5957 mmiowb();
5958 break;
5959 }
5960 }
5961
5962 return work_done;
5963
5964tx_recovery:
5965 /* work_done is guaranteed to be less than budget. */
5966 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00005967 tg3_reset_task_schedule(tp);
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005968 return work_done;
5969}
5970
Matt Carlsone64de4e2011-04-13 11:05:05 +00005971static void tg3_process_error(struct tg3 *tp)
5972{
5973 u32 val;
5974 bool real_error = false;
5975
Joe Perches63c3a662011-04-26 08:12:10 +00005976 if (tg3_flag(tp, ERROR_PROCESSED))
Matt Carlsone64de4e2011-04-13 11:05:05 +00005977 return;
5978
5979 /* Check Flow Attention register */
5980 val = tr32(HOSTCC_FLOW_ATTN);
5981 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
5982 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
5983 real_error = true;
5984 }
5985
5986 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
5987 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
5988 real_error = true;
5989 }
5990
5991 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
5992 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
5993 real_error = true;
5994 }
5995
5996 if (!real_error)
5997 return;
5998
5999 tg3_dump_state(tp);
6000
Joe Perches63c3a662011-04-26 08:12:10 +00006001 tg3_flag_set(tp, ERROR_PROCESSED);
Matt Carlsondb219972011-11-04 09:15:03 +00006002 tg3_reset_task_schedule(tp);
Matt Carlsone64de4e2011-04-13 11:05:05 +00006003}
6004
David S. Miller6f535762007-10-11 18:08:29 -07006005static int tg3_poll(struct napi_struct *napi, int budget)
6006{
Matt Carlson8ef04422009-08-28 14:01:37 +00006007 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
6008 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07006009 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00006010 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07006011
6012 while (1) {
Matt Carlsone64de4e2011-04-13 11:05:05 +00006013 if (sblk->status & SD_STATUS_ERROR)
6014 tg3_process_error(tp);
6015
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006016 tg3_poll_link(tp);
6017
Matt Carlson17375d22009-08-28 14:02:18 +00006018 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07006019
Joe Perches63c3a662011-04-26 08:12:10 +00006020 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
David S. Miller6f535762007-10-11 18:08:29 -07006021 goto tx_recovery;
6022
6023 if (unlikely(work_done >= budget))
6024 break;
6025
Joe Perches63c3a662011-04-26 08:12:10 +00006026 if (tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson17375d22009-08-28 14:02:18 +00006027 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07006028 * to tell the hw how much work has been processed,
6029 * so we must read it before checking for more work.
6030 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006031 tnapi->last_tag = sblk->status_tag;
6032 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07006033 rmb();
6034 } else
6035 sblk->status &= ~SD_STATUS_UPDATED;
6036
Matt Carlson17375d22009-08-28 14:02:18 +00006037 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08006038 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00006039 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07006040 break;
6041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006042 }
6043
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006044 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07006045
6046tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07006047 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08006048 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00006049 tg3_reset_task_schedule(tp);
Michael Chan4fd7ab52007-10-12 01:39:50 -07006050 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006051}
6052
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006053static void tg3_napi_disable(struct tg3 *tp)
6054{
6055 int i;
6056
6057 for (i = tp->irq_cnt - 1; i >= 0; i--)
6058 napi_disable(&tp->napi[i].napi);
6059}
6060
6061static void tg3_napi_enable(struct tg3 *tp)
6062{
6063 int i;
6064
6065 for (i = 0; i < tp->irq_cnt; i++)
6066 napi_enable(&tp->napi[i].napi);
6067}
6068
6069static void tg3_napi_init(struct tg3 *tp)
6070{
6071 int i;
6072
6073 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
6074 for (i = 1; i < tp->irq_cnt; i++)
6075 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
6076}
6077
6078static void tg3_napi_fini(struct tg3 *tp)
6079{
6080 int i;
6081
6082 for (i = 0; i < tp->irq_cnt; i++)
6083 netif_napi_del(&tp->napi[i].napi);
6084}
6085
6086static inline void tg3_netif_stop(struct tg3 *tp)
6087{
6088 tp->dev->trans_start = jiffies; /* prevent tx timeout */
6089 tg3_napi_disable(tp);
6090 netif_tx_disable(tp->dev);
6091}
6092
6093static inline void tg3_netif_start(struct tg3 *tp)
6094{
6095 /* NOTE: unconditional netif_tx_wake_all_queues is only
6096 * appropriate so long as all callers are assured to
6097 * have free tx slots (such as after tg3_init_hw)
6098 */
6099 netif_tx_wake_all_queues(tp->dev);
6100
6101 tg3_napi_enable(tp);
6102 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
6103 tg3_enable_ints(tp);
6104}
6105
David S. Millerf47c11e2005-06-24 20:18:35 -07006106static void tg3_irq_quiesce(struct tg3 *tp)
6107{
Matt Carlson4f125f42009-09-01 12:55:02 +00006108 int i;
6109
David S. Millerf47c11e2005-06-24 20:18:35 -07006110 BUG_ON(tp->irq_sync);
6111
6112 tp->irq_sync = 1;
6113 smp_mb();
6114
Matt Carlson4f125f42009-09-01 12:55:02 +00006115 for (i = 0; i < tp->irq_cnt; i++)
6116 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07006117}
6118
David S. Millerf47c11e2005-06-24 20:18:35 -07006119/* Fully shutdown all tg3 driver activity elsewhere in the system.
6120 * If irq_sync is non-zero, then the IRQ handler must be synchronized
6121 * with as well. Most of the time, this is not necessary except when
6122 * shutting down the device.
6123 */
6124static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
6125{
Michael Chan46966542007-07-11 19:47:19 -07006126 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07006127 if (irq_sync)
6128 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07006129}
6130
6131static inline void tg3_full_unlock(struct tg3 *tp)
6132{
David S. Millerf47c11e2005-06-24 20:18:35 -07006133 spin_unlock_bh(&tp->lock);
6134}
6135
Michael Chanfcfa0a32006-03-20 22:28:41 -08006136/* One-shot MSI handler - Chip automatically disables interrupt
6137 * after sending MSI so driver doesn't have to do it.
6138 */
David Howells7d12e782006-10-05 14:55:46 +01006139static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08006140{
Matt Carlson09943a12009-08-28 14:01:57 +00006141 struct tg3_napi *tnapi = dev_id;
6142 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08006143
Matt Carlson898a56f2009-08-28 14:02:40 +00006144 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006145 if (tnapi->rx_rcb)
6146 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08006147
6148 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00006149 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08006150
6151 return IRQ_HANDLED;
6152}
6153
Michael Chan88b06bc22005-04-21 17:13:25 -07006154/* MSI ISR - No need to check for interrupt sharing and no need to
6155 * flush status block and interrupt mailbox. PCI ordering rules
6156 * guarantee that MSI will arrive after the status block.
6157 */
David Howells7d12e782006-10-05 14:55:46 +01006158static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07006159{
Matt Carlson09943a12009-08-28 14:01:57 +00006160 struct tg3_napi *tnapi = dev_id;
6161 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07006162
Matt Carlson898a56f2009-08-28 14:02:40 +00006163 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006164 if (tnapi->rx_rcb)
6165 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07006166 /*
David S. Millerfac9b832005-05-18 22:46:34 -07006167 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07006168 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07006169 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07006170 * NIC to stop sending us irqs, engaging "in-intr-handler"
6171 * event coalescing.
6172 */
Matt Carlson5b39de92011-08-31 11:44:50 +00006173 tw32_mailbox(tnapi->int_mbox, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07006174 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00006175 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07006176
Michael Chan88b06bc22005-04-21 17:13:25 -07006177 return IRQ_RETVAL(1);
6178}
6179
David Howells7d12e782006-10-05 14:55:46 +01006180static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006181{
Matt Carlson09943a12009-08-28 14:01:57 +00006182 struct tg3_napi *tnapi = dev_id;
6183 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006184 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006185 unsigned int handled = 1;
6186
Linus Torvalds1da177e2005-04-16 15:20:36 -07006187 /* In INTx mode, it is possible for the interrupt to arrive at
6188 * the CPU before the status block posted prior to the interrupt.
6189 * Reading the PCI State register will confirm whether the
6190 * interrupt is ours and will flush the status block.
6191 */
Michael Chand18edcb2007-03-24 20:57:11 -07006192 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
Joe Perches63c3a662011-04-26 08:12:10 +00006193 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07006194 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
6195 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07006196 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07006197 }
Michael Chand18edcb2007-03-24 20:57:11 -07006198 }
6199
6200 /*
6201 * Writing any value to intr-mbox-0 clears PCI INTA# and
6202 * chip-internal interrupt pending events.
6203 * Writing non-zero to intr-mbox-0 additional tells the
6204 * NIC to stop sending us irqs, engaging "in-intr-handler"
6205 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07006206 *
6207 * Flush the mailbox to de-assert the IRQ immediately to prevent
6208 * spurious interrupts. The flush impacts performance but
6209 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07006210 */
Michael Chanc04cb342007-05-07 00:26:15 -07006211 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07006212 if (tg3_irq_sync(tp))
6213 goto out;
6214 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00006215 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00006216 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00006217 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07006218 } else {
6219 /* No work, shared interrupt perhaps? re-enable
6220 * interrupts, and flush that PCI write
6221 */
6222 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
6223 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07006224 }
David S. Millerf47c11e2005-06-24 20:18:35 -07006225out:
David S. Millerfac9b832005-05-18 22:46:34 -07006226 return IRQ_RETVAL(handled);
6227}
6228
David Howells7d12e782006-10-05 14:55:46 +01006229static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07006230{
Matt Carlson09943a12009-08-28 14:01:57 +00006231 struct tg3_napi *tnapi = dev_id;
6232 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006233 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07006234 unsigned int handled = 1;
6235
David S. Millerfac9b832005-05-18 22:46:34 -07006236 /* In INTx mode, it is possible for the interrupt to arrive at
6237 * the CPU before the status block posted prior to the interrupt.
6238 * Reading the PCI State register will confirm whether the
6239 * interrupt is ours and will flush the status block.
6240 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006241 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Joe Perches63c3a662011-04-26 08:12:10 +00006242 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07006243 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
6244 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07006245 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246 }
Michael Chand18edcb2007-03-24 20:57:11 -07006247 }
6248
6249 /*
6250 * writing any value to intr-mbox-0 clears PCI INTA# and
6251 * chip-internal interrupt pending events.
6252 * writing non-zero to intr-mbox-0 additional tells the
6253 * NIC to stop sending us irqs, engaging "in-intr-handler"
6254 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07006255 *
6256 * Flush the mailbox to de-assert the IRQ immediately to prevent
6257 * spurious interrupts. The flush impacts performance but
6258 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07006259 */
Michael Chanc04cb342007-05-07 00:26:15 -07006260 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00006261
6262 /*
6263 * In a shared interrupt configuration, sometimes other devices'
6264 * interrupts will scream. We record the current status tag here
6265 * so that the above check can report that the screaming interrupts
6266 * are unhandled. Eventually they will be silenced.
6267 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006268 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00006269
Michael Chand18edcb2007-03-24 20:57:11 -07006270 if (tg3_irq_sync(tp))
6271 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00006272
Matt Carlson72334482009-08-28 14:03:01 +00006273 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00006274
Matt Carlson09943a12009-08-28 14:01:57 +00006275 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00006276
David S. Millerf47c11e2005-06-24 20:18:35 -07006277out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006278 return IRQ_RETVAL(handled);
6279}
6280
Michael Chan79381092005-04-21 17:13:59 -07006281/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01006282static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07006283{
Matt Carlson09943a12009-08-28 14:01:57 +00006284 struct tg3_napi *tnapi = dev_id;
6285 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006286 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07006287
Michael Chanf9804dd2005-09-27 12:13:10 -07006288 if ((sblk->status & SD_STATUS_UPDATED) ||
6289 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07006290 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07006291 return IRQ_RETVAL(1);
6292 }
6293 return IRQ_RETVAL(0);
6294}
6295
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07006296static int tg3_init_hw(struct tg3 *, int);
Michael Chan944d9802005-05-29 14:57:48 -07006297static int tg3_halt(struct tg3 *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006298
Michael Chanb9ec6c12006-07-25 16:37:27 -07006299/* Restart hardware after configuration changes, self-test, etc.
6300 * Invoked with tp->lock held.
6301 */
6302static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
Eric Dumazet78c61462008-04-24 23:33:06 -07006303 __releases(tp->lock)
6304 __acquires(tp->lock)
Michael Chanb9ec6c12006-07-25 16:37:27 -07006305{
6306 int err;
6307
6308 err = tg3_init_hw(tp, reset_phy);
6309 if (err) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006310 netdev_err(tp->dev,
6311 "Failed to re-initialize device, aborting\n");
Michael Chanb9ec6c12006-07-25 16:37:27 -07006312 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
6313 tg3_full_unlock(tp);
6314 del_timer_sync(&tp->timer);
6315 tp->irq_sync = 0;
Matt Carlsonfed97812009-09-01 13:10:19 +00006316 tg3_napi_enable(tp);
Michael Chanb9ec6c12006-07-25 16:37:27 -07006317 dev_close(tp->dev);
6318 tg3_full_lock(tp, 0);
6319 }
6320 return err;
6321}
6322
Linus Torvalds1da177e2005-04-16 15:20:36 -07006323#ifdef CONFIG_NET_POLL_CONTROLLER
6324static void tg3_poll_controller(struct net_device *dev)
6325{
Matt Carlson4f125f42009-09-01 12:55:02 +00006326 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07006327 struct tg3 *tp = netdev_priv(dev);
6328
Matt Carlson4f125f42009-09-01 12:55:02 +00006329 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00006330 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006331}
6332#endif
6333
David Howellsc4028952006-11-22 14:57:56 +00006334static void tg3_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006335{
David Howellsc4028952006-11-22 14:57:56 +00006336 struct tg3 *tp = container_of(work, struct tg3, reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006337 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006338
Michael Chan7faa0062006-02-02 17:29:28 -08006339 tg3_full_lock(tp, 0);
Michael Chan7faa0062006-02-02 17:29:28 -08006340
6341 if (!netif_running(tp->dev)) {
Matt Carlsondb219972011-11-04 09:15:03 +00006342 tg3_flag_clear(tp, RESET_TASK_PENDING);
Michael Chan7faa0062006-02-02 17:29:28 -08006343 tg3_full_unlock(tp);
6344 return;
6345 }
6346
6347 tg3_full_unlock(tp);
6348
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006349 tg3_phy_stop(tp);
6350
Linus Torvalds1da177e2005-04-16 15:20:36 -07006351 tg3_netif_stop(tp);
6352
David S. Millerf47c11e2005-06-24 20:18:35 -07006353 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006354
Joe Perches63c3a662011-04-26 08:12:10 +00006355 if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
Michael Chandf3e6542006-05-26 17:48:07 -07006356 tp->write32_tx_mbox = tg3_write32_tx_mbox;
6357 tp->write32_rx_mbox = tg3_write_flush_reg32;
Joe Perches63c3a662011-04-26 08:12:10 +00006358 tg3_flag_set(tp, MBOX_WRITE_REORDER);
6359 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07006360 }
6361
Michael Chan944d9802005-05-29 14:57:48 -07006362 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006363 err = tg3_init_hw(tp, 1);
6364 if (err)
Michael Chanb9ec6c12006-07-25 16:37:27 -07006365 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006366
6367 tg3_netif_start(tp);
6368
Michael Chanb9ec6c12006-07-25 16:37:27 -07006369out:
Michael Chan7faa0062006-02-02 17:29:28 -08006370 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006371
6372 if (!err)
6373 tg3_phy_start(tp);
Matt Carlsondb219972011-11-04 09:15:03 +00006374
6375 tg3_flag_clear(tp, RESET_TASK_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006376}
6377
6378static void tg3_tx_timeout(struct net_device *dev)
6379{
6380 struct tg3 *tp = netdev_priv(dev);
6381
Michael Chanb0408752007-02-13 12:18:30 -08006382 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00006383 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00006384 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08006385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006386
Matt Carlsondb219972011-11-04 09:15:03 +00006387 tg3_reset_task_schedule(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006388}
6389
Michael Chanc58ec932005-09-17 00:46:27 -07006390/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
6391static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
6392{
6393 u32 base = (u32) mapping & 0xffffffff;
6394
Eric Dumazet807540b2010-09-23 05:40:09 +00006395 return (base > 0xffffdcc0) && (base + len + 8 < base);
Michael Chanc58ec932005-09-17 00:46:27 -07006396}
6397
Michael Chan72f2afb2006-03-06 19:28:35 -08006398/* Test for DMA addresses > 40-bit */
6399static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
6400 int len)
6401{
6402#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Joe Perches63c3a662011-04-26 08:12:10 +00006403 if (tg3_flag(tp, 40BIT_DMA_BUG))
Eric Dumazet807540b2010-09-23 05:40:09 +00006404 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08006405 return 0;
6406#else
6407 return 0;
6408#endif
6409}
6410
Matt Carlsond1a3b732011-07-27 14:20:51 +00006411static inline void tg3_tx_set_bd(struct tg3_tx_buffer_desc *txbd,
Matt Carlson92cd3a12011-07-27 14:20:47 +00006412 dma_addr_t mapping, u32 len, u32 flags,
6413 u32 mss, u32 vlan)
Matt Carlson2ffcc982011-05-19 12:12:44 +00006414{
Matt Carlson92cd3a12011-07-27 14:20:47 +00006415 txbd->addr_hi = ((u64) mapping >> 32);
6416 txbd->addr_lo = ((u64) mapping & 0xffffffff);
6417 txbd->len_flags = (len << TXD_LEN_SHIFT) | (flags & 0x0000ffff);
6418 txbd->vlan_tag = (mss << TXD_MSS_SHIFT) | (vlan << TXD_VLAN_TAG_SHIFT);
Matt Carlson2ffcc982011-05-19 12:12:44 +00006419}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006420
Matt Carlson84b67b22011-07-27 14:20:52 +00006421static bool tg3_tx_frag_set(struct tg3_napi *tnapi, u32 *entry, u32 *budget,
Matt Carlsond1a3b732011-07-27 14:20:51 +00006422 dma_addr_t map, u32 len, u32 flags,
6423 u32 mss, u32 vlan)
6424{
6425 struct tg3 *tp = tnapi->tp;
6426 bool hwbug = false;
6427
6428 if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
6429 hwbug = 1;
6430
6431 if (tg3_4g_overflow_test(map, len))
6432 hwbug = 1;
6433
6434 if (tg3_40bit_overflow_test(tp, map, len))
6435 hwbug = 1;
6436
Matt Carlsona4cb4282011-12-14 11:09:58 +00006437 if (tp->dma_limit) {
Matt Carlsonb9e45482011-11-04 09:14:59 +00006438 u32 prvidx = *entry;
Matt Carlsone31aa982011-07-27 14:20:53 +00006439 u32 tmp_flag = flags & ~TXD_FLAG_END;
Matt Carlsona4cb4282011-12-14 11:09:58 +00006440 while (len > tp->dma_limit && *budget) {
6441 u32 frag_len = tp->dma_limit;
6442 len -= tp->dma_limit;
Matt Carlsone31aa982011-07-27 14:20:53 +00006443
Matt Carlsonb9e45482011-11-04 09:14:59 +00006444 /* Avoid the 8byte DMA problem */
6445 if (len <= 8) {
Matt Carlsona4cb4282011-12-14 11:09:58 +00006446 len += tp->dma_limit / 2;
6447 frag_len = tp->dma_limit / 2;
Matt Carlsone31aa982011-07-27 14:20:53 +00006448 }
6449
Matt Carlsonb9e45482011-11-04 09:14:59 +00006450 tnapi->tx_buffers[*entry].fragmented = true;
6451
6452 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6453 frag_len, tmp_flag, mss, vlan);
6454 *budget -= 1;
6455 prvidx = *entry;
6456 *entry = NEXT_TX(*entry);
6457
Matt Carlsone31aa982011-07-27 14:20:53 +00006458 map += frag_len;
6459 }
6460
6461 if (len) {
6462 if (*budget) {
6463 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6464 len, flags, mss, vlan);
Matt Carlsonb9e45482011-11-04 09:14:59 +00006465 *budget -= 1;
Matt Carlsone31aa982011-07-27 14:20:53 +00006466 *entry = NEXT_TX(*entry);
6467 } else {
6468 hwbug = 1;
Matt Carlsonb9e45482011-11-04 09:14:59 +00006469 tnapi->tx_buffers[prvidx].fragmented = false;
Matt Carlsone31aa982011-07-27 14:20:53 +00006470 }
6471 }
6472 } else {
Matt Carlson84b67b22011-07-27 14:20:52 +00006473 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6474 len, flags, mss, vlan);
Matt Carlsone31aa982011-07-27 14:20:53 +00006475 *entry = NEXT_TX(*entry);
6476 }
Matt Carlsond1a3b732011-07-27 14:20:51 +00006477
6478 return hwbug;
6479}
6480
Matt Carlson0d681b22011-07-27 14:20:49 +00006481static void tg3_tx_skb_unmap(struct tg3_napi *tnapi, u32 entry, int last)
Matt Carlson432aa7e2011-05-19 12:12:45 +00006482{
6483 int i;
Matt Carlson0d681b22011-07-27 14:20:49 +00006484 struct sk_buff *skb;
Matt Carlsondf8944c2011-07-27 14:20:46 +00006485 struct tg3_tx_ring_info *txb = &tnapi->tx_buffers[entry];
Matt Carlson432aa7e2011-05-19 12:12:45 +00006486
Matt Carlson0d681b22011-07-27 14:20:49 +00006487 skb = txb->skb;
6488 txb->skb = NULL;
6489
Matt Carlson432aa7e2011-05-19 12:12:45 +00006490 pci_unmap_single(tnapi->tp->pdev,
6491 dma_unmap_addr(txb, mapping),
6492 skb_headlen(skb),
6493 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00006494
6495 while (txb->fragmented) {
6496 txb->fragmented = false;
6497 entry = NEXT_TX(entry);
6498 txb = &tnapi->tx_buffers[entry];
6499 }
6500
Matt Carlsonba1142e2011-11-04 09:15:00 +00006501 for (i = 0; i <= last; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00006502 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Matt Carlson432aa7e2011-05-19 12:12:45 +00006503
6504 entry = NEXT_TX(entry);
6505 txb = &tnapi->tx_buffers[entry];
6506
6507 pci_unmap_page(tnapi->tp->pdev,
6508 dma_unmap_addr(txb, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00006509 skb_frag_size(frag), 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 }
Matt Carlson432aa7e2011-05-19 12:12:45 +00006516 }
6517}
6518
Michael Chan72f2afb2006-03-06 19:28:35 -08006519/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006520static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
David S. Miller1805b2f2011-10-24 18:18:09 -04006521 struct sk_buff **pskb,
Matt Carlson84b67b22011-07-27 14:20:52 +00006522 u32 *entry, u32 *budget,
Matt Carlson92cd3a12011-07-27 14:20:47 +00006523 u32 base_flags, u32 mss, u32 vlan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006524{
Matt Carlson24f4efd2009-11-13 13:03:35 +00006525 struct tg3 *tp = tnapi->tp;
David S. Miller1805b2f2011-10-24 18:18:09 -04006526 struct sk_buff *new_skb, *skb = *pskb;
Michael Chanc58ec932005-09-17 00:46:27 -07006527 dma_addr_t new_addr = 0;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006528 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006529
Matt Carlson41588ba12008-04-19 18:12:33 -07006530 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
6531 new_skb = skb_copy(skb, GFP_ATOMIC);
6532 else {
6533 int more_headroom = 4 - ((unsigned long)skb->data & 3);
6534
6535 new_skb = skb_copy_expand(skb,
6536 skb_headroom(skb) + more_headroom,
6537 skb_tailroom(skb), GFP_ATOMIC);
6538 }
6539
Linus Torvalds1da177e2005-04-16 15:20:36 -07006540 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07006541 ret = -1;
6542 } else {
6543 /* New SKB is guaranteed to be linear. */
Alexander Duyckf4188d82009-12-02 16:48:38 +00006544 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
6545 PCI_DMA_TODEVICE);
6546 /* Make sure the mapping succeeded */
6547 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00006548 dev_kfree_skb(new_skb);
Michael Chanc58ec932005-09-17 00:46:27 -07006549 ret = -1;
Michael Chanc58ec932005-09-17 00:46:27 -07006550 } else {
Matt Carlsonb9e45482011-11-04 09:14:59 +00006551 u32 save_entry = *entry;
6552
Matt Carlson92cd3a12011-07-27 14:20:47 +00006553 base_flags |= TXD_FLAG_END;
6554
Matt Carlson84b67b22011-07-27 14:20:52 +00006555 tnapi->tx_buffers[*entry].skb = new_skb;
6556 dma_unmap_addr_set(&tnapi->tx_buffers[*entry],
Matt Carlson432aa7e2011-05-19 12:12:45 +00006557 mapping, new_addr);
6558
Matt Carlson84b67b22011-07-27 14:20:52 +00006559 if (tg3_tx_frag_set(tnapi, entry, budget, new_addr,
Matt Carlsond1a3b732011-07-27 14:20:51 +00006560 new_skb->len, base_flags,
6561 mss, vlan)) {
Matt Carlsonba1142e2011-11-04 09:15:00 +00006562 tg3_tx_skb_unmap(tnapi, save_entry, -1);
Matt Carlsond1a3b732011-07-27 14:20:51 +00006563 dev_kfree_skb(new_skb);
6564 ret = -1;
6565 }
Michael Chanc58ec932005-09-17 00:46:27 -07006566 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006567 }
6568
Linus Torvalds1da177e2005-04-16 15:20:36 -07006569 dev_kfree_skb(skb);
David S. Miller1805b2f2011-10-24 18:18:09 -04006570 *pskb = new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07006571 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006572}
6573
Matt Carlson2ffcc982011-05-19 12:12:44 +00006574static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07006575
6576/* Use GSO to workaround a rare TSO bug that may be triggered when the
6577 * TSO header is greater than 80 bytes.
6578 */
6579static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
6580{
6581 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006582 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07006583
6584 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006585 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07006586 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006587
6588 /* netif_tx_stop_queue() must be done before checking
6589 * checking tx index in tg3_tx_avail() below, because in
6590 * tg3_tx(), we update tx index before checking for
6591 * netif_tx_queue_stopped().
6592 */
6593 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006594 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08006595 return NETDEV_TX_BUSY;
6596
6597 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07006598 }
6599
6600 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07006601 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07006602 goto tg3_tso_bug_end;
6603
6604 do {
6605 nskb = segs;
6606 segs = segs->next;
6607 nskb->next = NULL;
Matt Carlson2ffcc982011-05-19 12:12:44 +00006608 tg3_start_xmit(nskb, tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07006609 } while (segs);
6610
6611tg3_tso_bug_end:
6612 dev_kfree_skb(skb);
6613
6614 return NETDEV_TX_OK;
6615}
Michael Chan52c0fd82006-06-29 20:15:54 -07006616
Michael Chan5a6f3072006-03-20 22:28:05 -08006617/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
Joe Perches63c3a662011-04-26 08:12:10 +00006618 * support TG3_FLAG_HW_TSO_1 or firmware TSO only.
Michael Chan5a6f3072006-03-20 22:28:05 -08006619 */
Matt Carlson2ffcc982011-05-19 12:12:44 +00006620static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08006621{
6622 struct tg3 *tp = netdev_priv(dev);
Matt Carlson92cd3a12011-07-27 14:20:47 +00006623 u32 len, entry, base_flags, mss, vlan = 0;
Matt Carlson84b67b22011-07-27 14:20:52 +00006624 u32 budget;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006625 int i = -1, would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07006626 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00006627 struct tg3_napi *tnapi;
6628 struct netdev_queue *txq;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006629 unsigned int last;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006630
Matt Carlson24f4efd2009-11-13 13:03:35 +00006631 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
6632 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Joe Perches63c3a662011-04-26 08:12:10 +00006633 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006634 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006635
Matt Carlson84b67b22011-07-27 14:20:52 +00006636 budget = tg3_tx_avail(tnapi);
6637
Michael Chan00b70502006-06-17 21:58:45 -07006638 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006639 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07006640 * interrupt. Furthermore, IRQ processing runs lockless so we have
6641 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07006642 */
Matt Carlson84b67b22011-07-27 14:20:52 +00006643 if (unlikely(budget <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006644 if (!netif_tx_queue_stopped(txq)) {
6645 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08006646
6647 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00006648 netdev_err(dev,
6649 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08006650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006651 return NETDEV_TX_BUSY;
6652 }
6653
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006654 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006655 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07006656 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006657 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00006658
Matt Carlsonbe98da62010-07-11 09:31:46 +00006659 mss = skb_shinfo(skb)->gso_size;
6660 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006661 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00006662 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006663
6664 if (skb_header_cloned(skb) &&
Eric Dumazet48855432011-10-24 07:53:03 +00006665 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
6666 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006667
Matt Carlson34195c32010-07-11 09:31:42 +00006668 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07006669 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006670
Matt Carlson02e96082010-09-15 08:59:59 +00006671 if (skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00006672 hdr_len = skb_headlen(skb) - ETH_HLEN;
6673 } else {
6674 u32 ip_tcp_len;
6675
6676 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
6677 hdr_len = ip_tcp_len + tcp_opt_len;
6678
6679 iph->check = 0;
6680 iph->tot_len = htons(mss + hdr_len);
6681 }
6682
Michael Chan52c0fd82006-06-29 20:15:54 -07006683 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Joe Perches63c3a662011-04-26 08:12:10 +00006684 tg3_flag(tp, TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00006685 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07006686
Linus Torvalds1da177e2005-04-16 15:20:36 -07006687 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
6688 TXD_FLAG_CPU_POST_DMA);
6689
Joe Perches63c3a662011-04-26 08:12:10 +00006690 if (tg3_flag(tp, HW_TSO_1) ||
6691 tg3_flag(tp, HW_TSO_2) ||
6692 tg3_flag(tp, HW_TSO_3)) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006693 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006694 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006695 } else
6696 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
6697 iph->daddr, 0,
6698 IPPROTO_TCP,
6699 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006700
Joe Perches63c3a662011-04-26 08:12:10 +00006701 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlson615774f2009-11-13 13:03:39 +00006702 mss |= (hdr_len & 0xc) << 12;
6703 if (hdr_len & 0x10)
6704 base_flags |= 0x00000010;
6705 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +00006706 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006707 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +00006708 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006709 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006710 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006711 int tsflags;
6712
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006713 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006714 mss |= (tsflags << 11);
6715 }
6716 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006717 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006718 int tsflags;
6719
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006720 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006721 base_flags |= tsflags << 12;
6722 }
6723 }
6724 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00006725
Matt Carlson93a700a2011-08-31 11:44:54 +00006726 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
6727 !mss && skb->len > VLAN_ETH_FRAME_LEN)
6728 base_flags |= TXD_FLAG_JMB_PKT;
6729
Matt Carlson92cd3a12011-07-27 14:20:47 +00006730 if (vlan_tx_tag_present(skb)) {
6731 base_flags |= TXD_FLAG_VLAN;
6732 vlan = vlan_tx_tag_get(skb);
6733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006734
Alexander Duyckf4188d82009-12-02 16:48:38 +00006735 len = skb_headlen(skb);
6736
6737 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
Eric Dumazet48855432011-10-24 07:53:03 +00006738 if (pci_dma_mapping_error(tp->pdev, mapping))
6739 goto drop;
6740
David S. Miller90079ce2008-09-11 04:52:51 -07006741
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006742 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006743 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006744
6745 would_hit_hwbug = 0;
6746
Joe Perches63c3a662011-04-26 08:12:10 +00006747 if (tg3_flag(tp, 5701_DMA_BUG))
Michael Chanc58ec932005-09-17 00:46:27 -07006748 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006749
Matt Carlson84b67b22011-07-27 14:20:52 +00006750 if (tg3_tx_frag_set(tnapi, &entry, &budget, mapping, len, base_flags |
Matt Carlsond1a3b732011-07-27 14:20:51 +00006751 ((skb_shinfo(skb)->nr_frags == 0) ? TXD_FLAG_END : 0),
Matt Carlsonba1142e2011-11-04 09:15:00 +00006752 mss, vlan)) {
Matt Carlsond1a3b732011-07-27 14:20:51 +00006753 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006754 /* Now loop through additional data fragments, and queue them. */
Matt Carlsonba1142e2011-11-04 09:15:00 +00006755 } else if (skb_shinfo(skb)->nr_frags > 0) {
Matt Carlson92cd3a12011-07-27 14:20:47 +00006756 u32 tmp_mss = mss;
6757
6758 if (!tg3_flag(tp, HW_TSO_1) &&
6759 !tg3_flag(tp, HW_TSO_2) &&
6760 !tg3_flag(tp, HW_TSO_3))
6761 tmp_mss = 0;
6762
Linus Torvalds1da177e2005-04-16 15:20:36 -07006763 last = skb_shinfo(skb)->nr_frags - 1;
6764 for (i = 0; i <= last; i++) {
6765 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6766
Eric Dumazet9e903e02011-10-18 21:00:24 +00006767 len = skb_frag_size(frag);
Ian Campbelldc234d02011-08-24 22:28:11 +00006768 mapping = skb_frag_dma_map(&tp->pdev->dev, frag, 0,
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01006769 len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006770
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006771 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006772 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00006773 mapping);
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01006774 if (dma_mapping_error(&tp->pdev->dev, mapping))
Alexander Duyckf4188d82009-12-02 16:48:38 +00006775 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006776
Matt Carlsonb9e45482011-11-04 09:14:59 +00006777 if (!budget ||
6778 tg3_tx_frag_set(tnapi, &entry, &budget, mapping,
Matt Carlson84b67b22011-07-27 14:20:52 +00006779 len, base_flags |
6780 ((i == last) ? TXD_FLAG_END : 0),
Matt Carlsonb9e45482011-11-04 09:14:59 +00006781 tmp_mss, vlan)) {
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006782 would_hit_hwbug = 1;
Matt Carlsonb9e45482011-11-04 09:14:59 +00006783 break;
6784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006785 }
6786 }
6787
6788 if (would_hit_hwbug) {
Matt Carlson0d681b22011-07-27 14:20:49 +00006789 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006790
6791 /* If the workaround fails due to memory/mapping
6792 * failure, silently drop this packet.
6793 */
Matt Carlson84b67b22011-07-27 14:20:52 +00006794 entry = tnapi->tx_prod;
6795 budget = tg3_tx_avail(tnapi);
David S. Miller1805b2f2011-10-24 18:18:09 -04006796 if (tigon3_dma_hwbug_workaround(tnapi, &skb, &entry, &budget,
Matt Carlson84b67b22011-07-27 14:20:52 +00006797 base_flags, mss, vlan))
Eric Dumazet48855432011-10-24 07:53:03 +00006798 goto drop_nofree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006799 }
6800
Richard Cochrand515b452011-06-19 03:31:41 +00006801 skb_tx_timestamp(skb);
Tom Herbert298376d2011-11-28 16:33:30 +00006802 netdev_sent_queue(tp->dev, skb->len);
Richard Cochrand515b452011-06-19 03:31:41 +00006803
Linus Torvalds1da177e2005-04-16 15:20:36 -07006804 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006805 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006806
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006807 tnapi->tx_prod = entry;
6808 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006809 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006810
6811 /* netif_tx_stop_queue() must be done before checking
6812 * checking tx index in tg3_tx_avail() below, because in
6813 * tg3_tx(), we update tx index before checking for
6814 * netif_tx_queue_stopped().
6815 */
6816 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006817 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006818 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07006819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006820
Eric Dumazetcdd0db02009-05-28 00:00:41 +00006821 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006822 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006823
6824dma_error:
Matt Carlsonba1142e2011-11-04 09:15:00 +00006825 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, --i);
Matt Carlson432aa7e2011-05-19 12:12:45 +00006826 tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
Eric Dumazet48855432011-10-24 07:53:03 +00006827drop:
6828 dev_kfree_skb(skb);
6829drop_nofree:
6830 tp->tx_dropped++;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006831 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006832}
6833
Matt Carlson6e01b202011-08-19 13:58:20 +00006834static void tg3_mac_loopback(struct tg3 *tp, bool enable)
6835{
6836 if (enable) {
6837 tp->mac_mode &= ~(MAC_MODE_HALF_DUPLEX |
6838 MAC_MODE_PORT_MODE_MASK);
6839
6840 tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
6841
6842 if (!tg3_flag(tp, 5705_PLUS))
6843 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
6844
6845 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
6846 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
6847 else
6848 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
6849 } else {
6850 tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
6851
6852 if (tg3_flag(tp, 5705_PLUS) ||
6853 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) ||
6854 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
6855 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
6856 }
6857
6858 tw32(MAC_MODE, tp->mac_mode);
6859 udelay(40);
6860}
6861
Matt Carlson941ec902011-08-19 13:58:23 +00006862static int tg3_phy_lpbk_set(struct tg3 *tp, u32 speed, bool extlpbk)
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006863{
Matt Carlson941ec902011-08-19 13:58:23 +00006864 u32 val, bmcr, mac_mode, ptest = 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006865
6866 tg3_phy_toggle_apd(tp, false);
6867 tg3_phy_toggle_automdix(tp, 0);
6868
Matt Carlson941ec902011-08-19 13:58:23 +00006869 if (extlpbk && tg3_phy_set_extloopbk(tp))
6870 return -EIO;
6871
6872 bmcr = BMCR_FULLDPLX;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006873 switch (speed) {
6874 case SPEED_10:
6875 break;
6876 case SPEED_100:
6877 bmcr |= BMCR_SPEED100;
6878 break;
6879 case SPEED_1000:
6880 default:
6881 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
6882 speed = SPEED_100;
6883 bmcr |= BMCR_SPEED100;
6884 } else {
6885 speed = SPEED_1000;
6886 bmcr |= BMCR_SPEED1000;
6887 }
6888 }
6889
Matt Carlson941ec902011-08-19 13:58:23 +00006890 if (extlpbk) {
6891 if (!(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
6892 tg3_readphy(tp, MII_CTRL1000, &val);
6893 val |= CTL1000_AS_MASTER |
6894 CTL1000_ENABLE_MASTER;
6895 tg3_writephy(tp, MII_CTRL1000, val);
6896 } else {
6897 ptest = MII_TG3_FET_PTEST_TRIM_SEL |
6898 MII_TG3_FET_PTEST_TRIM_2;
6899 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest);
6900 }
6901 } else
6902 bmcr |= BMCR_LOOPBACK;
6903
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006904 tg3_writephy(tp, MII_BMCR, bmcr);
6905
6906 /* The write needs to be flushed for the FETs */
6907 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
6908 tg3_readphy(tp, MII_BMCR, &bmcr);
6909
6910 udelay(40);
6911
6912 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
6913 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Matt Carlson941ec902011-08-19 13:58:23 +00006914 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest |
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006915 MII_TG3_FET_PTEST_FRC_TX_LINK |
6916 MII_TG3_FET_PTEST_FRC_TX_LOCK);
6917
6918 /* The write needs to be flushed for the AC131 */
6919 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
6920 }
6921
6922 /* Reset to prevent losing 1st rx packet intermittently */
6923 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
6924 tg3_flag(tp, 5780_CLASS)) {
6925 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
6926 udelay(10);
6927 tw32_f(MAC_RX_MODE, tp->rx_mode);
6928 }
6929
6930 mac_mode = tp->mac_mode &
6931 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
6932 if (speed == SPEED_1000)
6933 mac_mode |= MAC_MODE_PORT_MODE_GMII;
6934 else
6935 mac_mode |= MAC_MODE_PORT_MODE_MII;
6936
6937 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
6938 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
6939
6940 if (masked_phy_id == TG3_PHY_ID_BCM5401)
6941 mac_mode &= ~MAC_MODE_LINK_POLARITY;
6942 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
6943 mac_mode |= MAC_MODE_LINK_POLARITY;
6944
6945 tg3_writephy(tp, MII_TG3_EXT_CTRL,
6946 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
6947 }
6948
6949 tw32(MAC_MODE, mac_mode);
6950 udelay(40);
Matt Carlson941ec902011-08-19 13:58:23 +00006951
6952 return 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006953}
6954
Michał Mirosławc8f44af2011-11-15 15:29:55 +00006955static void tg3_set_loopback(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006956{
6957 struct tg3 *tp = netdev_priv(dev);
6958
6959 if (features & NETIF_F_LOOPBACK) {
6960 if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
6961 return;
6962
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006963 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00006964 tg3_mac_loopback(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006965 netif_carrier_on(tp->dev);
6966 spin_unlock_bh(&tp->lock);
6967 netdev_info(dev, "Internal MAC loopback mode enabled.\n");
6968 } else {
6969 if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
6970 return;
6971
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006972 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00006973 tg3_mac_loopback(tp, false);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006974 /* Force link status check */
6975 tg3_setup_phy(tp, 1);
6976 spin_unlock_bh(&tp->lock);
6977 netdev_info(dev, "Internal MAC loopback mode disabled.\n");
6978 }
6979}
6980
Michał Mirosławc8f44af2011-11-15 15:29:55 +00006981static netdev_features_t tg3_fix_features(struct net_device *dev,
6982 netdev_features_t features)
Michał Mirosławdc668912011-04-07 03:35:07 +00006983{
6984 struct tg3 *tp = netdev_priv(dev);
6985
Joe Perches63c3a662011-04-26 08:12:10 +00006986 if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
Michał Mirosławdc668912011-04-07 03:35:07 +00006987 features &= ~NETIF_F_ALL_TSO;
6988
6989 return features;
6990}
6991
Michał Mirosławc8f44af2011-11-15 15:29:55 +00006992static int tg3_set_features(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006993{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00006994 netdev_features_t changed = dev->features ^ features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006995
6996 if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
6997 tg3_set_loopback(dev, features);
6998
6999 return 0;
7000}
7001
Linus Torvalds1da177e2005-04-16 15:20:36 -07007002static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
7003 int new_mtu)
7004{
7005 dev->mtu = new_mtu;
7006
Michael Chanef7f5ec2005-07-25 12:32:25 -07007007 if (new_mtu > ETH_DATA_LEN) {
Joe Perches63c3a662011-04-26 08:12:10 +00007008 if (tg3_flag(tp, 5780_CLASS)) {
Michał Mirosławdc668912011-04-07 03:35:07 +00007009 netdev_update_features(dev);
Joe Perches63c3a662011-04-26 08:12:10 +00007010 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson859a588792010-04-05 10:19:28 +00007011 } else {
Joe Perches63c3a662011-04-26 08:12:10 +00007012 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Matt Carlson859a588792010-04-05 10:19:28 +00007013 }
Michael Chanef7f5ec2005-07-25 12:32:25 -07007014 } else {
Joe Perches63c3a662011-04-26 08:12:10 +00007015 if (tg3_flag(tp, 5780_CLASS)) {
7016 tg3_flag_set(tp, TSO_CAPABLE);
Michał Mirosławdc668912011-04-07 03:35:07 +00007017 netdev_update_features(dev);
7018 }
Joe Perches63c3a662011-04-26 08:12:10 +00007019 tg3_flag_clear(tp, JUMBO_RING_ENABLE);
Michael Chanef7f5ec2005-07-25 12:32:25 -07007020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007021}
7022
7023static int tg3_change_mtu(struct net_device *dev, int new_mtu)
7024{
7025 struct tg3 *tp = netdev_priv(dev);
Michael Chanb9ec6c12006-07-25 16:37:27 -07007026 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007027
7028 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
7029 return -EINVAL;
7030
7031 if (!netif_running(dev)) {
7032 /* We'll just catch it later when the
7033 * device is up'd.
7034 */
7035 tg3_set_mtu(dev, tp, new_mtu);
7036 return 0;
7037 }
7038
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07007039 tg3_phy_stop(tp);
7040
Linus Torvalds1da177e2005-04-16 15:20:36 -07007041 tg3_netif_stop(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07007042
7043 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007044
Michael Chan944d9802005-05-29 14:57:48 -07007045 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007046
7047 tg3_set_mtu(dev, tp, new_mtu);
7048
Michael Chanb9ec6c12006-07-25 16:37:27 -07007049 err = tg3_restart_hw(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007050
Michael Chanb9ec6c12006-07-25 16:37:27 -07007051 if (!err)
7052 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007053
David S. Millerf47c11e2005-06-24 20:18:35 -07007054 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007055
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07007056 if (!err)
7057 tg3_phy_start(tp);
7058
Michael Chanb9ec6c12006-07-25 16:37:27 -07007059 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007060}
7061
Matt Carlson21f581a2009-08-28 14:00:25 +00007062static void tg3_rx_prodring_free(struct tg3 *tp,
7063 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007064{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007065 int i;
7066
Matt Carlson8fea32b2010-09-15 08:59:58 +00007067 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007068 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00007069 i = (i + 1) & tp->rx_std_ring_mask)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007070 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007071 tp->rx_pkt_map_sz);
7072
Joe Perches63c3a662011-04-26 08:12:10 +00007073 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007074 for (i = tpr->rx_jmb_cons_idx;
7075 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00007076 i = (i + 1) & tp->rx_jmb_ring_mask) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007077 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007078 TG3_RX_JMB_MAP_SZ);
7079 }
7080 }
7081
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007082 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007084
Matt Carlson2c49a442010-09-30 10:34:35 +00007085 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007086 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007087 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007088
Joe Perches63c3a662011-04-26 08:12:10 +00007089 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007090 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007091 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007092 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007093 }
7094}
7095
Matt Carlsonc6cdf432010-04-05 10:19:26 +00007096/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007097 *
7098 * The chip has been shut down and the driver detached from
7099 * the networking, so no interrupts or new tx packets will
7100 * end up in the driver. tp->{tx,}lock are held and thus
7101 * we may not sleep.
7102 */
Matt Carlson21f581a2009-08-28 14:00:25 +00007103static int tg3_rx_prodring_alloc(struct tg3 *tp,
7104 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007105{
Matt Carlson287be122009-08-28 13:58:46 +00007106 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007107
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007108 tpr->rx_std_cons_idx = 0;
7109 tpr->rx_std_prod_idx = 0;
7110 tpr->rx_jmb_cons_idx = 0;
7111 tpr->rx_jmb_prod_idx = 0;
7112
Matt Carlson8fea32b2010-09-15 08:59:58 +00007113 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007114 memset(&tpr->rx_std_buffers[0], 0,
7115 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00007116 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007117 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00007118 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007119 goto done;
7120 }
7121
Linus Torvalds1da177e2005-04-16 15:20:36 -07007122 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00007123 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007124
Matt Carlson287be122009-08-28 13:58:46 +00007125 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00007126 if (tg3_flag(tp, 5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00007127 tp->dev->mtu > ETH_DATA_LEN)
7128 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
7129 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad42005-07-25 12:31:17 -07007130
Linus Torvalds1da177e2005-04-16 15:20:36 -07007131 /* Initialize invariants of the rings, we only set this
7132 * stuff once. This works because the card does not
7133 * write into the rx buffer posting rings.
7134 */
Matt Carlson2c49a442010-09-30 10:34:35 +00007135 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007136 struct tg3_rx_buffer_desc *rxd;
7137
Matt Carlson21f581a2009-08-28 14:00:25 +00007138 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00007139 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007140 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
7141 rxd->opaque = (RXD_OPAQUE_RING_STD |
7142 (i << RXD_OPAQUE_INDEX_SHIFT));
7143 }
7144
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007145 /* Now allocate fresh SKBs for each rx ring. */
7146 for (i = 0; i < tp->rx_pending; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007147 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_STD, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007148 netdev_warn(tp->dev,
7149 "Using a smaller RX standard ring. Only "
7150 "%d out of %d buffers were allocated "
7151 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007152 if (i == 0)
7153 goto initfail;
7154 tp->rx_pending = i;
7155 break;
7156 }
7157 }
7158
Joe Perches63c3a662011-04-26 08:12:10 +00007159 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007160 goto done;
7161
Matt Carlson2c49a442010-09-30 10:34:35 +00007162 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007163
Joe Perches63c3a662011-04-26 08:12:10 +00007164 if (!tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson0d86df82010-02-17 15:17:00 +00007165 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007166
Matt Carlson2c49a442010-09-30 10:34:35 +00007167 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00007168 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007169
Matt Carlson0d86df82010-02-17 15:17:00 +00007170 rxd = &tpr->rx_jmb[i].std;
7171 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
7172 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
7173 RXD_FLAG_JUMBO;
7174 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
7175 (i << RXD_OPAQUE_INDEX_SHIFT));
7176 }
7177
7178 for (i = 0; i < tp->rx_jumbo_pending; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007179 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_JUMBO, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007180 netdev_warn(tp->dev,
7181 "Using a smaller RX jumbo ring. Only %d "
7182 "out of %d buffers were allocated "
7183 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00007184 if (i == 0)
7185 goto initfail;
7186 tp->rx_jumbo_pending = i;
7187 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007188 }
7189 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007190
7191done:
Michael Chan32d8c572006-07-25 16:38:29 -07007192 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007193
7194initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00007195 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007196 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007197}
7198
Matt Carlson21f581a2009-08-28 14:00:25 +00007199static void tg3_rx_prodring_fini(struct tg3 *tp,
7200 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007201{
Matt Carlson21f581a2009-08-28 14:00:25 +00007202 kfree(tpr->rx_std_buffers);
7203 tpr->rx_std_buffers = NULL;
7204 kfree(tpr->rx_jmb_buffers);
7205 tpr->rx_jmb_buffers = NULL;
7206 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007207 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
7208 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00007209 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007210 }
Matt Carlson21f581a2009-08-28 14:00:25 +00007211 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007212 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
7213 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00007214 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007215 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007216}
7217
Matt Carlson21f581a2009-08-28 14:00:25 +00007218static int tg3_rx_prodring_init(struct tg3 *tp,
7219 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007220{
Matt Carlson2c49a442010-09-30 10:34:35 +00007221 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
7222 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007223 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007224 return -ENOMEM;
7225
Matt Carlson4bae65c2010-11-24 08:31:52 +00007226 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
7227 TG3_RX_STD_RING_BYTES(tp),
7228 &tpr->rx_std_mapping,
7229 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007230 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007231 goto err_out;
7232
Joe Perches63c3a662011-04-26 08:12:10 +00007233 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007234 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00007235 GFP_KERNEL);
7236 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007237 goto err_out;
7238
Matt Carlson4bae65c2010-11-24 08:31:52 +00007239 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
7240 TG3_RX_JMB_RING_BYTES(tp),
7241 &tpr->rx_jmb_mapping,
7242 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007243 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007244 goto err_out;
7245 }
7246
7247 return 0;
7248
7249err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00007250 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007251 return -ENOMEM;
7252}
7253
7254/* Free up pending packets in all rx/tx rings.
7255 *
7256 * The chip has been shut down and the driver detached from
7257 * the networking, so no interrupts or new tx packets will
7258 * end up in the driver. tp->{tx,}lock is not held and we are not
7259 * in an interrupt context and thus may sleep.
7260 */
7261static void tg3_free_rings(struct tg3 *tp)
7262{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007263 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007264
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007265 for (j = 0; j < tp->irq_cnt; j++) {
7266 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007267
Matt Carlson8fea32b2010-09-15 08:59:58 +00007268 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00007269
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007270 if (!tnapi->tx_buffers)
7271 continue;
7272
Matt Carlson0d681b22011-07-27 14:20:49 +00007273 for (i = 0; i < TG3_TX_RING_SIZE; i++) {
7274 struct sk_buff *skb = tnapi->tx_buffers[i].skb;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007275
Matt Carlson0d681b22011-07-27 14:20:49 +00007276 if (!skb)
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007277 continue;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007278
Matt Carlsonba1142e2011-11-04 09:15:00 +00007279 tg3_tx_skb_unmap(tnapi, i,
7280 skb_shinfo(skb)->nr_frags - 1);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007281
7282 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007283 }
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007284 }
Tom Herbert298376d2011-11-28 16:33:30 +00007285 netdev_reset_queue(tp->dev);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007286}
7287
7288/* Initialize tx/rx rings for packet processing.
7289 *
7290 * The chip has been shut down and the driver detached from
7291 * the networking, so no interrupts or new tx packets will
7292 * end up in the driver. tp->{tx,}lock are held and thus
7293 * we may not sleep.
7294 */
7295static int tg3_init_rings(struct tg3 *tp)
7296{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007297 int i;
Matt Carlson72334482009-08-28 14:03:01 +00007298
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007299 /* Free up all the SKBs. */
7300 tg3_free_rings(tp);
7301
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007302 for (i = 0; i < tp->irq_cnt; i++) {
7303 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007304
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007305 tnapi->last_tag = 0;
7306 tnapi->last_irq_tag = 0;
7307 tnapi->hw_status->status = 0;
7308 tnapi->hw_status->status_tag = 0;
7309 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7310
7311 tnapi->tx_prod = 0;
7312 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007313 if (tnapi->tx_ring)
7314 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007315
7316 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007317 if (tnapi->rx_rcb)
7318 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007319
Matt Carlson8fea32b2010-09-15 08:59:58 +00007320 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00007321 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007322 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00007323 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007324 }
Matt Carlson72334482009-08-28 14:03:01 +00007325
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007326 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007327}
7328
7329/*
7330 * Must not be invoked with interrupt sources disabled and
7331 * the hardware shutdown down.
7332 */
7333static void tg3_free_consistent(struct tg3 *tp)
7334{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007335 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00007336
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007337 for (i = 0; i < tp->irq_cnt; i++) {
7338 struct tg3_napi *tnapi = &tp->napi[i];
7339
7340 if (tnapi->tx_ring) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007341 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007342 tnapi->tx_ring, tnapi->tx_desc_mapping);
7343 tnapi->tx_ring = NULL;
7344 }
7345
7346 kfree(tnapi->tx_buffers);
7347 tnapi->tx_buffers = NULL;
7348
7349 if (tnapi->rx_rcb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007350 dma_free_coherent(&tp->pdev->dev,
7351 TG3_RX_RCB_RING_BYTES(tp),
7352 tnapi->rx_rcb,
7353 tnapi->rx_rcb_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007354 tnapi->rx_rcb = NULL;
7355 }
7356
Matt Carlson8fea32b2010-09-15 08:59:58 +00007357 tg3_rx_prodring_fini(tp, &tnapi->prodring);
7358
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007359 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007360 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
7361 tnapi->hw_status,
7362 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007363 tnapi->hw_status = NULL;
7364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007365 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007366
Linus Torvalds1da177e2005-04-16 15:20:36 -07007367 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007368 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
7369 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007370 tp->hw_stats = NULL;
7371 }
7372}
7373
7374/*
7375 * Must not be invoked with interrupt sources disabled and
7376 * the hardware shutdown down. Can sleep.
7377 */
7378static int tg3_alloc_consistent(struct tg3 *tp)
7379{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007380 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00007381
Matt Carlson4bae65c2010-11-24 08:31:52 +00007382 tp->hw_stats = dma_alloc_coherent(&tp->pdev->dev,
7383 sizeof(struct tg3_hw_stats),
7384 &tp->stats_mapping,
7385 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007386 if (!tp->hw_stats)
7387 goto err_out;
7388
Linus Torvalds1da177e2005-04-16 15:20:36 -07007389 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
7390
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007391 for (i = 0; i < tp->irq_cnt; i++) {
7392 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007393 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007394
Matt Carlson4bae65c2010-11-24 08:31:52 +00007395 tnapi->hw_status = dma_alloc_coherent(&tp->pdev->dev,
7396 TG3_HW_STATUS_SIZE,
7397 &tnapi->status_mapping,
7398 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007399 if (!tnapi->hw_status)
7400 goto err_out;
7401
7402 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007403 sblk = tnapi->hw_status;
7404
Matt Carlson8fea32b2010-09-15 08:59:58 +00007405 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
7406 goto err_out;
7407
Matt Carlson19cfaec2009-12-03 08:36:20 +00007408 /* If multivector TSS is enabled, vector 0 does not handle
7409 * tx interrupts. Don't allocate any resources for it.
7410 */
Joe Perches63c3a662011-04-26 08:12:10 +00007411 if ((!i && !tg3_flag(tp, ENABLE_TSS)) ||
7412 (i && tg3_flag(tp, ENABLE_TSS))) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00007413 tnapi->tx_buffers = kzalloc(
7414 sizeof(struct tg3_tx_ring_info) *
7415 TG3_TX_RING_SIZE, GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007416 if (!tnapi->tx_buffers)
7417 goto err_out;
7418
Matt Carlson4bae65c2010-11-24 08:31:52 +00007419 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
7420 TG3_TX_RING_BYTES,
7421 &tnapi->tx_desc_mapping,
7422 GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007423 if (!tnapi->tx_ring)
7424 goto err_out;
7425 }
7426
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007427 /*
7428 * When RSS is enabled, the status block format changes
7429 * slightly. The "rx_jumbo_consumer", "reserved",
7430 * and "rx_mini_consumer" members get mapped to the
7431 * other three rx return ring producer indexes.
7432 */
7433 switch (i) {
7434 default:
7435 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
7436 break;
7437 case 2:
7438 tnapi->rx_rcb_prod_idx = &sblk->rx_jumbo_consumer;
7439 break;
7440 case 3:
7441 tnapi->rx_rcb_prod_idx = &sblk->reserved;
7442 break;
7443 case 4:
7444 tnapi->rx_rcb_prod_idx = &sblk->rx_mini_consumer;
7445 break;
7446 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007447
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007448 /*
7449 * If multivector RSS is enabled, vector 0 does not handle
7450 * rx or tx interrupts. Don't allocate any resources for it.
7451 */
Joe Perches63c3a662011-04-26 08:12:10 +00007452 if (!i && tg3_flag(tp, ENABLE_RSS))
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007453 continue;
7454
Matt Carlson4bae65c2010-11-24 08:31:52 +00007455 tnapi->rx_rcb = dma_alloc_coherent(&tp->pdev->dev,
7456 TG3_RX_RCB_RING_BYTES(tp),
7457 &tnapi->rx_rcb_mapping,
7458 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007459 if (!tnapi->rx_rcb)
7460 goto err_out;
7461
7462 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007463 }
7464
Linus Torvalds1da177e2005-04-16 15:20:36 -07007465 return 0;
7466
7467err_out:
7468 tg3_free_consistent(tp);
7469 return -ENOMEM;
7470}
7471
7472#define MAX_WAIT_CNT 1000
7473
7474/* To stop a block, clear the enable bit and poll till it
7475 * clears. tp->lock is held.
7476 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007477static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007478{
7479 unsigned int i;
7480 u32 val;
7481
Joe Perches63c3a662011-04-26 08:12:10 +00007482 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007483 switch (ofs) {
7484 case RCVLSC_MODE:
7485 case DMAC_MODE:
7486 case MBFREE_MODE:
7487 case BUFMGR_MODE:
7488 case MEMARB_MODE:
7489 /* We can't enable/disable these bits of the
7490 * 5705/5750, just say success.
7491 */
7492 return 0;
7493
7494 default:
7495 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007497 }
7498
7499 val = tr32(ofs);
7500 val &= ~enable_bit;
7501 tw32_f(ofs, val);
7502
7503 for (i = 0; i < MAX_WAIT_CNT; i++) {
7504 udelay(100);
7505 val = tr32(ofs);
7506 if ((val & enable_bit) == 0)
7507 break;
7508 }
7509
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007510 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00007511 dev_err(&tp->pdev->dev,
7512 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
7513 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007514 return -ENODEV;
7515 }
7516
7517 return 0;
7518}
7519
7520/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007521static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007522{
7523 int i, err;
7524
7525 tg3_disable_ints(tp);
7526
7527 tp->rx_mode &= ~RX_MODE_ENABLE;
7528 tw32_f(MAC_RX_MODE, tp->rx_mode);
7529 udelay(10);
7530
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007531 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
7532 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
7533 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
7534 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
7535 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
7536 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007537
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007538 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
7539 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
7540 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
7541 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
7542 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
7543 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
7544 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007545
7546 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
7547 tw32_f(MAC_MODE, tp->mac_mode);
7548 udelay(40);
7549
7550 tp->tx_mode &= ~TX_MODE_ENABLE;
7551 tw32_f(MAC_TX_MODE, tp->tx_mode);
7552
7553 for (i = 0; i < MAX_WAIT_CNT; i++) {
7554 udelay(100);
7555 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
7556 break;
7557 }
7558 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00007559 dev_err(&tp->pdev->dev,
7560 "%s timed out, TX_MODE_ENABLE will not clear "
7561 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07007562 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007563 }
7564
Michael Chane6de8ad2005-05-05 14:42:41 -07007565 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007566 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
7567 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007568
7569 tw32(FTQ_RESET, 0xffffffff);
7570 tw32(FTQ_RESET, 0x00000000);
7571
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007572 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
7573 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007574
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007575 for (i = 0; i < tp->irq_cnt; i++) {
7576 struct tg3_napi *tnapi = &tp->napi[i];
7577 if (tnapi->hw_status)
7578 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007580
Linus Torvalds1da177e2005-04-16 15:20:36 -07007581 return err;
7582}
7583
Michael Chanee6a99b2007-07-18 21:49:10 -07007584/* Save PCI command register before chip reset */
7585static void tg3_save_pci_state(struct tg3 *tp)
7586{
Matt Carlson8a6eac92007-10-21 16:17:55 -07007587 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007588}
7589
7590/* Restore PCI state after chip reset */
7591static void tg3_restore_pci_state(struct tg3 *tp)
7592{
7593 u32 val;
7594
7595 /* Re-enable indirect register accesses. */
7596 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
7597 tp->misc_host_ctrl);
7598
7599 /* Set MAX PCI retry to zero. */
7600 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
7601 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007602 tg3_flag(tp, PCIX_MODE))
Michael Chanee6a99b2007-07-18 21:49:10 -07007603 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07007604 /* Allow reads and writes to the APE register and memory space. */
Joe Perches63c3a662011-04-26 08:12:10 +00007605 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07007606 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00007607 PCISTATE_ALLOW_APE_SHMEM_WR |
7608 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07007609 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
7610
Matt Carlson8a6eac92007-10-21 16:17:55 -07007611 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007612
Matt Carlson2c55a3d2011-11-28 09:41:04 +00007613 if (!tg3_flag(tp, PCI_EXPRESS)) {
7614 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
7615 tp->pci_cacheline_sz);
7616 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
7617 tp->pci_lat_timer);
Michael Chan114342f2007-10-15 02:12:26 -07007618 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08007619
Michael Chanee6a99b2007-07-18 21:49:10 -07007620 /* Make sure PCI-X relaxed ordering bit is clear. */
Joe Perches63c3a662011-04-26 08:12:10 +00007621 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07007622 u16 pcix_cmd;
7623
7624 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7625 &pcix_cmd);
7626 pcix_cmd &= ~PCI_X_CMD_ERO;
7627 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7628 pcix_cmd);
7629 }
Michael Chanee6a99b2007-07-18 21:49:10 -07007630
Joe Perches63c3a662011-04-26 08:12:10 +00007631 if (tg3_flag(tp, 5780_CLASS)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007632
7633 /* Chip reset on 5780 will reset MSI enable bit,
7634 * so need to restore it.
7635 */
Joe Perches63c3a662011-04-26 08:12:10 +00007636 if (tg3_flag(tp, USING_MSI)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007637 u16 ctrl;
7638
7639 pci_read_config_word(tp->pdev,
7640 tp->msi_cap + PCI_MSI_FLAGS,
7641 &ctrl);
7642 pci_write_config_word(tp->pdev,
7643 tp->msi_cap + PCI_MSI_FLAGS,
7644 ctrl | PCI_MSI_FLAGS_ENABLE);
7645 val = tr32(MSGINT_MODE);
7646 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
7647 }
7648 }
7649}
7650
Linus Torvalds1da177e2005-04-16 15:20:36 -07007651/* tp->lock is held. */
7652static int tg3_chip_reset(struct tg3 *tp)
7653{
7654 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07007655 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00007656 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007657
David S. Millerf49639e2006-06-09 11:58:36 -07007658 tg3_nvram_lock(tp);
7659
Matt Carlson77b483f2008-08-15 14:07:24 -07007660 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
7661
David S. Millerf49639e2006-06-09 11:58:36 -07007662 /* No matching tg3_nvram_unlock() after this because
7663 * chip reset below will undo the nvram lock.
7664 */
7665 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007666
Michael Chanee6a99b2007-07-18 21:49:10 -07007667 /* GRC_MISC_CFG core clock reset will clear the memory
7668 * enable bit in PCI register 4 and the MSI enable bit
7669 * on some chips, so we save relevant registers here.
7670 */
7671 tg3_save_pci_state(tp);
7672
Michael Chand9ab5ad12006-03-20 22:27:35 -08007673 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Joe Perches63c3a662011-04-26 08:12:10 +00007674 tg3_flag(tp, 5755_PLUS))
Michael Chand9ab5ad12006-03-20 22:27:35 -08007675 tw32(GRC_FASTBOOT_PC, 0);
7676
Linus Torvalds1da177e2005-04-16 15:20:36 -07007677 /*
7678 * We must avoid the readl() that normally takes place.
7679 * It locks machines, causes machine checks, and other
7680 * fun things. So, temporarily disable the 5701
7681 * hardware workaround, while we do the reset.
7682 */
Michael Chan1ee582d2005-08-09 20:16:46 -07007683 write_op = tp->write32;
7684 if (write_op == tg3_write_flush_reg32)
7685 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007686
Michael Chand18edcb2007-03-24 20:57:11 -07007687 /* Prevent the irq handler from reading or writing PCI registers
7688 * during chip reset when the memory enable bit in the PCI command
7689 * register may be cleared. The chip does not generate interrupt
7690 * at this time, but the irq handler may still be called due to irq
7691 * sharing or irqpoll.
7692 */
Joe Perches63c3a662011-04-26 08:12:10 +00007693 tg3_flag_set(tp, CHIP_RESETTING);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007694 for (i = 0; i < tp->irq_cnt; i++) {
7695 struct tg3_napi *tnapi = &tp->napi[i];
7696 if (tnapi->hw_status) {
7697 tnapi->hw_status->status = 0;
7698 tnapi->hw_status->status_tag = 0;
7699 }
7700 tnapi->last_tag = 0;
7701 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07007702 }
Michael Chand18edcb2007-03-24 20:57:11 -07007703 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00007704
7705 for (i = 0; i < tp->irq_cnt; i++)
7706 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07007707
Matt Carlson255ca312009-08-25 10:07:27 +00007708 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7709 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7710 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
7711 }
7712
Linus Torvalds1da177e2005-04-16 15:20:36 -07007713 /* do the reset */
7714 val = GRC_MISC_CFG_CORECLK_RESET;
7715
Joe Perches63c3a662011-04-26 08:12:10 +00007716 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson88075d92010-08-02 11:25:58 +00007717 /* Force PCIe 1.0a mode */
7718 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007719 !tg3_flag(tp, 57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00007720 tr32(TG3_PCIE_PHY_TSTCTL) ==
7721 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
7722 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
7723
Linus Torvalds1da177e2005-04-16 15:20:36 -07007724 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
7725 tw32(GRC_MISC_CFG, (1 << 29));
7726 val |= (1 << 29);
7727 }
7728 }
7729
Michael Chanb5d37722006-09-27 16:06:21 -07007730 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7731 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
7732 tw32(GRC_VCPU_EXT_CTRL,
7733 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
7734 }
7735
Matt Carlsonf37500d2010-08-02 11:25:59 +00007736 /* Manage gphy power for all CPMU absent PCIe devices. */
Joe Perches63c3a662011-04-26 08:12:10 +00007737 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007738 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00007739
Linus Torvalds1da177e2005-04-16 15:20:36 -07007740 tw32(GRC_MISC_CFG, val);
7741
Michael Chan1ee582d2005-08-09 20:16:46 -07007742 /* restore 5701 hardware bug workaround write method */
7743 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007744
7745 /* Unfortunately, we have to delay before the PCI read back.
7746 * Some 575X chips even will not respond to a PCI cfg access
7747 * when the reset command is given to the chip.
7748 *
7749 * How do these hardware designers expect things to work
7750 * properly if the PCI write is posted for a long period
7751 * of time? It is always necessary to have some method by
7752 * which a register read back can occur to push the write
7753 * out which does the reset.
7754 *
7755 * For most tg3 variants the trick below was working.
7756 * Ho hum...
7757 */
7758 udelay(120);
7759
7760 /* Flush PCI posted writes. The normal MMIO registers
7761 * are inaccessible at this time so this is the only
7762 * way to make this reliably (actually, this is no longer
7763 * the case, see above). I tried to use indirect
7764 * register read/write but this upset some 5701 variants.
7765 */
7766 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
7767
7768 udelay(120);
7769
Jon Mason708ebb3a2011-06-27 12:56:50 +00007770 if (tg3_flag(tp, PCI_EXPRESS) && pci_pcie_cap(tp->pdev)) {
Matt Carlsone7126992009-08-25 10:08:16 +00007771 u16 val16;
7772
Linus Torvalds1da177e2005-04-16 15:20:36 -07007773 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
7774 int i;
7775 u32 cfg_val;
7776
7777 /* Wait for link training to complete. */
7778 for (i = 0; i < 5000; i++)
7779 udelay(100);
7780
7781 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
7782 pci_write_config_dword(tp->pdev, 0xc4,
7783 cfg_val | (1 << 15));
7784 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007785
Matt Carlsone7126992009-08-25 10:08:16 +00007786 /* Clear the "no snoop" and "relaxed ordering" bits. */
7787 pci_read_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +00007788 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007789 &val16);
7790 val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
7791 PCI_EXP_DEVCTL_NOSNOOP_EN);
7792 /*
7793 * Older PCIe devices only support the 128 byte
7794 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007795 */
Joe Perches63c3a662011-04-26 08:12:10 +00007796 if (!tg3_flag(tp, CPMU_PRESENT))
Matt Carlsone7126992009-08-25 10:08:16 +00007797 val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007798 pci_write_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +00007799 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007800 val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007801
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007802 /* Clear error status */
7803 pci_write_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +00007804 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVSTA,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007805 PCI_EXP_DEVSTA_CED |
7806 PCI_EXP_DEVSTA_NFED |
7807 PCI_EXP_DEVSTA_FED |
7808 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007809 }
7810
Michael Chanee6a99b2007-07-18 21:49:10 -07007811 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007812
Joe Perches63c3a662011-04-26 08:12:10 +00007813 tg3_flag_clear(tp, CHIP_RESETTING);
7814 tg3_flag_clear(tp, ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07007815
Michael Chanee6a99b2007-07-18 21:49:10 -07007816 val = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00007817 if (tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07007818 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07007819 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007820
7821 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
7822 tg3_stop_fw(tp);
7823 tw32(0x5000, 0x400);
7824 }
7825
7826 tw32(GRC_MODE, tp->grc_mode);
7827
7828 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007829 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007830
7831 tw32(0xc4, val | (1 << 15));
7832 }
7833
7834 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
7835 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
7836 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
7837 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
7838 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
7839 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7840 }
7841
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007842 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00007843 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007844 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007845 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00007846 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007847 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007848 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007849 val = 0;
7850
7851 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007852 udelay(40);
7853
Matt Carlson77b483f2008-08-15 14:07:24 -07007854 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
7855
Michael Chan7a6f4362006-09-27 16:03:31 -07007856 err = tg3_poll_fw(tp);
7857 if (err)
7858 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007859
Matt Carlson0a9140c2009-08-28 12:27:50 +00007860 tg3_mdio_start(tp);
7861
Joe Perches63c3a662011-04-26 08:12:10 +00007862 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007863 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
7864 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007865 !tg3_flag(tp, 57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007866 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007867
7868 tw32(0x7c00, val | (1 << 25));
7869 }
7870
Matt Carlsond78b59f2011-04-05 14:22:46 +00007871 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
7872 val = tr32(TG3_CPMU_CLCK_ORIDE);
7873 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
7874 }
7875
Linus Torvalds1da177e2005-04-16 15:20:36 -07007876 /* Reprobe ASF enable state. */
Joe Perches63c3a662011-04-26 08:12:10 +00007877 tg3_flag_clear(tp, ENABLE_ASF);
7878 tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007879 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
7880 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
7881 u32 nic_cfg;
7882
7883 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
7884 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +00007885 tg3_flag_set(tp, ENABLE_ASF);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007886 tp->last_event_jiffies = jiffies;
Joe Perches63c3a662011-04-26 08:12:10 +00007887 if (tg3_flag(tp, 5750_PLUS))
7888 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007889 }
7890 }
7891
7892 return 0;
7893}
7894
Matt Carlson92feeab2011-12-08 14:40:14 +00007895static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *,
7896 struct rtnl_link_stats64 *);
7897static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *,
7898 struct tg3_ethtool_stats *);
7899
Linus Torvalds1da177e2005-04-16 15:20:36 -07007900/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07007901static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007902{
7903 int err;
7904
7905 tg3_stop_fw(tp);
7906
Michael Chan944d9802005-05-29 14:57:48 -07007907 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007908
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007909 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007910 err = tg3_chip_reset(tp);
7911
Matt Carlsondaba2a62009-04-20 06:58:52 +00007912 __tg3_set_mac_addr(tp, 0);
7913
Michael Chan944d9802005-05-29 14:57:48 -07007914 tg3_write_sig_legacy(tp, kind);
7915 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007916
Matt Carlson92feeab2011-12-08 14:40:14 +00007917 if (tp->hw_stats) {
7918 /* Save the stats across chip resets... */
7919 tg3_get_stats64(tp->dev, &tp->net_stats_prev),
7920 tg3_get_estats(tp, &tp->estats_prev);
7921
7922 /* And make sure the next sample is new data */
7923 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
7924 }
7925
Linus Torvalds1da177e2005-04-16 15:20:36 -07007926 if (err)
7927 return err;
7928
7929 return 0;
7930}
7931
Linus Torvalds1da177e2005-04-16 15:20:36 -07007932static int tg3_set_mac_addr(struct net_device *dev, void *p)
7933{
7934 struct tg3 *tp = netdev_priv(dev);
7935 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07007936 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007937
Michael Chanf9804dd2005-09-27 12:13:10 -07007938 if (!is_valid_ether_addr(addr->sa_data))
7939 return -EINVAL;
7940
Linus Torvalds1da177e2005-04-16 15:20:36 -07007941 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7942
Michael Chane75f7c92006-03-20 21:33:26 -08007943 if (!netif_running(dev))
7944 return 0;
7945
Joe Perches63c3a662011-04-26 08:12:10 +00007946 if (tg3_flag(tp, ENABLE_ASF)) {
Michael Chan986e0ae2007-05-05 12:10:20 -07007947 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07007948
Michael Chan986e0ae2007-05-05 12:10:20 -07007949 addr0_high = tr32(MAC_ADDR_0_HIGH);
7950 addr0_low = tr32(MAC_ADDR_0_LOW);
7951 addr1_high = tr32(MAC_ADDR_1_HIGH);
7952 addr1_low = tr32(MAC_ADDR_1_LOW);
7953
7954 /* Skip MAC addr 1 if ASF is using it. */
7955 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
7956 !(addr1_high == 0 && addr1_low == 0))
7957 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07007958 }
Michael Chan986e0ae2007-05-05 12:10:20 -07007959 spin_lock_bh(&tp->lock);
7960 __tg3_set_mac_addr(tp, skip_mac_1);
7961 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007962
Michael Chanb9ec6c12006-07-25 16:37:27 -07007963 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007964}
7965
7966/* tp->lock is held. */
7967static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
7968 dma_addr_t mapping, u32 maxlen_flags,
7969 u32 nic_addr)
7970{
7971 tg3_write_mem(tp,
7972 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
7973 ((u64) mapping >> 32));
7974 tg3_write_mem(tp,
7975 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
7976 ((u64) mapping & 0xffffffff));
7977 tg3_write_mem(tp,
7978 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
7979 maxlen_flags);
7980
Joe Perches63c3a662011-04-26 08:12:10 +00007981 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007982 tg3_write_mem(tp,
7983 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
7984 nic_addr);
7985}
7986
7987static void __tg3_set_rx_mode(struct net_device *);
Michael Chand244c892005-07-05 14:42:33 -07007988static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07007989{
Matt Carlsonb6080e12009-09-01 13:12:00 +00007990 int i;
7991
Joe Perches63c3a662011-04-26 08:12:10 +00007992 if (!tg3_flag(tp, ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007993 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
7994 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
7995 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007996 } else {
7997 tw32(HOSTCC_TXCOL_TICKS, 0);
7998 tw32(HOSTCC_TXMAX_FRAMES, 0);
7999 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00008000 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008001
Joe Perches63c3a662011-04-26 08:12:10 +00008002 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008003 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
8004 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
8005 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
8006 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00008007 tw32(HOSTCC_RXCOL_TICKS, 0);
8008 tw32(HOSTCC_RXMAX_FRAMES, 0);
8009 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07008010 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008011
Joe Perches63c3a662011-04-26 08:12:10 +00008012 if (!tg3_flag(tp, 5705_PLUS)) {
David S. Miller15f98502005-05-18 22:49:26 -07008013 u32 val = ec->stats_block_coalesce_usecs;
8014
Matt Carlsonb6080e12009-09-01 13:12:00 +00008015 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
8016 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
8017
David S. Miller15f98502005-05-18 22:49:26 -07008018 if (!netif_carrier_ok(tp->dev))
8019 val = 0;
8020
8021 tw32(HOSTCC_STAT_COAL_TICKS, val);
8022 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008023
8024 for (i = 0; i < tp->irq_cnt - 1; i++) {
8025 u32 reg;
8026
8027 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
8028 tw32(reg, ec->rx_coalesce_usecs);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008029 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
8030 tw32(reg, ec->rx_max_coalesced_frames);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008031 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
8032 tw32(reg, ec->rx_max_coalesced_frames_irq);
Matt Carlson19cfaec2009-12-03 08:36:20 +00008033
Joe Perches63c3a662011-04-26 08:12:10 +00008034 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008035 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
8036 tw32(reg, ec->tx_coalesce_usecs);
8037 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
8038 tw32(reg, ec->tx_max_coalesced_frames);
8039 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
8040 tw32(reg, ec->tx_max_coalesced_frames_irq);
8041 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008042 }
8043
8044 for (; i < tp->irq_max - 1; i++) {
8045 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008046 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008047 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00008048
Joe Perches63c3a662011-04-26 08:12:10 +00008049 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008050 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
8051 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
8052 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
8053 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008054 }
David S. Miller15f98502005-05-18 22:49:26 -07008055}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008056
8057/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00008058static void tg3_rings_reset(struct tg3 *tp)
8059{
8060 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008061 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008062 struct tg3_napi *tnapi = &tp->napi[0];
8063
8064 /* Disable all transmit rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008065 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008066 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Joe Perches63c3a662011-04-26 08:12:10 +00008067 else if (tg3_flag(tp, 5717_PLUS))
Matt Carlson3d377282010-10-14 10:37:39 +00008068 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
Matt Carlson55086ad2011-12-14 11:09:59 +00008069 else if (tg3_flag(tp, 57765_CLASS))
Matt Carlsonb703df62009-12-03 08:36:21 +00008070 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008071 else
8072 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8073
8074 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8075 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
8076 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
8077 BDINFO_FLAGS_DISABLED);
8078
8079
8080 /* Disable all receive return rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008081 if (tg3_flag(tp, 5717_PLUS))
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008082 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
Joe Perches63c3a662011-04-26 08:12:10 +00008083 else if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008084 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00008085 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00008086 tg3_flag(tp, 57765_CLASS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008087 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
8088 else
8089 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8090
8091 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8092 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
8093 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
8094 BDINFO_FLAGS_DISABLED);
8095
8096 /* Disable interrupts */
8097 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008098 tp->napi[0].chk_msi_cnt = 0;
8099 tp->napi[0].last_rx_cons = 0;
8100 tp->napi[0].last_tx_cons = 0;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008101
8102 /* Zero mailbox registers. */
Joe Perches63c3a662011-04-26 08:12:10 +00008103 if (tg3_flag(tp, SUPPORT_MSIX)) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00008104 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008105 tp->napi[i].tx_prod = 0;
8106 tp->napi[i].tx_cons = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00008107 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008108 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008109 tw32_rx_mbox(tp->napi[i].consmbox, 0);
8110 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
Matt Carlson7f230732011-08-31 11:44:48 +00008111 tp->napi[i].chk_msi_cnt = 0;
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008112 tp->napi[i].last_rx_cons = 0;
8113 tp->napi[i].last_tx_cons = 0;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008114 }
Joe Perches63c3a662011-04-26 08:12:10 +00008115 if (!tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008116 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008117 } else {
8118 tp->napi[0].tx_prod = 0;
8119 tp->napi[0].tx_cons = 0;
8120 tw32_mailbox(tp->napi[0].prodmbox, 0);
8121 tw32_rx_mbox(tp->napi[0].consmbox, 0);
8122 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008123
8124 /* Make sure the NIC-based send BD rings are disabled. */
Joe Perches63c3a662011-04-26 08:12:10 +00008125 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson2d31eca2009-09-01 12:53:31 +00008126 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
8127 for (i = 0; i < 16; i++)
8128 tw32_tx_mbox(mbox + i * 8, 0);
8129 }
8130
8131 txrcb = NIC_SRAM_SEND_RCB;
8132 rxrcb = NIC_SRAM_RCV_RET_RCB;
8133
8134 /* Clear status block in ram. */
8135 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8136
8137 /* Set status block DMA address */
8138 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8139 ((u64) tnapi->status_mapping >> 32));
8140 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8141 ((u64) tnapi->status_mapping & 0xffffffff));
8142
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008143 if (tnapi->tx_ring) {
8144 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8145 (TG3_TX_RING_SIZE <<
8146 BDINFO_FLAGS_MAXLEN_SHIFT),
8147 NIC_SRAM_TX_BUFFER_DESC);
8148 txrcb += TG3_BDINFO_SIZE;
8149 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008150
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008151 if (tnapi->rx_rcb) {
8152 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008153 (tp->rx_ret_ring_mask + 1) <<
8154 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008155 rxrcb += TG3_BDINFO_SIZE;
8156 }
8157
8158 stblk = HOSTCC_STATBLCK_RING1;
8159
8160 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
8161 u64 mapping = (u64)tnapi->status_mapping;
8162 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
8163 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
8164
8165 /* Clear status block in ram. */
8166 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8167
Matt Carlson19cfaec2009-12-03 08:36:20 +00008168 if (tnapi->tx_ring) {
8169 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8170 (TG3_TX_RING_SIZE <<
8171 BDINFO_FLAGS_MAXLEN_SHIFT),
8172 NIC_SRAM_TX_BUFFER_DESC);
8173 txrcb += TG3_BDINFO_SIZE;
8174 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008175
8176 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008177 ((tp->rx_ret_ring_mask + 1) <<
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008178 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
8179
8180 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008181 rxrcb += TG3_BDINFO_SIZE;
8182 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008183}
8184
Matt Carlsoneb07a942011-04-20 07:57:36 +00008185static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
8186{
8187 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
8188
Joe Perches63c3a662011-04-26 08:12:10 +00008189 if (!tg3_flag(tp, 5750_PLUS) ||
8190 tg3_flag(tp, 5780_CLASS) ||
Matt Carlsoneb07a942011-04-20 07:57:36 +00008191 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
Matt Carlson513aa6e2011-11-21 15:01:18 +00008192 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
8193 tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008194 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
8195 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
8196 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787)
8197 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
8198 else
8199 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
8200
8201 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
8202 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
8203
8204 val = min(nic_rep_thresh, host_rep_thresh);
8205 tw32(RCVBDI_STD_THRESH, val);
8206
Joe Perches63c3a662011-04-26 08:12:10 +00008207 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008208 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
8209
Joe Perches63c3a662011-04-26 08:12:10 +00008210 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008211 return;
8212
Matt Carlson513aa6e2011-11-21 15:01:18 +00008213 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
Matt Carlsoneb07a942011-04-20 07:57:36 +00008214
8215 host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
8216
8217 val = min(bdcache_maxcnt / 2, host_rep_thresh);
8218 tw32(RCVBDI_JUMBO_THRESH, val);
8219
Joe Perches63c3a662011-04-26 08:12:10 +00008220 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008221 tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
8222}
8223
Matt Carlson2d31eca2009-09-01 12:53:31 +00008224/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008225static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008226{
8227 u32 val, rdmac_mode;
8228 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00008229 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008230
8231 tg3_disable_ints(tp);
8232
8233 tg3_stop_fw(tp);
8234
8235 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
8236
Joe Perches63c3a662011-04-26 08:12:10 +00008237 if (tg3_flag(tp, INIT_COMPLETE))
Michael Chane6de8ad2005-05-05 14:42:41 -07008238 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008239
Matt Carlson699c0192010-12-06 08:28:51 +00008240 /* Enable MAC control of LPI */
8241 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
8242 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL,
8243 TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
8244 TG3_CPMU_EEE_LNKIDL_UART_IDL);
8245
8246 tw32_f(TG3_CPMU_EEE_CTRL,
8247 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
8248
Matt Carlsona386b902010-12-06 08:28:53 +00008249 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
8250 TG3_CPMU_EEEMD_LPI_IN_TX |
8251 TG3_CPMU_EEEMD_LPI_IN_RX |
8252 TG3_CPMU_EEEMD_EEE_ENABLE;
8253
8254 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8255 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
8256
Joe Perches63c3a662011-04-26 08:12:10 +00008257 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsona386b902010-12-06 08:28:53 +00008258 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
8259
8260 tw32_f(TG3_CPMU_EEE_MODE, val);
8261
8262 tw32_f(TG3_CPMU_EEE_DBTMR1,
8263 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
8264 TG3_CPMU_DBTMR1_LNKIDLE_2047US);
8265
8266 tw32_f(TG3_CPMU_EEE_DBTMR2,
Matt Carlsond7f2ab22011-01-25 15:58:56 +00008267 TG3_CPMU_DBTMR2_APE_TX_2047US |
Matt Carlsona386b902010-12-06 08:28:53 +00008268 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
Matt Carlson699c0192010-12-06 08:28:51 +00008269 }
8270
Matt Carlson603f1172010-02-12 14:47:10 +00008271 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08008272 tg3_phy_reset(tp);
8273
Linus Torvalds1da177e2005-04-16 15:20:36 -07008274 err = tg3_chip_reset(tp);
8275 if (err)
8276 return err;
8277
8278 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
8279
Matt Carlsonbcb37f62008-11-03 16:52:09 -08008280 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008281 val = tr32(TG3_CPMU_CTRL);
8282 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
8283 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08008284
8285 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8286 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8287 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8288 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
8289
8290 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
8291 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
8292 val |= CPMU_LNK_AWARE_MACCLK_6_25;
8293 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
8294
8295 val = tr32(TG3_CPMU_HST_ACC);
8296 val &= ~CPMU_HST_ACC_MACCLK_MASK;
8297 val |= CPMU_HST_ACC_MACCLK_6_25;
8298 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07008299 }
8300
Matt Carlson33466d932009-04-20 06:57:41 +00008301 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
8302 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
8303 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
8304 PCIE_PWR_MGMT_L1_THRESH_4MS;
8305 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00008306
8307 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
8308 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
8309
8310 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d932009-04-20 06:57:41 +00008311
Matt Carlsonf40386c2009-11-02 14:24:02 +00008312 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
8313 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00008314 }
8315
Joe Perches63c3a662011-04-26 08:12:10 +00008316 if (tg3_flag(tp, L1PLLPD_EN)) {
Matt Carlson614b0592010-01-20 16:58:02 +00008317 u32 grc_mode = tr32(GRC_MODE);
8318
8319 /* Access the lower 1K of PL PCIE block registers. */
8320 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8321 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
8322
8323 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
8324 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
8325 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
8326
8327 tw32(GRC_MODE, grc_mode);
8328 }
8329
Matt Carlson55086ad2011-12-14 11:09:59 +00008330 if (tg3_flag(tp, 57765_CLASS)) {
Matt Carlson5093eed2010-11-24 08:31:45 +00008331 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
8332 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00008333
Matt Carlson5093eed2010-11-24 08:31:45 +00008334 /* Access the lower 1K of PL PCIE block registers. */
8335 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8336 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00008337
Matt Carlson5093eed2010-11-24 08:31:45 +00008338 val = tr32(TG3_PCIE_TLDLPL_PORT +
8339 TG3_PCIE_PL_LO_PHYCTL5);
8340 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
8341 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00008342
Matt Carlson5093eed2010-11-24 08:31:45 +00008343 tw32(GRC_MODE, grc_mode);
8344 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00008345
Matt Carlson1ff30a52011-05-19 12:12:46 +00008346 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_57765_AX) {
8347 u32 grc_mode = tr32(GRC_MODE);
8348
8349 /* Access the lower 1K of DL PCIE block registers. */
8350 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8351 tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
8352
8353 val = tr32(TG3_PCIE_TLDLPL_PORT +
8354 TG3_PCIE_DL_LO_FTSMAX);
8355 val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
8356 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
8357 val | TG3_PCIE_DL_LO_FTSMAX_VAL);
8358
8359 tw32(GRC_MODE, grc_mode);
8360 }
8361
Matt Carlsona977dbe2010-04-12 06:58:26 +00008362 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8363 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8364 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8365 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00008366 }
8367
Linus Torvalds1da177e2005-04-16 15:20:36 -07008368 /* This works around an issue with Athlon chipsets on
8369 * B3 tigon3 silicon. This bit has no effect on any
8370 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07008371 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008372 */
Joe Perches63c3a662011-04-26 08:12:10 +00008373 if (!tg3_flag(tp, CPMU_PRESENT)) {
8374 if (!tg3_flag(tp, PCI_EXPRESS))
Matt Carlson795d01c2007-10-07 23:28:17 -07008375 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
8376 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
8377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008378
8379 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008380 tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008381 val = tr32(TG3PCI_PCISTATE);
8382 val |= PCISTATE_RETRY_SAME_DMA;
8383 tw32(TG3PCI_PCISTATE, val);
8384 }
8385
Joe Perches63c3a662011-04-26 08:12:10 +00008386 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -07008387 /* Allow reads and writes to the
8388 * APE register and memory space.
8389 */
8390 val = tr32(TG3PCI_PCISTATE);
8391 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00008392 PCISTATE_ALLOW_APE_SHMEM_WR |
8393 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008394 tw32(TG3PCI_PCISTATE, val);
8395 }
8396
Linus Torvalds1da177e2005-04-16 15:20:36 -07008397 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
8398 /* Enable some hw fixes. */
8399 val = tr32(TG3PCI_MSI_DATA);
8400 val |= (1 << 26) | (1 << 28) | (1 << 29);
8401 tw32(TG3PCI_MSI_DATA, val);
8402 }
8403
8404 /* Descriptor ring init may make accesses to the
8405 * NIC SRAM area to setup the TX descriptors, so we
8406 * can only do this after the hardware has been
8407 * successfully reset.
8408 */
Michael Chan32d8c572006-07-25 16:38:29 -07008409 err = tg3_init_rings(tp);
8410 if (err)
8411 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008412
Joe Perches63c3a662011-04-26 08:12:10 +00008413 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008414 val = tr32(TG3PCI_DMA_RW_CTRL) &
8415 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Matt Carlson1a319022010-04-12 06:58:25 +00008416 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
8417 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlson55086ad2011-12-14 11:09:59 +00008418 if (!tg3_flag(tp, 57765_CLASS) &&
Matt Carlson0aebff42011-04-25 12:42:45 +00008419 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8420 val |= DMA_RWCTRL_TAGGED_STAT_WA;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008421 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
8422 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
8423 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008424 /* This value is determined during the probe time DMA
8425 * engine test, tg3_test_dma.
8426 */
8427 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
8428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008429
8430 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
8431 GRC_MODE_4X_NIC_SEND_RINGS |
8432 GRC_MODE_NO_TX_PHDR_CSUM |
8433 GRC_MODE_NO_RX_PHDR_CSUM);
8434 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07008435
8436 /* Pseudo-header checksum is done by hardware logic and not
8437 * the offload processers, so make the chip do the pseudo-
8438 * header checksums on receive. For transmit it is more
8439 * convenient to do the pseudo-header checksum in software
8440 * as Linux does that on transmit for us in all cases.
8441 */
8442 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008443
8444 tw32(GRC_MODE,
8445 tp->grc_mode |
8446 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
8447
8448 /* Setup the timer prescalar register. Clock is always 66Mhz. */
8449 val = tr32(GRC_MISC_CFG);
8450 val &= ~0xff;
8451 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
8452 tw32(GRC_MISC_CFG, val);
8453
8454 /* Initialize MBUF/DESC pool. */
Joe Perches63c3a662011-04-26 08:12:10 +00008455 if (tg3_flag(tp, 5750_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008456 /* Do nothing. */
8457 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
8458 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
8459 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
8460 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
8461 else
8462 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
8463 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
8464 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Joe Perches63c3a662011-04-26 08:12:10 +00008465 } else if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008466 int fw_len;
8467
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08008468 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008469 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
8470 tw32(BUFMGR_MB_POOL_ADDR,
8471 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
8472 tw32(BUFMGR_MB_POOL_SIZE,
8473 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
8474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008475
Michael Chan0f893dc2005-07-25 12:30:38 -07008476 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008477 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8478 tp->bufmgr_config.mbuf_read_dma_low_water);
8479 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8480 tp->bufmgr_config.mbuf_mac_rx_low_water);
8481 tw32(BUFMGR_MB_HIGH_WATER,
8482 tp->bufmgr_config.mbuf_high_water);
8483 } else {
8484 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8485 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
8486 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8487 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
8488 tw32(BUFMGR_MB_HIGH_WATER,
8489 tp->bufmgr_config.mbuf_high_water_jumbo);
8490 }
8491 tw32(BUFMGR_DMA_LOW_WATER,
8492 tp->bufmgr_config.dma_low_water);
8493 tw32(BUFMGR_DMA_HIGH_WATER,
8494 tp->bufmgr_config.dma_high_water);
8495
Matt Carlsond309a462010-09-30 10:34:31 +00008496 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
8497 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
8498 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
Matt Carlson4d958472011-04-20 07:57:35 +00008499 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
8500 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
8501 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0)
8502 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
Matt Carlsond309a462010-09-30 10:34:31 +00008503 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008504 for (i = 0; i < 2000; i++) {
8505 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
8506 break;
8507 udelay(10);
8508 }
8509 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00008510 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008511 return -ENODEV;
8512 }
8513
Matt Carlsoneb07a942011-04-20 07:57:36 +00008514 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
8515 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
Michael Chanb5d37722006-09-27 16:06:21 -07008516
Matt Carlsoneb07a942011-04-20 07:57:36 +00008517 tg3_setup_rxbd_thresholds(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008518
8519 /* Initialize TG3_BDINFO's at:
8520 * RCVDBDI_STD_BD: standard eth size rx ring
8521 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
8522 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
8523 *
8524 * like so:
8525 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
8526 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
8527 * ring attribute flags
8528 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
8529 *
8530 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
8531 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
8532 *
8533 * The size of each ring is fixed in the firmware, but the location is
8534 * configurable.
8535 */
8536 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008537 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008538 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008539 ((u64) tpr->rx_std_mapping & 0xffffffff));
Joe Perches63c3a662011-04-26 08:12:10 +00008540 if (!tg3_flag(tp, 5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +00008541 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
8542 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008543
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008544 /* Disable the mini ring */
Joe Perches63c3a662011-04-26 08:12:10 +00008545 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008546 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
8547 BDINFO_FLAGS_DISABLED);
8548
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008549 /* Program the jumbo buffer descriptor ring control
8550 * blocks on those devices that have them.
8551 */
Matt Carlsona0512942011-07-27 14:20:54 +00008552 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008553 (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008554
Joe Perches63c3a662011-04-26 08:12:10 +00008555 if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008556 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008557 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008558 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008559 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +00008560 val = TG3_RX_JMB_RING_SIZE(tp) <<
8561 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008562 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +00008563 val | BDINFO_FLAGS_USE_EXT_RECV);
Joe Perches63c3a662011-04-26 08:12:10 +00008564 if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
Matt Carlson55086ad2011-12-14 11:09:59 +00008565 tg3_flag(tp, 57765_CLASS))
Matt Carlson87668d32009-11-13 13:03:34 +00008566 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
8567 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008568 } else {
8569 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
8570 BDINFO_FLAGS_DISABLED);
8571 }
8572
Joe Perches63c3a662011-04-26 08:12:10 +00008573 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonfa6b2aa2011-11-21 15:01:19 +00008574 val = TG3_RX_STD_RING_SIZE(tp);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008575 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
8576 val |= (TG3_RX_STD_DMA_SZ << 2);
8577 } else
Matt Carlson04380d42010-04-12 06:58:29 +00008578 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008579 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008580 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008581
8582 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008583
Matt Carlson411da642009-11-13 13:03:46 +00008584 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e662009-11-13 13:03:49 +00008585 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008586
Joe Perches63c3a662011-04-26 08:12:10 +00008587 tpr->rx_jmb_prod_idx =
8588 tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
Matt Carlson66711e662009-11-13 13:03:49 +00008589 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008590
Matt Carlson2d31eca2009-09-01 12:53:31 +00008591 tg3_rings_reset(tp);
8592
Linus Torvalds1da177e2005-04-16 15:20:36 -07008593 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07008594 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008595
8596 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00008597 tw32(MAC_RX_MTU_SIZE,
8598 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008599
8600 /* The slot time is changed by tg3_setup_phy if we
8601 * run at gigabit with half duplex.
8602 */
Matt Carlsonf2096f92011-04-05 14:22:48 +00008603 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
8604 (6 << TX_LENGTHS_IPG_SHIFT) |
8605 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
8606
8607 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8608 val |= tr32(MAC_TX_LENGTHS) &
8609 (TX_LENGTHS_JMB_FRM_LEN_MSK |
8610 TX_LENGTHS_CNT_DWN_VAL_MSK);
8611
8612 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008613
8614 /* Receive rules. */
8615 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
8616 tw32(RCVLPC_CONFIG, 0x0181);
8617
8618 /* Calculate RDMAC_MODE setting early, we need it to determine
8619 * the RCVLPC_STATE_ENABLE mask.
8620 */
8621 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
8622 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
8623 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
8624 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
8625 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07008626
Matt Carlsondeabaac2010-11-24 08:31:50 +00008627 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +00008628 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
8629
Matt Carlson57e69832008-05-25 23:48:31 -07008630 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08008631 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8632 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07008633 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
8634 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
8635 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
8636
Matt Carlsonc5908932011-03-09 16:58:25 +00008637 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8638 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00008639 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07008640 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008641 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
8642 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008643 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008644 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8645 }
8646 }
8647
Joe Perches63c3a662011-04-26 08:12:10 +00008648 if (tg3_flag(tp, PCI_EXPRESS))
Michael Chan85e94ce2005-04-21 17:05:28 -07008649 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8650
Matt Carlson55086ad2011-12-14 11:09:59 +00008651 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57766)
8652 rdmac_mode |= RDMAC_MODE_JMB_2K_MMRR;
8653
Joe Perches63c3a662011-04-26 08:12:10 +00008654 if (tg3_flag(tp, HW_TSO_1) ||
8655 tg3_flag(tp, HW_TSO_2) ||
8656 tg3_flag(tp, HW_TSO_3))
Matt Carlson027455a2008-12-21 20:19:30 -08008657 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
8658
Matt Carlson108a6c12011-05-19 12:12:47 +00008659 if (tg3_flag(tp, 57765_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +00008660 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson027455a2008-12-21 20:19:30 -08008661 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
8662 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008663
Matt Carlsonf2096f92011-04-05 14:22:48 +00008664 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8665 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
8666
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008667 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
8668 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
8669 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8670 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008671 tg3_flag(tp, 57765_PLUS)) {
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008672 val = tr32(TG3_RDMA_RSRVCTRL_REG);
Matt Carlsond78b59f2011-04-05 14:22:46 +00008673 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8674 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +00008675 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
8676 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
8677 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
8678 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
8679 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
8680 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +00008681 }
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008682 tw32(TG3_RDMA_RSRVCTRL_REG,
8683 val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
8684 }
8685
Matt Carlsond78b59f2011-04-05 14:22:46 +00008686 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8687 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsond309a462010-09-30 10:34:31 +00008688 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
8689 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val |
8690 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
8691 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
8692 }
8693
Linus Torvalds1da177e2005-04-16 15:20:36 -07008694 /* Receive/send statistics. */
Joe Perches63c3a662011-04-26 08:12:10 +00008695 if (tg3_flag(tp, 5750_PLUS)) {
Michael Chan16613942006-06-29 20:15:13 -07008696 val = tr32(RCVLPC_STATS_ENABLE);
8697 val &= ~RCVLPC_STATSENAB_DACK_FIX;
8698 tw32(RCVLPC_STATS_ENABLE, val);
8699 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008700 tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008701 val = tr32(RCVLPC_STATS_ENABLE);
8702 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
8703 tw32(RCVLPC_STATS_ENABLE, val);
8704 } else {
8705 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
8706 }
8707 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
8708 tw32(SNDDATAI_STATSENAB, 0xffffff);
8709 tw32(SNDDATAI_STATSCTRL,
8710 (SNDDATAI_SCTRL_ENABLE |
8711 SNDDATAI_SCTRL_FASTUPD));
8712
8713 /* Setup host coalescing engine. */
8714 tw32(HOSTCC_MODE, 0);
8715 for (i = 0; i < 2000; i++) {
8716 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
8717 break;
8718 udelay(10);
8719 }
8720
Michael Chand244c892005-07-05 14:42:33 -07008721 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008722
Joe Perches63c3a662011-04-26 08:12:10 +00008723 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008724 /* Status/statistics block address. See tg3_timer,
8725 * the tg3_periodic_fetch_stats call there, and
8726 * tg3_get_stats to see how this works for 5705/5750 chips.
8727 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008728 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8729 ((u64) tp->stats_mapping >> 32));
8730 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8731 ((u64) tp->stats_mapping & 0xffffffff));
8732 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008733
Linus Torvalds1da177e2005-04-16 15:20:36 -07008734 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008735
8736 /* Clear statistics and status block memory areas */
8737 for (i = NIC_SRAM_STATS_BLK;
8738 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
8739 i += sizeof(u32)) {
8740 tg3_write_mem(tp, i, 0);
8741 udelay(40);
8742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008743 }
8744
8745 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
8746
8747 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
8748 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008749 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008750 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
8751
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008752 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
8753 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -07008754 /* reset to prevent losing 1st rx packet intermittently */
8755 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8756 udelay(10);
8757 }
8758
Matt Carlson3bda1252008-08-15 14:08:22 -07008759 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Matt Carlson9e975cc2011-07-20 10:20:50 +00008760 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE |
8761 MAC_MODE_FHDE_ENABLE;
8762 if (tg3_flag(tp, ENABLE_APE))
8763 tp->mac_mode |= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Joe Perches63c3a662011-04-26 08:12:10 +00008764 if (!tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008765 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07008766 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
8767 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008768 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
8769 udelay(40);
8770
Michael Chan314fba32005-04-21 17:07:04 -07008771 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Joe Perches63c3a662011-04-26 08:12:10 +00008772 * If TG3_FLAG_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07008773 * register to preserve the GPIO settings for LOMs. The GPIOs,
8774 * whether used as inputs or outputs, are set by boot code after
8775 * reset.
8776 */
Joe Perches63c3a662011-04-26 08:12:10 +00008777 if (!tg3_flag(tp, IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07008778 u32 gpio_mask;
8779
Michael Chan9d26e212006-12-07 00:21:14 -08008780 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
8781 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
8782 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07008783
8784 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
8785 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
8786 GRC_LCLCTRL_GPIO_OUTPUT3;
8787
Michael Chanaf36e6b2006-03-23 01:28:06 -08008788 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
8789 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
8790
Gary Zambranoaaf84462007-05-05 11:51:45 -07008791 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07008792 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
8793
8794 /* GPIO1 must be driven high for eeprom write protect */
Joe Perches63c3a662011-04-26 08:12:10 +00008795 if (tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan9d26e212006-12-07 00:21:14 -08008796 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
8797 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07008798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008799 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8800 udelay(100);
8801
Joe Perches63c3a662011-04-26 08:12:10 +00008802 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008803 val = tr32(MSGINT_MODE);
8804 val |= MSGINT_MODE_MULTIVEC_EN | MSGINT_MODE_ENABLE;
Matt Carlson5b39de92011-08-31 11:44:50 +00008805 if (!tg3_flag(tp, 1SHOT_MSI))
8806 val |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008807 tw32(MSGINT_MODE, val);
8808 }
8809
Joe Perches63c3a662011-04-26 08:12:10 +00008810 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008811 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
8812 udelay(40);
8813 }
8814
8815 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
8816 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
8817 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
8818 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
8819 WDMAC_MODE_LNGREAD_ENAB);
8820
Matt Carlsonc5908932011-03-09 16:58:25 +00008821 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8822 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00008823 if (tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008824 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
8825 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
8826 /* nothing */
8827 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008828 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008829 val |= WDMAC_MODE_RX_ACCEL;
8830 }
8831 }
8832
Michael Chand9ab5ad12006-03-20 22:27:35 -08008833 /* Enable host coalescing bug fix */
Joe Perches63c3a662011-04-26 08:12:10 +00008834 if (tg3_flag(tp, 5755_PLUS))
Matt Carlsonf51f3562008-05-25 23:45:08 -07008835 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad12006-03-20 22:27:35 -08008836
Matt Carlson788a0352009-11-02 14:26:03 +00008837 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
8838 val |= WDMAC_MODE_BURST_ALL_DATA;
8839
Linus Torvalds1da177e2005-04-16 15:20:36 -07008840 tw32_f(WDMAC_MODE, val);
8841 udelay(40);
8842
Joe Perches63c3a662011-04-26 08:12:10 +00008843 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07008844 u16 pcix_cmd;
8845
8846 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8847 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008848 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07008849 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
8850 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008851 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07008852 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
8853 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008854 }
Matt Carlson9974a352007-10-07 23:27:28 -07008855 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8856 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008857 }
8858
8859 tw32_f(RDMAC_MODE, rdmac_mode);
8860 udelay(40);
8861
8862 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008863 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008864 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07008865
8866 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
8867 tw32(SNDDATAC_MODE,
8868 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
8869 else
8870 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
8871
Linus Torvalds1da177e2005-04-16 15:20:36 -07008872 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
8873 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008874 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00008875 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008876 val |= RCVDBDI_MODE_LRG_RING_SZ;
8877 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008878 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008879 if (tg3_flag(tp, HW_TSO_1) ||
8880 tg3_flag(tp, HW_TSO_2) ||
8881 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008882 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008883 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00008884 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008885 val |= SNDBDI_MODE_MULTI_TXQ_EN;
8886 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008887 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
8888
8889 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
8890 err = tg3_load_5701_a0_firmware_fix(tp);
8891 if (err)
8892 return err;
8893 }
8894
Joe Perches63c3a662011-04-26 08:12:10 +00008895 if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008896 err = tg3_load_tso_firmware(tp);
8897 if (err)
8898 return err;
8899 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008900
8901 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +00008902
Joe Perches63c3a662011-04-26 08:12:10 +00008903 if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsonb1d05212010-06-05 17:24:31 +00008904 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
8905 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +00008906
8907 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
8908 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
8909 tp->tx_mode &= ~val;
8910 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
8911 }
8912
Linus Torvalds1da177e2005-04-16 15:20:36 -07008913 tw32_f(MAC_TX_MODE, tp->tx_mode);
8914 udelay(100);
8915
Joe Perches63c3a662011-04-26 08:12:10 +00008916 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson9d53fa12011-07-20 10:20:54 +00008917 int i = 0;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008918 u32 reg = MAC_RSS_INDIR_TBL_0;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008919
Matt Carlson9d53fa12011-07-20 10:20:54 +00008920 if (tp->irq_cnt == 2) {
8921 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i += 8) {
8922 tw32(reg, 0x0);
8923 reg += 4;
8924 }
8925 } else {
8926 u32 val;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008927
Matt Carlson9d53fa12011-07-20 10:20:54 +00008928 while (i < TG3_RSS_INDIR_TBL_SIZE) {
8929 val = i % (tp->irq_cnt - 1);
8930 i++;
8931 for (; i % 8; i++) {
8932 val <<= 4;
8933 val |= (i % (tp->irq_cnt - 1));
8934 }
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008935 tw32(reg, val);
8936 reg += 4;
8937 }
8938 }
8939
8940 /* Setup the "secret" hash key. */
8941 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
8942 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
8943 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
8944 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
8945 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
8946 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
8947 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
8948 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
8949 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
8950 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
8951 }
8952
Linus Torvalds1da177e2005-04-16 15:20:36 -07008953 tp->rx_mode = RX_MODE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00008954 if (tg3_flag(tp, 5755_PLUS))
Michael Chanaf36e6b2006-03-23 01:28:06 -08008955 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
8956
Joe Perches63c3a662011-04-26 08:12:10 +00008957 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008958 tp->rx_mode |= RX_MODE_RSS_ENABLE |
8959 RX_MODE_RSS_ITBL_HASH_BITS_7 |
8960 RX_MODE_RSS_IPV6_HASH_EN |
8961 RX_MODE_RSS_TCP_IPV6_HASH_EN |
8962 RX_MODE_RSS_IPV4_HASH_EN |
8963 RX_MODE_RSS_TCP_IPV4_HASH_EN;
8964
Linus Torvalds1da177e2005-04-16 15:20:36 -07008965 tw32_f(MAC_RX_MODE, tp->rx_mode);
8966 udelay(10);
8967
Linus Torvalds1da177e2005-04-16 15:20:36 -07008968 tw32(MAC_LED_CTRL, tp->led_ctrl);
8969
8970 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008971 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008972 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8973 udelay(10);
8974 }
8975 tw32_f(MAC_RX_MODE, tp->rx_mode);
8976 udelay(10);
8977
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008978 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008979 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008980 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008981 /* Set drive transmission level to 1.2V */
8982 /* only if the signal pre-emphasis bit is not set */
8983 val = tr32(MAC_SERDES_CFG);
8984 val &= 0xfffff000;
8985 val |= 0x880;
8986 tw32(MAC_SERDES_CFG, val);
8987 }
8988 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
8989 tw32(MAC_SERDES_CFG, 0x616000);
8990 }
8991
8992 /* Prevent chip from dropping frames when flow control
8993 * is enabled.
8994 */
Matt Carlson55086ad2011-12-14 11:09:59 +00008995 if (tg3_flag(tp, 57765_CLASS))
Matt Carlson666bc832010-01-20 16:58:03 +00008996 val = 1;
8997 else
8998 val = 2;
8999 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009000
9001 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009002 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009003 /* Use hardware link auto-negotiation */
Joe Perches63c3a662011-04-26 08:12:10 +00009004 tg3_flag_set(tp, HW_AUTONEG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009005 }
9006
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009007 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +00009008 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08009009 u32 tmp;
9010
9011 tmp = tr32(SERDES_RX_CTRL);
9012 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
9013 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
9014 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
9015 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
9016 }
9017
Joe Perches63c3a662011-04-26 08:12:10 +00009018 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson80096062010-08-02 11:26:06 +00009019 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
9020 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07009021 tp->link_config.speed = tp->link_config.orig_speed;
9022 tp->link_config.duplex = tp->link_config.orig_duplex;
9023 tp->link_config.autoneg = tp->link_config.orig_autoneg;
9024 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009025
Matt Carlsondd477002008-05-25 23:45:58 -07009026 err = tg3_setup_phy(tp, 0);
9027 if (err)
9028 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009029
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009030 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
9031 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07009032 u32 tmp;
9033
9034 /* Clear CRC stats. */
9035 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
9036 tg3_writephy(tp, MII_TG3_TEST1,
9037 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009038 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -07009039 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009040 }
9041 }
9042
9043 __tg3_set_rx_mode(tp->dev);
9044
9045 /* Initialize receive rules. */
9046 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
9047 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
9048 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
9049 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
9050
Joe Perches63c3a662011-04-26 08:12:10 +00009051 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009052 limit = 8;
9053 else
9054 limit = 16;
Joe Perches63c3a662011-04-26 08:12:10 +00009055 if (tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009056 limit -= 4;
9057 switch (limit) {
9058 case 16:
9059 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
9060 case 15:
9061 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
9062 case 14:
9063 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
9064 case 13:
9065 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
9066 case 12:
9067 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
9068 case 11:
9069 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
9070 case 10:
9071 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
9072 case 9:
9073 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
9074 case 8:
9075 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
9076 case 7:
9077 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
9078 case 6:
9079 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
9080 case 5:
9081 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
9082 case 4:
9083 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
9084 case 3:
9085 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
9086 case 2:
9087 case 1:
9088
9089 default:
9090 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07009091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009092
Joe Perches63c3a662011-04-26 08:12:10 +00009093 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson9ce768e2007-10-11 19:49:11 -07009094 /* Write our heartbeat update interval to APE. */
9095 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
9096 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07009097
Linus Torvalds1da177e2005-04-16 15:20:36 -07009098 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
9099
Linus Torvalds1da177e2005-04-16 15:20:36 -07009100 return 0;
9101}
9102
9103/* Called at device open time to get the chip ready for
9104 * packet processing. Invoked with tp->lock held.
9105 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009106static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009107{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009108 tg3_switch_clocks(tp);
9109
9110 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
9111
Matt Carlson2f751b62008-08-04 23:17:34 -07009112 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009113}
9114
9115#define TG3_STAT_ADD32(PSTAT, REG) \
9116do { u32 __val = tr32(REG); \
9117 (PSTAT)->low += __val; \
9118 if ((PSTAT)->low < __val) \
9119 (PSTAT)->high += 1; \
9120} while (0)
9121
9122static void tg3_periodic_fetch_stats(struct tg3 *tp)
9123{
9124 struct tg3_hw_stats *sp = tp->hw_stats;
9125
9126 if (!netif_carrier_ok(tp->dev))
9127 return;
9128
9129 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
9130 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
9131 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
9132 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
9133 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
9134 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
9135 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
9136 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
9137 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
9138 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
9139 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
9140 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
9141 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
9142
9143 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
9144 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
9145 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
9146 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
9147 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
9148 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
9149 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
9150 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
9151 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
9152 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
9153 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
9154 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
9155 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
9156 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07009157
9158 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
Matt Carlson310050f2011-05-19 12:12:55 +00009159 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
9160 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0 &&
9161 tp->pci_chip_rev_id != CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +00009162 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
9163 } else {
9164 u32 val = tr32(HOSTCC_FLOW_ATTN);
9165 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
9166 if (val) {
9167 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
9168 sp->rx_discards.low += val;
9169 if (sp->rx_discards.low < val)
9170 sp->rx_discards.high += 1;
9171 }
9172 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
9173 }
Michael Chan463d3052006-05-22 16:36:27 -07009174 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009175}
9176
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009177static void tg3_chk_missed_msi(struct tg3 *tp)
9178{
9179 u32 i;
9180
9181 for (i = 0; i < tp->irq_cnt; i++) {
9182 struct tg3_napi *tnapi = &tp->napi[i];
9183
9184 if (tg3_has_work(tnapi)) {
9185 if (tnapi->last_rx_cons == tnapi->rx_rcb_ptr &&
9186 tnapi->last_tx_cons == tnapi->tx_cons) {
9187 if (tnapi->chk_msi_cnt < 1) {
9188 tnapi->chk_msi_cnt++;
9189 return;
9190 }
Matt Carlson7f230732011-08-31 11:44:48 +00009191 tg3_msi(0, tnapi);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009192 }
9193 }
9194 tnapi->chk_msi_cnt = 0;
9195 tnapi->last_rx_cons = tnapi->rx_rcb_ptr;
9196 tnapi->last_tx_cons = tnapi->tx_cons;
9197 }
9198}
9199
Linus Torvalds1da177e2005-04-16 15:20:36 -07009200static void tg3_timer(unsigned long __opaque)
9201{
9202 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009203
Matt Carlson5b190622011-11-04 09:15:04 +00009204 if (tp->irq_sync || tg3_flag(tp, RESET_TASK_PENDING))
Michael Chanf475f162006-03-27 23:20:14 -08009205 goto restart_timer;
9206
David S. Millerf47c11e2005-06-24 20:18:35 -07009207 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009208
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009209 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00009210 tg3_flag(tp, 57765_CLASS))
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009211 tg3_chk_missed_msi(tp);
9212
Joe Perches63c3a662011-04-26 08:12:10 +00009213 if (!tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -07009214 /* All of this garbage is because when using non-tagged
9215 * IRQ status the mailbox/status_block protocol the chip
9216 * uses with the cpu is race prone.
9217 */
Matt Carlson898a56f2009-08-28 14:02:40 +00009218 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -07009219 tw32(GRC_LOCAL_CTRL,
9220 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
9221 } else {
9222 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00009223 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -07009224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009225
David S. Millerfac9b832005-05-18 22:46:34 -07009226 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
David S. Millerf47c11e2005-06-24 20:18:35 -07009227 spin_unlock(&tp->lock);
Matt Carlsondb219972011-11-04 09:15:03 +00009228 tg3_reset_task_schedule(tp);
Matt Carlson5b190622011-11-04 09:15:04 +00009229 goto restart_timer;
David S. Millerfac9b832005-05-18 22:46:34 -07009230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009231 }
9232
Linus Torvalds1da177e2005-04-16 15:20:36 -07009233 /* This part only runs once per second. */
9234 if (!--tp->timer_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00009235 if (tg3_flag(tp, 5705_PLUS))
David S. Millerfac9b832005-05-18 22:46:34 -07009236 tg3_periodic_fetch_stats(tp);
9237
Matt Carlsonb0c59432011-05-19 12:12:48 +00009238 if (tp->setlpicnt && !--tp->setlpicnt)
9239 tg3_phy_eee_enable(tp);
Matt Carlson52b02d02010-10-14 10:37:41 +00009240
Joe Perches63c3a662011-04-26 08:12:10 +00009241 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009242 u32 mac_stat;
9243 int phy_event;
9244
9245 mac_stat = tr32(MAC_STATUS);
9246
9247 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009248 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009249 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
9250 phy_event = 1;
9251 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
9252 phy_event = 1;
9253
9254 if (phy_event)
9255 tg3_setup_phy(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +00009256 } else if (tg3_flag(tp, POLL_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009257 u32 mac_stat = tr32(MAC_STATUS);
9258 int need_setup = 0;
9259
9260 if (netif_carrier_ok(tp->dev) &&
9261 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
9262 need_setup = 1;
9263 }
Matt Carlsonbe98da62010-07-11 09:31:46 +00009264 if (!netif_carrier_ok(tp->dev) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009265 (mac_stat & (MAC_STATUS_PCS_SYNCED |
9266 MAC_STATUS_SIGNAL_DET))) {
9267 need_setup = 1;
9268 }
9269 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07009270 if (!tp->serdes_counter) {
9271 tw32_f(MAC_MODE,
9272 (tp->mac_mode &
9273 ~MAC_MODE_PORT_MODE_MASK));
9274 udelay(40);
9275 tw32_f(MAC_MODE, tp->mac_mode);
9276 udelay(40);
9277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009278 tg3_setup_phy(tp, 0);
9279 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009280 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009281 tg3_flag(tp, 5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -07009282 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +00009283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009284
9285 tp->timer_counter = tp->timer_multiplier;
9286 }
9287
Michael Chan130b8e42006-09-27 16:00:40 -07009288 /* Heartbeat is only sent once every 2 seconds.
9289 *
9290 * The heartbeat is to tell the ASF firmware that the host
9291 * driver is still alive. In the event that the OS crashes,
9292 * ASF needs to reset the hardware to free up the FIFO space
9293 * that may be filled with rx packets destined for the host.
9294 * If the FIFO is full, ASF will no longer function properly.
9295 *
9296 * Unintended resets have been reported on real time kernels
9297 * where the timer doesn't run on time. Netpoll will also have
9298 * same problem.
9299 *
9300 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
9301 * to check the ring condition when the heartbeat is expiring
9302 * before doing the reset. This will prevent most unintended
9303 * resets.
9304 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009305 if (!--tp->asf_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00009306 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07009307 tg3_wait_for_event_ack(tp);
9308
Michael Chanbbadf502006-04-06 21:46:34 -07009309 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07009310 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07009311 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +00009312 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
9313 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -07009314
9315 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009316 }
9317 tp->asf_counter = tp->asf_multiplier;
9318 }
9319
David S. Millerf47c11e2005-06-24 20:18:35 -07009320 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009321
Michael Chanf475f162006-03-27 23:20:14 -08009322restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07009323 tp->timer.expires = jiffies + tp->timer_offset;
9324 add_timer(&tp->timer);
9325}
9326
Matt Carlson4f125f42009-09-01 12:55:02 +00009327static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -08009328{
David Howells7d12e782006-10-05 14:55:46 +01009329 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009330 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +00009331 char *name;
9332 struct tg3_napi *tnapi = &tp->napi[irq_num];
9333
9334 if (tp->irq_cnt == 1)
9335 name = tp->dev->name;
9336 else {
9337 name = &tnapi->irq_lbl[0];
9338 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
9339 name[IFNAMSIZ-1] = 0;
9340 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009341
Joe Perches63c3a662011-04-26 08:12:10 +00009342 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Michael Chanfcfa0a32006-03-20 22:28:41 -08009343 fn = tg3_msi;
Joe Perches63c3a662011-04-26 08:12:10 +00009344 if (tg3_flag(tp, 1SHOT_MSI))
Michael Chanfcfa0a32006-03-20 22:28:41 -08009345 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009346 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009347 } else {
9348 fn = tg3_interrupt;
Joe Perches63c3a662011-04-26 08:12:10 +00009349 if (tg3_flag(tp, TAGGED_STATUS))
Michael Chanfcfa0a32006-03-20 22:28:41 -08009350 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009351 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009352 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009353
9354 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009355}
9356
Michael Chan79381092005-04-21 17:13:59 -07009357static int tg3_test_interrupt(struct tg3 *tp)
9358{
Matt Carlson09943a12009-08-28 14:01:57 +00009359 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -07009360 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -07009361 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009362 u32 val;
Michael Chan79381092005-04-21 17:13:59 -07009363
Michael Chand4bc3922005-05-29 14:59:20 -07009364 if (!netif_running(dev))
9365 return -ENODEV;
9366
Michael Chan79381092005-04-21 17:13:59 -07009367 tg3_disable_ints(tp);
9368
Matt Carlson4f125f42009-09-01 12:55:02 +00009369 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009370
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009371 /*
9372 * Turn off MSI one shot mode. Otherwise this test has no
9373 * observable way to know whether the interrupt was delivered.
9374 */
Matt Carlson3aa1cdf2011-07-20 10:20:55 +00009375 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009376 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
9377 tw32(MSGINT_MODE, val);
9378 }
9379
Matt Carlson4f125f42009-09-01 12:55:02 +00009380 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Matt Carlson09943a12009-08-28 14:01:57 +00009381 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009382 if (err)
9383 return err;
9384
Matt Carlson898a56f2009-08-28 14:02:40 +00009385 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -07009386 tg3_enable_ints(tp);
9387
9388 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00009389 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -07009390
9391 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -07009392 u32 int_mbox, misc_host_ctrl;
9393
Matt Carlson898a56f2009-08-28 14:02:40 +00009394 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -07009395 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
9396
9397 if ((int_mbox != 0) ||
9398 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
9399 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -07009400 break;
Michael Chanb16250e2006-09-27 16:10:14 -07009401 }
9402
Matt Carlson3aa1cdf2011-07-20 10:20:55 +00009403 if (tg3_flag(tp, 57765_PLUS) &&
9404 tnapi->hw_status->status_tag != tnapi->last_tag)
9405 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
9406
Michael Chan79381092005-04-21 17:13:59 -07009407 msleep(10);
9408 }
9409
9410 tg3_disable_ints(tp);
9411
Matt Carlson4f125f42009-09-01 12:55:02 +00009412 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009413
Matt Carlson4f125f42009-09-01 12:55:02 +00009414 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009415
9416 if (err)
9417 return err;
9418
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009419 if (intr_ok) {
9420 /* Reenable MSI one shot mode. */
Matt Carlson5b39de92011-08-31 11:44:50 +00009421 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, 1SHOT_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009422 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
9423 tw32(MSGINT_MODE, val);
9424 }
Michael Chan79381092005-04-21 17:13:59 -07009425 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009426 }
Michael Chan79381092005-04-21 17:13:59 -07009427
9428 return -EIO;
9429}
9430
9431/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
9432 * successfully restored
9433 */
9434static int tg3_test_msi(struct tg3 *tp)
9435{
Michael Chan79381092005-04-21 17:13:59 -07009436 int err;
9437 u16 pci_cmd;
9438
Joe Perches63c3a662011-04-26 08:12:10 +00009439 if (!tg3_flag(tp, USING_MSI))
Michael Chan79381092005-04-21 17:13:59 -07009440 return 0;
9441
9442 /* Turn off SERR reporting in case MSI terminates with Master
9443 * Abort.
9444 */
9445 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
9446 pci_write_config_word(tp->pdev, PCI_COMMAND,
9447 pci_cmd & ~PCI_COMMAND_SERR);
9448
9449 err = tg3_test_interrupt(tp);
9450
9451 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
9452
9453 if (!err)
9454 return 0;
9455
9456 /* other failures */
9457 if (err != -EIO)
9458 return err;
9459
9460 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009461 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
9462 "to INTx mode. Please report this failure to the PCI "
9463 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -07009464
Matt Carlson4f125f42009-09-01 12:55:02 +00009465 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +00009466
Michael Chan79381092005-04-21 17:13:59 -07009467 pci_disable_msi(tp->pdev);
9468
Joe Perches63c3a662011-04-26 08:12:10 +00009469 tg3_flag_clear(tp, USING_MSI);
Andre Detschdc8bf1b2010-04-26 07:27:07 +00009470 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -07009471
Matt Carlson4f125f42009-09-01 12:55:02 +00009472 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009473 if (err)
9474 return err;
9475
9476 /* Need to reset the chip because the MSI cycle may have terminated
9477 * with Master Abort.
9478 */
David S. Millerf47c11e2005-06-24 20:18:35 -07009479 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009480
Michael Chan944d9802005-05-29 14:57:48 -07009481 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009482 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009483
David S. Millerf47c11e2005-06-24 20:18:35 -07009484 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009485
9486 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +00009487 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -07009488
9489 return err;
9490}
9491
Matt Carlson9e9fd122009-01-19 16:57:45 -08009492static int tg3_request_firmware(struct tg3 *tp)
9493{
9494 const __be32 *fw_data;
9495
9496 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009497 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
9498 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009499 return -ENOENT;
9500 }
9501
9502 fw_data = (void *)tp->fw->data;
9503
9504 /* Firmware blob starts with version numbers, followed by
9505 * start address and _full_ length including BSS sections
9506 * (which must be longer than the actual data, of course
9507 */
9508
9509 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
9510 if (tp->fw_len < (tp->fw->size - 12)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009511 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
9512 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009513 release_firmware(tp->fw);
9514 tp->fw = NULL;
9515 return -EINVAL;
9516 }
9517
9518 /* We no longer need firmware; we have it. */
9519 tp->fw_needed = NULL;
9520 return 0;
9521}
9522
Matt Carlson679563f2009-09-01 12:55:46 +00009523static bool tg3_enable_msix(struct tg3 *tp)
9524{
9525 int i, rc, cpus = num_online_cpus();
9526 struct msix_entry msix_ent[tp->irq_max];
9527
9528 if (cpus == 1)
9529 /* Just fallback to the simpler MSI mode. */
9530 return false;
9531
9532 /*
9533 * We want as many rx rings enabled as there are cpus.
9534 * The first MSIX vector only deals with link interrupts, etc,
9535 * so we add one to the number of vectors we are requesting.
9536 */
9537 tp->irq_cnt = min_t(unsigned, cpus + 1, tp->irq_max);
9538
9539 for (i = 0; i < tp->irq_max; i++) {
9540 msix_ent[i].entry = i;
9541 msix_ent[i].vector = 0;
9542 }
9543
9544 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +00009545 if (rc < 0) {
9546 return false;
9547 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +00009548 if (pci_enable_msix(tp->pdev, msix_ent, rc))
9549 return false;
Joe Perches05dbe002010-02-17 19:44:19 +00009550 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
9551 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +00009552 tp->irq_cnt = rc;
9553 }
9554
9555 for (i = 0; i < tp->irq_max; i++)
9556 tp->napi[i].irq_vec = msix_ent[i].vector;
9557
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009558 netif_set_real_num_tx_queues(tp->dev, 1);
9559 rc = tp->irq_cnt > 1 ? tp->irq_cnt - 1 : 1;
9560 if (netif_set_real_num_rx_queues(tp->dev, rc)) {
9561 pci_disable_msix(tp->pdev);
9562 return false;
9563 }
Matt Carlsonb92b9042010-11-24 08:31:51 +00009564
9565 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +00009566 tg3_flag_set(tp, ENABLE_RSS);
Matt Carlsond78b59f2011-04-05 14:22:46 +00009567
9568 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
9569 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Joe Perches63c3a662011-04-26 08:12:10 +00009570 tg3_flag_set(tp, ENABLE_TSS);
Matt Carlsonb92b9042010-11-24 08:31:51 +00009571 netif_set_real_num_tx_queues(tp->dev, tp->irq_cnt - 1);
9572 }
9573 }
Matt Carlson2430b032010-06-05 17:24:34 +00009574
Matt Carlson679563f2009-09-01 12:55:46 +00009575 return true;
9576}
9577
Matt Carlson07b01732009-08-28 14:01:15 +00009578static void tg3_ints_init(struct tg3 *tp)
9579{
Joe Perches63c3a662011-04-26 08:12:10 +00009580 if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
9581 !tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +00009582 /* All MSI supporting chips should support tagged
9583 * status. Assert that this is the case.
9584 */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009585 netdev_warn(tp->dev,
9586 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +00009587 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +00009588 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009589
Joe Perches63c3a662011-04-26 08:12:10 +00009590 if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
9591 tg3_flag_set(tp, USING_MSIX);
9592 else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
9593 tg3_flag_set(tp, USING_MSI);
Matt Carlson679563f2009-09-01 12:55:46 +00009594
Joe Perches63c3a662011-04-26 08:12:10 +00009595 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009596 u32 msi_mode = tr32(MSGINT_MODE);
Joe Perches63c3a662011-04-26 08:12:10 +00009597 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009598 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +00009599 if (!tg3_flag(tp, 1SHOT_MSI))
9600 msi_mode |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlson679563f2009-09-01 12:55:46 +00009601 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
9602 }
9603defcfg:
Joe Perches63c3a662011-04-26 08:12:10 +00009604 if (!tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009605 tp->irq_cnt = 1;
9606 tp->napi[0].irq_vec = tp->pdev->irq;
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009607 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -07009608 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +00009609 }
Matt Carlson07b01732009-08-28 14:01:15 +00009610}
9611
9612static void tg3_ints_fini(struct tg3 *tp)
9613{
Joe Perches63c3a662011-04-26 08:12:10 +00009614 if (tg3_flag(tp, USING_MSIX))
Matt Carlson679563f2009-09-01 12:55:46 +00009615 pci_disable_msix(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009616 else if (tg3_flag(tp, USING_MSI))
Matt Carlson679563f2009-09-01 12:55:46 +00009617 pci_disable_msi(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009618 tg3_flag_clear(tp, USING_MSI);
9619 tg3_flag_clear(tp, USING_MSIX);
9620 tg3_flag_clear(tp, ENABLE_RSS);
9621 tg3_flag_clear(tp, ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +00009622}
9623
Linus Torvalds1da177e2005-04-16 15:20:36 -07009624static int tg3_open(struct net_device *dev)
9625{
9626 struct tg3 *tp = netdev_priv(dev);
Matt Carlson4f125f42009-09-01 12:55:02 +00009627 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009628
Matt Carlson9e9fd122009-01-19 16:57:45 -08009629 if (tp->fw_needed) {
9630 err = tg3_request_firmware(tp);
9631 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
9632 if (err)
9633 return err;
9634 } else if (err) {
Joe Perches05dbe002010-02-17 19:44:19 +00009635 netdev_warn(tp->dev, "TSO capability disabled\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009636 tg3_flag_clear(tp, TSO_CAPABLE);
9637 } else if (!tg3_flag(tp, TSO_CAPABLE)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009638 netdev_notice(tp->dev, "TSO capability restored\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009639 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009640 }
9641 }
9642
Michael Chanc49a1562006-12-17 17:07:29 -08009643 netif_carrier_off(tp->dev);
9644
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009645 err = tg3_power_up(tp);
Matt Carlson2f751b62008-08-04 23:17:34 -07009646 if (err)
Michael Chanbc1c7562006-03-20 17:48:03 -08009647 return err;
Matt Carlson2f751b62008-08-04 23:17:34 -07009648
9649 tg3_full_lock(tp, 0);
Michael Chanbc1c7562006-03-20 17:48:03 -08009650
Linus Torvalds1da177e2005-04-16 15:20:36 -07009651 tg3_disable_ints(tp);
Joe Perches63c3a662011-04-26 08:12:10 +00009652 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009653
David S. Millerf47c11e2005-06-24 20:18:35 -07009654 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009655
Matt Carlson679563f2009-09-01 12:55:46 +00009656 /*
9657 * Setup interrupts first so we know how
9658 * many NAPI resources to allocate
9659 */
9660 tg3_ints_init(tp);
9661
Linus Torvalds1da177e2005-04-16 15:20:36 -07009662 /* The placement of this call is tied
9663 * to the setup and use of Host TX descriptors.
9664 */
9665 err = tg3_alloc_consistent(tp);
9666 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009667 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009668
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009669 tg3_napi_init(tp);
9670
Matt Carlsonfed97812009-09-01 13:10:19 +00009671 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07009672
Matt Carlson4f125f42009-09-01 12:55:02 +00009673 for (i = 0; i < tp->irq_cnt; i++) {
9674 struct tg3_napi *tnapi = &tp->napi[i];
9675 err = tg3_request_irq(tp, i);
9676 if (err) {
Matt Carlson5bc09182011-11-04 09:15:01 +00009677 for (i--; i >= 0; i--) {
9678 tnapi = &tp->napi[i];
Matt Carlson4f125f42009-09-01 12:55:02 +00009679 free_irq(tnapi->irq_vec, tnapi);
Matt Carlson5bc09182011-11-04 09:15:01 +00009680 }
9681 goto err_out2;
Matt Carlson4f125f42009-09-01 12:55:02 +00009682 }
9683 }
Matt Carlson07b01732009-08-28 14:01:15 +00009684
David S. Millerf47c11e2005-06-24 20:18:35 -07009685 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009686
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009687 err = tg3_init_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009688 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -07009689 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009690 tg3_free_rings(tp);
9691 } else {
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009692 if (tg3_flag(tp, TAGGED_STATUS) &&
Matt Carlson55086ad2011-12-14 11:09:59 +00009693 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
9694 !tg3_flag(tp, 57765_CLASS))
David S. Millerfac9b832005-05-18 22:46:34 -07009695 tp->timer_offset = HZ;
9696 else
9697 tp->timer_offset = HZ / 10;
9698
9699 BUG_ON(tp->timer_offset > HZ);
9700 tp->timer_counter = tp->timer_multiplier =
9701 (HZ / tp->timer_offset);
9702 tp->asf_counter = tp->asf_multiplier =
Michael Chan28fbef72005-10-26 15:48:35 -07009703 ((HZ / tp->timer_offset) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009704
9705 init_timer(&tp->timer);
9706 tp->timer.expires = jiffies + tp->timer_offset;
9707 tp->timer.data = (unsigned long) tp;
9708 tp->timer.function = tg3_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009709 }
9710
David S. Millerf47c11e2005-06-24 20:18:35 -07009711 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009712
Matt Carlson07b01732009-08-28 14:01:15 +00009713 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009714 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009715
Joe Perches63c3a662011-04-26 08:12:10 +00009716 if (tg3_flag(tp, USING_MSI)) {
Michael Chan79381092005-04-21 17:13:59 -07009717 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -07009718
Michael Chan79381092005-04-21 17:13:59 -07009719 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -07009720 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -07009721 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -07009722 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07009723 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009724
Matt Carlson679563f2009-09-01 12:55:46 +00009725 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -07009726 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009727
Joe Perches63c3a662011-04-26 08:12:10 +00009728 if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009729 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009730
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009731 tw32(PCIE_TRANSACTION_CFG,
9732 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009733 }
Michael Chan79381092005-04-21 17:13:59 -07009734 }
9735
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009736 tg3_phy_start(tp);
9737
David S. Millerf47c11e2005-06-24 20:18:35 -07009738 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009739
Michael Chan79381092005-04-21 17:13:59 -07009740 add_timer(&tp->timer);
Joe Perches63c3a662011-04-26 08:12:10 +00009741 tg3_flag_set(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009742 tg3_enable_ints(tp);
9743
David S. Millerf47c11e2005-06-24 20:18:35 -07009744 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009745
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009746 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009747
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00009748 /*
9749 * Reset loopback feature if it was turned on while the device was down
9750 * make sure that it's installed properly now.
9751 */
9752 if (dev->features & NETIF_F_LOOPBACK)
9753 tg3_set_loopback(dev, dev->features);
9754
Linus Torvalds1da177e2005-04-16 15:20:36 -07009755 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +00009756
Matt Carlson679563f2009-09-01 12:55:46 +00009757err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +00009758 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9759 struct tg3_napi *tnapi = &tp->napi[i];
9760 free_irq(tnapi->irq_vec, tnapi);
9761 }
Matt Carlson07b01732009-08-28 14:01:15 +00009762
Matt Carlson679563f2009-09-01 12:55:46 +00009763err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +00009764 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009765 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00009766 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +00009767
9768err_out1:
9769 tg3_ints_fini(tp);
Matt Carlsoncd0d7222011-07-13 09:27:33 +00009770 tg3_frob_aux_power(tp, false);
9771 pci_set_power_state(tp->pdev, PCI_D3hot);
Matt Carlson07b01732009-08-28 14:01:15 +00009772 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009773}
9774
Linus Torvalds1da177e2005-04-16 15:20:36 -07009775static int tg3_close(struct net_device *dev)
9776{
Matt Carlson4f125f42009-09-01 12:55:02 +00009777 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009778 struct tg3 *tp = netdev_priv(dev);
9779
Matt Carlsonfed97812009-09-01 13:10:19 +00009780 tg3_napi_disable(tp);
Matt Carlsondb219972011-11-04 09:15:03 +00009781 tg3_reset_task_cancel(tp);
Michael Chan7faa0062006-02-02 17:29:28 -08009782
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009783 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009784
9785 del_timer_sync(&tp->timer);
9786
Matt Carlson24bb4fb2009-10-05 17:55:29 +00009787 tg3_phy_stop(tp);
9788
David S. Millerf47c11e2005-06-24 20:18:35 -07009789 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009790
9791 tg3_disable_ints(tp);
9792
Michael Chan944d9802005-05-29 14:57:48 -07009793 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009794 tg3_free_rings(tp);
Joe Perches63c3a662011-04-26 08:12:10 +00009795 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009796
David S. Millerf47c11e2005-06-24 20:18:35 -07009797 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009798
Matt Carlson4f125f42009-09-01 12:55:02 +00009799 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9800 struct tg3_napi *tnapi = &tp->napi[i];
9801 free_irq(tnapi->irq_vec, tnapi);
9802 }
Matt Carlson07b01732009-08-28 14:01:15 +00009803
9804 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009805
Matt Carlson92feeab2011-12-08 14:40:14 +00009806 /* Clear stats across close / open calls */
9807 memset(&tp->net_stats_prev, 0, sizeof(tp->net_stats_prev));
9808 memset(&tp->estats_prev, 0, sizeof(tp->estats_prev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07009809
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009810 tg3_napi_fini(tp);
9811
Linus Torvalds1da177e2005-04-16 15:20:36 -07009812 tg3_free_consistent(tp);
9813
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009814 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -08009815
9816 netif_carrier_off(tp->dev);
9817
Linus Torvalds1da177e2005-04-16 15:20:36 -07009818 return 0;
9819}
9820
Eric Dumazet511d2222010-07-07 20:44:24 +00009821static inline u64 get_stat64(tg3_stat64_t *val)
Stefan Buehler816f8b82008-08-15 14:10:54 -07009822{
9823 return ((u64)val->high << 32) | ((u64)val->low);
9824}
9825
Eric Dumazet511d2222010-07-07 20:44:24 +00009826static u64 calc_crc_errors(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009827{
9828 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9829
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009830 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009831 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
9832 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009833 u32 val;
9834
David S. Millerf47c11e2005-06-24 20:18:35 -07009835 spin_lock_bh(&tp->lock);
Michael Chan569a5df2007-02-13 12:18:15 -08009836 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
9837 tg3_writephy(tp, MII_TG3_TEST1,
9838 val | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009839 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009840 } else
9841 val = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07009842 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009843
9844 tp->phy_crc_errors += val;
9845
9846 return tp->phy_crc_errors;
9847 }
9848
9849 return get_stat64(&hw_stats->rx_fcs_errors);
9850}
9851
9852#define ESTAT_ADD(member) \
9853 estats->member = old_estats->member + \
Eric Dumazet511d2222010-07-07 20:44:24 +00009854 get_stat64(&hw_stats->member)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009855
Matt Carlson0e6c9da2011-12-08 14:40:13 +00009856static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *tp,
9857 struct tg3_ethtool_stats *estats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009858{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009859 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
9860 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9861
9862 if (!hw_stats)
9863 return old_estats;
9864
9865 ESTAT_ADD(rx_octets);
9866 ESTAT_ADD(rx_fragments);
9867 ESTAT_ADD(rx_ucast_packets);
9868 ESTAT_ADD(rx_mcast_packets);
9869 ESTAT_ADD(rx_bcast_packets);
9870 ESTAT_ADD(rx_fcs_errors);
9871 ESTAT_ADD(rx_align_errors);
9872 ESTAT_ADD(rx_xon_pause_rcvd);
9873 ESTAT_ADD(rx_xoff_pause_rcvd);
9874 ESTAT_ADD(rx_mac_ctrl_rcvd);
9875 ESTAT_ADD(rx_xoff_entered);
9876 ESTAT_ADD(rx_frame_too_long_errors);
9877 ESTAT_ADD(rx_jabbers);
9878 ESTAT_ADD(rx_undersize_packets);
9879 ESTAT_ADD(rx_in_length_errors);
9880 ESTAT_ADD(rx_out_length_errors);
9881 ESTAT_ADD(rx_64_or_less_octet_packets);
9882 ESTAT_ADD(rx_65_to_127_octet_packets);
9883 ESTAT_ADD(rx_128_to_255_octet_packets);
9884 ESTAT_ADD(rx_256_to_511_octet_packets);
9885 ESTAT_ADD(rx_512_to_1023_octet_packets);
9886 ESTAT_ADD(rx_1024_to_1522_octet_packets);
9887 ESTAT_ADD(rx_1523_to_2047_octet_packets);
9888 ESTAT_ADD(rx_2048_to_4095_octet_packets);
9889 ESTAT_ADD(rx_4096_to_8191_octet_packets);
9890 ESTAT_ADD(rx_8192_to_9022_octet_packets);
9891
9892 ESTAT_ADD(tx_octets);
9893 ESTAT_ADD(tx_collisions);
9894 ESTAT_ADD(tx_xon_sent);
9895 ESTAT_ADD(tx_xoff_sent);
9896 ESTAT_ADD(tx_flow_control);
9897 ESTAT_ADD(tx_mac_errors);
9898 ESTAT_ADD(tx_single_collisions);
9899 ESTAT_ADD(tx_mult_collisions);
9900 ESTAT_ADD(tx_deferred);
9901 ESTAT_ADD(tx_excessive_collisions);
9902 ESTAT_ADD(tx_late_collisions);
9903 ESTAT_ADD(tx_collide_2times);
9904 ESTAT_ADD(tx_collide_3times);
9905 ESTAT_ADD(tx_collide_4times);
9906 ESTAT_ADD(tx_collide_5times);
9907 ESTAT_ADD(tx_collide_6times);
9908 ESTAT_ADD(tx_collide_7times);
9909 ESTAT_ADD(tx_collide_8times);
9910 ESTAT_ADD(tx_collide_9times);
9911 ESTAT_ADD(tx_collide_10times);
9912 ESTAT_ADD(tx_collide_11times);
9913 ESTAT_ADD(tx_collide_12times);
9914 ESTAT_ADD(tx_collide_13times);
9915 ESTAT_ADD(tx_collide_14times);
9916 ESTAT_ADD(tx_collide_15times);
9917 ESTAT_ADD(tx_ucast_packets);
9918 ESTAT_ADD(tx_mcast_packets);
9919 ESTAT_ADD(tx_bcast_packets);
9920 ESTAT_ADD(tx_carrier_sense_errors);
9921 ESTAT_ADD(tx_discards);
9922 ESTAT_ADD(tx_errors);
9923
9924 ESTAT_ADD(dma_writeq_full);
9925 ESTAT_ADD(dma_write_prioq_full);
9926 ESTAT_ADD(rxbds_empty);
9927 ESTAT_ADD(rx_discards);
9928 ESTAT_ADD(rx_errors);
9929 ESTAT_ADD(rx_threshold_hit);
9930
9931 ESTAT_ADD(dma_readq_full);
9932 ESTAT_ADD(dma_read_prioq_full);
9933 ESTAT_ADD(tx_comp_queue_full);
9934
9935 ESTAT_ADD(ring_set_send_prod_index);
9936 ESTAT_ADD(ring_status_update);
9937 ESTAT_ADD(nic_irqs);
9938 ESTAT_ADD(nic_avoided_irqs);
9939 ESTAT_ADD(nic_tx_threshold_hit);
9940
Matt Carlson4452d092011-05-19 12:12:51 +00009941 ESTAT_ADD(mbuf_lwm_thresh_hit);
9942
Linus Torvalds1da177e2005-04-16 15:20:36 -07009943 return estats;
9944}
9945
Eric Dumazet511d2222010-07-07 20:44:24 +00009946static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
9947 struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009948{
9949 struct tg3 *tp = netdev_priv(dev);
Eric Dumazet511d2222010-07-07 20:44:24 +00009950 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009951 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9952
9953 if (!hw_stats)
9954 return old_stats;
9955
9956 stats->rx_packets = old_stats->rx_packets +
9957 get_stat64(&hw_stats->rx_ucast_packets) +
9958 get_stat64(&hw_stats->rx_mcast_packets) +
9959 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009960
Linus Torvalds1da177e2005-04-16 15:20:36 -07009961 stats->tx_packets = old_stats->tx_packets +
9962 get_stat64(&hw_stats->tx_ucast_packets) +
9963 get_stat64(&hw_stats->tx_mcast_packets) +
9964 get_stat64(&hw_stats->tx_bcast_packets);
9965
9966 stats->rx_bytes = old_stats->rx_bytes +
9967 get_stat64(&hw_stats->rx_octets);
9968 stats->tx_bytes = old_stats->tx_bytes +
9969 get_stat64(&hw_stats->tx_octets);
9970
9971 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -07009972 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009973 stats->tx_errors = old_stats->tx_errors +
9974 get_stat64(&hw_stats->tx_errors) +
9975 get_stat64(&hw_stats->tx_mac_errors) +
9976 get_stat64(&hw_stats->tx_carrier_sense_errors) +
9977 get_stat64(&hw_stats->tx_discards);
9978
9979 stats->multicast = old_stats->multicast +
9980 get_stat64(&hw_stats->rx_mcast_packets);
9981 stats->collisions = old_stats->collisions +
9982 get_stat64(&hw_stats->tx_collisions);
9983
9984 stats->rx_length_errors = old_stats->rx_length_errors +
9985 get_stat64(&hw_stats->rx_frame_too_long_errors) +
9986 get_stat64(&hw_stats->rx_undersize_packets);
9987
9988 stats->rx_over_errors = old_stats->rx_over_errors +
9989 get_stat64(&hw_stats->rxbds_empty);
9990 stats->rx_frame_errors = old_stats->rx_frame_errors +
9991 get_stat64(&hw_stats->rx_align_errors);
9992 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
9993 get_stat64(&hw_stats->tx_discards);
9994 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
9995 get_stat64(&hw_stats->tx_carrier_sense_errors);
9996
9997 stats->rx_crc_errors = old_stats->rx_crc_errors +
9998 calc_crc_errors(tp);
9999
John W. Linville4f63b872005-09-12 14:43:18 -070010000 stats->rx_missed_errors = old_stats->rx_missed_errors +
10001 get_stat64(&hw_stats->rx_discards);
10002
Eric Dumazetb0057c52010-10-10 19:55:52 +000010003 stats->rx_dropped = tp->rx_dropped;
Eric Dumazet48855432011-10-24 07:53:03 +000010004 stats->tx_dropped = tp->tx_dropped;
Eric Dumazetb0057c52010-10-10 19:55:52 +000010005
Linus Torvalds1da177e2005-04-16 15:20:36 -070010006 return stats;
10007}
10008
10009static inline u32 calc_crc(unsigned char *buf, int len)
10010{
10011 u32 reg;
10012 u32 tmp;
10013 int j, k;
10014
10015 reg = 0xffffffff;
10016
10017 for (j = 0; j < len; j++) {
10018 reg ^= buf[j];
10019
10020 for (k = 0; k < 8; k++) {
10021 tmp = reg & 0x01;
10022
10023 reg >>= 1;
10024
Matt Carlson859a588792010-04-05 10:19:28 +000010025 if (tmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010026 reg ^= 0xedb88320;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010027 }
10028 }
10029
10030 return ~reg;
10031}
10032
10033static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
10034{
10035 /* accept or reject all multicast frames */
10036 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
10037 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
10038 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
10039 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
10040}
10041
10042static void __tg3_set_rx_mode(struct net_device *dev)
10043{
10044 struct tg3 *tp = netdev_priv(dev);
10045 u32 rx_mode;
10046
10047 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
10048 RX_MODE_KEEP_VLAN_TAG);
10049
Matt Carlsonbf933c82011-01-25 15:58:49 +000010050#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010051 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
10052 * flag clear.
10053 */
Joe Perches63c3a662011-04-26 08:12:10 +000010054 if (!tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010055 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
10056#endif
10057
10058 if (dev->flags & IFF_PROMISC) {
10059 /* Promiscuous mode. */
10060 rx_mode |= RX_MODE_PROMISC;
10061 } else if (dev->flags & IFF_ALLMULTI) {
10062 /* Accept all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010063 tg3_set_multi(tp, 1);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +000010064 } else if (netdev_mc_empty(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010065 /* Reject all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010066 tg3_set_multi(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010067 } else {
10068 /* Accept one or more multicast(s). */
Jiri Pirko22bedad32010-04-01 21:22:57 +000010069 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010070 u32 mc_filter[4] = { 0, };
10071 u32 regidx;
10072 u32 bit;
10073 u32 crc;
10074
Jiri Pirko22bedad32010-04-01 21:22:57 +000010075 netdev_for_each_mc_addr(ha, dev) {
10076 crc = calc_crc(ha->addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010077 bit = ~crc & 0x7f;
10078 regidx = (bit & 0x60) >> 5;
10079 bit &= 0x1f;
10080 mc_filter[regidx] |= (1 << bit);
10081 }
10082
10083 tw32(MAC_HASH_REG_0, mc_filter[0]);
10084 tw32(MAC_HASH_REG_1, mc_filter[1]);
10085 tw32(MAC_HASH_REG_2, mc_filter[2]);
10086 tw32(MAC_HASH_REG_3, mc_filter[3]);
10087 }
10088
10089 if (rx_mode != tp->rx_mode) {
10090 tp->rx_mode = rx_mode;
10091 tw32_f(MAC_RX_MODE, rx_mode);
10092 udelay(10);
10093 }
10094}
10095
10096static void tg3_set_rx_mode(struct net_device *dev)
10097{
10098 struct tg3 *tp = netdev_priv(dev);
10099
Michael Chane75f7c92006-03-20 21:33:26 -080010100 if (!netif_running(dev))
10101 return;
10102
David S. Millerf47c11e2005-06-24 20:18:35 -070010103 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010104 __tg3_set_rx_mode(dev);
David S. Millerf47c11e2005-06-24 20:18:35 -070010105 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010106}
10107
Linus Torvalds1da177e2005-04-16 15:20:36 -070010108static int tg3_get_regs_len(struct net_device *dev)
10109{
Matt Carlson97bd8e42011-04-13 11:05:04 +000010110 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010111}
10112
10113static void tg3_get_regs(struct net_device *dev,
10114 struct ethtool_regs *regs, void *_p)
10115{
Linus Torvalds1da177e2005-04-16 15:20:36 -070010116 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010117
10118 regs->version = 0;
10119
Matt Carlson97bd8e42011-04-13 11:05:04 +000010120 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010121
Matt Carlson80096062010-08-02 11:26:06 +000010122 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010123 return;
10124
David S. Millerf47c11e2005-06-24 20:18:35 -070010125 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010126
Matt Carlson97bd8e42011-04-13 11:05:04 +000010127 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010128
David S. Millerf47c11e2005-06-24 20:18:35 -070010129 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010130}
10131
10132static int tg3_get_eeprom_len(struct net_device *dev)
10133{
10134 struct tg3 *tp = netdev_priv(dev);
10135
10136 return tp->nvram_size;
10137}
10138
Linus Torvalds1da177e2005-04-16 15:20:36 -070010139static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
10140{
10141 struct tg3 *tp = netdev_priv(dev);
10142 int ret;
10143 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -080010144 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010145 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010146
Joe Perches63c3a662011-04-26 08:12:10 +000010147 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000010148 return -EINVAL;
10149
Matt Carlson80096062010-08-02 11:26:06 +000010150 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010151 return -EAGAIN;
10152
Linus Torvalds1da177e2005-04-16 15:20:36 -070010153 offset = eeprom->offset;
10154 len = eeprom->len;
10155 eeprom->len = 0;
10156
10157 eeprom->magic = TG3_EEPROM_MAGIC;
10158
10159 if (offset & 3) {
10160 /* adjustments to start on required 4 byte boundary */
10161 b_offset = offset & 3;
10162 b_count = 4 - b_offset;
10163 if (b_count > len) {
10164 /* i.e. offset=1 len=2 */
10165 b_count = len;
10166 }
Matt Carlsona9dc5292009-02-25 14:25:30 +000010167 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010168 if (ret)
10169 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +000010170 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010171 len -= b_count;
10172 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010173 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010174 }
10175
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010176 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010177 pd = &data[eeprom->len];
10178 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010179 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010180 if (ret) {
10181 eeprom->len += i;
10182 return ret;
10183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010184 memcpy(pd + i, &val, 4);
10185 }
10186 eeprom->len += i;
10187
10188 if (len & 3) {
10189 /* read last bytes not ending on 4 byte boundary */
10190 pd = &data[eeprom->len];
10191 b_count = len & 3;
10192 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010193 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010194 if (ret)
10195 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080010196 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010197 eeprom->len += b_count;
10198 }
10199 return 0;
10200}
10201
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010202static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010203
10204static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
10205{
10206 struct tg3 *tp = netdev_priv(dev);
10207 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080010208 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010209 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010210 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010211
Matt Carlson80096062010-08-02 11:26:06 +000010212 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010213 return -EAGAIN;
10214
Joe Perches63c3a662011-04-26 08:12:10 +000010215 if (tg3_flag(tp, NO_NVRAM) ||
Matt Carlsondf259d82009-04-20 06:57:14 +000010216 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010217 return -EINVAL;
10218
10219 offset = eeprom->offset;
10220 len = eeprom->len;
10221
10222 if ((b_offset = (offset & 3))) {
10223 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010224 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010225 if (ret)
10226 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010227 len += b_offset;
10228 offset &= ~3;
Michael Chan1c8594b42005-04-21 17:12:46 -070010229 if (len < 4)
10230 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010231 }
10232
10233 odd_len = 0;
Michael Chan1c8594b42005-04-21 17:12:46 -070010234 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010235 /* adjustments to end on required 4 byte boundary */
10236 odd_len = 1;
10237 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010238 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010239 if (ret)
10240 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010241 }
10242
10243 buf = data;
10244 if (b_offset || odd_len) {
10245 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010246 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010247 return -ENOMEM;
10248 if (b_offset)
10249 memcpy(buf, &start, 4);
10250 if (odd_len)
10251 memcpy(buf+len-4, &end, 4);
10252 memcpy(buf + b_offset, data, eeprom->len);
10253 }
10254
10255 ret = tg3_nvram_write_block(tp, offset, len, buf);
10256
10257 if (buf != data)
10258 kfree(buf);
10259
10260 return ret;
10261}
10262
10263static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
10264{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010265 struct tg3 *tp = netdev_priv(dev);
10266
Joe Perches63c3a662011-04-26 08:12:10 +000010267 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010268 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010269 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010270 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010271 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10272 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010273 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010274
Linus Torvalds1da177e2005-04-16 15:20:36 -070010275 cmd->supported = (SUPPORTED_Autoneg);
10276
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010277 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010278 cmd->supported |= (SUPPORTED_1000baseT_Half |
10279 SUPPORTED_1000baseT_Full);
10280
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010281 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010282 cmd->supported |= (SUPPORTED_100baseT_Half |
10283 SUPPORTED_100baseT_Full |
10284 SUPPORTED_10baseT_Half |
10285 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -080010286 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -070010287 cmd->port = PORT_TP;
10288 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010289 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -070010290 cmd->port = PORT_FIBRE;
10291 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010292
Linus Torvalds1da177e2005-04-16 15:20:36 -070010293 cmd->advertising = tp->link_config.advertising;
Matt Carlson5bb09772011-06-13 13:39:00 +000010294 if (tg3_flag(tp, PAUSE_AUTONEG)) {
10295 if (tp->link_config.flowctrl & FLOW_CTRL_RX) {
10296 if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
10297 cmd->advertising |= ADVERTISED_Pause;
10298 } else {
10299 cmd->advertising |= ADVERTISED_Pause |
10300 ADVERTISED_Asym_Pause;
10301 }
10302 } else if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
10303 cmd->advertising |= ADVERTISED_Asym_Pause;
10304 }
10305 }
Matt Carlson859edb22011-12-08 14:40:16 +000010306 if (netif_running(dev) && netif_carrier_ok(dev)) {
David Decotigny70739492011-04-27 18:32:40 +000010307 ethtool_cmd_speed_set(cmd, tp->link_config.active_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010308 cmd->duplex = tp->link_config.active_duplex;
Matt Carlson859edb22011-12-08 14:40:16 +000010309 cmd->lp_advertising = tp->link_config.rmt_adv;
Matt Carlsone348c5e2011-11-21 15:01:20 +000010310 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
10311 if (tp->phy_flags & TG3_PHYFLG_MDIX_STATE)
10312 cmd->eth_tp_mdix = ETH_TP_MDI_X;
10313 else
10314 cmd->eth_tp_mdix = ETH_TP_MDI;
10315 }
Matt Carlson64c22182010-10-14 10:37:44 +000010316 } else {
David Decotigny70739492011-04-27 18:32:40 +000010317 ethtool_cmd_speed_set(cmd, SPEED_INVALID);
Matt Carlson64c22182010-10-14 10:37:44 +000010318 cmd->duplex = DUPLEX_INVALID;
Matt Carlsone348c5e2011-11-21 15:01:20 +000010319 cmd->eth_tp_mdix = ETH_TP_MDI_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010320 }
Matt Carlson882e9792009-09-01 13:21:36 +000010321 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +000010322 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010323 cmd->autoneg = tp->link_config.autoneg;
10324 cmd->maxtxpkt = 0;
10325 cmd->maxrxpkt = 0;
10326 return 0;
10327}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010328
Linus Torvalds1da177e2005-04-16 15:20:36 -070010329static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
10330{
10331 struct tg3 *tp = netdev_priv(dev);
David Decotigny25db0332011-04-27 18:32:39 +000010332 u32 speed = ethtool_cmd_speed(cmd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010333
Joe Perches63c3a662011-04-26 08:12:10 +000010334 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010335 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010336 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010337 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010338 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10339 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010340 }
10341
Matt Carlson7e5856b2009-02-25 14:23:01 +000010342 if (cmd->autoneg != AUTONEG_ENABLE &&
10343 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -070010344 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +000010345
10346 if (cmd->autoneg == AUTONEG_DISABLE &&
10347 cmd->duplex != DUPLEX_FULL &&
10348 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -070010349 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010350
Matt Carlson7e5856b2009-02-25 14:23:01 +000010351 if (cmd->autoneg == AUTONEG_ENABLE) {
10352 u32 mask = ADVERTISED_Autoneg |
10353 ADVERTISED_Pause |
10354 ADVERTISED_Asym_Pause;
10355
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010356 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010357 mask |= ADVERTISED_1000baseT_Half |
10358 ADVERTISED_1000baseT_Full;
10359
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010360 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010361 mask |= ADVERTISED_100baseT_Half |
10362 ADVERTISED_100baseT_Full |
10363 ADVERTISED_10baseT_Half |
10364 ADVERTISED_10baseT_Full |
10365 ADVERTISED_TP;
10366 else
10367 mask |= ADVERTISED_FIBRE;
10368
10369 if (cmd->advertising & ~mask)
10370 return -EINVAL;
10371
10372 mask &= (ADVERTISED_1000baseT_Half |
10373 ADVERTISED_1000baseT_Full |
10374 ADVERTISED_100baseT_Half |
10375 ADVERTISED_100baseT_Full |
10376 ADVERTISED_10baseT_Half |
10377 ADVERTISED_10baseT_Full);
10378
10379 cmd->advertising &= mask;
10380 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010381 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
David Decotigny25db0332011-04-27 18:32:39 +000010382 if (speed != SPEED_1000)
Matt Carlson7e5856b2009-02-25 14:23:01 +000010383 return -EINVAL;
10384
10385 if (cmd->duplex != DUPLEX_FULL)
10386 return -EINVAL;
10387 } else {
David Decotigny25db0332011-04-27 18:32:39 +000010388 if (speed != SPEED_100 &&
10389 speed != SPEED_10)
Matt Carlson7e5856b2009-02-25 14:23:01 +000010390 return -EINVAL;
10391 }
10392 }
10393
David S. Millerf47c11e2005-06-24 20:18:35 -070010394 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010395
10396 tp->link_config.autoneg = cmd->autoneg;
10397 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -070010398 tp->link_config.advertising = (cmd->advertising |
10399 ADVERTISED_Autoneg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010400 tp->link_config.speed = SPEED_INVALID;
10401 tp->link_config.duplex = DUPLEX_INVALID;
10402 } else {
10403 tp->link_config.advertising = 0;
David Decotigny25db0332011-04-27 18:32:39 +000010404 tp->link_config.speed = speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010405 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010406 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010407
Michael Chan24fcad62006-12-17 17:06:46 -080010408 tp->link_config.orig_speed = tp->link_config.speed;
10409 tp->link_config.orig_duplex = tp->link_config.duplex;
10410 tp->link_config.orig_autoneg = tp->link_config.autoneg;
10411
Linus Torvalds1da177e2005-04-16 15:20:36 -070010412 if (netif_running(dev))
10413 tg3_setup_phy(tp, 1);
10414
David S. Millerf47c11e2005-06-24 20:18:35 -070010415 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010416
Linus Torvalds1da177e2005-04-16 15:20:36 -070010417 return 0;
10418}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010419
Linus Torvalds1da177e2005-04-16 15:20:36 -070010420static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
10421{
10422 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010423
Rick Jones68aad782011-11-07 13:29:27 +000010424 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
10425 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
10426 strlcpy(info->fw_version, tp->fw_ver, sizeof(info->fw_version));
10427 strlcpy(info->bus_info, pci_name(tp->pdev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010428}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010429
Linus Torvalds1da177e2005-04-16 15:20:36 -070010430static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10431{
10432 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010433
Joe Perches63c3a662011-04-26 08:12:10 +000010434 if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070010435 wol->supported = WAKE_MAGIC;
10436 else
10437 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010438 wol->wolopts = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010439 if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010440 wol->wolopts = WAKE_MAGIC;
10441 memset(&wol->sopass, 0, sizeof(wol->sopass));
10442}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010443
Linus Torvalds1da177e2005-04-16 15:20:36 -070010444static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10445{
10446 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070010447 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010448
Linus Torvalds1da177e2005-04-16 15:20:36 -070010449 if (wol->wolopts & ~WAKE_MAGIC)
10450 return -EINVAL;
10451 if ((wol->wolopts & WAKE_MAGIC) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010452 !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010453 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010454
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010455 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
10456
David S. Millerf47c11e2005-06-24 20:18:35 -070010457 spin_lock_bh(&tp->lock);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010458 if (device_may_wakeup(dp))
Joe Perches63c3a662011-04-26 08:12:10 +000010459 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010460 else
Joe Perches63c3a662011-04-26 08:12:10 +000010461 tg3_flag_clear(tp, WOL_ENABLE);
David S. Millerf47c11e2005-06-24 20:18:35 -070010462 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010463
Linus Torvalds1da177e2005-04-16 15:20:36 -070010464 return 0;
10465}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010466
Linus Torvalds1da177e2005-04-16 15:20:36 -070010467static u32 tg3_get_msglevel(struct net_device *dev)
10468{
10469 struct tg3 *tp = netdev_priv(dev);
10470 return tp->msg_enable;
10471}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010472
Linus Torvalds1da177e2005-04-16 15:20:36 -070010473static void tg3_set_msglevel(struct net_device *dev, u32 value)
10474{
10475 struct tg3 *tp = netdev_priv(dev);
10476 tp->msg_enable = value;
10477}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010478
Linus Torvalds1da177e2005-04-16 15:20:36 -070010479static int tg3_nway_reset(struct net_device *dev)
10480{
10481 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010482 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010483
Linus Torvalds1da177e2005-04-16 15:20:36 -070010484 if (!netif_running(dev))
10485 return -EAGAIN;
10486
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010487 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070010488 return -EINVAL;
10489
Joe Perches63c3a662011-04-26 08:12:10 +000010490 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010491 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010492 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010493 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010494 } else {
10495 u32 bmcr;
10496
10497 spin_lock_bh(&tp->lock);
10498 r = -EINVAL;
10499 tg3_readphy(tp, MII_BMCR, &bmcr);
10500 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
10501 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010502 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010503 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
10504 BMCR_ANENABLE);
10505 r = 0;
10506 }
10507 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010508 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010509
Linus Torvalds1da177e2005-04-16 15:20:36 -070010510 return r;
10511}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010512
Linus Torvalds1da177e2005-04-16 15:20:36 -070010513static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10514{
10515 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010516
Matt Carlson2c49a442010-09-30 10:34:35 +000010517 ering->rx_max_pending = tp->rx_std_ring_mask;
Joe Perches63c3a662011-04-26 08:12:10 +000010518 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson2c49a442010-09-30 10:34:35 +000010519 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080010520 else
10521 ering->rx_jumbo_max_pending = 0;
10522
10523 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010524
10525 ering->rx_pending = tp->rx_pending;
Joe Perches63c3a662011-04-26 08:12:10 +000010526 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Michael Chan4f81c322006-03-20 21:33:42 -080010527 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
10528 else
10529 ering->rx_jumbo_pending = 0;
10530
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010531 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010532}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010533
Linus Torvalds1da177e2005-04-16 15:20:36 -070010534static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10535{
10536 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000010537 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010538
Matt Carlson2c49a442010-09-30 10:34:35 +000010539 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
10540 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070010541 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
10542 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Joe Perches63c3a662011-04-26 08:12:10 +000010543 (tg3_flag(tp, TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070010544 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010545 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010546
Michael Chanbbe832c2005-06-24 20:20:04 -070010547 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010548 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010549 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010550 irq_sync = 1;
10551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010552
Michael Chanbbe832c2005-06-24 20:20:04 -070010553 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010554
Linus Torvalds1da177e2005-04-16 15:20:36 -070010555 tp->rx_pending = ering->rx_pending;
10556
Joe Perches63c3a662011-04-26 08:12:10 +000010557 if (tg3_flag(tp, MAX_RXPEND_64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010558 tp->rx_pending > 63)
10559 tp->rx_pending = 63;
10560 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000010561
Matt Carlson6fd45cb2010-09-15 08:59:57 +000010562 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000010563 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010564
10565 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070010566 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -070010567 err = tg3_restart_hw(tp, 1);
10568 if (!err)
10569 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010570 }
10571
David S. Millerf47c11e2005-06-24 20:18:35 -070010572 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010573
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010574 if (irq_sync && !err)
10575 tg3_phy_start(tp);
10576
Michael Chanb9ec6c12006-07-25 16:37:27 -070010577 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010578}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010579
Linus Torvalds1da177e2005-04-16 15:20:36 -070010580static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10581{
10582 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010583
Joe Perches63c3a662011-04-26 08:12:10 +000010584 epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
Matt Carlson8d018622007-12-20 20:05:44 -080010585
Matt Carlson4a2db502011-12-08 14:40:17 +000010586 if (tp->link_config.flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080010587 epause->rx_pause = 1;
10588 else
10589 epause->rx_pause = 0;
10590
Matt Carlson4a2db502011-12-08 14:40:17 +000010591 if (tp->link_config.flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080010592 epause->tx_pause = 1;
10593 else
10594 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010595}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010596
Linus Torvalds1da177e2005-04-16 15:20:36 -070010597static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10598{
10599 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010600 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010601
Joe Perches63c3a662011-04-26 08:12:10 +000010602 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson27121682010-02-17 15:16:57 +000010603 u32 newadv;
10604 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010605
Matt Carlson27121682010-02-17 15:16:57 +000010606 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010607
Matt Carlson27121682010-02-17 15:16:57 +000010608 if (!(phydev->supported & SUPPORTED_Pause) ||
10609 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000010610 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000010611 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010612
Matt Carlson27121682010-02-17 15:16:57 +000010613 tp->link_config.flowctrl = 0;
10614 if (epause->rx_pause) {
10615 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010616
Matt Carlson27121682010-02-17 15:16:57 +000010617 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080010618 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000010619 newadv = ADVERTISED_Pause;
10620 } else
10621 newadv = ADVERTISED_Pause |
10622 ADVERTISED_Asym_Pause;
10623 } else if (epause->tx_pause) {
10624 tp->link_config.flowctrl |= FLOW_CTRL_TX;
10625 newadv = ADVERTISED_Asym_Pause;
10626 } else
10627 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010628
Matt Carlson27121682010-02-17 15:16:57 +000010629 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010630 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010631 else
Joe Perches63c3a662011-04-26 08:12:10 +000010632 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010633
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010634 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000010635 u32 oldadv = phydev->advertising &
10636 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
10637 if (oldadv != newadv) {
10638 phydev->advertising &=
10639 ~(ADVERTISED_Pause |
10640 ADVERTISED_Asym_Pause);
10641 phydev->advertising |= newadv;
10642 if (phydev->autoneg) {
10643 /*
10644 * Always renegotiate the link to
10645 * inform our link partner of our
10646 * flow control settings, even if the
10647 * flow control is forced. Let
10648 * tg3_adjust_link() do the final
10649 * flow control setup.
10650 */
10651 return phy_start_aneg(phydev);
10652 }
10653 }
10654
10655 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010656 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000010657 } else {
10658 tp->link_config.orig_advertising &=
10659 ~(ADVERTISED_Pause |
10660 ADVERTISED_Asym_Pause);
10661 tp->link_config.orig_advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010662 }
10663 } else {
10664 int irq_sync = 0;
10665
10666 if (netif_running(dev)) {
10667 tg3_netif_stop(tp);
10668 irq_sync = 1;
10669 }
10670
10671 tg3_full_lock(tp, irq_sync);
10672
10673 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010674 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010675 else
Joe Perches63c3a662011-04-26 08:12:10 +000010676 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010677 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010678 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010679 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010680 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010681 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010682 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010683 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010684 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010685
10686 if (netif_running(dev)) {
10687 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10688 err = tg3_restart_hw(tp, 1);
10689 if (!err)
10690 tg3_netif_start(tp);
10691 }
10692
10693 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010695
Michael Chanb9ec6c12006-07-25 16:37:27 -070010696 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010697}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010698
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010699static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010700{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010701 switch (sset) {
10702 case ETH_SS_TEST:
10703 return TG3_NUM_TEST;
10704 case ETH_SS_STATS:
10705 return TG3_NUM_STATS;
10706 default:
10707 return -EOPNOTSUPP;
10708 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070010709}
10710
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010711static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010712{
10713 switch (stringset) {
10714 case ETH_SS_STATS:
10715 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
10716 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070010717 case ETH_SS_TEST:
10718 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
10719 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010720 default:
10721 WARN_ON(1); /* we need a WARN() */
10722 break;
10723 }
10724}
10725
stephen hemminger81b87092011-04-04 08:43:50 +000010726static int tg3_set_phys_id(struct net_device *dev,
10727 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070010728{
10729 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070010730
10731 if (!netif_running(tp->dev))
10732 return -EAGAIN;
10733
stephen hemminger81b87092011-04-04 08:43:50 +000010734 switch (state) {
10735 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +000010736 return 1; /* cycle on/off once per second */
Michael Chan4009a932005-09-05 17:52:54 -070010737
stephen hemminger81b87092011-04-04 08:43:50 +000010738 case ETHTOOL_ID_ON:
10739 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10740 LED_CTRL_1000MBPS_ON |
10741 LED_CTRL_100MBPS_ON |
10742 LED_CTRL_10MBPS_ON |
10743 LED_CTRL_TRAFFIC_OVERRIDE |
10744 LED_CTRL_TRAFFIC_BLINK |
10745 LED_CTRL_TRAFFIC_LED);
10746 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010747
stephen hemminger81b87092011-04-04 08:43:50 +000010748 case ETHTOOL_ID_OFF:
10749 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10750 LED_CTRL_TRAFFIC_OVERRIDE);
10751 break;
Michael Chan4009a932005-09-05 17:52:54 -070010752
stephen hemminger81b87092011-04-04 08:43:50 +000010753 case ETHTOOL_ID_INACTIVE:
10754 tw32(MAC_LED_CTRL, tp->led_ctrl);
10755 break;
Michael Chan4009a932005-09-05 17:52:54 -070010756 }
stephen hemminger81b87092011-04-04 08:43:50 +000010757
Michael Chan4009a932005-09-05 17:52:54 -070010758 return 0;
10759}
10760
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010761static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010762 struct ethtool_stats *estats, u64 *tmp_stats)
10763{
10764 struct tg3 *tp = netdev_priv(dev);
Matt Carlson0e6c9da2011-12-08 14:40:13 +000010765
10766 tg3_get_estats(tp, (struct tg3_ethtool_stats *)tmp_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010767}
10768
Matt Carlson535a4902011-07-20 10:20:56 +000010769static __be32 *tg3_vpd_readblock(struct tg3 *tp, u32 *vpdlen)
Matt Carlsonc3e94502011-04-13 11:05:08 +000010770{
10771 int i;
10772 __be32 *buf;
10773 u32 offset = 0, len = 0;
10774 u32 magic, val;
10775
Joe Perches63c3a662011-04-26 08:12:10 +000010776 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
Matt Carlsonc3e94502011-04-13 11:05:08 +000010777 return NULL;
10778
10779 if (magic == TG3_EEPROM_MAGIC) {
10780 for (offset = TG3_NVM_DIR_START;
10781 offset < TG3_NVM_DIR_END;
10782 offset += TG3_NVM_DIRENT_SIZE) {
10783 if (tg3_nvram_read(tp, offset, &val))
10784 return NULL;
10785
10786 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
10787 TG3_NVM_DIRTYPE_EXTVPD)
10788 break;
10789 }
10790
10791 if (offset != TG3_NVM_DIR_END) {
10792 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
10793 if (tg3_nvram_read(tp, offset + 4, &offset))
10794 return NULL;
10795
10796 offset = tg3_nvram_logical_addr(tp, offset);
10797 }
10798 }
10799
10800 if (!offset || !len) {
10801 offset = TG3_NVM_VPD_OFF;
10802 len = TG3_NVM_VPD_LEN;
10803 }
10804
10805 buf = kmalloc(len, GFP_KERNEL);
10806 if (buf == NULL)
10807 return NULL;
10808
10809 if (magic == TG3_EEPROM_MAGIC) {
10810 for (i = 0; i < len; i += 4) {
10811 /* The data is in little-endian format in NVRAM.
10812 * Use the big-endian read routines to preserve
10813 * the byte order as it exists in NVRAM.
10814 */
10815 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
10816 goto error;
10817 }
10818 } else {
10819 u8 *ptr;
10820 ssize_t cnt;
10821 unsigned int pos = 0;
10822
10823 ptr = (u8 *)&buf[0];
10824 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
10825 cnt = pci_read_vpd(tp->pdev, pos,
10826 len - pos, ptr);
10827 if (cnt == -ETIMEDOUT || cnt == -EINTR)
10828 cnt = 0;
10829 else if (cnt < 0)
10830 goto error;
10831 }
10832 if (pos != len)
10833 goto error;
10834 }
10835
Matt Carlson535a4902011-07-20 10:20:56 +000010836 *vpdlen = len;
10837
Matt Carlsonc3e94502011-04-13 11:05:08 +000010838 return buf;
10839
10840error:
10841 kfree(buf);
10842 return NULL;
10843}
10844
Michael Chan566f86a2005-05-29 14:56:58 -070010845#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080010846#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
10847#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
10848#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Matt Carlson727a6d92011-06-13 13:38:58 +000010849#define NVRAM_SELFBOOT_FORMAT1_4_SIZE 0x20
10850#define NVRAM_SELFBOOT_FORMAT1_5_SIZE 0x24
Matt Carlsonbda18fa2011-07-20 10:20:57 +000010851#define NVRAM_SELFBOOT_FORMAT1_6_SIZE 0x50
Michael Chanb16250e2006-09-27 16:10:14 -070010852#define NVRAM_SELFBOOT_HW_SIZE 0x20
10853#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070010854
10855static int tg3_test_nvram(struct tg3 *tp)
10856{
Matt Carlson535a4902011-07-20 10:20:56 +000010857 u32 csum, magic, len;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010858 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010859 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070010860
Joe Perches63c3a662011-04-26 08:12:10 +000010861 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000010862 return 0;
10863
Matt Carlsone4f34112009-02-25 14:25:00 +000010864 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080010865 return -EIO;
10866
Michael Chan1b277772006-03-20 22:27:48 -080010867 if (magic == TG3_EEPROM_MAGIC)
10868 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070010869 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080010870 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
10871 TG3_EEPROM_SB_FORMAT_1) {
10872 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
10873 case TG3_EEPROM_SB_REVISION_0:
10874 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
10875 break;
10876 case TG3_EEPROM_SB_REVISION_2:
10877 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
10878 break;
10879 case TG3_EEPROM_SB_REVISION_3:
10880 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
10881 break;
Matt Carlson727a6d92011-06-13 13:38:58 +000010882 case TG3_EEPROM_SB_REVISION_4:
10883 size = NVRAM_SELFBOOT_FORMAT1_4_SIZE;
10884 break;
10885 case TG3_EEPROM_SB_REVISION_5:
10886 size = NVRAM_SELFBOOT_FORMAT1_5_SIZE;
10887 break;
10888 case TG3_EEPROM_SB_REVISION_6:
10889 size = NVRAM_SELFBOOT_FORMAT1_6_SIZE;
10890 break;
Matt Carlsona5767de2007-11-12 21:10:58 -080010891 default:
Matt Carlson727a6d92011-06-13 13:38:58 +000010892 return -EIO;
Matt Carlsona5767de2007-11-12 21:10:58 -080010893 }
10894 } else
Michael Chan1b277772006-03-20 22:27:48 -080010895 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070010896 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
10897 size = NVRAM_SELFBOOT_HW_SIZE;
10898 else
Michael Chan1b277772006-03-20 22:27:48 -080010899 return -EIO;
10900
10901 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070010902 if (buf == NULL)
10903 return -ENOMEM;
10904
Michael Chan1b277772006-03-20 22:27:48 -080010905 err = -EIO;
10906 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010907 err = tg3_nvram_read_be32(tp, i, &buf[j]);
10908 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070010909 break;
Michael Chan566f86a2005-05-29 14:56:58 -070010910 }
Michael Chan1b277772006-03-20 22:27:48 -080010911 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070010912 goto out;
10913
Michael Chan1b277772006-03-20 22:27:48 -080010914 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010915 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080010916 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010917 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080010918 u8 *buf8 = (u8 *) buf, csum8 = 0;
10919
Al Virob9fc7dc2007-12-17 22:59:57 -080010920 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080010921 TG3_EEPROM_SB_REVISION_2) {
10922 /* For rev 2, the csum doesn't include the MBA. */
10923 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
10924 csum8 += buf8[i];
10925 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
10926 csum8 += buf8[i];
10927 } else {
10928 for (i = 0; i < size; i++)
10929 csum8 += buf8[i];
10930 }
Michael Chan1b277772006-03-20 22:27:48 -080010931
Adrian Bunkad96b482006-04-05 22:21:04 -070010932 if (csum8 == 0) {
10933 err = 0;
10934 goto out;
10935 }
10936
10937 err = -EIO;
10938 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080010939 }
Michael Chan566f86a2005-05-29 14:56:58 -070010940
Al Virob9fc7dc2007-12-17 22:59:57 -080010941 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010942 TG3_EEPROM_MAGIC_HW) {
10943 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000010944 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070010945 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070010946
10947 /* Separate the parity bits and the data bytes. */
10948 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
10949 if ((i == 0) || (i == 8)) {
10950 int l;
10951 u8 msk;
10952
10953 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
10954 parity[k++] = buf8[i] & msk;
10955 i++;
Matt Carlson859a588792010-04-05 10:19:28 +000010956 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070010957 int l;
10958 u8 msk;
10959
10960 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
10961 parity[k++] = buf8[i] & msk;
10962 i++;
10963
10964 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
10965 parity[k++] = buf8[i] & msk;
10966 i++;
10967 }
10968 data[j++] = buf8[i];
10969 }
10970
10971 err = -EIO;
10972 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
10973 u8 hw8 = hweight8(data[i]);
10974
10975 if ((hw8 & 0x1) && parity[i])
10976 goto out;
10977 else if (!(hw8 & 0x1) && !parity[i])
10978 goto out;
10979 }
10980 err = 0;
10981 goto out;
10982 }
10983
Matt Carlson01c3a392011-03-09 16:58:20 +000010984 err = -EIO;
10985
Michael Chan566f86a2005-05-29 14:56:58 -070010986 /* Bootstrap checksum at offset 0x10 */
10987 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000010988 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070010989 goto out;
10990
10991 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
10992 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000010993 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000010994 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070010995
Matt Carlsonc3e94502011-04-13 11:05:08 +000010996 kfree(buf);
10997
Matt Carlson535a4902011-07-20 10:20:56 +000010998 buf = tg3_vpd_readblock(tp, &len);
Matt Carlsonc3e94502011-04-13 11:05:08 +000010999 if (!buf)
11000 return -ENOMEM;
Matt Carlsond4894f32011-03-09 16:58:21 +000011001
Matt Carlson535a4902011-07-20 10:20:56 +000011002 i = pci_vpd_find_tag((u8 *)buf, 0, len, PCI_VPD_LRDT_RO_DATA);
Matt Carlsond4894f32011-03-09 16:58:21 +000011003 if (i > 0) {
11004 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
11005 if (j < 0)
11006 goto out;
11007
Matt Carlson535a4902011-07-20 10:20:56 +000011008 if (i + PCI_VPD_LRDT_TAG_SIZE + j > len)
Matt Carlsond4894f32011-03-09 16:58:21 +000011009 goto out;
11010
11011 i += PCI_VPD_LRDT_TAG_SIZE;
11012 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
11013 PCI_VPD_RO_KEYWORD_CHKSUM);
11014 if (j > 0) {
11015 u8 csum8 = 0;
11016
11017 j += PCI_VPD_INFO_FLD_HDR_SIZE;
11018
11019 for (i = 0; i <= j; i++)
11020 csum8 += ((u8 *)buf)[i];
11021
11022 if (csum8)
11023 goto out;
11024 }
11025 }
11026
Michael Chan566f86a2005-05-29 14:56:58 -070011027 err = 0;
11028
11029out:
11030 kfree(buf);
11031 return err;
11032}
11033
Michael Chanca430072005-05-29 14:57:23 -070011034#define TG3_SERDES_TIMEOUT_SEC 2
11035#define TG3_COPPER_TIMEOUT_SEC 6
11036
11037static int tg3_test_link(struct tg3 *tp)
11038{
11039 int i, max;
11040
11041 if (!netif_running(tp->dev))
11042 return -ENODEV;
11043
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011044 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070011045 max = TG3_SERDES_TIMEOUT_SEC;
11046 else
11047 max = TG3_COPPER_TIMEOUT_SEC;
11048
11049 for (i = 0; i < max; i++) {
11050 if (netif_carrier_ok(tp->dev))
11051 return 0;
11052
11053 if (msleep_interruptible(1000))
11054 break;
11055 }
11056
11057 return -EIO;
11058}
11059
Michael Chana71116d2005-05-29 14:58:11 -070011060/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080011061static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070011062{
Michael Chanb16250e2006-09-27 16:10:14 -070011063 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070011064 u32 offset, read_mask, write_mask, val, save_val, read_val;
11065 static struct {
11066 u16 offset;
11067 u16 flags;
11068#define TG3_FL_5705 0x1
11069#define TG3_FL_NOT_5705 0x2
11070#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070011071#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070011072 u32 read_mask;
11073 u32 write_mask;
11074 } reg_tbl[] = {
11075 /* MAC Control Registers */
11076 { MAC_MODE, TG3_FL_NOT_5705,
11077 0x00000000, 0x00ef6f8c },
11078 { MAC_MODE, TG3_FL_5705,
11079 0x00000000, 0x01ef6b8c },
11080 { MAC_STATUS, TG3_FL_NOT_5705,
11081 0x03800107, 0x00000000 },
11082 { MAC_STATUS, TG3_FL_5705,
11083 0x03800100, 0x00000000 },
11084 { MAC_ADDR_0_HIGH, 0x0000,
11085 0x00000000, 0x0000ffff },
11086 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011087 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070011088 { MAC_RX_MTU_SIZE, 0x0000,
11089 0x00000000, 0x0000ffff },
11090 { MAC_TX_MODE, 0x0000,
11091 0x00000000, 0x00000070 },
11092 { MAC_TX_LENGTHS, 0x0000,
11093 0x00000000, 0x00003fff },
11094 { MAC_RX_MODE, TG3_FL_NOT_5705,
11095 0x00000000, 0x000007fc },
11096 { MAC_RX_MODE, TG3_FL_5705,
11097 0x00000000, 0x000007dc },
11098 { MAC_HASH_REG_0, 0x0000,
11099 0x00000000, 0xffffffff },
11100 { MAC_HASH_REG_1, 0x0000,
11101 0x00000000, 0xffffffff },
11102 { MAC_HASH_REG_2, 0x0000,
11103 0x00000000, 0xffffffff },
11104 { MAC_HASH_REG_3, 0x0000,
11105 0x00000000, 0xffffffff },
11106
11107 /* Receive Data and Receive BD Initiator Control Registers. */
11108 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
11109 0x00000000, 0xffffffff },
11110 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
11111 0x00000000, 0xffffffff },
11112 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
11113 0x00000000, 0x00000003 },
11114 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
11115 0x00000000, 0xffffffff },
11116 { RCVDBDI_STD_BD+0, 0x0000,
11117 0x00000000, 0xffffffff },
11118 { RCVDBDI_STD_BD+4, 0x0000,
11119 0x00000000, 0xffffffff },
11120 { RCVDBDI_STD_BD+8, 0x0000,
11121 0x00000000, 0xffff0002 },
11122 { RCVDBDI_STD_BD+0xc, 0x0000,
11123 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011124
Michael Chana71116d2005-05-29 14:58:11 -070011125 /* Receive BD Initiator Control Registers. */
11126 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
11127 0x00000000, 0xffffffff },
11128 { RCVBDI_STD_THRESH, TG3_FL_5705,
11129 0x00000000, 0x000003ff },
11130 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
11131 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011132
Michael Chana71116d2005-05-29 14:58:11 -070011133 /* Host Coalescing Control Registers. */
11134 { HOSTCC_MODE, TG3_FL_NOT_5705,
11135 0x00000000, 0x00000004 },
11136 { HOSTCC_MODE, TG3_FL_5705,
11137 0x00000000, 0x000000f6 },
11138 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
11139 0x00000000, 0xffffffff },
11140 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
11141 0x00000000, 0x000003ff },
11142 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
11143 0x00000000, 0xffffffff },
11144 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
11145 0x00000000, 0x000003ff },
11146 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
11147 0x00000000, 0xffffffff },
11148 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
11149 0x00000000, 0x000000ff },
11150 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
11151 0x00000000, 0xffffffff },
11152 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
11153 0x00000000, 0x000000ff },
11154 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
11155 0x00000000, 0xffffffff },
11156 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
11157 0x00000000, 0xffffffff },
11158 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
11159 0x00000000, 0xffffffff },
11160 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
11161 0x00000000, 0x000000ff },
11162 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
11163 0x00000000, 0xffffffff },
11164 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
11165 0x00000000, 0x000000ff },
11166 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
11167 0x00000000, 0xffffffff },
11168 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
11169 0x00000000, 0xffffffff },
11170 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
11171 0x00000000, 0xffffffff },
11172 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
11173 0x00000000, 0xffffffff },
11174 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
11175 0x00000000, 0xffffffff },
11176 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
11177 0xffffffff, 0x00000000 },
11178 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
11179 0xffffffff, 0x00000000 },
11180
11181 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070011182 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070011183 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070011184 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070011185 0x00000000, 0x007fffff },
11186 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
11187 0x00000000, 0x0000003f },
11188 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
11189 0x00000000, 0x000001ff },
11190 { BUFMGR_MB_HIGH_WATER, 0x0000,
11191 0x00000000, 0x000001ff },
11192 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
11193 0xffffffff, 0x00000000 },
11194 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
11195 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011196
Michael Chana71116d2005-05-29 14:58:11 -070011197 /* Mailbox Registers */
11198 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
11199 0x00000000, 0x000001ff },
11200 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
11201 0x00000000, 0x000001ff },
11202 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
11203 0x00000000, 0x000007ff },
11204 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
11205 0x00000000, 0x000001ff },
11206
11207 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
11208 };
11209
Michael Chanb16250e2006-09-27 16:10:14 -070011210 is_5705 = is_5750 = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000011211 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chana71116d2005-05-29 14:58:11 -070011212 is_5705 = 1;
Joe Perches63c3a662011-04-26 08:12:10 +000011213 if (tg3_flag(tp, 5750_PLUS))
Michael Chanb16250e2006-09-27 16:10:14 -070011214 is_5750 = 1;
11215 }
Michael Chana71116d2005-05-29 14:58:11 -070011216
11217 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
11218 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
11219 continue;
11220
11221 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
11222 continue;
11223
Joe Perches63c3a662011-04-26 08:12:10 +000011224 if (tg3_flag(tp, IS_5788) &&
Michael Chana71116d2005-05-29 14:58:11 -070011225 (reg_tbl[i].flags & TG3_FL_NOT_5788))
11226 continue;
11227
Michael Chanb16250e2006-09-27 16:10:14 -070011228 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
11229 continue;
11230
Michael Chana71116d2005-05-29 14:58:11 -070011231 offset = (u32) reg_tbl[i].offset;
11232 read_mask = reg_tbl[i].read_mask;
11233 write_mask = reg_tbl[i].write_mask;
11234
11235 /* Save the original register content */
11236 save_val = tr32(offset);
11237
11238 /* Determine the read-only value. */
11239 read_val = save_val & read_mask;
11240
11241 /* Write zero to the register, then make sure the read-only bits
11242 * are not changed and the read/write bits are all zeros.
11243 */
11244 tw32(offset, 0);
11245
11246 val = tr32(offset);
11247
11248 /* Test the read-only and read/write bits. */
11249 if (((val & read_mask) != read_val) || (val & write_mask))
11250 goto out;
11251
11252 /* Write ones to all the bits defined by RdMask and WrMask, then
11253 * make sure the read-only bits are not changed and the
11254 * read/write bits are all ones.
11255 */
11256 tw32(offset, read_mask | write_mask);
11257
11258 val = tr32(offset);
11259
11260 /* Test the read-only bits. */
11261 if ((val & read_mask) != read_val)
11262 goto out;
11263
11264 /* Test the read/write bits. */
11265 if ((val & write_mask) != write_mask)
11266 goto out;
11267
11268 tw32(offset, save_val);
11269 }
11270
11271 return 0;
11272
11273out:
Michael Chan9f88f292006-12-07 00:22:54 -080011274 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000011275 netdev_err(tp->dev,
11276 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070011277 tw32(offset, save_val);
11278 return -EIO;
11279}
11280
Michael Chan7942e1d2005-05-29 14:58:36 -070011281static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
11282{
Arjan van de Venf71e1302006-03-03 21:33:57 -050011283 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070011284 int i;
11285 u32 j;
11286
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020011287 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070011288 for (j = 0; j < len; j += 4) {
11289 u32 val;
11290
11291 tg3_write_mem(tp, offset + j, test_pattern[i]);
11292 tg3_read_mem(tp, offset + j, &val);
11293 if (val != test_pattern[i])
11294 return -EIO;
11295 }
11296 }
11297 return 0;
11298}
11299
11300static int tg3_test_memory(struct tg3 *tp)
11301{
11302 static struct mem_entry {
11303 u32 offset;
11304 u32 len;
11305 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080011306 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070011307 { 0x00002000, 0x1c000},
11308 { 0xffffffff, 0x00000}
11309 }, mem_tbl_5705[] = {
11310 { 0x00000100, 0x0000c},
11311 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070011312 { 0x00004000, 0x00800},
11313 { 0x00006000, 0x01000},
11314 { 0x00008000, 0x02000},
11315 { 0x00010000, 0x0e000},
11316 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080011317 }, mem_tbl_5755[] = {
11318 { 0x00000200, 0x00008},
11319 { 0x00004000, 0x00800},
11320 { 0x00006000, 0x00800},
11321 { 0x00008000, 0x02000},
11322 { 0x00010000, 0x0c000},
11323 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070011324 }, mem_tbl_5906[] = {
11325 { 0x00000200, 0x00008},
11326 { 0x00004000, 0x00400},
11327 { 0x00006000, 0x00400},
11328 { 0x00008000, 0x01000},
11329 { 0x00010000, 0x01000},
11330 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011331 }, mem_tbl_5717[] = {
11332 { 0x00000200, 0x00008},
11333 { 0x00010000, 0x0a000},
11334 { 0x00020000, 0x13c00},
11335 { 0xffffffff, 0x00000}
11336 }, mem_tbl_57765[] = {
11337 { 0x00000200, 0x00008},
11338 { 0x00004000, 0x00800},
11339 { 0x00006000, 0x09800},
11340 { 0x00010000, 0x0a000},
11341 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070011342 };
11343 struct mem_entry *mem_tbl;
11344 int err = 0;
11345 int i;
11346
Joe Perches63c3a662011-04-26 08:12:10 +000011347 if (tg3_flag(tp, 5717_PLUS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011348 mem_tbl = mem_tbl_5717;
Matt Carlson55086ad2011-12-14 11:09:59 +000011349 else if (tg3_flag(tp, 57765_CLASS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011350 mem_tbl = mem_tbl_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000011351 else if (tg3_flag(tp, 5755_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080011352 mem_tbl = mem_tbl_5755;
11353 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
11354 mem_tbl = mem_tbl_5906;
Joe Perches63c3a662011-04-26 08:12:10 +000011355 else if (tg3_flag(tp, 5705_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080011356 mem_tbl = mem_tbl_5705;
11357 else
Michael Chan7942e1d2005-05-29 14:58:36 -070011358 mem_tbl = mem_tbl_570x;
11359
11360 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000011361 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
11362 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070011363 break;
11364 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011365
Michael Chan7942e1d2005-05-29 14:58:36 -070011366 return err;
11367}
11368
Matt Carlsonbb158d62011-04-25 12:42:47 +000011369#define TG3_TSO_MSS 500
11370
11371#define TG3_TSO_IP_HDR_LEN 20
11372#define TG3_TSO_TCP_HDR_LEN 20
11373#define TG3_TSO_TCP_OPT_LEN 12
11374
11375static const u8 tg3_tso_header[] = {
113760x08, 0x00,
113770x45, 0x00, 0x00, 0x00,
113780x00, 0x00, 0x40, 0x00,
113790x40, 0x06, 0x00, 0x00,
113800x0a, 0x00, 0x00, 0x01,
113810x0a, 0x00, 0x00, 0x02,
113820x0d, 0x00, 0xe0, 0x00,
113830x00, 0x00, 0x01, 0x00,
113840x00, 0x00, 0x02, 0x00,
113850x80, 0x10, 0x10, 0x00,
113860x14, 0x09, 0x00, 0x00,
113870x01, 0x01, 0x08, 0x0a,
113880x11, 0x11, 0x11, 0x11,
113890x11, 0x11, 0x11, 0x11,
11390};
Michael Chan9f40dea2005-09-05 17:53:06 -070011391
Matt Carlson28a45952011-08-19 13:58:22 +000011392static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, bool tso_loopback)
Michael Chanc76949a2005-05-29 14:58:59 -070011393{
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011394 u32 rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011395 u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
Matt Carlson84b67b22011-07-27 14:20:52 +000011396 u32 budget;
Eric Dumazet9205fd92011-11-18 06:47:01 +000011397 struct sk_buff *skb;
11398 u8 *tx_data, *rx_data;
Michael Chanc76949a2005-05-29 14:58:59 -070011399 dma_addr_t map;
11400 int num_pkts, tx_len, rx_len, i, err;
11401 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000011402 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000011403 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070011404
Matt Carlsonc8873402010-02-12 14:47:11 +000011405 tnapi = &tp->napi[0];
11406 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011407 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +000011408 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlson1da85aa2010-09-30 10:34:34 +000011409 rnapi = &tp->napi[1];
Joe Perches63c3a662011-04-26 08:12:10 +000011410 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc8873402010-02-12 14:47:11 +000011411 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011412 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011413 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000011414
Michael Chanc76949a2005-05-29 14:58:59 -070011415 err = -EIO;
11416
Matt Carlson4852a862011-04-13 11:05:07 +000011417 tx_len = pktsz;
David S. Millera20e9c62006-07-31 22:38:16 -070011418 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070011419 if (!skb)
11420 return -ENOMEM;
11421
Michael Chanc76949a2005-05-29 14:58:59 -070011422 tx_data = skb_put(skb, tx_len);
11423 memcpy(tx_data, tp->dev->dev_addr, 6);
11424 memset(tx_data + 6, 0x0, 8);
11425
Matt Carlson4852a862011-04-13 11:05:07 +000011426 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
Michael Chanc76949a2005-05-29 14:58:59 -070011427
Matt Carlson28a45952011-08-19 13:58:22 +000011428 if (tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011429 struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
11430
11431 u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
11432 TG3_TSO_TCP_OPT_LEN;
11433
11434 memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
11435 sizeof(tg3_tso_header));
11436 mss = TG3_TSO_MSS;
11437
11438 val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
11439 num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
11440
11441 /* Set the total length field in the IP header */
11442 iph->tot_len = htons((u16)(mss + hdr_len));
11443
11444 base_flags = (TXD_FLAG_CPU_PRE_DMA |
11445 TXD_FLAG_CPU_POST_DMA);
11446
Joe Perches63c3a662011-04-26 08:12:10 +000011447 if (tg3_flag(tp, HW_TSO_1) ||
11448 tg3_flag(tp, HW_TSO_2) ||
11449 tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011450 struct tcphdr *th;
11451 val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
11452 th = (struct tcphdr *)&tx_data[val];
11453 th->check = 0;
11454 } else
11455 base_flags |= TXD_FLAG_TCPUDP_CSUM;
11456
Joe Perches63c3a662011-04-26 08:12:10 +000011457 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011458 mss |= (hdr_len & 0xc) << 12;
11459 if (hdr_len & 0x10)
11460 base_flags |= 0x00000010;
11461 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +000011462 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlsonbb158d62011-04-25 12:42:47 +000011463 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +000011464 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlsonbb158d62011-04-25 12:42:47 +000011465 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
11466 mss |= (TG3_TSO_TCP_OPT_LEN << 9);
11467 } else {
11468 base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
11469 }
11470
11471 data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
11472 } else {
11473 num_pkts = 1;
11474 data_off = ETH_HLEN;
11475 }
11476
11477 for (i = data_off; i < tx_len; i++)
Michael Chanc76949a2005-05-29 14:58:59 -070011478 tx_data[i] = (u8) (i & 0xff);
11479
Alexander Duyckf4188d82009-12-02 16:48:38 +000011480 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
11481 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000011482 dev_kfree_skb(skb);
11483 return -EIO;
11484 }
Michael Chanc76949a2005-05-29 14:58:59 -070011485
Matt Carlson0d681b22011-07-27 14:20:49 +000011486 val = tnapi->tx_prod;
11487 tnapi->tx_buffers[val].skb = skb;
11488 dma_unmap_addr_set(&tnapi->tx_buffers[val], mapping, map);
11489
Michael Chanc76949a2005-05-29 14:58:59 -070011490 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011491 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011492
11493 udelay(10);
11494
Matt Carlson898a56f2009-08-28 14:02:40 +000011495 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070011496
Matt Carlson84b67b22011-07-27 14:20:52 +000011497 budget = tg3_tx_avail(tnapi);
11498 if (tg3_tx_frag_set(tnapi, &val, &budget, map, tx_len,
Matt Carlsond1a3b732011-07-27 14:20:51 +000011499 base_flags | TXD_FLAG_END, mss, 0)) {
11500 tnapi->tx_buffers[val].skb = NULL;
11501 dev_kfree_skb(skb);
11502 return -EIO;
11503 }
Michael Chanc76949a2005-05-29 14:58:59 -070011504
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011505 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070011506
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011507 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
11508 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070011509
11510 udelay(10);
11511
Matt Carlson303fc922009-11-02 14:27:34 +000011512 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
11513 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070011514 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011515 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011516
11517 udelay(10);
11518
Matt Carlson898a56f2009-08-28 14:02:40 +000011519 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
11520 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011521 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070011522 (rx_idx == (rx_start_idx + num_pkts)))
11523 break;
11524 }
11525
Matt Carlsonba1142e2011-11-04 09:15:00 +000011526 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod - 1, -1);
Michael Chanc76949a2005-05-29 14:58:59 -070011527 dev_kfree_skb(skb);
11528
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011529 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070011530 goto out;
11531
11532 if (rx_idx != rx_start_idx + num_pkts)
11533 goto out;
11534
Matt Carlsonbb158d62011-04-25 12:42:47 +000011535 val = data_off;
11536 while (rx_idx != rx_start_idx) {
11537 desc = &rnapi->rx_rcb[rx_start_idx++];
11538 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
11539 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
Michael Chanc76949a2005-05-29 14:58:59 -070011540
Matt Carlsonbb158d62011-04-25 12:42:47 +000011541 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
11542 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
Matt Carlson4852a862011-04-13 11:05:07 +000011543 goto out;
Michael Chanc76949a2005-05-29 14:58:59 -070011544
Matt Carlsonbb158d62011-04-25 12:42:47 +000011545 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
11546 - ETH_FCS_LEN;
11547
Matt Carlson28a45952011-08-19 13:58:22 +000011548 if (!tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011549 if (rx_len != tx_len)
11550 goto out;
11551
11552 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
11553 if (opaque_key != RXD_OPAQUE_RING_STD)
11554 goto out;
11555 } else {
11556 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
11557 goto out;
11558 }
11559 } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
11560 (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
Matt Carlson54e0a672011-05-19 12:12:50 +000011561 >> RXD_TCPCSUM_SHIFT != 0xffff) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011562 goto out;
11563 }
11564
11565 if (opaque_key == RXD_OPAQUE_RING_STD) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000011566 rx_data = tpr->rx_std_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011567 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
11568 mapping);
11569 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000011570 rx_data = tpr->rx_jmb_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011571 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
11572 mapping);
11573 } else
Matt Carlson4852a862011-04-13 11:05:07 +000011574 goto out;
11575
Matt Carlsonbb158d62011-04-25 12:42:47 +000011576 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
11577 PCI_DMA_FROMDEVICE);
11578
Eric Dumazet9205fd92011-11-18 06:47:01 +000011579 rx_data += TG3_RX_OFFSET(tp);
Matt Carlsonbb158d62011-04-25 12:42:47 +000011580 for (i = data_off; i < rx_len; i++, val++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000011581 if (*(rx_data + i) != (u8) (val & 0xff))
Matt Carlsonbb158d62011-04-25 12:42:47 +000011582 goto out;
11583 }
Matt Carlson4852a862011-04-13 11:05:07 +000011584 }
11585
Michael Chanc76949a2005-05-29 14:58:59 -070011586 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011587
Eric Dumazet9205fd92011-11-18 06:47:01 +000011588 /* tg3_free_rings will unmap and free the rx_data */
Michael Chanc76949a2005-05-29 14:58:59 -070011589out:
11590 return err;
11591}
11592
Matt Carlson00c266b2011-04-25 12:42:46 +000011593#define TG3_STD_LOOPBACK_FAILED 1
11594#define TG3_JMB_LOOPBACK_FAILED 2
Matt Carlsonbb158d62011-04-25 12:42:47 +000011595#define TG3_TSO_LOOPBACK_FAILED 4
Matt Carlson28a45952011-08-19 13:58:22 +000011596#define TG3_LOOPBACK_FAILED \
11597 (TG3_STD_LOOPBACK_FAILED | \
11598 TG3_JMB_LOOPBACK_FAILED | \
11599 TG3_TSO_LOOPBACK_FAILED)
Matt Carlson00c266b2011-04-25 12:42:46 +000011600
Matt Carlson941ec902011-08-19 13:58:23 +000011601static int tg3_test_loopback(struct tg3 *tp, u64 *data, bool do_extlpbk)
Michael Chan9f40dea2005-09-05 17:53:06 -070011602{
Matt Carlson28a45952011-08-19 13:58:22 +000011603 int err = -EIO;
Matt Carlson2215e242011-08-19 13:58:19 +000011604 u32 eee_cap;
Michael Chan9f40dea2005-09-05 17:53:06 -070011605
Matt Carlsonab789042011-01-25 15:58:54 +000011606 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
11607 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
11608
Matt Carlson28a45952011-08-19 13:58:22 +000011609 if (!netif_running(tp->dev)) {
11610 data[0] = TG3_LOOPBACK_FAILED;
11611 data[1] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000011612 if (do_extlpbk)
11613 data[2] = TG3_LOOPBACK_FAILED;
Matt Carlson28a45952011-08-19 13:58:22 +000011614 goto done;
11615 }
11616
Michael Chanb9ec6c12006-07-25 16:37:27 -070011617 err = tg3_reset_hw(tp, 1);
Matt Carlsonab789042011-01-25 15:58:54 +000011618 if (err) {
Matt Carlson28a45952011-08-19 13:58:22 +000011619 data[0] = TG3_LOOPBACK_FAILED;
11620 data[1] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000011621 if (do_extlpbk)
11622 data[2] = TG3_LOOPBACK_FAILED;
Matt Carlsonab789042011-01-25 15:58:54 +000011623 goto done;
11624 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011625
Joe Perches63c3a662011-04-26 08:12:10 +000011626 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson4a85f092011-04-20 07:57:37 +000011627 int i;
11628
11629 /* Reroute all rx packets to the 1st queue */
11630 for (i = MAC_RSS_INDIR_TBL_0;
11631 i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
11632 tw32(i, 0x0);
11633 }
11634
Matt Carlson6e01b202011-08-19 13:58:20 +000011635 /* HW errata - mac loopback fails in some cases on 5780.
11636 * Normal traffic and PHY loopback are not affected by
11637 * errata. Also, the MAC loopback test is deprecated for
11638 * all newer ASIC revisions.
11639 */
11640 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
11641 !tg3_flag(tp, CPMU_PRESENT)) {
11642 tg3_mac_loopback(tp, true);
Matt Carlson9936bcf2007-10-10 18:03:07 -070011643
Matt Carlson28a45952011-08-19 13:58:22 +000011644 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
11645 data[0] |= TG3_STD_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000011646
11647 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000011648 tg3_run_loopback(tp, 9000 + ETH_HLEN, false))
11649 data[0] |= TG3_JMB_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000011650
11651 tg3_mac_loopback(tp, false);
11652 }
Matt Carlson4852a862011-04-13 11:05:07 +000011653
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011654 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000011655 !tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011656 int i;
11657
Matt Carlson941ec902011-08-19 13:58:23 +000011658 tg3_phy_lpbk_set(tp, 0, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011659
11660 /* Wait for link */
11661 for (i = 0; i < 100; i++) {
11662 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
11663 break;
11664 mdelay(1);
11665 }
11666
Matt Carlson28a45952011-08-19 13:58:22 +000011667 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
11668 data[1] |= TG3_STD_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000011669 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000011670 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
11671 data[1] |= TG3_TSO_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000011672 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000011673 tg3_run_loopback(tp, 9000 + ETH_HLEN, false))
11674 data[1] |= TG3_JMB_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070011675
Matt Carlson941ec902011-08-19 13:58:23 +000011676 if (do_extlpbk) {
11677 tg3_phy_lpbk_set(tp, 0, true);
11678
11679 /* All link indications report up, but the hardware
11680 * isn't really ready for about 20 msec. Double it
11681 * to be sure.
11682 */
11683 mdelay(40);
11684
11685 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
11686 data[2] |= TG3_STD_LOOPBACK_FAILED;
11687 if (tg3_flag(tp, TSO_CAPABLE) &&
11688 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
11689 data[2] |= TG3_TSO_LOOPBACK_FAILED;
11690 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
11691 tg3_run_loopback(tp, 9000 + ETH_HLEN, false))
11692 data[2] |= TG3_JMB_LOOPBACK_FAILED;
11693 }
11694
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011695 /* Re-enable gphy autopowerdown. */
11696 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
11697 tg3_phy_toggle_apd(tp, true);
11698 }
Matt Carlson6833c042008-11-21 17:18:59 -080011699
Matt Carlson941ec902011-08-19 13:58:23 +000011700 err = (data[0] | data[1] | data[2]) ? -EIO : 0;
Matt Carlson28a45952011-08-19 13:58:22 +000011701
Matt Carlsonab789042011-01-25 15:58:54 +000011702done:
11703 tp->phy_flags |= eee_cap;
11704
Michael Chan9f40dea2005-09-05 17:53:06 -070011705 return err;
11706}
11707
Michael Chan4cafd3f2005-05-29 14:56:34 -070011708static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
11709 u64 *data)
11710{
Michael Chan566f86a2005-05-29 14:56:58 -070011711 struct tg3 *tp = netdev_priv(dev);
Matt Carlson941ec902011-08-19 13:58:23 +000011712 bool doextlpbk = etest->flags & ETH_TEST_FL_EXTERNAL_LB;
Michael Chan566f86a2005-05-29 14:56:58 -070011713
Matt Carlsonbed98292011-07-13 09:27:29 +000011714 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
11715 tg3_power_up(tp)) {
11716 etest->flags |= ETH_TEST_FL_FAILED;
11717 memset(data, 1, sizeof(u64) * TG3_NUM_TEST);
11718 return;
11719 }
Michael Chanbc1c7562006-03-20 17:48:03 -080011720
Michael Chan566f86a2005-05-29 14:56:58 -070011721 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
11722
11723 if (tg3_test_nvram(tp) != 0) {
11724 etest->flags |= ETH_TEST_FL_FAILED;
11725 data[0] = 1;
11726 }
Matt Carlson941ec902011-08-19 13:58:23 +000011727 if (!doextlpbk && tg3_test_link(tp)) {
Michael Chanca430072005-05-29 14:57:23 -070011728 etest->flags |= ETH_TEST_FL_FAILED;
11729 data[1] = 1;
11730 }
Michael Chana71116d2005-05-29 14:58:11 -070011731 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011732 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070011733
Michael Chanbbe832c2005-06-24 20:20:04 -070011734 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011735 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070011736 tg3_netif_stop(tp);
11737 irq_sync = 1;
11738 }
11739
11740 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070011741
11742 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080011743 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011744 tg3_halt_cpu(tp, RX_CPU_BASE);
Joe Perches63c3a662011-04-26 08:12:10 +000011745 if (!tg3_flag(tp, 5705_PLUS))
Michael Chana71116d2005-05-29 14:58:11 -070011746 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080011747 if (!err)
11748 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011749
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011750 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad12006-03-20 22:27:35 -080011751 tg3_phy_reset(tp);
11752
Michael Chana71116d2005-05-29 14:58:11 -070011753 if (tg3_test_registers(tp) != 0) {
11754 etest->flags |= ETH_TEST_FL_FAILED;
11755 data[2] = 1;
11756 }
Matt Carlson28a45952011-08-19 13:58:22 +000011757
Michael Chan7942e1d2005-05-29 14:58:36 -070011758 if (tg3_test_memory(tp) != 0) {
11759 etest->flags |= ETH_TEST_FL_FAILED;
11760 data[3] = 1;
11761 }
Matt Carlson28a45952011-08-19 13:58:22 +000011762
Matt Carlson941ec902011-08-19 13:58:23 +000011763 if (doextlpbk)
11764 etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
11765
11766 if (tg3_test_loopback(tp, &data[4], doextlpbk))
Michael Chanc76949a2005-05-29 14:58:59 -070011767 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070011768
David S. Millerf47c11e2005-06-24 20:18:35 -070011769 tg3_full_unlock(tp);
11770
Michael Chand4bc3922005-05-29 14:59:20 -070011771 if (tg3_test_interrupt(tp) != 0) {
11772 etest->flags |= ETH_TEST_FL_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000011773 data[7] = 1;
Michael Chand4bc3922005-05-29 14:59:20 -070011774 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011775
11776 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070011777
Michael Chana71116d2005-05-29 14:58:11 -070011778 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
11779 if (netif_running(dev)) {
Joe Perches63c3a662011-04-26 08:12:10 +000011780 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011781 err2 = tg3_restart_hw(tp, 1);
11782 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070011783 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011784 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011785
11786 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011787
11788 if (irq_sync && !err2)
11789 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011790 }
Matt Carlson80096062010-08-02 11:26:06 +000011791 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000011792 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080011793
Michael Chan4cafd3f2005-05-29 14:56:34 -070011794}
11795
Linus Torvalds1da177e2005-04-16 15:20:36 -070011796static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
11797{
11798 struct mii_ioctl_data *data = if_mii(ifr);
11799 struct tg3 *tp = netdev_priv(dev);
11800 int err;
11801
Joe Perches63c3a662011-04-26 08:12:10 +000011802 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011803 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011804 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011805 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011806 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000011807 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011808 }
11809
Matt Carlson33f401a2010-04-05 10:19:27 +000011810 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011811 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000011812 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011813
11814 /* fallthru */
11815 case SIOCGMIIREG: {
11816 u32 mii_regval;
11817
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011818 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011819 break; /* We have no PHY */
11820
Matt Carlson34eea5a2011-04-20 07:57:38 +000011821 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080011822 return -EAGAIN;
11823
David S. Millerf47c11e2005-06-24 20:18:35 -070011824 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011825 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070011826 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011827
11828 data->val_out = mii_regval;
11829
11830 return err;
11831 }
11832
11833 case SIOCSMIIREG:
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_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070011842 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011843
11844 return err;
11845
11846 default:
11847 /* do nothing */
11848 break;
11849 }
11850 return -EOPNOTSUPP;
11851}
11852
David S. Miller15f98502005-05-18 22:49:26 -070011853static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11854{
11855 struct tg3 *tp = netdev_priv(dev);
11856
11857 memcpy(ec, &tp->coal, sizeof(*ec));
11858 return 0;
11859}
11860
Michael Chand244c892005-07-05 14:42:33 -070011861static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11862{
11863 struct tg3 *tp = netdev_priv(dev);
11864 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
11865 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
11866
Joe Perches63c3a662011-04-26 08:12:10 +000011867 if (!tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070011868 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
11869 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
11870 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
11871 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
11872 }
11873
11874 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
11875 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
11876 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
11877 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
11878 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
11879 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
11880 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
11881 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
11882 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
11883 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
11884 return -EINVAL;
11885
11886 /* No rx interrupts will be generated if both are zero */
11887 if ((ec->rx_coalesce_usecs == 0) &&
11888 (ec->rx_max_coalesced_frames == 0))
11889 return -EINVAL;
11890
11891 /* No tx interrupts will be generated if both are zero */
11892 if ((ec->tx_coalesce_usecs == 0) &&
11893 (ec->tx_max_coalesced_frames == 0))
11894 return -EINVAL;
11895
11896 /* Only copy relevant parameters, ignore all others. */
11897 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
11898 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
11899 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
11900 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
11901 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
11902 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
11903 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
11904 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
11905 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
11906
11907 if (netif_running(dev)) {
11908 tg3_full_lock(tp, 0);
11909 __tg3_set_coalesce(tp, &tp->coal);
11910 tg3_full_unlock(tp);
11911 }
11912 return 0;
11913}
11914
Jeff Garzik7282d492006-09-13 14:30:00 -040011915static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011916 .get_settings = tg3_get_settings,
11917 .set_settings = tg3_set_settings,
11918 .get_drvinfo = tg3_get_drvinfo,
11919 .get_regs_len = tg3_get_regs_len,
11920 .get_regs = tg3_get_regs,
11921 .get_wol = tg3_get_wol,
11922 .set_wol = tg3_set_wol,
11923 .get_msglevel = tg3_get_msglevel,
11924 .set_msglevel = tg3_set_msglevel,
11925 .nway_reset = tg3_nway_reset,
11926 .get_link = ethtool_op_get_link,
11927 .get_eeprom_len = tg3_get_eeprom_len,
11928 .get_eeprom = tg3_get_eeprom,
11929 .set_eeprom = tg3_set_eeprom,
11930 .get_ringparam = tg3_get_ringparam,
11931 .set_ringparam = tg3_set_ringparam,
11932 .get_pauseparam = tg3_get_pauseparam,
11933 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070011934 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011935 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000011936 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011937 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070011938 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070011939 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070011940 .get_sset_count = tg3_get_sset_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011941};
11942
11943static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
11944{
Michael Chan1b277772006-03-20 22:27:48 -080011945 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011946
11947 tp->nvram_size = EEPROM_CHIP_SIZE;
11948
Matt Carlsone4f34112009-02-25 14:25:00 +000011949 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011950 return;
11951
Michael Chanb16250e2006-09-27 16:10:14 -070011952 if ((magic != TG3_EEPROM_MAGIC) &&
11953 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
11954 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011955 return;
11956
11957 /*
11958 * Size the chip by reading offsets at increasing powers of two.
11959 * When we encounter our validation signature, we know the addressing
11960 * has wrapped around, and thus have our chip size.
11961 */
Michael Chan1b277772006-03-20 22:27:48 -080011962 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011963
11964 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000011965 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011966 return;
11967
Michael Chan18201802006-03-20 22:29:15 -080011968 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011969 break;
11970
11971 cursize <<= 1;
11972 }
11973
11974 tp->nvram_size = cursize;
11975}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011976
Linus Torvalds1da177e2005-04-16 15:20:36 -070011977static void __devinit tg3_get_nvram_size(struct tg3 *tp)
11978{
11979 u32 val;
11980
Joe Perches63c3a662011-04-26 08:12:10 +000011981 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080011982 return;
11983
11984 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080011985 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080011986 tg3_get_eeprom_size(tp);
11987 return;
11988 }
11989
Matt Carlson6d348f22009-02-25 14:25:52 +000011990 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011991 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000011992 /* This is confusing. We want to operate on the
11993 * 16-bit value at offset 0xf2. The tg3_nvram_read()
11994 * call will read from NVRAM and byteswap the data
11995 * according to the byteswapping settings for all
11996 * other register accesses. This ensures the data we
11997 * want will always reside in the lower 16-bits.
11998 * However, the data in NVRAM is in LE format, which
11999 * means the data from the NVRAM read will always be
12000 * opposite the endianness of the CPU. The 16-bit
12001 * byteswap then brings the data to CPU endianness.
12002 */
12003 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012004 return;
12005 }
12006 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070012007 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012008}
12009
12010static void __devinit tg3_get_nvram_info(struct tg3 *tp)
12011{
12012 u32 nvcfg1;
12013
12014 nvcfg1 = tr32(NVRAM_CFG1);
12015 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
Joe Perches63c3a662011-04-26 08:12:10 +000012016 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012017 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012018 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12019 tw32(NVRAM_CFG1, nvcfg1);
12020 }
12021
Matt Carlson6ff6f812011-05-19 12:12:54 +000012022 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
Joe Perches63c3a662011-04-26 08:12:10 +000012023 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012024 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012025 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
12026 tp->nvram_jedecnum = JEDEC_ATMEL;
12027 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012028 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012029 break;
12030 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
12031 tp->nvram_jedecnum = JEDEC_ATMEL;
12032 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
12033 break;
12034 case FLASH_VENDOR_ATMEL_EEPROM:
12035 tp->nvram_jedecnum = JEDEC_ATMEL;
12036 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012037 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012038 break;
12039 case FLASH_VENDOR_ST:
12040 tp->nvram_jedecnum = JEDEC_ST;
12041 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012042 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012043 break;
12044 case FLASH_VENDOR_SAIFUN:
12045 tp->nvram_jedecnum = JEDEC_SAIFUN;
12046 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
12047 break;
12048 case FLASH_VENDOR_SST_SMALL:
12049 case FLASH_VENDOR_SST_LARGE:
12050 tp->nvram_jedecnum = JEDEC_SST;
12051 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
12052 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012053 }
Matt Carlson8590a602009-08-28 12:29:16 +000012054 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012055 tp->nvram_jedecnum = JEDEC_ATMEL;
12056 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012057 tg3_flag_set(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012058 }
12059}
12060
Matt Carlsona1b950d2009-09-01 13:20:17 +000012061static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
12062{
12063 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
12064 case FLASH_5752PAGE_SIZE_256:
12065 tp->nvram_pagesize = 256;
12066 break;
12067 case FLASH_5752PAGE_SIZE_512:
12068 tp->nvram_pagesize = 512;
12069 break;
12070 case FLASH_5752PAGE_SIZE_1K:
12071 tp->nvram_pagesize = 1024;
12072 break;
12073 case FLASH_5752PAGE_SIZE_2K:
12074 tp->nvram_pagesize = 2048;
12075 break;
12076 case FLASH_5752PAGE_SIZE_4K:
12077 tp->nvram_pagesize = 4096;
12078 break;
12079 case FLASH_5752PAGE_SIZE_264:
12080 tp->nvram_pagesize = 264;
12081 break;
12082 case FLASH_5752PAGE_SIZE_528:
12083 tp->nvram_pagesize = 528;
12084 break;
12085 }
12086}
12087
Michael Chan361b4ac2005-04-21 17:11:21 -070012088static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
12089{
12090 u32 nvcfg1;
12091
12092 nvcfg1 = tr32(NVRAM_CFG1);
12093
Michael Chane6af3012005-04-21 17:12:05 -070012094 /* NVRAM protection for TPM */
12095 if (nvcfg1 & (1 << 27))
Joe Perches63c3a662011-04-26 08:12:10 +000012096 tg3_flag_set(tp, PROTECTED_NVRAM);
Michael Chane6af3012005-04-21 17:12:05 -070012097
Michael Chan361b4ac2005-04-21 17:11:21 -070012098 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012099 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
12100 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
12101 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012102 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012103 break;
12104 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12105 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012106 tg3_flag_set(tp, NVRAM_BUFFERED);
12107 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012108 break;
12109 case FLASH_5752VENDOR_ST_M45PE10:
12110 case FLASH_5752VENDOR_ST_M45PE20:
12111 case FLASH_5752VENDOR_ST_M45PE40:
12112 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012113 tg3_flag_set(tp, NVRAM_BUFFERED);
12114 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012115 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070012116 }
12117
Joe Perches63c3a662011-04-26 08:12:10 +000012118 if (tg3_flag(tp, FLASH)) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000012119 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000012120 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070012121 /* For eeprom, set pagesize to maximum eeprom size */
12122 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12123
12124 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12125 tw32(NVRAM_CFG1, nvcfg1);
12126 }
12127}
12128
Michael Chand3c7b882006-03-23 01:28:25 -080012129static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
12130{
Matt Carlson989a9d22007-05-05 11:51:05 -070012131 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080012132
12133 nvcfg1 = tr32(NVRAM_CFG1);
12134
12135 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070012136 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012137 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson989a9d22007-05-05 11:51:05 -070012138 protect = 1;
12139 }
Michael Chand3c7b882006-03-23 01:28:25 -080012140
Matt Carlson989a9d22007-05-05 11:51:05 -070012141 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
12142 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012143 case FLASH_5755VENDOR_ATMEL_FLASH_1:
12144 case FLASH_5755VENDOR_ATMEL_FLASH_2:
12145 case FLASH_5755VENDOR_ATMEL_FLASH_3:
12146 case FLASH_5755VENDOR_ATMEL_FLASH_5:
12147 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012148 tg3_flag_set(tp, NVRAM_BUFFERED);
12149 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012150 tp->nvram_pagesize = 264;
12151 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
12152 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
12153 tp->nvram_size = (protect ? 0x3e200 :
12154 TG3_NVRAM_SIZE_512KB);
12155 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
12156 tp->nvram_size = (protect ? 0x1f200 :
12157 TG3_NVRAM_SIZE_256KB);
12158 else
12159 tp->nvram_size = (protect ? 0x1f200 :
12160 TG3_NVRAM_SIZE_128KB);
12161 break;
12162 case FLASH_5752VENDOR_ST_M45PE10:
12163 case FLASH_5752VENDOR_ST_M45PE20:
12164 case FLASH_5752VENDOR_ST_M45PE40:
12165 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012166 tg3_flag_set(tp, NVRAM_BUFFERED);
12167 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012168 tp->nvram_pagesize = 256;
12169 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
12170 tp->nvram_size = (protect ?
12171 TG3_NVRAM_SIZE_64KB :
12172 TG3_NVRAM_SIZE_128KB);
12173 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
12174 tp->nvram_size = (protect ?
12175 TG3_NVRAM_SIZE_64KB :
12176 TG3_NVRAM_SIZE_256KB);
12177 else
12178 tp->nvram_size = (protect ?
12179 TG3_NVRAM_SIZE_128KB :
12180 TG3_NVRAM_SIZE_512KB);
12181 break;
Michael Chand3c7b882006-03-23 01:28:25 -080012182 }
12183}
12184
Michael Chan1b277772006-03-20 22:27:48 -080012185static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
12186{
12187 u32 nvcfg1;
12188
12189 nvcfg1 = tr32(NVRAM_CFG1);
12190
12191 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012192 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
12193 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
12194 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
12195 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
12196 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012197 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012198 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080012199
Matt Carlson8590a602009-08-28 12:29:16 +000012200 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12201 tw32(NVRAM_CFG1, nvcfg1);
12202 break;
12203 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12204 case FLASH_5755VENDOR_ATMEL_FLASH_1:
12205 case FLASH_5755VENDOR_ATMEL_FLASH_2:
12206 case FLASH_5755VENDOR_ATMEL_FLASH_3:
12207 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012208 tg3_flag_set(tp, NVRAM_BUFFERED);
12209 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012210 tp->nvram_pagesize = 264;
12211 break;
12212 case FLASH_5752VENDOR_ST_M45PE10:
12213 case FLASH_5752VENDOR_ST_M45PE20:
12214 case FLASH_5752VENDOR_ST_M45PE40:
12215 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012216 tg3_flag_set(tp, NVRAM_BUFFERED);
12217 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012218 tp->nvram_pagesize = 256;
12219 break;
Michael Chan1b277772006-03-20 22:27:48 -080012220 }
12221}
12222
Matt Carlson6b91fa02007-10-10 18:01:09 -070012223static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
12224{
12225 u32 nvcfg1, protect = 0;
12226
12227 nvcfg1 = tr32(NVRAM_CFG1);
12228
12229 /* NVRAM protection for TPM */
12230 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012231 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson6b91fa02007-10-10 18:01:09 -070012232 protect = 1;
12233 }
12234
12235 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
12236 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012237 case FLASH_5761VENDOR_ATMEL_ADB021D:
12238 case FLASH_5761VENDOR_ATMEL_ADB041D:
12239 case FLASH_5761VENDOR_ATMEL_ADB081D:
12240 case FLASH_5761VENDOR_ATMEL_ADB161D:
12241 case FLASH_5761VENDOR_ATMEL_MDB021D:
12242 case FLASH_5761VENDOR_ATMEL_MDB041D:
12243 case FLASH_5761VENDOR_ATMEL_MDB081D:
12244 case FLASH_5761VENDOR_ATMEL_MDB161D:
12245 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012246 tg3_flag_set(tp, NVRAM_BUFFERED);
12247 tg3_flag_set(tp, FLASH);
12248 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson8590a602009-08-28 12:29:16 +000012249 tp->nvram_pagesize = 256;
12250 break;
12251 case FLASH_5761VENDOR_ST_A_M45PE20:
12252 case FLASH_5761VENDOR_ST_A_M45PE40:
12253 case FLASH_5761VENDOR_ST_A_M45PE80:
12254 case FLASH_5761VENDOR_ST_A_M45PE16:
12255 case FLASH_5761VENDOR_ST_M_M45PE20:
12256 case FLASH_5761VENDOR_ST_M_M45PE40:
12257 case FLASH_5761VENDOR_ST_M_M45PE80:
12258 case FLASH_5761VENDOR_ST_M_M45PE16:
12259 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012260 tg3_flag_set(tp, NVRAM_BUFFERED);
12261 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012262 tp->nvram_pagesize = 256;
12263 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070012264 }
12265
12266 if (protect) {
12267 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
12268 } else {
12269 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012270 case FLASH_5761VENDOR_ATMEL_ADB161D:
12271 case FLASH_5761VENDOR_ATMEL_MDB161D:
12272 case FLASH_5761VENDOR_ST_A_M45PE16:
12273 case FLASH_5761VENDOR_ST_M_M45PE16:
12274 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
12275 break;
12276 case FLASH_5761VENDOR_ATMEL_ADB081D:
12277 case FLASH_5761VENDOR_ATMEL_MDB081D:
12278 case FLASH_5761VENDOR_ST_A_M45PE80:
12279 case FLASH_5761VENDOR_ST_M_M45PE80:
12280 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12281 break;
12282 case FLASH_5761VENDOR_ATMEL_ADB041D:
12283 case FLASH_5761VENDOR_ATMEL_MDB041D:
12284 case FLASH_5761VENDOR_ST_A_M45PE40:
12285 case FLASH_5761VENDOR_ST_M_M45PE40:
12286 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12287 break;
12288 case FLASH_5761VENDOR_ATMEL_ADB021D:
12289 case FLASH_5761VENDOR_ATMEL_MDB021D:
12290 case FLASH_5761VENDOR_ST_A_M45PE20:
12291 case FLASH_5761VENDOR_ST_M_M45PE20:
12292 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12293 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070012294 }
12295 }
12296}
12297
Michael Chanb5d37722006-09-27 16:06:21 -070012298static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
12299{
12300 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012301 tg3_flag_set(tp, NVRAM_BUFFERED);
Michael Chanb5d37722006-09-27 16:06:21 -070012302 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12303}
12304
Matt Carlson321d32a2008-11-21 17:22:19 -080012305static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
12306{
12307 u32 nvcfg1;
12308
12309 nvcfg1 = tr32(NVRAM_CFG1);
12310
12311 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12312 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
12313 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
12314 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012315 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson321d32a2008-11-21 17:22:19 -080012316 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12317
12318 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12319 tw32(NVRAM_CFG1, nvcfg1);
12320 return;
12321 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12322 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
12323 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
12324 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
12325 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
12326 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
12327 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
12328 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012329 tg3_flag_set(tp, NVRAM_BUFFERED);
12330 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080012331
12332 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12333 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12334 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
12335 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
12336 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12337 break;
12338 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
12339 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
12340 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12341 break;
12342 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
12343 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
12344 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12345 break;
12346 }
12347 break;
12348 case FLASH_5752VENDOR_ST_M45PE10:
12349 case FLASH_5752VENDOR_ST_M45PE20:
12350 case FLASH_5752VENDOR_ST_M45PE40:
12351 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012352 tg3_flag_set(tp, NVRAM_BUFFERED);
12353 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080012354
12355 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12356 case FLASH_5752VENDOR_ST_M45PE10:
12357 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12358 break;
12359 case FLASH_5752VENDOR_ST_M45PE20:
12360 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12361 break;
12362 case FLASH_5752VENDOR_ST_M45PE40:
12363 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12364 break;
12365 }
12366 break;
12367 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012368 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson321d32a2008-11-21 17:22:19 -080012369 return;
12370 }
12371
Matt Carlsona1b950d2009-09-01 13:20:17 +000012372 tg3_nvram_get_pagesize(tp, nvcfg1);
12373 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012374 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012375}
12376
12377
12378static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
12379{
12380 u32 nvcfg1;
12381
12382 nvcfg1 = tr32(NVRAM_CFG1);
12383
12384 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12385 case FLASH_5717VENDOR_ATMEL_EEPROM:
12386 case FLASH_5717VENDOR_MICRO_EEPROM:
12387 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012388 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012389 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12390
12391 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12392 tw32(NVRAM_CFG1, nvcfg1);
12393 return;
12394 case FLASH_5717VENDOR_ATMEL_MDB011D:
12395 case FLASH_5717VENDOR_ATMEL_ADB011B:
12396 case FLASH_5717VENDOR_ATMEL_ADB011D:
12397 case FLASH_5717VENDOR_ATMEL_MDB021D:
12398 case FLASH_5717VENDOR_ATMEL_ADB021B:
12399 case FLASH_5717VENDOR_ATMEL_ADB021D:
12400 case FLASH_5717VENDOR_ATMEL_45USPT:
12401 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012402 tg3_flag_set(tp, NVRAM_BUFFERED);
12403 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012404
12405 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12406 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012407 /* Detect size with tg3_nvram_get_size() */
12408 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012409 case FLASH_5717VENDOR_ATMEL_ADB021B:
12410 case FLASH_5717VENDOR_ATMEL_ADB021D:
12411 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12412 break;
12413 default:
12414 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12415 break;
12416 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012417 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012418 case FLASH_5717VENDOR_ST_M_M25PE10:
12419 case FLASH_5717VENDOR_ST_A_M25PE10:
12420 case FLASH_5717VENDOR_ST_M_M45PE10:
12421 case FLASH_5717VENDOR_ST_A_M45PE10:
12422 case FLASH_5717VENDOR_ST_M_M25PE20:
12423 case FLASH_5717VENDOR_ST_A_M25PE20:
12424 case FLASH_5717VENDOR_ST_M_M45PE20:
12425 case FLASH_5717VENDOR_ST_A_M45PE20:
12426 case FLASH_5717VENDOR_ST_25USPT:
12427 case FLASH_5717VENDOR_ST_45USPT:
12428 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012429 tg3_flag_set(tp, NVRAM_BUFFERED);
12430 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012431
12432 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12433 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012434 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012435 /* Detect size with tg3_nvram_get_size() */
12436 break;
12437 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012438 case FLASH_5717VENDOR_ST_A_M45PE20:
12439 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12440 break;
12441 default:
12442 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12443 break;
12444 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012445 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012446 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012447 tg3_flag_set(tp, NO_NVRAM);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012448 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080012449 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000012450
12451 tg3_nvram_get_pagesize(tp, nvcfg1);
12452 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012453 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson321d32a2008-11-21 17:22:19 -080012454}
12455
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012456static void __devinit tg3_get_5720_nvram_info(struct tg3 *tp)
12457{
12458 u32 nvcfg1, nvmpinstrp;
12459
12460 nvcfg1 = tr32(NVRAM_CFG1);
12461 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
12462
12463 switch (nvmpinstrp) {
12464 case FLASH_5720_EEPROM_HD:
12465 case FLASH_5720_EEPROM_LD:
12466 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012467 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012468
12469 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12470 tw32(NVRAM_CFG1, nvcfg1);
12471 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
12472 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12473 else
12474 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
12475 return;
12476 case FLASH_5720VENDOR_M_ATMEL_DB011D:
12477 case FLASH_5720VENDOR_A_ATMEL_DB011B:
12478 case FLASH_5720VENDOR_A_ATMEL_DB011D:
12479 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12480 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12481 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12482 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12483 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12484 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12485 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12486 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12487 case FLASH_5720VENDOR_ATMEL_45USPT:
12488 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012489 tg3_flag_set(tp, NVRAM_BUFFERED);
12490 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012491
12492 switch (nvmpinstrp) {
12493 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12494 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12495 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12496 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12497 break;
12498 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12499 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12500 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12501 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12502 break;
12503 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12504 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12505 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12506 break;
12507 default:
12508 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12509 break;
12510 }
12511 break;
12512 case FLASH_5720VENDOR_M_ST_M25PE10:
12513 case FLASH_5720VENDOR_M_ST_M45PE10:
12514 case FLASH_5720VENDOR_A_ST_M25PE10:
12515 case FLASH_5720VENDOR_A_ST_M45PE10:
12516 case FLASH_5720VENDOR_M_ST_M25PE20:
12517 case FLASH_5720VENDOR_M_ST_M45PE20:
12518 case FLASH_5720VENDOR_A_ST_M25PE20:
12519 case FLASH_5720VENDOR_A_ST_M45PE20:
12520 case FLASH_5720VENDOR_M_ST_M25PE40:
12521 case FLASH_5720VENDOR_M_ST_M45PE40:
12522 case FLASH_5720VENDOR_A_ST_M25PE40:
12523 case FLASH_5720VENDOR_A_ST_M45PE40:
12524 case FLASH_5720VENDOR_M_ST_M25PE80:
12525 case FLASH_5720VENDOR_M_ST_M45PE80:
12526 case FLASH_5720VENDOR_A_ST_M25PE80:
12527 case FLASH_5720VENDOR_A_ST_M45PE80:
12528 case FLASH_5720VENDOR_ST_25USPT:
12529 case FLASH_5720VENDOR_ST_45USPT:
12530 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012531 tg3_flag_set(tp, NVRAM_BUFFERED);
12532 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012533
12534 switch (nvmpinstrp) {
12535 case FLASH_5720VENDOR_M_ST_M25PE20:
12536 case FLASH_5720VENDOR_M_ST_M45PE20:
12537 case FLASH_5720VENDOR_A_ST_M25PE20:
12538 case FLASH_5720VENDOR_A_ST_M45PE20:
12539 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12540 break;
12541 case FLASH_5720VENDOR_M_ST_M25PE40:
12542 case FLASH_5720VENDOR_M_ST_M45PE40:
12543 case FLASH_5720VENDOR_A_ST_M25PE40:
12544 case FLASH_5720VENDOR_A_ST_M45PE40:
12545 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12546 break;
12547 case FLASH_5720VENDOR_M_ST_M25PE80:
12548 case FLASH_5720VENDOR_M_ST_M45PE80:
12549 case FLASH_5720VENDOR_A_ST_M25PE80:
12550 case FLASH_5720VENDOR_A_ST_M45PE80:
12551 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12552 break;
12553 default:
12554 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12555 break;
12556 }
12557 break;
12558 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012559 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012560 return;
12561 }
12562
12563 tg3_nvram_get_pagesize(tp, nvcfg1);
12564 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012565 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012566}
12567
Linus Torvalds1da177e2005-04-16 15:20:36 -070012568/* Chips other than 5700/5701 use the NVRAM for fetching info. */
12569static void __devinit tg3_nvram_init(struct tg3 *tp)
12570{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012571 tw32_f(GRC_EEPROM_ADDR,
12572 (EEPROM_ADDR_FSM_RESET |
12573 (EEPROM_DEFAULT_CLOCK_PERIOD <<
12574 EEPROM_ADDR_CLKPERD_SHIFT)));
12575
Michael Chan9d57f012006-12-07 00:23:25 -080012576 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012577
12578 /* Enable seeprom accesses. */
12579 tw32_f(GRC_LOCAL_CTRL,
12580 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
12581 udelay(100);
12582
12583 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
12584 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
Joe Perches63c3a662011-04-26 08:12:10 +000012585 tg3_flag_set(tp, NVRAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012586
Michael Chanec41c7d2006-01-17 02:40:55 -080012587 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000012588 netdev_warn(tp->dev,
12589 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000012590 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080012591 return;
12592 }
Michael Chane6af3012005-04-21 17:12:05 -070012593 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012594
Matt Carlson989a9d22007-05-05 11:51:05 -070012595 tp->nvram_size = 0;
12596
Michael Chan361b4ac2005-04-21 17:11:21 -070012597 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
12598 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080012599 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
12600 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070012601 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070012602 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
12603 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080012604 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070012605 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
12606 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070012607 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12608 tg3_get_5906_nvram_info(tp);
Matt Carlsonb703df62009-12-03 08:36:21 +000012609 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000012610 tg3_flag(tp, 57765_CLASS))
Matt Carlson321d32a2008-11-21 17:22:19 -080012611 tg3_get_57780_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012612 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
12613 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000012614 tg3_get_5717_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012615 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
12616 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070012617 else
12618 tg3_get_nvram_info(tp);
12619
Matt Carlson989a9d22007-05-05 11:51:05 -070012620 if (tp->nvram_size == 0)
12621 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012622
Michael Chane6af3012005-04-21 17:12:05 -070012623 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080012624 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012625
12626 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000012627 tg3_flag_clear(tp, NVRAM);
12628 tg3_flag_clear(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012629
12630 tg3_get_eeprom_size(tp);
12631 }
12632}
12633
Linus Torvalds1da177e2005-04-16 15:20:36 -070012634static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
12635 u32 offset, u32 len, u8 *buf)
12636{
12637 int i, j, rc = 0;
12638 u32 val;
12639
12640 for (i = 0; i < len; i += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012641 u32 addr;
Matt Carlsona9dc5292009-02-25 14:25:30 +000012642 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012643
12644 addr = offset + i;
12645
12646 memcpy(&data, buf + i, 4);
12647
Matt Carlson62cedd12009-04-20 14:52:29 -070012648 /*
12649 * The SEEPROM interface expects the data to always be opposite
12650 * the native endian format. We accomplish this by reversing
12651 * all the operations that would have been performed on the
12652 * data from a call to tg3_nvram_read_be32().
12653 */
12654 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012655
12656 val = tr32(GRC_EEPROM_ADDR);
12657 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
12658
12659 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
12660 EEPROM_ADDR_READ);
12661 tw32(GRC_EEPROM_ADDR, val |
12662 (0 << EEPROM_ADDR_DEVID_SHIFT) |
12663 (addr & EEPROM_ADDR_ADDR_MASK) |
12664 EEPROM_ADDR_START |
12665 EEPROM_ADDR_WRITE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012666
Michael Chan9d57f012006-12-07 00:23:25 -080012667 for (j = 0; j < 1000; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012668 val = tr32(GRC_EEPROM_ADDR);
12669
12670 if (val & EEPROM_ADDR_COMPLETE)
12671 break;
Michael Chan9d57f012006-12-07 00:23:25 -080012672 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012673 }
12674 if (!(val & EEPROM_ADDR_COMPLETE)) {
12675 rc = -EBUSY;
12676 break;
12677 }
12678 }
12679
12680 return rc;
12681}
12682
12683/* offset and length are dword aligned */
12684static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
12685 u8 *buf)
12686{
12687 int ret = 0;
12688 u32 pagesize = tp->nvram_pagesize;
12689 u32 pagemask = pagesize - 1;
12690 u32 nvram_cmd;
12691 u8 *tmp;
12692
12693 tmp = kmalloc(pagesize, GFP_KERNEL);
12694 if (tmp == NULL)
12695 return -ENOMEM;
12696
12697 while (len) {
12698 int j;
Michael Chane6af3012005-04-21 17:12:05 -070012699 u32 phy_addr, page_off, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012700
12701 phy_addr = offset & ~pagemask;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012702
Linus Torvalds1da177e2005-04-16 15:20:36 -070012703 for (j = 0; j < pagesize; j += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012704 ret = tg3_nvram_read_be32(tp, phy_addr + j,
12705 (__be32 *) (tmp + j));
12706 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012707 break;
12708 }
12709 if (ret)
12710 break;
12711
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012712 page_off = offset & pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012713 size = pagesize;
12714 if (len < size)
12715 size = len;
12716
12717 len -= size;
12718
12719 memcpy(tmp + page_off, buf, size);
12720
12721 offset = offset + (pagesize - page_off);
12722
Michael Chane6af3012005-04-21 17:12:05 -070012723 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012724
12725 /*
12726 * Before we can erase the flash page, we need
12727 * to issue a special "write enable" command.
12728 */
12729 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12730
12731 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12732 break;
12733
12734 /* Erase the target page */
12735 tw32(NVRAM_ADDR, phy_addr);
12736
12737 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
12738 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
12739
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012740 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012741 break;
12742
12743 /* Issue another write enable to start the write. */
12744 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12745
12746 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12747 break;
12748
12749 for (j = 0; j < pagesize; j += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012750 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012751
Al Virob9fc7dc2007-12-17 22:59:57 -080012752 data = *((__be32 *) (tmp + j));
Matt Carlsona9dc5292009-02-25 14:25:30 +000012753
Al Virob9fc7dc2007-12-17 22:59:57 -080012754 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012755
12756 tw32(NVRAM_ADDR, phy_addr + j);
12757
12758 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
12759 NVRAM_CMD_WR;
12760
12761 if (j == 0)
12762 nvram_cmd |= NVRAM_CMD_FIRST;
12763 else if (j == (pagesize - 4))
12764 nvram_cmd |= NVRAM_CMD_LAST;
12765
12766 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12767 break;
12768 }
12769 if (ret)
12770 break;
12771 }
12772
12773 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12774 tg3_nvram_exec_cmd(tp, nvram_cmd);
12775
12776 kfree(tmp);
12777
12778 return ret;
12779}
12780
12781/* offset and length are dword aligned */
12782static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
12783 u8 *buf)
12784{
12785 int i, ret = 0;
12786
12787 for (i = 0; i < len; i += 4, offset += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012788 u32 page_off, phy_addr, nvram_cmd;
12789 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012790
12791 memcpy(&data, buf + i, 4);
Al Virob9fc7dc2007-12-17 22:59:57 -080012792 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012793
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012794 page_off = offset % tp->nvram_pagesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012795
Michael Chan18201802006-03-20 22:29:15 -080012796 phy_addr = tg3_nvram_phys_addr(tp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012797
12798 tw32(NVRAM_ADDR, phy_addr);
12799
12800 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
12801
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012802 if (page_off == 0 || i == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012803 nvram_cmd |= NVRAM_CMD_FIRST;
Michael Chanf6d9a252006-04-29 19:00:24 -070012804 if (page_off == (tp->nvram_pagesize - 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012805 nvram_cmd |= NVRAM_CMD_LAST;
12806
12807 if (i == (len - 4))
12808 nvram_cmd |= NVRAM_CMD_LAST;
12809
Matt Carlson321d32a2008-11-21 17:22:19 -080012810 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
Joe Perches63c3a662011-04-26 08:12:10 +000012811 !tg3_flag(tp, 5755_PLUS) &&
Michael Chan4c987482005-09-05 17:52:38 -070012812 (tp->nvram_jedecnum == JEDEC_ST) &&
12813 (nvram_cmd & NVRAM_CMD_FIRST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012814
12815 if ((ret = tg3_nvram_exec_cmd(tp,
12816 NVRAM_CMD_WREN | NVRAM_CMD_GO |
12817 NVRAM_CMD_DONE)))
12818
12819 break;
12820 }
Joe Perches63c3a662011-04-26 08:12:10 +000012821 if (!tg3_flag(tp, FLASH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012822 /* We always do complete word writes to eeprom. */
12823 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
12824 }
12825
12826 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12827 break;
12828 }
12829 return ret;
12830}
12831
12832/* offset and length are dword aligned */
12833static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
12834{
12835 int ret;
12836
Joe Perches63c3a662011-04-26 08:12:10 +000012837 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
Michael Chan314fba32005-04-21 17:07:04 -070012838 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
12839 ~GRC_LCLCTRL_GPIO_OUTPUT1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012840 udelay(40);
12841 }
12842
Joe Perches63c3a662011-04-26 08:12:10 +000012843 if (!tg3_flag(tp, NVRAM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012844 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
Matt Carlson859a588792010-04-05 10:19:28 +000012845 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012846 u32 grc_mode;
12847
Michael Chanec41c7d2006-01-17 02:40:55 -080012848 ret = tg3_nvram_lock(tp);
12849 if (ret)
12850 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012851
Michael Chane6af3012005-04-21 17:12:05 -070012852 tg3_enable_nvram_access(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000012853 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012854 tw32(NVRAM_WRITE1, 0x406);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012855
12856 grc_mode = tr32(GRC_MODE);
12857 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
12858
Joe Perches63c3a662011-04-26 08:12:10 +000012859 if (tg3_flag(tp, NVRAM_BUFFERED) || !tg3_flag(tp, FLASH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012860 ret = tg3_nvram_write_block_buffered(tp, offset, len,
12861 buf);
Matt Carlson859a588792010-04-05 10:19:28 +000012862 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012863 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
12864 buf);
12865 }
12866
12867 grc_mode = tr32(GRC_MODE);
12868 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
12869
Michael Chane6af3012005-04-21 17:12:05 -070012870 tg3_disable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012871 tg3_nvram_unlock(tp);
12872 }
12873
Joe Perches63c3a662011-04-26 08:12:10 +000012874 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
Michael Chan314fba32005-04-21 17:07:04 -070012875 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012876 udelay(40);
12877 }
12878
12879 return ret;
12880}
12881
12882struct subsys_tbl_ent {
12883 u16 subsys_vendor, subsys_devid;
12884 u32 phy_id;
12885};
12886
Matt Carlson24daf2b2010-02-17 15:17:02 +000012887static struct subsys_tbl_ent subsys_id_to_phy_id[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012888 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012889 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012890 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012891 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012892 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012893 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012894 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012895 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12896 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
12897 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012898 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012899 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012900 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012901 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12902 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
12903 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012904 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012905 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012906 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012907 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012908 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012909 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012910 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012911
12912 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012913 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012914 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012915 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012916 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012917 { TG3PCI_SUBVENDOR_ID_3COM,
12918 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
12919 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012920 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012921 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012922 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012923
12924 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012925 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012926 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012927 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012928 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012929 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012930 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012931 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012932 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012933
12934 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012935 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012936 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012937 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012938 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012939 { TG3PCI_SUBVENDOR_ID_COMPAQ,
12940 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
12941 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012942 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012943 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012944 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012945
12946 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012947 { TG3PCI_SUBVENDOR_ID_IBM,
12948 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012949};
12950
Matt Carlson24daf2b2010-02-17 15:17:02 +000012951static struct subsys_tbl_ent * __devinit tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012952{
12953 int i;
12954
12955 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
12956 if ((subsys_id_to_phy_id[i].subsys_vendor ==
12957 tp->pdev->subsystem_vendor) &&
12958 (subsys_id_to_phy_id[i].subsys_devid ==
12959 tp->pdev->subsystem_device))
12960 return &subsys_id_to_phy_id[i];
12961 }
12962 return NULL;
12963}
12964
Michael Chan7d0c41e2005-04-21 17:06:20 -070012965static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012966{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012967 u32 val;
David S. Millerf49639e2006-06-09 11:58:36 -070012968
Matt Carlson79eb6902010-02-17 15:17:03 +000012969 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012970 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12971
Gary Zambranoa85feb82007-05-05 11:52:19 -070012972 /* Assume an onboard device and WOL capable by default. */
Joe Perches63c3a662011-04-26 08:12:10 +000012973 tg3_flag_set(tp, EEPROM_WRITE_PROT);
12974 tg3_flag_set(tp, WOL_CAP);
David S. Miller72b845e2006-03-14 14:11:48 -080012975
Michael Chanb5d37722006-09-27 16:06:21 -070012976 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080012977 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012978 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
12979 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080012980 }
Matt Carlson0527ba32007-10-10 18:03:30 -070012981 val = tr32(VCPU_CFGSHDW);
12982 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Joe Perches63c3a662011-04-26 08:12:10 +000012983 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson0527ba32007-10-10 18:03:30 -070012984 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000012985 (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012986 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000012987 device_set_wakeup_enable(&tp->pdev->dev, true);
12988 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012989 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070012990 }
12991
Linus Torvalds1da177e2005-04-16 15:20:36 -070012992 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
12993 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
12994 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070012995 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012996 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012997
12998 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
12999 tp->nic_sram_data_cfg = nic_cfg;
13000
13001 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
13002 ver >>= NIC_SRAM_DATA_VER_SHIFT;
Matt Carlson6ff6f812011-05-19 12:12:54 +000013003 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13004 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13005 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070013006 (ver > 0) && (ver < 0x100))
13007 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
13008
Matt Carlsona9daf362008-05-25 23:49:44 -070013009 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
13010 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
13011
Linus Torvalds1da177e2005-04-16 15:20:36 -070013012 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
13013 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
13014 eeprom_phy_serdes = 1;
13015
13016 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
13017 if (nic_phy_id != 0) {
13018 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
13019 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
13020
13021 eeprom_phy_id = (id1 >> 16) << 10;
13022 eeprom_phy_id |= (id2 & 0xfc00) << 16;
13023 eeprom_phy_id |= (id2 & 0x03ff) << 0;
13024 } else
13025 eeprom_phy_id = 0;
13026
Michael Chan7d0c41e2005-04-21 17:06:20 -070013027 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070013028 if (eeprom_phy_serdes) {
Joe Perches63c3a662011-04-26 08:12:10 +000013029 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013030 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000013031 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013032 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070013033 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070013034
Joe Perches63c3a662011-04-26 08:12:10 +000013035 if (tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013036 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
13037 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070013038 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070013039 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
13040
13041 switch (led_cfg) {
13042 default:
13043 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
13044 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13045 break;
13046
13047 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
13048 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
13049 break;
13050
13051 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
13052 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070013053
13054 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
13055 * read on some older 5700/5701 bootcode.
13056 */
13057 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
13058 ASIC_REV_5700 ||
13059 GET_ASIC_REV(tp->pci_chip_rev_id) ==
13060 ASIC_REV_5701)
13061 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13062
Linus Torvalds1da177e2005-04-16 15:20:36 -070013063 break;
13064
13065 case SHASTA_EXT_LED_SHARED:
13066 tp->led_ctrl = LED_CTRL_MODE_SHARED;
13067 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
13068 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
13069 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
13070 LED_CTRL_MODE_PHY_2);
13071 break;
13072
13073 case SHASTA_EXT_LED_MAC:
13074 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
13075 break;
13076
13077 case SHASTA_EXT_LED_COMBO:
13078 tp->led_ctrl = LED_CTRL_MODE_COMBO;
13079 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
13080 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
13081 LED_CTRL_MODE_PHY_2);
13082 break;
13083
Stephen Hemminger855e1112008-04-16 16:37:28 -070013084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013085
13086 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13087 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
13088 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
13089 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
13090
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013091 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
13092 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080013093
Michael Chan9d26e212006-12-07 00:21:14 -080013094 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Joe Perches63c3a662011-04-26 08:12:10 +000013095 tg3_flag_set(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080013096 if ((tp->pdev->subsystem_vendor ==
13097 PCI_VENDOR_ID_ARIMA) &&
13098 (tp->pdev->subsystem_device == 0x205a ||
13099 tp->pdev->subsystem_device == 0x2063))
Joe Perches63c3a662011-04-26 08:12:10 +000013100 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080013101 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000013102 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
13103 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080013104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013105
13106 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +000013107 tg3_flag_set(tp, ENABLE_ASF);
13108 if (tg3_flag(tp, 5750_PLUS))
13109 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013110 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080013111
13112 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013113 tg3_flag(tp, 5750_PLUS))
13114 tg3_flag_set(tp, ENABLE_APE);
Matt Carlsonb2b98d42008-11-03 16:52:32 -080013115
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013116 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070013117 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
Joe Perches63c3a662011-04-26 08:12:10 +000013118 tg3_flag_clear(tp, WOL_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013119
Joe Perches63c3a662011-04-26 08:12:10 +000013120 if (tg3_flag(tp, WOL_CAP) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013121 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013122 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013123 device_set_wakeup_enable(&tp->pdev->dev, true);
13124 }
Matt Carlson0527ba32007-10-10 18:03:30 -070013125
Linus Torvalds1da177e2005-04-16 15:20:36 -070013126 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013127 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013128
13129 /* serdes signal pre-emphasis in register 0x590 set by */
13130 /* bootcode if bit 18 is set */
13131 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013132 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070013133
Joe Perches63c3a662011-04-26 08:12:10 +000013134 if ((tg3_flag(tp, 57765_PLUS) ||
13135 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
13136 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080013137 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013138 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080013139
Joe Perches63c3a662011-04-26 08:12:10 +000013140 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson8c69b1e2010-08-02 11:26:00 +000013141 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +000013142 !tg3_flag(tp, 57765_PLUS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070013143 u32 cfg3;
13144
13145 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
13146 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
Joe Perches63c3a662011-04-26 08:12:10 +000013147 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson8ed5d972007-05-07 00:25:49 -070013148 }
Matt Carlsona9daf362008-05-25 23:49:44 -070013149
Matt Carlson14417062010-02-17 15:16:59 +000013150 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
Joe Perches63c3a662011-04-26 08:12:10 +000013151 tg3_flag_set(tp, RGMII_INBAND_DISABLE);
Matt Carlsona9daf362008-05-25 23:49:44 -070013152 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000013153 tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
Matt Carlsona9daf362008-05-25 23:49:44 -070013154 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000013155 tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013156 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080013157done:
Joe Perches63c3a662011-04-26 08:12:10 +000013158 if (tg3_flag(tp, WOL_CAP))
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000013159 device_set_wakeup_enable(&tp->pdev->dev,
Joe Perches63c3a662011-04-26 08:12:10 +000013160 tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000013161 else
13162 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070013163}
13164
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013165static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
13166{
13167 int i;
13168 u32 val;
13169
13170 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
13171 tw32(OTP_CTRL, cmd);
13172
13173 /* Wait for up to 1 ms for command to execute. */
13174 for (i = 0; i < 100; i++) {
13175 val = tr32(OTP_STATUS);
13176 if (val & OTP_STATUS_CMD_DONE)
13177 break;
13178 udelay(10);
13179 }
13180
13181 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
13182}
13183
13184/* Read the gphy configuration from the OTP region of the chip. The gphy
13185 * configuration is a 32-bit value that straddles the alignment boundary.
13186 * We do two 32-bit reads and then shift and merge the results.
13187 */
13188static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
13189{
13190 u32 bhalf_otp, thalf_otp;
13191
13192 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
13193
13194 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
13195 return 0;
13196
13197 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
13198
13199 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
13200 return 0;
13201
13202 thalf_otp = tr32(OTP_READ_DATA);
13203
13204 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
13205
13206 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
13207 return 0;
13208
13209 bhalf_otp = tr32(OTP_READ_DATA);
13210
13211 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
13212}
13213
Matt Carlsone256f8a2011-03-09 16:58:24 +000013214static void __devinit tg3_phy_init_link_config(struct tg3 *tp)
13215{
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +000013216 u32 adv = ADVERTISED_Autoneg;
Matt Carlsone256f8a2011-03-09 16:58:24 +000013217
13218 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
13219 adv |= ADVERTISED_1000baseT_Half |
13220 ADVERTISED_1000baseT_Full;
13221
13222 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
13223 adv |= ADVERTISED_100baseT_Half |
13224 ADVERTISED_100baseT_Full |
13225 ADVERTISED_10baseT_Half |
13226 ADVERTISED_10baseT_Full |
13227 ADVERTISED_TP;
13228 else
13229 adv |= ADVERTISED_FIBRE;
13230
13231 tp->link_config.advertising = adv;
13232 tp->link_config.speed = SPEED_INVALID;
13233 tp->link_config.duplex = DUPLEX_INVALID;
13234 tp->link_config.autoneg = AUTONEG_ENABLE;
13235 tp->link_config.active_speed = SPEED_INVALID;
13236 tp->link_config.active_duplex = DUPLEX_INVALID;
13237 tp->link_config.orig_speed = SPEED_INVALID;
13238 tp->link_config.orig_duplex = DUPLEX_INVALID;
13239 tp->link_config.orig_autoneg = AUTONEG_INVALID;
13240}
13241
Michael Chan7d0c41e2005-04-21 17:06:20 -070013242static int __devinit tg3_phy_probe(struct tg3 *tp)
13243{
13244 u32 hw_phy_id_1, hw_phy_id_2;
13245 u32 hw_phy_id, hw_phy_id_masked;
13246 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013247
Matt Carlsone256f8a2011-03-09 16:58:24 +000013248 /* flow control autonegotiation is default behavior */
Joe Perches63c3a662011-04-26 08:12:10 +000013249 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsone256f8a2011-03-09 16:58:24 +000013250 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
13251
Joe Perches63c3a662011-04-26 08:12:10 +000013252 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013253 return tg3_phy_init(tp);
13254
Linus Torvalds1da177e2005-04-16 15:20:36 -070013255 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010013256 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013257 */
13258 err = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000013259 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000013260 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013261 } else {
13262 /* Now read the physical PHY_ID from the chip and verify
13263 * that it is sane. If it doesn't look good, we fall back
13264 * to either the hard-coded table based PHY_ID and failing
13265 * that the value found in the eeprom area.
13266 */
13267 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
13268 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
13269
13270 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
13271 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
13272 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
13273
Matt Carlson79eb6902010-02-17 15:17:03 +000013274 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013275 }
13276
Matt Carlson79eb6902010-02-17 15:17:03 +000013277 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013278 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000013279 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013280 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070013281 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013282 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013283 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000013284 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070013285 /* Do nothing, phy ID already set up in
13286 * tg3_get_eeprom_hw_cfg().
13287 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070013288 } else {
13289 struct subsys_tbl_ent *p;
13290
13291 /* No eeprom signature? Try the hardcoded
13292 * subsys device table.
13293 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013294 p = tg3_lookup_by_subsys(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013295 if (!p)
13296 return -ENODEV;
13297
13298 tp->phy_id = p->phy_id;
13299 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000013300 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013301 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013302 }
13303 }
13304
Matt Carlsona6b68da2010-12-06 08:28:52 +000013305 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Matt Carlson5baa5e92011-07-20 10:20:53 +000013306 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13307 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720 ||
13308 (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +000013309 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0) ||
13310 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
13311 tp->pci_chip_rev_id != CHIPREV_ID_57765_A0)))
Matt Carlson52b02d02010-10-14 10:37:41 +000013312 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
13313
Matt Carlsone256f8a2011-03-09 16:58:24 +000013314 tg3_phy_init_link_config(tp);
13315
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013316 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013317 !tg3_flag(tp, ENABLE_APE) &&
13318 !tg3_flag(tp, ENABLE_ASF)) {
Matt Carlsone2bf73e2011-12-08 14:40:15 +000013319 u32 bmsr, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013320
13321 tg3_readphy(tp, MII_BMSR, &bmsr);
13322 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
13323 (bmsr & BMSR_LSTATUS))
13324 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013325
Linus Torvalds1da177e2005-04-16 15:20:36 -070013326 err = tg3_phy_reset(tp);
13327 if (err)
13328 return err;
13329
Matt Carlson42b64a42011-05-19 12:12:49 +000013330 tg3_phy_set_wirespeed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013331
Matt Carlsone2bf73e2011-12-08 14:40:15 +000013332 if (!tg3_phy_copper_an_config_ok(tp, &dummy)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000013333 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
13334 tp->link_config.flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013335
13336 tg3_writephy(tp, MII_BMCR,
13337 BMCR_ANENABLE | BMCR_ANRESTART);
13338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013339 }
13340
13341skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000013342 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013343 err = tg3_init_5401phy_dsp(tp);
13344 if (err)
13345 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013346
Linus Torvalds1da177e2005-04-16 15:20:36 -070013347 err = tg3_init_5401phy_dsp(tp);
13348 }
13349
Linus Torvalds1da177e2005-04-16 15:20:36 -070013350 return err;
13351}
13352
Matt Carlson184b8902010-04-05 10:19:25 +000013353static void __devinit tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013354{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013355 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013356 unsigned int block_end, rosize, len;
Matt Carlson535a4902011-07-20 10:20:56 +000013357 u32 vpdlen;
Matt Carlson184b8902010-04-05 10:19:25 +000013358 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013359
Matt Carlson535a4902011-07-20 10:20:56 +000013360 vpd_data = (u8 *)tg3_vpd_readblock(tp, &vpdlen);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013361 if (!vpd_data)
13362 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013363
Matt Carlson535a4902011-07-20 10:20:56 +000013364 i = pci_vpd_find_tag(vpd_data, 0, vpdlen, PCI_VPD_LRDT_RO_DATA);
Matt Carlson4181b2c2010-02-26 14:04:45 +000013365 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013366 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013367
13368 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
13369 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
13370 i += PCI_VPD_LRDT_TAG_SIZE;
13371
Matt Carlson535a4902011-07-20 10:20:56 +000013372 if (block_end > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000013373 goto out_not_found;
13374
Matt Carlson184b8902010-04-05 10:19:25 +000013375 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13376 PCI_VPD_RO_KEYWORD_MFR_ID);
13377 if (j > 0) {
13378 len = pci_vpd_info_field_size(&vpd_data[j]);
13379
13380 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13381 if (j + len > block_end || len != 4 ||
13382 memcmp(&vpd_data[j], "1028", 4))
13383 goto partno;
13384
13385 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13386 PCI_VPD_RO_KEYWORD_VENDOR0);
13387 if (j < 0)
13388 goto partno;
13389
13390 len = pci_vpd_info_field_size(&vpd_data[j]);
13391
13392 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13393 if (j + len > block_end)
13394 goto partno;
13395
13396 memcpy(tp->fw_ver, &vpd_data[j], len);
Matt Carlson535a4902011-07-20 10:20:56 +000013397 strncat(tp->fw_ver, " bc ", vpdlen - len - 1);
Matt Carlson184b8902010-04-05 10:19:25 +000013398 }
13399
13400partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000013401 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13402 PCI_VPD_RO_KEYWORD_PARTNO);
13403 if (i < 0)
13404 goto out_not_found;
13405
13406 len = pci_vpd_info_field_size(&vpd_data[i]);
13407
13408 i += PCI_VPD_INFO_FLD_HDR_SIZE;
13409 if (len > TG3_BPN_SIZE ||
Matt Carlson535a4902011-07-20 10:20:56 +000013410 (len + i) > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000013411 goto out_not_found;
13412
13413 memcpy(tp->board_part_number, &vpd_data[i], len);
13414
Linus Torvalds1da177e2005-04-16 15:20:36 -070013415out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013416 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000013417 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013418 return;
13419
13420out_no_vpd:
Matt Carlson37a949c2010-09-30 10:34:33 +000013421 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
13422 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717)
13423 strcpy(tp->board_part_number, "BCM5717");
13424 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
13425 strcpy(tp->board_part_number, "BCM5718");
13426 else
13427 goto nomatch;
13428 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
13429 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
13430 strcpy(tp->board_part_number, "BCM57780");
13431 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
13432 strcpy(tp->board_part_number, "BCM57760");
13433 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
13434 strcpy(tp->board_part_number, "BCM57790");
13435 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
13436 strcpy(tp->board_part_number, "BCM57788");
13437 else
13438 goto nomatch;
13439 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
13440 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
13441 strcpy(tp->board_part_number, "BCM57761");
13442 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
13443 strcpy(tp->board_part_number, "BCM57765");
13444 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
13445 strcpy(tp->board_part_number, "BCM57781");
13446 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
13447 strcpy(tp->board_part_number, "BCM57785");
13448 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
13449 strcpy(tp->board_part_number, "BCM57791");
13450 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13451 strcpy(tp->board_part_number, "BCM57795");
13452 else
13453 goto nomatch;
Matt Carlson55086ad2011-12-14 11:09:59 +000013454 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57766) {
13455 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762)
13456 strcpy(tp->board_part_number, "BCM57762");
13457 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766)
13458 strcpy(tp->board_part_number, "BCM57766");
13459 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782)
13460 strcpy(tp->board_part_number, "BCM57782");
13461 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
13462 strcpy(tp->board_part_number, "BCM57786");
13463 else
13464 goto nomatch;
Matt Carlson37a949c2010-09-30 10:34:33 +000013465 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070013466 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000013467 } else {
13468nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070013469 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000013470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013471}
13472
Matt Carlson9c8a6202007-10-21 16:16:08 -070013473static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
13474{
13475 u32 val;
13476
Matt Carlsone4f34112009-02-25 14:25:00 +000013477 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013478 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013479 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013480 val != 0)
13481 return 0;
13482
13483 return 1;
13484}
13485
Matt Carlsonacd9c112009-02-25 14:26:33 +000013486static void __devinit tg3_read_bc_ver(struct tg3 *tp)
13487{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013488 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000013489 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013490 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013491
13492 if (tg3_nvram_read(tp, 0xc, &offset) ||
13493 tg3_nvram_read(tp, 0x4, &start))
13494 return;
13495
13496 offset = tg3_nvram_logical_addr(tp, offset);
13497
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013498 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013499 return;
13500
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013501 if ((val & 0xfc000000) == 0x0c000000) {
13502 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013503 return;
13504
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013505 if (val == 0)
13506 newver = true;
13507 }
13508
Matt Carlson75f99362010-04-05 10:19:24 +000013509 dst_off = strlen(tp->fw_ver);
13510
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013511 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000013512 if (TG3_VER_SIZE - dst_off < 16 ||
13513 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013514 return;
13515
13516 offset = offset + ver_offset - start;
13517 for (i = 0; i < 16; i += 4) {
13518 __be32 v;
13519 if (tg3_nvram_read_be32(tp, offset + i, &v))
13520 return;
13521
Matt Carlson75f99362010-04-05 10:19:24 +000013522 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013523 }
13524 } else {
13525 u32 major, minor;
13526
13527 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
13528 return;
13529
13530 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
13531 TG3_NVM_BCVER_MAJSFT;
13532 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000013533 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
13534 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013535 }
13536}
13537
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013538static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
13539{
13540 u32 val, major, minor;
13541
13542 /* Use native endian representation */
13543 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
13544 return;
13545
13546 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
13547 TG3_NVM_HWSB_CFG1_MAJSFT;
13548 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
13549 TG3_NVM_HWSB_CFG1_MINSFT;
13550
13551 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
13552}
13553
Matt Carlsondfe00d72008-11-21 17:19:41 -080013554static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
13555{
13556 u32 offset, major, minor, build;
13557
Matt Carlson75f99362010-04-05 10:19:24 +000013558 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013559
13560 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
13561 return;
13562
13563 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
13564 case TG3_EEPROM_SB_REVISION_0:
13565 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
13566 break;
13567 case TG3_EEPROM_SB_REVISION_2:
13568 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
13569 break;
13570 case TG3_EEPROM_SB_REVISION_3:
13571 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
13572 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000013573 case TG3_EEPROM_SB_REVISION_4:
13574 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
13575 break;
13576 case TG3_EEPROM_SB_REVISION_5:
13577 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
13578 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000013579 case TG3_EEPROM_SB_REVISION_6:
13580 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
13581 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013582 default:
13583 return;
13584 }
13585
Matt Carlsone4f34112009-02-25 14:25:00 +000013586 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080013587 return;
13588
13589 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
13590 TG3_EEPROM_SB_EDH_BLD_SHFT;
13591 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
13592 TG3_EEPROM_SB_EDH_MAJ_SHFT;
13593 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
13594
13595 if (minor > 99 || build > 26)
13596 return;
13597
Matt Carlson75f99362010-04-05 10:19:24 +000013598 offset = strlen(tp->fw_ver);
13599 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
13600 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013601
13602 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000013603 offset = strlen(tp->fw_ver);
13604 if (offset < TG3_VER_SIZE - 1)
13605 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013606 }
13607}
13608
Matt Carlsonacd9c112009-02-25 14:26:33 +000013609static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080013610{
13611 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013612 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070013613
13614 for (offset = TG3_NVM_DIR_START;
13615 offset < TG3_NVM_DIR_END;
13616 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000013617 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013618 return;
13619
13620 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
13621 break;
13622 }
13623
13624 if (offset == TG3_NVM_DIR_END)
13625 return;
13626
Joe Perches63c3a662011-04-26 08:12:10 +000013627 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013628 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000013629 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013630 return;
13631
Matt Carlsone4f34112009-02-25 14:25:00 +000013632 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013633 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013634 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013635 return;
13636
13637 offset += val - start;
13638
Matt Carlsonacd9c112009-02-25 14:26:33 +000013639 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013640
Matt Carlsonacd9c112009-02-25 14:26:33 +000013641 tp->fw_ver[vlen++] = ',';
13642 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070013643
13644 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000013645 __be32 v;
13646 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013647 return;
13648
Al Virob9fc7dc2007-12-17 22:59:57 -080013649 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013650
Matt Carlsonacd9c112009-02-25 14:26:33 +000013651 if (vlen > TG3_VER_SIZE - sizeof(v)) {
13652 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013653 break;
13654 }
13655
Matt Carlsonacd9c112009-02-25 14:26:33 +000013656 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
13657 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013658 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000013659}
13660
Matt Carlson7fd76442009-02-25 14:27:20 +000013661static void __devinit tg3_read_dash_ver(struct tg3 *tp)
13662{
13663 int vlen;
13664 u32 apedata;
Matt Carlsonecc79642010-08-02 11:26:01 +000013665 char *fwtype;
Matt Carlson7fd76442009-02-25 14:27:20 +000013666
Joe Perches63c3a662011-04-26 08:12:10 +000013667 if (!tg3_flag(tp, ENABLE_APE) || !tg3_flag(tp, ENABLE_ASF))
Matt Carlson7fd76442009-02-25 14:27:20 +000013668 return;
13669
13670 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
13671 if (apedata != APE_SEG_SIG_MAGIC)
13672 return;
13673
13674 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
13675 if (!(apedata & APE_FW_STATUS_READY))
13676 return;
13677
13678 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
13679
Matt Carlsondc6d0742010-09-15 08:59:55 +000013680 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI) {
Joe Perches63c3a662011-04-26 08:12:10 +000013681 tg3_flag_set(tp, APE_HAS_NCSI);
Matt Carlsonecc79642010-08-02 11:26:01 +000013682 fwtype = "NCSI";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013683 } else {
Matt Carlsonecc79642010-08-02 11:26:01 +000013684 fwtype = "DASH";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013685 }
Matt Carlsonecc79642010-08-02 11:26:01 +000013686
Matt Carlson7fd76442009-02-25 14:27:20 +000013687 vlen = strlen(tp->fw_ver);
13688
Matt Carlsonecc79642010-08-02 11:26:01 +000013689 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
13690 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000013691 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
13692 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
13693 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
13694 (apedata & APE_FW_VERSION_BLDMSK));
13695}
13696
Matt Carlsonacd9c112009-02-25 14:26:33 +000013697static void __devinit tg3_read_fw_ver(struct tg3 *tp)
13698{
13699 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000013700 bool vpd_vers = false;
13701
13702 if (tp->fw_ver[0] != 0)
13703 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013704
Joe Perches63c3a662011-04-26 08:12:10 +000013705 if (tg3_flag(tp, NO_NVRAM)) {
Matt Carlson75f99362010-04-05 10:19:24 +000013706 strcat(tp->fw_ver, "sb");
Matt Carlsondf259d82009-04-20 06:57:14 +000013707 return;
13708 }
13709
Matt Carlsonacd9c112009-02-25 14:26:33 +000013710 if (tg3_nvram_read(tp, 0, &val))
13711 return;
13712
13713 if (val == TG3_EEPROM_MAGIC)
13714 tg3_read_bc_ver(tp);
13715 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
13716 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013717 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
13718 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013719 else
13720 return;
13721
Matt Carlsonc9cab242011-07-13 09:27:27 +000013722 if (vpd_vers)
Matt Carlson75f99362010-04-05 10:19:24 +000013723 goto done;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013724
Matt Carlsonc9cab242011-07-13 09:27:27 +000013725 if (tg3_flag(tp, ENABLE_APE)) {
13726 if (tg3_flag(tp, ENABLE_ASF))
13727 tg3_read_dash_ver(tp);
13728 } else if (tg3_flag(tp, ENABLE_ASF)) {
13729 tg3_read_mgmtfw_ver(tp);
13730 }
Matt Carlson9c8a6202007-10-21 16:16:08 -070013731
Matt Carlson75f99362010-04-05 10:19:24 +000013732done:
Matt Carlson9c8a6202007-10-21 16:16:08 -070013733 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080013734}
13735
Michael Chan7544b092007-05-05 13:08:32 -070013736static struct pci_dev * __devinit tg3_find_peer(struct tg3 *);
13737
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013738static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
13739{
Joe Perches63c3a662011-04-26 08:12:10 +000013740 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013741 return TG3_RX_RET_MAX_SIZE_5717;
Joe Perches63c3a662011-04-26 08:12:10 +000013742 else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013743 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013744 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000013745 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013746}
13747
Matt Carlson41434702011-03-09 16:58:22 +000013748static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080013749 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
13750 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
13751 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
13752 { },
13753};
13754
Linus Torvalds1da177e2005-04-16 15:20:36 -070013755static int __devinit tg3_get_invariants(struct tg3 *tp)
13756{
Linus Torvalds1da177e2005-04-16 15:20:36 -070013757 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013758 u32 pci_state_reg, grc_misc_cfg;
13759 u32 val;
13760 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013761 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013762
Linus Torvalds1da177e2005-04-16 15:20:36 -070013763 /* Force memory write invalidate off. If we leave it on,
13764 * then on 5700_BX chips we have to enable a workaround.
13765 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
13766 * to match the cacheline size. The Broadcom driver have this
13767 * workaround but turns MWI off all the times so never uses
13768 * it. This seems to suggest that the workaround is insufficient.
13769 */
13770 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13771 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
13772 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13773
Matt Carlson16821282011-07-13 09:27:28 +000013774 /* Important! -- Make sure register accesses are byteswapped
13775 * correctly. Also, for those chips that require it, make
13776 * sure that indirect register accesses are enabled before
13777 * the first operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013778 */
13779 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13780 &misc_ctrl_reg);
Matt Carlson16821282011-07-13 09:27:28 +000013781 tp->misc_host_ctrl |= (misc_ctrl_reg &
13782 MISC_HOST_CTRL_CHIPREV);
13783 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13784 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013785
13786 tp->pci_chip_rev_id = (misc_ctrl_reg >>
13787 MISC_HOST_CTRL_CHIPREV_SHIFT);
Matt Carlson795d01c2007-10-07 23:28:17 -070013788 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
13789 u32 prod_id_asic_rev;
13790
Matt Carlson5001e2f2009-11-13 13:03:51 +000013791 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
13792 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013793 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
13794 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013795 pci_read_config_dword(tp->pdev,
13796 TG3PCI_GEN2_PRODID_ASICREV,
13797 &prod_id_asic_rev);
Matt Carlsonb703df62009-12-03 08:36:21 +000013798 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
13799 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
13800 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
13801 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
13802 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000013803 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
13804 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762 ||
13805 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766 ||
13806 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782 ||
13807 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
Matt Carlsonb703df62009-12-03 08:36:21 +000013808 pci_read_config_dword(tp->pdev,
13809 TG3PCI_GEN15_PRODID_ASICREV,
13810 &prod_id_asic_rev);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013811 else
13812 pci_read_config_dword(tp->pdev, TG3PCI_PRODID_ASICREV,
13813 &prod_id_asic_rev);
13814
Matt Carlson321d32a2008-11-21 17:22:19 -080013815 tp->pci_chip_rev_id = prod_id_asic_rev;
Matt Carlson795d01c2007-10-07 23:28:17 -070013816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013817
Michael Chanff645be2005-04-21 17:09:53 -070013818 /* Wrong chip ID in 5752 A0. This code can be removed later
13819 * as A0 is not in production.
13820 */
13821 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
13822 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
13823
Michael Chan68929142005-08-09 20:17:14 -070013824 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
13825 * we need to disable memory and use config. cycles
13826 * only to access all registers. The 5702/03 chips
13827 * can mistakenly decode the special cycles from the
13828 * ICH chipsets as memory write cycles, causing corruption
13829 * of register and memory space. Only certain ICH bridges
13830 * will drive special cycles with non-zero data during the
13831 * address phase which can fall within the 5703's address
13832 * range. This is not an ICH bug as the PCI spec allows
13833 * non-zero address during special cycles. However, only
13834 * these ICH bridges are known to drive non-zero addresses
13835 * during special cycles.
13836 *
13837 * Since special cycles do not cross PCI bridges, we only
13838 * enable this workaround if the 5703 is on the secondary
13839 * bus of these ICH bridges.
13840 */
13841 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
13842 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
13843 static struct tg3_dev_id {
13844 u32 vendor;
13845 u32 device;
13846 u32 rev;
13847 } ich_chipsets[] = {
13848 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
13849 PCI_ANY_ID },
13850 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
13851 PCI_ANY_ID },
13852 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
13853 0xa },
13854 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
13855 PCI_ANY_ID },
13856 { },
13857 };
13858 struct tg3_dev_id *pci_id = &ich_chipsets[0];
13859 struct pci_dev *bridge = NULL;
13860
13861 while (pci_id->vendor != 0) {
13862 bridge = pci_get_device(pci_id->vendor, pci_id->device,
13863 bridge);
13864 if (!bridge) {
13865 pci_id++;
13866 continue;
13867 }
13868 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070013869 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070013870 continue;
13871 }
13872 if (bridge->subordinate &&
13873 (bridge->subordinate->number ==
13874 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013875 tg3_flag_set(tp, ICH_WORKAROUND);
Michael Chan68929142005-08-09 20:17:14 -070013876 pci_dev_put(bridge);
13877 break;
13878 }
13879 }
13880 }
13881
Matt Carlson6ff6f812011-05-19 12:12:54 +000013882 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Matt Carlson41588ba12008-04-19 18:12:33 -070013883 static struct tg3_dev_id {
13884 u32 vendor;
13885 u32 device;
13886 } bridge_chipsets[] = {
13887 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
13888 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
13889 { },
13890 };
13891 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
13892 struct pci_dev *bridge = NULL;
13893
13894 while (pci_id->vendor != 0) {
13895 bridge = pci_get_device(pci_id->vendor,
13896 pci_id->device,
13897 bridge);
13898 if (!bridge) {
13899 pci_id++;
13900 continue;
13901 }
13902 if (bridge->subordinate &&
13903 (bridge->subordinate->number <=
13904 tp->pdev->bus->number) &&
13905 (bridge->subordinate->subordinate >=
13906 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013907 tg3_flag_set(tp, 5701_DMA_BUG);
Matt Carlson41588ba12008-04-19 18:12:33 -070013908 pci_dev_put(bridge);
13909 break;
13910 }
13911 }
13912 }
13913
Michael Chan4a29cc22006-03-19 13:21:12 -080013914 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
13915 * DMA addresses > 40-bit. This bridge may have other additional
13916 * 57xx devices behind it in some 4-port NIC designs for example.
13917 * Any tg3 device found behind the bridge will also need the 40-bit
13918 * DMA workaround.
13919 */
Michael Chana4e2b342005-10-26 15:46:52 -070013920 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
13921 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Joe Perches63c3a662011-04-26 08:12:10 +000013922 tg3_flag_set(tp, 5780_CLASS);
13923 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4cf78e42005-07-25 12:29:19 -070013924 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a588792010-04-05 10:19:28 +000013925 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080013926 struct pci_dev *bridge = NULL;
13927
13928 do {
13929 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
13930 PCI_DEVICE_ID_SERVERWORKS_EPB,
13931 bridge);
13932 if (bridge && bridge->subordinate &&
13933 (bridge->subordinate->number <=
13934 tp->pdev->bus->number) &&
13935 (bridge->subordinate->subordinate >=
13936 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013937 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4a29cc22006-03-19 13:21:12 -080013938 pci_dev_put(bridge);
13939 break;
13940 }
13941 } while (bridge);
13942 }
Michael Chan4cf78e42005-07-25 12:29:19 -070013943
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013944 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Matt Carlson3a1e19d2011-07-13 09:27:32 +000013945 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)
Michael Chan7544b092007-05-05 13:08:32 -070013946 tp->pdev_peer = tg3_find_peer(tp);
13947
Matt Carlsonc885e822010-08-02 11:25:57 +000013948 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013949 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13950 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Joe Perches63c3a662011-04-26 08:12:10 +000013951 tg3_flag_set(tp, 5717_PLUS);
Matt Carlson0a58d662011-04-05 14:22:45 +000013952
13953 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000013954 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57766)
13955 tg3_flag_set(tp, 57765_CLASS);
13956
13957 if (tg3_flag(tp, 57765_CLASS) || tg3_flag(tp, 5717_PLUS))
Joe Perches63c3a662011-04-26 08:12:10 +000013958 tg3_flag_set(tp, 57765_PLUS);
Matt Carlsonc885e822010-08-02 11:25:57 +000013959
Matt Carlson321d32a2008-11-21 17:22:19 -080013960 /* Intentionally exclude ASIC_REV_5906 */
13961 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Michael Chand9ab5ad12006-03-20 22:27:35 -080013962 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013963 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013964 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013965 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013966 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013967 tg3_flag(tp, 57765_PLUS))
13968 tg3_flag_set(tp, 5755_PLUS);
Matt Carlson321d32a2008-11-21 17:22:19 -080013969
13970 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13971 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Michael Chanb5d37722006-09-27 16:06:21 -070013972 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013973 tg3_flag(tp, 5755_PLUS) ||
13974 tg3_flag(tp, 5780_CLASS))
13975 tg3_flag_set(tp, 5750_PLUS);
John W. Linville6708e5c2005-04-21 17:00:52 -070013976
Matt Carlson6ff6f812011-05-19 12:12:54 +000013977 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013978 tg3_flag(tp, 5750_PLUS))
13979 tg3_flag_set(tp, 5705_PLUS);
John W. Linville1b440c562005-04-21 17:03:18 -070013980
Matt Carlson507399f2009-11-13 13:03:37 +000013981 /* Determine TSO capabilities */
Matt Carlsona0512942011-07-27 14:20:54 +000013982 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0)
Matt Carlson4d163b72011-01-25 15:58:48 +000013983 ; /* Do nothing. HW bug. */
Joe Perches63c3a662011-04-26 08:12:10 +000013984 else if (tg3_flag(tp, 57765_PLUS))
13985 tg3_flag_set(tp, HW_TSO_3);
13986 else if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000013987 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000013988 tg3_flag_set(tp, HW_TSO_2);
13989 else if (tg3_flag(tp, 5750_PLUS)) {
13990 tg3_flag_set(tp, HW_TSO_1);
13991 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013992 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
13993 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
Joe Perches63c3a662011-04-26 08:12:10 +000013994 tg3_flag_clear(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013995 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13996 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13997 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000013998 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013999 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
14000 tp->fw_needed = FIRMWARE_TG3TSO5;
14001 else
14002 tp->fw_needed = FIRMWARE_TG3TSO;
14003 }
14004
Matt Carlsondabc5c62011-05-19 12:12:52 +000014005 /* Selectively allow TSO based on operating conditions */
Matt Carlson6ff6f812011-05-19 12:12:54 +000014006 if (tg3_flag(tp, HW_TSO_1) ||
14007 tg3_flag(tp, HW_TSO_2) ||
14008 tg3_flag(tp, HW_TSO_3) ||
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000014009 tp->fw_needed) {
14010 /* For firmware TSO, assume ASF is disabled.
14011 * We'll disable TSO later if we discover ASF
14012 * is enabled in tg3_get_eeprom_hw_cfg().
14013 */
Matt Carlsondabc5c62011-05-19 12:12:52 +000014014 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000014015 } else {
Matt Carlsondabc5c62011-05-19 12:12:52 +000014016 tg3_flag_clear(tp, TSO_CAPABLE);
14017 tg3_flag_clear(tp, TSO_BUG);
14018 tp->fw_needed = NULL;
14019 }
14020
14021 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
14022 tp->fw_needed = FIRMWARE_TG3;
14023
Matt Carlson507399f2009-11-13 13:03:37 +000014024 tp->irq_max = 1;
14025
Joe Perches63c3a662011-04-26 08:12:10 +000014026 if (tg3_flag(tp, 5750_PLUS)) {
14027 tg3_flag_set(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070014028 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
14029 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
14030 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
14031 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
14032 tp->pdev_peer == tp->pdev))
Joe Perches63c3a662011-04-26 08:12:10 +000014033 tg3_flag_clear(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070014034
Joe Perches63c3a662011-04-26 08:12:10 +000014035 if (tg3_flag(tp, 5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070014036 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000014037 tg3_flag_set(tp, 1SHOT_MSI);
Michael Chan52c0fd82006-06-29 20:15:54 -070014038 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014039
Joe Perches63c3a662011-04-26 08:12:10 +000014040 if (tg3_flag(tp, 57765_PLUS)) {
14041 tg3_flag_set(tp, SUPPORT_MSIX);
Matt Carlson507399f2009-11-13 13:03:37 +000014042 tp->irq_max = TG3_IRQ_MAX_VECS;
14043 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014044 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000014045
Matt Carlson2ffcc982011-05-19 12:12:44 +000014046 if (tg3_flag(tp, 5755_PLUS))
Joe Perches63c3a662011-04-26 08:12:10 +000014047 tg3_flag_set(tp, SHORT_DMA_BUG);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014048
Matt Carlsone31aa982011-07-27 14:20:53 +000014049 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona4cb4282011-12-14 11:09:58 +000014050 tp->dma_limit = TG3_TX_BD_DMA_MAX_4K;
Matt Carlson55086ad2011-12-14 11:09:59 +000014051 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57766)
14052 tp->dma_limit = TG3_TX_BD_DMA_MAX_2K;
Matt Carlsone31aa982011-07-27 14:20:53 +000014053
Matt Carlsonfa6b2aa2011-11-21 15:01:19 +000014054 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14055 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
14056 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Joe Perches63c3a662011-04-26 08:12:10 +000014057 tg3_flag_set(tp, LRG_PROD_RING_CAP);
Matt Carlsonde9f5232011-04-05 14:22:43 +000014058
Joe Perches63c3a662011-04-26 08:12:10 +000014059 if (tg3_flag(tp, 57765_PLUS) &&
Matt Carlsona0512942011-07-27 14:20:54 +000014060 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0)
Joe Perches63c3a662011-04-26 08:12:10 +000014061 tg3_flag_set(tp, USE_JUMBO_BDFLAG);
Matt Carlsonb703df62009-12-03 08:36:21 +000014062
Joe Perches63c3a662011-04-26 08:12:10 +000014063 if (!tg3_flag(tp, 5705_PLUS) ||
14064 tg3_flag(tp, 5780_CLASS) ||
14065 tg3_flag(tp, USE_JUMBO_BDFLAG))
14066 tg3_flag_set(tp, JUMBO_CAPABLE);
Michael Chan0f893dc2005-07-25 12:30:38 -070014067
Matt Carlson52f44902008-11-21 17:17:04 -080014068 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14069 &pci_state_reg);
14070
Jon Mason708ebb3a2011-06-27 12:56:50 +000014071 if (pci_is_pcie(tp->pdev)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014072 u16 lnkctl;
14073
Joe Perches63c3a662011-04-26 08:12:10 +000014074 tg3_flag_set(tp, PCI_EXPRESS);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080014075
Matt Carlson2c55a3d2011-11-28 09:41:04 +000014076 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0) {
14077 int readrq = pcie_get_readrq(tp->pdev);
14078 if (readrq > 2048)
14079 pcie_set_readrq(tp->pdev, 2048);
14080 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -080014081
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014082 pci_read_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +000014083 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014084 &lnkctl);
14085 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
Matt Carlson7196cd62011-05-19 16:02:44 +000014086 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
14087 ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000014088 tg3_flag_clear(tp, HW_TSO_2);
Matt Carlsondabc5c62011-05-19 12:12:52 +000014089 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson7196cd62011-05-19 16:02:44 +000014090 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014091 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014092 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000014093 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
14094 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Joe Perches63c3a662011-04-26 08:12:10 +000014095 tg3_flag_set(tp, CLKREQ_BUG);
Matt Carlson614b0592010-01-20 16:58:02 +000014096 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000014097 tg3_flag_set(tp, L1PLLPD_EN);
Michael Chanc7835a72006-11-15 21:14:42 -080014098 }
Matt Carlson52f44902008-11-21 17:17:04 -080014099 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Jon Mason708ebb3a2011-06-27 12:56:50 +000014100 /* BCM5785 devices are effectively PCIe devices, and should
14101 * follow PCIe codepaths, but do not have a PCIe capabilities
14102 * section.
Matt Carlson93a700a2011-08-31 11:44:54 +000014103 */
Joe Perches63c3a662011-04-26 08:12:10 +000014104 tg3_flag_set(tp, PCI_EXPRESS);
14105 } else if (!tg3_flag(tp, 5705_PLUS) ||
14106 tg3_flag(tp, 5780_CLASS)) {
Matt Carlson52f44902008-11-21 17:17:04 -080014107 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
14108 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000014109 dev_err(&tp->pdev->dev,
14110 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080014111 return -EIO;
14112 }
14113
14114 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
Joe Perches63c3a662011-04-26 08:12:10 +000014115 tg3_flag_set(tp, PCIX_MODE);
Matt Carlson52f44902008-11-21 17:17:04 -080014116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014117
Michael Chan399de502005-10-03 14:02:39 -070014118 /* If we have an AMD 762 or VIA K8T800 chipset, write
14119 * reordering to the mailbox registers done by the host
14120 * controller can cause major troubles. We read back from
14121 * every mailbox register write to force the writes to be
14122 * posted to the chip in order.
14123 */
Matt Carlson41434702011-03-09 16:58:22 +000014124 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Joe Perches63c3a662011-04-26 08:12:10 +000014125 !tg3_flag(tp, PCI_EXPRESS))
14126 tg3_flag_set(tp, MBOX_WRITE_REORDER);
Michael Chan399de502005-10-03 14:02:39 -070014127
Matt Carlson69fc4052008-12-21 20:19:57 -080014128 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
14129 &tp->pci_cacheline_sz);
14130 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
14131 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014132 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14133 tp->pci_lat_timer < 64) {
14134 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080014135 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
14136 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014137 }
14138
Matt Carlson16821282011-07-13 09:27:28 +000014139 /* Important! -- It is critical that the PCI-X hw workaround
14140 * situation is decided before the first MMIO register access.
14141 */
Matt Carlson52f44902008-11-21 17:17:04 -080014142 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
14143 /* 5700 BX chips need to have their TX producer index
14144 * mailboxes written twice to workaround a bug.
14145 */
Joe Perches63c3a662011-04-26 08:12:10 +000014146 tg3_flag_set(tp, TXD_MBOX_HWBUG);
Matt Carlson9974a352007-10-07 23:27:28 -070014147
Matt Carlson52f44902008-11-21 17:17:04 -080014148 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014149 *
14150 * The workaround is to use indirect register accesses
14151 * for all chip writes not to mailbox registers.
14152 */
Joe Perches63c3a662011-04-26 08:12:10 +000014153 if (tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014154 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014155
Joe Perches63c3a662011-04-26 08:12:10 +000014156 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014157
14158 /* The chip can have it's power management PCI config
14159 * space registers clobbered due to this bug.
14160 * So explicitly force the chip into D0 here.
14161 */
Matt Carlson9974a352007-10-07 23:27:28 -070014162 pci_read_config_dword(tp->pdev,
14163 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014164 &pm_reg);
14165 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
14166 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070014167 pci_write_config_dword(tp->pdev,
14168 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014169 pm_reg);
14170
14171 /* Also, force SERR#/PERR# in PCI command. */
14172 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
14173 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
14174 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
14175 }
14176 }
14177
Linus Torvalds1da177e2005-04-16 15:20:36 -070014178 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000014179 tg3_flag_set(tp, PCI_HIGH_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014180 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000014181 tg3_flag_set(tp, PCI_32BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014182
14183 /* Chip-specific fixup from Broadcom driver */
14184 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
14185 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
14186 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
14187 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
14188 }
14189
Michael Chan1ee582d2005-08-09 20:16:46 -070014190 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070014191 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070014192 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070014193 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070014194 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070014195 tp->write32_tx_mbox = tg3_write32;
14196 tp->write32_rx_mbox = tg3_write32;
14197
14198 /* Various workaround register access methods */
Joe Perches63c3a662011-04-26 08:12:10 +000014199 if (tg3_flag(tp, PCIX_TARGET_HWBUG))
Michael Chan1ee582d2005-08-09 20:16:46 -070014200 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070014201 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014202 (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson98efd8a2007-05-05 12:47:25 -070014203 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
14204 /*
14205 * Back to back register writes can cause problems on these
14206 * chips, the workaround is to read back all reg writes
14207 * except those to mailbox regs.
14208 *
14209 * See tg3_write_indirect_reg32().
14210 */
Michael Chan1ee582d2005-08-09 20:16:46 -070014211 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070014212 }
14213
Joe Perches63c3a662011-04-26 08:12:10 +000014214 if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
Michael Chan1ee582d2005-08-09 20:16:46 -070014215 tp->write32_tx_mbox = tg3_write32_tx_mbox;
Joe Perches63c3a662011-04-26 08:12:10 +000014216 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Michael Chan1ee582d2005-08-09 20:16:46 -070014217 tp->write32_rx_mbox = tg3_write_flush_reg32;
14218 }
Michael Chan20094932005-08-09 20:16:32 -070014219
Joe Perches63c3a662011-04-26 08:12:10 +000014220 if (tg3_flag(tp, ICH_WORKAROUND)) {
Michael Chan68929142005-08-09 20:17:14 -070014221 tp->read32 = tg3_read_indirect_reg32;
14222 tp->write32 = tg3_write_indirect_reg32;
14223 tp->read32_mbox = tg3_read_indirect_mbox;
14224 tp->write32_mbox = tg3_write_indirect_mbox;
14225 tp->write32_tx_mbox = tg3_write_indirect_mbox;
14226 tp->write32_rx_mbox = tg3_write_indirect_mbox;
14227
14228 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070014229 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070014230
14231 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
14232 pci_cmd &= ~PCI_COMMAND_MEMORY;
14233 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
14234 }
Michael Chanb5d37722006-09-27 16:06:21 -070014235 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
14236 tp->read32_mbox = tg3_read32_mbox_5906;
14237 tp->write32_mbox = tg3_write32_mbox_5906;
14238 tp->write32_tx_mbox = tg3_write32_mbox_5906;
14239 tp->write32_rx_mbox = tg3_write32_mbox_5906;
14240 }
Michael Chan68929142005-08-09 20:17:14 -070014241
Michael Chanbbadf502006-04-06 21:46:34 -070014242 if (tp->write32 == tg3_write_indirect_reg32 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014243 (tg3_flag(tp, PCIX_MODE) &&
Michael Chanbbadf502006-04-06 21:46:34 -070014244 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070014245 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Joe Perches63c3a662011-04-26 08:12:10 +000014246 tg3_flag_set(tp, SRAM_USE_CONFIG);
Michael Chanbbadf502006-04-06 21:46:34 -070014247
Matt Carlson16821282011-07-13 09:27:28 +000014248 /* The memory arbiter has to be enabled in order for SRAM accesses
14249 * to succeed. Normally on powerup the tg3 chip firmware will make
14250 * sure it is enabled, but other entities such as system netboot
14251 * code might disable it.
14252 */
14253 val = tr32(MEMARB_MODE);
14254 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
14255
Matt Carlson9dc5e342011-11-04 09:15:02 +000014256 tp->pci_fn = PCI_FUNC(tp->pdev->devfn) & 3;
14257 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
14258 tg3_flag(tp, 5780_CLASS)) {
14259 if (tg3_flag(tp, PCIX_MODE)) {
14260 pci_read_config_dword(tp->pdev,
14261 tp->pcix_cap + PCI_X_STATUS,
14262 &val);
14263 tp->pci_fn = val & 0x7;
14264 }
14265 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
14266 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
14267 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) ==
14268 NIC_SRAM_CPMUSTAT_SIG) {
14269 tp->pci_fn = val & TG3_CPMU_STATUS_FMSK_5717;
14270 tp->pci_fn = tp->pci_fn ? 1 : 0;
14271 }
14272 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
14273 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
14274 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
14275 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) ==
14276 NIC_SRAM_CPMUSTAT_SIG) {
14277 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5719) >>
14278 TG3_CPMU_STATUS_FSHFT_5719;
14279 }
Matt Carlson69f11c92011-07-13 09:27:30 +000014280 }
14281
Michael Chan7d0c41e2005-04-21 17:06:20 -070014282 /* Get eeprom hw config before calling tg3_set_power_state().
Joe Perches63c3a662011-04-26 08:12:10 +000014283 * In particular, the TG3_FLAG_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070014284 * determined before calling tg3_set_power_state() so that
14285 * we know whether or not to switch out of Vaux power.
14286 * When the flag is set, it means that GPIO1 is used for eeprom
14287 * write protect and also implies that it is a LOM where GPIOs
14288 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040014289 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070014290 tg3_get_eeprom_hw_cfg(tp);
14291
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000014292 if (tp->fw_needed && tg3_flag(tp, ENABLE_ASF)) {
14293 tg3_flag_clear(tp, TSO_CAPABLE);
14294 tg3_flag_clear(tp, TSO_BUG);
14295 tp->fw_needed = NULL;
14296 }
14297
Joe Perches63c3a662011-04-26 08:12:10 +000014298 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070014299 /* Allow reads and writes to the
14300 * APE register and memory space.
14301 */
14302 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +000014303 PCISTATE_ALLOW_APE_SHMEM_WR |
14304 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070014305 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
14306 pci_state_reg);
Matt Carlsonc9cab242011-07-13 09:27:27 +000014307
14308 tg3_ape_lock_init(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070014309 }
14310
Matt Carlson9936bcf2007-10-10 18:03:07 -070014311 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson57e69832008-05-25 23:48:31 -070014312 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014313 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014314 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014315 tg3_flag(tp, 57765_PLUS))
14316 tg3_flag_set(tp, CPMU_PRESENT);
Matt Carlsond30cdd22007-10-07 23:28:35 -070014317
Matt Carlson16821282011-07-13 09:27:28 +000014318 /* Set up tp->grc_local_ctrl before calling
14319 * tg3_pwrsrc_switch_to_vmain(). GPIO1 driven high
14320 * will bring 5700's external PHY out of reset.
Michael Chan314fba32005-04-21 17:07:04 -070014321 * It is also used as eeprom write protect on LOMs.
14322 */
14323 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
Matt Carlson6ff6f812011-05-19 12:12:54 +000014324 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014325 tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan314fba32005-04-21 17:07:04 -070014326 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
14327 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070014328 /* Unused GPIO3 must be driven as output on 5752 because there
14329 * are no pull-up resistors on unused GPIO pins.
14330 */
14331 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
14332 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070014333
Matt Carlson321d32a2008-11-21 17:22:19 -080014334 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsoncb4ed1f2010-01-20 16:58:09 +000014335 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000014336 tg3_flag(tp, 57765_CLASS))
Michael Chanaf36e6b2006-03-23 01:28:06 -080014337 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
14338
Matt Carlson8d519ab2009-04-20 06:58:01 +000014339 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
14340 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070014341 /* Turn off the debug UART. */
14342 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014343 if (tg3_flag(tp, IS_NIC))
Matt Carlson5f0c4a32008-06-09 15:41:12 -070014344 /* Keep VMain power. */
14345 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
14346 GRC_LCLCTRL_GPIO_OUTPUT0;
14347 }
14348
Matt Carlson16821282011-07-13 09:27:28 +000014349 /* Switch out of Vaux if it is a NIC */
14350 tg3_pwrsrc_switch_to_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014351
Linus Torvalds1da177e2005-04-16 15:20:36 -070014352 /* Derive initial jumbo mode from MTU assigned in
14353 * ether_setup() via the alloc_etherdev() call
14354 */
Joe Perches63c3a662011-04-26 08:12:10 +000014355 if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
14356 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014357
14358 /* Determine WakeOnLan speed to use. */
14359 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14360 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
14361 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
14362 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
Joe Perches63c3a662011-04-26 08:12:10 +000014363 tg3_flag_clear(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014364 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000014365 tg3_flag_set(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014366 }
14367
Matt Carlson7f97a4b2009-08-25 10:10:03 +000014368 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014369 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000014370
Linus Torvalds1da177e2005-04-16 15:20:36 -070014371 /* A few boards don't want Ethernet@WireSpeed phy feature */
Matt Carlson6ff6f812011-05-19 12:12:54 +000014372 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14373 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070014374 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070014375 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014376 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
14377 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
14378 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014379
14380 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
14381 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014382 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014383 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014384 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014385
Joe Perches63c3a662011-04-26 08:12:10 +000014386 if (tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014387 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080014388 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014389 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014390 !tg3_flag(tp, 57765_PLUS)) {
Michael Chanc424cb22006-04-29 18:56:34 -070014391 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070014392 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070014393 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
14394 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080014395 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
14396 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014397 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080014398 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014399 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080014400 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014401 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070014402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014403
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014404 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
14405 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
14406 tp->phy_otp = tg3_read_otp_phycfg(tp);
14407 if (tp->phy_otp == 0)
14408 tp->phy_otp = TG3_OTP_DEFAULT;
14409 }
14410
Joe Perches63c3a662011-04-26 08:12:10 +000014411 if (tg3_flag(tp, CPMU_PRESENT))
Matt Carlson8ef21422008-05-02 16:47:53 -070014412 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
14413 else
14414 tp->mi_mode = MAC_MI_MODE_BASE;
14415
Linus Torvalds1da177e2005-04-16 15:20:36 -070014416 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014417 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
14418 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
14419 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
14420
Matt Carlson4d958472011-04-20 07:57:35 +000014421 /* Set these bits to enable statistics workaround. */
14422 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14423 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
14424 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0) {
14425 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
14426 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
14427 }
14428
Matt Carlson321d32a2008-11-21 17:22:19 -080014429 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
14430 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Joe Perches63c3a662011-04-26 08:12:10 +000014431 tg3_flag_set(tp, USE_PHYLIB);
Matt Carlson57e69832008-05-25 23:48:31 -070014432
Matt Carlson158d7ab2008-05-29 01:37:54 -070014433 err = tg3_mdio_init(tp);
14434 if (err)
14435 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014436
14437 /* Initialize data/descriptor byte/word swapping. */
14438 val = tr32(GRC_MODE);
Matt Carlsonf2096f92011-04-05 14:22:48 +000014439 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
14440 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
14441 GRC_MODE_WORD_SWAP_B2HRX_DATA |
14442 GRC_MODE_B2HRX_ENABLE |
14443 GRC_MODE_HTX2B_ENABLE |
14444 GRC_MODE_HOST_STACKUP);
14445 else
14446 val &= GRC_MODE_HOST_STACKUP;
14447
Linus Torvalds1da177e2005-04-16 15:20:36 -070014448 tw32(GRC_MODE, val | tp->grc_mode);
14449
14450 tg3_switch_clocks(tp);
14451
14452 /* Clear this out for sanity. */
14453 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
14454
14455 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14456 &pci_state_reg);
14457 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014458 !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014459 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
14460
14461 if (chiprevid == CHIPREV_ID_5701_A0 ||
14462 chiprevid == CHIPREV_ID_5701_B0 ||
14463 chiprevid == CHIPREV_ID_5701_B2 ||
14464 chiprevid == CHIPREV_ID_5701_B5) {
14465 void __iomem *sram_base;
14466
14467 /* Write some dummy words into the SRAM status block
14468 * area, see if it reads back correctly. If the return
14469 * value is bad, force enable the PCIX workaround.
14470 */
14471 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
14472
14473 writel(0x00000000, sram_base);
14474 writel(0x00000000, sram_base + 4);
14475 writel(0xffffffff, sram_base + 4);
14476 if (readl(sram_base) != 0x00000000)
Joe Perches63c3a662011-04-26 08:12:10 +000014477 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014478 }
14479 }
14480
14481 udelay(50);
14482 tg3_nvram_init(tp);
14483
14484 grc_misc_cfg = tr32(GRC_MISC_CFG);
14485 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
14486
Linus Torvalds1da177e2005-04-16 15:20:36 -070014487 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14488 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
14489 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
Joe Perches63c3a662011-04-26 08:12:10 +000014490 tg3_flag_set(tp, IS_5788);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014491
Joe Perches63c3a662011-04-26 08:12:10 +000014492 if (!tg3_flag(tp, IS_5788) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +000014493 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014494 tg3_flag_set(tp, TAGGED_STATUS);
14495 if (tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070014496 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
14497 HOSTCC_MODE_CLRTICK_TXBD);
14498
14499 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
14500 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
14501 tp->misc_host_ctrl);
14502 }
14503
Matt Carlson3bda1252008-08-15 14:08:22 -070014504 /* Preserve the APE MAC_MODE bits */
Joe Perches63c3a662011-04-26 08:12:10 +000014505 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +000014506 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070014507 else
Matt Carlson6e01b202011-08-19 13:58:20 +000014508 tp->mac_mode = 0;
Matt Carlson3bda1252008-08-15 14:08:22 -070014509
Linus Torvalds1da177e2005-04-16 15:20:36 -070014510 /* these are limited to 10/100 only */
14511 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14512 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
14513 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14514 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14515 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
14516 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
14517 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
14518 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14519 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
Michael Chan676917d2006-12-07 00:20:22 -080014520 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
14521 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014522 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
Matt Carlsond1101142010-02-17 15:16:55 +000014523 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
14524 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014525 (tp->phy_flags & TG3_PHYFLG_IS_FET))
14526 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014527
14528 err = tg3_phy_probe(tp);
14529 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014530 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014531 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014532 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014533 }
14534
Matt Carlson184b8902010-04-05 10:19:25 +000014535 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080014536 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014537
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014538 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
14539 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014540 } else {
14541 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014542 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014543 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014544 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014545 }
14546
14547 /* 5700 {AX,BX} chips have a broken status block link
14548 * change bit implementation, so we must use the
14549 * status register in those cases.
14550 */
14551 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014552 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014553 else
Joe Perches63c3a662011-04-26 08:12:10 +000014554 tg3_flag_clear(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014555
14556 /* The led_ctrl is set during tg3_phy_probe, here we might
14557 * have to force the link status polling mechanism based
14558 * upon subsystem IDs.
14559 */
14560 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070014561 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014562 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
14563 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Joe Perches63c3a662011-04-26 08:12:10 +000014564 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014565 }
14566
14567 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014568 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Joe Perches63c3a662011-04-26 08:12:10 +000014569 tg3_flag_set(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014570 else
Joe Perches63c3a662011-04-26 08:12:10 +000014571 tg3_flag_clear(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014572
Eric Dumazet9205fd92011-11-18 06:47:01 +000014573 tp->rx_offset = NET_SKB_PAD + NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014574 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014575 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014576 tg3_flag(tp, PCIX_MODE)) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000014577 tp->rx_offset = NET_SKB_PAD;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014578#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000014579 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014580#endif
14581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014582
Matt Carlson2c49a442010-09-30 10:34:35 +000014583 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
14584 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000014585 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
14586
Matt Carlson2c49a442010-09-30 10:34:35 +000014587 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070014588
14589 /* Increment the rx prod index on the rx std ring by at most
14590 * 8 for these chips to workaround hw errata.
14591 */
14592 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
14593 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
14594 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
14595 tp->rx_std_max_post = 8;
14596
Joe Perches63c3a662011-04-26 08:12:10 +000014597 if (tg3_flag(tp, ASPM_WORKAROUND))
Matt Carlson8ed5d972007-05-07 00:25:49 -070014598 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
14599 PCIE_PWR_MGMT_L1_THRESH_MSK;
14600
Linus Torvalds1da177e2005-04-16 15:20:36 -070014601 return err;
14602}
14603
David S. Miller49b6e95f2007-03-29 01:38:42 -070014604#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014605static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
14606{
14607 struct net_device *dev = tp->dev;
14608 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014609 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070014610 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014611 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014612
David S. Miller49b6e95f2007-03-29 01:38:42 -070014613 addr = of_get_property(dp, "local-mac-address", &len);
14614 if (addr && len == 6) {
14615 memcpy(dev->dev_addr, addr, 6);
14616 memcpy(dev->perm_addr, dev->dev_addr, 6);
14617 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014618 }
14619 return -ENODEV;
14620}
14621
14622static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
14623{
14624 struct net_device *dev = tp->dev;
14625
14626 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070014627 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014628 return 0;
14629}
14630#endif
14631
14632static int __devinit tg3_get_device_address(struct tg3 *tp)
14633{
14634 struct net_device *dev = tp->dev;
14635 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080014636 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014637
David S. Miller49b6e95f2007-03-29 01:38:42 -070014638#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014639 if (!tg3_get_macaddr_sparc(tp))
14640 return 0;
14641#endif
14642
14643 mac_offset = 0x7c;
Matt Carlson6ff6f812011-05-19 12:12:54 +000014644 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014645 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014646 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
14647 mac_offset = 0xcc;
14648 if (tg3_nvram_lock(tp))
14649 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
14650 else
14651 tg3_nvram_unlock(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000014652 } else if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson69f11c92011-07-13 09:27:30 +000014653 if (tp->pci_fn & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014654 mac_offset = 0xcc;
Matt Carlson69f11c92011-07-13 09:27:30 +000014655 if (tp->pci_fn > 1)
Matt Carlsona50d0792010-06-05 17:24:37 +000014656 mac_offset += 0x18c;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014657 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070014658 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014659
14660 /* First try to get it from MAC address mailbox. */
14661 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
14662 if ((hi >> 16) == 0x484b) {
14663 dev->dev_addr[0] = (hi >> 8) & 0xff;
14664 dev->dev_addr[1] = (hi >> 0) & 0xff;
14665
14666 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
14667 dev->dev_addr[2] = (lo >> 24) & 0xff;
14668 dev->dev_addr[3] = (lo >> 16) & 0xff;
14669 dev->dev_addr[4] = (lo >> 8) & 0xff;
14670 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014671
Michael Chan008652b2006-03-27 23:14:53 -080014672 /* Some old bootcode may report a 0 MAC address in SRAM */
14673 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
14674 }
14675 if (!addr_ok) {
14676 /* Next, try NVRAM. */
Joe Perches63c3a662011-04-26 08:12:10 +000014677 if (!tg3_flag(tp, NO_NVRAM) &&
Matt Carlsondf259d82009-04-20 06:57:14 +000014678 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000014679 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070014680 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
14681 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080014682 }
14683 /* Finally just fetch it out of the MAC control regs. */
14684 else {
14685 hi = tr32(MAC_ADDR_0_HIGH);
14686 lo = tr32(MAC_ADDR_0_LOW);
14687
14688 dev->dev_addr[5] = lo & 0xff;
14689 dev->dev_addr[4] = (lo >> 8) & 0xff;
14690 dev->dev_addr[3] = (lo >> 16) & 0xff;
14691 dev->dev_addr[2] = (lo >> 24) & 0xff;
14692 dev->dev_addr[1] = hi & 0xff;
14693 dev->dev_addr[0] = (hi >> 8) & 0xff;
14694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014695 }
14696
14697 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070014698#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014699 if (!tg3_get_default_macaddr_sparc(tp))
14700 return 0;
14701#endif
14702 return -EINVAL;
14703 }
John W. Linville2ff43692005-09-12 14:44:20 -070014704 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014705 return 0;
14706}
14707
David S. Miller59e6b432005-05-18 22:50:10 -070014708#define BOUNDARY_SINGLE_CACHELINE 1
14709#define BOUNDARY_MULTI_CACHELINE 2
14710
14711static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
14712{
14713 int cacheline_size;
14714 u8 byte;
14715 int goal;
14716
14717 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
14718 if (byte == 0)
14719 cacheline_size = 1024;
14720 else
14721 cacheline_size = (int) byte * 4;
14722
14723 /* On 5703 and later chips, the boundary bits have no
14724 * effect.
14725 */
14726 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14727 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014728 !tg3_flag(tp, PCI_EXPRESS))
David S. Miller59e6b432005-05-18 22:50:10 -070014729 goto out;
14730
14731#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
14732 goal = BOUNDARY_MULTI_CACHELINE;
14733#else
14734#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
14735 goal = BOUNDARY_SINGLE_CACHELINE;
14736#else
14737 goal = 0;
14738#endif
14739#endif
14740
Joe Perches63c3a662011-04-26 08:12:10 +000014741 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014742 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
14743 goto out;
14744 }
14745
David S. Miller59e6b432005-05-18 22:50:10 -070014746 if (!goal)
14747 goto out;
14748
14749 /* PCI controllers on most RISC systems tend to disconnect
14750 * when a device tries to burst across a cache-line boundary.
14751 * Therefore, letting tg3 do so just wastes PCI bandwidth.
14752 *
14753 * Unfortunately, for PCI-E there are only limited
14754 * write-side controls for this, and thus for reads
14755 * we will still get the disconnects. We'll also waste
14756 * these PCI cycles for both read and write for chips
14757 * other than 5700 and 5701 which do not implement the
14758 * boundary bits.
14759 */
Joe Perches63c3a662011-04-26 08:12:10 +000014760 if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014761 switch (cacheline_size) {
14762 case 16:
14763 case 32:
14764 case 64:
14765 case 128:
14766 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14767 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
14768 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
14769 } else {
14770 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14771 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14772 }
14773 break;
14774
14775 case 256:
14776 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
14777 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
14778 break;
14779
14780 default:
14781 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14782 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14783 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014784 }
Joe Perches63c3a662011-04-26 08:12:10 +000014785 } else if (tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014786 switch (cacheline_size) {
14787 case 16:
14788 case 32:
14789 case 64:
14790 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14791 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14792 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
14793 break;
14794 }
14795 /* fallthrough */
14796 case 128:
14797 default:
14798 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14799 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
14800 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014801 }
David S. Miller59e6b432005-05-18 22:50:10 -070014802 } else {
14803 switch (cacheline_size) {
14804 case 16:
14805 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14806 val |= (DMA_RWCTRL_READ_BNDRY_16 |
14807 DMA_RWCTRL_WRITE_BNDRY_16);
14808 break;
14809 }
14810 /* fallthrough */
14811 case 32:
14812 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14813 val |= (DMA_RWCTRL_READ_BNDRY_32 |
14814 DMA_RWCTRL_WRITE_BNDRY_32);
14815 break;
14816 }
14817 /* fallthrough */
14818 case 64:
14819 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14820 val |= (DMA_RWCTRL_READ_BNDRY_64 |
14821 DMA_RWCTRL_WRITE_BNDRY_64);
14822 break;
14823 }
14824 /* fallthrough */
14825 case 128:
14826 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14827 val |= (DMA_RWCTRL_READ_BNDRY_128 |
14828 DMA_RWCTRL_WRITE_BNDRY_128);
14829 break;
14830 }
14831 /* fallthrough */
14832 case 256:
14833 val |= (DMA_RWCTRL_READ_BNDRY_256 |
14834 DMA_RWCTRL_WRITE_BNDRY_256);
14835 break;
14836 case 512:
14837 val |= (DMA_RWCTRL_READ_BNDRY_512 |
14838 DMA_RWCTRL_WRITE_BNDRY_512);
14839 break;
14840 case 1024:
14841 default:
14842 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
14843 DMA_RWCTRL_WRITE_BNDRY_1024);
14844 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014845 }
David S. Miller59e6b432005-05-18 22:50:10 -070014846 }
14847
14848out:
14849 return val;
14850}
14851
Linus Torvalds1da177e2005-04-16 15:20:36 -070014852static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
14853{
14854 struct tg3_internal_buffer_desc test_desc;
14855 u32 sram_dma_descs;
14856 int i, ret;
14857
14858 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
14859
14860 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
14861 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
14862 tw32(RDMAC_STATUS, 0);
14863 tw32(WDMAC_STATUS, 0);
14864
14865 tw32(BUFMGR_MODE, 0);
14866 tw32(FTQ_RESET, 0);
14867
14868 test_desc.addr_hi = ((u64) buf_dma) >> 32;
14869 test_desc.addr_lo = buf_dma & 0xffffffff;
14870 test_desc.nic_mbuf = 0x00002100;
14871 test_desc.len = size;
14872
14873 /*
14874 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
14875 * the *second* time the tg3 driver was getting loaded after an
14876 * initial scan.
14877 *
14878 * Broadcom tells me:
14879 * ...the DMA engine is connected to the GRC block and a DMA
14880 * reset may affect the GRC block in some unpredictable way...
14881 * The behavior of resets to individual blocks has not been tested.
14882 *
14883 * Broadcom noted the GRC reset will also reset all sub-components.
14884 */
14885 if (to_device) {
14886 test_desc.cqid_sqid = (13 << 8) | 2;
14887
14888 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
14889 udelay(40);
14890 } else {
14891 test_desc.cqid_sqid = (16 << 8) | 7;
14892
14893 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
14894 udelay(40);
14895 }
14896 test_desc.flags = 0x00000005;
14897
14898 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
14899 u32 val;
14900
14901 val = *(((u32 *)&test_desc) + i);
14902 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
14903 sram_dma_descs + (i * sizeof(u32)));
14904 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
14905 }
14906 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
14907
Matt Carlson859a588792010-04-05 10:19:28 +000014908 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014909 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a588792010-04-05 10:19:28 +000014910 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070014911 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014912
14913 ret = -ENODEV;
14914 for (i = 0; i < 40; i++) {
14915 u32 val;
14916
14917 if (to_device)
14918 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
14919 else
14920 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
14921 if ((val & 0xffff) == sram_dma_descs) {
14922 ret = 0;
14923 break;
14924 }
14925
14926 udelay(100);
14927 }
14928
14929 return ret;
14930}
14931
David S. Millerded73402005-05-23 13:59:47 -070014932#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070014933
Matt Carlson41434702011-03-09 16:58:22 +000014934static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080014935 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
14936 { },
14937};
14938
Linus Torvalds1da177e2005-04-16 15:20:36 -070014939static int __devinit tg3_test_dma(struct tg3 *tp)
14940{
14941 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070014942 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014943 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014944
Matt Carlson4bae65c2010-11-24 08:31:52 +000014945 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
14946 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014947 if (!buf) {
14948 ret = -ENOMEM;
14949 goto out_nofree;
14950 }
14951
14952 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
14953 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
14954
David S. Miller59e6b432005-05-18 22:50:10 -070014955 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014956
Joe Perches63c3a662011-04-26 08:12:10 +000014957 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014958 goto out;
14959
Joe Perches63c3a662011-04-26 08:12:10 +000014960 if (tg3_flag(tp, PCI_EXPRESS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014961 /* DMA read watermark not used on PCIE */
14962 tp->dma_rwctrl |= 0x00180000;
Joe Perches63c3a662011-04-26 08:12:10 +000014963 } else if (!tg3_flag(tp, PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070014964 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
14965 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014966 tp->dma_rwctrl |= 0x003f0000;
14967 else
14968 tp->dma_rwctrl |= 0x003f000f;
14969 } else {
14970 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14971 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
14972 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080014973 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014974
Michael Chan4a29cc22006-03-19 13:21:12 -080014975 /* If the 5704 is behind the EPB bridge, we can
14976 * do the less restrictive ONE_DMA workaround for
14977 * better performance.
14978 */
Joe Perches63c3a662011-04-26 08:12:10 +000014979 if (tg3_flag(tp, 40BIT_DMA_BUG) &&
Michael Chan4a29cc22006-03-19 13:21:12 -080014980 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14981 tp->dma_rwctrl |= 0x8000;
14982 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014983 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
14984
Michael Chan49afdeb2007-02-13 12:17:03 -080014985 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
14986 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070014987 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080014988 tp->dma_rwctrl |=
14989 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
14990 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
14991 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070014992 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
14993 /* 5780 always in PCIX mode */
14994 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070014995 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
14996 /* 5714 always in PCIX mode */
14997 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014998 } else {
14999 tp->dma_rwctrl |= 0x001b000f;
15000 }
15001 }
15002
15003 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
15004 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
15005 tp->dma_rwctrl &= 0xfffffff0;
15006
15007 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
15008 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
15009 /* Remove this if it causes problems for some boards. */
15010 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
15011
15012 /* On 5700/5701 chips, we need to set this bit.
15013 * Otherwise the chip will issue cacheline transactions
15014 * to streamable DMA memory with not all the byte
15015 * enables turned on. This is an error on several
15016 * RISC PCI controllers, in particular sparc64.
15017 *
15018 * On 5703/5704 chips, this bit has been reassigned
15019 * a different meaning. In particular, it is used
15020 * on those chips to enable a PCI-X workaround.
15021 */
15022 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
15023 }
15024
15025 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15026
15027#if 0
15028 /* Unneeded, already done by tg3_get_invariants. */
15029 tg3_switch_clocks(tp);
15030#endif
15031
Linus Torvalds1da177e2005-04-16 15:20:36 -070015032 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
15033 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
15034 goto out;
15035
David S. Miller59e6b432005-05-18 22:50:10 -070015036 /* It is best to perform DMA test with maximum write burst size
15037 * to expose the 5700/5701 write DMA bug.
15038 */
15039 saved_dma_rwctrl = tp->dma_rwctrl;
15040 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
15041 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15042
Linus Torvalds1da177e2005-04-16 15:20:36 -070015043 while (1) {
15044 u32 *p = buf, i;
15045
15046 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
15047 p[i] = i;
15048
15049 /* Send the buffer to the chip. */
15050 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
15051 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000015052 dev_err(&tp->pdev->dev,
15053 "%s: Buffer write failed. err = %d\n",
15054 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015055 break;
15056 }
15057
15058#if 0
15059 /* validate data reached card RAM correctly. */
15060 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
15061 u32 val;
15062 tg3_read_mem(tp, 0x2100 + (i*4), &val);
15063 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000015064 dev_err(&tp->pdev->dev,
15065 "%s: Buffer corrupted on device! "
15066 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015067 /* ret = -ENODEV here? */
15068 }
15069 p[i] = 0;
15070 }
15071#endif
15072 /* Now read it back. */
15073 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
15074 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000015075 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
15076 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015077 break;
15078 }
15079
15080 /* Verify it. */
15081 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
15082 if (p[i] == i)
15083 continue;
15084
David S. Miller59e6b432005-05-18 22:50:10 -070015085 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
15086 DMA_RWCTRL_WRITE_BNDRY_16) {
15087 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015088 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
15089 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15090 break;
15091 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000015092 dev_err(&tp->pdev->dev,
15093 "%s: Buffer corrupted on read back! "
15094 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015095 ret = -ENODEV;
15096 goto out;
15097 }
15098 }
15099
15100 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
15101 /* Success. */
15102 ret = 0;
15103 break;
15104 }
15105 }
David S. Miller59e6b432005-05-18 22:50:10 -070015106 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
15107 DMA_RWCTRL_WRITE_BNDRY_16) {
15108 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070015109 * now look for chipsets that are known to expose the
15110 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070015111 */
Matt Carlson41434702011-03-09 16:58:22 +000015112 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070015113 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
15114 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a588792010-04-05 10:19:28 +000015115 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070015116 /* Safe to use the calculated DMA boundary. */
15117 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a588792010-04-05 10:19:28 +000015118 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070015119
David S. Miller59e6b432005-05-18 22:50:10 -070015120 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15121 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015122
15123out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000015124 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015125out_nofree:
15126 return ret;
15127}
15128
Linus Torvalds1da177e2005-04-16 15:20:36 -070015129static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
15130{
Joe Perches63c3a662011-04-26 08:12:10 +000015131 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson666bc832010-01-20 16:58:03 +000015132 tp->bufmgr_config.mbuf_read_dma_low_water =
15133 DEFAULT_MB_RDMA_LOW_WATER_5705;
15134 tp->bufmgr_config.mbuf_mac_rx_low_water =
15135 DEFAULT_MB_MACRX_LOW_WATER_57765;
15136 tp->bufmgr_config.mbuf_high_water =
15137 DEFAULT_MB_HIGH_WATER_57765;
15138
15139 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15140 DEFAULT_MB_RDMA_LOW_WATER_5705;
15141 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15142 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
15143 tp->bufmgr_config.mbuf_high_water_jumbo =
15144 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000015145 } else if (tg3_flag(tp, 5705_PLUS)) {
Michael Chanfdfec1722005-07-25 12:31:48 -070015146 tp->bufmgr_config.mbuf_read_dma_low_water =
15147 DEFAULT_MB_RDMA_LOW_WATER_5705;
15148 tp->bufmgr_config.mbuf_mac_rx_low_water =
15149 DEFAULT_MB_MACRX_LOW_WATER_5705;
15150 tp->bufmgr_config.mbuf_high_water =
15151 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070015152 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
15153 tp->bufmgr_config.mbuf_mac_rx_low_water =
15154 DEFAULT_MB_MACRX_LOW_WATER_5906;
15155 tp->bufmgr_config.mbuf_high_water =
15156 DEFAULT_MB_HIGH_WATER_5906;
15157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015158
Michael Chanfdfec1722005-07-25 12:31:48 -070015159 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15160 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
15161 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15162 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
15163 tp->bufmgr_config.mbuf_high_water_jumbo =
15164 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
15165 } else {
15166 tp->bufmgr_config.mbuf_read_dma_low_water =
15167 DEFAULT_MB_RDMA_LOW_WATER;
15168 tp->bufmgr_config.mbuf_mac_rx_low_water =
15169 DEFAULT_MB_MACRX_LOW_WATER;
15170 tp->bufmgr_config.mbuf_high_water =
15171 DEFAULT_MB_HIGH_WATER;
15172
15173 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15174 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
15175 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15176 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
15177 tp->bufmgr_config.mbuf_high_water_jumbo =
15178 DEFAULT_MB_HIGH_WATER_JUMBO;
15179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015180
15181 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
15182 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
15183}
15184
15185static char * __devinit tg3_phy_string(struct tg3 *tp)
15186{
Matt Carlson79eb6902010-02-17 15:17:03 +000015187 switch (tp->phy_id & TG3_PHY_ID_MASK) {
15188 case TG3_PHY_ID_BCM5400: return "5400";
15189 case TG3_PHY_ID_BCM5401: return "5401";
15190 case TG3_PHY_ID_BCM5411: return "5411";
15191 case TG3_PHY_ID_BCM5701: return "5701";
15192 case TG3_PHY_ID_BCM5703: return "5703";
15193 case TG3_PHY_ID_BCM5704: return "5704";
15194 case TG3_PHY_ID_BCM5705: return "5705";
15195 case TG3_PHY_ID_BCM5750: return "5750";
15196 case TG3_PHY_ID_BCM5752: return "5752";
15197 case TG3_PHY_ID_BCM5714: return "5714";
15198 case TG3_PHY_ID_BCM5780: return "5780";
15199 case TG3_PHY_ID_BCM5755: return "5755";
15200 case TG3_PHY_ID_BCM5787: return "5787";
15201 case TG3_PHY_ID_BCM5784: return "5784";
15202 case TG3_PHY_ID_BCM5756: return "5722/5756";
15203 case TG3_PHY_ID_BCM5906: return "5906";
15204 case TG3_PHY_ID_BCM5761: return "5761";
15205 case TG3_PHY_ID_BCM5718C: return "5718C";
15206 case TG3_PHY_ID_BCM5718S: return "5718S";
15207 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000015208 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000015209 case TG3_PHY_ID_BCM5720C: return "5720C";
Matt Carlson79eb6902010-02-17 15:17:03 +000015210 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070015211 case 0: return "serdes";
15212 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070015213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015214}
15215
Michael Chanf9804dd2005-09-27 12:13:10 -070015216static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
15217{
Joe Perches63c3a662011-04-26 08:12:10 +000015218 if (tg3_flag(tp, PCI_EXPRESS)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070015219 strcpy(str, "PCI Express");
15220 return str;
Joe Perches63c3a662011-04-26 08:12:10 +000015221 } else if (tg3_flag(tp, PCIX_MODE)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070015222 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
15223
15224 strcpy(str, "PCIX:");
15225
15226 if ((clock_ctrl == 7) ||
15227 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
15228 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
15229 strcat(str, "133MHz");
15230 else if (clock_ctrl == 0)
15231 strcat(str, "33MHz");
15232 else if (clock_ctrl == 2)
15233 strcat(str, "50MHz");
15234 else if (clock_ctrl == 4)
15235 strcat(str, "66MHz");
15236 else if (clock_ctrl == 6)
15237 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070015238 } else {
15239 strcpy(str, "PCI:");
Joe Perches63c3a662011-04-26 08:12:10 +000015240 if (tg3_flag(tp, PCI_HIGH_SPEED))
Michael Chanf9804dd2005-09-27 12:13:10 -070015241 strcat(str, "66MHz");
15242 else
15243 strcat(str, "33MHz");
15244 }
Joe Perches63c3a662011-04-26 08:12:10 +000015245 if (tg3_flag(tp, PCI_32BIT))
Michael Chanf9804dd2005-09-27 12:13:10 -070015246 strcat(str, ":32-bit");
15247 else
15248 strcat(str, ":64-bit");
15249 return str;
15250}
15251
Michael Chan8c2dc7e2005-12-19 16:26:02 -080015252static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015253{
15254 struct pci_dev *peer;
15255 unsigned int func, devnr = tp->pdev->devfn & ~7;
15256
15257 for (func = 0; func < 8; func++) {
15258 peer = pci_get_slot(tp->pdev->bus, devnr | func);
15259 if (peer && peer != tp->pdev)
15260 break;
15261 pci_dev_put(peer);
15262 }
Michael Chan16fe9d72005-12-13 21:09:54 -080015263 /* 5704 can be configured in single-port mode, set peer to
15264 * tp->pdev in that case.
15265 */
15266 if (!peer) {
15267 peer = tp->pdev;
15268 return peer;
15269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015270
15271 /*
15272 * We don't need to keep the refcount elevated; there's no way
15273 * to remove one half of this device without removing the other
15274 */
15275 pci_dev_put(peer);
15276
15277 return peer;
15278}
15279
David S. Miller15f98502005-05-18 22:49:26 -070015280static void __devinit tg3_init_coal(struct tg3 *tp)
15281{
15282 struct ethtool_coalesce *ec = &tp->coal;
15283
15284 memset(ec, 0, sizeof(*ec));
15285 ec->cmd = ETHTOOL_GCOALESCE;
15286 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
15287 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
15288 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
15289 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
15290 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
15291 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
15292 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
15293 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
15294 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
15295
15296 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
15297 HOSTCC_MODE_CLRTICK_TXBD)) {
15298 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
15299 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
15300 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
15301 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
15302 }
Michael Chand244c892005-07-05 14:42:33 -070015303
Joe Perches63c3a662011-04-26 08:12:10 +000015304 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070015305 ec->rx_coalesce_usecs_irq = 0;
15306 ec->tx_coalesce_usecs_irq = 0;
15307 ec->stats_block_coalesce_usecs = 0;
15308 }
David S. Miller15f98502005-05-18 22:49:26 -070015309}
15310
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080015311static const struct net_device_ops tg3_netdev_ops = {
15312 .ndo_open = tg3_open,
15313 .ndo_stop = tg3_close,
Stephen Hemminger00829822008-11-20 20:14:53 -080015314 .ndo_start_xmit = tg3_start_xmit,
Eric Dumazet511d2222010-07-07 20:44:24 +000015315 .ndo_get_stats64 = tg3_get_stats64,
Stephen Hemminger00829822008-11-20 20:14:53 -080015316 .ndo_validate_addr = eth_validate_addr,
Jiri Pirkoafc4b132011-08-16 06:29:01 +000015317 .ndo_set_rx_mode = tg3_set_rx_mode,
Stephen Hemminger00829822008-11-20 20:14:53 -080015318 .ndo_set_mac_address = tg3_set_mac_addr,
15319 .ndo_do_ioctl = tg3_ioctl,
15320 .ndo_tx_timeout = tg3_tx_timeout,
15321 .ndo_change_mtu = tg3_change_mtu,
Michał Mirosławdc668912011-04-07 03:35:07 +000015322 .ndo_fix_features = tg3_fix_features,
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015323 .ndo_set_features = tg3_set_features,
Stephen Hemminger00829822008-11-20 20:14:53 -080015324#ifdef CONFIG_NET_POLL_CONTROLLER
15325 .ndo_poll_controller = tg3_poll_controller,
15326#endif
15327};
15328
Linus Torvalds1da177e2005-04-16 15:20:36 -070015329static int __devinit tg3_init_one(struct pci_dev *pdev,
15330 const struct pci_device_id *ent)
15331{
Linus Torvalds1da177e2005-04-16 15:20:36 -070015332 struct net_device *dev;
15333 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000015334 int i, err, pm_cap;
15335 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070015336 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080015337 u64 dma_mask, persist_dma_mask;
Michał Mirosławc8f44af2011-11-15 15:29:55 +000015338 netdev_features_t features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015339
Joe Perches05dbe002010-02-17 19:44:19 +000015340 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015341
15342 err = pci_enable_device(pdev);
15343 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015344 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015345 return err;
15346 }
15347
Linus Torvalds1da177e2005-04-16 15:20:36 -070015348 err = pci_request_regions(pdev, DRV_MODULE_NAME);
15349 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015350 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015351 goto err_out_disable_pdev;
15352 }
15353
15354 pci_set_master(pdev);
15355
15356 /* Find power-management capability. */
15357 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
15358 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000015359 dev_err(&pdev->dev,
15360 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015361 err = -EIO;
15362 goto err_out_free_res;
15363 }
15364
Matt Carlson16821282011-07-13 09:27:28 +000015365 err = pci_set_power_state(pdev, PCI_D0);
15366 if (err) {
15367 dev_err(&pdev->dev, "Transition to D0 failed, aborting\n");
15368 goto err_out_free_res;
15369 }
15370
Matt Carlsonfe5f5782009-09-01 13:09:39 +000015371 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015372 if (!dev) {
Matt Carlson2445e462010-04-05 10:19:21 +000015373 dev_err(&pdev->dev, "Etherdev alloc failed, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015374 err = -ENOMEM;
Matt Carlson16821282011-07-13 09:27:28 +000015375 goto err_out_power_down;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015376 }
15377
Linus Torvalds1da177e2005-04-16 15:20:36 -070015378 SET_NETDEV_DEV(dev, &pdev->dev);
15379
Linus Torvalds1da177e2005-04-16 15:20:36 -070015380 tp = netdev_priv(dev);
15381 tp->pdev = pdev;
15382 tp->dev = dev;
15383 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015384 tp->rx_mode = TG3_DEF_RX_MODE;
15385 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070015386
Linus Torvalds1da177e2005-04-16 15:20:36 -070015387 if (tg3_debug > 0)
15388 tp->msg_enable = tg3_debug;
15389 else
15390 tp->msg_enable = TG3_DEF_MSG_ENABLE;
15391
15392 /* The word/byte swap controls here control register access byte
15393 * swapping. DMA data byte swapping is controlled in the GRC_MODE
15394 * setting below.
15395 */
15396 tp->misc_host_ctrl =
15397 MISC_HOST_CTRL_MASK_PCI_INT |
15398 MISC_HOST_CTRL_WORD_SWAP |
15399 MISC_HOST_CTRL_INDIR_ACCESS |
15400 MISC_HOST_CTRL_PCISTATE_RW;
15401
15402 /* The NONFRM (non-frame) byte/word swap controls take effect
15403 * on descriptor entries, anything which isn't packet data.
15404 *
15405 * The StrongARM chips on the board (one for tx, one for rx)
15406 * are running in big-endian mode.
15407 */
15408 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
15409 GRC_MODE_WSWAP_NONFRM_DATA);
15410#ifdef __BIG_ENDIAN
15411 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
15412#endif
15413 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015414 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000015415 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015416
Matt Carlsond5fe4882008-11-21 17:20:32 -080015417 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010015418 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015419 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015420 err = -ENOMEM;
15421 goto err_out_free_dev;
15422 }
15423
Matt Carlsonc9cab242011-07-13 09:27:27 +000015424 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
15425 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761E ||
15426 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S ||
15427 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761SE ||
15428 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
15429 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
15430 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
15431 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720) {
15432 tg3_flag_set(tp, ENABLE_APE);
15433 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
15434 if (!tp->aperegs) {
15435 dev_err(&pdev->dev,
15436 "Cannot map APE registers, aborting\n");
15437 err = -ENOMEM;
15438 goto err_out_iounmap;
15439 }
15440 }
15441
Linus Torvalds1da177e2005-04-16 15:20:36 -070015442 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
15443 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015444
Linus Torvalds1da177e2005-04-16 15:20:36 -070015445 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015446 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Matt Carlson2ffcc982011-05-19 12:12:44 +000015447 dev->netdev_ops = &tg3_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015448 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015449
15450 err = tg3_get_invariants(tp);
15451 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015452 dev_err(&pdev->dev,
15453 "Problem fetching invariants of chip, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015454 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015455 }
15456
Michael Chan4a29cc22006-03-19 13:21:12 -080015457 /* The EPB bridge inside 5714, 5715, and 5780 and any
15458 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080015459 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
15460 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
15461 * do DMA address check in tg3_start_xmit().
15462 */
Joe Perches63c3a662011-04-26 08:12:10 +000015463 if (tg3_flag(tp, IS_5788))
Yang Hongyang284901a2009-04-06 19:01:15 -070015464 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Joe Perches63c3a662011-04-26 08:12:10 +000015465 else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070015466 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080015467#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070015468 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015469#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080015470 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070015471 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015472
15473 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070015474 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080015475 err = pci_set_dma_mask(pdev, dma_mask);
15476 if (!err) {
Matt Carlson0da06062011-05-19 12:12:53 +000015477 features |= NETIF_F_HIGHDMA;
Michael Chan72f2afb2006-03-06 19:28:35 -080015478 err = pci_set_consistent_dma_mask(pdev,
15479 persist_dma_mask);
15480 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015481 dev_err(&pdev->dev, "Unable to obtain 64 bit "
15482 "DMA for consistent allocations\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015483 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080015484 }
15485 }
15486 }
Yang Hongyang284901a2009-04-06 19:01:15 -070015487 if (err || dma_mask == DMA_BIT_MASK(32)) {
15488 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080015489 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015490 dev_err(&pdev->dev,
15491 "No usable DMA configuration, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015492 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080015493 }
15494 }
15495
Michael Chanfdfec1722005-07-25 12:31:48 -070015496 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015497
Matt Carlson0da06062011-05-19 12:12:53 +000015498 features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
15499
15500 /* 5700 B0 chips do not support checksumming correctly due
15501 * to hardware bugs.
15502 */
15503 if (tp->pci_chip_rev_id != CHIPREV_ID_5700_B0) {
15504 features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
15505
15506 if (tg3_flag(tp, 5755_PLUS))
15507 features |= NETIF_F_IPV6_CSUM;
15508 }
15509
Michael Chan4e3a7aa2006-03-20 17:47:44 -080015510 /* TSO is on by default on chips that support hardware TSO.
15511 * Firmware TSO on older chips gives lower performance, so it
15512 * is off by default, but can be enabled using ethtool.
15513 */
Joe Perches63c3a662011-04-26 08:12:10 +000015514 if ((tg3_flag(tp, HW_TSO_1) ||
15515 tg3_flag(tp, HW_TSO_2) ||
15516 tg3_flag(tp, HW_TSO_3)) &&
Matt Carlson0da06062011-05-19 12:12:53 +000015517 (features & NETIF_F_IP_CSUM))
15518 features |= NETIF_F_TSO;
Joe Perches63c3a662011-04-26 08:12:10 +000015519 if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
Matt Carlson0da06062011-05-19 12:12:53 +000015520 if (features & NETIF_F_IPV6_CSUM)
15521 features |= NETIF_F_TSO6;
Joe Perches63c3a662011-04-26 08:12:10 +000015522 if (tg3_flag(tp, HW_TSO_3) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000015523 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070015524 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
15525 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Joe Perches63c3a662011-04-26 08:12:10 +000015526 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Michał Mirosławdc668912011-04-07 03:35:07 +000015527 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson0da06062011-05-19 12:12:53 +000015528 features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070015529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015530
Matt Carlsond542fe22011-05-19 16:02:43 +000015531 dev->features |= features;
15532 dev->vlan_features |= features;
15533
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015534 /*
15535 * Add loopback capability only for a subset of devices that support
15536 * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
15537 * loopback for the remaining devices.
15538 */
15539 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
15540 !tg3_flag(tp, CPMU_PRESENT))
15541 /* Add the loopback capability */
Matt Carlson0da06062011-05-19 12:12:53 +000015542 features |= NETIF_F_LOOPBACK;
15543
Matt Carlson0da06062011-05-19 12:12:53 +000015544 dev->hw_features |= features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015545
Linus Torvalds1da177e2005-04-16 15:20:36 -070015546 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015547 !tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015548 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015549 tg3_flag_set(tp, MAX_RXPEND_64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015550 tp->rx_pending = 63;
15551 }
15552
Linus Torvalds1da177e2005-04-16 15:20:36 -070015553 err = tg3_get_device_address(tp);
15554 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015555 dev_err(&pdev->dev,
15556 "Could not obtain valid ethernet address, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015557 goto err_out_apeunmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070015558 }
15559
Matt Carlsonc88864d2007-11-12 21:07:01 -080015560 /*
15561 * Reset chip in case UNDI or EFI driver did not shutdown
15562 * DMA self test will enable WDMAC and we'll see (spurious)
15563 * pending DMA on the PCI bus at that point.
15564 */
15565 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
15566 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
15567 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
15568 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
15569 }
15570
15571 err = tg3_test_dma(tp);
15572 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015573 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080015574 goto err_out_apeunmap;
15575 }
15576
Matt Carlson78f90dc2009-11-13 13:03:42 +000015577 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
15578 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
15579 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000015580 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000015581 struct tg3_napi *tnapi = &tp->napi[i];
15582
15583 tnapi->tp = tp;
15584 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
15585
15586 tnapi->int_mbox = intmbx;
Matt Carlson93a700a2011-08-31 11:44:54 +000015587 if (i <= 4)
Matt Carlson78f90dc2009-11-13 13:03:42 +000015588 intmbx += 0x8;
15589 else
15590 intmbx += 0x4;
15591
15592 tnapi->consmbox = rcvmbx;
15593 tnapi->prodmbox = sndmbx;
15594
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015595 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000015596 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015597 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000015598 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000015599
Joe Perches63c3a662011-04-26 08:12:10 +000015600 if (!tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson78f90dc2009-11-13 13:03:42 +000015601 break;
15602
15603 /*
15604 * If we support MSIX, we'll be using RSS. If we're using
15605 * RSS, the first vector only handles link interrupts and the
15606 * remaining vectors handle rx and tx interrupts. Reuse the
15607 * mailbox values for the next iteration. The values we setup
15608 * above are still useful for the single vectored mode.
15609 */
15610 if (!i)
15611 continue;
15612
15613 rcvmbx += 0x8;
15614
15615 if (sndmbx & 0x4)
15616 sndmbx -= 0x4;
15617 else
15618 sndmbx += 0xc;
15619 }
15620
Matt Carlsonc88864d2007-11-12 21:07:01 -080015621 tg3_init_coal(tp);
15622
Michael Chanc49a1562006-12-17 17:07:29 -080015623 pci_set_drvdata(pdev, dev);
15624
Matt Carlsoncd0d7222011-07-13 09:27:33 +000015625 if (tg3_flag(tp, 5717_PLUS)) {
15626 /* Resume a low-power mode */
15627 tg3_frob_aux_power(tp, false);
15628 }
15629
Linus Torvalds1da177e2005-04-16 15:20:36 -070015630 err = register_netdev(dev);
15631 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015632 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070015633 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015634 }
15635
Joe Perches05dbe002010-02-17 19:44:19 +000015636 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
15637 tp->board_part_number,
15638 tp->pci_chip_rev_id,
15639 tg3_bus_string(tp, str),
15640 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015641
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015642 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000015643 struct phy_device *phydev;
15644 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000015645 netdev_info(dev,
15646 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000015647 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015648 } else {
15649 char *ethtype;
15650
15651 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
15652 ethtype = "10/100Base-TX";
15653 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
15654 ethtype = "1000Base-SX";
15655 else
15656 ethtype = "10/100/1000Base-T";
15657
Matt Carlson5129c3a2010-04-05 10:19:23 +000015658 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlson47007832011-04-20 07:57:43 +000015659 "(WireSpeed[%d], EEE[%d])\n",
15660 tg3_phy_string(tp), ethtype,
15661 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
15662 (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015663 }
Matt Carlsondf59c942008-11-03 16:52:56 -080015664
Joe Perches05dbe002010-02-17 19:44:19 +000015665 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000015666 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015667 tg3_flag(tp, USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015668 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015669 tg3_flag(tp, ENABLE_ASF) != 0,
15670 tg3_flag(tp, TSO_CAPABLE) != 0);
Joe Perches05dbe002010-02-17 19:44:19 +000015671 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
15672 tp->dma_rwctrl,
15673 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
15674 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015675
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015676 pci_save_state(pdev);
15677
Linus Torvalds1da177e2005-04-16 15:20:36 -070015678 return 0;
15679
Matt Carlson0d3031d2007-10-10 18:02:43 -070015680err_out_apeunmap:
15681 if (tp->aperegs) {
15682 iounmap(tp->aperegs);
15683 tp->aperegs = NULL;
15684 }
15685
Linus Torvalds1da177e2005-04-16 15:20:36 -070015686err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070015687 if (tp->regs) {
15688 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015689 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015691
15692err_out_free_dev:
15693 free_netdev(dev);
15694
Matt Carlson16821282011-07-13 09:27:28 +000015695err_out_power_down:
15696 pci_set_power_state(pdev, PCI_D3hot);
15697
Linus Torvalds1da177e2005-04-16 15:20:36 -070015698err_out_free_res:
15699 pci_release_regions(pdev);
15700
15701err_out_disable_pdev:
15702 pci_disable_device(pdev);
15703 pci_set_drvdata(pdev, NULL);
15704 return err;
15705}
15706
15707static void __devexit tg3_remove_one(struct pci_dev *pdev)
15708{
15709 struct net_device *dev = pci_get_drvdata(pdev);
15710
15711 if (dev) {
15712 struct tg3 *tp = netdev_priv(dev);
15713
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080015714 if (tp->fw)
15715 release_firmware(tp->fw);
15716
Matt Carlsondb219972011-11-04 09:15:03 +000015717 tg3_reset_task_cancel(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015718
David S. Miller1805b2f2011-10-24 18:18:09 -040015719 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015720 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015721 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015722 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070015723
Linus Torvalds1da177e2005-04-16 15:20:36 -070015724 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015725 if (tp->aperegs) {
15726 iounmap(tp->aperegs);
15727 tp->aperegs = NULL;
15728 }
Michael Chan68929142005-08-09 20:17:14 -070015729 if (tp->regs) {
15730 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015731 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015732 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015733 free_netdev(dev);
15734 pci_release_regions(pdev);
15735 pci_disable_device(pdev);
15736 pci_set_drvdata(pdev, NULL);
15737 }
15738}
15739
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015740#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015741static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015742{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015743 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015744 struct net_device *dev = pci_get_drvdata(pdev);
15745 struct tg3 *tp = netdev_priv(dev);
15746 int err;
15747
15748 if (!netif_running(dev))
15749 return 0;
15750
Matt Carlsondb219972011-11-04 09:15:03 +000015751 tg3_reset_task_cancel(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015752 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015753 tg3_netif_stop(tp);
15754
15755 del_timer_sync(&tp->timer);
15756
David S. Millerf47c11e2005-06-24 20:18:35 -070015757 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015758 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070015759 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015760
15761 netif_device_detach(dev);
15762
David S. Millerf47c11e2005-06-24 20:18:35 -070015763 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070015764 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches63c3a662011-04-26 08:12:10 +000015765 tg3_flag_clear(tp, INIT_COMPLETE);
David S. Millerf47c11e2005-06-24 20:18:35 -070015766 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015767
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015768 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015769 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015770 int err2;
15771
David S. Millerf47c11e2005-06-24 20:18:35 -070015772 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015773
Joe Perches63c3a662011-04-26 08:12:10 +000015774 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015775 err2 = tg3_restart_hw(tp, 1);
15776 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070015777 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015778
15779 tp->timer.expires = jiffies + tp->timer_offset;
15780 add_timer(&tp->timer);
15781
15782 netif_device_attach(dev);
15783 tg3_netif_start(tp);
15784
Michael Chanb9ec6c12006-07-25 16:37:27 -070015785out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015786 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015787
15788 if (!err2)
15789 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015790 }
15791
15792 return err;
15793}
15794
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015795static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015796{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015797 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015798 struct net_device *dev = pci_get_drvdata(pdev);
15799 struct tg3 *tp = netdev_priv(dev);
15800 int err;
15801
15802 if (!netif_running(dev))
15803 return 0;
15804
Linus Torvalds1da177e2005-04-16 15:20:36 -070015805 netif_device_attach(dev);
15806
David S. Millerf47c11e2005-06-24 20:18:35 -070015807 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015808
Joe Perches63c3a662011-04-26 08:12:10 +000015809 tg3_flag_set(tp, INIT_COMPLETE);
Michael Chanb9ec6c12006-07-25 16:37:27 -070015810 err = tg3_restart_hw(tp, 1);
15811 if (err)
15812 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015813
15814 tp->timer.expires = jiffies + tp->timer_offset;
15815 add_timer(&tp->timer);
15816
Linus Torvalds1da177e2005-04-16 15:20:36 -070015817 tg3_netif_start(tp);
15818
Michael Chanb9ec6c12006-07-25 16:37:27 -070015819out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015820 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015821
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015822 if (!err)
15823 tg3_phy_start(tp);
15824
Michael Chanb9ec6c12006-07-25 16:37:27 -070015825 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015826}
15827
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015828static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015829#define TG3_PM_OPS (&tg3_pm_ops)
15830
15831#else
15832
15833#define TG3_PM_OPS NULL
15834
15835#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015836
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015837/**
15838 * tg3_io_error_detected - called when PCI error is detected
15839 * @pdev: Pointer to PCI device
15840 * @state: The current pci connection state
15841 *
15842 * This function is called after a PCI bus error affecting
15843 * this device has been detected.
15844 */
15845static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
15846 pci_channel_state_t state)
15847{
15848 struct net_device *netdev = pci_get_drvdata(pdev);
15849 struct tg3 *tp = netdev_priv(netdev);
15850 pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
15851
15852 netdev_info(netdev, "PCI I/O error detected\n");
15853
15854 rtnl_lock();
15855
15856 if (!netif_running(netdev))
15857 goto done;
15858
15859 tg3_phy_stop(tp);
15860
15861 tg3_netif_stop(tp);
15862
15863 del_timer_sync(&tp->timer);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015864
15865 /* Want to make sure that the reset task doesn't run */
Matt Carlsondb219972011-11-04 09:15:03 +000015866 tg3_reset_task_cancel(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000015867 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015868
15869 netif_device_detach(netdev);
15870
15871 /* Clean up software state, even if MMIO is blocked */
15872 tg3_full_lock(tp, 0);
15873 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
15874 tg3_full_unlock(tp);
15875
15876done:
15877 if (state == pci_channel_io_perm_failure)
15878 err = PCI_ERS_RESULT_DISCONNECT;
15879 else
15880 pci_disable_device(pdev);
15881
15882 rtnl_unlock();
15883
15884 return err;
15885}
15886
15887/**
15888 * tg3_io_slot_reset - called after the pci bus has been reset.
15889 * @pdev: Pointer to PCI device
15890 *
15891 * Restart the card from scratch, as if from a cold-boot.
15892 * At this point, the card has exprienced a hard reset,
15893 * followed by fixups by BIOS, and has its config space
15894 * set up identically to what it was at cold boot.
15895 */
15896static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
15897{
15898 struct net_device *netdev = pci_get_drvdata(pdev);
15899 struct tg3 *tp = netdev_priv(netdev);
15900 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
15901 int err;
15902
15903 rtnl_lock();
15904
15905 if (pci_enable_device(pdev)) {
15906 netdev_err(netdev, "Cannot re-enable PCI device after reset.\n");
15907 goto done;
15908 }
15909
15910 pci_set_master(pdev);
15911 pci_restore_state(pdev);
15912 pci_save_state(pdev);
15913
15914 if (!netif_running(netdev)) {
15915 rc = PCI_ERS_RESULT_RECOVERED;
15916 goto done;
15917 }
15918
15919 err = tg3_power_up(tp);
Matt Carlsonbed98292011-07-13 09:27:29 +000015920 if (err)
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015921 goto done;
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015922
15923 rc = PCI_ERS_RESULT_RECOVERED;
15924
15925done:
15926 rtnl_unlock();
15927
15928 return rc;
15929}
15930
15931/**
15932 * tg3_io_resume - called when traffic can start flowing again.
15933 * @pdev: Pointer to PCI device
15934 *
15935 * This callback is called when the error recovery driver tells
15936 * us that its OK to resume normal operation.
15937 */
15938static void tg3_io_resume(struct pci_dev *pdev)
15939{
15940 struct net_device *netdev = pci_get_drvdata(pdev);
15941 struct tg3 *tp = netdev_priv(netdev);
15942 int err;
15943
15944 rtnl_lock();
15945
15946 if (!netif_running(netdev))
15947 goto done;
15948
15949 tg3_full_lock(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +000015950 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015951 err = tg3_restart_hw(tp, 1);
15952 tg3_full_unlock(tp);
15953 if (err) {
15954 netdev_err(netdev, "Cannot restart hardware after reset.\n");
15955 goto done;
15956 }
15957
15958 netif_device_attach(netdev);
15959
15960 tp->timer.expires = jiffies + tp->timer_offset;
15961 add_timer(&tp->timer);
15962
15963 tg3_netif_start(tp);
15964
15965 tg3_phy_start(tp);
15966
15967done:
15968 rtnl_unlock();
15969}
15970
15971static struct pci_error_handlers tg3_err_handler = {
15972 .error_detected = tg3_io_error_detected,
15973 .slot_reset = tg3_io_slot_reset,
15974 .resume = tg3_io_resume
15975};
15976
Linus Torvalds1da177e2005-04-16 15:20:36 -070015977static struct pci_driver tg3_driver = {
15978 .name = DRV_MODULE_NAME,
15979 .id_table = tg3_pci_tbl,
15980 .probe = tg3_init_one,
15981 .remove = __devexit_p(tg3_remove_one),
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015982 .err_handler = &tg3_err_handler,
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015983 .driver.pm = TG3_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070015984};
15985
15986static int __init tg3_init(void)
15987{
Jeff Garzik29917622006-08-19 17:48:59 -040015988 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015989}
15990
15991static void __exit tg3_cleanup(void)
15992{
15993 pci_unregister_driver(&tg3_driver);
15994}
15995
15996module_init(tg3_init);
15997module_exit(tg3_cleanup);