blob: 0da93dbbd0c8a6a91e1aeaa560c181a4f5ddcdff [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 Carlson9e056c02012-02-13 15:20:17 +00007 * Copyright (C) 2005-2012 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
138
139/* Do not place this n-ring entries value into the tp struct itself,
140 * we really want to expose these constants to GCC so that modulo et
141 * al. operations are done with shifts and masks instead of with
142 * hw multiply/modulo instructions. Another solution would be to
143 * replace things like '% foo' with '& (foo - 1)'.
144 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146#define TG3_TX_RING_SIZE 512
147#define TG3_DEF_TX_RING_PENDING (TG3_TX_RING_SIZE - 1)
148
Matt Carlson2c49a442010-09-30 10:34:35 +0000149#define TG3_RX_STD_RING_BYTES(tp) \
150 (sizeof(struct tg3_rx_buffer_desc) * TG3_RX_STD_RING_SIZE(tp))
151#define TG3_RX_JMB_RING_BYTES(tp) \
152 (sizeof(struct tg3_ext_rx_buffer_desc) * TG3_RX_JMB_RING_SIZE(tp))
153#define TG3_RX_RCB_RING_BYTES(tp) \
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000154 (sizeof(struct tg3_rx_buffer_desc) * (tp->rx_ret_ring_mask + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155#define TG3_TX_RING_BYTES (sizeof(struct tg3_tx_buffer_desc) * \
156 TG3_TX_RING_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157#define NEXT_TX(N) (((N) + 1) & (TG3_TX_RING_SIZE - 1))
158
Matt Carlson287be122009-08-28 13:58:46 +0000159#define TG3_DMA_BYTE_ENAB 64
160
161#define TG3_RX_STD_DMA_SZ 1536
162#define TG3_RX_JMB_DMA_SZ 9046
163
164#define TG3_RX_DMA_TO_MAP_SZ(x) ((x) + TG3_DMA_BYTE_ENAB)
165
166#define TG3_RX_STD_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_STD_DMA_SZ)
167#define TG3_RX_JMB_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_JMB_DMA_SZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Matt Carlson2c49a442010-09-30 10:34:35 +0000169#define TG3_RX_STD_BUFF_RING_SIZE(tp) \
170 (sizeof(struct ring_info) * TG3_RX_STD_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000171
Matt Carlson2c49a442010-09-30 10:34:35 +0000172#define TG3_RX_JMB_BUFF_RING_SIZE(tp) \
173 (sizeof(struct ring_info) * TG3_RX_JMB_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000174
Matt Carlsond2757fc2010-04-12 06:58:27 +0000175/* Due to a hardware bug, the 5701 can only DMA to memory addresses
176 * that are at least dword aligned when used in PCIX mode. The driver
177 * works around this bug by double copying the packet. This workaround
178 * is built into the normal double copy length check for efficiency.
179 *
180 * However, the double copy is only necessary on those architectures
181 * where unaligned memory accesses are inefficient. For those architectures
182 * where unaligned memory accesses incur little penalty, we can reintegrate
183 * the 5701 in the normal rx path. Doing so saves a device structure
184 * dereference by hardcoding the double copy threshold in place.
185 */
186#define TG3_RX_COPY_THRESHOLD 256
187#if NET_IP_ALIGN == 0 || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
188 #define TG3_RX_COPY_THRESH(tp) TG3_RX_COPY_THRESHOLD
189#else
190 #define TG3_RX_COPY_THRESH(tp) ((tp)->rx_copy_thresh)
191#endif
192
Matt Carlson81389f52011-08-31 11:44:49 +0000193#if (NET_IP_ALIGN != 0)
194#define TG3_RX_OFFSET(tp) ((tp)->rx_offset)
195#else
Eric Dumazet9205fd92011-11-18 06:47:01 +0000196#define TG3_RX_OFFSET(tp) (NET_SKB_PAD)
Matt Carlson81389f52011-08-31 11:44:49 +0000197#endif
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199/* minimum number of free TX descriptors required to wake up TX process */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000200#define TG3_TX_WAKEUP_THRESH(tnapi) ((tnapi)->tx_pending / 4)
Matt Carlson55086ad2011-12-14 11:09:59 +0000201#define TG3_TX_BD_DMA_MAX_2K 2048
Matt Carlsona4cb4282011-12-14 11:09:58 +0000202#define TG3_TX_BD_DMA_MAX_4K 4096
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Matt Carlsonad829262008-11-21 17:16:16 -0800204#define TG3_RAW_IP_ALIGN 2
205
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000206#define TG3_FW_UPDATE_TIMEOUT_SEC 5
Matt Carlson21f76382012-02-22 12:35:21 +0000207#define TG3_FW_UPDATE_FREQ_SEC (TG3_FW_UPDATE_TIMEOUT_SEC / 2)
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000208
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800209#define FIRMWARE_TG3 "tigon/tg3.bin"
210#define FIRMWARE_TG3TSO "tigon/tg3_tso.bin"
211#define FIRMWARE_TG3TSO5 "tigon/tg3_tso5.bin"
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213static char version[] __devinitdata =
Joe Perches05dbe002010-02-17 19:44:19 +0000214 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216MODULE_AUTHOR("David S. Miller (davem@redhat.com) and Jeff Garzik (jgarzik@pobox.com)");
217MODULE_DESCRIPTION("Broadcom Tigon3 ethernet driver");
218MODULE_LICENSE("GPL");
219MODULE_VERSION(DRV_MODULE_VERSION);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800220MODULE_FIRMWARE(FIRMWARE_TG3);
221MODULE_FIRMWARE(FIRMWARE_TG3TSO);
222MODULE_FIRMWARE(FIRMWARE_TG3TSO5);
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224static int tg3_debug = -1; /* -1 == use TG3_DEF_MSG_ENABLE as value */
225module_param(tg3_debug, int, 0);
226MODULE_PARM_DESC(tg3_debug, "Tigon3 bitmapped debugging message enable value");
227
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000228static DEFINE_PCI_DEVICE_TABLE(tg3_pci_tbl) = {
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700229 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5700)},
230 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5701)},
231 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702)},
232 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703)},
233 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704)},
234 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702FE)},
235 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705)},
236 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705_2)},
237 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M)},
238 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M_2)},
239 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702X)},
240 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703X)},
241 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S)},
242 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702A3)},
243 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703A3)},
244 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5782)},
245 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5788)},
246 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5789)},
247 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901)},
248 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901_2)},
249 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S_2)},
250 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705F)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700251 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5721)},
Michael Chan126a3362006-09-27 16:03:07 -0700252 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5722)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700253 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700254 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751M)},
255 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751F)},
256 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752)},
257 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752M)},
258 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753)},
259 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753M)},
260 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753F)},
261 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754)},
262 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754M)},
263 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755)},
264 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755M)},
Michael Chan126a3362006-09-27 16:03:07 -0700265 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5756)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700266 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5786)},
267 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787)},
268 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787M)},
Michael Chan676917d2006-12-07 00:20:22 -0800269 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787F)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700270 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714)},
271 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714S)},
272 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715)},
273 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715S)},
274 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780)},
275 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780S)},
276 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5781)},
Michael Chanb5d37722006-09-27 16:06:21 -0700277 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906)},
278 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906M)},
Matt Carlsond30cdd22007-10-07 23:28:35 -0700279 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5784)},
280 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5764)},
Matt Carlson6c7af272007-10-21 16:12:02 -0700281 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5723)},
Matt Carlson9936bcf2007-10-10 18:03:07 -0700282 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761)},
283 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761E)},
Matt Carlsonc88e6682008-11-03 16:49:18 -0800284 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761S)},
285 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761SE)},
Matt Carlson2befdce2009-08-28 12:28:45 +0000286 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_G)},
287 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_F)},
Matt Carlson321d32a2008-11-21 17:22:19 -0800288 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780)},
289 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57760)},
290 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57790)},
Matt Carlson5e7ccf22009-08-25 10:08:42 +0000291 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57788)},
Matt Carlson5001e2f2009-11-13 13:03:51 +0000292 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717)},
293 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5718)},
Matt Carlsonb0f75222010-01-20 16:58:11 +0000294 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57781)},
295 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57785)},
296 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57761)},
297 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57765)},
298 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57791)},
299 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57795)},
Matt Carlson302b5002010-06-05 17:24:38 +0000300 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5719)},
Matt Carlsonba1f3c72011-04-05 14:22:50 +0000301 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5720)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700302 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9DXX)},
303 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9MXX)},
304 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1000)},
305 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1001)},
306 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1003)},
307 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC9100)},
308 {PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_TIGON3)},
Meelis Roos1dcb14d2011-05-25 05:43:47 +0000309 {PCI_DEVICE(0x10cf, 0x11a2)}, /* Fujitsu 1000base-SX with BCM5703SKHB */
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700310 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311};
312
313MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
314
Andreas Mohr50da8592006-08-14 23:54:30 -0700315static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000317} ethtool_stats_keys[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 { "rx_octets" },
319 { "rx_fragments" },
320 { "rx_ucast_packets" },
321 { "rx_mcast_packets" },
322 { "rx_bcast_packets" },
323 { "rx_fcs_errors" },
324 { "rx_align_errors" },
325 { "rx_xon_pause_rcvd" },
326 { "rx_xoff_pause_rcvd" },
327 { "rx_mac_ctrl_rcvd" },
328 { "rx_xoff_entered" },
329 { "rx_frame_too_long_errors" },
330 { "rx_jabbers" },
331 { "rx_undersize_packets" },
332 { "rx_in_length_errors" },
333 { "rx_out_length_errors" },
334 { "rx_64_or_less_octet_packets" },
335 { "rx_65_to_127_octet_packets" },
336 { "rx_128_to_255_octet_packets" },
337 { "rx_256_to_511_octet_packets" },
338 { "rx_512_to_1023_octet_packets" },
339 { "rx_1024_to_1522_octet_packets" },
340 { "rx_1523_to_2047_octet_packets" },
341 { "rx_2048_to_4095_octet_packets" },
342 { "rx_4096_to_8191_octet_packets" },
343 { "rx_8192_to_9022_octet_packets" },
344
345 { "tx_octets" },
346 { "tx_collisions" },
347
348 { "tx_xon_sent" },
349 { "tx_xoff_sent" },
350 { "tx_flow_control" },
351 { "tx_mac_errors" },
352 { "tx_single_collisions" },
353 { "tx_mult_collisions" },
354 { "tx_deferred" },
355 { "tx_excessive_collisions" },
356 { "tx_late_collisions" },
357 { "tx_collide_2times" },
358 { "tx_collide_3times" },
359 { "tx_collide_4times" },
360 { "tx_collide_5times" },
361 { "tx_collide_6times" },
362 { "tx_collide_7times" },
363 { "tx_collide_8times" },
364 { "tx_collide_9times" },
365 { "tx_collide_10times" },
366 { "tx_collide_11times" },
367 { "tx_collide_12times" },
368 { "tx_collide_13times" },
369 { "tx_collide_14times" },
370 { "tx_collide_15times" },
371 { "tx_ucast_packets" },
372 { "tx_mcast_packets" },
373 { "tx_bcast_packets" },
374 { "tx_carrier_sense_errors" },
375 { "tx_discards" },
376 { "tx_errors" },
377
378 { "dma_writeq_full" },
379 { "dma_write_prioq_full" },
380 { "rxbds_empty" },
381 { "rx_discards" },
382 { "rx_errors" },
383 { "rx_threshold_hit" },
384
385 { "dma_readq_full" },
386 { "dma_read_prioq_full" },
387 { "tx_comp_queue_full" },
388
389 { "ring_set_send_prod_index" },
390 { "ring_status_update" },
391 { "nic_irqs" },
392 { "nic_avoided_irqs" },
Matt Carlson4452d092011-05-19 12:12:51 +0000393 { "nic_tx_threshold_hit" },
394
395 { "mbuf_lwm_thresh_hit" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396};
397
Matt Carlson48fa55a2011-04-13 11:05:06 +0000398#define TG3_NUM_STATS ARRAY_SIZE(ethtool_stats_keys)
399
400
Andreas Mohr50da8592006-08-14 23:54:30 -0700401static const struct {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700402 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000403} ethtool_test_keys[] = {
Matt Carlson28a45952011-08-19 13:58:22 +0000404 { "nvram test (online) " },
405 { "link test (online) " },
406 { "register test (offline)" },
407 { "memory test (offline)" },
408 { "mac loopback test (offline)" },
409 { "phy loopback test (offline)" },
Matt Carlson941ec902011-08-19 13:58:23 +0000410 { "ext loopback test (offline)" },
Matt Carlson28a45952011-08-19 13:58:22 +0000411 { "interrupt test (offline)" },
Michael Chan4cafd3f2005-05-29 14:56:34 -0700412};
413
Matt Carlson48fa55a2011-04-13 11:05:06 +0000414#define TG3_NUM_TEST ARRAY_SIZE(ethtool_test_keys)
415
416
Michael Chanb401e9e2005-12-19 16:27:04 -0800417static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
418{
419 writel(val, tp->regs + off);
420}
421
422static u32 tg3_read32(struct tg3 *tp, u32 off)
423{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000424 return readl(tp->regs + off);
Michael Chanb401e9e2005-12-19 16:27:04 -0800425}
426
Matt Carlson0d3031d2007-10-10 18:02:43 -0700427static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
428{
429 writel(val, tp->aperegs + off);
430}
431
432static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
433{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000434 return readl(tp->aperegs + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700435}
436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
438{
Michael Chan68929142005-08-09 20:17:14 -0700439 unsigned long flags;
440
441 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700442 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
443 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
Michael Chan68929142005-08-09 20:17:14 -0700444 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700445}
446
447static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
448{
449 writel(val, tp->regs + off);
450 readl(tp->regs + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451}
452
Michael Chan68929142005-08-09 20:17:14 -0700453static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
454{
455 unsigned long flags;
456 u32 val;
457
458 spin_lock_irqsave(&tp->indirect_lock, flags);
459 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
460 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
461 spin_unlock_irqrestore(&tp->indirect_lock, flags);
462 return val;
463}
464
465static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
466{
467 unsigned long flags;
468
469 if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
470 pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
471 TG3_64BIT_REG_LOW, val);
472 return;
473 }
Matt Carlson66711e62009-11-13 13:03:49 +0000474 if (off == TG3_RX_STD_PROD_IDX_REG) {
Michael Chan68929142005-08-09 20:17:14 -0700475 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
476 TG3_64BIT_REG_LOW, val);
477 return;
478 }
479
480 spin_lock_irqsave(&tp->indirect_lock, flags);
481 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
482 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
483 spin_unlock_irqrestore(&tp->indirect_lock, flags);
484
485 /* In indirect mode when disabling interrupts, we also need
486 * to clear the interrupt bit in the GRC local ctrl register.
487 */
488 if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
489 (val == 0x1)) {
490 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
491 tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
492 }
493}
494
495static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
496{
497 unsigned long flags;
498 u32 val;
499
500 spin_lock_irqsave(&tp->indirect_lock, flags);
501 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
502 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
503 spin_unlock_irqrestore(&tp->indirect_lock, flags);
504 return val;
505}
506
Michael Chanb401e9e2005-12-19 16:27:04 -0800507/* usec_wait specifies the wait time in usec when writing to certain registers
508 * where it is unsafe to read back the register without some delay.
509 * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
510 * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
511 */
512static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
Joe Perches63c3a662011-04-26 08:12:10 +0000514 if (tg3_flag(tp, PCIX_TARGET_HWBUG) || tg3_flag(tp, ICH_WORKAROUND))
Michael Chanb401e9e2005-12-19 16:27:04 -0800515 /* Non-posted methods */
516 tp->write32(tp, off, val);
517 else {
518 /* Posted method */
519 tg3_write32(tp, off, val);
520 if (usec_wait)
521 udelay(usec_wait);
522 tp->read32(tp, off);
523 }
524 /* Wait again after the read for the posted method to guarantee that
525 * the wait time is met.
526 */
527 if (usec_wait)
528 udelay(usec_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529}
530
Michael Chan09ee9292005-08-09 20:17:00 -0700531static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
532{
533 tp->write32_mbox(tp, off, val);
Joe Perches63c3a662011-04-26 08:12:10 +0000534 if (!tg3_flag(tp, MBOX_WRITE_REORDER) && !tg3_flag(tp, ICH_WORKAROUND))
Michael Chan68929142005-08-09 20:17:14 -0700535 tp->read32_mbox(tp, off);
Michael Chan09ee9292005-08-09 20:17:00 -0700536}
537
Michael Chan20094932005-08-09 20:16:32 -0700538static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
540 void __iomem *mbox = tp->regs + off;
541 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000542 if (tg3_flag(tp, TXD_MBOX_HWBUG))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000544 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 readl(mbox);
546}
547
Michael Chanb5d37722006-09-27 16:06:21 -0700548static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
549{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000550 return readl(tp->regs + off + GRCMBOX_BASE);
Michael Chanb5d37722006-09-27 16:06:21 -0700551}
552
553static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
554{
555 writel(val, tp->regs + off + GRCMBOX_BASE);
556}
557
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000558#define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700559#define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000560#define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
561#define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
562#define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
Michael Chan20094932005-08-09 20:16:32 -0700563
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000564#define tw32(reg, val) tp->write32(tp, reg, val)
565#define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
566#define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
567#define tr32(reg) tp->read32(tp, reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
570{
Michael Chan68929142005-08-09 20:17:14 -0700571 unsigned long flags;
572
Matt Carlson6ff6f812011-05-19 12:12:54 +0000573 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700574 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
575 return;
576
Michael Chan68929142005-08-09 20:17:14 -0700577 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000578 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700579 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
580 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Michael Chanbbadf502006-04-06 21:46:34 -0700582 /* Always leave this as zero. */
583 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
584 } else {
585 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
586 tw32_f(TG3PCI_MEM_WIN_DATA, val);
587
588 /* Always leave this as zero. */
589 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
590 }
Michael Chan68929142005-08-09 20:17:14 -0700591 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592}
593
594static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
595{
Michael Chan68929142005-08-09 20:17:14 -0700596 unsigned long flags;
597
Matt Carlson6ff6f812011-05-19 12:12:54 +0000598 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700599 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
600 *val = 0;
601 return;
602 }
603
Michael Chan68929142005-08-09 20:17:14 -0700604 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000605 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700606 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
607 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Michael Chanbbadf502006-04-06 21:46:34 -0700609 /* Always leave this as zero. */
610 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
611 } else {
612 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
613 *val = tr32(TG3PCI_MEM_WIN_DATA);
614
615 /* Always leave this as zero. */
616 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
617 }
Michael Chan68929142005-08-09 20:17:14 -0700618 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619}
620
Matt Carlson0d3031d2007-10-10 18:02:43 -0700621static void tg3_ape_lock_init(struct tg3 *tp)
622{
623 int i;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000624 u32 regbase, bit;
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000625
626 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
627 regbase = TG3_APE_LOCK_GRANT;
628 else
629 regbase = TG3_APE_PER_LOCK_GRANT;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700630
631 /* Make sure the driver hasn't any stale locks. */
Matt Carlson78f94dc2011-11-04 09:14:58 +0000632 for (i = TG3_APE_LOCK_PHY0; i <= TG3_APE_LOCK_GPIO; i++) {
633 switch (i) {
634 case TG3_APE_LOCK_PHY0:
635 case TG3_APE_LOCK_PHY1:
636 case TG3_APE_LOCK_PHY2:
637 case TG3_APE_LOCK_PHY3:
638 bit = APE_LOCK_GRANT_DRIVER;
639 break;
640 default:
641 if (!tp->pci_fn)
642 bit = APE_LOCK_GRANT_DRIVER;
643 else
644 bit = 1 << tp->pci_fn;
645 }
646 tg3_ape_write32(tp, regbase + 4 * i, bit);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000647 }
648
Matt Carlson0d3031d2007-10-10 18:02:43 -0700649}
650
651static int tg3_ape_lock(struct tg3 *tp, int locknum)
652{
653 int i, off;
654 int ret = 0;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000655 u32 status, req, gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700656
Joe Perches63c3a662011-04-26 08:12:10 +0000657 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700658 return 0;
659
660 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000661 case TG3_APE_LOCK_GPIO:
662 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
663 return 0;
Matt Carlson33f401a2010-04-05 10:19:27 +0000664 case TG3_APE_LOCK_GRC:
665 case TG3_APE_LOCK_MEM:
Matt Carlson78f94dc2011-11-04 09:14:58 +0000666 if (!tp->pci_fn)
667 bit = APE_LOCK_REQ_DRIVER;
668 else
669 bit = 1 << tp->pci_fn;
Matt Carlson33f401a2010-04-05 10:19:27 +0000670 break;
671 default:
672 return -EINVAL;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700673 }
674
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000675 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
676 req = TG3_APE_LOCK_REQ;
677 gnt = TG3_APE_LOCK_GRANT;
678 } else {
679 req = TG3_APE_PER_LOCK_REQ;
680 gnt = TG3_APE_PER_LOCK_GRANT;
681 }
682
Matt Carlson0d3031d2007-10-10 18:02:43 -0700683 off = 4 * locknum;
684
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000685 tg3_ape_write32(tp, req + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700686
687 /* Wait for up to 1 millisecond to acquire lock. */
688 for (i = 0; i < 100; i++) {
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000689 status = tg3_ape_read32(tp, gnt + off);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000690 if (status == bit)
Matt Carlson0d3031d2007-10-10 18:02:43 -0700691 break;
692 udelay(10);
693 }
694
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000695 if (status != bit) {
Matt Carlson0d3031d2007-10-10 18:02:43 -0700696 /* Revoke the lock request. */
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000697 tg3_ape_write32(tp, gnt + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700698 ret = -EBUSY;
699 }
700
701 return ret;
702}
703
704static void tg3_ape_unlock(struct tg3 *tp, int locknum)
705{
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000706 u32 gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700707
Joe Perches63c3a662011-04-26 08:12:10 +0000708 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700709 return;
710
711 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000712 case TG3_APE_LOCK_GPIO:
713 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
714 return;
Matt Carlson33f401a2010-04-05 10:19:27 +0000715 case TG3_APE_LOCK_GRC:
716 case TG3_APE_LOCK_MEM:
Matt Carlson78f94dc2011-11-04 09:14:58 +0000717 if (!tp->pci_fn)
718 bit = APE_LOCK_GRANT_DRIVER;
719 else
720 bit = 1 << tp->pci_fn;
Matt Carlson33f401a2010-04-05 10:19:27 +0000721 break;
722 default:
723 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700724 }
725
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000726 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
727 gnt = TG3_APE_LOCK_GRANT;
728 else
729 gnt = TG3_APE_PER_LOCK_GRANT;
730
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000731 tg3_ape_write32(tp, gnt + 4 * locknum, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700732}
733
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000734static void tg3_ape_send_event(struct tg3 *tp, u32 event)
735{
736 int i;
737 u32 apedata;
738
739 /* NCSI does not support APE events */
740 if (tg3_flag(tp, APE_HAS_NCSI))
741 return;
742
743 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
744 if (apedata != APE_SEG_SIG_MAGIC)
745 return;
746
747 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
748 if (!(apedata & APE_FW_STATUS_READY))
749 return;
750
751 /* Wait for up to 1 millisecond for APE to service previous event. */
752 for (i = 0; i < 10; i++) {
753 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
754 return;
755
756 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
757
758 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
759 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
760 event | APE_EVENT_STATUS_EVENT_PENDING);
761
762 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
763
764 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
765 break;
766
767 udelay(100);
768 }
769
770 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
771 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
772}
773
774static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
775{
776 u32 event;
777 u32 apedata;
778
779 if (!tg3_flag(tp, ENABLE_APE))
780 return;
781
782 switch (kind) {
783 case RESET_KIND_INIT:
784 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
785 APE_HOST_SEG_SIG_MAGIC);
786 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
787 APE_HOST_SEG_LEN_MAGIC);
788 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
789 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
790 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
791 APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
792 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
793 APE_HOST_BEHAV_NO_PHYLOCK);
794 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
795 TG3_APE_HOST_DRVR_STATE_START);
796
797 event = APE_EVENT_STATUS_STATE_START;
798 break;
799 case RESET_KIND_SHUTDOWN:
800 /* With the interface we are currently using,
801 * APE does not track driver state. Wiping
802 * out the HOST SEGMENT SIGNATURE forces
803 * the APE to assume OS absent status.
804 */
805 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
806
807 if (device_may_wakeup(&tp->pdev->dev) &&
808 tg3_flag(tp, WOL_ENABLE)) {
809 tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
810 TG3_APE_HOST_WOL_SPEED_AUTO);
811 apedata = TG3_APE_HOST_DRVR_STATE_WOL;
812 } else
813 apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
814
815 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
816
817 event = APE_EVENT_STATUS_STATE_UNLOAD;
818 break;
819 case RESET_KIND_SUSPEND:
820 event = APE_EVENT_STATUS_STATE_SUSPEND;
821 break;
822 default:
823 return;
824 }
825
826 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
827
828 tg3_ape_send_event(tp, event);
829}
830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831static void tg3_disable_ints(struct tg3 *tp)
832{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000833 int i;
834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 tw32(TG3PCI_MISC_HOST_CTRL,
836 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000837 for (i = 0; i < tp->irq_max; i++)
838 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839}
840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841static void tg3_enable_ints(struct tg3 *tp)
842{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000843 int i;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000844
Michael Chanbbe832c2005-06-24 20:20:04 -0700845 tp->irq_sync = 0;
846 wmb();
847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 tw32(TG3PCI_MISC_HOST_CTRL,
849 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000850
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000851 tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000852 for (i = 0; i < tp->irq_cnt; i++) {
853 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000854
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000855 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
Joe Perches63c3a662011-04-26 08:12:10 +0000856 if (tg3_flag(tp, 1SHOT_MSI))
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000857 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
858
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000859 tp->coal_now |= tnapi->coal_now;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000860 }
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000861
862 /* Force an initial interrupt */
Joe Perches63c3a662011-04-26 08:12:10 +0000863 if (!tg3_flag(tp, TAGGED_STATUS) &&
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000864 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
865 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
866 else
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000867 tw32(HOSTCC_MODE, tp->coal_now);
868
869 tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870}
871
Matt Carlson17375d22009-08-28 14:02:18 +0000872static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
Michael Chan04237dd2005-04-25 15:17:17 -0700873{
Matt Carlson17375d22009-08-28 14:02:18 +0000874 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +0000875 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan04237dd2005-04-25 15:17:17 -0700876 unsigned int work_exists = 0;
877
878 /* check for phy events */
Joe Perches63c3a662011-04-26 08:12:10 +0000879 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Michael Chan04237dd2005-04-25 15:17:17 -0700880 if (sblk->status & SD_STATUS_LINK_CHG)
881 work_exists = 1;
882 }
883 /* check for RX/TX work to do */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000884 if (sblk->idx[0].tx_consumer != tnapi->tx_cons ||
Matt Carlson8d9d7cf2009-09-01 13:19:05 +0000885 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -0700886 work_exists = 1;
887
888 return work_exists;
889}
890
Matt Carlson17375d22009-08-28 14:02:18 +0000891/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -0700892 * similar to tg3_enable_ints, but it accurately determines whether there
893 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400894 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 */
Matt Carlson17375d22009-08-28 14:02:18 +0000896static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
Matt Carlson17375d22009-08-28 14:02:18 +0000898 struct tg3 *tp = tnapi->tp;
899
Matt Carlson898a56f2009-08-28 14:02:40 +0000900 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 mmiowb();
902
David S. Millerfac9b832005-05-18 22:46:34 -0700903 /* When doing tagged status, this work check is unnecessary.
904 * The last_tag we write above tells the chip which piece of
905 * work we've completed.
906 */
Joe Perches63c3a662011-04-26 08:12:10 +0000907 if (!tg3_flag(tp, TAGGED_STATUS) && tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -0700908 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +0000909 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910}
911
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912static void tg3_switch_clocks(struct tg3 *tp)
913{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000914 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 u32 orig_clock_ctrl;
916
Joe Perches63c3a662011-04-26 08:12:10 +0000917 if (tg3_flag(tp, CPMU_PRESENT) || tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -0700918 return;
919
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000920 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
921
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 orig_clock_ctrl = clock_ctrl;
923 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
924 CLOCK_CTRL_CLKRUN_OENABLE |
925 0x1f);
926 tp->pci_clock_ctrl = clock_ctrl;
927
Joe Perches63c3a662011-04-26 08:12:10 +0000928 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800930 tw32_wait_f(TG3PCI_CLOCK_CTRL,
931 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 }
933 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800934 tw32_wait_f(TG3PCI_CLOCK_CTRL,
935 clock_ctrl |
936 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
937 40);
938 tw32_wait_f(TG3PCI_CLOCK_CTRL,
939 clock_ctrl | (CLOCK_CTRL_ALTCLK),
940 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 }
Michael Chanb401e9e2005-12-19 16:27:04 -0800942 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943}
944
945#define PHY_BUSY_LOOPS 5000
946
947static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
948{
949 u32 frame_val;
950 unsigned int loops;
951 int ret;
952
953 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
954 tw32_f(MAC_MI_MODE,
955 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
956 udelay(80);
957 }
958
959 *val = 0x0;
960
Matt Carlson882e9792009-09-01 13:21:36 +0000961 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 MI_COM_PHY_ADDR_MASK);
963 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
964 MI_COM_REG_ADDR_MASK);
965 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 tw32_f(MAC_MI_COM, frame_val);
968
969 loops = PHY_BUSY_LOOPS;
970 while (loops != 0) {
971 udelay(10);
972 frame_val = tr32(MAC_MI_COM);
973
974 if ((frame_val & MI_COM_BUSY) == 0) {
975 udelay(5);
976 frame_val = tr32(MAC_MI_COM);
977 break;
978 }
979 loops -= 1;
980 }
981
982 ret = -EBUSY;
983 if (loops != 0) {
984 *val = frame_val & MI_COM_DATA_MASK;
985 ret = 0;
986 }
987
988 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
989 tw32_f(MAC_MI_MODE, tp->mi_mode);
990 udelay(80);
991 }
992
993 return ret;
994}
995
996static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
997{
998 u32 frame_val;
999 unsigned int loops;
1000 int ret;
1001
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001002 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson221c5632011-06-13 13:39:01 +00001003 (reg == MII_CTRL1000 || reg == MII_TG3_AUX_CTRL))
Michael Chanb5d37722006-09-27 16:06:21 -07001004 return 0;
1005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1007 tw32_f(MAC_MI_MODE,
1008 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
1009 udelay(80);
1010 }
1011
Matt Carlson882e9792009-09-01 13:21:36 +00001012 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 MI_COM_PHY_ADDR_MASK);
1014 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
1015 MI_COM_REG_ADDR_MASK);
1016 frame_val |= (val & MI_COM_DATA_MASK);
1017 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 tw32_f(MAC_MI_COM, frame_val);
1020
1021 loops = PHY_BUSY_LOOPS;
1022 while (loops != 0) {
1023 udelay(10);
1024 frame_val = tr32(MAC_MI_COM);
1025 if ((frame_val & MI_COM_BUSY) == 0) {
1026 udelay(5);
1027 frame_val = tr32(MAC_MI_COM);
1028 break;
1029 }
1030 loops -= 1;
1031 }
1032
1033 ret = -EBUSY;
1034 if (loops != 0)
1035 ret = 0;
1036
1037 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1038 tw32_f(MAC_MI_MODE, tp->mi_mode);
1039 udelay(80);
1040 }
1041
1042 return ret;
1043}
1044
Matt Carlsonb0988c12011-04-20 07:57:39 +00001045static int tg3_phy_cl45_write(struct tg3 *tp, u32 devad, u32 addr, u32 val)
1046{
1047 int err;
1048
1049 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1050 if (err)
1051 goto done;
1052
1053 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1054 if (err)
1055 goto done;
1056
1057 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1058 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1059 if (err)
1060 goto done;
1061
1062 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, val);
1063
1064done:
1065 return err;
1066}
1067
1068static int tg3_phy_cl45_read(struct tg3 *tp, u32 devad, u32 addr, u32 *val)
1069{
1070 int err;
1071
1072 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1073 if (err)
1074 goto done;
1075
1076 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1077 if (err)
1078 goto done;
1079
1080 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1081 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1082 if (err)
1083 goto done;
1084
1085 err = tg3_readphy(tp, MII_TG3_MMD_ADDRESS, val);
1086
1087done:
1088 return err;
1089}
1090
1091static int tg3_phydsp_read(struct tg3 *tp, u32 reg, u32 *val)
1092{
1093 int err;
1094
1095 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1096 if (!err)
1097 err = tg3_readphy(tp, MII_TG3_DSP_RW_PORT, val);
1098
1099 return err;
1100}
1101
1102static int tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
1103{
1104 int err;
1105
1106 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1107 if (!err)
1108 err = tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
1109
1110 return err;
1111}
1112
Matt Carlson15ee95c2011-04-20 07:57:40 +00001113static int tg3_phy_auxctl_read(struct tg3 *tp, int reg, u32 *val)
1114{
1115 int err;
1116
1117 err = tg3_writephy(tp, MII_TG3_AUX_CTRL,
1118 (reg << MII_TG3_AUXCTL_MISC_RDSEL_SHIFT) |
1119 MII_TG3_AUXCTL_SHDWSEL_MISC);
1120 if (!err)
1121 err = tg3_readphy(tp, MII_TG3_AUX_CTRL, val);
1122
1123 return err;
1124}
1125
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001126static int tg3_phy_auxctl_write(struct tg3 *tp, int reg, u32 set)
1127{
1128 if (reg == MII_TG3_AUXCTL_SHDWSEL_MISC)
1129 set |= MII_TG3_AUXCTL_MISC_WREN;
1130
1131 return tg3_writephy(tp, MII_TG3_AUX_CTRL, set | reg);
1132}
1133
Matt Carlson1d36ba42011-04-20 07:57:42 +00001134#define TG3_PHY_AUXCTL_SMDSP_ENABLE(tp) \
1135 tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
1136 MII_TG3_AUXCTL_ACTL_SMDSP_ENA | \
1137 MII_TG3_AUXCTL_ACTL_TX_6DB)
1138
1139#define TG3_PHY_AUXCTL_SMDSP_DISABLE(tp) \
1140 tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
1141 MII_TG3_AUXCTL_ACTL_TX_6DB);
1142
Matt Carlson95e28692008-05-25 23:44:14 -07001143static int tg3_bmcr_reset(struct tg3 *tp)
1144{
1145 u32 phy_control;
1146 int limit, err;
1147
1148 /* OK, reset it, and poll the BMCR_RESET bit until it
1149 * clears or we time out.
1150 */
1151 phy_control = BMCR_RESET;
1152 err = tg3_writephy(tp, MII_BMCR, phy_control);
1153 if (err != 0)
1154 return -EBUSY;
1155
1156 limit = 5000;
1157 while (limit--) {
1158 err = tg3_readphy(tp, MII_BMCR, &phy_control);
1159 if (err != 0)
1160 return -EBUSY;
1161
1162 if ((phy_control & BMCR_RESET) == 0) {
1163 udelay(40);
1164 break;
1165 }
1166 udelay(10);
1167 }
Roel Kluind4675b52009-02-12 16:33:27 -08001168 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -07001169 return -EBUSY;
1170
1171 return 0;
1172}
1173
Matt Carlson158d7ab2008-05-29 01:37:54 -07001174static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
1175{
Francois Romieu3d165432009-01-19 16:56:50 -08001176 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001177 u32 val;
1178
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001179 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001180
1181 if (tg3_readphy(tp, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001182 val = -EIO;
1183
1184 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001185
1186 return val;
1187}
1188
1189static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
1190{
Francois Romieu3d165432009-01-19 16:56:50 -08001191 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001192 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001193
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001194 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001195
1196 if (tg3_writephy(tp, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001197 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001198
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001199 spin_unlock_bh(&tp->lock);
1200
1201 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001202}
1203
1204static int tg3_mdio_reset(struct mii_bus *bp)
1205{
1206 return 0;
1207}
1208
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001209static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -07001210{
1211 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001212 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -07001213
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001214 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001215 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001216 case PHY_ID_BCM50610:
1217 case PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001218 val = MAC_PHYCFG2_50610_LED_MODES;
1219 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001220 case PHY_ID_BCMAC131:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001221 val = MAC_PHYCFG2_AC131_LED_MODES;
1222 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001223 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001224 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
1225 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001226 case PHY_ID_RTL8201E:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001227 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
1228 break;
1229 default:
Matt Carlsona9daf362008-05-25 23:49:44 -07001230 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001231 }
1232
1233 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
1234 tw32(MAC_PHYCFG2, val);
1235
1236 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001237 val &= ~(MAC_PHYCFG1_RGMII_INT |
1238 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
1239 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001240 tw32(MAC_PHYCFG1, val);
1241
1242 return;
1243 }
1244
Joe Perches63c3a662011-04-26 08:12:10 +00001245 if (!tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001246 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
1247 MAC_PHYCFG2_FMODE_MASK_MASK |
1248 MAC_PHYCFG2_GMODE_MASK_MASK |
1249 MAC_PHYCFG2_ACT_MASK_MASK |
1250 MAC_PHYCFG2_QUAL_MASK_MASK |
1251 MAC_PHYCFG2_INBAND_ENABLE;
1252
1253 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001254
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001255 val = tr32(MAC_PHYCFG1);
1256 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
1257 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
Joe Perches63c3a662011-04-26 08:12:10 +00001258 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1259 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001260 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
Joe Perches63c3a662011-04-26 08:12:10 +00001261 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001262 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1263 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001264 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1265 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1266 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001267
Matt Carlsona9daf362008-05-25 23:49:44 -07001268 val = tr32(MAC_EXT_RGMII_MODE);
1269 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1270 MAC_RGMII_MODE_RX_QUALITY |
1271 MAC_RGMII_MODE_RX_ACTIVITY |
1272 MAC_RGMII_MODE_RX_ENG_DET |
1273 MAC_RGMII_MODE_TX_ENABLE |
1274 MAC_RGMII_MODE_TX_LOWPWR |
1275 MAC_RGMII_MODE_TX_RESET);
Joe Perches63c3a662011-04-26 08:12:10 +00001276 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1277 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001278 val |= MAC_RGMII_MODE_RX_INT_B |
1279 MAC_RGMII_MODE_RX_QUALITY |
1280 MAC_RGMII_MODE_RX_ACTIVITY |
1281 MAC_RGMII_MODE_RX_ENG_DET;
Joe Perches63c3a662011-04-26 08:12:10 +00001282 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001283 val |= MAC_RGMII_MODE_TX_ENABLE |
1284 MAC_RGMII_MODE_TX_LOWPWR |
1285 MAC_RGMII_MODE_TX_RESET;
1286 }
1287 tw32(MAC_EXT_RGMII_MODE, val);
1288}
1289
Matt Carlson158d7ab2008-05-29 01:37:54 -07001290static void tg3_mdio_start(struct tg3 *tp)
1291{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001292 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1293 tw32_f(MAC_MI_MODE, tp->mi_mode);
1294 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001295
Joe Perches63c3a662011-04-26 08:12:10 +00001296 if (tg3_flag(tp, MDIOBUS_INITED) &&
Matt Carlson9ea48182010-02-17 15:17:01 +00001297 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1298 tg3_mdio_config_5785(tp);
1299}
1300
1301static int tg3_mdio_init(struct tg3 *tp)
1302{
1303 int i;
1304 u32 reg;
1305 struct phy_device *phydev;
1306
Joe Perches63c3a662011-04-26 08:12:10 +00001307 if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson9c7df912010-06-05 17:24:36 +00001308 u32 is_serdes;
Matt Carlson882e9792009-09-01 13:21:36 +00001309
Matt Carlson69f11c92011-07-13 09:27:30 +00001310 tp->phy_addr = tp->pci_fn + 1;
Matt Carlson882e9792009-09-01 13:21:36 +00001311
Matt Carlsond1ec96a2010-01-12 10:11:38 +00001312 if (tp->pci_chip_rev_id != CHIPREV_ID_5717_A0)
1313 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1314 else
1315 is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1316 TG3_CPMU_PHY_STRAP_IS_SERDES;
Matt Carlson882e9792009-09-01 13:21:36 +00001317 if (is_serdes)
1318 tp->phy_addr += 7;
1319 } else
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001320 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001321
Matt Carlson158d7ab2008-05-29 01:37:54 -07001322 tg3_mdio_start(tp);
1323
Joe Perches63c3a662011-04-26 08:12:10 +00001324 if (!tg3_flag(tp, USE_PHYLIB) || tg3_flag(tp, MDIOBUS_INITED))
Matt Carlson158d7ab2008-05-29 01:37:54 -07001325 return 0;
1326
Lennert Buytenhek298cf9be2008-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 Buytenhek298cf9be2008-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 Buytenhek298cf9be2008-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 Buytenhek298cf9be2008-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 Buytenhek298cf9be2008-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 Buytenhek298cf9be2008-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 Buytenhek298cf9be2008-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. */
Matt Carlsonb28f3892012-02-13 15:20:12 +00001457static void tg3_phy_gather_ump_data(struct tg3 *tp, u32 *data)
Matt Carlson95e28692008-05-25 23:44:14 -07001458{
Matt Carlsonb28f3892012-02-13 15:20:12 +00001459 u32 reg, val;
Matt Carlson95e28692008-05-25 23:44:14 -07001460
1461 val = 0;
1462 if (!tg3_readphy(tp, MII_BMCR, &reg))
1463 val = reg << 16;
1464 if (!tg3_readphy(tp, MII_BMSR, &reg))
1465 val |= (reg & 0xffff);
Matt Carlsonb28f3892012-02-13 15:20:12 +00001466 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001467
1468 val = 0;
1469 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1470 val = reg << 16;
1471 if (!tg3_readphy(tp, MII_LPA, &reg))
1472 val |= (reg & 0xffff);
Matt Carlsonb28f3892012-02-13 15:20:12 +00001473 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001474
1475 val = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001476 if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
Matt Carlson95e28692008-05-25 23:44:14 -07001477 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1478 val = reg << 16;
1479 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1480 val |= (reg & 0xffff);
1481 }
Matt Carlsonb28f3892012-02-13 15:20:12 +00001482 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001483
1484 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1485 val = reg << 16;
1486 else
1487 val = 0;
Matt Carlsonb28f3892012-02-13 15:20:12 +00001488 *data++ = val;
1489}
1490
1491/* tp->lock is held. */
1492static void tg3_ump_link_report(struct tg3 *tp)
1493{
1494 u32 data[4];
1495
1496 if (!tg3_flag(tp, 5780_CLASS) || !tg3_flag(tp, ENABLE_ASF))
1497 return;
1498
1499 tg3_phy_gather_ump_data(tp, data);
1500
1501 tg3_wait_for_event_ack(tp);
1502
1503 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1504 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1505 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x0, data[0]);
1506 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x4, data[1]);
1507 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x8, data[2]);
1508 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0xc, data[3]);
Matt Carlson95e28692008-05-25 23:44:14 -07001509
Matt Carlson4ba526c2008-08-15 14:10:04 -07001510 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001511}
1512
Matt Carlson8d5a89b2011-08-31 11:44:51 +00001513/* tp->lock is held. */
1514static void tg3_stop_fw(struct tg3 *tp)
1515{
1516 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
1517 /* Wait for RX cpu to ACK the previous event. */
1518 tg3_wait_for_event_ack(tp);
1519
1520 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
1521
1522 tg3_generate_fw_event(tp);
1523
1524 /* Wait for RX cpu to ACK this event. */
1525 tg3_wait_for_event_ack(tp);
1526 }
1527}
1528
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001529/* tp->lock is held. */
1530static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
1531{
1532 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
1533 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
1534
1535 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1536 switch (kind) {
1537 case RESET_KIND_INIT:
1538 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1539 DRV_STATE_START);
1540 break;
1541
1542 case RESET_KIND_SHUTDOWN:
1543 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1544 DRV_STATE_UNLOAD);
1545 break;
1546
1547 case RESET_KIND_SUSPEND:
1548 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1549 DRV_STATE_SUSPEND);
1550 break;
1551
1552 default:
1553 break;
1554 }
1555 }
1556
1557 if (kind == RESET_KIND_INIT ||
1558 kind == RESET_KIND_SUSPEND)
1559 tg3_ape_driver_state_change(tp, kind);
1560}
1561
1562/* tp->lock is held. */
1563static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
1564{
1565 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1566 switch (kind) {
1567 case RESET_KIND_INIT:
1568 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1569 DRV_STATE_START_DONE);
1570 break;
1571
1572 case RESET_KIND_SHUTDOWN:
1573 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1574 DRV_STATE_UNLOAD_DONE);
1575 break;
1576
1577 default:
1578 break;
1579 }
1580 }
1581
1582 if (kind == RESET_KIND_SHUTDOWN)
1583 tg3_ape_driver_state_change(tp, kind);
1584}
1585
1586/* tp->lock is held. */
1587static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
1588{
1589 if (tg3_flag(tp, ENABLE_ASF)) {
1590 switch (kind) {
1591 case RESET_KIND_INIT:
1592 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1593 DRV_STATE_START);
1594 break;
1595
1596 case RESET_KIND_SHUTDOWN:
1597 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1598 DRV_STATE_UNLOAD);
1599 break;
1600
1601 case RESET_KIND_SUSPEND:
1602 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1603 DRV_STATE_SUSPEND);
1604 break;
1605
1606 default:
1607 break;
1608 }
1609 }
1610}
1611
1612static int tg3_poll_fw(struct tg3 *tp)
1613{
1614 int i;
1615 u32 val;
1616
1617 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
1618 /* Wait up to 20ms for init done. */
1619 for (i = 0; i < 200; i++) {
1620 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
1621 return 0;
1622 udelay(100);
1623 }
1624 return -ENODEV;
1625 }
1626
1627 /* Wait for firmware initialization to complete. */
1628 for (i = 0; i < 100000; i++) {
1629 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
1630 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
1631 break;
1632 udelay(10);
1633 }
1634
1635 /* Chip might not be fitted with firmware. Some Sun onboard
1636 * parts are configured like that. So don't signal the timeout
1637 * of the above loop as an error, but do report the lack of
1638 * running firmware once.
1639 */
1640 if (i >= 100000 && !tg3_flag(tp, NO_FWARE_REPORTED)) {
1641 tg3_flag_set(tp, NO_FWARE_REPORTED);
1642
1643 netdev_info(tp->dev, "No firmware running\n");
1644 }
1645
1646 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
1647 /* The 57765 A0 needs a little more
1648 * time to do some important work.
1649 */
1650 mdelay(10);
1651 }
1652
1653 return 0;
1654}
1655
Matt Carlson95e28692008-05-25 23:44:14 -07001656static void tg3_link_report(struct tg3 *tp)
1657{
1658 if (!netif_carrier_ok(tp->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001659 netif_info(tp, link, tp->dev, "Link is down\n");
Matt Carlson95e28692008-05-25 23:44:14 -07001660 tg3_ump_link_report(tp);
1661 } else if (netif_msg_link(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001662 netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
1663 (tp->link_config.active_speed == SPEED_1000 ?
1664 1000 :
1665 (tp->link_config.active_speed == SPEED_100 ?
1666 100 : 10)),
1667 (tp->link_config.active_duplex == DUPLEX_FULL ?
1668 "full" : "half"));
Matt Carlson95e28692008-05-25 23:44:14 -07001669
Joe Perches05dbe002010-02-17 19:44:19 +00001670 netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
1671 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
1672 "on" : "off",
1673 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
1674 "on" : "off");
Matt Carlson47007832011-04-20 07:57:43 +00001675
1676 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
1677 netdev_info(tp->dev, "EEE is %s\n",
1678 tp->setlpicnt ? "enabled" : "disabled");
1679
Matt Carlson95e28692008-05-25 23:44:14 -07001680 tg3_ump_link_report(tp);
1681 }
1682}
1683
Matt Carlson95e28692008-05-25 23:44:14 -07001684static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1685{
1686 u16 miireg;
1687
Steve Glendinninge18ce342008-12-16 02:00:00 -08001688 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001689 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001690 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001691 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001692 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001693 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1694 else
1695 miireg = 0;
1696
1697 return miireg;
1698}
1699
Matt Carlson95e28692008-05-25 23:44:14 -07001700static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1701{
1702 u8 cap = 0;
1703
Matt Carlsonf3791cd2011-11-21 15:01:17 +00001704 if (lcladv & rmtadv & ADVERTISE_1000XPAUSE) {
1705 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
1706 } else if (lcladv & rmtadv & ADVERTISE_1000XPSE_ASYM) {
1707 if (lcladv & ADVERTISE_1000XPAUSE)
1708 cap = FLOW_CTRL_RX;
1709 if (rmtadv & ADVERTISE_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001710 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001711 }
1712
1713 return cap;
1714}
1715
Matt Carlsonf51f3562008-05-25 23:45:08 -07001716static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001717{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001718 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001719 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001720 u32 old_rx_mode = tp->rx_mode;
1721 u32 old_tx_mode = tp->tx_mode;
1722
Joe Perches63c3a662011-04-26 08:12:10 +00001723 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001724 autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001725 else
1726 autoneg = tp->link_config.autoneg;
1727
Joe Perches63c3a662011-04-26 08:12:10 +00001728 if (autoneg == AUTONEG_ENABLE && tg3_flag(tp, PAUSE_AUTONEG)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001729 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001730 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001731 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001732 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001733 } else
1734 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001735
Matt Carlsonf51f3562008-05-25 23:45:08 -07001736 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001737
Steve Glendinninge18ce342008-12-16 02:00:00 -08001738 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001739 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1740 else
1741 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1742
Matt Carlsonf51f3562008-05-25 23:45:08 -07001743 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001744 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001745
Steve Glendinninge18ce342008-12-16 02:00:00 -08001746 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001747 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1748 else
1749 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1750
Matt Carlsonf51f3562008-05-25 23:45:08 -07001751 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001752 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001753}
1754
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001755static void tg3_adjust_link(struct net_device *dev)
1756{
1757 u8 oldflowctrl, linkmesg = 0;
1758 u32 mac_mode, lcl_adv, rmt_adv;
1759 struct tg3 *tp = netdev_priv(dev);
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001760 struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001761
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001762 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001763
1764 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
1765 MAC_MODE_HALF_DUPLEX);
1766
1767 oldflowctrl = tp->link_config.active_flowctrl;
1768
1769 if (phydev->link) {
1770 lcl_adv = 0;
1771 rmt_adv = 0;
1772
1773 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
1774 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001775 else if (phydev->speed == SPEED_1000 ||
1776 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001777 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001778 else
1779 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001780
1781 if (phydev->duplex == DUPLEX_HALF)
1782 mac_mode |= MAC_MODE_HALF_DUPLEX;
1783 else {
Matt Carlsonf88788f2011-12-14 11:10:00 +00001784 lcl_adv = mii_advertise_flowctrl(
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001785 tp->link_config.flowctrl);
1786
1787 if (phydev->pause)
1788 rmt_adv = LPA_PAUSE_CAP;
1789 if (phydev->asym_pause)
1790 rmt_adv |= LPA_PAUSE_ASYM;
1791 }
1792
1793 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
1794 } else
1795 mac_mode |= MAC_MODE_PORT_MODE_GMII;
1796
1797 if (mac_mode != tp->mac_mode) {
1798 tp->mac_mode = mac_mode;
1799 tw32_f(MAC_MODE, tp->mac_mode);
1800 udelay(40);
1801 }
1802
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001803 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
1804 if (phydev->speed == SPEED_10)
1805 tw32(MAC_MI_STAT,
1806 MAC_MI_STAT_10MBPS_MODE |
1807 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1808 else
1809 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1810 }
1811
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001812 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
1813 tw32(MAC_TX_LENGTHS,
1814 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1815 (6 << TX_LENGTHS_IPG_SHIFT) |
1816 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
1817 else
1818 tw32(MAC_TX_LENGTHS,
1819 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1820 (6 << TX_LENGTHS_IPG_SHIFT) |
1821 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
1822
Matt Carlson34655ad2012-02-22 12:35:18 +00001823 if (phydev->link != tp->old_link ||
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001824 phydev->speed != tp->link_config.active_speed ||
1825 phydev->duplex != tp->link_config.active_duplex ||
1826 oldflowctrl != tp->link_config.active_flowctrl)
Matt Carlsonc6cdf432010-04-05 10:19:26 +00001827 linkmesg = 1;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001828
Matt Carlson34655ad2012-02-22 12:35:18 +00001829 tp->old_link = phydev->link;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001830 tp->link_config.active_speed = phydev->speed;
1831 tp->link_config.active_duplex = phydev->duplex;
1832
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001833 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001834
1835 if (linkmesg)
1836 tg3_link_report(tp);
1837}
1838
1839static int tg3_phy_init(struct tg3 *tp)
1840{
1841 struct phy_device *phydev;
1842
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001843 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001844 return 0;
1845
1846 /* Bring the PHY back to a known state. */
1847 tg3_bmcr_reset(tp);
1848
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001849 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001850
1851 /* Attach the MAC to the PHY. */
Kay Sieversfb28ad32008-11-10 13:55:14 -08001852 phydev = phy_connect(tp->dev, dev_name(&phydev->dev), tg3_adjust_link,
Matt Carlsona9daf362008-05-25 23:49:44 -07001853 phydev->dev_flags, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001854 if (IS_ERR(phydev)) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001855 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001856 return PTR_ERR(phydev);
1857 }
1858
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001859 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001860 switch (phydev->interface) {
1861 case PHY_INTERFACE_MODE_GMII:
1862 case PHY_INTERFACE_MODE_RGMII:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001863 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Matt Carlson321d32a2008-11-21 17:22:19 -08001864 phydev->supported &= (PHY_GBIT_FEATURES |
1865 SUPPORTED_Pause |
1866 SUPPORTED_Asym_Pause);
1867 break;
1868 }
1869 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001870 case PHY_INTERFACE_MODE_MII:
1871 phydev->supported &= (PHY_BASIC_FEATURES |
1872 SUPPORTED_Pause |
1873 SUPPORTED_Asym_Pause);
1874 break;
1875 default:
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001876 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001877 return -EINVAL;
1878 }
1879
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001880 tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001881
1882 phydev->advertising = phydev->supported;
1883
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001884 return 0;
1885}
1886
1887static void tg3_phy_start(struct tg3 *tp)
1888{
1889 struct phy_device *phydev;
1890
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001891 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001892 return;
1893
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001894 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001895
Matt Carlson800960682010-08-02 11:26:06 +00001896 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
1897 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonc6700ce2012-02-13 15:20:15 +00001898 phydev->speed = tp->link_config.speed;
1899 phydev->duplex = tp->link_config.duplex;
1900 phydev->autoneg = tp->link_config.autoneg;
1901 phydev->advertising = tp->link_config.advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001902 }
1903
1904 phy_start(phydev);
1905
1906 phy_start_aneg(phydev);
1907}
1908
1909static void tg3_phy_stop(struct tg3 *tp)
1910{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001911 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001912 return;
1913
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001914 phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001915}
1916
1917static void tg3_phy_fini(struct tg3 *tp)
1918{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001919 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001920 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001921 tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001922 }
1923}
1924
Matt Carlson941ec902011-08-19 13:58:23 +00001925static int tg3_phy_set_extloopbk(struct tg3 *tp)
1926{
1927 int err;
1928 u32 val;
1929
1930 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
1931 return 0;
1932
1933 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
1934 /* Cannot do read-modify-write on 5401 */
1935 err = tg3_phy_auxctl_write(tp,
1936 MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
1937 MII_TG3_AUXCTL_ACTL_EXTLOOPBK |
1938 0x4c20);
1939 goto done;
1940 }
1941
1942 err = tg3_phy_auxctl_read(tp,
1943 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
1944 if (err)
1945 return err;
1946
1947 val |= MII_TG3_AUXCTL_ACTL_EXTLOOPBK;
1948 err = tg3_phy_auxctl_write(tp,
1949 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, val);
1950
1951done:
1952 return err;
1953}
1954
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001955static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
1956{
1957 u32 phytest;
1958
1959 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
1960 u32 phy;
1961
1962 tg3_writephy(tp, MII_TG3_FET_TEST,
1963 phytest | MII_TG3_FET_SHADOW_EN);
1964 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
1965 if (enable)
1966 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
1967 else
1968 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
1969 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
1970 }
1971 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
1972 }
1973}
1974
Matt Carlson6833c042008-11-21 17:18:59 -08001975static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
1976{
1977 u32 reg;
1978
Joe Perches63c3a662011-04-26 08:12:10 +00001979 if (!tg3_flag(tp, 5705_PLUS) ||
1980 (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001981 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08001982 return;
1983
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001984 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001985 tg3_phy_fet_toggle_apd(tp, enable);
1986 return;
1987 }
1988
Matt Carlson6833c042008-11-21 17:18:59 -08001989 reg = MII_TG3_MISC_SHDW_WREN |
1990 MII_TG3_MISC_SHDW_SCR5_SEL |
1991 MII_TG3_MISC_SHDW_SCR5_LPED |
1992 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
1993 MII_TG3_MISC_SHDW_SCR5_SDTL |
1994 MII_TG3_MISC_SHDW_SCR5_C125OE;
1995 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 || !enable)
1996 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
1997
1998 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1999
2000
2001 reg = MII_TG3_MISC_SHDW_WREN |
2002 MII_TG3_MISC_SHDW_APD_SEL |
2003 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
2004 if (enable)
2005 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
2006
2007 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
2008}
2009
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002010static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
2011{
2012 u32 phy;
2013
Joe Perches63c3a662011-04-26 08:12:10 +00002014 if (!tg3_flag(tp, 5705_PLUS) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002015 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002016 return;
2017
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002018 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002019 u32 ephy;
2020
Matt Carlson535ef6e2009-08-25 10:09:36 +00002021 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
2022 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
2023
2024 tg3_writephy(tp, MII_TG3_FET_TEST,
2025 ephy | MII_TG3_FET_SHADOW_EN);
2026 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002027 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00002028 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002029 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00002030 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
2031 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002032 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00002033 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002034 }
2035 } else {
Matt Carlson15ee95c2011-04-20 07:57:40 +00002036 int ret;
2037
2038 ret = tg3_phy_auxctl_read(tp,
2039 MII_TG3_AUXCTL_SHDWSEL_MISC, &phy);
2040 if (!ret) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002041 if (enable)
2042 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
2043 else
2044 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002045 tg3_phy_auxctl_write(tp,
2046 MII_TG3_AUXCTL_SHDWSEL_MISC, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002047 }
2048 }
2049}
2050
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051static void tg3_phy_set_wirespeed(struct tg3 *tp)
2052{
Matt Carlson15ee95c2011-04-20 07:57:40 +00002053 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 u32 val;
2055
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002056 if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 return;
2058
Matt Carlson15ee95c2011-04-20 07:57:40 +00002059 ret = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_MISC, &val);
2060 if (!ret)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002061 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_MISC,
2062 val | MII_TG3_AUXCTL_MISC_WIRESPD_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063}
2064
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002065static void tg3_phy_apply_otp(struct tg3 *tp)
2066{
2067 u32 otp, phy;
2068
2069 if (!tp->phy_otp)
2070 return;
2071
2072 otp = tp->phy_otp;
2073
Matt Carlson1d36ba42011-04-20 07:57:42 +00002074 if (TG3_PHY_AUXCTL_SMDSP_ENABLE(tp))
2075 return;
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002076
2077 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
2078 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
2079 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
2080
2081 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
2082 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
2083 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
2084
2085 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
2086 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
2087 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
2088
2089 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
2090 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
2091
2092 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
2093 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
2094
2095 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
2096 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
2097 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
2098
Matt Carlson1d36ba42011-04-20 07:57:42 +00002099 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002100}
2101
Matt Carlson52b02d02010-10-14 10:37:41 +00002102static void tg3_phy_eee_adjust(struct tg3 *tp, u32 current_link_up)
2103{
2104 u32 val;
2105
2106 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
2107 return;
2108
2109 tp->setlpicnt = 0;
2110
2111 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
2112 current_link_up == 1 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +00002113 tp->link_config.active_duplex == DUPLEX_FULL &&
2114 (tp->link_config.active_speed == SPEED_100 ||
2115 tp->link_config.active_speed == SPEED_1000)) {
Matt Carlson52b02d02010-10-14 10:37:41 +00002116 u32 eeectl;
2117
2118 if (tp->link_config.active_speed == SPEED_1000)
2119 eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
2120 else
2121 eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
2122
2123 tw32(TG3_CPMU_EEE_CTRL, eeectl);
2124
Matt Carlson3110f5f52010-12-06 08:28:50 +00002125 tg3_phy_cl45_read(tp, MDIO_MMD_AN,
2126 TG3_CL45_D7_EEERES_STAT, &val);
Matt Carlson52b02d02010-10-14 10:37:41 +00002127
Matt Carlsonb0c59432011-05-19 12:12:48 +00002128 if (val == TG3_CL45_D7_EEERES_STAT_LP_1000T ||
2129 val == TG3_CL45_D7_EEERES_STAT_LP_100TX)
Matt Carlson52b02d02010-10-14 10:37:41 +00002130 tp->setlpicnt = 2;
2131 }
2132
2133 if (!tp->setlpicnt) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002134 if (current_link_up == 1 &&
2135 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2136 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0000);
2137 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2138 }
2139
Matt Carlson52b02d02010-10-14 10:37:41 +00002140 val = tr32(TG3_CPMU_EEE_MODE);
2141 tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
2142 }
2143}
2144
Matt Carlsonb0c59432011-05-19 12:12:48 +00002145static void tg3_phy_eee_enable(struct tg3 *tp)
2146{
2147 u32 val;
2148
2149 if (tp->link_config.active_speed == SPEED_1000 &&
2150 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2151 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00002152 tg3_flag(tp, 57765_CLASS)) &&
Matt Carlsonb0c59432011-05-19 12:12:48 +00002153 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002154 val = MII_TG3_DSP_TAP26_ALNOKO |
2155 MII_TG3_DSP_TAP26_RMRXSTO;
2156 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
Matt Carlsonb0c59432011-05-19 12:12:48 +00002157 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2158 }
2159
2160 val = tr32(TG3_CPMU_EEE_MODE);
2161 tw32(TG3_CPMU_EEE_MODE, val | TG3_CPMU_EEEMD_LPI_ENABLE);
2162}
2163
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164static int tg3_wait_macro_done(struct tg3 *tp)
2165{
2166 int limit = 100;
2167
2168 while (limit--) {
2169 u32 tmp32;
2170
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002171 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 if ((tmp32 & 0x1000) == 0)
2173 break;
2174 }
2175 }
Roel Kluind4675b52009-02-12 16:33:27 -08002176 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 return -EBUSY;
2178
2179 return 0;
2180}
2181
2182static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
2183{
2184 static const u32 test_pat[4][6] = {
2185 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
2186 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
2187 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
2188 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
2189 };
2190 int chan;
2191
2192 for (chan = 0; chan < 4; chan++) {
2193 int i;
2194
2195 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2196 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002197 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
2199 for (i = 0; i < 6; i++)
2200 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
2201 test_pat[chan][i]);
2202
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002203 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 if (tg3_wait_macro_done(tp)) {
2205 *resetp = 1;
2206 return -EBUSY;
2207 }
2208
2209 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2210 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002211 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 if (tg3_wait_macro_done(tp)) {
2213 *resetp = 1;
2214 return -EBUSY;
2215 }
2216
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002217 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 if (tg3_wait_macro_done(tp)) {
2219 *resetp = 1;
2220 return -EBUSY;
2221 }
2222
2223 for (i = 0; i < 6; i += 2) {
2224 u32 low, high;
2225
2226 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
2227 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
2228 tg3_wait_macro_done(tp)) {
2229 *resetp = 1;
2230 return -EBUSY;
2231 }
2232 low &= 0x7fff;
2233 high &= 0x000f;
2234 if (low != test_pat[chan][i] ||
2235 high != test_pat[chan][i+1]) {
2236 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
2237 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
2238 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
2239
2240 return -EBUSY;
2241 }
2242 }
2243 }
2244
2245 return 0;
2246}
2247
2248static int tg3_phy_reset_chanpat(struct tg3 *tp)
2249{
2250 int chan;
2251
2252 for (chan = 0; chan < 4; chan++) {
2253 int i;
2254
2255 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2256 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002257 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 for (i = 0; i < 6; i++)
2259 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002260 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 if (tg3_wait_macro_done(tp))
2262 return -EBUSY;
2263 }
2264
2265 return 0;
2266}
2267
2268static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
2269{
2270 u32 reg32, phy9_orig;
2271 int retries, do_phy_reset, err;
2272
2273 retries = 10;
2274 do_phy_reset = 1;
2275 do {
2276 if (do_phy_reset) {
2277 err = tg3_bmcr_reset(tp);
2278 if (err)
2279 return err;
2280 do_phy_reset = 0;
2281 }
2282
2283 /* Disable transmitter and interrupt. */
2284 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
2285 continue;
2286
2287 reg32 |= 0x3000;
2288 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2289
2290 /* Set full-duplex, 1000 mbps. */
2291 tg3_writephy(tp, MII_BMCR,
Matt Carlson221c5632011-06-13 13:39:01 +00002292 BMCR_FULLDPLX | BMCR_SPEED1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
2294 /* Set to master mode. */
Matt Carlson221c5632011-06-13 13:39:01 +00002295 if (tg3_readphy(tp, MII_CTRL1000, &phy9_orig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 continue;
2297
Matt Carlson221c5632011-06-13 13:39:01 +00002298 tg3_writephy(tp, MII_CTRL1000,
2299 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300
Matt Carlson1d36ba42011-04-20 07:57:42 +00002301 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
2302 if (err)
2303 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
2305 /* Block the PHY control access. */
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002306 tg3_phydsp_write(tp, 0x8005, 0x0800);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
2308 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
2309 if (!err)
2310 break;
2311 } while (--retries);
2312
2313 err = tg3_phy_reset_chanpat(tp);
2314 if (err)
2315 return err;
2316
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002317 tg3_phydsp_write(tp, 0x8005, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318
2319 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002320 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
Matt Carlson1d36ba42011-04-20 07:57:42 +00002322 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
Matt Carlson221c5632011-06-13 13:39:01 +00002324 tg3_writephy(tp, MII_CTRL1000, phy9_orig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325
2326 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
2327 reg32 &= ~0x3000;
2328 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2329 } else if (!err)
2330 err = -EBUSY;
2331
2332 return err;
2333}
2334
2335/* This will reset the tigon3 PHY if there is no valid
2336 * link unless the FORCE argument is non-zero.
2337 */
2338static int tg3_phy_reset(struct tg3 *tp)
2339{
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002340 u32 val, cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 int err;
2342
Michael Chan60189dd2006-12-17 17:08:07 -08002343 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002344 val = tr32(GRC_MISC_CFG);
2345 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
2346 udelay(40);
2347 }
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002348 err = tg3_readphy(tp, MII_BMSR, &val);
2349 err |= tg3_readphy(tp, MII_BMSR, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 if (err != 0)
2351 return -EBUSY;
2352
Michael Chanc8e1e822006-04-29 18:55:17 -07002353 if (netif_running(tp->dev) && netif_carrier_ok(tp->dev)) {
2354 netif_carrier_off(tp->dev);
2355 tg3_link_report(tp);
2356 }
2357
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2359 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2360 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
2361 err = tg3_phy_reset_5703_4_5(tp);
2362 if (err)
2363 return err;
2364 goto out;
2365 }
2366
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002367 cpmuctrl = 0;
2368 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
2369 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
2370 cpmuctrl = tr32(TG3_CPMU_CTRL);
2371 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
2372 tw32(TG3_CPMU_CTRL,
2373 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
2374 }
2375
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 err = tg3_bmcr_reset(tp);
2377 if (err)
2378 return err;
2379
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002380 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002381 val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
2382 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002383
2384 tw32(TG3_CPMU_CTRL, cpmuctrl);
2385 }
2386
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002387 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2388 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002389 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2390 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
2391 CPMU_LSPD_1000MB_MACCLK_12_5) {
2392 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2393 udelay(40);
2394 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2395 }
2396 }
2397
Joe Perches63c3a662011-04-26 08:12:10 +00002398 if (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002399 (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
Matt Carlsonecf14102010-01-20 16:58:05 +00002400 return 0;
2401
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002402 tg3_phy_apply_otp(tp);
2403
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002404 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -08002405 tg3_phy_toggle_apd(tp, true);
2406 else
2407 tg3_phy_toggle_apd(tp, false);
2408
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409out:
Matt Carlson1d36ba42011-04-20 07:57:42 +00002410 if ((tp->phy_flags & TG3_PHYFLG_ADC_BUG) &&
2411 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002412 tg3_phydsp_write(tp, 0x201f, 0x2aaa);
2413 tg3_phydsp_write(tp, 0x000a, 0x0323);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002414 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002416
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002417 if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002418 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2419 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002421
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002422 if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002423 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2424 tg3_phydsp_write(tp, 0x000a, 0x310b);
2425 tg3_phydsp_write(tp, 0x201f, 0x9506);
2426 tg3_phydsp_write(tp, 0x401f, 0x14e2);
2427 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2428 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002429 } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002430 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2431 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
2432 if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
2433 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2434 tg3_writephy(tp, MII_TG3_TEST1,
2435 MII_TG3_TEST1_TRIM_EN | 0x4);
2436 } else
2437 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
2438
2439 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2440 }
Michael Chanc424cb22006-04-29 18:56:34 -07002441 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002442
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 /* Set Extended packet length bit (bit 14) on all chips that */
2444 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002445 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 /* Cannot do read-modify-write on 5401 */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002447 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Joe Perches63c3a662011-04-26 08:12:10 +00002448 } else if (tg3_flag(tp, JUMBO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 /* Set bit 14 with read-modify-write to preserve other bits */
Matt Carlson15ee95c2011-04-20 07:57:40 +00002450 err = tg3_phy_auxctl_read(tp,
2451 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2452 if (!err)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002453 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2454 val | MII_TG3_AUXCTL_ACTL_EXTPKTLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 }
2456
2457 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2458 * jumbo frames transmission.
2459 */
Joe Perches63c3a662011-04-26 08:12:10 +00002460 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002461 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002462 tg3_writephy(tp, MII_TG3_EXT_CTRL,
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002463 val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 }
2465
Michael Chan715116a2006-09-27 16:09:25 -07002466 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002467 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002468 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002469 }
2470
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002471 tg3_phy_toggle_automdix(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 tg3_phy_set_wirespeed(tp);
2473 return 0;
2474}
2475
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002476#define TG3_GPIO_MSG_DRVR_PRES 0x00000001
2477#define TG3_GPIO_MSG_NEED_VAUX 0x00000002
2478#define TG3_GPIO_MSG_MASK (TG3_GPIO_MSG_DRVR_PRES | \
2479 TG3_GPIO_MSG_NEED_VAUX)
2480#define TG3_GPIO_MSG_ALL_DRVR_PRES_MASK \
2481 ((TG3_GPIO_MSG_DRVR_PRES << 0) | \
2482 (TG3_GPIO_MSG_DRVR_PRES << 4) | \
2483 (TG3_GPIO_MSG_DRVR_PRES << 8) | \
2484 (TG3_GPIO_MSG_DRVR_PRES << 12))
2485
2486#define TG3_GPIO_MSG_ALL_NEED_VAUX_MASK \
2487 ((TG3_GPIO_MSG_NEED_VAUX << 0) | \
2488 (TG3_GPIO_MSG_NEED_VAUX << 4) | \
2489 (TG3_GPIO_MSG_NEED_VAUX << 8) | \
2490 (TG3_GPIO_MSG_NEED_VAUX << 12))
2491
2492static inline u32 tg3_set_function_status(struct tg3 *tp, u32 newstat)
2493{
2494 u32 status, 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 status = tg3_ape_read32(tp, TG3_APE_GPIO_MSG);
2499 else
2500 status = tr32(TG3_CPMU_DRV_STATUS);
2501
2502 shift = TG3_APE_GPIO_MSG_SHIFT + 4 * tp->pci_fn;
2503 status &= ~(TG3_GPIO_MSG_MASK << shift);
2504 status |= (newstat << shift);
2505
2506 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2507 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
2508 tg3_ape_write32(tp, TG3_APE_GPIO_MSG, status);
2509 else
2510 tw32(TG3_CPMU_DRV_STATUS, status);
2511
2512 return status >> TG3_APE_GPIO_MSG_SHIFT;
2513}
2514
Matt Carlson520b2752011-06-13 13:39:02 +00002515static inline int tg3_pwrsrc_switch_to_vmain(struct tg3 *tp)
2516{
2517 if (!tg3_flag(tp, IS_NIC))
2518 return 0;
2519
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002520 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2521 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
2522 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
2523 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2524 return -EIO;
Matt Carlson520b2752011-06-13 13:39:02 +00002525
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002526 tg3_set_function_status(tp, TG3_GPIO_MSG_DRVR_PRES);
2527
2528 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2529 TG3_GRC_LCLCTL_PWRSW_DELAY);
2530
2531 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
2532 } else {
2533 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2534 TG3_GRC_LCLCTL_PWRSW_DELAY);
2535 }
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002536
Matt Carlson520b2752011-06-13 13:39:02 +00002537 return 0;
2538}
2539
2540static void tg3_pwrsrc_die_with_vmain(struct tg3 *tp)
2541{
2542 u32 grc_local_ctrl;
2543
2544 if (!tg3_flag(tp, IS_NIC) ||
2545 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2546 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)
2547 return;
2548
2549 grc_local_ctrl = tp->grc_local_ctrl | GRC_LCLCTRL_GPIO_OE1;
2550
2551 tw32_wait_f(GRC_LOCAL_CTRL,
2552 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2553 TG3_GRC_LCLCTL_PWRSW_DELAY);
2554
2555 tw32_wait_f(GRC_LOCAL_CTRL,
2556 grc_local_ctrl,
2557 TG3_GRC_LCLCTL_PWRSW_DELAY);
2558
2559 tw32_wait_f(GRC_LOCAL_CTRL,
2560 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2561 TG3_GRC_LCLCTL_PWRSW_DELAY);
2562}
2563
2564static void tg3_pwrsrc_switch_to_vaux(struct tg3 *tp)
2565{
2566 if (!tg3_flag(tp, IS_NIC))
2567 return;
2568
2569 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2570 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2571 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2572 (GRC_LCLCTRL_GPIO_OE0 |
2573 GRC_LCLCTRL_GPIO_OE1 |
2574 GRC_LCLCTRL_GPIO_OE2 |
2575 GRC_LCLCTRL_GPIO_OUTPUT0 |
2576 GRC_LCLCTRL_GPIO_OUTPUT1),
2577 TG3_GRC_LCLCTL_PWRSW_DELAY);
2578 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2579 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
2580 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2581 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2582 GRC_LCLCTRL_GPIO_OE1 |
2583 GRC_LCLCTRL_GPIO_OE2 |
2584 GRC_LCLCTRL_GPIO_OUTPUT0 |
2585 GRC_LCLCTRL_GPIO_OUTPUT1 |
2586 tp->grc_local_ctrl;
2587 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2588 TG3_GRC_LCLCTL_PWRSW_DELAY);
2589
2590 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2591 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2592 TG3_GRC_LCLCTL_PWRSW_DELAY);
2593
2594 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2595 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2596 TG3_GRC_LCLCTL_PWRSW_DELAY);
2597 } else {
2598 u32 no_gpio2;
2599 u32 grc_local_ctrl = 0;
2600
2601 /* Workaround to prevent overdrawing Amps. */
2602 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
2603 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
2604 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2605 grc_local_ctrl,
2606 TG3_GRC_LCLCTL_PWRSW_DELAY);
2607 }
2608
2609 /* On 5753 and variants, GPIO2 cannot be used. */
2610 no_gpio2 = tp->nic_sram_data_cfg &
2611 NIC_SRAM_DATA_CFG_NO_GPIO2;
2612
2613 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
2614 GRC_LCLCTRL_GPIO_OE1 |
2615 GRC_LCLCTRL_GPIO_OE2 |
2616 GRC_LCLCTRL_GPIO_OUTPUT1 |
2617 GRC_LCLCTRL_GPIO_OUTPUT2;
2618 if (no_gpio2) {
2619 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2620 GRC_LCLCTRL_GPIO_OUTPUT2);
2621 }
2622 tw32_wait_f(GRC_LOCAL_CTRL,
2623 tp->grc_local_ctrl | grc_local_ctrl,
2624 TG3_GRC_LCLCTL_PWRSW_DELAY);
2625
2626 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2627
2628 tw32_wait_f(GRC_LOCAL_CTRL,
2629 tp->grc_local_ctrl | grc_local_ctrl,
2630 TG3_GRC_LCLCTL_PWRSW_DELAY);
2631
2632 if (!no_gpio2) {
2633 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
2634 tw32_wait_f(GRC_LOCAL_CTRL,
2635 tp->grc_local_ctrl | grc_local_ctrl,
2636 TG3_GRC_LCLCTL_PWRSW_DELAY);
2637 }
2638 }
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002639}
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002640
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002641static void tg3_frob_aux_power_5717(struct tg3 *tp, bool wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002642{
2643 u32 msg = 0;
2644
2645 /* Serialize power state transitions */
2646 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2647 return;
2648
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002649 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE) || wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002650 msg = TG3_GPIO_MSG_NEED_VAUX;
2651
2652 msg = tg3_set_function_status(tp, msg);
2653
2654 if (msg & TG3_GPIO_MSG_ALL_DRVR_PRES_MASK)
2655 goto done;
2656
2657 if (msg & TG3_GPIO_MSG_ALL_NEED_VAUX_MASK)
2658 tg3_pwrsrc_switch_to_vaux(tp);
2659 else
2660 tg3_pwrsrc_die_with_vmain(tp);
2661
2662done:
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002663 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
Matt Carlson520b2752011-06-13 13:39:02 +00002664}
2665
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002666static void tg3_frob_aux_power(struct tg3 *tp, bool include_wol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667{
Matt Carlson683644b2011-03-09 16:58:23 +00002668 bool need_vaux = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
Matt Carlson334355a2010-01-20 16:58:10 +00002670 /* The GPIOs do something completely different on 57765. */
Matt Carlson55086ad2011-12-14 11:09:59 +00002671 if (!tg3_flag(tp, IS_NIC) || tg3_flag(tp, 57765_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 return;
2673
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002674 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2675 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
2676 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002677 tg3_frob_aux_power_5717(tp, include_wol ?
2678 tg3_flag(tp, WOL_ENABLE) != 0 : 0);
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002679 return;
2680 }
2681
2682 if (tp->pdev_peer && tp->pdev_peer != tp->pdev) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002683 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002685 dev_peer = pci_get_drvdata(tp->pdev_peer);
Matt Carlson683644b2011-03-09 16:58:23 +00002686
Michael Chanbc1c7562006-03-20 17:48:03 -08002687 /* remove_one() may have been run on the peer. */
Matt Carlson683644b2011-03-09 16:58:23 +00002688 if (dev_peer) {
2689 struct tg3 *tp_peer = netdev_priv(dev_peer);
2690
Joe Perches63c3a662011-04-26 08:12:10 +00002691 if (tg3_flag(tp_peer, INIT_COMPLETE))
Matt Carlson683644b2011-03-09 16:58:23 +00002692 return;
2693
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002694 if ((include_wol && tg3_flag(tp_peer, WOL_ENABLE)) ||
Joe Perches63c3a662011-04-26 08:12:10 +00002695 tg3_flag(tp_peer, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002696 need_vaux = true;
2697 }
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002698 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002700 if ((include_wol && tg3_flag(tp, WOL_ENABLE)) ||
2701 tg3_flag(tp, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002702 need_vaux = true;
2703
Matt Carlson520b2752011-06-13 13:39:02 +00002704 if (need_vaux)
2705 tg3_pwrsrc_switch_to_vaux(tp);
2706 else
2707 tg3_pwrsrc_die_with_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708}
2709
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002710static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2711{
2712 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2713 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00002714 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002715 if (speed != SPEED_10)
2716 return 1;
2717 } else if (speed == SPEED_10)
2718 return 1;
2719
2720 return 0;
2721}
2722
Matt Carlson0a459aa2008-11-03 16:54:15 -08002723static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002724{
Matt Carlsonce057f02007-11-12 21:08:03 -08002725 u32 val;
2726
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002727 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Michael Chan51297242007-02-13 12:17:57 -08002728 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2729 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2730 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2731
2732 sg_dig_ctrl |=
2733 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2734 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2735 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2736 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002737 return;
Michael Chan51297242007-02-13 12:17:57 -08002738 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002739
Michael Chan60189dd2006-12-17 17:08:07 -08002740 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002741 tg3_bmcr_reset(tp);
2742 val = tr32(GRC_MISC_CFG);
2743 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2744 udelay(40);
2745 return;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002746 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson0e5f7842009-11-02 14:26:38 +00002747 u32 phytest;
2748 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2749 u32 phy;
2750
2751 tg3_writephy(tp, MII_ADVERTISE, 0);
2752 tg3_writephy(tp, MII_BMCR,
2753 BMCR_ANENABLE | BMCR_ANRESTART);
2754
2755 tg3_writephy(tp, MII_TG3_FET_TEST,
2756 phytest | MII_TG3_FET_SHADOW_EN);
2757 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
2758 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
2759 tg3_writephy(tp,
2760 MII_TG3_FET_SHDW_AUXMODE4,
2761 phy);
2762 }
2763 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2764 }
2765 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002766 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07002767 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2768 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002769
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002770 val = MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2771 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2772 MII_TG3_AUXCTL_PCTL_VREG_11V;
2773 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, val);
Michael Chan715116a2006-09-27 16:09:25 -07002774 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002775
Michael Chan15c3b692006-03-22 01:06:52 -08002776 /* The PHY should not be powered down on some chips because
2777 * of bugs.
2778 */
2779 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2780 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2781 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002782 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Michael Chan15c3b692006-03-22 01:06:52 -08002783 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08002784
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002785 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2786 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002787 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2788 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2789 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2790 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2791 }
2792
Michael Chan15c3b692006-03-22 01:06:52 -08002793 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2794}
2795
Matt Carlson3f007892008-11-03 16:51:36 -08002796/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002797static int tg3_nvram_lock(struct tg3 *tp)
2798{
Joe Perches63c3a662011-04-26 08:12:10 +00002799 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002800 int i;
2801
2802 if (tp->nvram_lock_cnt == 0) {
2803 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2804 for (i = 0; i < 8000; i++) {
2805 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2806 break;
2807 udelay(20);
2808 }
2809 if (i == 8000) {
2810 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2811 return -ENODEV;
2812 }
2813 }
2814 tp->nvram_lock_cnt++;
2815 }
2816 return 0;
2817}
2818
2819/* tp->lock is held. */
2820static void tg3_nvram_unlock(struct tg3 *tp)
2821{
Joe Perches63c3a662011-04-26 08:12:10 +00002822 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002823 if (tp->nvram_lock_cnt > 0)
2824 tp->nvram_lock_cnt--;
2825 if (tp->nvram_lock_cnt == 0)
2826 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
2827 }
2828}
2829
2830/* tp->lock is held. */
2831static void tg3_enable_nvram_access(struct tg3 *tp)
2832{
Joe Perches63c3a662011-04-26 08:12:10 +00002833 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002834 u32 nvaccess = tr32(NVRAM_ACCESS);
2835
2836 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
2837 }
2838}
2839
2840/* tp->lock is held. */
2841static void tg3_disable_nvram_access(struct tg3 *tp)
2842{
Joe Perches63c3a662011-04-26 08:12:10 +00002843 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002844 u32 nvaccess = tr32(NVRAM_ACCESS);
2845
2846 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
2847 }
2848}
2849
2850static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
2851 u32 offset, u32 *val)
2852{
2853 u32 tmp;
2854 int i;
2855
2856 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
2857 return -EINVAL;
2858
2859 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
2860 EEPROM_ADDR_DEVID_MASK |
2861 EEPROM_ADDR_READ);
2862 tw32(GRC_EEPROM_ADDR,
2863 tmp |
2864 (0 << EEPROM_ADDR_DEVID_SHIFT) |
2865 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
2866 EEPROM_ADDR_ADDR_MASK) |
2867 EEPROM_ADDR_READ | EEPROM_ADDR_START);
2868
2869 for (i = 0; i < 1000; i++) {
2870 tmp = tr32(GRC_EEPROM_ADDR);
2871
2872 if (tmp & EEPROM_ADDR_COMPLETE)
2873 break;
2874 msleep(1);
2875 }
2876 if (!(tmp & EEPROM_ADDR_COMPLETE))
2877 return -EBUSY;
2878
Matt Carlson62cedd12009-04-20 14:52:29 -07002879 tmp = tr32(GRC_EEPROM_DATA);
2880
2881 /*
2882 * The data will always be opposite the native endian
2883 * format. Perform a blind byteswap to compensate.
2884 */
2885 *val = swab32(tmp);
2886
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002887 return 0;
2888}
2889
2890#define NVRAM_CMD_TIMEOUT 10000
2891
2892static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
2893{
2894 int i;
2895
2896 tw32(NVRAM_CMD, nvram_cmd);
2897 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
2898 udelay(10);
2899 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
2900 udelay(10);
2901 break;
2902 }
2903 }
2904
2905 if (i == NVRAM_CMD_TIMEOUT)
2906 return -EBUSY;
2907
2908 return 0;
2909}
2910
2911static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
2912{
Joe Perches63c3a662011-04-26 08:12:10 +00002913 if (tg3_flag(tp, NVRAM) &&
2914 tg3_flag(tp, NVRAM_BUFFERED) &&
2915 tg3_flag(tp, FLASH) &&
2916 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002917 (tp->nvram_jedecnum == JEDEC_ATMEL))
2918
2919 addr = ((addr / tp->nvram_pagesize) <<
2920 ATMEL_AT45DB0X1B_PAGE_POS) +
2921 (addr % tp->nvram_pagesize);
2922
2923 return addr;
2924}
2925
2926static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
2927{
Joe Perches63c3a662011-04-26 08:12:10 +00002928 if (tg3_flag(tp, NVRAM) &&
2929 tg3_flag(tp, NVRAM_BUFFERED) &&
2930 tg3_flag(tp, FLASH) &&
2931 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002932 (tp->nvram_jedecnum == JEDEC_ATMEL))
2933
2934 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
2935 tp->nvram_pagesize) +
2936 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
2937
2938 return addr;
2939}
2940
Matt Carlsone4f34112009-02-25 14:25:00 +00002941/* NOTE: Data read in from NVRAM is byteswapped according to
2942 * the byteswapping settings for all other register accesses.
2943 * tg3 devices are BE devices, so on a BE machine, the data
2944 * returned will be exactly as it is seen in NVRAM. On a LE
2945 * machine, the 32-bit value will be byteswapped.
2946 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002947static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
2948{
2949 int ret;
2950
Joe Perches63c3a662011-04-26 08:12:10 +00002951 if (!tg3_flag(tp, NVRAM))
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002952 return tg3_nvram_read_using_eeprom(tp, offset, val);
2953
2954 offset = tg3_nvram_phys_addr(tp, offset);
2955
2956 if (offset > NVRAM_ADDR_MSK)
2957 return -EINVAL;
2958
2959 ret = tg3_nvram_lock(tp);
2960 if (ret)
2961 return ret;
2962
2963 tg3_enable_nvram_access(tp);
2964
2965 tw32(NVRAM_ADDR, offset);
2966 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
2967 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
2968
2969 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00002970 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002971
2972 tg3_disable_nvram_access(tp);
2973
2974 tg3_nvram_unlock(tp);
2975
2976 return ret;
2977}
2978
Matt Carlsona9dc5292009-02-25 14:25:30 +00002979/* Ensures NVRAM data is in bytestream format. */
2980static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002981{
2982 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00002983 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002984 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00002985 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002986 return res;
2987}
2988
Matt Carlsondbe9b922012-02-13 10:20:09 +00002989static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
2990 u32 offset, u32 len, u8 *buf)
2991{
2992 int i, j, rc = 0;
2993 u32 val;
2994
2995 for (i = 0; i < len; i += 4) {
2996 u32 addr;
2997 __be32 data;
2998
2999 addr = offset + i;
3000
3001 memcpy(&data, buf + i, 4);
3002
3003 /*
3004 * The SEEPROM interface expects the data to always be opposite
3005 * the native endian format. We accomplish this by reversing
3006 * all the operations that would have been performed on the
3007 * data from a call to tg3_nvram_read_be32().
3008 */
3009 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
3010
3011 val = tr32(GRC_EEPROM_ADDR);
3012 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
3013
3014 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
3015 EEPROM_ADDR_READ);
3016 tw32(GRC_EEPROM_ADDR, val |
3017 (0 << EEPROM_ADDR_DEVID_SHIFT) |
3018 (addr & EEPROM_ADDR_ADDR_MASK) |
3019 EEPROM_ADDR_START |
3020 EEPROM_ADDR_WRITE);
3021
3022 for (j = 0; j < 1000; j++) {
3023 val = tr32(GRC_EEPROM_ADDR);
3024
3025 if (val & EEPROM_ADDR_COMPLETE)
3026 break;
3027 msleep(1);
3028 }
3029 if (!(val & EEPROM_ADDR_COMPLETE)) {
3030 rc = -EBUSY;
3031 break;
3032 }
3033 }
3034
3035 return rc;
3036}
3037
3038/* offset and length are dword aligned */
3039static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
3040 u8 *buf)
3041{
3042 int ret = 0;
3043 u32 pagesize = tp->nvram_pagesize;
3044 u32 pagemask = pagesize - 1;
3045 u32 nvram_cmd;
3046 u8 *tmp;
3047
3048 tmp = kmalloc(pagesize, GFP_KERNEL);
3049 if (tmp == NULL)
3050 return -ENOMEM;
3051
3052 while (len) {
3053 int j;
3054 u32 phy_addr, page_off, size;
3055
3056 phy_addr = offset & ~pagemask;
3057
3058 for (j = 0; j < pagesize; j += 4) {
3059 ret = tg3_nvram_read_be32(tp, phy_addr + j,
3060 (__be32 *) (tmp + j));
3061 if (ret)
3062 break;
3063 }
3064 if (ret)
3065 break;
3066
3067 page_off = offset & pagemask;
3068 size = pagesize;
3069 if (len < size)
3070 size = len;
3071
3072 len -= size;
3073
3074 memcpy(tmp + page_off, buf, size);
3075
3076 offset = offset + (pagesize - page_off);
3077
3078 tg3_enable_nvram_access(tp);
3079
3080 /*
3081 * Before we can erase the flash page, we need
3082 * to issue a special "write enable" command.
3083 */
3084 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3085
3086 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3087 break;
3088
3089 /* Erase the target page */
3090 tw32(NVRAM_ADDR, phy_addr);
3091
3092 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
3093 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
3094
3095 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3096 break;
3097
3098 /* Issue another write enable to start the write. */
3099 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3100
3101 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3102 break;
3103
3104 for (j = 0; j < pagesize; j += 4) {
3105 __be32 data;
3106
3107 data = *((__be32 *) (tmp + j));
3108
3109 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3110
3111 tw32(NVRAM_ADDR, phy_addr + j);
3112
3113 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
3114 NVRAM_CMD_WR;
3115
3116 if (j == 0)
3117 nvram_cmd |= NVRAM_CMD_FIRST;
3118 else if (j == (pagesize - 4))
3119 nvram_cmd |= NVRAM_CMD_LAST;
3120
3121 ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
3122 if (ret)
3123 break;
3124 }
3125 if (ret)
3126 break;
3127 }
3128
3129 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3130 tg3_nvram_exec_cmd(tp, nvram_cmd);
3131
3132 kfree(tmp);
3133
3134 return ret;
3135}
3136
3137/* offset and length are dword aligned */
3138static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
3139 u8 *buf)
3140{
3141 int i, ret = 0;
3142
3143 for (i = 0; i < len; i += 4, offset += 4) {
3144 u32 page_off, phy_addr, nvram_cmd;
3145 __be32 data;
3146
3147 memcpy(&data, buf + i, 4);
3148 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3149
3150 page_off = offset % tp->nvram_pagesize;
3151
3152 phy_addr = tg3_nvram_phys_addr(tp, offset);
3153
Matt Carlsondbe9b922012-02-13 10:20:09 +00003154 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
3155
3156 if (page_off == 0 || i == 0)
3157 nvram_cmd |= NVRAM_CMD_FIRST;
3158 if (page_off == (tp->nvram_pagesize - 4))
3159 nvram_cmd |= NVRAM_CMD_LAST;
3160
3161 if (i == (len - 4))
3162 nvram_cmd |= NVRAM_CMD_LAST;
3163
Matt Carlson42278222012-02-13 15:20:11 +00003164 if ((nvram_cmd & NVRAM_CMD_FIRST) ||
3165 !tg3_flag(tp, FLASH) ||
3166 !tg3_flag(tp, 57765_PLUS))
3167 tw32(NVRAM_ADDR, phy_addr);
3168
Matt Carlsondbe9b922012-02-13 10:20:09 +00003169 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
3170 !tg3_flag(tp, 5755_PLUS) &&
3171 (tp->nvram_jedecnum == JEDEC_ST) &&
3172 (nvram_cmd & NVRAM_CMD_FIRST)) {
3173 u32 cmd;
3174
3175 cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3176 ret = tg3_nvram_exec_cmd(tp, cmd);
3177 if (ret)
3178 break;
3179 }
3180 if (!tg3_flag(tp, FLASH)) {
3181 /* We always do complete word writes to eeprom. */
3182 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
3183 }
3184
3185 ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
3186 if (ret)
3187 break;
3188 }
3189 return ret;
3190}
3191
3192/* offset and length are dword aligned */
3193static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
3194{
3195 int ret;
3196
3197 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
3198 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
3199 ~GRC_LCLCTRL_GPIO_OUTPUT1);
3200 udelay(40);
3201 }
3202
3203 if (!tg3_flag(tp, NVRAM)) {
3204 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
3205 } else {
3206 u32 grc_mode;
3207
3208 ret = tg3_nvram_lock(tp);
3209 if (ret)
3210 return ret;
3211
3212 tg3_enable_nvram_access(tp);
3213 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM))
3214 tw32(NVRAM_WRITE1, 0x406);
3215
3216 grc_mode = tr32(GRC_MODE);
3217 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
3218
3219 if (tg3_flag(tp, NVRAM_BUFFERED) || !tg3_flag(tp, FLASH)) {
3220 ret = tg3_nvram_write_block_buffered(tp, offset, len,
3221 buf);
3222 } else {
3223 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
3224 buf);
3225 }
3226
3227 grc_mode = tr32(GRC_MODE);
3228 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
3229
3230 tg3_disable_nvram_access(tp);
3231 tg3_nvram_unlock(tp);
3232 }
3233
3234 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
3235 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
3236 udelay(40);
3237 }
3238
3239 return ret;
3240}
3241
Matt Carlson997b4f12011-08-31 11:44:53 +00003242#define RX_CPU_SCRATCH_BASE 0x30000
3243#define RX_CPU_SCRATCH_SIZE 0x04000
3244#define TX_CPU_SCRATCH_BASE 0x34000
3245#define TX_CPU_SCRATCH_SIZE 0x04000
3246
3247/* tp->lock is held. */
3248static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
3249{
3250 int i;
3251
3252 BUG_ON(offset == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS));
3253
3254 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
3255 u32 val = tr32(GRC_VCPU_EXT_CTRL);
3256
3257 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
3258 return 0;
3259 }
3260 if (offset == RX_CPU_BASE) {
3261 for (i = 0; i < 10000; i++) {
3262 tw32(offset + CPU_STATE, 0xffffffff);
3263 tw32(offset + CPU_MODE, CPU_MODE_HALT);
3264 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
3265 break;
3266 }
3267
3268 tw32(offset + CPU_STATE, 0xffffffff);
3269 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
3270 udelay(10);
3271 } else {
3272 for (i = 0; i < 10000; i++) {
3273 tw32(offset + CPU_STATE, 0xffffffff);
3274 tw32(offset + CPU_MODE, CPU_MODE_HALT);
3275 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
3276 break;
3277 }
3278 }
3279
3280 if (i >= 10000) {
3281 netdev_err(tp->dev, "%s timed out, %s CPU\n",
3282 __func__, offset == RX_CPU_BASE ? "RX" : "TX");
3283 return -ENODEV;
3284 }
3285
3286 /* Clear firmware's nvram arbitration. */
3287 if (tg3_flag(tp, NVRAM))
3288 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
3289 return 0;
3290}
3291
3292struct fw_info {
3293 unsigned int fw_base;
3294 unsigned int fw_len;
3295 const __be32 *fw_data;
3296};
3297
3298/* tp->lock is held. */
3299static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base,
3300 u32 cpu_scratch_base, int cpu_scratch_size,
3301 struct fw_info *info)
3302{
3303 int err, lock_err, i;
3304 void (*write_op)(struct tg3 *, u32, u32);
3305
3306 if (cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS)) {
3307 netdev_err(tp->dev,
3308 "%s: Trying to load TX cpu firmware which is 5705\n",
3309 __func__);
3310 return -EINVAL;
3311 }
3312
3313 if (tg3_flag(tp, 5705_PLUS))
3314 write_op = tg3_write_mem;
3315 else
3316 write_op = tg3_write_indirect_reg32;
3317
3318 /* It is possible that bootcode is still loading at this point.
3319 * Get the nvram lock first before halting the cpu.
3320 */
3321 lock_err = tg3_nvram_lock(tp);
3322 err = tg3_halt_cpu(tp, cpu_base);
3323 if (!lock_err)
3324 tg3_nvram_unlock(tp);
3325 if (err)
3326 goto out;
3327
3328 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
3329 write_op(tp, cpu_scratch_base + i, 0);
3330 tw32(cpu_base + CPU_STATE, 0xffffffff);
3331 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
3332 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
3333 write_op(tp, (cpu_scratch_base +
3334 (info->fw_base & 0xffff) +
3335 (i * sizeof(u32))),
3336 be32_to_cpu(info->fw_data[i]));
3337
3338 err = 0;
3339
3340out:
3341 return err;
3342}
3343
3344/* tp->lock is held. */
3345static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
3346{
3347 struct fw_info info;
3348 const __be32 *fw_data;
3349 int err, i;
3350
3351 fw_data = (void *)tp->fw->data;
3352
3353 /* Firmware blob starts with version numbers, followed by
3354 start address and length. We are setting complete length.
3355 length = end_address_of_bss - start_address_of_text.
3356 Remainder is the blob to be loaded contiguously
3357 from start address. */
3358
3359 info.fw_base = be32_to_cpu(fw_data[1]);
3360 info.fw_len = tp->fw->size - 12;
3361 info.fw_data = &fw_data[3];
3362
3363 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
3364 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
3365 &info);
3366 if (err)
3367 return err;
3368
3369 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
3370 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
3371 &info);
3372 if (err)
3373 return err;
3374
3375 /* Now startup only the RX cpu. */
3376 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3377 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
3378
3379 for (i = 0; i < 5; i++) {
3380 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
3381 break;
3382 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3383 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
3384 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
3385 udelay(1000);
3386 }
3387 if (i >= 5) {
3388 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
3389 "should be %08x\n", __func__,
3390 tr32(RX_CPU_BASE + CPU_PC), info.fw_base);
3391 return -ENODEV;
3392 }
3393 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3394 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
3395
3396 return 0;
3397}
3398
3399/* tp->lock is held. */
3400static int tg3_load_tso_firmware(struct tg3 *tp)
3401{
3402 struct fw_info info;
3403 const __be32 *fw_data;
3404 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
3405 int err, i;
3406
3407 if (tg3_flag(tp, HW_TSO_1) ||
3408 tg3_flag(tp, HW_TSO_2) ||
3409 tg3_flag(tp, HW_TSO_3))
3410 return 0;
3411
3412 fw_data = (void *)tp->fw->data;
3413
3414 /* Firmware blob starts with version numbers, followed by
3415 start address and length. We are setting complete length.
3416 length = end_address_of_bss - start_address_of_text.
3417 Remainder is the blob to be loaded contiguously
3418 from start address. */
3419
3420 info.fw_base = be32_to_cpu(fw_data[1]);
3421 cpu_scratch_size = tp->fw_len;
3422 info.fw_len = tp->fw->size - 12;
3423 info.fw_data = &fw_data[3];
3424
3425 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
3426 cpu_base = RX_CPU_BASE;
3427 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
3428 } else {
3429 cpu_base = TX_CPU_BASE;
3430 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
3431 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
3432 }
3433
3434 err = tg3_load_firmware_cpu(tp, cpu_base,
3435 cpu_scratch_base, cpu_scratch_size,
3436 &info);
3437 if (err)
3438 return err;
3439
3440 /* Now startup the cpu. */
3441 tw32(cpu_base + CPU_STATE, 0xffffffff);
3442 tw32_f(cpu_base + CPU_PC, info.fw_base);
3443
3444 for (i = 0; i < 5; i++) {
3445 if (tr32(cpu_base + CPU_PC) == info.fw_base)
3446 break;
3447 tw32(cpu_base + CPU_STATE, 0xffffffff);
3448 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
3449 tw32_f(cpu_base + CPU_PC, info.fw_base);
3450 udelay(1000);
3451 }
3452 if (i >= 5) {
3453 netdev_err(tp->dev,
3454 "%s fails to set CPU PC, is %08x should be %08x\n",
3455 __func__, tr32(cpu_base + CPU_PC), info.fw_base);
3456 return -ENODEV;
3457 }
3458 tw32(cpu_base + CPU_STATE, 0xffffffff);
3459 tw32_f(cpu_base + CPU_MODE, 0x00000000);
3460 return 0;
3461}
3462
3463
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003464/* tp->lock is held. */
Matt Carlson3f007892008-11-03 16:51:36 -08003465static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
3466{
3467 u32 addr_high, addr_low;
3468 int i;
3469
3470 addr_high = ((tp->dev->dev_addr[0] << 8) |
3471 tp->dev->dev_addr[1]);
3472 addr_low = ((tp->dev->dev_addr[2] << 24) |
3473 (tp->dev->dev_addr[3] << 16) |
3474 (tp->dev->dev_addr[4] << 8) |
3475 (tp->dev->dev_addr[5] << 0));
3476 for (i = 0; i < 4; i++) {
3477 if (i == 1 && skip_mac_1)
3478 continue;
3479 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
3480 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
3481 }
3482
3483 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3484 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
3485 for (i = 0; i < 12; i++) {
3486 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
3487 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
3488 }
3489 }
3490
3491 addr_high = (tp->dev->dev_addr[0] +
3492 tp->dev->dev_addr[1] +
3493 tp->dev->dev_addr[2] +
3494 tp->dev->dev_addr[3] +
3495 tp->dev->dev_addr[4] +
3496 tp->dev->dev_addr[5]) &
3497 TX_BACKOFF_SEED_MASK;
3498 tw32(MAC_TX_BACKOFF_SEED, addr_high);
3499}
3500
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003501static void tg3_enable_register_access(struct tg3 *tp)
3502{
3503 /*
3504 * Make sure register accesses (indirect or otherwise) will function
3505 * correctly.
3506 */
3507 pci_write_config_dword(tp->pdev,
3508 TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
3509}
3510
3511static int tg3_power_up(struct tg3 *tp)
3512{
Matt Carlsonbed98292011-07-13 09:27:29 +00003513 int err;
3514
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003515 tg3_enable_register_access(tp);
3516
Matt Carlsonbed98292011-07-13 09:27:29 +00003517 err = pci_set_power_state(tp->pdev, PCI_D0);
3518 if (!err) {
3519 /* Switch out of Vaux if it is a NIC */
3520 tg3_pwrsrc_switch_to_vmain(tp);
3521 } else {
3522 netdev_err(tp->dev, "Transition to D0 failed\n");
3523 }
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003524
Matt Carlsonbed98292011-07-13 09:27:29 +00003525 return err;
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003526}
3527
Matt Carlson4b409522012-02-13 10:20:11 +00003528static int tg3_setup_phy(struct tg3 *, int);
3529
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003530static int tg3_power_down_prepare(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531{
3532 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003533 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003535 tg3_enable_register_access(tp);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003536
3537 /* Restore the CLKREQ setting. */
Joe Perches63c3a662011-04-26 08:12:10 +00003538 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003539 u16 lnkctl;
3540
3541 pci_read_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00003542 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003543 &lnkctl);
3544 lnkctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
3545 pci_write_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00003546 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003547 lnkctl);
3548 }
3549
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
3551 tw32(TG3PCI_MISC_HOST_CTRL,
3552 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
3553
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003554 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00003555 tg3_flag(tp, WOL_ENABLE);
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003556
Joe Perches63c3a662011-04-26 08:12:10 +00003557 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08003558 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003559 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson800960682010-08-02 11:26:06 +00003560 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003561 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003562 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003563
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00003564 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003565
Matt Carlson800960682010-08-02 11:26:06 +00003566 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003567
Matt Carlsonc6700ce2012-02-13 15:20:15 +00003568 tp->link_config.speed = phydev->speed;
3569 tp->link_config.duplex = phydev->duplex;
3570 tp->link_config.autoneg = phydev->autoneg;
3571 tp->link_config.advertising = phydev->advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003572
3573 advertising = ADVERTISED_TP |
3574 ADVERTISED_Pause |
3575 ADVERTISED_Autoneg |
3576 ADVERTISED_10baseT_Half;
3577
Joe Perches63c3a662011-04-26 08:12:10 +00003578 if (tg3_flag(tp, ENABLE_ASF) || device_should_wake) {
3579 if (tg3_flag(tp, WOL_SPEED_100MB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003580 advertising |=
3581 ADVERTISED_100baseT_Half |
3582 ADVERTISED_100baseT_Full |
3583 ADVERTISED_10baseT_Full;
3584 else
3585 advertising |= ADVERTISED_10baseT_Full;
3586 }
3587
3588 phydev->advertising = advertising;
3589
3590 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08003591
3592 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00003593 if (phyid != PHY_ID_BCMAC131) {
3594 phyid &= PHY_BCM_OUI_MASK;
3595 if (phyid == PHY_BCM_OUI_1 ||
3596 phyid == PHY_BCM_OUI_2 ||
3597 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08003598 do_low_power = true;
3599 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003600 }
Matt Carlsondd477002008-05-25 23:45:58 -07003601 } else {
Matt Carlson20232762008-12-21 20:18:56 -08003602 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003603
Matt Carlsonc6700ce2012-02-13 15:20:15 +00003604 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER))
Matt Carlson800960682010-08-02 11:26:06 +00003605 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606
Matt Carlson2855b9f2012-02-13 15:20:14 +00003607 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlsondd477002008-05-25 23:45:58 -07003608 tg3_setup_phy(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 }
3610
Michael Chanb5d37722006-09-27 16:06:21 -07003611 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
3612 u32 val;
3613
3614 val = tr32(GRC_VCPU_EXT_CTRL);
3615 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
Joe Perches63c3a662011-04-26 08:12:10 +00003616 } else if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08003617 int i;
3618 u32 val;
3619
3620 for (i = 0; i < 200; i++) {
3621 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
3622 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
3623 break;
3624 msleep(1);
3625 }
3626 }
Joe Perches63c3a662011-04-26 08:12:10 +00003627 if (tg3_flag(tp, WOL_CAP))
Gary Zambranoa85feb82007-05-05 11:52:19 -07003628 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
3629 WOL_DRV_STATE_SHUTDOWN |
3630 WOL_DRV_WOL |
3631 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08003632
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003633 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 u32 mac_mode;
3635
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003636 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003637 if (do_low_power &&
3638 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
3639 tg3_phy_auxctl_write(tp,
3640 MII_TG3_AUXCTL_SHDWSEL_PWRCTL,
3641 MII_TG3_AUXCTL_PCTL_WOL_EN |
3642 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
3643 MII_TG3_AUXCTL_PCTL_CL_AB_TXDAC);
Matt Carlsondd477002008-05-25 23:45:58 -07003644 udelay(40);
3645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003647 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07003648 mac_mode = MAC_MODE_PORT_MODE_GMII;
3649 else
3650 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003652 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
3653 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
3654 ASIC_REV_5700) {
Joe Perches63c3a662011-04-26 08:12:10 +00003655 u32 speed = tg3_flag(tp, WOL_SPEED_100MB) ?
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003656 SPEED_100 : SPEED_10;
3657 if (tg3_5700_link_polarity(tp, speed))
3658 mac_mode |= MAC_MODE_LINK_POLARITY;
3659 else
3660 mac_mode &= ~MAC_MODE_LINK_POLARITY;
3661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 } else {
3663 mac_mode = MAC_MODE_PORT_MODE_TBI;
3664 }
3665
Joe Perches63c3a662011-04-26 08:12:10 +00003666 if (!tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 tw32(MAC_LED_CTRL, tp->led_ctrl);
3668
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003669 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00003670 if ((tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS)) &&
3671 (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)))
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003672 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673
Joe Perches63c3a662011-04-26 08:12:10 +00003674 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00003675 mac_mode |= MAC_MODE_APE_TX_EN |
3676 MAC_MODE_APE_RX_EN |
3677 MAC_MODE_TDE_ENABLE;
Matt Carlson3bda1252008-08-15 14:08:22 -07003678
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 tw32_f(MAC_MODE, mac_mode);
3680 udelay(100);
3681
3682 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
3683 udelay(10);
3684 }
3685
Joe Perches63c3a662011-04-26 08:12:10 +00003686 if (!tg3_flag(tp, WOL_SPEED_100MB) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3688 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
3689 u32 base_val;
3690
3691 base_val = tp->pci_clock_ctrl;
3692 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
3693 CLOCK_CTRL_TXCLK_DISABLE);
3694
Michael Chanb401e9e2005-12-19 16:27:04 -08003695 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
3696 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Joe Perches63c3a662011-04-26 08:12:10 +00003697 } else if (tg3_flag(tp, 5780_CLASS) ||
3698 tg3_flag(tp, CPMU_PRESENT) ||
Matt Carlson6ff6f812011-05-19 12:12:54 +00003699 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan4cf78e42005-07-25 12:29:19 -07003700 /* do nothing */
Joe Perches63c3a662011-04-26 08:12:10 +00003701 } else if (!(tg3_flag(tp, 5750_PLUS) && tg3_flag(tp, ENABLE_ASF))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 u32 newbits1, newbits2;
3703
3704 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3705 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3706 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
3707 CLOCK_CTRL_TXCLK_DISABLE |
3708 CLOCK_CTRL_ALTCLK);
3709 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
Joe Perches63c3a662011-04-26 08:12:10 +00003710 } else if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 newbits1 = CLOCK_CTRL_625_CORE;
3712 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
3713 } else {
3714 newbits1 = CLOCK_CTRL_ALTCLK;
3715 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
3716 }
3717
Michael Chanb401e9e2005-12-19 16:27:04 -08003718 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
3719 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720
Michael Chanb401e9e2005-12-19 16:27:04 -08003721 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
3722 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723
Joe Perches63c3a662011-04-26 08:12:10 +00003724 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 u32 newbits3;
3726
3727 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3728 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3729 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
3730 CLOCK_CTRL_TXCLK_DISABLE |
3731 CLOCK_CTRL_44MHZ_CORE);
3732 } else {
3733 newbits3 = CLOCK_CTRL_44MHZ_CORE;
3734 }
3735
Michael Chanb401e9e2005-12-19 16:27:04 -08003736 tw32_wait_f(TG3PCI_CLOCK_CTRL,
3737 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 }
3739 }
3740
Joe Perches63c3a662011-04-26 08:12:10 +00003741 if (!(device_should_wake) && !tg3_flag(tp, ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08003742 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08003743
Matt Carlsoncd0d7222011-07-13 09:27:33 +00003744 tg3_frob_aux_power(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745
3746 /* Workaround for unstable PLL clock */
3747 if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
3748 (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
3749 u32 val = tr32(0x7d00);
3750
3751 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
3752 tw32(0x7d00, val);
Joe Perches63c3a662011-04-26 08:12:10 +00003753 if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08003754 int err;
3755
3756 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08003758 if (!err)
3759 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08003760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 }
3762
Michael Chanbbadf502006-04-06 21:46:34 -07003763 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
3764
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 return 0;
3766}
3767
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003768static void tg3_power_down(struct tg3 *tp)
3769{
3770 tg3_power_down_prepare(tp);
3771
Joe Perches63c3a662011-04-26 08:12:10 +00003772 pci_wake_from_d3(tp->pdev, tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003773 pci_set_power_state(tp->pdev, PCI_D3hot);
3774}
3775
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
3777{
3778 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
3779 case MII_TG3_AUX_STAT_10HALF:
3780 *speed = SPEED_10;
3781 *duplex = DUPLEX_HALF;
3782 break;
3783
3784 case MII_TG3_AUX_STAT_10FULL:
3785 *speed = SPEED_10;
3786 *duplex = DUPLEX_FULL;
3787 break;
3788
3789 case MII_TG3_AUX_STAT_100HALF:
3790 *speed = SPEED_100;
3791 *duplex = DUPLEX_HALF;
3792 break;
3793
3794 case MII_TG3_AUX_STAT_100FULL:
3795 *speed = SPEED_100;
3796 *duplex = DUPLEX_FULL;
3797 break;
3798
3799 case MII_TG3_AUX_STAT_1000HALF:
3800 *speed = SPEED_1000;
3801 *duplex = DUPLEX_HALF;
3802 break;
3803
3804 case MII_TG3_AUX_STAT_1000FULL:
3805 *speed = SPEED_1000;
3806 *duplex = DUPLEX_FULL;
3807 break;
3808
3809 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003810 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07003811 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
3812 SPEED_10;
3813 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
3814 DUPLEX_HALF;
3815 break;
3816 }
Matt Carlsone7405222012-02-13 15:20:16 +00003817 *speed = SPEED_UNKNOWN;
3818 *duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821}
3822
Matt Carlson42b64a42011-05-19 12:12:49 +00003823static int tg3_phy_autoneg_cfg(struct tg3 *tp, u32 advertise, u32 flowctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824{
Matt Carlson42b64a42011-05-19 12:12:49 +00003825 int err = 0;
3826 u32 val, new_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827
Matt Carlson42b64a42011-05-19 12:12:49 +00003828 new_adv = ADVERTISE_CSMA;
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +00003829 new_adv |= ethtool_adv_to_mii_adv_t(advertise) & ADVERTISE_ALL;
Matt Carlsonf88788f2011-12-14 11:10:00 +00003830 new_adv |= mii_advertise_flowctrl(flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831
Matt Carlson42b64a42011-05-19 12:12:49 +00003832 err = tg3_writephy(tp, MII_ADVERTISE, new_adv);
3833 if (err)
3834 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835
Matt Carlson4f272092011-12-14 11:09:57 +00003836 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
3837 new_adv = ethtool_adv_to_mii_ctrl1000_t(advertise);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003838
Matt Carlson4f272092011-12-14 11:09:57 +00003839 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3840 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
3841 new_adv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003842
Matt Carlson4f272092011-12-14 11:09:57 +00003843 err = tg3_writephy(tp, MII_CTRL1000, new_adv);
3844 if (err)
3845 goto done;
3846 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003847
Matt Carlson42b64a42011-05-19 12:12:49 +00003848 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
3849 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850
Matt Carlson42b64a42011-05-19 12:12:49 +00003851 tw32(TG3_CPMU_EEE_MODE,
3852 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003853
Matt Carlson42b64a42011-05-19 12:12:49 +00003854 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
3855 if (!err) {
3856 u32 err2;
Matt Carlson52b02d02010-10-14 10:37:41 +00003857
Matt Carlsona6b68da2010-12-06 08:28:52 +00003858 val = 0;
Matt Carlson42b64a42011-05-19 12:12:49 +00003859 /* Advertise 100-BaseTX EEE ability */
3860 if (advertise & ADVERTISED_100baseT_Full)
3861 val |= MDIO_AN_EEE_ADV_100TX;
3862 /* Advertise 1000-BaseT EEE ability */
3863 if (advertise & ADVERTISED_1000baseT_Full)
3864 val |= MDIO_AN_EEE_ADV_1000T;
3865 err = tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlsonb715ce92011-07-20 10:20:52 +00003866 if (err)
3867 val = 0;
3868
3869 switch (GET_ASIC_REV(tp->pci_chip_rev_id)) {
3870 case ASIC_REV_5717:
3871 case ASIC_REV_57765:
Matt Carlson55086ad2011-12-14 11:09:59 +00003872 case ASIC_REV_57766:
Matt Carlsonb715ce92011-07-20 10:20:52 +00003873 case ASIC_REV_5719:
3874 /* If we advertised any eee advertisements above... */
3875 if (val)
3876 val = MII_TG3_DSP_TAP26_ALNOKO |
3877 MII_TG3_DSP_TAP26_RMRXSTO |
3878 MII_TG3_DSP_TAP26_OPCSINPT;
3879 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
3880 /* Fall through */
3881 case ASIC_REV_5720:
3882 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
3883 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
3884 MII_TG3_DSP_CH34TP2_HIBW01);
3885 }
Matt Carlson52b02d02010-10-14 10:37:41 +00003886
Matt Carlson42b64a42011-05-19 12:12:49 +00003887 err2 = TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
3888 if (!err)
3889 err = err2;
3890 }
3891
3892done:
3893 return err;
3894}
3895
3896static void tg3_phy_copper_begin(struct tg3 *tp)
3897{
Matt Carlsond13ba512012-02-22 12:35:19 +00003898 if (tp->link_config.autoneg == AUTONEG_ENABLE ||
3899 (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
3900 u32 adv, fc;
Matt Carlson42b64a42011-05-19 12:12:49 +00003901
Matt Carlsond13ba512012-02-22 12:35:19 +00003902 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
3903 adv = ADVERTISED_10baseT_Half |
3904 ADVERTISED_10baseT_Full;
3905 if (tg3_flag(tp, WOL_SPEED_100MB))
3906 adv |= ADVERTISED_100baseT_Half |
3907 ADVERTISED_100baseT_Full;
Matt Carlson42b64a42011-05-19 12:12:49 +00003908
Matt Carlsond13ba512012-02-22 12:35:19 +00003909 fc = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson42b64a42011-05-19 12:12:49 +00003910 } else {
Matt Carlsond13ba512012-02-22 12:35:19 +00003911 adv = tp->link_config.advertising;
3912 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
3913 adv &= ~(ADVERTISED_1000baseT_Half |
3914 ADVERTISED_1000baseT_Full);
3915
3916 fc = tp->link_config.flowctrl;
Matt Carlson42b64a42011-05-19 12:12:49 +00003917 }
3918
Matt Carlsond13ba512012-02-22 12:35:19 +00003919 tg3_phy_autoneg_cfg(tp, adv, fc);
Matt Carlson52b02d02010-10-14 10:37:41 +00003920
Matt Carlsond13ba512012-02-22 12:35:19 +00003921 tg3_writephy(tp, MII_BMCR,
3922 BMCR_ANENABLE | BMCR_ANRESTART);
3923 } else {
3924 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 u32 bmcr, orig_bmcr;
3926
3927 tp->link_config.active_speed = tp->link_config.speed;
3928 tp->link_config.active_duplex = tp->link_config.duplex;
3929
3930 bmcr = 0;
3931 switch (tp->link_config.speed) {
3932 default:
3933 case SPEED_10:
3934 break;
3935
3936 case SPEED_100:
3937 bmcr |= BMCR_SPEED100;
3938 break;
3939
3940 case SPEED_1000:
Matt Carlson221c5632011-06-13 13:39:01 +00003941 bmcr |= BMCR_SPEED1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944
3945 if (tp->link_config.duplex == DUPLEX_FULL)
3946 bmcr |= BMCR_FULLDPLX;
3947
3948 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
3949 (bmcr != orig_bmcr)) {
3950 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
3951 for (i = 0; i < 1500; i++) {
3952 u32 tmp;
3953
3954 udelay(10);
3955 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
3956 tg3_readphy(tp, MII_BMSR, &tmp))
3957 continue;
3958 if (!(tmp & BMSR_LSTATUS)) {
3959 udelay(40);
3960 break;
3961 }
3962 }
3963 tg3_writephy(tp, MII_BMCR, bmcr);
3964 udelay(40);
3965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 }
3967}
3968
3969static int tg3_init_5401phy_dsp(struct tg3 *tp)
3970{
3971 int err;
3972
3973 /* Turn off tap power management. */
3974 /* Set Extended packet length bit */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003975 err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00003977 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
3978 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
3979 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
3980 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
3981 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982
3983 udelay(40);
3984
3985 return err;
3986}
3987
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003988static bool tg3_phy_copper_an_config_ok(struct tg3 *tp, u32 *lcladv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989{
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003990 u32 advmsk, tgtadv, advertising;
Michael Chan3600d912006-12-07 00:21:48 -08003991
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003992 advertising = tp->link_config.advertising;
3993 tgtadv = ethtool_adv_to_mii_adv_t(advertising) & ADVERTISE_ALL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003995 advmsk = ADVERTISE_ALL;
3996 if (tp->link_config.active_duplex == DUPLEX_FULL) {
Matt Carlsonf88788f2011-12-14 11:10:00 +00003997 tgtadv |= mii_advertise_flowctrl(tp->link_config.flowctrl);
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003998 advmsk |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
3999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004001 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
4002 return false;
4003
4004 if ((*lcladv & advmsk) != tgtadv)
4005 return false;
Matt Carlsonb99d2a52011-08-31 11:44:47 +00004006
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004007 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 u32 tg3_ctrl;
4009
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004010 tgtadv = ethtool_adv_to_mii_ctrl1000_t(advertising);
Michael Chan3600d912006-12-07 00:21:48 -08004011
Matt Carlson221c5632011-06-13 13:39:01 +00004012 if (tg3_readphy(tp, MII_CTRL1000, &tg3_ctrl))
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004013 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014
Matt Carlson3198e072012-02-13 15:20:10 +00004015 if (tgtadv &&
4016 (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
4017 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)) {
4018 tgtadv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
4019 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL |
4020 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
4021 } else {
4022 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL);
4023 }
4024
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004025 if (tg3_ctrl != tgtadv)
4026 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 }
Matt Carlson93a700a2011-08-31 11:44:54 +00004028
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004029 return true;
Matt Carlsonef167e22007-12-20 20:10:01 -08004030}
4031
Matt Carlson859edb22011-12-08 14:40:16 +00004032static bool tg3_phy_copper_fetch_rmtadv(struct tg3 *tp, u32 *rmtadv)
4033{
4034 u32 lpeth = 0;
4035
4036 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4037 u32 val;
4038
4039 if (tg3_readphy(tp, MII_STAT1000, &val))
4040 return false;
4041
4042 lpeth = mii_stat1000_to_ethtool_lpa_t(val);
4043 }
4044
4045 if (tg3_readphy(tp, MII_LPA, rmtadv))
4046 return false;
4047
4048 lpeth |= mii_lpa_to_ethtool_lpa_t(*rmtadv);
4049 tp->link_config.rmt_adv = lpeth;
4050
4051 return true;
4052}
4053
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
4055{
4056 int current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004057 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08004058 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 u16 current_speed;
4060 u8 current_duplex;
4061 int i, err;
4062
4063 tw32(MAC_EVENT, 0);
4064
4065 tw32_f(MAC_STATUS,
4066 (MAC_STATUS_SYNC_CHANGED |
4067 MAC_STATUS_CFG_CHANGED |
4068 MAC_STATUS_MI_COMPLETION |
4069 MAC_STATUS_LNKSTATE_CHANGED));
4070 udelay(40);
4071
Matt Carlson8ef21422008-05-02 16:47:53 -07004072 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
4073 tw32_f(MAC_MI_MODE,
4074 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
4075 udelay(80);
4076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004078 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079
4080 /* Some third-party PHYs need to be reset on link going
4081 * down.
4082 */
4083 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
4084 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
4085 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
4086 netif_carrier_ok(tp->dev)) {
4087 tg3_readphy(tp, MII_BMSR, &bmsr);
4088 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4089 !(bmsr & BMSR_LSTATUS))
4090 force_reset = 1;
4091 }
4092 if (force_reset)
4093 tg3_phy_reset(tp);
4094
Matt Carlson79eb6902010-02-17 15:17:03 +00004095 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 tg3_readphy(tp, MII_BMSR, &bmsr);
4097 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
Joe Perches63c3a662011-04-26 08:12:10 +00004098 !tg3_flag(tp, INIT_COMPLETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 bmsr = 0;
4100
4101 if (!(bmsr & BMSR_LSTATUS)) {
4102 err = tg3_init_5401phy_dsp(tp);
4103 if (err)
4104 return err;
4105
4106 tg3_readphy(tp, MII_BMSR, &bmsr);
4107 for (i = 0; i < 1000; i++) {
4108 udelay(10);
4109 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4110 (bmsr & BMSR_LSTATUS)) {
4111 udelay(40);
4112 break;
4113 }
4114 }
4115
Matt Carlson79eb6902010-02-17 15:17:03 +00004116 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
4117 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 !(bmsr & BMSR_LSTATUS) &&
4119 tp->link_config.active_speed == SPEED_1000) {
4120 err = tg3_phy_reset(tp);
4121 if (!err)
4122 err = tg3_init_5401phy_dsp(tp);
4123 if (err)
4124 return err;
4125 }
4126 }
4127 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
4128 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
4129 /* 5701 {A0,B0} CRC bug workaround */
4130 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004131 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
4132 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
4133 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 }
4135
4136 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004137 tg3_readphy(tp, MII_TG3_ISTAT, &val);
4138 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004140 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004142 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 tg3_writephy(tp, MII_TG3_IMASK, ~0);
4144
4145 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
4146 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
4147 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
4148 tg3_writephy(tp, MII_TG3_EXT_CTRL,
4149 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
4150 else
4151 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
4152 }
4153
4154 current_link_up = 0;
Matt Carlsone7405222012-02-13 15:20:16 +00004155 current_speed = SPEED_UNKNOWN;
4156 current_duplex = DUPLEX_UNKNOWN;
Matt Carlsone348c5e2011-11-21 15:01:20 +00004157 tp->phy_flags &= ~TG3_PHYFLG_MDIX_STATE;
Matt Carlson859edb22011-12-08 14:40:16 +00004158 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004160 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Matt Carlson15ee95c2011-04-20 07:57:40 +00004161 err = tg3_phy_auxctl_read(tp,
4162 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4163 &val);
4164 if (!err && !(val & (1 << 10))) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004165 tg3_phy_auxctl_write(tp,
4166 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4167 val | (1 << 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 goto relink;
4169 }
4170 }
4171
4172 bmsr = 0;
4173 for (i = 0; i < 100; i++) {
4174 tg3_readphy(tp, MII_BMSR, &bmsr);
4175 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4176 (bmsr & BMSR_LSTATUS))
4177 break;
4178 udelay(40);
4179 }
4180
4181 if (bmsr & BMSR_LSTATUS) {
4182 u32 aux_stat, bmcr;
4183
4184 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
4185 for (i = 0; i < 2000; i++) {
4186 udelay(10);
4187 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
4188 aux_stat)
4189 break;
4190 }
4191
4192 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
4193 &current_speed,
4194 &current_duplex);
4195
4196 bmcr = 0;
4197 for (i = 0; i < 200; i++) {
4198 tg3_readphy(tp, MII_BMCR, &bmcr);
4199 if (tg3_readphy(tp, MII_BMCR, &bmcr))
4200 continue;
4201 if (bmcr && bmcr != 0x7fff)
4202 break;
4203 udelay(10);
4204 }
4205
Matt Carlsonef167e22007-12-20 20:10:01 -08004206 lcl_adv = 0;
4207 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208
Matt Carlsonef167e22007-12-20 20:10:01 -08004209 tp->link_config.active_speed = current_speed;
4210 tp->link_config.active_duplex = current_duplex;
4211
4212 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4213 if ((bmcr & BMCR_ANENABLE) &&
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004214 tg3_phy_copper_an_config_ok(tp, &lcl_adv) &&
Matt Carlson859edb22011-12-08 14:40:16 +00004215 tg3_phy_copper_fetch_rmtadv(tp, &rmt_adv))
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004216 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 } else {
4218 if (!(bmcr & BMCR_ANENABLE) &&
4219 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08004220 tp->link_config.duplex == current_duplex &&
4221 tp->link_config.flowctrl ==
4222 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 }
4225 }
4226
Matt Carlsonef167e22007-12-20 20:10:01 -08004227 if (current_link_up == 1 &&
Matt Carlsone348c5e2011-11-21 15:01:20 +00004228 tp->link_config.active_duplex == DUPLEX_FULL) {
4229 u32 reg, bit;
4230
4231 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
4232 reg = MII_TG3_FET_GEN_STAT;
4233 bit = MII_TG3_FET_GEN_STAT_MDIXSTAT;
4234 } else {
4235 reg = MII_TG3_EXT_STAT;
4236 bit = MII_TG3_EXT_STAT_MDIX;
4237 }
4238
4239 if (!tg3_readphy(tp, reg, &val) && (val & bit))
4240 tp->phy_flags |= TG3_PHYFLG_MDIX_STATE;
4241
Matt Carlsonef167e22007-12-20 20:10:01 -08004242 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Matt Carlsone348c5e2011-11-21 15:01:20 +00004243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244 }
4245
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246relink:
Matt Carlson800960682010-08-02 11:26:06 +00004247 if (current_link_up == 0 || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 tg3_phy_copper_begin(tp);
4249
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004250 tg3_readphy(tp, MII_BMSR, &bmsr);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00004251 if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
4252 (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 current_link_up = 1;
4254 }
4255
4256 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
4257 if (current_link_up == 1) {
4258 if (tp->link_config.active_speed == SPEED_100 ||
4259 tp->link_config.active_speed == SPEED_10)
4260 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4261 else
4262 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004263 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00004264 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4265 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4267
4268 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4269 if (tp->link_config.active_duplex == DUPLEX_HALF)
4270 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4271
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004273 if (current_link_up == 1 &&
4274 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004276 else
4277 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 }
4279
4280 /* ??? Without this setting Netgear GA302T PHY does not
4281 * ??? send/receive packets...
4282 */
Matt Carlson79eb6902010-02-17 15:17:03 +00004283 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
4285 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
4286 tw32_f(MAC_MI_MODE, tp->mi_mode);
4287 udelay(80);
4288 }
4289
4290 tw32_f(MAC_MODE, tp->mac_mode);
4291 udelay(40);
4292
Matt Carlson52b02d02010-10-14 10:37:41 +00004293 tg3_phy_eee_adjust(tp, current_link_up);
4294
Joe Perches63c3a662011-04-26 08:12:10 +00004295 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 /* Polled via timer. */
4297 tw32_f(MAC_EVENT, 0);
4298 } else {
4299 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4300 }
4301 udelay(40);
4302
4303 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
4304 current_link_up == 1 &&
4305 tp->link_config.active_speed == SPEED_1000 &&
Joe Perches63c3a662011-04-26 08:12:10 +00004306 (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 udelay(120);
4308 tw32_f(MAC_STATUS,
4309 (MAC_STATUS_SYNC_CHANGED |
4310 MAC_STATUS_CFG_CHANGED));
4311 udelay(40);
4312 tg3_write_mem(tp,
4313 NIC_SRAM_FIRMWARE_MBOX,
4314 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
4315 }
4316
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004317 /* Prevent send BD corruption. */
Joe Perches63c3a662011-04-26 08:12:10 +00004318 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004319 u16 oldlnkctl, newlnkctl;
4320
4321 pci_read_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00004322 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004323 &oldlnkctl);
4324 if (tp->link_config.active_speed == SPEED_100 ||
4325 tp->link_config.active_speed == SPEED_10)
4326 newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
4327 else
4328 newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
4329 if (newlnkctl != oldlnkctl)
4330 pci_write_config_word(tp->pdev,
Matt Carlson93a700a2011-08-31 11:44:54 +00004331 pci_pcie_cap(tp->pdev) +
4332 PCI_EXP_LNKCTL, newlnkctl);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004333 }
4334
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 if (current_link_up != netif_carrier_ok(tp->dev)) {
4336 if (current_link_up)
4337 netif_carrier_on(tp->dev);
4338 else
4339 netif_carrier_off(tp->dev);
4340 tg3_link_report(tp);
4341 }
4342
4343 return 0;
4344}
4345
4346struct tg3_fiber_aneginfo {
4347 int state;
4348#define ANEG_STATE_UNKNOWN 0
4349#define ANEG_STATE_AN_ENABLE 1
4350#define ANEG_STATE_RESTART_INIT 2
4351#define ANEG_STATE_RESTART 3
4352#define ANEG_STATE_DISABLE_LINK_OK 4
4353#define ANEG_STATE_ABILITY_DETECT_INIT 5
4354#define ANEG_STATE_ABILITY_DETECT 6
4355#define ANEG_STATE_ACK_DETECT_INIT 7
4356#define ANEG_STATE_ACK_DETECT 8
4357#define ANEG_STATE_COMPLETE_ACK_INIT 9
4358#define ANEG_STATE_COMPLETE_ACK 10
4359#define ANEG_STATE_IDLE_DETECT_INIT 11
4360#define ANEG_STATE_IDLE_DETECT 12
4361#define ANEG_STATE_LINK_OK 13
4362#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
4363#define ANEG_STATE_NEXT_PAGE_WAIT 15
4364
4365 u32 flags;
4366#define MR_AN_ENABLE 0x00000001
4367#define MR_RESTART_AN 0x00000002
4368#define MR_AN_COMPLETE 0x00000004
4369#define MR_PAGE_RX 0x00000008
4370#define MR_NP_LOADED 0x00000010
4371#define MR_TOGGLE_TX 0x00000020
4372#define MR_LP_ADV_FULL_DUPLEX 0x00000040
4373#define MR_LP_ADV_HALF_DUPLEX 0x00000080
4374#define MR_LP_ADV_SYM_PAUSE 0x00000100
4375#define MR_LP_ADV_ASYM_PAUSE 0x00000200
4376#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
4377#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
4378#define MR_LP_ADV_NEXT_PAGE 0x00001000
4379#define MR_TOGGLE_RX 0x00002000
4380#define MR_NP_RX 0x00004000
4381
4382#define MR_LINK_OK 0x80000000
4383
4384 unsigned long link_time, cur_time;
4385
4386 u32 ability_match_cfg;
4387 int ability_match_count;
4388
4389 char ability_match, idle_match, ack_match;
4390
4391 u32 txconfig, rxconfig;
4392#define ANEG_CFG_NP 0x00000080
4393#define ANEG_CFG_ACK 0x00000040
4394#define ANEG_CFG_RF2 0x00000020
4395#define ANEG_CFG_RF1 0x00000010
4396#define ANEG_CFG_PS2 0x00000001
4397#define ANEG_CFG_PS1 0x00008000
4398#define ANEG_CFG_HD 0x00004000
4399#define ANEG_CFG_FD 0x00002000
4400#define ANEG_CFG_INVAL 0x00001f06
4401
4402};
4403#define ANEG_OK 0
4404#define ANEG_DONE 1
4405#define ANEG_TIMER_ENAB 2
4406#define ANEG_FAILED -1
4407
4408#define ANEG_STATE_SETTLE_TIME 10000
4409
4410static int tg3_fiber_aneg_smachine(struct tg3 *tp,
4411 struct tg3_fiber_aneginfo *ap)
4412{
Matt Carlson5be73b42007-12-20 20:09:29 -08004413 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 unsigned long delta;
4415 u32 rx_cfg_reg;
4416 int ret;
4417
4418 if (ap->state == ANEG_STATE_UNKNOWN) {
4419 ap->rxconfig = 0;
4420 ap->link_time = 0;
4421 ap->cur_time = 0;
4422 ap->ability_match_cfg = 0;
4423 ap->ability_match_count = 0;
4424 ap->ability_match = 0;
4425 ap->idle_match = 0;
4426 ap->ack_match = 0;
4427 }
4428 ap->cur_time++;
4429
4430 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
4431 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
4432
4433 if (rx_cfg_reg != ap->ability_match_cfg) {
4434 ap->ability_match_cfg = rx_cfg_reg;
4435 ap->ability_match = 0;
4436 ap->ability_match_count = 0;
4437 } else {
4438 if (++ap->ability_match_count > 1) {
4439 ap->ability_match = 1;
4440 ap->ability_match_cfg = rx_cfg_reg;
4441 }
4442 }
4443 if (rx_cfg_reg & ANEG_CFG_ACK)
4444 ap->ack_match = 1;
4445 else
4446 ap->ack_match = 0;
4447
4448 ap->idle_match = 0;
4449 } else {
4450 ap->idle_match = 1;
4451 ap->ability_match_cfg = 0;
4452 ap->ability_match_count = 0;
4453 ap->ability_match = 0;
4454 ap->ack_match = 0;
4455
4456 rx_cfg_reg = 0;
4457 }
4458
4459 ap->rxconfig = rx_cfg_reg;
4460 ret = ANEG_OK;
4461
Matt Carlson33f401a2010-04-05 10:19:27 +00004462 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 case ANEG_STATE_UNKNOWN:
4464 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
4465 ap->state = ANEG_STATE_AN_ENABLE;
4466
4467 /* fallthru */
4468 case ANEG_STATE_AN_ENABLE:
4469 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
4470 if (ap->flags & MR_AN_ENABLE) {
4471 ap->link_time = 0;
4472 ap->cur_time = 0;
4473 ap->ability_match_cfg = 0;
4474 ap->ability_match_count = 0;
4475 ap->ability_match = 0;
4476 ap->idle_match = 0;
4477 ap->ack_match = 0;
4478
4479 ap->state = ANEG_STATE_RESTART_INIT;
4480 } else {
4481 ap->state = ANEG_STATE_DISABLE_LINK_OK;
4482 }
4483 break;
4484
4485 case ANEG_STATE_RESTART_INIT:
4486 ap->link_time = ap->cur_time;
4487 ap->flags &= ~(MR_NP_LOADED);
4488 ap->txconfig = 0;
4489 tw32(MAC_TX_AUTO_NEG, 0);
4490 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4491 tw32_f(MAC_MODE, tp->mac_mode);
4492 udelay(40);
4493
4494 ret = ANEG_TIMER_ENAB;
4495 ap->state = ANEG_STATE_RESTART;
4496
4497 /* fallthru */
4498 case ANEG_STATE_RESTART:
4499 delta = ap->cur_time - ap->link_time;
Matt Carlson859a588792010-04-05 10:19:28 +00004500 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a588792010-04-05 10:19:28 +00004502 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504 break;
4505
4506 case ANEG_STATE_DISABLE_LINK_OK:
4507 ret = ANEG_DONE;
4508 break;
4509
4510 case ANEG_STATE_ABILITY_DETECT_INIT:
4511 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08004512 ap->txconfig = ANEG_CFG_FD;
4513 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
4514 if (flowctrl & ADVERTISE_1000XPAUSE)
4515 ap->txconfig |= ANEG_CFG_PS1;
4516 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
4517 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
4519 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4520 tw32_f(MAC_MODE, tp->mac_mode);
4521 udelay(40);
4522
4523 ap->state = ANEG_STATE_ABILITY_DETECT;
4524 break;
4525
4526 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a588792010-04-05 10:19:28 +00004527 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 break;
4530
4531 case ANEG_STATE_ACK_DETECT_INIT:
4532 ap->txconfig |= ANEG_CFG_ACK;
4533 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
4534 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4535 tw32_f(MAC_MODE, tp->mac_mode);
4536 udelay(40);
4537
4538 ap->state = ANEG_STATE_ACK_DETECT;
4539
4540 /* fallthru */
4541 case ANEG_STATE_ACK_DETECT:
4542 if (ap->ack_match != 0) {
4543 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
4544 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
4545 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
4546 } else {
4547 ap->state = ANEG_STATE_AN_ENABLE;
4548 }
4549 } else if (ap->ability_match != 0 &&
4550 ap->rxconfig == 0) {
4551 ap->state = ANEG_STATE_AN_ENABLE;
4552 }
4553 break;
4554
4555 case ANEG_STATE_COMPLETE_ACK_INIT:
4556 if (ap->rxconfig & ANEG_CFG_INVAL) {
4557 ret = ANEG_FAILED;
4558 break;
4559 }
4560 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
4561 MR_LP_ADV_HALF_DUPLEX |
4562 MR_LP_ADV_SYM_PAUSE |
4563 MR_LP_ADV_ASYM_PAUSE |
4564 MR_LP_ADV_REMOTE_FAULT1 |
4565 MR_LP_ADV_REMOTE_FAULT2 |
4566 MR_LP_ADV_NEXT_PAGE |
4567 MR_TOGGLE_RX |
4568 MR_NP_RX);
4569 if (ap->rxconfig & ANEG_CFG_FD)
4570 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
4571 if (ap->rxconfig & ANEG_CFG_HD)
4572 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
4573 if (ap->rxconfig & ANEG_CFG_PS1)
4574 ap->flags |= MR_LP_ADV_SYM_PAUSE;
4575 if (ap->rxconfig & ANEG_CFG_PS2)
4576 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
4577 if (ap->rxconfig & ANEG_CFG_RF1)
4578 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
4579 if (ap->rxconfig & ANEG_CFG_RF2)
4580 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
4581 if (ap->rxconfig & ANEG_CFG_NP)
4582 ap->flags |= MR_LP_ADV_NEXT_PAGE;
4583
4584 ap->link_time = ap->cur_time;
4585
4586 ap->flags ^= (MR_TOGGLE_TX);
4587 if (ap->rxconfig & 0x0008)
4588 ap->flags |= MR_TOGGLE_RX;
4589 if (ap->rxconfig & ANEG_CFG_NP)
4590 ap->flags |= MR_NP_RX;
4591 ap->flags |= MR_PAGE_RX;
4592
4593 ap->state = ANEG_STATE_COMPLETE_ACK;
4594 ret = ANEG_TIMER_ENAB;
4595 break;
4596
4597 case ANEG_STATE_COMPLETE_ACK:
4598 if (ap->ability_match != 0 &&
4599 ap->rxconfig == 0) {
4600 ap->state = ANEG_STATE_AN_ENABLE;
4601 break;
4602 }
4603 delta = ap->cur_time - ap->link_time;
4604 if (delta > ANEG_STATE_SETTLE_TIME) {
4605 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
4606 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
4607 } else {
4608 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
4609 !(ap->flags & MR_NP_RX)) {
4610 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
4611 } else {
4612 ret = ANEG_FAILED;
4613 }
4614 }
4615 }
4616 break;
4617
4618 case ANEG_STATE_IDLE_DETECT_INIT:
4619 ap->link_time = ap->cur_time;
4620 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
4621 tw32_f(MAC_MODE, tp->mac_mode);
4622 udelay(40);
4623
4624 ap->state = ANEG_STATE_IDLE_DETECT;
4625 ret = ANEG_TIMER_ENAB;
4626 break;
4627
4628 case ANEG_STATE_IDLE_DETECT:
4629 if (ap->ability_match != 0 &&
4630 ap->rxconfig == 0) {
4631 ap->state = ANEG_STATE_AN_ENABLE;
4632 break;
4633 }
4634 delta = ap->cur_time - ap->link_time;
4635 if (delta > ANEG_STATE_SETTLE_TIME) {
4636 /* XXX another gem from the Broadcom driver :( */
4637 ap->state = ANEG_STATE_LINK_OK;
4638 }
4639 break;
4640
4641 case ANEG_STATE_LINK_OK:
4642 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
4643 ret = ANEG_DONE;
4644 break;
4645
4646 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
4647 /* ??? unimplemented */
4648 break;
4649
4650 case ANEG_STATE_NEXT_PAGE_WAIT:
4651 /* ??? unimplemented */
4652 break;
4653
4654 default:
4655 ret = ANEG_FAILED;
4656 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658
4659 return ret;
4660}
4661
Matt Carlson5be73b42007-12-20 20:09:29 -08004662static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663{
4664 int res = 0;
4665 struct tg3_fiber_aneginfo aninfo;
4666 int status = ANEG_FAILED;
4667 unsigned int tick;
4668 u32 tmp;
4669
4670 tw32_f(MAC_TX_AUTO_NEG, 0);
4671
4672 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
4673 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
4674 udelay(40);
4675
4676 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
4677 udelay(40);
4678
4679 memset(&aninfo, 0, sizeof(aninfo));
4680 aninfo.flags |= MR_AN_ENABLE;
4681 aninfo.state = ANEG_STATE_UNKNOWN;
4682 aninfo.cur_time = 0;
4683 tick = 0;
4684 while (++tick < 195000) {
4685 status = tg3_fiber_aneg_smachine(tp, &aninfo);
4686 if (status == ANEG_DONE || status == ANEG_FAILED)
4687 break;
4688
4689 udelay(1);
4690 }
4691
4692 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
4693 tw32_f(MAC_MODE, tp->mac_mode);
4694 udelay(40);
4695
Matt Carlson5be73b42007-12-20 20:09:29 -08004696 *txflags = aninfo.txconfig;
4697 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698
4699 if (status == ANEG_DONE &&
4700 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
4701 MR_LP_ADV_FULL_DUPLEX)))
4702 res = 1;
4703
4704 return res;
4705}
4706
4707static void tg3_init_bcm8002(struct tg3 *tp)
4708{
4709 u32 mac_status = tr32(MAC_STATUS);
4710 int i;
4711
4712 /* Reset when initting first time or we have a link. */
Joe Perches63c3a662011-04-26 08:12:10 +00004713 if (tg3_flag(tp, INIT_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714 !(mac_status & MAC_STATUS_PCS_SYNCED))
4715 return;
4716
4717 /* Set PLL lock range. */
4718 tg3_writephy(tp, 0x16, 0x8007);
4719
4720 /* SW reset */
4721 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
4722
4723 /* Wait for reset to complete. */
4724 /* XXX schedule_timeout() ... */
4725 for (i = 0; i < 500; i++)
4726 udelay(10);
4727
4728 /* Config mode; select PMA/Ch 1 regs. */
4729 tg3_writephy(tp, 0x10, 0x8411);
4730
4731 /* Enable auto-lock and comdet, select txclk for tx. */
4732 tg3_writephy(tp, 0x11, 0x0a10);
4733
4734 tg3_writephy(tp, 0x18, 0x00a0);
4735 tg3_writephy(tp, 0x16, 0x41ff);
4736
4737 /* Assert and deassert POR. */
4738 tg3_writephy(tp, 0x13, 0x0400);
4739 udelay(40);
4740 tg3_writephy(tp, 0x13, 0x0000);
4741
4742 tg3_writephy(tp, 0x11, 0x0a50);
4743 udelay(40);
4744 tg3_writephy(tp, 0x11, 0x0a10);
4745
4746 /* Wait for signal to stabilize */
4747 /* XXX schedule_timeout() ... */
4748 for (i = 0; i < 15000; i++)
4749 udelay(10);
4750
4751 /* Deselect the channel register so we can read the PHYID
4752 * later.
4753 */
4754 tg3_writephy(tp, 0x10, 0x8011);
4755}
4756
4757static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
4758{
Matt Carlson82cd3d12007-12-20 20:09:00 -08004759 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 u32 sg_dig_ctrl, sg_dig_status;
4761 u32 serdes_cfg, expected_sg_dig_ctrl;
4762 int workaround, port_a;
4763 int current_link_up;
4764
4765 serdes_cfg = 0;
4766 expected_sg_dig_ctrl = 0;
4767 workaround = 0;
4768 port_a = 1;
4769 current_link_up = 0;
4770
4771 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
4772 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
4773 workaround = 1;
4774 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
4775 port_a = 0;
4776
4777 /* preserve bits 0-11,13,14 for signal pre-emphasis */
4778 /* preserve bits 20-23 for voltage regulator */
4779 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
4780 }
4781
4782 sg_dig_ctrl = tr32(SG_DIG_CTRL);
4783
4784 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004785 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786 if (workaround) {
4787 u32 val = serdes_cfg;
4788
4789 if (port_a)
4790 val |= 0xc010000;
4791 else
4792 val |= 0x4010000;
4793 tw32_f(MAC_SERDES_CFG, val);
4794 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004795
4796 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797 }
4798 if (mac_status & MAC_STATUS_PCS_SYNCED) {
4799 tg3_setup_flow_control(tp, 0, 0);
4800 current_link_up = 1;
4801 }
4802 goto out;
4803 }
4804
4805 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004806 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807
Matt Carlson82cd3d12007-12-20 20:09:00 -08004808 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
4809 if (flowctrl & ADVERTISE_1000XPAUSE)
4810 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
4811 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
4812 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813
4814 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004815 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07004816 tp->serdes_counter &&
4817 ((mac_status & (MAC_STATUS_PCS_SYNCED |
4818 MAC_STATUS_RCVD_CFG)) ==
4819 MAC_STATUS_PCS_SYNCED)) {
4820 tp->serdes_counter--;
4821 current_link_up = 1;
4822 goto out;
4823 }
4824restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825 if (workaround)
4826 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004827 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828 udelay(5);
4829 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
4830
Michael Chan3d3ebe72006-09-27 15:59:15 -07004831 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004832 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
4834 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07004835 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836 mac_status = tr32(MAC_STATUS);
4837
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004838 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08004840 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841
Matt Carlson82cd3d12007-12-20 20:09:00 -08004842 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
4843 local_adv |= ADVERTISE_1000XPAUSE;
4844 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
4845 local_adv |= ADVERTISE_1000XPSE_ASYM;
4846
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004847 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08004848 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004849 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08004850 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851
Matt Carlson859edb22011-12-08 14:40:16 +00004852 tp->link_config.rmt_adv =
4853 mii_adv_to_ethtool_adv_x(remote_adv);
4854
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855 tg3_setup_flow_control(tp, local_adv, remote_adv);
4856 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004857 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004858 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004859 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07004860 if (tp->serdes_counter)
4861 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862 else {
4863 if (workaround) {
4864 u32 val = serdes_cfg;
4865
4866 if (port_a)
4867 val |= 0xc010000;
4868 else
4869 val |= 0x4010000;
4870
4871 tw32_f(MAC_SERDES_CFG, val);
4872 }
4873
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004874 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875 udelay(40);
4876
4877 /* Link parallel detection - link is up */
4878 /* only if we have PCS_SYNC and not */
4879 /* receiving config code words */
4880 mac_status = tr32(MAC_STATUS);
4881 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
4882 !(mac_status & MAC_STATUS_RCVD_CFG)) {
4883 tg3_setup_flow_control(tp, 0, 0);
4884 current_link_up = 1;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004885 tp->phy_flags |=
4886 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004887 tp->serdes_counter =
4888 SERDES_PARALLEL_DET_TIMEOUT;
4889 } else
4890 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891 }
4892 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07004893 } else {
4894 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004895 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896 }
4897
4898out:
4899 return current_link_up;
4900}
4901
4902static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
4903{
4904 int current_link_up = 0;
4905
Michael Chan5cf64b8a2007-05-05 12:11:21 -07004906 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908
4909 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08004910 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004912
Matt Carlson5be73b42007-12-20 20:09:29 -08004913 if (fiber_autoneg(tp, &txflags, &rxflags)) {
4914 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915
Matt Carlson5be73b42007-12-20 20:09:29 -08004916 if (txflags & ANEG_CFG_PS1)
4917 local_adv |= ADVERTISE_1000XPAUSE;
4918 if (txflags & ANEG_CFG_PS2)
4919 local_adv |= ADVERTISE_1000XPSE_ASYM;
4920
4921 if (rxflags & MR_LP_ADV_SYM_PAUSE)
4922 remote_adv |= LPA_1000XPAUSE;
4923 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
4924 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925
Matt Carlson859edb22011-12-08 14:40:16 +00004926 tp->link_config.rmt_adv =
4927 mii_adv_to_ethtool_adv_x(remote_adv);
4928
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929 tg3_setup_flow_control(tp, local_adv, remote_adv);
4930
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931 current_link_up = 1;
4932 }
4933 for (i = 0; i < 30; i++) {
4934 udelay(20);
4935 tw32_f(MAC_STATUS,
4936 (MAC_STATUS_SYNC_CHANGED |
4937 MAC_STATUS_CFG_CHANGED));
4938 udelay(40);
4939 if ((tr32(MAC_STATUS) &
4940 (MAC_STATUS_SYNC_CHANGED |
4941 MAC_STATUS_CFG_CHANGED)) == 0)
4942 break;
4943 }
4944
4945 mac_status = tr32(MAC_STATUS);
4946 if (current_link_up == 0 &&
4947 (mac_status & MAC_STATUS_PCS_SYNCED) &&
4948 !(mac_status & MAC_STATUS_RCVD_CFG))
4949 current_link_up = 1;
4950 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08004951 tg3_setup_flow_control(tp, 0, 0);
4952
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953 /* Forcing 1000FD link up. */
4954 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955
4956 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
4957 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004958
4959 tw32_f(MAC_MODE, tp->mac_mode);
4960 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 }
4962
4963out:
4964 return current_link_up;
4965}
4966
4967static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
4968{
4969 u32 orig_pause_cfg;
4970 u16 orig_active_speed;
4971 u8 orig_active_duplex;
4972 u32 mac_status;
4973 int current_link_up;
4974 int i;
4975
Matt Carlson8d018622007-12-20 20:05:44 -08004976 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977 orig_active_speed = tp->link_config.active_speed;
4978 orig_active_duplex = tp->link_config.active_duplex;
4979
Joe Perches63c3a662011-04-26 08:12:10 +00004980 if (!tg3_flag(tp, HW_AUTONEG) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 netif_carrier_ok(tp->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00004982 tg3_flag(tp, INIT_COMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983 mac_status = tr32(MAC_STATUS);
4984 mac_status &= (MAC_STATUS_PCS_SYNCED |
4985 MAC_STATUS_SIGNAL_DET |
4986 MAC_STATUS_CFG_CHANGED |
4987 MAC_STATUS_RCVD_CFG);
4988 if (mac_status == (MAC_STATUS_PCS_SYNCED |
4989 MAC_STATUS_SIGNAL_DET)) {
4990 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4991 MAC_STATUS_CFG_CHANGED));
4992 return 0;
4993 }
4994 }
4995
4996 tw32_f(MAC_TX_AUTO_NEG, 0);
4997
4998 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
4999 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
5000 tw32_f(MAC_MODE, tp->mac_mode);
5001 udelay(40);
5002
Matt Carlson79eb6902010-02-17 15:17:03 +00005003 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004 tg3_init_bcm8002(tp);
5005
5006 /* Enable link change event even when serdes polling. */
5007 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5008 udelay(40);
5009
5010 current_link_up = 0;
Matt Carlson859edb22011-12-08 14:40:16 +00005011 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012 mac_status = tr32(MAC_STATUS);
5013
Joe Perches63c3a662011-04-26 08:12:10 +00005014 if (tg3_flag(tp, HW_AUTONEG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
5016 else
5017 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
5018
Matt Carlson898a56f2009-08-28 14:02:40 +00005019 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00005021 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022
5023 for (i = 0; i < 100; i++) {
5024 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
5025 MAC_STATUS_CFG_CHANGED));
5026 udelay(5);
5027 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07005028 MAC_STATUS_CFG_CHANGED |
5029 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030 break;
5031 }
5032
5033 mac_status = tr32(MAC_STATUS);
5034 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
5035 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005036 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
5037 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038 tw32_f(MAC_MODE, (tp->mac_mode |
5039 MAC_MODE_SEND_CONFIGS));
5040 udelay(1);
5041 tw32_f(MAC_MODE, tp->mac_mode);
5042 }
5043 }
5044
5045 if (current_link_up == 1) {
5046 tp->link_config.active_speed = SPEED_1000;
5047 tp->link_config.active_duplex = DUPLEX_FULL;
5048 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5049 LED_CTRL_LNKLED_OVERRIDE |
5050 LED_CTRL_1000MBPS_ON));
5051 } else {
Matt Carlsone7405222012-02-13 15:20:16 +00005052 tp->link_config.active_speed = SPEED_UNKNOWN;
5053 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5055 LED_CTRL_LNKLED_OVERRIDE |
5056 LED_CTRL_TRAFFIC_OVERRIDE));
5057 }
5058
5059 if (current_link_up != netif_carrier_ok(tp->dev)) {
5060 if (current_link_up)
5061 netif_carrier_on(tp->dev);
5062 else
5063 netif_carrier_off(tp->dev);
5064 tg3_link_report(tp);
5065 } else {
Matt Carlson8d018622007-12-20 20:05:44 -08005066 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067 if (orig_pause_cfg != now_pause_cfg ||
5068 orig_active_speed != tp->link_config.active_speed ||
5069 orig_active_duplex != tp->link_config.active_duplex)
5070 tg3_link_report(tp);
5071 }
5072
5073 return 0;
5074}
5075
Michael Chan747e8f82005-07-25 12:33:22 -07005076static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
5077{
5078 int current_link_up, err = 0;
5079 u32 bmsr, bmcr;
5080 u16 current_speed;
5081 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08005082 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07005083
5084 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5085 tw32_f(MAC_MODE, tp->mac_mode);
5086 udelay(40);
5087
5088 tw32(MAC_EVENT, 0);
5089
5090 tw32_f(MAC_STATUS,
5091 (MAC_STATUS_SYNC_CHANGED |
5092 MAC_STATUS_CFG_CHANGED |
5093 MAC_STATUS_MI_COMPLETION |
5094 MAC_STATUS_LNKSTATE_CHANGED));
5095 udelay(40);
5096
5097 if (force_reset)
5098 tg3_phy_reset(tp);
5099
5100 current_link_up = 0;
Matt Carlsone7405222012-02-13 15:20:16 +00005101 current_speed = SPEED_UNKNOWN;
5102 current_duplex = DUPLEX_UNKNOWN;
Matt Carlson859edb22011-12-08 14:40:16 +00005103 tp->link_config.rmt_adv = 0;
Michael Chan747e8f82005-07-25 12:33:22 -07005104
5105 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5106 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08005107 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
5108 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5109 bmsr |= BMSR_LSTATUS;
5110 else
5111 bmsr &= ~BMSR_LSTATUS;
5112 }
Michael Chan747e8f82005-07-25 12:33:22 -07005113
5114 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
5115
5116 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005117 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005118 /* do nothing, just check for link up at the end */
5119 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson28011cf2011-11-16 18:36:59 -05005120 u32 adv, newadv;
Michael Chan747e8f82005-07-25 12:33:22 -07005121
5122 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
Matt Carlson28011cf2011-11-16 18:36:59 -05005123 newadv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
5124 ADVERTISE_1000XPAUSE |
5125 ADVERTISE_1000XPSE_ASYM |
5126 ADVERTISE_SLCT);
Michael Chan747e8f82005-07-25 12:33:22 -07005127
Matt Carlson28011cf2011-11-16 18:36:59 -05005128 newadv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Matt Carlson37f07022011-11-17 14:30:55 +00005129 newadv |= ethtool_adv_to_mii_adv_x(tp->link_config.advertising);
Michael Chan747e8f82005-07-25 12:33:22 -07005130
Matt Carlson28011cf2011-11-16 18:36:59 -05005131 if ((newadv != adv) || !(bmcr & BMCR_ANENABLE)) {
5132 tg3_writephy(tp, MII_ADVERTISE, newadv);
Michael Chan747e8f82005-07-25 12:33:22 -07005133 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
5134 tg3_writephy(tp, MII_BMCR, bmcr);
5135
5136 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07005137 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005138 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005139
5140 return err;
5141 }
5142 } else {
5143 u32 new_bmcr;
5144
5145 bmcr &= ~BMCR_SPEED1000;
5146 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
5147
5148 if (tp->link_config.duplex == DUPLEX_FULL)
5149 new_bmcr |= BMCR_FULLDPLX;
5150
5151 if (new_bmcr != bmcr) {
5152 /* BMCR_SPEED1000 is a reserved bit that needs
5153 * to be set on write.
5154 */
5155 new_bmcr |= BMCR_SPEED1000;
5156
5157 /* Force a linkdown */
5158 if (netif_carrier_ok(tp->dev)) {
5159 u32 adv;
5160
5161 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
5162 adv &= ~(ADVERTISE_1000XFULL |
5163 ADVERTISE_1000XHALF |
5164 ADVERTISE_SLCT);
5165 tg3_writephy(tp, MII_ADVERTISE, adv);
5166 tg3_writephy(tp, MII_BMCR, bmcr |
5167 BMCR_ANRESTART |
5168 BMCR_ANENABLE);
5169 udelay(10);
5170 netif_carrier_off(tp->dev);
5171 }
5172 tg3_writephy(tp, MII_BMCR, new_bmcr);
5173 bmcr = new_bmcr;
5174 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5175 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08005176 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
5177 ASIC_REV_5714) {
5178 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5179 bmsr |= BMSR_LSTATUS;
5180 else
5181 bmsr &= ~BMSR_LSTATUS;
5182 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005183 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005184 }
5185 }
5186
5187 if (bmsr & BMSR_LSTATUS) {
5188 current_speed = SPEED_1000;
5189 current_link_up = 1;
5190 if (bmcr & BMCR_FULLDPLX)
5191 current_duplex = DUPLEX_FULL;
5192 else
5193 current_duplex = DUPLEX_HALF;
5194
Matt Carlsonef167e22007-12-20 20:10:01 -08005195 local_adv = 0;
5196 remote_adv = 0;
5197
Michael Chan747e8f82005-07-25 12:33:22 -07005198 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08005199 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07005200
5201 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
5202 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
5203 common = local_adv & remote_adv;
5204 if (common & (ADVERTISE_1000XHALF |
5205 ADVERTISE_1000XFULL)) {
5206 if (common & ADVERTISE_1000XFULL)
5207 current_duplex = DUPLEX_FULL;
5208 else
5209 current_duplex = DUPLEX_HALF;
Matt Carlson859edb22011-12-08 14:40:16 +00005210
5211 tp->link_config.rmt_adv =
5212 mii_adv_to_ethtool_adv_x(remote_adv);
Joe Perches63c3a662011-04-26 08:12:10 +00005213 } else if (!tg3_flag(tp, 5780_CLASS)) {
Matt Carlson57d8b882010-06-05 17:24:35 +00005214 /* Link is up via parallel detect */
Matt Carlson859a588792010-04-05 10:19:28 +00005215 } else {
Michael Chan747e8f82005-07-25 12:33:22 -07005216 current_link_up = 0;
Matt Carlson859a588792010-04-05 10:19:28 +00005217 }
Michael Chan747e8f82005-07-25 12:33:22 -07005218 }
5219 }
5220
Matt Carlsonef167e22007-12-20 20:10:01 -08005221 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
5222 tg3_setup_flow_control(tp, local_adv, remote_adv);
5223
Michael Chan747e8f82005-07-25 12:33:22 -07005224 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
5225 if (tp->link_config.active_duplex == DUPLEX_HALF)
5226 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
5227
5228 tw32_f(MAC_MODE, tp->mac_mode);
5229 udelay(40);
5230
5231 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5232
5233 tp->link_config.active_speed = current_speed;
5234 tp->link_config.active_duplex = current_duplex;
5235
5236 if (current_link_up != netif_carrier_ok(tp->dev)) {
5237 if (current_link_up)
5238 netif_carrier_on(tp->dev);
5239 else {
5240 netif_carrier_off(tp->dev);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005241 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005242 }
5243 tg3_link_report(tp);
5244 }
5245 return err;
5246}
5247
5248static void tg3_serdes_parallel_detect(struct tg3 *tp)
5249{
Michael Chan3d3ebe72006-09-27 15:59:15 -07005250 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07005251 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07005252 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07005253 return;
5254 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005255
Michael Chan747e8f82005-07-25 12:33:22 -07005256 if (!netif_carrier_ok(tp->dev) &&
5257 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
5258 u32 bmcr;
5259
5260 tg3_readphy(tp, MII_BMCR, &bmcr);
5261 if (bmcr & BMCR_ANENABLE) {
5262 u32 phy1, phy2;
5263
5264 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005265 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
5266 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07005267
5268 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005269 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5270 MII_TG3_DSP_EXP1_INT_STAT);
5271 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
5272 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005273
5274 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
5275 /* We have signal detect and not receiving
5276 * config code words, link is up by parallel
5277 * detection.
5278 */
5279
5280 bmcr &= ~BMCR_ANENABLE;
5281 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
5282 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005283 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005284 }
5285 }
Matt Carlson859a588792010-04-05 10:19:28 +00005286 } else if (netif_carrier_ok(tp->dev) &&
5287 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005288 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005289 u32 phy2;
5290
5291 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005292 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5293 MII_TG3_DSP_EXP1_INT_STAT);
5294 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005295 if (phy2 & 0x20) {
5296 u32 bmcr;
5297
5298 /* Config code words received, turn on autoneg. */
5299 tg3_readphy(tp, MII_BMCR, &bmcr);
5300 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
5301
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005302 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005303
5304 }
5305 }
5306}
5307
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308static int tg3_setup_phy(struct tg3 *tp, int force_reset)
5309{
Matt Carlsonf2096f92011-04-05 14:22:48 +00005310 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311 int err;
5312
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005313 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005315 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07005316 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a588792010-04-05 10:19:28 +00005317 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319
Matt Carlsonbcb37f62008-11-03 16:52:09 -08005320 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00005321 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08005322
5323 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
5324 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
5325 scale = 65;
5326 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
5327 scale = 6;
5328 else
5329 scale = 12;
5330
5331 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
5332 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
5333 tw32(GRC_MISC_CFG, val);
5334 }
5335
Matt Carlsonf2096f92011-04-05 14:22:48 +00005336 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
5337 (6 << TX_LENGTHS_IPG_SHIFT);
5338 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
5339 val |= tr32(MAC_TX_LENGTHS) &
5340 (TX_LENGTHS_JMB_FRM_LEN_MSK |
5341 TX_LENGTHS_CNT_DWN_VAL_MSK);
5342
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343 if (tp->link_config.active_speed == SPEED_1000 &&
5344 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00005345 tw32(MAC_TX_LENGTHS, val |
5346 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00005348 tw32(MAC_TX_LENGTHS, val |
5349 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350
Joe Perches63c3a662011-04-26 08:12:10 +00005351 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352 if (netif_carrier_ok(tp->dev)) {
5353 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07005354 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355 } else {
5356 tw32(HOSTCC_STAT_COAL_TICKS, 0);
5357 }
5358 }
5359
Joe Perches63c3a662011-04-26 08:12:10 +00005360 if (tg3_flag(tp, ASPM_WORKAROUND)) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00005361 val = tr32(PCIE_PWR_MGMT_THRESH);
Matt Carlson8ed5d972007-05-07 00:25:49 -07005362 if (!netif_carrier_ok(tp->dev))
5363 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
5364 tp->pwrmgmt_thresh;
5365 else
5366 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
5367 tw32(PCIE_PWR_MGMT_THRESH, val);
5368 }
5369
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370 return err;
5371}
5372
Matt Carlson66cfd1b2010-09-30 10:34:30 +00005373static inline int tg3_irq_sync(struct tg3 *tp)
5374{
5375 return tp->irq_sync;
5376}
5377
Matt Carlson97bd8e42011-04-13 11:05:04 +00005378static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
5379{
5380 int i;
5381
5382 dst = (u32 *)((u8 *)dst + off);
5383 for (i = 0; i < len; i += sizeof(u32))
5384 *dst++ = tr32(off + i);
5385}
5386
5387static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
5388{
5389 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
5390 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
5391 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
5392 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
5393 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
5394 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
5395 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
5396 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
5397 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
5398 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
5399 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
5400 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
5401 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
5402 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
5403 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
5404 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
5405 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
5406 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
5407 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
5408
Joe Perches63c3a662011-04-26 08:12:10 +00005409 if (tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson97bd8e42011-04-13 11:05:04 +00005410 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
5411
5412 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
5413 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
5414 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
5415 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
5416 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
5417 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
5418 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
5419 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
5420
Joe Perches63c3a662011-04-26 08:12:10 +00005421 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00005422 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
5423 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
5424 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
5425 }
5426
5427 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
5428 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
5429 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
5430 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
5431 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
5432
Joe Perches63c3a662011-04-26 08:12:10 +00005433 if (tg3_flag(tp, NVRAM))
Matt Carlson97bd8e42011-04-13 11:05:04 +00005434 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
5435}
5436
5437static void tg3_dump_state(struct tg3 *tp)
5438{
5439 int i;
5440 u32 *regs;
5441
5442 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
5443 if (!regs) {
5444 netdev_err(tp->dev, "Failed allocating register dump buffer\n");
5445 return;
5446 }
5447
Joe Perches63c3a662011-04-26 08:12:10 +00005448 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00005449 /* Read up to but not including private PCI registers */
5450 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
5451 regs[i / sizeof(u32)] = tr32(i);
5452 } else
5453 tg3_dump_legacy_regs(tp, regs);
5454
5455 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
5456 if (!regs[i + 0] && !regs[i + 1] &&
5457 !regs[i + 2] && !regs[i + 3])
5458 continue;
5459
5460 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
5461 i * 4,
5462 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
5463 }
5464
5465 kfree(regs);
5466
5467 for (i = 0; i < tp->irq_cnt; i++) {
5468 struct tg3_napi *tnapi = &tp->napi[i];
5469
5470 /* SW status block */
5471 netdev_err(tp->dev,
5472 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
5473 i,
5474 tnapi->hw_status->status,
5475 tnapi->hw_status->status_tag,
5476 tnapi->hw_status->rx_jumbo_consumer,
5477 tnapi->hw_status->rx_consumer,
5478 tnapi->hw_status->rx_mini_consumer,
5479 tnapi->hw_status->idx[0].rx_producer,
5480 tnapi->hw_status->idx[0].tx_consumer);
5481
5482 netdev_err(tp->dev,
5483 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
5484 i,
5485 tnapi->last_tag, tnapi->last_irq_tag,
5486 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
5487 tnapi->rx_rcb_ptr,
5488 tnapi->prodring.rx_std_prod_idx,
5489 tnapi->prodring.rx_std_cons_idx,
5490 tnapi->prodring.rx_jmb_prod_idx,
5491 tnapi->prodring.rx_jmb_cons_idx);
5492 }
5493}
5494
Michael Chandf3e6542006-05-26 17:48:07 -07005495/* This is called whenever we suspect that the system chipset is re-
5496 * ordering the sequence of MMIO to the tx send mailbox. The symptom
5497 * is bogus tx completions. We try to recover by setting the
5498 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
5499 * in the workqueue.
5500 */
5501static void tg3_tx_recover(struct tg3 *tp)
5502{
Joe Perches63c3a662011-04-26 08:12:10 +00005503 BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
Michael Chandf3e6542006-05-26 17:48:07 -07005504 tp->write32_tx_mbox == tg3_write_indirect_mbox);
5505
Matt Carlson5129c3a2010-04-05 10:19:23 +00005506 netdev_warn(tp->dev,
5507 "The system may be re-ordering memory-mapped I/O "
5508 "cycles to the network device, attempting to recover. "
5509 "Please report the problem to the driver maintainer "
5510 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07005511
5512 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00005513 tg3_flag_set(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07005514 spin_unlock(&tp->lock);
5515}
5516
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005517static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07005518{
Matt Carlsonf65aac12010-08-02 11:26:03 +00005519 /* Tell compiler to fetch tx indices from memory. */
5520 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005521 return tnapi->tx_pending -
5522 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07005523}
5524
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525/* Tigon3 never reports partial packet sends. So we do not
5526 * need special logic to handle SKBs that have not had all
5527 * of their frags sent yet, like SunGEM does.
5528 */
Matt Carlson17375d22009-08-28 14:02:18 +00005529static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530{
Matt Carlson17375d22009-08-28 14:02:18 +00005531 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005532 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005533 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005534 struct netdev_queue *txq;
5535 int index = tnapi - tp->napi;
Tom Herbert298376d2011-11-28 16:33:30 +00005536 unsigned int pkts_compl = 0, bytes_compl = 0;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005537
Joe Perches63c3a662011-04-26 08:12:10 +00005538 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005539 index--;
5540
5541 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542
5543 while (sw_idx != hw_idx) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00005544 struct tg3_tx_ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07005546 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547
Michael Chandf3e6542006-05-26 17:48:07 -07005548 if (unlikely(skb == NULL)) {
5549 tg3_tx_recover(tp);
5550 return;
5551 }
5552
Alexander Duyckf4188d82009-12-02 16:48:38 +00005553 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005554 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005555 skb_headlen(skb),
5556 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557
5558 ri->skb = NULL;
5559
Matt Carlsone01ee142011-07-27 14:20:50 +00005560 while (ri->fragmented) {
5561 ri->fragmented = false;
5562 sw_idx = NEXT_TX(sw_idx);
5563 ri = &tnapi->tx_buffers[sw_idx];
5564 }
5565
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566 sw_idx = NEXT_TX(sw_idx);
5567
5568 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005569 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07005570 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
5571 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005572
5573 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005574 dma_unmap_addr(ri, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00005575 skb_frag_size(&skb_shinfo(skb)->frags[i]),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005576 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00005577
5578 while (ri->fragmented) {
5579 ri->fragmented = false;
5580 sw_idx = NEXT_TX(sw_idx);
5581 ri = &tnapi->tx_buffers[sw_idx];
5582 }
5583
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584 sw_idx = NEXT_TX(sw_idx);
5585 }
5586
Tom Herbert298376d2011-11-28 16:33:30 +00005587 pkts_compl++;
5588 bytes_compl += skb->len;
5589
David S. Millerf47c11e2005-06-24 20:18:35 -07005590 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07005591
5592 if (unlikely(tx_bug)) {
5593 tg3_tx_recover(tp);
5594 return;
5595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005596 }
5597
Tom Herbert298376d2011-11-28 16:33:30 +00005598 netdev_completed_queue(tp->dev, pkts_compl, bytes_compl);
5599
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005600 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601
Michael Chan1b2a7202006-08-07 21:46:02 -07005602 /* Need to make the tx_cons update visible to tg3_start_xmit()
5603 * before checking for netif_queue_stopped(). Without the
5604 * memory barrier, there is a small possibility that tg3_start_xmit()
5605 * will miss it and cause the queue to be stopped forever.
5606 */
5607 smp_mb();
5608
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005609 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005610 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005611 __netif_tx_lock(txq, smp_processor_id());
5612 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005613 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005614 netif_tx_wake_queue(txq);
5615 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07005616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617}
5618
Eric Dumazet9205fd92011-11-18 06:47:01 +00005619static void tg3_rx_data_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005620{
Eric Dumazet9205fd92011-11-18 06:47:01 +00005621 if (!ri->data)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005622 return;
5623
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005624 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005625 map_sz, PCI_DMA_FROMDEVICE);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005626 kfree(ri->data);
5627 ri->data = NULL;
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005628}
5629
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630/* Returns size of skb allocated or < 0 on error.
5631 *
5632 * We only need to fill in the address because the other members
5633 * of the RX descriptor are invariant, see tg3_init_rings.
5634 *
5635 * Note the purposeful assymetry of cpu vs. chip accesses. For
5636 * posting buffers we only dirty the first cache line of the RX
5637 * descriptor (containing the address). Whereas for the RX status
5638 * buffers the cpu only reads the last cacheline of the RX descriptor
5639 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
5640 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00005641static int tg3_alloc_rx_data(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Matt Carlsona3896162009-11-13 13:03:44 +00005642 u32 opaque_key, u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643{
5644 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00005645 struct ring_info *map;
Eric Dumazet9205fd92011-11-18 06:47:01 +00005646 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647 dma_addr_t mapping;
Eric Dumazet9205fd92011-11-18 06:47:01 +00005648 int skb_size, data_size, dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650 switch (opaque_key) {
5651 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00005652 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00005653 desc = &tpr->rx_std[dest_idx];
5654 map = &tpr->rx_std_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00005655 data_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656 break;
5657
5658 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00005659 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00005660 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00005661 map = &tpr->rx_jmb_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00005662 data_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663 break;
5664
5665 default:
5666 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668
5669 /* Do not overwrite any of the map or rp information
5670 * until we are sure we can commit to a new buffer.
5671 *
5672 * Callers depend upon this behavior and assume that
5673 * we leave everything unchanged if we fail.
5674 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00005675 skb_size = SKB_DATA_ALIGN(data_size + TG3_RX_OFFSET(tp)) +
5676 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
5677 data = kmalloc(skb_size, GFP_ATOMIC);
5678 if (!data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679 return -ENOMEM;
5680
Eric Dumazet9205fd92011-11-18 06:47:01 +00005681 mapping = pci_map_single(tp->pdev,
5682 data + TG3_RX_OFFSET(tp),
5683 data_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005684 PCI_DMA_FROMDEVICE);
Matt Carlsona21771d2009-11-02 14:25:31 +00005685 if (pci_dma_mapping_error(tp->pdev, mapping)) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00005686 kfree(data);
Matt Carlsona21771d2009-11-02 14:25:31 +00005687 return -EIO;
5688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689
Eric Dumazet9205fd92011-11-18 06:47:01 +00005690 map->data = data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005691 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692
Linus Torvalds1da177e2005-04-16 15:20:36 -07005693 desc->addr_hi = ((u64)mapping >> 32);
5694 desc->addr_lo = ((u64)mapping & 0xffffffff);
5695
Eric Dumazet9205fd92011-11-18 06:47:01 +00005696 return data_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697}
5698
5699/* We only need to move over in the address because the other
5700 * members of the RX descriptor are invariant. See notes above
Eric Dumazet9205fd92011-11-18 06:47:01 +00005701 * tg3_alloc_rx_data for full details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702 */
Matt Carlsona3896162009-11-13 13:03:44 +00005703static void tg3_recycle_rx(struct tg3_napi *tnapi,
5704 struct tg3_rx_prodring_set *dpr,
5705 u32 opaque_key, int src_idx,
5706 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707{
Matt Carlson17375d22009-08-28 14:02:18 +00005708 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
5710 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00005711 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005712 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713
5714 switch (opaque_key) {
5715 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00005716 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00005717 dest_desc = &dpr->rx_std[dest_idx];
5718 dest_map = &dpr->rx_std_buffers[dest_idx];
5719 src_desc = &spr->rx_std[src_idx];
5720 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721 break;
5722
5723 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00005724 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00005725 dest_desc = &dpr->rx_jmb[dest_idx].std;
5726 dest_map = &dpr->rx_jmb_buffers[dest_idx];
5727 src_desc = &spr->rx_jmb[src_idx].std;
5728 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729 break;
5730
5731 default:
5732 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734
Eric Dumazet9205fd92011-11-18 06:47:01 +00005735 dest_map->data = src_map->data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005736 dma_unmap_addr_set(dest_map, mapping,
5737 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738 dest_desc->addr_hi = src_desc->addr_hi;
5739 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00005740
5741 /* Ensure that the update to the skb happens after the physical
5742 * addresses have been transferred to the new BD location.
5743 */
5744 smp_wmb();
5745
Eric Dumazet9205fd92011-11-18 06:47:01 +00005746 src_map->data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747}
5748
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749/* The RX ring scheme is composed of multiple rings which post fresh
5750 * buffers to the chip, and one special ring the chip uses to report
5751 * status back to the host.
5752 *
5753 * The special ring reports the status of received packets to the
5754 * host. The chip does not write into the original descriptor the
5755 * RX buffer was obtained from. The chip simply takes the original
5756 * descriptor as provided by the host, updates the status and length
5757 * field, then writes this into the next status ring entry.
5758 *
5759 * Each ring the host uses to post buffers to the chip is described
5760 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
5761 * it is first placed into the on-chip ram. When the packet's length
5762 * is known, it walks down the TG3_BDINFO entries to select the ring.
5763 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
5764 * which is within the range of the new packet's length is chosen.
5765 *
5766 * The "separate ring for rx status" scheme may sound queer, but it makes
5767 * sense from a cache coherency perspective. If only the host writes
5768 * to the buffer post rings, and only the chip writes to the rx status
5769 * rings, then cache lines never move beyond shared-modified state.
5770 * If both the host and chip were to write into the same ring, cache line
5771 * eviction could occur since both entities want it in an exclusive state.
5772 */
Matt Carlson17375d22009-08-28 14:02:18 +00005773static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774{
Matt Carlson17375d22009-08-28 14:02:18 +00005775 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07005776 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00005777 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00005778 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07005779 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00005781 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005783 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784 /*
5785 * We need to order the read of hw_idx and the read of
5786 * the opaque cookie.
5787 */
5788 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005789 work_mask = 0;
5790 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00005791 std_prod_idx = tpr->rx_std_prod_idx;
5792 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00005794 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00005795 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796 unsigned int len;
5797 struct sk_buff *skb;
5798 dma_addr_t dma_addr;
5799 u32 opaque_key, desc_idx, *post_ptr;
Eric Dumazet9205fd92011-11-18 06:47:01 +00005800 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801
5802 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
5803 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
5804 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005805 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005806 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005807 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00005808 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07005809 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005810 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005811 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005812 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005813 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00005814 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00005815 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005816 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817
5818 work_mask |= opaque_key;
5819
5820 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
5821 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
5822 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00005823 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005824 desc_idx, *post_ptr);
5825 drop_it_no_recycle:
5826 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00005827 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828 goto next_pkt;
5829 }
5830
Eric Dumazet9205fd92011-11-18 06:47:01 +00005831 prefetch(data + TG3_RX_OFFSET(tp));
Matt Carlsonad829262008-11-21 17:16:16 -08005832 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
5833 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005834
Matt Carlsond2757fc2010-04-12 06:58:27 +00005835 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836 int skb_size;
5837
Eric Dumazet9205fd92011-11-18 06:47:01 +00005838 skb_size = tg3_alloc_rx_data(tp, tpr, opaque_key,
Matt Carlsonafc081f2009-11-13 13:03:43 +00005839 *post_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840 if (skb_size < 0)
5841 goto drop_it;
5842
Matt Carlson287be122009-08-28 13:58:46 +00005843 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005844 PCI_DMA_FROMDEVICE);
5845
Eric Dumazet9205fd92011-11-18 06:47:01 +00005846 skb = build_skb(data);
5847 if (!skb) {
5848 kfree(data);
5849 goto drop_it_no_recycle;
5850 }
5851 skb_reserve(skb, TG3_RX_OFFSET(tp));
5852 /* Ensure that the update to the data happens
Matt Carlson61e800c2010-02-17 15:16:54 +00005853 * after the usage of the old DMA mapping.
5854 */
5855 smp_wmb();
5856
Eric Dumazet9205fd92011-11-18 06:47:01 +00005857 ri->data = NULL;
Matt Carlson61e800c2010-02-17 15:16:54 +00005858
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859 } else {
Matt Carlsona3896162009-11-13 13:03:44 +00005860 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005861 desc_idx, *post_ptr);
5862
Eric Dumazet9205fd92011-11-18 06:47:01 +00005863 skb = netdev_alloc_skb(tp->dev,
5864 len + TG3_RAW_IP_ALIGN);
5865 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005866 goto drop_it_no_recycle;
5867
Eric Dumazet9205fd92011-11-18 06:47:01 +00005868 skb_reserve(skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005869 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005870 memcpy(skb->data,
5871 data + TG3_RX_OFFSET(tp),
5872 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005874 }
5875
Eric Dumazet9205fd92011-11-18 06:47:01 +00005876 skb_put(skb, len);
Michał Mirosławdc668912011-04-07 03:35:07 +00005877 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005878 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
5879 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
5880 >> RXD_TCPCSUM_SHIFT) == 0xffff))
5881 skb->ip_summed = CHECKSUM_UNNECESSARY;
5882 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005883 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005884
5885 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00005886
5887 if (len > (tp->dev->mtu + ETH_HLEN) &&
5888 skb->protocol != htons(ETH_P_8021Q)) {
5889 dev_kfree_skb(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00005890 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00005891 }
5892
Matt Carlson9dc7a112010-04-12 06:58:28 +00005893 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00005894 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
5895 __vlan_hwaccel_put_tag(skb,
5896 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00005897
Matt Carlsonbf933c82011-01-25 15:58:49 +00005898 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900 received++;
5901 budget--;
5902
5903next_pkt:
5904 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07005905
5906 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005907 tpr->rx_std_prod_idx = std_prod_idx &
5908 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00005909 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5910 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07005911 work_mask &= ~RXD_OPAQUE_RING_STD;
5912 rx_std_posted = 0;
5913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07005915 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00005916 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07005917
5918 /* Refresh hw_idx to see if there is new work */
5919 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005920 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07005921 rmb();
5922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923 }
5924
5925 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00005926 tnapi->rx_rcb_ptr = sw_idx;
5927 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005928
5929 /* Refill RX ring(s). */
Joe Perches63c3a662011-04-26 08:12:10 +00005930 if (!tg3_flag(tp, ENABLE_RSS)) {
Michael Chan6541b802012-03-04 14:48:14 +00005931 /* Sync BD data before updating mailbox */
5932 wmb();
5933
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005934 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005935 tpr->rx_std_prod_idx = std_prod_idx &
5936 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005937 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5938 tpr->rx_std_prod_idx);
5939 }
5940 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005941 tpr->rx_jmb_prod_idx = jmb_prod_idx &
5942 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005943 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5944 tpr->rx_jmb_prod_idx);
5945 }
5946 mmiowb();
5947 } else if (work_mask) {
5948 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
5949 * updated before the producer indices can be updated.
5950 */
5951 smp_wmb();
5952
Matt Carlson2c49a442010-09-30 10:34:35 +00005953 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
5954 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005955
Matt Carlsone4af1af2010-02-12 14:47:05 +00005956 if (tnapi != &tp->napi[1])
5957 napi_schedule(&tp->napi[1].napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005959
5960 return received;
5961}
5962
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005963static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005964{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005965 /* handle link change and other phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00005966 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005967 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
5968
Linus Torvalds1da177e2005-04-16 15:20:36 -07005969 if (sblk->status & SD_STATUS_LINK_CHG) {
5970 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005971 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07005972 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00005973 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsondd477002008-05-25 23:45:58 -07005974 tw32_f(MAC_STATUS,
5975 (MAC_STATUS_SYNC_CHANGED |
5976 MAC_STATUS_CFG_CHANGED |
5977 MAC_STATUS_MI_COMPLETION |
5978 MAC_STATUS_LNKSTATE_CHANGED));
5979 udelay(40);
5980 } else
5981 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07005982 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005983 }
5984 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005985}
5986
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005987static int tg3_rx_prodring_xfer(struct tg3 *tp,
5988 struct tg3_rx_prodring_set *dpr,
5989 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005990{
5991 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005992 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005993
5994 while (1) {
5995 src_prod_idx = spr->rx_std_prod_idx;
5996
5997 /* Make sure updates to the rx_std_buffers[] entries and the
5998 * standard producer index are seen in the correct order.
5999 */
6000 smp_rmb();
6001
6002 if (spr->rx_std_cons_idx == src_prod_idx)
6003 break;
6004
6005 if (spr->rx_std_cons_idx < src_prod_idx)
6006 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
6007 else
Matt Carlson2c49a442010-09-30 10:34:35 +00006008 cpycnt = tp->rx_std_ring_mask + 1 -
6009 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006010
Matt Carlson2c49a442010-09-30 10:34:35 +00006011 cpycnt = min(cpycnt,
6012 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006013
6014 si = spr->rx_std_cons_idx;
6015 di = dpr->rx_std_prod_idx;
6016
Matt Carlsone92967b2010-02-12 14:47:06 +00006017 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00006018 if (dpr->rx_std_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00006019 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006020 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00006021 break;
6022 }
6023 }
6024
6025 if (!cpycnt)
6026 break;
6027
6028 /* Ensure that updates to the rx_std_buffers ring and the
6029 * shadowed hardware producer ring from tg3_recycle_skb() are
6030 * ordered correctly WRT the skb check above.
6031 */
6032 smp_rmb();
6033
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006034 memcpy(&dpr->rx_std_buffers[di],
6035 &spr->rx_std_buffers[si],
6036 cpycnt * sizeof(struct ring_info));
6037
6038 for (i = 0; i < cpycnt; i++, di++, si++) {
6039 struct tg3_rx_buffer_desc *sbd, *dbd;
6040 sbd = &spr->rx_std[si];
6041 dbd = &dpr->rx_std[di];
6042 dbd->addr_hi = sbd->addr_hi;
6043 dbd->addr_lo = sbd->addr_lo;
6044 }
6045
Matt Carlson2c49a442010-09-30 10:34:35 +00006046 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
6047 tp->rx_std_ring_mask;
6048 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
6049 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006050 }
6051
6052 while (1) {
6053 src_prod_idx = spr->rx_jmb_prod_idx;
6054
6055 /* Make sure updates to the rx_jmb_buffers[] entries and
6056 * the jumbo producer index are seen in the correct order.
6057 */
6058 smp_rmb();
6059
6060 if (spr->rx_jmb_cons_idx == src_prod_idx)
6061 break;
6062
6063 if (spr->rx_jmb_cons_idx < src_prod_idx)
6064 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
6065 else
Matt Carlson2c49a442010-09-30 10:34:35 +00006066 cpycnt = tp->rx_jmb_ring_mask + 1 -
6067 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006068
6069 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00006070 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006071
6072 si = spr->rx_jmb_cons_idx;
6073 di = dpr->rx_jmb_prod_idx;
6074
Matt Carlsone92967b2010-02-12 14:47:06 +00006075 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00006076 if (dpr->rx_jmb_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00006077 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006078 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00006079 break;
6080 }
6081 }
6082
6083 if (!cpycnt)
6084 break;
6085
6086 /* Ensure that updates to the rx_jmb_buffers ring and the
6087 * shadowed hardware producer ring from tg3_recycle_skb() are
6088 * ordered correctly WRT the skb check above.
6089 */
6090 smp_rmb();
6091
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006092 memcpy(&dpr->rx_jmb_buffers[di],
6093 &spr->rx_jmb_buffers[si],
6094 cpycnt * sizeof(struct ring_info));
6095
6096 for (i = 0; i < cpycnt; i++, di++, si++) {
6097 struct tg3_rx_buffer_desc *sbd, *dbd;
6098 sbd = &spr->rx_jmb[si].std;
6099 dbd = &dpr->rx_jmb[di].std;
6100 dbd->addr_hi = sbd->addr_hi;
6101 dbd->addr_lo = sbd->addr_lo;
6102 }
6103
Matt Carlson2c49a442010-09-30 10:34:35 +00006104 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
6105 tp->rx_jmb_ring_mask;
6106 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
6107 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006108 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006109
6110 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006111}
6112
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006113static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
6114{
6115 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006116
6117 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006118 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00006119 tg3_tx(tnapi);
Joe Perches63c3a662011-04-26 08:12:10 +00006120 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Michael Chan4fd7ab52007-10-12 01:39:50 -07006121 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006122 }
6123
Linus Torvalds1da177e2005-04-16 15:20:36 -07006124 /* run RX thread, within the bounds set by NAPI.
6125 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006126 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07006127 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006128 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00006129 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006130
Joe Perches63c3a662011-04-26 08:12:10 +00006131 if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006132 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006133 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006134 u32 std_prod_idx = dpr->rx_std_prod_idx;
6135 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006136
Matt Carlsone4af1af2010-02-12 14:47:05 +00006137 for (i = 1; i < tp->irq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006138 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00006139 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006140
6141 wmb();
6142
Matt Carlsone4af1af2010-02-12 14:47:05 +00006143 if (std_prod_idx != dpr->rx_std_prod_idx)
6144 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6145 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006146
Matt Carlsone4af1af2010-02-12 14:47:05 +00006147 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
6148 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
6149 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006150
6151 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006152
6153 if (err)
6154 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006155 }
6156
David S. Miller6f535762007-10-11 18:08:29 -07006157 return work_done;
6158}
David S. Millerf7383c22005-05-18 22:50:53 -07006159
Matt Carlsondb219972011-11-04 09:15:03 +00006160static inline void tg3_reset_task_schedule(struct tg3 *tp)
6161{
6162 if (!test_and_set_bit(TG3_FLAG_RESET_TASK_PENDING, tp->tg3_flags))
6163 schedule_work(&tp->reset_task);
6164}
6165
6166static inline void tg3_reset_task_cancel(struct tg3 *tp)
6167{
6168 cancel_work_sync(&tp->reset_task);
6169 tg3_flag_clear(tp, RESET_TASK_PENDING);
Matt Carlsonc7101352012-02-22 12:35:20 +00006170 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Matt Carlsondb219972011-11-04 09:15:03 +00006171}
6172
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006173static int tg3_poll_msix(struct napi_struct *napi, int budget)
6174{
6175 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
6176 struct tg3 *tp = tnapi->tp;
6177 int work_done = 0;
6178 struct tg3_hw_status *sblk = tnapi->hw_status;
6179
6180 while (1) {
6181 work_done = tg3_poll_work(tnapi, work_done, budget);
6182
Joe Perches63c3a662011-04-26 08:12:10 +00006183 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006184 goto tx_recovery;
6185
6186 if (unlikely(work_done >= budget))
6187 break;
6188
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006189 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006190 * to tell the hw how much work has been processed,
6191 * so we must read it before checking for more work.
6192 */
6193 tnapi->last_tag = sblk->status_tag;
6194 tnapi->last_irq_tag = tnapi->last_tag;
6195 rmb();
6196
6197 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00006198 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
6199 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006200 napi_complete(napi);
6201 /* Reenable interrupts. */
6202 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
6203 mmiowb();
6204 break;
6205 }
6206 }
6207
6208 return work_done;
6209
6210tx_recovery:
6211 /* work_done is guaranteed to be less than budget. */
6212 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00006213 tg3_reset_task_schedule(tp);
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006214 return work_done;
6215}
6216
Matt Carlsone64de4e2011-04-13 11:05:05 +00006217static void tg3_process_error(struct tg3 *tp)
6218{
6219 u32 val;
6220 bool real_error = false;
6221
Joe Perches63c3a662011-04-26 08:12:10 +00006222 if (tg3_flag(tp, ERROR_PROCESSED))
Matt Carlsone64de4e2011-04-13 11:05:05 +00006223 return;
6224
6225 /* Check Flow Attention register */
6226 val = tr32(HOSTCC_FLOW_ATTN);
6227 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
6228 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
6229 real_error = true;
6230 }
6231
6232 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
6233 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
6234 real_error = true;
6235 }
6236
6237 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
6238 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
6239 real_error = true;
6240 }
6241
6242 if (!real_error)
6243 return;
6244
6245 tg3_dump_state(tp);
6246
Joe Perches63c3a662011-04-26 08:12:10 +00006247 tg3_flag_set(tp, ERROR_PROCESSED);
Matt Carlsondb219972011-11-04 09:15:03 +00006248 tg3_reset_task_schedule(tp);
Matt Carlsone64de4e2011-04-13 11:05:05 +00006249}
6250
David S. Miller6f535762007-10-11 18:08:29 -07006251static int tg3_poll(struct napi_struct *napi, int budget)
6252{
Matt Carlson8ef04422009-08-28 14:01:37 +00006253 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
6254 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07006255 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00006256 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07006257
6258 while (1) {
Matt Carlsone64de4e2011-04-13 11:05:05 +00006259 if (sblk->status & SD_STATUS_ERROR)
6260 tg3_process_error(tp);
6261
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006262 tg3_poll_link(tp);
6263
Matt Carlson17375d22009-08-28 14:02:18 +00006264 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07006265
Joe Perches63c3a662011-04-26 08:12:10 +00006266 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
David S. Miller6f535762007-10-11 18:08:29 -07006267 goto tx_recovery;
6268
6269 if (unlikely(work_done >= budget))
6270 break;
6271
Joe Perches63c3a662011-04-26 08:12:10 +00006272 if (tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson17375d22009-08-28 14:02:18 +00006273 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07006274 * to tell the hw how much work has been processed,
6275 * so we must read it before checking for more work.
6276 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006277 tnapi->last_tag = sblk->status_tag;
6278 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07006279 rmb();
6280 } else
6281 sblk->status &= ~SD_STATUS_UPDATED;
6282
Matt Carlson17375d22009-08-28 14:02:18 +00006283 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08006284 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00006285 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07006286 break;
6287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006288 }
6289
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006290 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07006291
6292tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07006293 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08006294 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00006295 tg3_reset_task_schedule(tp);
Michael Chan4fd7ab52007-10-12 01:39:50 -07006296 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006297}
6298
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006299static void tg3_napi_disable(struct tg3 *tp)
6300{
6301 int i;
6302
6303 for (i = tp->irq_cnt - 1; i >= 0; i--)
6304 napi_disable(&tp->napi[i].napi);
6305}
6306
6307static void tg3_napi_enable(struct tg3 *tp)
6308{
6309 int i;
6310
6311 for (i = 0; i < tp->irq_cnt; i++)
6312 napi_enable(&tp->napi[i].napi);
6313}
6314
6315static void tg3_napi_init(struct tg3 *tp)
6316{
6317 int i;
6318
6319 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
6320 for (i = 1; i < tp->irq_cnt; i++)
6321 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
6322}
6323
6324static void tg3_napi_fini(struct tg3 *tp)
6325{
6326 int i;
6327
6328 for (i = 0; i < tp->irq_cnt; i++)
6329 netif_napi_del(&tp->napi[i].napi);
6330}
6331
6332static inline void tg3_netif_stop(struct tg3 *tp)
6333{
6334 tp->dev->trans_start = jiffies; /* prevent tx timeout */
6335 tg3_napi_disable(tp);
6336 netif_tx_disable(tp->dev);
6337}
6338
6339static inline void tg3_netif_start(struct tg3 *tp)
6340{
6341 /* NOTE: unconditional netif_tx_wake_all_queues is only
6342 * appropriate so long as all callers are assured to
6343 * have free tx slots (such as after tg3_init_hw)
6344 */
6345 netif_tx_wake_all_queues(tp->dev);
6346
6347 tg3_napi_enable(tp);
6348 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
6349 tg3_enable_ints(tp);
6350}
6351
David S. Millerf47c11e2005-06-24 20:18:35 -07006352static void tg3_irq_quiesce(struct tg3 *tp)
6353{
Matt Carlson4f125f42009-09-01 12:55:02 +00006354 int i;
6355
David S. Millerf47c11e2005-06-24 20:18:35 -07006356 BUG_ON(tp->irq_sync);
6357
6358 tp->irq_sync = 1;
6359 smp_mb();
6360
Matt Carlson4f125f42009-09-01 12:55:02 +00006361 for (i = 0; i < tp->irq_cnt; i++)
6362 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07006363}
6364
David S. Millerf47c11e2005-06-24 20:18:35 -07006365/* Fully shutdown all tg3 driver activity elsewhere in the system.
6366 * If irq_sync is non-zero, then the IRQ handler must be synchronized
6367 * with as well. Most of the time, this is not necessary except when
6368 * shutting down the device.
6369 */
6370static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
6371{
Michael Chan46966542007-07-11 19:47:19 -07006372 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07006373 if (irq_sync)
6374 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07006375}
6376
6377static inline void tg3_full_unlock(struct tg3 *tp)
6378{
David S. Millerf47c11e2005-06-24 20:18:35 -07006379 spin_unlock_bh(&tp->lock);
6380}
6381
Michael Chanfcfa0a32006-03-20 22:28:41 -08006382/* One-shot MSI handler - Chip automatically disables interrupt
6383 * after sending MSI so driver doesn't have to do it.
6384 */
David Howells7d12e782006-10-05 14:55:46 +01006385static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08006386{
Matt Carlson09943a12009-08-28 14:01:57 +00006387 struct tg3_napi *tnapi = dev_id;
6388 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08006389
Matt Carlson898a56f2009-08-28 14:02:40 +00006390 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006391 if (tnapi->rx_rcb)
6392 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08006393
6394 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00006395 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08006396
6397 return IRQ_HANDLED;
6398}
6399
Michael Chan88b06bc22005-04-21 17:13:25 -07006400/* MSI ISR - No need to check for interrupt sharing and no need to
6401 * flush status block and interrupt mailbox. PCI ordering rules
6402 * guarantee that MSI will arrive after the status block.
6403 */
David Howells7d12e782006-10-05 14:55:46 +01006404static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07006405{
Matt Carlson09943a12009-08-28 14:01:57 +00006406 struct tg3_napi *tnapi = dev_id;
6407 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07006408
Matt Carlson898a56f2009-08-28 14:02:40 +00006409 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006410 if (tnapi->rx_rcb)
6411 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07006412 /*
David S. Millerfac9b832005-05-18 22:46:34 -07006413 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07006414 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07006415 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07006416 * NIC to stop sending us irqs, engaging "in-intr-handler"
6417 * event coalescing.
6418 */
Matt Carlson5b39de92011-08-31 11:44:50 +00006419 tw32_mailbox(tnapi->int_mbox, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07006420 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00006421 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07006422
Michael Chan88b06bc22005-04-21 17:13:25 -07006423 return IRQ_RETVAL(1);
6424}
6425
David Howells7d12e782006-10-05 14:55:46 +01006426static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006427{
Matt Carlson09943a12009-08-28 14:01:57 +00006428 struct tg3_napi *tnapi = dev_id;
6429 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006430 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006431 unsigned int handled = 1;
6432
Linus Torvalds1da177e2005-04-16 15:20:36 -07006433 /* In INTx mode, it is possible for the interrupt to arrive at
6434 * the CPU before the status block posted prior to the interrupt.
6435 * Reading the PCI State register will confirm whether the
6436 * interrupt is ours and will flush the status block.
6437 */
Michael Chand18edcb2007-03-24 20:57:11 -07006438 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
Joe Perches63c3a662011-04-26 08:12:10 +00006439 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07006440 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
6441 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07006442 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07006443 }
Michael Chand18edcb2007-03-24 20:57:11 -07006444 }
6445
6446 /*
6447 * Writing any value to intr-mbox-0 clears PCI INTA# and
6448 * chip-internal interrupt pending events.
6449 * Writing non-zero to intr-mbox-0 additional tells the
6450 * NIC to stop sending us irqs, engaging "in-intr-handler"
6451 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07006452 *
6453 * Flush the mailbox to de-assert the IRQ immediately to prevent
6454 * spurious interrupts. The flush impacts performance but
6455 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07006456 */
Michael Chanc04cb342007-05-07 00:26:15 -07006457 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07006458 if (tg3_irq_sync(tp))
6459 goto out;
6460 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00006461 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00006462 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00006463 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07006464 } else {
6465 /* No work, shared interrupt perhaps? re-enable
6466 * interrupts, and flush that PCI write
6467 */
6468 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
6469 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07006470 }
David S. Millerf47c11e2005-06-24 20:18:35 -07006471out:
David S. Millerfac9b832005-05-18 22:46:34 -07006472 return IRQ_RETVAL(handled);
6473}
6474
David Howells7d12e782006-10-05 14:55:46 +01006475static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07006476{
Matt Carlson09943a12009-08-28 14:01:57 +00006477 struct tg3_napi *tnapi = dev_id;
6478 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006479 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07006480 unsigned int handled = 1;
6481
David S. Millerfac9b832005-05-18 22:46:34 -07006482 /* In INTx mode, it is possible for the interrupt to arrive at
6483 * the CPU before the status block posted prior to the interrupt.
6484 * Reading the PCI State register will confirm whether the
6485 * interrupt is ours and will flush the status block.
6486 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006487 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Joe Perches63c3a662011-04-26 08:12:10 +00006488 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07006489 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
6490 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07006491 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006492 }
Michael Chand18edcb2007-03-24 20:57:11 -07006493 }
6494
6495 /*
6496 * writing any value to intr-mbox-0 clears PCI INTA# and
6497 * chip-internal interrupt pending events.
6498 * writing non-zero to intr-mbox-0 additional tells the
6499 * NIC to stop sending us irqs, engaging "in-intr-handler"
6500 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07006501 *
6502 * Flush the mailbox to de-assert the IRQ immediately to prevent
6503 * spurious interrupts. The flush impacts performance but
6504 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07006505 */
Michael Chanc04cb342007-05-07 00:26:15 -07006506 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00006507
6508 /*
6509 * In a shared interrupt configuration, sometimes other devices'
6510 * interrupts will scream. We record the current status tag here
6511 * so that the above check can report that the screaming interrupts
6512 * are unhandled. Eventually they will be silenced.
6513 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006514 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00006515
Michael Chand18edcb2007-03-24 20:57:11 -07006516 if (tg3_irq_sync(tp))
6517 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00006518
Matt Carlson72334482009-08-28 14:03:01 +00006519 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00006520
Matt Carlson09943a12009-08-28 14:01:57 +00006521 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00006522
David S. Millerf47c11e2005-06-24 20:18:35 -07006523out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006524 return IRQ_RETVAL(handled);
6525}
6526
Michael Chan79381092005-04-21 17:13:59 -07006527/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01006528static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07006529{
Matt Carlson09943a12009-08-28 14:01:57 +00006530 struct tg3_napi *tnapi = dev_id;
6531 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006532 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07006533
Michael Chanf9804dd2005-09-27 12:13:10 -07006534 if ((sblk->status & SD_STATUS_UPDATED) ||
6535 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07006536 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07006537 return IRQ_RETVAL(1);
6538 }
6539 return IRQ_RETVAL(0);
6540}
6541
Linus Torvalds1da177e2005-04-16 15:20:36 -07006542#ifdef CONFIG_NET_POLL_CONTROLLER
6543static void tg3_poll_controller(struct net_device *dev)
6544{
Matt Carlson4f125f42009-09-01 12:55:02 +00006545 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07006546 struct tg3 *tp = netdev_priv(dev);
6547
Matt Carlson4f125f42009-09-01 12:55:02 +00006548 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00006549 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006550}
6551#endif
6552
Linus Torvalds1da177e2005-04-16 15:20:36 -07006553static void tg3_tx_timeout(struct net_device *dev)
6554{
6555 struct tg3 *tp = netdev_priv(dev);
6556
Michael Chanb0408752007-02-13 12:18:30 -08006557 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00006558 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00006559 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08006560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006561
Matt Carlsondb219972011-11-04 09:15:03 +00006562 tg3_reset_task_schedule(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006563}
6564
Michael Chanc58ec932005-09-17 00:46:27 -07006565/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
6566static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
6567{
6568 u32 base = (u32) mapping & 0xffffffff;
6569
Eric Dumazet807540b2010-09-23 05:40:09 +00006570 return (base > 0xffffdcc0) && (base + len + 8 < base);
Michael Chanc58ec932005-09-17 00:46:27 -07006571}
6572
Michael Chan72f2afb2006-03-06 19:28:35 -08006573/* Test for DMA addresses > 40-bit */
6574static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
6575 int len)
6576{
6577#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Joe Perches63c3a662011-04-26 08:12:10 +00006578 if (tg3_flag(tp, 40BIT_DMA_BUG))
Eric Dumazet807540b2010-09-23 05:40:09 +00006579 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08006580 return 0;
6581#else
6582 return 0;
6583#endif
6584}
6585
Matt Carlsond1a3b732011-07-27 14:20:51 +00006586static inline void tg3_tx_set_bd(struct tg3_tx_buffer_desc *txbd,
Matt Carlson92cd3a12011-07-27 14:20:47 +00006587 dma_addr_t mapping, u32 len, u32 flags,
6588 u32 mss, u32 vlan)
Matt Carlson2ffcc982011-05-19 12:12:44 +00006589{
Matt Carlson92cd3a12011-07-27 14:20:47 +00006590 txbd->addr_hi = ((u64) mapping >> 32);
6591 txbd->addr_lo = ((u64) mapping & 0xffffffff);
6592 txbd->len_flags = (len << TXD_LEN_SHIFT) | (flags & 0x0000ffff);
6593 txbd->vlan_tag = (mss << TXD_MSS_SHIFT) | (vlan << TXD_VLAN_TAG_SHIFT);
Matt Carlson2ffcc982011-05-19 12:12:44 +00006594}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006595
Matt Carlson84b67b22011-07-27 14:20:52 +00006596static bool tg3_tx_frag_set(struct tg3_napi *tnapi, u32 *entry, u32 *budget,
Matt Carlsond1a3b732011-07-27 14:20:51 +00006597 dma_addr_t map, u32 len, u32 flags,
6598 u32 mss, u32 vlan)
6599{
6600 struct tg3 *tp = tnapi->tp;
6601 bool hwbug = false;
6602
6603 if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
Rusty Russell3db1cd52011-12-19 13:56:45 +00006604 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00006605
6606 if (tg3_4g_overflow_test(map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00006607 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00006608
6609 if (tg3_40bit_overflow_test(tp, map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00006610 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00006611
Matt Carlsona4cb4282011-12-14 11:09:58 +00006612 if (tp->dma_limit) {
Matt Carlsonb9e45482011-11-04 09:14:59 +00006613 u32 prvidx = *entry;
Matt Carlsone31aa982011-07-27 14:20:53 +00006614 u32 tmp_flag = flags & ~TXD_FLAG_END;
Matt Carlsona4cb4282011-12-14 11:09:58 +00006615 while (len > tp->dma_limit && *budget) {
6616 u32 frag_len = tp->dma_limit;
6617 len -= tp->dma_limit;
Matt Carlsone31aa982011-07-27 14:20:53 +00006618
Matt Carlsonb9e45482011-11-04 09:14:59 +00006619 /* Avoid the 8byte DMA problem */
6620 if (len <= 8) {
Matt Carlsona4cb4282011-12-14 11:09:58 +00006621 len += tp->dma_limit / 2;
6622 frag_len = tp->dma_limit / 2;
Matt Carlsone31aa982011-07-27 14:20:53 +00006623 }
6624
Matt Carlsonb9e45482011-11-04 09:14:59 +00006625 tnapi->tx_buffers[*entry].fragmented = true;
6626
6627 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6628 frag_len, tmp_flag, mss, vlan);
6629 *budget -= 1;
6630 prvidx = *entry;
6631 *entry = NEXT_TX(*entry);
6632
Matt Carlsone31aa982011-07-27 14:20:53 +00006633 map += frag_len;
6634 }
6635
6636 if (len) {
6637 if (*budget) {
6638 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6639 len, flags, mss, vlan);
Matt Carlsonb9e45482011-11-04 09:14:59 +00006640 *budget -= 1;
Matt Carlsone31aa982011-07-27 14:20:53 +00006641 *entry = NEXT_TX(*entry);
6642 } else {
Rusty Russell3db1cd52011-12-19 13:56:45 +00006643 hwbug = true;
Matt Carlsonb9e45482011-11-04 09:14:59 +00006644 tnapi->tx_buffers[prvidx].fragmented = false;
Matt Carlsone31aa982011-07-27 14:20:53 +00006645 }
6646 }
6647 } else {
Matt Carlson84b67b22011-07-27 14:20:52 +00006648 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6649 len, flags, mss, vlan);
Matt Carlsone31aa982011-07-27 14:20:53 +00006650 *entry = NEXT_TX(*entry);
6651 }
Matt Carlsond1a3b732011-07-27 14:20:51 +00006652
6653 return hwbug;
6654}
6655
Matt Carlson0d681b22011-07-27 14:20:49 +00006656static void tg3_tx_skb_unmap(struct tg3_napi *tnapi, u32 entry, int last)
Matt Carlson432aa7e2011-05-19 12:12:45 +00006657{
6658 int i;
Matt Carlson0d681b22011-07-27 14:20:49 +00006659 struct sk_buff *skb;
Matt Carlsondf8944c2011-07-27 14:20:46 +00006660 struct tg3_tx_ring_info *txb = &tnapi->tx_buffers[entry];
Matt Carlson432aa7e2011-05-19 12:12:45 +00006661
Matt Carlson0d681b22011-07-27 14:20:49 +00006662 skb = txb->skb;
6663 txb->skb = NULL;
6664
Matt Carlson432aa7e2011-05-19 12:12:45 +00006665 pci_unmap_single(tnapi->tp->pdev,
6666 dma_unmap_addr(txb, mapping),
6667 skb_headlen(skb),
6668 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00006669
6670 while (txb->fragmented) {
6671 txb->fragmented = false;
6672 entry = NEXT_TX(entry);
6673 txb = &tnapi->tx_buffers[entry];
6674 }
6675
Matt Carlsonba1142e2011-11-04 09:15:00 +00006676 for (i = 0; i <= last; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00006677 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Matt Carlson432aa7e2011-05-19 12:12:45 +00006678
6679 entry = NEXT_TX(entry);
6680 txb = &tnapi->tx_buffers[entry];
6681
6682 pci_unmap_page(tnapi->tp->pdev,
6683 dma_unmap_addr(txb, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00006684 skb_frag_size(frag), PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00006685
6686 while (txb->fragmented) {
6687 txb->fragmented = false;
6688 entry = NEXT_TX(entry);
6689 txb = &tnapi->tx_buffers[entry];
6690 }
Matt Carlson432aa7e2011-05-19 12:12:45 +00006691 }
6692}
6693
Michael Chan72f2afb2006-03-06 19:28:35 -08006694/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006695static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
David S. Miller1805b2f2011-10-24 18:18:09 -04006696 struct sk_buff **pskb,
Matt Carlson84b67b22011-07-27 14:20:52 +00006697 u32 *entry, u32 *budget,
Matt Carlson92cd3a12011-07-27 14:20:47 +00006698 u32 base_flags, u32 mss, u32 vlan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006699{
Matt Carlson24f4efd2009-11-13 13:03:35 +00006700 struct tg3 *tp = tnapi->tp;
David S. Miller1805b2f2011-10-24 18:18:09 -04006701 struct sk_buff *new_skb, *skb = *pskb;
Michael Chanc58ec932005-09-17 00:46:27 -07006702 dma_addr_t new_addr = 0;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006703 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006704
Matt Carlson41588ba2008-04-19 18:12:33 -07006705 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
6706 new_skb = skb_copy(skb, GFP_ATOMIC);
6707 else {
6708 int more_headroom = 4 - ((unsigned long)skb->data & 3);
6709
6710 new_skb = skb_copy_expand(skb,
6711 skb_headroom(skb) + more_headroom,
6712 skb_tailroom(skb), GFP_ATOMIC);
6713 }
6714
Linus Torvalds1da177e2005-04-16 15:20:36 -07006715 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07006716 ret = -1;
6717 } else {
6718 /* New SKB is guaranteed to be linear. */
Alexander Duyckf4188d82009-12-02 16:48:38 +00006719 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
6720 PCI_DMA_TODEVICE);
6721 /* Make sure the mapping succeeded */
6722 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00006723 dev_kfree_skb(new_skb);
Michael Chanc58ec932005-09-17 00:46:27 -07006724 ret = -1;
Michael Chanc58ec932005-09-17 00:46:27 -07006725 } else {
Matt Carlsonb9e45482011-11-04 09:14:59 +00006726 u32 save_entry = *entry;
6727
Matt Carlson92cd3a12011-07-27 14:20:47 +00006728 base_flags |= TXD_FLAG_END;
6729
Matt Carlson84b67b22011-07-27 14:20:52 +00006730 tnapi->tx_buffers[*entry].skb = new_skb;
6731 dma_unmap_addr_set(&tnapi->tx_buffers[*entry],
Matt Carlson432aa7e2011-05-19 12:12:45 +00006732 mapping, new_addr);
6733
Matt Carlson84b67b22011-07-27 14:20:52 +00006734 if (tg3_tx_frag_set(tnapi, entry, budget, new_addr,
Matt Carlsond1a3b732011-07-27 14:20:51 +00006735 new_skb->len, base_flags,
6736 mss, vlan)) {
Matt Carlsonba1142e2011-11-04 09:15:00 +00006737 tg3_tx_skb_unmap(tnapi, save_entry, -1);
Matt Carlsond1a3b732011-07-27 14:20:51 +00006738 dev_kfree_skb(new_skb);
6739 ret = -1;
6740 }
Michael Chanc58ec932005-09-17 00:46:27 -07006741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006742 }
6743
Linus Torvalds1da177e2005-04-16 15:20:36 -07006744 dev_kfree_skb(skb);
David S. Miller1805b2f2011-10-24 18:18:09 -04006745 *pskb = new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07006746 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006747}
6748
Matt Carlson2ffcc982011-05-19 12:12:44 +00006749static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07006750
6751/* Use GSO to workaround a rare TSO bug that may be triggered when the
6752 * TSO header is greater than 80 bytes.
6753 */
6754static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
6755{
6756 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006757 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07006758
6759 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006760 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07006761 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006762
6763 /* netif_tx_stop_queue() must be done before checking
6764 * checking tx index in tg3_tx_avail() below, because in
6765 * tg3_tx(), we update tx index before checking for
6766 * netif_tx_queue_stopped().
6767 */
6768 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006769 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08006770 return NETDEV_TX_BUSY;
6771
6772 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07006773 }
6774
6775 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07006776 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07006777 goto tg3_tso_bug_end;
6778
6779 do {
6780 nskb = segs;
6781 segs = segs->next;
6782 nskb->next = NULL;
Matt Carlson2ffcc982011-05-19 12:12:44 +00006783 tg3_start_xmit(nskb, tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07006784 } while (segs);
6785
6786tg3_tso_bug_end:
6787 dev_kfree_skb(skb);
6788
6789 return NETDEV_TX_OK;
6790}
Michael Chan52c0fd82006-06-29 20:15:54 -07006791
Michael Chan5a6f3072006-03-20 22:28:05 -08006792/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
Joe Perches63c3a662011-04-26 08:12:10 +00006793 * support TG3_FLAG_HW_TSO_1 or firmware TSO only.
Michael Chan5a6f3072006-03-20 22:28:05 -08006794 */
Matt Carlson2ffcc982011-05-19 12:12:44 +00006795static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08006796{
6797 struct tg3 *tp = netdev_priv(dev);
Matt Carlson92cd3a12011-07-27 14:20:47 +00006798 u32 len, entry, base_flags, mss, vlan = 0;
Matt Carlson84b67b22011-07-27 14:20:52 +00006799 u32 budget;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006800 int i = -1, would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07006801 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00006802 struct tg3_napi *tnapi;
6803 struct netdev_queue *txq;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006804 unsigned int last;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006805
Matt Carlson24f4efd2009-11-13 13:03:35 +00006806 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
6807 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Joe Perches63c3a662011-04-26 08:12:10 +00006808 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006809 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006810
Matt Carlson84b67b22011-07-27 14:20:52 +00006811 budget = tg3_tx_avail(tnapi);
6812
Michael Chan00b70502006-06-17 21:58:45 -07006813 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006814 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07006815 * interrupt. Furthermore, IRQ processing runs lockless so we have
6816 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07006817 */
Matt Carlson84b67b22011-07-27 14:20:52 +00006818 if (unlikely(budget <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006819 if (!netif_tx_queue_stopped(txq)) {
6820 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08006821
6822 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00006823 netdev_err(dev,
6824 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08006825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006826 return NETDEV_TX_BUSY;
6827 }
6828
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006829 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006830 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07006831 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006832 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00006833
Matt Carlsonbe98da62010-07-11 09:31:46 +00006834 mss = skb_shinfo(skb)->gso_size;
6835 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006836 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00006837 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006838
6839 if (skb_header_cloned(skb) &&
Eric Dumazet48855432011-10-24 07:53:03 +00006840 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
6841 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006842
Matt Carlson34195c32010-07-11 09:31:42 +00006843 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07006844 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006845
Eric Dumazeta5a11952012-01-23 01:22:09 +00006846 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb) - ETH_HLEN;
Matt Carlson34195c32010-07-11 09:31:42 +00006847
Eric Dumazeta5a11952012-01-23 01:22:09 +00006848 if (!skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00006849 iph->check = 0;
6850 iph->tot_len = htons(mss + hdr_len);
6851 }
6852
Michael Chan52c0fd82006-06-29 20:15:54 -07006853 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Joe Perches63c3a662011-04-26 08:12:10 +00006854 tg3_flag(tp, TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00006855 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07006856
Linus Torvalds1da177e2005-04-16 15:20:36 -07006857 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
6858 TXD_FLAG_CPU_POST_DMA);
6859
Joe Perches63c3a662011-04-26 08:12:10 +00006860 if (tg3_flag(tp, HW_TSO_1) ||
6861 tg3_flag(tp, HW_TSO_2) ||
6862 tg3_flag(tp, HW_TSO_3)) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006863 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006864 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006865 } else
6866 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
6867 iph->daddr, 0,
6868 IPPROTO_TCP,
6869 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006870
Joe Perches63c3a662011-04-26 08:12:10 +00006871 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlson615774f2009-11-13 13:03:39 +00006872 mss |= (hdr_len & 0xc) << 12;
6873 if (hdr_len & 0x10)
6874 base_flags |= 0x00000010;
6875 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +00006876 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006877 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +00006878 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006879 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006880 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006881 int tsflags;
6882
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006883 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006884 mss |= (tsflags << 11);
6885 }
6886 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006887 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006888 int tsflags;
6889
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006890 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006891 base_flags |= tsflags << 12;
6892 }
6893 }
6894 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00006895
Matt Carlson93a700a2011-08-31 11:44:54 +00006896 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
6897 !mss && skb->len > VLAN_ETH_FRAME_LEN)
6898 base_flags |= TXD_FLAG_JMB_PKT;
6899
Matt Carlson92cd3a12011-07-27 14:20:47 +00006900 if (vlan_tx_tag_present(skb)) {
6901 base_flags |= TXD_FLAG_VLAN;
6902 vlan = vlan_tx_tag_get(skb);
6903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006904
Alexander Duyckf4188d82009-12-02 16:48:38 +00006905 len = skb_headlen(skb);
6906
6907 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
Eric Dumazet48855432011-10-24 07:53:03 +00006908 if (pci_dma_mapping_error(tp->pdev, mapping))
6909 goto drop;
6910
David S. Miller90079ce2008-09-11 04:52:51 -07006911
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006912 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006913 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006914
6915 would_hit_hwbug = 0;
6916
Joe Perches63c3a662011-04-26 08:12:10 +00006917 if (tg3_flag(tp, 5701_DMA_BUG))
Michael Chanc58ec932005-09-17 00:46:27 -07006918 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006919
Matt Carlson84b67b22011-07-27 14:20:52 +00006920 if (tg3_tx_frag_set(tnapi, &entry, &budget, mapping, len, base_flags |
Matt Carlsond1a3b732011-07-27 14:20:51 +00006921 ((skb_shinfo(skb)->nr_frags == 0) ? TXD_FLAG_END : 0),
Matt Carlsonba1142e2011-11-04 09:15:00 +00006922 mss, vlan)) {
Matt Carlsond1a3b732011-07-27 14:20:51 +00006923 would_hit_hwbug = 1;
Matt Carlsonba1142e2011-11-04 09:15:00 +00006924 } else if (skb_shinfo(skb)->nr_frags > 0) {
Matt Carlson92cd3a12011-07-27 14:20:47 +00006925 u32 tmp_mss = mss;
6926
6927 if (!tg3_flag(tp, HW_TSO_1) &&
6928 !tg3_flag(tp, HW_TSO_2) &&
6929 !tg3_flag(tp, HW_TSO_3))
6930 tmp_mss = 0;
6931
Matt Carlsonc5665a52012-02-13 10:20:12 +00006932 /* Now loop through additional data
6933 * fragments, and queue them.
6934 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006935 last = skb_shinfo(skb)->nr_frags - 1;
6936 for (i = 0; i <= last; i++) {
6937 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6938
Eric Dumazet9e903e02011-10-18 21:00:24 +00006939 len = skb_frag_size(frag);
Ian Campbelldc234d02011-08-24 22:28:11 +00006940 mapping = skb_frag_dma_map(&tp->pdev->dev, frag, 0,
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01006941 len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006942
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006943 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006944 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00006945 mapping);
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01006946 if (dma_mapping_error(&tp->pdev->dev, mapping))
Alexander Duyckf4188d82009-12-02 16:48:38 +00006947 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006948
Matt Carlsonb9e45482011-11-04 09:14:59 +00006949 if (!budget ||
6950 tg3_tx_frag_set(tnapi, &entry, &budget, mapping,
Matt Carlson84b67b22011-07-27 14:20:52 +00006951 len, base_flags |
6952 ((i == last) ? TXD_FLAG_END : 0),
Matt Carlsonb9e45482011-11-04 09:14:59 +00006953 tmp_mss, vlan)) {
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006954 would_hit_hwbug = 1;
Matt Carlsonb9e45482011-11-04 09:14:59 +00006955 break;
6956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006957 }
6958 }
6959
6960 if (would_hit_hwbug) {
Matt Carlson0d681b22011-07-27 14:20:49 +00006961 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006962
6963 /* If the workaround fails due to memory/mapping
6964 * failure, silently drop this packet.
6965 */
Matt Carlson84b67b22011-07-27 14:20:52 +00006966 entry = tnapi->tx_prod;
6967 budget = tg3_tx_avail(tnapi);
David S. Miller1805b2f2011-10-24 18:18:09 -04006968 if (tigon3_dma_hwbug_workaround(tnapi, &skb, &entry, &budget,
Matt Carlson84b67b22011-07-27 14:20:52 +00006969 base_flags, mss, vlan))
Eric Dumazet48855432011-10-24 07:53:03 +00006970 goto drop_nofree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006971 }
6972
Richard Cochrand515b452011-06-19 03:31:41 +00006973 skb_tx_timestamp(skb);
Tom Herbert298376d2011-11-28 16:33:30 +00006974 netdev_sent_queue(tp->dev, skb->len);
Richard Cochrand515b452011-06-19 03:31:41 +00006975
Michael Chan6541b802012-03-04 14:48:14 +00006976 /* Sync BD data before updating mailbox */
6977 wmb();
6978
Linus Torvalds1da177e2005-04-16 15:20:36 -07006979 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006980 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006981
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006982 tnapi->tx_prod = entry;
6983 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006984 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006985
6986 /* netif_tx_stop_queue() must be done before checking
6987 * checking tx index in tg3_tx_avail() below, because in
6988 * tg3_tx(), we update tx index before checking for
6989 * netif_tx_queue_stopped().
6990 */
6991 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006992 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006993 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07006994 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006995
Eric Dumazetcdd0db02009-05-28 00:00:41 +00006996 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006997 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006998
6999dma_error:
Matt Carlsonba1142e2011-11-04 09:15:00 +00007000 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, --i);
Matt Carlson432aa7e2011-05-19 12:12:45 +00007001 tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
Eric Dumazet48855432011-10-24 07:53:03 +00007002drop:
7003 dev_kfree_skb(skb);
7004drop_nofree:
7005 tp->tx_dropped++;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007006 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007007}
7008
Matt Carlson6e01b202011-08-19 13:58:20 +00007009static void tg3_mac_loopback(struct tg3 *tp, bool enable)
7010{
7011 if (enable) {
7012 tp->mac_mode &= ~(MAC_MODE_HALF_DUPLEX |
7013 MAC_MODE_PORT_MODE_MASK);
7014
7015 tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
7016
7017 if (!tg3_flag(tp, 5705_PLUS))
7018 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
7019
7020 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
7021 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
7022 else
7023 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
7024 } else {
7025 tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
7026
7027 if (tg3_flag(tp, 5705_PLUS) ||
7028 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) ||
7029 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
7030 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
7031 }
7032
7033 tw32(MAC_MODE, tp->mac_mode);
7034 udelay(40);
7035}
7036
Matt Carlson941ec902011-08-19 13:58:23 +00007037static int tg3_phy_lpbk_set(struct tg3 *tp, u32 speed, bool extlpbk)
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007038{
Matt Carlson941ec902011-08-19 13:58:23 +00007039 u32 val, bmcr, mac_mode, ptest = 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007040
7041 tg3_phy_toggle_apd(tp, false);
7042 tg3_phy_toggle_automdix(tp, 0);
7043
Matt Carlson941ec902011-08-19 13:58:23 +00007044 if (extlpbk && tg3_phy_set_extloopbk(tp))
7045 return -EIO;
7046
7047 bmcr = BMCR_FULLDPLX;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007048 switch (speed) {
7049 case SPEED_10:
7050 break;
7051 case SPEED_100:
7052 bmcr |= BMCR_SPEED100;
7053 break;
7054 case SPEED_1000:
7055 default:
7056 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
7057 speed = SPEED_100;
7058 bmcr |= BMCR_SPEED100;
7059 } else {
7060 speed = SPEED_1000;
7061 bmcr |= BMCR_SPEED1000;
7062 }
7063 }
7064
Matt Carlson941ec902011-08-19 13:58:23 +00007065 if (extlpbk) {
7066 if (!(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
7067 tg3_readphy(tp, MII_CTRL1000, &val);
7068 val |= CTL1000_AS_MASTER |
7069 CTL1000_ENABLE_MASTER;
7070 tg3_writephy(tp, MII_CTRL1000, val);
7071 } else {
7072 ptest = MII_TG3_FET_PTEST_TRIM_SEL |
7073 MII_TG3_FET_PTEST_TRIM_2;
7074 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest);
7075 }
7076 } else
7077 bmcr |= BMCR_LOOPBACK;
7078
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007079 tg3_writephy(tp, MII_BMCR, bmcr);
7080
7081 /* The write needs to be flushed for the FETs */
7082 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
7083 tg3_readphy(tp, MII_BMCR, &bmcr);
7084
7085 udelay(40);
7086
7087 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
7088 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Matt Carlson941ec902011-08-19 13:58:23 +00007089 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest |
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007090 MII_TG3_FET_PTEST_FRC_TX_LINK |
7091 MII_TG3_FET_PTEST_FRC_TX_LOCK);
7092
7093 /* The write needs to be flushed for the AC131 */
7094 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
7095 }
7096
7097 /* Reset to prevent losing 1st rx packet intermittently */
7098 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
7099 tg3_flag(tp, 5780_CLASS)) {
7100 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
7101 udelay(10);
7102 tw32_f(MAC_RX_MODE, tp->rx_mode);
7103 }
7104
7105 mac_mode = tp->mac_mode &
7106 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
7107 if (speed == SPEED_1000)
7108 mac_mode |= MAC_MODE_PORT_MODE_GMII;
7109 else
7110 mac_mode |= MAC_MODE_PORT_MODE_MII;
7111
7112 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
7113 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
7114
7115 if (masked_phy_id == TG3_PHY_ID_BCM5401)
7116 mac_mode &= ~MAC_MODE_LINK_POLARITY;
7117 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
7118 mac_mode |= MAC_MODE_LINK_POLARITY;
7119
7120 tg3_writephy(tp, MII_TG3_EXT_CTRL,
7121 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
7122 }
7123
7124 tw32(MAC_MODE, mac_mode);
7125 udelay(40);
Matt Carlson941ec902011-08-19 13:58:23 +00007126
7127 return 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007128}
7129
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007130static void tg3_set_loopback(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007131{
7132 struct tg3 *tp = netdev_priv(dev);
7133
7134 if (features & NETIF_F_LOOPBACK) {
7135 if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
7136 return;
7137
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007138 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00007139 tg3_mac_loopback(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007140 netif_carrier_on(tp->dev);
7141 spin_unlock_bh(&tp->lock);
7142 netdev_info(dev, "Internal MAC loopback mode enabled.\n");
7143 } else {
7144 if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
7145 return;
7146
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007147 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00007148 tg3_mac_loopback(tp, false);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007149 /* Force link status check */
7150 tg3_setup_phy(tp, 1);
7151 spin_unlock_bh(&tp->lock);
7152 netdev_info(dev, "Internal MAC loopback mode disabled.\n");
7153 }
7154}
7155
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007156static netdev_features_t tg3_fix_features(struct net_device *dev,
7157 netdev_features_t features)
Michał Mirosławdc668912011-04-07 03:35:07 +00007158{
7159 struct tg3 *tp = netdev_priv(dev);
7160
Joe Perches63c3a662011-04-26 08:12:10 +00007161 if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
Michał Mirosławdc668912011-04-07 03:35:07 +00007162 features &= ~NETIF_F_ALL_TSO;
7163
7164 return features;
7165}
7166
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007167static int tg3_set_features(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007168{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007169 netdev_features_t changed = dev->features ^ features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007170
7171 if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
7172 tg3_set_loopback(dev, features);
7173
7174 return 0;
7175}
7176
Matt Carlson21f581a2009-08-28 14:00:25 +00007177static void tg3_rx_prodring_free(struct tg3 *tp,
7178 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007179{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007180 int i;
7181
Matt Carlson8fea32b2010-09-15 08:59:58 +00007182 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007183 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00007184 i = (i + 1) & tp->rx_std_ring_mask)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007185 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007186 tp->rx_pkt_map_sz);
7187
Joe Perches63c3a662011-04-26 08:12:10 +00007188 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007189 for (i = tpr->rx_jmb_cons_idx;
7190 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00007191 i = (i + 1) & tp->rx_jmb_ring_mask) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007192 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007193 TG3_RX_JMB_MAP_SZ);
7194 }
7195 }
7196
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007197 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007199
Matt Carlson2c49a442010-09-30 10:34:35 +00007200 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007201 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007202 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007203
Joe Perches63c3a662011-04-26 08:12:10 +00007204 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007205 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007206 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007207 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007208 }
7209}
7210
Matt Carlsonc6cdf432010-04-05 10:19:26 +00007211/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007212 *
7213 * The chip has been shut down and the driver detached from
7214 * the networking, so no interrupts or new tx packets will
7215 * end up in the driver. tp->{tx,}lock are held and thus
7216 * we may not sleep.
7217 */
Matt Carlson21f581a2009-08-28 14:00:25 +00007218static int tg3_rx_prodring_alloc(struct tg3 *tp,
7219 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007220{
Matt Carlson287be122009-08-28 13:58:46 +00007221 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007222
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007223 tpr->rx_std_cons_idx = 0;
7224 tpr->rx_std_prod_idx = 0;
7225 tpr->rx_jmb_cons_idx = 0;
7226 tpr->rx_jmb_prod_idx = 0;
7227
Matt Carlson8fea32b2010-09-15 08:59:58 +00007228 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007229 memset(&tpr->rx_std_buffers[0], 0,
7230 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00007231 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007232 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00007233 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007234 goto done;
7235 }
7236
Linus Torvalds1da177e2005-04-16 15:20:36 -07007237 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00007238 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007239
Matt Carlson287be122009-08-28 13:58:46 +00007240 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00007241 if (tg3_flag(tp, 5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00007242 tp->dev->mtu > ETH_DATA_LEN)
7243 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
7244 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07007245
Linus Torvalds1da177e2005-04-16 15:20:36 -07007246 /* Initialize invariants of the rings, we only set this
7247 * stuff once. This works because the card does not
7248 * write into the rx buffer posting rings.
7249 */
Matt Carlson2c49a442010-09-30 10:34:35 +00007250 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007251 struct tg3_rx_buffer_desc *rxd;
7252
Matt Carlson21f581a2009-08-28 14:00:25 +00007253 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00007254 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007255 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
7256 rxd->opaque = (RXD_OPAQUE_RING_STD |
7257 (i << RXD_OPAQUE_INDEX_SHIFT));
7258 }
7259
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007260 /* Now allocate fresh SKBs for each rx ring. */
7261 for (i = 0; i < tp->rx_pending; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007262 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_STD, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007263 netdev_warn(tp->dev,
7264 "Using a smaller RX standard ring. Only "
7265 "%d out of %d buffers were allocated "
7266 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007267 if (i == 0)
7268 goto initfail;
7269 tp->rx_pending = i;
7270 break;
7271 }
7272 }
7273
Joe Perches63c3a662011-04-26 08:12:10 +00007274 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007275 goto done;
7276
Matt Carlson2c49a442010-09-30 10:34:35 +00007277 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007278
Joe Perches63c3a662011-04-26 08:12:10 +00007279 if (!tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson0d86df82010-02-17 15:17:00 +00007280 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007281
Matt Carlson2c49a442010-09-30 10:34:35 +00007282 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00007283 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007284
Matt Carlson0d86df82010-02-17 15:17:00 +00007285 rxd = &tpr->rx_jmb[i].std;
7286 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
7287 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
7288 RXD_FLAG_JUMBO;
7289 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
7290 (i << RXD_OPAQUE_INDEX_SHIFT));
7291 }
7292
7293 for (i = 0; i < tp->rx_jumbo_pending; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007294 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_JUMBO, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007295 netdev_warn(tp->dev,
7296 "Using a smaller RX jumbo ring. Only %d "
7297 "out of %d buffers were allocated "
7298 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00007299 if (i == 0)
7300 goto initfail;
7301 tp->rx_jumbo_pending = i;
7302 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007303 }
7304 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007305
7306done:
Michael Chan32d8c572006-07-25 16:38:29 -07007307 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007308
7309initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00007310 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007311 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007312}
7313
Matt Carlson21f581a2009-08-28 14:00:25 +00007314static void tg3_rx_prodring_fini(struct tg3 *tp,
7315 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007316{
Matt Carlson21f581a2009-08-28 14:00:25 +00007317 kfree(tpr->rx_std_buffers);
7318 tpr->rx_std_buffers = NULL;
7319 kfree(tpr->rx_jmb_buffers);
7320 tpr->rx_jmb_buffers = NULL;
7321 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007322 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
7323 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00007324 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007325 }
Matt Carlson21f581a2009-08-28 14:00:25 +00007326 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007327 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
7328 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00007329 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007330 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007331}
7332
Matt Carlson21f581a2009-08-28 14:00:25 +00007333static int tg3_rx_prodring_init(struct tg3 *tp,
7334 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007335{
Matt Carlson2c49a442010-09-30 10:34:35 +00007336 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
7337 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007338 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007339 return -ENOMEM;
7340
Matt Carlson4bae65c2010-11-24 08:31:52 +00007341 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
7342 TG3_RX_STD_RING_BYTES(tp),
7343 &tpr->rx_std_mapping,
7344 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007345 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007346 goto err_out;
7347
Joe Perches63c3a662011-04-26 08:12:10 +00007348 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007349 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00007350 GFP_KERNEL);
7351 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007352 goto err_out;
7353
Matt Carlson4bae65c2010-11-24 08:31:52 +00007354 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
7355 TG3_RX_JMB_RING_BYTES(tp),
7356 &tpr->rx_jmb_mapping,
7357 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007358 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007359 goto err_out;
7360 }
7361
7362 return 0;
7363
7364err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00007365 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007366 return -ENOMEM;
7367}
7368
7369/* Free up pending packets in all rx/tx rings.
7370 *
7371 * The chip has been shut down and the driver detached from
7372 * the networking, so no interrupts or new tx packets will
7373 * end up in the driver. tp->{tx,}lock is not held and we are not
7374 * in an interrupt context and thus may sleep.
7375 */
7376static void tg3_free_rings(struct tg3 *tp)
7377{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007378 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007379
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007380 for (j = 0; j < tp->irq_cnt; j++) {
7381 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007382
Matt Carlson8fea32b2010-09-15 08:59:58 +00007383 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00007384
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007385 if (!tnapi->tx_buffers)
7386 continue;
7387
Matt Carlson0d681b22011-07-27 14:20:49 +00007388 for (i = 0; i < TG3_TX_RING_SIZE; i++) {
7389 struct sk_buff *skb = tnapi->tx_buffers[i].skb;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007390
Matt Carlson0d681b22011-07-27 14:20:49 +00007391 if (!skb)
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007392 continue;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007393
Matt Carlsonba1142e2011-11-04 09:15:00 +00007394 tg3_tx_skb_unmap(tnapi, i,
7395 skb_shinfo(skb)->nr_frags - 1);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007396
7397 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007398 }
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007399 }
Tom Herbert298376d2011-11-28 16:33:30 +00007400 netdev_reset_queue(tp->dev);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007401}
7402
7403/* Initialize tx/rx rings for packet processing.
7404 *
7405 * The chip has been shut down and the driver detached from
7406 * the networking, so no interrupts or new tx packets will
7407 * end up in the driver. tp->{tx,}lock are held and thus
7408 * we may not sleep.
7409 */
7410static int tg3_init_rings(struct tg3 *tp)
7411{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007412 int i;
Matt Carlson72334482009-08-28 14:03:01 +00007413
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007414 /* Free up all the SKBs. */
7415 tg3_free_rings(tp);
7416
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007417 for (i = 0; i < tp->irq_cnt; i++) {
7418 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007419
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007420 tnapi->last_tag = 0;
7421 tnapi->last_irq_tag = 0;
7422 tnapi->hw_status->status = 0;
7423 tnapi->hw_status->status_tag = 0;
7424 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7425
7426 tnapi->tx_prod = 0;
7427 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007428 if (tnapi->tx_ring)
7429 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007430
7431 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007432 if (tnapi->rx_rcb)
7433 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007434
Matt Carlson8fea32b2010-09-15 08:59:58 +00007435 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00007436 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007437 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00007438 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007439 }
Matt Carlson72334482009-08-28 14:03:01 +00007440
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007441 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007442}
7443
7444/*
7445 * Must not be invoked with interrupt sources disabled and
7446 * the hardware shutdown down.
7447 */
7448static void tg3_free_consistent(struct tg3 *tp)
7449{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007450 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00007451
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007452 for (i = 0; i < tp->irq_cnt; i++) {
7453 struct tg3_napi *tnapi = &tp->napi[i];
7454
7455 if (tnapi->tx_ring) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007456 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007457 tnapi->tx_ring, tnapi->tx_desc_mapping);
7458 tnapi->tx_ring = NULL;
7459 }
7460
7461 kfree(tnapi->tx_buffers);
7462 tnapi->tx_buffers = NULL;
7463
7464 if (tnapi->rx_rcb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007465 dma_free_coherent(&tp->pdev->dev,
7466 TG3_RX_RCB_RING_BYTES(tp),
7467 tnapi->rx_rcb,
7468 tnapi->rx_rcb_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007469 tnapi->rx_rcb = NULL;
7470 }
7471
Matt Carlson8fea32b2010-09-15 08:59:58 +00007472 tg3_rx_prodring_fini(tp, &tnapi->prodring);
7473
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007474 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007475 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
7476 tnapi->hw_status,
7477 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007478 tnapi->hw_status = NULL;
7479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007480 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007481
Linus Torvalds1da177e2005-04-16 15:20:36 -07007482 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007483 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
7484 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007485 tp->hw_stats = NULL;
7486 }
7487}
7488
7489/*
7490 * Must not be invoked with interrupt sources disabled and
7491 * the hardware shutdown down. Can sleep.
7492 */
7493static int tg3_alloc_consistent(struct tg3 *tp)
7494{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007495 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00007496
Matt Carlson4bae65c2010-11-24 08:31:52 +00007497 tp->hw_stats = dma_alloc_coherent(&tp->pdev->dev,
7498 sizeof(struct tg3_hw_stats),
7499 &tp->stats_mapping,
7500 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007501 if (!tp->hw_stats)
7502 goto err_out;
7503
Linus Torvalds1da177e2005-04-16 15:20:36 -07007504 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
7505
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007506 for (i = 0; i < tp->irq_cnt; i++) {
7507 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007508 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007509
Matt Carlson4bae65c2010-11-24 08:31:52 +00007510 tnapi->hw_status = dma_alloc_coherent(&tp->pdev->dev,
7511 TG3_HW_STATUS_SIZE,
7512 &tnapi->status_mapping,
7513 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007514 if (!tnapi->hw_status)
7515 goto err_out;
7516
7517 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007518 sblk = tnapi->hw_status;
7519
Matt Carlson8fea32b2010-09-15 08:59:58 +00007520 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
7521 goto err_out;
7522
Matt Carlson19cfaec2009-12-03 08:36:20 +00007523 /* If multivector TSS is enabled, vector 0 does not handle
7524 * tx interrupts. Don't allocate any resources for it.
7525 */
Joe Perches63c3a662011-04-26 08:12:10 +00007526 if ((!i && !tg3_flag(tp, ENABLE_TSS)) ||
7527 (i && tg3_flag(tp, ENABLE_TSS))) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00007528 tnapi->tx_buffers = kzalloc(
7529 sizeof(struct tg3_tx_ring_info) *
7530 TG3_TX_RING_SIZE, GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007531 if (!tnapi->tx_buffers)
7532 goto err_out;
7533
Matt Carlson4bae65c2010-11-24 08:31:52 +00007534 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
7535 TG3_TX_RING_BYTES,
7536 &tnapi->tx_desc_mapping,
7537 GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007538 if (!tnapi->tx_ring)
7539 goto err_out;
7540 }
7541
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007542 /*
7543 * When RSS is enabled, the status block format changes
7544 * slightly. The "rx_jumbo_consumer", "reserved",
7545 * and "rx_mini_consumer" members get mapped to the
7546 * other three rx return ring producer indexes.
7547 */
7548 switch (i) {
7549 default:
7550 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
7551 break;
7552 case 2:
7553 tnapi->rx_rcb_prod_idx = &sblk->rx_jumbo_consumer;
7554 break;
7555 case 3:
7556 tnapi->rx_rcb_prod_idx = &sblk->reserved;
7557 break;
7558 case 4:
7559 tnapi->rx_rcb_prod_idx = &sblk->rx_mini_consumer;
7560 break;
7561 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007562
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007563 /*
7564 * If multivector RSS is enabled, vector 0 does not handle
7565 * rx or tx interrupts. Don't allocate any resources for it.
7566 */
Joe Perches63c3a662011-04-26 08:12:10 +00007567 if (!i && tg3_flag(tp, ENABLE_RSS))
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007568 continue;
7569
Matt Carlson4bae65c2010-11-24 08:31:52 +00007570 tnapi->rx_rcb = dma_alloc_coherent(&tp->pdev->dev,
7571 TG3_RX_RCB_RING_BYTES(tp),
7572 &tnapi->rx_rcb_mapping,
7573 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007574 if (!tnapi->rx_rcb)
7575 goto err_out;
7576
7577 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007578 }
7579
Linus Torvalds1da177e2005-04-16 15:20:36 -07007580 return 0;
7581
7582err_out:
7583 tg3_free_consistent(tp);
7584 return -ENOMEM;
7585}
7586
7587#define MAX_WAIT_CNT 1000
7588
7589/* To stop a block, clear the enable bit and poll till it
7590 * clears. tp->lock is held.
7591 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007592static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007593{
7594 unsigned int i;
7595 u32 val;
7596
Joe Perches63c3a662011-04-26 08:12:10 +00007597 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007598 switch (ofs) {
7599 case RCVLSC_MODE:
7600 case DMAC_MODE:
7601 case MBFREE_MODE:
7602 case BUFMGR_MODE:
7603 case MEMARB_MODE:
7604 /* We can't enable/disable these bits of the
7605 * 5705/5750, just say success.
7606 */
7607 return 0;
7608
7609 default:
7610 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007611 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007612 }
7613
7614 val = tr32(ofs);
7615 val &= ~enable_bit;
7616 tw32_f(ofs, val);
7617
7618 for (i = 0; i < MAX_WAIT_CNT; i++) {
7619 udelay(100);
7620 val = tr32(ofs);
7621 if ((val & enable_bit) == 0)
7622 break;
7623 }
7624
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007625 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00007626 dev_err(&tp->pdev->dev,
7627 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
7628 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007629 return -ENODEV;
7630 }
7631
7632 return 0;
7633}
7634
7635/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007636static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007637{
7638 int i, err;
7639
7640 tg3_disable_ints(tp);
7641
7642 tp->rx_mode &= ~RX_MODE_ENABLE;
7643 tw32_f(MAC_RX_MODE, tp->rx_mode);
7644 udelay(10);
7645
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007646 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
7647 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
7648 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
7649 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
7650 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
7651 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007652
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007653 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
7654 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
7655 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
7656 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
7657 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
7658 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
7659 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007660
7661 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
7662 tw32_f(MAC_MODE, tp->mac_mode);
7663 udelay(40);
7664
7665 tp->tx_mode &= ~TX_MODE_ENABLE;
7666 tw32_f(MAC_TX_MODE, tp->tx_mode);
7667
7668 for (i = 0; i < MAX_WAIT_CNT; i++) {
7669 udelay(100);
7670 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
7671 break;
7672 }
7673 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00007674 dev_err(&tp->pdev->dev,
7675 "%s timed out, TX_MODE_ENABLE will not clear "
7676 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07007677 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007678 }
7679
Michael Chane6de8ad2005-05-05 14:42:41 -07007680 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007681 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
7682 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007683
7684 tw32(FTQ_RESET, 0xffffffff);
7685 tw32(FTQ_RESET, 0x00000000);
7686
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007687 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
7688 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007689
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007690 for (i = 0; i < tp->irq_cnt; i++) {
7691 struct tg3_napi *tnapi = &tp->napi[i];
7692 if (tnapi->hw_status)
7693 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007695
Linus Torvalds1da177e2005-04-16 15:20:36 -07007696 return err;
7697}
7698
Michael Chanee6a99b2007-07-18 21:49:10 -07007699/* Save PCI command register before chip reset */
7700static void tg3_save_pci_state(struct tg3 *tp)
7701{
Matt Carlson8a6eac92007-10-21 16:17:55 -07007702 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007703}
7704
7705/* Restore PCI state after chip reset */
7706static void tg3_restore_pci_state(struct tg3 *tp)
7707{
7708 u32 val;
7709
7710 /* Re-enable indirect register accesses. */
7711 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
7712 tp->misc_host_ctrl);
7713
7714 /* Set MAX PCI retry to zero. */
7715 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
7716 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007717 tg3_flag(tp, PCIX_MODE))
Michael Chanee6a99b2007-07-18 21:49:10 -07007718 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07007719 /* Allow reads and writes to the APE register and memory space. */
Joe Perches63c3a662011-04-26 08:12:10 +00007720 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07007721 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00007722 PCISTATE_ALLOW_APE_SHMEM_WR |
7723 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07007724 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
7725
Matt Carlson8a6eac92007-10-21 16:17:55 -07007726 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007727
Matt Carlson2c55a3d2011-11-28 09:41:04 +00007728 if (!tg3_flag(tp, PCI_EXPRESS)) {
7729 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
7730 tp->pci_cacheline_sz);
7731 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
7732 tp->pci_lat_timer);
Michael Chan114342f2007-10-15 02:12:26 -07007733 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08007734
Michael Chanee6a99b2007-07-18 21:49:10 -07007735 /* Make sure PCI-X relaxed ordering bit is clear. */
Joe Perches63c3a662011-04-26 08:12:10 +00007736 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07007737 u16 pcix_cmd;
7738
7739 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7740 &pcix_cmd);
7741 pcix_cmd &= ~PCI_X_CMD_ERO;
7742 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7743 pcix_cmd);
7744 }
Michael Chanee6a99b2007-07-18 21:49:10 -07007745
Joe Perches63c3a662011-04-26 08:12:10 +00007746 if (tg3_flag(tp, 5780_CLASS)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007747
7748 /* Chip reset on 5780 will reset MSI enable bit,
7749 * so need to restore it.
7750 */
Joe Perches63c3a662011-04-26 08:12:10 +00007751 if (tg3_flag(tp, USING_MSI)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007752 u16 ctrl;
7753
7754 pci_read_config_word(tp->pdev,
7755 tp->msi_cap + PCI_MSI_FLAGS,
7756 &ctrl);
7757 pci_write_config_word(tp->pdev,
7758 tp->msi_cap + PCI_MSI_FLAGS,
7759 ctrl | PCI_MSI_FLAGS_ENABLE);
7760 val = tr32(MSGINT_MODE);
7761 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
7762 }
7763 }
7764}
7765
Linus Torvalds1da177e2005-04-16 15:20:36 -07007766/* tp->lock is held. */
7767static int tg3_chip_reset(struct tg3 *tp)
7768{
7769 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07007770 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00007771 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007772
David S. Millerf49639e2006-06-09 11:58:36 -07007773 tg3_nvram_lock(tp);
7774
Matt Carlson77b483f2008-08-15 14:07:24 -07007775 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
7776
David S. Millerf49639e2006-06-09 11:58:36 -07007777 /* No matching tg3_nvram_unlock() after this because
7778 * chip reset below will undo the nvram lock.
7779 */
7780 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007781
Michael Chanee6a99b2007-07-18 21:49:10 -07007782 /* GRC_MISC_CFG core clock reset will clear the memory
7783 * enable bit in PCI register 4 and the MSI enable bit
7784 * on some chips, so we save relevant registers here.
7785 */
7786 tg3_save_pci_state(tp);
7787
Michael Chand9ab5ad2006-03-20 22:27:35 -08007788 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Joe Perches63c3a662011-04-26 08:12:10 +00007789 tg3_flag(tp, 5755_PLUS))
Michael Chand9ab5ad2006-03-20 22:27:35 -08007790 tw32(GRC_FASTBOOT_PC, 0);
7791
Linus Torvalds1da177e2005-04-16 15:20:36 -07007792 /*
7793 * We must avoid the readl() that normally takes place.
7794 * It locks machines, causes machine checks, and other
7795 * fun things. So, temporarily disable the 5701
7796 * hardware workaround, while we do the reset.
7797 */
Michael Chan1ee582d2005-08-09 20:16:46 -07007798 write_op = tp->write32;
7799 if (write_op == tg3_write_flush_reg32)
7800 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007801
Michael Chand18edcb2007-03-24 20:57:11 -07007802 /* Prevent the irq handler from reading or writing PCI registers
7803 * during chip reset when the memory enable bit in the PCI command
7804 * register may be cleared. The chip does not generate interrupt
7805 * at this time, but the irq handler may still be called due to irq
7806 * sharing or irqpoll.
7807 */
Joe Perches63c3a662011-04-26 08:12:10 +00007808 tg3_flag_set(tp, CHIP_RESETTING);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007809 for (i = 0; i < tp->irq_cnt; i++) {
7810 struct tg3_napi *tnapi = &tp->napi[i];
7811 if (tnapi->hw_status) {
7812 tnapi->hw_status->status = 0;
7813 tnapi->hw_status->status_tag = 0;
7814 }
7815 tnapi->last_tag = 0;
7816 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07007817 }
Michael Chand18edcb2007-03-24 20:57:11 -07007818 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00007819
7820 for (i = 0; i < tp->irq_cnt; i++)
7821 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07007822
Matt Carlson255ca312009-08-25 10:07:27 +00007823 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7824 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7825 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
7826 }
7827
Linus Torvalds1da177e2005-04-16 15:20:36 -07007828 /* do the reset */
7829 val = GRC_MISC_CFG_CORECLK_RESET;
7830
Joe Perches63c3a662011-04-26 08:12:10 +00007831 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson88075d92010-08-02 11:25:58 +00007832 /* Force PCIe 1.0a mode */
7833 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007834 !tg3_flag(tp, 57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00007835 tr32(TG3_PCIE_PHY_TSTCTL) ==
7836 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
7837 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
7838
Linus Torvalds1da177e2005-04-16 15:20:36 -07007839 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
7840 tw32(GRC_MISC_CFG, (1 << 29));
7841 val |= (1 << 29);
7842 }
7843 }
7844
Michael Chanb5d37722006-09-27 16:06:21 -07007845 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7846 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
7847 tw32(GRC_VCPU_EXT_CTRL,
7848 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
7849 }
7850
Matt Carlsonf37500d2010-08-02 11:25:59 +00007851 /* Manage gphy power for all CPMU absent PCIe devices. */
Joe Perches63c3a662011-04-26 08:12:10 +00007852 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007853 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00007854
Linus Torvalds1da177e2005-04-16 15:20:36 -07007855 tw32(GRC_MISC_CFG, val);
7856
Michael Chan1ee582d2005-08-09 20:16:46 -07007857 /* restore 5701 hardware bug workaround write method */
7858 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007859
7860 /* Unfortunately, we have to delay before the PCI read back.
7861 * Some 575X chips even will not respond to a PCI cfg access
7862 * when the reset command is given to the chip.
7863 *
7864 * How do these hardware designers expect things to work
7865 * properly if the PCI write is posted for a long period
7866 * of time? It is always necessary to have some method by
7867 * which a register read back can occur to push the write
7868 * out which does the reset.
7869 *
7870 * For most tg3 variants the trick below was working.
7871 * Ho hum...
7872 */
7873 udelay(120);
7874
7875 /* Flush PCI posted writes. The normal MMIO registers
7876 * are inaccessible at this time so this is the only
7877 * way to make this reliably (actually, this is no longer
7878 * the case, see above). I tried to use indirect
7879 * register read/write but this upset some 5701 variants.
7880 */
7881 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
7882
7883 udelay(120);
7884
Jon Mason708ebb32011-06-27 12:56:50 +00007885 if (tg3_flag(tp, PCI_EXPRESS) && pci_pcie_cap(tp->pdev)) {
Matt Carlsone7126992009-08-25 10:08:16 +00007886 u16 val16;
7887
Linus Torvalds1da177e2005-04-16 15:20:36 -07007888 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
7889 int i;
7890 u32 cfg_val;
7891
7892 /* Wait for link training to complete. */
7893 for (i = 0; i < 5000; i++)
7894 udelay(100);
7895
7896 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
7897 pci_write_config_dword(tp->pdev, 0xc4,
7898 cfg_val | (1 << 15));
7899 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007900
Matt Carlsone7126992009-08-25 10:08:16 +00007901 /* Clear the "no snoop" and "relaxed ordering" bits. */
7902 pci_read_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00007903 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007904 &val16);
7905 val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
7906 PCI_EXP_DEVCTL_NOSNOOP_EN);
7907 /*
7908 * Older PCIe devices only support the 128 byte
7909 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007910 */
Joe Perches63c3a662011-04-26 08:12:10 +00007911 if (!tg3_flag(tp, CPMU_PRESENT))
Matt Carlsone7126992009-08-25 10:08:16 +00007912 val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007913 pci_write_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00007914 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007915 val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007916
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007917 /* Clear error status */
7918 pci_write_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00007919 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVSTA,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007920 PCI_EXP_DEVSTA_CED |
7921 PCI_EXP_DEVSTA_NFED |
7922 PCI_EXP_DEVSTA_FED |
7923 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007924 }
7925
Michael Chanee6a99b2007-07-18 21:49:10 -07007926 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007927
Joe Perches63c3a662011-04-26 08:12:10 +00007928 tg3_flag_clear(tp, CHIP_RESETTING);
7929 tg3_flag_clear(tp, ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07007930
Michael Chanee6a99b2007-07-18 21:49:10 -07007931 val = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00007932 if (tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07007933 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07007934 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007935
7936 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
7937 tg3_stop_fw(tp);
7938 tw32(0x5000, 0x400);
7939 }
7940
7941 tw32(GRC_MODE, tp->grc_mode);
7942
7943 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007944 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007945
7946 tw32(0xc4, val | (1 << 15));
7947 }
7948
7949 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
7950 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
7951 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
7952 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
7953 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
7954 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7955 }
7956
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007957 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00007958 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007959 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007960 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00007961 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007962 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007963 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007964 val = 0;
7965
7966 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007967 udelay(40);
7968
Matt Carlson77b483f2008-08-15 14:07:24 -07007969 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
7970
Michael Chan7a6f4362006-09-27 16:03:31 -07007971 err = tg3_poll_fw(tp);
7972 if (err)
7973 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007974
Matt Carlson0a9140c2009-08-28 12:27:50 +00007975 tg3_mdio_start(tp);
7976
Joe Perches63c3a662011-04-26 08:12:10 +00007977 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007978 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
7979 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007980 !tg3_flag(tp, 57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007981 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007982
7983 tw32(0x7c00, val | (1 << 25));
7984 }
7985
Matt Carlsond78b59f2011-04-05 14:22:46 +00007986 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
7987 val = tr32(TG3_CPMU_CLCK_ORIDE);
7988 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
7989 }
7990
Linus Torvalds1da177e2005-04-16 15:20:36 -07007991 /* Reprobe ASF enable state. */
Joe Perches63c3a662011-04-26 08:12:10 +00007992 tg3_flag_clear(tp, ENABLE_ASF);
7993 tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007994 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
7995 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
7996 u32 nic_cfg;
7997
7998 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
7999 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +00008000 tg3_flag_set(tp, ENABLE_ASF);
Matt Carlson4ba526c2008-08-15 14:10:04 -07008001 tp->last_event_jiffies = jiffies;
Joe Perches63c3a662011-04-26 08:12:10 +00008002 if (tg3_flag(tp, 5750_PLUS))
8003 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008004 }
8005 }
8006
8007 return 0;
8008}
8009
Matt Carlson65ec6982012-02-28 23:33:37 +00008010static void tg3_get_nstats(struct tg3 *, struct rtnl_link_stats64 *);
8011static void tg3_get_estats(struct tg3 *, struct tg3_ethtool_stats *);
Matt Carlson92feeab2011-12-08 14:40:14 +00008012
Linus Torvalds1da177e2005-04-16 15:20:36 -07008013/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07008014static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008015{
8016 int err;
8017
8018 tg3_stop_fw(tp);
8019
Michael Chan944d9802005-05-29 14:57:48 -07008020 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008021
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008022 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008023 err = tg3_chip_reset(tp);
8024
Matt Carlsondaba2a62009-04-20 06:58:52 +00008025 __tg3_set_mac_addr(tp, 0);
8026
Michael Chan944d9802005-05-29 14:57:48 -07008027 tg3_write_sig_legacy(tp, kind);
8028 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008029
Matt Carlson92feeab2011-12-08 14:40:14 +00008030 if (tp->hw_stats) {
8031 /* Save the stats across chip resets... */
David S. Millerb4017c52012-03-01 17:57:40 -05008032 tg3_get_nstats(tp, &tp->net_stats_prev);
Matt Carlson92feeab2011-12-08 14:40:14 +00008033 tg3_get_estats(tp, &tp->estats_prev);
8034
8035 /* And make sure the next sample is new data */
8036 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
8037 }
8038
Linus Torvalds1da177e2005-04-16 15:20:36 -07008039 if (err)
8040 return err;
8041
8042 return 0;
8043}
8044
Linus Torvalds1da177e2005-04-16 15:20:36 -07008045static int tg3_set_mac_addr(struct net_device *dev, void *p)
8046{
8047 struct tg3 *tp = netdev_priv(dev);
8048 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07008049 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008050
Michael Chanf9804dd2005-09-27 12:13:10 -07008051 if (!is_valid_ether_addr(addr->sa_data))
Danny Kukawka504f9b52012-02-21 02:07:49 +00008052 return -EADDRNOTAVAIL;
Michael Chanf9804dd2005-09-27 12:13:10 -07008053
Linus Torvalds1da177e2005-04-16 15:20:36 -07008054 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
8055
Michael Chane75f7c92006-03-20 21:33:26 -08008056 if (!netif_running(dev))
8057 return 0;
8058
Joe Perches63c3a662011-04-26 08:12:10 +00008059 if (tg3_flag(tp, ENABLE_ASF)) {
Michael Chan986e0ae2007-05-05 12:10:20 -07008060 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07008061
Michael Chan986e0ae2007-05-05 12:10:20 -07008062 addr0_high = tr32(MAC_ADDR_0_HIGH);
8063 addr0_low = tr32(MAC_ADDR_0_LOW);
8064 addr1_high = tr32(MAC_ADDR_1_HIGH);
8065 addr1_low = tr32(MAC_ADDR_1_LOW);
8066
8067 /* Skip MAC addr 1 if ASF is using it. */
8068 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
8069 !(addr1_high == 0 && addr1_low == 0))
8070 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07008071 }
Michael Chan986e0ae2007-05-05 12:10:20 -07008072 spin_lock_bh(&tp->lock);
8073 __tg3_set_mac_addr(tp, skip_mac_1);
8074 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008075
Michael Chanb9ec6c12006-07-25 16:37:27 -07008076 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008077}
8078
8079/* tp->lock is held. */
8080static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
8081 dma_addr_t mapping, u32 maxlen_flags,
8082 u32 nic_addr)
8083{
8084 tg3_write_mem(tp,
8085 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
8086 ((u64) mapping >> 32));
8087 tg3_write_mem(tp,
8088 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
8089 ((u64) mapping & 0xffffffff));
8090 tg3_write_mem(tp,
8091 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
8092 maxlen_flags);
8093
Joe Perches63c3a662011-04-26 08:12:10 +00008094 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008095 tg3_write_mem(tp,
8096 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
8097 nic_addr);
8098}
8099
Michael Chand244c892005-07-05 14:42:33 -07008100static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07008101{
Matt Carlsonb6080e12009-09-01 13:12:00 +00008102 int i;
8103
Joe Perches63c3a662011-04-26 08:12:10 +00008104 if (!tg3_flag(tp, ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00008105 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
8106 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
8107 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008108 } else {
8109 tw32(HOSTCC_TXCOL_TICKS, 0);
8110 tw32(HOSTCC_TXMAX_FRAMES, 0);
8111 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00008112 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008113
Joe Perches63c3a662011-04-26 08:12:10 +00008114 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008115 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
8116 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
8117 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
8118 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00008119 tw32(HOSTCC_RXCOL_TICKS, 0);
8120 tw32(HOSTCC_RXMAX_FRAMES, 0);
8121 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07008122 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008123
Joe Perches63c3a662011-04-26 08:12:10 +00008124 if (!tg3_flag(tp, 5705_PLUS)) {
David S. Miller15f98502005-05-18 22:49:26 -07008125 u32 val = ec->stats_block_coalesce_usecs;
8126
Matt Carlsonb6080e12009-09-01 13:12:00 +00008127 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
8128 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
8129
David S. Miller15f98502005-05-18 22:49:26 -07008130 if (!netif_carrier_ok(tp->dev))
8131 val = 0;
8132
8133 tw32(HOSTCC_STAT_COAL_TICKS, val);
8134 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008135
8136 for (i = 0; i < tp->irq_cnt - 1; i++) {
8137 u32 reg;
8138
8139 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
8140 tw32(reg, ec->rx_coalesce_usecs);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008141 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
8142 tw32(reg, ec->rx_max_coalesced_frames);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008143 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
8144 tw32(reg, ec->rx_max_coalesced_frames_irq);
Matt Carlson19cfaec2009-12-03 08:36:20 +00008145
Joe Perches63c3a662011-04-26 08:12:10 +00008146 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008147 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
8148 tw32(reg, ec->tx_coalesce_usecs);
8149 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
8150 tw32(reg, ec->tx_max_coalesced_frames);
8151 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
8152 tw32(reg, ec->tx_max_coalesced_frames_irq);
8153 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008154 }
8155
8156 for (; i < tp->irq_max - 1; i++) {
8157 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008158 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008159 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00008160
Joe Perches63c3a662011-04-26 08:12:10 +00008161 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008162 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
8163 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
8164 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
8165 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008166 }
David S. Miller15f98502005-05-18 22:49:26 -07008167}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008168
8169/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00008170static void tg3_rings_reset(struct tg3 *tp)
8171{
8172 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008173 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008174 struct tg3_napi *tnapi = &tp->napi[0];
8175
8176 /* Disable all transmit rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008177 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008178 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Joe Perches63c3a662011-04-26 08:12:10 +00008179 else if (tg3_flag(tp, 5717_PLUS))
Matt Carlson3d377282010-10-14 10:37:39 +00008180 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
Matt Carlson55086ad2011-12-14 11:09:59 +00008181 else if (tg3_flag(tp, 57765_CLASS))
Matt Carlsonb703df62009-12-03 08:36:21 +00008182 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008183 else
8184 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8185
8186 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8187 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
8188 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
8189 BDINFO_FLAGS_DISABLED);
8190
8191
8192 /* Disable all receive return rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008193 if (tg3_flag(tp, 5717_PLUS))
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008194 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
Joe Perches63c3a662011-04-26 08:12:10 +00008195 else if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008196 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00008197 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00008198 tg3_flag(tp, 57765_CLASS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008199 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
8200 else
8201 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8202
8203 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8204 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
8205 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
8206 BDINFO_FLAGS_DISABLED);
8207
8208 /* Disable interrupts */
8209 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008210 tp->napi[0].chk_msi_cnt = 0;
8211 tp->napi[0].last_rx_cons = 0;
8212 tp->napi[0].last_tx_cons = 0;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008213
8214 /* Zero mailbox registers. */
Joe Perches63c3a662011-04-26 08:12:10 +00008215 if (tg3_flag(tp, SUPPORT_MSIX)) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00008216 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008217 tp->napi[i].tx_prod = 0;
8218 tp->napi[i].tx_cons = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00008219 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008220 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008221 tw32_rx_mbox(tp->napi[i].consmbox, 0);
8222 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
Matt Carlson7f230732011-08-31 11:44:48 +00008223 tp->napi[i].chk_msi_cnt = 0;
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008224 tp->napi[i].last_rx_cons = 0;
8225 tp->napi[i].last_tx_cons = 0;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008226 }
Joe Perches63c3a662011-04-26 08:12:10 +00008227 if (!tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008228 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008229 } else {
8230 tp->napi[0].tx_prod = 0;
8231 tp->napi[0].tx_cons = 0;
8232 tw32_mailbox(tp->napi[0].prodmbox, 0);
8233 tw32_rx_mbox(tp->napi[0].consmbox, 0);
8234 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008235
8236 /* Make sure the NIC-based send BD rings are disabled. */
Joe Perches63c3a662011-04-26 08:12:10 +00008237 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson2d31eca2009-09-01 12:53:31 +00008238 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
8239 for (i = 0; i < 16; i++)
8240 tw32_tx_mbox(mbox + i * 8, 0);
8241 }
8242
8243 txrcb = NIC_SRAM_SEND_RCB;
8244 rxrcb = NIC_SRAM_RCV_RET_RCB;
8245
8246 /* Clear status block in ram. */
8247 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8248
8249 /* Set status block DMA address */
8250 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8251 ((u64) tnapi->status_mapping >> 32));
8252 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8253 ((u64) tnapi->status_mapping & 0xffffffff));
8254
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008255 if (tnapi->tx_ring) {
8256 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8257 (TG3_TX_RING_SIZE <<
8258 BDINFO_FLAGS_MAXLEN_SHIFT),
8259 NIC_SRAM_TX_BUFFER_DESC);
8260 txrcb += TG3_BDINFO_SIZE;
8261 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008262
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008263 if (tnapi->rx_rcb) {
8264 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008265 (tp->rx_ret_ring_mask + 1) <<
8266 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008267 rxrcb += TG3_BDINFO_SIZE;
8268 }
8269
8270 stblk = HOSTCC_STATBLCK_RING1;
8271
8272 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
8273 u64 mapping = (u64)tnapi->status_mapping;
8274 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
8275 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
8276
8277 /* Clear status block in ram. */
8278 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8279
Matt Carlson19cfaec2009-12-03 08:36:20 +00008280 if (tnapi->tx_ring) {
8281 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8282 (TG3_TX_RING_SIZE <<
8283 BDINFO_FLAGS_MAXLEN_SHIFT),
8284 NIC_SRAM_TX_BUFFER_DESC);
8285 txrcb += TG3_BDINFO_SIZE;
8286 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008287
8288 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008289 ((tp->rx_ret_ring_mask + 1) <<
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008290 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
8291
8292 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008293 rxrcb += TG3_BDINFO_SIZE;
8294 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008295}
8296
Matt Carlsoneb07a942011-04-20 07:57:36 +00008297static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
8298{
8299 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
8300
Joe Perches63c3a662011-04-26 08:12:10 +00008301 if (!tg3_flag(tp, 5750_PLUS) ||
8302 tg3_flag(tp, 5780_CLASS) ||
Matt Carlsoneb07a942011-04-20 07:57:36 +00008303 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
Matt Carlson513aa6e2011-11-21 15:01:18 +00008304 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
8305 tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008306 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
8307 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
8308 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787)
8309 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
8310 else
8311 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
8312
8313 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
8314 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
8315
8316 val = min(nic_rep_thresh, host_rep_thresh);
8317 tw32(RCVBDI_STD_THRESH, val);
8318
Joe Perches63c3a662011-04-26 08:12:10 +00008319 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008320 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
8321
Joe Perches63c3a662011-04-26 08:12:10 +00008322 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008323 return;
8324
Matt Carlson513aa6e2011-11-21 15:01:18 +00008325 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
Matt Carlsoneb07a942011-04-20 07:57:36 +00008326
8327 host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
8328
8329 val = min(bdcache_maxcnt / 2, host_rep_thresh);
8330 tw32(RCVBDI_JUMBO_THRESH, val);
8331
Joe Perches63c3a662011-04-26 08:12:10 +00008332 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008333 tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
8334}
8335
Matt Carlsonccd5ba92012-02-13 10:20:08 +00008336static inline u32 calc_crc(unsigned char *buf, int len)
8337{
8338 u32 reg;
8339 u32 tmp;
8340 int j, k;
8341
8342 reg = 0xffffffff;
8343
8344 for (j = 0; j < len; j++) {
8345 reg ^= buf[j];
8346
8347 for (k = 0; k < 8; k++) {
8348 tmp = reg & 0x01;
8349
8350 reg >>= 1;
8351
8352 if (tmp)
8353 reg ^= 0xedb88320;
8354 }
8355 }
8356
8357 return ~reg;
8358}
8359
8360static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
8361{
8362 /* accept or reject all multicast frames */
8363 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
8364 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
8365 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
8366 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
8367}
8368
8369static void __tg3_set_rx_mode(struct net_device *dev)
8370{
8371 struct tg3 *tp = netdev_priv(dev);
8372 u32 rx_mode;
8373
8374 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
8375 RX_MODE_KEEP_VLAN_TAG);
8376
8377#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
8378 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
8379 * flag clear.
8380 */
8381 if (!tg3_flag(tp, ENABLE_ASF))
8382 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
8383#endif
8384
8385 if (dev->flags & IFF_PROMISC) {
8386 /* Promiscuous mode. */
8387 rx_mode |= RX_MODE_PROMISC;
8388 } else if (dev->flags & IFF_ALLMULTI) {
8389 /* Accept all multicast. */
8390 tg3_set_multi(tp, 1);
8391 } else if (netdev_mc_empty(dev)) {
8392 /* Reject all multicast. */
8393 tg3_set_multi(tp, 0);
8394 } else {
8395 /* Accept one or more multicast(s). */
8396 struct netdev_hw_addr *ha;
8397 u32 mc_filter[4] = { 0, };
8398 u32 regidx;
8399 u32 bit;
8400 u32 crc;
8401
8402 netdev_for_each_mc_addr(ha, dev) {
8403 crc = calc_crc(ha->addr, ETH_ALEN);
8404 bit = ~crc & 0x7f;
8405 regidx = (bit & 0x60) >> 5;
8406 bit &= 0x1f;
8407 mc_filter[regidx] |= (1 << bit);
8408 }
8409
8410 tw32(MAC_HASH_REG_0, mc_filter[0]);
8411 tw32(MAC_HASH_REG_1, mc_filter[1]);
8412 tw32(MAC_HASH_REG_2, mc_filter[2]);
8413 tw32(MAC_HASH_REG_3, mc_filter[3]);
8414 }
8415
8416 if (rx_mode != tp->rx_mode) {
8417 tp->rx_mode = rx_mode;
8418 tw32_f(MAC_RX_MODE, rx_mode);
8419 udelay(10);
8420 }
8421}
8422
Matt Carlson90415472011-12-16 13:33:23 +00008423static void tg3_rss_init_dflt_indir_tbl(struct tg3 *tp)
8424{
8425 int i;
8426
8427 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
8428 tp->rss_ind_tbl[i] =
8429 ethtool_rxfh_indir_default(i, tp->irq_cnt - 1);
8430}
8431
8432static void tg3_rss_check_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008433{
8434 int i;
8435
8436 if (!tg3_flag(tp, SUPPORT_MSIX))
8437 return;
8438
Matt Carlson90415472011-12-16 13:33:23 +00008439 if (tp->irq_cnt <= 2) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008440 memset(&tp->rss_ind_tbl[0], 0, sizeof(tp->rss_ind_tbl));
Matt Carlson90415472011-12-16 13:33:23 +00008441 return;
8442 }
8443
8444 /* Validate table against current IRQ count */
8445 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
8446 if (tp->rss_ind_tbl[i] >= tp->irq_cnt - 1)
8447 break;
8448 }
8449
8450 if (i != TG3_RSS_INDIR_TBL_SIZE)
8451 tg3_rss_init_dflt_indir_tbl(tp);
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008452}
8453
Matt Carlson90415472011-12-16 13:33:23 +00008454static void tg3_rss_write_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008455{
8456 int i = 0;
8457 u32 reg = MAC_RSS_INDIR_TBL_0;
8458
8459 while (i < TG3_RSS_INDIR_TBL_SIZE) {
8460 u32 val = tp->rss_ind_tbl[i];
8461 i++;
8462 for (; i % 8; i++) {
8463 val <<= 4;
8464 val |= tp->rss_ind_tbl[i];
8465 }
8466 tw32(reg, val);
8467 reg += 4;
8468 }
8469}
8470
Matt Carlson2d31eca2009-09-01 12:53:31 +00008471/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008472static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008473{
8474 u32 val, rdmac_mode;
8475 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00008476 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008477
8478 tg3_disable_ints(tp);
8479
8480 tg3_stop_fw(tp);
8481
8482 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
8483
Joe Perches63c3a662011-04-26 08:12:10 +00008484 if (tg3_flag(tp, INIT_COMPLETE))
Michael Chane6de8ad2005-05-05 14:42:41 -07008485 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008486
Matt Carlson699c0192010-12-06 08:28:51 +00008487 /* Enable MAC control of LPI */
8488 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
8489 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL,
8490 TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
8491 TG3_CPMU_EEE_LNKIDL_UART_IDL);
8492
8493 tw32_f(TG3_CPMU_EEE_CTRL,
8494 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
8495
Matt Carlsona386b902010-12-06 08:28:53 +00008496 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
8497 TG3_CPMU_EEEMD_LPI_IN_TX |
8498 TG3_CPMU_EEEMD_LPI_IN_RX |
8499 TG3_CPMU_EEEMD_EEE_ENABLE;
8500
8501 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8502 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
8503
Joe Perches63c3a662011-04-26 08:12:10 +00008504 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsona386b902010-12-06 08:28:53 +00008505 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
8506
8507 tw32_f(TG3_CPMU_EEE_MODE, val);
8508
8509 tw32_f(TG3_CPMU_EEE_DBTMR1,
8510 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
8511 TG3_CPMU_DBTMR1_LNKIDLE_2047US);
8512
8513 tw32_f(TG3_CPMU_EEE_DBTMR2,
Matt Carlsond7f2ab22011-01-25 15:58:56 +00008514 TG3_CPMU_DBTMR2_APE_TX_2047US |
Matt Carlsona386b902010-12-06 08:28:53 +00008515 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
Matt Carlson699c0192010-12-06 08:28:51 +00008516 }
8517
Matt Carlson603f1172010-02-12 14:47:10 +00008518 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08008519 tg3_phy_reset(tp);
8520
Linus Torvalds1da177e2005-04-16 15:20:36 -07008521 err = tg3_chip_reset(tp);
8522 if (err)
8523 return err;
8524
8525 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
8526
Matt Carlsonbcb37f62008-11-03 16:52:09 -08008527 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008528 val = tr32(TG3_CPMU_CTRL);
8529 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
8530 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08008531
8532 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8533 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8534 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8535 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
8536
8537 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
8538 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
8539 val |= CPMU_LNK_AWARE_MACCLK_6_25;
8540 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
8541
8542 val = tr32(TG3_CPMU_HST_ACC);
8543 val &= ~CPMU_HST_ACC_MACCLK_MASK;
8544 val |= CPMU_HST_ACC_MACCLK_6_25;
8545 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07008546 }
8547
Matt Carlson33466d92009-04-20 06:57:41 +00008548 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
8549 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
8550 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
8551 PCIE_PWR_MGMT_L1_THRESH_4MS;
8552 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00008553
8554 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
8555 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
8556
8557 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00008558
Matt Carlsonf40386c2009-11-02 14:24:02 +00008559 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
8560 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00008561 }
8562
Joe Perches63c3a662011-04-26 08:12:10 +00008563 if (tg3_flag(tp, L1PLLPD_EN)) {
Matt Carlson614b0592010-01-20 16:58:02 +00008564 u32 grc_mode = tr32(GRC_MODE);
8565
8566 /* Access the lower 1K of PL PCIE block registers. */
8567 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8568 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
8569
8570 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
8571 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
8572 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
8573
8574 tw32(GRC_MODE, grc_mode);
8575 }
8576
Matt Carlson55086ad2011-12-14 11:09:59 +00008577 if (tg3_flag(tp, 57765_CLASS)) {
Matt Carlson5093eed2010-11-24 08:31:45 +00008578 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
8579 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00008580
Matt Carlson5093eed2010-11-24 08:31:45 +00008581 /* Access the lower 1K of PL PCIE block registers. */
8582 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8583 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00008584
Matt Carlson5093eed2010-11-24 08:31:45 +00008585 val = tr32(TG3_PCIE_TLDLPL_PORT +
8586 TG3_PCIE_PL_LO_PHYCTL5);
8587 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
8588 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00008589
Matt Carlson5093eed2010-11-24 08:31:45 +00008590 tw32(GRC_MODE, grc_mode);
8591 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00008592
Matt Carlson1ff30a52011-05-19 12:12:46 +00008593 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_57765_AX) {
8594 u32 grc_mode = tr32(GRC_MODE);
8595
8596 /* Access the lower 1K of DL PCIE block registers. */
8597 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8598 tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
8599
8600 val = tr32(TG3_PCIE_TLDLPL_PORT +
8601 TG3_PCIE_DL_LO_FTSMAX);
8602 val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
8603 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
8604 val | TG3_PCIE_DL_LO_FTSMAX_VAL);
8605
8606 tw32(GRC_MODE, grc_mode);
8607 }
8608
Matt Carlsona977dbe2010-04-12 06:58:26 +00008609 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8610 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8611 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8612 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00008613 }
8614
Linus Torvalds1da177e2005-04-16 15:20:36 -07008615 /* This works around an issue with Athlon chipsets on
8616 * B3 tigon3 silicon. This bit has no effect on any
8617 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07008618 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008619 */
Joe Perches63c3a662011-04-26 08:12:10 +00008620 if (!tg3_flag(tp, CPMU_PRESENT)) {
8621 if (!tg3_flag(tp, PCI_EXPRESS))
Matt Carlson795d01c2007-10-07 23:28:17 -07008622 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
8623 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
8624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008625
8626 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008627 tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008628 val = tr32(TG3PCI_PCISTATE);
8629 val |= PCISTATE_RETRY_SAME_DMA;
8630 tw32(TG3PCI_PCISTATE, val);
8631 }
8632
Joe Perches63c3a662011-04-26 08:12:10 +00008633 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -07008634 /* Allow reads and writes to the
8635 * APE register and memory space.
8636 */
8637 val = tr32(TG3PCI_PCISTATE);
8638 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00008639 PCISTATE_ALLOW_APE_SHMEM_WR |
8640 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008641 tw32(TG3PCI_PCISTATE, val);
8642 }
8643
Linus Torvalds1da177e2005-04-16 15:20:36 -07008644 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
8645 /* Enable some hw fixes. */
8646 val = tr32(TG3PCI_MSI_DATA);
8647 val |= (1 << 26) | (1 << 28) | (1 << 29);
8648 tw32(TG3PCI_MSI_DATA, val);
8649 }
8650
8651 /* Descriptor ring init may make accesses to the
8652 * NIC SRAM area to setup the TX descriptors, so we
8653 * can only do this after the hardware has been
8654 * successfully reset.
8655 */
Michael Chan32d8c572006-07-25 16:38:29 -07008656 err = tg3_init_rings(tp);
8657 if (err)
8658 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008659
Joe Perches63c3a662011-04-26 08:12:10 +00008660 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008661 val = tr32(TG3PCI_DMA_RW_CTRL) &
8662 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Matt Carlson1a319022010-04-12 06:58:25 +00008663 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
8664 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlson55086ad2011-12-14 11:09:59 +00008665 if (!tg3_flag(tp, 57765_CLASS) &&
Matt Carlson0aebff42011-04-25 12:42:45 +00008666 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8667 val |= DMA_RWCTRL_TAGGED_STAT_WA;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008668 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
8669 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
8670 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008671 /* This value is determined during the probe time DMA
8672 * engine test, tg3_test_dma.
8673 */
8674 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
8675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008676
8677 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
8678 GRC_MODE_4X_NIC_SEND_RINGS |
8679 GRC_MODE_NO_TX_PHDR_CSUM |
8680 GRC_MODE_NO_RX_PHDR_CSUM);
8681 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07008682
8683 /* Pseudo-header checksum is done by hardware logic and not
8684 * the offload processers, so make the chip do the pseudo-
8685 * header checksums on receive. For transmit it is more
8686 * convenient to do the pseudo-header checksum in software
8687 * as Linux does that on transmit for us in all cases.
8688 */
8689 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008690
8691 tw32(GRC_MODE,
8692 tp->grc_mode |
8693 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
8694
8695 /* Setup the timer prescalar register. Clock is always 66Mhz. */
8696 val = tr32(GRC_MISC_CFG);
8697 val &= ~0xff;
8698 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
8699 tw32(GRC_MISC_CFG, val);
8700
8701 /* Initialize MBUF/DESC pool. */
Joe Perches63c3a662011-04-26 08:12:10 +00008702 if (tg3_flag(tp, 5750_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008703 /* Do nothing. */
8704 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
8705 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
8706 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
8707 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
8708 else
8709 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
8710 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
8711 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Joe Perches63c3a662011-04-26 08:12:10 +00008712 } else if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008713 int fw_len;
8714
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08008715 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008716 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
8717 tw32(BUFMGR_MB_POOL_ADDR,
8718 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
8719 tw32(BUFMGR_MB_POOL_SIZE,
8720 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
8721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008722
Michael Chan0f893dc2005-07-25 12:30:38 -07008723 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008724 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8725 tp->bufmgr_config.mbuf_read_dma_low_water);
8726 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8727 tp->bufmgr_config.mbuf_mac_rx_low_water);
8728 tw32(BUFMGR_MB_HIGH_WATER,
8729 tp->bufmgr_config.mbuf_high_water);
8730 } else {
8731 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8732 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
8733 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8734 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
8735 tw32(BUFMGR_MB_HIGH_WATER,
8736 tp->bufmgr_config.mbuf_high_water_jumbo);
8737 }
8738 tw32(BUFMGR_DMA_LOW_WATER,
8739 tp->bufmgr_config.dma_low_water);
8740 tw32(BUFMGR_DMA_HIGH_WATER,
8741 tp->bufmgr_config.dma_high_water);
8742
Matt Carlsond309a462010-09-30 10:34:31 +00008743 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
8744 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
8745 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
Matt Carlson4d958472011-04-20 07:57:35 +00008746 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
8747 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
8748 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0)
8749 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
Matt Carlsond309a462010-09-30 10:34:31 +00008750 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008751 for (i = 0; i < 2000; i++) {
8752 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
8753 break;
8754 udelay(10);
8755 }
8756 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00008757 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008758 return -ENODEV;
8759 }
8760
Matt Carlsoneb07a942011-04-20 07:57:36 +00008761 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
8762 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
Michael Chanb5d37722006-09-27 16:06:21 -07008763
Matt Carlsoneb07a942011-04-20 07:57:36 +00008764 tg3_setup_rxbd_thresholds(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008765
8766 /* Initialize TG3_BDINFO's at:
8767 * RCVDBDI_STD_BD: standard eth size rx ring
8768 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
8769 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
8770 *
8771 * like so:
8772 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
8773 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
8774 * ring attribute flags
8775 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
8776 *
8777 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
8778 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
8779 *
8780 * The size of each ring is fixed in the firmware, but the location is
8781 * configurable.
8782 */
8783 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008784 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008785 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008786 ((u64) tpr->rx_std_mapping & 0xffffffff));
Joe Perches63c3a662011-04-26 08:12:10 +00008787 if (!tg3_flag(tp, 5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +00008788 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
8789 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008790
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008791 /* Disable the mini ring */
Joe Perches63c3a662011-04-26 08:12:10 +00008792 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008793 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
8794 BDINFO_FLAGS_DISABLED);
8795
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008796 /* Program the jumbo buffer descriptor ring control
8797 * blocks on those devices that have them.
8798 */
Matt Carlsona0512942011-07-27 14:20:54 +00008799 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008800 (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008801
Joe Perches63c3a662011-04-26 08:12:10 +00008802 if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008803 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008804 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008805 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008806 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +00008807 val = TG3_RX_JMB_RING_SIZE(tp) <<
8808 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008809 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +00008810 val | BDINFO_FLAGS_USE_EXT_RECV);
Joe Perches63c3a662011-04-26 08:12:10 +00008811 if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
Matt Carlson55086ad2011-12-14 11:09:59 +00008812 tg3_flag(tp, 57765_CLASS))
Matt Carlson87668d32009-11-13 13:03:34 +00008813 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
8814 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008815 } else {
8816 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
8817 BDINFO_FLAGS_DISABLED);
8818 }
8819
Joe Perches63c3a662011-04-26 08:12:10 +00008820 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonfa6b2aa2011-11-21 15:01:19 +00008821 val = TG3_RX_STD_RING_SIZE(tp);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008822 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
8823 val |= (TG3_RX_STD_DMA_SZ << 2);
8824 } else
Matt Carlson04380d42010-04-12 06:58:29 +00008825 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008826 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008827 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008828
8829 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008830
Matt Carlson411da642009-11-13 13:03:46 +00008831 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e62009-11-13 13:03:49 +00008832 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008833
Joe Perches63c3a662011-04-26 08:12:10 +00008834 tpr->rx_jmb_prod_idx =
8835 tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
Matt Carlson66711e62009-11-13 13:03:49 +00008836 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008837
Matt Carlson2d31eca2009-09-01 12:53:31 +00008838 tg3_rings_reset(tp);
8839
Linus Torvalds1da177e2005-04-16 15:20:36 -07008840 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07008841 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008842
8843 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00008844 tw32(MAC_RX_MTU_SIZE,
8845 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008846
8847 /* The slot time is changed by tg3_setup_phy if we
8848 * run at gigabit with half duplex.
8849 */
Matt Carlsonf2096f92011-04-05 14:22:48 +00008850 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
8851 (6 << TX_LENGTHS_IPG_SHIFT) |
8852 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
8853
8854 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8855 val |= tr32(MAC_TX_LENGTHS) &
8856 (TX_LENGTHS_JMB_FRM_LEN_MSK |
8857 TX_LENGTHS_CNT_DWN_VAL_MSK);
8858
8859 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008860
8861 /* Receive rules. */
8862 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
8863 tw32(RCVLPC_CONFIG, 0x0181);
8864
8865 /* Calculate RDMAC_MODE setting early, we need it to determine
8866 * the RCVLPC_STATE_ENABLE mask.
8867 */
8868 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
8869 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
8870 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
8871 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
8872 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07008873
Matt Carlsondeabaac2010-11-24 08:31:50 +00008874 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +00008875 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
8876
Matt Carlson57e69832008-05-25 23:48:31 -07008877 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08008878 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8879 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07008880 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
8881 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
8882 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
8883
Matt Carlsonc5908932011-03-09 16:58:25 +00008884 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8885 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00008886 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07008887 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008888 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
8889 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008890 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008891 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8892 }
8893 }
8894
Joe Perches63c3a662011-04-26 08:12:10 +00008895 if (tg3_flag(tp, PCI_EXPRESS))
Michael Chan85e94ce2005-04-21 17:05:28 -07008896 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8897
Joe Perches63c3a662011-04-26 08:12:10 +00008898 if (tg3_flag(tp, HW_TSO_1) ||
8899 tg3_flag(tp, HW_TSO_2) ||
8900 tg3_flag(tp, HW_TSO_3))
Matt Carlson027455a2008-12-21 20:19:30 -08008901 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
8902
Matt Carlson108a6c12011-05-19 12:12:47 +00008903 if (tg3_flag(tp, 57765_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +00008904 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson027455a2008-12-21 20:19:30 -08008905 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
8906 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008907
Matt Carlsonf2096f92011-04-05 14:22:48 +00008908 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8909 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
8910
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008911 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
8912 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
8913 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8914 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008915 tg3_flag(tp, 57765_PLUS)) {
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008916 val = tr32(TG3_RDMA_RSRVCTRL_REG);
Matt Carlsond78b59f2011-04-05 14:22:46 +00008917 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8918 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +00008919 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
8920 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
8921 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
8922 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
8923 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
8924 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +00008925 }
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008926 tw32(TG3_RDMA_RSRVCTRL_REG,
8927 val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
8928 }
8929
Matt Carlsond78b59f2011-04-05 14:22:46 +00008930 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8931 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsond309a462010-09-30 10:34:31 +00008932 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
8933 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val |
8934 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
8935 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
8936 }
8937
Linus Torvalds1da177e2005-04-16 15:20:36 -07008938 /* Receive/send statistics. */
Joe Perches63c3a662011-04-26 08:12:10 +00008939 if (tg3_flag(tp, 5750_PLUS)) {
Michael Chan16613942006-06-29 20:15:13 -07008940 val = tr32(RCVLPC_STATS_ENABLE);
8941 val &= ~RCVLPC_STATSENAB_DACK_FIX;
8942 tw32(RCVLPC_STATS_ENABLE, val);
8943 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008944 tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008945 val = tr32(RCVLPC_STATS_ENABLE);
8946 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
8947 tw32(RCVLPC_STATS_ENABLE, val);
8948 } else {
8949 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
8950 }
8951 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
8952 tw32(SNDDATAI_STATSENAB, 0xffffff);
8953 tw32(SNDDATAI_STATSCTRL,
8954 (SNDDATAI_SCTRL_ENABLE |
8955 SNDDATAI_SCTRL_FASTUPD));
8956
8957 /* Setup host coalescing engine. */
8958 tw32(HOSTCC_MODE, 0);
8959 for (i = 0; i < 2000; i++) {
8960 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
8961 break;
8962 udelay(10);
8963 }
8964
Michael Chand244c892005-07-05 14:42:33 -07008965 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008966
Joe Perches63c3a662011-04-26 08:12:10 +00008967 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008968 /* Status/statistics block address. See tg3_timer,
8969 * the tg3_periodic_fetch_stats call there, and
8970 * tg3_get_stats to see how this works for 5705/5750 chips.
8971 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008972 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8973 ((u64) tp->stats_mapping >> 32));
8974 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8975 ((u64) tp->stats_mapping & 0xffffffff));
8976 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008977
Linus Torvalds1da177e2005-04-16 15:20:36 -07008978 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008979
8980 /* Clear statistics and status block memory areas */
8981 for (i = NIC_SRAM_STATS_BLK;
8982 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
8983 i += sizeof(u32)) {
8984 tg3_write_mem(tp, i, 0);
8985 udelay(40);
8986 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008987 }
8988
8989 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
8990
8991 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
8992 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008993 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008994 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
8995
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008996 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
8997 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -07008998 /* reset to prevent losing 1st rx packet intermittently */
8999 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
9000 udelay(10);
9001 }
9002
Matt Carlson3bda1252008-08-15 14:08:22 -07009003 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Matt Carlson9e975cc2011-07-20 10:20:50 +00009004 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE |
9005 MAC_MODE_FHDE_ENABLE;
9006 if (tg3_flag(tp, ENABLE_APE))
9007 tp->mac_mode |= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Joe Perches63c3a662011-04-26 08:12:10 +00009008 if (!tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009009 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07009010 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
9011 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009012 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
9013 udelay(40);
9014
Michael Chan314fba32005-04-21 17:07:04 -07009015 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Joe Perches63c3a662011-04-26 08:12:10 +00009016 * If TG3_FLAG_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07009017 * register to preserve the GPIO settings for LOMs. The GPIOs,
9018 * whether used as inputs or outputs, are set by boot code after
9019 * reset.
9020 */
Joe Perches63c3a662011-04-26 08:12:10 +00009021 if (!tg3_flag(tp, IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07009022 u32 gpio_mask;
9023
Michael Chan9d26e212006-12-07 00:21:14 -08009024 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
9025 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
9026 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07009027
9028 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
9029 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
9030 GRC_LCLCTRL_GPIO_OUTPUT3;
9031
Michael Chanaf36e6b2006-03-23 01:28:06 -08009032 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
9033 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
9034
Gary Zambranoaaf84462007-05-05 11:51:45 -07009035 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07009036 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
9037
9038 /* GPIO1 must be driven high for eeprom write protect */
Joe Perches63c3a662011-04-26 08:12:10 +00009039 if (tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan9d26e212006-12-07 00:21:14 -08009040 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
9041 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07009042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009043 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
9044 udelay(100);
9045
Matt Carlsonc3b50032012-01-17 15:27:23 +00009046 if (tg3_flag(tp, USING_MSIX)) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009047 val = tr32(MSGINT_MODE);
Matt Carlsonc3b50032012-01-17 15:27:23 +00009048 val |= MSGINT_MODE_ENABLE;
9049 if (tp->irq_cnt > 1)
9050 val |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +00009051 if (!tg3_flag(tp, 1SHOT_MSI))
9052 val |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009053 tw32(MSGINT_MODE, val);
9054 }
9055
Joe Perches63c3a662011-04-26 08:12:10 +00009056 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009057 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
9058 udelay(40);
9059 }
9060
9061 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
9062 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
9063 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
9064 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
9065 WDMAC_MODE_LNGREAD_ENAB);
9066
Matt Carlsonc5908932011-03-09 16:58:25 +00009067 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
9068 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00009069 if (tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009070 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
9071 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
9072 /* nothing */
9073 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009074 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009075 val |= WDMAC_MODE_RX_ACCEL;
9076 }
9077 }
9078
Michael Chand9ab5ad2006-03-20 22:27:35 -08009079 /* Enable host coalescing bug fix */
Joe Perches63c3a662011-04-26 08:12:10 +00009080 if (tg3_flag(tp, 5755_PLUS))
Matt Carlsonf51f3562008-05-25 23:45:08 -07009081 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad2006-03-20 22:27:35 -08009082
Matt Carlson788a0352009-11-02 14:26:03 +00009083 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
9084 val |= WDMAC_MODE_BURST_ALL_DATA;
9085
Linus Torvalds1da177e2005-04-16 15:20:36 -07009086 tw32_f(WDMAC_MODE, val);
9087 udelay(40);
9088
Joe Perches63c3a662011-04-26 08:12:10 +00009089 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07009090 u16 pcix_cmd;
9091
9092 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
9093 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009094 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07009095 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
9096 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009097 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07009098 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
9099 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009100 }
Matt Carlson9974a352007-10-07 23:27:28 -07009101 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
9102 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009103 }
9104
9105 tw32_f(RDMAC_MODE, rdmac_mode);
9106 udelay(40);
9107
9108 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00009109 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009110 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07009111
9112 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
9113 tw32(SNDDATAC_MODE,
9114 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
9115 else
9116 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
9117
Linus Torvalds1da177e2005-04-16 15:20:36 -07009118 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
9119 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00009120 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00009121 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlson7cb32cf2010-09-30 10:34:36 +00009122 val |= RCVDBDI_MODE_LRG_RING_SZ;
9123 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009124 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00009125 if (tg3_flag(tp, HW_TSO_1) ||
9126 tg3_flag(tp, HW_TSO_2) ||
9127 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009128 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009129 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00009130 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009131 val |= SNDBDI_MODE_MULTI_TXQ_EN;
9132 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009133 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
9134
9135 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
9136 err = tg3_load_5701_a0_firmware_fix(tp);
9137 if (err)
9138 return err;
9139 }
9140
Joe Perches63c3a662011-04-26 08:12:10 +00009141 if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009142 err = tg3_load_tso_firmware(tp);
9143 if (err)
9144 return err;
9145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009146
9147 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +00009148
Joe Perches63c3a662011-04-26 08:12:10 +00009149 if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsonb1d05212010-06-05 17:24:31 +00009150 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
9151 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +00009152
9153 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
9154 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
9155 tp->tx_mode &= ~val;
9156 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
9157 }
9158
Linus Torvalds1da177e2005-04-16 15:20:36 -07009159 tw32_f(MAC_TX_MODE, tp->tx_mode);
9160 udelay(100);
9161
Joe Perches63c3a662011-04-26 08:12:10 +00009162 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009163 tg3_rss_write_indir_tbl(tp);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009164
9165 /* Setup the "secret" hash key. */
9166 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
9167 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
9168 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
9169 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
9170 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
9171 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
9172 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
9173 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
9174 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
9175 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
9176 }
9177
Linus Torvalds1da177e2005-04-16 15:20:36 -07009178 tp->rx_mode = RX_MODE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00009179 if (tg3_flag(tp, 5755_PLUS))
Michael Chanaf36e6b2006-03-23 01:28:06 -08009180 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
9181
Joe Perches63c3a662011-04-26 08:12:10 +00009182 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009183 tp->rx_mode |= RX_MODE_RSS_ENABLE |
9184 RX_MODE_RSS_ITBL_HASH_BITS_7 |
9185 RX_MODE_RSS_IPV6_HASH_EN |
9186 RX_MODE_RSS_TCP_IPV6_HASH_EN |
9187 RX_MODE_RSS_IPV4_HASH_EN |
9188 RX_MODE_RSS_TCP_IPV4_HASH_EN;
9189
Linus Torvalds1da177e2005-04-16 15:20:36 -07009190 tw32_f(MAC_RX_MODE, tp->rx_mode);
9191 udelay(10);
9192
Linus Torvalds1da177e2005-04-16 15:20:36 -07009193 tw32(MAC_LED_CTRL, tp->led_ctrl);
9194
9195 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009196 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009197 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
9198 udelay(10);
9199 }
9200 tw32_f(MAC_RX_MODE, tp->rx_mode);
9201 udelay(10);
9202
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009203 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009204 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009205 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009206 /* Set drive transmission level to 1.2V */
9207 /* only if the signal pre-emphasis bit is not set */
9208 val = tr32(MAC_SERDES_CFG);
9209 val &= 0xfffff000;
9210 val |= 0x880;
9211 tw32(MAC_SERDES_CFG, val);
9212 }
9213 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
9214 tw32(MAC_SERDES_CFG, 0x616000);
9215 }
9216
9217 /* Prevent chip from dropping frames when flow control
9218 * is enabled.
9219 */
Matt Carlson55086ad2011-12-14 11:09:59 +00009220 if (tg3_flag(tp, 57765_CLASS))
Matt Carlson666bc832010-01-20 16:58:03 +00009221 val = 1;
9222 else
9223 val = 2;
9224 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009225
9226 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009227 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009228 /* Use hardware link auto-negotiation */
Joe Perches63c3a662011-04-26 08:12:10 +00009229 tg3_flag_set(tp, HW_AUTONEG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009230 }
9231
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009232 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +00009233 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08009234 u32 tmp;
9235
9236 tmp = tr32(SERDES_RX_CTRL);
9237 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
9238 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
9239 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
9240 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
9241 }
9242
Joe Perches63c3a662011-04-26 08:12:10 +00009243 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonc6700ce2012-02-13 15:20:15 +00009244 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Matt Carlson800960682010-08-02 11:26:06 +00009245 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009246
Matt Carlsondd477002008-05-25 23:45:58 -07009247 err = tg3_setup_phy(tp, 0);
9248 if (err)
9249 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009250
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009251 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
9252 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07009253 u32 tmp;
9254
9255 /* Clear CRC stats. */
9256 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
9257 tg3_writephy(tp, MII_TG3_TEST1,
9258 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009259 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -07009260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009261 }
9262 }
9263
9264 __tg3_set_rx_mode(tp->dev);
9265
9266 /* Initialize receive rules. */
9267 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
9268 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
9269 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
9270 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
9271
Joe Perches63c3a662011-04-26 08:12:10 +00009272 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009273 limit = 8;
9274 else
9275 limit = 16;
Joe Perches63c3a662011-04-26 08:12:10 +00009276 if (tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009277 limit -= 4;
9278 switch (limit) {
9279 case 16:
9280 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
9281 case 15:
9282 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
9283 case 14:
9284 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
9285 case 13:
9286 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
9287 case 12:
9288 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
9289 case 11:
9290 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
9291 case 10:
9292 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
9293 case 9:
9294 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
9295 case 8:
9296 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
9297 case 7:
9298 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
9299 case 6:
9300 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
9301 case 5:
9302 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
9303 case 4:
9304 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
9305 case 3:
9306 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
9307 case 2:
9308 case 1:
9309
9310 default:
9311 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07009312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009313
Joe Perches63c3a662011-04-26 08:12:10 +00009314 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson9ce768e2007-10-11 19:49:11 -07009315 /* Write our heartbeat update interval to APE. */
9316 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
9317 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07009318
Linus Torvalds1da177e2005-04-16 15:20:36 -07009319 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
9320
Linus Torvalds1da177e2005-04-16 15:20:36 -07009321 return 0;
9322}
9323
9324/* Called at device open time to get the chip ready for
9325 * packet processing. Invoked with tp->lock held.
9326 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009327static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009328{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009329 tg3_switch_clocks(tp);
9330
9331 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
9332
Matt Carlson2f751b62008-08-04 23:17:34 -07009333 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009334}
9335
9336#define TG3_STAT_ADD32(PSTAT, REG) \
9337do { u32 __val = tr32(REG); \
9338 (PSTAT)->low += __val; \
9339 if ((PSTAT)->low < __val) \
9340 (PSTAT)->high += 1; \
9341} while (0)
9342
9343static void tg3_periodic_fetch_stats(struct tg3 *tp)
9344{
9345 struct tg3_hw_stats *sp = tp->hw_stats;
9346
9347 if (!netif_carrier_ok(tp->dev))
9348 return;
9349
9350 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
9351 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
9352 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
9353 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
9354 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
9355 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
9356 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
9357 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
9358 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
9359 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
9360 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
9361 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
9362 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
9363
9364 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
9365 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
9366 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
9367 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
9368 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
9369 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
9370 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
9371 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
9372 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
9373 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
9374 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
9375 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
9376 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
9377 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07009378
9379 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
Matt Carlson310050f2011-05-19 12:12:55 +00009380 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
9381 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0 &&
9382 tp->pci_chip_rev_id != CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +00009383 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
9384 } else {
9385 u32 val = tr32(HOSTCC_FLOW_ATTN);
9386 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
9387 if (val) {
9388 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
9389 sp->rx_discards.low += val;
9390 if (sp->rx_discards.low < val)
9391 sp->rx_discards.high += 1;
9392 }
9393 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
9394 }
Michael Chan463d3052006-05-22 16:36:27 -07009395 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009396}
9397
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009398static void tg3_chk_missed_msi(struct tg3 *tp)
9399{
9400 u32 i;
9401
9402 for (i = 0; i < tp->irq_cnt; i++) {
9403 struct tg3_napi *tnapi = &tp->napi[i];
9404
9405 if (tg3_has_work(tnapi)) {
9406 if (tnapi->last_rx_cons == tnapi->rx_rcb_ptr &&
9407 tnapi->last_tx_cons == tnapi->tx_cons) {
9408 if (tnapi->chk_msi_cnt < 1) {
9409 tnapi->chk_msi_cnt++;
9410 return;
9411 }
Matt Carlson7f230732011-08-31 11:44:48 +00009412 tg3_msi(0, tnapi);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009413 }
9414 }
9415 tnapi->chk_msi_cnt = 0;
9416 tnapi->last_rx_cons = tnapi->rx_rcb_ptr;
9417 tnapi->last_tx_cons = tnapi->tx_cons;
9418 }
9419}
9420
Linus Torvalds1da177e2005-04-16 15:20:36 -07009421static void tg3_timer(unsigned long __opaque)
9422{
9423 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009424
Matt Carlson5b190622011-11-04 09:15:04 +00009425 if (tp->irq_sync || tg3_flag(tp, RESET_TASK_PENDING))
Michael Chanf475f162006-03-27 23:20:14 -08009426 goto restart_timer;
9427
David S. Millerf47c11e2005-06-24 20:18:35 -07009428 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009429
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009430 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00009431 tg3_flag(tp, 57765_CLASS))
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009432 tg3_chk_missed_msi(tp);
9433
Joe Perches63c3a662011-04-26 08:12:10 +00009434 if (!tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -07009435 /* All of this garbage is because when using non-tagged
9436 * IRQ status the mailbox/status_block protocol the chip
9437 * uses with the cpu is race prone.
9438 */
Matt Carlson898a56f2009-08-28 14:02:40 +00009439 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -07009440 tw32(GRC_LOCAL_CTRL,
9441 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
9442 } else {
9443 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00009444 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -07009445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009446
David S. Millerfac9b832005-05-18 22:46:34 -07009447 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
David S. Millerf47c11e2005-06-24 20:18:35 -07009448 spin_unlock(&tp->lock);
Matt Carlsondb219972011-11-04 09:15:03 +00009449 tg3_reset_task_schedule(tp);
Matt Carlson5b190622011-11-04 09:15:04 +00009450 goto restart_timer;
David S. Millerfac9b832005-05-18 22:46:34 -07009451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009452 }
9453
Linus Torvalds1da177e2005-04-16 15:20:36 -07009454 /* This part only runs once per second. */
9455 if (!--tp->timer_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00009456 if (tg3_flag(tp, 5705_PLUS))
David S. Millerfac9b832005-05-18 22:46:34 -07009457 tg3_periodic_fetch_stats(tp);
9458
Matt Carlsonb0c59432011-05-19 12:12:48 +00009459 if (tp->setlpicnt && !--tp->setlpicnt)
9460 tg3_phy_eee_enable(tp);
Matt Carlson52b02d02010-10-14 10:37:41 +00009461
Joe Perches63c3a662011-04-26 08:12:10 +00009462 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009463 u32 mac_stat;
9464 int phy_event;
9465
9466 mac_stat = tr32(MAC_STATUS);
9467
9468 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009469 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009470 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
9471 phy_event = 1;
9472 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
9473 phy_event = 1;
9474
9475 if (phy_event)
9476 tg3_setup_phy(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +00009477 } else if (tg3_flag(tp, POLL_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009478 u32 mac_stat = tr32(MAC_STATUS);
9479 int need_setup = 0;
9480
9481 if (netif_carrier_ok(tp->dev) &&
9482 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
9483 need_setup = 1;
9484 }
Matt Carlsonbe98da62010-07-11 09:31:46 +00009485 if (!netif_carrier_ok(tp->dev) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009486 (mac_stat & (MAC_STATUS_PCS_SYNCED |
9487 MAC_STATUS_SIGNAL_DET))) {
9488 need_setup = 1;
9489 }
9490 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07009491 if (!tp->serdes_counter) {
9492 tw32_f(MAC_MODE,
9493 (tp->mac_mode &
9494 ~MAC_MODE_PORT_MODE_MASK));
9495 udelay(40);
9496 tw32_f(MAC_MODE, tp->mac_mode);
9497 udelay(40);
9498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009499 tg3_setup_phy(tp, 0);
9500 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009501 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009502 tg3_flag(tp, 5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -07009503 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +00009504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009505
9506 tp->timer_counter = tp->timer_multiplier;
9507 }
9508
Michael Chan130b8e42006-09-27 16:00:40 -07009509 /* Heartbeat is only sent once every 2 seconds.
9510 *
9511 * The heartbeat is to tell the ASF firmware that the host
9512 * driver is still alive. In the event that the OS crashes,
9513 * ASF needs to reset the hardware to free up the FIFO space
9514 * that may be filled with rx packets destined for the host.
9515 * If the FIFO is full, ASF will no longer function properly.
9516 *
9517 * Unintended resets have been reported on real time kernels
9518 * where the timer doesn't run on time. Netpoll will also have
9519 * same problem.
9520 *
9521 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
9522 * to check the ring condition when the heartbeat is expiring
9523 * before doing the reset. This will prevent most unintended
9524 * resets.
9525 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009526 if (!--tp->asf_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00009527 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07009528 tg3_wait_for_event_ack(tp);
9529
Michael Chanbbadf502006-04-06 21:46:34 -07009530 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07009531 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07009532 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +00009533 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
9534 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -07009535
9536 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009537 }
9538 tp->asf_counter = tp->asf_multiplier;
9539 }
9540
David S. Millerf47c11e2005-06-24 20:18:35 -07009541 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009542
Michael Chanf475f162006-03-27 23:20:14 -08009543restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07009544 tp->timer.expires = jiffies + tp->timer_offset;
9545 add_timer(&tp->timer);
9546}
9547
Matt Carlson21f76382012-02-22 12:35:21 +00009548static void __devinit tg3_timer_init(struct tg3 *tp)
9549{
9550 if (tg3_flag(tp, TAGGED_STATUS) &&
9551 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
9552 !tg3_flag(tp, 57765_CLASS))
9553 tp->timer_offset = HZ;
9554 else
9555 tp->timer_offset = HZ / 10;
9556
9557 BUG_ON(tp->timer_offset > HZ);
9558
9559 tp->timer_multiplier = (HZ / tp->timer_offset);
9560 tp->asf_multiplier = (HZ / tp->timer_offset) *
9561 TG3_FW_UPDATE_FREQ_SEC;
9562
9563 init_timer(&tp->timer);
9564 tp->timer.data = (unsigned long) tp;
9565 tp->timer.function = tg3_timer;
9566}
9567
9568static void tg3_timer_start(struct tg3 *tp)
9569{
9570 tp->asf_counter = tp->asf_multiplier;
9571 tp->timer_counter = tp->timer_multiplier;
9572
9573 tp->timer.expires = jiffies + tp->timer_offset;
9574 add_timer(&tp->timer);
9575}
9576
9577static void tg3_timer_stop(struct tg3 *tp)
9578{
9579 del_timer_sync(&tp->timer);
9580}
9581
9582/* Restart hardware after configuration changes, self-test, etc.
9583 * Invoked with tp->lock held.
9584 */
9585static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
9586 __releases(tp->lock)
9587 __acquires(tp->lock)
9588{
9589 int err;
9590
9591 err = tg3_init_hw(tp, reset_phy);
9592 if (err) {
9593 netdev_err(tp->dev,
9594 "Failed to re-initialize device, aborting\n");
9595 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
9596 tg3_full_unlock(tp);
9597 tg3_timer_stop(tp);
9598 tp->irq_sync = 0;
9599 tg3_napi_enable(tp);
9600 dev_close(tp->dev);
9601 tg3_full_lock(tp, 0);
9602 }
9603 return err;
9604}
9605
9606static void tg3_reset_task(struct work_struct *work)
9607{
9608 struct tg3 *tp = container_of(work, struct tg3, reset_task);
9609 int err;
9610
9611 tg3_full_lock(tp, 0);
9612
9613 if (!netif_running(tp->dev)) {
9614 tg3_flag_clear(tp, RESET_TASK_PENDING);
9615 tg3_full_unlock(tp);
9616 return;
9617 }
9618
9619 tg3_full_unlock(tp);
9620
9621 tg3_phy_stop(tp);
9622
9623 tg3_netif_stop(tp);
9624
9625 tg3_full_lock(tp, 1);
9626
9627 if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
9628 tp->write32_tx_mbox = tg3_write32_tx_mbox;
9629 tp->write32_rx_mbox = tg3_write_flush_reg32;
9630 tg3_flag_set(tp, MBOX_WRITE_REORDER);
9631 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
9632 }
9633
9634 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
9635 err = tg3_init_hw(tp, 1);
9636 if (err)
9637 goto out;
9638
9639 tg3_netif_start(tp);
9640
9641out:
9642 tg3_full_unlock(tp);
9643
9644 if (!err)
9645 tg3_phy_start(tp);
9646
9647 tg3_flag_clear(tp, RESET_TASK_PENDING);
9648}
9649
Matt Carlson4f125f42009-09-01 12:55:02 +00009650static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -08009651{
David Howells7d12e782006-10-05 14:55:46 +01009652 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009653 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +00009654 char *name;
9655 struct tg3_napi *tnapi = &tp->napi[irq_num];
9656
9657 if (tp->irq_cnt == 1)
9658 name = tp->dev->name;
9659 else {
9660 name = &tnapi->irq_lbl[0];
9661 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
9662 name[IFNAMSIZ-1] = 0;
9663 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009664
Joe Perches63c3a662011-04-26 08:12:10 +00009665 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Michael Chanfcfa0a32006-03-20 22:28:41 -08009666 fn = tg3_msi;
Joe Perches63c3a662011-04-26 08:12:10 +00009667 if (tg3_flag(tp, 1SHOT_MSI))
Michael Chanfcfa0a32006-03-20 22:28:41 -08009668 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009669 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009670 } else {
9671 fn = tg3_interrupt;
Joe Perches63c3a662011-04-26 08:12:10 +00009672 if (tg3_flag(tp, TAGGED_STATUS))
Michael Chanfcfa0a32006-03-20 22:28:41 -08009673 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009674 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009675 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009676
9677 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009678}
9679
Michael Chan79381092005-04-21 17:13:59 -07009680static int tg3_test_interrupt(struct tg3 *tp)
9681{
Matt Carlson09943a12009-08-28 14:01:57 +00009682 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -07009683 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -07009684 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009685 u32 val;
Michael Chan79381092005-04-21 17:13:59 -07009686
Michael Chand4bc3922005-05-29 14:59:20 -07009687 if (!netif_running(dev))
9688 return -ENODEV;
9689
Michael Chan79381092005-04-21 17:13:59 -07009690 tg3_disable_ints(tp);
9691
Matt Carlson4f125f42009-09-01 12:55:02 +00009692 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009693
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009694 /*
9695 * Turn off MSI one shot mode. Otherwise this test has no
9696 * observable way to know whether the interrupt was delivered.
9697 */
Matt Carlson3aa1cdf2011-07-20 10:20:55 +00009698 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009699 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
9700 tw32(MSGINT_MODE, val);
9701 }
9702
Matt Carlson4f125f42009-09-01 12:55:02 +00009703 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Davidlohr Buesof274fd92012-02-22 03:06:54 +00009704 IRQF_SHARED, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009705 if (err)
9706 return err;
9707
Matt Carlson898a56f2009-08-28 14:02:40 +00009708 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -07009709 tg3_enable_ints(tp);
9710
9711 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00009712 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -07009713
9714 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -07009715 u32 int_mbox, misc_host_ctrl;
9716
Matt Carlson898a56f2009-08-28 14:02:40 +00009717 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -07009718 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
9719
9720 if ((int_mbox != 0) ||
9721 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
9722 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -07009723 break;
Michael Chanb16250e2006-09-27 16:10:14 -07009724 }
9725
Matt Carlson3aa1cdf2011-07-20 10:20:55 +00009726 if (tg3_flag(tp, 57765_PLUS) &&
9727 tnapi->hw_status->status_tag != tnapi->last_tag)
9728 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
9729
Michael Chan79381092005-04-21 17:13:59 -07009730 msleep(10);
9731 }
9732
9733 tg3_disable_ints(tp);
9734
Matt Carlson4f125f42009-09-01 12:55:02 +00009735 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009736
Matt Carlson4f125f42009-09-01 12:55:02 +00009737 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009738
9739 if (err)
9740 return err;
9741
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009742 if (intr_ok) {
9743 /* Reenable MSI one shot mode. */
Matt Carlson5b39de92011-08-31 11:44:50 +00009744 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, 1SHOT_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009745 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
9746 tw32(MSGINT_MODE, val);
9747 }
Michael Chan79381092005-04-21 17:13:59 -07009748 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009749 }
Michael Chan79381092005-04-21 17:13:59 -07009750
9751 return -EIO;
9752}
9753
9754/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
9755 * successfully restored
9756 */
9757static int tg3_test_msi(struct tg3 *tp)
9758{
Michael Chan79381092005-04-21 17:13:59 -07009759 int err;
9760 u16 pci_cmd;
9761
Joe Perches63c3a662011-04-26 08:12:10 +00009762 if (!tg3_flag(tp, USING_MSI))
Michael Chan79381092005-04-21 17:13:59 -07009763 return 0;
9764
9765 /* Turn off SERR reporting in case MSI terminates with Master
9766 * Abort.
9767 */
9768 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
9769 pci_write_config_word(tp->pdev, PCI_COMMAND,
9770 pci_cmd & ~PCI_COMMAND_SERR);
9771
9772 err = tg3_test_interrupt(tp);
9773
9774 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
9775
9776 if (!err)
9777 return 0;
9778
9779 /* other failures */
9780 if (err != -EIO)
9781 return err;
9782
9783 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009784 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
9785 "to INTx mode. Please report this failure to the PCI "
9786 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -07009787
Matt Carlson4f125f42009-09-01 12:55:02 +00009788 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +00009789
Michael Chan79381092005-04-21 17:13:59 -07009790 pci_disable_msi(tp->pdev);
9791
Joe Perches63c3a662011-04-26 08:12:10 +00009792 tg3_flag_clear(tp, USING_MSI);
Andre Detschdc8bf1b2010-04-26 07:27:07 +00009793 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -07009794
Matt Carlson4f125f42009-09-01 12:55:02 +00009795 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009796 if (err)
9797 return err;
9798
9799 /* Need to reset the chip because the MSI cycle may have terminated
9800 * with Master Abort.
9801 */
David S. Millerf47c11e2005-06-24 20:18:35 -07009802 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009803
Michael Chan944d9802005-05-29 14:57:48 -07009804 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009805 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009806
David S. Millerf47c11e2005-06-24 20:18:35 -07009807 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009808
9809 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +00009810 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -07009811
9812 return err;
9813}
9814
Matt Carlson9e9fd122009-01-19 16:57:45 -08009815static int tg3_request_firmware(struct tg3 *tp)
9816{
9817 const __be32 *fw_data;
9818
9819 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009820 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
9821 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009822 return -ENOENT;
9823 }
9824
9825 fw_data = (void *)tp->fw->data;
9826
9827 /* Firmware blob starts with version numbers, followed by
9828 * start address and _full_ length including BSS sections
9829 * (which must be longer than the actual data, of course
9830 */
9831
9832 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
9833 if (tp->fw_len < (tp->fw->size - 12)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009834 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
9835 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009836 release_firmware(tp->fw);
9837 tp->fw = NULL;
9838 return -EINVAL;
9839 }
9840
9841 /* We no longer need firmware; we have it. */
9842 tp->fw_needed = NULL;
9843 return 0;
9844}
9845
Matt Carlson679563f2009-09-01 12:55:46 +00009846static bool tg3_enable_msix(struct tg3 *tp)
9847{
Matt Carlsonc3b50032012-01-17 15:27:23 +00009848 int i, rc;
Matt Carlson679563f2009-09-01 12:55:46 +00009849 struct msix_entry msix_ent[tp->irq_max];
9850
Matt Carlsonc3b50032012-01-17 15:27:23 +00009851 tp->irq_cnt = num_online_cpus();
9852 if (tp->irq_cnt > 1) {
9853 /* We want as many rx rings enabled as there are cpus.
9854 * In multiqueue MSI-X mode, the first MSI-X vector
9855 * only deals with link interrupts, etc, so we add
9856 * one to the number of vectors we are requesting.
9857 */
9858 tp->irq_cnt = min_t(unsigned, tp->irq_cnt + 1, tp->irq_max);
9859 }
Matt Carlson679563f2009-09-01 12:55:46 +00009860
9861 for (i = 0; i < tp->irq_max; i++) {
9862 msix_ent[i].entry = i;
9863 msix_ent[i].vector = 0;
9864 }
9865
9866 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +00009867 if (rc < 0) {
9868 return false;
9869 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +00009870 if (pci_enable_msix(tp->pdev, msix_ent, rc))
9871 return false;
Joe Perches05dbe002010-02-17 19:44:19 +00009872 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
9873 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +00009874 tp->irq_cnt = rc;
9875 }
9876
9877 for (i = 0; i < tp->irq_max; i++)
9878 tp->napi[i].irq_vec = msix_ent[i].vector;
9879
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009880 netif_set_real_num_tx_queues(tp->dev, 1);
9881 rc = tp->irq_cnt > 1 ? tp->irq_cnt - 1 : 1;
9882 if (netif_set_real_num_rx_queues(tp->dev, rc)) {
9883 pci_disable_msix(tp->pdev);
9884 return false;
9885 }
Matt Carlsonb92b9042010-11-24 08:31:51 +00009886
9887 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +00009888 tg3_flag_set(tp, ENABLE_RSS);
Matt Carlsond78b59f2011-04-05 14:22:46 +00009889
9890 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
9891 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Joe Perches63c3a662011-04-26 08:12:10 +00009892 tg3_flag_set(tp, ENABLE_TSS);
Matt Carlsonb92b9042010-11-24 08:31:51 +00009893 netif_set_real_num_tx_queues(tp->dev, tp->irq_cnt - 1);
9894 }
9895 }
Matt Carlson2430b032010-06-05 17:24:34 +00009896
Matt Carlson679563f2009-09-01 12:55:46 +00009897 return true;
9898}
9899
Matt Carlson07b01732009-08-28 14:01:15 +00009900static void tg3_ints_init(struct tg3 *tp)
9901{
Joe Perches63c3a662011-04-26 08:12:10 +00009902 if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
9903 !tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +00009904 /* All MSI supporting chips should support tagged
9905 * status. Assert that this is the case.
9906 */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009907 netdev_warn(tp->dev,
9908 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +00009909 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +00009910 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009911
Joe Perches63c3a662011-04-26 08:12:10 +00009912 if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
9913 tg3_flag_set(tp, USING_MSIX);
9914 else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
9915 tg3_flag_set(tp, USING_MSI);
Matt Carlson679563f2009-09-01 12:55:46 +00009916
Joe Perches63c3a662011-04-26 08:12:10 +00009917 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009918 u32 msi_mode = tr32(MSGINT_MODE);
Joe Perches63c3a662011-04-26 08:12:10 +00009919 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009920 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +00009921 if (!tg3_flag(tp, 1SHOT_MSI))
9922 msi_mode |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlson679563f2009-09-01 12:55:46 +00009923 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
9924 }
9925defcfg:
Joe Perches63c3a662011-04-26 08:12:10 +00009926 if (!tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009927 tp->irq_cnt = 1;
9928 tp->napi[0].irq_vec = tp->pdev->irq;
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009929 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -07009930 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +00009931 }
Matt Carlson07b01732009-08-28 14:01:15 +00009932}
9933
9934static void tg3_ints_fini(struct tg3 *tp)
9935{
Joe Perches63c3a662011-04-26 08:12:10 +00009936 if (tg3_flag(tp, USING_MSIX))
Matt Carlson679563f2009-09-01 12:55:46 +00009937 pci_disable_msix(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009938 else if (tg3_flag(tp, USING_MSI))
Matt Carlson679563f2009-09-01 12:55:46 +00009939 pci_disable_msi(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009940 tg3_flag_clear(tp, USING_MSI);
9941 tg3_flag_clear(tp, USING_MSIX);
9942 tg3_flag_clear(tp, ENABLE_RSS);
9943 tg3_flag_clear(tp, ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +00009944}
9945
Linus Torvalds1da177e2005-04-16 15:20:36 -07009946static int tg3_open(struct net_device *dev)
9947{
9948 struct tg3 *tp = netdev_priv(dev);
Matt Carlson4f125f42009-09-01 12:55:02 +00009949 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009950
Matt Carlson9e9fd122009-01-19 16:57:45 -08009951 if (tp->fw_needed) {
9952 err = tg3_request_firmware(tp);
9953 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
9954 if (err)
9955 return err;
9956 } else if (err) {
Joe Perches05dbe002010-02-17 19:44:19 +00009957 netdev_warn(tp->dev, "TSO capability disabled\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009958 tg3_flag_clear(tp, TSO_CAPABLE);
9959 } else if (!tg3_flag(tp, TSO_CAPABLE)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009960 netdev_notice(tp->dev, "TSO capability restored\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009961 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009962 }
9963 }
9964
Michael Chanc49a1562006-12-17 17:07:29 -08009965 netif_carrier_off(tp->dev);
9966
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009967 err = tg3_power_up(tp);
Matt Carlson2f751b62008-08-04 23:17:34 -07009968 if (err)
Michael Chanbc1c7562006-03-20 17:48:03 -08009969 return err;
Matt Carlson2f751b62008-08-04 23:17:34 -07009970
9971 tg3_full_lock(tp, 0);
Michael Chanbc1c7562006-03-20 17:48:03 -08009972
Linus Torvalds1da177e2005-04-16 15:20:36 -07009973 tg3_disable_ints(tp);
Joe Perches63c3a662011-04-26 08:12:10 +00009974 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009975
David S. Millerf47c11e2005-06-24 20:18:35 -07009976 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009977
Matt Carlson679563f2009-09-01 12:55:46 +00009978 /*
9979 * Setup interrupts first so we know how
9980 * many NAPI resources to allocate
9981 */
9982 tg3_ints_init(tp);
9983
Matt Carlson90415472011-12-16 13:33:23 +00009984 tg3_rss_check_indir_tbl(tp);
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009985
Linus Torvalds1da177e2005-04-16 15:20:36 -07009986 /* The placement of this call is tied
9987 * to the setup and use of Host TX descriptors.
9988 */
9989 err = tg3_alloc_consistent(tp);
9990 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009991 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009992
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009993 tg3_napi_init(tp);
9994
Matt Carlsonfed97812009-09-01 13:10:19 +00009995 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07009996
Matt Carlson4f125f42009-09-01 12:55:02 +00009997 for (i = 0; i < tp->irq_cnt; i++) {
9998 struct tg3_napi *tnapi = &tp->napi[i];
9999 err = tg3_request_irq(tp, i);
10000 if (err) {
Matt Carlson5bc09182011-11-04 09:15:01 +000010001 for (i--; i >= 0; i--) {
10002 tnapi = &tp->napi[i];
Matt Carlson4f125f42009-09-01 12:55:02 +000010003 free_irq(tnapi->irq_vec, tnapi);
Matt Carlson5bc09182011-11-04 09:15:01 +000010004 }
10005 goto err_out2;
Matt Carlson4f125f42009-09-01 12:55:02 +000010006 }
10007 }
Matt Carlson07b01732009-08-28 14:01:15 +000010008
David S. Millerf47c11e2005-06-24 20:18:35 -070010009 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010010
Gary Zambrano8e7a22e2006-04-29 18:59:13 -070010011 err = tg3_init_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010012 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -070010013 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010014 tg3_free_rings(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010015 }
10016
David S. Millerf47c11e2005-06-24 20:18:35 -070010017 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010018
Matt Carlson07b01732009-08-28 14:01:15 +000010019 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +000010020 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010021
Joe Perches63c3a662011-04-26 08:12:10 +000010022 if (tg3_flag(tp, USING_MSI)) {
Michael Chan79381092005-04-21 17:13:59 -070010023 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -070010024
Michael Chan79381092005-04-21 17:13:59 -070010025 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -070010026 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070010027 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -070010028 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070010029 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -070010030
Matt Carlson679563f2009-09-01 12:55:46 +000010031 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -070010032 }
Michael Chanfcfa0a32006-03-20 22:28:41 -080010033
Joe Perches63c3a662011-04-26 08:12:10 +000010034 if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010035 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -080010036
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010037 tw32(PCIE_TRANSACTION_CFG,
10038 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -080010039 }
Michael Chan79381092005-04-21 17:13:59 -070010040 }
10041
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010042 tg3_phy_start(tp);
10043
David S. Millerf47c11e2005-06-24 20:18:35 -070010044 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010045
Matt Carlson21f76382012-02-22 12:35:21 +000010046 tg3_timer_start(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000010047 tg3_flag_set(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010048 tg3_enable_ints(tp);
10049
David S. Millerf47c11e2005-06-24 20:18:35 -070010050 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010051
Matt Carlsonfe5f5782009-09-01 13:09:39 +000010052 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010053
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000010054 /*
10055 * Reset loopback feature if it was turned on while the device was down
10056 * make sure that it's installed properly now.
10057 */
10058 if (dev->features & NETIF_F_LOOPBACK)
10059 tg3_set_loopback(dev, dev->features);
10060
Linus Torvalds1da177e2005-04-16 15:20:36 -070010061 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +000010062
Matt Carlson679563f2009-09-01 12:55:46 +000010063err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +000010064 for (i = tp->irq_cnt - 1; i >= 0; i--) {
10065 struct tg3_napi *tnapi = &tp->napi[i];
10066 free_irq(tnapi->irq_vec, tnapi);
10067 }
Matt Carlson07b01732009-08-28 14:01:15 +000010068
Matt Carlson679563f2009-09-01 12:55:46 +000010069err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +000010070 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000010071 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +000010072 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +000010073
10074err_out1:
10075 tg3_ints_fini(tp);
Matt Carlsoncd0d7222011-07-13 09:27:33 +000010076 tg3_frob_aux_power(tp, false);
10077 pci_set_power_state(tp->pdev, PCI_D3hot);
Matt Carlson07b01732009-08-28 14:01:15 +000010078 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010079}
10080
Linus Torvalds1da177e2005-04-16 15:20:36 -070010081static int tg3_close(struct net_device *dev)
10082{
Matt Carlson4f125f42009-09-01 12:55:02 +000010083 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010084 struct tg3 *tp = netdev_priv(dev);
10085
Matt Carlsonfed97812009-09-01 13:10:19 +000010086 tg3_napi_disable(tp);
Matt Carlsondb219972011-11-04 09:15:03 +000010087 tg3_reset_task_cancel(tp);
Michael Chan7faa0062006-02-02 17:29:28 -080010088
Matt Carlsonfe5f5782009-09-01 13:09:39 +000010089 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010090
Matt Carlson21f76382012-02-22 12:35:21 +000010091 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010092
Matt Carlson24bb4fb2009-10-05 17:55:29 +000010093 tg3_phy_stop(tp);
10094
David S. Millerf47c11e2005-06-24 20:18:35 -070010095 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010096
10097 tg3_disable_ints(tp);
10098
Michael Chan944d9802005-05-29 14:57:48 -070010099 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010100 tg3_free_rings(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000010101 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010102
David S. Millerf47c11e2005-06-24 20:18:35 -070010103 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010104
Matt Carlson4f125f42009-09-01 12:55:02 +000010105 for (i = tp->irq_cnt - 1; i >= 0; i--) {
10106 struct tg3_napi *tnapi = &tp->napi[i];
10107 free_irq(tnapi->irq_vec, tnapi);
10108 }
Matt Carlson07b01732009-08-28 14:01:15 +000010109
10110 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010111
Matt Carlson92feeab2011-12-08 14:40:14 +000010112 /* Clear stats across close / open calls */
10113 memset(&tp->net_stats_prev, 0, sizeof(tp->net_stats_prev));
10114 memset(&tp->estats_prev, 0, sizeof(tp->estats_prev));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010115
Matt Carlson66cfd1b2010-09-30 10:34:30 +000010116 tg3_napi_fini(tp);
10117
Linus Torvalds1da177e2005-04-16 15:20:36 -070010118 tg3_free_consistent(tp);
10119
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000010120 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080010121
10122 netif_carrier_off(tp->dev);
10123
Linus Torvalds1da177e2005-04-16 15:20:36 -070010124 return 0;
10125}
10126
Eric Dumazet511d2222010-07-07 20:44:24 +000010127static inline u64 get_stat64(tg3_stat64_t *val)
Stefan Buehler816f8b82008-08-15 14:10:54 -070010128{
10129 return ((u64)val->high << 32) | ((u64)val->low);
10130}
10131
Matt Carlson65ec6982012-02-28 23:33:37 +000010132static u64 tg3_calc_crc_errors(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010133{
10134 struct tg3_hw_stats *hw_stats = tp->hw_stats;
10135
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010136 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010137 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
10138 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010139 u32 val;
10140
Michael Chan569a5df2007-02-13 12:18:15 -080010141 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
10142 tg3_writephy(tp, MII_TG3_TEST1,
10143 val | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +000010144 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010145 } else
10146 val = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010147
10148 tp->phy_crc_errors += val;
10149
10150 return tp->phy_crc_errors;
10151 }
10152
10153 return get_stat64(&hw_stats->rx_fcs_errors);
10154}
10155
10156#define ESTAT_ADD(member) \
10157 estats->member = old_estats->member + \
Eric Dumazet511d2222010-07-07 20:44:24 +000010158 get_stat64(&hw_stats->member)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010159
Matt Carlson65ec6982012-02-28 23:33:37 +000010160static void tg3_get_estats(struct tg3 *tp, struct tg3_ethtool_stats *estats)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010161{
Linus Torvalds1da177e2005-04-16 15:20:36 -070010162 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
10163 struct tg3_hw_stats *hw_stats = tp->hw_stats;
10164
Linus Torvalds1da177e2005-04-16 15:20:36 -070010165 ESTAT_ADD(rx_octets);
10166 ESTAT_ADD(rx_fragments);
10167 ESTAT_ADD(rx_ucast_packets);
10168 ESTAT_ADD(rx_mcast_packets);
10169 ESTAT_ADD(rx_bcast_packets);
10170 ESTAT_ADD(rx_fcs_errors);
10171 ESTAT_ADD(rx_align_errors);
10172 ESTAT_ADD(rx_xon_pause_rcvd);
10173 ESTAT_ADD(rx_xoff_pause_rcvd);
10174 ESTAT_ADD(rx_mac_ctrl_rcvd);
10175 ESTAT_ADD(rx_xoff_entered);
10176 ESTAT_ADD(rx_frame_too_long_errors);
10177 ESTAT_ADD(rx_jabbers);
10178 ESTAT_ADD(rx_undersize_packets);
10179 ESTAT_ADD(rx_in_length_errors);
10180 ESTAT_ADD(rx_out_length_errors);
10181 ESTAT_ADD(rx_64_or_less_octet_packets);
10182 ESTAT_ADD(rx_65_to_127_octet_packets);
10183 ESTAT_ADD(rx_128_to_255_octet_packets);
10184 ESTAT_ADD(rx_256_to_511_octet_packets);
10185 ESTAT_ADD(rx_512_to_1023_octet_packets);
10186 ESTAT_ADD(rx_1024_to_1522_octet_packets);
10187 ESTAT_ADD(rx_1523_to_2047_octet_packets);
10188 ESTAT_ADD(rx_2048_to_4095_octet_packets);
10189 ESTAT_ADD(rx_4096_to_8191_octet_packets);
10190 ESTAT_ADD(rx_8192_to_9022_octet_packets);
10191
10192 ESTAT_ADD(tx_octets);
10193 ESTAT_ADD(tx_collisions);
10194 ESTAT_ADD(tx_xon_sent);
10195 ESTAT_ADD(tx_xoff_sent);
10196 ESTAT_ADD(tx_flow_control);
10197 ESTAT_ADD(tx_mac_errors);
10198 ESTAT_ADD(tx_single_collisions);
10199 ESTAT_ADD(tx_mult_collisions);
10200 ESTAT_ADD(tx_deferred);
10201 ESTAT_ADD(tx_excessive_collisions);
10202 ESTAT_ADD(tx_late_collisions);
10203 ESTAT_ADD(tx_collide_2times);
10204 ESTAT_ADD(tx_collide_3times);
10205 ESTAT_ADD(tx_collide_4times);
10206 ESTAT_ADD(tx_collide_5times);
10207 ESTAT_ADD(tx_collide_6times);
10208 ESTAT_ADD(tx_collide_7times);
10209 ESTAT_ADD(tx_collide_8times);
10210 ESTAT_ADD(tx_collide_9times);
10211 ESTAT_ADD(tx_collide_10times);
10212 ESTAT_ADD(tx_collide_11times);
10213 ESTAT_ADD(tx_collide_12times);
10214 ESTAT_ADD(tx_collide_13times);
10215 ESTAT_ADD(tx_collide_14times);
10216 ESTAT_ADD(tx_collide_15times);
10217 ESTAT_ADD(tx_ucast_packets);
10218 ESTAT_ADD(tx_mcast_packets);
10219 ESTAT_ADD(tx_bcast_packets);
10220 ESTAT_ADD(tx_carrier_sense_errors);
10221 ESTAT_ADD(tx_discards);
10222 ESTAT_ADD(tx_errors);
10223
10224 ESTAT_ADD(dma_writeq_full);
10225 ESTAT_ADD(dma_write_prioq_full);
10226 ESTAT_ADD(rxbds_empty);
10227 ESTAT_ADD(rx_discards);
10228 ESTAT_ADD(rx_errors);
10229 ESTAT_ADD(rx_threshold_hit);
10230
10231 ESTAT_ADD(dma_readq_full);
10232 ESTAT_ADD(dma_read_prioq_full);
10233 ESTAT_ADD(tx_comp_queue_full);
10234
10235 ESTAT_ADD(ring_set_send_prod_index);
10236 ESTAT_ADD(ring_status_update);
10237 ESTAT_ADD(nic_irqs);
10238 ESTAT_ADD(nic_avoided_irqs);
10239 ESTAT_ADD(nic_tx_threshold_hit);
10240
Matt Carlson4452d092011-05-19 12:12:51 +000010241 ESTAT_ADD(mbuf_lwm_thresh_hit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010242}
10243
Matt Carlson65ec6982012-02-28 23:33:37 +000010244static void tg3_get_nstats(struct tg3 *tp, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010245{
Eric Dumazet511d2222010-07-07 20:44:24 +000010246 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010247 struct tg3_hw_stats *hw_stats = tp->hw_stats;
10248
Linus Torvalds1da177e2005-04-16 15:20:36 -070010249 stats->rx_packets = old_stats->rx_packets +
10250 get_stat64(&hw_stats->rx_ucast_packets) +
10251 get_stat64(&hw_stats->rx_mcast_packets) +
10252 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010253
Linus Torvalds1da177e2005-04-16 15:20:36 -070010254 stats->tx_packets = old_stats->tx_packets +
10255 get_stat64(&hw_stats->tx_ucast_packets) +
10256 get_stat64(&hw_stats->tx_mcast_packets) +
10257 get_stat64(&hw_stats->tx_bcast_packets);
10258
10259 stats->rx_bytes = old_stats->rx_bytes +
10260 get_stat64(&hw_stats->rx_octets);
10261 stats->tx_bytes = old_stats->tx_bytes +
10262 get_stat64(&hw_stats->tx_octets);
10263
10264 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -070010265 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010266 stats->tx_errors = old_stats->tx_errors +
10267 get_stat64(&hw_stats->tx_errors) +
10268 get_stat64(&hw_stats->tx_mac_errors) +
10269 get_stat64(&hw_stats->tx_carrier_sense_errors) +
10270 get_stat64(&hw_stats->tx_discards);
10271
10272 stats->multicast = old_stats->multicast +
10273 get_stat64(&hw_stats->rx_mcast_packets);
10274 stats->collisions = old_stats->collisions +
10275 get_stat64(&hw_stats->tx_collisions);
10276
10277 stats->rx_length_errors = old_stats->rx_length_errors +
10278 get_stat64(&hw_stats->rx_frame_too_long_errors) +
10279 get_stat64(&hw_stats->rx_undersize_packets);
10280
10281 stats->rx_over_errors = old_stats->rx_over_errors +
10282 get_stat64(&hw_stats->rxbds_empty);
10283 stats->rx_frame_errors = old_stats->rx_frame_errors +
10284 get_stat64(&hw_stats->rx_align_errors);
10285 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
10286 get_stat64(&hw_stats->tx_discards);
10287 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
10288 get_stat64(&hw_stats->tx_carrier_sense_errors);
10289
10290 stats->rx_crc_errors = old_stats->rx_crc_errors +
Matt Carlson65ec6982012-02-28 23:33:37 +000010291 tg3_calc_crc_errors(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010292
John W. Linville4f63b872005-09-12 14:43:18 -070010293 stats->rx_missed_errors = old_stats->rx_missed_errors +
10294 get_stat64(&hw_stats->rx_discards);
10295
Eric Dumazetb0057c52010-10-10 19:55:52 +000010296 stats->rx_dropped = tp->rx_dropped;
Eric Dumazet48855432011-10-24 07:53:03 +000010297 stats->tx_dropped = tp->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010298}
10299
Linus Torvalds1da177e2005-04-16 15:20:36 -070010300static int tg3_get_regs_len(struct net_device *dev)
10301{
Matt Carlson97bd8e42011-04-13 11:05:04 +000010302 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010303}
10304
10305static void tg3_get_regs(struct net_device *dev,
10306 struct ethtool_regs *regs, void *_p)
10307{
Linus Torvalds1da177e2005-04-16 15:20:36 -070010308 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010309
10310 regs->version = 0;
10311
Matt Carlson97bd8e42011-04-13 11:05:04 +000010312 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010313
Matt Carlson800960682010-08-02 11:26:06 +000010314 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010315 return;
10316
David S. Millerf47c11e2005-06-24 20:18:35 -070010317 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010318
Matt Carlson97bd8e42011-04-13 11:05:04 +000010319 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010320
David S. Millerf47c11e2005-06-24 20:18:35 -070010321 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010322}
10323
10324static int tg3_get_eeprom_len(struct net_device *dev)
10325{
10326 struct tg3 *tp = netdev_priv(dev);
10327
10328 return tp->nvram_size;
10329}
10330
Linus Torvalds1da177e2005-04-16 15:20:36 -070010331static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
10332{
10333 struct tg3 *tp = netdev_priv(dev);
10334 int ret;
10335 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -080010336 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010337 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010338
Joe Perches63c3a662011-04-26 08:12:10 +000010339 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000010340 return -EINVAL;
10341
Matt Carlson800960682010-08-02 11:26:06 +000010342 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010343 return -EAGAIN;
10344
Linus Torvalds1da177e2005-04-16 15:20:36 -070010345 offset = eeprom->offset;
10346 len = eeprom->len;
10347 eeprom->len = 0;
10348
10349 eeprom->magic = TG3_EEPROM_MAGIC;
10350
10351 if (offset & 3) {
10352 /* adjustments to start on required 4 byte boundary */
10353 b_offset = offset & 3;
10354 b_count = 4 - b_offset;
10355 if (b_count > len) {
10356 /* i.e. offset=1 len=2 */
10357 b_count = len;
10358 }
Matt Carlsona9dc5292009-02-25 14:25:30 +000010359 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010360 if (ret)
10361 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +000010362 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010363 len -= b_count;
10364 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010365 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010366 }
10367
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010368 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010369 pd = &data[eeprom->len];
10370 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010371 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010372 if (ret) {
10373 eeprom->len += i;
10374 return ret;
10375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010376 memcpy(pd + i, &val, 4);
10377 }
10378 eeprom->len += i;
10379
10380 if (len & 3) {
10381 /* read last bytes not ending on 4 byte boundary */
10382 pd = &data[eeprom->len];
10383 b_count = len & 3;
10384 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010385 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010386 if (ret)
10387 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080010388 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010389 eeprom->len += b_count;
10390 }
10391 return 0;
10392}
10393
Linus Torvalds1da177e2005-04-16 15:20:36 -070010394static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
10395{
10396 struct tg3 *tp = netdev_priv(dev);
10397 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080010398 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010399 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010400 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010401
Matt Carlson800960682010-08-02 11:26:06 +000010402 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010403 return -EAGAIN;
10404
Joe Perches63c3a662011-04-26 08:12:10 +000010405 if (tg3_flag(tp, NO_NVRAM) ||
Matt Carlsondf259d82009-04-20 06:57:14 +000010406 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010407 return -EINVAL;
10408
10409 offset = eeprom->offset;
10410 len = eeprom->len;
10411
10412 if ((b_offset = (offset & 3))) {
10413 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010414 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010415 if (ret)
10416 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010417 len += b_offset;
10418 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -070010419 if (len < 4)
10420 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010421 }
10422
10423 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -070010424 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010425 /* adjustments to end on required 4 byte boundary */
10426 odd_len = 1;
10427 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010428 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010429 if (ret)
10430 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010431 }
10432
10433 buf = data;
10434 if (b_offset || odd_len) {
10435 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010436 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010437 return -ENOMEM;
10438 if (b_offset)
10439 memcpy(buf, &start, 4);
10440 if (odd_len)
10441 memcpy(buf+len-4, &end, 4);
10442 memcpy(buf + b_offset, data, eeprom->len);
10443 }
10444
10445 ret = tg3_nvram_write_block(tp, offset, len, buf);
10446
10447 if (buf != data)
10448 kfree(buf);
10449
10450 return ret;
10451}
10452
10453static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
10454{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010455 struct tg3 *tp = netdev_priv(dev);
10456
Joe Perches63c3a662011-04-26 08:12:10 +000010457 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010458 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010459 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010460 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010461 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10462 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010463 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010464
Linus Torvalds1da177e2005-04-16 15:20:36 -070010465 cmd->supported = (SUPPORTED_Autoneg);
10466
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010467 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010468 cmd->supported |= (SUPPORTED_1000baseT_Half |
10469 SUPPORTED_1000baseT_Full);
10470
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010471 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010472 cmd->supported |= (SUPPORTED_100baseT_Half |
10473 SUPPORTED_100baseT_Full |
10474 SUPPORTED_10baseT_Half |
10475 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -080010476 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -070010477 cmd->port = PORT_TP;
10478 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010479 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -070010480 cmd->port = PORT_FIBRE;
10481 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010482
Linus Torvalds1da177e2005-04-16 15:20:36 -070010483 cmd->advertising = tp->link_config.advertising;
Matt Carlson5bb09772011-06-13 13:39:00 +000010484 if (tg3_flag(tp, PAUSE_AUTONEG)) {
10485 if (tp->link_config.flowctrl & FLOW_CTRL_RX) {
10486 if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
10487 cmd->advertising |= ADVERTISED_Pause;
10488 } else {
10489 cmd->advertising |= ADVERTISED_Pause |
10490 ADVERTISED_Asym_Pause;
10491 }
10492 } else if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
10493 cmd->advertising |= ADVERTISED_Asym_Pause;
10494 }
10495 }
Matt Carlson859edb22011-12-08 14:40:16 +000010496 if (netif_running(dev) && netif_carrier_ok(dev)) {
David Decotigny70739492011-04-27 18:32:40 +000010497 ethtool_cmd_speed_set(cmd, tp->link_config.active_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010498 cmd->duplex = tp->link_config.active_duplex;
Matt Carlson859edb22011-12-08 14:40:16 +000010499 cmd->lp_advertising = tp->link_config.rmt_adv;
Matt Carlsone348c5e2011-11-21 15:01:20 +000010500 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
10501 if (tp->phy_flags & TG3_PHYFLG_MDIX_STATE)
10502 cmd->eth_tp_mdix = ETH_TP_MDI_X;
10503 else
10504 cmd->eth_tp_mdix = ETH_TP_MDI;
10505 }
Matt Carlson64c22182010-10-14 10:37:44 +000010506 } else {
Matt Carlsone7405222012-02-13 15:20:16 +000010507 ethtool_cmd_speed_set(cmd, SPEED_UNKNOWN);
10508 cmd->duplex = DUPLEX_UNKNOWN;
Matt Carlsone348c5e2011-11-21 15:01:20 +000010509 cmd->eth_tp_mdix = ETH_TP_MDI_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010510 }
Matt Carlson882e9792009-09-01 13:21:36 +000010511 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +000010512 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010513 cmd->autoneg = tp->link_config.autoneg;
10514 cmd->maxtxpkt = 0;
10515 cmd->maxrxpkt = 0;
10516 return 0;
10517}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010518
Linus Torvalds1da177e2005-04-16 15:20:36 -070010519static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
10520{
10521 struct tg3 *tp = netdev_priv(dev);
David Decotigny25db0332011-04-27 18:32:39 +000010522 u32 speed = ethtool_cmd_speed(cmd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010523
Joe Perches63c3a662011-04-26 08:12:10 +000010524 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010525 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010526 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010527 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010528 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10529 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010530 }
10531
Matt Carlson7e5856b2009-02-25 14:23:01 +000010532 if (cmd->autoneg != AUTONEG_ENABLE &&
10533 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -070010534 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +000010535
10536 if (cmd->autoneg == AUTONEG_DISABLE &&
10537 cmd->duplex != DUPLEX_FULL &&
10538 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -070010539 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010540
Matt Carlson7e5856b2009-02-25 14:23:01 +000010541 if (cmd->autoneg == AUTONEG_ENABLE) {
10542 u32 mask = ADVERTISED_Autoneg |
10543 ADVERTISED_Pause |
10544 ADVERTISED_Asym_Pause;
10545
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010546 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010547 mask |= ADVERTISED_1000baseT_Half |
10548 ADVERTISED_1000baseT_Full;
10549
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010550 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010551 mask |= ADVERTISED_100baseT_Half |
10552 ADVERTISED_100baseT_Full |
10553 ADVERTISED_10baseT_Half |
10554 ADVERTISED_10baseT_Full |
10555 ADVERTISED_TP;
10556 else
10557 mask |= ADVERTISED_FIBRE;
10558
10559 if (cmd->advertising & ~mask)
10560 return -EINVAL;
10561
10562 mask &= (ADVERTISED_1000baseT_Half |
10563 ADVERTISED_1000baseT_Full |
10564 ADVERTISED_100baseT_Half |
10565 ADVERTISED_100baseT_Full |
10566 ADVERTISED_10baseT_Half |
10567 ADVERTISED_10baseT_Full);
10568
10569 cmd->advertising &= mask;
10570 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010571 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
David Decotigny25db0332011-04-27 18:32:39 +000010572 if (speed != SPEED_1000)
Matt Carlson7e5856b2009-02-25 14:23:01 +000010573 return -EINVAL;
10574
10575 if (cmd->duplex != DUPLEX_FULL)
10576 return -EINVAL;
10577 } else {
David Decotigny25db0332011-04-27 18:32:39 +000010578 if (speed != SPEED_100 &&
10579 speed != SPEED_10)
Matt Carlson7e5856b2009-02-25 14:23:01 +000010580 return -EINVAL;
10581 }
10582 }
10583
David S. Millerf47c11e2005-06-24 20:18:35 -070010584 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010585
10586 tp->link_config.autoneg = cmd->autoneg;
10587 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -070010588 tp->link_config.advertising = (cmd->advertising |
10589 ADVERTISED_Autoneg);
Matt Carlsone7405222012-02-13 15:20:16 +000010590 tp->link_config.speed = SPEED_UNKNOWN;
10591 tp->link_config.duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010592 } else {
10593 tp->link_config.advertising = 0;
David Decotigny25db0332011-04-27 18:32:39 +000010594 tp->link_config.speed = speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010595 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010596 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010597
Linus Torvalds1da177e2005-04-16 15:20:36 -070010598 if (netif_running(dev))
10599 tg3_setup_phy(tp, 1);
10600
David S. Millerf47c11e2005-06-24 20:18:35 -070010601 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010602
Linus Torvalds1da177e2005-04-16 15:20:36 -070010603 return 0;
10604}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010605
Linus Torvalds1da177e2005-04-16 15:20:36 -070010606static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
10607{
10608 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010609
Rick Jones68aad782011-11-07 13:29:27 +000010610 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
10611 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
10612 strlcpy(info->fw_version, tp->fw_ver, sizeof(info->fw_version));
10613 strlcpy(info->bus_info, pci_name(tp->pdev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010614}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010615
Linus Torvalds1da177e2005-04-16 15:20:36 -070010616static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10617{
10618 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010619
Joe Perches63c3a662011-04-26 08:12:10 +000010620 if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070010621 wol->supported = WAKE_MAGIC;
10622 else
10623 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010624 wol->wolopts = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010625 if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010626 wol->wolopts = WAKE_MAGIC;
10627 memset(&wol->sopass, 0, sizeof(wol->sopass));
10628}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010629
Linus Torvalds1da177e2005-04-16 15:20:36 -070010630static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10631{
10632 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070010633 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010634
Linus Torvalds1da177e2005-04-16 15:20:36 -070010635 if (wol->wolopts & ~WAKE_MAGIC)
10636 return -EINVAL;
10637 if ((wol->wolopts & WAKE_MAGIC) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010638 !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010639 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010640
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010641 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
10642
David S. Millerf47c11e2005-06-24 20:18:35 -070010643 spin_lock_bh(&tp->lock);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010644 if (device_may_wakeup(dp))
Joe Perches63c3a662011-04-26 08:12:10 +000010645 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010646 else
Joe Perches63c3a662011-04-26 08:12:10 +000010647 tg3_flag_clear(tp, WOL_ENABLE);
David S. Millerf47c11e2005-06-24 20:18:35 -070010648 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010649
Linus Torvalds1da177e2005-04-16 15:20:36 -070010650 return 0;
10651}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010652
Linus Torvalds1da177e2005-04-16 15:20:36 -070010653static u32 tg3_get_msglevel(struct net_device *dev)
10654{
10655 struct tg3 *tp = netdev_priv(dev);
10656 return tp->msg_enable;
10657}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010658
Linus Torvalds1da177e2005-04-16 15:20:36 -070010659static void tg3_set_msglevel(struct net_device *dev, u32 value)
10660{
10661 struct tg3 *tp = netdev_priv(dev);
10662 tp->msg_enable = value;
10663}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010664
Linus Torvalds1da177e2005-04-16 15:20:36 -070010665static int tg3_nway_reset(struct net_device *dev)
10666{
10667 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010668 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010669
Linus Torvalds1da177e2005-04-16 15:20:36 -070010670 if (!netif_running(dev))
10671 return -EAGAIN;
10672
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010673 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070010674 return -EINVAL;
10675
Joe Perches63c3a662011-04-26 08:12:10 +000010676 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010677 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010678 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010679 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010680 } else {
10681 u32 bmcr;
10682
10683 spin_lock_bh(&tp->lock);
10684 r = -EINVAL;
10685 tg3_readphy(tp, MII_BMCR, &bmcr);
10686 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
10687 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010688 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010689 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
10690 BMCR_ANENABLE);
10691 r = 0;
10692 }
10693 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010694 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010695
Linus Torvalds1da177e2005-04-16 15:20:36 -070010696 return r;
10697}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010698
Linus Torvalds1da177e2005-04-16 15:20:36 -070010699static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10700{
10701 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010702
Matt Carlson2c49a442010-09-30 10:34:35 +000010703 ering->rx_max_pending = tp->rx_std_ring_mask;
Joe Perches63c3a662011-04-26 08:12:10 +000010704 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson2c49a442010-09-30 10:34:35 +000010705 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080010706 else
10707 ering->rx_jumbo_max_pending = 0;
10708
10709 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010710
10711 ering->rx_pending = tp->rx_pending;
Joe Perches63c3a662011-04-26 08:12:10 +000010712 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Michael Chan4f81c322006-03-20 21:33:42 -080010713 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
10714 else
10715 ering->rx_jumbo_pending = 0;
10716
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010717 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010718}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010719
Linus Torvalds1da177e2005-04-16 15:20:36 -070010720static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10721{
10722 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000010723 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010724
Matt Carlson2c49a442010-09-30 10:34:35 +000010725 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
10726 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070010727 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
10728 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Joe Perches63c3a662011-04-26 08:12:10 +000010729 (tg3_flag(tp, TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070010730 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010731 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010732
Michael Chanbbe832c2005-06-24 20:20:04 -070010733 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010734 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010735 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010736 irq_sync = 1;
10737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010738
Michael Chanbbe832c2005-06-24 20:20:04 -070010739 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010740
Linus Torvalds1da177e2005-04-16 15:20:36 -070010741 tp->rx_pending = ering->rx_pending;
10742
Joe Perches63c3a662011-04-26 08:12:10 +000010743 if (tg3_flag(tp, MAX_RXPEND_64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010744 tp->rx_pending > 63)
10745 tp->rx_pending = 63;
10746 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000010747
Matt Carlson6fd45cb2010-09-15 08:59:57 +000010748 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000010749 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010750
10751 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070010752 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -070010753 err = tg3_restart_hw(tp, 1);
10754 if (!err)
10755 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010756 }
10757
David S. Millerf47c11e2005-06-24 20:18:35 -070010758 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010759
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010760 if (irq_sync && !err)
10761 tg3_phy_start(tp);
10762
Michael Chanb9ec6c12006-07-25 16:37:27 -070010763 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010764}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010765
Linus Torvalds1da177e2005-04-16 15:20:36 -070010766static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10767{
10768 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010769
Joe Perches63c3a662011-04-26 08:12:10 +000010770 epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
Matt Carlson8d018622007-12-20 20:05:44 -080010771
Matt Carlson4a2db502011-12-08 14:40:17 +000010772 if (tp->link_config.flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080010773 epause->rx_pause = 1;
10774 else
10775 epause->rx_pause = 0;
10776
Matt Carlson4a2db502011-12-08 14:40:17 +000010777 if (tp->link_config.flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080010778 epause->tx_pause = 1;
10779 else
10780 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010781}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010782
Linus Torvalds1da177e2005-04-16 15:20:36 -070010783static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10784{
10785 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010786 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010787
Joe Perches63c3a662011-04-26 08:12:10 +000010788 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson27121682010-02-17 15:16:57 +000010789 u32 newadv;
10790 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010791
Matt Carlson27121682010-02-17 15:16:57 +000010792 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010793
Matt Carlson27121682010-02-17 15:16:57 +000010794 if (!(phydev->supported & SUPPORTED_Pause) ||
10795 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000010796 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000010797 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010798
Matt Carlson27121682010-02-17 15:16:57 +000010799 tp->link_config.flowctrl = 0;
10800 if (epause->rx_pause) {
10801 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010802
Matt Carlson27121682010-02-17 15:16:57 +000010803 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080010804 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000010805 newadv = ADVERTISED_Pause;
10806 } else
10807 newadv = ADVERTISED_Pause |
10808 ADVERTISED_Asym_Pause;
10809 } else if (epause->tx_pause) {
10810 tp->link_config.flowctrl |= FLOW_CTRL_TX;
10811 newadv = ADVERTISED_Asym_Pause;
10812 } else
10813 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010814
Matt Carlson27121682010-02-17 15:16:57 +000010815 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010816 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010817 else
Joe Perches63c3a662011-04-26 08:12:10 +000010818 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010819
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010820 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000010821 u32 oldadv = phydev->advertising &
10822 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
10823 if (oldadv != newadv) {
10824 phydev->advertising &=
10825 ~(ADVERTISED_Pause |
10826 ADVERTISED_Asym_Pause);
10827 phydev->advertising |= newadv;
10828 if (phydev->autoneg) {
10829 /*
10830 * Always renegotiate the link to
10831 * inform our link partner of our
10832 * flow control settings, even if the
10833 * flow control is forced. Let
10834 * tg3_adjust_link() do the final
10835 * flow control setup.
10836 */
10837 return phy_start_aneg(phydev);
10838 }
10839 }
10840
10841 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010842 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000010843 } else {
Matt Carlsonc6700ce2012-02-13 15:20:15 +000010844 tp->link_config.advertising &=
Matt Carlson27121682010-02-17 15:16:57 +000010845 ~(ADVERTISED_Pause |
10846 ADVERTISED_Asym_Pause);
Matt Carlsonc6700ce2012-02-13 15:20:15 +000010847 tp->link_config.advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010848 }
10849 } else {
10850 int irq_sync = 0;
10851
10852 if (netif_running(dev)) {
10853 tg3_netif_stop(tp);
10854 irq_sync = 1;
10855 }
10856
10857 tg3_full_lock(tp, irq_sync);
10858
10859 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010860 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010861 else
Joe Perches63c3a662011-04-26 08:12:10 +000010862 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010863 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010864 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010865 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010866 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010867 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010868 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010869 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010870 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010871
10872 if (netif_running(dev)) {
10873 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10874 err = tg3_restart_hw(tp, 1);
10875 if (!err)
10876 tg3_netif_start(tp);
10877 }
10878
10879 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010880 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010881
Michael Chanb9ec6c12006-07-25 16:37:27 -070010882 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010883}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010884
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010885static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010886{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010887 switch (sset) {
10888 case ETH_SS_TEST:
10889 return TG3_NUM_TEST;
10890 case ETH_SS_STATS:
10891 return TG3_NUM_STATS;
10892 default:
10893 return -EOPNOTSUPP;
10894 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070010895}
10896
Matt Carlson90415472011-12-16 13:33:23 +000010897static int tg3_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
10898 u32 *rules __always_unused)
10899{
10900 struct tg3 *tp = netdev_priv(dev);
10901
10902 if (!tg3_flag(tp, SUPPORT_MSIX))
10903 return -EOPNOTSUPP;
10904
10905 switch (info->cmd) {
10906 case ETHTOOL_GRXRINGS:
10907 if (netif_running(tp->dev))
10908 info->data = tp->irq_cnt;
10909 else {
10910 info->data = num_online_cpus();
10911 if (info->data > TG3_IRQ_MAX_VECS_RSS)
10912 info->data = TG3_IRQ_MAX_VECS_RSS;
10913 }
10914
10915 /* The first interrupt vector only
10916 * handles link interrupts.
10917 */
10918 info->data -= 1;
10919 return 0;
10920
10921 default:
10922 return -EOPNOTSUPP;
10923 }
10924}
10925
10926static u32 tg3_get_rxfh_indir_size(struct net_device *dev)
10927{
10928 u32 size = 0;
10929 struct tg3 *tp = netdev_priv(dev);
10930
10931 if (tg3_flag(tp, SUPPORT_MSIX))
10932 size = TG3_RSS_INDIR_TBL_SIZE;
10933
10934 return size;
10935}
10936
10937static int tg3_get_rxfh_indir(struct net_device *dev, u32 *indir)
10938{
10939 struct tg3 *tp = netdev_priv(dev);
10940 int i;
10941
10942 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
10943 indir[i] = tp->rss_ind_tbl[i];
10944
10945 return 0;
10946}
10947
10948static int tg3_set_rxfh_indir(struct net_device *dev, const u32 *indir)
10949{
10950 struct tg3 *tp = netdev_priv(dev);
10951 size_t i;
10952
10953 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
10954 tp->rss_ind_tbl[i] = indir[i];
10955
10956 if (!netif_running(dev) || !tg3_flag(tp, ENABLE_RSS))
10957 return 0;
10958
10959 /* It is legal to write the indirection
10960 * table while the device is running.
10961 */
10962 tg3_full_lock(tp, 0);
10963 tg3_rss_write_indir_tbl(tp);
10964 tg3_full_unlock(tp);
10965
10966 return 0;
10967}
10968
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010969static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010970{
10971 switch (stringset) {
10972 case ETH_SS_STATS:
10973 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
10974 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070010975 case ETH_SS_TEST:
10976 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
10977 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010978 default:
10979 WARN_ON(1); /* we need a WARN() */
10980 break;
10981 }
10982}
10983
stephen hemminger81b87092011-04-04 08:43:50 +000010984static int tg3_set_phys_id(struct net_device *dev,
10985 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070010986{
10987 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070010988
10989 if (!netif_running(tp->dev))
10990 return -EAGAIN;
10991
stephen hemminger81b87092011-04-04 08:43:50 +000010992 switch (state) {
10993 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +000010994 return 1; /* cycle on/off once per second */
Michael Chan4009a932005-09-05 17:52:54 -070010995
stephen hemminger81b87092011-04-04 08:43:50 +000010996 case ETHTOOL_ID_ON:
10997 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10998 LED_CTRL_1000MBPS_ON |
10999 LED_CTRL_100MBPS_ON |
11000 LED_CTRL_10MBPS_ON |
11001 LED_CTRL_TRAFFIC_OVERRIDE |
11002 LED_CTRL_TRAFFIC_BLINK |
11003 LED_CTRL_TRAFFIC_LED);
11004 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011005
stephen hemminger81b87092011-04-04 08:43:50 +000011006 case ETHTOOL_ID_OFF:
11007 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
11008 LED_CTRL_TRAFFIC_OVERRIDE);
11009 break;
Michael Chan4009a932005-09-05 17:52:54 -070011010
stephen hemminger81b87092011-04-04 08:43:50 +000011011 case ETHTOOL_ID_INACTIVE:
11012 tw32(MAC_LED_CTRL, tp->led_ctrl);
11013 break;
Michael Chan4009a932005-09-05 17:52:54 -070011014 }
stephen hemminger81b87092011-04-04 08:43:50 +000011015
Michael Chan4009a932005-09-05 17:52:54 -070011016 return 0;
11017}
11018
Matt Carlsonde6f31e2010-04-12 06:58:30 +000011019static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011020 struct ethtool_stats *estats, u64 *tmp_stats)
11021{
11022 struct tg3 *tp = netdev_priv(dev);
Matt Carlson0e6c9da2011-12-08 14:40:13 +000011023
Matt Carlsonb546e462012-02-13 15:20:09 +000011024 if (tp->hw_stats)
11025 tg3_get_estats(tp, (struct tg3_ethtool_stats *)tmp_stats);
11026 else
11027 memset(tmp_stats, 0, sizeof(struct tg3_ethtool_stats));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011028}
11029
Matt Carlson535a4902011-07-20 10:20:56 +000011030static __be32 *tg3_vpd_readblock(struct tg3 *tp, u32 *vpdlen)
Matt Carlsonc3e94502011-04-13 11:05:08 +000011031{
11032 int i;
11033 __be32 *buf;
11034 u32 offset = 0, len = 0;
11035 u32 magic, val;
11036
Joe Perches63c3a662011-04-26 08:12:10 +000011037 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
Matt Carlsonc3e94502011-04-13 11:05:08 +000011038 return NULL;
11039
11040 if (magic == TG3_EEPROM_MAGIC) {
11041 for (offset = TG3_NVM_DIR_START;
11042 offset < TG3_NVM_DIR_END;
11043 offset += TG3_NVM_DIRENT_SIZE) {
11044 if (tg3_nvram_read(tp, offset, &val))
11045 return NULL;
11046
11047 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
11048 TG3_NVM_DIRTYPE_EXTVPD)
11049 break;
11050 }
11051
11052 if (offset != TG3_NVM_DIR_END) {
11053 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
11054 if (tg3_nvram_read(tp, offset + 4, &offset))
11055 return NULL;
11056
11057 offset = tg3_nvram_logical_addr(tp, offset);
11058 }
11059 }
11060
11061 if (!offset || !len) {
11062 offset = TG3_NVM_VPD_OFF;
11063 len = TG3_NVM_VPD_LEN;
11064 }
11065
11066 buf = kmalloc(len, GFP_KERNEL);
11067 if (buf == NULL)
11068 return NULL;
11069
11070 if (magic == TG3_EEPROM_MAGIC) {
11071 for (i = 0; i < len; i += 4) {
11072 /* The data is in little-endian format in NVRAM.
11073 * Use the big-endian read routines to preserve
11074 * the byte order as it exists in NVRAM.
11075 */
11076 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
11077 goto error;
11078 }
11079 } else {
11080 u8 *ptr;
11081 ssize_t cnt;
11082 unsigned int pos = 0;
11083
11084 ptr = (u8 *)&buf[0];
11085 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
11086 cnt = pci_read_vpd(tp->pdev, pos,
11087 len - pos, ptr);
11088 if (cnt == -ETIMEDOUT || cnt == -EINTR)
11089 cnt = 0;
11090 else if (cnt < 0)
11091 goto error;
11092 }
11093 if (pos != len)
11094 goto error;
11095 }
11096
Matt Carlson535a4902011-07-20 10:20:56 +000011097 *vpdlen = len;
11098
Matt Carlsonc3e94502011-04-13 11:05:08 +000011099 return buf;
11100
11101error:
11102 kfree(buf);
11103 return NULL;
11104}
11105
Michael Chan566f86a2005-05-29 14:56:58 -070011106#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080011107#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
11108#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
11109#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Matt Carlson727a6d92011-06-13 13:38:58 +000011110#define NVRAM_SELFBOOT_FORMAT1_4_SIZE 0x20
11111#define NVRAM_SELFBOOT_FORMAT1_5_SIZE 0x24
Matt Carlsonbda18fa2011-07-20 10:20:57 +000011112#define NVRAM_SELFBOOT_FORMAT1_6_SIZE 0x50
Michael Chanb16250e2006-09-27 16:10:14 -070011113#define NVRAM_SELFBOOT_HW_SIZE 0x20
11114#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070011115
11116static int tg3_test_nvram(struct tg3 *tp)
11117{
Matt Carlson535a4902011-07-20 10:20:56 +000011118 u32 csum, magic, len;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011119 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010011120 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070011121
Joe Perches63c3a662011-04-26 08:12:10 +000011122 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000011123 return 0;
11124
Matt Carlsone4f34112009-02-25 14:25:00 +000011125 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080011126 return -EIO;
11127
Michael Chan1b277772006-03-20 22:27:48 -080011128 if (magic == TG3_EEPROM_MAGIC)
11129 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070011130 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080011131 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
11132 TG3_EEPROM_SB_FORMAT_1) {
11133 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
11134 case TG3_EEPROM_SB_REVISION_0:
11135 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
11136 break;
11137 case TG3_EEPROM_SB_REVISION_2:
11138 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
11139 break;
11140 case TG3_EEPROM_SB_REVISION_3:
11141 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
11142 break;
Matt Carlson727a6d92011-06-13 13:38:58 +000011143 case TG3_EEPROM_SB_REVISION_4:
11144 size = NVRAM_SELFBOOT_FORMAT1_4_SIZE;
11145 break;
11146 case TG3_EEPROM_SB_REVISION_5:
11147 size = NVRAM_SELFBOOT_FORMAT1_5_SIZE;
11148 break;
11149 case TG3_EEPROM_SB_REVISION_6:
11150 size = NVRAM_SELFBOOT_FORMAT1_6_SIZE;
11151 break;
Matt Carlsona5767de2007-11-12 21:10:58 -080011152 default:
Matt Carlson727a6d92011-06-13 13:38:58 +000011153 return -EIO;
Matt Carlsona5767de2007-11-12 21:10:58 -080011154 }
11155 } else
Michael Chan1b277772006-03-20 22:27:48 -080011156 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070011157 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
11158 size = NVRAM_SELFBOOT_HW_SIZE;
11159 else
Michael Chan1b277772006-03-20 22:27:48 -080011160 return -EIO;
11161
11162 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070011163 if (buf == NULL)
11164 return -ENOMEM;
11165
Michael Chan1b277772006-03-20 22:27:48 -080011166 err = -EIO;
11167 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000011168 err = tg3_nvram_read_be32(tp, i, &buf[j]);
11169 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070011170 break;
Michael Chan566f86a2005-05-29 14:56:58 -070011171 }
Michael Chan1b277772006-03-20 22:27:48 -080011172 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070011173 goto out;
11174
Michael Chan1b277772006-03-20 22:27:48 -080011175 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000011176 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080011177 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070011178 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080011179 u8 *buf8 = (u8 *) buf, csum8 = 0;
11180
Al Virob9fc7dc2007-12-17 22:59:57 -080011181 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080011182 TG3_EEPROM_SB_REVISION_2) {
11183 /* For rev 2, the csum doesn't include the MBA. */
11184 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
11185 csum8 += buf8[i];
11186 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
11187 csum8 += buf8[i];
11188 } else {
11189 for (i = 0; i < size; i++)
11190 csum8 += buf8[i];
11191 }
Michael Chan1b277772006-03-20 22:27:48 -080011192
Adrian Bunkad96b482006-04-05 22:21:04 -070011193 if (csum8 == 0) {
11194 err = 0;
11195 goto out;
11196 }
11197
11198 err = -EIO;
11199 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080011200 }
Michael Chan566f86a2005-05-29 14:56:58 -070011201
Al Virob9fc7dc2007-12-17 22:59:57 -080011202 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070011203 TG3_EEPROM_MAGIC_HW) {
11204 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000011205 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070011206 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070011207
11208 /* Separate the parity bits and the data bytes. */
11209 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
11210 if ((i == 0) || (i == 8)) {
11211 int l;
11212 u8 msk;
11213
11214 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
11215 parity[k++] = buf8[i] & msk;
11216 i++;
Matt Carlson859a588792010-04-05 10:19:28 +000011217 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070011218 int l;
11219 u8 msk;
11220
11221 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
11222 parity[k++] = buf8[i] & msk;
11223 i++;
11224
11225 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
11226 parity[k++] = buf8[i] & msk;
11227 i++;
11228 }
11229 data[j++] = buf8[i];
11230 }
11231
11232 err = -EIO;
11233 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
11234 u8 hw8 = hweight8(data[i]);
11235
11236 if ((hw8 & 0x1) && parity[i])
11237 goto out;
11238 else if (!(hw8 & 0x1) && !parity[i])
11239 goto out;
11240 }
11241 err = 0;
11242 goto out;
11243 }
11244
Matt Carlson01c3a392011-03-09 16:58:20 +000011245 err = -EIO;
11246
Michael Chan566f86a2005-05-29 14:56:58 -070011247 /* Bootstrap checksum at offset 0x10 */
11248 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000011249 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070011250 goto out;
11251
11252 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
11253 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000011254 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000011255 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070011256
Matt Carlsonc3e94502011-04-13 11:05:08 +000011257 kfree(buf);
11258
Matt Carlson535a4902011-07-20 10:20:56 +000011259 buf = tg3_vpd_readblock(tp, &len);
Matt Carlsonc3e94502011-04-13 11:05:08 +000011260 if (!buf)
11261 return -ENOMEM;
Matt Carlsond4894f32011-03-09 16:58:21 +000011262
Matt Carlson535a4902011-07-20 10:20:56 +000011263 i = pci_vpd_find_tag((u8 *)buf, 0, len, PCI_VPD_LRDT_RO_DATA);
Matt Carlsond4894f32011-03-09 16:58:21 +000011264 if (i > 0) {
11265 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
11266 if (j < 0)
11267 goto out;
11268
Matt Carlson535a4902011-07-20 10:20:56 +000011269 if (i + PCI_VPD_LRDT_TAG_SIZE + j > len)
Matt Carlsond4894f32011-03-09 16:58:21 +000011270 goto out;
11271
11272 i += PCI_VPD_LRDT_TAG_SIZE;
11273 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
11274 PCI_VPD_RO_KEYWORD_CHKSUM);
11275 if (j > 0) {
11276 u8 csum8 = 0;
11277
11278 j += PCI_VPD_INFO_FLD_HDR_SIZE;
11279
11280 for (i = 0; i <= j; i++)
11281 csum8 += ((u8 *)buf)[i];
11282
11283 if (csum8)
11284 goto out;
11285 }
11286 }
11287
Michael Chan566f86a2005-05-29 14:56:58 -070011288 err = 0;
11289
11290out:
11291 kfree(buf);
11292 return err;
11293}
11294
Michael Chanca430072005-05-29 14:57:23 -070011295#define TG3_SERDES_TIMEOUT_SEC 2
11296#define TG3_COPPER_TIMEOUT_SEC 6
11297
11298static int tg3_test_link(struct tg3 *tp)
11299{
11300 int i, max;
11301
11302 if (!netif_running(tp->dev))
11303 return -ENODEV;
11304
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011305 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070011306 max = TG3_SERDES_TIMEOUT_SEC;
11307 else
11308 max = TG3_COPPER_TIMEOUT_SEC;
11309
11310 for (i = 0; i < max; i++) {
11311 if (netif_carrier_ok(tp->dev))
11312 return 0;
11313
11314 if (msleep_interruptible(1000))
11315 break;
11316 }
11317
11318 return -EIO;
11319}
11320
Michael Chana71116d2005-05-29 14:58:11 -070011321/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080011322static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070011323{
Michael Chanb16250e2006-09-27 16:10:14 -070011324 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070011325 u32 offset, read_mask, write_mask, val, save_val, read_val;
11326 static struct {
11327 u16 offset;
11328 u16 flags;
11329#define TG3_FL_5705 0x1
11330#define TG3_FL_NOT_5705 0x2
11331#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070011332#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070011333 u32 read_mask;
11334 u32 write_mask;
11335 } reg_tbl[] = {
11336 /* MAC Control Registers */
11337 { MAC_MODE, TG3_FL_NOT_5705,
11338 0x00000000, 0x00ef6f8c },
11339 { MAC_MODE, TG3_FL_5705,
11340 0x00000000, 0x01ef6b8c },
11341 { MAC_STATUS, TG3_FL_NOT_5705,
11342 0x03800107, 0x00000000 },
11343 { MAC_STATUS, TG3_FL_5705,
11344 0x03800100, 0x00000000 },
11345 { MAC_ADDR_0_HIGH, 0x0000,
11346 0x00000000, 0x0000ffff },
11347 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011348 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070011349 { MAC_RX_MTU_SIZE, 0x0000,
11350 0x00000000, 0x0000ffff },
11351 { MAC_TX_MODE, 0x0000,
11352 0x00000000, 0x00000070 },
11353 { MAC_TX_LENGTHS, 0x0000,
11354 0x00000000, 0x00003fff },
11355 { MAC_RX_MODE, TG3_FL_NOT_5705,
11356 0x00000000, 0x000007fc },
11357 { MAC_RX_MODE, TG3_FL_5705,
11358 0x00000000, 0x000007dc },
11359 { MAC_HASH_REG_0, 0x0000,
11360 0x00000000, 0xffffffff },
11361 { MAC_HASH_REG_1, 0x0000,
11362 0x00000000, 0xffffffff },
11363 { MAC_HASH_REG_2, 0x0000,
11364 0x00000000, 0xffffffff },
11365 { MAC_HASH_REG_3, 0x0000,
11366 0x00000000, 0xffffffff },
11367
11368 /* Receive Data and Receive BD Initiator Control Registers. */
11369 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
11370 0x00000000, 0xffffffff },
11371 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
11372 0x00000000, 0xffffffff },
11373 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
11374 0x00000000, 0x00000003 },
11375 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
11376 0x00000000, 0xffffffff },
11377 { RCVDBDI_STD_BD+0, 0x0000,
11378 0x00000000, 0xffffffff },
11379 { RCVDBDI_STD_BD+4, 0x0000,
11380 0x00000000, 0xffffffff },
11381 { RCVDBDI_STD_BD+8, 0x0000,
11382 0x00000000, 0xffff0002 },
11383 { RCVDBDI_STD_BD+0xc, 0x0000,
11384 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011385
Michael Chana71116d2005-05-29 14:58:11 -070011386 /* Receive BD Initiator Control Registers. */
11387 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
11388 0x00000000, 0xffffffff },
11389 { RCVBDI_STD_THRESH, TG3_FL_5705,
11390 0x00000000, 0x000003ff },
11391 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
11392 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011393
Michael Chana71116d2005-05-29 14:58:11 -070011394 /* Host Coalescing Control Registers. */
11395 { HOSTCC_MODE, TG3_FL_NOT_5705,
11396 0x00000000, 0x00000004 },
11397 { HOSTCC_MODE, TG3_FL_5705,
11398 0x00000000, 0x000000f6 },
11399 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
11400 0x00000000, 0xffffffff },
11401 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
11402 0x00000000, 0x000003ff },
11403 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
11404 0x00000000, 0xffffffff },
11405 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
11406 0x00000000, 0x000003ff },
11407 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
11408 0x00000000, 0xffffffff },
11409 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
11410 0x00000000, 0x000000ff },
11411 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
11412 0x00000000, 0xffffffff },
11413 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
11414 0x00000000, 0x000000ff },
11415 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
11416 0x00000000, 0xffffffff },
11417 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
11418 0x00000000, 0xffffffff },
11419 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
11420 0x00000000, 0xffffffff },
11421 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
11422 0x00000000, 0x000000ff },
11423 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
11424 0x00000000, 0xffffffff },
11425 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
11426 0x00000000, 0x000000ff },
11427 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
11428 0x00000000, 0xffffffff },
11429 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
11430 0x00000000, 0xffffffff },
11431 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
11432 0x00000000, 0xffffffff },
11433 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
11434 0x00000000, 0xffffffff },
11435 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
11436 0x00000000, 0xffffffff },
11437 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
11438 0xffffffff, 0x00000000 },
11439 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
11440 0xffffffff, 0x00000000 },
11441
11442 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070011443 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070011444 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070011445 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070011446 0x00000000, 0x007fffff },
11447 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
11448 0x00000000, 0x0000003f },
11449 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
11450 0x00000000, 0x000001ff },
11451 { BUFMGR_MB_HIGH_WATER, 0x0000,
11452 0x00000000, 0x000001ff },
11453 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
11454 0xffffffff, 0x00000000 },
11455 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
11456 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011457
Michael Chana71116d2005-05-29 14:58:11 -070011458 /* Mailbox Registers */
11459 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
11460 0x00000000, 0x000001ff },
11461 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
11462 0x00000000, 0x000001ff },
11463 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
11464 0x00000000, 0x000007ff },
11465 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
11466 0x00000000, 0x000001ff },
11467
11468 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
11469 };
11470
Michael Chanb16250e2006-09-27 16:10:14 -070011471 is_5705 = is_5750 = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000011472 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chana71116d2005-05-29 14:58:11 -070011473 is_5705 = 1;
Joe Perches63c3a662011-04-26 08:12:10 +000011474 if (tg3_flag(tp, 5750_PLUS))
Michael Chanb16250e2006-09-27 16:10:14 -070011475 is_5750 = 1;
11476 }
Michael Chana71116d2005-05-29 14:58:11 -070011477
11478 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
11479 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
11480 continue;
11481
11482 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
11483 continue;
11484
Joe Perches63c3a662011-04-26 08:12:10 +000011485 if (tg3_flag(tp, IS_5788) &&
Michael Chana71116d2005-05-29 14:58:11 -070011486 (reg_tbl[i].flags & TG3_FL_NOT_5788))
11487 continue;
11488
Michael Chanb16250e2006-09-27 16:10:14 -070011489 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
11490 continue;
11491
Michael Chana71116d2005-05-29 14:58:11 -070011492 offset = (u32) reg_tbl[i].offset;
11493 read_mask = reg_tbl[i].read_mask;
11494 write_mask = reg_tbl[i].write_mask;
11495
11496 /* Save the original register content */
11497 save_val = tr32(offset);
11498
11499 /* Determine the read-only value. */
11500 read_val = save_val & read_mask;
11501
11502 /* Write zero to the register, then make sure the read-only bits
11503 * are not changed and the read/write bits are all zeros.
11504 */
11505 tw32(offset, 0);
11506
11507 val = tr32(offset);
11508
11509 /* Test the read-only and read/write bits. */
11510 if (((val & read_mask) != read_val) || (val & write_mask))
11511 goto out;
11512
11513 /* Write ones to all the bits defined by RdMask and WrMask, then
11514 * make sure the read-only bits are not changed and the
11515 * read/write bits are all ones.
11516 */
11517 tw32(offset, read_mask | write_mask);
11518
11519 val = tr32(offset);
11520
11521 /* Test the read-only bits. */
11522 if ((val & read_mask) != read_val)
11523 goto out;
11524
11525 /* Test the read/write bits. */
11526 if ((val & write_mask) != write_mask)
11527 goto out;
11528
11529 tw32(offset, save_val);
11530 }
11531
11532 return 0;
11533
11534out:
Michael Chan9f88f292006-12-07 00:22:54 -080011535 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000011536 netdev_err(tp->dev,
11537 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070011538 tw32(offset, save_val);
11539 return -EIO;
11540}
11541
Michael Chan7942e1d2005-05-29 14:58:36 -070011542static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
11543{
Arjan van de Venf71e1302006-03-03 21:33:57 -050011544 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070011545 int i;
11546 u32 j;
11547
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020011548 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070011549 for (j = 0; j < len; j += 4) {
11550 u32 val;
11551
11552 tg3_write_mem(tp, offset + j, test_pattern[i]);
11553 tg3_read_mem(tp, offset + j, &val);
11554 if (val != test_pattern[i])
11555 return -EIO;
11556 }
11557 }
11558 return 0;
11559}
11560
11561static int tg3_test_memory(struct tg3 *tp)
11562{
11563 static struct mem_entry {
11564 u32 offset;
11565 u32 len;
11566 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080011567 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070011568 { 0x00002000, 0x1c000},
11569 { 0xffffffff, 0x00000}
11570 }, mem_tbl_5705[] = {
11571 { 0x00000100, 0x0000c},
11572 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070011573 { 0x00004000, 0x00800},
11574 { 0x00006000, 0x01000},
11575 { 0x00008000, 0x02000},
11576 { 0x00010000, 0x0e000},
11577 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080011578 }, mem_tbl_5755[] = {
11579 { 0x00000200, 0x00008},
11580 { 0x00004000, 0x00800},
11581 { 0x00006000, 0x00800},
11582 { 0x00008000, 0x02000},
11583 { 0x00010000, 0x0c000},
11584 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070011585 }, mem_tbl_5906[] = {
11586 { 0x00000200, 0x00008},
11587 { 0x00004000, 0x00400},
11588 { 0x00006000, 0x00400},
11589 { 0x00008000, 0x01000},
11590 { 0x00010000, 0x01000},
11591 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011592 }, mem_tbl_5717[] = {
11593 { 0x00000200, 0x00008},
11594 { 0x00010000, 0x0a000},
11595 { 0x00020000, 0x13c00},
11596 { 0xffffffff, 0x00000}
11597 }, mem_tbl_57765[] = {
11598 { 0x00000200, 0x00008},
11599 { 0x00004000, 0x00800},
11600 { 0x00006000, 0x09800},
11601 { 0x00010000, 0x0a000},
11602 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070011603 };
11604 struct mem_entry *mem_tbl;
11605 int err = 0;
11606 int i;
11607
Joe Perches63c3a662011-04-26 08:12:10 +000011608 if (tg3_flag(tp, 5717_PLUS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011609 mem_tbl = mem_tbl_5717;
Matt Carlson55086ad2011-12-14 11:09:59 +000011610 else if (tg3_flag(tp, 57765_CLASS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011611 mem_tbl = mem_tbl_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000011612 else if (tg3_flag(tp, 5755_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080011613 mem_tbl = mem_tbl_5755;
11614 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
11615 mem_tbl = mem_tbl_5906;
Joe Perches63c3a662011-04-26 08:12:10 +000011616 else if (tg3_flag(tp, 5705_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080011617 mem_tbl = mem_tbl_5705;
11618 else
Michael Chan7942e1d2005-05-29 14:58:36 -070011619 mem_tbl = mem_tbl_570x;
11620
11621 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000011622 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
11623 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070011624 break;
11625 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011626
Michael Chan7942e1d2005-05-29 14:58:36 -070011627 return err;
11628}
11629
Matt Carlsonbb158d62011-04-25 12:42:47 +000011630#define TG3_TSO_MSS 500
11631
11632#define TG3_TSO_IP_HDR_LEN 20
11633#define TG3_TSO_TCP_HDR_LEN 20
11634#define TG3_TSO_TCP_OPT_LEN 12
11635
11636static const u8 tg3_tso_header[] = {
116370x08, 0x00,
116380x45, 0x00, 0x00, 0x00,
116390x00, 0x00, 0x40, 0x00,
116400x40, 0x06, 0x00, 0x00,
116410x0a, 0x00, 0x00, 0x01,
116420x0a, 0x00, 0x00, 0x02,
116430x0d, 0x00, 0xe0, 0x00,
116440x00, 0x00, 0x01, 0x00,
116450x00, 0x00, 0x02, 0x00,
116460x80, 0x10, 0x10, 0x00,
116470x14, 0x09, 0x00, 0x00,
116480x01, 0x01, 0x08, 0x0a,
116490x11, 0x11, 0x11, 0x11,
116500x11, 0x11, 0x11, 0x11,
11651};
Michael Chan9f40dea2005-09-05 17:53:06 -070011652
Matt Carlson28a45952011-08-19 13:58:22 +000011653static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, bool tso_loopback)
Michael Chanc76949a2005-05-29 14:58:59 -070011654{
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011655 u32 rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011656 u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
Matt Carlson84b67b22011-07-27 14:20:52 +000011657 u32 budget;
Eric Dumazet9205fd92011-11-18 06:47:01 +000011658 struct sk_buff *skb;
11659 u8 *tx_data, *rx_data;
Michael Chanc76949a2005-05-29 14:58:59 -070011660 dma_addr_t map;
11661 int num_pkts, tx_len, rx_len, i, err;
11662 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000011663 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000011664 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070011665
Matt Carlsonc8873402010-02-12 14:47:11 +000011666 tnapi = &tp->napi[0];
11667 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011668 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +000011669 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlson1da85aa2010-09-30 10:34:34 +000011670 rnapi = &tp->napi[1];
Joe Perches63c3a662011-04-26 08:12:10 +000011671 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc8873402010-02-12 14:47:11 +000011672 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011673 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011674 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000011675
Michael Chanc76949a2005-05-29 14:58:59 -070011676 err = -EIO;
11677
Matt Carlson4852a862011-04-13 11:05:07 +000011678 tx_len = pktsz;
David S. Millera20e9c62006-07-31 22:38:16 -070011679 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070011680 if (!skb)
11681 return -ENOMEM;
11682
Michael Chanc76949a2005-05-29 14:58:59 -070011683 tx_data = skb_put(skb, tx_len);
11684 memcpy(tx_data, tp->dev->dev_addr, 6);
11685 memset(tx_data + 6, 0x0, 8);
11686
Matt Carlson4852a862011-04-13 11:05:07 +000011687 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
Michael Chanc76949a2005-05-29 14:58:59 -070011688
Matt Carlson28a45952011-08-19 13:58:22 +000011689 if (tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011690 struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
11691
11692 u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
11693 TG3_TSO_TCP_OPT_LEN;
11694
11695 memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
11696 sizeof(tg3_tso_header));
11697 mss = TG3_TSO_MSS;
11698
11699 val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
11700 num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
11701
11702 /* Set the total length field in the IP header */
11703 iph->tot_len = htons((u16)(mss + hdr_len));
11704
11705 base_flags = (TXD_FLAG_CPU_PRE_DMA |
11706 TXD_FLAG_CPU_POST_DMA);
11707
Joe Perches63c3a662011-04-26 08:12:10 +000011708 if (tg3_flag(tp, HW_TSO_1) ||
11709 tg3_flag(tp, HW_TSO_2) ||
11710 tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011711 struct tcphdr *th;
11712 val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
11713 th = (struct tcphdr *)&tx_data[val];
11714 th->check = 0;
11715 } else
11716 base_flags |= TXD_FLAG_TCPUDP_CSUM;
11717
Joe Perches63c3a662011-04-26 08:12:10 +000011718 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011719 mss |= (hdr_len & 0xc) << 12;
11720 if (hdr_len & 0x10)
11721 base_flags |= 0x00000010;
11722 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +000011723 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlsonbb158d62011-04-25 12:42:47 +000011724 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +000011725 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlsonbb158d62011-04-25 12:42:47 +000011726 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
11727 mss |= (TG3_TSO_TCP_OPT_LEN << 9);
11728 } else {
11729 base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
11730 }
11731
11732 data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
11733 } else {
11734 num_pkts = 1;
11735 data_off = ETH_HLEN;
Michael Chanc441b452012-03-04 14:48:13 +000011736
11737 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
11738 tx_len > VLAN_ETH_FRAME_LEN)
11739 base_flags |= TXD_FLAG_JMB_PKT;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011740 }
11741
11742 for (i = data_off; i < tx_len; i++)
Michael Chanc76949a2005-05-29 14:58:59 -070011743 tx_data[i] = (u8) (i & 0xff);
11744
Alexander Duyckf4188d82009-12-02 16:48:38 +000011745 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
11746 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000011747 dev_kfree_skb(skb);
11748 return -EIO;
11749 }
Michael Chanc76949a2005-05-29 14:58:59 -070011750
Matt Carlson0d681b22011-07-27 14:20:49 +000011751 val = tnapi->tx_prod;
11752 tnapi->tx_buffers[val].skb = skb;
11753 dma_unmap_addr_set(&tnapi->tx_buffers[val], mapping, map);
11754
Michael Chanc76949a2005-05-29 14:58:59 -070011755 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011756 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011757
11758 udelay(10);
11759
Matt Carlson898a56f2009-08-28 14:02:40 +000011760 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070011761
Matt Carlson84b67b22011-07-27 14:20:52 +000011762 budget = tg3_tx_avail(tnapi);
11763 if (tg3_tx_frag_set(tnapi, &val, &budget, map, tx_len,
Matt Carlsond1a3b732011-07-27 14:20:51 +000011764 base_flags | TXD_FLAG_END, mss, 0)) {
11765 tnapi->tx_buffers[val].skb = NULL;
11766 dev_kfree_skb(skb);
11767 return -EIO;
11768 }
Michael Chanc76949a2005-05-29 14:58:59 -070011769
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011770 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070011771
Michael Chan6541b802012-03-04 14:48:14 +000011772 /* Sync BD data before updating mailbox */
11773 wmb();
11774
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011775 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
11776 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070011777
11778 udelay(10);
11779
Matt Carlson303fc922009-11-02 14:27:34 +000011780 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
11781 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070011782 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011783 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011784
11785 udelay(10);
11786
Matt Carlson898a56f2009-08-28 14:02:40 +000011787 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
11788 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011789 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070011790 (rx_idx == (rx_start_idx + num_pkts)))
11791 break;
11792 }
11793
Matt Carlsonba1142e2011-11-04 09:15:00 +000011794 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod - 1, -1);
Michael Chanc76949a2005-05-29 14:58:59 -070011795 dev_kfree_skb(skb);
11796
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011797 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070011798 goto out;
11799
11800 if (rx_idx != rx_start_idx + num_pkts)
11801 goto out;
11802
Matt Carlsonbb158d62011-04-25 12:42:47 +000011803 val = data_off;
11804 while (rx_idx != rx_start_idx) {
11805 desc = &rnapi->rx_rcb[rx_start_idx++];
11806 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
11807 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
Michael Chanc76949a2005-05-29 14:58:59 -070011808
Matt Carlsonbb158d62011-04-25 12:42:47 +000011809 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
11810 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
Matt Carlson4852a862011-04-13 11:05:07 +000011811 goto out;
Michael Chanc76949a2005-05-29 14:58:59 -070011812
Matt Carlsonbb158d62011-04-25 12:42:47 +000011813 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
11814 - ETH_FCS_LEN;
11815
Matt Carlson28a45952011-08-19 13:58:22 +000011816 if (!tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011817 if (rx_len != tx_len)
11818 goto out;
11819
11820 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
11821 if (opaque_key != RXD_OPAQUE_RING_STD)
11822 goto out;
11823 } else {
11824 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
11825 goto out;
11826 }
11827 } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
11828 (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
Matt Carlson54e0a672011-05-19 12:12:50 +000011829 >> RXD_TCPCSUM_SHIFT != 0xffff) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011830 goto out;
11831 }
11832
11833 if (opaque_key == RXD_OPAQUE_RING_STD) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000011834 rx_data = tpr->rx_std_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011835 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
11836 mapping);
11837 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000011838 rx_data = tpr->rx_jmb_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011839 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
11840 mapping);
11841 } else
Matt Carlson4852a862011-04-13 11:05:07 +000011842 goto out;
11843
Matt Carlsonbb158d62011-04-25 12:42:47 +000011844 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
11845 PCI_DMA_FROMDEVICE);
11846
Eric Dumazet9205fd92011-11-18 06:47:01 +000011847 rx_data += TG3_RX_OFFSET(tp);
Matt Carlsonbb158d62011-04-25 12:42:47 +000011848 for (i = data_off; i < rx_len; i++, val++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000011849 if (*(rx_data + i) != (u8) (val & 0xff))
Matt Carlsonbb158d62011-04-25 12:42:47 +000011850 goto out;
11851 }
Matt Carlson4852a862011-04-13 11:05:07 +000011852 }
11853
Michael Chanc76949a2005-05-29 14:58:59 -070011854 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011855
Eric Dumazet9205fd92011-11-18 06:47:01 +000011856 /* tg3_free_rings will unmap and free the rx_data */
Michael Chanc76949a2005-05-29 14:58:59 -070011857out:
11858 return err;
11859}
11860
Matt Carlson00c266b2011-04-25 12:42:46 +000011861#define TG3_STD_LOOPBACK_FAILED 1
11862#define TG3_JMB_LOOPBACK_FAILED 2
Matt Carlsonbb158d62011-04-25 12:42:47 +000011863#define TG3_TSO_LOOPBACK_FAILED 4
Matt Carlson28a45952011-08-19 13:58:22 +000011864#define TG3_LOOPBACK_FAILED \
11865 (TG3_STD_LOOPBACK_FAILED | \
11866 TG3_JMB_LOOPBACK_FAILED | \
11867 TG3_TSO_LOOPBACK_FAILED)
Matt Carlson00c266b2011-04-25 12:42:46 +000011868
Matt Carlson941ec902011-08-19 13:58:23 +000011869static int tg3_test_loopback(struct tg3 *tp, u64 *data, bool do_extlpbk)
Michael Chan9f40dea2005-09-05 17:53:06 -070011870{
Matt Carlson28a45952011-08-19 13:58:22 +000011871 int err = -EIO;
Matt Carlson2215e242011-08-19 13:58:19 +000011872 u32 eee_cap;
Michael Chanc441b452012-03-04 14:48:13 +000011873 u32 jmb_pkt_sz = 9000;
11874
11875 if (tp->dma_limit)
11876 jmb_pkt_sz = tp->dma_limit - ETH_HLEN;
Michael Chan9f40dea2005-09-05 17:53:06 -070011877
Matt Carlsonab789042011-01-25 15:58:54 +000011878 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
11879 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
11880
Matt Carlson28a45952011-08-19 13:58:22 +000011881 if (!netif_running(tp->dev)) {
11882 data[0] = TG3_LOOPBACK_FAILED;
11883 data[1] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000011884 if (do_extlpbk)
11885 data[2] = TG3_LOOPBACK_FAILED;
Matt Carlson28a45952011-08-19 13:58:22 +000011886 goto done;
11887 }
11888
Michael Chanb9ec6c12006-07-25 16:37:27 -070011889 err = tg3_reset_hw(tp, 1);
Matt Carlsonab789042011-01-25 15:58:54 +000011890 if (err) {
Matt Carlson28a45952011-08-19 13:58:22 +000011891 data[0] = TG3_LOOPBACK_FAILED;
11892 data[1] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000011893 if (do_extlpbk)
11894 data[2] = TG3_LOOPBACK_FAILED;
Matt Carlsonab789042011-01-25 15:58:54 +000011895 goto done;
11896 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011897
Joe Perches63c3a662011-04-26 08:12:10 +000011898 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson4a85f092011-04-20 07:57:37 +000011899 int i;
11900
11901 /* Reroute all rx packets to the 1st queue */
11902 for (i = MAC_RSS_INDIR_TBL_0;
11903 i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
11904 tw32(i, 0x0);
11905 }
11906
Matt Carlson6e01b202011-08-19 13:58:20 +000011907 /* HW errata - mac loopback fails in some cases on 5780.
11908 * Normal traffic and PHY loopback are not affected by
11909 * errata. Also, the MAC loopback test is deprecated for
11910 * all newer ASIC revisions.
11911 */
11912 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
11913 !tg3_flag(tp, CPMU_PRESENT)) {
11914 tg3_mac_loopback(tp, true);
Matt Carlson9936bcf2007-10-10 18:03:07 -070011915
Matt Carlson28a45952011-08-19 13:58:22 +000011916 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
11917 data[0] |= TG3_STD_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000011918
11919 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000011920 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Matt Carlson28a45952011-08-19 13:58:22 +000011921 data[0] |= TG3_JMB_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000011922
11923 tg3_mac_loopback(tp, false);
11924 }
Matt Carlson4852a862011-04-13 11:05:07 +000011925
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011926 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000011927 !tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011928 int i;
11929
Matt Carlson941ec902011-08-19 13:58:23 +000011930 tg3_phy_lpbk_set(tp, 0, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011931
11932 /* Wait for link */
11933 for (i = 0; i < 100; i++) {
11934 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
11935 break;
11936 mdelay(1);
11937 }
11938
Matt Carlson28a45952011-08-19 13:58:22 +000011939 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
11940 data[1] |= TG3_STD_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000011941 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000011942 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
11943 data[1] |= TG3_TSO_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000011944 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000011945 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Matt Carlson28a45952011-08-19 13:58:22 +000011946 data[1] |= TG3_JMB_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070011947
Matt Carlson941ec902011-08-19 13:58:23 +000011948 if (do_extlpbk) {
11949 tg3_phy_lpbk_set(tp, 0, true);
11950
11951 /* All link indications report up, but the hardware
11952 * isn't really ready for about 20 msec. Double it
11953 * to be sure.
11954 */
11955 mdelay(40);
11956
11957 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
11958 data[2] |= TG3_STD_LOOPBACK_FAILED;
11959 if (tg3_flag(tp, TSO_CAPABLE) &&
11960 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
11961 data[2] |= TG3_TSO_LOOPBACK_FAILED;
11962 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000011963 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Matt Carlson941ec902011-08-19 13:58:23 +000011964 data[2] |= TG3_JMB_LOOPBACK_FAILED;
11965 }
11966
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011967 /* Re-enable gphy autopowerdown. */
11968 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
11969 tg3_phy_toggle_apd(tp, true);
11970 }
Matt Carlson6833c042008-11-21 17:18:59 -080011971
Matt Carlson941ec902011-08-19 13:58:23 +000011972 err = (data[0] | data[1] | data[2]) ? -EIO : 0;
Matt Carlson28a45952011-08-19 13:58:22 +000011973
Matt Carlsonab789042011-01-25 15:58:54 +000011974done:
11975 tp->phy_flags |= eee_cap;
11976
Michael Chan9f40dea2005-09-05 17:53:06 -070011977 return err;
11978}
11979
Michael Chan4cafd3f2005-05-29 14:56:34 -070011980static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
11981 u64 *data)
11982{
Michael Chan566f86a2005-05-29 14:56:58 -070011983 struct tg3 *tp = netdev_priv(dev);
Matt Carlson941ec902011-08-19 13:58:23 +000011984 bool doextlpbk = etest->flags & ETH_TEST_FL_EXTERNAL_LB;
Michael Chan566f86a2005-05-29 14:56:58 -070011985
Matt Carlsonbed98292011-07-13 09:27:29 +000011986 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
11987 tg3_power_up(tp)) {
11988 etest->flags |= ETH_TEST_FL_FAILED;
11989 memset(data, 1, sizeof(u64) * TG3_NUM_TEST);
11990 return;
11991 }
Michael Chanbc1c7562006-03-20 17:48:03 -080011992
Michael Chan566f86a2005-05-29 14:56:58 -070011993 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
11994
11995 if (tg3_test_nvram(tp) != 0) {
11996 etest->flags |= ETH_TEST_FL_FAILED;
11997 data[0] = 1;
11998 }
Matt Carlson941ec902011-08-19 13:58:23 +000011999 if (!doextlpbk && tg3_test_link(tp)) {
Michael Chanca430072005-05-29 14:57:23 -070012000 etest->flags |= ETH_TEST_FL_FAILED;
12001 data[1] = 1;
12002 }
Michael Chana71116d2005-05-29 14:58:11 -070012003 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012004 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070012005
Michael Chanbbe832c2005-06-24 20:20:04 -070012006 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012007 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070012008 tg3_netif_stop(tp);
12009 irq_sync = 1;
12010 }
12011
12012 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070012013
12014 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080012015 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012016 tg3_halt_cpu(tp, RX_CPU_BASE);
Joe Perches63c3a662011-04-26 08:12:10 +000012017 if (!tg3_flag(tp, 5705_PLUS))
Michael Chana71116d2005-05-29 14:58:11 -070012018 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080012019 if (!err)
12020 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012021
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012022 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad2006-03-20 22:27:35 -080012023 tg3_phy_reset(tp);
12024
Michael Chana71116d2005-05-29 14:58:11 -070012025 if (tg3_test_registers(tp) != 0) {
12026 etest->flags |= ETH_TEST_FL_FAILED;
12027 data[2] = 1;
12028 }
Matt Carlson28a45952011-08-19 13:58:22 +000012029
Michael Chan7942e1d2005-05-29 14:58:36 -070012030 if (tg3_test_memory(tp) != 0) {
12031 etest->flags |= ETH_TEST_FL_FAILED;
12032 data[3] = 1;
12033 }
Matt Carlson28a45952011-08-19 13:58:22 +000012034
Matt Carlson941ec902011-08-19 13:58:23 +000012035 if (doextlpbk)
12036 etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
12037
12038 if (tg3_test_loopback(tp, &data[4], doextlpbk))
Michael Chanc76949a2005-05-29 14:58:59 -070012039 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070012040
David S. Millerf47c11e2005-06-24 20:18:35 -070012041 tg3_full_unlock(tp);
12042
Michael Chand4bc3922005-05-29 14:59:20 -070012043 if (tg3_test_interrupt(tp) != 0) {
12044 etest->flags |= ETH_TEST_FL_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000012045 data[7] = 1;
Michael Chand4bc3922005-05-29 14:59:20 -070012046 }
David S. Millerf47c11e2005-06-24 20:18:35 -070012047
12048 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070012049
Michael Chana71116d2005-05-29 14:58:11 -070012050 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
12051 if (netif_running(dev)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012052 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012053 err2 = tg3_restart_hw(tp, 1);
12054 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070012055 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012056 }
David S. Millerf47c11e2005-06-24 20:18:35 -070012057
12058 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012059
12060 if (irq_sync && !err2)
12061 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012062 }
Matt Carlson800960682010-08-02 11:26:06 +000012063 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000012064 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080012065
Michael Chan4cafd3f2005-05-29 14:56:34 -070012066}
12067
Linus Torvalds1da177e2005-04-16 15:20:36 -070012068static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
12069{
12070 struct mii_ioctl_data *data = if_mii(ifr);
12071 struct tg3 *tp = netdev_priv(dev);
12072 int err;
12073
Joe Perches63c3a662011-04-26 08:12:10 +000012074 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000012075 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012076 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012077 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000012078 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000012079 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012080 }
12081
Matt Carlson33f401a2010-04-05 10:19:27 +000012082 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012083 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000012084 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012085
12086 /* fallthru */
12087 case SIOCGMIIREG: {
12088 u32 mii_regval;
12089
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012090 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012091 break; /* We have no PHY */
12092
Matt Carlson34eea5a2011-04-20 07:57:38 +000012093 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080012094 return -EAGAIN;
12095
David S. Millerf47c11e2005-06-24 20:18:35 -070012096 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012097 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070012098 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012099
12100 data->val_out = mii_regval;
12101
12102 return err;
12103 }
12104
12105 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012106 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012107 break; /* We have no PHY */
12108
Matt Carlson34eea5a2011-04-20 07:57:38 +000012109 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080012110 return -EAGAIN;
12111
David S. Millerf47c11e2005-06-24 20:18:35 -070012112 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012113 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070012114 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012115
12116 return err;
12117
12118 default:
12119 /* do nothing */
12120 break;
12121 }
12122 return -EOPNOTSUPP;
12123}
12124
David S. Miller15f98502005-05-18 22:49:26 -070012125static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
12126{
12127 struct tg3 *tp = netdev_priv(dev);
12128
12129 memcpy(ec, &tp->coal, sizeof(*ec));
12130 return 0;
12131}
12132
Michael Chand244c892005-07-05 14:42:33 -070012133static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
12134{
12135 struct tg3 *tp = netdev_priv(dev);
12136 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
12137 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
12138
Joe Perches63c3a662011-04-26 08:12:10 +000012139 if (!tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070012140 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
12141 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
12142 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
12143 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
12144 }
12145
12146 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
12147 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
12148 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
12149 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
12150 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
12151 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
12152 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
12153 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
12154 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
12155 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
12156 return -EINVAL;
12157
12158 /* No rx interrupts will be generated if both are zero */
12159 if ((ec->rx_coalesce_usecs == 0) &&
12160 (ec->rx_max_coalesced_frames == 0))
12161 return -EINVAL;
12162
12163 /* No tx interrupts will be generated if both are zero */
12164 if ((ec->tx_coalesce_usecs == 0) &&
12165 (ec->tx_max_coalesced_frames == 0))
12166 return -EINVAL;
12167
12168 /* Only copy relevant parameters, ignore all others. */
12169 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
12170 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
12171 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
12172 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
12173 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
12174 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
12175 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
12176 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
12177 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
12178
12179 if (netif_running(dev)) {
12180 tg3_full_lock(tp, 0);
12181 __tg3_set_coalesce(tp, &tp->coal);
12182 tg3_full_unlock(tp);
12183 }
12184 return 0;
12185}
12186
Jeff Garzik7282d492006-09-13 14:30:00 -040012187static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012188 .get_settings = tg3_get_settings,
12189 .set_settings = tg3_set_settings,
12190 .get_drvinfo = tg3_get_drvinfo,
12191 .get_regs_len = tg3_get_regs_len,
12192 .get_regs = tg3_get_regs,
12193 .get_wol = tg3_get_wol,
12194 .set_wol = tg3_set_wol,
12195 .get_msglevel = tg3_get_msglevel,
12196 .set_msglevel = tg3_set_msglevel,
12197 .nway_reset = tg3_nway_reset,
12198 .get_link = ethtool_op_get_link,
12199 .get_eeprom_len = tg3_get_eeprom_len,
12200 .get_eeprom = tg3_get_eeprom,
12201 .set_eeprom = tg3_set_eeprom,
12202 .get_ringparam = tg3_get_ringparam,
12203 .set_ringparam = tg3_set_ringparam,
12204 .get_pauseparam = tg3_get_pauseparam,
12205 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070012206 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012207 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000012208 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012209 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070012210 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070012211 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070012212 .get_sset_count = tg3_get_sset_count,
Matt Carlson90415472011-12-16 13:33:23 +000012213 .get_rxnfc = tg3_get_rxnfc,
12214 .get_rxfh_indir_size = tg3_get_rxfh_indir_size,
12215 .get_rxfh_indir = tg3_get_rxfh_indir,
12216 .set_rxfh_indir = tg3_set_rxfh_indir,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012217};
12218
David S. Millerb4017c52012-03-01 17:57:40 -050012219static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
12220 struct rtnl_link_stats64 *stats)
12221{
12222 struct tg3 *tp = netdev_priv(dev);
12223
12224 if (!tp->hw_stats)
12225 return &tp->net_stats_prev;
12226
12227 spin_lock_bh(&tp->lock);
12228 tg3_get_nstats(tp, stats);
12229 spin_unlock_bh(&tp->lock);
12230
12231 return stats;
12232}
12233
Matt Carlsonccd5ba92012-02-13 10:20:08 +000012234static void tg3_set_rx_mode(struct net_device *dev)
12235{
12236 struct tg3 *tp = netdev_priv(dev);
12237
12238 if (!netif_running(dev))
12239 return;
12240
12241 tg3_full_lock(tp, 0);
12242 __tg3_set_rx_mode(dev);
12243 tg3_full_unlock(tp);
12244}
12245
Matt Carlsonfaf16272012-02-13 10:20:07 +000012246static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
12247 int new_mtu)
12248{
12249 dev->mtu = new_mtu;
12250
12251 if (new_mtu > ETH_DATA_LEN) {
12252 if (tg3_flag(tp, 5780_CLASS)) {
12253 netdev_update_features(dev);
12254 tg3_flag_clear(tp, TSO_CAPABLE);
12255 } else {
12256 tg3_flag_set(tp, JUMBO_RING_ENABLE);
12257 }
12258 } else {
12259 if (tg3_flag(tp, 5780_CLASS)) {
12260 tg3_flag_set(tp, TSO_CAPABLE);
12261 netdev_update_features(dev);
12262 }
12263 tg3_flag_clear(tp, JUMBO_RING_ENABLE);
12264 }
12265}
12266
12267static int tg3_change_mtu(struct net_device *dev, int new_mtu)
12268{
12269 struct tg3 *tp = netdev_priv(dev);
12270 int err;
12271
12272 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
12273 return -EINVAL;
12274
12275 if (!netif_running(dev)) {
12276 /* We'll just catch it later when the
12277 * device is up'd.
12278 */
12279 tg3_set_mtu(dev, tp, new_mtu);
12280 return 0;
12281 }
12282
12283 tg3_phy_stop(tp);
12284
12285 tg3_netif_stop(tp);
12286
12287 tg3_full_lock(tp, 1);
12288
12289 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
12290
12291 tg3_set_mtu(dev, tp, new_mtu);
12292
12293 err = tg3_restart_hw(tp, 0);
12294
12295 if (!err)
12296 tg3_netif_start(tp);
12297
12298 tg3_full_unlock(tp);
12299
12300 if (!err)
12301 tg3_phy_start(tp);
12302
12303 return err;
12304}
12305
12306static const struct net_device_ops tg3_netdev_ops = {
12307 .ndo_open = tg3_open,
12308 .ndo_stop = tg3_close,
12309 .ndo_start_xmit = tg3_start_xmit,
12310 .ndo_get_stats64 = tg3_get_stats64,
12311 .ndo_validate_addr = eth_validate_addr,
12312 .ndo_set_rx_mode = tg3_set_rx_mode,
12313 .ndo_set_mac_address = tg3_set_mac_addr,
12314 .ndo_do_ioctl = tg3_ioctl,
12315 .ndo_tx_timeout = tg3_tx_timeout,
12316 .ndo_change_mtu = tg3_change_mtu,
12317 .ndo_fix_features = tg3_fix_features,
12318 .ndo_set_features = tg3_set_features,
12319#ifdef CONFIG_NET_POLL_CONTROLLER
12320 .ndo_poll_controller = tg3_poll_controller,
12321#endif
12322};
12323
Linus Torvalds1da177e2005-04-16 15:20:36 -070012324static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
12325{
Michael Chan1b277772006-03-20 22:27:48 -080012326 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012327
12328 tp->nvram_size = EEPROM_CHIP_SIZE;
12329
Matt Carlsone4f34112009-02-25 14:25:00 +000012330 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012331 return;
12332
Michael Chanb16250e2006-09-27 16:10:14 -070012333 if ((magic != TG3_EEPROM_MAGIC) &&
12334 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
12335 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012336 return;
12337
12338 /*
12339 * Size the chip by reading offsets at increasing powers of two.
12340 * When we encounter our validation signature, we know the addressing
12341 * has wrapped around, and thus have our chip size.
12342 */
Michael Chan1b277772006-03-20 22:27:48 -080012343 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012344
12345 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000012346 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012347 return;
12348
Michael Chan18201802006-03-20 22:29:15 -080012349 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012350 break;
12351
12352 cursize <<= 1;
12353 }
12354
12355 tp->nvram_size = cursize;
12356}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012357
Linus Torvalds1da177e2005-04-16 15:20:36 -070012358static void __devinit tg3_get_nvram_size(struct tg3 *tp)
12359{
12360 u32 val;
12361
Joe Perches63c3a662011-04-26 08:12:10 +000012362 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080012363 return;
12364
12365 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080012366 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080012367 tg3_get_eeprom_size(tp);
12368 return;
12369 }
12370
Matt Carlson6d348f22009-02-25 14:25:52 +000012371 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012372 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000012373 /* This is confusing. We want to operate on the
12374 * 16-bit value at offset 0xf2. The tg3_nvram_read()
12375 * call will read from NVRAM and byteswap the data
12376 * according to the byteswapping settings for all
12377 * other register accesses. This ensures the data we
12378 * want will always reside in the lower 16-bits.
12379 * However, the data in NVRAM is in LE format, which
12380 * means the data from the NVRAM read will always be
12381 * opposite the endianness of the CPU. The 16-bit
12382 * byteswap then brings the data to CPU endianness.
12383 */
12384 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012385 return;
12386 }
12387 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070012388 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012389}
12390
12391static void __devinit tg3_get_nvram_info(struct tg3 *tp)
12392{
12393 u32 nvcfg1;
12394
12395 nvcfg1 = tr32(NVRAM_CFG1);
12396 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
Joe Perches63c3a662011-04-26 08:12:10 +000012397 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012398 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012399 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12400 tw32(NVRAM_CFG1, nvcfg1);
12401 }
12402
Matt Carlson6ff6f812011-05-19 12:12:54 +000012403 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
Joe Perches63c3a662011-04-26 08:12:10 +000012404 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012405 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012406 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
12407 tp->nvram_jedecnum = JEDEC_ATMEL;
12408 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012409 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012410 break;
12411 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
12412 tp->nvram_jedecnum = JEDEC_ATMEL;
12413 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
12414 break;
12415 case FLASH_VENDOR_ATMEL_EEPROM:
12416 tp->nvram_jedecnum = JEDEC_ATMEL;
12417 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012418 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012419 break;
12420 case FLASH_VENDOR_ST:
12421 tp->nvram_jedecnum = JEDEC_ST;
12422 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012423 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012424 break;
12425 case FLASH_VENDOR_SAIFUN:
12426 tp->nvram_jedecnum = JEDEC_SAIFUN;
12427 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
12428 break;
12429 case FLASH_VENDOR_SST_SMALL:
12430 case FLASH_VENDOR_SST_LARGE:
12431 tp->nvram_jedecnum = JEDEC_SST;
12432 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
12433 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012434 }
Matt Carlson8590a602009-08-28 12:29:16 +000012435 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012436 tp->nvram_jedecnum = JEDEC_ATMEL;
12437 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012438 tg3_flag_set(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012439 }
12440}
12441
Matt Carlsona1b950d2009-09-01 13:20:17 +000012442static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
12443{
12444 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
12445 case FLASH_5752PAGE_SIZE_256:
12446 tp->nvram_pagesize = 256;
12447 break;
12448 case FLASH_5752PAGE_SIZE_512:
12449 tp->nvram_pagesize = 512;
12450 break;
12451 case FLASH_5752PAGE_SIZE_1K:
12452 tp->nvram_pagesize = 1024;
12453 break;
12454 case FLASH_5752PAGE_SIZE_2K:
12455 tp->nvram_pagesize = 2048;
12456 break;
12457 case FLASH_5752PAGE_SIZE_4K:
12458 tp->nvram_pagesize = 4096;
12459 break;
12460 case FLASH_5752PAGE_SIZE_264:
12461 tp->nvram_pagesize = 264;
12462 break;
12463 case FLASH_5752PAGE_SIZE_528:
12464 tp->nvram_pagesize = 528;
12465 break;
12466 }
12467}
12468
Michael Chan361b4ac2005-04-21 17:11:21 -070012469static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
12470{
12471 u32 nvcfg1;
12472
12473 nvcfg1 = tr32(NVRAM_CFG1);
12474
Michael Chane6af3012005-04-21 17:12:05 -070012475 /* NVRAM protection for TPM */
12476 if (nvcfg1 & (1 << 27))
Joe Perches63c3a662011-04-26 08:12:10 +000012477 tg3_flag_set(tp, PROTECTED_NVRAM);
Michael Chane6af3012005-04-21 17:12:05 -070012478
Michael Chan361b4ac2005-04-21 17:11:21 -070012479 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012480 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
12481 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
12482 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012483 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012484 break;
12485 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12486 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012487 tg3_flag_set(tp, NVRAM_BUFFERED);
12488 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012489 break;
12490 case FLASH_5752VENDOR_ST_M45PE10:
12491 case FLASH_5752VENDOR_ST_M45PE20:
12492 case FLASH_5752VENDOR_ST_M45PE40:
12493 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012494 tg3_flag_set(tp, NVRAM_BUFFERED);
12495 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012496 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070012497 }
12498
Joe Perches63c3a662011-04-26 08:12:10 +000012499 if (tg3_flag(tp, FLASH)) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000012500 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000012501 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070012502 /* For eeprom, set pagesize to maximum eeprom size */
12503 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12504
12505 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12506 tw32(NVRAM_CFG1, nvcfg1);
12507 }
12508}
12509
Michael Chand3c7b882006-03-23 01:28:25 -080012510static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
12511{
Matt Carlson989a9d22007-05-05 11:51:05 -070012512 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080012513
12514 nvcfg1 = tr32(NVRAM_CFG1);
12515
12516 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070012517 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012518 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson989a9d22007-05-05 11:51:05 -070012519 protect = 1;
12520 }
Michael Chand3c7b882006-03-23 01:28:25 -080012521
Matt Carlson989a9d22007-05-05 11:51:05 -070012522 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
12523 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012524 case FLASH_5755VENDOR_ATMEL_FLASH_1:
12525 case FLASH_5755VENDOR_ATMEL_FLASH_2:
12526 case FLASH_5755VENDOR_ATMEL_FLASH_3:
12527 case FLASH_5755VENDOR_ATMEL_FLASH_5:
12528 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012529 tg3_flag_set(tp, NVRAM_BUFFERED);
12530 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012531 tp->nvram_pagesize = 264;
12532 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
12533 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
12534 tp->nvram_size = (protect ? 0x3e200 :
12535 TG3_NVRAM_SIZE_512KB);
12536 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
12537 tp->nvram_size = (protect ? 0x1f200 :
12538 TG3_NVRAM_SIZE_256KB);
12539 else
12540 tp->nvram_size = (protect ? 0x1f200 :
12541 TG3_NVRAM_SIZE_128KB);
12542 break;
12543 case FLASH_5752VENDOR_ST_M45PE10:
12544 case FLASH_5752VENDOR_ST_M45PE20:
12545 case FLASH_5752VENDOR_ST_M45PE40:
12546 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012547 tg3_flag_set(tp, NVRAM_BUFFERED);
12548 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012549 tp->nvram_pagesize = 256;
12550 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
12551 tp->nvram_size = (protect ?
12552 TG3_NVRAM_SIZE_64KB :
12553 TG3_NVRAM_SIZE_128KB);
12554 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
12555 tp->nvram_size = (protect ?
12556 TG3_NVRAM_SIZE_64KB :
12557 TG3_NVRAM_SIZE_256KB);
12558 else
12559 tp->nvram_size = (protect ?
12560 TG3_NVRAM_SIZE_128KB :
12561 TG3_NVRAM_SIZE_512KB);
12562 break;
Michael Chand3c7b882006-03-23 01:28:25 -080012563 }
12564}
12565
Michael Chan1b277772006-03-20 22:27:48 -080012566static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
12567{
12568 u32 nvcfg1;
12569
12570 nvcfg1 = tr32(NVRAM_CFG1);
12571
12572 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012573 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
12574 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
12575 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
12576 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
12577 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012578 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012579 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080012580
Matt Carlson8590a602009-08-28 12:29:16 +000012581 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12582 tw32(NVRAM_CFG1, nvcfg1);
12583 break;
12584 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12585 case FLASH_5755VENDOR_ATMEL_FLASH_1:
12586 case FLASH_5755VENDOR_ATMEL_FLASH_2:
12587 case FLASH_5755VENDOR_ATMEL_FLASH_3:
12588 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012589 tg3_flag_set(tp, NVRAM_BUFFERED);
12590 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012591 tp->nvram_pagesize = 264;
12592 break;
12593 case FLASH_5752VENDOR_ST_M45PE10:
12594 case FLASH_5752VENDOR_ST_M45PE20:
12595 case FLASH_5752VENDOR_ST_M45PE40:
12596 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012597 tg3_flag_set(tp, NVRAM_BUFFERED);
12598 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012599 tp->nvram_pagesize = 256;
12600 break;
Michael Chan1b277772006-03-20 22:27:48 -080012601 }
12602}
12603
Matt Carlson6b91fa02007-10-10 18:01:09 -070012604static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
12605{
12606 u32 nvcfg1, protect = 0;
12607
12608 nvcfg1 = tr32(NVRAM_CFG1);
12609
12610 /* NVRAM protection for TPM */
12611 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012612 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson6b91fa02007-10-10 18:01:09 -070012613 protect = 1;
12614 }
12615
12616 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
12617 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012618 case FLASH_5761VENDOR_ATMEL_ADB021D:
12619 case FLASH_5761VENDOR_ATMEL_ADB041D:
12620 case FLASH_5761VENDOR_ATMEL_ADB081D:
12621 case FLASH_5761VENDOR_ATMEL_ADB161D:
12622 case FLASH_5761VENDOR_ATMEL_MDB021D:
12623 case FLASH_5761VENDOR_ATMEL_MDB041D:
12624 case FLASH_5761VENDOR_ATMEL_MDB081D:
12625 case FLASH_5761VENDOR_ATMEL_MDB161D:
12626 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012627 tg3_flag_set(tp, NVRAM_BUFFERED);
12628 tg3_flag_set(tp, FLASH);
12629 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson8590a602009-08-28 12:29:16 +000012630 tp->nvram_pagesize = 256;
12631 break;
12632 case FLASH_5761VENDOR_ST_A_M45PE20:
12633 case FLASH_5761VENDOR_ST_A_M45PE40:
12634 case FLASH_5761VENDOR_ST_A_M45PE80:
12635 case FLASH_5761VENDOR_ST_A_M45PE16:
12636 case FLASH_5761VENDOR_ST_M_M45PE20:
12637 case FLASH_5761VENDOR_ST_M_M45PE40:
12638 case FLASH_5761VENDOR_ST_M_M45PE80:
12639 case FLASH_5761VENDOR_ST_M_M45PE16:
12640 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012641 tg3_flag_set(tp, NVRAM_BUFFERED);
12642 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012643 tp->nvram_pagesize = 256;
12644 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070012645 }
12646
12647 if (protect) {
12648 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
12649 } else {
12650 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012651 case FLASH_5761VENDOR_ATMEL_ADB161D:
12652 case FLASH_5761VENDOR_ATMEL_MDB161D:
12653 case FLASH_5761VENDOR_ST_A_M45PE16:
12654 case FLASH_5761VENDOR_ST_M_M45PE16:
12655 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
12656 break;
12657 case FLASH_5761VENDOR_ATMEL_ADB081D:
12658 case FLASH_5761VENDOR_ATMEL_MDB081D:
12659 case FLASH_5761VENDOR_ST_A_M45PE80:
12660 case FLASH_5761VENDOR_ST_M_M45PE80:
12661 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12662 break;
12663 case FLASH_5761VENDOR_ATMEL_ADB041D:
12664 case FLASH_5761VENDOR_ATMEL_MDB041D:
12665 case FLASH_5761VENDOR_ST_A_M45PE40:
12666 case FLASH_5761VENDOR_ST_M_M45PE40:
12667 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12668 break;
12669 case FLASH_5761VENDOR_ATMEL_ADB021D:
12670 case FLASH_5761VENDOR_ATMEL_MDB021D:
12671 case FLASH_5761VENDOR_ST_A_M45PE20:
12672 case FLASH_5761VENDOR_ST_M_M45PE20:
12673 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12674 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070012675 }
12676 }
12677}
12678
Michael Chanb5d37722006-09-27 16:06:21 -070012679static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
12680{
12681 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012682 tg3_flag_set(tp, NVRAM_BUFFERED);
Michael Chanb5d37722006-09-27 16:06:21 -070012683 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12684}
12685
Matt Carlson321d32a2008-11-21 17:22:19 -080012686static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
12687{
12688 u32 nvcfg1;
12689
12690 nvcfg1 = tr32(NVRAM_CFG1);
12691
12692 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12693 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
12694 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
12695 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012696 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson321d32a2008-11-21 17:22:19 -080012697 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12698
12699 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12700 tw32(NVRAM_CFG1, nvcfg1);
12701 return;
12702 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12703 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
12704 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
12705 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
12706 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
12707 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
12708 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
12709 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012710 tg3_flag_set(tp, NVRAM_BUFFERED);
12711 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080012712
12713 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12714 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12715 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
12716 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
12717 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12718 break;
12719 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
12720 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
12721 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12722 break;
12723 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
12724 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
12725 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12726 break;
12727 }
12728 break;
12729 case FLASH_5752VENDOR_ST_M45PE10:
12730 case FLASH_5752VENDOR_ST_M45PE20:
12731 case FLASH_5752VENDOR_ST_M45PE40:
12732 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012733 tg3_flag_set(tp, NVRAM_BUFFERED);
12734 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080012735
12736 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12737 case FLASH_5752VENDOR_ST_M45PE10:
12738 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12739 break;
12740 case FLASH_5752VENDOR_ST_M45PE20:
12741 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12742 break;
12743 case FLASH_5752VENDOR_ST_M45PE40:
12744 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12745 break;
12746 }
12747 break;
12748 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012749 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson321d32a2008-11-21 17:22:19 -080012750 return;
12751 }
12752
Matt Carlsona1b950d2009-09-01 13:20:17 +000012753 tg3_nvram_get_pagesize(tp, nvcfg1);
12754 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012755 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012756}
12757
12758
12759static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
12760{
12761 u32 nvcfg1;
12762
12763 nvcfg1 = tr32(NVRAM_CFG1);
12764
12765 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12766 case FLASH_5717VENDOR_ATMEL_EEPROM:
12767 case FLASH_5717VENDOR_MICRO_EEPROM:
12768 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012769 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012770 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12771
12772 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12773 tw32(NVRAM_CFG1, nvcfg1);
12774 return;
12775 case FLASH_5717VENDOR_ATMEL_MDB011D:
12776 case FLASH_5717VENDOR_ATMEL_ADB011B:
12777 case FLASH_5717VENDOR_ATMEL_ADB011D:
12778 case FLASH_5717VENDOR_ATMEL_MDB021D:
12779 case FLASH_5717VENDOR_ATMEL_ADB021B:
12780 case FLASH_5717VENDOR_ATMEL_ADB021D:
12781 case FLASH_5717VENDOR_ATMEL_45USPT:
12782 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012783 tg3_flag_set(tp, NVRAM_BUFFERED);
12784 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012785
12786 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12787 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012788 /* Detect size with tg3_nvram_get_size() */
12789 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012790 case FLASH_5717VENDOR_ATMEL_ADB021B:
12791 case FLASH_5717VENDOR_ATMEL_ADB021D:
12792 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12793 break;
12794 default:
12795 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12796 break;
12797 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012798 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012799 case FLASH_5717VENDOR_ST_M_M25PE10:
12800 case FLASH_5717VENDOR_ST_A_M25PE10:
12801 case FLASH_5717VENDOR_ST_M_M45PE10:
12802 case FLASH_5717VENDOR_ST_A_M45PE10:
12803 case FLASH_5717VENDOR_ST_M_M25PE20:
12804 case FLASH_5717VENDOR_ST_A_M25PE20:
12805 case FLASH_5717VENDOR_ST_M_M45PE20:
12806 case FLASH_5717VENDOR_ST_A_M45PE20:
12807 case FLASH_5717VENDOR_ST_25USPT:
12808 case FLASH_5717VENDOR_ST_45USPT:
12809 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012810 tg3_flag_set(tp, NVRAM_BUFFERED);
12811 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012812
12813 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12814 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012815 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012816 /* Detect size with tg3_nvram_get_size() */
12817 break;
12818 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012819 case FLASH_5717VENDOR_ST_A_M45PE20:
12820 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12821 break;
12822 default:
12823 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12824 break;
12825 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012826 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012827 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012828 tg3_flag_set(tp, NO_NVRAM);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012829 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080012830 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000012831
12832 tg3_nvram_get_pagesize(tp, nvcfg1);
12833 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012834 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson321d32a2008-11-21 17:22:19 -080012835}
12836
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012837static void __devinit tg3_get_5720_nvram_info(struct tg3 *tp)
12838{
12839 u32 nvcfg1, nvmpinstrp;
12840
12841 nvcfg1 = tr32(NVRAM_CFG1);
12842 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
12843
12844 switch (nvmpinstrp) {
12845 case FLASH_5720_EEPROM_HD:
12846 case FLASH_5720_EEPROM_LD:
12847 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012848 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012849
12850 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12851 tw32(NVRAM_CFG1, nvcfg1);
12852 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
12853 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12854 else
12855 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
12856 return;
12857 case FLASH_5720VENDOR_M_ATMEL_DB011D:
12858 case FLASH_5720VENDOR_A_ATMEL_DB011B:
12859 case FLASH_5720VENDOR_A_ATMEL_DB011D:
12860 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12861 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12862 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12863 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12864 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12865 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12866 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12867 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12868 case FLASH_5720VENDOR_ATMEL_45USPT:
12869 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012870 tg3_flag_set(tp, NVRAM_BUFFERED);
12871 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012872
12873 switch (nvmpinstrp) {
12874 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12875 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12876 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12877 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12878 break;
12879 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12880 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12881 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12882 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12883 break;
12884 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12885 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12886 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12887 break;
12888 default:
12889 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12890 break;
12891 }
12892 break;
12893 case FLASH_5720VENDOR_M_ST_M25PE10:
12894 case FLASH_5720VENDOR_M_ST_M45PE10:
12895 case FLASH_5720VENDOR_A_ST_M25PE10:
12896 case FLASH_5720VENDOR_A_ST_M45PE10:
12897 case FLASH_5720VENDOR_M_ST_M25PE20:
12898 case FLASH_5720VENDOR_M_ST_M45PE20:
12899 case FLASH_5720VENDOR_A_ST_M25PE20:
12900 case FLASH_5720VENDOR_A_ST_M45PE20:
12901 case FLASH_5720VENDOR_M_ST_M25PE40:
12902 case FLASH_5720VENDOR_M_ST_M45PE40:
12903 case FLASH_5720VENDOR_A_ST_M25PE40:
12904 case FLASH_5720VENDOR_A_ST_M45PE40:
12905 case FLASH_5720VENDOR_M_ST_M25PE80:
12906 case FLASH_5720VENDOR_M_ST_M45PE80:
12907 case FLASH_5720VENDOR_A_ST_M25PE80:
12908 case FLASH_5720VENDOR_A_ST_M45PE80:
12909 case FLASH_5720VENDOR_ST_25USPT:
12910 case FLASH_5720VENDOR_ST_45USPT:
12911 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012912 tg3_flag_set(tp, NVRAM_BUFFERED);
12913 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012914
12915 switch (nvmpinstrp) {
12916 case FLASH_5720VENDOR_M_ST_M25PE20:
12917 case FLASH_5720VENDOR_M_ST_M45PE20:
12918 case FLASH_5720VENDOR_A_ST_M25PE20:
12919 case FLASH_5720VENDOR_A_ST_M45PE20:
12920 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12921 break;
12922 case FLASH_5720VENDOR_M_ST_M25PE40:
12923 case FLASH_5720VENDOR_M_ST_M45PE40:
12924 case FLASH_5720VENDOR_A_ST_M25PE40:
12925 case FLASH_5720VENDOR_A_ST_M45PE40:
12926 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12927 break;
12928 case FLASH_5720VENDOR_M_ST_M25PE80:
12929 case FLASH_5720VENDOR_M_ST_M45PE80:
12930 case FLASH_5720VENDOR_A_ST_M25PE80:
12931 case FLASH_5720VENDOR_A_ST_M45PE80:
12932 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12933 break;
12934 default:
12935 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12936 break;
12937 }
12938 break;
12939 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012940 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012941 return;
12942 }
12943
12944 tg3_nvram_get_pagesize(tp, nvcfg1);
12945 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012946 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012947}
12948
Linus Torvalds1da177e2005-04-16 15:20:36 -070012949/* Chips other than 5700/5701 use the NVRAM for fetching info. */
12950static void __devinit tg3_nvram_init(struct tg3 *tp)
12951{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012952 tw32_f(GRC_EEPROM_ADDR,
12953 (EEPROM_ADDR_FSM_RESET |
12954 (EEPROM_DEFAULT_CLOCK_PERIOD <<
12955 EEPROM_ADDR_CLKPERD_SHIFT)));
12956
Michael Chan9d57f012006-12-07 00:23:25 -080012957 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012958
12959 /* Enable seeprom accesses. */
12960 tw32_f(GRC_LOCAL_CTRL,
12961 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
12962 udelay(100);
12963
12964 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
12965 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
Joe Perches63c3a662011-04-26 08:12:10 +000012966 tg3_flag_set(tp, NVRAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012967
Michael Chanec41c7d2006-01-17 02:40:55 -080012968 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000012969 netdev_warn(tp->dev,
12970 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000012971 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080012972 return;
12973 }
Michael Chane6af3012005-04-21 17:12:05 -070012974 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012975
Matt Carlson989a9d22007-05-05 11:51:05 -070012976 tp->nvram_size = 0;
12977
Michael Chan361b4ac2005-04-21 17:11:21 -070012978 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
12979 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080012980 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
12981 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070012982 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070012983 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
12984 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080012985 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070012986 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
12987 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070012988 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12989 tg3_get_5906_nvram_info(tp);
Matt Carlsonb703df62009-12-03 08:36:21 +000012990 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000012991 tg3_flag(tp, 57765_CLASS))
Matt Carlson321d32a2008-11-21 17:22:19 -080012992 tg3_get_57780_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012993 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
12994 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000012995 tg3_get_5717_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012996 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
12997 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070012998 else
12999 tg3_get_nvram_info(tp);
13000
Matt Carlson989a9d22007-05-05 11:51:05 -070013001 if (tp->nvram_size == 0)
13002 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013003
Michael Chane6af3012005-04-21 17:12:05 -070013004 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080013005 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013006
13007 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000013008 tg3_flag_clear(tp, NVRAM);
13009 tg3_flag_clear(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013010
13011 tg3_get_eeprom_size(tp);
13012 }
13013}
13014
Linus Torvalds1da177e2005-04-16 15:20:36 -070013015struct subsys_tbl_ent {
13016 u16 subsys_vendor, subsys_devid;
13017 u32 phy_id;
13018};
13019
Matt Carlson24daf2b2010-02-17 15:17:02 +000013020static struct subsys_tbl_ent subsys_id_to_phy_id[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013021 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013022 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013023 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013024 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013025 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013026 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013027 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013028 { TG3PCI_SUBVENDOR_ID_BROADCOM,
13029 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
13030 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013031 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013032 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013033 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013034 { TG3PCI_SUBVENDOR_ID_BROADCOM,
13035 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
13036 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013037 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013038 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013039 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013040 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013041 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013042 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013043 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013044
13045 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013046 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013047 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013048 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013049 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013050 { TG3PCI_SUBVENDOR_ID_3COM,
13051 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
13052 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013053 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013054 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013055 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013056
13057 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013058 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013059 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013060 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013061 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013062 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013063 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013064 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013065 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013066
13067 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013068 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013069 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013070 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013071 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013072 { TG3PCI_SUBVENDOR_ID_COMPAQ,
13073 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
13074 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013075 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013076 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013077 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013078
13079 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013080 { TG3PCI_SUBVENDOR_ID_IBM,
13081 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013082};
13083
Matt Carlson24daf2b2010-02-17 15:17:02 +000013084static struct subsys_tbl_ent * __devinit tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013085{
13086 int i;
13087
13088 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
13089 if ((subsys_id_to_phy_id[i].subsys_vendor ==
13090 tp->pdev->subsystem_vendor) &&
13091 (subsys_id_to_phy_id[i].subsys_devid ==
13092 tp->pdev->subsystem_device))
13093 return &subsys_id_to_phy_id[i];
13094 }
13095 return NULL;
13096}
13097
Michael Chan7d0c41e2005-04-21 17:06:20 -070013098static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013099{
Linus Torvalds1da177e2005-04-16 15:20:36 -070013100 u32 val;
David S. Millerf49639e2006-06-09 11:58:36 -070013101
Matt Carlson79eb6902010-02-17 15:17:03 +000013102 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070013103 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13104
Gary Zambranoa85feb82007-05-05 11:52:19 -070013105 /* Assume an onboard device and WOL capable by default. */
Joe Perches63c3a662011-04-26 08:12:10 +000013106 tg3_flag_set(tp, EEPROM_WRITE_PROT);
13107 tg3_flag_set(tp, WOL_CAP);
David S. Miller72b845e2006-03-14 14:11:48 -080013108
Michael Chanb5d37722006-09-27 16:06:21 -070013109 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080013110 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013111 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
13112 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080013113 }
Matt Carlson0527ba32007-10-10 18:03:30 -070013114 val = tr32(VCPU_CFGSHDW);
13115 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Joe Perches63c3a662011-04-26 08:12:10 +000013116 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson0527ba32007-10-10 18:03:30 -070013117 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013118 (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013119 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013120 device_set_wakeup_enable(&tp->pdev->dev, true);
13121 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080013122 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070013123 }
13124
Linus Torvalds1da177e2005-04-16 15:20:36 -070013125 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
13126 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
13127 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070013128 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070013129 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013130
13131 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
13132 tp->nic_sram_data_cfg = nic_cfg;
13133
13134 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
13135 ver >>= NIC_SRAM_DATA_VER_SHIFT;
Matt Carlson6ff6f812011-05-19 12:12:54 +000013136 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13137 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13138 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070013139 (ver > 0) && (ver < 0x100))
13140 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
13141
Matt Carlsona9daf362008-05-25 23:49:44 -070013142 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
13143 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
13144
Linus Torvalds1da177e2005-04-16 15:20:36 -070013145 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
13146 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
13147 eeprom_phy_serdes = 1;
13148
13149 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
13150 if (nic_phy_id != 0) {
13151 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
13152 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
13153
13154 eeprom_phy_id = (id1 >> 16) << 10;
13155 eeprom_phy_id |= (id2 & 0xfc00) << 16;
13156 eeprom_phy_id |= (id2 & 0x03ff) << 0;
13157 } else
13158 eeprom_phy_id = 0;
13159
Michael Chan7d0c41e2005-04-21 17:06:20 -070013160 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070013161 if (eeprom_phy_serdes) {
Joe Perches63c3a662011-04-26 08:12:10 +000013162 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013163 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000013164 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013165 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070013166 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070013167
Joe Perches63c3a662011-04-26 08:12:10 +000013168 if (tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013169 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
13170 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070013171 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070013172 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
13173
13174 switch (led_cfg) {
13175 default:
13176 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
13177 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13178 break;
13179
13180 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
13181 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
13182 break;
13183
13184 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
13185 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070013186
13187 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
13188 * read on some older 5700/5701 bootcode.
13189 */
13190 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
13191 ASIC_REV_5700 ||
13192 GET_ASIC_REV(tp->pci_chip_rev_id) ==
13193 ASIC_REV_5701)
13194 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13195
Linus Torvalds1da177e2005-04-16 15:20:36 -070013196 break;
13197
13198 case SHASTA_EXT_LED_SHARED:
13199 tp->led_ctrl = LED_CTRL_MODE_SHARED;
13200 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
13201 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
13202 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
13203 LED_CTRL_MODE_PHY_2);
13204 break;
13205
13206 case SHASTA_EXT_LED_MAC:
13207 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
13208 break;
13209
13210 case SHASTA_EXT_LED_COMBO:
13211 tp->led_ctrl = LED_CTRL_MODE_COMBO;
13212 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
13213 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
13214 LED_CTRL_MODE_PHY_2);
13215 break;
13216
Stephen Hemminger855e1112008-04-16 16:37:28 -070013217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013218
13219 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13220 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
13221 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
13222 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
13223
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013224 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
13225 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080013226
Michael Chan9d26e212006-12-07 00:21:14 -080013227 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Joe Perches63c3a662011-04-26 08:12:10 +000013228 tg3_flag_set(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080013229 if ((tp->pdev->subsystem_vendor ==
13230 PCI_VENDOR_ID_ARIMA) &&
13231 (tp->pdev->subsystem_device == 0x205a ||
13232 tp->pdev->subsystem_device == 0x2063))
Joe Perches63c3a662011-04-26 08:12:10 +000013233 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080013234 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000013235 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
13236 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080013237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013238
13239 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +000013240 tg3_flag_set(tp, ENABLE_ASF);
13241 if (tg3_flag(tp, 5750_PLUS))
13242 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013243 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080013244
13245 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013246 tg3_flag(tp, 5750_PLUS))
13247 tg3_flag_set(tp, ENABLE_APE);
Matt Carlsonb2b98d42008-11-03 16:52:32 -080013248
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013249 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070013250 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
Joe Perches63c3a662011-04-26 08:12:10 +000013251 tg3_flag_clear(tp, WOL_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013252
Joe Perches63c3a662011-04-26 08:12:10 +000013253 if (tg3_flag(tp, WOL_CAP) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013254 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013255 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013256 device_set_wakeup_enable(&tp->pdev->dev, true);
13257 }
Matt Carlson0527ba32007-10-10 18:03:30 -070013258
Linus Torvalds1da177e2005-04-16 15:20:36 -070013259 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013260 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013261
13262 /* serdes signal pre-emphasis in register 0x590 set by */
13263 /* bootcode if bit 18 is set */
13264 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013265 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070013266
Joe Perches63c3a662011-04-26 08:12:10 +000013267 if ((tg3_flag(tp, 57765_PLUS) ||
13268 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
13269 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080013270 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013271 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080013272
Joe Perches63c3a662011-04-26 08:12:10 +000013273 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson8c69b1e2010-08-02 11:26:00 +000013274 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +000013275 !tg3_flag(tp, 57765_PLUS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070013276 u32 cfg3;
13277
13278 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
13279 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
Joe Perches63c3a662011-04-26 08:12:10 +000013280 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson8ed5d972007-05-07 00:25:49 -070013281 }
Matt Carlsona9daf362008-05-25 23:49:44 -070013282
Matt Carlson14417062010-02-17 15:16:59 +000013283 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
Joe Perches63c3a662011-04-26 08:12:10 +000013284 tg3_flag_set(tp, RGMII_INBAND_DISABLE);
Matt Carlsona9daf362008-05-25 23:49:44 -070013285 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000013286 tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
Matt Carlsona9daf362008-05-25 23:49:44 -070013287 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000013288 tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013289 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080013290done:
Joe Perches63c3a662011-04-26 08:12:10 +000013291 if (tg3_flag(tp, WOL_CAP))
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000013292 device_set_wakeup_enable(&tp->pdev->dev,
Joe Perches63c3a662011-04-26 08:12:10 +000013293 tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000013294 else
13295 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070013296}
13297
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013298static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
13299{
13300 int i;
13301 u32 val;
13302
13303 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
13304 tw32(OTP_CTRL, cmd);
13305
13306 /* Wait for up to 1 ms for command to execute. */
13307 for (i = 0; i < 100; i++) {
13308 val = tr32(OTP_STATUS);
13309 if (val & OTP_STATUS_CMD_DONE)
13310 break;
13311 udelay(10);
13312 }
13313
13314 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
13315}
13316
13317/* Read the gphy configuration from the OTP region of the chip. The gphy
13318 * configuration is a 32-bit value that straddles the alignment boundary.
13319 * We do two 32-bit reads and then shift and merge the results.
13320 */
13321static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
13322{
13323 u32 bhalf_otp, thalf_otp;
13324
13325 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
13326
13327 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
13328 return 0;
13329
13330 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
13331
13332 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
13333 return 0;
13334
13335 thalf_otp = tr32(OTP_READ_DATA);
13336
13337 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
13338
13339 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
13340 return 0;
13341
13342 bhalf_otp = tr32(OTP_READ_DATA);
13343
13344 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
13345}
13346
Matt Carlsone256f8a2011-03-09 16:58:24 +000013347static void __devinit tg3_phy_init_link_config(struct tg3 *tp)
13348{
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +000013349 u32 adv = ADVERTISED_Autoneg;
Matt Carlsone256f8a2011-03-09 16:58:24 +000013350
13351 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
13352 adv |= ADVERTISED_1000baseT_Half |
13353 ADVERTISED_1000baseT_Full;
13354
13355 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
13356 adv |= ADVERTISED_100baseT_Half |
13357 ADVERTISED_100baseT_Full |
13358 ADVERTISED_10baseT_Half |
13359 ADVERTISED_10baseT_Full |
13360 ADVERTISED_TP;
13361 else
13362 adv |= ADVERTISED_FIBRE;
13363
13364 tp->link_config.advertising = adv;
Matt Carlsone7405222012-02-13 15:20:16 +000013365 tp->link_config.speed = SPEED_UNKNOWN;
13366 tp->link_config.duplex = DUPLEX_UNKNOWN;
Matt Carlsone256f8a2011-03-09 16:58:24 +000013367 tp->link_config.autoneg = AUTONEG_ENABLE;
Matt Carlsone7405222012-02-13 15:20:16 +000013368 tp->link_config.active_speed = SPEED_UNKNOWN;
13369 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Matt Carlson34655ad2012-02-22 12:35:18 +000013370
13371 tp->old_link = -1;
Matt Carlsone256f8a2011-03-09 16:58:24 +000013372}
13373
Michael Chan7d0c41e2005-04-21 17:06:20 -070013374static int __devinit tg3_phy_probe(struct tg3 *tp)
13375{
13376 u32 hw_phy_id_1, hw_phy_id_2;
13377 u32 hw_phy_id, hw_phy_id_masked;
13378 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013379
Matt Carlsone256f8a2011-03-09 16:58:24 +000013380 /* flow control autonegotiation is default behavior */
Joe Perches63c3a662011-04-26 08:12:10 +000013381 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsone256f8a2011-03-09 16:58:24 +000013382 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
13383
Joe Perches63c3a662011-04-26 08:12:10 +000013384 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013385 return tg3_phy_init(tp);
13386
Linus Torvalds1da177e2005-04-16 15:20:36 -070013387 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010013388 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013389 */
13390 err = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000013391 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000013392 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013393 } else {
13394 /* Now read the physical PHY_ID from the chip and verify
13395 * that it is sane. If it doesn't look good, we fall back
13396 * to either the hard-coded table based PHY_ID and failing
13397 * that the value found in the eeprom area.
13398 */
13399 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
13400 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
13401
13402 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
13403 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
13404 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
13405
Matt Carlson79eb6902010-02-17 15:17:03 +000013406 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013407 }
13408
Matt Carlson79eb6902010-02-17 15:17:03 +000013409 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013410 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000013411 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013412 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070013413 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013414 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013415 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000013416 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070013417 /* Do nothing, phy ID already set up in
13418 * tg3_get_eeprom_hw_cfg().
13419 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070013420 } else {
13421 struct subsys_tbl_ent *p;
13422
13423 /* No eeprom signature? Try the hardcoded
13424 * subsys device table.
13425 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013426 p = tg3_lookup_by_subsys(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013427 if (!p)
13428 return -ENODEV;
13429
13430 tp->phy_id = p->phy_id;
13431 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000013432 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013433 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013434 }
13435 }
13436
Matt Carlsona6b68da2010-12-06 08:28:52 +000013437 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Matt Carlson5baa5e92011-07-20 10:20:53 +000013438 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13439 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720 ||
13440 (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +000013441 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0) ||
13442 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
13443 tp->pci_chip_rev_id != CHIPREV_ID_57765_A0)))
Matt Carlson52b02d02010-10-14 10:37:41 +000013444 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
13445
Matt Carlsone256f8a2011-03-09 16:58:24 +000013446 tg3_phy_init_link_config(tp);
13447
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013448 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013449 !tg3_flag(tp, ENABLE_APE) &&
13450 !tg3_flag(tp, ENABLE_ASF)) {
Matt Carlsone2bf73e2011-12-08 14:40:15 +000013451 u32 bmsr, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013452
13453 tg3_readphy(tp, MII_BMSR, &bmsr);
13454 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
13455 (bmsr & BMSR_LSTATUS))
13456 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013457
Linus Torvalds1da177e2005-04-16 15:20:36 -070013458 err = tg3_phy_reset(tp);
13459 if (err)
13460 return err;
13461
Matt Carlson42b64a42011-05-19 12:12:49 +000013462 tg3_phy_set_wirespeed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013463
Matt Carlsone2bf73e2011-12-08 14:40:15 +000013464 if (!tg3_phy_copper_an_config_ok(tp, &dummy)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000013465 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
13466 tp->link_config.flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013467
13468 tg3_writephy(tp, MII_BMCR,
13469 BMCR_ANENABLE | BMCR_ANRESTART);
13470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013471 }
13472
13473skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000013474 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013475 err = tg3_init_5401phy_dsp(tp);
13476 if (err)
13477 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013478
Linus Torvalds1da177e2005-04-16 15:20:36 -070013479 err = tg3_init_5401phy_dsp(tp);
13480 }
13481
Linus Torvalds1da177e2005-04-16 15:20:36 -070013482 return err;
13483}
13484
Matt Carlson184b8902010-04-05 10:19:25 +000013485static void __devinit tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013486{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013487 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013488 unsigned int block_end, rosize, len;
Matt Carlson535a4902011-07-20 10:20:56 +000013489 u32 vpdlen;
Matt Carlson184b8902010-04-05 10:19:25 +000013490 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013491
Matt Carlson535a4902011-07-20 10:20:56 +000013492 vpd_data = (u8 *)tg3_vpd_readblock(tp, &vpdlen);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013493 if (!vpd_data)
13494 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013495
Matt Carlson535a4902011-07-20 10:20:56 +000013496 i = pci_vpd_find_tag(vpd_data, 0, vpdlen, PCI_VPD_LRDT_RO_DATA);
Matt Carlson4181b2c2010-02-26 14:04:45 +000013497 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013498 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013499
13500 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
13501 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
13502 i += PCI_VPD_LRDT_TAG_SIZE;
13503
Matt Carlson535a4902011-07-20 10:20:56 +000013504 if (block_end > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000013505 goto out_not_found;
13506
Matt Carlson184b8902010-04-05 10:19:25 +000013507 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13508 PCI_VPD_RO_KEYWORD_MFR_ID);
13509 if (j > 0) {
13510 len = pci_vpd_info_field_size(&vpd_data[j]);
13511
13512 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13513 if (j + len > block_end || len != 4 ||
13514 memcmp(&vpd_data[j], "1028", 4))
13515 goto partno;
13516
13517 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13518 PCI_VPD_RO_KEYWORD_VENDOR0);
13519 if (j < 0)
13520 goto partno;
13521
13522 len = pci_vpd_info_field_size(&vpd_data[j]);
13523
13524 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13525 if (j + len > block_end)
13526 goto partno;
13527
13528 memcpy(tp->fw_ver, &vpd_data[j], len);
Matt Carlson535a4902011-07-20 10:20:56 +000013529 strncat(tp->fw_ver, " bc ", vpdlen - len - 1);
Matt Carlson184b8902010-04-05 10:19:25 +000013530 }
13531
13532partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000013533 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13534 PCI_VPD_RO_KEYWORD_PARTNO);
13535 if (i < 0)
13536 goto out_not_found;
13537
13538 len = pci_vpd_info_field_size(&vpd_data[i]);
13539
13540 i += PCI_VPD_INFO_FLD_HDR_SIZE;
13541 if (len > TG3_BPN_SIZE ||
Matt Carlson535a4902011-07-20 10:20:56 +000013542 (len + i) > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000013543 goto out_not_found;
13544
13545 memcpy(tp->board_part_number, &vpd_data[i], len);
13546
Linus Torvalds1da177e2005-04-16 15:20:36 -070013547out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013548 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000013549 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013550 return;
13551
13552out_no_vpd:
Matt Carlson37a949c2010-09-30 10:34:33 +000013553 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
13554 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717)
13555 strcpy(tp->board_part_number, "BCM5717");
13556 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
13557 strcpy(tp->board_part_number, "BCM5718");
13558 else
13559 goto nomatch;
13560 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
13561 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
13562 strcpy(tp->board_part_number, "BCM57780");
13563 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
13564 strcpy(tp->board_part_number, "BCM57760");
13565 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
13566 strcpy(tp->board_part_number, "BCM57790");
13567 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
13568 strcpy(tp->board_part_number, "BCM57788");
13569 else
13570 goto nomatch;
13571 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
13572 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
13573 strcpy(tp->board_part_number, "BCM57761");
13574 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
13575 strcpy(tp->board_part_number, "BCM57765");
13576 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
13577 strcpy(tp->board_part_number, "BCM57781");
13578 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
13579 strcpy(tp->board_part_number, "BCM57785");
13580 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
13581 strcpy(tp->board_part_number, "BCM57791");
13582 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13583 strcpy(tp->board_part_number, "BCM57795");
13584 else
13585 goto nomatch;
Matt Carlson55086ad2011-12-14 11:09:59 +000013586 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57766) {
13587 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762)
13588 strcpy(tp->board_part_number, "BCM57762");
13589 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766)
13590 strcpy(tp->board_part_number, "BCM57766");
13591 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782)
13592 strcpy(tp->board_part_number, "BCM57782");
13593 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
13594 strcpy(tp->board_part_number, "BCM57786");
13595 else
13596 goto nomatch;
Matt Carlson37a949c2010-09-30 10:34:33 +000013597 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070013598 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000013599 } else {
13600nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070013601 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000013602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013603}
13604
Matt Carlson9c8a6202007-10-21 16:16:08 -070013605static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
13606{
13607 u32 val;
13608
Matt Carlsone4f34112009-02-25 14:25:00 +000013609 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013610 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013611 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013612 val != 0)
13613 return 0;
13614
13615 return 1;
13616}
13617
Matt Carlsonacd9c112009-02-25 14:26:33 +000013618static void __devinit tg3_read_bc_ver(struct tg3 *tp)
13619{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013620 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000013621 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013622 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013623
13624 if (tg3_nvram_read(tp, 0xc, &offset) ||
13625 tg3_nvram_read(tp, 0x4, &start))
13626 return;
13627
13628 offset = tg3_nvram_logical_addr(tp, offset);
13629
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013630 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013631 return;
13632
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013633 if ((val & 0xfc000000) == 0x0c000000) {
13634 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013635 return;
13636
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013637 if (val == 0)
13638 newver = true;
13639 }
13640
Matt Carlson75f99362010-04-05 10:19:24 +000013641 dst_off = strlen(tp->fw_ver);
13642
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013643 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000013644 if (TG3_VER_SIZE - dst_off < 16 ||
13645 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013646 return;
13647
13648 offset = offset + ver_offset - start;
13649 for (i = 0; i < 16; i += 4) {
13650 __be32 v;
13651 if (tg3_nvram_read_be32(tp, offset + i, &v))
13652 return;
13653
Matt Carlson75f99362010-04-05 10:19:24 +000013654 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013655 }
13656 } else {
13657 u32 major, minor;
13658
13659 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
13660 return;
13661
13662 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
13663 TG3_NVM_BCVER_MAJSFT;
13664 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000013665 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
13666 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013667 }
13668}
13669
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013670static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
13671{
13672 u32 val, major, minor;
13673
13674 /* Use native endian representation */
13675 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
13676 return;
13677
13678 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
13679 TG3_NVM_HWSB_CFG1_MAJSFT;
13680 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
13681 TG3_NVM_HWSB_CFG1_MINSFT;
13682
13683 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
13684}
13685
Matt Carlsondfe00d72008-11-21 17:19:41 -080013686static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
13687{
13688 u32 offset, major, minor, build;
13689
Matt Carlson75f99362010-04-05 10:19:24 +000013690 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013691
13692 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
13693 return;
13694
13695 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
13696 case TG3_EEPROM_SB_REVISION_0:
13697 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
13698 break;
13699 case TG3_EEPROM_SB_REVISION_2:
13700 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
13701 break;
13702 case TG3_EEPROM_SB_REVISION_3:
13703 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
13704 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000013705 case TG3_EEPROM_SB_REVISION_4:
13706 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
13707 break;
13708 case TG3_EEPROM_SB_REVISION_5:
13709 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
13710 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000013711 case TG3_EEPROM_SB_REVISION_6:
13712 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
13713 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013714 default:
13715 return;
13716 }
13717
Matt Carlsone4f34112009-02-25 14:25:00 +000013718 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080013719 return;
13720
13721 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
13722 TG3_EEPROM_SB_EDH_BLD_SHFT;
13723 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
13724 TG3_EEPROM_SB_EDH_MAJ_SHFT;
13725 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
13726
13727 if (minor > 99 || build > 26)
13728 return;
13729
Matt Carlson75f99362010-04-05 10:19:24 +000013730 offset = strlen(tp->fw_ver);
13731 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
13732 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013733
13734 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000013735 offset = strlen(tp->fw_ver);
13736 if (offset < TG3_VER_SIZE - 1)
13737 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013738 }
13739}
13740
Matt Carlsonacd9c112009-02-25 14:26:33 +000013741static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080013742{
13743 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013744 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070013745
13746 for (offset = TG3_NVM_DIR_START;
13747 offset < TG3_NVM_DIR_END;
13748 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000013749 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013750 return;
13751
13752 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
13753 break;
13754 }
13755
13756 if (offset == TG3_NVM_DIR_END)
13757 return;
13758
Joe Perches63c3a662011-04-26 08:12:10 +000013759 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013760 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000013761 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013762 return;
13763
Matt Carlsone4f34112009-02-25 14:25:00 +000013764 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013765 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013766 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013767 return;
13768
13769 offset += val - start;
13770
Matt Carlsonacd9c112009-02-25 14:26:33 +000013771 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013772
Matt Carlsonacd9c112009-02-25 14:26:33 +000013773 tp->fw_ver[vlen++] = ',';
13774 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070013775
13776 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000013777 __be32 v;
13778 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013779 return;
13780
Al Virob9fc7dc2007-12-17 22:59:57 -080013781 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013782
Matt Carlsonacd9c112009-02-25 14:26:33 +000013783 if (vlen > TG3_VER_SIZE - sizeof(v)) {
13784 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013785 break;
13786 }
13787
Matt Carlsonacd9c112009-02-25 14:26:33 +000013788 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
13789 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013790 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000013791}
13792
Matt Carlson7fd76442009-02-25 14:27:20 +000013793static void __devinit tg3_read_dash_ver(struct tg3 *tp)
13794{
13795 int vlen;
13796 u32 apedata;
Matt Carlsonecc79642010-08-02 11:26:01 +000013797 char *fwtype;
Matt Carlson7fd76442009-02-25 14:27:20 +000013798
Joe Perches63c3a662011-04-26 08:12:10 +000013799 if (!tg3_flag(tp, ENABLE_APE) || !tg3_flag(tp, ENABLE_ASF))
Matt Carlson7fd76442009-02-25 14:27:20 +000013800 return;
13801
13802 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
13803 if (apedata != APE_SEG_SIG_MAGIC)
13804 return;
13805
13806 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
13807 if (!(apedata & APE_FW_STATUS_READY))
13808 return;
13809
13810 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
13811
Matt Carlsondc6d0742010-09-15 08:59:55 +000013812 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI) {
Joe Perches63c3a662011-04-26 08:12:10 +000013813 tg3_flag_set(tp, APE_HAS_NCSI);
Matt Carlsonecc79642010-08-02 11:26:01 +000013814 fwtype = "NCSI";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013815 } else {
Matt Carlsonecc79642010-08-02 11:26:01 +000013816 fwtype = "DASH";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013817 }
Matt Carlsonecc79642010-08-02 11:26:01 +000013818
Matt Carlson7fd76442009-02-25 14:27:20 +000013819 vlen = strlen(tp->fw_ver);
13820
Matt Carlsonecc79642010-08-02 11:26:01 +000013821 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
13822 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000013823 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
13824 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
13825 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
13826 (apedata & APE_FW_VERSION_BLDMSK));
13827}
13828
Matt Carlsonacd9c112009-02-25 14:26:33 +000013829static void __devinit tg3_read_fw_ver(struct tg3 *tp)
13830{
13831 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000013832 bool vpd_vers = false;
13833
13834 if (tp->fw_ver[0] != 0)
13835 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013836
Joe Perches63c3a662011-04-26 08:12:10 +000013837 if (tg3_flag(tp, NO_NVRAM)) {
Matt Carlson75f99362010-04-05 10:19:24 +000013838 strcat(tp->fw_ver, "sb");
Matt Carlsondf259d82009-04-20 06:57:14 +000013839 return;
13840 }
13841
Matt Carlsonacd9c112009-02-25 14:26:33 +000013842 if (tg3_nvram_read(tp, 0, &val))
13843 return;
13844
13845 if (val == TG3_EEPROM_MAGIC)
13846 tg3_read_bc_ver(tp);
13847 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
13848 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013849 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
13850 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013851 else
13852 return;
13853
Matt Carlsonc9cab242011-07-13 09:27:27 +000013854 if (vpd_vers)
Matt Carlson75f99362010-04-05 10:19:24 +000013855 goto done;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013856
Matt Carlsonc9cab242011-07-13 09:27:27 +000013857 if (tg3_flag(tp, ENABLE_APE)) {
13858 if (tg3_flag(tp, ENABLE_ASF))
13859 tg3_read_dash_ver(tp);
13860 } else if (tg3_flag(tp, ENABLE_ASF)) {
13861 tg3_read_mgmtfw_ver(tp);
13862 }
Matt Carlson9c8a6202007-10-21 16:16:08 -070013863
Matt Carlson75f99362010-04-05 10:19:24 +000013864done:
Matt Carlson9c8a6202007-10-21 16:16:08 -070013865 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080013866}
13867
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013868static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
13869{
Joe Perches63c3a662011-04-26 08:12:10 +000013870 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013871 return TG3_RX_RET_MAX_SIZE_5717;
Joe Perches63c3a662011-04-26 08:12:10 +000013872 else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013873 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013874 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000013875 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013876}
13877
Matt Carlson41434702011-03-09 16:58:22 +000013878static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080013879 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
13880 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
13881 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
13882 { },
13883};
13884
Matt Carlson16c7fa72012-02-13 10:20:10 +000013885static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
13886{
13887 struct pci_dev *peer;
13888 unsigned int func, devnr = tp->pdev->devfn & ~7;
13889
13890 for (func = 0; func < 8; func++) {
13891 peer = pci_get_slot(tp->pdev->bus, devnr | func);
13892 if (peer && peer != tp->pdev)
13893 break;
13894 pci_dev_put(peer);
13895 }
13896 /* 5704 can be configured in single-port mode, set peer to
13897 * tp->pdev in that case.
13898 */
13899 if (!peer) {
13900 peer = tp->pdev;
13901 return peer;
13902 }
13903
13904 /*
13905 * We don't need to keep the refcount elevated; there's no way
13906 * to remove one half of this device without removing the other
13907 */
13908 pci_dev_put(peer);
13909
13910 return peer;
13911}
13912
Matt Carlson42b123b2012-02-13 15:20:13 +000013913static void __devinit tg3_detect_asic_rev(struct tg3 *tp, u32 misc_ctrl_reg)
13914{
13915 tp->pci_chip_rev_id = misc_ctrl_reg >> MISC_HOST_CTRL_CHIPREV_SHIFT;
13916 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
13917 u32 reg;
13918
13919 /* All devices that use the alternate
13920 * ASIC REV location have a CPMU.
13921 */
13922 tg3_flag_set(tp, CPMU_PRESENT);
13923
13924 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
13925 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
13926 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
13927 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720)
13928 reg = TG3PCI_GEN2_PRODID_ASICREV;
13929 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
13930 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
13931 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
13932 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
13933 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
13934 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
13935 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762 ||
13936 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766 ||
13937 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782 ||
13938 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
13939 reg = TG3PCI_GEN15_PRODID_ASICREV;
13940 else
13941 reg = TG3PCI_PRODID_ASICREV;
13942
13943 pci_read_config_dword(tp->pdev, reg, &tp->pci_chip_rev_id);
13944 }
13945
13946 /* Wrong chip ID in 5752 A0. This code can be removed later
13947 * as A0 is not in production.
13948 */
13949 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
13950 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
13951
13952 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13953 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13954 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
13955 tg3_flag_set(tp, 5717_PLUS);
13956
13957 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 ||
13958 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57766)
13959 tg3_flag_set(tp, 57765_CLASS);
13960
13961 if (tg3_flag(tp, 57765_CLASS) || tg3_flag(tp, 5717_PLUS))
13962 tg3_flag_set(tp, 57765_PLUS);
13963
13964 /* Intentionally exclude ASIC_REV_5906 */
13965 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
13966 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
13967 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
13968 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
13969 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
13970 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
13971 tg3_flag(tp, 57765_PLUS))
13972 tg3_flag_set(tp, 5755_PLUS);
13973
13974 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
13975 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)
13976 tg3_flag_set(tp, 5780_CLASS);
13977
13978 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13979 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
13980 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
13981 tg3_flag(tp, 5755_PLUS) ||
13982 tg3_flag(tp, 5780_CLASS))
13983 tg3_flag_set(tp, 5750_PLUS);
13984
13985 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
13986 tg3_flag(tp, 5750_PLUS))
13987 tg3_flag_set(tp, 5705_PLUS);
13988}
13989
Linus Torvalds1da177e2005-04-16 15:20:36 -070013990static int __devinit tg3_get_invariants(struct tg3 *tp)
13991{
Linus Torvalds1da177e2005-04-16 15:20:36 -070013992 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013993 u32 pci_state_reg, grc_misc_cfg;
13994 u32 val;
13995 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013996 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013997
Linus Torvalds1da177e2005-04-16 15:20:36 -070013998 /* Force memory write invalidate off. If we leave it on,
13999 * then on 5700_BX chips we have to enable a workaround.
14000 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
14001 * to match the cacheline size. The Broadcom driver have this
14002 * workaround but turns MWI off all the times so never uses
14003 * it. This seems to suggest that the workaround is insufficient.
14004 */
14005 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
14006 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
14007 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
14008
Matt Carlson16821282011-07-13 09:27:28 +000014009 /* Important! -- Make sure register accesses are byteswapped
14010 * correctly. Also, for those chips that require it, make
14011 * sure that indirect register accesses are enabled before
14012 * the first operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014013 */
14014 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
14015 &misc_ctrl_reg);
Matt Carlson16821282011-07-13 09:27:28 +000014016 tp->misc_host_ctrl |= (misc_ctrl_reg &
14017 MISC_HOST_CTRL_CHIPREV);
14018 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
14019 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014020
Matt Carlson42b123b2012-02-13 15:20:13 +000014021 tg3_detect_asic_rev(tp, misc_ctrl_reg);
Michael Chanff645be2005-04-21 17:09:53 -070014022
Michael Chan68929142005-08-09 20:17:14 -070014023 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
14024 * we need to disable memory and use config. cycles
14025 * only to access all registers. The 5702/03 chips
14026 * can mistakenly decode the special cycles from the
14027 * ICH chipsets as memory write cycles, causing corruption
14028 * of register and memory space. Only certain ICH bridges
14029 * will drive special cycles with non-zero data during the
14030 * address phase which can fall within the 5703's address
14031 * range. This is not an ICH bug as the PCI spec allows
14032 * non-zero address during special cycles. However, only
14033 * these ICH bridges are known to drive non-zero addresses
14034 * during special cycles.
14035 *
14036 * Since special cycles do not cross PCI bridges, we only
14037 * enable this workaround if the 5703 is on the secondary
14038 * bus of these ICH bridges.
14039 */
14040 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
14041 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
14042 static struct tg3_dev_id {
14043 u32 vendor;
14044 u32 device;
14045 u32 rev;
14046 } ich_chipsets[] = {
14047 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
14048 PCI_ANY_ID },
14049 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
14050 PCI_ANY_ID },
14051 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
14052 0xa },
14053 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
14054 PCI_ANY_ID },
14055 { },
14056 };
14057 struct tg3_dev_id *pci_id = &ich_chipsets[0];
14058 struct pci_dev *bridge = NULL;
14059
14060 while (pci_id->vendor != 0) {
14061 bridge = pci_get_device(pci_id->vendor, pci_id->device,
14062 bridge);
14063 if (!bridge) {
14064 pci_id++;
14065 continue;
14066 }
14067 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070014068 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070014069 continue;
14070 }
14071 if (bridge->subordinate &&
14072 (bridge->subordinate->number ==
14073 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014074 tg3_flag_set(tp, ICH_WORKAROUND);
Michael Chan68929142005-08-09 20:17:14 -070014075 pci_dev_put(bridge);
14076 break;
14077 }
14078 }
14079 }
14080
Matt Carlson6ff6f812011-05-19 12:12:54 +000014081 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Matt Carlson41588ba2008-04-19 18:12:33 -070014082 static struct tg3_dev_id {
14083 u32 vendor;
14084 u32 device;
14085 } bridge_chipsets[] = {
14086 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
14087 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
14088 { },
14089 };
14090 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
14091 struct pci_dev *bridge = NULL;
14092
14093 while (pci_id->vendor != 0) {
14094 bridge = pci_get_device(pci_id->vendor,
14095 pci_id->device,
14096 bridge);
14097 if (!bridge) {
14098 pci_id++;
14099 continue;
14100 }
14101 if (bridge->subordinate &&
14102 (bridge->subordinate->number <=
14103 tp->pdev->bus->number) &&
14104 (bridge->subordinate->subordinate >=
14105 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014106 tg3_flag_set(tp, 5701_DMA_BUG);
Matt Carlson41588ba2008-04-19 18:12:33 -070014107 pci_dev_put(bridge);
14108 break;
14109 }
14110 }
14111 }
14112
Michael Chan4a29cc22006-03-19 13:21:12 -080014113 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
14114 * DMA addresses > 40-bit. This bridge may have other additional
14115 * 57xx devices behind it in some 4-port NIC designs for example.
14116 * Any tg3 device found behind the bridge will also need the 40-bit
14117 * DMA workaround.
14118 */
Matt Carlson42b123b2012-02-13 15:20:13 +000014119 if (tg3_flag(tp, 5780_CLASS)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014120 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4cf78e42005-07-25 12:29:19 -070014121 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a588792010-04-05 10:19:28 +000014122 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080014123 struct pci_dev *bridge = NULL;
14124
14125 do {
14126 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
14127 PCI_DEVICE_ID_SERVERWORKS_EPB,
14128 bridge);
14129 if (bridge && bridge->subordinate &&
14130 (bridge->subordinate->number <=
14131 tp->pdev->bus->number) &&
14132 (bridge->subordinate->subordinate >=
14133 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014134 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4a29cc22006-03-19 13:21:12 -080014135 pci_dev_put(bridge);
14136 break;
14137 }
14138 } while (bridge);
14139 }
Michael Chan4cf78e42005-07-25 12:29:19 -070014140
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014141 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Matt Carlson3a1e19d2011-07-13 09:27:32 +000014142 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)
Michael Chan7544b092007-05-05 13:08:32 -070014143 tp->pdev_peer = tg3_find_peer(tp);
14144
Matt Carlson507399f2009-11-13 13:03:37 +000014145 /* Determine TSO capabilities */
Matt Carlsona0512942011-07-27 14:20:54 +000014146 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0)
Matt Carlson4d163b72011-01-25 15:58:48 +000014147 ; /* Do nothing. HW bug. */
Joe Perches63c3a662011-04-26 08:12:10 +000014148 else if (tg3_flag(tp, 57765_PLUS))
14149 tg3_flag_set(tp, HW_TSO_3);
14150 else if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000014151 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000014152 tg3_flag_set(tp, HW_TSO_2);
14153 else if (tg3_flag(tp, 5750_PLUS)) {
14154 tg3_flag_set(tp, HW_TSO_1);
14155 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000014156 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
14157 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
Joe Perches63c3a662011-04-26 08:12:10 +000014158 tg3_flag_clear(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000014159 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14160 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
14161 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000014162 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000014163 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
14164 tp->fw_needed = FIRMWARE_TG3TSO5;
14165 else
14166 tp->fw_needed = FIRMWARE_TG3TSO;
14167 }
14168
Matt Carlsondabc5c62011-05-19 12:12:52 +000014169 /* Selectively allow TSO based on operating conditions */
Matt Carlson6ff6f812011-05-19 12:12:54 +000014170 if (tg3_flag(tp, HW_TSO_1) ||
14171 tg3_flag(tp, HW_TSO_2) ||
14172 tg3_flag(tp, HW_TSO_3) ||
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000014173 tp->fw_needed) {
14174 /* For firmware TSO, assume ASF is disabled.
14175 * We'll disable TSO later if we discover ASF
14176 * is enabled in tg3_get_eeprom_hw_cfg().
14177 */
Matt Carlsondabc5c62011-05-19 12:12:52 +000014178 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000014179 } else {
Matt Carlsondabc5c62011-05-19 12:12:52 +000014180 tg3_flag_clear(tp, TSO_CAPABLE);
14181 tg3_flag_clear(tp, TSO_BUG);
14182 tp->fw_needed = NULL;
14183 }
14184
14185 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
14186 tp->fw_needed = FIRMWARE_TG3;
14187
Matt Carlson507399f2009-11-13 13:03:37 +000014188 tp->irq_max = 1;
14189
Joe Perches63c3a662011-04-26 08:12:10 +000014190 if (tg3_flag(tp, 5750_PLUS)) {
14191 tg3_flag_set(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070014192 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
14193 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
14194 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
14195 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
14196 tp->pdev_peer == tp->pdev))
Joe Perches63c3a662011-04-26 08:12:10 +000014197 tg3_flag_clear(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070014198
Joe Perches63c3a662011-04-26 08:12:10 +000014199 if (tg3_flag(tp, 5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070014200 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000014201 tg3_flag_set(tp, 1SHOT_MSI);
Michael Chan52c0fd82006-06-29 20:15:54 -070014202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014203
Joe Perches63c3a662011-04-26 08:12:10 +000014204 if (tg3_flag(tp, 57765_PLUS)) {
14205 tg3_flag_set(tp, SUPPORT_MSIX);
Matt Carlson507399f2009-11-13 13:03:37 +000014206 tp->irq_max = TG3_IRQ_MAX_VECS;
Matt Carlson90415472011-12-16 13:33:23 +000014207 tg3_rss_init_dflt_indir_tbl(tp);
Matt Carlson507399f2009-11-13 13:03:37 +000014208 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014209 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000014210
Matt Carlson2ffcc982011-05-19 12:12:44 +000014211 if (tg3_flag(tp, 5755_PLUS))
Joe Perches63c3a662011-04-26 08:12:10 +000014212 tg3_flag_set(tp, SHORT_DMA_BUG);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014213
Matt Carlsone31aa982011-07-27 14:20:53 +000014214 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona4cb4282011-12-14 11:09:58 +000014215 tp->dma_limit = TG3_TX_BD_DMA_MAX_4K;
Matt Carlsone31aa982011-07-27 14:20:53 +000014216
Matt Carlsonfa6b2aa2011-11-21 15:01:19 +000014217 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14218 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
14219 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Joe Perches63c3a662011-04-26 08:12:10 +000014220 tg3_flag_set(tp, LRG_PROD_RING_CAP);
Matt Carlsonde9f5232011-04-05 14:22:43 +000014221
Joe Perches63c3a662011-04-26 08:12:10 +000014222 if (tg3_flag(tp, 57765_PLUS) &&
Matt Carlsona0512942011-07-27 14:20:54 +000014223 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0)
Joe Perches63c3a662011-04-26 08:12:10 +000014224 tg3_flag_set(tp, USE_JUMBO_BDFLAG);
Matt Carlsonb703df62009-12-03 08:36:21 +000014225
Joe Perches63c3a662011-04-26 08:12:10 +000014226 if (!tg3_flag(tp, 5705_PLUS) ||
14227 tg3_flag(tp, 5780_CLASS) ||
14228 tg3_flag(tp, USE_JUMBO_BDFLAG))
14229 tg3_flag_set(tp, JUMBO_CAPABLE);
Michael Chan0f893dc2005-07-25 12:30:38 -070014230
Matt Carlson52f44902008-11-21 17:17:04 -080014231 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14232 &pci_state_reg);
14233
Jon Mason708ebb32011-06-27 12:56:50 +000014234 if (pci_is_pcie(tp->pdev)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014235 u16 lnkctl;
14236
Joe Perches63c3a662011-04-26 08:12:10 +000014237 tg3_flag_set(tp, PCI_EXPRESS);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080014238
Matt Carlson2c55a3d2011-11-28 09:41:04 +000014239 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0) {
14240 int readrq = pcie_get_readrq(tp->pdev);
14241 if (readrq > 2048)
14242 pcie_set_readrq(tp->pdev, 2048);
14243 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -080014244
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014245 pci_read_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +000014246 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014247 &lnkctl);
14248 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
Matt Carlson7196cd62011-05-19 16:02:44 +000014249 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
14250 ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000014251 tg3_flag_clear(tp, HW_TSO_2);
Matt Carlsondabc5c62011-05-19 12:12:52 +000014252 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson7196cd62011-05-19 16:02:44 +000014253 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014254 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014255 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000014256 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
14257 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Joe Perches63c3a662011-04-26 08:12:10 +000014258 tg3_flag_set(tp, CLKREQ_BUG);
Matt Carlson614b0592010-01-20 16:58:02 +000014259 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000014260 tg3_flag_set(tp, L1PLLPD_EN);
Michael Chanc7835a72006-11-15 21:14:42 -080014261 }
Matt Carlson52f44902008-11-21 17:17:04 -080014262 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Jon Mason708ebb32011-06-27 12:56:50 +000014263 /* BCM5785 devices are effectively PCIe devices, and should
14264 * follow PCIe codepaths, but do not have a PCIe capabilities
14265 * section.
Matt Carlson93a700a2011-08-31 11:44:54 +000014266 */
Joe Perches63c3a662011-04-26 08:12:10 +000014267 tg3_flag_set(tp, PCI_EXPRESS);
14268 } else if (!tg3_flag(tp, 5705_PLUS) ||
14269 tg3_flag(tp, 5780_CLASS)) {
Matt Carlson52f44902008-11-21 17:17:04 -080014270 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
14271 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000014272 dev_err(&tp->pdev->dev,
14273 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080014274 return -EIO;
14275 }
14276
14277 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
Joe Perches63c3a662011-04-26 08:12:10 +000014278 tg3_flag_set(tp, PCIX_MODE);
Matt Carlson52f44902008-11-21 17:17:04 -080014279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014280
Michael Chan399de502005-10-03 14:02:39 -070014281 /* If we have an AMD 762 or VIA K8T800 chipset, write
14282 * reordering to the mailbox registers done by the host
14283 * controller can cause major troubles. We read back from
14284 * every mailbox register write to force the writes to be
14285 * posted to the chip in order.
14286 */
Matt Carlson41434702011-03-09 16:58:22 +000014287 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Joe Perches63c3a662011-04-26 08:12:10 +000014288 !tg3_flag(tp, PCI_EXPRESS))
14289 tg3_flag_set(tp, MBOX_WRITE_REORDER);
Michael Chan399de502005-10-03 14:02:39 -070014290
Matt Carlson69fc4052008-12-21 20:19:57 -080014291 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
14292 &tp->pci_cacheline_sz);
14293 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
14294 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014295 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14296 tp->pci_lat_timer < 64) {
14297 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080014298 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
14299 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014300 }
14301
Matt Carlson16821282011-07-13 09:27:28 +000014302 /* Important! -- It is critical that the PCI-X hw workaround
14303 * situation is decided before the first MMIO register access.
14304 */
Matt Carlson52f44902008-11-21 17:17:04 -080014305 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
14306 /* 5700 BX chips need to have their TX producer index
14307 * mailboxes written twice to workaround a bug.
14308 */
Joe Perches63c3a662011-04-26 08:12:10 +000014309 tg3_flag_set(tp, TXD_MBOX_HWBUG);
Matt Carlson9974a352007-10-07 23:27:28 -070014310
Matt Carlson52f44902008-11-21 17:17:04 -080014311 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014312 *
14313 * The workaround is to use indirect register accesses
14314 * for all chip writes not to mailbox registers.
14315 */
Joe Perches63c3a662011-04-26 08:12:10 +000014316 if (tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014317 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014318
Joe Perches63c3a662011-04-26 08:12:10 +000014319 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014320
14321 /* The chip can have it's power management PCI config
14322 * space registers clobbered due to this bug.
14323 * So explicitly force the chip into D0 here.
14324 */
Matt Carlson9974a352007-10-07 23:27:28 -070014325 pci_read_config_dword(tp->pdev,
14326 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014327 &pm_reg);
14328 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
14329 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070014330 pci_write_config_dword(tp->pdev,
14331 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014332 pm_reg);
14333
14334 /* Also, force SERR#/PERR# in PCI command. */
14335 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
14336 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
14337 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
14338 }
14339 }
14340
Linus Torvalds1da177e2005-04-16 15:20:36 -070014341 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000014342 tg3_flag_set(tp, PCI_HIGH_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014343 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000014344 tg3_flag_set(tp, PCI_32BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014345
14346 /* Chip-specific fixup from Broadcom driver */
14347 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
14348 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
14349 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
14350 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
14351 }
14352
Michael Chan1ee582d2005-08-09 20:16:46 -070014353 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070014354 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070014355 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070014356 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070014357 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070014358 tp->write32_tx_mbox = tg3_write32;
14359 tp->write32_rx_mbox = tg3_write32;
14360
14361 /* Various workaround register access methods */
Joe Perches63c3a662011-04-26 08:12:10 +000014362 if (tg3_flag(tp, PCIX_TARGET_HWBUG))
Michael Chan1ee582d2005-08-09 20:16:46 -070014363 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070014364 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014365 (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson98efd8a2007-05-05 12:47:25 -070014366 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
14367 /*
14368 * Back to back register writes can cause problems on these
14369 * chips, the workaround is to read back all reg writes
14370 * except those to mailbox regs.
14371 *
14372 * See tg3_write_indirect_reg32().
14373 */
Michael Chan1ee582d2005-08-09 20:16:46 -070014374 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070014375 }
14376
Joe Perches63c3a662011-04-26 08:12:10 +000014377 if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
Michael Chan1ee582d2005-08-09 20:16:46 -070014378 tp->write32_tx_mbox = tg3_write32_tx_mbox;
Joe Perches63c3a662011-04-26 08:12:10 +000014379 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Michael Chan1ee582d2005-08-09 20:16:46 -070014380 tp->write32_rx_mbox = tg3_write_flush_reg32;
14381 }
Michael Chan20094932005-08-09 20:16:32 -070014382
Joe Perches63c3a662011-04-26 08:12:10 +000014383 if (tg3_flag(tp, ICH_WORKAROUND)) {
Michael Chan68929142005-08-09 20:17:14 -070014384 tp->read32 = tg3_read_indirect_reg32;
14385 tp->write32 = tg3_write_indirect_reg32;
14386 tp->read32_mbox = tg3_read_indirect_mbox;
14387 tp->write32_mbox = tg3_write_indirect_mbox;
14388 tp->write32_tx_mbox = tg3_write_indirect_mbox;
14389 tp->write32_rx_mbox = tg3_write_indirect_mbox;
14390
14391 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070014392 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070014393
14394 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
14395 pci_cmd &= ~PCI_COMMAND_MEMORY;
14396 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
14397 }
Michael Chanb5d37722006-09-27 16:06:21 -070014398 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
14399 tp->read32_mbox = tg3_read32_mbox_5906;
14400 tp->write32_mbox = tg3_write32_mbox_5906;
14401 tp->write32_tx_mbox = tg3_write32_mbox_5906;
14402 tp->write32_rx_mbox = tg3_write32_mbox_5906;
14403 }
Michael Chan68929142005-08-09 20:17:14 -070014404
Michael Chanbbadf502006-04-06 21:46:34 -070014405 if (tp->write32 == tg3_write_indirect_reg32 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014406 (tg3_flag(tp, PCIX_MODE) &&
Michael Chanbbadf502006-04-06 21:46:34 -070014407 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070014408 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Joe Perches63c3a662011-04-26 08:12:10 +000014409 tg3_flag_set(tp, SRAM_USE_CONFIG);
Michael Chanbbadf502006-04-06 21:46:34 -070014410
Matt Carlson16821282011-07-13 09:27:28 +000014411 /* The memory arbiter has to be enabled in order for SRAM accesses
14412 * to succeed. Normally on powerup the tg3 chip firmware will make
14413 * sure it is enabled, but other entities such as system netboot
14414 * code might disable it.
14415 */
14416 val = tr32(MEMARB_MODE);
14417 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
14418
Matt Carlson9dc5e342011-11-04 09:15:02 +000014419 tp->pci_fn = PCI_FUNC(tp->pdev->devfn) & 3;
14420 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
14421 tg3_flag(tp, 5780_CLASS)) {
14422 if (tg3_flag(tp, PCIX_MODE)) {
14423 pci_read_config_dword(tp->pdev,
14424 tp->pcix_cap + PCI_X_STATUS,
14425 &val);
14426 tp->pci_fn = val & 0x7;
14427 }
14428 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
14429 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
14430 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) ==
14431 NIC_SRAM_CPMUSTAT_SIG) {
14432 tp->pci_fn = val & TG3_CPMU_STATUS_FMSK_5717;
14433 tp->pci_fn = tp->pci_fn ? 1 : 0;
14434 }
14435 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
14436 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
14437 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
14438 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) ==
14439 NIC_SRAM_CPMUSTAT_SIG) {
14440 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5719) >>
14441 TG3_CPMU_STATUS_FSHFT_5719;
14442 }
Matt Carlson69f11c92011-07-13 09:27:30 +000014443 }
14444
Michael Chan7d0c41e2005-04-21 17:06:20 -070014445 /* Get eeprom hw config before calling tg3_set_power_state().
Joe Perches63c3a662011-04-26 08:12:10 +000014446 * In particular, the TG3_FLAG_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070014447 * determined before calling tg3_set_power_state() so that
14448 * we know whether or not to switch out of Vaux power.
14449 * When the flag is set, it means that GPIO1 is used for eeprom
14450 * write protect and also implies that it is a LOM where GPIOs
14451 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040014452 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070014453 tg3_get_eeprom_hw_cfg(tp);
14454
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000014455 if (tp->fw_needed && tg3_flag(tp, ENABLE_ASF)) {
14456 tg3_flag_clear(tp, TSO_CAPABLE);
14457 tg3_flag_clear(tp, TSO_BUG);
14458 tp->fw_needed = NULL;
14459 }
14460
Joe Perches63c3a662011-04-26 08:12:10 +000014461 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070014462 /* Allow reads and writes to the
14463 * APE register and memory space.
14464 */
14465 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +000014466 PCISTATE_ALLOW_APE_SHMEM_WR |
14467 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070014468 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
14469 pci_state_reg);
Matt Carlsonc9cab242011-07-13 09:27:27 +000014470
14471 tg3_ape_lock_init(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070014472 }
14473
Matt Carlson16821282011-07-13 09:27:28 +000014474 /* Set up tp->grc_local_ctrl before calling
14475 * tg3_pwrsrc_switch_to_vmain(). GPIO1 driven high
14476 * will bring 5700's external PHY out of reset.
Michael Chan314fba32005-04-21 17:07:04 -070014477 * It is also used as eeprom write protect on LOMs.
14478 */
14479 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
Matt Carlson6ff6f812011-05-19 12:12:54 +000014480 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014481 tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan314fba32005-04-21 17:07:04 -070014482 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
14483 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070014484 /* Unused GPIO3 must be driven as output on 5752 because there
14485 * are no pull-up resistors on unused GPIO pins.
14486 */
14487 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
14488 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070014489
Matt Carlson321d32a2008-11-21 17:22:19 -080014490 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsoncb4ed1f2010-01-20 16:58:09 +000014491 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000014492 tg3_flag(tp, 57765_CLASS))
Michael Chanaf36e6b2006-03-23 01:28:06 -080014493 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
14494
Matt Carlson8d519ab2009-04-20 06:58:01 +000014495 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
14496 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070014497 /* Turn off the debug UART. */
14498 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014499 if (tg3_flag(tp, IS_NIC))
Matt Carlson5f0c4a32008-06-09 15:41:12 -070014500 /* Keep VMain power. */
14501 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
14502 GRC_LCLCTRL_GPIO_OUTPUT0;
14503 }
14504
Matt Carlson16821282011-07-13 09:27:28 +000014505 /* Switch out of Vaux if it is a NIC */
14506 tg3_pwrsrc_switch_to_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014507
Linus Torvalds1da177e2005-04-16 15:20:36 -070014508 /* Derive initial jumbo mode from MTU assigned in
14509 * ether_setup() via the alloc_etherdev() call
14510 */
Joe Perches63c3a662011-04-26 08:12:10 +000014511 if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
14512 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014513
14514 /* Determine WakeOnLan speed to use. */
14515 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14516 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
14517 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
14518 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
Joe Perches63c3a662011-04-26 08:12:10 +000014519 tg3_flag_clear(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014520 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000014521 tg3_flag_set(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014522 }
14523
Matt Carlson7f97a4b2009-08-25 10:10:03 +000014524 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014525 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000014526
Linus Torvalds1da177e2005-04-16 15:20:36 -070014527 /* A few boards don't want Ethernet@WireSpeed phy feature */
Matt Carlson6ff6f812011-05-19 12:12:54 +000014528 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14529 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070014530 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070014531 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014532 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
14533 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
14534 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014535
14536 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
14537 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014538 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014539 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014540 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014541
Joe Perches63c3a662011-04-26 08:12:10 +000014542 if (tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014543 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080014544 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014545 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014546 !tg3_flag(tp, 57765_PLUS)) {
Michael Chanc424cb22006-04-29 18:56:34 -070014547 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070014548 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070014549 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
14550 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080014551 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
14552 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014553 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080014554 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014555 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080014556 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014557 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070014558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014559
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014560 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
14561 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
14562 tp->phy_otp = tg3_read_otp_phycfg(tp);
14563 if (tp->phy_otp == 0)
14564 tp->phy_otp = TG3_OTP_DEFAULT;
14565 }
14566
Joe Perches63c3a662011-04-26 08:12:10 +000014567 if (tg3_flag(tp, CPMU_PRESENT))
Matt Carlson8ef21422008-05-02 16:47:53 -070014568 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
14569 else
14570 tp->mi_mode = MAC_MI_MODE_BASE;
14571
Linus Torvalds1da177e2005-04-16 15:20:36 -070014572 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014573 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
14574 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
14575 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
14576
Matt Carlson4d958472011-04-20 07:57:35 +000014577 /* Set these bits to enable statistics workaround. */
14578 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14579 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
14580 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0) {
14581 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
14582 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
14583 }
14584
Matt Carlson321d32a2008-11-21 17:22:19 -080014585 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
14586 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Joe Perches63c3a662011-04-26 08:12:10 +000014587 tg3_flag_set(tp, USE_PHYLIB);
Matt Carlson57e69832008-05-25 23:48:31 -070014588
Matt Carlson158d7ab2008-05-29 01:37:54 -070014589 err = tg3_mdio_init(tp);
14590 if (err)
14591 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014592
14593 /* Initialize data/descriptor byte/word swapping. */
14594 val = tr32(GRC_MODE);
Matt Carlsonf2096f92011-04-05 14:22:48 +000014595 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
14596 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
14597 GRC_MODE_WORD_SWAP_B2HRX_DATA |
14598 GRC_MODE_B2HRX_ENABLE |
14599 GRC_MODE_HTX2B_ENABLE |
14600 GRC_MODE_HOST_STACKUP);
14601 else
14602 val &= GRC_MODE_HOST_STACKUP;
14603
Linus Torvalds1da177e2005-04-16 15:20:36 -070014604 tw32(GRC_MODE, val | tp->grc_mode);
14605
14606 tg3_switch_clocks(tp);
14607
14608 /* Clear this out for sanity. */
14609 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
14610
14611 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14612 &pci_state_reg);
14613 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014614 !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014615 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
14616
14617 if (chiprevid == CHIPREV_ID_5701_A0 ||
14618 chiprevid == CHIPREV_ID_5701_B0 ||
14619 chiprevid == CHIPREV_ID_5701_B2 ||
14620 chiprevid == CHIPREV_ID_5701_B5) {
14621 void __iomem *sram_base;
14622
14623 /* Write some dummy words into the SRAM status block
14624 * area, see if it reads back correctly. If the return
14625 * value is bad, force enable the PCIX workaround.
14626 */
14627 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
14628
14629 writel(0x00000000, sram_base);
14630 writel(0x00000000, sram_base + 4);
14631 writel(0xffffffff, sram_base + 4);
14632 if (readl(sram_base) != 0x00000000)
Joe Perches63c3a662011-04-26 08:12:10 +000014633 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014634 }
14635 }
14636
14637 udelay(50);
14638 tg3_nvram_init(tp);
14639
14640 grc_misc_cfg = tr32(GRC_MISC_CFG);
14641 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
14642
Linus Torvalds1da177e2005-04-16 15:20:36 -070014643 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14644 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
14645 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
Joe Perches63c3a662011-04-26 08:12:10 +000014646 tg3_flag_set(tp, IS_5788);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014647
Joe Perches63c3a662011-04-26 08:12:10 +000014648 if (!tg3_flag(tp, IS_5788) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +000014649 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014650 tg3_flag_set(tp, TAGGED_STATUS);
14651 if (tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070014652 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
14653 HOSTCC_MODE_CLRTICK_TXBD);
14654
14655 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
14656 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
14657 tp->misc_host_ctrl);
14658 }
14659
Matt Carlson3bda1252008-08-15 14:08:22 -070014660 /* Preserve the APE MAC_MODE bits */
Joe Perches63c3a662011-04-26 08:12:10 +000014661 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +000014662 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070014663 else
Matt Carlson6e01b202011-08-19 13:58:20 +000014664 tp->mac_mode = 0;
Matt Carlson3bda1252008-08-15 14:08:22 -070014665
Linus Torvalds1da177e2005-04-16 15:20:36 -070014666 /* these are limited to 10/100 only */
14667 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14668 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
14669 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14670 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14671 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
14672 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
14673 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
14674 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14675 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
Michael Chan676917d2006-12-07 00:20:22 -080014676 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
14677 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014678 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
Matt Carlsond1101142010-02-17 15:16:55 +000014679 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
14680 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014681 (tp->phy_flags & TG3_PHYFLG_IS_FET))
14682 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014683
14684 err = tg3_phy_probe(tp);
14685 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014686 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014687 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014688 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014689 }
14690
Matt Carlson184b8902010-04-05 10:19:25 +000014691 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080014692 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014693
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014694 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
14695 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014696 } else {
14697 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014698 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014699 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014700 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014701 }
14702
14703 /* 5700 {AX,BX} chips have a broken status block link
14704 * change bit implementation, so we must use the
14705 * status register in those cases.
14706 */
14707 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014708 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014709 else
Joe Perches63c3a662011-04-26 08:12:10 +000014710 tg3_flag_clear(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014711
14712 /* The led_ctrl is set during tg3_phy_probe, here we might
14713 * have to force the link status polling mechanism based
14714 * upon subsystem IDs.
14715 */
14716 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070014717 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014718 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
14719 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Joe Perches63c3a662011-04-26 08:12:10 +000014720 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014721 }
14722
14723 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014724 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Joe Perches63c3a662011-04-26 08:12:10 +000014725 tg3_flag_set(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014726 else
Joe Perches63c3a662011-04-26 08:12:10 +000014727 tg3_flag_clear(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014728
Eric Dumazet9205fd92011-11-18 06:47:01 +000014729 tp->rx_offset = NET_SKB_PAD + NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014730 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014731 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014732 tg3_flag(tp, PCIX_MODE)) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000014733 tp->rx_offset = NET_SKB_PAD;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014734#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000014735 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014736#endif
14737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014738
Matt Carlson2c49a442010-09-30 10:34:35 +000014739 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
14740 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000014741 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
14742
Matt Carlson2c49a442010-09-30 10:34:35 +000014743 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070014744
14745 /* Increment the rx prod index on the rx std ring by at most
14746 * 8 for these chips to workaround hw errata.
14747 */
14748 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
14749 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
14750 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
14751 tp->rx_std_max_post = 8;
14752
Joe Perches63c3a662011-04-26 08:12:10 +000014753 if (tg3_flag(tp, ASPM_WORKAROUND))
Matt Carlson8ed5d972007-05-07 00:25:49 -070014754 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
14755 PCIE_PWR_MGMT_L1_THRESH_MSK;
14756
Linus Torvalds1da177e2005-04-16 15:20:36 -070014757 return err;
14758}
14759
David S. Miller49b6e95f2007-03-29 01:38:42 -070014760#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014761static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
14762{
14763 struct net_device *dev = tp->dev;
14764 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014765 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070014766 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014767 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014768
David S. Miller49b6e95f2007-03-29 01:38:42 -070014769 addr = of_get_property(dp, "local-mac-address", &len);
14770 if (addr && len == 6) {
14771 memcpy(dev->dev_addr, addr, 6);
14772 memcpy(dev->perm_addr, dev->dev_addr, 6);
14773 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014774 }
14775 return -ENODEV;
14776}
14777
14778static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
14779{
14780 struct net_device *dev = tp->dev;
14781
14782 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070014783 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014784 return 0;
14785}
14786#endif
14787
14788static int __devinit tg3_get_device_address(struct tg3 *tp)
14789{
14790 struct net_device *dev = tp->dev;
14791 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080014792 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014793
David S. Miller49b6e95f2007-03-29 01:38:42 -070014794#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014795 if (!tg3_get_macaddr_sparc(tp))
14796 return 0;
14797#endif
14798
14799 mac_offset = 0x7c;
Matt Carlson6ff6f812011-05-19 12:12:54 +000014800 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014801 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014802 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
14803 mac_offset = 0xcc;
14804 if (tg3_nvram_lock(tp))
14805 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
14806 else
14807 tg3_nvram_unlock(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000014808 } else if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson69f11c92011-07-13 09:27:30 +000014809 if (tp->pci_fn & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014810 mac_offset = 0xcc;
Matt Carlson69f11c92011-07-13 09:27:30 +000014811 if (tp->pci_fn > 1)
Matt Carlsona50d0792010-06-05 17:24:37 +000014812 mac_offset += 0x18c;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014813 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070014814 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014815
14816 /* First try to get it from MAC address mailbox. */
14817 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
14818 if ((hi >> 16) == 0x484b) {
14819 dev->dev_addr[0] = (hi >> 8) & 0xff;
14820 dev->dev_addr[1] = (hi >> 0) & 0xff;
14821
14822 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
14823 dev->dev_addr[2] = (lo >> 24) & 0xff;
14824 dev->dev_addr[3] = (lo >> 16) & 0xff;
14825 dev->dev_addr[4] = (lo >> 8) & 0xff;
14826 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014827
Michael Chan008652b2006-03-27 23:14:53 -080014828 /* Some old bootcode may report a 0 MAC address in SRAM */
14829 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
14830 }
14831 if (!addr_ok) {
14832 /* Next, try NVRAM. */
Joe Perches63c3a662011-04-26 08:12:10 +000014833 if (!tg3_flag(tp, NO_NVRAM) &&
Matt Carlsondf259d82009-04-20 06:57:14 +000014834 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000014835 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070014836 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
14837 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080014838 }
14839 /* Finally just fetch it out of the MAC control regs. */
14840 else {
14841 hi = tr32(MAC_ADDR_0_HIGH);
14842 lo = tr32(MAC_ADDR_0_LOW);
14843
14844 dev->dev_addr[5] = lo & 0xff;
14845 dev->dev_addr[4] = (lo >> 8) & 0xff;
14846 dev->dev_addr[3] = (lo >> 16) & 0xff;
14847 dev->dev_addr[2] = (lo >> 24) & 0xff;
14848 dev->dev_addr[1] = hi & 0xff;
14849 dev->dev_addr[0] = (hi >> 8) & 0xff;
14850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014851 }
14852
14853 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070014854#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014855 if (!tg3_get_default_macaddr_sparc(tp))
14856 return 0;
14857#endif
14858 return -EINVAL;
14859 }
John W. Linville2ff43692005-09-12 14:44:20 -070014860 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014861 return 0;
14862}
14863
David S. Miller59e6b432005-05-18 22:50:10 -070014864#define BOUNDARY_SINGLE_CACHELINE 1
14865#define BOUNDARY_MULTI_CACHELINE 2
14866
14867static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
14868{
14869 int cacheline_size;
14870 u8 byte;
14871 int goal;
14872
14873 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
14874 if (byte == 0)
14875 cacheline_size = 1024;
14876 else
14877 cacheline_size = (int) byte * 4;
14878
14879 /* On 5703 and later chips, the boundary bits have no
14880 * effect.
14881 */
14882 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14883 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014884 !tg3_flag(tp, PCI_EXPRESS))
David S. Miller59e6b432005-05-18 22:50:10 -070014885 goto out;
14886
14887#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
14888 goal = BOUNDARY_MULTI_CACHELINE;
14889#else
14890#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
14891 goal = BOUNDARY_SINGLE_CACHELINE;
14892#else
14893 goal = 0;
14894#endif
14895#endif
14896
Joe Perches63c3a662011-04-26 08:12:10 +000014897 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014898 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
14899 goto out;
14900 }
14901
David S. Miller59e6b432005-05-18 22:50:10 -070014902 if (!goal)
14903 goto out;
14904
14905 /* PCI controllers on most RISC systems tend to disconnect
14906 * when a device tries to burst across a cache-line boundary.
14907 * Therefore, letting tg3 do so just wastes PCI bandwidth.
14908 *
14909 * Unfortunately, for PCI-E there are only limited
14910 * write-side controls for this, and thus for reads
14911 * we will still get the disconnects. We'll also waste
14912 * these PCI cycles for both read and write for chips
14913 * other than 5700 and 5701 which do not implement the
14914 * boundary bits.
14915 */
Joe Perches63c3a662011-04-26 08:12:10 +000014916 if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014917 switch (cacheline_size) {
14918 case 16:
14919 case 32:
14920 case 64:
14921 case 128:
14922 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14923 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
14924 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
14925 } else {
14926 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14927 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14928 }
14929 break;
14930
14931 case 256:
14932 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
14933 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
14934 break;
14935
14936 default:
14937 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14938 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14939 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014940 }
Joe Perches63c3a662011-04-26 08:12:10 +000014941 } else if (tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014942 switch (cacheline_size) {
14943 case 16:
14944 case 32:
14945 case 64:
14946 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14947 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14948 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
14949 break;
14950 }
14951 /* fallthrough */
14952 case 128:
14953 default:
14954 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14955 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
14956 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014957 }
David S. Miller59e6b432005-05-18 22:50:10 -070014958 } else {
14959 switch (cacheline_size) {
14960 case 16:
14961 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14962 val |= (DMA_RWCTRL_READ_BNDRY_16 |
14963 DMA_RWCTRL_WRITE_BNDRY_16);
14964 break;
14965 }
14966 /* fallthrough */
14967 case 32:
14968 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14969 val |= (DMA_RWCTRL_READ_BNDRY_32 |
14970 DMA_RWCTRL_WRITE_BNDRY_32);
14971 break;
14972 }
14973 /* fallthrough */
14974 case 64:
14975 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14976 val |= (DMA_RWCTRL_READ_BNDRY_64 |
14977 DMA_RWCTRL_WRITE_BNDRY_64);
14978 break;
14979 }
14980 /* fallthrough */
14981 case 128:
14982 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14983 val |= (DMA_RWCTRL_READ_BNDRY_128 |
14984 DMA_RWCTRL_WRITE_BNDRY_128);
14985 break;
14986 }
14987 /* fallthrough */
14988 case 256:
14989 val |= (DMA_RWCTRL_READ_BNDRY_256 |
14990 DMA_RWCTRL_WRITE_BNDRY_256);
14991 break;
14992 case 512:
14993 val |= (DMA_RWCTRL_READ_BNDRY_512 |
14994 DMA_RWCTRL_WRITE_BNDRY_512);
14995 break;
14996 case 1024:
14997 default:
14998 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
14999 DMA_RWCTRL_WRITE_BNDRY_1024);
15000 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070015001 }
David S. Miller59e6b432005-05-18 22:50:10 -070015002 }
15003
15004out:
15005 return val;
15006}
15007
Linus Torvalds1da177e2005-04-16 15:20:36 -070015008static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
15009{
15010 struct tg3_internal_buffer_desc test_desc;
15011 u32 sram_dma_descs;
15012 int i, ret;
15013
15014 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
15015
15016 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
15017 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
15018 tw32(RDMAC_STATUS, 0);
15019 tw32(WDMAC_STATUS, 0);
15020
15021 tw32(BUFMGR_MODE, 0);
15022 tw32(FTQ_RESET, 0);
15023
15024 test_desc.addr_hi = ((u64) buf_dma) >> 32;
15025 test_desc.addr_lo = buf_dma & 0xffffffff;
15026 test_desc.nic_mbuf = 0x00002100;
15027 test_desc.len = size;
15028
15029 /*
15030 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
15031 * the *second* time the tg3 driver was getting loaded after an
15032 * initial scan.
15033 *
15034 * Broadcom tells me:
15035 * ...the DMA engine is connected to the GRC block and a DMA
15036 * reset may affect the GRC block in some unpredictable way...
15037 * The behavior of resets to individual blocks has not been tested.
15038 *
15039 * Broadcom noted the GRC reset will also reset all sub-components.
15040 */
15041 if (to_device) {
15042 test_desc.cqid_sqid = (13 << 8) | 2;
15043
15044 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
15045 udelay(40);
15046 } else {
15047 test_desc.cqid_sqid = (16 << 8) | 7;
15048
15049 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
15050 udelay(40);
15051 }
15052 test_desc.flags = 0x00000005;
15053
15054 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
15055 u32 val;
15056
15057 val = *(((u32 *)&test_desc) + i);
15058 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
15059 sram_dma_descs + (i * sizeof(u32)));
15060 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
15061 }
15062 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
15063
Matt Carlson859a588792010-04-05 10:19:28 +000015064 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015065 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a588792010-04-05 10:19:28 +000015066 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070015067 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015068
15069 ret = -ENODEV;
15070 for (i = 0; i < 40; i++) {
15071 u32 val;
15072
15073 if (to_device)
15074 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
15075 else
15076 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
15077 if ((val & 0xffff) == sram_dma_descs) {
15078 ret = 0;
15079 break;
15080 }
15081
15082 udelay(100);
15083 }
15084
15085 return ret;
15086}
15087
David S. Millerded73402005-05-23 13:59:47 -070015088#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070015089
Matt Carlson41434702011-03-09 16:58:22 +000015090static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080015091 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
15092 { },
15093};
15094
Linus Torvalds1da177e2005-04-16 15:20:36 -070015095static int __devinit tg3_test_dma(struct tg3 *tp)
15096{
15097 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070015098 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000015099 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015100
Matt Carlson4bae65c2010-11-24 08:31:52 +000015101 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
15102 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015103 if (!buf) {
15104 ret = -ENOMEM;
15105 goto out_nofree;
15106 }
15107
15108 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
15109 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
15110
David S. Miller59e6b432005-05-18 22:50:10 -070015111 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015112
Joe Perches63c3a662011-04-26 08:12:10 +000015113 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000015114 goto out;
15115
Joe Perches63c3a662011-04-26 08:12:10 +000015116 if (tg3_flag(tp, PCI_EXPRESS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015117 /* DMA read watermark not used on PCIE */
15118 tp->dma_rwctrl |= 0x00180000;
Joe Perches63c3a662011-04-26 08:12:10 +000015119 } else if (!tg3_flag(tp, PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070015120 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
15121 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015122 tp->dma_rwctrl |= 0x003f0000;
15123 else
15124 tp->dma_rwctrl |= 0x003f000f;
15125 } else {
15126 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
15127 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
15128 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080015129 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015130
Michael Chan4a29cc22006-03-19 13:21:12 -080015131 /* If the 5704 is behind the EPB bridge, we can
15132 * do the less restrictive ONE_DMA workaround for
15133 * better performance.
15134 */
Joe Perches63c3a662011-04-26 08:12:10 +000015135 if (tg3_flag(tp, 40BIT_DMA_BUG) &&
Michael Chan4a29cc22006-03-19 13:21:12 -080015136 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
15137 tp->dma_rwctrl |= 0x8000;
15138 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015139 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
15140
Michael Chan49afdeb2007-02-13 12:17:03 -080015141 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
15142 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070015143 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080015144 tp->dma_rwctrl |=
15145 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
15146 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
15147 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070015148 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
15149 /* 5780 always in PCIX mode */
15150 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070015151 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
15152 /* 5714 always in PCIX mode */
15153 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015154 } else {
15155 tp->dma_rwctrl |= 0x001b000f;
15156 }
15157 }
15158
15159 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
15160 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
15161 tp->dma_rwctrl &= 0xfffffff0;
15162
15163 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
15164 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
15165 /* Remove this if it causes problems for some boards. */
15166 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
15167
15168 /* On 5700/5701 chips, we need to set this bit.
15169 * Otherwise the chip will issue cacheline transactions
15170 * to streamable DMA memory with not all the byte
15171 * enables turned on. This is an error on several
15172 * RISC PCI controllers, in particular sparc64.
15173 *
15174 * On 5703/5704 chips, this bit has been reassigned
15175 * a different meaning. In particular, it is used
15176 * on those chips to enable a PCI-X workaround.
15177 */
15178 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
15179 }
15180
15181 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15182
15183#if 0
15184 /* Unneeded, already done by tg3_get_invariants. */
15185 tg3_switch_clocks(tp);
15186#endif
15187
Linus Torvalds1da177e2005-04-16 15:20:36 -070015188 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
15189 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
15190 goto out;
15191
David S. Miller59e6b432005-05-18 22:50:10 -070015192 /* It is best to perform DMA test with maximum write burst size
15193 * to expose the 5700/5701 write DMA bug.
15194 */
15195 saved_dma_rwctrl = tp->dma_rwctrl;
15196 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
15197 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15198
Linus Torvalds1da177e2005-04-16 15:20:36 -070015199 while (1) {
15200 u32 *p = buf, i;
15201
15202 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
15203 p[i] = i;
15204
15205 /* Send the buffer to the chip. */
15206 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
15207 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000015208 dev_err(&tp->pdev->dev,
15209 "%s: Buffer write failed. err = %d\n",
15210 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015211 break;
15212 }
15213
15214#if 0
15215 /* validate data reached card RAM correctly. */
15216 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
15217 u32 val;
15218 tg3_read_mem(tp, 0x2100 + (i*4), &val);
15219 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000015220 dev_err(&tp->pdev->dev,
15221 "%s: Buffer corrupted on device! "
15222 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015223 /* ret = -ENODEV here? */
15224 }
15225 p[i] = 0;
15226 }
15227#endif
15228 /* Now read it back. */
15229 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
15230 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000015231 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
15232 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015233 break;
15234 }
15235
15236 /* Verify it. */
15237 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
15238 if (p[i] == i)
15239 continue;
15240
David S. Miller59e6b432005-05-18 22:50:10 -070015241 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
15242 DMA_RWCTRL_WRITE_BNDRY_16) {
15243 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015244 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
15245 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15246 break;
15247 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000015248 dev_err(&tp->pdev->dev,
15249 "%s: Buffer corrupted on read back! "
15250 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015251 ret = -ENODEV;
15252 goto out;
15253 }
15254 }
15255
15256 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
15257 /* Success. */
15258 ret = 0;
15259 break;
15260 }
15261 }
David S. Miller59e6b432005-05-18 22:50:10 -070015262 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
15263 DMA_RWCTRL_WRITE_BNDRY_16) {
15264 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070015265 * now look for chipsets that are known to expose the
15266 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070015267 */
Matt Carlson41434702011-03-09 16:58:22 +000015268 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070015269 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
15270 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a588792010-04-05 10:19:28 +000015271 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070015272 /* Safe to use the calculated DMA boundary. */
15273 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a588792010-04-05 10:19:28 +000015274 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070015275
David S. Miller59e6b432005-05-18 22:50:10 -070015276 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015278
15279out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000015280 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015281out_nofree:
15282 return ret;
15283}
15284
Linus Torvalds1da177e2005-04-16 15:20:36 -070015285static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
15286{
Joe Perches63c3a662011-04-26 08:12:10 +000015287 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson666bc832010-01-20 16:58:03 +000015288 tp->bufmgr_config.mbuf_read_dma_low_water =
15289 DEFAULT_MB_RDMA_LOW_WATER_5705;
15290 tp->bufmgr_config.mbuf_mac_rx_low_water =
15291 DEFAULT_MB_MACRX_LOW_WATER_57765;
15292 tp->bufmgr_config.mbuf_high_water =
15293 DEFAULT_MB_HIGH_WATER_57765;
15294
15295 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15296 DEFAULT_MB_RDMA_LOW_WATER_5705;
15297 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15298 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
15299 tp->bufmgr_config.mbuf_high_water_jumbo =
15300 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000015301 } else if (tg3_flag(tp, 5705_PLUS)) {
Michael Chanfdfec1722005-07-25 12:31:48 -070015302 tp->bufmgr_config.mbuf_read_dma_low_water =
15303 DEFAULT_MB_RDMA_LOW_WATER_5705;
15304 tp->bufmgr_config.mbuf_mac_rx_low_water =
15305 DEFAULT_MB_MACRX_LOW_WATER_5705;
15306 tp->bufmgr_config.mbuf_high_water =
15307 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070015308 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
15309 tp->bufmgr_config.mbuf_mac_rx_low_water =
15310 DEFAULT_MB_MACRX_LOW_WATER_5906;
15311 tp->bufmgr_config.mbuf_high_water =
15312 DEFAULT_MB_HIGH_WATER_5906;
15313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015314
Michael Chanfdfec1722005-07-25 12:31:48 -070015315 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15316 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
15317 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15318 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
15319 tp->bufmgr_config.mbuf_high_water_jumbo =
15320 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
15321 } else {
15322 tp->bufmgr_config.mbuf_read_dma_low_water =
15323 DEFAULT_MB_RDMA_LOW_WATER;
15324 tp->bufmgr_config.mbuf_mac_rx_low_water =
15325 DEFAULT_MB_MACRX_LOW_WATER;
15326 tp->bufmgr_config.mbuf_high_water =
15327 DEFAULT_MB_HIGH_WATER;
15328
15329 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15330 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
15331 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15332 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
15333 tp->bufmgr_config.mbuf_high_water_jumbo =
15334 DEFAULT_MB_HIGH_WATER_JUMBO;
15335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015336
15337 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
15338 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
15339}
15340
15341static char * __devinit tg3_phy_string(struct tg3 *tp)
15342{
Matt Carlson79eb6902010-02-17 15:17:03 +000015343 switch (tp->phy_id & TG3_PHY_ID_MASK) {
15344 case TG3_PHY_ID_BCM5400: return "5400";
15345 case TG3_PHY_ID_BCM5401: return "5401";
15346 case TG3_PHY_ID_BCM5411: return "5411";
15347 case TG3_PHY_ID_BCM5701: return "5701";
15348 case TG3_PHY_ID_BCM5703: return "5703";
15349 case TG3_PHY_ID_BCM5704: return "5704";
15350 case TG3_PHY_ID_BCM5705: return "5705";
15351 case TG3_PHY_ID_BCM5750: return "5750";
15352 case TG3_PHY_ID_BCM5752: return "5752";
15353 case TG3_PHY_ID_BCM5714: return "5714";
15354 case TG3_PHY_ID_BCM5780: return "5780";
15355 case TG3_PHY_ID_BCM5755: return "5755";
15356 case TG3_PHY_ID_BCM5787: return "5787";
15357 case TG3_PHY_ID_BCM5784: return "5784";
15358 case TG3_PHY_ID_BCM5756: return "5722/5756";
15359 case TG3_PHY_ID_BCM5906: return "5906";
15360 case TG3_PHY_ID_BCM5761: return "5761";
15361 case TG3_PHY_ID_BCM5718C: return "5718C";
15362 case TG3_PHY_ID_BCM5718S: return "5718S";
15363 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000015364 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000015365 case TG3_PHY_ID_BCM5720C: return "5720C";
Matt Carlson79eb6902010-02-17 15:17:03 +000015366 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070015367 case 0: return "serdes";
15368 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070015369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015370}
15371
Michael Chanf9804dd2005-09-27 12:13:10 -070015372static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
15373{
Joe Perches63c3a662011-04-26 08:12:10 +000015374 if (tg3_flag(tp, PCI_EXPRESS)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070015375 strcpy(str, "PCI Express");
15376 return str;
Joe Perches63c3a662011-04-26 08:12:10 +000015377 } else if (tg3_flag(tp, PCIX_MODE)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070015378 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
15379
15380 strcpy(str, "PCIX:");
15381
15382 if ((clock_ctrl == 7) ||
15383 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
15384 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
15385 strcat(str, "133MHz");
15386 else if (clock_ctrl == 0)
15387 strcat(str, "33MHz");
15388 else if (clock_ctrl == 2)
15389 strcat(str, "50MHz");
15390 else if (clock_ctrl == 4)
15391 strcat(str, "66MHz");
15392 else if (clock_ctrl == 6)
15393 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070015394 } else {
15395 strcpy(str, "PCI:");
Joe Perches63c3a662011-04-26 08:12:10 +000015396 if (tg3_flag(tp, PCI_HIGH_SPEED))
Michael Chanf9804dd2005-09-27 12:13:10 -070015397 strcat(str, "66MHz");
15398 else
15399 strcat(str, "33MHz");
15400 }
Joe Perches63c3a662011-04-26 08:12:10 +000015401 if (tg3_flag(tp, PCI_32BIT))
Michael Chanf9804dd2005-09-27 12:13:10 -070015402 strcat(str, ":32-bit");
15403 else
15404 strcat(str, ":64-bit");
15405 return str;
15406}
15407
David S. Miller15f98502005-05-18 22:49:26 -070015408static void __devinit tg3_init_coal(struct tg3 *tp)
15409{
15410 struct ethtool_coalesce *ec = &tp->coal;
15411
15412 memset(ec, 0, sizeof(*ec));
15413 ec->cmd = ETHTOOL_GCOALESCE;
15414 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
15415 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
15416 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
15417 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
15418 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
15419 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
15420 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
15421 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
15422 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
15423
15424 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
15425 HOSTCC_MODE_CLRTICK_TXBD)) {
15426 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
15427 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
15428 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
15429 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
15430 }
Michael Chand244c892005-07-05 14:42:33 -070015431
Joe Perches63c3a662011-04-26 08:12:10 +000015432 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070015433 ec->rx_coalesce_usecs_irq = 0;
15434 ec->tx_coalesce_usecs_irq = 0;
15435 ec->stats_block_coalesce_usecs = 0;
15436 }
David S. Miller15f98502005-05-18 22:49:26 -070015437}
15438
Linus Torvalds1da177e2005-04-16 15:20:36 -070015439static int __devinit tg3_init_one(struct pci_dev *pdev,
15440 const struct pci_device_id *ent)
15441{
Linus Torvalds1da177e2005-04-16 15:20:36 -070015442 struct net_device *dev;
15443 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000015444 int i, err, pm_cap;
15445 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070015446 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080015447 u64 dma_mask, persist_dma_mask;
Michał Mirosławc8f44af2011-11-15 15:29:55 +000015448 netdev_features_t features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015449
Joe Perches05dbe002010-02-17 19:44:19 +000015450 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015451
15452 err = pci_enable_device(pdev);
15453 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015454 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015455 return err;
15456 }
15457
Linus Torvalds1da177e2005-04-16 15:20:36 -070015458 err = pci_request_regions(pdev, DRV_MODULE_NAME);
15459 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015460 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015461 goto err_out_disable_pdev;
15462 }
15463
15464 pci_set_master(pdev);
15465
15466 /* Find power-management capability. */
15467 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
15468 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000015469 dev_err(&pdev->dev,
15470 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015471 err = -EIO;
15472 goto err_out_free_res;
15473 }
15474
Matt Carlson16821282011-07-13 09:27:28 +000015475 err = pci_set_power_state(pdev, PCI_D0);
15476 if (err) {
15477 dev_err(&pdev->dev, "Transition to D0 failed, aborting\n");
15478 goto err_out_free_res;
15479 }
15480
Matt Carlsonfe5f5782009-09-01 13:09:39 +000015481 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015482 if (!dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015483 err = -ENOMEM;
Matt Carlson16821282011-07-13 09:27:28 +000015484 goto err_out_power_down;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015485 }
15486
Linus Torvalds1da177e2005-04-16 15:20:36 -070015487 SET_NETDEV_DEV(dev, &pdev->dev);
15488
Linus Torvalds1da177e2005-04-16 15:20:36 -070015489 tp = netdev_priv(dev);
15490 tp->pdev = pdev;
15491 tp->dev = dev;
15492 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015493 tp->rx_mode = TG3_DEF_RX_MODE;
15494 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070015495
Linus Torvalds1da177e2005-04-16 15:20:36 -070015496 if (tg3_debug > 0)
15497 tp->msg_enable = tg3_debug;
15498 else
15499 tp->msg_enable = TG3_DEF_MSG_ENABLE;
15500
15501 /* The word/byte swap controls here control register access byte
15502 * swapping. DMA data byte swapping is controlled in the GRC_MODE
15503 * setting below.
15504 */
15505 tp->misc_host_ctrl =
15506 MISC_HOST_CTRL_MASK_PCI_INT |
15507 MISC_HOST_CTRL_WORD_SWAP |
15508 MISC_HOST_CTRL_INDIR_ACCESS |
15509 MISC_HOST_CTRL_PCISTATE_RW;
15510
15511 /* The NONFRM (non-frame) byte/word swap controls take effect
15512 * on descriptor entries, anything which isn't packet data.
15513 *
15514 * The StrongARM chips on the board (one for tx, one for rx)
15515 * are running in big-endian mode.
15516 */
15517 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
15518 GRC_MODE_WSWAP_NONFRM_DATA);
15519#ifdef __BIG_ENDIAN
15520 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
15521#endif
15522 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015523 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000015524 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015525
Matt Carlsond5fe4882008-11-21 17:20:32 -080015526 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010015527 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015528 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015529 err = -ENOMEM;
15530 goto err_out_free_dev;
15531 }
15532
Matt Carlsonc9cab242011-07-13 09:27:27 +000015533 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
15534 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761E ||
15535 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S ||
15536 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761SE ||
15537 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
15538 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
15539 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
15540 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720) {
15541 tg3_flag_set(tp, ENABLE_APE);
15542 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
15543 if (!tp->aperegs) {
15544 dev_err(&pdev->dev,
15545 "Cannot map APE registers, aborting\n");
15546 err = -ENOMEM;
15547 goto err_out_iounmap;
15548 }
15549 }
15550
Linus Torvalds1da177e2005-04-16 15:20:36 -070015551 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
15552 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015553
Linus Torvalds1da177e2005-04-16 15:20:36 -070015554 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015555 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Matt Carlson2ffcc982011-05-19 12:12:44 +000015556 dev->netdev_ops = &tg3_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015557 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015558
15559 err = tg3_get_invariants(tp);
15560 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015561 dev_err(&pdev->dev,
15562 "Problem fetching invariants of chip, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015563 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015564 }
15565
Michael Chan4a29cc22006-03-19 13:21:12 -080015566 /* The EPB bridge inside 5714, 5715, and 5780 and any
15567 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080015568 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
15569 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
15570 * do DMA address check in tg3_start_xmit().
15571 */
Joe Perches63c3a662011-04-26 08:12:10 +000015572 if (tg3_flag(tp, IS_5788))
Yang Hongyang284901a2009-04-06 19:01:15 -070015573 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Joe Perches63c3a662011-04-26 08:12:10 +000015574 else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070015575 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080015576#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070015577 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015578#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080015579 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070015580 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015581
15582 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070015583 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080015584 err = pci_set_dma_mask(pdev, dma_mask);
15585 if (!err) {
Matt Carlson0da06062011-05-19 12:12:53 +000015586 features |= NETIF_F_HIGHDMA;
Michael Chan72f2afb2006-03-06 19:28:35 -080015587 err = pci_set_consistent_dma_mask(pdev,
15588 persist_dma_mask);
15589 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015590 dev_err(&pdev->dev, "Unable to obtain 64 bit "
15591 "DMA for consistent allocations\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015592 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080015593 }
15594 }
15595 }
Yang Hongyang284901a2009-04-06 19:01:15 -070015596 if (err || dma_mask == DMA_BIT_MASK(32)) {
15597 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080015598 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015599 dev_err(&pdev->dev,
15600 "No usable DMA configuration, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015601 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080015602 }
15603 }
15604
Michael Chanfdfec1722005-07-25 12:31:48 -070015605 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015606
Matt Carlson0da06062011-05-19 12:12:53 +000015607 features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
15608
15609 /* 5700 B0 chips do not support checksumming correctly due
15610 * to hardware bugs.
15611 */
15612 if (tp->pci_chip_rev_id != CHIPREV_ID_5700_B0) {
15613 features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
15614
15615 if (tg3_flag(tp, 5755_PLUS))
15616 features |= NETIF_F_IPV6_CSUM;
15617 }
15618
Michael Chan4e3a7aa2006-03-20 17:47:44 -080015619 /* TSO is on by default on chips that support hardware TSO.
15620 * Firmware TSO on older chips gives lower performance, so it
15621 * is off by default, but can be enabled using ethtool.
15622 */
Joe Perches63c3a662011-04-26 08:12:10 +000015623 if ((tg3_flag(tp, HW_TSO_1) ||
15624 tg3_flag(tp, HW_TSO_2) ||
15625 tg3_flag(tp, HW_TSO_3)) &&
Matt Carlson0da06062011-05-19 12:12:53 +000015626 (features & NETIF_F_IP_CSUM))
15627 features |= NETIF_F_TSO;
Joe Perches63c3a662011-04-26 08:12:10 +000015628 if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
Matt Carlson0da06062011-05-19 12:12:53 +000015629 if (features & NETIF_F_IPV6_CSUM)
15630 features |= NETIF_F_TSO6;
Joe Perches63c3a662011-04-26 08:12:10 +000015631 if (tg3_flag(tp, HW_TSO_3) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000015632 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070015633 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
15634 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Joe Perches63c3a662011-04-26 08:12:10 +000015635 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Michał Mirosławdc668912011-04-07 03:35:07 +000015636 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson0da06062011-05-19 12:12:53 +000015637 features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070015638 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015639
Matt Carlsond542fe22011-05-19 16:02:43 +000015640 dev->features |= features;
15641 dev->vlan_features |= features;
15642
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015643 /*
15644 * Add loopback capability only for a subset of devices that support
15645 * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
15646 * loopback for the remaining devices.
15647 */
15648 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
15649 !tg3_flag(tp, CPMU_PRESENT))
15650 /* Add the loopback capability */
Matt Carlson0da06062011-05-19 12:12:53 +000015651 features |= NETIF_F_LOOPBACK;
15652
Matt Carlson0da06062011-05-19 12:12:53 +000015653 dev->hw_features |= features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015654
Linus Torvalds1da177e2005-04-16 15:20:36 -070015655 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015656 !tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015657 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015658 tg3_flag_set(tp, MAX_RXPEND_64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015659 tp->rx_pending = 63;
15660 }
15661
Linus Torvalds1da177e2005-04-16 15:20:36 -070015662 err = tg3_get_device_address(tp);
15663 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015664 dev_err(&pdev->dev,
15665 "Could not obtain valid ethernet address, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015666 goto err_out_apeunmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070015667 }
15668
Matt Carlsonc88864d2007-11-12 21:07:01 -080015669 /*
15670 * Reset chip in case UNDI or EFI driver did not shutdown
15671 * DMA self test will enable WDMAC and we'll see (spurious)
15672 * pending DMA on the PCI bus at that point.
15673 */
15674 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
15675 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
15676 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
15677 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
15678 }
15679
15680 err = tg3_test_dma(tp);
15681 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015682 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080015683 goto err_out_apeunmap;
15684 }
15685
Matt Carlson78f90dc2009-11-13 13:03:42 +000015686 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
15687 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
15688 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000015689 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000015690 struct tg3_napi *tnapi = &tp->napi[i];
15691
15692 tnapi->tp = tp;
15693 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
15694
15695 tnapi->int_mbox = intmbx;
Matt Carlson93a700a2011-08-31 11:44:54 +000015696 if (i <= 4)
Matt Carlson78f90dc2009-11-13 13:03:42 +000015697 intmbx += 0x8;
15698 else
15699 intmbx += 0x4;
15700
15701 tnapi->consmbox = rcvmbx;
15702 tnapi->prodmbox = sndmbx;
15703
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015704 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000015705 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015706 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000015707 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000015708
Joe Perches63c3a662011-04-26 08:12:10 +000015709 if (!tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson78f90dc2009-11-13 13:03:42 +000015710 break;
15711
15712 /*
15713 * If we support MSIX, we'll be using RSS. If we're using
15714 * RSS, the first vector only handles link interrupts and the
15715 * remaining vectors handle rx and tx interrupts. Reuse the
15716 * mailbox values for the next iteration. The values we setup
15717 * above are still useful for the single vectored mode.
15718 */
15719 if (!i)
15720 continue;
15721
15722 rcvmbx += 0x8;
15723
15724 if (sndmbx & 0x4)
15725 sndmbx -= 0x4;
15726 else
15727 sndmbx += 0xc;
15728 }
15729
Matt Carlsonc88864d2007-11-12 21:07:01 -080015730 tg3_init_coal(tp);
15731
Michael Chanc49a1562006-12-17 17:07:29 -080015732 pci_set_drvdata(pdev, dev);
15733
Matt Carlsoncd0d7222011-07-13 09:27:33 +000015734 if (tg3_flag(tp, 5717_PLUS)) {
15735 /* Resume a low-power mode */
15736 tg3_frob_aux_power(tp, false);
15737 }
15738
Matt Carlson21f76382012-02-22 12:35:21 +000015739 tg3_timer_init(tp);
15740
Linus Torvalds1da177e2005-04-16 15:20:36 -070015741 err = register_netdev(dev);
15742 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015743 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070015744 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015745 }
15746
Joe Perches05dbe002010-02-17 19:44:19 +000015747 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
15748 tp->board_part_number,
15749 tp->pci_chip_rev_id,
15750 tg3_bus_string(tp, str),
15751 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015752
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015753 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000015754 struct phy_device *phydev;
15755 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000015756 netdev_info(dev,
15757 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000015758 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015759 } else {
15760 char *ethtype;
15761
15762 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
15763 ethtype = "10/100Base-TX";
15764 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
15765 ethtype = "1000Base-SX";
15766 else
15767 ethtype = "10/100/1000Base-T";
15768
Matt Carlson5129c3a2010-04-05 10:19:23 +000015769 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlson47007832011-04-20 07:57:43 +000015770 "(WireSpeed[%d], EEE[%d])\n",
15771 tg3_phy_string(tp), ethtype,
15772 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
15773 (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015774 }
Matt Carlsondf59c942008-11-03 16:52:56 -080015775
Joe Perches05dbe002010-02-17 19:44:19 +000015776 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000015777 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015778 tg3_flag(tp, USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015779 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015780 tg3_flag(tp, ENABLE_ASF) != 0,
15781 tg3_flag(tp, TSO_CAPABLE) != 0);
Joe Perches05dbe002010-02-17 19:44:19 +000015782 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
15783 tp->dma_rwctrl,
15784 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
15785 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015786
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015787 pci_save_state(pdev);
15788
Linus Torvalds1da177e2005-04-16 15:20:36 -070015789 return 0;
15790
Matt Carlson0d3031d2007-10-10 18:02:43 -070015791err_out_apeunmap:
15792 if (tp->aperegs) {
15793 iounmap(tp->aperegs);
15794 tp->aperegs = NULL;
15795 }
15796
Linus Torvalds1da177e2005-04-16 15:20:36 -070015797err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070015798 if (tp->regs) {
15799 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015800 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015802
15803err_out_free_dev:
15804 free_netdev(dev);
15805
Matt Carlson16821282011-07-13 09:27:28 +000015806err_out_power_down:
15807 pci_set_power_state(pdev, PCI_D3hot);
15808
Linus Torvalds1da177e2005-04-16 15:20:36 -070015809err_out_free_res:
15810 pci_release_regions(pdev);
15811
15812err_out_disable_pdev:
15813 pci_disable_device(pdev);
15814 pci_set_drvdata(pdev, NULL);
15815 return err;
15816}
15817
15818static void __devexit tg3_remove_one(struct pci_dev *pdev)
15819{
15820 struct net_device *dev = pci_get_drvdata(pdev);
15821
15822 if (dev) {
15823 struct tg3 *tp = netdev_priv(dev);
15824
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080015825 if (tp->fw)
15826 release_firmware(tp->fw);
15827
Matt Carlsondb219972011-11-04 09:15:03 +000015828 tg3_reset_task_cancel(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015829
David S. Miller1805b2f2011-10-24 18:18:09 -040015830 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015831 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015832 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015833 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070015834
Linus Torvalds1da177e2005-04-16 15:20:36 -070015835 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015836 if (tp->aperegs) {
15837 iounmap(tp->aperegs);
15838 tp->aperegs = NULL;
15839 }
Michael Chan68929142005-08-09 20:17:14 -070015840 if (tp->regs) {
15841 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015842 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015844 free_netdev(dev);
15845 pci_release_regions(pdev);
15846 pci_disable_device(pdev);
15847 pci_set_drvdata(pdev, NULL);
15848 }
15849}
15850
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015851#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015852static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015853{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015854 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015855 struct net_device *dev = pci_get_drvdata(pdev);
15856 struct tg3 *tp = netdev_priv(dev);
15857 int err;
15858
15859 if (!netif_running(dev))
15860 return 0;
15861
Matt Carlsondb219972011-11-04 09:15:03 +000015862 tg3_reset_task_cancel(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015863 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015864 tg3_netif_stop(tp);
15865
Matt Carlson21f76382012-02-22 12:35:21 +000015866 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015867
David S. Millerf47c11e2005-06-24 20:18:35 -070015868 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015869 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070015870 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015871
15872 netif_device_detach(dev);
15873
David S. Millerf47c11e2005-06-24 20:18:35 -070015874 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070015875 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches63c3a662011-04-26 08:12:10 +000015876 tg3_flag_clear(tp, INIT_COMPLETE);
David S. Millerf47c11e2005-06-24 20:18:35 -070015877 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015878
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015879 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015880 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015881 int err2;
15882
David S. Millerf47c11e2005-06-24 20:18:35 -070015883 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015884
Joe Perches63c3a662011-04-26 08:12:10 +000015885 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015886 err2 = tg3_restart_hw(tp, 1);
15887 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070015888 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015889
Matt Carlson21f76382012-02-22 12:35:21 +000015890 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015891
15892 netif_device_attach(dev);
15893 tg3_netif_start(tp);
15894
Michael Chanb9ec6c12006-07-25 16:37:27 -070015895out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015896 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015897
15898 if (!err2)
15899 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015900 }
15901
15902 return err;
15903}
15904
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015905static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015906{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015907 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015908 struct net_device *dev = pci_get_drvdata(pdev);
15909 struct tg3 *tp = netdev_priv(dev);
15910 int err;
15911
15912 if (!netif_running(dev))
15913 return 0;
15914
Linus Torvalds1da177e2005-04-16 15:20:36 -070015915 netif_device_attach(dev);
15916
David S. Millerf47c11e2005-06-24 20:18:35 -070015917 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015918
Joe Perches63c3a662011-04-26 08:12:10 +000015919 tg3_flag_set(tp, INIT_COMPLETE);
Michael Chanb9ec6c12006-07-25 16:37:27 -070015920 err = tg3_restart_hw(tp, 1);
15921 if (err)
15922 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015923
Matt Carlson21f76382012-02-22 12:35:21 +000015924 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015925
Linus Torvalds1da177e2005-04-16 15:20:36 -070015926 tg3_netif_start(tp);
15927
Michael Chanb9ec6c12006-07-25 16:37:27 -070015928out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015929 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015930
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015931 if (!err)
15932 tg3_phy_start(tp);
15933
Michael Chanb9ec6c12006-07-25 16:37:27 -070015934 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015935}
15936
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015937static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015938#define TG3_PM_OPS (&tg3_pm_ops)
15939
15940#else
15941
15942#define TG3_PM_OPS NULL
15943
15944#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015945
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015946/**
15947 * tg3_io_error_detected - called when PCI error is detected
15948 * @pdev: Pointer to PCI device
15949 * @state: The current pci connection state
15950 *
15951 * This function is called after a PCI bus error affecting
15952 * this device has been detected.
15953 */
15954static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
15955 pci_channel_state_t state)
15956{
15957 struct net_device *netdev = pci_get_drvdata(pdev);
15958 struct tg3 *tp = netdev_priv(netdev);
15959 pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
15960
15961 netdev_info(netdev, "PCI I/O error detected\n");
15962
15963 rtnl_lock();
15964
15965 if (!netif_running(netdev))
15966 goto done;
15967
15968 tg3_phy_stop(tp);
15969
15970 tg3_netif_stop(tp);
15971
Matt Carlson21f76382012-02-22 12:35:21 +000015972 tg3_timer_stop(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015973
15974 /* Want to make sure that the reset task doesn't run */
Matt Carlsondb219972011-11-04 09:15:03 +000015975 tg3_reset_task_cancel(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015976
15977 netif_device_detach(netdev);
15978
15979 /* Clean up software state, even if MMIO is blocked */
15980 tg3_full_lock(tp, 0);
15981 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
15982 tg3_full_unlock(tp);
15983
15984done:
15985 if (state == pci_channel_io_perm_failure)
15986 err = PCI_ERS_RESULT_DISCONNECT;
15987 else
15988 pci_disable_device(pdev);
15989
15990 rtnl_unlock();
15991
15992 return err;
15993}
15994
15995/**
15996 * tg3_io_slot_reset - called after the pci bus has been reset.
15997 * @pdev: Pointer to PCI device
15998 *
15999 * Restart the card from scratch, as if from a cold-boot.
16000 * At this point, the card has exprienced a hard reset,
16001 * followed by fixups by BIOS, and has its config space
16002 * set up identically to what it was at cold boot.
16003 */
16004static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
16005{
16006 struct net_device *netdev = pci_get_drvdata(pdev);
16007 struct tg3 *tp = netdev_priv(netdev);
16008 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
16009 int err;
16010
16011 rtnl_lock();
16012
16013 if (pci_enable_device(pdev)) {
16014 netdev_err(netdev, "Cannot re-enable PCI device after reset.\n");
16015 goto done;
16016 }
16017
16018 pci_set_master(pdev);
16019 pci_restore_state(pdev);
16020 pci_save_state(pdev);
16021
16022 if (!netif_running(netdev)) {
16023 rc = PCI_ERS_RESULT_RECOVERED;
16024 goto done;
16025 }
16026
16027 err = tg3_power_up(tp);
Matt Carlsonbed98292011-07-13 09:27:29 +000016028 if (err)
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016029 goto done;
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016030
16031 rc = PCI_ERS_RESULT_RECOVERED;
16032
16033done:
16034 rtnl_unlock();
16035
16036 return rc;
16037}
16038
16039/**
16040 * tg3_io_resume - called when traffic can start flowing again.
16041 * @pdev: Pointer to PCI device
16042 *
16043 * This callback is called when the error recovery driver tells
16044 * us that its OK to resume normal operation.
16045 */
16046static void tg3_io_resume(struct pci_dev *pdev)
16047{
16048 struct net_device *netdev = pci_get_drvdata(pdev);
16049 struct tg3 *tp = netdev_priv(netdev);
16050 int err;
16051
16052 rtnl_lock();
16053
16054 if (!netif_running(netdev))
16055 goto done;
16056
16057 tg3_full_lock(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +000016058 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016059 err = tg3_restart_hw(tp, 1);
16060 tg3_full_unlock(tp);
16061 if (err) {
16062 netdev_err(netdev, "Cannot restart hardware after reset.\n");
16063 goto done;
16064 }
16065
16066 netif_device_attach(netdev);
16067
Matt Carlson21f76382012-02-22 12:35:21 +000016068 tg3_timer_start(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016069
16070 tg3_netif_start(tp);
16071
16072 tg3_phy_start(tp);
16073
16074done:
16075 rtnl_unlock();
16076}
16077
16078static struct pci_error_handlers tg3_err_handler = {
16079 .error_detected = tg3_io_error_detected,
16080 .slot_reset = tg3_io_slot_reset,
16081 .resume = tg3_io_resume
16082};
16083
Linus Torvalds1da177e2005-04-16 15:20:36 -070016084static struct pci_driver tg3_driver = {
16085 .name = DRV_MODULE_NAME,
16086 .id_table = tg3_pci_tbl,
16087 .probe = tg3_init_one,
16088 .remove = __devexit_p(tg3_remove_one),
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016089 .err_handler = &tg3_err_handler,
Eric Dumazetaa6027c2011-01-01 05:22:46 +000016090 .driver.pm = TG3_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070016091};
16092
16093static int __init tg3_init(void)
16094{
Jeff Garzik29917622006-08-19 17:48:59 -040016095 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016096}
16097
16098static void __exit tg3_cleanup(void)
16099{
16100 pci_unregister_driver(&tg3_driver);
16101}
16102
16103module_init(tg3_init);
16104module_exit(tg3_cleanup);