blob: d2ff8ee39e84676926a48f52cf87ccb01309402a [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
Michael Chan7ae52892012-03-21 15:38:33 +000092#define TG3_MIN_NUM 123
Matt Carlson6867c842010-07-11 09:31:44 +000093#define DRV_MODULE_VERSION \
94 __stringify(TG3_MAJ_NUM) "." __stringify(TG3_MIN_NUM)
Michael Chan7ae52892012-03-21 15:38:33 +000095#define DRV_MODULE_RELDATE "March 21, 2012"
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 Carlson66711e662009-11-13 13:03:49 +0000474 if (off == TG3_RX_STD_PROD_IDX_REG) {
Michael Chan68929142005-08-09 20:17:14 -0700475 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
476 TG3_64BIT_REG_LOW, val);
477 return;
478 }
479
480 spin_lock_irqsave(&tp->indirect_lock, flags);
481 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
482 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
483 spin_unlock_irqrestore(&tp->indirect_lock, flags);
484
485 /* In indirect mode when disabling interrupts, we also need
486 * to clear the interrupt bit in the GRC local ctrl register.
487 */
488 if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
489 (val == 0x1)) {
490 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
491 tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
492 }
493}
494
495static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
496{
497 unsigned long flags;
498 u32 val;
499
500 spin_lock_irqsave(&tp->indirect_lock, flags);
501 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
502 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
503 spin_unlock_irqrestore(&tp->indirect_lock, flags);
504 return val;
505}
506
Michael Chanb401e9e2005-12-19 16:27:04 -0800507/* usec_wait specifies the wait time in usec when writing to certain registers
508 * where it is unsafe to read back the register without some delay.
509 * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
510 * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
511 */
512static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
Joe Perches63c3a662011-04-26 08:12:10 +0000514 if (tg3_flag(tp, PCIX_TARGET_HWBUG) || tg3_flag(tp, ICH_WORKAROUND))
Michael Chanb401e9e2005-12-19 16:27:04 -0800515 /* Non-posted methods */
516 tp->write32(tp, off, val);
517 else {
518 /* Posted method */
519 tg3_write32(tp, off, val);
520 if (usec_wait)
521 udelay(usec_wait);
522 tp->read32(tp, off);
523 }
524 /* Wait again after the read for the posted method to guarantee that
525 * the wait time is met.
526 */
527 if (usec_wait)
528 udelay(usec_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529}
530
Michael Chan09ee9292005-08-09 20:17:00 -0700531static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
532{
533 tp->write32_mbox(tp, off, val);
Joe Perches63c3a662011-04-26 08:12:10 +0000534 if (!tg3_flag(tp, MBOX_WRITE_REORDER) && !tg3_flag(tp, ICH_WORKAROUND))
Michael Chan68929142005-08-09 20:17:14 -0700535 tp->read32_mbox(tp, off);
Michael Chan09ee9292005-08-09 20:17:00 -0700536}
537
Michael Chan20094932005-08-09 20:16:32 -0700538static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
540 void __iomem *mbox = tp->regs + off;
541 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000542 if (tg3_flag(tp, TXD_MBOX_HWBUG))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000544 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 readl(mbox);
546}
547
Michael Chanb5d37722006-09-27 16:06:21 -0700548static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
549{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000550 return readl(tp->regs + off + GRCMBOX_BASE);
Michael Chanb5d37722006-09-27 16:06:21 -0700551}
552
553static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
554{
555 writel(val, tp->regs + off + GRCMBOX_BASE);
556}
557
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000558#define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700559#define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000560#define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
561#define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
562#define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
Michael Chan20094932005-08-09 20:16:32 -0700563
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000564#define tw32(reg, val) tp->write32(tp, reg, val)
565#define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
566#define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
567#define tr32(reg) tp->read32(tp, reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
570{
Michael Chan68929142005-08-09 20:17:14 -0700571 unsigned long flags;
572
Matt Carlson6ff6f812011-05-19 12:12:54 +0000573 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700574 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
575 return;
576
Michael Chan68929142005-08-09 20:17:14 -0700577 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000578 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700579 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
580 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Michael Chanbbadf502006-04-06 21:46:34 -0700582 /* Always leave this as zero. */
583 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
584 } else {
585 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
586 tw32_f(TG3PCI_MEM_WIN_DATA, val);
587
588 /* Always leave this as zero. */
589 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
590 }
Michael Chan68929142005-08-09 20:17:14 -0700591 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592}
593
594static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
595{
Michael Chan68929142005-08-09 20:17:14 -0700596 unsigned long flags;
597
Matt Carlson6ff6f812011-05-19 12:12:54 +0000598 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700599 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
600 *val = 0;
601 return;
602 }
603
Michael Chan68929142005-08-09 20:17:14 -0700604 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000605 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700606 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
607 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Michael Chanbbadf502006-04-06 21:46:34 -0700609 /* Always leave this as zero. */
610 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
611 } else {
612 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
613 *val = tr32(TG3PCI_MEM_WIN_DATA);
614
615 /* Always leave this as zero. */
616 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
617 }
Michael Chan68929142005-08-09 20:17:14 -0700618 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619}
620
Matt Carlson0d3031d2007-10-10 18:02:43 -0700621static void tg3_ape_lock_init(struct tg3 *tp)
622{
623 int i;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000624 u32 regbase, bit;
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000625
626 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
627 regbase = TG3_APE_LOCK_GRANT;
628 else
629 regbase = TG3_APE_PER_LOCK_GRANT;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700630
631 /* Make sure the driver hasn't any stale locks. */
Matt Carlson78f94dc2011-11-04 09:14:58 +0000632 for (i = TG3_APE_LOCK_PHY0; i <= TG3_APE_LOCK_GPIO; i++) {
633 switch (i) {
634 case TG3_APE_LOCK_PHY0:
635 case TG3_APE_LOCK_PHY1:
636 case TG3_APE_LOCK_PHY2:
637 case TG3_APE_LOCK_PHY3:
638 bit = APE_LOCK_GRANT_DRIVER;
639 break;
640 default:
641 if (!tp->pci_fn)
642 bit = APE_LOCK_GRANT_DRIVER;
643 else
644 bit = 1 << tp->pci_fn;
645 }
646 tg3_ape_write32(tp, regbase + 4 * i, bit);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000647 }
648
Matt Carlson0d3031d2007-10-10 18:02:43 -0700649}
650
651static int tg3_ape_lock(struct tg3 *tp, int locknum)
652{
653 int i, off;
654 int ret = 0;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000655 u32 status, req, gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700656
Joe Perches63c3a662011-04-26 08:12:10 +0000657 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700658 return 0;
659
660 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000661 case TG3_APE_LOCK_GPIO:
662 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
663 return 0;
Matt Carlson33f401a2010-04-05 10:19:27 +0000664 case TG3_APE_LOCK_GRC:
665 case TG3_APE_LOCK_MEM:
Matt Carlson78f94dc2011-11-04 09:14:58 +0000666 if (!tp->pci_fn)
667 bit = APE_LOCK_REQ_DRIVER;
668 else
669 bit = 1 << tp->pci_fn;
Matt Carlson33f401a2010-04-05 10:19:27 +0000670 break;
671 default:
672 return -EINVAL;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700673 }
674
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000675 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
676 req = TG3_APE_LOCK_REQ;
677 gnt = TG3_APE_LOCK_GRANT;
678 } else {
679 req = TG3_APE_PER_LOCK_REQ;
680 gnt = TG3_APE_PER_LOCK_GRANT;
681 }
682
Matt Carlson0d3031d2007-10-10 18:02:43 -0700683 off = 4 * locknum;
684
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000685 tg3_ape_write32(tp, req + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700686
687 /* Wait for up to 1 millisecond to acquire lock. */
688 for (i = 0; i < 100; i++) {
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000689 status = tg3_ape_read32(tp, gnt + off);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000690 if (status == bit)
Matt Carlson0d3031d2007-10-10 18:02:43 -0700691 break;
692 udelay(10);
693 }
694
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000695 if (status != bit) {
Matt Carlson0d3031d2007-10-10 18:02:43 -0700696 /* Revoke the lock request. */
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000697 tg3_ape_write32(tp, gnt + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700698 ret = -EBUSY;
699 }
700
701 return ret;
702}
703
704static void tg3_ape_unlock(struct tg3 *tp, int locknum)
705{
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000706 u32 gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700707
Joe Perches63c3a662011-04-26 08:12:10 +0000708 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700709 return;
710
711 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000712 case TG3_APE_LOCK_GPIO:
713 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
714 return;
Matt Carlson33f401a2010-04-05 10:19:27 +0000715 case TG3_APE_LOCK_GRC:
716 case TG3_APE_LOCK_MEM:
Matt Carlson78f94dc2011-11-04 09:14:58 +0000717 if (!tp->pci_fn)
718 bit = APE_LOCK_GRANT_DRIVER;
719 else
720 bit = 1 << tp->pci_fn;
Matt Carlson33f401a2010-04-05 10:19:27 +0000721 break;
722 default:
723 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700724 }
725
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000726 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
727 gnt = TG3_APE_LOCK_GRANT;
728 else
729 gnt = TG3_APE_PER_LOCK_GRANT;
730
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000731 tg3_ape_write32(tp, gnt + 4 * locknum, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700732}
733
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000734static void tg3_ape_send_event(struct tg3 *tp, u32 event)
735{
736 int i;
737 u32 apedata;
738
739 /* NCSI does not support APE events */
740 if (tg3_flag(tp, APE_HAS_NCSI))
741 return;
742
743 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
744 if (apedata != APE_SEG_SIG_MAGIC)
745 return;
746
747 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
748 if (!(apedata & APE_FW_STATUS_READY))
749 return;
750
751 /* Wait for up to 1 millisecond for APE to service previous event. */
752 for (i = 0; i < 10; i++) {
753 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
754 return;
755
756 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
757
758 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
759 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
760 event | APE_EVENT_STATUS_EVENT_PENDING);
761
762 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
763
764 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
765 break;
766
767 udelay(100);
768 }
769
770 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
771 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
772}
773
774static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
775{
776 u32 event;
777 u32 apedata;
778
779 if (!tg3_flag(tp, ENABLE_APE))
780 return;
781
782 switch (kind) {
783 case RESET_KIND_INIT:
784 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
785 APE_HOST_SEG_SIG_MAGIC);
786 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
787 APE_HOST_SEG_LEN_MAGIC);
788 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
789 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
790 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
791 APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
792 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
793 APE_HOST_BEHAV_NO_PHYLOCK);
794 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
795 TG3_APE_HOST_DRVR_STATE_START);
796
797 event = APE_EVENT_STATUS_STATE_START;
798 break;
799 case RESET_KIND_SHUTDOWN:
800 /* With the interface we are currently using,
801 * APE does not track driver state. Wiping
802 * out the HOST SEGMENT SIGNATURE forces
803 * the APE to assume OS absent status.
804 */
805 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
806
807 if (device_may_wakeup(&tp->pdev->dev) &&
808 tg3_flag(tp, WOL_ENABLE)) {
809 tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
810 TG3_APE_HOST_WOL_SPEED_AUTO);
811 apedata = TG3_APE_HOST_DRVR_STATE_WOL;
812 } else
813 apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
814
815 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
816
817 event = APE_EVENT_STATUS_STATE_UNLOAD;
818 break;
819 case RESET_KIND_SUSPEND:
820 event = APE_EVENT_STATUS_STATE_SUSPEND;
821 break;
822 default:
823 return;
824 }
825
826 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
827
828 tg3_ape_send_event(tp, event);
829}
830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831static void tg3_disable_ints(struct tg3 *tp)
832{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000833 int i;
834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 tw32(TG3PCI_MISC_HOST_CTRL,
836 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000837 for (i = 0; i < tp->irq_max; i++)
838 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839}
840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841static void tg3_enable_ints(struct tg3 *tp)
842{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000843 int i;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000844
Michael Chanbbe832c2005-06-24 20:20:04 -0700845 tp->irq_sync = 0;
846 wmb();
847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 tw32(TG3PCI_MISC_HOST_CTRL,
849 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000850
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000851 tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000852 for (i = 0; i < tp->irq_cnt; i++) {
853 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000854
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000855 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
Joe Perches63c3a662011-04-26 08:12:10 +0000856 if (tg3_flag(tp, 1SHOT_MSI))
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000857 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
858
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000859 tp->coal_now |= tnapi->coal_now;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000860 }
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000861
862 /* Force an initial interrupt */
Joe Perches63c3a662011-04-26 08:12:10 +0000863 if (!tg3_flag(tp, TAGGED_STATUS) &&
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000864 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
865 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
866 else
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000867 tw32(HOSTCC_MODE, tp->coal_now);
868
869 tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870}
871
Matt Carlson17375d22009-08-28 14:02:18 +0000872static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
Michael Chan04237dd2005-04-25 15:17:17 -0700873{
Matt Carlson17375d22009-08-28 14:02:18 +0000874 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +0000875 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan04237dd2005-04-25 15:17:17 -0700876 unsigned int work_exists = 0;
877
878 /* check for phy events */
Joe Perches63c3a662011-04-26 08:12:10 +0000879 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Michael Chan04237dd2005-04-25 15:17:17 -0700880 if (sblk->status & SD_STATUS_LINK_CHG)
881 work_exists = 1;
882 }
883 /* check for RX/TX work to do */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000884 if (sblk->idx[0].tx_consumer != tnapi->tx_cons ||
Matt Carlson8d9d7cf2009-09-01 13:19:05 +0000885 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -0700886 work_exists = 1;
887
888 return work_exists;
889}
890
Matt Carlson17375d22009-08-28 14:02:18 +0000891/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -0700892 * similar to tg3_enable_ints, but it accurately determines whether there
893 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400894 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 */
Matt Carlson17375d22009-08-28 14:02:18 +0000896static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
Matt Carlson17375d22009-08-28 14:02:18 +0000898 struct tg3 *tp = tnapi->tp;
899
Matt Carlson898a56f2009-08-28 14:02:40 +0000900 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 mmiowb();
902
David S. Millerfac9b832005-05-18 22:46:34 -0700903 /* When doing tagged status, this work check is unnecessary.
904 * The last_tag we write above tells the chip which piece of
905 * work we've completed.
906 */
Joe Perches63c3a662011-04-26 08:12:10 +0000907 if (!tg3_flag(tp, TAGGED_STATUS) && tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -0700908 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +0000909 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910}
911
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912static void tg3_switch_clocks(struct tg3 *tp)
913{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000914 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 u32 orig_clock_ctrl;
916
Joe Perches63c3a662011-04-26 08:12:10 +0000917 if (tg3_flag(tp, CPMU_PRESENT) || tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -0700918 return;
919
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000920 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
921
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 orig_clock_ctrl = clock_ctrl;
923 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
924 CLOCK_CTRL_CLKRUN_OENABLE |
925 0x1f);
926 tp->pci_clock_ctrl = clock_ctrl;
927
Joe Perches63c3a662011-04-26 08:12:10 +0000928 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800930 tw32_wait_f(TG3PCI_CLOCK_CTRL,
931 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 }
933 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800934 tw32_wait_f(TG3PCI_CLOCK_CTRL,
935 clock_ctrl |
936 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
937 40);
938 tw32_wait_f(TG3PCI_CLOCK_CTRL,
939 clock_ctrl | (CLOCK_CTRL_ALTCLK),
940 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 }
Michael Chanb401e9e2005-12-19 16:27:04 -0800942 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943}
944
945#define PHY_BUSY_LOOPS 5000
946
947static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
948{
949 u32 frame_val;
950 unsigned int loops;
951 int ret;
952
953 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
954 tw32_f(MAC_MI_MODE,
955 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
956 udelay(80);
957 }
958
959 *val = 0x0;
960
Matt Carlson882e9792009-09-01 13:21:36 +0000961 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 MI_COM_PHY_ADDR_MASK);
963 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
964 MI_COM_REG_ADDR_MASK);
965 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 tw32_f(MAC_MI_COM, frame_val);
968
969 loops = PHY_BUSY_LOOPS;
970 while (loops != 0) {
971 udelay(10);
972 frame_val = tr32(MAC_MI_COM);
973
974 if ((frame_val & MI_COM_BUSY) == 0) {
975 udelay(5);
976 frame_val = tr32(MAC_MI_COM);
977 break;
978 }
979 loops -= 1;
980 }
981
982 ret = -EBUSY;
983 if (loops != 0) {
984 *val = frame_val & MI_COM_DATA_MASK;
985 ret = 0;
986 }
987
988 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
989 tw32_f(MAC_MI_MODE, tp->mi_mode);
990 udelay(80);
991 }
992
993 return ret;
994}
995
996static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
997{
998 u32 frame_val;
999 unsigned int loops;
1000 int ret;
1001
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001002 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson221c5632011-06-13 13:39:01 +00001003 (reg == MII_CTRL1000 || reg == MII_TG3_AUX_CTRL))
Michael Chanb5d37722006-09-27 16:06:21 -07001004 return 0;
1005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1007 tw32_f(MAC_MI_MODE,
1008 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
1009 udelay(80);
1010 }
1011
Matt Carlson882e9792009-09-01 13:21:36 +00001012 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 MI_COM_PHY_ADDR_MASK);
1014 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
1015 MI_COM_REG_ADDR_MASK);
1016 frame_val |= (val & MI_COM_DATA_MASK);
1017 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 tw32_f(MAC_MI_COM, frame_val);
1020
1021 loops = PHY_BUSY_LOOPS;
1022 while (loops != 0) {
1023 udelay(10);
1024 frame_val = tr32(MAC_MI_COM);
1025 if ((frame_val & MI_COM_BUSY) == 0) {
1026 udelay(5);
1027 frame_val = tr32(MAC_MI_COM);
1028 break;
1029 }
1030 loops -= 1;
1031 }
1032
1033 ret = -EBUSY;
1034 if (loops != 0)
1035 ret = 0;
1036
1037 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1038 tw32_f(MAC_MI_MODE, tp->mi_mode);
1039 udelay(80);
1040 }
1041
1042 return ret;
1043}
1044
Matt Carlsonb0988c12011-04-20 07:57:39 +00001045static int tg3_phy_cl45_write(struct tg3 *tp, u32 devad, u32 addr, u32 val)
1046{
1047 int err;
1048
1049 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1050 if (err)
1051 goto done;
1052
1053 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1054 if (err)
1055 goto done;
1056
1057 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1058 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1059 if (err)
1060 goto done;
1061
1062 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, val);
1063
1064done:
1065 return err;
1066}
1067
1068static int tg3_phy_cl45_read(struct tg3 *tp, u32 devad, u32 addr, u32 *val)
1069{
1070 int err;
1071
1072 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1073 if (err)
1074 goto done;
1075
1076 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1077 if (err)
1078 goto done;
1079
1080 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1081 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1082 if (err)
1083 goto done;
1084
1085 err = tg3_readphy(tp, MII_TG3_MMD_ADDRESS, val);
1086
1087done:
1088 return err;
1089}
1090
1091static int tg3_phydsp_read(struct tg3 *tp, u32 reg, u32 *val)
1092{
1093 int err;
1094
1095 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1096 if (!err)
1097 err = tg3_readphy(tp, MII_TG3_DSP_RW_PORT, val);
1098
1099 return err;
1100}
1101
1102static int tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
1103{
1104 int err;
1105
1106 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1107 if (!err)
1108 err = tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
1109
1110 return err;
1111}
1112
Matt Carlson15ee95c2011-04-20 07:57:40 +00001113static int tg3_phy_auxctl_read(struct tg3 *tp, int reg, u32 *val)
1114{
1115 int err;
1116
1117 err = tg3_writephy(tp, MII_TG3_AUX_CTRL,
1118 (reg << MII_TG3_AUXCTL_MISC_RDSEL_SHIFT) |
1119 MII_TG3_AUXCTL_SHDWSEL_MISC);
1120 if (!err)
1121 err = tg3_readphy(tp, MII_TG3_AUX_CTRL, val);
1122
1123 return err;
1124}
1125
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001126static int tg3_phy_auxctl_write(struct tg3 *tp, int reg, u32 set)
1127{
1128 if (reg == MII_TG3_AUXCTL_SHDWSEL_MISC)
1129 set |= MII_TG3_AUXCTL_MISC_WREN;
1130
1131 return tg3_writephy(tp, MII_TG3_AUX_CTRL, set | reg);
1132}
1133
Matt Carlson1d36ba42011-04-20 07:57:42 +00001134#define TG3_PHY_AUXCTL_SMDSP_ENABLE(tp) \
1135 tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
1136 MII_TG3_AUXCTL_ACTL_SMDSP_ENA | \
1137 MII_TG3_AUXCTL_ACTL_TX_6DB)
1138
1139#define TG3_PHY_AUXCTL_SMDSP_DISABLE(tp) \
1140 tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
1141 MII_TG3_AUXCTL_ACTL_TX_6DB);
1142
Matt Carlson95e28692008-05-25 23:44:14 -07001143static int tg3_bmcr_reset(struct tg3 *tp)
1144{
1145 u32 phy_control;
1146 int limit, err;
1147
1148 /* OK, reset it, and poll the BMCR_RESET bit until it
1149 * clears or we time out.
1150 */
1151 phy_control = BMCR_RESET;
1152 err = tg3_writephy(tp, MII_BMCR, phy_control);
1153 if (err != 0)
1154 return -EBUSY;
1155
1156 limit = 5000;
1157 while (limit--) {
1158 err = tg3_readphy(tp, MII_BMCR, &phy_control);
1159 if (err != 0)
1160 return -EBUSY;
1161
1162 if ((phy_control & BMCR_RESET) == 0) {
1163 udelay(40);
1164 break;
1165 }
1166 udelay(10);
1167 }
Roel Kluind4675b52009-02-12 16:33:27 -08001168 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -07001169 return -EBUSY;
1170
1171 return 0;
1172}
1173
Matt Carlson158d7ab2008-05-29 01:37:54 -07001174static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
1175{
Francois Romieu3d165432009-01-19 16:56:50 -08001176 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001177 u32 val;
1178
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001179 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001180
1181 if (tg3_readphy(tp, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001182 val = -EIO;
1183
1184 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001185
1186 return val;
1187}
1188
1189static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
1190{
Francois Romieu3d165432009-01-19 16:56:50 -08001191 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001192 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001193
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001194 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001195
1196 if (tg3_writephy(tp, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001197 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001198
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001199 spin_unlock_bh(&tp->lock);
1200
1201 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001202}
1203
1204static int tg3_mdio_reset(struct mii_bus *bp)
1205{
1206 return 0;
1207}
1208
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001209static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -07001210{
1211 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001212 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -07001213
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001214 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001215 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001216 case PHY_ID_BCM50610:
1217 case PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001218 val = MAC_PHYCFG2_50610_LED_MODES;
1219 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001220 case PHY_ID_BCMAC131:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001221 val = MAC_PHYCFG2_AC131_LED_MODES;
1222 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001223 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001224 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
1225 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001226 case PHY_ID_RTL8201E:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001227 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
1228 break;
1229 default:
Matt Carlsona9daf362008-05-25 23:49:44 -07001230 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001231 }
1232
1233 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
1234 tw32(MAC_PHYCFG2, val);
1235
1236 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001237 val &= ~(MAC_PHYCFG1_RGMII_INT |
1238 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
1239 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001240 tw32(MAC_PHYCFG1, val);
1241
1242 return;
1243 }
1244
Joe Perches63c3a662011-04-26 08:12:10 +00001245 if (!tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001246 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
1247 MAC_PHYCFG2_FMODE_MASK_MASK |
1248 MAC_PHYCFG2_GMODE_MASK_MASK |
1249 MAC_PHYCFG2_ACT_MASK_MASK |
1250 MAC_PHYCFG2_QUAL_MASK_MASK |
1251 MAC_PHYCFG2_INBAND_ENABLE;
1252
1253 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001254
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001255 val = tr32(MAC_PHYCFG1);
1256 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
1257 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
Joe Perches63c3a662011-04-26 08:12:10 +00001258 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1259 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001260 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
Joe Perches63c3a662011-04-26 08:12:10 +00001261 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001262 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1263 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001264 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1265 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1266 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001267
Matt Carlsona9daf362008-05-25 23:49:44 -07001268 val = tr32(MAC_EXT_RGMII_MODE);
1269 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1270 MAC_RGMII_MODE_RX_QUALITY |
1271 MAC_RGMII_MODE_RX_ACTIVITY |
1272 MAC_RGMII_MODE_RX_ENG_DET |
1273 MAC_RGMII_MODE_TX_ENABLE |
1274 MAC_RGMII_MODE_TX_LOWPWR |
1275 MAC_RGMII_MODE_TX_RESET);
Joe Perches63c3a662011-04-26 08:12:10 +00001276 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1277 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001278 val |= MAC_RGMII_MODE_RX_INT_B |
1279 MAC_RGMII_MODE_RX_QUALITY |
1280 MAC_RGMII_MODE_RX_ACTIVITY |
1281 MAC_RGMII_MODE_RX_ENG_DET;
Joe Perches63c3a662011-04-26 08:12:10 +00001282 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001283 val |= MAC_RGMII_MODE_TX_ENABLE |
1284 MAC_RGMII_MODE_TX_LOWPWR |
1285 MAC_RGMII_MODE_TX_RESET;
1286 }
1287 tw32(MAC_EXT_RGMII_MODE, val);
1288}
1289
Matt Carlson158d7ab2008-05-29 01:37:54 -07001290static void tg3_mdio_start(struct tg3 *tp)
1291{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001292 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1293 tw32_f(MAC_MI_MODE, tp->mi_mode);
1294 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001295
Joe Perches63c3a662011-04-26 08:12:10 +00001296 if (tg3_flag(tp, MDIOBUS_INITED) &&
Matt Carlson9ea48182010-02-17 15:17:01 +00001297 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1298 tg3_mdio_config_5785(tp);
1299}
1300
1301static int tg3_mdio_init(struct tg3 *tp)
1302{
1303 int i;
1304 u32 reg;
1305 struct phy_device *phydev;
1306
Joe Perches63c3a662011-04-26 08:12:10 +00001307 if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson9c7df912010-06-05 17:24:36 +00001308 u32 is_serdes;
Matt Carlson882e9792009-09-01 13:21:36 +00001309
Matt Carlson69f11c92011-07-13 09:27:30 +00001310 tp->phy_addr = tp->pci_fn + 1;
Matt Carlson882e9792009-09-01 13:21:36 +00001311
Matt Carlsond1ec96a2010-01-12 10:11:38 +00001312 if (tp->pci_chip_rev_id != CHIPREV_ID_5717_A0)
1313 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1314 else
1315 is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1316 TG3_CPMU_PHY_STRAP_IS_SERDES;
Matt Carlson882e9792009-09-01 13:21:36 +00001317 if (is_serdes)
1318 tp->phy_addr += 7;
1319 } else
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001320 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001321
Matt Carlson158d7ab2008-05-29 01:37:54 -07001322 tg3_mdio_start(tp);
1323
Joe Perches63c3a662011-04-26 08:12:10 +00001324 if (!tg3_flag(tp, USE_PHYLIB) || tg3_flag(tp, MDIOBUS_INITED))
Matt Carlson158d7ab2008-05-29 01:37:54 -07001325 return 0;
1326
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001327 tp->mdio_bus = mdiobus_alloc();
1328 if (tp->mdio_bus == NULL)
1329 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001330
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001331 tp->mdio_bus->name = "tg3 mdio bus";
1332 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001333 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001334 tp->mdio_bus->priv = tp;
1335 tp->mdio_bus->parent = &tp->pdev->dev;
1336 tp->mdio_bus->read = &tg3_mdio_read;
1337 tp->mdio_bus->write = &tg3_mdio_write;
1338 tp->mdio_bus->reset = &tg3_mdio_reset;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001339 tp->mdio_bus->phy_mask = ~(1 << TG3_PHY_MII_ADDR);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001340 tp->mdio_bus->irq = &tp->mdio_irq[0];
Matt Carlson158d7ab2008-05-29 01:37:54 -07001341
1342 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001343 tp->mdio_bus->irq[i] = PHY_POLL;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001344
1345 /* The bus registration will look for all the PHYs on the mdio bus.
1346 * Unfortunately, it does not ensure the PHY is powered up before
1347 * accessing the PHY ID registers. A chip reset is the
1348 * quickest way to bring the device back to an operational state..
1349 */
1350 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1351 tg3_bmcr_reset(tp);
1352
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001353 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001354 if (i) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001355 dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001356 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001357 return i;
1358 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001359
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001360 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsona9daf362008-05-25 23:49:44 -07001361
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001362 if (!phydev || !phydev->drv) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001363 dev_warn(&tp->pdev->dev, "No PHY devices\n");
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001364 mdiobus_unregister(tp->mdio_bus);
1365 mdiobus_free(tp->mdio_bus);
1366 return -ENODEV;
1367 }
1368
1369 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001370 case PHY_ID_BCM57780:
Matt Carlson321d32a2008-11-21 17:22:19 -08001371 phydev->interface = PHY_INTERFACE_MODE_GMII;
Matt Carlsonc704dc22009-11-02 14:32:12 +00001372 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08001373 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001374 case PHY_ID_BCM50610:
1375 case PHY_ID_BCM50610M:
Matt Carlson32e5a8d2009-11-02 14:31:39 +00001376 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001377 PHY_BRCM_RX_REFCLK_UNUSED |
Matt Carlson52fae082009-11-02 14:32:38 +00001378 PHY_BRCM_DIS_TXCRXC_NOENRGY |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001379 PHY_BRCM_AUTO_PWRDWN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001380 if (tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsona9daf362008-05-25 23:49:44 -07001381 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001382 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001383 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001384 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001385 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001386 /* fallthru */
Matt Carlson6a443a02010-02-17 15:17:04 +00001387 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001388 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001389 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001390 case PHY_ID_RTL8201E:
1391 case PHY_ID_BCMAC131:
Matt Carlsona9daf362008-05-25 23:49:44 -07001392 phydev->interface = PHY_INTERFACE_MODE_MII;
Matt Carlsoncdd4e09d2009-11-02 14:31:11 +00001393 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001394 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlsona9daf362008-05-25 23:49:44 -07001395 break;
1396 }
1397
Joe Perches63c3a662011-04-26 08:12:10 +00001398 tg3_flag_set(tp, MDIOBUS_INITED);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001399
1400 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1401 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001402
1403 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001404}
1405
1406static void tg3_mdio_fini(struct tg3 *tp)
1407{
Joe Perches63c3a662011-04-26 08:12:10 +00001408 if (tg3_flag(tp, MDIOBUS_INITED)) {
1409 tg3_flag_clear(tp, MDIOBUS_INITED);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001410 mdiobus_unregister(tp->mdio_bus);
1411 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001412 }
1413}
1414
Matt Carlson95e28692008-05-25 23:44:14 -07001415/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001416static inline void tg3_generate_fw_event(struct tg3 *tp)
1417{
1418 u32 val;
1419
1420 val = tr32(GRC_RX_CPU_EVENT);
1421 val |= GRC_RX_CPU_DRIVER_EVENT;
1422 tw32_f(GRC_RX_CPU_EVENT, val);
1423
1424 tp->last_event_jiffies = jiffies;
1425}
1426
1427#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1428
1429/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001430static void tg3_wait_for_event_ack(struct tg3 *tp)
1431{
1432 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001433 unsigned int delay_cnt;
1434 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001435
Matt Carlson4ba526c2008-08-15 14:10:04 -07001436 /* If enough time has passed, no wait is necessary. */
1437 time_remain = (long)(tp->last_event_jiffies + 1 +
1438 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1439 (long)jiffies;
1440 if (time_remain < 0)
1441 return;
1442
1443 /* Check if we can shorten the wait time. */
1444 delay_cnt = jiffies_to_usecs(time_remain);
1445 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1446 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1447 delay_cnt = (delay_cnt >> 3) + 1;
1448
1449 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001450 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1451 break;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001452 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001453 }
1454}
1455
1456/* tp->lock is held. */
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 Carlson80096062010-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 Carlson085f1af2012-04-02 09:01:40 +00002782 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)) ||
2783 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 &&
2784 !tp->pci_fn))
Michael Chan15c3b692006-03-22 01:06:52 -08002785 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08002786
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002787 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2788 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002789 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2790 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2791 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2792 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2793 }
2794
Michael Chan15c3b692006-03-22 01:06:52 -08002795 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2796}
2797
Matt Carlson3f007892008-11-03 16:51:36 -08002798/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002799static int tg3_nvram_lock(struct tg3 *tp)
2800{
Joe Perches63c3a662011-04-26 08:12:10 +00002801 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002802 int i;
2803
2804 if (tp->nvram_lock_cnt == 0) {
2805 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2806 for (i = 0; i < 8000; i++) {
2807 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2808 break;
2809 udelay(20);
2810 }
2811 if (i == 8000) {
2812 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2813 return -ENODEV;
2814 }
2815 }
2816 tp->nvram_lock_cnt++;
2817 }
2818 return 0;
2819}
2820
2821/* tp->lock is held. */
2822static void tg3_nvram_unlock(struct tg3 *tp)
2823{
Joe Perches63c3a662011-04-26 08:12:10 +00002824 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002825 if (tp->nvram_lock_cnt > 0)
2826 tp->nvram_lock_cnt--;
2827 if (tp->nvram_lock_cnt == 0)
2828 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
2829 }
2830}
2831
2832/* tp->lock is held. */
2833static void tg3_enable_nvram_access(struct tg3 *tp)
2834{
Joe Perches63c3a662011-04-26 08:12:10 +00002835 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002836 u32 nvaccess = tr32(NVRAM_ACCESS);
2837
2838 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
2839 }
2840}
2841
2842/* tp->lock is held. */
2843static void tg3_disable_nvram_access(struct tg3 *tp)
2844{
Joe Perches63c3a662011-04-26 08:12:10 +00002845 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002846 u32 nvaccess = tr32(NVRAM_ACCESS);
2847
2848 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
2849 }
2850}
2851
2852static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
2853 u32 offset, u32 *val)
2854{
2855 u32 tmp;
2856 int i;
2857
2858 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
2859 return -EINVAL;
2860
2861 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
2862 EEPROM_ADDR_DEVID_MASK |
2863 EEPROM_ADDR_READ);
2864 tw32(GRC_EEPROM_ADDR,
2865 tmp |
2866 (0 << EEPROM_ADDR_DEVID_SHIFT) |
2867 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
2868 EEPROM_ADDR_ADDR_MASK) |
2869 EEPROM_ADDR_READ | EEPROM_ADDR_START);
2870
2871 for (i = 0; i < 1000; i++) {
2872 tmp = tr32(GRC_EEPROM_ADDR);
2873
2874 if (tmp & EEPROM_ADDR_COMPLETE)
2875 break;
2876 msleep(1);
2877 }
2878 if (!(tmp & EEPROM_ADDR_COMPLETE))
2879 return -EBUSY;
2880
Matt Carlson62cedd12009-04-20 14:52:29 -07002881 tmp = tr32(GRC_EEPROM_DATA);
2882
2883 /*
2884 * The data will always be opposite the native endian
2885 * format. Perform a blind byteswap to compensate.
2886 */
2887 *val = swab32(tmp);
2888
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002889 return 0;
2890}
2891
2892#define NVRAM_CMD_TIMEOUT 10000
2893
2894static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
2895{
2896 int i;
2897
2898 tw32(NVRAM_CMD, nvram_cmd);
2899 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
2900 udelay(10);
2901 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
2902 udelay(10);
2903 break;
2904 }
2905 }
2906
2907 if (i == NVRAM_CMD_TIMEOUT)
2908 return -EBUSY;
2909
2910 return 0;
2911}
2912
2913static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
2914{
Joe Perches63c3a662011-04-26 08:12:10 +00002915 if (tg3_flag(tp, NVRAM) &&
2916 tg3_flag(tp, NVRAM_BUFFERED) &&
2917 tg3_flag(tp, FLASH) &&
2918 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002919 (tp->nvram_jedecnum == JEDEC_ATMEL))
2920
2921 addr = ((addr / tp->nvram_pagesize) <<
2922 ATMEL_AT45DB0X1B_PAGE_POS) +
2923 (addr % tp->nvram_pagesize);
2924
2925 return addr;
2926}
2927
2928static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
2929{
Joe Perches63c3a662011-04-26 08:12:10 +00002930 if (tg3_flag(tp, NVRAM) &&
2931 tg3_flag(tp, NVRAM_BUFFERED) &&
2932 tg3_flag(tp, FLASH) &&
2933 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002934 (tp->nvram_jedecnum == JEDEC_ATMEL))
2935
2936 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
2937 tp->nvram_pagesize) +
2938 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
2939
2940 return addr;
2941}
2942
Matt Carlsone4f34112009-02-25 14:25:00 +00002943/* NOTE: Data read in from NVRAM is byteswapped according to
2944 * the byteswapping settings for all other register accesses.
2945 * tg3 devices are BE devices, so on a BE machine, the data
2946 * returned will be exactly as it is seen in NVRAM. On a LE
2947 * machine, the 32-bit value will be byteswapped.
2948 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002949static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
2950{
2951 int ret;
2952
Joe Perches63c3a662011-04-26 08:12:10 +00002953 if (!tg3_flag(tp, NVRAM))
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002954 return tg3_nvram_read_using_eeprom(tp, offset, val);
2955
2956 offset = tg3_nvram_phys_addr(tp, offset);
2957
2958 if (offset > NVRAM_ADDR_MSK)
2959 return -EINVAL;
2960
2961 ret = tg3_nvram_lock(tp);
2962 if (ret)
2963 return ret;
2964
2965 tg3_enable_nvram_access(tp);
2966
2967 tw32(NVRAM_ADDR, offset);
2968 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
2969 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
2970
2971 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00002972 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002973
2974 tg3_disable_nvram_access(tp);
2975
2976 tg3_nvram_unlock(tp);
2977
2978 return ret;
2979}
2980
Matt Carlsona9dc5292009-02-25 14:25:30 +00002981/* Ensures NVRAM data is in bytestream format. */
2982static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002983{
2984 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00002985 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002986 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00002987 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002988 return res;
2989}
2990
Matt Carlsondbe9b922012-02-13 10:20:09 +00002991static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
2992 u32 offset, u32 len, u8 *buf)
2993{
2994 int i, j, rc = 0;
2995 u32 val;
2996
2997 for (i = 0; i < len; i += 4) {
2998 u32 addr;
2999 __be32 data;
3000
3001 addr = offset + i;
3002
3003 memcpy(&data, buf + i, 4);
3004
3005 /*
3006 * The SEEPROM interface expects the data to always be opposite
3007 * the native endian format. We accomplish this by reversing
3008 * all the operations that would have been performed on the
3009 * data from a call to tg3_nvram_read_be32().
3010 */
3011 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
3012
3013 val = tr32(GRC_EEPROM_ADDR);
3014 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
3015
3016 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
3017 EEPROM_ADDR_READ);
3018 tw32(GRC_EEPROM_ADDR, val |
3019 (0 << EEPROM_ADDR_DEVID_SHIFT) |
3020 (addr & EEPROM_ADDR_ADDR_MASK) |
3021 EEPROM_ADDR_START |
3022 EEPROM_ADDR_WRITE);
3023
3024 for (j = 0; j < 1000; j++) {
3025 val = tr32(GRC_EEPROM_ADDR);
3026
3027 if (val & EEPROM_ADDR_COMPLETE)
3028 break;
3029 msleep(1);
3030 }
3031 if (!(val & EEPROM_ADDR_COMPLETE)) {
3032 rc = -EBUSY;
3033 break;
3034 }
3035 }
3036
3037 return rc;
3038}
3039
3040/* offset and length are dword aligned */
3041static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
3042 u8 *buf)
3043{
3044 int ret = 0;
3045 u32 pagesize = tp->nvram_pagesize;
3046 u32 pagemask = pagesize - 1;
3047 u32 nvram_cmd;
3048 u8 *tmp;
3049
3050 tmp = kmalloc(pagesize, GFP_KERNEL);
3051 if (tmp == NULL)
3052 return -ENOMEM;
3053
3054 while (len) {
3055 int j;
3056 u32 phy_addr, page_off, size;
3057
3058 phy_addr = offset & ~pagemask;
3059
3060 for (j = 0; j < pagesize; j += 4) {
3061 ret = tg3_nvram_read_be32(tp, phy_addr + j,
3062 (__be32 *) (tmp + j));
3063 if (ret)
3064 break;
3065 }
3066 if (ret)
3067 break;
3068
3069 page_off = offset & pagemask;
3070 size = pagesize;
3071 if (len < size)
3072 size = len;
3073
3074 len -= size;
3075
3076 memcpy(tmp + page_off, buf, size);
3077
3078 offset = offset + (pagesize - page_off);
3079
3080 tg3_enable_nvram_access(tp);
3081
3082 /*
3083 * Before we can erase the flash page, we need
3084 * to issue a special "write enable" command.
3085 */
3086 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3087
3088 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3089 break;
3090
3091 /* Erase the target page */
3092 tw32(NVRAM_ADDR, phy_addr);
3093
3094 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
3095 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
3096
3097 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3098 break;
3099
3100 /* Issue another write enable to start the write. */
3101 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3102
3103 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3104 break;
3105
3106 for (j = 0; j < pagesize; j += 4) {
3107 __be32 data;
3108
3109 data = *((__be32 *) (tmp + j));
3110
3111 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3112
3113 tw32(NVRAM_ADDR, phy_addr + j);
3114
3115 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
3116 NVRAM_CMD_WR;
3117
3118 if (j == 0)
3119 nvram_cmd |= NVRAM_CMD_FIRST;
3120 else if (j == (pagesize - 4))
3121 nvram_cmd |= NVRAM_CMD_LAST;
3122
3123 ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
3124 if (ret)
3125 break;
3126 }
3127 if (ret)
3128 break;
3129 }
3130
3131 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3132 tg3_nvram_exec_cmd(tp, nvram_cmd);
3133
3134 kfree(tmp);
3135
3136 return ret;
3137}
3138
3139/* offset and length are dword aligned */
3140static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
3141 u8 *buf)
3142{
3143 int i, ret = 0;
3144
3145 for (i = 0; i < len; i += 4, offset += 4) {
3146 u32 page_off, phy_addr, nvram_cmd;
3147 __be32 data;
3148
3149 memcpy(&data, buf + i, 4);
3150 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3151
3152 page_off = offset % tp->nvram_pagesize;
3153
3154 phy_addr = tg3_nvram_phys_addr(tp, offset);
3155
Matt Carlsondbe9b922012-02-13 10:20:09 +00003156 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
3157
3158 if (page_off == 0 || i == 0)
3159 nvram_cmd |= NVRAM_CMD_FIRST;
3160 if (page_off == (tp->nvram_pagesize - 4))
3161 nvram_cmd |= NVRAM_CMD_LAST;
3162
3163 if (i == (len - 4))
3164 nvram_cmd |= NVRAM_CMD_LAST;
3165
Matt Carlson42278222012-02-13 15:20:11 +00003166 if ((nvram_cmd & NVRAM_CMD_FIRST) ||
3167 !tg3_flag(tp, FLASH) ||
3168 !tg3_flag(tp, 57765_PLUS))
3169 tw32(NVRAM_ADDR, phy_addr);
3170
Matt Carlsondbe9b922012-02-13 10:20:09 +00003171 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
3172 !tg3_flag(tp, 5755_PLUS) &&
3173 (tp->nvram_jedecnum == JEDEC_ST) &&
3174 (nvram_cmd & NVRAM_CMD_FIRST)) {
3175 u32 cmd;
3176
3177 cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3178 ret = tg3_nvram_exec_cmd(tp, cmd);
3179 if (ret)
3180 break;
3181 }
3182 if (!tg3_flag(tp, FLASH)) {
3183 /* We always do complete word writes to eeprom. */
3184 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
3185 }
3186
3187 ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
3188 if (ret)
3189 break;
3190 }
3191 return ret;
3192}
3193
3194/* offset and length are dword aligned */
3195static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
3196{
3197 int ret;
3198
3199 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
3200 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
3201 ~GRC_LCLCTRL_GPIO_OUTPUT1);
3202 udelay(40);
3203 }
3204
3205 if (!tg3_flag(tp, NVRAM)) {
3206 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
3207 } else {
3208 u32 grc_mode;
3209
3210 ret = tg3_nvram_lock(tp);
3211 if (ret)
3212 return ret;
3213
3214 tg3_enable_nvram_access(tp);
3215 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM))
3216 tw32(NVRAM_WRITE1, 0x406);
3217
3218 grc_mode = tr32(GRC_MODE);
3219 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
3220
3221 if (tg3_flag(tp, NVRAM_BUFFERED) || !tg3_flag(tp, FLASH)) {
3222 ret = tg3_nvram_write_block_buffered(tp, offset, len,
3223 buf);
3224 } else {
3225 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
3226 buf);
3227 }
3228
3229 grc_mode = tr32(GRC_MODE);
3230 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
3231
3232 tg3_disable_nvram_access(tp);
3233 tg3_nvram_unlock(tp);
3234 }
3235
3236 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
3237 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
3238 udelay(40);
3239 }
3240
3241 return ret;
3242}
3243
Matt Carlson997b4f12011-08-31 11:44:53 +00003244#define RX_CPU_SCRATCH_BASE 0x30000
3245#define RX_CPU_SCRATCH_SIZE 0x04000
3246#define TX_CPU_SCRATCH_BASE 0x34000
3247#define TX_CPU_SCRATCH_SIZE 0x04000
3248
3249/* tp->lock is held. */
3250static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
3251{
3252 int i;
3253
3254 BUG_ON(offset == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS));
3255
3256 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
3257 u32 val = tr32(GRC_VCPU_EXT_CTRL);
3258
3259 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
3260 return 0;
3261 }
3262 if (offset == RX_CPU_BASE) {
3263 for (i = 0; i < 10000; i++) {
3264 tw32(offset + CPU_STATE, 0xffffffff);
3265 tw32(offset + CPU_MODE, CPU_MODE_HALT);
3266 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
3267 break;
3268 }
3269
3270 tw32(offset + CPU_STATE, 0xffffffff);
3271 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
3272 udelay(10);
3273 } else {
3274 for (i = 0; i < 10000; i++) {
3275 tw32(offset + CPU_STATE, 0xffffffff);
3276 tw32(offset + CPU_MODE, CPU_MODE_HALT);
3277 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
3278 break;
3279 }
3280 }
3281
3282 if (i >= 10000) {
3283 netdev_err(tp->dev, "%s timed out, %s CPU\n",
3284 __func__, offset == RX_CPU_BASE ? "RX" : "TX");
3285 return -ENODEV;
3286 }
3287
3288 /* Clear firmware's nvram arbitration. */
3289 if (tg3_flag(tp, NVRAM))
3290 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
3291 return 0;
3292}
3293
3294struct fw_info {
3295 unsigned int fw_base;
3296 unsigned int fw_len;
3297 const __be32 *fw_data;
3298};
3299
3300/* tp->lock is held. */
3301static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base,
3302 u32 cpu_scratch_base, int cpu_scratch_size,
3303 struct fw_info *info)
3304{
3305 int err, lock_err, i;
3306 void (*write_op)(struct tg3 *, u32, u32);
3307
3308 if (cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS)) {
3309 netdev_err(tp->dev,
3310 "%s: Trying to load TX cpu firmware which is 5705\n",
3311 __func__);
3312 return -EINVAL;
3313 }
3314
3315 if (tg3_flag(tp, 5705_PLUS))
3316 write_op = tg3_write_mem;
3317 else
3318 write_op = tg3_write_indirect_reg32;
3319
3320 /* It is possible that bootcode is still loading at this point.
3321 * Get the nvram lock first before halting the cpu.
3322 */
3323 lock_err = tg3_nvram_lock(tp);
3324 err = tg3_halt_cpu(tp, cpu_base);
3325 if (!lock_err)
3326 tg3_nvram_unlock(tp);
3327 if (err)
3328 goto out;
3329
3330 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
3331 write_op(tp, cpu_scratch_base + i, 0);
3332 tw32(cpu_base + CPU_STATE, 0xffffffff);
3333 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
3334 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
3335 write_op(tp, (cpu_scratch_base +
3336 (info->fw_base & 0xffff) +
3337 (i * sizeof(u32))),
3338 be32_to_cpu(info->fw_data[i]));
3339
3340 err = 0;
3341
3342out:
3343 return err;
3344}
3345
3346/* tp->lock is held. */
3347static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
3348{
3349 struct fw_info info;
3350 const __be32 *fw_data;
3351 int err, i;
3352
3353 fw_data = (void *)tp->fw->data;
3354
3355 /* Firmware blob starts with version numbers, followed by
3356 start address and length. We are setting complete length.
3357 length = end_address_of_bss - start_address_of_text.
3358 Remainder is the blob to be loaded contiguously
3359 from start address. */
3360
3361 info.fw_base = be32_to_cpu(fw_data[1]);
3362 info.fw_len = tp->fw->size - 12;
3363 info.fw_data = &fw_data[3];
3364
3365 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
3366 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
3367 &info);
3368 if (err)
3369 return err;
3370
3371 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
3372 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
3373 &info);
3374 if (err)
3375 return err;
3376
3377 /* Now startup only the RX cpu. */
3378 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3379 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
3380
3381 for (i = 0; i < 5; i++) {
3382 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
3383 break;
3384 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3385 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
3386 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
3387 udelay(1000);
3388 }
3389 if (i >= 5) {
3390 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
3391 "should be %08x\n", __func__,
3392 tr32(RX_CPU_BASE + CPU_PC), info.fw_base);
3393 return -ENODEV;
3394 }
3395 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3396 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
3397
3398 return 0;
3399}
3400
3401/* tp->lock is held. */
3402static int tg3_load_tso_firmware(struct tg3 *tp)
3403{
3404 struct fw_info info;
3405 const __be32 *fw_data;
3406 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
3407 int err, i;
3408
3409 if (tg3_flag(tp, HW_TSO_1) ||
3410 tg3_flag(tp, HW_TSO_2) ||
3411 tg3_flag(tp, HW_TSO_3))
3412 return 0;
3413
3414 fw_data = (void *)tp->fw->data;
3415
3416 /* Firmware blob starts with version numbers, followed by
3417 start address and length. We are setting complete length.
3418 length = end_address_of_bss - start_address_of_text.
3419 Remainder is the blob to be loaded contiguously
3420 from start address. */
3421
3422 info.fw_base = be32_to_cpu(fw_data[1]);
3423 cpu_scratch_size = tp->fw_len;
3424 info.fw_len = tp->fw->size - 12;
3425 info.fw_data = &fw_data[3];
3426
3427 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
3428 cpu_base = RX_CPU_BASE;
3429 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
3430 } else {
3431 cpu_base = TX_CPU_BASE;
3432 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
3433 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
3434 }
3435
3436 err = tg3_load_firmware_cpu(tp, cpu_base,
3437 cpu_scratch_base, cpu_scratch_size,
3438 &info);
3439 if (err)
3440 return err;
3441
3442 /* Now startup the cpu. */
3443 tw32(cpu_base + CPU_STATE, 0xffffffff);
3444 tw32_f(cpu_base + CPU_PC, info.fw_base);
3445
3446 for (i = 0; i < 5; i++) {
3447 if (tr32(cpu_base + CPU_PC) == info.fw_base)
3448 break;
3449 tw32(cpu_base + CPU_STATE, 0xffffffff);
3450 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
3451 tw32_f(cpu_base + CPU_PC, info.fw_base);
3452 udelay(1000);
3453 }
3454 if (i >= 5) {
3455 netdev_err(tp->dev,
3456 "%s fails to set CPU PC, is %08x should be %08x\n",
3457 __func__, tr32(cpu_base + CPU_PC), info.fw_base);
3458 return -ENODEV;
3459 }
3460 tw32(cpu_base + CPU_STATE, 0xffffffff);
3461 tw32_f(cpu_base + CPU_MODE, 0x00000000);
3462 return 0;
3463}
3464
3465
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003466/* tp->lock is held. */
Matt Carlson3f007892008-11-03 16:51:36 -08003467static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
3468{
3469 u32 addr_high, addr_low;
3470 int i;
3471
3472 addr_high = ((tp->dev->dev_addr[0] << 8) |
3473 tp->dev->dev_addr[1]);
3474 addr_low = ((tp->dev->dev_addr[2] << 24) |
3475 (tp->dev->dev_addr[3] << 16) |
3476 (tp->dev->dev_addr[4] << 8) |
3477 (tp->dev->dev_addr[5] << 0));
3478 for (i = 0; i < 4; i++) {
3479 if (i == 1 && skip_mac_1)
3480 continue;
3481 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
3482 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
3483 }
3484
3485 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3486 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
3487 for (i = 0; i < 12; i++) {
3488 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
3489 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
3490 }
3491 }
3492
3493 addr_high = (tp->dev->dev_addr[0] +
3494 tp->dev->dev_addr[1] +
3495 tp->dev->dev_addr[2] +
3496 tp->dev->dev_addr[3] +
3497 tp->dev->dev_addr[4] +
3498 tp->dev->dev_addr[5]) &
3499 TX_BACKOFF_SEED_MASK;
3500 tw32(MAC_TX_BACKOFF_SEED, addr_high);
3501}
3502
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003503static void tg3_enable_register_access(struct tg3 *tp)
3504{
3505 /*
3506 * Make sure register accesses (indirect or otherwise) will function
3507 * correctly.
3508 */
3509 pci_write_config_dword(tp->pdev,
3510 TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
3511}
3512
3513static int tg3_power_up(struct tg3 *tp)
3514{
Matt Carlsonbed98292011-07-13 09:27:29 +00003515 int err;
3516
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003517 tg3_enable_register_access(tp);
3518
Matt Carlsonbed98292011-07-13 09:27:29 +00003519 err = pci_set_power_state(tp->pdev, PCI_D0);
3520 if (!err) {
3521 /* Switch out of Vaux if it is a NIC */
3522 tg3_pwrsrc_switch_to_vmain(tp);
3523 } else {
3524 netdev_err(tp->dev, "Transition to D0 failed\n");
3525 }
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003526
Matt Carlsonbed98292011-07-13 09:27:29 +00003527 return err;
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003528}
3529
Matt Carlson4b409522012-02-13 10:20:11 +00003530static int tg3_setup_phy(struct tg3 *, int);
3531
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003532static int tg3_power_down_prepare(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533{
3534 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003535 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003537 tg3_enable_register_access(tp);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003538
3539 /* Restore the CLKREQ setting. */
Joe Perches63c3a662011-04-26 08:12:10 +00003540 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003541 u16 lnkctl;
3542
3543 pci_read_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +00003544 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003545 &lnkctl);
3546 lnkctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
3547 pci_write_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +00003548 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003549 lnkctl);
3550 }
3551
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
3553 tw32(TG3PCI_MISC_HOST_CTRL,
3554 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
3555
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003556 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00003557 tg3_flag(tp, WOL_ENABLE);
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003558
Joe Perches63c3a662011-04-26 08:12:10 +00003559 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08003560 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003561 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson80096062010-08-02 11:26:06 +00003562 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003563 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003564 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003565
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00003566 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003567
Matt Carlson80096062010-08-02 11:26:06 +00003568 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003569
Matt Carlsonc6700ce2012-02-13 15:20:15 +00003570 tp->link_config.speed = phydev->speed;
3571 tp->link_config.duplex = phydev->duplex;
3572 tp->link_config.autoneg = phydev->autoneg;
3573 tp->link_config.advertising = phydev->advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003574
3575 advertising = ADVERTISED_TP |
3576 ADVERTISED_Pause |
3577 ADVERTISED_Autoneg |
3578 ADVERTISED_10baseT_Half;
3579
Joe Perches63c3a662011-04-26 08:12:10 +00003580 if (tg3_flag(tp, ENABLE_ASF) || device_should_wake) {
3581 if (tg3_flag(tp, WOL_SPEED_100MB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003582 advertising |=
3583 ADVERTISED_100baseT_Half |
3584 ADVERTISED_100baseT_Full |
3585 ADVERTISED_10baseT_Full;
3586 else
3587 advertising |= ADVERTISED_10baseT_Full;
3588 }
3589
3590 phydev->advertising = advertising;
3591
3592 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08003593
3594 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00003595 if (phyid != PHY_ID_BCMAC131) {
3596 phyid &= PHY_BCM_OUI_MASK;
3597 if (phyid == PHY_BCM_OUI_1 ||
3598 phyid == PHY_BCM_OUI_2 ||
3599 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08003600 do_low_power = true;
3601 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003602 }
Matt Carlsondd477002008-05-25 23:45:58 -07003603 } else {
Matt Carlson20232762008-12-21 20:18:56 -08003604 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003605
Matt Carlsonc6700ce2012-02-13 15:20:15 +00003606 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER))
Matt Carlson80096062010-08-02 11:26:06 +00003607 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608
Matt Carlson2855b9f2012-02-13 15:20:14 +00003609 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlsondd477002008-05-25 23:45:58 -07003610 tg3_setup_phy(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 }
3612
Michael Chanb5d37722006-09-27 16:06:21 -07003613 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
3614 u32 val;
3615
3616 val = tr32(GRC_VCPU_EXT_CTRL);
3617 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
Joe Perches63c3a662011-04-26 08:12:10 +00003618 } else if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08003619 int i;
3620 u32 val;
3621
3622 for (i = 0; i < 200; i++) {
3623 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
3624 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
3625 break;
3626 msleep(1);
3627 }
3628 }
Joe Perches63c3a662011-04-26 08:12:10 +00003629 if (tg3_flag(tp, WOL_CAP))
Gary Zambranoa85feb82007-05-05 11:52:19 -07003630 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
3631 WOL_DRV_STATE_SHUTDOWN |
3632 WOL_DRV_WOL |
3633 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08003634
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003635 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 u32 mac_mode;
3637
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003638 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003639 if (do_low_power &&
3640 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
3641 tg3_phy_auxctl_write(tp,
3642 MII_TG3_AUXCTL_SHDWSEL_PWRCTL,
3643 MII_TG3_AUXCTL_PCTL_WOL_EN |
3644 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
3645 MII_TG3_AUXCTL_PCTL_CL_AB_TXDAC);
Matt Carlsondd477002008-05-25 23:45:58 -07003646 udelay(40);
3647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003649 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07003650 mac_mode = MAC_MODE_PORT_MODE_GMII;
3651 else
3652 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003654 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
3655 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
3656 ASIC_REV_5700) {
Joe Perches63c3a662011-04-26 08:12:10 +00003657 u32 speed = tg3_flag(tp, WOL_SPEED_100MB) ?
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003658 SPEED_100 : SPEED_10;
3659 if (tg3_5700_link_polarity(tp, speed))
3660 mac_mode |= MAC_MODE_LINK_POLARITY;
3661 else
3662 mac_mode &= ~MAC_MODE_LINK_POLARITY;
3663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 } else {
3665 mac_mode = MAC_MODE_PORT_MODE_TBI;
3666 }
3667
Joe Perches63c3a662011-04-26 08:12:10 +00003668 if (!tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 tw32(MAC_LED_CTRL, tp->led_ctrl);
3670
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003671 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00003672 if ((tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS)) &&
3673 (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)))
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003674 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675
Joe Perches63c3a662011-04-26 08:12:10 +00003676 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00003677 mac_mode |= MAC_MODE_APE_TX_EN |
3678 MAC_MODE_APE_RX_EN |
3679 MAC_MODE_TDE_ENABLE;
Matt Carlson3bda1252008-08-15 14:08:22 -07003680
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 tw32_f(MAC_MODE, mac_mode);
3682 udelay(100);
3683
3684 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
3685 udelay(10);
3686 }
3687
Joe Perches63c3a662011-04-26 08:12:10 +00003688 if (!tg3_flag(tp, WOL_SPEED_100MB) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3690 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
3691 u32 base_val;
3692
3693 base_val = tp->pci_clock_ctrl;
3694 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
3695 CLOCK_CTRL_TXCLK_DISABLE);
3696
Michael Chanb401e9e2005-12-19 16:27:04 -08003697 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
3698 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Joe Perches63c3a662011-04-26 08:12:10 +00003699 } else if (tg3_flag(tp, 5780_CLASS) ||
3700 tg3_flag(tp, CPMU_PRESENT) ||
Matt Carlson6ff6f812011-05-19 12:12:54 +00003701 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan4cf78e42005-07-25 12:29:19 -07003702 /* do nothing */
Joe Perches63c3a662011-04-26 08:12:10 +00003703 } else if (!(tg3_flag(tp, 5750_PLUS) && tg3_flag(tp, ENABLE_ASF))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 u32 newbits1, newbits2;
3705
3706 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3707 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3708 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
3709 CLOCK_CTRL_TXCLK_DISABLE |
3710 CLOCK_CTRL_ALTCLK);
3711 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
Joe Perches63c3a662011-04-26 08:12:10 +00003712 } else if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 newbits1 = CLOCK_CTRL_625_CORE;
3714 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
3715 } else {
3716 newbits1 = CLOCK_CTRL_ALTCLK;
3717 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
3718 }
3719
Michael Chanb401e9e2005-12-19 16:27:04 -08003720 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
3721 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722
Michael Chanb401e9e2005-12-19 16:27:04 -08003723 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
3724 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725
Joe Perches63c3a662011-04-26 08:12:10 +00003726 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 u32 newbits3;
3728
3729 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3730 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3731 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
3732 CLOCK_CTRL_TXCLK_DISABLE |
3733 CLOCK_CTRL_44MHZ_CORE);
3734 } else {
3735 newbits3 = CLOCK_CTRL_44MHZ_CORE;
3736 }
3737
Michael Chanb401e9e2005-12-19 16:27:04 -08003738 tw32_wait_f(TG3PCI_CLOCK_CTRL,
3739 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 }
3741 }
3742
Joe Perches63c3a662011-04-26 08:12:10 +00003743 if (!(device_should_wake) && !tg3_flag(tp, ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08003744 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08003745
Matt Carlsoncd0d7222011-07-13 09:27:33 +00003746 tg3_frob_aux_power(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747
3748 /* Workaround for unstable PLL clock */
3749 if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
3750 (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
3751 u32 val = tr32(0x7d00);
3752
3753 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
3754 tw32(0x7d00, val);
Joe Perches63c3a662011-04-26 08:12:10 +00003755 if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08003756 int err;
3757
3758 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08003760 if (!err)
3761 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08003762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763 }
3764
Michael Chanbbadf502006-04-06 21:46:34 -07003765 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
3766
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 return 0;
3768}
3769
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003770static void tg3_power_down(struct tg3 *tp)
3771{
3772 tg3_power_down_prepare(tp);
3773
Joe Perches63c3a662011-04-26 08:12:10 +00003774 pci_wake_from_d3(tp->pdev, tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003775 pci_set_power_state(tp->pdev, PCI_D3hot);
3776}
3777
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
3779{
3780 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
3781 case MII_TG3_AUX_STAT_10HALF:
3782 *speed = SPEED_10;
3783 *duplex = DUPLEX_HALF;
3784 break;
3785
3786 case MII_TG3_AUX_STAT_10FULL:
3787 *speed = SPEED_10;
3788 *duplex = DUPLEX_FULL;
3789 break;
3790
3791 case MII_TG3_AUX_STAT_100HALF:
3792 *speed = SPEED_100;
3793 *duplex = DUPLEX_HALF;
3794 break;
3795
3796 case MII_TG3_AUX_STAT_100FULL:
3797 *speed = SPEED_100;
3798 *duplex = DUPLEX_FULL;
3799 break;
3800
3801 case MII_TG3_AUX_STAT_1000HALF:
3802 *speed = SPEED_1000;
3803 *duplex = DUPLEX_HALF;
3804 break;
3805
3806 case MII_TG3_AUX_STAT_1000FULL:
3807 *speed = SPEED_1000;
3808 *duplex = DUPLEX_FULL;
3809 break;
3810
3811 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003812 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07003813 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
3814 SPEED_10;
3815 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
3816 DUPLEX_HALF;
3817 break;
3818 }
Matt Carlsone7405222012-02-13 15:20:16 +00003819 *speed = SPEED_UNKNOWN;
3820 *duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823}
3824
Matt Carlson42b64a42011-05-19 12:12:49 +00003825static int tg3_phy_autoneg_cfg(struct tg3 *tp, u32 advertise, u32 flowctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826{
Matt Carlson42b64a42011-05-19 12:12:49 +00003827 int err = 0;
3828 u32 val, new_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829
Matt Carlson42b64a42011-05-19 12:12:49 +00003830 new_adv = ADVERTISE_CSMA;
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +00003831 new_adv |= ethtool_adv_to_mii_adv_t(advertise) & ADVERTISE_ALL;
Matt Carlsonf88788f2011-12-14 11:10:00 +00003832 new_adv |= mii_advertise_flowctrl(flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833
Matt Carlson42b64a42011-05-19 12:12:49 +00003834 err = tg3_writephy(tp, MII_ADVERTISE, new_adv);
3835 if (err)
3836 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837
Matt Carlson4f272092011-12-14 11:09:57 +00003838 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
3839 new_adv = ethtool_adv_to_mii_ctrl1000_t(advertise);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003840
Matt Carlson4f272092011-12-14 11:09:57 +00003841 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3842 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
3843 new_adv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003844
Matt Carlson4f272092011-12-14 11:09:57 +00003845 err = tg3_writephy(tp, MII_CTRL1000, new_adv);
3846 if (err)
3847 goto done;
3848 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003849
Matt Carlson42b64a42011-05-19 12:12:49 +00003850 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
3851 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852
Matt Carlson42b64a42011-05-19 12:12:49 +00003853 tw32(TG3_CPMU_EEE_MODE,
3854 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003855
Matt Carlson42b64a42011-05-19 12:12:49 +00003856 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
3857 if (!err) {
3858 u32 err2;
Matt Carlson52b02d02010-10-14 10:37:41 +00003859
Matt Carlsona6b68da2010-12-06 08:28:52 +00003860 val = 0;
Matt Carlson42b64a42011-05-19 12:12:49 +00003861 /* Advertise 100-BaseTX EEE ability */
3862 if (advertise & ADVERTISED_100baseT_Full)
3863 val |= MDIO_AN_EEE_ADV_100TX;
3864 /* Advertise 1000-BaseT EEE ability */
3865 if (advertise & ADVERTISED_1000baseT_Full)
3866 val |= MDIO_AN_EEE_ADV_1000T;
3867 err = tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlsonb715ce92011-07-20 10:20:52 +00003868 if (err)
3869 val = 0;
3870
3871 switch (GET_ASIC_REV(tp->pci_chip_rev_id)) {
3872 case ASIC_REV_5717:
3873 case ASIC_REV_57765:
Matt Carlson55086ad2011-12-14 11:09:59 +00003874 case ASIC_REV_57766:
Matt Carlsonb715ce92011-07-20 10:20:52 +00003875 case ASIC_REV_5719:
3876 /* If we advertised any eee advertisements above... */
3877 if (val)
3878 val = MII_TG3_DSP_TAP26_ALNOKO |
3879 MII_TG3_DSP_TAP26_RMRXSTO |
3880 MII_TG3_DSP_TAP26_OPCSINPT;
3881 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
3882 /* Fall through */
3883 case ASIC_REV_5720:
3884 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
3885 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
3886 MII_TG3_DSP_CH34TP2_HIBW01);
3887 }
Matt Carlson52b02d02010-10-14 10:37:41 +00003888
Matt Carlson42b64a42011-05-19 12:12:49 +00003889 err2 = TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
3890 if (!err)
3891 err = err2;
3892 }
3893
3894done:
3895 return err;
3896}
3897
3898static void tg3_phy_copper_begin(struct tg3 *tp)
3899{
Matt Carlsond13ba512012-02-22 12:35:19 +00003900 if (tp->link_config.autoneg == AUTONEG_ENABLE ||
3901 (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
3902 u32 adv, fc;
Matt Carlson42b64a42011-05-19 12:12:49 +00003903
Matt Carlsond13ba512012-02-22 12:35:19 +00003904 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
3905 adv = ADVERTISED_10baseT_Half |
3906 ADVERTISED_10baseT_Full;
3907 if (tg3_flag(tp, WOL_SPEED_100MB))
3908 adv |= ADVERTISED_100baseT_Half |
3909 ADVERTISED_100baseT_Full;
Matt Carlson42b64a42011-05-19 12:12:49 +00003910
Matt Carlsond13ba512012-02-22 12:35:19 +00003911 fc = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson42b64a42011-05-19 12:12:49 +00003912 } else {
Matt Carlsond13ba512012-02-22 12:35:19 +00003913 adv = tp->link_config.advertising;
3914 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
3915 adv &= ~(ADVERTISED_1000baseT_Half |
3916 ADVERTISED_1000baseT_Full);
3917
3918 fc = tp->link_config.flowctrl;
Matt Carlson42b64a42011-05-19 12:12:49 +00003919 }
3920
Matt Carlsond13ba512012-02-22 12:35:19 +00003921 tg3_phy_autoneg_cfg(tp, adv, fc);
Matt Carlson52b02d02010-10-14 10:37:41 +00003922
Matt Carlsond13ba512012-02-22 12:35:19 +00003923 tg3_writephy(tp, MII_BMCR,
3924 BMCR_ANENABLE | BMCR_ANRESTART);
3925 } else {
3926 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 u32 bmcr, orig_bmcr;
3928
3929 tp->link_config.active_speed = tp->link_config.speed;
3930 tp->link_config.active_duplex = tp->link_config.duplex;
3931
3932 bmcr = 0;
3933 switch (tp->link_config.speed) {
3934 default:
3935 case SPEED_10:
3936 break;
3937
3938 case SPEED_100:
3939 bmcr |= BMCR_SPEED100;
3940 break;
3941
3942 case SPEED_1000:
Matt Carlson221c5632011-06-13 13:39:01 +00003943 bmcr |= BMCR_SPEED1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946
3947 if (tp->link_config.duplex == DUPLEX_FULL)
3948 bmcr |= BMCR_FULLDPLX;
3949
3950 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
3951 (bmcr != orig_bmcr)) {
3952 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
3953 for (i = 0; i < 1500; i++) {
3954 u32 tmp;
3955
3956 udelay(10);
3957 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
3958 tg3_readphy(tp, MII_BMSR, &tmp))
3959 continue;
3960 if (!(tmp & BMSR_LSTATUS)) {
3961 udelay(40);
3962 break;
3963 }
3964 }
3965 tg3_writephy(tp, MII_BMCR, bmcr);
3966 udelay(40);
3967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 }
3969}
3970
3971static int tg3_init_5401phy_dsp(struct tg3 *tp)
3972{
3973 int err;
3974
3975 /* Turn off tap power management. */
3976 /* Set Extended packet length bit */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003977 err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00003979 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
3980 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
3981 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
3982 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
3983 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984
3985 udelay(40);
3986
3987 return err;
3988}
3989
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003990static bool tg3_phy_copper_an_config_ok(struct tg3 *tp, u32 *lcladv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991{
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003992 u32 advmsk, tgtadv, advertising;
Michael Chan3600d912006-12-07 00:21:48 -08003993
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003994 advertising = tp->link_config.advertising;
3995 tgtadv = ethtool_adv_to_mii_adv_t(advertising) & ADVERTISE_ALL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003997 advmsk = ADVERTISE_ALL;
3998 if (tp->link_config.active_duplex == DUPLEX_FULL) {
Matt Carlsonf88788f2011-12-14 11:10:00 +00003999 tgtadv |= mii_advertise_flowctrl(tp->link_config.flowctrl);
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004000 advmsk |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
4001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004003 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
4004 return false;
4005
4006 if ((*lcladv & advmsk) != tgtadv)
4007 return false;
Matt Carlsonb99d2a52011-08-31 11:44:47 +00004008
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004009 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 u32 tg3_ctrl;
4011
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004012 tgtadv = ethtool_adv_to_mii_ctrl1000_t(advertising);
Michael Chan3600d912006-12-07 00:21:48 -08004013
Matt Carlson221c5632011-06-13 13:39:01 +00004014 if (tg3_readphy(tp, MII_CTRL1000, &tg3_ctrl))
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004015 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016
Matt Carlson3198e072012-02-13 15:20:10 +00004017 if (tgtadv &&
4018 (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
4019 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)) {
4020 tgtadv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
4021 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL |
4022 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
4023 } else {
4024 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL);
4025 }
4026
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004027 if (tg3_ctrl != tgtadv)
4028 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 }
Matt Carlson93a700a2011-08-31 11:44:54 +00004030
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004031 return true;
Matt Carlsonef167e22007-12-20 20:10:01 -08004032}
4033
Matt Carlson859edb22011-12-08 14:40:16 +00004034static bool tg3_phy_copper_fetch_rmtadv(struct tg3 *tp, u32 *rmtadv)
4035{
4036 u32 lpeth = 0;
4037
4038 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4039 u32 val;
4040
4041 if (tg3_readphy(tp, MII_STAT1000, &val))
4042 return false;
4043
4044 lpeth = mii_stat1000_to_ethtool_lpa_t(val);
4045 }
4046
4047 if (tg3_readphy(tp, MII_LPA, rmtadv))
4048 return false;
4049
4050 lpeth |= mii_lpa_to_ethtool_lpa_t(*rmtadv);
4051 tp->link_config.rmt_adv = lpeth;
4052
4053 return true;
4054}
4055
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
4057{
4058 int current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004059 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08004060 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 u16 current_speed;
4062 u8 current_duplex;
4063 int i, err;
4064
4065 tw32(MAC_EVENT, 0);
4066
4067 tw32_f(MAC_STATUS,
4068 (MAC_STATUS_SYNC_CHANGED |
4069 MAC_STATUS_CFG_CHANGED |
4070 MAC_STATUS_MI_COMPLETION |
4071 MAC_STATUS_LNKSTATE_CHANGED));
4072 udelay(40);
4073
Matt Carlson8ef21422008-05-02 16:47:53 -07004074 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
4075 tw32_f(MAC_MI_MODE,
4076 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
4077 udelay(80);
4078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004080 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081
4082 /* Some third-party PHYs need to be reset on link going
4083 * down.
4084 */
4085 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
4086 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
4087 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
4088 netif_carrier_ok(tp->dev)) {
4089 tg3_readphy(tp, MII_BMSR, &bmsr);
4090 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4091 !(bmsr & BMSR_LSTATUS))
4092 force_reset = 1;
4093 }
4094 if (force_reset)
4095 tg3_phy_reset(tp);
4096
Matt Carlson79eb6902010-02-17 15:17:03 +00004097 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098 tg3_readphy(tp, MII_BMSR, &bmsr);
4099 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
Joe Perches63c3a662011-04-26 08:12:10 +00004100 !tg3_flag(tp, INIT_COMPLETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 bmsr = 0;
4102
4103 if (!(bmsr & BMSR_LSTATUS)) {
4104 err = tg3_init_5401phy_dsp(tp);
4105 if (err)
4106 return err;
4107
4108 tg3_readphy(tp, MII_BMSR, &bmsr);
4109 for (i = 0; i < 1000; i++) {
4110 udelay(10);
4111 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4112 (bmsr & BMSR_LSTATUS)) {
4113 udelay(40);
4114 break;
4115 }
4116 }
4117
Matt Carlson79eb6902010-02-17 15:17:03 +00004118 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
4119 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 !(bmsr & BMSR_LSTATUS) &&
4121 tp->link_config.active_speed == SPEED_1000) {
4122 err = tg3_phy_reset(tp);
4123 if (!err)
4124 err = tg3_init_5401phy_dsp(tp);
4125 if (err)
4126 return err;
4127 }
4128 }
4129 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
4130 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
4131 /* 5701 {A0,B0} CRC bug workaround */
4132 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004133 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
4134 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
4135 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 }
4137
4138 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004139 tg3_readphy(tp, MII_TG3_ISTAT, &val);
4140 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004142 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004144 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 tg3_writephy(tp, MII_TG3_IMASK, ~0);
4146
4147 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
4148 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
4149 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
4150 tg3_writephy(tp, MII_TG3_EXT_CTRL,
4151 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
4152 else
4153 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
4154 }
4155
4156 current_link_up = 0;
Matt Carlsone7405222012-02-13 15:20:16 +00004157 current_speed = SPEED_UNKNOWN;
4158 current_duplex = DUPLEX_UNKNOWN;
Matt Carlsone348c5e2011-11-21 15:01:20 +00004159 tp->phy_flags &= ~TG3_PHYFLG_MDIX_STATE;
Matt Carlson859edb22011-12-08 14:40:16 +00004160 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004162 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Matt Carlson15ee95c2011-04-20 07:57:40 +00004163 err = tg3_phy_auxctl_read(tp,
4164 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4165 &val);
4166 if (!err && !(val & (1 << 10))) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004167 tg3_phy_auxctl_write(tp,
4168 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4169 val | (1 << 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 goto relink;
4171 }
4172 }
4173
4174 bmsr = 0;
4175 for (i = 0; i < 100; i++) {
4176 tg3_readphy(tp, MII_BMSR, &bmsr);
4177 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4178 (bmsr & BMSR_LSTATUS))
4179 break;
4180 udelay(40);
4181 }
4182
4183 if (bmsr & BMSR_LSTATUS) {
4184 u32 aux_stat, bmcr;
4185
4186 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
4187 for (i = 0; i < 2000; i++) {
4188 udelay(10);
4189 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
4190 aux_stat)
4191 break;
4192 }
4193
4194 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
4195 &current_speed,
4196 &current_duplex);
4197
4198 bmcr = 0;
4199 for (i = 0; i < 200; i++) {
4200 tg3_readphy(tp, MII_BMCR, &bmcr);
4201 if (tg3_readphy(tp, MII_BMCR, &bmcr))
4202 continue;
4203 if (bmcr && bmcr != 0x7fff)
4204 break;
4205 udelay(10);
4206 }
4207
Matt Carlsonef167e22007-12-20 20:10:01 -08004208 lcl_adv = 0;
4209 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210
Matt Carlsonef167e22007-12-20 20:10:01 -08004211 tp->link_config.active_speed = current_speed;
4212 tp->link_config.active_duplex = current_duplex;
4213
4214 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4215 if ((bmcr & BMCR_ANENABLE) &&
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004216 tg3_phy_copper_an_config_ok(tp, &lcl_adv) &&
Matt Carlson859edb22011-12-08 14:40:16 +00004217 tg3_phy_copper_fetch_rmtadv(tp, &rmt_adv))
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004218 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 } else {
4220 if (!(bmcr & BMCR_ANENABLE) &&
4221 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08004222 tp->link_config.duplex == current_duplex &&
4223 tp->link_config.flowctrl ==
4224 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 }
4227 }
4228
Matt Carlsonef167e22007-12-20 20:10:01 -08004229 if (current_link_up == 1 &&
Matt Carlsone348c5e2011-11-21 15:01:20 +00004230 tp->link_config.active_duplex == DUPLEX_FULL) {
4231 u32 reg, bit;
4232
4233 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
4234 reg = MII_TG3_FET_GEN_STAT;
4235 bit = MII_TG3_FET_GEN_STAT_MDIXSTAT;
4236 } else {
4237 reg = MII_TG3_EXT_STAT;
4238 bit = MII_TG3_EXT_STAT_MDIX;
4239 }
4240
4241 if (!tg3_readphy(tp, reg, &val) && (val & bit))
4242 tp->phy_flags |= TG3_PHYFLG_MDIX_STATE;
4243
Matt Carlsonef167e22007-12-20 20:10:01 -08004244 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Matt Carlsone348c5e2011-11-21 15:01:20 +00004245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 }
4247
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248relink:
Matt Carlson80096062010-08-02 11:26:06 +00004249 if (current_link_up == 0 || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 tg3_phy_copper_begin(tp);
4251
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004252 tg3_readphy(tp, MII_BMSR, &bmsr);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00004253 if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
4254 (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 current_link_up = 1;
4256 }
4257
4258 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
4259 if (current_link_up == 1) {
4260 if (tp->link_config.active_speed == SPEED_100 ||
4261 tp->link_config.active_speed == SPEED_10)
4262 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4263 else
4264 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004265 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00004266 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4267 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4269
4270 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4271 if (tp->link_config.active_duplex == DUPLEX_HALF)
4272 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4273
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004275 if (current_link_up == 1 &&
4276 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004278 else
4279 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 }
4281
4282 /* ??? Without this setting Netgear GA302T PHY does not
4283 * ??? send/receive packets...
4284 */
Matt Carlson79eb6902010-02-17 15:17:03 +00004285 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
4287 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
4288 tw32_f(MAC_MI_MODE, tp->mi_mode);
4289 udelay(80);
4290 }
4291
4292 tw32_f(MAC_MODE, tp->mac_mode);
4293 udelay(40);
4294
Matt Carlson52b02d02010-10-14 10:37:41 +00004295 tg3_phy_eee_adjust(tp, current_link_up);
4296
Joe Perches63c3a662011-04-26 08:12:10 +00004297 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 /* Polled via timer. */
4299 tw32_f(MAC_EVENT, 0);
4300 } else {
4301 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4302 }
4303 udelay(40);
4304
4305 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
4306 current_link_up == 1 &&
4307 tp->link_config.active_speed == SPEED_1000 &&
Joe Perches63c3a662011-04-26 08:12:10 +00004308 (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 udelay(120);
4310 tw32_f(MAC_STATUS,
4311 (MAC_STATUS_SYNC_CHANGED |
4312 MAC_STATUS_CFG_CHANGED));
4313 udelay(40);
4314 tg3_write_mem(tp,
4315 NIC_SRAM_FIRMWARE_MBOX,
4316 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
4317 }
4318
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004319 /* Prevent send BD corruption. */
Joe Perches63c3a662011-04-26 08:12:10 +00004320 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004321 u16 oldlnkctl, newlnkctl;
4322
4323 pci_read_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +00004324 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004325 &oldlnkctl);
4326 if (tp->link_config.active_speed == SPEED_100 ||
4327 tp->link_config.active_speed == SPEED_10)
4328 newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
4329 else
4330 newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
4331 if (newlnkctl != oldlnkctl)
4332 pci_write_config_word(tp->pdev,
Matt Carlson93a700a2011-08-31 11:44:54 +00004333 pci_pcie_cap(tp->pdev) +
4334 PCI_EXP_LNKCTL, newlnkctl);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004335 }
4336
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 if (current_link_up != netif_carrier_ok(tp->dev)) {
4338 if (current_link_up)
4339 netif_carrier_on(tp->dev);
4340 else
4341 netif_carrier_off(tp->dev);
4342 tg3_link_report(tp);
4343 }
4344
4345 return 0;
4346}
4347
4348struct tg3_fiber_aneginfo {
4349 int state;
4350#define ANEG_STATE_UNKNOWN 0
4351#define ANEG_STATE_AN_ENABLE 1
4352#define ANEG_STATE_RESTART_INIT 2
4353#define ANEG_STATE_RESTART 3
4354#define ANEG_STATE_DISABLE_LINK_OK 4
4355#define ANEG_STATE_ABILITY_DETECT_INIT 5
4356#define ANEG_STATE_ABILITY_DETECT 6
4357#define ANEG_STATE_ACK_DETECT_INIT 7
4358#define ANEG_STATE_ACK_DETECT 8
4359#define ANEG_STATE_COMPLETE_ACK_INIT 9
4360#define ANEG_STATE_COMPLETE_ACK 10
4361#define ANEG_STATE_IDLE_DETECT_INIT 11
4362#define ANEG_STATE_IDLE_DETECT 12
4363#define ANEG_STATE_LINK_OK 13
4364#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
4365#define ANEG_STATE_NEXT_PAGE_WAIT 15
4366
4367 u32 flags;
4368#define MR_AN_ENABLE 0x00000001
4369#define MR_RESTART_AN 0x00000002
4370#define MR_AN_COMPLETE 0x00000004
4371#define MR_PAGE_RX 0x00000008
4372#define MR_NP_LOADED 0x00000010
4373#define MR_TOGGLE_TX 0x00000020
4374#define MR_LP_ADV_FULL_DUPLEX 0x00000040
4375#define MR_LP_ADV_HALF_DUPLEX 0x00000080
4376#define MR_LP_ADV_SYM_PAUSE 0x00000100
4377#define MR_LP_ADV_ASYM_PAUSE 0x00000200
4378#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
4379#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
4380#define MR_LP_ADV_NEXT_PAGE 0x00001000
4381#define MR_TOGGLE_RX 0x00002000
4382#define MR_NP_RX 0x00004000
4383
4384#define MR_LINK_OK 0x80000000
4385
4386 unsigned long link_time, cur_time;
4387
4388 u32 ability_match_cfg;
4389 int ability_match_count;
4390
4391 char ability_match, idle_match, ack_match;
4392
4393 u32 txconfig, rxconfig;
4394#define ANEG_CFG_NP 0x00000080
4395#define ANEG_CFG_ACK 0x00000040
4396#define ANEG_CFG_RF2 0x00000020
4397#define ANEG_CFG_RF1 0x00000010
4398#define ANEG_CFG_PS2 0x00000001
4399#define ANEG_CFG_PS1 0x00008000
4400#define ANEG_CFG_HD 0x00004000
4401#define ANEG_CFG_FD 0x00002000
4402#define ANEG_CFG_INVAL 0x00001f06
4403
4404};
4405#define ANEG_OK 0
4406#define ANEG_DONE 1
4407#define ANEG_TIMER_ENAB 2
4408#define ANEG_FAILED -1
4409
4410#define ANEG_STATE_SETTLE_TIME 10000
4411
4412static int tg3_fiber_aneg_smachine(struct tg3 *tp,
4413 struct tg3_fiber_aneginfo *ap)
4414{
Matt Carlson5be73b42007-12-20 20:09:29 -08004415 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 unsigned long delta;
4417 u32 rx_cfg_reg;
4418 int ret;
4419
4420 if (ap->state == ANEG_STATE_UNKNOWN) {
4421 ap->rxconfig = 0;
4422 ap->link_time = 0;
4423 ap->cur_time = 0;
4424 ap->ability_match_cfg = 0;
4425 ap->ability_match_count = 0;
4426 ap->ability_match = 0;
4427 ap->idle_match = 0;
4428 ap->ack_match = 0;
4429 }
4430 ap->cur_time++;
4431
4432 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
4433 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
4434
4435 if (rx_cfg_reg != ap->ability_match_cfg) {
4436 ap->ability_match_cfg = rx_cfg_reg;
4437 ap->ability_match = 0;
4438 ap->ability_match_count = 0;
4439 } else {
4440 if (++ap->ability_match_count > 1) {
4441 ap->ability_match = 1;
4442 ap->ability_match_cfg = rx_cfg_reg;
4443 }
4444 }
4445 if (rx_cfg_reg & ANEG_CFG_ACK)
4446 ap->ack_match = 1;
4447 else
4448 ap->ack_match = 0;
4449
4450 ap->idle_match = 0;
4451 } else {
4452 ap->idle_match = 1;
4453 ap->ability_match_cfg = 0;
4454 ap->ability_match_count = 0;
4455 ap->ability_match = 0;
4456 ap->ack_match = 0;
4457
4458 rx_cfg_reg = 0;
4459 }
4460
4461 ap->rxconfig = rx_cfg_reg;
4462 ret = ANEG_OK;
4463
Matt Carlson33f401a2010-04-05 10:19:27 +00004464 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465 case ANEG_STATE_UNKNOWN:
4466 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
4467 ap->state = ANEG_STATE_AN_ENABLE;
4468
4469 /* fallthru */
4470 case ANEG_STATE_AN_ENABLE:
4471 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
4472 if (ap->flags & MR_AN_ENABLE) {
4473 ap->link_time = 0;
4474 ap->cur_time = 0;
4475 ap->ability_match_cfg = 0;
4476 ap->ability_match_count = 0;
4477 ap->ability_match = 0;
4478 ap->idle_match = 0;
4479 ap->ack_match = 0;
4480
4481 ap->state = ANEG_STATE_RESTART_INIT;
4482 } else {
4483 ap->state = ANEG_STATE_DISABLE_LINK_OK;
4484 }
4485 break;
4486
4487 case ANEG_STATE_RESTART_INIT:
4488 ap->link_time = ap->cur_time;
4489 ap->flags &= ~(MR_NP_LOADED);
4490 ap->txconfig = 0;
4491 tw32(MAC_TX_AUTO_NEG, 0);
4492 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4493 tw32_f(MAC_MODE, tp->mac_mode);
4494 udelay(40);
4495
4496 ret = ANEG_TIMER_ENAB;
4497 ap->state = ANEG_STATE_RESTART;
4498
4499 /* fallthru */
4500 case ANEG_STATE_RESTART:
4501 delta = ap->cur_time - ap->link_time;
Matt Carlson859a588792010-04-05 10:19:28 +00004502 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a588792010-04-05 10:19:28 +00004504 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 break;
4507
4508 case ANEG_STATE_DISABLE_LINK_OK:
4509 ret = ANEG_DONE;
4510 break;
4511
4512 case ANEG_STATE_ABILITY_DETECT_INIT:
4513 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08004514 ap->txconfig = ANEG_CFG_FD;
4515 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
4516 if (flowctrl & ADVERTISE_1000XPAUSE)
4517 ap->txconfig |= ANEG_CFG_PS1;
4518 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
4519 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
4521 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4522 tw32_f(MAC_MODE, tp->mac_mode);
4523 udelay(40);
4524
4525 ap->state = ANEG_STATE_ABILITY_DETECT;
4526 break;
4527
4528 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a588792010-04-05 10:19:28 +00004529 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 break;
4532
4533 case ANEG_STATE_ACK_DETECT_INIT:
4534 ap->txconfig |= ANEG_CFG_ACK;
4535 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
4536 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4537 tw32_f(MAC_MODE, tp->mac_mode);
4538 udelay(40);
4539
4540 ap->state = ANEG_STATE_ACK_DETECT;
4541
4542 /* fallthru */
4543 case ANEG_STATE_ACK_DETECT:
4544 if (ap->ack_match != 0) {
4545 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
4546 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
4547 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
4548 } else {
4549 ap->state = ANEG_STATE_AN_ENABLE;
4550 }
4551 } else if (ap->ability_match != 0 &&
4552 ap->rxconfig == 0) {
4553 ap->state = ANEG_STATE_AN_ENABLE;
4554 }
4555 break;
4556
4557 case ANEG_STATE_COMPLETE_ACK_INIT:
4558 if (ap->rxconfig & ANEG_CFG_INVAL) {
4559 ret = ANEG_FAILED;
4560 break;
4561 }
4562 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
4563 MR_LP_ADV_HALF_DUPLEX |
4564 MR_LP_ADV_SYM_PAUSE |
4565 MR_LP_ADV_ASYM_PAUSE |
4566 MR_LP_ADV_REMOTE_FAULT1 |
4567 MR_LP_ADV_REMOTE_FAULT2 |
4568 MR_LP_ADV_NEXT_PAGE |
4569 MR_TOGGLE_RX |
4570 MR_NP_RX);
4571 if (ap->rxconfig & ANEG_CFG_FD)
4572 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
4573 if (ap->rxconfig & ANEG_CFG_HD)
4574 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
4575 if (ap->rxconfig & ANEG_CFG_PS1)
4576 ap->flags |= MR_LP_ADV_SYM_PAUSE;
4577 if (ap->rxconfig & ANEG_CFG_PS2)
4578 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
4579 if (ap->rxconfig & ANEG_CFG_RF1)
4580 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
4581 if (ap->rxconfig & ANEG_CFG_RF2)
4582 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
4583 if (ap->rxconfig & ANEG_CFG_NP)
4584 ap->flags |= MR_LP_ADV_NEXT_PAGE;
4585
4586 ap->link_time = ap->cur_time;
4587
4588 ap->flags ^= (MR_TOGGLE_TX);
4589 if (ap->rxconfig & 0x0008)
4590 ap->flags |= MR_TOGGLE_RX;
4591 if (ap->rxconfig & ANEG_CFG_NP)
4592 ap->flags |= MR_NP_RX;
4593 ap->flags |= MR_PAGE_RX;
4594
4595 ap->state = ANEG_STATE_COMPLETE_ACK;
4596 ret = ANEG_TIMER_ENAB;
4597 break;
4598
4599 case ANEG_STATE_COMPLETE_ACK:
4600 if (ap->ability_match != 0 &&
4601 ap->rxconfig == 0) {
4602 ap->state = ANEG_STATE_AN_ENABLE;
4603 break;
4604 }
4605 delta = ap->cur_time - ap->link_time;
4606 if (delta > ANEG_STATE_SETTLE_TIME) {
4607 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
4608 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
4609 } else {
4610 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
4611 !(ap->flags & MR_NP_RX)) {
4612 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
4613 } else {
4614 ret = ANEG_FAILED;
4615 }
4616 }
4617 }
4618 break;
4619
4620 case ANEG_STATE_IDLE_DETECT_INIT:
4621 ap->link_time = ap->cur_time;
4622 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
4623 tw32_f(MAC_MODE, tp->mac_mode);
4624 udelay(40);
4625
4626 ap->state = ANEG_STATE_IDLE_DETECT;
4627 ret = ANEG_TIMER_ENAB;
4628 break;
4629
4630 case ANEG_STATE_IDLE_DETECT:
4631 if (ap->ability_match != 0 &&
4632 ap->rxconfig == 0) {
4633 ap->state = ANEG_STATE_AN_ENABLE;
4634 break;
4635 }
4636 delta = ap->cur_time - ap->link_time;
4637 if (delta > ANEG_STATE_SETTLE_TIME) {
4638 /* XXX another gem from the Broadcom driver :( */
4639 ap->state = ANEG_STATE_LINK_OK;
4640 }
4641 break;
4642
4643 case ANEG_STATE_LINK_OK:
4644 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
4645 ret = ANEG_DONE;
4646 break;
4647
4648 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
4649 /* ??? unimplemented */
4650 break;
4651
4652 case ANEG_STATE_NEXT_PAGE_WAIT:
4653 /* ??? unimplemented */
4654 break;
4655
4656 default:
4657 ret = ANEG_FAILED;
4658 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660
4661 return ret;
4662}
4663
Matt Carlson5be73b42007-12-20 20:09:29 -08004664static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665{
4666 int res = 0;
4667 struct tg3_fiber_aneginfo aninfo;
4668 int status = ANEG_FAILED;
4669 unsigned int tick;
4670 u32 tmp;
4671
4672 tw32_f(MAC_TX_AUTO_NEG, 0);
4673
4674 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
4675 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
4676 udelay(40);
4677
4678 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
4679 udelay(40);
4680
4681 memset(&aninfo, 0, sizeof(aninfo));
4682 aninfo.flags |= MR_AN_ENABLE;
4683 aninfo.state = ANEG_STATE_UNKNOWN;
4684 aninfo.cur_time = 0;
4685 tick = 0;
4686 while (++tick < 195000) {
4687 status = tg3_fiber_aneg_smachine(tp, &aninfo);
4688 if (status == ANEG_DONE || status == ANEG_FAILED)
4689 break;
4690
4691 udelay(1);
4692 }
4693
4694 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
4695 tw32_f(MAC_MODE, tp->mac_mode);
4696 udelay(40);
4697
Matt Carlson5be73b42007-12-20 20:09:29 -08004698 *txflags = aninfo.txconfig;
4699 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700
4701 if (status == ANEG_DONE &&
4702 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
4703 MR_LP_ADV_FULL_DUPLEX)))
4704 res = 1;
4705
4706 return res;
4707}
4708
4709static void tg3_init_bcm8002(struct tg3 *tp)
4710{
4711 u32 mac_status = tr32(MAC_STATUS);
4712 int i;
4713
4714 /* Reset when initting first time or we have a link. */
Joe Perches63c3a662011-04-26 08:12:10 +00004715 if (tg3_flag(tp, INIT_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716 !(mac_status & MAC_STATUS_PCS_SYNCED))
4717 return;
4718
4719 /* Set PLL lock range. */
4720 tg3_writephy(tp, 0x16, 0x8007);
4721
4722 /* SW reset */
4723 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
4724
4725 /* Wait for reset to complete. */
4726 /* XXX schedule_timeout() ... */
4727 for (i = 0; i < 500; i++)
4728 udelay(10);
4729
4730 /* Config mode; select PMA/Ch 1 regs. */
4731 tg3_writephy(tp, 0x10, 0x8411);
4732
4733 /* Enable auto-lock and comdet, select txclk for tx. */
4734 tg3_writephy(tp, 0x11, 0x0a10);
4735
4736 tg3_writephy(tp, 0x18, 0x00a0);
4737 tg3_writephy(tp, 0x16, 0x41ff);
4738
4739 /* Assert and deassert POR. */
4740 tg3_writephy(tp, 0x13, 0x0400);
4741 udelay(40);
4742 tg3_writephy(tp, 0x13, 0x0000);
4743
4744 tg3_writephy(tp, 0x11, 0x0a50);
4745 udelay(40);
4746 tg3_writephy(tp, 0x11, 0x0a10);
4747
4748 /* Wait for signal to stabilize */
4749 /* XXX schedule_timeout() ... */
4750 for (i = 0; i < 15000; i++)
4751 udelay(10);
4752
4753 /* Deselect the channel register so we can read the PHYID
4754 * later.
4755 */
4756 tg3_writephy(tp, 0x10, 0x8011);
4757}
4758
4759static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
4760{
Matt Carlson82cd3d12007-12-20 20:09:00 -08004761 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762 u32 sg_dig_ctrl, sg_dig_status;
4763 u32 serdes_cfg, expected_sg_dig_ctrl;
4764 int workaround, port_a;
4765 int current_link_up;
4766
4767 serdes_cfg = 0;
4768 expected_sg_dig_ctrl = 0;
4769 workaround = 0;
4770 port_a = 1;
4771 current_link_up = 0;
4772
4773 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
4774 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
4775 workaround = 1;
4776 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
4777 port_a = 0;
4778
4779 /* preserve bits 0-11,13,14 for signal pre-emphasis */
4780 /* preserve bits 20-23 for voltage regulator */
4781 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
4782 }
4783
4784 sg_dig_ctrl = tr32(SG_DIG_CTRL);
4785
4786 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004787 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788 if (workaround) {
4789 u32 val = serdes_cfg;
4790
4791 if (port_a)
4792 val |= 0xc010000;
4793 else
4794 val |= 0x4010000;
4795 tw32_f(MAC_SERDES_CFG, val);
4796 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004797
4798 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799 }
4800 if (mac_status & MAC_STATUS_PCS_SYNCED) {
4801 tg3_setup_flow_control(tp, 0, 0);
4802 current_link_up = 1;
4803 }
4804 goto out;
4805 }
4806
4807 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004808 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809
Matt Carlson82cd3d12007-12-20 20:09:00 -08004810 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
4811 if (flowctrl & ADVERTISE_1000XPAUSE)
4812 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
4813 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
4814 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815
4816 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004817 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07004818 tp->serdes_counter &&
4819 ((mac_status & (MAC_STATUS_PCS_SYNCED |
4820 MAC_STATUS_RCVD_CFG)) ==
4821 MAC_STATUS_PCS_SYNCED)) {
4822 tp->serdes_counter--;
4823 current_link_up = 1;
4824 goto out;
4825 }
4826restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827 if (workaround)
4828 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004829 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830 udelay(5);
4831 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
4832
Michael Chan3d3ebe72006-09-27 15:59:15 -07004833 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004834 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
4836 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07004837 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838 mac_status = tr32(MAC_STATUS);
4839
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004840 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08004842 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843
Matt Carlson82cd3d12007-12-20 20:09:00 -08004844 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
4845 local_adv |= ADVERTISE_1000XPAUSE;
4846 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
4847 local_adv |= ADVERTISE_1000XPSE_ASYM;
4848
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004849 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08004850 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004851 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08004852 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853
Matt Carlson859edb22011-12-08 14:40:16 +00004854 tp->link_config.rmt_adv =
4855 mii_adv_to_ethtool_adv_x(remote_adv);
4856
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857 tg3_setup_flow_control(tp, local_adv, remote_adv);
4858 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004859 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004860 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004861 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07004862 if (tp->serdes_counter)
4863 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864 else {
4865 if (workaround) {
4866 u32 val = serdes_cfg;
4867
4868 if (port_a)
4869 val |= 0xc010000;
4870 else
4871 val |= 0x4010000;
4872
4873 tw32_f(MAC_SERDES_CFG, val);
4874 }
4875
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004876 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877 udelay(40);
4878
4879 /* Link parallel detection - link is up */
4880 /* only if we have PCS_SYNC and not */
4881 /* receiving config code words */
4882 mac_status = tr32(MAC_STATUS);
4883 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
4884 !(mac_status & MAC_STATUS_RCVD_CFG)) {
4885 tg3_setup_flow_control(tp, 0, 0);
4886 current_link_up = 1;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004887 tp->phy_flags |=
4888 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004889 tp->serdes_counter =
4890 SERDES_PARALLEL_DET_TIMEOUT;
4891 } else
4892 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893 }
4894 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07004895 } else {
4896 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004897 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898 }
4899
4900out:
4901 return current_link_up;
4902}
4903
4904static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
4905{
4906 int current_link_up = 0;
4907
Michael Chan5cf64b8a2007-05-05 12:11:21 -07004908 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910
4911 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08004912 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004914
Matt Carlson5be73b42007-12-20 20:09:29 -08004915 if (fiber_autoneg(tp, &txflags, &rxflags)) {
4916 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917
Matt Carlson5be73b42007-12-20 20:09:29 -08004918 if (txflags & ANEG_CFG_PS1)
4919 local_adv |= ADVERTISE_1000XPAUSE;
4920 if (txflags & ANEG_CFG_PS2)
4921 local_adv |= ADVERTISE_1000XPSE_ASYM;
4922
4923 if (rxflags & MR_LP_ADV_SYM_PAUSE)
4924 remote_adv |= LPA_1000XPAUSE;
4925 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
4926 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927
Matt Carlson859edb22011-12-08 14:40:16 +00004928 tp->link_config.rmt_adv =
4929 mii_adv_to_ethtool_adv_x(remote_adv);
4930
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931 tg3_setup_flow_control(tp, local_adv, remote_adv);
4932
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933 current_link_up = 1;
4934 }
4935 for (i = 0; i < 30; i++) {
4936 udelay(20);
4937 tw32_f(MAC_STATUS,
4938 (MAC_STATUS_SYNC_CHANGED |
4939 MAC_STATUS_CFG_CHANGED));
4940 udelay(40);
4941 if ((tr32(MAC_STATUS) &
4942 (MAC_STATUS_SYNC_CHANGED |
4943 MAC_STATUS_CFG_CHANGED)) == 0)
4944 break;
4945 }
4946
4947 mac_status = tr32(MAC_STATUS);
4948 if (current_link_up == 0 &&
4949 (mac_status & MAC_STATUS_PCS_SYNCED) &&
4950 !(mac_status & MAC_STATUS_RCVD_CFG))
4951 current_link_up = 1;
4952 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08004953 tg3_setup_flow_control(tp, 0, 0);
4954
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955 /* Forcing 1000FD link up. */
4956 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957
4958 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
4959 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004960
4961 tw32_f(MAC_MODE, tp->mac_mode);
4962 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963 }
4964
4965out:
4966 return current_link_up;
4967}
4968
4969static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
4970{
4971 u32 orig_pause_cfg;
4972 u16 orig_active_speed;
4973 u8 orig_active_duplex;
4974 u32 mac_status;
4975 int current_link_up;
4976 int i;
4977
Matt Carlson8d018622007-12-20 20:05:44 -08004978 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979 orig_active_speed = tp->link_config.active_speed;
4980 orig_active_duplex = tp->link_config.active_duplex;
4981
Joe Perches63c3a662011-04-26 08:12:10 +00004982 if (!tg3_flag(tp, HW_AUTONEG) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983 netif_carrier_ok(tp->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00004984 tg3_flag(tp, INIT_COMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985 mac_status = tr32(MAC_STATUS);
4986 mac_status &= (MAC_STATUS_PCS_SYNCED |
4987 MAC_STATUS_SIGNAL_DET |
4988 MAC_STATUS_CFG_CHANGED |
4989 MAC_STATUS_RCVD_CFG);
4990 if (mac_status == (MAC_STATUS_PCS_SYNCED |
4991 MAC_STATUS_SIGNAL_DET)) {
4992 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4993 MAC_STATUS_CFG_CHANGED));
4994 return 0;
4995 }
4996 }
4997
4998 tw32_f(MAC_TX_AUTO_NEG, 0);
4999
5000 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
5001 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
5002 tw32_f(MAC_MODE, tp->mac_mode);
5003 udelay(40);
5004
Matt Carlson79eb6902010-02-17 15:17:03 +00005005 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006 tg3_init_bcm8002(tp);
5007
5008 /* Enable link change event even when serdes polling. */
5009 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5010 udelay(40);
5011
5012 current_link_up = 0;
Matt Carlson859edb22011-12-08 14:40:16 +00005013 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014 mac_status = tr32(MAC_STATUS);
5015
Joe Perches63c3a662011-04-26 08:12:10 +00005016 if (tg3_flag(tp, HW_AUTONEG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
5018 else
5019 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
5020
Matt Carlson898a56f2009-08-28 14:02:40 +00005021 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00005023 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024
5025 for (i = 0; i < 100; i++) {
5026 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
5027 MAC_STATUS_CFG_CHANGED));
5028 udelay(5);
5029 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07005030 MAC_STATUS_CFG_CHANGED |
5031 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032 break;
5033 }
5034
5035 mac_status = tr32(MAC_STATUS);
5036 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
5037 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005038 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
5039 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040 tw32_f(MAC_MODE, (tp->mac_mode |
5041 MAC_MODE_SEND_CONFIGS));
5042 udelay(1);
5043 tw32_f(MAC_MODE, tp->mac_mode);
5044 }
5045 }
5046
5047 if (current_link_up == 1) {
5048 tp->link_config.active_speed = SPEED_1000;
5049 tp->link_config.active_duplex = DUPLEX_FULL;
5050 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5051 LED_CTRL_LNKLED_OVERRIDE |
5052 LED_CTRL_1000MBPS_ON));
5053 } else {
Matt Carlsone7405222012-02-13 15:20:16 +00005054 tp->link_config.active_speed = SPEED_UNKNOWN;
5055 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5057 LED_CTRL_LNKLED_OVERRIDE |
5058 LED_CTRL_TRAFFIC_OVERRIDE));
5059 }
5060
5061 if (current_link_up != netif_carrier_ok(tp->dev)) {
5062 if (current_link_up)
5063 netif_carrier_on(tp->dev);
5064 else
5065 netif_carrier_off(tp->dev);
5066 tg3_link_report(tp);
5067 } else {
Matt Carlson8d018622007-12-20 20:05:44 -08005068 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 if (orig_pause_cfg != now_pause_cfg ||
5070 orig_active_speed != tp->link_config.active_speed ||
5071 orig_active_duplex != tp->link_config.active_duplex)
5072 tg3_link_report(tp);
5073 }
5074
5075 return 0;
5076}
5077
Michael Chan747e8f82005-07-25 12:33:22 -07005078static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
5079{
5080 int current_link_up, err = 0;
5081 u32 bmsr, bmcr;
5082 u16 current_speed;
5083 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08005084 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07005085
5086 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5087 tw32_f(MAC_MODE, tp->mac_mode);
5088 udelay(40);
5089
5090 tw32(MAC_EVENT, 0);
5091
5092 tw32_f(MAC_STATUS,
5093 (MAC_STATUS_SYNC_CHANGED |
5094 MAC_STATUS_CFG_CHANGED |
5095 MAC_STATUS_MI_COMPLETION |
5096 MAC_STATUS_LNKSTATE_CHANGED));
5097 udelay(40);
5098
5099 if (force_reset)
5100 tg3_phy_reset(tp);
5101
5102 current_link_up = 0;
Matt Carlsone7405222012-02-13 15:20:16 +00005103 current_speed = SPEED_UNKNOWN;
5104 current_duplex = DUPLEX_UNKNOWN;
Matt Carlson859edb22011-12-08 14:40:16 +00005105 tp->link_config.rmt_adv = 0;
Michael Chan747e8f82005-07-25 12:33:22 -07005106
5107 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5108 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08005109 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
5110 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5111 bmsr |= BMSR_LSTATUS;
5112 else
5113 bmsr &= ~BMSR_LSTATUS;
5114 }
Michael Chan747e8f82005-07-25 12:33:22 -07005115
5116 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
5117
5118 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005119 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005120 /* do nothing, just check for link up at the end */
5121 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson28011cf2011-11-16 18:36:59 -05005122 u32 adv, newadv;
Michael Chan747e8f82005-07-25 12:33:22 -07005123
5124 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
Matt Carlson28011cf2011-11-16 18:36:59 -05005125 newadv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
5126 ADVERTISE_1000XPAUSE |
5127 ADVERTISE_1000XPSE_ASYM |
5128 ADVERTISE_SLCT);
Michael Chan747e8f82005-07-25 12:33:22 -07005129
Matt Carlson28011cf2011-11-16 18:36:59 -05005130 newadv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Matt Carlson37f07022011-11-17 14:30:55 +00005131 newadv |= ethtool_adv_to_mii_adv_x(tp->link_config.advertising);
Michael Chan747e8f82005-07-25 12:33:22 -07005132
Matt Carlson28011cf2011-11-16 18:36:59 -05005133 if ((newadv != adv) || !(bmcr & BMCR_ANENABLE)) {
5134 tg3_writephy(tp, MII_ADVERTISE, newadv);
Michael Chan747e8f82005-07-25 12:33:22 -07005135 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
5136 tg3_writephy(tp, MII_BMCR, bmcr);
5137
5138 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07005139 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005140 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005141
5142 return err;
5143 }
5144 } else {
5145 u32 new_bmcr;
5146
5147 bmcr &= ~BMCR_SPEED1000;
5148 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
5149
5150 if (tp->link_config.duplex == DUPLEX_FULL)
5151 new_bmcr |= BMCR_FULLDPLX;
5152
5153 if (new_bmcr != bmcr) {
5154 /* BMCR_SPEED1000 is a reserved bit that needs
5155 * to be set on write.
5156 */
5157 new_bmcr |= BMCR_SPEED1000;
5158
5159 /* Force a linkdown */
5160 if (netif_carrier_ok(tp->dev)) {
5161 u32 adv;
5162
5163 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
5164 adv &= ~(ADVERTISE_1000XFULL |
5165 ADVERTISE_1000XHALF |
5166 ADVERTISE_SLCT);
5167 tg3_writephy(tp, MII_ADVERTISE, adv);
5168 tg3_writephy(tp, MII_BMCR, bmcr |
5169 BMCR_ANRESTART |
5170 BMCR_ANENABLE);
5171 udelay(10);
5172 netif_carrier_off(tp->dev);
5173 }
5174 tg3_writephy(tp, MII_BMCR, new_bmcr);
5175 bmcr = new_bmcr;
5176 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5177 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08005178 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
5179 ASIC_REV_5714) {
5180 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5181 bmsr |= BMSR_LSTATUS;
5182 else
5183 bmsr &= ~BMSR_LSTATUS;
5184 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005185 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005186 }
5187 }
5188
5189 if (bmsr & BMSR_LSTATUS) {
5190 current_speed = SPEED_1000;
5191 current_link_up = 1;
5192 if (bmcr & BMCR_FULLDPLX)
5193 current_duplex = DUPLEX_FULL;
5194 else
5195 current_duplex = DUPLEX_HALF;
5196
Matt Carlsonef167e22007-12-20 20:10:01 -08005197 local_adv = 0;
5198 remote_adv = 0;
5199
Michael Chan747e8f82005-07-25 12:33:22 -07005200 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08005201 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07005202
5203 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
5204 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
5205 common = local_adv & remote_adv;
5206 if (common & (ADVERTISE_1000XHALF |
5207 ADVERTISE_1000XFULL)) {
5208 if (common & ADVERTISE_1000XFULL)
5209 current_duplex = DUPLEX_FULL;
5210 else
5211 current_duplex = DUPLEX_HALF;
Matt Carlson859edb22011-12-08 14:40:16 +00005212
5213 tp->link_config.rmt_adv =
5214 mii_adv_to_ethtool_adv_x(remote_adv);
Joe Perches63c3a662011-04-26 08:12:10 +00005215 } else if (!tg3_flag(tp, 5780_CLASS)) {
Matt Carlson57d8b882010-06-05 17:24:35 +00005216 /* Link is up via parallel detect */
Matt Carlson859a588792010-04-05 10:19:28 +00005217 } else {
Michael Chan747e8f82005-07-25 12:33:22 -07005218 current_link_up = 0;
Matt Carlson859a588792010-04-05 10:19:28 +00005219 }
Michael Chan747e8f82005-07-25 12:33:22 -07005220 }
5221 }
5222
Matt Carlsonef167e22007-12-20 20:10:01 -08005223 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
5224 tg3_setup_flow_control(tp, local_adv, remote_adv);
5225
Michael Chan747e8f82005-07-25 12:33:22 -07005226 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
5227 if (tp->link_config.active_duplex == DUPLEX_HALF)
5228 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
5229
5230 tw32_f(MAC_MODE, tp->mac_mode);
5231 udelay(40);
5232
5233 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5234
5235 tp->link_config.active_speed = current_speed;
5236 tp->link_config.active_duplex = current_duplex;
5237
5238 if (current_link_up != netif_carrier_ok(tp->dev)) {
5239 if (current_link_up)
5240 netif_carrier_on(tp->dev);
5241 else {
5242 netif_carrier_off(tp->dev);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005243 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005244 }
5245 tg3_link_report(tp);
5246 }
5247 return err;
5248}
5249
5250static void tg3_serdes_parallel_detect(struct tg3 *tp)
5251{
Michael Chan3d3ebe72006-09-27 15:59:15 -07005252 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07005253 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07005254 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07005255 return;
5256 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005257
Michael Chan747e8f82005-07-25 12:33:22 -07005258 if (!netif_carrier_ok(tp->dev) &&
5259 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
5260 u32 bmcr;
5261
5262 tg3_readphy(tp, MII_BMCR, &bmcr);
5263 if (bmcr & BMCR_ANENABLE) {
5264 u32 phy1, phy2;
5265
5266 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005267 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
5268 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07005269
5270 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005271 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5272 MII_TG3_DSP_EXP1_INT_STAT);
5273 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
5274 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005275
5276 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
5277 /* We have signal detect and not receiving
5278 * config code words, link is up by parallel
5279 * detection.
5280 */
5281
5282 bmcr &= ~BMCR_ANENABLE;
5283 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
5284 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005285 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005286 }
5287 }
Matt Carlson859a588792010-04-05 10:19:28 +00005288 } else if (netif_carrier_ok(tp->dev) &&
5289 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005290 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005291 u32 phy2;
5292
5293 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005294 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5295 MII_TG3_DSP_EXP1_INT_STAT);
5296 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005297 if (phy2 & 0x20) {
5298 u32 bmcr;
5299
5300 /* Config code words received, turn on autoneg. */
5301 tg3_readphy(tp, MII_BMCR, &bmcr);
5302 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
5303
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005304 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005305
5306 }
5307 }
5308}
5309
Linus Torvalds1da177e2005-04-16 15:20:36 -07005310static int tg3_setup_phy(struct tg3 *tp, int force_reset)
5311{
Matt Carlsonf2096f92011-04-05 14:22:48 +00005312 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313 int err;
5314
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005315 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005317 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07005318 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a588792010-04-05 10:19:28 +00005319 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321
Matt Carlsonbcb37f62008-11-03 16:52:09 -08005322 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00005323 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08005324
5325 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
5326 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
5327 scale = 65;
5328 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
5329 scale = 6;
5330 else
5331 scale = 12;
5332
5333 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
5334 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
5335 tw32(GRC_MISC_CFG, val);
5336 }
5337
Matt Carlsonf2096f92011-04-05 14:22:48 +00005338 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
5339 (6 << TX_LENGTHS_IPG_SHIFT);
5340 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
5341 val |= tr32(MAC_TX_LENGTHS) &
5342 (TX_LENGTHS_JMB_FRM_LEN_MSK |
5343 TX_LENGTHS_CNT_DWN_VAL_MSK);
5344
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345 if (tp->link_config.active_speed == SPEED_1000 &&
5346 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00005347 tw32(MAC_TX_LENGTHS, val |
5348 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00005350 tw32(MAC_TX_LENGTHS, val |
5351 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352
Joe Perches63c3a662011-04-26 08:12:10 +00005353 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354 if (netif_carrier_ok(tp->dev)) {
5355 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07005356 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357 } else {
5358 tw32(HOSTCC_STAT_COAL_TICKS, 0);
5359 }
5360 }
5361
Joe Perches63c3a662011-04-26 08:12:10 +00005362 if (tg3_flag(tp, ASPM_WORKAROUND)) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00005363 val = tr32(PCIE_PWR_MGMT_THRESH);
Matt Carlson8ed5d972007-05-07 00:25:49 -07005364 if (!netif_carrier_ok(tp->dev))
5365 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
5366 tp->pwrmgmt_thresh;
5367 else
5368 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
5369 tw32(PCIE_PWR_MGMT_THRESH, val);
5370 }
5371
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372 return err;
5373}
5374
Matt Carlson66cfd1b2010-09-30 10:34:30 +00005375static inline int tg3_irq_sync(struct tg3 *tp)
5376{
5377 return tp->irq_sync;
5378}
5379
Matt Carlson97bd8e42011-04-13 11:05:04 +00005380static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
5381{
5382 int i;
5383
5384 dst = (u32 *)((u8 *)dst + off);
5385 for (i = 0; i < len; i += sizeof(u32))
5386 *dst++ = tr32(off + i);
5387}
5388
5389static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
5390{
5391 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
5392 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
5393 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
5394 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
5395 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
5396 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
5397 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
5398 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
5399 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
5400 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
5401 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
5402 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
5403 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
5404 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
5405 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
5406 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
5407 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
5408 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
5409 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
5410
Joe Perches63c3a662011-04-26 08:12:10 +00005411 if (tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson97bd8e42011-04-13 11:05:04 +00005412 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
5413
5414 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
5415 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
5416 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
5417 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
5418 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
5419 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
5420 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
5421 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
5422
Joe Perches63c3a662011-04-26 08:12:10 +00005423 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00005424 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
5425 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
5426 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
5427 }
5428
5429 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
5430 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
5431 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
5432 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
5433 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
5434
Joe Perches63c3a662011-04-26 08:12:10 +00005435 if (tg3_flag(tp, NVRAM))
Matt Carlson97bd8e42011-04-13 11:05:04 +00005436 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
5437}
5438
5439static void tg3_dump_state(struct tg3 *tp)
5440{
5441 int i;
5442 u32 *regs;
5443
5444 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
5445 if (!regs) {
5446 netdev_err(tp->dev, "Failed allocating register dump buffer\n");
5447 return;
5448 }
5449
Joe Perches63c3a662011-04-26 08:12:10 +00005450 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00005451 /* Read up to but not including private PCI registers */
5452 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
5453 regs[i / sizeof(u32)] = tr32(i);
5454 } else
5455 tg3_dump_legacy_regs(tp, regs);
5456
5457 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
5458 if (!regs[i + 0] && !regs[i + 1] &&
5459 !regs[i + 2] && !regs[i + 3])
5460 continue;
5461
5462 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
5463 i * 4,
5464 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
5465 }
5466
5467 kfree(regs);
5468
5469 for (i = 0; i < tp->irq_cnt; i++) {
5470 struct tg3_napi *tnapi = &tp->napi[i];
5471
5472 /* SW status block */
5473 netdev_err(tp->dev,
5474 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
5475 i,
5476 tnapi->hw_status->status,
5477 tnapi->hw_status->status_tag,
5478 tnapi->hw_status->rx_jumbo_consumer,
5479 tnapi->hw_status->rx_consumer,
5480 tnapi->hw_status->rx_mini_consumer,
5481 tnapi->hw_status->idx[0].rx_producer,
5482 tnapi->hw_status->idx[0].tx_consumer);
5483
5484 netdev_err(tp->dev,
5485 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
5486 i,
5487 tnapi->last_tag, tnapi->last_irq_tag,
5488 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
5489 tnapi->rx_rcb_ptr,
5490 tnapi->prodring.rx_std_prod_idx,
5491 tnapi->prodring.rx_std_cons_idx,
5492 tnapi->prodring.rx_jmb_prod_idx,
5493 tnapi->prodring.rx_jmb_cons_idx);
5494 }
5495}
5496
Michael Chandf3e6542006-05-26 17:48:07 -07005497/* This is called whenever we suspect that the system chipset is re-
5498 * ordering the sequence of MMIO to the tx send mailbox. The symptom
5499 * is bogus tx completions. We try to recover by setting the
5500 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
5501 * in the workqueue.
5502 */
5503static void tg3_tx_recover(struct tg3 *tp)
5504{
Joe Perches63c3a662011-04-26 08:12:10 +00005505 BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
Michael Chandf3e6542006-05-26 17:48:07 -07005506 tp->write32_tx_mbox == tg3_write_indirect_mbox);
5507
Matt Carlson5129c3a2010-04-05 10:19:23 +00005508 netdev_warn(tp->dev,
5509 "The system may be re-ordering memory-mapped I/O "
5510 "cycles to the network device, attempting to recover. "
5511 "Please report the problem to the driver maintainer "
5512 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07005513
5514 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00005515 tg3_flag_set(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07005516 spin_unlock(&tp->lock);
5517}
5518
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005519static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07005520{
Matt Carlsonf65aac12010-08-02 11:26:03 +00005521 /* Tell compiler to fetch tx indices from memory. */
5522 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005523 return tnapi->tx_pending -
5524 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07005525}
5526
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527/* Tigon3 never reports partial packet sends. So we do not
5528 * need special logic to handle SKBs that have not had all
5529 * of their frags sent yet, like SunGEM does.
5530 */
Matt Carlson17375d22009-08-28 14:02:18 +00005531static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532{
Matt Carlson17375d22009-08-28 14:02:18 +00005533 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005534 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005535 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005536 struct netdev_queue *txq;
5537 int index = tnapi - tp->napi;
Tom Herbert298376d2011-11-28 16:33:30 +00005538 unsigned int pkts_compl = 0, bytes_compl = 0;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005539
Joe Perches63c3a662011-04-26 08:12:10 +00005540 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005541 index--;
5542
5543 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544
5545 while (sw_idx != hw_idx) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00005546 struct tg3_tx_ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07005548 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549
Michael Chandf3e6542006-05-26 17:48:07 -07005550 if (unlikely(skb == NULL)) {
5551 tg3_tx_recover(tp);
5552 return;
5553 }
5554
Alexander Duyckf4188d82009-12-02 16:48:38 +00005555 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005556 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005557 skb_headlen(skb),
5558 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559
5560 ri->skb = NULL;
5561
Matt Carlsone01ee142011-07-27 14:20:50 +00005562 while (ri->fragmented) {
5563 ri->fragmented = false;
5564 sw_idx = NEXT_TX(sw_idx);
5565 ri = &tnapi->tx_buffers[sw_idx];
5566 }
5567
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568 sw_idx = NEXT_TX(sw_idx);
5569
5570 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005571 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07005572 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
5573 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005574
5575 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005576 dma_unmap_addr(ri, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00005577 skb_frag_size(&skb_shinfo(skb)->frags[i]),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005578 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00005579
5580 while (ri->fragmented) {
5581 ri->fragmented = false;
5582 sw_idx = NEXT_TX(sw_idx);
5583 ri = &tnapi->tx_buffers[sw_idx];
5584 }
5585
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586 sw_idx = NEXT_TX(sw_idx);
5587 }
5588
Tom Herbert298376d2011-11-28 16:33:30 +00005589 pkts_compl++;
5590 bytes_compl += skb->len;
5591
David S. Millerf47c11e2005-06-24 20:18:35 -07005592 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07005593
5594 if (unlikely(tx_bug)) {
5595 tg3_tx_recover(tp);
5596 return;
5597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598 }
5599
Tom Herbert5cb917b2012-03-05 19:53:50 +00005600 netdev_tx_completed_queue(txq, pkts_compl, bytes_compl);
Tom Herbert298376d2011-11-28 16:33:30 +00005601
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005602 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603
Michael Chan1b2a7202006-08-07 21:46:02 -07005604 /* Need to make the tx_cons update visible to tg3_start_xmit()
5605 * before checking for netif_queue_stopped(). Without the
5606 * memory barrier, there is a small possibility that tg3_start_xmit()
5607 * will miss it and cause the queue to be stopped forever.
5608 */
5609 smp_mb();
5610
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005611 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005612 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005613 __netif_tx_lock(txq, smp_processor_id());
5614 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005615 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005616 netif_tx_wake_queue(txq);
5617 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07005618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619}
5620
Eric Dumazet9205fd92011-11-18 06:47:01 +00005621static void tg3_rx_data_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005622{
Eric Dumazet9205fd92011-11-18 06:47:01 +00005623 if (!ri->data)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005624 return;
5625
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005626 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005627 map_sz, PCI_DMA_FROMDEVICE);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005628 kfree(ri->data);
5629 ri->data = NULL;
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005630}
5631
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632/* Returns size of skb allocated or < 0 on error.
5633 *
5634 * We only need to fill in the address because the other members
5635 * of the RX descriptor are invariant, see tg3_init_rings.
5636 *
5637 * Note the purposeful assymetry of cpu vs. chip accesses. For
5638 * posting buffers we only dirty the first cache line of the RX
5639 * descriptor (containing the address). Whereas for the RX status
5640 * buffers the cpu only reads the last cacheline of the RX descriptor
5641 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
5642 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00005643static int tg3_alloc_rx_data(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Matt Carlsona3896162009-11-13 13:03:44 +00005644 u32 opaque_key, u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645{
5646 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00005647 struct ring_info *map;
Eric Dumazet9205fd92011-11-18 06:47:01 +00005648 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649 dma_addr_t mapping;
Eric Dumazet9205fd92011-11-18 06:47:01 +00005650 int skb_size, data_size, dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652 switch (opaque_key) {
5653 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00005654 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00005655 desc = &tpr->rx_std[dest_idx];
5656 map = &tpr->rx_std_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00005657 data_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658 break;
5659
5660 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00005661 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00005662 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00005663 map = &tpr->rx_jmb_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00005664 data_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665 break;
5666
5667 default:
5668 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670
5671 /* Do not overwrite any of the map or rp information
5672 * until we are sure we can commit to a new buffer.
5673 *
5674 * Callers depend upon this behavior and assume that
5675 * we leave everything unchanged if we fail.
5676 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00005677 skb_size = SKB_DATA_ALIGN(data_size + TG3_RX_OFFSET(tp)) +
5678 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
5679 data = kmalloc(skb_size, GFP_ATOMIC);
5680 if (!data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681 return -ENOMEM;
5682
Eric Dumazet9205fd92011-11-18 06:47:01 +00005683 mapping = pci_map_single(tp->pdev,
5684 data + TG3_RX_OFFSET(tp),
5685 data_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686 PCI_DMA_FROMDEVICE);
Matt Carlsona21771d2009-11-02 14:25:31 +00005687 if (pci_dma_mapping_error(tp->pdev, mapping)) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00005688 kfree(data);
Matt Carlsona21771d2009-11-02 14:25:31 +00005689 return -EIO;
5690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691
Eric Dumazet9205fd92011-11-18 06:47:01 +00005692 map->data = data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005693 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695 desc->addr_hi = ((u64)mapping >> 32);
5696 desc->addr_lo = ((u64)mapping & 0xffffffff);
5697
Eric Dumazet9205fd92011-11-18 06:47:01 +00005698 return data_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699}
5700
5701/* We only need to move over in the address because the other
5702 * members of the RX descriptor are invariant. See notes above
Eric Dumazet9205fd92011-11-18 06:47:01 +00005703 * tg3_alloc_rx_data for full details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704 */
Matt Carlsona3896162009-11-13 13:03:44 +00005705static void tg3_recycle_rx(struct tg3_napi *tnapi,
5706 struct tg3_rx_prodring_set *dpr,
5707 u32 opaque_key, int src_idx,
5708 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709{
Matt Carlson17375d22009-08-28 14:02:18 +00005710 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
5712 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00005713 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005714 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715
5716 switch (opaque_key) {
5717 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00005718 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00005719 dest_desc = &dpr->rx_std[dest_idx];
5720 dest_map = &dpr->rx_std_buffers[dest_idx];
5721 src_desc = &spr->rx_std[src_idx];
5722 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723 break;
5724
5725 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00005726 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00005727 dest_desc = &dpr->rx_jmb[dest_idx].std;
5728 dest_map = &dpr->rx_jmb_buffers[dest_idx];
5729 src_desc = &spr->rx_jmb[src_idx].std;
5730 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731 break;
5732
5733 default:
5734 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736
Eric Dumazet9205fd92011-11-18 06:47:01 +00005737 dest_map->data = src_map->data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005738 dma_unmap_addr_set(dest_map, mapping,
5739 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740 dest_desc->addr_hi = src_desc->addr_hi;
5741 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00005742
5743 /* Ensure that the update to the skb happens after the physical
5744 * addresses have been transferred to the new BD location.
5745 */
5746 smp_wmb();
5747
Eric Dumazet9205fd92011-11-18 06:47:01 +00005748 src_map->data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749}
5750
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751/* The RX ring scheme is composed of multiple rings which post fresh
5752 * buffers to the chip, and one special ring the chip uses to report
5753 * status back to the host.
5754 *
5755 * The special ring reports the status of received packets to the
5756 * host. The chip does not write into the original descriptor the
5757 * RX buffer was obtained from. The chip simply takes the original
5758 * descriptor as provided by the host, updates the status and length
5759 * field, then writes this into the next status ring entry.
5760 *
5761 * Each ring the host uses to post buffers to the chip is described
5762 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
5763 * it is first placed into the on-chip ram. When the packet's length
5764 * is known, it walks down the TG3_BDINFO entries to select the ring.
5765 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
5766 * which is within the range of the new packet's length is chosen.
5767 *
5768 * The "separate ring for rx status" scheme may sound queer, but it makes
5769 * sense from a cache coherency perspective. If only the host writes
5770 * to the buffer post rings, and only the chip writes to the rx status
5771 * rings, then cache lines never move beyond shared-modified state.
5772 * If both the host and chip were to write into the same ring, cache line
5773 * eviction could occur since both entities want it in an exclusive state.
5774 */
Matt Carlson17375d22009-08-28 14:02:18 +00005775static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005776{
Matt Carlson17375d22009-08-28 14:02:18 +00005777 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07005778 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00005779 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00005780 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07005781 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00005783 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005785 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005786 /*
5787 * We need to order the read of hw_idx and the read of
5788 * the opaque cookie.
5789 */
5790 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791 work_mask = 0;
5792 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00005793 std_prod_idx = tpr->rx_std_prod_idx;
5794 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005795 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00005796 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00005797 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798 unsigned int len;
5799 struct sk_buff *skb;
5800 dma_addr_t dma_addr;
5801 u32 opaque_key, desc_idx, *post_ptr;
Eric Dumazet9205fd92011-11-18 06:47:01 +00005802 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005803
5804 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
5805 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
5806 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005807 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005808 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005809 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00005810 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07005811 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005812 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005813 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005814 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005815 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00005816 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00005817 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005818 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005819
5820 work_mask |= opaque_key;
5821
5822 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
5823 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
5824 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00005825 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826 desc_idx, *post_ptr);
5827 drop_it_no_recycle:
5828 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00005829 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005830 goto next_pkt;
5831 }
5832
Eric Dumazet9205fd92011-11-18 06:47:01 +00005833 prefetch(data + TG3_RX_OFFSET(tp));
Matt Carlsonad829262008-11-21 17:16:16 -08005834 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
5835 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836
Matt Carlsond2757fc2010-04-12 06:58:27 +00005837 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005838 int skb_size;
5839
Eric Dumazet9205fd92011-11-18 06:47:01 +00005840 skb_size = tg3_alloc_rx_data(tp, tpr, opaque_key,
Matt Carlsonafc081f2009-11-13 13:03:43 +00005841 *post_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005842 if (skb_size < 0)
5843 goto drop_it;
5844
Matt Carlson287be122009-08-28 13:58:46 +00005845 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846 PCI_DMA_FROMDEVICE);
5847
Eric Dumazet9205fd92011-11-18 06:47:01 +00005848 skb = build_skb(data);
5849 if (!skb) {
5850 kfree(data);
5851 goto drop_it_no_recycle;
5852 }
5853 skb_reserve(skb, TG3_RX_OFFSET(tp));
5854 /* Ensure that the update to the data happens
Matt Carlson61e800c2010-02-17 15:16:54 +00005855 * after the usage of the old DMA mapping.
5856 */
5857 smp_wmb();
5858
Eric Dumazet9205fd92011-11-18 06:47:01 +00005859 ri->data = NULL;
Matt Carlson61e800c2010-02-17 15:16:54 +00005860
Linus Torvalds1da177e2005-04-16 15:20:36 -07005861 } else {
Matt Carlsona3896162009-11-13 13:03:44 +00005862 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005863 desc_idx, *post_ptr);
5864
Eric Dumazet9205fd92011-11-18 06:47:01 +00005865 skb = netdev_alloc_skb(tp->dev,
5866 len + TG3_RAW_IP_ALIGN);
5867 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005868 goto drop_it_no_recycle;
5869
Eric Dumazet9205fd92011-11-18 06:47:01 +00005870 skb_reserve(skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005871 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005872 memcpy(skb->data,
5873 data + TG3_RX_OFFSET(tp),
5874 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005875 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005876 }
5877
Eric Dumazet9205fd92011-11-18 06:47:01 +00005878 skb_put(skb, len);
Michał Mirosławdc668912011-04-07 03:35:07 +00005879 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
5881 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
5882 >> RXD_TCPCSUM_SHIFT) == 0xffff))
5883 skb->ip_summed = CHECKSUM_UNNECESSARY;
5884 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005885 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005886
5887 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00005888
5889 if (len > (tp->dev->mtu + ETH_HLEN) &&
5890 skb->protocol != htons(ETH_P_8021Q)) {
5891 dev_kfree_skb(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00005892 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00005893 }
5894
Matt Carlson9dc7a112010-04-12 06:58:28 +00005895 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00005896 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
5897 __vlan_hwaccel_put_tag(skb,
5898 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00005899
Matt Carlsonbf933c82011-01-25 15:58:49 +00005900 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005901
Linus Torvalds1da177e2005-04-16 15:20:36 -07005902 received++;
5903 budget--;
5904
5905next_pkt:
5906 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07005907
5908 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005909 tpr->rx_std_prod_idx = std_prod_idx &
5910 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00005911 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5912 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07005913 work_mask &= ~RXD_OPAQUE_RING_STD;
5914 rx_std_posted = 0;
5915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005916next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07005917 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00005918 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07005919
5920 /* Refresh hw_idx to see if there is new work */
5921 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005922 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07005923 rmb();
5924 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005925 }
5926
5927 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00005928 tnapi->rx_rcb_ptr = sw_idx;
5929 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005930
5931 /* Refill RX ring(s). */
Joe Perches63c3a662011-04-26 08:12:10 +00005932 if (!tg3_flag(tp, ENABLE_RSS)) {
Michael Chan6541b802012-03-04 14:48:14 +00005933 /* Sync BD data before updating mailbox */
5934 wmb();
5935
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005936 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005937 tpr->rx_std_prod_idx = std_prod_idx &
5938 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005939 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5940 tpr->rx_std_prod_idx);
5941 }
5942 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005943 tpr->rx_jmb_prod_idx = jmb_prod_idx &
5944 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005945 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5946 tpr->rx_jmb_prod_idx);
5947 }
5948 mmiowb();
5949 } else if (work_mask) {
5950 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
5951 * updated before the producer indices can be updated.
5952 */
5953 smp_wmb();
5954
Matt Carlson2c49a442010-09-30 10:34:35 +00005955 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
5956 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005957
Michael Chan7ae52892012-03-21 15:38:33 +00005958 if (tnapi != &tp->napi[1]) {
5959 tp->rx_refill = true;
Matt Carlsone4af1af2010-02-12 14:47:05 +00005960 napi_schedule(&tp->napi[1].napi);
Michael Chan7ae52892012-03-21 15:38:33 +00005961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005962 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005963
5964 return received;
5965}
5966
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005967static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005968{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005969 /* handle link change and other phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00005970 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005971 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
5972
Linus Torvalds1da177e2005-04-16 15:20:36 -07005973 if (sblk->status & SD_STATUS_LINK_CHG) {
5974 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005975 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07005976 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00005977 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsondd477002008-05-25 23:45:58 -07005978 tw32_f(MAC_STATUS,
5979 (MAC_STATUS_SYNC_CHANGED |
5980 MAC_STATUS_CFG_CHANGED |
5981 MAC_STATUS_MI_COMPLETION |
5982 MAC_STATUS_LNKSTATE_CHANGED));
5983 udelay(40);
5984 } else
5985 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07005986 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005987 }
5988 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005989}
5990
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005991static int tg3_rx_prodring_xfer(struct tg3 *tp,
5992 struct tg3_rx_prodring_set *dpr,
5993 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005994{
5995 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005996 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005997
5998 while (1) {
5999 src_prod_idx = spr->rx_std_prod_idx;
6000
6001 /* Make sure updates to the rx_std_buffers[] entries and the
6002 * standard producer index are seen in the correct order.
6003 */
6004 smp_rmb();
6005
6006 if (spr->rx_std_cons_idx == src_prod_idx)
6007 break;
6008
6009 if (spr->rx_std_cons_idx < src_prod_idx)
6010 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
6011 else
Matt Carlson2c49a442010-09-30 10:34:35 +00006012 cpycnt = tp->rx_std_ring_mask + 1 -
6013 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006014
Matt Carlson2c49a442010-09-30 10:34:35 +00006015 cpycnt = min(cpycnt,
6016 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006017
6018 si = spr->rx_std_cons_idx;
6019 di = dpr->rx_std_prod_idx;
6020
Matt Carlsone92967b2010-02-12 14:47:06 +00006021 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00006022 if (dpr->rx_std_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00006023 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006024 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00006025 break;
6026 }
6027 }
6028
6029 if (!cpycnt)
6030 break;
6031
6032 /* Ensure that updates to the rx_std_buffers ring and the
6033 * shadowed hardware producer ring from tg3_recycle_skb() are
6034 * ordered correctly WRT the skb check above.
6035 */
6036 smp_rmb();
6037
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006038 memcpy(&dpr->rx_std_buffers[di],
6039 &spr->rx_std_buffers[si],
6040 cpycnt * sizeof(struct ring_info));
6041
6042 for (i = 0; i < cpycnt; i++, di++, si++) {
6043 struct tg3_rx_buffer_desc *sbd, *dbd;
6044 sbd = &spr->rx_std[si];
6045 dbd = &dpr->rx_std[di];
6046 dbd->addr_hi = sbd->addr_hi;
6047 dbd->addr_lo = sbd->addr_lo;
6048 }
6049
Matt Carlson2c49a442010-09-30 10:34:35 +00006050 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
6051 tp->rx_std_ring_mask;
6052 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
6053 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006054 }
6055
6056 while (1) {
6057 src_prod_idx = spr->rx_jmb_prod_idx;
6058
6059 /* Make sure updates to the rx_jmb_buffers[] entries and
6060 * the jumbo producer index are seen in the correct order.
6061 */
6062 smp_rmb();
6063
6064 if (spr->rx_jmb_cons_idx == src_prod_idx)
6065 break;
6066
6067 if (spr->rx_jmb_cons_idx < src_prod_idx)
6068 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
6069 else
Matt Carlson2c49a442010-09-30 10:34:35 +00006070 cpycnt = tp->rx_jmb_ring_mask + 1 -
6071 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006072
6073 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00006074 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006075
6076 si = spr->rx_jmb_cons_idx;
6077 di = dpr->rx_jmb_prod_idx;
6078
Matt Carlsone92967b2010-02-12 14:47:06 +00006079 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00006080 if (dpr->rx_jmb_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00006081 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006082 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00006083 break;
6084 }
6085 }
6086
6087 if (!cpycnt)
6088 break;
6089
6090 /* Ensure that updates to the rx_jmb_buffers ring and the
6091 * shadowed hardware producer ring from tg3_recycle_skb() are
6092 * ordered correctly WRT the skb check above.
6093 */
6094 smp_rmb();
6095
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006096 memcpy(&dpr->rx_jmb_buffers[di],
6097 &spr->rx_jmb_buffers[si],
6098 cpycnt * sizeof(struct ring_info));
6099
6100 for (i = 0; i < cpycnt; i++, di++, si++) {
6101 struct tg3_rx_buffer_desc *sbd, *dbd;
6102 sbd = &spr->rx_jmb[si].std;
6103 dbd = &dpr->rx_jmb[di].std;
6104 dbd->addr_hi = sbd->addr_hi;
6105 dbd->addr_lo = sbd->addr_lo;
6106 }
6107
Matt Carlson2c49a442010-09-30 10:34:35 +00006108 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
6109 tp->rx_jmb_ring_mask;
6110 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
6111 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006112 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006113
6114 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006115}
6116
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006117static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
6118{
6119 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120
6121 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006122 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00006123 tg3_tx(tnapi);
Joe Perches63c3a662011-04-26 08:12:10 +00006124 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Michael Chan4fd7ab52007-10-12 01:39:50 -07006125 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006126 }
6127
Linus Torvalds1da177e2005-04-16 15:20:36 -07006128 /* run RX thread, within the bounds set by NAPI.
6129 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006130 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07006131 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006132 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00006133 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006134
Joe Perches63c3a662011-04-26 08:12:10 +00006135 if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006136 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006137 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006138 u32 std_prod_idx = dpr->rx_std_prod_idx;
6139 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006140
Michael Chan7ae52892012-03-21 15:38:33 +00006141 tp->rx_refill = false;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006142 for (i = 1; i < tp->irq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006143 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00006144 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006145
6146 wmb();
6147
Matt Carlsone4af1af2010-02-12 14:47:05 +00006148 if (std_prod_idx != dpr->rx_std_prod_idx)
6149 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6150 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006151
Matt Carlsone4af1af2010-02-12 14:47:05 +00006152 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
6153 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
6154 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006155
6156 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006157
6158 if (err)
6159 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006160 }
6161
David S. Miller6f535762007-10-11 18:08:29 -07006162 return work_done;
6163}
David S. Millerf7383c222005-05-18 22:50:53 -07006164
Matt Carlsondb219972011-11-04 09:15:03 +00006165static inline void tg3_reset_task_schedule(struct tg3 *tp)
6166{
6167 if (!test_and_set_bit(TG3_FLAG_RESET_TASK_PENDING, tp->tg3_flags))
6168 schedule_work(&tp->reset_task);
6169}
6170
6171static inline void tg3_reset_task_cancel(struct tg3 *tp)
6172{
6173 cancel_work_sync(&tp->reset_task);
6174 tg3_flag_clear(tp, RESET_TASK_PENDING);
Matt Carlsonc7101352012-02-22 12:35:20 +00006175 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Matt Carlsondb219972011-11-04 09:15:03 +00006176}
6177
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006178static int tg3_poll_msix(struct napi_struct *napi, int budget)
6179{
6180 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
6181 struct tg3 *tp = tnapi->tp;
6182 int work_done = 0;
6183 struct tg3_hw_status *sblk = tnapi->hw_status;
6184
6185 while (1) {
6186 work_done = tg3_poll_work(tnapi, work_done, budget);
6187
Joe Perches63c3a662011-04-26 08:12:10 +00006188 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006189 goto tx_recovery;
6190
6191 if (unlikely(work_done >= budget))
6192 break;
6193
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006194 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006195 * to tell the hw how much work has been processed,
6196 * so we must read it before checking for more work.
6197 */
6198 tnapi->last_tag = sblk->status_tag;
6199 tnapi->last_irq_tag = tnapi->last_tag;
6200 rmb();
6201
6202 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00006203 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
6204 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Michael Chan7ae52892012-03-21 15:38:33 +00006205
6206 /* This test here is not race free, but will reduce
6207 * the number of interrupts by looping again.
6208 */
6209 if (tnapi == &tp->napi[1] && tp->rx_refill)
6210 continue;
6211
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006212 napi_complete(napi);
6213 /* Reenable interrupts. */
6214 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Michael Chan7ae52892012-03-21 15:38:33 +00006215
6216 /* This test here is synchronized by napi_schedule()
6217 * and napi_complete() to close the race condition.
6218 */
6219 if (unlikely(tnapi == &tp->napi[1] && tp->rx_refill)) {
6220 tw32(HOSTCC_MODE, tp->coalesce_mode |
6221 HOSTCC_MODE_ENABLE |
6222 tnapi->coal_now);
6223 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006224 mmiowb();
6225 break;
6226 }
6227 }
6228
6229 return work_done;
6230
6231tx_recovery:
6232 /* work_done is guaranteed to be less than budget. */
6233 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00006234 tg3_reset_task_schedule(tp);
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006235 return work_done;
6236}
6237
Matt Carlsone64de4e2011-04-13 11:05:05 +00006238static void tg3_process_error(struct tg3 *tp)
6239{
6240 u32 val;
6241 bool real_error = false;
6242
Joe Perches63c3a662011-04-26 08:12:10 +00006243 if (tg3_flag(tp, ERROR_PROCESSED))
Matt Carlsone64de4e2011-04-13 11:05:05 +00006244 return;
6245
6246 /* Check Flow Attention register */
6247 val = tr32(HOSTCC_FLOW_ATTN);
6248 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
6249 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
6250 real_error = true;
6251 }
6252
6253 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
6254 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
6255 real_error = true;
6256 }
6257
6258 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
6259 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
6260 real_error = true;
6261 }
6262
6263 if (!real_error)
6264 return;
6265
6266 tg3_dump_state(tp);
6267
Joe Perches63c3a662011-04-26 08:12:10 +00006268 tg3_flag_set(tp, ERROR_PROCESSED);
Matt Carlsondb219972011-11-04 09:15:03 +00006269 tg3_reset_task_schedule(tp);
Matt Carlsone64de4e2011-04-13 11:05:05 +00006270}
6271
David S. Miller6f535762007-10-11 18:08:29 -07006272static int tg3_poll(struct napi_struct *napi, int budget)
6273{
Matt Carlson8ef04422009-08-28 14:01:37 +00006274 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
6275 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07006276 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00006277 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07006278
6279 while (1) {
Matt Carlsone64de4e2011-04-13 11:05:05 +00006280 if (sblk->status & SD_STATUS_ERROR)
6281 tg3_process_error(tp);
6282
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006283 tg3_poll_link(tp);
6284
Matt Carlson17375d22009-08-28 14:02:18 +00006285 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07006286
Joe Perches63c3a662011-04-26 08:12:10 +00006287 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
David S. Miller6f535762007-10-11 18:08:29 -07006288 goto tx_recovery;
6289
6290 if (unlikely(work_done >= budget))
6291 break;
6292
Joe Perches63c3a662011-04-26 08:12:10 +00006293 if (tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson17375d22009-08-28 14:02:18 +00006294 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07006295 * to tell the hw how much work has been processed,
6296 * so we must read it before checking for more work.
6297 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006298 tnapi->last_tag = sblk->status_tag;
6299 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07006300 rmb();
6301 } else
6302 sblk->status &= ~SD_STATUS_UPDATED;
6303
Matt Carlson17375d22009-08-28 14:02:18 +00006304 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08006305 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00006306 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07006307 break;
6308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006309 }
6310
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006311 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07006312
6313tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07006314 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08006315 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00006316 tg3_reset_task_schedule(tp);
Michael Chan4fd7ab52007-10-12 01:39:50 -07006317 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006318}
6319
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006320static void tg3_napi_disable(struct tg3 *tp)
6321{
6322 int i;
6323
6324 for (i = tp->irq_cnt - 1; i >= 0; i--)
6325 napi_disable(&tp->napi[i].napi);
6326}
6327
6328static void tg3_napi_enable(struct tg3 *tp)
6329{
6330 int i;
6331
6332 for (i = 0; i < tp->irq_cnt; i++)
6333 napi_enable(&tp->napi[i].napi);
6334}
6335
6336static void tg3_napi_init(struct tg3 *tp)
6337{
6338 int i;
6339
6340 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
6341 for (i = 1; i < tp->irq_cnt; i++)
6342 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
6343}
6344
6345static void tg3_napi_fini(struct tg3 *tp)
6346{
6347 int i;
6348
6349 for (i = 0; i < tp->irq_cnt; i++)
6350 netif_napi_del(&tp->napi[i].napi);
6351}
6352
6353static inline void tg3_netif_stop(struct tg3 *tp)
6354{
6355 tp->dev->trans_start = jiffies; /* prevent tx timeout */
6356 tg3_napi_disable(tp);
6357 netif_tx_disable(tp->dev);
6358}
6359
6360static inline void tg3_netif_start(struct tg3 *tp)
6361{
6362 /* NOTE: unconditional netif_tx_wake_all_queues is only
6363 * appropriate so long as all callers are assured to
6364 * have free tx slots (such as after tg3_init_hw)
6365 */
6366 netif_tx_wake_all_queues(tp->dev);
6367
6368 tg3_napi_enable(tp);
6369 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
6370 tg3_enable_ints(tp);
6371}
6372
David S. Millerf47c11e2005-06-24 20:18:35 -07006373static void tg3_irq_quiesce(struct tg3 *tp)
6374{
Matt Carlson4f125f42009-09-01 12:55:02 +00006375 int i;
6376
David S. Millerf47c11e2005-06-24 20:18:35 -07006377 BUG_ON(tp->irq_sync);
6378
6379 tp->irq_sync = 1;
6380 smp_mb();
6381
Matt Carlson4f125f42009-09-01 12:55:02 +00006382 for (i = 0; i < tp->irq_cnt; i++)
6383 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07006384}
6385
David S. Millerf47c11e2005-06-24 20:18:35 -07006386/* Fully shutdown all tg3 driver activity elsewhere in the system.
6387 * If irq_sync is non-zero, then the IRQ handler must be synchronized
6388 * with as well. Most of the time, this is not necessary except when
6389 * shutting down the device.
6390 */
6391static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
6392{
Michael Chan46966542007-07-11 19:47:19 -07006393 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07006394 if (irq_sync)
6395 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07006396}
6397
6398static inline void tg3_full_unlock(struct tg3 *tp)
6399{
David S. Millerf47c11e2005-06-24 20:18:35 -07006400 spin_unlock_bh(&tp->lock);
6401}
6402
Michael Chanfcfa0a32006-03-20 22:28:41 -08006403/* One-shot MSI handler - Chip automatically disables interrupt
6404 * after sending MSI so driver doesn't have to do it.
6405 */
David Howells7d12e782006-10-05 14:55:46 +01006406static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08006407{
Matt Carlson09943a12009-08-28 14:01:57 +00006408 struct tg3_napi *tnapi = dev_id;
6409 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08006410
Matt Carlson898a56f2009-08-28 14:02:40 +00006411 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006412 if (tnapi->rx_rcb)
6413 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08006414
6415 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00006416 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08006417
6418 return IRQ_HANDLED;
6419}
6420
Michael Chan88b06bc22005-04-21 17:13:25 -07006421/* MSI ISR - No need to check for interrupt sharing and no need to
6422 * flush status block and interrupt mailbox. PCI ordering rules
6423 * guarantee that MSI will arrive after the status block.
6424 */
David Howells7d12e782006-10-05 14:55:46 +01006425static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07006426{
Matt Carlson09943a12009-08-28 14:01:57 +00006427 struct tg3_napi *tnapi = dev_id;
6428 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07006429
Matt Carlson898a56f2009-08-28 14:02:40 +00006430 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006431 if (tnapi->rx_rcb)
6432 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07006433 /*
David S. Millerfac9b832005-05-18 22:46:34 -07006434 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07006435 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07006436 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07006437 * NIC to stop sending us irqs, engaging "in-intr-handler"
6438 * event coalescing.
6439 */
Matt Carlson5b39de92011-08-31 11:44:50 +00006440 tw32_mailbox(tnapi->int_mbox, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07006441 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00006442 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07006443
Michael Chan88b06bc22005-04-21 17:13:25 -07006444 return IRQ_RETVAL(1);
6445}
6446
David Howells7d12e782006-10-05 14:55:46 +01006447static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006448{
Matt Carlson09943a12009-08-28 14:01:57 +00006449 struct tg3_napi *tnapi = dev_id;
6450 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006451 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006452 unsigned int handled = 1;
6453
Linus Torvalds1da177e2005-04-16 15:20:36 -07006454 /* In INTx mode, it is possible for the interrupt to arrive at
6455 * the CPU before the status block posted prior to the interrupt.
6456 * Reading the PCI State register will confirm whether the
6457 * interrupt is ours and will flush the status block.
6458 */
Michael Chand18edcb2007-03-24 20:57:11 -07006459 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
Joe Perches63c3a662011-04-26 08:12:10 +00006460 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07006461 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
6462 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07006463 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07006464 }
Michael Chand18edcb2007-03-24 20:57:11 -07006465 }
6466
6467 /*
6468 * Writing any value to intr-mbox-0 clears PCI INTA# and
6469 * chip-internal interrupt pending events.
6470 * Writing non-zero to intr-mbox-0 additional tells the
6471 * NIC to stop sending us irqs, engaging "in-intr-handler"
6472 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07006473 *
6474 * Flush the mailbox to de-assert the IRQ immediately to prevent
6475 * spurious interrupts. The flush impacts performance but
6476 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07006477 */
Michael Chanc04cb342007-05-07 00:26:15 -07006478 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07006479 if (tg3_irq_sync(tp))
6480 goto out;
6481 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00006482 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00006483 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00006484 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07006485 } else {
6486 /* No work, shared interrupt perhaps? re-enable
6487 * interrupts, and flush that PCI write
6488 */
6489 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
6490 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07006491 }
David S. Millerf47c11e2005-06-24 20:18:35 -07006492out:
David S. Millerfac9b832005-05-18 22:46:34 -07006493 return IRQ_RETVAL(handled);
6494}
6495
David Howells7d12e782006-10-05 14:55:46 +01006496static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07006497{
Matt Carlson09943a12009-08-28 14:01:57 +00006498 struct tg3_napi *tnapi = dev_id;
6499 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006500 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07006501 unsigned int handled = 1;
6502
David S. Millerfac9b832005-05-18 22:46:34 -07006503 /* In INTx mode, it is possible for the interrupt to arrive at
6504 * the CPU before the status block posted prior to the interrupt.
6505 * Reading the PCI State register will confirm whether the
6506 * interrupt is ours and will flush the status block.
6507 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006508 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Joe Perches63c3a662011-04-26 08:12:10 +00006509 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07006510 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
6511 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07006512 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006513 }
Michael Chand18edcb2007-03-24 20:57:11 -07006514 }
6515
6516 /*
6517 * writing any value to intr-mbox-0 clears PCI INTA# and
6518 * chip-internal interrupt pending events.
6519 * writing non-zero to intr-mbox-0 additional tells the
6520 * NIC to stop sending us irqs, engaging "in-intr-handler"
6521 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07006522 *
6523 * Flush the mailbox to de-assert the IRQ immediately to prevent
6524 * spurious interrupts. The flush impacts performance but
6525 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07006526 */
Michael Chanc04cb342007-05-07 00:26:15 -07006527 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00006528
6529 /*
6530 * In a shared interrupt configuration, sometimes other devices'
6531 * interrupts will scream. We record the current status tag here
6532 * so that the above check can report that the screaming interrupts
6533 * are unhandled. Eventually they will be silenced.
6534 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006535 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00006536
Michael Chand18edcb2007-03-24 20:57:11 -07006537 if (tg3_irq_sync(tp))
6538 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00006539
Matt Carlson72334482009-08-28 14:03:01 +00006540 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00006541
Matt Carlson09943a12009-08-28 14:01:57 +00006542 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00006543
David S. Millerf47c11e2005-06-24 20:18:35 -07006544out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006545 return IRQ_RETVAL(handled);
6546}
6547
Michael Chan79381092005-04-21 17:13:59 -07006548/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01006549static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07006550{
Matt Carlson09943a12009-08-28 14:01:57 +00006551 struct tg3_napi *tnapi = dev_id;
6552 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006553 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07006554
Michael Chanf9804dd2005-09-27 12:13:10 -07006555 if ((sblk->status & SD_STATUS_UPDATED) ||
6556 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07006557 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07006558 return IRQ_RETVAL(1);
6559 }
6560 return IRQ_RETVAL(0);
6561}
6562
Linus Torvalds1da177e2005-04-16 15:20:36 -07006563#ifdef CONFIG_NET_POLL_CONTROLLER
6564static void tg3_poll_controller(struct net_device *dev)
6565{
Matt Carlson4f125f42009-09-01 12:55:02 +00006566 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07006567 struct tg3 *tp = netdev_priv(dev);
6568
Matt Carlson4f125f42009-09-01 12:55:02 +00006569 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00006570 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006571}
6572#endif
6573
Linus Torvalds1da177e2005-04-16 15:20:36 -07006574static void tg3_tx_timeout(struct net_device *dev)
6575{
6576 struct tg3 *tp = netdev_priv(dev);
6577
Michael Chanb0408752007-02-13 12:18:30 -08006578 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00006579 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00006580 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08006581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006582
Matt Carlsondb219972011-11-04 09:15:03 +00006583 tg3_reset_task_schedule(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006584}
6585
Michael Chanc58ec932005-09-17 00:46:27 -07006586/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
6587static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
6588{
6589 u32 base = (u32) mapping & 0xffffffff;
6590
Eric Dumazet807540b2010-09-23 05:40:09 +00006591 return (base > 0xffffdcc0) && (base + len + 8 < base);
Michael Chanc58ec932005-09-17 00:46:27 -07006592}
6593
Michael Chan72f2afb2006-03-06 19:28:35 -08006594/* Test for DMA addresses > 40-bit */
6595static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
6596 int len)
6597{
6598#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Joe Perches63c3a662011-04-26 08:12:10 +00006599 if (tg3_flag(tp, 40BIT_DMA_BUG))
Eric Dumazet807540b2010-09-23 05:40:09 +00006600 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08006601 return 0;
6602#else
6603 return 0;
6604#endif
6605}
6606
Matt Carlsond1a3b732011-07-27 14:20:51 +00006607static inline void tg3_tx_set_bd(struct tg3_tx_buffer_desc *txbd,
Matt Carlson92cd3a12011-07-27 14:20:47 +00006608 dma_addr_t mapping, u32 len, u32 flags,
6609 u32 mss, u32 vlan)
Matt Carlson2ffcc982011-05-19 12:12:44 +00006610{
Matt Carlson92cd3a12011-07-27 14:20:47 +00006611 txbd->addr_hi = ((u64) mapping >> 32);
6612 txbd->addr_lo = ((u64) mapping & 0xffffffff);
6613 txbd->len_flags = (len << TXD_LEN_SHIFT) | (flags & 0x0000ffff);
6614 txbd->vlan_tag = (mss << TXD_MSS_SHIFT) | (vlan << TXD_VLAN_TAG_SHIFT);
Matt Carlson2ffcc982011-05-19 12:12:44 +00006615}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006616
Matt Carlson84b67b22011-07-27 14:20:52 +00006617static bool tg3_tx_frag_set(struct tg3_napi *tnapi, u32 *entry, u32 *budget,
Matt Carlsond1a3b732011-07-27 14:20:51 +00006618 dma_addr_t map, u32 len, u32 flags,
6619 u32 mss, u32 vlan)
6620{
6621 struct tg3 *tp = tnapi->tp;
6622 bool hwbug = false;
6623
6624 if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
Rusty Russell3db1cd52011-12-19 13:56:45 +00006625 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00006626
6627 if (tg3_4g_overflow_test(map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00006628 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00006629
6630 if (tg3_40bit_overflow_test(tp, map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00006631 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00006632
Matt Carlsona4cb4282011-12-14 11:09:58 +00006633 if (tp->dma_limit) {
Matt Carlsonb9e45482011-11-04 09:14:59 +00006634 u32 prvidx = *entry;
Matt Carlsone31aa982011-07-27 14:20:53 +00006635 u32 tmp_flag = flags & ~TXD_FLAG_END;
Matt Carlsona4cb4282011-12-14 11:09:58 +00006636 while (len > tp->dma_limit && *budget) {
6637 u32 frag_len = tp->dma_limit;
6638 len -= tp->dma_limit;
Matt Carlsone31aa982011-07-27 14:20:53 +00006639
Matt Carlsonb9e45482011-11-04 09:14:59 +00006640 /* Avoid the 8byte DMA problem */
6641 if (len <= 8) {
Matt Carlsona4cb4282011-12-14 11:09:58 +00006642 len += tp->dma_limit / 2;
6643 frag_len = tp->dma_limit / 2;
Matt Carlsone31aa982011-07-27 14:20:53 +00006644 }
6645
Matt Carlsonb9e45482011-11-04 09:14:59 +00006646 tnapi->tx_buffers[*entry].fragmented = true;
6647
6648 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6649 frag_len, tmp_flag, mss, vlan);
6650 *budget -= 1;
6651 prvidx = *entry;
6652 *entry = NEXT_TX(*entry);
6653
Matt Carlsone31aa982011-07-27 14:20:53 +00006654 map += frag_len;
6655 }
6656
6657 if (len) {
6658 if (*budget) {
6659 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6660 len, flags, mss, vlan);
Matt Carlsonb9e45482011-11-04 09:14:59 +00006661 *budget -= 1;
Matt Carlsone31aa982011-07-27 14:20:53 +00006662 *entry = NEXT_TX(*entry);
6663 } else {
Rusty Russell3db1cd52011-12-19 13:56:45 +00006664 hwbug = true;
Matt Carlsonb9e45482011-11-04 09:14:59 +00006665 tnapi->tx_buffers[prvidx].fragmented = false;
Matt Carlsone31aa982011-07-27 14:20:53 +00006666 }
6667 }
6668 } else {
Matt Carlson84b67b22011-07-27 14:20:52 +00006669 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6670 len, flags, mss, vlan);
Matt Carlsone31aa982011-07-27 14:20:53 +00006671 *entry = NEXT_TX(*entry);
6672 }
Matt Carlsond1a3b732011-07-27 14:20:51 +00006673
6674 return hwbug;
6675}
6676
Matt Carlson0d681b22011-07-27 14:20:49 +00006677static void tg3_tx_skb_unmap(struct tg3_napi *tnapi, u32 entry, int last)
Matt Carlson432aa7e2011-05-19 12:12:45 +00006678{
6679 int i;
Matt Carlson0d681b22011-07-27 14:20:49 +00006680 struct sk_buff *skb;
Matt Carlsondf8944c2011-07-27 14:20:46 +00006681 struct tg3_tx_ring_info *txb = &tnapi->tx_buffers[entry];
Matt Carlson432aa7e2011-05-19 12:12:45 +00006682
Matt Carlson0d681b22011-07-27 14:20:49 +00006683 skb = txb->skb;
6684 txb->skb = NULL;
6685
Matt Carlson432aa7e2011-05-19 12:12:45 +00006686 pci_unmap_single(tnapi->tp->pdev,
6687 dma_unmap_addr(txb, mapping),
6688 skb_headlen(skb),
6689 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00006690
6691 while (txb->fragmented) {
6692 txb->fragmented = false;
6693 entry = NEXT_TX(entry);
6694 txb = &tnapi->tx_buffers[entry];
6695 }
6696
Matt Carlsonba1142e2011-11-04 09:15:00 +00006697 for (i = 0; i <= last; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00006698 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Matt Carlson432aa7e2011-05-19 12:12:45 +00006699
6700 entry = NEXT_TX(entry);
6701 txb = &tnapi->tx_buffers[entry];
6702
6703 pci_unmap_page(tnapi->tp->pdev,
6704 dma_unmap_addr(txb, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00006705 skb_frag_size(frag), PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00006706
6707 while (txb->fragmented) {
6708 txb->fragmented = false;
6709 entry = NEXT_TX(entry);
6710 txb = &tnapi->tx_buffers[entry];
6711 }
Matt Carlson432aa7e2011-05-19 12:12:45 +00006712 }
6713}
6714
Michael Chan72f2afb2006-03-06 19:28:35 -08006715/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006716static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
David S. Miller1805b2f2011-10-24 18:18:09 -04006717 struct sk_buff **pskb,
Matt Carlson84b67b22011-07-27 14:20:52 +00006718 u32 *entry, u32 *budget,
Matt Carlson92cd3a12011-07-27 14:20:47 +00006719 u32 base_flags, u32 mss, u32 vlan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006720{
Matt Carlson24f4efd2009-11-13 13:03:35 +00006721 struct tg3 *tp = tnapi->tp;
David S. Miller1805b2f2011-10-24 18:18:09 -04006722 struct sk_buff *new_skb, *skb = *pskb;
Michael Chanc58ec932005-09-17 00:46:27 -07006723 dma_addr_t new_addr = 0;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006724 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006725
Matt Carlson41588ba12008-04-19 18:12:33 -07006726 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
6727 new_skb = skb_copy(skb, GFP_ATOMIC);
6728 else {
6729 int more_headroom = 4 - ((unsigned long)skb->data & 3);
6730
6731 new_skb = skb_copy_expand(skb,
6732 skb_headroom(skb) + more_headroom,
6733 skb_tailroom(skb), GFP_ATOMIC);
6734 }
6735
Linus Torvalds1da177e2005-04-16 15:20:36 -07006736 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07006737 ret = -1;
6738 } else {
6739 /* New SKB is guaranteed to be linear. */
Alexander Duyckf4188d82009-12-02 16:48:38 +00006740 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
6741 PCI_DMA_TODEVICE);
6742 /* Make sure the mapping succeeded */
6743 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00006744 dev_kfree_skb(new_skb);
Michael Chanc58ec932005-09-17 00:46:27 -07006745 ret = -1;
Michael Chanc58ec932005-09-17 00:46:27 -07006746 } else {
Matt Carlsonb9e45482011-11-04 09:14:59 +00006747 u32 save_entry = *entry;
6748
Matt Carlson92cd3a12011-07-27 14:20:47 +00006749 base_flags |= TXD_FLAG_END;
6750
Matt Carlson84b67b22011-07-27 14:20:52 +00006751 tnapi->tx_buffers[*entry].skb = new_skb;
6752 dma_unmap_addr_set(&tnapi->tx_buffers[*entry],
Matt Carlson432aa7e2011-05-19 12:12:45 +00006753 mapping, new_addr);
6754
Matt Carlson84b67b22011-07-27 14:20:52 +00006755 if (tg3_tx_frag_set(tnapi, entry, budget, new_addr,
Matt Carlsond1a3b732011-07-27 14:20:51 +00006756 new_skb->len, base_flags,
6757 mss, vlan)) {
Matt Carlsonba1142e2011-11-04 09:15:00 +00006758 tg3_tx_skb_unmap(tnapi, save_entry, -1);
Matt Carlsond1a3b732011-07-27 14:20:51 +00006759 dev_kfree_skb(new_skb);
6760 ret = -1;
6761 }
Michael Chanc58ec932005-09-17 00:46:27 -07006762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006763 }
6764
Linus Torvalds1da177e2005-04-16 15:20:36 -07006765 dev_kfree_skb(skb);
David S. Miller1805b2f2011-10-24 18:18:09 -04006766 *pskb = new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07006767 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006768}
6769
Matt Carlson2ffcc982011-05-19 12:12:44 +00006770static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07006771
6772/* Use GSO to workaround a rare TSO bug that may be triggered when the
6773 * TSO header is greater than 80 bytes.
6774 */
6775static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
6776{
6777 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006778 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07006779
6780 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006781 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07006782 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006783
6784 /* netif_tx_stop_queue() must be done before checking
6785 * checking tx index in tg3_tx_avail() below, because in
6786 * tg3_tx(), we update tx index before checking for
6787 * netif_tx_queue_stopped().
6788 */
6789 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006790 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08006791 return NETDEV_TX_BUSY;
6792
6793 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07006794 }
6795
6796 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07006797 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07006798 goto tg3_tso_bug_end;
6799
6800 do {
6801 nskb = segs;
6802 segs = segs->next;
6803 nskb->next = NULL;
Matt Carlson2ffcc982011-05-19 12:12:44 +00006804 tg3_start_xmit(nskb, tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07006805 } while (segs);
6806
6807tg3_tso_bug_end:
6808 dev_kfree_skb(skb);
6809
6810 return NETDEV_TX_OK;
6811}
Michael Chan52c0fd82006-06-29 20:15:54 -07006812
Michael Chan5a6f3072006-03-20 22:28:05 -08006813/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
Joe Perches63c3a662011-04-26 08:12:10 +00006814 * support TG3_FLAG_HW_TSO_1 or firmware TSO only.
Michael Chan5a6f3072006-03-20 22:28:05 -08006815 */
Matt Carlson2ffcc982011-05-19 12:12:44 +00006816static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08006817{
6818 struct tg3 *tp = netdev_priv(dev);
Matt Carlson92cd3a12011-07-27 14:20:47 +00006819 u32 len, entry, base_flags, mss, vlan = 0;
Matt Carlson84b67b22011-07-27 14:20:52 +00006820 u32 budget;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006821 int i = -1, would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07006822 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00006823 struct tg3_napi *tnapi;
6824 struct netdev_queue *txq;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006825 unsigned int last;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006826
Matt Carlson24f4efd2009-11-13 13:03:35 +00006827 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
6828 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Joe Perches63c3a662011-04-26 08:12:10 +00006829 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006830 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006831
Matt Carlson84b67b22011-07-27 14:20:52 +00006832 budget = tg3_tx_avail(tnapi);
6833
Michael Chan00b70502006-06-17 21:58:45 -07006834 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006835 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07006836 * interrupt. Furthermore, IRQ processing runs lockless so we have
6837 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07006838 */
Matt Carlson84b67b22011-07-27 14:20:52 +00006839 if (unlikely(budget <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006840 if (!netif_tx_queue_stopped(txq)) {
6841 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08006842
6843 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00006844 netdev_err(dev,
6845 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08006846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006847 return NETDEV_TX_BUSY;
6848 }
6849
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006850 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006851 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07006852 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006853 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00006854
Matt Carlsonbe98da62010-07-11 09:31:46 +00006855 mss = skb_shinfo(skb)->gso_size;
6856 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006857 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00006858 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006859
6860 if (skb_header_cloned(skb) &&
Eric Dumazet48855432011-10-24 07:53:03 +00006861 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
6862 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006863
Matt Carlson34195c32010-07-11 09:31:42 +00006864 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07006865 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006866
Eric Dumazeta5a11952012-01-23 01:22:09 +00006867 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb) - ETH_HLEN;
Matt Carlson34195c32010-07-11 09:31:42 +00006868
Eric Dumazeta5a11952012-01-23 01:22:09 +00006869 if (!skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00006870 iph->check = 0;
6871 iph->tot_len = htons(mss + hdr_len);
6872 }
6873
Michael Chan52c0fd82006-06-29 20:15:54 -07006874 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Joe Perches63c3a662011-04-26 08:12:10 +00006875 tg3_flag(tp, TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00006876 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07006877
Linus Torvalds1da177e2005-04-16 15:20:36 -07006878 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
6879 TXD_FLAG_CPU_POST_DMA);
6880
Joe Perches63c3a662011-04-26 08:12:10 +00006881 if (tg3_flag(tp, HW_TSO_1) ||
6882 tg3_flag(tp, HW_TSO_2) ||
6883 tg3_flag(tp, HW_TSO_3)) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006884 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006885 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006886 } else
6887 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
6888 iph->daddr, 0,
6889 IPPROTO_TCP,
6890 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006891
Joe Perches63c3a662011-04-26 08:12:10 +00006892 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlson615774f2009-11-13 13:03:39 +00006893 mss |= (hdr_len & 0xc) << 12;
6894 if (hdr_len & 0x10)
6895 base_flags |= 0x00000010;
6896 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +00006897 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006898 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +00006899 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006900 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006901 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006902 int tsflags;
6903
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006904 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006905 mss |= (tsflags << 11);
6906 }
6907 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006908 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006909 int tsflags;
6910
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006911 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006912 base_flags |= tsflags << 12;
6913 }
6914 }
6915 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00006916
Matt Carlson93a700a2011-08-31 11:44:54 +00006917 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
6918 !mss && skb->len > VLAN_ETH_FRAME_LEN)
6919 base_flags |= TXD_FLAG_JMB_PKT;
6920
Matt Carlson92cd3a12011-07-27 14:20:47 +00006921 if (vlan_tx_tag_present(skb)) {
6922 base_flags |= TXD_FLAG_VLAN;
6923 vlan = vlan_tx_tag_get(skb);
6924 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006925
Alexander Duyckf4188d82009-12-02 16:48:38 +00006926 len = skb_headlen(skb);
6927
6928 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
Eric Dumazet48855432011-10-24 07:53:03 +00006929 if (pci_dma_mapping_error(tp->pdev, mapping))
6930 goto drop;
6931
David S. Miller90079ce2008-09-11 04:52:51 -07006932
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006933 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006934 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006935
6936 would_hit_hwbug = 0;
6937
Joe Perches63c3a662011-04-26 08:12:10 +00006938 if (tg3_flag(tp, 5701_DMA_BUG))
Michael Chanc58ec932005-09-17 00:46:27 -07006939 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006940
Matt Carlson84b67b22011-07-27 14:20:52 +00006941 if (tg3_tx_frag_set(tnapi, &entry, &budget, mapping, len, base_flags |
Matt Carlsond1a3b732011-07-27 14:20:51 +00006942 ((skb_shinfo(skb)->nr_frags == 0) ? TXD_FLAG_END : 0),
Matt Carlsonba1142e2011-11-04 09:15:00 +00006943 mss, vlan)) {
Matt Carlsond1a3b732011-07-27 14:20:51 +00006944 would_hit_hwbug = 1;
Matt Carlsonba1142e2011-11-04 09:15:00 +00006945 } else if (skb_shinfo(skb)->nr_frags > 0) {
Matt Carlson92cd3a12011-07-27 14:20:47 +00006946 u32 tmp_mss = mss;
6947
6948 if (!tg3_flag(tp, HW_TSO_1) &&
6949 !tg3_flag(tp, HW_TSO_2) &&
6950 !tg3_flag(tp, HW_TSO_3))
6951 tmp_mss = 0;
6952
Matt Carlsonc5665a52012-02-13 10:20:12 +00006953 /* Now loop through additional data
6954 * fragments, and queue them.
6955 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006956 last = skb_shinfo(skb)->nr_frags - 1;
6957 for (i = 0; i <= last; i++) {
6958 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6959
Eric Dumazet9e903e02011-10-18 21:00:24 +00006960 len = skb_frag_size(frag);
Ian Campbelldc234d02011-08-24 22:28:11 +00006961 mapping = skb_frag_dma_map(&tp->pdev->dev, frag, 0,
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01006962 len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006963
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006964 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006965 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00006966 mapping);
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01006967 if (dma_mapping_error(&tp->pdev->dev, mapping))
Alexander Duyckf4188d82009-12-02 16:48:38 +00006968 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006969
Matt Carlsonb9e45482011-11-04 09:14:59 +00006970 if (!budget ||
6971 tg3_tx_frag_set(tnapi, &entry, &budget, mapping,
Matt Carlson84b67b22011-07-27 14:20:52 +00006972 len, base_flags |
6973 ((i == last) ? TXD_FLAG_END : 0),
Matt Carlsonb9e45482011-11-04 09:14:59 +00006974 tmp_mss, vlan)) {
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006975 would_hit_hwbug = 1;
Matt Carlsonb9e45482011-11-04 09:14:59 +00006976 break;
6977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006978 }
6979 }
6980
6981 if (would_hit_hwbug) {
Matt Carlson0d681b22011-07-27 14:20:49 +00006982 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006983
6984 /* If the workaround fails due to memory/mapping
6985 * failure, silently drop this packet.
6986 */
Matt Carlson84b67b22011-07-27 14:20:52 +00006987 entry = tnapi->tx_prod;
6988 budget = tg3_tx_avail(tnapi);
David S. Miller1805b2f2011-10-24 18:18:09 -04006989 if (tigon3_dma_hwbug_workaround(tnapi, &skb, &entry, &budget,
Matt Carlson84b67b22011-07-27 14:20:52 +00006990 base_flags, mss, vlan))
Eric Dumazet48855432011-10-24 07:53:03 +00006991 goto drop_nofree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006992 }
6993
Richard Cochrand515b452011-06-19 03:31:41 +00006994 skb_tx_timestamp(skb);
Tom Herbert5cb917b2012-03-05 19:53:50 +00006995 netdev_tx_sent_queue(txq, skb->len);
Richard Cochrand515b452011-06-19 03:31:41 +00006996
Michael Chan6541b802012-03-04 14:48:14 +00006997 /* Sync BD data before updating mailbox */
6998 wmb();
6999
Linus Torvalds1da177e2005-04-16 15:20:36 -07007000 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00007001 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007002
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007003 tnapi->tx_prod = entry;
7004 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00007005 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00007006
7007 /* netif_tx_stop_queue() must be done before checking
7008 * checking tx index in tg3_tx_avail() below, because in
7009 * tg3_tx(), we update tx index before checking for
7010 * netif_tx_queue_stopped().
7011 */
7012 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007013 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00007014 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07007015 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007016
Eric Dumazetcdd0db02009-05-28 00:00:41 +00007017 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007018 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007019
7020dma_error:
Matt Carlsonba1142e2011-11-04 09:15:00 +00007021 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, --i);
Matt Carlson432aa7e2011-05-19 12:12:45 +00007022 tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
Eric Dumazet48855432011-10-24 07:53:03 +00007023drop:
7024 dev_kfree_skb(skb);
7025drop_nofree:
7026 tp->tx_dropped++;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007027 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007028}
7029
Matt Carlson6e01b202011-08-19 13:58:20 +00007030static void tg3_mac_loopback(struct tg3 *tp, bool enable)
7031{
7032 if (enable) {
7033 tp->mac_mode &= ~(MAC_MODE_HALF_DUPLEX |
7034 MAC_MODE_PORT_MODE_MASK);
7035
7036 tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
7037
7038 if (!tg3_flag(tp, 5705_PLUS))
7039 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
7040
7041 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
7042 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
7043 else
7044 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
7045 } else {
7046 tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
7047
7048 if (tg3_flag(tp, 5705_PLUS) ||
7049 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) ||
7050 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
7051 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
7052 }
7053
7054 tw32(MAC_MODE, tp->mac_mode);
7055 udelay(40);
7056}
7057
Matt Carlson941ec902011-08-19 13:58:23 +00007058static int tg3_phy_lpbk_set(struct tg3 *tp, u32 speed, bool extlpbk)
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007059{
Matt Carlson941ec902011-08-19 13:58:23 +00007060 u32 val, bmcr, mac_mode, ptest = 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007061
7062 tg3_phy_toggle_apd(tp, false);
7063 tg3_phy_toggle_automdix(tp, 0);
7064
Matt Carlson941ec902011-08-19 13:58:23 +00007065 if (extlpbk && tg3_phy_set_extloopbk(tp))
7066 return -EIO;
7067
7068 bmcr = BMCR_FULLDPLX;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007069 switch (speed) {
7070 case SPEED_10:
7071 break;
7072 case SPEED_100:
7073 bmcr |= BMCR_SPEED100;
7074 break;
7075 case SPEED_1000:
7076 default:
7077 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
7078 speed = SPEED_100;
7079 bmcr |= BMCR_SPEED100;
7080 } else {
7081 speed = SPEED_1000;
7082 bmcr |= BMCR_SPEED1000;
7083 }
7084 }
7085
Matt Carlson941ec902011-08-19 13:58:23 +00007086 if (extlpbk) {
7087 if (!(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
7088 tg3_readphy(tp, MII_CTRL1000, &val);
7089 val |= CTL1000_AS_MASTER |
7090 CTL1000_ENABLE_MASTER;
7091 tg3_writephy(tp, MII_CTRL1000, val);
7092 } else {
7093 ptest = MII_TG3_FET_PTEST_TRIM_SEL |
7094 MII_TG3_FET_PTEST_TRIM_2;
7095 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest);
7096 }
7097 } else
7098 bmcr |= BMCR_LOOPBACK;
7099
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007100 tg3_writephy(tp, MII_BMCR, bmcr);
7101
7102 /* The write needs to be flushed for the FETs */
7103 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
7104 tg3_readphy(tp, MII_BMCR, &bmcr);
7105
7106 udelay(40);
7107
7108 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
7109 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Matt Carlson941ec902011-08-19 13:58:23 +00007110 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest |
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007111 MII_TG3_FET_PTEST_FRC_TX_LINK |
7112 MII_TG3_FET_PTEST_FRC_TX_LOCK);
7113
7114 /* The write needs to be flushed for the AC131 */
7115 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
7116 }
7117
7118 /* Reset to prevent losing 1st rx packet intermittently */
7119 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
7120 tg3_flag(tp, 5780_CLASS)) {
7121 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
7122 udelay(10);
7123 tw32_f(MAC_RX_MODE, tp->rx_mode);
7124 }
7125
7126 mac_mode = tp->mac_mode &
7127 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
7128 if (speed == SPEED_1000)
7129 mac_mode |= MAC_MODE_PORT_MODE_GMII;
7130 else
7131 mac_mode |= MAC_MODE_PORT_MODE_MII;
7132
7133 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
7134 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
7135
7136 if (masked_phy_id == TG3_PHY_ID_BCM5401)
7137 mac_mode &= ~MAC_MODE_LINK_POLARITY;
7138 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
7139 mac_mode |= MAC_MODE_LINK_POLARITY;
7140
7141 tg3_writephy(tp, MII_TG3_EXT_CTRL,
7142 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
7143 }
7144
7145 tw32(MAC_MODE, mac_mode);
7146 udelay(40);
Matt Carlson941ec902011-08-19 13:58:23 +00007147
7148 return 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007149}
7150
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007151static void tg3_set_loopback(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007152{
7153 struct tg3 *tp = netdev_priv(dev);
7154
7155 if (features & NETIF_F_LOOPBACK) {
7156 if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
7157 return;
7158
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007159 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00007160 tg3_mac_loopback(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007161 netif_carrier_on(tp->dev);
7162 spin_unlock_bh(&tp->lock);
7163 netdev_info(dev, "Internal MAC loopback mode enabled.\n");
7164 } else {
7165 if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
7166 return;
7167
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007168 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00007169 tg3_mac_loopback(tp, false);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007170 /* Force link status check */
7171 tg3_setup_phy(tp, 1);
7172 spin_unlock_bh(&tp->lock);
7173 netdev_info(dev, "Internal MAC loopback mode disabled.\n");
7174 }
7175}
7176
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007177static netdev_features_t tg3_fix_features(struct net_device *dev,
7178 netdev_features_t features)
Michał Mirosławdc668912011-04-07 03:35:07 +00007179{
7180 struct tg3 *tp = netdev_priv(dev);
7181
Joe Perches63c3a662011-04-26 08:12:10 +00007182 if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
Michał Mirosławdc668912011-04-07 03:35:07 +00007183 features &= ~NETIF_F_ALL_TSO;
7184
7185 return features;
7186}
7187
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007188static int tg3_set_features(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007189{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007190 netdev_features_t changed = dev->features ^ features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007191
7192 if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
7193 tg3_set_loopback(dev, features);
7194
7195 return 0;
7196}
7197
Matt Carlson21f581a2009-08-28 14:00:25 +00007198static void tg3_rx_prodring_free(struct tg3 *tp,
7199 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007200{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007201 int i;
7202
Matt Carlson8fea32b2010-09-15 08:59:58 +00007203 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007204 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00007205 i = (i + 1) & tp->rx_std_ring_mask)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007206 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007207 tp->rx_pkt_map_sz);
7208
Joe Perches63c3a662011-04-26 08:12:10 +00007209 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007210 for (i = tpr->rx_jmb_cons_idx;
7211 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00007212 i = (i + 1) & tp->rx_jmb_ring_mask) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007213 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007214 TG3_RX_JMB_MAP_SZ);
7215 }
7216 }
7217
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007218 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007220
Matt Carlson2c49a442010-09-30 10:34:35 +00007221 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007222 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007223 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007224
Joe Perches63c3a662011-04-26 08:12:10 +00007225 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007226 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007227 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007228 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007229 }
7230}
7231
Matt Carlsonc6cdf432010-04-05 10:19:26 +00007232/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007233 *
7234 * The chip has been shut down and the driver detached from
7235 * the networking, so no interrupts or new tx packets will
7236 * end up in the driver. tp->{tx,}lock are held and thus
7237 * we may not sleep.
7238 */
Matt Carlson21f581a2009-08-28 14:00:25 +00007239static int tg3_rx_prodring_alloc(struct tg3 *tp,
7240 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007241{
Matt Carlson287be122009-08-28 13:58:46 +00007242 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007243
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007244 tpr->rx_std_cons_idx = 0;
7245 tpr->rx_std_prod_idx = 0;
7246 tpr->rx_jmb_cons_idx = 0;
7247 tpr->rx_jmb_prod_idx = 0;
7248
Matt Carlson8fea32b2010-09-15 08:59:58 +00007249 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007250 memset(&tpr->rx_std_buffers[0], 0,
7251 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00007252 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007253 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00007254 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007255 goto done;
7256 }
7257
Linus Torvalds1da177e2005-04-16 15:20:36 -07007258 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00007259 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007260
Matt Carlson287be122009-08-28 13:58:46 +00007261 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00007262 if (tg3_flag(tp, 5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00007263 tp->dev->mtu > ETH_DATA_LEN)
7264 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
7265 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad42005-07-25 12:31:17 -07007266
Linus Torvalds1da177e2005-04-16 15:20:36 -07007267 /* Initialize invariants of the rings, we only set this
7268 * stuff once. This works because the card does not
7269 * write into the rx buffer posting rings.
7270 */
Matt Carlson2c49a442010-09-30 10:34:35 +00007271 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007272 struct tg3_rx_buffer_desc *rxd;
7273
Matt Carlson21f581a2009-08-28 14:00:25 +00007274 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00007275 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007276 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
7277 rxd->opaque = (RXD_OPAQUE_RING_STD |
7278 (i << RXD_OPAQUE_INDEX_SHIFT));
7279 }
7280
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007281 /* Now allocate fresh SKBs for each rx ring. */
7282 for (i = 0; i < tp->rx_pending; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007283 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_STD, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007284 netdev_warn(tp->dev,
7285 "Using a smaller RX standard ring. Only "
7286 "%d out of %d buffers were allocated "
7287 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007288 if (i == 0)
7289 goto initfail;
7290 tp->rx_pending = i;
7291 break;
7292 }
7293 }
7294
Joe Perches63c3a662011-04-26 08:12:10 +00007295 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007296 goto done;
7297
Matt Carlson2c49a442010-09-30 10:34:35 +00007298 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007299
Joe Perches63c3a662011-04-26 08:12:10 +00007300 if (!tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson0d86df82010-02-17 15:17:00 +00007301 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007302
Matt Carlson2c49a442010-09-30 10:34:35 +00007303 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00007304 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007305
Matt Carlson0d86df82010-02-17 15:17:00 +00007306 rxd = &tpr->rx_jmb[i].std;
7307 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
7308 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
7309 RXD_FLAG_JUMBO;
7310 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
7311 (i << RXD_OPAQUE_INDEX_SHIFT));
7312 }
7313
7314 for (i = 0; i < tp->rx_jumbo_pending; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007315 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_JUMBO, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007316 netdev_warn(tp->dev,
7317 "Using a smaller RX jumbo ring. Only %d "
7318 "out of %d buffers were allocated "
7319 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00007320 if (i == 0)
7321 goto initfail;
7322 tp->rx_jumbo_pending = i;
7323 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007324 }
7325 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007326
7327done:
Michael Chan32d8c572006-07-25 16:38:29 -07007328 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007329
7330initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00007331 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007332 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007333}
7334
Matt Carlson21f581a2009-08-28 14:00:25 +00007335static void tg3_rx_prodring_fini(struct tg3 *tp,
7336 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007337{
Matt Carlson21f581a2009-08-28 14:00:25 +00007338 kfree(tpr->rx_std_buffers);
7339 tpr->rx_std_buffers = NULL;
7340 kfree(tpr->rx_jmb_buffers);
7341 tpr->rx_jmb_buffers = NULL;
7342 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007343 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
7344 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00007345 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007346 }
Matt Carlson21f581a2009-08-28 14:00:25 +00007347 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007348 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
7349 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00007350 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007351 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007352}
7353
Matt Carlson21f581a2009-08-28 14:00:25 +00007354static int tg3_rx_prodring_init(struct tg3 *tp,
7355 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007356{
Matt Carlson2c49a442010-09-30 10:34:35 +00007357 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
7358 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007359 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007360 return -ENOMEM;
7361
Matt Carlson4bae65c2010-11-24 08:31:52 +00007362 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
7363 TG3_RX_STD_RING_BYTES(tp),
7364 &tpr->rx_std_mapping,
7365 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007366 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007367 goto err_out;
7368
Joe Perches63c3a662011-04-26 08:12:10 +00007369 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007370 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00007371 GFP_KERNEL);
7372 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007373 goto err_out;
7374
Matt Carlson4bae65c2010-11-24 08:31:52 +00007375 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
7376 TG3_RX_JMB_RING_BYTES(tp),
7377 &tpr->rx_jmb_mapping,
7378 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007379 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007380 goto err_out;
7381 }
7382
7383 return 0;
7384
7385err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00007386 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007387 return -ENOMEM;
7388}
7389
7390/* Free up pending packets in all rx/tx rings.
7391 *
7392 * The chip has been shut down and the driver detached from
7393 * the networking, so no interrupts or new tx packets will
7394 * end up in the driver. tp->{tx,}lock is not held and we are not
7395 * in an interrupt context and thus may sleep.
7396 */
7397static void tg3_free_rings(struct tg3 *tp)
7398{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007399 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007400
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007401 for (j = 0; j < tp->irq_cnt; j++) {
7402 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007403
Matt Carlson8fea32b2010-09-15 08:59:58 +00007404 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00007405
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007406 if (!tnapi->tx_buffers)
7407 continue;
7408
Matt Carlson0d681b22011-07-27 14:20:49 +00007409 for (i = 0; i < TG3_TX_RING_SIZE; i++) {
7410 struct sk_buff *skb = tnapi->tx_buffers[i].skb;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007411
Matt Carlson0d681b22011-07-27 14:20:49 +00007412 if (!skb)
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007413 continue;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007414
Matt Carlsonba1142e2011-11-04 09:15:00 +00007415 tg3_tx_skb_unmap(tnapi, i,
7416 skb_shinfo(skb)->nr_frags - 1);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007417
7418 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007419 }
Tom Herbert5cb917b2012-03-05 19:53:50 +00007420 netdev_tx_reset_queue(netdev_get_tx_queue(tp->dev, j));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007421 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007422}
7423
7424/* Initialize tx/rx rings for packet processing.
7425 *
7426 * The chip has been shut down and the driver detached from
7427 * the networking, so no interrupts or new tx packets will
7428 * end up in the driver. tp->{tx,}lock are held and thus
7429 * we may not sleep.
7430 */
7431static int tg3_init_rings(struct tg3 *tp)
7432{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007433 int i;
Matt Carlson72334482009-08-28 14:03:01 +00007434
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007435 /* Free up all the SKBs. */
7436 tg3_free_rings(tp);
7437
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007438 for (i = 0; i < tp->irq_cnt; i++) {
7439 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007440
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007441 tnapi->last_tag = 0;
7442 tnapi->last_irq_tag = 0;
7443 tnapi->hw_status->status = 0;
7444 tnapi->hw_status->status_tag = 0;
7445 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7446
7447 tnapi->tx_prod = 0;
7448 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007449 if (tnapi->tx_ring)
7450 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007451
7452 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007453 if (tnapi->rx_rcb)
7454 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007455
Matt Carlson8fea32b2010-09-15 08:59:58 +00007456 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00007457 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007458 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00007459 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007460 }
Matt Carlson72334482009-08-28 14:03:01 +00007461
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007462 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007463}
7464
7465/*
7466 * Must not be invoked with interrupt sources disabled and
7467 * the hardware shutdown down.
7468 */
7469static void tg3_free_consistent(struct tg3 *tp)
7470{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007471 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00007472
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007473 for (i = 0; i < tp->irq_cnt; i++) {
7474 struct tg3_napi *tnapi = &tp->napi[i];
7475
7476 if (tnapi->tx_ring) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007477 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007478 tnapi->tx_ring, tnapi->tx_desc_mapping);
7479 tnapi->tx_ring = NULL;
7480 }
7481
7482 kfree(tnapi->tx_buffers);
7483 tnapi->tx_buffers = NULL;
7484
7485 if (tnapi->rx_rcb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007486 dma_free_coherent(&tp->pdev->dev,
7487 TG3_RX_RCB_RING_BYTES(tp),
7488 tnapi->rx_rcb,
7489 tnapi->rx_rcb_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007490 tnapi->rx_rcb = NULL;
7491 }
7492
Matt Carlson8fea32b2010-09-15 08:59:58 +00007493 tg3_rx_prodring_fini(tp, &tnapi->prodring);
7494
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007495 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007496 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
7497 tnapi->hw_status,
7498 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007499 tnapi->hw_status = NULL;
7500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007501 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007502
Linus Torvalds1da177e2005-04-16 15:20:36 -07007503 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007504 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
7505 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007506 tp->hw_stats = NULL;
7507 }
7508}
7509
7510/*
7511 * Must not be invoked with interrupt sources disabled and
7512 * the hardware shutdown down. Can sleep.
7513 */
7514static int tg3_alloc_consistent(struct tg3 *tp)
7515{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007516 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00007517
Matt Carlson4bae65c2010-11-24 08:31:52 +00007518 tp->hw_stats = dma_alloc_coherent(&tp->pdev->dev,
7519 sizeof(struct tg3_hw_stats),
7520 &tp->stats_mapping,
7521 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007522 if (!tp->hw_stats)
7523 goto err_out;
7524
Linus Torvalds1da177e2005-04-16 15:20:36 -07007525 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
7526
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007527 for (i = 0; i < tp->irq_cnt; i++) {
7528 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007529 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007530
Matt Carlson4bae65c2010-11-24 08:31:52 +00007531 tnapi->hw_status = dma_alloc_coherent(&tp->pdev->dev,
7532 TG3_HW_STATUS_SIZE,
7533 &tnapi->status_mapping,
7534 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007535 if (!tnapi->hw_status)
7536 goto err_out;
7537
7538 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007539 sblk = tnapi->hw_status;
7540
Matt Carlson8fea32b2010-09-15 08:59:58 +00007541 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
7542 goto err_out;
7543
Matt Carlson19cfaec2009-12-03 08:36:20 +00007544 /* If multivector TSS is enabled, vector 0 does not handle
7545 * tx interrupts. Don't allocate any resources for it.
7546 */
Joe Perches63c3a662011-04-26 08:12:10 +00007547 if ((!i && !tg3_flag(tp, ENABLE_TSS)) ||
7548 (i && tg3_flag(tp, ENABLE_TSS))) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00007549 tnapi->tx_buffers = kzalloc(
7550 sizeof(struct tg3_tx_ring_info) *
7551 TG3_TX_RING_SIZE, GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007552 if (!tnapi->tx_buffers)
7553 goto err_out;
7554
Matt Carlson4bae65c2010-11-24 08:31:52 +00007555 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
7556 TG3_TX_RING_BYTES,
7557 &tnapi->tx_desc_mapping,
7558 GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007559 if (!tnapi->tx_ring)
7560 goto err_out;
7561 }
7562
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007563 /*
7564 * When RSS is enabled, the status block format changes
7565 * slightly. The "rx_jumbo_consumer", "reserved",
7566 * and "rx_mini_consumer" members get mapped to the
7567 * other three rx return ring producer indexes.
7568 */
7569 switch (i) {
7570 default:
7571 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
7572 break;
7573 case 2:
7574 tnapi->rx_rcb_prod_idx = &sblk->rx_jumbo_consumer;
7575 break;
7576 case 3:
7577 tnapi->rx_rcb_prod_idx = &sblk->reserved;
7578 break;
7579 case 4:
7580 tnapi->rx_rcb_prod_idx = &sblk->rx_mini_consumer;
7581 break;
7582 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007583
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007584 /*
7585 * If multivector RSS is enabled, vector 0 does not handle
7586 * rx or tx interrupts. Don't allocate any resources for it.
7587 */
Joe Perches63c3a662011-04-26 08:12:10 +00007588 if (!i && tg3_flag(tp, ENABLE_RSS))
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007589 continue;
7590
Matt Carlson4bae65c2010-11-24 08:31:52 +00007591 tnapi->rx_rcb = dma_alloc_coherent(&tp->pdev->dev,
7592 TG3_RX_RCB_RING_BYTES(tp),
7593 &tnapi->rx_rcb_mapping,
7594 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007595 if (!tnapi->rx_rcb)
7596 goto err_out;
7597
7598 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007599 }
7600
Linus Torvalds1da177e2005-04-16 15:20:36 -07007601 return 0;
7602
7603err_out:
7604 tg3_free_consistent(tp);
7605 return -ENOMEM;
7606}
7607
7608#define MAX_WAIT_CNT 1000
7609
7610/* To stop a block, clear the enable bit and poll till it
7611 * clears. tp->lock is held.
7612 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007613static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007614{
7615 unsigned int i;
7616 u32 val;
7617
Joe Perches63c3a662011-04-26 08:12:10 +00007618 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007619 switch (ofs) {
7620 case RCVLSC_MODE:
7621 case DMAC_MODE:
7622 case MBFREE_MODE:
7623 case BUFMGR_MODE:
7624 case MEMARB_MODE:
7625 /* We can't enable/disable these bits of the
7626 * 5705/5750, just say success.
7627 */
7628 return 0;
7629
7630 default:
7631 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007633 }
7634
7635 val = tr32(ofs);
7636 val &= ~enable_bit;
7637 tw32_f(ofs, val);
7638
7639 for (i = 0; i < MAX_WAIT_CNT; i++) {
7640 udelay(100);
7641 val = tr32(ofs);
7642 if ((val & enable_bit) == 0)
7643 break;
7644 }
7645
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007646 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00007647 dev_err(&tp->pdev->dev,
7648 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
7649 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007650 return -ENODEV;
7651 }
7652
7653 return 0;
7654}
7655
7656/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007657static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007658{
7659 int i, err;
7660
7661 tg3_disable_ints(tp);
7662
7663 tp->rx_mode &= ~RX_MODE_ENABLE;
7664 tw32_f(MAC_RX_MODE, tp->rx_mode);
7665 udelay(10);
7666
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007667 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
7668 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
7669 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
7670 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
7671 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
7672 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007673
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007674 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
7675 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
7676 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
7677 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
7678 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
7679 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
7680 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007681
7682 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
7683 tw32_f(MAC_MODE, tp->mac_mode);
7684 udelay(40);
7685
7686 tp->tx_mode &= ~TX_MODE_ENABLE;
7687 tw32_f(MAC_TX_MODE, tp->tx_mode);
7688
7689 for (i = 0; i < MAX_WAIT_CNT; i++) {
7690 udelay(100);
7691 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
7692 break;
7693 }
7694 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00007695 dev_err(&tp->pdev->dev,
7696 "%s timed out, TX_MODE_ENABLE will not clear "
7697 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07007698 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007699 }
7700
Michael Chane6de8ad2005-05-05 14:42:41 -07007701 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007702 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
7703 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007704
7705 tw32(FTQ_RESET, 0xffffffff);
7706 tw32(FTQ_RESET, 0x00000000);
7707
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007708 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
7709 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007710
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007711 for (i = 0; i < tp->irq_cnt; i++) {
7712 struct tg3_napi *tnapi = &tp->napi[i];
7713 if (tnapi->hw_status)
7714 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007716
Linus Torvalds1da177e2005-04-16 15:20:36 -07007717 return err;
7718}
7719
Michael Chanee6a99b2007-07-18 21:49:10 -07007720/* Save PCI command register before chip reset */
7721static void tg3_save_pci_state(struct tg3 *tp)
7722{
Matt Carlson8a6eac92007-10-21 16:17:55 -07007723 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007724}
7725
7726/* Restore PCI state after chip reset */
7727static void tg3_restore_pci_state(struct tg3 *tp)
7728{
7729 u32 val;
7730
7731 /* Re-enable indirect register accesses. */
7732 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
7733 tp->misc_host_ctrl);
7734
7735 /* Set MAX PCI retry to zero. */
7736 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
7737 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007738 tg3_flag(tp, PCIX_MODE))
Michael Chanee6a99b2007-07-18 21:49:10 -07007739 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07007740 /* Allow reads and writes to the APE register and memory space. */
Joe Perches63c3a662011-04-26 08:12:10 +00007741 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07007742 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00007743 PCISTATE_ALLOW_APE_SHMEM_WR |
7744 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07007745 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
7746
Matt Carlson8a6eac92007-10-21 16:17:55 -07007747 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007748
Matt Carlson2c55a3d2011-11-28 09:41:04 +00007749 if (!tg3_flag(tp, PCI_EXPRESS)) {
7750 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
7751 tp->pci_cacheline_sz);
7752 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
7753 tp->pci_lat_timer);
Michael Chan114342f2007-10-15 02:12:26 -07007754 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08007755
Michael Chanee6a99b2007-07-18 21:49:10 -07007756 /* Make sure PCI-X relaxed ordering bit is clear. */
Joe Perches63c3a662011-04-26 08:12:10 +00007757 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07007758 u16 pcix_cmd;
7759
7760 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7761 &pcix_cmd);
7762 pcix_cmd &= ~PCI_X_CMD_ERO;
7763 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7764 pcix_cmd);
7765 }
Michael Chanee6a99b2007-07-18 21:49:10 -07007766
Joe Perches63c3a662011-04-26 08:12:10 +00007767 if (tg3_flag(tp, 5780_CLASS)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007768
7769 /* Chip reset on 5780 will reset MSI enable bit,
7770 * so need to restore it.
7771 */
Joe Perches63c3a662011-04-26 08:12:10 +00007772 if (tg3_flag(tp, USING_MSI)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007773 u16 ctrl;
7774
7775 pci_read_config_word(tp->pdev,
7776 tp->msi_cap + PCI_MSI_FLAGS,
7777 &ctrl);
7778 pci_write_config_word(tp->pdev,
7779 tp->msi_cap + PCI_MSI_FLAGS,
7780 ctrl | PCI_MSI_FLAGS_ENABLE);
7781 val = tr32(MSGINT_MODE);
7782 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
7783 }
7784 }
7785}
7786
Linus Torvalds1da177e2005-04-16 15:20:36 -07007787/* tp->lock is held. */
7788static int tg3_chip_reset(struct tg3 *tp)
7789{
7790 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07007791 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00007792 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007793
David S. Millerf49639e2006-06-09 11:58:36 -07007794 tg3_nvram_lock(tp);
7795
Matt Carlson77b483f2008-08-15 14:07:24 -07007796 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
7797
David S. Millerf49639e2006-06-09 11:58:36 -07007798 /* No matching tg3_nvram_unlock() after this because
7799 * chip reset below will undo the nvram lock.
7800 */
7801 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007802
Michael Chanee6a99b2007-07-18 21:49:10 -07007803 /* GRC_MISC_CFG core clock reset will clear the memory
7804 * enable bit in PCI register 4 and the MSI enable bit
7805 * on some chips, so we save relevant registers here.
7806 */
7807 tg3_save_pci_state(tp);
7808
Michael Chand9ab5ad12006-03-20 22:27:35 -08007809 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Joe Perches63c3a662011-04-26 08:12:10 +00007810 tg3_flag(tp, 5755_PLUS))
Michael Chand9ab5ad12006-03-20 22:27:35 -08007811 tw32(GRC_FASTBOOT_PC, 0);
7812
Linus Torvalds1da177e2005-04-16 15:20:36 -07007813 /*
7814 * We must avoid the readl() that normally takes place.
7815 * It locks machines, causes machine checks, and other
7816 * fun things. So, temporarily disable the 5701
7817 * hardware workaround, while we do the reset.
7818 */
Michael Chan1ee582d2005-08-09 20:16:46 -07007819 write_op = tp->write32;
7820 if (write_op == tg3_write_flush_reg32)
7821 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007822
Michael Chand18edcb2007-03-24 20:57:11 -07007823 /* Prevent the irq handler from reading or writing PCI registers
7824 * during chip reset when the memory enable bit in the PCI command
7825 * register may be cleared. The chip does not generate interrupt
7826 * at this time, but the irq handler may still be called due to irq
7827 * sharing or irqpoll.
7828 */
Joe Perches63c3a662011-04-26 08:12:10 +00007829 tg3_flag_set(tp, CHIP_RESETTING);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007830 for (i = 0; i < tp->irq_cnt; i++) {
7831 struct tg3_napi *tnapi = &tp->napi[i];
7832 if (tnapi->hw_status) {
7833 tnapi->hw_status->status = 0;
7834 tnapi->hw_status->status_tag = 0;
7835 }
7836 tnapi->last_tag = 0;
7837 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07007838 }
Michael Chand18edcb2007-03-24 20:57:11 -07007839 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00007840
7841 for (i = 0; i < tp->irq_cnt; i++)
7842 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07007843
Matt Carlson255ca312009-08-25 10:07:27 +00007844 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7845 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7846 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
7847 }
7848
Linus Torvalds1da177e2005-04-16 15:20:36 -07007849 /* do the reset */
7850 val = GRC_MISC_CFG_CORECLK_RESET;
7851
Joe Perches63c3a662011-04-26 08:12:10 +00007852 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson88075d92010-08-02 11:25:58 +00007853 /* Force PCIe 1.0a mode */
7854 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007855 !tg3_flag(tp, 57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00007856 tr32(TG3_PCIE_PHY_TSTCTL) ==
7857 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
7858 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
7859
Linus Torvalds1da177e2005-04-16 15:20:36 -07007860 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
7861 tw32(GRC_MISC_CFG, (1 << 29));
7862 val |= (1 << 29);
7863 }
7864 }
7865
Michael Chanb5d37722006-09-27 16:06:21 -07007866 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7867 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
7868 tw32(GRC_VCPU_EXT_CTRL,
7869 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
7870 }
7871
Matt Carlsonf37500d2010-08-02 11:25:59 +00007872 /* Manage gphy power for all CPMU absent PCIe devices. */
Joe Perches63c3a662011-04-26 08:12:10 +00007873 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007874 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00007875
Linus Torvalds1da177e2005-04-16 15:20:36 -07007876 tw32(GRC_MISC_CFG, val);
7877
Michael Chan1ee582d2005-08-09 20:16:46 -07007878 /* restore 5701 hardware bug workaround write method */
7879 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007880
7881 /* Unfortunately, we have to delay before the PCI read back.
7882 * Some 575X chips even will not respond to a PCI cfg access
7883 * when the reset command is given to the chip.
7884 *
7885 * How do these hardware designers expect things to work
7886 * properly if the PCI write is posted for a long period
7887 * of time? It is always necessary to have some method by
7888 * which a register read back can occur to push the write
7889 * out which does the reset.
7890 *
7891 * For most tg3 variants the trick below was working.
7892 * Ho hum...
7893 */
7894 udelay(120);
7895
7896 /* Flush PCI posted writes. The normal MMIO registers
7897 * are inaccessible at this time so this is the only
7898 * way to make this reliably (actually, this is no longer
7899 * the case, see above). I tried to use indirect
7900 * register read/write but this upset some 5701 variants.
7901 */
7902 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
7903
7904 udelay(120);
7905
Jon Mason708ebb3a2011-06-27 12:56:50 +00007906 if (tg3_flag(tp, PCI_EXPRESS) && pci_pcie_cap(tp->pdev)) {
Matt Carlsone7126992009-08-25 10:08:16 +00007907 u16 val16;
7908
Linus Torvalds1da177e2005-04-16 15:20:36 -07007909 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
7910 int i;
7911 u32 cfg_val;
7912
7913 /* Wait for link training to complete. */
7914 for (i = 0; i < 5000; i++)
7915 udelay(100);
7916
7917 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
7918 pci_write_config_dword(tp->pdev, 0xc4,
7919 cfg_val | (1 << 15));
7920 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007921
Matt Carlsone7126992009-08-25 10:08:16 +00007922 /* Clear the "no snoop" and "relaxed ordering" bits. */
7923 pci_read_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +00007924 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007925 &val16);
7926 val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
7927 PCI_EXP_DEVCTL_NOSNOOP_EN);
7928 /*
7929 * Older PCIe devices only support the 128 byte
7930 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007931 */
Joe Perches63c3a662011-04-26 08:12:10 +00007932 if (!tg3_flag(tp, CPMU_PRESENT))
Matt Carlsone7126992009-08-25 10:08:16 +00007933 val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007934 pci_write_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +00007935 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007936 val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007937
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007938 /* Clear error status */
7939 pci_write_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +00007940 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVSTA,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007941 PCI_EXP_DEVSTA_CED |
7942 PCI_EXP_DEVSTA_NFED |
7943 PCI_EXP_DEVSTA_FED |
7944 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007945 }
7946
Michael Chanee6a99b2007-07-18 21:49:10 -07007947 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007948
Joe Perches63c3a662011-04-26 08:12:10 +00007949 tg3_flag_clear(tp, CHIP_RESETTING);
7950 tg3_flag_clear(tp, ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07007951
Michael Chanee6a99b2007-07-18 21:49:10 -07007952 val = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00007953 if (tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07007954 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07007955 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007956
7957 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
7958 tg3_stop_fw(tp);
7959 tw32(0x5000, 0x400);
7960 }
7961
7962 tw32(GRC_MODE, tp->grc_mode);
7963
7964 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007965 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007966
7967 tw32(0xc4, val | (1 << 15));
7968 }
7969
7970 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
7971 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
7972 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
7973 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
7974 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
7975 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7976 }
7977
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007978 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00007979 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007980 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007981 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00007982 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007983 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007984 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007985 val = 0;
7986
7987 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007988 udelay(40);
7989
Matt Carlson77b483f2008-08-15 14:07:24 -07007990 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
7991
Michael Chan7a6f4362006-09-27 16:03:31 -07007992 err = tg3_poll_fw(tp);
7993 if (err)
7994 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007995
Matt Carlson0a9140c2009-08-28 12:27:50 +00007996 tg3_mdio_start(tp);
7997
Joe Perches63c3a662011-04-26 08:12:10 +00007998 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007999 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
8000 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008001 !tg3_flag(tp, 57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01008002 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008003
8004 tw32(0x7c00, val | (1 << 25));
8005 }
8006
Matt Carlsond78b59f2011-04-05 14:22:46 +00008007 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
8008 val = tr32(TG3_CPMU_CLCK_ORIDE);
8009 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
8010 }
8011
Linus Torvalds1da177e2005-04-16 15:20:36 -07008012 /* Reprobe ASF enable state. */
Joe Perches63c3a662011-04-26 08:12:10 +00008013 tg3_flag_clear(tp, ENABLE_ASF);
8014 tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008015 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
8016 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
8017 u32 nic_cfg;
8018
8019 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
8020 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +00008021 tg3_flag_set(tp, ENABLE_ASF);
Matt Carlson4ba526c2008-08-15 14:10:04 -07008022 tp->last_event_jiffies = jiffies;
Joe Perches63c3a662011-04-26 08:12:10 +00008023 if (tg3_flag(tp, 5750_PLUS))
8024 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008025 }
8026 }
8027
8028 return 0;
8029}
8030
Matt Carlson65ec6982012-02-28 23:33:37 +00008031static void tg3_get_nstats(struct tg3 *, struct rtnl_link_stats64 *);
8032static void tg3_get_estats(struct tg3 *, struct tg3_ethtool_stats *);
Matt Carlson92feeab2011-12-08 14:40:14 +00008033
Linus Torvalds1da177e2005-04-16 15:20:36 -07008034/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07008035static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008036{
8037 int err;
8038
8039 tg3_stop_fw(tp);
8040
Michael Chan944d9802005-05-29 14:57:48 -07008041 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008042
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008043 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008044 err = tg3_chip_reset(tp);
8045
Matt Carlsondaba2a62009-04-20 06:58:52 +00008046 __tg3_set_mac_addr(tp, 0);
8047
Michael Chan944d9802005-05-29 14:57:48 -07008048 tg3_write_sig_legacy(tp, kind);
8049 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008050
Matt Carlson92feeab2011-12-08 14:40:14 +00008051 if (tp->hw_stats) {
8052 /* Save the stats across chip resets... */
David S. Millerb4017c52012-03-01 17:57:40 -05008053 tg3_get_nstats(tp, &tp->net_stats_prev);
Matt Carlson92feeab2011-12-08 14:40:14 +00008054 tg3_get_estats(tp, &tp->estats_prev);
8055
8056 /* And make sure the next sample is new data */
8057 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
8058 }
8059
Linus Torvalds1da177e2005-04-16 15:20:36 -07008060 if (err)
8061 return err;
8062
8063 return 0;
8064}
8065
Linus Torvalds1da177e2005-04-16 15:20:36 -07008066static int tg3_set_mac_addr(struct net_device *dev, void *p)
8067{
8068 struct tg3 *tp = netdev_priv(dev);
8069 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07008070 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008071
Michael Chanf9804dd2005-09-27 12:13:10 -07008072 if (!is_valid_ether_addr(addr->sa_data))
Danny Kukawka504f9b52012-02-21 02:07:49 +00008073 return -EADDRNOTAVAIL;
Michael Chanf9804dd2005-09-27 12:13:10 -07008074
Linus Torvalds1da177e2005-04-16 15:20:36 -07008075 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
8076
Michael Chane75f7c92006-03-20 21:33:26 -08008077 if (!netif_running(dev))
8078 return 0;
8079
Joe Perches63c3a662011-04-26 08:12:10 +00008080 if (tg3_flag(tp, ENABLE_ASF)) {
Michael Chan986e0ae2007-05-05 12:10:20 -07008081 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07008082
Michael Chan986e0ae2007-05-05 12:10:20 -07008083 addr0_high = tr32(MAC_ADDR_0_HIGH);
8084 addr0_low = tr32(MAC_ADDR_0_LOW);
8085 addr1_high = tr32(MAC_ADDR_1_HIGH);
8086 addr1_low = tr32(MAC_ADDR_1_LOW);
8087
8088 /* Skip MAC addr 1 if ASF is using it. */
8089 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
8090 !(addr1_high == 0 && addr1_low == 0))
8091 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07008092 }
Michael Chan986e0ae2007-05-05 12:10:20 -07008093 spin_lock_bh(&tp->lock);
8094 __tg3_set_mac_addr(tp, skip_mac_1);
8095 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008096
Michael Chanb9ec6c12006-07-25 16:37:27 -07008097 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008098}
8099
8100/* tp->lock is held. */
8101static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
8102 dma_addr_t mapping, u32 maxlen_flags,
8103 u32 nic_addr)
8104{
8105 tg3_write_mem(tp,
8106 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
8107 ((u64) mapping >> 32));
8108 tg3_write_mem(tp,
8109 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
8110 ((u64) mapping & 0xffffffff));
8111 tg3_write_mem(tp,
8112 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
8113 maxlen_flags);
8114
Joe Perches63c3a662011-04-26 08:12:10 +00008115 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008116 tg3_write_mem(tp,
8117 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
8118 nic_addr);
8119}
8120
Michael Chand244c892005-07-05 14:42:33 -07008121static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07008122{
Matt Carlsonb6080e12009-09-01 13:12:00 +00008123 int i;
8124
Joe Perches63c3a662011-04-26 08:12:10 +00008125 if (!tg3_flag(tp, ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00008126 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
8127 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
8128 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008129 } else {
8130 tw32(HOSTCC_TXCOL_TICKS, 0);
8131 tw32(HOSTCC_TXMAX_FRAMES, 0);
8132 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00008133 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008134
Joe Perches63c3a662011-04-26 08:12:10 +00008135 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008136 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
8137 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
8138 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
8139 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00008140 tw32(HOSTCC_RXCOL_TICKS, 0);
8141 tw32(HOSTCC_RXMAX_FRAMES, 0);
8142 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07008143 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008144
Joe Perches63c3a662011-04-26 08:12:10 +00008145 if (!tg3_flag(tp, 5705_PLUS)) {
David S. Miller15f98502005-05-18 22:49:26 -07008146 u32 val = ec->stats_block_coalesce_usecs;
8147
Matt Carlsonb6080e12009-09-01 13:12:00 +00008148 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
8149 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
8150
David S. Miller15f98502005-05-18 22:49:26 -07008151 if (!netif_carrier_ok(tp->dev))
8152 val = 0;
8153
8154 tw32(HOSTCC_STAT_COAL_TICKS, val);
8155 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008156
8157 for (i = 0; i < tp->irq_cnt - 1; i++) {
8158 u32 reg;
8159
8160 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
8161 tw32(reg, ec->rx_coalesce_usecs);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008162 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
8163 tw32(reg, ec->rx_max_coalesced_frames);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008164 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
8165 tw32(reg, ec->rx_max_coalesced_frames_irq);
Matt Carlson19cfaec2009-12-03 08:36:20 +00008166
Joe Perches63c3a662011-04-26 08:12:10 +00008167 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008168 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
8169 tw32(reg, ec->tx_coalesce_usecs);
8170 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
8171 tw32(reg, ec->tx_max_coalesced_frames);
8172 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
8173 tw32(reg, ec->tx_max_coalesced_frames_irq);
8174 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008175 }
8176
8177 for (; i < tp->irq_max - 1; i++) {
8178 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008179 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008180 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00008181
Joe Perches63c3a662011-04-26 08:12:10 +00008182 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008183 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
8184 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
8185 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
8186 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008187 }
David S. Miller15f98502005-05-18 22:49:26 -07008188}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008189
8190/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00008191static void tg3_rings_reset(struct tg3 *tp)
8192{
8193 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008194 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008195 struct tg3_napi *tnapi = &tp->napi[0];
8196
8197 /* Disable all transmit rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008198 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008199 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Joe Perches63c3a662011-04-26 08:12:10 +00008200 else if (tg3_flag(tp, 5717_PLUS))
Matt Carlson3d377282010-10-14 10:37:39 +00008201 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
Matt Carlson55086ad2011-12-14 11:09:59 +00008202 else if (tg3_flag(tp, 57765_CLASS))
Matt Carlsonb703df62009-12-03 08:36:21 +00008203 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008204 else
8205 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8206
8207 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8208 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
8209 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
8210 BDINFO_FLAGS_DISABLED);
8211
8212
8213 /* Disable all receive return rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008214 if (tg3_flag(tp, 5717_PLUS))
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008215 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
Joe Perches63c3a662011-04-26 08:12:10 +00008216 else if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008217 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00008218 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00008219 tg3_flag(tp, 57765_CLASS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008220 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
8221 else
8222 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8223
8224 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8225 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
8226 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
8227 BDINFO_FLAGS_DISABLED);
8228
8229 /* Disable interrupts */
8230 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008231 tp->napi[0].chk_msi_cnt = 0;
8232 tp->napi[0].last_rx_cons = 0;
8233 tp->napi[0].last_tx_cons = 0;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008234
8235 /* Zero mailbox registers. */
Joe Perches63c3a662011-04-26 08:12:10 +00008236 if (tg3_flag(tp, SUPPORT_MSIX)) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00008237 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008238 tp->napi[i].tx_prod = 0;
8239 tp->napi[i].tx_cons = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00008240 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008241 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008242 tw32_rx_mbox(tp->napi[i].consmbox, 0);
8243 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
Matt Carlson7f230732011-08-31 11:44:48 +00008244 tp->napi[i].chk_msi_cnt = 0;
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008245 tp->napi[i].last_rx_cons = 0;
8246 tp->napi[i].last_tx_cons = 0;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008247 }
Joe Perches63c3a662011-04-26 08:12:10 +00008248 if (!tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008249 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008250 } else {
8251 tp->napi[0].tx_prod = 0;
8252 tp->napi[0].tx_cons = 0;
8253 tw32_mailbox(tp->napi[0].prodmbox, 0);
8254 tw32_rx_mbox(tp->napi[0].consmbox, 0);
8255 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008256
8257 /* Make sure the NIC-based send BD rings are disabled. */
Joe Perches63c3a662011-04-26 08:12:10 +00008258 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson2d31eca2009-09-01 12:53:31 +00008259 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
8260 for (i = 0; i < 16; i++)
8261 tw32_tx_mbox(mbox + i * 8, 0);
8262 }
8263
8264 txrcb = NIC_SRAM_SEND_RCB;
8265 rxrcb = NIC_SRAM_RCV_RET_RCB;
8266
8267 /* Clear status block in ram. */
8268 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8269
8270 /* Set status block DMA address */
8271 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8272 ((u64) tnapi->status_mapping >> 32));
8273 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8274 ((u64) tnapi->status_mapping & 0xffffffff));
8275
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008276 if (tnapi->tx_ring) {
8277 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8278 (TG3_TX_RING_SIZE <<
8279 BDINFO_FLAGS_MAXLEN_SHIFT),
8280 NIC_SRAM_TX_BUFFER_DESC);
8281 txrcb += TG3_BDINFO_SIZE;
8282 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008283
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008284 if (tnapi->rx_rcb) {
8285 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008286 (tp->rx_ret_ring_mask + 1) <<
8287 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008288 rxrcb += TG3_BDINFO_SIZE;
8289 }
8290
8291 stblk = HOSTCC_STATBLCK_RING1;
8292
8293 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
8294 u64 mapping = (u64)tnapi->status_mapping;
8295 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
8296 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
8297
8298 /* Clear status block in ram. */
8299 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8300
Matt Carlson19cfaec2009-12-03 08:36:20 +00008301 if (tnapi->tx_ring) {
8302 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8303 (TG3_TX_RING_SIZE <<
8304 BDINFO_FLAGS_MAXLEN_SHIFT),
8305 NIC_SRAM_TX_BUFFER_DESC);
8306 txrcb += TG3_BDINFO_SIZE;
8307 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008308
8309 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008310 ((tp->rx_ret_ring_mask + 1) <<
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008311 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
8312
8313 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008314 rxrcb += TG3_BDINFO_SIZE;
8315 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008316}
8317
Matt Carlsoneb07a942011-04-20 07:57:36 +00008318static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
8319{
8320 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
8321
Joe Perches63c3a662011-04-26 08:12:10 +00008322 if (!tg3_flag(tp, 5750_PLUS) ||
8323 tg3_flag(tp, 5780_CLASS) ||
Matt Carlsoneb07a942011-04-20 07:57:36 +00008324 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
Matt Carlson513aa6e2011-11-21 15:01:18 +00008325 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
8326 tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008327 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
8328 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
8329 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787)
8330 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
8331 else
8332 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
8333
8334 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
8335 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
8336
8337 val = min(nic_rep_thresh, host_rep_thresh);
8338 tw32(RCVBDI_STD_THRESH, val);
8339
Joe Perches63c3a662011-04-26 08:12:10 +00008340 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008341 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
8342
Joe Perches63c3a662011-04-26 08:12:10 +00008343 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008344 return;
8345
Matt Carlson513aa6e2011-11-21 15:01:18 +00008346 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
Matt Carlsoneb07a942011-04-20 07:57:36 +00008347
8348 host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
8349
8350 val = min(bdcache_maxcnt / 2, host_rep_thresh);
8351 tw32(RCVBDI_JUMBO_THRESH, val);
8352
Joe Perches63c3a662011-04-26 08:12:10 +00008353 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008354 tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
8355}
8356
Matt Carlsonccd5ba92012-02-13 10:20:08 +00008357static inline u32 calc_crc(unsigned char *buf, int len)
8358{
8359 u32 reg;
8360 u32 tmp;
8361 int j, k;
8362
8363 reg = 0xffffffff;
8364
8365 for (j = 0; j < len; j++) {
8366 reg ^= buf[j];
8367
8368 for (k = 0; k < 8; k++) {
8369 tmp = reg & 0x01;
8370
8371 reg >>= 1;
8372
8373 if (tmp)
8374 reg ^= 0xedb88320;
8375 }
8376 }
8377
8378 return ~reg;
8379}
8380
8381static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
8382{
8383 /* accept or reject all multicast frames */
8384 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
8385 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
8386 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
8387 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
8388}
8389
8390static void __tg3_set_rx_mode(struct net_device *dev)
8391{
8392 struct tg3 *tp = netdev_priv(dev);
8393 u32 rx_mode;
8394
8395 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
8396 RX_MODE_KEEP_VLAN_TAG);
8397
8398#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
8399 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
8400 * flag clear.
8401 */
8402 if (!tg3_flag(tp, ENABLE_ASF))
8403 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
8404#endif
8405
8406 if (dev->flags & IFF_PROMISC) {
8407 /* Promiscuous mode. */
8408 rx_mode |= RX_MODE_PROMISC;
8409 } else if (dev->flags & IFF_ALLMULTI) {
8410 /* Accept all multicast. */
8411 tg3_set_multi(tp, 1);
8412 } else if (netdev_mc_empty(dev)) {
8413 /* Reject all multicast. */
8414 tg3_set_multi(tp, 0);
8415 } else {
8416 /* Accept one or more multicast(s). */
8417 struct netdev_hw_addr *ha;
8418 u32 mc_filter[4] = { 0, };
8419 u32 regidx;
8420 u32 bit;
8421 u32 crc;
8422
8423 netdev_for_each_mc_addr(ha, dev) {
8424 crc = calc_crc(ha->addr, ETH_ALEN);
8425 bit = ~crc & 0x7f;
8426 regidx = (bit & 0x60) >> 5;
8427 bit &= 0x1f;
8428 mc_filter[regidx] |= (1 << bit);
8429 }
8430
8431 tw32(MAC_HASH_REG_0, mc_filter[0]);
8432 tw32(MAC_HASH_REG_1, mc_filter[1]);
8433 tw32(MAC_HASH_REG_2, mc_filter[2]);
8434 tw32(MAC_HASH_REG_3, mc_filter[3]);
8435 }
8436
8437 if (rx_mode != tp->rx_mode) {
8438 tp->rx_mode = rx_mode;
8439 tw32_f(MAC_RX_MODE, rx_mode);
8440 udelay(10);
8441 }
8442}
8443
Matt Carlson90415472011-12-16 13:33:23 +00008444static void tg3_rss_init_dflt_indir_tbl(struct tg3 *tp)
8445{
8446 int i;
8447
8448 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
8449 tp->rss_ind_tbl[i] =
8450 ethtool_rxfh_indir_default(i, tp->irq_cnt - 1);
8451}
8452
8453static void tg3_rss_check_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008454{
8455 int i;
8456
8457 if (!tg3_flag(tp, SUPPORT_MSIX))
8458 return;
8459
Matt Carlson90415472011-12-16 13:33:23 +00008460 if (tp->irq_cnt <= 2) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008461 memset(&tp->rss_ind_tbl[0], 0, sizeof(tp->rss_ind_tbl));
Matt Carlson90415472011-12-16 13:33:23 +00008462 return;
8463 }
8464
8465 /* Validate table against current IRQ count */
8466 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
8467 if (tp->rss_ind_tbl[i] >= tp->irq_cnt - 1)
8468 break;
8469 }
8470
8471 if (i != TG3_RSS_INDIR_TBL_SIZE)
8472 tg3_rss_init_dflt_indir_tbl(tp);
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008473}
8474
Matt Carlson90415472011-12-16 13:33:23 +00008475static void tg3_rss_write_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008476{
8477 int i = 0;
8478 u32 reg = MAC_RSS_INDIR_TBL_0;
8479
8480 while (i < TG3_RSS_INDIR_TBL_SIZE) {
8481 u32 val = tp->rss_ind_tbl[i];
8482 i++;
8483 for (; i % 8; i++) {
8484 val <<= 4;
8485 val |= tp->rss_ind_tbl[i];
8486 }
8487 tw32(reg, val);
8488 reg += 4;
8489 }
8490}
8491
Matt Carlson2d31eca2009-09-01 12:53:31 +00008492/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008493static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008494{
8495 u32 val, rdmac_mode;
8496 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00008497 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008498
8499 tg3_disable_ints(tp);
8500
8501 tg3_stop_fw(tp);
8502
8503 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
8504
Joe Perches63c3a662011-04-26 08:12:10 +00008505 if (tg3_flag(tp, INIT_COMPLETE))
Michael Chane6de8ad2005-05-05 14:42:41 -07008506 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008507
Matt Carlson699c0192010-12-06 08:28:51 +00008508 /* Enable MAC control of LPI */
8509 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
8510 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL,
8511 TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
8512 TG3_CPMU_EEE_LNKIDL_UART_IDL);
8513
8514 tw32_f(TG3_CPMU_EEE_CTRL,
8515 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
8516
Matt Carlsona386b902010-12-06 08:28:53 +00008517 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
8518 TG3_CPMU_EEEMD_LPI_IN_TX |
8519 TG3_CPMU_EEEMD_LPI_IN_RX |
8520 TG3_CPMU_EEEMD_EEE_ENABLE;
8521
8522 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8523 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
8524
Joe Perches63c3a662011-04-26 08:12:10 +00008525 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsona386b902010-12-06 08:28:53 +00008526 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
8527
8528 tw32_f(TG3_CPMU_EEE_MODE, val);
8529
8530 tw32_f(TG3_CPMU_EEE_DBTMR1,
8531 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
8532 TG3_CPMU_DBTMR1_LNKIDLE_2047US);
8533
8534 tw32_f(TG3_CPMU_EEE_DBTMR2,
Matt Carlsond7f2ab22011-01-25 15:58:56 +00008535 TG3_CPMU_DBTMR2_APE_TX_2047US |
Matt Carlsona386b902010-12-06 08:28:53 +00008536 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
Matt Carlson699c0192010-12-06 08:28:51 +00008537 }
8538
Matt Carlson603f1172010-02-12 14:47:10 +00008539 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08008540 tg3_phy_reset(tp);
8541
Linus Torvalds1da177e2005-04-16 15:20:36 -07008542 err = tg3_chip_reset(tp);
8543 if (err)
8544 return err;
8545
8546 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
8547
Matt Carlsonbcb37f62008-11-03 16:52:09 -08008548 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008549 val = tr32(TG3_CPMU_CTRL);
8550 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
8551 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08008552
8553 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8554 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8555 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8556 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
8557
8558 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
8559 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
8560 val |= CPMU_LNK_AWARE_MACCLK_6_25;
8561 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
8562
8563 val = tr32(TG3_CPMU_HST_ACC);
8564 val &= ~CPMU_HST_ACC_MACCLK_MASK;
8565 val |= CPMU_HST_ACC_MACCLK_6_25;
8566 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07008567 }
8568
Matt Carlson33466d932009-04-20 06:57:41 +00008569 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
8570 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
8571 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
8572 PCIE_PWR_MGMT_L1_THRESH_4MS;
8573 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00008574
8575 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
8576 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
8577
8578 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d932009-04-20 06:57:41 +00008579
Matt Carlsonf40386c2009-11-02 14:24:02 +00008580 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
8581 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00008582 }
8583
Joe Perches63c3a662011-04-26 08:12:10 +00008584 if (tg3_flag(tp, L1PLLPD_EN)) {
Matt Carlson614b0592010-01-20 16:58:02 +00008585 u32 grc_mode = tr32(GRC_MODE);
8586
8587 /* Access the lower 1K of PL PCIE block registers. */
8588 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8589 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
8590
8591 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
8592 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
8593 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
8594
8595 tw32(GRC_MODE, grc_mode);
8596 }
8597
Matt Carlson55086ad2011-12-14 11:09:59 +00008598 if (tg3_flag(tp, 57765_CLASS)) {
Matt Carlson5093eed2010-11-24 08:31:45 +00008599 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
8600 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00008601
Matt Carlson5093eed2010-11-24 08:31:45 +00008602 /* Access the lower 1K of PL PCIE block registers. */
8603 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8604 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00008605
Matt Carlson5093eed2010-11-24 08:31:45 +00008606 val = tr32(TG3_PCIE_TLDLPL_PORT +
8607 TG3_PCIE_PL_LO_PHYCTL5);
8608 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
8609 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00008610
Matt Carlson5093eed2010-11-24 08:31:45 +00008611 tw32(GRC_MODE, grc_mode);
8612 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00008613
Matt Carlson1ff30a52011-05-19 12:12:46 +00008614 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_57765_AX) {
8615 u32 grc_mode = tr32(GRC_MODE);
8616
8617 /* Access the lower 1K of DL PCIE block registers. */
8618 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8619 tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
8620
8621 val = tr32(TG3_PCIE_TLDLPL_PORT +
8622 TG3_PCIE_DL_LO_FTSMAX);
8623 val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
8624 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
8625 val | TG3_PCIE_DL_LO_FTSMAX_VAL);
8626
8627 tw32(GRC_MODE, grc_mode);
8628 }
8629
Matt Carlsona977dbe2010-04-12 06:58:26 +00008630 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8631 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8632 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8633 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00008634 }
8635
Linus Torvalds1da177e2005-04-16 15:20:36 -07008636 /* This works around an issue with Athlon chipsets on
8637 * B3 tigon3 silicon. This bit has no effect on any
8638 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07008639 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008640 */
Joe Perches63c3a662011-04-26 08:12:10 +00008641 if (!tg3_flag(tp, CPMU_PRESENT)) {
8642 if (!tg3_flag(tp, PCI_EXPRESS))
Matt Carlson795d01c2007-10-07 23:28:17 -07008643 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
8644 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
8645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008646
8647 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008648 tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008649 val = tr32(TG3PCI_PCISTATE);
8650 val |= PCISTATE_RETRY_SAME_DMA;
8651 tw32(TG3PCI_PCISTATE, val);
8652 }
8653
Joe Perches63c3a662011-04-26 08:12:10 +00008654 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -07008655 /* Allow reads and writes to the
8656 * APE register and memory space.
8657 */
8658 val = tr32(TG3PCI_PCISTATE);
8659 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00008660 PCISTATE_ALLOW_APE_SHMEM_WR |
8661 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008662 tw32(TG3PCI_PCISTATE, val);
8663 }
8664
Linus Torvalds1da177e2005-04-16 15:20:36 -07008665 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
8666 /* Enable some hw fixes. */
8667 val = tr32(TG3PCI_MSI_DATA);
8668 val |= (1 << 26) | (1 << 28) | (1 << 29);
8669 tw32(TG3PCI_MSI_DATA, val);
8670 }
8671
8672 /* Descriptor ring init may make accesses to the
8673 * NIC SRAM area to setup the TX descriptors, so we
8674 * can only do this after the hardware has been
8675 * successfully reset.
8676 */
Michael Chan32d8c572006-07-25 16:38:29 -07008677 err = tg3_init_rings(tp);
8678 if (err)
8679 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008680
Joe Perches63c3a662011-04-26 08:12:10 +00008681 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008682 val = tr32(TG3PCI_DMA_RW_CTRL) &
8683 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Matt Carlson1a319022010-04-12 06:58:25 +00008684 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
8685 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlson55086ad2011-12-14 11:09:59 +00008686 if (!tg3_flag(tp, 57765_CLASS) &&
Matt Carlson0aebff42011-04-25 12:42:45 +00008687 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8688 val |= DMA_RWCTRL_TAGGED_STAT_WA;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008689 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
8690 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
8691 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008692 /* This value is determined during the probe time DMA
8693 * engine test, tg3_test_dma.
8694 */
8695 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
8696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008697
8698 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
8699 GRC_MODE_4X_NIC_SEND_RINGS |
8700 GRC_MODE_NO_TX_PHDR_CSUM |
8701 GRC_MODE_NO_RX_PHDR_CSUM);
8702 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07008703
8704 /* Pseudo-header checksum is done by hardware logic and not
8705 * the offload processers, so make the chip do the pseudo-
8706 * header checksums on receive. For transmit it is more
8707 * convenient to do the pseudo-header checksum in software
8708 * as Linux does that on transmit for us in all cases.
8709 */
8710 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008711
8712 tw32(GRC_MODE,
8713 tp->grc_mode |
8714 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
8715
8716 /* Setup the timer prescalar register. Clock is always 66Mhz. */
8717 val = tr32(GRC_MISC_CFG);
8718 val &= ~0xff;
8719 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
8720 tw32(GRC_MISC_CFG, val);
8721
8722 /* Initialize MBUF/DESC pool. */
Joe Perches63c3a662011-04-26 08:12:10 +00008723 if (tg3_flag(tp, 5750_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008724 /* Do nothing. */
8725 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
8726 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
8727 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
8728 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
8729 else
8730 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
8731 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
8732 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Joe Perches63c3a662011-04-26 08:12:10 +00008733 } else if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008734 int fw_len;
8735
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08008736 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008737 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
8738 tw32(BUFMGR_MB_POOL_ADDR,
8739 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
8740 tw32(BUFMGR_MB_POOL_SIZE,
8741 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
8742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008743
Michael Chan0f893dc2005-07-25 12:30:38 -07008744 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008745 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8746 tp->bufmgr_config.mbuf_read_dma_low_water);
8747 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8748 tp->bufmgr_config.mbuf_mac_rx_low_water);
8749 tw32(BUFMGR_MB_HIGH_WATER,
8750 tp->bufmgr_config.mbuf_high_water);
8751 } else {
8752 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8753 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
8754 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8755 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
8756 tw32(BUFMGR_MB_HIGH_WATER,
8757 tp->bufmgr_config.mbuf_high_water_jumbo);
8758 }
8759 tw32(BUFMGR_DMA_LOW_WATER,
8760 tp->bufmgr_config.dma_low_water);
8761 tw32(BUFMGR_DMA_HIGH_WATER,
8762 tp->bufmgr_config.dma_high_water);
8763
Matt Carlsond309a462010-09-30 10:34:31 +00008764 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
8765 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
8766 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
Matt Carlson4d958472011-04-20 07:57:35 +00008767 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
8768 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
8769 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0)
8770 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
Matt Carlsond309a462010-09-30 10:34:31 +00008771 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008772 for (i = 0; i < 2000; i++) {
8773 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
8774 break;
8775 udelay(10);
8776 }
8777 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00008778 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008779 return -ENODEV;
8780 }
8781
Matt Carlsoneb07a942011-04-20 07:57:36 +00008782 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
8783 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
Michael Chanb5d37722006-09-27 16:06:21 -07008784
Matt Carlsoneb07a942011-04-20 07:57:36 +00008785 tg3_setup_rxbd_thresholds(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008786
8787 /* Initialize TG3_BDINFO's at:
8788 * RCVDBDI_STD_BD: standard eth size rx ring
8789 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
8790 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
8791 *
8792 * like so:
8793 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
8794 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
8795 * ring attribute flags
8796 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
8797 *
8798 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
8799 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
8800 *
8801 * The size of each ring is fixed in the firmware, but the location is
8802 * configurable.
8803 */
8804 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008805 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008806 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008807 ((u64) tpr->rx_std_mapping & 0xffffffff));
Joe Perches63c3a662011-04-26 08:12:10 +00008808 if (!tg3_flag(tp, 5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +00008809 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
8810 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008811
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008812 /* Disable the mini ring */
Joe Perches63c3a662011-04-26 08:12:10 +00008813 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008814 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
8815 BDINFO_FLAGS_DISABLED);
8816
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008817 /* Program the jumbo buffer descriptor ring control
8818 * blocks on those devices that have them.
8819 */
Matt Carlsona0512942011-07-27 14:20:54 +00008820 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008821 (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008822
Joe Perches63c3a662011-04-26 08:12:10 +00008823 if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008824 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008825 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008826 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008827 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +00008828 val = TG3_RX_JMB_RING_SIZE(tp) <<
8829 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008830 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +00008831 val | BDINFO_FLAGS_USE_EXT_RECV);
Joe Perches63c3a662011-04-26 08:12:10 +00008832 if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
Matt Carlson55086ad2011-12-14 11:09:59 +00008833 tg3_flag(tp, 57765_CLASS))
Matt Carlson87668d32009-11-13 13:03:34 +00008834 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
8835 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008836 } else {
8837 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
8838 BDINFO_FLAGS_DISABLED);
8839 }
8840
Joe Perches63c3a662011-04-26 08:12:10 +00008841 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonfa6b2aa2011-11-21 15:01:19 +00008842 val = TG3_RX_STD_RING_SIZE(tp);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008843 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
8844 val |= (TG3_RX_STD_DMA_SZ << 2);
8845 } else
Matt Carlson04380d42010-04-12 06:58:29 +00008846 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008847 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008848 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008849
8850 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008851
Matt Carlson411da642009-11-13 13:03:46 +00008852 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e662009-11-13 13:03:49 +00008853 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008854
Joe Perches63c3a662011-04-26 08:12:10 +00008855 tpr->rx_jmb_prod_idx =
8856 tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
Matt Carlson66711e662009-11-13 13:03:49 +00008857 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008858
Matt Carlson2d31eca2009-09-01 12:53:31 +00008859 tg3_rings_reset(tp);
8860
Linus Torvalds1da177e2005-04-16 15:20:36 -07008861 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07008862 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008863
8864 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00008865 tw32(MAC_RX_MTU_SIZE,
8866 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008867
8868 /* The slot time is changed by tg3_setup_phy if we
8869 * run at gigabit with half duplex.
8870 */
Matt Carlsonf2096f92011-04-05 14:22:48 +00008871 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
8872 (6 << TX_LENGTHS_IPG_SHIFT) |
8873 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
8874
8875 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8876 val |= tr32(MAC_TX_LENGTHS) &
8877 (TX_LENGTHS_JMB_FRM_LEN_MSK |
8878 TX_LENGTHS_CNT_DWN_VAL_MSK);
8879
8880 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008881
8882 /* Receive rules. */
8883 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
8884 tw32(RCVLPC_CONFIG, 0x0181);
8885
8886 /* Calculate RDMAC_MODE setting early, we need it to determine
8887 * the RCVLPC_STATE_ENABLE mask.
8888 */
8889 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
8890 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
8891 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
8892 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
8893 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07008894
Matt Carlsondeabaac2010-11-24 08:31:50 +00008895 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +00008896 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
8897
Matt Carlson57e69832008-05-25 23:48:31 -07008898 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08008899 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8900 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07008901 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
8902 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
8903 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
8904
Matt Carlsonc5908932011-03-09 16:58:25 +00008905 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8906 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00008907 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07008908 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008909 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
8910 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008911 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008912 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8913 }
8914 }
8915
Joe Perches63c3a662011-04-26 08:12:10 +00008916 if (tg3_flag(tp, PCI_EXPRESS))
Michael Chan85e94ce2005-04-21 17:05:28 -07008917 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8918
Joe Perches63c3a662011-04-26 08:12:10 +00008919 if (tg3_flag(tp, HW_TSO_1) ||
8920 tg3_flag(tp, HW_TSO_2) ||
8921 tg3_flag(tp, HW_TSO_3))
Matt Carlson027455a2008-12-21 20:19:30 -08008922 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
8923
Matt Carlson108a6c12011-05-19 12:12:47 +00008924 if (tg3_flag(tp, 57765_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +00008925 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson027455a2008-12-21 20:19:30 -08008926 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
8927 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008928
Matt Carlsonf2096f92011-04-05 14:22:48 +00008929 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8930 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
8931
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008932 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
8933 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
8934 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8935 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008936 tg3_flag(tp, 57765_PLUS)) {
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008937 val = tr32(TG3_RDMA_RSRVCTRL_REG);
Matt Carlsond78b59f2011-04-05 14:22:46 +00008938 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8939 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +00008940 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
8941 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
8942 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
8943 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
8944 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
8945 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +00008946 }
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008947 tw32(TG3_RDMA_RSRVCTRL_REG,
8948 val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
8949 }
8950
Matt Carlsond78b59f2011-04-05 14:22:46 +00008951 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8952 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsond309a462010-09-30 10:34:31 +00008953 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
8954 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val |
8955 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
8956 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
8957 }
8958
Linus Torvalds1da177e2005-04-16 15:20:36 -07008959 /* Receive/send statistics. */
Joe Perches63c3a662011-04-26 08:12:10 +00008960 if (tg3_flag(tp, 5750_PLUS)) {
Michael Chan16613942006-06-29 20:15:13 -07008961 val = tr32(RCVLPC_STATS_ENABLE);
8962 val &= ~RCVLPC_STATSENAB_DACK_FIX;
8963 tw32(RCVLPC_STATS_ENABLE, val);
8964 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008965 tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008966 val = tr32(RCVLPC_STATS_ENABLE);
8967 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
8968 tw32(RCVLPC_STATS_ENABLE, val);
8969 } else {
8970 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
8971 }
8972 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
8973 tw32(SNDDATAI_STATSENAB, 0xffffff);
8974 tw32(SNDDATAI_STATSCTRL,
8975 (SNDDATAI_SCTRL_ENABLE |
8976 SNDDATAI_SCTRL_FASTUPD));
8977
8978 /* Setup host coalescing engine. */
8979 tw32(HOSTCC_MODE, 0);
8980 for (i = 0; i < 2000; i++) {
8981 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
8982 break;
8983 udelay(10);
8984 }
8985
Michael Chand244c892005-07-05 14:42:33 -07008986 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008987
Joe Perches63c3a662011-04-26 08:12:10 +00008988 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008989 /* Status/statistics block address. See tg3_timer,
8990 * the tg3_periodic_fetch_stats call there, and
8991 * tg3_get_stats to see how this works for 5705/5750 chips.
8992 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008993 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8994 ((u64) tp->stats_mapping >> 32));
8995 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8996 ((u64) tp->stats_mapping & 0xffffffff));
8997 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008998
Linus Torvalds1da177e2005-04-16 15:20:36 -07008999 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009000
9001 /* Clear statistics and status block memory areas */
9002 for (i = NIC_SRAM_STATS_BLK;
9003 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
9004 i += sizeof(u32)) {
9005 tg3_write_mem(tp, i, 0);
9006 udelay(40);
9007 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009008 }
9009
9010 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
9011
9012 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
9013 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00009014 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009015 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
9016
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009017 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
9018 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -07009019 /* reset to prevent losing 1st rx packet intermittently */
9020 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
9021 udelay(10);
9022 }
9023
Matt Carlson3bda1252008-08-15 14:08:22 -07009024 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Matt Carlson9e975cc2011-07-20 10:20:50 +00009025 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE |
9026 MAC_MODE_FHDE_ENABLE;
9027 if (tg3_flag(tp, ENABLE_APE))
9028 tp->mac_mode |= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Joe Perches63c3a662011-04-26 08:12:10 +00009029 if (!tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009030 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07009031 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
9032 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009033 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
9034 udelay(40);
9035
Michael Chan314fba32005-04-21 17:07:04 -07009036 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Joe Perches63c3a662011-04-26 08:12:10 +00009037 * If TG3_FLAG_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07009038 * register to preserve the GPIO settings for LOMs. The GPIOs,
9039 * whether used as inputs or outputs, are set by boot code after
9040 * reset.
9041 */
Joe Perches63c3a662011-04-26 08:12:10 +00009042 if (!tg3_flag(tp, IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07009043 u32 gpio_mask;
9044
Michael Chan9d26e212006-12-07 00:21:14 -08009045 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
9046 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
9047 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07009048
9049 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
9050 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
9051 GRC_LCLCTRL_GPIO_OUTPUT3;
9052
Michael Chanaf36e6b2006-03-23 01:28:06 -08009053 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
9054 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
9055
Gary Zambranoaaf84462007-05-05 11:51:45 -07009056 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07009057 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
9058
9059 /* GPIO1 must be driven high for eeprom write protect */
Joe Perches63c3a662011-04-26 08:12:10 +00009060 if (tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan9d26e212006-12-07 00:21:14 -08009061 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
9062 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07009063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009064 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
9065 udelay(100);
9066
Matt Carlsonc3b50032012-01-17 15:27:23 +00009067 if (tg3_flag(tp, USING_MSIX)) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009068 val = tr32(MSGINT_MODE);
Matt Carlsonc3b50032012-01-17 15:27:23 +00009069 val |= MSGINT_MODE_ENABLE;
9070 if (tp->irq_cnt > 1)
9071 val |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +00009072 if (!tg3_flag(tp, 1SHOT_MSI))
9073 val |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009074 tw32(MSGINT_MODE, val);
9075 }
9076
Joe Perches63c3a662011-04-26 08:12:10 +00009077 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009078 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
9079 udelay(40);
9080 }
9081
9082 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
9083 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
9084 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
9085 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
9086 WDMAC_MODE_LNGREAD_ENAB);
9087
Matt Carlsonc5908932011-03-09 16:58:25 +00009088 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
9089 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00009090 if (tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009091 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
9092 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
9093 /* nothing */
9094 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009095 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009096 val |= WDMAC_MODE_RX_ACCEL;
9097 }
9098 }
9099
Michael Chand9ab5ad12006-03-20 22:27:35 -08009100 /* Enable host coalescing bug fix */
Joe Perches63c3a662011-04-26 08:12:10 +00009101 if (tg3_flag(tp, 5755_PLUS))
Matt Carlsonf51f3562008-05-25 23:45:08 -07009102 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad12006-03-20 22:27:35 -08009103
Matt Carlson788a0352009-11-02 14:26:03 +00009104 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
9105 val |= WDMAC_MODE_BURST_ALL_DATA;
9106
Linus Torvalds1da177e2005-04-16 15:20:36 -07009107 tw32_f(WDMAC_MODE, val);
9108 udelay(40);
9109
Joe Perches63c3a662011-04-26 08:12:10 +00009110 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07009111 u16 pcix_cmd;
9112
9113 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
9114 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009115 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07009116 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
9117 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009118 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07009119 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
9120 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009121 }
Matt Carlson9974a352007-10-07 23:27:28 -07009122 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
9123 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009124 }
9125
9126 tw32_f(RDMAC_MODE, rdmac_mode);
9127 udelay(40);
9128
9129 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00009130 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009131 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07009132
9133 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
9134 tw32(SNDDATAC_MODE,
9135 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
9136 else
9137 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
9138
Linus Torvalds1da177e2005-04-16 15:20:36 -07009139 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
9140 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00009141 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00009142 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlson7cb32cf2010-09-30 10:34:36 +00009143 val |= RCVDBDI_MODE_LRG_RING_SZ;
9144 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009145 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00009146 if (tg3_flag(tp, HW_TSO_1) ||
9147 tg3_flag(tp, HW_TSO_2) ||
9148 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009149 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009150 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00009151 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009152 val |= SNDBDI_MODE_MULTI_TXQ_EN;
9153 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009154 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
9155
9156 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
9157 err = tg3_load_5701_a0_firmware_fix(tp);
9158 if (err)
9159 return err;
9160 }
9161
Joe Perches63c3a662011-04-26 08:12:10 +00009162 if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009163 err = tg3_load_tso_firmware(tp);
9164 if (err)
9165 return err;
9166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009167
9168 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +00009169
Joe Perches63c3a662011-04-26 08:12:10 +00009170 if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsonb1d05212010-06-05 17:24:31 +00009171 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
9172 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +00009173
9174 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
9175 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
9176 tp->tx_mode &= ~val;
9177 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
9178 }
9179
Linus Torvalds1da177e2005-04-16 15:20:36 -07009180 tw32_f(MAC_TX_MODE, tp->tx_mode);
9181 udelay(100);
9182
Joe Perches63c3a662011-04-26 08:12:10 +00009183 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009184 tg3_rss_write_indir_tbl(tp);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009185
9186 /* Setup the "secret" hash key. */
9187 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
9188 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
9189 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
9190 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
9191 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
9192 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
9193 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
9194 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
9195 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
9196 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
9197 }
9198
Linus Torvalds1da177e2005-04-16 15:20:36 -07009199 tp->rx_mode = RX_MODE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00009200 if (tg3_flag(tp, 5755_PLUS))
Michael Chanaf36e6b2006-03-23 01:28:06 -08009201 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
9202
Joe Perches63c3a662011-04-26 08:12:10 +00009203 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009204 tp->rx_mode |= RX_MODE_RSS_ENABLE |
9205 RX_MODE_RSS_ITBL_HASH_BITS_7 |
9206 RX_MODE_RSS_IPV6_HASH_EN |
9207 RX_MODE_RSS_TCP_IPV6_HASH_EN |
9208 RX_MODE_RSS_IPV4_HASH_EN |
9209 RX_MODE_RSS_TCP_IPV4_HASH_EN;
9210
Linus Torvalds1da177e2005-04-16 15:20:36 -07009211 tw32_f(MAC_RX_MODE, tp->rx_mode);
9212 udelay(10);
9213
Linus Torvalds1da177e2005-04-16 15:20:36 -07009214 tw32(MAC_LED_CTRL, tp->led_ctrl);
9215
9216 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009217 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009218 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
9219 udelay(10);
9220 }
9221 tw32_f(MAC_RX_MODE, tp->rx_mode);
9222 udelay(10);
9223
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009224 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009225 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009226 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009227 /* Set drive transmission level to 1.2V */
9228 /* only if the signal pre-emphasis bit is not set */
9229 val = tr32(MAC_SERDES_CFG);
9230 val &= 0xfffff000;
9231 val |= 0x880;
9232 tw32(MAC_SERDES_CFG, val);
9233 }
9234 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
9235 tw32(MAC_SERDES_CFG, 0x616000);
9236 }
9237
9238 /* Prevent chip from dropping frames when flow control
9239 * is enabled.
9240 */
Matt Carlson55086ad2011-12-14 11:09:59 +00009241 if (tg3_flag(tp, 57765_CLASS))
Matt Carlson666bc832010-01-20 16:58:03 +00009242 val = 1;
9243 else
9244 val = 2;
9245 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009246
9247 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009248 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009249 /* Use hardware link auto-negotiation */
Joe Perches63c3a662011-04-26 08:12:10 +00009250 tg3_flag_set(tp, HW_AUTONEG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009251 }
9252
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009253 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +00009254 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08009255 u32 tmp;
9256
9257 tmp = tr32(SERDES_RX_CTRL);
9258 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
9259 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
9260 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
9261 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
9262 }
9263
Joe Perches63c3a662011-04-26 08:12:10 +00009264 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonc6700ce2012-02-13 15:20:15 +00009265 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Matt Carlson80096062010-08-02 11:26:06 +00009266 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009267
Matt Carlsondd477002008-05-25 23:45:58 -07009268 err = tg3_setup_phy(tp, 0);
9269 if (err)
9270 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009271
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009272 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
9273 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07009274 u32 tmp;
9275
9276 /* Clear CRC stats. */
9277 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
9278 tg3_writephy(tp, MII_TG3_TEST1,
9279 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009280 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -07009281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009282 }
9283 }
9284
9285 __tg3_set_rx_mode(tp->dev);
9286
9287 /* Initialize receive rules. */
9288 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
9289 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
9290 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
9291 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
9292
Joe Perches63c3a662011-04-26 08:12:10 +00009293 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009294 limit = 8;
9295 else
9296 limit = 16;
Joe Perches63c3a662011-04-26 08:12:10 +00009297 if (tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009298 limit -= 4;
9299 switch (limit) {
9300 case 16:
9301 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
9302 case 15:
9303 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
9304 case 14:
9305 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
9306 case 13:
9307 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
9308 case 12:
9309 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
9310 case 11:
9311 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
9312 case 10:
9313 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
9314 case 9:
9315 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
9316 case 8:
9317 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
9318 case 7:
9319 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
9320 case 6:
9321 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
9322 case 5:
9323 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
9324 case 4:
9325 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
9326 case 3:
9327 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
9328 case 2:
9329 case 1:
9330
9331 default:
9332 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07009333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009334
Joe Perches63c3a662011-04-26 08:12:10 +00009335 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson9ce768e2007-10-11 19:49:11 -07009336 /* Write our heartbeat update interval to APE. */
9337 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
9338 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07009339
Linus Torvalds1da177e2005-04-16 15:20:36 -07009340 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
9341
Linus Torvalds1da177e2005-04-16 15:20:36 -07009342 return 0;
9343}
9344
9345/* Called at device open time to get the chip ready for
9346 * packet processing. Invoked with tp->lock held.
9347 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009348static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009349{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009350 tg3_switch_clocks(tp);
9351
9352 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
9353
Matt Carlson2f751b62008-08-04 23:17:34 -07009354 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009355}
9356
9357#define TG3_STAT_ADD32(PSTAT, REG) \
9358do { u32 __val = tr32(REG); \
9359 (PSTAT)->low += __val; \
9360 if ((PSTAT)->low < __val) \
9361 (PSTAT)->high += 1; \
9362} while (0)
9363
9364static void tg3_periodic_fetch_stats(struct tg3 *tp)
9365{
9366 struct tg3_hw_stats *sp = tp->hw_stats;
9367
9368 if (!netif_carrier_ok(tp->dev))
9369 return;
9370
9371 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
9372 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
9373 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
9374 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
9375 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
9376 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
9377 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
9378 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
9379 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
9380 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
9381 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
9382 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
9383 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
9384
9385 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
9386 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
9387 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
9388 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
9389 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
9390 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
9391 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
9392 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
9393 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
9394 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
9395 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
9396 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
9397 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
9398 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07009399
9400 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
Matt Carlson310050f2011-05-19 12:12:55 +00009401 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
9402 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0 &&
9403 tp->pci_chip_rev_id != CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +00009404 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
9405 } else {
9406 u32 val = tr32(HOSTCC_FLOW_ATTN);
9407 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
9408 if (val) {
9409 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
9410 sp->rx_discards.low += val;
9411 if (sp->rx_discards.low < val)
9412 sp->rx_discards.high += 1;
9413 }
9414 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
9415 }
Michael Chan463d3052006-05-22 16:36:27 -07009416 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009417}
9418
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009419static void tg3_chk_missed_msi(struct tg3 *tp)
9420{
9421 u32 i;
9422
9423 for (i = 0; i < tp->irq_cnt; i++) {
9424 struct tg3_napi *tnapi = &tp->napi[i];
9425
9426 if (tg3_has_work(tnapi)) {
9427 if (tnapi->last_rx_cons == tnapi->rx_rcb_ptr &&
9428 tnapi->last_tx_cons == tnapi->tx_cons) {
9429 if (tnapi->chk_msi_cnt < 1) {
9430 tnapi->chk_msi_cnt++;
9431 return;
9432 }
Matt Carlson7f230732011-08-31 11:44:48 +00009433 tg3_msi(0, tnapi);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009434 }
9435 }
9436 tnapi->chk_msi_cnt = 0;
9437 tnapi->last_rx_cons = tnapi->rx_rcb_ptr;
9438 tnapi->last_tx_cons = tnapi->tx_cons;
9439 }
9440}
9441
Linus Torvalds1da177e2005-04-16 15:20:36 -07009442static void tg3_timer(unsigned long __opaque)
9443{
9444 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009445
Matt Carlson5b190622011-11-04 09:15:04 +00009446 if (tp->irq_sync || tg3_flag(tp, RESET_TASK_PENDING))
Michael Chanf475f162006-03-27 23:20:14 -08009447 goto restart_timer;
9448
David S. Millerf47c11e2005-06-24 20:18:35 -07009449 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009450
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009451 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00009452 tg3_flag(tp, 57765_CLASS))
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009453 tg3_chk_missed_msi(tp);
9454
Joe Perches63c3a662011-04-26 08:12:10 +00009455 if (!tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -07009456 /* All of this garbage is because when using non-tagged
9457 * IRQ status the mailbox/status_block protocol the chip
9458 * uses with the cpu is race prone.
9459 */
Matt Carlson898a56f2009-08-28 14:02:40 +00009460 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -07009461 tw32(GRC_LOCAL_CTRL,
9462 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
9463 } else {
9464 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00009465 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -07009466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009467
David S. Millerfac9b832005-05-18 22:46:34 -07009468 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
David S. Millerf47c11e2005-06-24 20:18:35 -07009469 spin_unlock(&tp->lock);
Matt Carlsondb219972011-11-04 09:15:03 +00009470 tg3_reset_task_schedule(tp);
Matt Carlson5b190622011-11-04 09:15:04 +00009471 goto restart_timer;
David S. Millerfac9b832005-05-18 22:46:34 -07009472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009473 }
9474
Linus Torvalds1da177e2005-04-16 15:20:36 -07009475 /* This part only runs once per second. */
9476 if (!--tp->timer_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00009477 if (tg3_flag(tp, 5705_PLUS))
David S. Millerfac9b832005-05-18 22:46:34 -07009478 tg3_periodic_fetch_stats(tp);
9479
Matt Carlsonb0c59432011-05-19 12:12:48 +00009480 if (tp->setlpicnt && !--tp->setlpicnt)
9481 tg3_phy_eee_enable(tp);
Matt Carlson52b02d02010-10-14 10:37:41 +00009482
Joe Perches63c3a662011-04-26 08:12:10 +00009483 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009484 u32 mac_stat;
9485 int phy_event;
9486
9487 mac_stat = tr32(MAC_STATUS);
9488
9489 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009490 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009491 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
9492 phy_event = 1;
9493 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
9494 phy_event = 1;
9495
9496 if (phy_event)
9497 tg3_setup_phy(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +00009498 } else if (tg3_flag(tp, POLL_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009499 u32 mac_stat = tr32(MAC_STATUS);
9500 int need_setup = 0;
9501
9502 if (netif_carrier_ok(tp->dev) &&
9503 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
9504 need_setup = 1;
9505 }
Matt Carlsonbe98da62010-07-11 09:31:46 +00009506 if (!netif_carrier_ok(tp->dev) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009507 (mac_stat & (MAC_STATUS_PCS_SYNCED |
9508 MAC_STATUS_SIGNAL_DET))) {
9509 need_setup = 1;
9510 }
9511 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07009512 if (!tp->serdes_counter) {
9513 tw32_f(MAC_MODE,
9514 (tp->mac_mode &
9515 ~MAC_MODE_PORT_MODE_MASK));
9516 udelay(40);
9517 tw32_f(MAC_MODE, tp->mac_mode);
9518 udelay(40);
9519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009520 tg3_setup_phy(tp, 0);
9521 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009522 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009523 tg3_flag(tp, 5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -07009524 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +00009525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009526
9527 tp->timer_counter = tp->timer_multiplier;
9528 }
9529
Michael Chan130b8e42006-09-27 16:00:40 -07009530 /* Heartbeat is only sent once every 2 seconds.
9531 *
9532 * The heartbeat is to tell the ASF firmware that the host
9533 * driver is still alive. In the event that the OS crashes,
9534 * ASF needs to reset the hardware to free up the FIFO space
9535 * that may be filled with rx packets destined for the host.
9536 * If the FIFO is full, ASF will no longer function properly.
9537 *
9538 * Unintended resets have been reported on real time kernels
9539 * where the timer doesn't run on time. Netpoll will also have
9540 * same problem.
9541 *
9542 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
9543 * to check the ring condition when the heartbeat is expiring
9544 * before doing the reset. This will prevent most unintended
9545 * resets.
9546 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009547 if (!--tp->asf_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00009548 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07009549 tg3_wait_for_event_ack(tp);
9550
Michael Chanbbadf502006-04-06 21:46:34 -07009551 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07009552 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07009553 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +00009554 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
9555 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -07009556
9557 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009558 }
9559 tp->asf_counter = tp->asf_multiplier;
9560 }
9561
David S. Millerf47c11e2005-06-24 20:18:35 -07009562 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009563
Michael Chanf475f162006-03-27 23:20:14 -08009564restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07009565 tp->timer.expires = jiffies + tp->timer_offset;
9566 add_timer(&tp->timer);
9567}
9568
Matt Carlson21f76382012-02-22 12:35:21 +00009569static void __devinit tg3_timer_init(struct tg3 *tp)
9570{
9571 if (tg3_flag(tp, TAGGED_STATUS) &&
9572 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
9573 !tg3_flag(tp, 57765_CLASS))
9574 tp->timer_offset = HZ;
9575 else
9576 tp->timer_offset = HZ / 10;
9577
9578 BUG_ON(tp->timer_offset > HZ);
9579
9580 tp->timer_multiplier = (HZ / tp->timer_offset);
9581 tp->asf_multiplier = (HZ / tp->timer_offset) *
9582 TG3_FW_UPDATE_FREQ_SEC;
9583
9584 init_timer(&tp->timer);
9585 tp->timer.data = (unsigned long) tp;
9586 tp->timer.function = tg3_timer;
9587}
9588
9589static void tg3_timer_start(struct tg3 *tp)
9590{
9591 tp->asf_counter = tp->asf_multiplier;
9592 tp->timer_counter = tp->timer_multiplier;
9593
9594 tp->timer.expires = jiffies + tp->timer_offset;
9595 add_timer(&tp->timer);
9596}
9597
9598static void tg3_timer_stop(struct tg3 *tp)
9599{
9600 del_timer_sync(&tp->timer);
9601}
9602
9603/* Restart hardware after configuration changes, self-test, etc.
9604 * Invoked with tp->lock held.
9605 */
9606static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
9607 __releases(tp->lock)
9608 __acquires(tp->lock)
9609{
9610 int err;
9611
9612 err = tg3_init_hw(tp, reset_phy);
9613 if (err) {
9614 netdev_err(tp->dev,
9615 "Failed to re-initialize device, aborting\n");
9616 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
9617 tg3_full_unlock(tp);
9618 tg3_timer_stop(tp);
9619 tp->irq_sync = 0;
9620 tg3_napi_enable(tp);
9621 dev_close(tp->dev);
9622 tg3_full_lock(tp, 0);
9623 }
9624 return err;
9625}
9626
9627static void tg3_reset_task(struct work_struct *work)
9628{
9629 struct tg3 *tp = container_of(work, struct tg3, reset_task);
9630 int err;
9631
9632 tg3_full_lock(tp, 0);
9633
9634 if (!netif_running(tp->dev)) {
9635 tg3_flag_clear(tp, RESET_TASK_PENDING);
9636 tg3_full_unlock(tp);
9637 return;
9638 }
9639
9640 tg3_full_unlock(tp);
9641
9642 tg3_phy_stop(tp);
9643
9644 tg3_netif_stop(tp);
9645
9646 tg3_full_lock(tp, 1);
9647
9648 if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
9649 tp->write32_tx_mbox = tg3_write32_tx_mbox;
9650 tp->write32_rx_mbox = tg3_write_flush_reg32;
9651 tg3_flag_set(tp, MBOX_WRITE_REORDER);
9652 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
9653 }
9654
9655 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
9656 err = tg3_init_hw(tp, 1);
9657 if (err)
9658 goto out;
9659
9660 tg3_netif_start(tp);
9661
9662out:
9663 tg3_full_unlock(tp);
9664
9665 if (!err)
9666 tg3_phy_start(tp);
9667
9668 tg3_flag_clear(tp, RESET_TASK_PENDING);
9669}
9670
Matt Carlson4f125f42009-09-01 12:55:02 +00009671static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -08009672{
David Howells7d12e782006-10-05 14:55:46 +01009673 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009674 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +00009675 char *name;
9676 struct tg3_napi *tnapi = &tp->napi[irq_num];
9677
9678 if (tp->irq_cnt == 1)
9679 name = tp->dev->name;
9680 else {
9681 name = &tnapi->irq_lbl[0];
9682 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
9683 name[IFNAMSIZ-1] = 0;
9684 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009685
Joe Perches63c3a662011-04-26 08:12:10 +00009686 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Michael Chanfcfa0a32006-03-20 22:28:41 -08009687 fn = tg3_msi;
Joe Perches63c3a662011-04-26 08:12:10 +00009688 if (tg3_flag(tp, 1SHOT_MSI))
Michael Chanfcfa0a32006-03-20 22:28:41 -08009689 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009690 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009691 } else {
9692 fn = tg3_interrupt;
Joe Perches63c3a662011-04-26 08:12:10 +00009693 if (tg3_flag(tp, TAGGED_STATUS))
Michael Chanfcfa0a32006-03-20 22:28:41 -08009694 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009695 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009696 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009697
9698 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009699}
9700
Michael Chan79381092005-04-21 17:13:59 -07009701static int tg3_test_interrupt(struct tg3 *tp)
9702{
Matt Carlson09943a12009-08-28 14:01:57 +00009703 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -07009704 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -07009705 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009706 u32 val;
Michael Chan79381092005-04-21 17:13:59 -07009707
Michael Chand4bc3922005-05-29 14:59:20 -07009708 if (!netif_running(dev))
9709 return -ENODEV;
9710
Michael Chan79381092005-04-21 17:13:59 -07009711 tg3_disable_ints(tp);
9712
Matt Carlson4f125f42009-09-01 12:55:02 +00009713 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009714
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009715 /*
9716 * Turn off MSI one shot mode. Otherwise this test has no
9717 * observable way to know whether the interrupt was delivered.
9718 */
Matt Carlson3aa1cdf2011-07-20 10:20:55 +00009719 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009720 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
9721 tw32(MSGINT_MODE, val);
9722 }
9723
Matt Carlson4f125f42009-09-01 12:55:02 +00009724 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Davidlohr Buesof274fd92012-02-22 03:06:54 +00009725 IRQF_SHARED, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009726 if (err)
9727 return err;
9728
Matt Carlson898a56f2009-08-28 14:02:40 +00009729 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -07009730 tg3_enable_ints(tp);
9731
9732 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00009733 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -07009734
9735 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -07009736 u32 int_mbox, misc_host_ctrl;
9737
Matt Carlson898a56f2009-08-28 14:02:40 +00009738 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -07009739 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
9740
9741 if ((int_mbox != 0) ||
9742 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
9743 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -07009744 break;
Michael Chanb16250e2006-09-27 16:10:14 -07009745 }
9746
Matt Carlson3aa1cdf2011-07-20 10:20:55 +00009747 if (tg3_flag(tp, 57765_PLUS) &&
9748 tnapi->hw_status->status_tag != tnapi->last_tag)
9749 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
9750
Michael Chan79381092005-04-21 17:13:59 -07009751 msleep(10);
9752 }
9753
9754 tg3_disable_ints(tp);
9755
Matt Carlson4f125f42009-09-01 12:55:02 +00009756 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009757
Matt Carlson4f125f42009-09-01 12:55:02 +00009758 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009759
9760 if (err)
9761 return err;
9762
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009763 if (intr_ok) {
9764 /* Reenable MSI one shot mode. */
Matt Carlson5b39de92011-08-31 11:44:50 +00009765 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, 1SHOT_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009766 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
9767 tw32(MSGINT_MODE, val);
9768 }
Michael Chan79381092005-04-21 17:13:59 -07009769 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009770 }
Michael Chan79381092005-04-21 17:13:59 -07009771
9772 return -EIO;
9773}
9774
9775/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
9776 * successfully restored
9777 */
9778static int tg3_test_msi(struct tg3 *tp)
9779{
Michael Chan79381092005-04-21 17:13:59 -07009780 int err;
9781 u16 pci_cmd;
9782
Joe Perches63c3a662011-04-26 08:12:10 +00009783 if (!tg3_flag(tp, USING_MSI))
Michael Chan79381092005-04-21 17:13:59 -07009784 return 0;
9785
9786 /* Turn off SERR reporting in case MSI terminates with Master
9787 * Abort.
9788 */
9789 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
9790 pci_write_config_word(tp->pdev, PCI_COMMAND,
9791 pci_cmd & ~PCI_COMMAND_SERR);
9792
9793 err = tg3_test_interrupt(tp);
9794
9795 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
9796
9797 if (!err)
9798 return 0;
9799
9800 /* other failures */
9801 if (err != -EIO)
9802 return err;
9803
9804 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009805 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
9806 "to INTx mode. Please report this failure to the PCI "
9807 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -07009808
Matt Carlson4f125f42009-09-01 12:55:02 +00009809 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +00009810
Michael Chan79381092005-04-21 17:13:59 -07009811 pci_disable_msi(tp->pdev);
9812
Joe Perches63c3a662011-04-26 08:12:10 +00009813 tg3_flag_clear(tp, USING_MSI);
Andre Detschdc8bf1b2010-04-26 07:27:07 +00009814 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -07009815
Matt Carlson4f125f42009-09-01 12:55:02 +00009816 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009817 if (err)
9818 return err;
9819
9820 /* Need to reset the chip because the MSI cycle may have terminated
9821 * with Master Abort.
9822 */
David S. Millerf47c11e2005-06-24 20:18:35 -07009823 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009824
Michael Chan944d9802005-05-29 14:57:48 -07009825 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009826 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009827
David S. Millerf47c11e2005-06-24 20:18:35 -07009828 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009829
9830 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +00009831 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -07009832
9833 return err;
9834}
9835
Matt Carlson9e9fd122009-01-19 16:57:45 -08009836static int tg3_request_firmware(struct tg3 *tp)
9837{
9838 const __be32 *fw_data;
9839
9840 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009841 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
9842 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009843 return -ENOENT;
9844 }
9845
9846 fw_data = (void *)tp->fw->data;
9847
9848 /* Firmware blob starts with version numbers, followed by
9849 * start address and _full_ length including BSS sections
9850 * (which must be longer than the actual data, of course
9851 */
9852
9853 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
9854 if (tp->fw_len < (tp->fw->size - 12)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009855 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
9856 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009857 release_firmware(tp->fw);
9858 tp->fw = NULL;
9859 return -EINVAL;
9860 }
9861
9862 /* We no longer need firmware; we have it. */
9863 tp->fw_needed = NULL;
9864 return 0;
9865}
9866
Matt Carlson679563f2009-09-01 12:55:46 +00009867static bool tg3_enable_msix(struct tg3 *tp)
9868{
Matt Carlsonc3b50032012-01-17 15:27:23 +00009869 int i, rc;
Matt Carlson679563f2009-09-01 12:55:46 +00009870 struct msix_entry msix_ent[tp->irq_max];
9871
Matt Carlsonc3b50032012-01-17 15:27:23 +00009872 tp->irq_cnt = num_online_cpus();
9873 if (tp->irq_cnt > 1) {
9874 /* We want as many rx rings enabled as there are cpus.
9875 * In multiqueue MSI-X mode, the first MSI-X vector
9876 * only deals with link interrupts, etc, so we add
9877 * one to the number of vectors we are requesting.
9878 */
9879 tp->irq_cnt = min_t(unsigned, tp->irq_cnt + 1, tp->irq_max);
9880 }
Matt Carlson679563f2009-09-01 12:55:46 +00009881
9882 for (i = 0; i < tp->irq_max; i++) {
9883 msix_ent[i].entry = i;
9884 msix_ent[i].vector = 0;
9885 }
9886
9887 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +00009888 if (rc < 0) {
9889 return false;
9890 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +00009891 if (pci_enable_msix(tp->pdev, msix_ent, rc))
9892 return false;
Joe Perches05dbe002010-02-17 19:44:19 +00009893 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
9894 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +00009895 tp->irq_cnt = rc;
9896 }
9897
9898 for (i = 0; i < tp->irq_max; i++)
9899 tp->napi[i].irq_vec = msix_ent[i].vector;
9900
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009901 netif_set_real_num_tx_queues(tp->dev, 1);
9902 rc = tp->irq_cnt > 1 ? tp->irq_cnt - 1 : 1;
9903 if (netif_set_real_num_rx_queues(tp->dev, rc)) {
9904 pci_disable_msix(tp->pdev);
9905 return false;
9906 }
Matt Carlsonb92b9042010-11-24 08:31:51 +00009907
9908 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +00009909 tg3_flag_set(tp, ENABLE_RSS);
Matt Carlsond78b59f2011-04-05 14:22:46 +00009910
9911 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
9912 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Joe Perches63c3a662011-04-26 08:12:10 +00009913 tg3_flag_set(tp, ENABLE_TSS);
Matt Carlsonb92b9042010-11-24 08:31:51 +00009914 netif_set_real_num_tx_queues(tp->dev, tp->irq_cnt - 1);
9915 }
9916 }
Matt Carlson2430b032010-06-05 17:24:34 +00009917
Matt Carlson679563f2009-09-01 12:55:46 +00009918 return true;
9919}
9920
Matt Carlson07b01732009-08-28 14:01:15 +00009921static void tg3_ints_init(struct tg3 *tp)
9922{
Joe Perches63c3a662011-04-26 08:12:10 +00009923 if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
9924 !tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +00009925 /* All MSI supporting chips should support tagged
9926 * status. Assert that this is the case.
9927 */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009928 netdev_warn(tp->dev,
9929 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +00009930 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +00009931 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009932
Joe Perches63c3a662011-04-26 08:12:10 +00009933 if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
9934 tg3_flag_set(tp, USING_MSIX);
9935 else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
9936 tg3_flag_set(tp, USING_MSI);
Matt Carlson679563f2009-09-01 12:55:46 +00009937
Joe Perches63c3a662011-04-26 08:12:10 +00009938 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009939 u32 msi_mode = tr32(MSGINT_MODE);
Joe Perches63c3a662011-04-26 08:12:10 +00009940 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009941 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +00009942 if (!tg3_flag(tp, 1SHOT_MSI))
9943 msi_mode |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlson679563f2009-09-01 12:55:46 +00009944 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
9945 }
9946defcfg:
Joe Perches63c3a662011-04-26 08:12:10 +00009947 if (!tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009948 tp->irq_cnt = 1;
9949 tp->napi[0].irq_vec = tp->pdev->irq;
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009950 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -07009951 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +00009952 }
Matt Carlson07b01732009-08-28 14:01:15 +00009953}
9954
9955static void tg3_ints_fini(struct tg3 *tp)
9956{
Joe Perches63c3a662011-04-26 08:12:10 +00009957 if (tg3_flag(tp, USING_MSIX))
Matt Carlson679563f2009-09-01 12:55:46 +00009958 pci_disable_msix(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009959 else if (tg3_flag(tp, USING_MSI))
Matt Carlson679563f2009-09-01 12:55:46 +00009960 pci_disable_msi(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009961 tg3_flag_clear(tp, USING_MSI);
9962 tg3_flag_clear(tp, USING_MSIX);
9963 tg3_flag_clear(tp, ENABLE_RSS);
9964 tg3_flag_clear(tp, ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +00009965}
9966
Linus Torvalds1da177e2005-04-16 15:20:36 -07009967static int tg3_open(struct net_device *dev)
9968{
9969 struct tg3 *tp = netdev_priv(dev);
Matt Carlson4f125f42009-09-01 12:55:02 +00009970 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009971
Matt Carlson9e9fd122009-01-19 16:57:45 -08009972 if (tp->fw_needed) {
9973 err = tg3_request_firmware(tp);
9974 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
9975 if (err)
9976 return err;
9977 } else if (err) {
Joe Perches05dbe002010-02-17 19:44:19 +00009978 netdev_warn(tp->dev, "TSO capability disabled\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009979 tg3_flag_clear(tp, TSO_CAPABLE);
9980 } else if (!tg3_flag(tp, TSO_CAPABLE)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009981 netdev_notice(tp->dev, "TSO capability restored\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009982 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009983 }
9984 }
9985
Michael Chanc49a1562006-12-17 17:07:29 -08009986 netif_carrier_off(tp->dev);
9987
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009988 err = tg3_power_up(tp);
Matt Carlson2f751b62008-08-04 23:17:34 -07009989 if (err)
Michael Chanbc1c7562006-03-20 17:48:03 -08009990 return err;
Matt Carlson2f751b62008-08-04 23:17:34 -07009991
9992 tg3_full_lock(tp, 0);
Michael Chanbc1c7562006-03-20 17:48:03 -08009993
Linus Torvalds1da177e2005-04-16 15:20:36 -07009994 tg3_disable_ints(tp);
Joe Perches63c3a662011-04-26 08:12:10 +00009995 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009996
David S. Millerf47c11e2005-06-24 20:18:35 -07009997 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009998
Matt Carlson679563f2009-09-01 12:55:46 +00009999 /*
10000 * Setup interrupts first so we know how
10001 * many NAPI resources to allocate
10002 */
10003 tg3_ints_init(tp);
10004
Matt Carlson90415472011-12-16 13:33:23 +000010005 tg3_rss_check_indir_tbl(tp);
Matt Carlsonbcebcc42011-12-14 11:10:01 +000010006
Linus Torvalds1da177e2005-04-16 15:20:36 -070010007 /* The placement of this call is tied
10008 * to the setup and use of Host TX descriptors.
10009 */
10010 err = tg3_alloc_consistent(tp);
10011 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +000010012 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010013
Matt Carlson66cfd1b2010-09-30 10:34:30 +000010014 tg3_napi_init(tp);
10015
Matt Carlsonfed97812009-09-01 13:10:19 +000010016 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -070010017
Matt Carlson4f125f42009-09-01 12:55:02 +000010018 for (i = 0; i < tp->irq_cnt; i++) {
10019 struct tg3_napi *tnapi = &tp->napi[i];
10020 err = tg3_request_irq(tp, i);
10021 if (err) {
Matt Carlson5bc09182011-11-04 09:15:01 +000010022 for (i--; i >= 0; i--) {
10023 tnapi = &tp->napi[i];
Matt Carlson4f125f42009-09-01 12:55:02 +000010024 free_irq(tnapi->irq_vec, tnapi);
Matt Carlson5bc09182011-11-04 09:15:01 +000010025 }
10026 goto err_out2;
Matt Carlson4f125f42009-09-01 12:55:02 +000010027 }
10028 }
Matt Carlson07b01732009-08-28 14:01:15 +000010029
David S. Millerf47c11e2005-06-24 20:18:35 -070010030 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010031
Gary Zambrano8e7a22e2006-04-29 18:59:13 -070010032 err = tg3_init_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010033 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -070010034 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010035 tg3_free_rings(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010036 }
10037
David S. Millerf47c11e2005-06-24 20:18:35 -070010038 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010039
Matt Carlson07b01732009-08-28 14:01:15 +000010040 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +000010041 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010042
Joe Perches63c3a662011-04-26 08:12:10 +000010043 if (tg3_flag(tp, USING_MSI)) {
Michael Chan79381092005-04-21 17:13:59 -070010044 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -070010045
Michael Chan79381092005-04-21 17:13:59 -070010046 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -070010047 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070010048 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -070010049 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070010050 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -070010051
Matt Carlson679563f2009-09-01 12:55:46 +000010052 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -070010053 }
Michael Chanfcfa0a32006-03-20 22:28:41 -080010054
Joe Perches63c3a662011-04-26 08:12:10 +000010055 if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010056 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -080010057
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010058 tw32(PCIE_TRANSACTION_CFG,
10059 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -080010060 }
Michael Chan79381092005-04-21 17:13:59 -070010061 }
10062
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010063 tg3_phy_start(tp);
10064
David S. Millerf47c11e2005-06-24 20:18:35 -070010065 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010066
Matt Carlson21f76382012-02-22 12:35:21 +000010067 tg3_timer_start(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000010068 tg3_flag_set(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010069 tg3_enable_ints(tp);
10070
David S. Millerf47c11e2005-06-24 20:18:35 -070010071 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010072
Matt Carlsonfe5f5782009-09-01 13:09:39 +000010073 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010074
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000010075 /*
10076 * Reset loopback feature if it was turned on while the device was down
10077 * make sure that it's installed properly now.
10078 */
10079 if (dev->features & NETIF_F_LOOPBACK)
10080 tg3_set_loopback(dev, dev->features);
10081
Linus Torvalds1da177e2005-04-16 15:20:36 -070010082 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +000010083
Matt Carlson679563f2009-09-01 12:55:46 +000010084err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +000010085 for (i = tp->irq_cnt - 1; i >= 0; i--) {
10086 struct tg3_napi *tnapi = &tp->napi[i];
10087 free_irq(tnapi->irq_vec, tnapi);
10088 }
Matt Carlson07b01732009-08-28 14:01:15 +000010089
Matt Carlson679563f2009-09-01 12:55:46 +000010090err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +000010091 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000010092 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +000010093 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +000010094
10095err_out1:
10096 tg3_ints_fini(tp);
Matt Carlsoncd0d7222011-07-13 09:27:33 +000010097 tg3_frob_aux_power(tp, false);
10098 pci_set_power_state(tp->pdev, PCI_D3hot);
Matt Carlson07b01732009-08-28 14:01:15 +000010099 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010100}
10101
Linus Torvalds1da177e2005-04-16 15:20:36 -070010102static int tg3_close(struct net_device *dev)
10103{
Matt Carlson4f125f42009-09-01 12:55:02 +000010104 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010105 struct tg3 *tp = netdev_priv(dev);
10106
Matt Carlsonfed97812009-09-01 13:10:19 +000010107 tg3_napi_disable(tp);
Matt Carlsondb219972011-11-04 09:15:03 +000010108 tg3_reset_task_cancel(tp);
Michael Chan7faa0062006-02-02 17:29:28 -080010109
Matt Carlsonfe5f5782009-09-01 13:09:39 +000010110 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010111
Matt Carlson21f76382012-02-22 12:35:21 +000010112 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010113
Matt Carlson24bb4fb2009-10-05 17:55:29 +000010114 tg3_phy_stop(tp);
10115
David S. Millerf47c11e2005-06-24 20:18:35 -070010116 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010117
10118 tg3_disable_ints(tp);
10119
Michael Chan944d9802005-05-29 14:57:48 -070010120 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010121 tg3_free_rings(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000010122 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010123
David S. Millerf47c11e2005-06-24 20:18:35 -070010124 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010125
Matt Carlson4f125f42009-09-01 12:55:02 +000010126 for (i = tp->irq_cnt - 1; i >= 0; i--) {
10127 struct tg3_napi *tnapi = &tp->napi[i];
10128 free_irq(tnapi->irq_vec, tnapi);
10129 }
Matt Carlson07b01732009-08-28 14:01:15 +000010130
10131 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010132
Matt Carlson92feeab2011-12-08 14:40:14 +000010133 /* Clear stats across close / open calls */
10134 memset(&tp->net_stats_prev, 0, sizeof(tp->net_stats_prev));
10135 memset(&tp->estats_prev, 0, sizeof(tp->estats_prev));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010136
Matt Carlson66cfd1b2010-09-30 10:34:30 +000010137 tg3_napi_fini(tp);
10138
Linus Torvalds1da177e2005-04-16 15:20:36 -070010139 tg3_free_consistent(tp);
10140
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000010141 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080010142
10143 netif_carrier_off(tp->dev);
10144
Linus Torvalds1da177e2005-04-16 15:20:36 -070010145 return 0;
10146}
10147
Eric Dumazet511d2222010-07-07 20:44:24 +000010148static inline u64 get_stat64(tg3_stat64_t *val)
Stefan Buehler816f8b82008-08-15 14:10:54 -070010149{
10150 return ((u64)val->high << 32) | ((u64)val->low);
10151}
10152
Matt Carlson65ec6982012-02-28 23:33:37 +000010153static u64 tg3_calc_crc_errors(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010154{
10155 struct tg3_hw_stats *hw_stats = tp->hw_stats;
10156
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010157 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010158 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
10159 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010160 u32 val;
10161
Michael Chan569a5df2007-02-13 12:18:15 -080010162 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
10163 tg3_writephy(tp, MII_TG3_TEST1,
10164 val | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +000010165 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010166 } else
10167 val = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010168
10169 tp->phy_crc_errors += val;
10170
10171 return tp->phy_crc_errors;
10172 }
10173
10174 return get_stat64(&hw_stats->rx_fcs_errors);
10175}
10176
10177#define ESTAT_ADD(member) \
10178 estats->member = old_estats->member + \
Eric Dumazet511d2222010-07-07 20:44:24 +000010179 get_stat64(&hw_stats->member)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010180
Matt Carlson65ec6982012-02-28 23:33:37 +000010181static void tg3_get_estats(struct tg3 *tp, struct tg3_ethtool_stats *estats)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010182{
Linus Torvalds1da177e2005-04-16 15:20:36 -070010183 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
10184 struct tg3_hw_stats *hw_stats = tp->hw_stats;
10185
Linus Torvalds1da177e2005-04-16 15:20:36 -070010186 ESTAT_ADD(rx_octets);
10187 ESTAT_ADD(rx_fragments);
10188 ESTAT_ADD(rx_ucast_packets);
10189 ESTAT_ADD(rx_mcast_packets);
10190 ESTAT_ADD(rx_bcast_packets);
10191 ESTAT_ADD(rx_fcs_errors);
10192 ESTAT_ADD(rx_align_errors);
10193 ESTAT_ADD(rx_xon_pause_rcvd);
10194 ESTAT_ADD(rx_xoff_pause_rcvd);
10195 ESTAT_ADD(rx_mac_ctrl_rcvd);
10196 ESTAT_ADD(rx_xoff_entered);
10197 ESTAT_ADD(rx_frame_too_long_errors);
10198 ESTAT_ADD(rx_jabbers);
10199 ESTAT_ADD(rx_undersize_packets);
10200 ESTAT_ADD(rx_in_length_errors);
10201 ESTAT_ADD(rx_out_length_errors);
10202 ESTAT_ADD(rx_64_or_less_octet_packets);
10203 ESTAT_ADD(rx_65_to_127_octet_packets);
10204 ESTAT_ADD(rx_128_to_255_octet_packets);
10205 ESTAT_ADD(rx_256_to_511_octet_packets);
10206 ESTAT_ADD(rx_512_to_1023_octet_packets);
10207 ESTAT_ADD(rx_1024_to_1522_octet_packets);
10208 ESTAT_ADD(rx_1523_to_2047_octet_packets);
10209 ESTAT_ADD(rx_2048_to_4095_octet_packets);
10210 ESTAT_ADD(rx_4096_to_8191_octet_packets);
10211 ESTAT_ADD(rx_8192_to_9022_octet_packets);
10212
10213 ESTAT_ADD(tx_octets);
10214 ESTAT_ADD(tx_collisions);
10215 ESTAT_ADD(tx_xon_sent);
10216 ESTAT_ADD(tx_xoff_sent);
10217 ESTAT_ADD(tx_flow_control);
10218 ESTAT_ADD(tx_mac_errors);
10219 ESTAT_ADD(tx_single_collisions);
10220 ESTAT_ADD(tx_mult_collisions);
10221 ESTAT_ADD(tx_deferred);
10222 ESTAT_ADD(tx_excessive_collisions);
10223 ESTAT_ADD(tx_late_collisions);
10224 ESTAT_ADD(tx_collide_2times);
10225 ESTAT_ADD(tx_collide_3times);
10226 ESTAT_ADD(tx_collide_4times);
10227 ESTAT_ADD(tx_collide_5times);
10228 ESTAT_ADD(tx_collide_6times);
10229 ESTAT_ADD(tx_collide_7times);
10230 ESTAT_ADD(tx_collide_8times);
10231 ESTAT_ADD(tx_collide_9times);
10232 ESTAT_ADD(tx_collide_10times);
10233 ESTAT_ADD(tx_collide_11times);
10234 ESTAT_ADD(tx_collide_12times);
10235 ESTAT_ADD(tx_collide_13times);
10236 ESTAT_ADD(tx_collide_14times);
10237 ESTAT_ADD(tx_collide_15times);
10238 ESTAT_ADD(tx_ucast_packets);
10239 ESTAT_ADD(tx_mcast_packets);
10240 ESTAT_ADD(tx_bcast_packets);
10241 ESTAT_ADD(tx_carrier_sense_errors);
10242 ESTAT_ADD(tx_discards);
10243 ESTAT_ADD(tx_errors);
10244
10245 ESTAT_ADD(dma_writeq_full);
10246 ESTAT_ADD(dma_write_prioq_full);
10247 ESTAT_ADD(rxbds_empty);
10248 ESTAT_ADD(rx_discards);
10249 ESTAT_ADD(rx_errors);
10250 ESTAT_ADD(rx_threshold_hit);
10251
10252 ESTAT_ADD(dma_readq_full);
10253 ESTAT_ADD(dma_read_prioq_full);
10254 ESTAT_ADD(tx_comp_queue_full);
10255
10256 ESTAT_ADD(ring_set_send_prod_index);
10257 ESTAT_ADD(ring_status_update);
10258 ESTAT_ADD(nic_irqs);
10259 ESTAT_ADD(nic_avoided_irqs);
10260 ESTAT_ADD(nic_tx_threshold_hit);
10261
Matt Carlson4452d092011-05-19 12:12:51 +000010262 ESTAT_ADD(mbuf_lwm_thresh_hit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010263}
10264
Matt Carlson65ec6982012-02-28 23:33:37 +000010265static void tg3_get_nstats(struct tg3 *tp, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010266{
Eric Dumazet511d2222010-07-07 20:44:24 +000010267 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010268 struct tg3_hw_stats *hw_stats = tp->hw_stats;
10269
Linus Torvalds1da177e2005-04-16 15:20:36 -070010270 stats->rx_packets = old_stats->rx_packets +
10271 get_stat64(&hw_stats->rx_ucast_packets) +
10272 get_stat64(&hw_stats->rx_mcast_packets) +
10273 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010274
Linus Torvalds1da177e2005-04-16 15:20:36 -070010275 stats->tx_packets = old_stats->tx_packets +
10276 get_stat64(&hw_stats->tx_ucast_packets) +
10277 get_stat64(&hw_stats->tx_mcast_packets) +
10278 get_stat64(&hw_stats->tx_bcast_packets);
10279
10280 stats->rx_bytes = old_stats->rx_bytes +
10281 get_stat64(&hw_stats->rx_octets);
10282 stats->tx_bytes = old_stats->tx_bytes +
10283 get_stat64(&hw_stats->tx_octets);
10284
10285 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -070010286 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010287 stats->tx_errors = old_stats->tx_errors +
10288 get_stat64(&hw_stats->tx_errors) +
10289 get_stat64(&hw_stats->tx_mac_errors) +
10290 get_stat64(&hw_stats->tx_carrier_sense_errors) +
10291 get_stat64(&hw_stats->tx_discards);
10292
10293 stats->multicast = old_stats->multicast +
10294 get_stat64(&hw_stats->rx_mcast_packets);
10295 stats->collisions = old_stats->collisions +
10296 get_stat64(&hw_stats->tx_collisions);
10297
10298 stats->rx_length_errors = old_stats->rx_length_errors +
10299 get_stat64(&hw_stats->rx_frame_too_long_errors) +
10300 get_stat64(&hw_stats->rx_undersize_packets);
10301
10302 stats->rx_over_errors = old_stats->rx_over_errors +
10303 get_stat64(&hw_stats->rxbds_empty);
10304 stats->rx_frame_errors = old_stats->rx_frame_errors +
10305 get_stat64(&hw_stats->rx_align_errors);
10306 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
10307 get_stat64(&hw_stats->tx_discards);
10308 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
10309 get_stat64(&hw_stats->tx_carrier_sense_errors);
10310
10311 stats->rx_crc_errors = old_stats->rx_crc_errors +
Matt Carlson65ec6982012-02-28 23:33:37 +000010312 tg3_calc_crc_errors(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010313
John W. Linville4f63b872005-09-12 14:43:18 -070010314 stats->rx_missed_errors = old_stats->rx_missed_errors +
10315 get_stat64(&hw_stats->rx_discards);
10316
Eric Dumazetb0057c52010-10-10 19:55:52 +000010317 stats->rx_dropped = tp->rx_dropped;
Eric Dumazet48855432011-10-24 07:53:03 +000010318 stats->tx_dropped = tp->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010319}
10320
Linus Torvalds1da177e2005-04-16 15:20:36 -070010321static int tg3_get_regs_len(struct net_device *dev)
10322{
Matt Carlson97bd8e42011-04-13 11:05:04 +000010323 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010324}
10325
10326static void tg3_get_regs(struct net_device *dev,
10327 struct ethtool_regs *regs, void *_p)
10328{
Linus Torvalds1da177e2005-04-16 15:20:36 -070010329 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010330
10331 regs->version = 0;
10332
Matt Carlson97bd8e42011-04-13 11:05:04 +000010333 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010334
Matt Carlson80096062010-08-02 11:26:06 +000010335 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010336 return;
10337
David S. Millerf47c11e2005-06-24 20:18:35 -070010338 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010339
Matt Carlson97bd8e42011-04-13 11:05:04 +000010340 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010341
David S. Millerf47c11e2005-06-24 20:18:35 -070010342 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010343}
10344
10345static int tg3_get_eeprom_len(struct net_device *dev)
10346{
10347 struct tg3 *tp = netdev_priv(dev);
10348
10349 return tp->nvram_size;
10350}
10351
Linus Torvalds1da177e2005-04-16 15:20:36 -070010352static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
10353{
10354 struct tg3 *tp = netdev_priv(dev);
10355 int ret;
10356 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -080010357 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010358 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010359
Joe Perches63c3a662011-04-26 08:12:10 +000010360 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000010361 return -EINVAL;
10362
Matt Carlson80096062010-08-02 11:26:06 +000010363 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010364 return -EAGAIN;
10365
Linus Torvalds1da177e2005-04-16 15:20:36 -070010366 offset = eeprom->offset;
10367 len = eeprom->len;
10368 eeprom->len = 0;
10369
10370 eeprom->magic = TG3_EEPROM_MAGIC;
10371
10372 if (offset & 3) {
10373 /* adjustments to start on required 4 byte boundary */
10374 b_offset = offset & 3;
10375 b_count = 4 - b_offset;
10376 if (b_count > len) {
10377 /* i.e. offset=1 len=2 */
10378 b_count = len;
10379 }
Matt Carlsona9dc5292009-02-25 14:25:30 +000010380 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010381 if (ret)
10382 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +000010383 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010384 len -= b_count;
10385 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010386 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010387 }
10388
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010389 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010390 pd = &data[eeprom->len];
10391 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010392 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010393 if (ret) {
10394 eeprom->len += i;
10395 return ret;
10396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010397 memcpy(pd + i, &val, 4);
10398 }
10399 eeprom->len += i;
10400
10401 if (len & 3) {
10402 /* read last bytes not ending on 4 byte boundary */
10403 pd = &data[eeprom->len];
10404 b_count = len & 3;
10405 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010406 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010407 if (ret)
10408 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080010409 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010410 eeprom->len += b_count;
10411 }
10412 return 0;
10413}
10414
Linus Torvalds1da177e2005-04-16 15:20:36 -070010415static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
10416{
10417 struct tg3 *tp = netdev_priv(dev);
10418 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080010419 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010420 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010421 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010422
Matt Carlson80096062010-08-02 11:26:06 +000010423 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010424 return -EAGAIN;
10425
Joe Perches63c3a662011-04-26 08:12:10 +000010426 if (tg3_flag(tp, NO_NVRAM) ||
Matt Carlsondf259d82009-04-20 06:57:14 +000010427 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010428 return -EINVAL;
10429
10430 offset = eeprom->offset;
10431 len = eeprom->len;
10432
10433 if ((b_offset = (offset & 3))) {
10434 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010435 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010436 if (ret)
10437 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010438 len += b_offset;
10439 offset &= ~3;
Michael Chan1c8594b42005-04-21 17:12:46 -070010440 if (len < 4)
10441 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010442 }
10443
10444 odd_len = 0;
Michael Chan1c8594b42005-04-21 17:12:46 -070010445 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010446 /* adjustments to end on required 4 byte boundary */
10447 odd_len = 1;
10448 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010449 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010450 if (ret)
10451 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010452 }
10453
10454 buf = data;
10455 if (b_offset || odd_len) {
10456 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010457 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010458 return -ENOMEM;
10459 if (b_offset)
10460 memcpy(buf, &start, 4);
10461 if (odd_len)
10462 memcpy(buf+len-4, &end, 4);
10463 memcpy(buf + b_offset, data, eeprom->len);
10464 }
10465
10466 ret = tg3_nvram_write_block(tp, offset, len, buf);
10467
10468 if (buf != data)
10469 kfree(buf);
10470
10471 return ret;
10472}
10473
10474static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
10475{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010476 struct tg3 *tp = netdev_priv(dev);
10477
Joe Perches63c3a662011-04-26 08:12:10 +000010478 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010479 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010480 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010481 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010482 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10483 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010484 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010485
Linus Torvalds1da177e2005-04-16 15:20:36 -070010486 cmd->supported = (SUPPORTED_Autoneg);
10487
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010488 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010489 cmd->supported |= (SUPPORTED_1000baseT_Half |
10490 SUPPORTED_1000baseT_Full);
10491
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010492 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010493 cmd->supported |= (SUPPORTED_100baseT_Half |
10494 SUPPORTED_100baseT_Full |
10495 SUPPORTED_10baseT_Half |
10496 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -080010497 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -070010498 cmd->port = PORT_TP;
10499 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010500 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -070010501 cmd->port = PORT_FIBRE;
10502 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010503
Linus Torvalds1da177e2005-04-16 15:20:36 -070010504 cmd->advertising = tp->link_config.advertising;
Matt Carlson5bb09772011-06-13 13:39:00 +000010505 if (tg3_flag(tp, PAUSE_AUTONEG)) {
10506 if (tp->link_config.flowctrl & FLOW_CTRL_RX) {
10507 if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
10508 cmd->advertising |= ADVERTISED_Pause;
10509 } else {
10510 cmd->advertising |= ADVERTISED_Pause |
10511 ADVERTISED_Asym_Pause;
10512 }
10513 } else if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
10514 cmd->advertising |= ADVERTISED_Asym_Pause;
10515 }
10516 }
Matt Carlson859edb22011-12-08 14:40:16 +000010517 if (netif_running(dev) && netif_carrier_ok(dev)) {
David Decotigny70739492011-04-27 18:32:40 +000010518 ethtool_cmd_speed_set(cmd, tp->link_config.active_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010519 cmd->duplex = tp->link_config.active_duplex;
Matt Carlson859edb22011-12-08 14:40:16 +000010520 cmd->lp_advertising = tp->link_config.rmt_adv;
Matt Carlsone348c5e2011-11-21 15:01:20 +000010521 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
10522 if (tp->phy_flags & TG3_PHYFLG_MDIX_STATE)
10523 cmd->eth_tp_mdix = ETH_TP_MDI_X;
10524 else
10525 cmd->eth_tp_mdix = ETH_TP_MDI;
10526 }
Matt Carlson64c22182010-10-14 10:37:44 +000010527 } else {
Matt Carlsone7405222012-02-13 15:20:16 +000010528 ethtool_cmd_speed_set(cmd, SPEED_UNKNOWN);
10529 cmd->duplex = DUPLEX_UNKNOWN;
Matt Carlsone348c5e2011-11-21 15:01:20 +000010530 cmd->eth_tp_mdix = ETH_TP_MDI_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010531 }
Matt Carlson882e9792009-09-01 13:21:36 +000010532 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +000010533 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010534 cmd->autoneg = tp->link_config.autoneg;
10535 cmd->maxtxpkt = 0;
10536 cmd->maxrxpkt = 0;
10537 return 0;
10538}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010539
Linus Torvalds1da177e2005-04-16 15:20:36 -070010540static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
10541{
10542 struct tg3 *tp = netdev_priv(dev);
David Decotigny25db0332011-04-27 18:32:39 +000010543 u32 speed = ethtool_cmd_speed(cmd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010544
Joe Perches63c3a662011-04-26 08:12:10 +000010545 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010546 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010547 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010548 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010549 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10550 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010551 }
10552
Matt Carlson7e5856b2009-02-25 14:23:01 +000010553 if (cmd->autoneg != AUTONEG_ENABLE &&
10554 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -070010555 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +000010556
10557 if (cmd->autoneg == AUTONEG_DISABLE &&
10558 cmd->duplex != DUPLEX_FULL &&
10559 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -070010560 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010561
Matt Carlson7e5856b2009-02-25 14:23:01 +000010562 if (cmd->autoneg == AUTONEG_ENABLE) {
10563 u32 mask = ADVERTISED_Autoneg |
10564 ADVERTISED_Pause |
10565 ADVERTISED_Asym_Pause;
10566
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010567 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010568 mask |= ADVERTISED_1000baseT_Half |
10569 ADVERTISED_1000baseT_Full;
10570
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010571 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010572 mask |= ADVERTISED_100baseT_Half |
10573 ADVERTISED_100baseT_Full |
10574 ADVERTISED_10baseT_Half |
10575 ADVERTISED_10baseT_Full |
10576 ADVERTISED_TP;
10577 else
10578 mask |= ADVERTISED_FIBRE;
10579
10580 if (cmd->advertising & ~mask)
10581 return -EINVAL;
10582
10583 mask &= (ADVERTISED_1000baseT_Half |
10584 ADVERTISED_1000baseT_Full |
10585 ADVERTISED_100baseT_Half |
10586 ADVERTISED_100baseT_Full |
10587 ADVERTISED_10baseT_Half |
10588 ADVERTISED_10baseT_Full);
10589
10590 cmd->advertising &= mask;
10591 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010592 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
David Decotigny25db0332011-04-27 18:32:39 +000010593 if (speed != SPEED_1000)
Matt Carlson7e5856b2009-02-25 14:23:01 +000010594 return -EINVAL;
10595
10596 if (cmd->duplex != DUPLEX_FULL)
10597 return -EINVAL;
10598 } else {
David Decotigny25db0332011-04-27 18:32:39 +000010599 if (speed != SPEED_100 &&
10600 speed != SPEED_10)
Matt Carlson7e5856b2009-02-25 14:23:01 +000010601 return -EINVAL;
10602 }
10603 }
10604
David S. Millerf47c11e2005-06-24 20:18:35 -070010605 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010606
10607 tp->link_config.autoneg = cmd->autoneg;
10608 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -070010609 tp->link_config.advertising = (cmd->advertising |
10610 ADVERTISED_Autoneg);
Matt Carlsone7405222012-02-13 15:20:16 +000010611 tp->link_config.speed = SPEED_UNKNOWN;
10612 tp->link_config.duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010613 } else {
10614 tp->link_config.advertising = 0;
David Decotigny25db0332011-04-27 18:32:39 +000010615 tp->link_config.speed = speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010616 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010617 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010618
Linus Torvalds1da177e2005-04-16 15:20:36 -070010619 if (netif_running(dev))
10620 tg3_setup_phy(tp, 1);
10621
David S. Millerf47c11e2005-06-24 20:18:35 -070010622 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010623
Linus Torvalds1da177e2005-04-16 15:20:36 -070010624 return 0;
10625}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010626
Linus Torvalds1da177e2005-04-16 15:20:36 -070010627static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
10628{
10629 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010630
Rick Jones68aad782011-11-07 13:29:27 +000010631 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
10632 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
10633 strlcpy(info->fw_version, tp->fw_ver, sizeof(info->fw_version));
10634 strlcpy(info->bus_info, pci_name(tp->pdev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010635}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010636
Linus Torvalds1da177e2005-04-16 15:20:36 -070010637static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10638{
10639 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010640
Joe Perches63c3a662011-04-26 08:12:10 +000010641 if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070010642 wol->supported = WAKE_MAGIC;
10643 else
10644 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010645 wol->wolopts = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010646 if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010647 wol->wolopts = WAKE_MAGIC;
10648 memset(&wol->sopass, 0, sizeof(wol->sopass));
10649}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010650
Linus Torvalds1da177e2005-04-16 15:20:36 -070010651static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10652{
10653 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070010654 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010655
Linus Torvalds1da177e2005-04-16 15:20:36 -070010656 if (wol->wolopts & ~WAKE_MAGIC)
10657 return -EINVAL;
10658 if ((wol->wolopts & WAKE_MAGIC) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010659 !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010660 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010661
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010662 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
10663
David S. Millerf47c11e2005-06-24 20:18:35 -070010664 spin_lock_bh(&tp->lock);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010665 if (device_may_wakeup(dp))
Joe Perches63c3a662011-04-26 08:12:10 +000010666 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010667 else
Joe Perches63c3a662011-04-26 08:12:10 +000010668 tg3_flag_clear(tp, WOL_ENABLE);
David S. Millerf47c11e2005-06-24 20:18:35 -070010669 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010670
Linus Torvalds1da177e2005-04-16 15:20:36 -070010671 return 0;
10672}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010673
Linus Torvalds1da177e2005-04-16 15:20:36 -070010674static u32 tg3_get_msglevel(struct net_device *dev)
10675{
10676 struct tg3 *tp = netdev_priv(dev);
10677 return tp->msg_enable;
10678}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010679
Linus Torvalds1da177e2005-04-16 15:20:36 -070010680static void tg3_set_msglevel(struct net_device *dev, u32 value)
10681{
10682 struct tg3 *tp = netdev_priv(dev);
10683 tp->msg_enable = value;
10684}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010685
Linus Torvalds1da177e2005-04-16 15:20:36 -070010686static int tg3_nway_reset(struct net_device *dev)
10687{
10688 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010689 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010690
Linus Torvalds1da177e2005-04-16 15:20:36 -070010691 if (!netif_running(dev))
10692 return -EAGAIN;
10693
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010694 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070010695 return -EINVAL;
10696
Joe Perches63c3a662011-04-26 08:12:10 +000010697 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010698 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010699 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010700 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010701 } else {
10702 u32 bmcr;
10703
10704 spin_lock_bh(&tp->lock);
10705 r = -EINVAL;
10706 tg3_readphy(tp, MII_BMCR, &bmcr);
10707 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
10708 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010709 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010710 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
10711 BMCR_ANENABLE);
10712 r = 0;
10713 }
10714 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010715 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010716
Linus Torvalds1da177e2005-04-16 15:20:36 -070010717 return r;
10718}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010719
Linus Torvalds1da177e2005-04-16 15:20:36 -070010720static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10721{
10722 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010723
Matt Carlson2c49a442010-09-30 10:34:35 +000010724 ering->rx_max_pending = tp->rx_std_ring_mask;
Joe Perches63c3a662011-04-26 08:12:10 +000010725 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson2c49a442010-09-30 10:34:35 +000010726 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080010727 else
10728 ering->rx_jumbo_max_pending = 0;
10729
10730 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010731
10732 ering->rx_pending = tp->rx_pending;
Joe Perches63c3a662011-04-26 08:12:10 +000010733 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Michael Chan4f81c322006-03-20 21:33:42 -080010734 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
10735 else
10736 ering->rx_jumbo_pending = 0;
10737
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010738 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010739}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010740
Linus Torvalds1da177e2005-04-16 15:20:36 -070010741static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10742{
10743 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000010744 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010745
Matt Carlson2c49a442010-09-30 10:34:35 +000010746 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
10747 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070010748 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
10749 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Joe Perches63c3a662011-04-26 08:12:10 +000010750 (tg3_flag(tp, TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070010751 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010752 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010753
Michael Chanbbe832c2005-06-24 20:20:04 -070010754 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010755 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010756 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010757 irq_sync = 1;
10758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010759
Michael Chanbbe832c2005-06-24 20:20:04 -070010760 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010761
Linus Torvalds1da177e2005-04-16 15:20:36 -070010762 tp->rx_pending = ering->rx_pending;
10763
Joe Perches63c3a662011-04-26 08:12:10 +000010764 if (tg3_flag(tp, MAX_RXPEND_64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010765 tp->rx_pending > 63)
10766 tp->rx_pending = 63;
10767 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000010768
Matt Carlson6fd45cb2010-09-15 08:59:57 +000010769 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000010770 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010771
10772 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070010773 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -070010774 err = tg3_restart_hw(tp, 1);
10775 if (!err)
10776 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010777 }
10778
David S. Millerf47c11e2005-06-24 20:18:35 -070010779 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010780
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010781 if (irq_sync && !err)
10782 tg3_phy_start(tp);
10783
Michael Chanb9ec6c12006-07-25 16:37:27 -070010784 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010785}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010786
Linus Torvalds1da177e2005-04-16 15:20:36 -070010787static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10788{
10789 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010790
Joe Perches63c3a662011-04-26 08:12:10 +000010791 epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
Matt Carlson8d018622007-12-20 20:05:44 -080010792
Matt Carlson4a2db502011-12-08 14:40:17 +000010793 if (tp->link_config.flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080010794 epause->rx_pause = 1;
10795 else
10796 epause->rx_pause = 0;
10797
Matt Carlson4a2db502011-12-08 14:40:17 +000010798 if (tp->link_config.flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080010799 epause->tx_pause = 1;
10800 else
10801 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010802}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010803
Linus Torvalds1da177e2005-04-16 15:20:36 -070010804static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10805{
10806 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010807 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010808
Joe Perches63c3a662011-04-26 08:12:10 +000010809 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson27121682010-02-17 15:16:57 +000010810 u32 newadv;
10811 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010812
Matt Carlson27121682010-02-17 15:16:57 +000010813 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010814
Matt Carlson27121682010-02-17 15:16:57 +000010815 if (!(phydev->supported & SUPPORTED_Pause) ||
10816 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000010817 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000010818 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010819
Matt Carlson27121682010-02-17 15:16:57 +000010820 tp->link_config.flowctrl = 0;
10821 if (epause->rx_pause) {
10822 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010823
Matt Carlson27121682010-02-17 15:16:57 +000010824 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080010825 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000010826 newadv = ADVERTISED_Pause;
10827 } else
10828 newadv = ADVERTISED_Pause |
10829 ADVERTISED_Asym_Pause;
10830 } else if (epause->tx_pause) {
10831 tp->link_config.flowctrl |= FLOW_CTRL_TX;
10832 newadv = ADVERTISED_Asym_Pause;
10833 } else
10834 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010835
Matt Carlson27121682010-02-17 15:16:57 +000010836 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010837 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010838 else
Joe Perches63c3a662011-04-26 08:12:10 +000010839 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010840
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010841 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000010842 u32 oldadv = phydev->advertising &
10843 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
10844 if (oldadv != newadv) {
10845 phydev->advertising &=
10846 ~(ADVERTISED_Pause |
10847 ADVERTISED_Asym_Pause);
10848 phydev->advertising |= newadv;
10849 if (phydev->autoneg) {
10850 /*
10851 * Always renegotiate the link to
10852 * inform our link partner of our
10853 * flow control settings, even if the
10854 * flow control is forced. Let
10855 * tg3_adjust_link() do the final
10856 * flow control setup.
10857 */
10858 return phy_start_aneg(phydev);
10859 }
10860 }
10861
10862 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010863 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000010864 } else {
Matt Carlsonc6700ce2012-02-13 15:20:15 +000010865 tp->link_config.advertising &=
Matt Carlson27121682010-02-17 15:16:57 +000010866 ~(ADVERTISED_Pause |
10867 ADVERTISED_Asym_Pause);
Matt Carlsonc6700ce2012-02-13 15:20:15 +000010868 tp->link_config.advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010869 }
10870 } else {
10871 int irq_sync = 0;
10872
10873 if (netif_running(dev)) {
10874 tg3_netif_stop(tp);
10875 irq_sync = 1;
10876 }
10877
10878 tg3_full_lock(tp, irq_sync);
10879
10880 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010881 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010882 else
Joe Perches63c3a662011-04-26 08:12:10 +000010883 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010884 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010885 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010886 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010887 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010888 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010889 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010890 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010891 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010892
10893 if (netif_running(dev)) {
10894 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10895 err = tg3_restart_hw(tp, 1);
10896 if (!err)
10897 tg3_netif_start(tp);
10898 }
10899
10900 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010902
Michael Chanb9ec6c12006-07-25 16:37:27 -070010903 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010904}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010905
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010906static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010907{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010908 switch (sset) {
10909 case ETH_SS_TEST:
10910 return TG3_NUM_TEST;
10911 case ETH_SS_STATS:
10912 return TG3_NUM_STATS;
10913 default:
10914 return -EOPNOTSUPP;
10915 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070010916}
10917
Matt Carlson90415472011-12-16 13:33:23 +000010918static int tg3_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
10919 u32 *rules __always_unused)
10920{
10921 struct tg3 *tp = netdev_priv(dev);
10922
10923 if (!tg3_flag(tp, SUPPORT_MSIX))
10924 return -EOPNOTSUPP;
10925
10926 switch (info->cmd) {
10927 case ETHTOOL_GRXRINGS:
10928 if (netif_running(tp->dev))
10929 info->data = tp->irq_cnt;
10930 else {
10931 info->data = num_online_cpus();
10932 if (info->data > TG3_IRQ_MAX_VECS_RSS)
10933 info->data = TG3_IRQ_MAX_VECS_RSS;
10934 }
10935
10936 /* The first interrupt vector only
10937 * handles link interrupts.
10938 */
10939 info->data -= 1;
10940 return 0;
10941
10942 default:
10943 return -EOPNOTSUPP;
10944 }
10945}
10946
10947static u32 tg3_get_rxfh_indir_size(struct net_device *dev)
10948{
10949 u32 size = 0;
10950 struct tg3 *tp = netdev_priv(dev);
10951
10952 if (tg3_flag(tp, SUPPORT_MSIX))
10953 size = TG3_RSS_INDIR_TBL_SIZE;
10954
10955 return size;
10956}
10957
10958static int tg3_get_rxfh_indir(struct net_device *dev, u32 *indir)
10959{
10960 struct tg3 *tp = netdev_priv(dev);
10961 int i;
10962
10963 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
10964 indir[i] = tp->rss_ind_tbl[i];
10965
10966 return 0;
10967}
10968
10969static int tg3_set_rxfh_indir(struct net_device *dev, const u32 *indir)
10970{
10971 struct tg3 *tp = netdev_priv(dev);
10972 size_t i;
10973
10974 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
10975 tp->rss_ind_tbl[i] = indir[i];
10976
10977 if (!netif_running(dev) || !tg3_flag(tp, ENABLE_RSS))
10978 return 0;
10979
10980 /* It is legal to write the indirection
10981 * table while the device is running.
10982 */
10983 tg3_full_lock(tp, 0);
10984 tg3_rss_write_indir_tbl(tp);
10985 tg3_full_unlock(tp);
10986
10987 return 0;
10988}
10989
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010990static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010991{
10992 switch (stringset) {
10993 case ETH_SS_STATS:
10994 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
10995 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070010996 case ETH_SS_TEST:
10997 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
10998 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010999 default:
11000 WARN_ON(1); /* we need a WARN() */
11001 break;
11002 }
11003}
11004
stephen hemminger81b87092011-04-04 08:43:50 +000011005static int tg3_set_phys_id(struct net_device *dev,
11006 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070011007{
11008 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070011009
11010 if (!netif_running(tp->dev))
11011 return -EAGAIN;
11012
stephen hemminger81b87092011-04-04 08:43:50 +000011013 switch (state) {
11014 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +000011015 return 1; /* cycle on/off once per second */
Michael Chan4009a932005-09-05 17:52:54 -070011016
stephen hemminger81b87092011-04-04 08:43:50 +000011017 case ETHTOOL_ID_ON:
11018 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
11019 LED_CTRL_1000MBPS_ON |
11020 LED_CTRL_100MBPS_ON |
11021 LED_CTRL_10MBPS_ON |
11022 LED_CTRL_TRAFFIC_OVERRIDE |
11023 LED_CTRL_TRAFFIC_BLINK |
11024 LED_CTRL_TRAFFIC_LED);
11025 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011026
stephen hemminger81b87092011-04-04 08:43:50 +000011027 case ETHTOOL_ID_OFF:
11028 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
11029 LED_CTRL_TRAFFIC_OVERRIDE);
11030 break;
Michael Chan4009a932005-09-05 17:52:54 -070011031
stephen hemminger81b87092011-04-04 08:43:50 +000011032 case ETHTOOL_ID_INACTIVE:
11033 tw32(MAC_LED_CTRL, tp->led_ctrl);
11034 break;
Michael Chan4009a932005-09-05 17:52:54 -070011035 }
stephen hemminger81b87092011-04-04 08:43:50 +000011036
Michael Chan4009a932005-09-05 17:52:54 -070011037 return 0;
11038}
11039
Matt Carlsonde6f31e2010-04-12 06:58:30 +000011040static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011041 struct ethtool_stats *estats, u64 *tmp_stats)
11042{
11043 struct tg3 *tp = netdev_priv(dev);
Matt Carlson0e6c9da2011-12-08 14:40:13 +000011044
Matt Carlsonb546e462012-02-13 15:20:09 +000011045 if (tp->hw_stats)
11046 tg3_get_estats(tp, (struct tg3_ethtool_stats *)tmp_stats);
11047 else
11048 memset(tmp_stats, 0, sizeof(struct tg3_ethtool_stats));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011049}
11050
Matt Carlson535a4902011-07-20 10:20:56 +000011051static __be32 *tg3_vpd_readblock(struct tg3 *tp, u32 *vpdlen)
Matt Carlsonc3e94502011-04-13 11:05:08 +000011052{
11053 int i;
11054 __be32 *buf;
11055 u32 offset = 0, len = 0;
11056 u32 magic, val;
11057
Joe Perches63c3a662011-04-26 08:12:10 +000011058 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
Matt Carlsonc3e94502011-04-13 11:05:08 +000011059 return NULL;
11060
11061 if (magic == TG3_EEPROM_MAGIC) {
11062 for (offset = TG3_NVM_DIR_START;
11063 offset < TG3_NVM_DIR_END;
11064 offset += TG3_NVM_DIRENT_SIZE) {
11065 if (tg3_nvram_read(tp, offset, &val))
11066 return NULL;
11067
11068 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
11069 TG3_NVM_DIRTYPE_EXTVPD)
11070 break;
11071 }
11072
11073 if (offset != TG3_NVM_DIR_END) {
11074 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
11075 if (tg3_nvram_read(tp, offset + 4, &offset))
11076 return NULL;
11077
11078 offset = tg3_nvram_logical_addr(tp, offset);
11079 }
11080 }
11081
11082 if (!offset || !len) {
11083 offset = TG3_NVM_VPD_OFF;
11084 len = TG3_NVM_VPD_LEN;
11085 }
11086
11087 buf = kmalloc(len, GFP_KERNEL);
11088 if (buf == NULL)
11089 return NULL;
11090
11091 if (magic == TG3_EEPROM_MAGIC) {
11092 for (i = 0; i < len; i += 4) {
11093 /* The data is in little-endian format in NVRAM.
11094 * Use the big-endian read routines to preserve
11095 * the byte order as it exists in NVRAM.
11096 */
11097 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
11098 goto error;
11099 }
11100 } else {
11101 u8 *ptr;
11102 ssize_t cnt;
11103 unsigned int pos = 0;
11104
11105 ptr = (u8 *)&buf[0];
11106 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
11107 cnt = pci_read_vpd(tp->pdev, pos,
11108 len - pos, ptr);
11109 if (cnt == -ETIMEDOUT || cnt == -EINTR)
11110 cnt = 0;
11111 else if (cnt < 0)
11112 goto error;
11113 }
11114 if (pos != len)
11115 goto error;
11116 }
11117
Matt Carlson535a4902011-07-20 10:20:56 +000011118 *vpdlen = len;
11119
Matt Carlsonc3e94502011-04-13 11:05:08 +000011120 return buf;
11121
11122error:
11123 kfree(buf);
11124 return NULL;
11125}
11126
Michael Chan566f86a2005-05-29 14:56:58 -070011127#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080011128#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
11129#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
11130#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Matt Carlson727a6d92011-06-13 13:38:58 +000011131#define NVRAM_SELFBOOT_FORMAT1_4_SIZE 0x20
11132#define NVRAM_SELFBOOT_FORMAT1_5_SIZE 0x24
Matt Carlsonbda18fa2011-07-20 10:20:57 +000011133#define NVRAM_SELFBOOT_FORMAT1_6_SIZE 0x50
Michael Chanb16250e2006-09-27 16:10:14 -070011134#define NVRAM_SELFBOOT_HW_SIZE 0x20
11135#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070011136
11137static int tg3_test_nvram(struct tg3 *tp)
11138{
Matt Carlson535a4902011-07-20 10:20:56 +000011139 u32 csum, magic, len;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011140 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010011141 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070011142
Joe Perches63c3a662011-04-26 08:12:10 +000011143 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000011144 return 0;
11145
Matt Carlsone4f34112009-02-25 14:25:00 +000011146 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080011147 return -EIO;
11148
Michael Chan1b277772006-03-20 22:27:48 -080011149 if (magic == TG3_EEPROM_MAGIC)
11150 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070011151 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080011152 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
11153 TG3_EEPROM_SB_FORMAT_1) {
11154 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
11155 case TG3_EEPROM_SB_REVISION_0:
11156 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
11157 break;
11158 case TG3_EEPROM_SB_REVISION_2:
11159 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
11160 break;
11161 case TG3_EEPROM_SB_REVISION_3:
11162 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
11163 break;
Matt Carlson727a6d92011-06-13 13:38:58 +000011164 case TG3_EEPROM_SB_REVISION_4:
11165 size = NVRAM_SELFBOOT_FORMAT1_4_SIZE;
11166 break;
11167 case TG3_EEPROM_SB_REVISION_5:
11168 size = NVRAM_SELFBOOT_FORMAT1_5_SIZE;
11169 break;
11170 case TG3_EEPROM_SB_REVISION_6:
11171 size = NVRAM_SELFBOOT_FORMAT1_6_SIZE;
11172 break;
Matt Carlsona5767de2007-11-12 21:10:58 -080011173 default:
Matt Carlson727a6d92011-06-13 13:38:58 +000011174 return -EIO;
Matt Carlsona5767de2007-11-12 21:10:58 -080011175 }
11176 } else
Michael Chan1b277772006-03-20 22:27:48 -080011177 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070011178 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
11179 size = NVRAM_SELFBOOT_HW_SIZE;
11180 else
Michael Chan1b277772006-03-20 22:27:48 -080011181 return -EIO;
11182
11183 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070011184 if (buf == NULL)
11185 return -ENOMEM;
11186
Michael Chan1b277772006-03-20 22:27:48 -080011187 err = -EIO;
11188 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000011189 err = tg3_nvram_read_be32(tp, i, &buf[j]);
11190 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070011191 break;
Michael Chan566f86a2005-05-29 14:56:58 -070011192 }
Michael Chan1b277772006-03-20 22:27:48 -080011193 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070011194 goto out;
11195
Michael Chan1b277772006-03-20 22:27:48 -080011196 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000011197 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080011198 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070011199 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080011200 u8 *buf8 = (u8 *) buf, csum8 = 0;
11201
Al Virob9fc7dc2007-12-17 22:59:57 -080011202 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080011203 TG3_EEPROM_SB_REVISION_2) {
11204 /* For rev 2, the csum doesn't include the MBA. */
11205 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
11206 csum8 += buf8[i];
11207 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
11208 csum8 += buf8[i];
11209 } else {
11210 for (i = 0; i < size; i++)
11211 csum8 += buf8[i];
11212 }
Michael Chan1b277772006-03-20 22:27:48 -080011213
Adrian Bunkad96b482006-04-05 22:21:04 -070011214 if (csum8 == 0) {
11215 err = 0;
11216 goto out;
11217 }
11218
11219 err = -EIO;
11220 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080011221 }
Michael Chan566f86a2005-05-29 14:56:58 -070011222
Al Virob9fc7dc2007-12-17 22:59:57 -080011223 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070011224 TG3_EEPROM_MAGIC_HW) {
11225 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000011226 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070011227 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070011228
11229 /* Separate the parity bits and the data bytes. */
11230 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
11231 if ((i == 0) || (i == 8)) {
11232 int l;
11233 u8 msk;
11234
11235 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
11236 parity[k++] = buf8[i] & msk;
11237 i++;
Matt Carlson859a588792010-04-05 10:19:28 +000011238 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070011239 int l;
11240 u8 msk;
11241
11242 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
11243 parity[k++] = buf8[i] & msk;
11244 i++;
11245
11246 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
11247 parity[k++] = buf8[i] & msk;
11248 i++;
11249 }
11250 data[j++] = buf8[i];
11251 }
11252
11253 err = -EIO;
11254 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
11255 u8 hw8 = hweight8(data[i]);
11256
11257 if ((hw8 & 0x1) && parity[i])
11258 goto out;
11259 else if (!(hw8 & 0x1) && !parity[i])
11260 goto out;
11261 }
11262 err = 0;
11263 goto out;
11264 }
11265
Matt Carlson01c3a392011-03-09 16:58:20 +000011266 err = -EIO;
11267
Michael Chan566f86a2005-05-29 14:56:58 -070011268 /* Bootstrap checksum at offset 0x10 */
11269 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000011270 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070011271 goto out;
11272
11273 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
11274 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000011275 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000011276 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070011277
Matt Carlsonc3e94502011-04-13 11:05:08 +000011278 kfree(buf);
11279
Matt Carlson535a4902011-07-20 10:20:56 +000011280 buf = tg3_vpd_readblock(tp, &len);
Matt Carlsonc3e94502011-04-13 11:05:08 +000011281 if (!buf)
11282 return -ENOMEM;
Matt Carlsond4894f32011-03-09 16:58:21 +000011283
Matt Carlson535a4902011-07-20 10:20:56 +000011284 i = pci_vpd_find_tag((u8 *)buf, 0, len, PCI_VPD_LRDT_RO_DATA);
Matt Carlsond4894f32011-03-09 16:58:21 +000011285 if (i > 0) {
11286 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
11287 if (j < 0)
11288 goto out;
11289
Matt Carlson535a4902011-07-20 10:20:56 +000011290 if (i + PCI_VPD_LRDT_TAG_SIZE + j > len)
Matt Carlsond4894f32011-03-09 16:58:21 +000011291 goto out;
11292
11293 i += PCI_VPD_LRDT_TAG_SIZE;
11294 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
11295 PCI_VPD_RO_KEYWORD_CHKSUM);
11296 if (j > 0) {
11297 u8 csum8 = 0;
11298
11299 j += PCI_VPD_INFO_FLD_HDR_SIZE;
11300
11301 for (i = 0; i <= j; i++)
11302 csum8 += ((u8 *)buf)[i];
11303
11304 if (csum8)
11305 goto out;
11306 }
11307 }
11308
Michael Chan566f86a2005-05-29 14:56:58 -070011309 err = 0;
11310
11311out:
11312 kfree(buf);
11313 return err;
11314}
11315
Michael Chanca430072005-05-29 14:57:23 -070011316#define TG3_SERDES_TIMEOUT_SEC 2
11317#define TG3_COPPER_TIMEOUT_SEC 6
11318
11319static int tg3_test_link(struct tg3 *tp)
11320{
11321 int i, max;
11322
11323 if (!netif_running(tp->dev))
11324 return -ENODEV;
11325
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011326 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070011327 max = TG3_SERDES_TIMEOUT_SEC;
11328 else
11329 max = TG3_COPPER_TIMEOUT_SEC;
11330
11331 for (i = 0; i < max; i++) {
11332 if (netif_carrier_ok(tp->dev))
11333 return 0;
11334
11335 if (msleep_interruptible(1000))
11336 break;
11337 }
11338
11339 return -EIO;
11340}
11341
Michael Chana71116d2005-05-29 14:58:11 -070011342/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080011343static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070011344{
Michael Chanb16250e2006-09-27 16:10:14 -070011345 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070011346 u32 offset, read_mask, write_mask, val, save_val, read_val;
11347 static struct {
11348 u16 offset;
11349 u16 flags;
11350#define TG3_FL_5705 0x1
11351#define TG3_FL_NOT_5705 0x2
11352#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070011353#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070011354 u32 read_mask;
11355 u32 write_mask;
11356 } reg_tbl[] = {
11357 /* MAC Control Registers */
11358 { MAC_MODE, TG3_FL_NOT_5705,
11359 0x00000000, 0x00ef6f8c },
11360 { MAC_MODE, TG3_FL_5705,
11361 0x00000000, 0x01ef6b8c },
11362 { MAC_STATUS, TG3_FL_NOT_5705,
11363 0x03800107, 0x00000000 },
11364 { MAC_STATUS, TG3_FL_5705,
11365 0x03800100, 0x00000000 },
11366 { MAC_ADDR_0_HIGH, 0x0000,
11367 0x00000000, 0x0000ffff },
11368 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011369 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070011370 { MAC_RX_MTU_SIZE, 0x0000,
11371 0x00000000, 0x0000ffff },
11372 { MAC_TX_MODE, 0x0000,
11373 0x00000000, 0x00000070 },
11374 { MAC_TX_LENGTHS, 0x0000,
11375 0x00000000, 0x00003fff },
11376 { MAC_RX_MODE, TG3_FL_NOT_5705,
11377 0x00000000, 0x000007fc },
11378 { MAC_RX_MODE, TG3_FL_5705,
11379 0x00000000, 0x000007dc },
11380 { MAC_HASH_REG_0, 0x0000,
11381 0x00000000, 0xffffffff },
11382 { MAC_HASH_REG_1, 0x0000,
11383 0x00000000, 0xffffffff },
11384 { MAC_HASH_REG_2, 0x0000,
11385 0x00000000, 0xffffffff },
11386 { MAC_HASH_REG_3, 0x0000,
11387 0x00000000, 0xffffffff },
11388
11389 /* Receive Data and Receive BD Initiator Control Registers. */
11390 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
11391 0x00000000, 0xffffffff },
11392 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
11393 0x00000000, 0xffffffff },
11394 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
11395 0x00000000, 0x00000003 },
11396 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
11397 0x00000000, 0xffffffff },
11398 { RCVDBDI_STD_BD+0, 0x0000,
11399 0x00000000, 0xffffffff },
11400 { RCVDBDI_STD_BD+4, 0x0000,
11401 0x00000000, 0xffffffff },
11402 { RCVDBDI_STD_BD+8, 0x0000,
11403 0x00000000, 0xffff0002 },
11404 { RCVDBDI_STD_BD+0xc, 0x0000,
11405 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011406
Michael Chana71116d2005-05-29 14:58:11 -070011407 /* Receive BD Initiator Control Registers. */
11408 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
11409 0x00000000, 0xffffffff },
11410 { RCVBDI_STD_THRESH, TG3_FL_5705,
11411 0x00000000, 0x000003ff },
11412 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
11413 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011414
Michael Chana71116d2005-05-29 14:58:11 -070011415 /* Host Coalescing Control Registers. */
11416 { HOSTCC_MODE, TG3_FL_NOT_5705,
11417 0x00000000, 0x00000004 },
11418 { HOSTCC_MODE, TG3_FL_5705,
11419 0x00000000, 0x000000f6 },
11420 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
11421 0x00000000, 0xffffffff },
11422 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
11423 0x00000000, 0x000003ff },
11424 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
11425 0x00000000, 0xffffffff },
11426 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
11427 0x00000000, 0x000003ff },
11428 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
11429 0x00000000, 0xffffffff },
11430 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
11431 0x00000000, 0x000000ff },
11432 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
11433 0x00000000, 0xffffffff },
11434 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
11435 0x00000000, 0x000000ff },
11436 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
11437 0x00000000, 0xffffffff },
11438 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
11439 0x00000000, 0xffffffff },
11440 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
11441 0x00000000, 0xffffffff },
11442 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
11443 0x00000000, 0x000000ff },
11444 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
11445 0x00000000, 0xffffffff },
11446 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
11447 0x00000000, 0x000000ff },
11448 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
11449 0x00000000, 0xffffffff },
11450 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
11451 0x00000000, 0xffffffff },
11452 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
11453 0x00000000, 0xffffffff },
11454 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
11455 0x00000000, 0xffffffff },
11456 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
11457 0x00000000, 0xffffffff },
11458 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
11459 0xffffffff, 0x00000000 },
11460 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
11461 0xffffffff, 0x00000000 },
11462
11463 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070011464 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070011465 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070011466 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070011467 0x00000000, 0x007fffff },
11468 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
11469 0x00000000, 0x0000003f },
11470 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
11471 0x00000000, 0x000001ff },
11472 { BUFMGR_MB_HIGH_WATER, 0x0000,
11473 0x00000000, 0x000001ff },
11474 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
11475 0xffffffff, 0x00000000 },
11476 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
11477 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011478
Michael Chana71116d2005-05-29 14:58:11 -070011479 /* Mailbox Registers */
11480 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
11481 0x00000000, 0x000001ff },
11482 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
11483 0x00000000, 0x000001ff },
11484 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
11485 0x00000000, 0x000007ff },
11486 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
11487 0x00000000, 0x000001ff },
11488
11489 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
11490 };
11491
Michael Chanb16250e2006-09-27 16:10:14 -070011492 is_5705 = is_5750 = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000011493 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chana71116d2005-05-29 14:58:11 -070011494 is_5705 = 1;
Joe Perches63c3a662011-04-26 08:12:10 +000011495 if (tg3_flag(tp, 5750_PLUS))
Michael Chanb16250e2006-09-27 16:10:14 -070011496 is_5750 = 1;
11497 }
Michael Chana71116d2005-05-29 14:58:11 -070011498
11499 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
11500 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
11501 continue;
11502
11503 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
11504 continue;
11505
Joe Perches63c3a662011-04-26 08:12:10 +000011506 if (tg3_flag(tp, IS_5788) &&
Michael Chana71116d2005-05-29 14:58:11 -070011507 (reg_tbl[i].flags & TG3_FL_NOT_5788))
11508 continue;
11509
Michael Chanb16250e2006-09-27 16:10:14 -070011510 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
11511 continue;
11512
Michael Chana71116d2005-05-29 14:58:11 -070011513 offset = (u32) reg_tbl[i].offset;
11514 read_mask = reg_tbl[i].read_mask;
11515 write_mask = reg_tbl[i].write_mask;
11516
11517 /* Save the original register content */
11518 save_val = tr32(offset);
11519
11520 /* Determine the read-only value. */
11521 read_val = save_val & read_mask;
11522
11523 /* Write zero to the register, then make sure the read-only bits
11524 * are not changed and the read/write bits are all zeros.
11525 */
11526 tw32(offset, 0);
11527
11528 val = tr32(offset);
11529
11530 /* Test the read-only and read/write bits. */
11531 if (((val & read_mask) != read_val) || (val & write_mask))
11532 goto out;
11533
11534 /* Write ones to all the bits defined by RdMask and WrMask, then
11535 * make sure the read-only bits are not changed and the
11536 * read/write bits are all ones.
11537 */
11538 tw32(offset, read_mask | write_mask);
11539
11540 val = tr32(offset);
11541
11542 /* Test the read-only bits. */
11543 if ((val & read_mask) != read_val)
11544 goto out;
11545
11546 /* Test the read/write bits. */
11547 if ((val & write_mask) != write_mask)
11548 goto out;
11549
11550 tw32(offset, save_val);
11551 }
11552
11553 return 0;
11554
11555out:
Michael Chan9f88f292006-12-07 00:22:54 -080011556 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000011557 netdev_err(tp->dev,
11558 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070011559 tw32(offset, save_val);
11560 return -EIO;
11561}
11562
Michael Chan7942e1d2005-05-29 14:58:36 -070011563static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
11564{
Arjan van de Venf71e1302006-03-03 21:33:57 -050011565 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070011566 int i;
11567 u32 j;
11568
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020011569 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070011570 for (j = 0; j < len; j += 4) {
11571 u32 val;
11572
11573 tg3_write_mem(tp, offset + j, test_pattern[i]);
11574 tg3_read_mem(tp, offset + j, &val);
11575 if (val != test_pattern[i])
11576 return -EIO;
11577 }
11578 }
11579 return 0;
11580}
11581
11582static int tg3_test_memory(struct tg3 *tp)
11583{
11584 static struct mem_entry {
11585 u32 offset;
11586 u32 len;
11587 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080011588 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070011589 { 0x00002000, 0x1c000},
11590 { 0xffffffff, 0x00000}
11591 }, mem_tbl_5705[] = {
11592 { 0x00000100, 0x0000c},
11593 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070011594 { 0x00004000, 0x00800},
11595 { 0x00006000, 0x01000},
11596 { 0x00008000, 0x02000},
11597 { 0x00010000, 0x0e000},
11598 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080011599 }, mem_tbl_5755[] = {
11600 { 0x00000200, 0x00008},
11601 { 0x00004000, 0x00800},
11602 { 0x00006000, 0x00800},
11603 { 0x00008000, 0x02000},
11604 { 0x00010000, 0x0c000},
11605 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070011606 }, mem_tbl_5906[] = {
11607 { 0x00000200, 0x00008},
11608 { 0x00004000, 0x00400},
11609 { 0x00006000, 0x00400},
11610 { 0x00008000, 0x01000},
11611 { 0x00010000, 0x01000},
11612 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011613 }, mem_tbl_5717[] = {
11614 { 0x00000200, 0x00008},
11615 { 0x00010000, 0x0a000},
11616 { 0x00020000, 0x13c00},
11617 { 0xffffffff, 0x00000}
11618 }, mem_tbl_57765[] = {
11619 { 0x00000200, 0x00008},
11620 { 0x00004000, 0x00800},
11621 { 0x00006000, 0x09800},
11622 { 0x00010000, 0x0a000},
11623 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070011624 };
11625 struct mem_entry *mem_tbl;
11626 int err = 0;
11627 int i;
11628
Joe Perches63c3a662011-04-26 08:12:10 +000011629 if (tg3_flag(tp, 5717_PLUS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011630 mem_tbl = mem_tbl_5717;
Matt Carlson55086ad2011-12-14 11:09:59 +000011631 else if (tg3_flag(tp, 57765_CLASS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011632 mem_tbl = mem_tbl_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000011633 else if (tg3_flag(tp, 5755_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080011634 mem_tbl = mem_tbl_5755;
11635 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
11636 mem_tbl = mem_tbl_5906;
Joe Perches63c3a662011-04-26 08:12:10 +000011637 else if (tg3_flag(tp, 5705_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080011638 mem_tbl = mem_tbl_5705;
11639 else
Michael Chan7942e1d2005-05-29 14:58:36 -070011640 mem_tbl = mem_tbl_570x;
11641
11642 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000011643 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
11644 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070011645 break;
11646 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011647
Michael Chan7942e1d2005-05-29 14:58:36 -070011648 return err;
11649}
11650
Matt Carlsonbb158d62011-04-25 12:42:47 +000011651#define TG3_TSO_MSS 500
11652
11653#define TG3_TSO_IP_HDR_LEN 20
11654#define TG3_TSO_TCP_HDR_LEN 20
11655#define TG3_TSO_TCP_OPT_LEN 12
11656
11657static const u8 tg3_tso_header[] = {
116580x08, 0x00,
116590x45, 0x00, 0x00, 0x00,
116600x00, 0x00, 0x40, 0x00,
116610x40, 0x06, 0x00, 0x00,
116620x0a, 0x00, 0x00, 0x01,
116630x0a, 0x00, 0x00, 0x02,
116640x0d, 0x00, 0xe0, 0x00,
116650x00, 0x00, 0x01, 0x00,
116660x00, 0x00, 0x02, 0x00,
116670x80, 0x10, 0x10, 0x00,
116680x14, 0x09, 0x00, 0x00,
116690x01, 0x01, 0x08, 0x0a,
116700x11, 0x11, 0x11, 0x11,
116710x11, 0x11, 0x11, 0x11,
11672};
Michael Chan9f40dea2005-09-05 17:53:06 -070011673
Matt Carlson28a45952011-08-19 13:58:22 +000011674static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, bool tso_loopback)
Michael Chanc76949a2005-05-29 14:58:59 -070011675{
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011676 u32 rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011677 u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
Matt Carlson84b67b22011-07-27 14:20:52 +000011678 u32 budget;
Eric Dumazet9205fd92011-11-18 06:47:01 +000011679 struct sk_buff *skb;
11680 u8 *tx_data, *rx_data;
Michael Chanc76949a2005-05-29 14:58:59 -070011681 dma_addr_t map;
11682 int num_pkts, tx_len, rx_len, i, err;
11683 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000011684 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000011685 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070011686
Matt Carlsonc8873402010-02-12 14:47:11 +000011687 tnapi = &tp->napi[0];
11688 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011689 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +000011690 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlson1da85aa2010-09-30 10:34:34 +000011691 rnapi = &tp->napi[1];
Joe Perches63c3a662011-04-26 08:12:10 +000011692 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc8873402010-02-12 14:47:11 +000011693 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011694 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011695 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000011696
Michael Chanc76949a2005-05-29 14:58:59 -070011697 err = -EIO;
11698
Matt Carlson4852a862011-04-13 11:05:07 +000011699 tx_len = pktsz;
David S. Millera20e9c62006-07-31 22:38:16 -070011700 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070011701 if (!skb)
11702 return -ENOMEM;
11703
Michael Chanc76949a2005-05-29 14:58:59 -070011704 tx_data = skb_put(skb, tx_len);
11705 memcpy(tx_data, tp->dev->dev_addr, 6);
11706 memset(tx_data + 6, 0x0, 8);
11707
Matt Carlson4852a862011-04-13 11:05:07 +000011708 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
Michael Chanc76949a2005-05-29 14:58:59 -070011709
Matt Carlson28a45952011-08-19 13:58:22 +000011710 if (tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011711 struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
11712
11713 u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
11714 TG3_TSO_TCP_OPT_LEN;
11715
11716 memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
11717 sizeof(tg3_tso_header));
11718 mss = TG3_TSO_MSS;
11719
11720 val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
11721 num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
11722
11723 /* Set the total length field in the IP header */
11724 iph->tot_len = htons((u16)(mss + hdr_len));
11725
11726 base_flags = (TXD_FLAG_CPU_PRE_DMA |
11727 TXD_FLAG_CPU_POST_DMA);
11728
Joe Perches63c3a662011-04-26 08:12:10 +000011729 if (tg3_flag(tp, HW_TSO_1) ||
11730 tg3_flag(tp, HW_TSO_2) ||
11731 tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011732 struct tcphdr *th;
11733 val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
11734 th = (struct tcphdr *)&tx_data[val];
11735 th->check = 0;
11736 } else
11737 base_flags |= TXD_FLAG_TCPUDP_CSUM;
11738
Joe Perches63c3a662011-04-26 08:12:10 +000011739 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011740 mss |= (hdr_len & 0xc) << 12;
11741 if (hdr_len & 0x10)
11742 base_flags |= 0x00000010;
11743 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +000011744 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlsonbb158d62011-04-25 12:42:47 +000011745 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +000011746 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlsonbb158d62011-04-25 12:42:47 +000011747 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
11748 mss |= (TG3_TSO_TCP_OPT_LEN << 9);
11749 } else {
11750 base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
11751 }
11752
11753 data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
11754 } else {
11755 num_pkts = 1;
11756 data_off = ETH_HLEN;
Michael Chanc441b452012-03-04 14:48:13 +000011757
11758 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
11759 tx_len > VLAN_ETH_FRAME_LEN)
11760 base_flags |= TXD_FLAG_JMB_PKT;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011761 }
11762
11763 for (i = data_off; i < tx_len; i++)
Michael Chanc76949a2005-05-29 14:58:59 -070011764 tx_data[i] = (u8) (i & 0xff);
11765
Alexander Duyckf4188d82009-12-02 16:48:38 +000011766 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
11767 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000011768 dev_kfree_skb(skb);
11769 return -EIO;
11770 }
Michael Chanc76949a2005-05-29 14:58:59 -070011771
Matt Carlson0d681b22011-07-27 14:20:49 +000011772 val = tnapi->tx_prod;
11773 tnapi->tx_buffers[val].skb = skb;
11774 dma_unmap_addr_set(&tnapi->tx_buffers[val], mapping, map);
11775
Michael Chanc76949a2005-05-29 14:58:59 -070011776 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011777 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011778
11779 udelay(10);
11780
Matt Carlson898a56f2009-08-28 14:02:40 +000011781 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070011782
Matt Carlson84b67b22011-07-27 14:20:52 +000011783 budget = tg3_tx_avail(tnapi);
11784 if (tg3_tx_frag_set(tnapi, &val, &budget, map, tx_len,
Matt Carlsond1a3b732011-07-27 14:20:51 +000011785 base_flags | TXD_FLAG_END, mss, 0)) {
11786 tnapi->tx_buffers[val].skb = NULL;
11787 dev_kfree_skb(skb);
11788 return -EIO;
11789 }
Michael Chanc76949a2005-05-29 14:58:59 -070011790
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011791 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070011792
Michael Chan6541b802012-03-04 14:48:14 +000011793 /* Sync BD data before updating mailbox */
11794 wmb();
11795
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011796 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
11797 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070011798
11799 udelay(10);
11800
Matt Carlson303fc922009-11-02 14:27:34 +000011801 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
11802 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070011803 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011804 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011805
11806 udelay(10);
11807
Matt Carlson898a56f2009-08-28 14:02:40 +000011808 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
11809 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011810 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070011811 (rx_idx == (rx_start_idx + num_pkts)))
11812 break;
11813 }
11814
Matt Carlsonba1142e2011-11-04 09:15:00 +000011815 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod - 1, -1);
Michael Chanc76949a2005-05-29 14:58:59 -070011816 dev_kfree_skb(skb);
11817
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011818 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070011819 goto out;
11820
11821 if (rx_idx != rx_start_idx + num_pkts)
11822 goto out;
11823
Matt Carlsonbb158d62011-04-25 12:42:47 +000011824 val = data_off;
11825 while (rx_idx != rx_start_idx) {
11826 desc = &rnapi->rx_rcb[rx_start_idx++];
11827 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
11828 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
Michael Chanc76949a2005-05-29 14:58:59 -070011829
Matt Carlsonbb158d62011-04-25 12:42:47 +000011830 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
11831 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
Matt Carlson4852a862011-04-13 11:05:07 +000011832 goto out;
Michael Chanc76949a2005-05-29 14:58:59 -070011833
Matt Carlsonbb158d62011-04-25 12:42:47 +000011834 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
11835 - ETH_FCS_LEN;
11836
Matt Carlson28a45952011-08-19 13:58:22 +000011837 if (!tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011838 if (rx_len != tx_len)
11839 goto out;
11840
11841 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
11842 if (opaque_key != RXD_OPAQUE_RING_STD)
11843 goto out;
11844 } else {
11845 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
11846 goto out;
11847 }
11848 } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
11849 (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
Matt Carlson54e0a672011-05-19 12:12:50 +000011850 >> RXD_TCPCSUM_SHIFT != 0xffff) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011851 goto out;
11852 }
11853
11854 if (opaque_key == RXD_OPAQUE_RING_STD) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000011855 rx_data = tpr->rx_std_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011856 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
11857 mapping);
11858 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000011859 rx_data = tpr->rx_jmb_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011860 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
11861 mapping);
11862 } else
Matt Carlson4852a862011-04-13 11:05:07 +000011863 goto out;
11864
Matt Carlsonbb158d62011-04-25 12:42:47 +000011865 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
11866 PCI_DMA_FROMDEVICE);
11867
Eric Dumazet9205fd92011-11-18 06:47:01 +000011868 rx_data += TG3_RX_OFFSET(tp);
Matt Carlsonbb158d62011-04-25 12:42:47 +000011869 for (i = data_off; i < rx_len; i++, val++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000011870 if (*(rx_data + i) != (u8) (val & 0xff))
Matt Carlsonbb158d62011-04-25 12:42:47 +000011871 goto out;
11872 }
Matt Carlson4852a862011-04-13 11:05:07 +000011873 }
11874
Michael Chanc76949a2005-05-29 14:58:59 -070011875 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011876
Eric Dumazet9205fd92011-11-18 06:47:01 +000011877 /* tg3_free_rings will unmap and free the rx_data */
Michael Chanc76949a2005-05-29 14:58:59 -070011878out:
11879 return err;
11880}
11881
Matt Carlson00c266b2011-04-25 12:42:46 +000011882#define TG3_STD_LOOPBACK_FAILED 1
11883#define TG3_JMB_LOOPBACK_FAILED 2
Matt Carlsonbb158d62011-04-25 12:42:47 +000011884#define TG3_TSO_LOOPBACK_FAILED 4
Matt Carlson28a45952011-08-19 13:58:22 +000011885#define TG3_LOOPBACK_FAILED \
11886 (TG3_STD_LOOPBACK_FAILED | \
11887 TG3_JMB_LOOPBACK_FAILED | \
11888 TG3_TSO_LOOPBACK_FAILED)
Matt Carlson00c266b2011-04-25 12:42:46 +000011889
Matt Carlson941ec902011-08-19 13:58:23 +000011890static int tg3_test_loopback(struct tg3 *tp, u64 *data, bool do_extlpbk)
Michael Chan9f40dea2005-09-05 17:53:06 -070011891{
Matt Carlson28a45952011-08-19 13:58:22 +000011892 int err = -EIO;
Matt Carlson2215e242011-08-19 13:58:19 +000011893 u32 eee_cap;
Michael Chanc441b452012-03-04 14:48:13 +000011894 u32 jmb_pkt_sz = 9000;
11895
11896 if (tp->dma_limit)
11897 jmb_pkt_sz = tp->dma_limit - ETH_HLEN;
Michael Chan9f40dea2005-09-05 17:53:06 -070011898
Matt Carlsonab789042011-01-25 15:58:54 +000011899 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
11900 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
11901
Matt Carlson28a45952011-08-19 13:58:22 +000011902 if (!netif_running(tp->dev)) {
11903 data[0] = TG3_LOOPBACK_FAILED;
11904 data[1] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000011905 if (do_extlpbk)
11906 data[2] = TG3_LOOPBACK_FAILED;
Matt Carlson28a45952011-08-19 13:58:22 +000011907 goto done;
11908 }
11909
Michael Chanb9ec6c12006-07-25 16:37:27 -070011910 err = tg3_reset_hw(tp, 1);
Matt Carlsonab789042011-01-25 15:58:54 +000011911 if (err) {
Matt Carlson28a45952011-08-19 13:58:22 +000011912 data[0] = TG3_LOOPBACK_FAILED;
11913 data[1] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000011914 if (do_extlpbk)
11915 data[2] = TG3_LOOPBACK_FAILED;
Matt Carlsonab789042011-01-25 15:58:54 +000011916 goto done;
11917 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011918
Joe Perches63c3a662011-04-26 08:12:10 +000011919 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson4a85f092011-04-20 07:57:37 +000011920 int i;
11921
11922 /* Reroute all rx packets to the 1st queue */
11923 for (i = MAC_RSS_INDIR_TBL_0;
11924 i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
11925 tw32(i, 0x0);
11926 }
11927
Matt Carlson6e01b202011-08-19 13:58:20 +000011928 /* HW errata - mac loopback fails in some cases on 5780.
11929 * Normal traffic and PHY loopback are not affected by
11930 * errata. Also, the MAC loopback test is deprecated for
11931 * all newer ASIC revisions.
11932 */
11933 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
11934 !tg3_flag(tp, CPMU_PRESENT)) {
11935 tg3_mac_loopback(tp, true);
Matt Carlson9936bcf2007-10-10 18:03:07 -070011936
Matt Carlson28a45952011-08-19 13:58:22 +000011937 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
11938 data[0] |= TG3_STD_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000011939
11940 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000011941 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Matt Carlson28a45952011-08-19 13:58:22 +000011942 data[0] |= TG3_JMB_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000011943
11944 tg3_mac_loopback(tp, false);
11945 }
Matt Carlson4852a862011-04-13 11:05:07 +000011946
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011947 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000011948 !tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011949 int i;
11950
Matt Carlson941ec902011-08-19 13:58:23 +000011951 tg3_phy_lpbk_set(tp, 0, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011952
11953 /* Wait for link */
11954 for (i = 0; i < 100; i++) {
11955 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
11956 break;
11957 mdelay(1);
11958 }
11959
Matt Carlson28a45952011-08-19 13:58:22 +000011960 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
11961 data[1] |= TG3_STD_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000011962 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000011963 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
11964 data[1] |= TG3_TSO_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000011965 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000011966 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Matt Carlson28a45952011-08-19 13:58:22 +000011967 data[1] |= TG3_JMB_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070011968
Matt Carlson941ec902011-08-19 13:58:23 +000011969 if (do_extlpbk) {
11970 tg3_phy_lpbk_set(tp, 0, true);
11971
11972 /* All link indications report up, but the hardware
11973 * isn't really ready for about 20 msec. Double it
11974 * to be sure.
11975 */
11976 mdelay(40);
11977
11978 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
11979 data[2] |= TG3_STD_LOOPBACK_FAILED;
11980 if (tg3_flag(tp, TSO_CAPABLE) &&
11981 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
11982 data[2] |= TG3_TSO_LOOPBACK_FAILED;
11983 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000011984 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Matt Carlson941ec902011-08-19 13:58:23 +000011985 data[2] |= TG3_JMB_LOOPBACK_FAILED;
11986 }
11987
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011988 /* Re-enable gphy autopowerdown. */
11989 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
11990 tg3_phy_toggle_apd(tp, true);
11991 }
Matt Carlson6833c042008-11-21 17:18:59 -080011992
Matt Carlson941ec902011-08-19 13:58:23 +000011993 err = (data[0] | data[1] | data[2]) ? -EIO : 0;
Matt Carlson28a45952011-08-19 13:58:22 +000011994
Matt Carlsonab789042011-01-25 15:58:54 +000011995done:
11996 tp->phy_flags |= eee_cap;
11997
Michael Chan9f40dea2005-09-05 17:53:06 -070011998 return err;
11999}
12000
Michael Chan4cafd3f2005-05-29 14:56:34 -070012001static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
12002 u64 *data)
12003{
Michael Chan566f86a2005-05-29 14:56:58 -070012004 struct tg3 *tp = netdev_priv(dev);
Matt Carlson941ec902011-08-19 13:58:23 +000012005 bool doextlpbk = etest->flags & ETH_TEST_FL_EXTERNAL_LB;
Michael Chan566f86a2005-05-29 14:56:58 -070012006
Matt Carlsonbed98292011-07-13 09:27:29 +000012007 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
12008 tg3_power_up(tp)) {
12009 etest->flags |= ETH_TEST_FL_FAILED;
12010 memset(data, 1, sizeof(u64) * TG3_NUM_TEST);
12011 return;
12012 }
Michael Chanbc1c7562006-03-20 17:48:03 -080012013
Michael Chan566f86a2005-05-29 14:56:58 -070012014 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
12015
12016 if (tg3_test_nvram(tp) != 0) {
12017 etest->flags |= ETH_TEST_FL_FAILED;
12018 data[0] = 1;
12019 }
Matt Carlson941ec902011-08-19 13:58:23 +000012020 if (!doextlpbk && tg3_test_link(tp)) {
Michael Chanca430072005-05-29 14:57:23 -070012021 etest->flags |= ETH_TEST_FL_FAILED;
12022 data[1] = 1;
12023 }
Michael Chana71116d2005-05-29 14:58:11 -070012024 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012025 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070012026
Michael Chanbbe832c2005-06-24 20:20:04 -070012027 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012028 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070012029 tg3_netif_stop(tp);
12030 irq_sync = 1;
12031 }
12032
12033 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070012034
12035 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080012036 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012037 tg3_halt_cpu(tp, RX_CPU_BASE);
Joe Perches63c3a662011-04-26 08:12:10 +000012038 if (!tg3_flag(tp, 5705_PLUS))
Michael Chana71116d2005-05-29 14:58:11 -070012039 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080012040 if (!err)
12041 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012042
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012043 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad12006-03-20 22:27:35 -080012044 tg3_phy_reset(tp);
12045
Michael Chana71116d2005-05-29 14:58:11 -070012046 if (tg3_test_registers(tp) != 0) {
12047 etest->flags |= ETH_TEST_FL_FAILED;
12048 data[2] = 1;
12049 }
Matt Carlson28a45952011-08-19 13:58:22 +000012050
Michael Chan7942e1d2005-05-29 14:58:36 -070012051 if (tg3_test_memory(tp) != 0) {
12052 etest->flags |= ETH_TEST_FL_FAILED;
12053 data[3] = 1;
12054 }
Matt Carlson28a45952011-08-19 13:58:22 +000012055
Matt Carlson941ec902011-08-19 13:58:23 +000012056 if (doextlpbk)
12057 etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
12058
12059 if (tg3_test_loopback(tp, &data[4], doextlpbk))
Michael Chanc76949a2005-05-29 14:58:59 -070012060 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070012061
David S. Millerf47c11e2005-06-24 20:18:35 -070012062 tg3_full_unlock(tp);
12063
Michael Chand4bc3922005-05-29 14:59:20 -070012064 if (tg3_test_interrupt(tp) != 0) {
12065 etest->flags |= ETH_TEST_FL_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000012066 data[7] = 1;
Michael Chand4bc3922005-05-29 14:59:20 -070012067 }
David S. Millerf47c11e2005-06-24 20:18:35 -070012068
12069 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070012070
Michael Chana71116d2005-05-29 14:58:11 -070012071 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
12072 if (netif_running(dev)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012073 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012074 err2 = tg3_restart_hw(tp, 1);
12075 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070012076 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012077 }
David S. Millerf47c11e2005-06-24 20:18:35 -070012078
12079 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012080
12081 if (irq_sync && !err2)
12082 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012083 }
Matt Carlson80096062010-08-02 11:26:06 +000012084 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000012085 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080012086
Michael Chan4cafd3f2005-05-29 14:56:34 -070012087}
12088
Linus Torvalds1da177e2005-04-16 15:20:36 -070012089static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
12090{
12091 struct mii_ioctl_data *data = if_mii(ifr);
12092 struct tg3 *tp = netdev_priv(dev);
12093 int err;
12094
Joe Perches63c3a662011-04-26 08:12:10 +000012095 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000012096 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012097 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012098 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000012099 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000012100 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012101 }
12102
Matt Carlson33f401a2010-04-05 10:19:27 +000012103 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012104 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000012105 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012106
12107 /* fallthru */
12108 case SIOCGMIIREG: {
12109 u32 mii_regval;
12110
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012111 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012112 break; /* We have no PHY */
12113
Matt Carlson34eea5a2011-04-20 07:57:38 +000012114 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080012115 return -EAGAIN;
12116
David S. Millerf47c11e2005-06-24 20:18:35 -070012117 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012118 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070012119 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012120
12121 data->val_out = mii_regval;
12122
12123 return err;
12124 }
12125
12126 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012127 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012128 break; /* We have no PHY */
12129
Matt Carlson34eea5a2011-04-20 07:57:38 +000012130 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080012131 return -EAGAIN;
12132
David S. Millerf47c11e2005-06-24 20:18:35 -070012133 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012134 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070012135 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012136
12137 return err;
12138
12139 default:
12140 /* do nothing */
12141 break;
12142 }
12143 return -EOPNOTSUPP;
12144}
12145
David S. Miller15f98502005-05-18 22:49:26 -070012146static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
12147{
12148 struct tg3 *tp = netdev_priv(dev);
12149
12150 memcpy(ec, &tp->coal, sizeof(*ec));
12151 return 0;
12152}
12153
Michael Chand244c892005-07-05 14:42:33 -070012154static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
12155{
12156 struct tg3 *tp = netdev_priv(dev);
12157 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
12158 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
12159
Joe Perches63c3a662011-04-26 08:12:10 +000012160 if (!tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070012161 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
12162 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
12163 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
12164 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
12165 }
12166
12167 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
12168 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
12169 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
12170 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
12171 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
12172 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
12173 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
12174 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
12175 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
12176 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
12177 return -EINVAL;
12178
12179 /* No rx interrupts will be generated if both are zero */
12180 if ((ec->rx_coalesce_usecs == 0) &&
12181 (ec->rx_max_coalesced_frames == 0))
12182 return -EINVAL;
12183
12184 /* No tx interrupts will be generated if both are zero */
12185 if ((ec->tx_coalesce_usecs == 0) &&
12186 (ec->tx_max_coalesced_frames == 0))
12187 return -EINVAL;
12188
12189 /* Only copy relevant parameters, ignore all others. */
12190 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
12191 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
12192 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
12193 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
12194 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
12195 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
12196 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
12197 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
12198 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
12199
12200 if (netif_running(dev)) {
12201 tg3_full_lock(tp, 0);
12202 __tg3_set_coalesce(tp, &tp->coal);
12203 tg3_full_unlock(tp);
12204 }
12205 return 0;
12206}
12207
Jeff Garzik7282d492006-09-13 14:30:00 -040012208static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012209 .get_settings = tg3_get_settings,
12210 .set_settings = tg3_set_settings,
12211 .get_drvinfo = tg3_get_drvinfo,
12212 .get_regs_len = tg3_get_regs_len,
12213 .get_regs = tg3_get_regs,
12214 .get_wol = tg3_get_wol,
12215 .set_wol = tg3_set_wol,
12216 .get_msglevel = tg3_get_msglevel,
12217 .set_msglevel = tg3_set_msglevel,
12218 .nway_reset = tg3_nway_reset,
12219 .get_link = ethtool_op_get_link,
12220 .get_eeprom_len = tg3_get_eeprom_len,
12221 .get_eeprom = tg3_get_eeprom,
12222 .set_eeprom = tg3_set_eeprom,
12223 .get_ringparam = tg3_get_ringparam,
12224 .set_ringparam = tg3_set_ringparam,
12225 .get_pauseparam = tg3_get_pauseparam,
12226 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070012227 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012228 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000012229 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012230 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070012231 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070012232 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070012233 .get_sset_count = tg3_get_sset_count,
Matt Carlson90415472011-12-16 13:33:23 +000012234 .get_rxnfc = tg3_get_rxnfc,
12235 .get_rxfh_indir_size = tg3_get_rxfh_indir_size,
12236 .get_rxfh_indir = tg3_get_rxfh_indir,
12237 .set_rxfh_indir = tg3_set_rxfh_indir,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012238};
12239
David S. Millerb4017c52012-03-01 17:57:40 -050012240static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
12241 struct rtnl_link_stats64 *stats)
12242{
12243 struct tg3 *tp = netdev_priv(dev);
12244
12245 if (!tp->hw_stats)
12246 return &tp->net_stats_prev;
12247
12248 spin_lock_bh(&tp->lock);
12249 tg3_get_nstats(tp, stats);
12250 spin_unlock_bh(&tp->lock);
12251
12252 return stats;
12253}
12254
Matt Carlsonccd5ba92012-02-13 10:20:08 +000012255static void tg3_set_rx_mode(struct net_device *dev)
12256{
12257 struct tg3 *tp = netdev_priv(dev);
12258
12259 if (!netif_running(dev))
12260 return;
12261
12262 tg3_full_lock(tp, 0);
12263 __tg3_set_rx_mode(dev);
12264 tg3_full_unlock(tp);
12265}
12266
Matt Carlsonfaf16272012-02-13 10:20:07 +000012267static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
12268 int new_mtu)
12269{
12270 dev->mtu = new_mtu;
12271
12272 if (new_mtu > ETH_DATA_LEN) {
12273 if (tg3_flag(tp, 5780_CLASS)) {
12274 netdev_update_features(dev);
12275 tg3_flag_clear(tp, TSO_CAPABLE);
12276 } else {
12277 tg3_flag_set(tp, JUMBO_RING_ENABLE);
12278 }
12279 } else {
12280 if (tg3_flag(tp, 5780_CLASS)) {
12281 tg3_flag_set(tp, TSO_CAPABLE);
12282 netdev_update_features(dev);
12283 }
12284 tg3_flag_clear(tp, JUMBO_RING_ENABLE);
12285 }
12286}
12287
12288static int tg3_change_mtu(struct net_device *dev, int new_mtu)
12289{
12290 struct tg3 *tp = netdev_priv(dev);
Michael Chan2fae5e32012-03-04 14:48:15 +000012291 int err, reset_phy = 0;
Matt Carlsonfaf16272012-02-13 10:20:07 +000012292
12293 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
12294 return -EINVAL;
12295
12296 if (!netif_running(dev)) {
12297 /* We'll just catch it later when the
12298 * device is up'd.
12299 */
12300 tg3_set_mtu(dev, tp, new_mtu);
12301 return 0;
12302 }
12303
12304 tg3_phy_stop(tp);
12305
12306 tg3_netif_stop(tp);
12307
12308 tg3_full_lock(tp, 1);
12309
12310 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
12311
12312 tg3_set_mtu(dev, tp, new_mtu);
12313
Michael Chan2fae5e32012-03-04 14:48:15 +000012314 /* Reset PHY, otherwise the read DMA engine will be in a mode that
12315 * breaks all requests to 256 bytes.
12316 */
12317 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57766)
12318 reset_phy = 1;
12319
12320 err = tg3_restart_hw(tp, reset_phy);
Matt Carlsonfaf16272012-02-13 10:20:07 +000012321
12322 if (!err)
12323 tg3_netif_start(tp);
12324
12325 tg3_full_unlock(tp);
12326
12327 if (!err)
12328 tg3_phy_start(tp);
12329
12330 return err;
12331}
12332
12333static const struct net_device_ops tg3_netdev_ops = {
12334 .ndo_open = tg3_open,
12335 .ndo_stop = tg3_close,
12336 .ndo_start_xmit = tg3_start_xmit,
12337 .ndo_get_stats64 = tg3_get_stats64,
12338 .ndo_validate_addr = eth_validate_addr,
12339 .ndo_set_rx_mode = tg3_set_rx_mode,
12340 .ndo_set_mac_address = tg3_set_mac_addr,
12341 .ndo_do_ioctl = tg3_ioctl,
12342 .ndo_tx_timeout = tg3_tx_timeout,
12343 .ndo_change_mtu = tg3_change_mtu,
12344 .ndo_fix_features = tg3_fix_features,
12345 .ndo_set_features = tg3_set_features,
12346#ifdef CONFIG_NET_POLL_CONTROLLER
12347 .ndo_poll_controller = tg3_poll_controller,
12348#endif
12349};
12350
Linus Torvalds1da177e2005-04-16 15:20:36 -070012351static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
12352{
Michael Chan1b277772006-03-20 22:27:48 -080012353 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012354
12355 tp->nvram_size = EEPROM_CHIP_SIZE;
12356
Matt Carlsone4f34112009-02-25 14:25:00 +000012357 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012358 return;
12359
Michael Chanb16250e2006-09-27 16:10:14 -070012360 if ((magic != TG3_EEPROM_MAGIC) &&
12361 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
12362 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012363 return;
12364
12365 /*
12366 * Size the chip by reading offsets at increasing powers of two.
12367 * When we encounter our validation signature, we know the addressing
12368 * has wrapped around, and thus have our chip size.
12369 */
Michael Chan1b277772006-03-20 22:27:48 -080012370 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012371
12372 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000012373 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012374 return;
12375
Michael Chan18201802006-03-20 22:29:15 -080012376 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012377 break;
12378
12379 cursize <<= 1;
12380 }
12381
12382 tp->nvram_size = cursize;
12383}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012384
Linus Torvalds1da177e2005-04-16 15:20:36 -070012385static void __devinit tg3_get_nvram_size(struct tg3 *tp)
12386{
12387 u32 val;
12388
Joe Perches63c3a662011-04-26 08:12:10 +000012389 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080012390 return;
12391
12392 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080012393 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080012394 tg3_get_eeprom_size(tp);
12395 return;
12396 }
12397
Matt Carlson6d348f22009-02-25 14:25:52 +000012398 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012399 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000012400 /* This is confusing. We want to operate on the
12401 * 16-bit value at offset 0xf2. The tg3_nvram_read()
12402 * call will read from NVRAM and byteswap the data
12403 * according to the byteswapping settings for all
12404 * other register accesses. This ensures the data we
12405 * want will always reside in the lower 16-bits.
12406 * However, the data in NVRAM is in LE format, which
12407 * means the data from the NVRAM read will always be
12408 * opposite the endianness of the CPU. The 16-bit
12409 * byteswap then brings the data to CPU endianness.
12410 */
12411 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012412 return;
12413 }
12414 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070012415 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012416}
12417
12418static void __devinit tg3_get_nvram_info(struct tg3 *tp)
12419{
12420 u32 nvcfg1;
12421
12422 nvcfg1 = tr32(NVRAM_CFG1);
12423 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
Joe Perches63c3a662011-04-26 08:12:10 +000012424 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012425 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012426 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12427 tw32(NVRAM_CFG1, nvcfg1);
12428 }
12429
Matt Carlson6ff6f812011-05-19 12:12:54 +000012430 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
Joe Perches63c3a662011-04-26 08:12:10 +000012431 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012432 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012433 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
12434 tp->nvram_jedecnum = JEDEC_ATMEL;
12435 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012436 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012437 break;
12438 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
12439 tp->nvram_jedecnum = JEDEC_ATMEL;
12440 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
12441 break;
12442 case FLASH_VENDOR_ATMEL_EEPROM:
12443 tp->nvram_jedecnum = JEDEC_ATMEL;
12444 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012445 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012446 break;
12447 case FLASH_VENDOR_ST:
12448 tp->nvram_jedecnum = JEDEC_ST;
12449 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012450 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012451 break;
12452 case FLASH_VENDOR_SAIFUN:
12453 tp->nvram_jedecnum = JEDEC_SAIFUN;
12454 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
12455 break;
12456 case FLASH_VENDOR_SST_SMALL:
12457 case FLASH_VENDOR_SST_LARGE:
12458 tp->nvram_jedecnum = JEDEC_SST;
12459 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
12460 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012461 }
Matt Carlson8590a602009-08-28 12:29:16 +000012462 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012463 tp->nvram_jedecnum = JEDEC_ATMEL;
12464 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012465 tg3_flag_set(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012466 }
12467}
12468
Matt Carlsona1b950d2009-09-01 13:20:17 +000012469static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
12470{
12471 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
12472 case FLASH_5752PAGE_SIZE_256:
12473 tp->nvram_pagesize = 256;
12474 break;
12475 case FLASH_5752PAGE_SIZE_512:
12476 tp->nvram_pagesize = 512;
12477 break;
12478 case FLASH_5752PAGE_SIZE_1K:
12479 tp->nvram_pagesize = 1024;
12480 break;
12481 case FLASH_5752PAGE_SIZE_2K:
12482 tp->nvram_pagesize = 2048;
12483 break;
12484 case FLASH_5752PAGE_SIZE_4K:
12485 tp->nvram_pagesize = 4096;
12486 break;
12487 case FLASH_5752PAGE_SIZE_264:
12488 tp->nvram_pagesize = 264;
12489 break;
12490 case FLASH_5752PAGE_SIZE_528:
12491 tp->nvram_pagesize = 528;
12492 break;
12493 }
12494}
12495
Michael Chan361b4ac2005-04-21 17:11:21 -070012496static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
12497{
12498 u32 nvcfg1;
12499
12500 nvcfg1 = tr32(NVRAM_CFG1);
12501
Michael Chane6af3012005-04-21 17:12:05 -070012502 /* NVRAM protection for TPM */
12503 if (nvcfg1 & (1 << 27))
Joe Perches63c3a662011-04-26 08:12:10 +000012504 tg3_flag_set(tp, PROTECTED_NVRAM);
Michael Chane6af3012005-04-21 17:12:05 -070012505
Michael Chan361b4ac2005-04-21 17:11:21 -070012506 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012507 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
12508 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
12509 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012510 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012511 break;
12512 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12513 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012514 tg3_flag_set(tp, NVRAM_BUFFERED);
12515 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012516 break;
12517 case FLASH_5752VENDOR_ST_M45PE10:
12518 case FLASH_5752VENDOR_ST_M45PE20:
12519 case FLASH_5752VENDOR_ST_M45PE40:
12520 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012521 tg3_flag_set(tp, NVRAM_BUFFERED);
12522 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012523 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070012524 }
12525
Joe Perches63c3a662011-04-26 08:12:10 +000012526 if (tg3_flag(tp, FLASH)) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000012527 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000012528 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070012529 /* For eeprom, set pagesize to maximum eeprom size */
12530 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12531
12532 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12533 tw32(NVRAM_CFG1, nvcfg1);
12534 }
12535}
12536
Michael Chand3c7b882006-03-23 01:28:25 -080012537static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
12538{
Matt Carlson989a9d22007-05-05 11:51:05 -070012539 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080012540
12541 nvcfg1 = tr32(NVRAM_CFG1);
12542
12543 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070012544 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012545 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson989a9d22007-05-05 11:51:05 -070012546 protect = 1;
12547 }
Michael Chand3c7b882006-03-23 01:28:25 -080012548
Matt Carlson989a9d22007-05-05 11:51:05 -070012549 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
12550 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012551 case FLASH_5755VENDOR_ATMEL_FLASH_1:
12552 case FLASH_5755VENDOR_ATMEL_FLASH_2:
12553 case FLASH_5755VENDOR_ATMEL_FLASH_3:
12554 case FLASH_5755VENDOR_ATMEL_FLASH_5:
12555 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012556 tg3_flag_set(tp, NVRAM_BUFFERED);
12557 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012558 tp->nvram_pagesize = 264;
12559 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
12560 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
12561 tp->nvram_size = (protect ? 0x3e200 :
12562 TG3_NVRAM_SIZE_512KB);
12563 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
12564 tp->nvram_size = (protect ? 0x1f200 :
12565 TG3_NVRAM_SIZE_256KB);
12566 else
12567 tp->nvram_size = (protect ? 0x1f200 :
12568 TG3_NVRAM_SIZE_128KB);
12569 break;
12570 case FLASH_5752VENDOR_ST_M45PE10:
12571 case FLASH_5752VENDOR_ST_M45PE20:
12572 case FLASH_5752VENDOR_ST_M45PE40:
12573 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012574 tg3_flag_set(tp, NVRAM_BUFFERED);
12575 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012576 tp->nvram_pagesize = 256;
12577 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
12578 tp->nvram_size = (protect ?
12579 TG3_NVRAM_SIZE_64KB :
12580 TG3_NVRAM_SIZE_128KB);
12581 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
12582 tp->nvram_size = (protect ?
12583 TG3_NVRAM_SIZE_64KB :
12584 TG3_NVRAM_SIZE_256KB);
12585 else
12586 tp->nvram_size = (protect ?
12587 TG3_NVRAM_SIZE_128KB :
12588 TG3_NVRAM_SIZE_512KB);
12589 break;
Michael Chand3c7b882006-03-23 01:28:25 -080012590 }
12591}
12592
Michael Chan1b277772006-03-20 22:27:48 -080012593static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
12594{
12595 u32 nvcfg1;
12596
12597 nvcfg1 = tr32(NVRAM_CFG1);
12598
12599 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012600 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
12601 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
12602 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
12603 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
12604 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012605 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012606 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080012607
Matt Carlson8590a602009-08-28 12:29:16 +000012608 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12609 tw32(NVRAM_CFG1, nvcfg1);
12610 break;
12611 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12612 case FLASH_5755VENDOR_ATMEL_FLASH_1:
12613 case FLASH_5755VENDOR_ATMEL_FLASH_2:
12614 case FLASH_5755VENDOR_ATMEL_FLASH_3:
12615 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012616 tg3_flag_set(tp, NVRAM_BUFFERED);
12617 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012618 tp->nvram_pagesize = 264;
12619 break;
12620 case FLASH_5752VENDOR_ST_M45PE10:
12621 case FLASH_5752VENDOR_ST_M45PE20:
12622 case FLASH_5752VENDOR_ST_M45PE40:
12623 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012624 tg3_flag_set(tp, NVRAM_BUFFERED);
12625 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012626 tp->nvram_pagesize = 256;
12627 break;
Michael Chan1b277772006-03-20 22:27:48 -080012628 }
12629}
12630
Matt Carlson6b91fa02007-10-10 18:01:09 -070012631static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
12632{
12633 u32 nvcfg1, protect = 0;
12634
12635 nvcfg1 = tr32(NVRAM_CFG1);
12636
12637 /* NVRAM protection for TPM */
12638 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012639 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson6b91fa02007-10-10 18:01:09 -070012640 protect = 1;
12641 }
12642
12643 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
12644 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012645 case FLASH_5761VENDOR_ATMEL_ADB021D:
12646 case FLASH_5761VENDOR_ATMEL_ADB041D:
12647 case FLASH_5761VENDOR_ATMEL_ADB081D:
12648 case FLASH_5761VENDOR_ATMEL_ADB161D:
12649 case FLASH_5761VENDOR_ATMEL_MDB021D:
12650 case FLASH_5761VENDOR_ATMEL_MDB041D:
12651 case FLASH_5761VENDOR_ATMEL_MDB081D:
12652 case FLASH_5761VENDOR_ATMEL_MDB161D:
12653 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012654 tg3_flag_set(tp, NVRAM_BUFFERED);
12655 tg3_flag_set(tp, FLASH);
12656 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson8590a602009-08-28 12:29:16 +000012657 tp->nvram_pagesize = 256;
12658 break;
12659 case FLASH_5761VENDOR_ST_A_M45PE20:
12660 case FLASH_5761VENDOR_ST_A_M45PE40:
12661 case FLASH_5761VENDOR_ST_A_M45PE80:
12662 case FLASH_5761VENDOR_ST_A_M45PE16:
12663 case FLASH_5761VENDOR_ST_M_M45PE20:
12664 case FLASH_5761VENDOR_ST_M_M45PE40:
12665 case FLASH_5761VENDOR_ST_M_M45PE80:
12666 case FLASH_5761VENDOR_ST_M_M45PE16:
12667 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012668 tg3_flag_set(tp, NVRAM_BUFFERED);
12669 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012670 tp->nvram_pagesize = 256;
12671 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070012672 }
12673
12674 if (protect) {
12675 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
12676 } else {
12677 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012678 case FLASH_5761VENDOR_ATMEL_ADB161D:
12679 case FLASH_5761VENDOR_ATMEL_MDB161D:
12680 case FLASH_5761VENDOR_ST_A_M45PE16:
12681 case FLASH_5761VENDOR_ST_M_M45PE16:
12682 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
12683 break;
12684 case FLASH_5761VENDOR_ATMEL_ADB081D:
12685 case FLASH_5761VENDOR_ATMEL_MDB081D:
12686 case FLASH_5761VENDOR_ST_A_M45PE80:
12687 case FLASH_5761VENDOR_ST_M_M45PE80:
12688 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12689 break;
12690 case FLASH_5761VENDOR_ATMEL_ADB041D:
12691 case FLASH_5761VENDOR_ATMEL_MDB041D:
12692 case FLASH_5761VENDOR_ST_A_M45PE40:
12693 case FLASH_5761VENDOR_ST_M_M45PE40:
12694 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12695 break;
12696 case FLASH_5761VENDOR_ATMEL_ADB021D:
12697 case FLASH_5761VENDOR_ATMEL_MDB021D:
12698 case FLASH_5761VENDOR_ST_A_M45PE20:
12699 case FLASH_5761VENDOR_ST_M_M45PE20:
12700 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12701 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070012702 }
12703 }
12704}
12705
Michael Chanb5d37722006-09-27 16:06:21 -070012706static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
12707{
12708 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012709 tg3_flag_set(tp, NVRAM_BUFFERED);
Michael Chanb5d37722006-09-27 16:06:21 -070012710 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12711}
12712
Matt Carlson321d32a2008-11-21 17:22:19 -080012713static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
12714{
12715 u32 nvcfg1;
12716
12717 nvcfg1 = tr32(NVRAM_CFG1);
12718
12719 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12720 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
12721 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
12722 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012723 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson321d32a2008-11-21 17:22:19 -080012724 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12725
12726 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12727 tw32(NVRAM_CFG1, nvcfg1);
12728 return;
12729 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12730 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
12731 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
12732 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
12733 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
12734 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
12735 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
12736 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012737 tg3_flag_set(tp, NVRAM_BUFFERED);
12738 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080012739
12740 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12741 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12742 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
12743 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
12744 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12745 break;
12746 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
12747 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
12748 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12749 break;
12750 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
12751 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
12752 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12753 break;
12754 }
12755 break;
12756 case FLASH_5752VENDOR_ST_M45PE10:
12757 case FLASH_5752VENDOR_ST_M45PE20:
12758 case FLASH_5752VENDOR_ST_M45PE40:
12759 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012760 tg3_flag_set(tp, NVRAM_BUFFERED);
12761 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080012762
12763 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12764 case FLASH_5752VENDOR_ST_M45PE10:
12765 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12766 break;
12767 case FLASH_5752VENDOR_ST_M45PE20:
12768 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12769 break;
12770 case FLASH_5752VENDOR_ST_M45PE40:
12771 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12772 break;
12773 }
12774 break;
12775 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012776 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson321d32a2008-11-21 17:22:19 -080012777 return;
12778 }
12779
Matt Carlsona1b950d2009-09-01 13:20:17 +000012780 tg3_nvram_get_pagesize(tp, nvcfg1);
12781 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012782 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012783}
12784
12785
12786static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
12787{
12788 u32 nvcfg1;
12789
12790 nvcfg1 = tr32(NVRAM_CFG1);
12791
12792 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12793 case FLASH_5717VENDOR_ATMEL_EEPROM:
12794 case FLASH_5717VENDOR_MICRO_EEPROM:
12795 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012796 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012797 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12798
12799 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12800 tw32(NVRAM_CFG1, nvcfg1);
12801 return;
12802 case FLASH_5717VENDOR_ATMEL_MDB011D:
12803 case FLASH_5717VENDOR_ATMEL_ADB011B:
12804 case FLASH_5717VENDOR_ATMEL_ADB011D:
12805 case FLASH_5717VENDOR_ATMEL_MDB021D:
12806 case FLASH_5717VENDOR_ATMEL_ADB021B:
12807 case FLASH_5717VENDOR_ATMEL_ADB021D:
12808 case FLASH_5717VENDOR_ATMEL_45USPT:
12809 tp->nvram_jedecnum = JEDEC_ATMEL;
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_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012815 /* Detect size with tg3_nvram_get_size() */
12816 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012817 case FLASH_5717VENDOR_ATMEL_ADB021B:
12818 case FLASH_5717VENDOR_ATMEL_ADB021D:
12819 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12820 break;
12821 default:
12822 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12823 break;
12824 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012825 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012826 case FLASH_5717VENDOR_ST_M_M25PE10:
12827 case FLASH_5717VENDOR_ST_A_M25PE10:
12828 case FLASH_5717VENDOR_ST_M_M45PE10:
12829 case FLASH_5717VENDOR_ST_A_M45PE10:
12830 case FLASH_5717VENDOR_ST_M_M25PE20:
12831 case FLASH_5717VENDOR_ST_A_M25PE20:
12832 case FLASH_5717VENDOR_ST_M_M45PE20:
12833 case FLASH_5717VENDOR_ST_A_M45PE20:
12834 case FLASH_5717VENDOR_ST_25USPT:
12835 case FLASH_5717VENDOR_ST_45USPT:
12836 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012837 tg3_flag_set(tp, NVRAM_BUFFERED);
12838 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012839
12840 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12841 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012842 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012843 /* Detect size with tg3_nvram_get_size() */
12844 break;
12845 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012846 case FLASH_5717VENDOR_ST_A_M45PE20:
12847 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12848 break;
12849 default:
12850 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12851 break;
12852 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012853 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012854 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012855 tg3_flag_set(tp, NO_NVRAM);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012856 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080012857 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000012858
12859 tg3_nvram_get_pagesize(tp, nvcfg1);
12860 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012861 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson321d32a2008-11-21 17:22:19 -080012862}
12863
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012864static void __devinit tg3_get_5720_nvram_info(struct tg3 *tp)
12865{
12866 u32 nvcfg1, nvmpinstrp;
12867
12868 nvcfg1 = tr32(NVRAM_CFG1);
12869 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
12870
12871 switch (nvmpinstrp) {
12872 case FLASH_5720_EEPROM_HD:
12873 case FLASH_5720_EEPROM_LD:
12874 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012875 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012876
12877 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12878 tw32(NVRAM_CFG1, nvcfg1);
12879 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
12880 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12881 else
12882 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
12883 return;
12884 case FLASH_5720VENDOR_M_ATMEL_DB011D:
12885 case FLASH_5720VENDOR_A_ATMEL_DB011B:
12886 case FLASH_5720VENDOR_A_ATMEL_DB011D:
12887 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12888 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12889 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12890 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12891 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12892 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12893 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12894 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12895 case FLASH_5720VENDOR_ATMEL_45USPT:
12896 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012897 tg3_flag_set(tp, NVRAM_BUFFERED);
12898 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012899
12900 switch (nvmpinstrp) {
12901 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12902 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12903 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12904 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12905 break;
12906 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12907 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12908 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12909 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12910 break;
12911 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12912 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12913 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12914 break;
12915 default:
12916 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12917 break;
12918 }
12919 break;
12920 case FLASH_5720VENDOR_M_ST_M25PE10:
12921 case FLASH_5720VENDOR_M_ST_M45PE10:
12922 case FLASH_5720VENDOR_A_ST_M25PE10:
12923 case FLASH_5720VENDOR_A_ST_M45PE10:
12924 case FLASH_5720VENDOR_M_ST_M25PE20:
12925 case FLASH_5720VENDOR_M_ST_M45PE20:
12926 case FLASH_5720VENDOR_A_ST_M25PE20:
12927 case FLASH_5720VENDOR_A_ST_M45PE20:
12928 case FLASH_5720VENDOR_M_ST_M25PE40:
12929 case FLASH_5720VENDOR_M_ST_M45PE40:
12930 case FLASH_5720VENDOR_A_ST_M25PE40:
12931 case FLASH_5720VENDOR_A_ST_M45PE40:
12932 case FLASH_5720VENDOR_M_ST_M25PE80:
12933 case FLASH_5720VENDOR_M_ST_M45PE80:
12934 case FLASH_5720VENDOR_A_ST_M25PE80:
12935 case FLASH_5720VENDOR_A_ST_M45PE80:
12936 case FLASH_5720VENDOR_ST_25USPT:
12937 case FLASH_5720VENDOR_ST_45USPT:
12938 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012939 tg3_flag_set(tp, NVRAM_BUFFERED);
12940 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012941
12942 switch (nvmpinstrp) {
12943 case FLASH_5720VENDOR_M_ST_M25PE20:
12944 case FLASH_5720VENDOR_M_ST_M45PE20:
12945 case FLASH_5720VENDOR_A_ST_M25PE20:
12946 case FLASH_5720VENDOR_A_ST_M45PE20:
12947 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12948 break;
12949 case FLASH_5720VENDOR_M_ST_M25PE40:
12950 case FLASH_5720VENDOR_M_ST_M45PE40:
12951 case FLASH_5720VENDOR_A_ST_M25PE40:
12952 case FLASH_5720VENDOR_A_ST_M45PE40:
12953 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12954 break;
12955 case FLASH_5720VENDOR_M_ST_M25PE80:
12956 case FLASH_5720VENDOR_M_ST_M45PE80:
12957 case FLASH_5720VENDOR_A_ST_M25PE80:
12958 case FLASH_5720VENDOR_A_ST_M45PE80:
12959 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12960 break;
12961 default:
12962 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12963 break;
12964 }
12965 break;
12966 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012967 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012968 return;
12969 }
12970
12971 tg3_nvram_get_pagesize(tp, nvcfg1);
12972 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012973 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012974}
12975
Linus Torvalds1da177e2005-04-16 15:20:36 -070012976/* Chips other than 5700/5701 use the NVRAM for fetching info. */
12977static void __devinit tg3_nvram_init(struct tg3 *tp)
12978{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012979 tw32_f(GRC_EEPROM_ADDR,
12980 (EEPROM_ADDR_FSM_RESET |
12981 (EEPROM_DEFAULT_CLOCK_PERIOD <<
12982 EEPROM_ADDR_CLKPERD_SHIFT)));
12983
Michael Chan9d57f012006-12-07 00:23:25 -080012984 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012985
12986 /* Enable seeprom accesses. */
12987 tw32_f(GRC_LOCAL_CTRL,
12988 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
12989 udelay(100);
12990
12991 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
12992 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
Joe Perches63c3a662011-04-26 08:12:10 +000012993 tg3_flag_set(tp, NVRAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012994
Michael Chanec41c7d2006-01-17 02:40:55 -080012995 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000012996 netdev_warn(tp->dev,
12997 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000012998 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080012999 return;
13000 }
Michael Chane6af3012005-04-21 17:12:05 -070013001 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013002
Matt Carlson989a9d22007-05-05 11:51:05 -070013003 tp->nvram_size = 0;
13004
Michael Chan361b4ac2005-04-21 17:11:21 -070013005 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
13006 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080013007 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
13008 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070013009 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013010 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
13011 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080013012 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070013013 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
13014 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070013015 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
13016 tg3_get_5906_nvram_info(tp);
Matt Carlsonb703df62009-12-03 08:36:21 +000013017 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000013018 tg3_flag(tp, 57765_CLASS))
Matt Carlson321d32a2008-11-21 17:22:19 -080013019 tg3_get_57780_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013020 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13021 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000013022 tg3_get_5717_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013023 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
13024 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070013025 else
13026 tg3_get_nvram_info(tp);
13027
Matt Carlson989a9d22007-05-05 11:51:05 -070013028 if (tp->nvram_size == 0)
13029 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013030
Michael Chane6af3012005-04-21 17:12:05 -070013031 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080013032 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013033
13034 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000013035 tg3_flag_clear(tp, NVRAM);
13036 tg3_flag_clear(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013037
13038 tg3_get_eeprom_size(tp);
13039 }
13040}
13041
Linus Torvalds1da177e2005-04-16 15:20:36 -070013042struct subsys_tbl_ent {
13043 u16 subsys_vendor, subsys_devid;
13044 u32 phy_id;
13045};
13046
Matt Carlson24daf2b2010-02-17 15:17:02 +000013047static struct subsys_tbl_ent subsys_id_to_phy_id[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013048 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013049 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013050 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013051 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013052 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013053 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013054 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013055 { TG3PCI_SUBVENDOR_ID_BROADCOM,
13056 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
13057 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013058 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013059 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013060 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013061 { TG3PCI_SUBVENDOR_ID_BROADCOM,
13062 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
13063 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013064 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013065 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013066 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013067 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013068 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013069 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013070 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013071
13072 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013073 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013074 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013075 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013076 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013077 { TG3PCI_SUBVENDOR_ID_3COM,
13078 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
13079 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013080 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013081 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013082 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013083
13084 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013085 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013086 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013087 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013088 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013089 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013090 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013091 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013092 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013093
13094 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013095 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013096 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013097 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013098 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013099 { TG3PCI_SUBVENDOR_ID_COMPAQ,
13100 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
13101 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013102 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013103 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013104 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013105
13106 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013107 { TG3PCI_SUBVENDOR_ID_IBM,
13108 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013109};
13110
Matt Carlson24daf2b2010-02-17 15:17:02 +000013111static struct subsys_tbl_ent * __devinit tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013112{
13113 int i;
13114
13115 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
13116 if ((subsys_id_to_phy_id[i].subsys_vendor ==
13117 tp->pdev->subsystem_vendor) &&
13118 (subsys_id_to_phy_id[i].subsys_devid ==
13119 tp->pdev->subsystem_device))
13120 return &subsys_id_to_phy_id[i];
13121 }
13122 return NULL;
13123}
13124
Michael Chan7d0c41e2005-04-21 17:06:20 -070013125static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013126{
Linus Torvalds1da177e2005-04-16 15:20:36 -070013127 u32 val;
David S. Millerf49639e2006-06-09 11:58:36 -070013128
Matt Carlson79eb6902010-02-17 15:17:03 +000013129 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070013130 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13131
Gary Zambranoa85feb82007-05-05 11:52:19 -070013132 /* Assume an onboard device and WOL capable by default. */
Joe Perches63c3a662011-04-26 08:12:10 +000013133 tg3_flag_set(tp, EEPROM_WRITE_PROT);
13134 tg3_flag_set(tp, WOL_CAP);
David S. Miller72b845e2006-03-14 14:11:48 -080013135
Michael Chanb5d37722006-09-27 16:06:21 -070013136 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080013137 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013138 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
13139 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080013140 }
Matt Carlson0527ba32007-10-10 18:03:30 -070013141 val = tr32(VCPU_CFGSHDW);
13142 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Joe Perches63c3a662011-04-26 08:12:10 +000013143 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson0527ba32007-10-10 18:03:30 -070013144 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013145 (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013146 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013147 device_set_wakeup_enable(&tp->pdev->dev, true);
13148 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080013149 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070013150 }
13151
Linus Torvalds1da177e2005-04-16 15:20:36 -070013152 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
13153 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
13154 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070013155 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070013156 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013157
13158 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
13159 tp->nic_sram_data_cfg = nic_cfg;
13160
13161 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
13162 ver >>= NIC_SRAM_DATA_VER_SHIFT;
Matt Carlson6ff6f812011-05-19 12:12:54 +000013163 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13164 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13165 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070013166 (ver > 0) && (ver < 0x100))
13167 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
13168
Matt Carlsona9daf362008-05-25 23:49:44 -070013169 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
13170 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
13171
Linus Torvalds1da177e2005-04-16 15:20:36 -070013172 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
13173 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
13174 eeprom_phy_serdes = 1;
13175
13176 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
13177 if (nic_phy_id != 0) {
13178 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
13179 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
13180
13181 eeprom_phy_id = (id1 >> 16) << 10;
13182 eeprom_phy_id |= (id2 & 0xfc00) << 16;
13183 eeprom_phy_id |= (id2 & 0x03ff) << 0;
13184 } else
13185 eeprom_phy_id = 0;
13186
Michael Chan7d0c41e2005-04-21 17:06:20 -070013187 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070013188 if (eeprom_phy_serdes) {
Joe Perches63c3a662011-04-26 08:12:10 +000013189 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013190 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000013191 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013192 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070013193 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070013194
Joe Perches63c3a662011-04-26 08:12:10 +000013195 if (tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013196 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
13197 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070013198 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070013199 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
13200
13201 switch (led_cfg) {
13202 default:
13203 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
13204 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13205 break;
13206
13207 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
13208 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
13209 break;
13210
13211 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
13212 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070013213
13214 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
13215 * read on some older 5700/5701 bootcode.
13216 */
13217 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
13218 ASIC_REV_5700 ||
13219 GET_ASIC_REV(tp->pci_chip_rev_id) ==
13220 ASIC_REV_5701)
13221 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13222
Linus Torvalds1da177e2005-04-16 15:20:36 -070013223 break;
13224
13225 case SHASTA_EXT_LED_SHARED:
13226 tp->led_ctrl = LED_CTRL_MODE_SHARED;
13227 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
13228 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
13229 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
13230 LED_CTRL_MODE_PHY_2);
13231 break;
13232
13233 case SHASTA_EXT_LED_MAC:
13234 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
13235 break;
13236
13237 case SHASTA_EXT_LED_COMBO:
13238 tp->led_ctrl = LED_CTRL_MODE_COMBO;
13239 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
13240 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
13241 LED_CTRL_MODE_PHY_2);
13242 break;
13243
Stephen Hemminger855e1112008-04-16 16:37:28 -070013244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013245
13246 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13247 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
13248 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
13249 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
13250
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013251 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
13252 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080013253
Michael Chan9d26e212006-12-07 00:21:14 -080013254 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Joe Perches63c3a662011-04-26 08:12:10 +000013255 tg3_flag_set(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080013256 if ((tp->pdev->subsystem_vendor ==
13257 PCI_VENDOR_ID_ARIMA) &&
13258 (tp->pdev->subsystem_device == 0x205a ||
13259 tp->pdev->subsystem_device == 0x2063))
Joe Perches63c3a662011-04-26 08:12:10 +000013260 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080013261 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000013262 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
13263 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080013264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013265
13266 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +000013267 tg3_flag_set(tp, ENABLE_ASF);
13268 if (tg3_flag(tp, 5750_PLUS))
13269 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013270 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080013271
13272 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013273 tg3_flag(tp, 5750_PLUS))
13274 tg3_flag_set(tp, ENABLE_APE);
Matt Carlsonb2b98d42008-11-03 16:52:32 -080013275
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013276 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070013277 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
Joe Perches63c3a662011-04-26 08:12:10 +000013278 tg3_flag_clear(tp, WOL_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013279
Joe Perches63c3a662011-04-26 08:12:10 +000013280 if (tg3_flag(tp, WOL_CAP) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013281 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013282 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013283 device_set_wakeup_enable(&tp->pdev->dev, true);
13284 }
Matt Carlson0527ba32007-10-10 18:03:30 -070013285
Linus Torvalds1da177e2005-04-16 15:20:36 -070013286 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013287 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013288
13289 /* serdes signal pre-emphasis in register 0x590 set by */
13290 /* bootcode if bit 18 is set */
13291 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013292 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070013293
Joe Perches63c3a662011-04-26 08:12:10 +000013294 if ((tg3_flag(tp, 57765_PLUS) ||
13295 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
13296 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080013297 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013298 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080013299
Joe Perches63c3a662011-04-26 08:12:10 +000013300 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson8c69b1e2010-08-02 11:26:00 +000013301 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +000013302 !tg3_flag(tp, 57765_PLUS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070013303 u32 cfg3;
13304
13305 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
13306 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
Joe Perches63c3a662011-04-26 08:12:10 +000013307 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson8ed5d972007-05-07 00:25:49 -070013308 }
Matt Carlsona9daf362008-05-25 23:49:44 -070013309
Matt Carlson14417062010-02-17 15:16:59 +000013310 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
Joe Perches63c3a662011-04-26 08:12:10 +000013311 tg3_flag_set(tp, RGMII_INBAND_DISABLE);
Matt Carlsona9daf362008-05-25 23:49:44 -070013312 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000013313 tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
Matt Carlsona9daf362008-05-25 23:49:44 -070013314 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000013315 tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013316 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080013317done:
Joe Perches63c3a662011-04-26 08:12:10 +000013318 if (tg3_flag(tp, WOL_CAP))
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000013319 device_set_wakeup_enable(&tp->pdev->dev,
Joe Perches63c3a662011-04-26 08:12:10 +000013320 tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000013321 else
13322 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070013323}
13324
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013325static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
13326{
13327 int i;
13328 u32 val;
13329
13330 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
13331 tw32(OTP_CTRL, cmd);
13332
13333 /* Wait for up to 1 ms for command to execute. */
13334 for (i = 0; i < 100; i++) {
13335 val = tr32(OTP_STATUS);
13336 if (val & OTP_STATUS_CMD_DONE)
13337 break;
13338 udelay(10);
13339 }
13340
13341 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
13342}
13343
13344/* Read the gphy configuration from the OTP region of the chip. The gphy
13345 * configuration is a 32-bit value that straddles the alignment boundary.
13346 * We do two 32-bit reads and then shift and merge the results.
13347 */
13348static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
13349{
13350 u32 bhalf_otp, thalf_otp;
13351
13352 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
13353
13354 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
13355 return 0;
13356
13357 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
13358
13359 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
13360 return 0;
13361
13362 thalf_otp = tr32(OTP_READ_DATA);
13363
13364 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
13365
13366 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
13367 return 0;
13368
13369 bhalf_otp = tr32(OTP_READ_DATA);
13370
13371 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
13372}
13373
Matt Carlsone256f8a2011-03-09 16:58:24 +000013374static void __devinit tg3_phy_init_link_config(struct tg3 *tp)
13375{
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +000013376 u32 adv = ADVERTISED_Autoneg;
Matt Carlsone256f8a2011-03-09 16:58:24 +000013377
13378 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
13379 adv |= ADVERTISED_1000baseT_Half |
13380 ADVERTISED_1000baseT_Full;
13381
13382 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
13383 adv |= ADVERTISED_100baseT_Half |
13384 ADVERTISED_100baseT_Full |
13385 ADVERTISED_10baseT_Half |
13386 ADVERTISED_10baseT_Full |
13387 ADVERTISED_TP;
13388 else
13389 adv |= ADVERTISED_FIBRE;
13390
13391 tp->link_config.advertising = adv;
Matt Carlsone7405222012-02-13 15:20:16 +000013392 tp->link_config.speed = SPEED_UNKNOWN;
13393 tp->link_config.duplex = DUPLEX_UNKNOWN;
Matt Carlsone256f8a2011-03-09 16:58:24 +000013394 tp->link_config.autoneg = AUTONEG_ENABLE;
Matt Carlsone7405222012-02-13 15:20:16 +000013395 tp->link_config.active_speed = SPEED_UNKNOWN;
13396 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Matt Carlson34655ad2012-02-22 12:35:18 +000013397
13398 tp->old_link = -1;
Matt Carlsone256f8a2011-03-09 16:58:24 +000013399}
13400
Michael Chan7d0c41e2005-04-21 17:06:20 -070013401static int __devinit tg3_phy_probe(struct tg3 *tp)
13402{
13403 u32 hw_phy_id_1, hw_phy_id_2;
13404 u32 hw_phy_id, hw_phy_id_masked;
13405 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013406
Matt Carlsone256f8a2011-03-09 16:58:24 +000013407 /* flow control autonegotiation is default behavior */
Joe Perches63c3a662011-04-26 08:12:10 +000013408 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsone256f8a2011-03-09 16:58:24 +000013409 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
13410
Joe Perches63c3a662011-04-26 08:12:10 +000013411 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013412 return tg3_phy_init(tp);
13413
Linus Torvalds1da177e2005-04-16 15:20:36 -070013414 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010013415 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013416 */
13417 err = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000013418 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000013419 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013420 } else {
13421 /* Now read the physical PHY_ID from the chip and verify
13422 * that it is sane. If it doesn't look good, we fall back
13423 * to either the hard-coded table based PHY_ID and failing
13424 * that the value found in the eeprom area.
13425 */
13426 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
13427 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
13428
13429 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
13430 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
13431 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
13432
Matt Carlson79eb6902010-02-17 15:17:03 +000013433 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013434 }
13435
Matt Carlson79eb6902010-02-17 15:17:03 +000013436 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013437 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000013438 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013439 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070013440 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013441 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013442 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000013443 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070013444 /* Do nothing, phy ID already set up in
13445 * tg3_get_eeprom_hw_cfg().
13446 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070013447 } else {
13448 struct subsys_tbl_ent *p;
13449
13450 /* No eeprom signature? Try the hardcoded
13451 * subsys device table.
13452 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013453 p = tg3_lookup_by_subsys(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013454 if (!p)
13455 return -ENODEV;
13456
13457 tp->phy_id = p->phy_id;
13458 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000013459 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013460 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013461 }
13462 }
13463
Matt Carlsona6b68da2010-12-06 08:28:52 +000013464 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Matt Carlson5baa5e92011-07-20 10:20:53 +000013465 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13466 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720 ||
13467 (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +000013468 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0) ||
13469 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
13470 tp->pci_chip_rev_id != CHIPREV_ID_57765_A0)))
Matt Carlson52b02d02010-10-14 10:37:41 +000013471 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
13472
Matt Carlsone256f8a2011-03-09 16:58:24 +000013473 tg3_phy_init_link_config(tp);
13474
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013475 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013476 !tg3_flag(tp, ENABLE_APE) &&
13477 !tg3_flag(tp, ENABLE_ASF)) {
Matt Carlsone2bf73e2011-12-08 14:40:15 +000013478 u32 bmsr, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013479
13480 tg3_readphy(tp, MII_BMSR, &bmsr);
13481 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
13482 (bmsr & BMSR_LSTATUS))
13483 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013484
Linus Torvalds1da177e2005-04-16 15:20:36 -070013485 err = tg3_phy_reset(tp);
13486 if (err)
13487 return err;
13488
Matt Carlson42b64a42011-05-19 12:12:49 +000013489 tg3_phy_set_wirespeed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013490
Matt Carlsone2bf73e2011-12-08 14:40:15 +000013491 if (!tg3_phy_copper_an_config_ok(tp, &dummy)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000013492 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
13493 tp->link_config.flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013494
13495 tg3_writephy(tp, MII_BMCR,
13496 BMCR_ANENABLE | BMCR_ANRESTART);
13497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013498 }
13499
13500skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000013501 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013502 err = tg3_init_5401phy_dsp(tp);
13503 if (err)
13504 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013505
Linus Torvalds1da177e2005-04-16 15:20:36 -070013506 err = tg3_init_5401phy_dsp(tp);
13507 }
13508
Linus Torvalds1da177e2005-04-16 15:20:36 -070013509 return err;
13510}
13511
Matt Carlson184b8902010-04-05 10:19:25 +000013512static void __devinit tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013513{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013514 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013515 unsigned int block_end, rosize, len;
Matt Carlson535a4902011-07-20 10:20:56 +000013516 u32 vpdlen;
Matt Carlson184b8902010-04-05 10:19:25 +000013517 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013518
Matt Carlson535a4902011-07-20 10:20:56 +000013519 vpd_data = (u8 *)tg3_vpd_readblock(tp, &vpdlen);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013520 if (!vpd_data)
13521 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013522
Matt Carlson535a4902011-07-20 10:20:56 +000013523 i = pci_vpd_find_tag(vpd_data, 0, vpdlen, PCI_VPD_LRDT_RO_DATA);
Matt Carlson4181b2c2010-02-26 14:04:45 +000013524 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013525 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013526
13527 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
13528 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
13529 i += PCI_VPD_LRDT_TAG_SIZE;
13530
Matt Carlson535a4902011-07-20 10:20:56 +000013531 if (block_end > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000013532 goto out_not_found;
13533
Matt Carlson184b8902010-04-05 10:19:25 +000013534 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13535 PCI_VPD_RO_KEYWORD_MFR_ID);
13536 if (j > 0) {
13537 len = pci_vpd_info_field_size(&vpd_data[j]);
13538
13539 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13540 if (j + len > block_end || len != 4 ||
13541 memcmp(&vpd_data[j], "1028", 4))
13542 goto partno;
13543
13544 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13545 PCI_VPD_RO_KEYWORD_VENDOR0);
13546 if (j < 0)
13547 goto partno;
13548
13549 len = pci_vpd_info_field_size(&vpd_data[j]);
13550
13551 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13552 if (j + len > block_end)
13553 goto partno;
13554
13555 memcpy(tp->fw_ver, &vpd_data[j], len);
Matt Carlson535a4902011-07-20 10:20:56 +000013556 strncat(tp->fw_ver, " bc ", vpdlen - len - 1);
Matt Carlson184b8902010-04-05 10:19:25 +000013557 }
13558
13559partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000013560 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13561 PCI_VPD_RO_KEYWORD_PARTNO);
13562 if (i < 0)
13563 goto out_not_found;
13564
13565 len = pci_vpd_info_field_size(&vpd_data[i]);
13566
13567 i += PCI_VPD_INFO_FLD_HDR_SIZE;
13568 if (len > TG3_BPN_SIZE ||
Matt Carlson535a4902011-07-20 10:20:56 +000013569 (len + i) > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000013570 goto out_not_found;
13571
13572 memcpy(tp->board_part_number, &vpd_data[i], len);
13573
Linus Torvalds1da177e2005-04-16 15:20:36 -070013574out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013575 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000013576 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013577 return;
13578
13579out_no_vpd:
Matt Carlson37a949c2010-09-30 10:34:33 +000013580 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
13581 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717)
13582 strcpy(tp->board_part_number, "BCM5717");
13583 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
13584 strcpy(tp->board_part_number, "BCM5718");
13585 else
13586 goto nomatch;
13587 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
13588 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
13589 strcpy(tp->board_part_number, "BCM57780");
13590 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
13591 strcpy(tp->board_part_number, "BCM57760");
13592 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
13593 strcpy(tp->board_part_number, "BCM57790");
13594 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
13595 strcpy(tp->board_part_number, "BCM57788");
13596 else
13597 goto nomatch;
13598 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
13599 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
13600 strcpy(tp->board_part_number, "BCM57761");
13601 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
13602 strcpy(tp->board_part_number, "BCM57765");
13603 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
13604 strcpy(tp->board_part_number, "BCM57781");
13605 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
13606 strcpy(tp->board_part_number, "BCM57785");
13607 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
13608 strcpy(tp->board_part_number, "BCM57791");
13609 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13610 strcpy(tp->board_part_number, "BCM57795");
13611 else
13612 goto nomatch;
Matt Carlson55086ad2011-12-14 11:09:59 +000013613 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57766) {
13614 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762)
13615 strcpy(tp->board_part_number, "BCM57762");
13616 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766)
13617 strcpy(tp->board_part_number, "BCM57766");
13618 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782)
13619 strcpy(tp->board_part_number, "BCM57782");
13620 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
13621 strcpy(tp->board_part_number, "BCM57786");
13622 else
13623 goto nomatch;
Matt Carlson37a949c2010-09-30 10:34:33 +000013624 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070013625 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000013626 } else {
13627nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070013628 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000013629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013630}
13631
Matt Carlson9c8a6202007-10-21 16:16:08 -070013632static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
13633{
13634 u32 val;
13635
Matt Carlsone4f34112009-02-25 14:25:00 +000013636 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013637 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013638 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013639 val != 0)
13640 return 0;
13641
13642 return 1;
13643}
13644
Matt Carlsonacd9c112009-02-25 14:26:33 +000013645static void __devinit tg3_read_bc_ver(struct tg3 *tp)
13646{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013647 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000013648 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013649 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013650
13651 if (tg3_nvram_read(tp, 0xc, &offset) ||
13652 tg3_nvram_read(tp, 0x4, &start))
13653 return;
13654
13655 offset = tg3_nvram_logical_addr(tp, offset);
13656
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013657 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013658 return;
13659
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013660 if ((val & 0xfc000000) == 0x0c000000) {
13661 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013662 return;
13663
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013664 if (val == 0)
13665 newver = true;
13666 }
13667
Matt Carlson75f99362010-04-05 10:19:24 +000013668 dst_off = strlen(tp->fw_ver);
13669
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013670 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000013671 if (TG3_VER_SIZE - dst_off < 16 ||
13672 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013673 return;
13674
13675 offset = offset + ver_offset - start;
13676 for (i = 0; i < 16; i += 4) {
13677 __be32 v;
13678 if (tg3_nvram_read_be32(tp, offset + i, &v))
13679 return;
13680
Matt Carlson75f99362010-04-05 10:19:24 +000013681 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013682 }
13683 } else {
13684 u32 major, minor;
13685
13686 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
13687 return;
13688
13689 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
13690 TG3_NVM_BCVER_MAJSFT;
13691 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000013692 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
13693 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013694 }
13695}
13696
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013697static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
13698{
13699 u32 val, major, minor;
13700
13701 /* Use native endian representation */
13702 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
13703 return;
13704
13705 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
13706 TG3_NVM_HWSB_CFG1_MAJSFT;
13707 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
13708 TG3_NVM_HWSB_CFG1_MINSFT;
13709
13710 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
13711}
13712
Matt Carlsondfe00d72008-11-21 17:19:41 -080013713static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
13714{
13715 u32 offset, major, minor, build;
13716
Matt Carlson75f99362010-04-05 10:19:24 +000013717 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013718
13719 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
13720 return;
13721
13722 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
13723 case TG3_EEPROM_SB_REVISION_0:
13724 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
13725 break;
13726 case TG3_EEPROM_SB_REVISION_2:
13727 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
13728 break;
13729 case TG3_EEPROM_SB_REVISION_3:
13730 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
13731 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000013732 case TG3_EEPROM_SB_REVISION_4:
13733 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
13734 break;
13735 case TG3_EEPROM_SB_REVISION_5:
13736 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
13737 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000013738 case TG3_EEPROM_SB_REVISION_6:
13739 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
13740 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013741 default:
13742 return;
13743 }
13744
Matt Carlsone4f34112009-02-25 14:25:00 +000013745 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080013746 return;
13747
13748 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
13749 TG3_EEPROM_SB_EDH_BLD_SHFT;
13750 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
13751 TG3_EEPROM_SB_EDH_MAJ_SHFT;
13752 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
13753
13754 if (minor > 99 || build > 26)
13755 return;
13756
Matt Carlson75f99362010-04-05 10:19:24 +000013757 offset = strlen(tp->fw_ver);
13758 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
13759 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013760
13761 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000013762 offset = strlen(tp->fw_ver);
13763 if (offset < TG3_VER_SIZE - 1)
13764 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013765 }
13766}
13767
Matt Carlsonacd9c112009-02-25 14:26:33 +000013768static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080013769{
13770 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013771 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070013772
13773 for (offset = TG3_NVM_DIR_START;
13774 offset < TG3_NVM_DIR_END;
13775 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000013776 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013777 return;
13778
13779 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
13780 break;
13781 }
13782
13783 if (offset == TG3_NVM_DIR_END)
13784 return;
13785
Joe Perches63c3a662011-04-26 08:12:10 +000013786 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013787 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000013788 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013789 return;
13790
Matt Carlsone4f34112009-02-25 14:25:00 +000013791 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013792 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013793 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013794 return;
13795
13796 offset += val - start;
13797
Matt Carlsonacd9c112009-02-25 14:26:33 +000013798 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013799
Matt Carlsonacd9c112009-02-25 14:26:33 +000013800 tp->fw_ver[vlen++] = ',';
13801 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070013802
13803 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000013804 __be32 v;
13805 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013806 return;
13807
Al Virob9fc7dc2007-12-17 22:59:57 -080013808 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013809
Matt Carlsonacd9c112009-02-25 14:26:33 +000013810 if (vlen > TG3_VER_SIZE - sizeof(v)) {
13811 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013812 break;
13813 }
13814
Matt Carlsonacd9c112009-02-25 14:26:33 +000013815 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
13816 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013817 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000013818}
13819
Matt Carlson7fd76442009-02-25 14:27:20 +000013820static void __devinit tg3_read_dash_ver(struct tg3 *tp)
13821{
13822 int vlen;
13823 u32 apedata;
Matt Carlsonecc79642010-08-02 11:26:01 +000013824 char *fwtype;
Matt Carlson7fd76442009-02-25 14:27:20 +000013825
Joe Perches63c3a662011-04-26 08:12:10 +000013826 if (!tg3_flag(tp, ENABLE_APE) || !tg3_flag(tp, ENABLE_ASF))
Matt Carlson7fd76442009-02-25 14:27:20 +000013827 return;
13828
13829 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
13830 if (apedata != APE_SEG_SIG_MAGIC)
13831 return;
13832
13833 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
13834 if (!(apedata & APE_FW_STATUS_READY))
13835 return;
13836
13837 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
13838
Matt Carlsondc6d0742010-09-15 08:59:55 +000013839 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI) {
Joe Perches63c3a662011-04-26 08:12:10 +000013840 tg3_flag_set(tp, APE_HAS_NCSI);
Matt Carlsonecc79642010-08-02 11:26:01 +000013841 fwtype = "NCSI";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013842 } else {
Matt Carlsonecc79642010-08-02 11:26:01 +000013843 fwtype = "DASH";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013844 }
Matt Carlsonecc79642010-08-02 11:26:01 +000013845
Matt Carlson7fd76442009-02-25 14:27:20 +000013846 vlen = strlen(tp->fw_ver);
13847
Matt Carlsonecc79642010-08-02 11:26:01 +000013848 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
13849 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000013850 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
13851 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
13852 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
13853 (apedata & APE_FW_VERSION_BLDMSK));
13854}
13855
Matt Carlsonacd9c112009-02-25 14:26:33 +000013856static void __devinit tg3_read_fw_ver(struct tg3 *tp)
13857{
13858 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000013859 bool vpd_vers = false;
13860
13861 if (tp->fw_ver[0] != 0)
13862 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013863
Joe Perches63c3a662011-04-26 08:12:10 +000013864 if (tg3_flag(tp, NO_NVRAM)) {
Matt Carlson75f99362010-04-05 10:19:24 +000013865 strcat(tp->fw_ver, "sb");
Matt Carlsondf259d82009-04-20 06:57:14 +000013866 return;
13867 }
13868
Matt Carlsonacd9c112009-02-25 14:26:33 +000013869 if (tg3_nvram_read(tp, 0, &val))
13870 return;
13871
13872 if (val == TG3_EEPROM_MAGIC)
13873 tg3_read_bc_ver(tp);
13874 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
13875 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013876 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
13877 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013878 else
13879 return;
13880
Matt Carlsonc9cab242011-07-13 09:27:27 +000013881 if (vpd_vers)
Matt Carlson75f99362010-04-05 10:19:24 +000013882 goto done;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013883
Matt Carlsonc9cab242011-07-13 09:27:27 +000013884 if (tg3_flag(tp, ENABLE_APE)) {
13885 if (tg3_flag(tp, ENABLE_ASF))
13886 tg3_read_dash_ver(tp);
13887 } else if (tg3_flag(tp, ENABLE_ASF)) {
13888 tg3_read_mgmtfw_ver(tp);
13889 }
Matt Carlson9c8a6202007-10-21 16:16:08 -070013890
Matt Carlson75f99362010-04-05 10:19:24 +000013891done:
Matt Carlson9c8a6202007-10-21 16:16:08 -070013892 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080013893}
13894
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013895static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
13896{
Joe Perches63c3a662011-04-26 08:12:10 +000013897 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013898 return TG3_RX_RET_MAX_SIZE_5717;
Joe Perches63c3a662011-04-26 08:12:10 +000013899 else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013900 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013901 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000013902 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013903}
13904
Matt Carlson41434702011-03-09 16:58:22 +000013905static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080013906 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
13907 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
13908 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
13909 { },
13910};
13911
Matt Carlson16c7fa72012-02-13 10:20:10 +000013912static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
13913{
13914 struct pci_dev *peer;
13915 unsigned int func, devnr = tp->pdev->devfn & ~7;
13916
13917 for (func = 0; func < 8; func++) {
13918 peer = pci_get_slot(tp->pdev->bus, devnr | func);
13919 if (peer && peer != tp->pdev)
13920 break;
13921 pci_dev_put(peer);
13922 }
13923 /* 5704 can be configured in single-port mode, set peer to
13924 * tp->pdev in that case.
13925 */
13926 if (!peer) {
13927 peer = tp->pdev;
13928 return peer;
13929 }
13930
13931 /*
13932 * We don't need to keep the refcount elevated; there's no way
13933 * to remove one half of this device without removing the other
13934 */
13935 pci_dev_put(peer);
13936
13937 return peer;
13938}
13939
Matt Carlson42b123b2012-02-13 15:20:13 +000013940static void __devinit tg3_detect_asic_rev(struct tg3 *tp, u32 misc_ctrl_reg)
13941{
13942 tp->pci_chip_rev_id = misc_ctrl_reg >> MISC_HOST_CTRL_CHIPREV_SHIFT;
13943 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
13944 u32 reg;
13945
13946 /* All devices that use the alternate
13947 * ASIC REV location have a CPMU.
13948 */
13949 tg3_flag_set(tp, CPMU_PRESENT);
13950
13951 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
13952 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
13953 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
13954 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720)
13955 reg = TG3PCI_GEN2_PRODID_ASICREV;
13956 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
13957 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
13958 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
13959 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
13960 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
13961 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
13962 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762 ||
13963 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766 ||
13964 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782 ||
13965 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
13966 reg = TG3PCI_GEN15_PRODID_ASICREV;
13967 else
13968 reg = TG3PCI_PRODID_ASICREV;
13969
13970 pci_read_config_dword(tp->pdev, reg, &tp->pci_chip_rev_id);
13971 }
13972
13973 /* Wrong chip ID in 5752 A0. This code can be removed later
13974 * as A0 is not in production.
13975 */
13976 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
13977 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
13978
13979 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13980 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13981 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
13982 tg3_flag_set(tp, 5717_PLUS);
13983
13984 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 ||
13985 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57766)
13986 tg3_flag_set(tp, 57765_CLASS);
13987
13988 if (tg3_flag(tp, 57765_CLASS) || tg3_flag(tp, 5717_PLUS))
13989 tg3_flag_set(tp, 57765_PLUS);
13990
13991 /* Intentionally exclude ASIC_REV_5906 */
13992 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
13993 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
13994 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
13995 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
13996 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
13997 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
13998 tg3_flag(tp, 57765_PLUS))
13999 tg3_flag_set(tp, 5755_PLUS);
14000
14001 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
14002 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)
14003 tg3_flag_set(tp, 5780_CLASS);
14004
14005 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
14006 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
14007 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
14008 tg3_flag(tp, 5755_PLUS) ||
14009 tg3_flag(tp, 5780_CLASS))
14010 tg3_flag_set(tp, 5750_PLUS);
14011
14012 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
14013 tg3_flag(tp, 5750_PLUS))
14014 tg3_flag_set(tp, 5705_PLUS);
14015}
14016
Linus Torvalds1da177e2005-04-16 15:20:36 -070014017static int __devinit tg3_get_invariants(struct tg3 *tp)
14018{
Linus Torvalds1da177e2005-04-16 15:20:36 -070014019 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014020 u32 pci_state_reg, grc_misc_cfg;
14021 u32 val;
14022 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014023 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014024
Linus Torvalds1da177e2005-04-16 15:20:36 -070014025 /* Force memory write invalidate off. If we leave it on,
14026 * then on 5700_BX chips we have to enable a workaround.
14027 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
14028 * to match the cacheline size. The Broadcom driver have this
14029 * workaround but turns MWI off all the times so never uses
14030 * it. This seems to suggest that the workaround is insufficient.
14031 */
14032 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
14033 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
14034 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
14035
Matt Carlson16821282011-07-13 09:27:28 +000014036 /* Important! -- Make sure register accesses are byteswapped
14037 * correctly. Also, for those chips that require it, make
14038 * sure that indirect register accesses are enabled before
14039 * the first operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014040 */
14041 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
14042 &misc_ctrl_reg);
Matt Carlson16821282011-07-13 09:27:28 +000014043 tp->misc_host_ctrl |= (misc_ctrl_reg &
14044 MISC_HOST_CTRL_CHIPREV);
14045 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
14046 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014047
Matt Carlson42b123b2012-02-13 15:20:13 +000014048 tg3_detect_asic_rev(tp, misc_ctrl_reg);
Michael Chanff645be2005-04-21 17:09:53 -070014049
Michael Chan68929142005-08-09 20:17:14 -070014050 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
14051 * we need to disable memory and use config. cycles
14052 * only to access all registers. The 5702/03 chips
14053 * can mistakenly decode the special cycles from the
14054 * ICH chipsets as memory write cycles, causing corruption
14055 * of register and memory space. Only certain ICH bridges
14056 * will drive special cycles with non-zero data during the
14057 * address phase which can fall within the 5703's address
14058 * range. This is not an ICH bug as the PCI spec allows
14059 * non-zero address during special cycles. However, only
14060 * these ICH bridges are known to drive non-zero addresses
14061 * during special cycles.
14062 *
14063 * Since special cycles do not cross PCI bridges, we only
14064 * enable this workaround if the 5703 is on the secondary
14065 * bus of these ICH bridges.
14066 */
14067 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
14068 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
14069 static struct tg3_dev_id {
14070 u32 vendor;
14071 u32 device;
14072 u32 rev;
14073 } ich_chipsets[] = {
14074 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
14075 PCI_ANY_ID },
14076 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
14077 PCI_ANY_ID },
14078 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
14079 0xa },
14080 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
14081 PCI_ANY_ID },
14082 { },
14083 };
14084 struct tg3_dev_id *pci_id = &ich_chipsets[0];
14085 struct pci_dev *bridge = NULL;
14086
14087 while (pci_id->vendor != 0) {
14088 bridge = pci_get_device(pci_id->vendor, pci_id->device,
14089 bridge);
14090 if (!bridge) {
14091 pci_id++;
14092 continue;
14093 }
14094 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070014095 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070014096 continue;
14097 }
14098 if (bridge->subordinate &&
14099 (bridge->subordinate->number ==
14100 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014101 tg3_flag_set(tp, ICH_WORKAROUND);
Michael Chan68929142005-08-09 20:17:14 -070014102 pci_dev_put(bridge);
14103 break;
14104 }
14105 }
14106 }
14107
Matt Carlson6ff6f812011-05-19 12:12:54 +000014108 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Matt Carlson41588ba12008-04-19 18:12:33 -070014109 static struct tg3_dev_id {
14110 u32 vendor;
14111 u32 device;
14112 } bridge_chipsets[] = {
14113 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
14114 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
14115 { },
14116 };
14117 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
14118 struct pci_dev *bridge = NULL;
14119
14120 while (pci_id->vendor != 0) {
14121 bridge = pci_get_device(pci_id->vendor,
14122 pci_id->device,
14123 bridge);
14124 if (!bridge) {
14125 pci_id++;
14126 continue;
14127 }
14128 if (bridge->subordinate &&
14129 (bridge->subordinate->number <=
14130 tp->pdev->bus->number) &&
14131 (bridge->subordinate->subordinate >=
14132 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014133 tg3_flag_set(tp, 5701_DMA_BUG);
Matt Carlson41588ba12008-04-19 18:12:33 -070014134 pci_dev_put(bridge);
14135 break;
14136 }
14137 }
14138 }
14139
Michael Chan4a29cc22006-03-19 13:21:12 -080014140 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
14141 * DMA addresses > 40-bit. This bridge may have other additional
14142 * 57xx devices behind it in some 4-port NIC designs for example.
14143 * Any tg3 device found behind the bridge will also need the 40-bit
14144 * DMA workaround.
14145 */
Matt Carlson42b123b2012-02-13 15:20:13 +000014146 if (tg3_flag(tp, 5780_CLASS)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014147 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4cf78e42005-07-25 12:29:19 -070014148 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a588792010-04-05 10:19:28 +000014149 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080014150 struct pci_dev *bridge = NULL;
14151
14152 do {
14153 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
14154 PCI_DEVICE_ID_SERVERWORKS_EPB,
14155 bridge);
14156 if (bridge && bridge->subordinate &&
14157 (bridge->subordinate->number <=
14158 tp->pdev->bus->number) &&
14159 (bridge->subordinate->subordinate >=
14160 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014161 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4a29cc22006-03-19 13:21:12 -080014162 pci_dev_put(bridge);
14163 break;
14164 }
14165 } while (bridge);
14166 }
Michael Chan4cf78e42005-07-25 12:29:19 -070014167
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014168 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Matt Carlson3a1e19d2011-07-13 09:27:32 +000014169 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)
Michael Chan7544b092007-05-05 13:08:32 -070014170 tp->pdev_peer = tg3_find_peer(tp);
14171
Matt Carlson507399f2009-11-13 13:03:37 +000014172 /* Determine TSO capabilities */
Matt Carlsona0512942011-07-27 14:20:54 +000014173 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0)
Matt Carlson4d163b72011-01-25 15:58:48 +000014174 ; /* Do nothing. HW bug. */
Joe Perches63c3a662011-04-26 08:12:10 +000014175 else if (tg3_flag(tp, 57765_PLUS))
14176 tg3_flag_set(tp, HW_TSO_3);
14177 else if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000014178 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000014179 tg3_flag_set(tp, HW_TSO_2);
14180 else if (tg3_flag(tp, 5750_PLUS)) {
14181 tg3_flag_set(tp, HW_TSO_1);
14182 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000014183 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
14184 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
Joe Perches63c3a662011-04-26 08:12:10 +000014185 tg3_flag_clear(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000014186 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14187 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
14188 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000014189 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000014190 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
14191 tp->fw_needed = FIRMWARE_TG3TSO5;
14192 else
14193 tp->fw_needed = FIRMWARE_TG3TSO;
14194 }
14195
Matt Carlsondabc5c62011-05-19 12:12:52 +000014196 /* Selectively allow TSO based on operating conditions */
Matt Carlson6ff6f812011-05-19 12:12:54 +000014197 if (tg3_flag(tp, HW_TSO_1) ||
14198 tg3_flag(tp, HW_TSO_2) ||
14199 tg3_flag(tp, HW_TSO_3) ||
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000014200 tp->fw_needed) {
14201 /* For firmware TSO, assume ASF is disabled.
14202 * We'll disable TSO later if we discover ASF
14203 * is enabled in tg3_get_eeprom_hw_cfg().
14204 */
Matt Carlsondabc5c62011-05-19 12:12:52 +000014205 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000014206 } else {
Matt Carlsondabc5c62011-05-19 12:12:52 +000014207 tg3_flag_clear(tp, TSO_CAPABLE);
14208 tg3_flag_clear(tp, TSO_BUG);
14209 tp->fw_needed = NULL;
14210 }
14211
14212 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
14213 tp->fw_needed = FIRMWARE_TG3;
14214
Matt Carlson507399f2009-11-13 13:03:37 +000014215 tp->irq_max = 1;
14216
Joe Perches63c3a662011-04-26 08:12:10 +000014217 if (tg3_flag(tp, 5750_PLUS)) {
14218 tg3_flag_set(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070014219 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
14220 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
14221 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
14222 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
14223 tp->pdev_peer == tp->pdev))
Joe Perches63c3a662011-04-26 08:12:10 +000014224 tg3_flag_clear(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070014225
Joe Perches63c3a662011-04-26 08:12:10 +000014226 if (tg3_flag(tp, 5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070014227 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000014228 tg3_flag_set(tp, 1SHOT_MSI);
Michael Chan52c0fd82006-06-29 20:15:54 -070014229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014230
Joe Perches63c3a662011-04-26 08:12:10 +000014231 if (tg3_flag(tp, 57765_PLUS)) {
14232 tg3_flag_set(tp, SUPPORT_MSIX);
Matt Carlson507399f2009-11-13 13:03:37 +000014233 tp->irq_max = TG3_IRQ_MAX_VECS;
Matt Carlson90415472011-12-16 13:33:23 +000014234 tg3_rss_init_dflt_indir_tbl(tp);
Matt Carlson507399f2009-11-13 13:03:37 +000014235 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014236 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000014237
Matt Carlson2ffcc982011-05-19 12:12:44 +000014238 if (tg3_flag(tp, 5755_PLUS))
Joe Perches63c3a662011-04-26 08:12:10 +000014239 tg3_flag_set(tp, SHORT_DMA_BUG);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014240
Matt Carlsone31aa982011-07-27 14:20:53 +000014241 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona4cb4282011-12-14 11:09:58 +000014242 tp->dma_limit = TG3_TX_BD_DMA_MAX_4K;
Matt Carlsone31aa982011-07-27 14:20:53 +000014243
Matt Carlsonfa6b2aa2011-11-21 15:01:19 +000014244 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14245 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
14246 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Joe Perches63c3a662011-04-26 08:12:10 +000014247 tg3_flag_set(tp, LRG_PROD_RING_CAP);
Matt Carlsonde9f5232011-04-05 14:22:43 +000014248
Joe Perches63c3a662011-04-26 08:12:10 +000014249 if (tg3_flag(tp, 57765_PLUS) &&
Matt Carlsona0512942011-07-27 14:20:54 +000014250 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0)
Joe Perches63c3a662011-04-26 08:12:10 +000014251 tg3_flag_set(tp, USE_JUMBO_BDFLAG);
Matt Carlsonb703df62009-12-03 08:36:21 +000014252
Joe Perches63c3a662011-04-26 08:12:10 +000014253 if (!tg3_flag(tp, 5705_PLUS) ||
14254 tg3_flag(tp, 5780_CLASS) ||
14255 tg3_flag(tp, USE_JUMBO_BDFLAG))
14256 tg3_flag_set(tp, JUMBO_CAPABLE);
Michael Chan0f893dc2005-07-25 12:30:38 -070014257
Matt Carlson52f44902008-11-21 17:17:04 -080014258 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14259 &pci_state_reg);
14260
Jon Mason708ebb3a2011-06-27 12:56:50 +000014261 if (pci_is_pcie(tp->pdev)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014262 u16 lnkctl;
14263
Joe Perches63c3a662011-04-26 08:12:10 +000014264 tg3_flag_set(tp, PCI_EXPRESS);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080014265
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014266 pci_read_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +000014267 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014268 &lnkctl);
14269 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
Matt Carlson7196cd62011-05-19 16:02:44 +000014270 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
14271 ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000014272 tg3_flag_clear(tp, HW_TSO_2);
Matt Carlsondabc5c62011-05-19 12:12:52 +000014273 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson7196cd62011-05-19 16:02:44 +000014274 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014275 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014276 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000014277 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
14278 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Joe Perches63c3a662011-04-26 08:12:10 +000014279 tg3_flag_set(tp, CLKREQ_BUG);
Matt Carlson614b0592010-01-20 16:58:02 +000014280 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000014281 tg3_flag_set(tp, L1PLLPD_EN);
Michael Chanc7835a72006-11-15 21:14:42 -080014282 }
Matt Carlson52f44902008-11-21 17:17:04 -080014283 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Jon Mason708ebb3a2011-06-27 12:56:50 +000014284 /* BCM5785 devices are effectively PCIe devices, and should
14285 * follow PCIe codepaths, but do not have a PCIe capabilities
14286 * section.
Matt Carlson93a700a2011-08-31 11:44:54 +000014287 */
Joe Perches63c3a662011-04-26 08:12:10 +000014288 tg3_flag_set(tp, PCI_EXPRESS);
14289 } else if (!tg3_flag(tp, 5705_PLUS) ||
14290 tg3_flag(tp, 5780_CLASS)) {
Matt Carlson52f44902008-11-21 17:17:04 -080014291 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
14292 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000014293 dev_err(&tp->pdev->dev,
14294 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080014295 return -EIO;
14296 }
14297
14298 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
Joe Perches63c3a662011-04-26 08:12:10 +000014299 tg3_flag_set(tp, PCIX_MODE);
Matt Carlson52f44902008-11-21 17:17:04 -080014300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014301
Michael Chan399de502005-10-03 14:02:39 -070014302 /* If we have an AMD 762 or VIA K8T800 chipset, write
14303 * reordering to the mailbox registers done by the host
14304 * controller can cause major troubles. We read back from
14305 * every mailbox register write to force the writes to be
14306 * posted to the chip in order.
14307 */
Matt Carlson41434702011-03-09 16:58:22 +000014308 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Joe Perches63c3a662011-04-26 08:12:10 +000014309 !tg3_flag(tp, PCI_EXPRESS))
14310 tg3_flag_set(tp, MBOX_WRITE_REORDER);
Michael Chan399de502005-10-03 14:02:39 -070014311
Matt Carlson69fc4052008-12-21 20:19:57 -080014312 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
14313 &tp->pci_cacheline_sz);
14314 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
14315 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014316 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14317 tp->pci_lat_timer < 64) {
14318 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080014319 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
14320 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014321 }
14322
Matt Carlson16821282011-07-13 09:27:28 +000014323 /* Important! -- It is critical that the PCI-X hw workaround
14324 * situation is decided before the first MMIO register access.
14325 */
Matt Carlson52f44902008-11-21 17:17:04 -080014326 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
14327 /* 5700 BX chips need to have their TX producer index
14328 * mailboxes written twice to workaround a bug.
14329 */
Joe Perches63c3a662011-04-26 08:12:10 +000014330 tg3_flag_set(tp, TXD_MBOX_HWBUG);
Matt Carlson9974a352007-10-07 23:27:28 -070014331
Matt Carlson52f44902008-11-21 17:17:04 -080014332 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014333 *
14334 * The workaround is to use indirect register accesses
14335 * for all chip writes not to mailbox registers.
14336 */
Joe Perches63c3a662011-04-26 08:12:10 +000014337 if (tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014338 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014339
Joe Perches63c3a662011-04-26 08:12:10 +000014340 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014341
14342 /* The chip can have it's power management PCI config
14343 * space registers clobbered due to this bug.
14344 * So explicitly force the chip into D0 here.
14345 */
Matt Carlson9974a352007-10-07 23:27:28 -070014346 pci_read_config_dword(tp->pdev,
14347 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014348 &pm_reg);
14349 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
14350 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070014351 pci_write_config_dword(tp->pdev,
14352 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014353 pm_reg);
14354
14355 /* Also, force SERR#/PERR# in PCI command. */
14356 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
14357 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
14358 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
14359 }
14360 }
14361
Linus Torvalds1da177e2005-04-16 15:20:36 -070014362 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000014363 tg3_flag_set(tp, PCI_HIGH_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014364 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000014365 tg3_flag_set(tp, PCI_32BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014366
14367 /* Chip-specific fixup from Broadcom driver */
14368 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
14369 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
14370 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
14371 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
14372 }
14373
Michael Chan1ee582d2005-08-09 20:16:46 -070014374 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070014375 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070014376 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070014377 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070014378 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070014379 tp->write32_tx_mbox = tg3_write32;
14380 tp->write32_rx_mbox = tg3_write32;
14381
14382 /* Various workaround register access methods */
Joe Perches63c3a662011-04-26 08:12:10 +000014383 if (tg3_flag(tp, PCIX_TARGET_HWBUG))
Michael Chan1ee582d2005-08-09 20:16:46 -070014384 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070014385 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014386 (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson98efd8a2007-05-05 12:47:25 -070014387 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
14388 /*
14389 * Back to back register writes can cause problems on these
14390 * chips, the workaround is to read back all reg writes
14391 * except those to mailbox regs.
14392 *
14393 * See tg3_write_indirect_reg32().
14394 */
Michael Chan1ee582d2005-08-09 20:16:46 -070014395 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070014396 }
14397
Joe Perches63c3a662011-04-26 08:12:10 +000014398 if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
Michael Chan1ee582d2005-08-09 20:16:46 -070014399 tp->write32_tx_mbox = tg3_write32_tx_mbox;
Joe Perches63c3a662011-04-26 08:12:10 +000014400 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Michael Chan1ee582d2005-08-09 20:16:46 -070014401 tp->write32_rx_mbox = tg3_write_flush_reg32;
14402 }
Michael Chan20094932005-08-09 20:16:32 -070014403
Joe Perches63c3a662011-04-26 08:12:10 +000014404 if (tg3_flag(tp, ICH_WORKAROUND)) {
Michael Chan68929142005-08-09 20:17:14 -070014405 tp->read32 = tg3_read_indirect_reg32;
14406 tp->write32 = tg3_write_indirect_reg32;
14407 tp->read32_mbox = tg3_read_indirect_mbox;
14408 tp->write32_mbox = tg3_write_indirect_mbox;
14409 tp->write32_tx_mbox = tg3_write_indirect_mbox;
14410 tp->write32_rx_mbox = tg3_write_indirect_mbox;
14411
14412 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070014413 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070014414
14415 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
14416 pci_cmd &= ~PCI_COMMAND_MEMORY;
14417 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
14418 }
Michael Chanb5d37722006-09-27 16:06:21 -070014419 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
14420 tp->read32_mbox = tg3_read32_mbox_5906;
14421 tp->write32_mbox = tg3_write32_mbox_5906;
14422 tp->write32_tx_mbox = tg3_write32_mbox_5906;
14423 tp->write32_rx_mbox = tg3_write32_mbox_5906;
14424 }
Michael Chan68929142005-08-09 20:17:14 -070014425
Michael Chanbbadf502006-04-06 21:46:34 -070014426 if (tp->write32 == tg3_write_indirect_reg32 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014427 (tg3_flag(tp, PCIX_MODE) &&
Michael Chanbbadf502006-04-06 21:46:34 -070014428 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070014429 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Joe Perches63c3a662011-04-26 08:12:10 +000014430 tg3_flag_set(tp, SRAM_USE_CONFIG);
Michael Chanbbadf502006-04-06 21:46:34 -070014431
Matt Carlson16821282011-07-13 09:27:28 +000014432 /* The memory arbiter has to be enabled in order for SRAM accesses
14433 * to succeed. Normally on powerup the tg3 chip firmware will make
14434 * sure it is enabled, but other entities such as system netboot
14435 * code might disable it.
14436 */
14437 val = tr32(MEMARB_MODE);
14438 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
14439
Matt Carlson9dc5e342011-11-04 09:15:02 +000014440 tp->pci_fn = PCI_FUNC(tp->pdev->devfn) & 3;
14441 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
14442 tg3_flag(tp, 5780_CLASS)) {
14443 if (tg3_flag(tp, PCIX_MODE)) {
14444 pci_read_config_dword(tp->pdev,
14445 tp->pcix_cap + PCI_X_STATUS,
14446 &val);
14447 tp->pci_fn = val & 0x7;
14448 }
14449 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
14450 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
14451 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) ==
14452 NIC_SRAM_CPMUSTAT_SIG) {
14453 tp->pci_fn = val & TG3_CPMU_STATUS_FMSK_5717;
14454 tp->pci_fn = tp->pci_fn ? 1 : 0;
14455 }
14456 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
14457 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
14458 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
14459 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) ==
14460 NIC_SRAM_CPMUSTAT_SIG) {
14461 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5719) >>
14462 TG3_CPMU_STATUS_FSHFT_5719;
14463 }
Matt Carlson69f11c92011-07-13 09:27:30 +000014464 }
14465
Michael Chan7d0c41e2005-04-21 17:06:20 -070014466 /* Get eeprom hw config before calling tg3_set_power_state().
Joe Perches63c3a662011-04-26 08:12:10 +000014467 * In particular, the TG3_FLAG_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070014468 * determined before calling tg3_set_power_state() so that
14469 * we know whether or not to switch out of Vaux power.
14470 * When the flag is set, it means that GPIO1 is used for eeprom
14471 * write protect and also implies that it is a LOM where GPIOs
14472 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040014473 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070014474 tg3_get_eeprom_hw_cfg(tp);
14475
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000014476 if (tp->fw_needed && tg3_flag(tp, ENABLE_ASF)) {
14477 tg3_flag_clear(tp, TSO_CAPABLE);
14478 tg3_flag_clear(tp, TSO_BUG);
14479 tp->fw_needed = NULL;
14480 }
14481
Joe Perches63c3a662011-04-26 08:12:10 +000014482 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070014483 /* Allow reads and writes to the
14484 * APE register and memory space.
14485 */
14486 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +000014487 PCISTATE_ALLOW_APE_SHMEM_WR |
14488 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070014489 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
14490 pci_state_reg);
Matt Carlsonc9cab242011-07-13 09:27:27 +000014491
14492 tg3_ape_lock_init(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070014493 }
14494
Matt Carlson16821282011-07-13 09:27:28 +000014495 /* Set up tp->grc_local_ctrl before calling
14496 * tg3_pwrsrc_switch_to_vmain(). GPIO1 driven high
14497 * will bring 5700's external PHY out of reset.
Michael Chan314fba32005-04-21 17:07:04 -070014498 * It is also used as eeprom write protect on LOMs.
14499 */
14500 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
Matt Carlson6ff6f812011-05-19 12:12:54 +000014501 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014502 tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan314fba32005-04-21 17:07:04 -070014503 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
14504 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070014505 /* Unused GPIO3 must be driven as output on 5752 because there
14506 * are no pull-up resistors on unused GPIO pins.
14507 */
14508 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
14509 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070014510
Matt Carlson321d32a2008-11-21 17:22:19 -080014511 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsoncb4ed1f2010-01-20 16:58:09 +000014512 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000014513 tg3_flag(tp, 57765_CLASS))
Michael Chanaf36e6b2006-03-23 01:28:06 -080014514 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
14515
Matt Carlson8d519ab2009-04-20 06:58:01 +000014516 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
14517 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070014518 /* Turn off the debug UART. */
14519 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014520 if (tg3_flag(tp, IS_NIC))
Matt Carlson5f0c4a32008-06-09 15:41:12 -070014521 /* Keep VMain power. */
14522 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
14523 GRC_LCLCTRL_GPIO_OUTPUT0;
14524 }
14525
Matt Carlson16821282011-07-13 09:27:28 +000014526 /* Switch out of Vaux if it is a NIC */
14527 tg3_pwrsrc_switch_to_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014528
Linus Torvalds1da177e2005-04-16 15:20:36 -070014529 /* Derive initial jumbo mode from MTU assigned in
14530 * ether_setup() via the alloc_etherdev() call
14531 */
Joe Perches63c3a662011-04-26 08:12:10 +000014532 if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
14533 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014534
14535 /* Determine WakeOnLan speed to use. */
14536 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14537 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
14538 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
14539 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
Joe Perches63c3a662011-04-26 08:12:10 +000014540 tg3_flag_clear(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014541 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000014542 tg3_flag_set(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014543 }
14544
Matt Carlson7f97a4b2009-08-25 10:10:03 +000014545 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014546 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000014547
Linus Torvalds1da177e2005-04-16 15:20:36 -070014548 /* A few boards don't want Ethernet@WireSpeed phy feature */
Matt Carlson6ff6f812011-05-19 12:12:54 +000014549 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14550 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070014551 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070014552 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014553 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
14554 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
14555 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014556
14557 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
14558 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014559 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014560 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014561 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014562
Joe Perches63c3a662011-04-26 08:12:10 +000014563 if (tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014564 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080014565 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014566 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014567 !tg3_flag(tp, 57765_PLUS)) {
Michael Chanc424cb22006-04-29 18:56:34 -070014568 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070014569 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070014570 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
14571 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080014572 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
14573 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014574 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080014575 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014576 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080014577 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014578 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070014579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014580
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014581 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
14582 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
14583 tp->phy_otp = tg3_read_otp_phycfg(tp);
14584 if (tp->phy_otp == 0)
14585 tp->phy_otp = TG3_OTP_DEFAULT;
14586 }
14587
Joe Perches63c3a662011-04-26 08:12:10 +000014588 if (tg3_flag(tp, CPMU_PRESENT))
Matt Carlson8ef21422008-05-02 16:47:53 -070014589 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
14590 else
14591 tp->mi_mode = MAC_MI_MODE_BASE;
14592
Linus Torvalds1da177e2005-04-16 15:20:36 -070014593 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014594 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
14595 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
14596 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
14597
Matt Carlson4d958472011-04-20 07:57:35 +000014598 /* Set these bits to enable statistics workaround. */
14599 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14600 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
14601 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0) {
14602 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
14603 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
14604 }
14605
Matt Carlson321d32a2008-11-21 17:22:19 -080014606 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
14607 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Joe Perches63c3a662011-04-26 08:12:10 +000014608 tg3_flag_set(tp, USE_PHYLIB);
Matt Carlson57e69832008-05-25 23:48:31 -070014609
Matt Carlson158d7ab2008-05-29 01:37:54 -070014610 err = tg3_mdio_init(tp);
14611 if (err)
14612 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014613
14614 /* Initialize data/descriptor byte/word swapping. */
14615 val = tr32(GRC_MODE);
Matt Carlsonf2096f92011-04-05 14:22:48 +000014616 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
14617 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
14618 GRC_MODE_WORD_SWAP_B2HRX_DATA |
14619 GRC_MODE_B2HRX_ENABLE |
14620 GRC_MODE_HTX2B_ENABLE |
14621 GRC_MODE_HOST_STACKUP);
14622 else
14623 val &= GRC_MODE_HOST_STACKUP;
14624
Linus Torvalds1da177e2005-04-16 15:20:36 -070014625 tw32(GRC_MODE, val | tp->grc_mode);
14626
14627 tg3_switch_clocks(tp);
14628
14629 /* Clear this out for sanity. */
14630 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
14631
14632 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14633 &pci_state_reg);
14634 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014635 !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014636 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
14637
14638 if (chiprevid == CHIPREV_ID_5701_A0 ||
14639 chiprevid == CHIPREV_ID_5701_B0 ||
14640 chiprevid == CHIPREV_ID_5701_B2 ||
14641 chiprevid == CHIPREV_ID_5701_B5) {
14642 void __iomem *sram_base;
14643
14644 /* Write some dummy words into the SRAM status block
14645 * area, see if it reads back correctly. If the return
14646 * value is bad, force enable the PCIX workaround.
14647 */
14648 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
14649
14650 writel(0x00000000, sram_base);
14651 writel(0x00000000, sram_base + 4);
14652 writel(0xffffffff, sram_base + 4);
14653 if (readl(sram_base) != 0x00000000)
Joe Perches63c3a662011-04-26 08:12:10 +000014654 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014655 }
14656 }
14657
14658 udelay(50);
14659 tg3_nvram_init(tp);
14660
14661 grc_misc_cfg = tr32(GRC_MISC_CFG);
14662 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
14663
Linus Torvalds1da177e2005-04-16 15:20:36 -070014664 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14665 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
14666 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
Joe Perches63c3a662011-04-26 08:12:10 +000014667 tg3_flag_set(tp, IS_5788);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014668
Joe Perches63c3a662011-04-26 08:12:10 +000014669 if (!tg3_flag(tp, IS_5788) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +000014670 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014671 tg3_flag_set(tp, TAGGED_STATUS);
14672 if (tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070014673 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
14674 HOSTCC_MODE_CLRTICK_TXBD);
14675
14676 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
14677 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
14678 tp->misc_host_ctrl);
14679 }
14680
Matt Carlson3bda1252008-08-15 14:08:22 -070014681 /* Preserve the APE MAC_MODE bits */
Joe Perches63c3a662011-04-26 08:12:10 +000014682 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +000014683 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070014684 else
Matt Carlson6e01b202011-08-19 13:58:20 +000014685 tp->mac_mode = 0;
Matt Carlson3bda1252008-08-15 14:08:22 -070014686
Linus Torvalds1da177e2005-04-16 15:20:36 -070014687 /* these are limited to 10/100 only */
14688 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14689 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
14690 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14691 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14692 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
14693 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
14694 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
14695 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14696 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
Michael Chan676917d2006-12-07 00:20:22 -080014697 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
14698 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014699 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
Matt Carlsond1101142010-02-17 15:16:55 +000014700 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
14701 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014702 (tp->phy_flags & TG3_PHYFLG_IS_FET))
14703 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014704
14705 err = tg3_phy_probe(tp);
14706 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014707 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014708 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014709 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014710 }
14711
Matt Carlson184b8902010-04-05 10:19:25 +000014712 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080014713 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014714
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014715 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
14716 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014717 } else {
14718 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014719 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014720 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014721 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014722 }
14723
14724 /* 5700 {AX,BX} chips have a broken status block link
14725 * change bit implementation, so we must use the
14726 * status register in those cases.
14727 */
14728 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014729 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014730 else
Joe Perches63c3a662011-04-26 08:12:10 +000014731 tg3_flag_clear(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014732
14733 /* The led_ctrl is set during tg3_phy_probe, here we might
14734 * have to force the link status polling mechanism based
14735 * upon subsystem IDs.
14736 */
14737 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070014738 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014739 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
14740 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Joe Perches63c3a662011-04-26 08:12:10 +000014741 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014742 }
14743
14744 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014745 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Joe Perches63c3a662011-04-26 08:12:10 +000014746 tg3_flag_set(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014747 else
Joe Perches63c3a662011-04-26 08:12:10 +000014748 tg3_flag_clear(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014749
Eric Dumazet9205fd92011-11-18 06:47:01 +000014750 tp->rx_offset = NET_SKB_PAD + NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014751 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014752 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014753 tg3_flag(tp, PCIX_MODE)) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000014754 tp->rx_offset = NET_SKB_PAD;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014755#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000014756 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014757#endif
14758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014759
Matt Carlson2c49a442010-09-30 10:34:35 +000014760 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
14761 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000014762 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
14763
Matt Carlson2c49a442010-09-30 10:34:35 +000014764 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070014765
14766 /* Increment the rx prod index on the rx std ring by at most
14767 * 8 for these chips to workaround hw errata.
14768 */
14769 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
14770 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
14771 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
14772 tp->rx_std_max_post = 8;
14773
Joe Perches63c3a662011-04-26 08:12:10 +000014774 if (tg3_flag(tp, ASPM_WORKAROUND))
Matt Carlson8ed5d972007-05-07 00:25:49 -070014775 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
14776 PCIE_PWR_MGMT_L1_THRESH_MSK;
14777
Linus Torvalds1da177e2005-04-16 15:20:36 -070014778 return err;
14779}
14780
David S. Miller49b6e95f2007-03-29 01:38:42 -070014781#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014782static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
14783{
14784 struct net_device *dev = tp->dev;
14785 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014786 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070014787 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014788 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014789
David S. Miller49b6e95f2007-03-29 01:38:42 -070014790 addr = of_get_property(dp, "local-mac-address", &len);
14791 if (addr && len == 6) {
14792 memcpy(dev->dev_addr, addr, 6);
14793 memcpy(dev->perm_addr, dev->dev_addr, 6);
14794 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014795 }
14796 return -ENODEV;
14797}
14798
14799static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
14800{
14801 struct net_device *dev = tp->dev;
14802
14803 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070014804 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014805 return 0;
14806}
14807#endif
14808
14809static int __devinit tg3_get_device_address(struct tg3 *tp)
14810{
14811 struct net_device *dev = tp->dev;
14812 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080014813 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014814
David S. Miller49b6e95f2007-03-29 01:38:42 -070014815#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014816 if (!tg3_get_macaddr_sparc(tp))
14817 return 0;
14818#endif
14819
14820 mac_offset = 0x7c;
Matt Carlson6ff6f812011-05-19 12:12:54 +000014821 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014822 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014823 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
14824 mac_offset = 0xcc;
14825 if (tg3_nvram_lock(tp))
14826 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
14827 else
14828 tg3_nvram_unlock(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000014829 } else if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson69f11c92011-07-13 09:27:30 +000014830 if (tp->pci_fn & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014831 mac_offset = 0xcc;
Matt Carlson69f11c92011-07-13 09:27:30 +000014832 if (tp->pci_fn > 1)
Matt Carlsona50d0792010-06-05 17:24:37 +000014833 mac_offset += 0x18c;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014834 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070014835 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014836
14837 /* First try to get it from MAC address mailbox. */
14838 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
14839 if ((hi >> 16) == 0x484b) {
14840 dev->dev_addr[0] = (hi >> 8) & 0xff;
14841 dev->dev_addr[1] = (hi >> 0) & 0xff;
14842
14843 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
14844 dev->dev_addr[2] = (lo >> 24) & 0xff;
14845 dev->dev_addr[3] = (lo >> 16) & 0xff;
14846 dev->dev_addr[4] = (lo >> 8) & 0xff;
14847 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014848
Michael Chan008652b2006-03-27 23:14:53 -080014849 /* Some old bootcode may report a 0 MAC address in SRAM */
14850 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
14851 }
14852 if (!addr_ok) {
14853 /* Next, try NVRAM. */
Joe Perches63c3a662011-04-26 08:12:10 +000014854 if (!tg3_flag(tp, NO_NVRAM) &&
Matt Carlsondf259d82009-04-20 06:57:14 +000014855 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000014856 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070014857 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
14858 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080014859 }
14860 /* Finally just fetch it out of the MAC control regs. */
14861 else {
14862 hi = tr32(MAC_ADDR_0_HIGH);
14863 lo = tr32(MAC_ADDR_0_LOW);
14864
14865 dev->dev_addr[5] = lo & 0xff;
14866 dev->dev_addr[4] = (lo >> 8) & 0xff;
14867 dev->dev_addr[3] = (lo >> 16) & 0xff;
14868 dev->dev_addr[2] = (lo >> 24) & 0xff;
14869 dev->dev_addr[1] = hi & 0xff;
14870 dev->dev_addr[0] = (hi >> 8) & 0xff;
14871 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014872 }
14873
14874 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070014875#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014876 if (!tg3_get_default_macaddr_sparc(tp))
14877 return 0;
14878#endif
14879 return -EINVAL;
14880 }
John W. Linville2ff43692005-09-12 14:44:20 -070014881 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014882 return 0;
14883}
14884
David S. Miller59e6b432005-05-18 22:50:10 -070014885#define BOUNDARY_SINGLE_CACHELINE 1
14886#define BOUNDARY_MULTI_CACHELINE 2
14887
14888static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
14889{
14890 int cacheline_size;
14891 u8 byte;
14892 int goal;
14893
14894 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
14895 if (byte == 0)
14896 cacheline_size = 1024;
14897 else
14898 cacheline_size = (int) byte * 4;
14899
14900 /* On 5703 and later chips, the boundary bits have no
14901 * effect.
14902 */
14903 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14904 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014905 !tg3_flag(tp, PCI_EXPRESS))
David S. Miller59e6b432005-05-18 22:50:10 -070014906 goto out;
14907
14908#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
14909 goal = BOUNDARY_MULTI_CACHELINE;
14910#else
14911#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
14912 goal = BOUNDARY_SINGLE_CACHELINE;
14913#else
14914 goal = 0;
14915#endif
14916#endif
14917
Joe Perches63c3a662011-04-26 08:12:10 +000014918 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014919 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
14920 goto out;
14921 }
14922
David S. Miller59e6b432005-05-18 22:50:10 -070014923 if (!goal)
14924 goto out;
14925
14926 /* PCI controllers on most RISC systems tend to disconnect
14927 * when a device tries to burst across a cache-line boundary.
14928 * Therefore, letting tg3 do so just wastes PCI bandwidth.
14929 *
14930 * Unfortunately, for PCI-E there are only limited
14931 * write-side controls for this, and thus for reads
14932 * we will still get the disconnects. We'll also waste
14933 * these PCI cycles for both read and write for chips
14934 * other than 5700 and 5701 which do not implement the
14935 * boundary bits.
14936 */
Joe Perches63c3a662011-04-26 08:12:10 +000014937 if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014938 switch (cacheline_size) {
14939 case 16:
14940 case 32:
14941 case 64:
14942 case 128:
14943 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14944 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
14945 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
14946 } else {
14947 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14948 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14949 }
14950 break;
14951
14952 case 256:
14953 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
14954 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
14955 break;
14956
14957 default:
14958 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14959 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14960 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014961 }
Joe Perches63c3a662011-04-26 08:12:10 +000014962 } else if (tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014963 switch (cacheline_size) {
14964 case 16:
14965 case 32:
14966 case 64:
14967 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14968 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14969 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
14970 break;
14971 }
14972 /* fallthrough */
14973 case 128:
14974 default:
14975 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14976 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
14977 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014978 }
David S. Miller59e6b432005-05-18 22:50:10 -070014979 } else {
14980 switch (cacheline_size) {
14981 case 16:
14982 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14983 val |= (DMA_RWCTRL_READ_BNDRY_16 |
14984 DMA_RWCTRL_WRITE_BNDRY_16);
14985 break;
14986 }
14987 /* fallthrough */
14988 case 32:
14989 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14990 val |= (DMA_RWCTRL_READ_BNDRY_32 |
14991 DMA_RWCTRL_WRITE_BNDRY_32);
14992 break;
14993 }
14994 /* fallthrough */
14995 case 64:
14996 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14997 val |= (DMA_RWCTRL_READ_BNDRY_64 |
14998 DMA_RWCTRL_WRITE_BNDRY_64);
14999 break;
15000 }
15001 /* fallthrough */
15002 case 128:
15003 if (goal == BOUNDARY_SINGLE_CACHELINE) {
15004 val |= (DMA_RWCTRL_READ_BNDRY_128 |
15005 DMA_RWCTRL_WRITE_BNDRY_128);
15006 break;
15007 }
15008 /* fallthrough */
15009 case 256:
15010 val |= (DMA_RWCTRL_READ_BNDRY_256 |
15011 DMA_RWCTRL_WRITE_BNDRY_256);
15012 break;
15013 case 512:
15014 val |= (DMA_RWCTRL_READ_BNDRY_512 |
15015 DMA_RWCTRL_WRITE_BNDRY_512);
15016 break;
15017 case 1024:
15018 default:
15019 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
15020 DMA_RWCTRL_WRITE_BNDRY_1024);
15021 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070015022 }
David S. Miller59e6b432005-05-18 22:50:10 -070015023 }
15024
15025out:
15026 return val;
15027}
15028
Linus Torvalds1da177e2005-04-16 15:20:36 -070015029static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
15030{
15031 struct tg3_internal_buffer_desc test_desc;
15032 u32 sram_dma_descs;
15033 int i, ret;
15034
15035 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
15036
15037 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
15038 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
15039 tw32(RDMAC_STATUS, 0);
15040 tw32(WDMAC_STATUS, 0);
15041
15042 tw32(BUFMGR_MODE, 0);
15043 tw32(FTQ_RESET, 0);
15044
15045 test_desc.addr_hi = ((u64) buf_dma) >> 32;
15046 test_desc.addr_lo = buf_dma & 0xffffffff;
15047 test_desc.nic_mbuf = 0x00002100;
15048 test_desc.len = size;
15049
15050 /*
15051 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
15052 * the *second* time the tg3 driver was getting loaded after an
15053 * initial scan.
15054 *
15055 * Broadcom tells me:
15056 * ...the DMA engine is connected to the GRC block and a DMA
15057 * reset may affect the GRC block in some unpredictable way...
15058 * The behavior of resets to individual blocks has not been tested.
15059 *
15060 * Broadcom noted the GRC reset will also reset all sub-components.
15061 */
15062 if (to_device) {
15063 test_desc.cqid_sqid = (13 << 8) | 2;
15064
15065 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
15066 udelay(40);
15067 } else {
15068 test_desc.cqid_sqid = (16 << 8) | 7;
15069
15070 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
15071 udelay(40);
15072 }
15073 test_desc.flags = 0x00000005;
15074
15075 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
15076 u32 val;
15077
15078 val = *(((u32 *)&test_desc) + i);
15079 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
15080 sram_dma_descs + (i * sizeof(u32)));
15081 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
15082 }
15083 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
15084
Matt Carlson859a588792010-04-05 10:19:28 +000015085 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015086 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a588792010-04-05 10:19:28 +000015087 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070015088 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015089
15090 ret = -ENODEV;
15091 for (i = 0; i < 40; i++) {
15092 u32 val;
15093
15094 if (to_device)
15095 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
15096 else
15097 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
15098 if ((val & 0xffff) == sram_dma_descs) {
15099 ret = 0;
15100 break;
15101 }
15102
15103 udelay(100);
15104 }
15105
15106 return ret;
15107}
15108
David S. Millerded73402005-05-23 13:59:47 -070015109#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070015110
Matt Carlson41434702011-03-09 16:58:22 +000015111static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080015112 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
15113 { },
15114};
15115
Linus Torvalds1da177e2005-04-16 15:20:36 -070015116static int __devinit tg3_test_dma(struct tg3 *tp)
15117{
15118 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070015119 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000015120 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015121
Matt Carlson4bae65c2010-11-24 08:31:52 +000015122 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
15123 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015124 if (!buf) {
15125 ret = -ENOMEM;
15126 goto out_nofree;
15127 }
15128
15129 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
15130 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
15131
David S. Miller59e6b432005-05-18 22:50:10 -070015132 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015133
Joe Perches63c3a662011-04-26 08:12:10 +000015134 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000015135 goto out;
15136
Joe Perches63c3a662011-04-26 08:12:10 +000015137 if (tg3_flag(tp, PCI_EXPRESS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015138 /* DMA read watermark not used on PCIE */
15139 tp->dma_rwctrl |= 0x00180000;
Joe Perches63c3a662011-04-26 08:12:10 +000015140 } else if (!tg3_flag(tp, PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070015141 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
15142 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015143 tp->dma_rwctrl |= 0x003f0000;
15144 else
15145 tp->dma_rwctrl |= 0x003f000f;
15146 } else {
15147 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
15148 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
15149 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080015150 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015151
Michael Chan4a29cc22006-03-19 13:21:12 -080015152 /* If the 5704 is behind the EPB bridge, we can
15153 * do the less restrictive ONE_DMA workaround for
15154 * better performance.
15155 */
Joe Perches63c3a662011-04-26 08:12:10 +000015156 if (tg3_flag(tp, 40BIT_DMA_BUG) &&
Michael Chan4a29cc22006-03-19 13:21:12 -080015157 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
15158 tp->dma_rwctrl |= 0x8000;
15159 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015160 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
15161
Michael Chan49afdeb2007-02-13 12:17:03 -080015162 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
15163 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070015164 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080015165 tp->dma_rwctrl |=
15166 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
15167 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
15168 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070015169 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
15170 /* 5780 always in PCIX mode */
15171 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070015172 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
15173 /* 5714 always in PCIX mode */
15174 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015175 } else {
15176 tp->dma_rwctrl |= 0x001b000f;
15177 }
15178 }
15179
15180 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
15181 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
15182 tp->dma_rwctrl &= 0xfffffff0;
15183
15184 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
15185 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
15186 /* Remove this if it causes problems for some boards. */
15187 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
15188
15189 /* On 5700/5701 chips, we need to set this bit.
15190 * Otherwise the chip will issue cacheline transactions
15191 * to streamable DMA memory with not all the byte
15192 * enables turned on. This is an error on several
15193 * RISC PCI controllers, in particular sparc64.
15194 *
15195 * On 5703/5704 chips, this bit has been reassigned
15196 * a different meaning. In particular, it is used
15197 * on those chips to enable a PCI-X workaround.
15198 */
15199 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
15200 }
15201
15202 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15203
15204#if 0
15205 /* Unneeded, already done by tg3_get_invariants. */
15206 tg3_switch_clocks(tp);
15207#endif
15208
Linus Torvalds1da177e2005-04-16 15:20:36 -070015209 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
15210 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
15211 goto out;
15212
David S. Miller59e6b432005-05-18 22:50:10 -070015213 /* It is best to perform DMA test with maximum write burst size
15214 * to expose the 5700/5701 write DMA bug.
15215 */
15216 saved_dma_rwctrl = tp->dma_rwctrl;
15217 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
15218 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15219
Linus Torvalds1da177e2005-04-16 15:20:36 -070015220 while (1) {
15221 u32 *p = buf, i;
15222
15223 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
15224 p[i] = i;
15225
15226 /* Send the buffer to the chip. */
15227 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
15228 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000015229 dev_err(&tp->pdev->dev,
15230 "%s: Buffer write failed. err = %d\n",
15231 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015232 break;
15233 }
15234
15235#if 0
15236 /* validate data reached card RAM correctly. */
15237 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
15238 u32 val;
15239 tg3_read_mem(tp, 0x2100 + (i*4), &val);
15240 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000015241 dev_err(&tp->pdev->dev,
15242 "%s: Buffer corrupted on device! "
15243 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015244 /* ret = -ENODEV here? */
15245 }
15246 p[i] = 0;
15247 }
15248#endif
15249 /* Now read it back. */
15250 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
15251 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000015252 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
15253 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015254 break;
15255 }
15256
15257 /* Verify it. */
15258 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
15259 if (p[i] == i)
15260 continue;
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 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015265 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
15266 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15267 break;
15268 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000015269 dev_err(&tp->pdev->dev,
15270 "%s: Buffer corrupted on read back! "
15271 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015272 ret = -ENODEV;
15273 goto out;
15274 }
15275 }
15276
15277 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
15278 /* Success. */
15279 ret = 0;
15280 break;
15281 }
15282 }
David S. Miller59e6b432005-05-18 22:50:10 -070015283 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
15284 DMA_RWCTRL_WRITE_BNDRY_16) {
15285 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070015286 * now look for chipsets that are known to expose the
15287 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070015288 */
Matt Carlson41434702011-03-09 16:58:22 +000015289 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070015290 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
15291 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a588792010-04-05 10:19:28 +000015292 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070015293 /* Safe to use the calculated DMA boundary. */
15294 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a588792010-04-05 10:19:28 +000015295 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070015296
David S. Miller59e6b432005-05-18 22:50:10 -070015297 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015299
15300out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000015301 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015302out_nofree:
15303 return ret;
15304}
15305
Linus Torvalds1da177e2005-04-16 15:20:36 -070015306static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
15307{
Joe Perches63c3a662011-04-26 08:12:10 +000015308 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson666bc832010-01-20 16:58:03 +000015309 tp->bufmgr_config.mbuf_read_dma_low_water =
15310 DEFAULT_MB_RDMA_LOW_WATER_5705;
15311 tp->bufmgr_config.mbuf_mac_rx_low_water =
15312 DEFAULT_MB_MACRX_LOW_WATER_57765;
15313 tp->bufmgr_config.mbuf_high_water =
15314 DEFAULT_MB_HIGH_WATER_57765;
15315
15316 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15317 DEFAULT_MB_RDMA_LOW_WATER_5705;
15318 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15319 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
15320 tp->bufmgr_config.mbuf_high_water_jumbo =
15321 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000015322 } else if (tg3_flag(tp, 5705_PLUS)) {
Michael Chanfdfec1722005-07-25 12:31:48 -070015323 tp->bufmgr_config.mbuf_read_dma_low_water =
15324 DEFAULT_MB_RDMA_LOW_WATER_5705;
15325 tp->bufmgr_config.mbuf_mac_rx_low_water =
15326 DEFAULT_MB_MACRX_LOW_WATER_5705;
15327 tp->bufmgr_config.mbuf_high_water =
15328 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070015329 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
15330 tp->bufmgr_config.mbuf_mac_rx_low_water =
15331 DEFAULT_MB_MACRX_LOW_WATER_5906;
15332 tp->bufmgr_config.mbuf_high_water =
15333 DEFAULT_MB_HIGH_WATER_5906;
15334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015335
Michael Chanfdfec1722005-07-25 12:31:48 -070015336 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15337 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
15338 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15339 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
15340 tp->bufmgr_config.mbuf_high_water_jumbo =
15341 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
15342 } else {
15343 tp->bufmgr_config.mbuf_read_dma_low_water =
15344 DEFAULT_MB_RDMA_LOW_WATER;
15345 tp->bufmgr_config.mbuf_mac_rx_low_water =
15346 DEFAULT_MB_MACRX_LOW_WATER;
15347 tp->bufmgr_config.mbuf_high_water =
15348 DEFAULT_MB_HIGH_WATER;
15349
15350 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15351 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
15352 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15353 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
15354 tp->bufmgr_config.mbuf_high_water_jumbo =
15355 DEFAULT_MB_HIGH_WATER_JUMBO;
15356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015357
15358 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
15359 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
15360}
15361
15362static char * __devinit tg3_phy_string(struct tg3 *tp)
15363{
Matt Carlson79eb6902010-02-17 15:17:03 +000015364 switch (tp->phy_id & TG3_PHY_ID_MASK) {
15365 case TG3_PHY_ID_BCM5400: return "5400";
15366 case TG3_PHY_ID_BCM5401: return "5401";
15367 case TG3_PHY_ID_BCM5411: return "5411";
15368 case TG3_PHY_ID_BCM5701: return "5701";
15369 case TG3_PHY_ID_BCM5703: return "5703";
15370 case TG3_PHY_ID_BCM5704: return "5704";
15371 case TG3_PHY_ID_BCM5705: return "5705";
15372 case TG3_PHY_ID_BCM5750: return "5750";
15373 case TG3_PHY_ID_BCM5752: return "5752";
15374 case TG3_PHY_ID_BCM5714: return "5714";
15375 case TG3_PHY_ID_BCM5780: return "5780";
15376 case TG3_PHY_ID_BCM5755: return "5755";
15377 case TG3_PHY_ID_BCM5787: return "5787";
15378 case TG3_PHY_ID_BCM5784: return "5784";
15379 case TG3_PHY_ID_BCM5756: return "5722/5756";
15380 case TG3_PHY_ID_BCM5906: return "5906";
15381 case TG3_PHY_ID_BCM5761: return "5761";
15382 case TG3_PHY_ID_BCM5718C: return "5718C";
15383 case TG3_PHY_ID_BCM5718S: return "5718S";
15384 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000015385 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000015386 case TG3_PHY_ID_BCM5720C: return "5720C";
Matt Carlson79eb6902010-02-17 15:17:03 +000015387 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070015388 case 0: return "serdes";
15389 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070015390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015391}
15392
Michael Chanf9804dd2005-09-27 12:13:10 -070015393static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
15394{
Joe Perches63c3a662011-04-26 08:12:10 +000015395 if (tg3_flag(tp, PCI_EXPRESS)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070015396 strcpy(str, "PCI Express");
15397 return str;
Joe Perches63c3a662011-04-26 08:12:10 +000015398 } else if (tg3_flag(tp, PCIX_MODE)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070015399 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
15400
15401 strcpy(str, "PCIX:");
15402
15403 if ((clock_ctrl == 7) ||
15404 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
15405 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
15406 strcat(str, "133MHz");
15407 else if (clock_ctrl == 0)
15408 strcat(str, "33MHz");
15409 else if (clock_ctrl == 2)
15410 strcat(str, "50MHz");
15411 else if (clock_ctrl == 4)
15412 strcat(str, "66MHz");
15413 else if (clock_ctrl == 6)
15414 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070015415 } else {
15416 strcpy(str, "PCI:");
Joe Perches63c3a662011-04-26 08:12:10 +000015417 if (tg3_flag(tp, PCI_HIGH_SPEED))
Michael Chanf9804dd2005-09-27 12:13:10 -070015418 strcat(str, "66MHz");
15419 else
15420 strcat(str, "33MHz");
15421 }
Joe Perches63c3a662011-04-26 08:12:10 +000015422 if (tg3_flag(tp, PCI_32BIT))
Michael Chanf9804dd2005-09-27 12:13:10 -070015423 strcat(str, ":32-bit");
15424 else
15425 strcat(str, ":64-bit");
15426 return str;
15427}
15428
David S. Miller15f98502005-05-18 22:49:26 -070015429static void __devinit tg3_init_coal(struct tg3 *tp)
15430{
15431 struct ethtool_coalesce *ec = &tp->coal;
15432
15433 memset(ec, 0, sizeof(*ec));
15434 ec->cmd = ETHTOOL_GCOALESCE;
15435 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
15436 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
15437 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
15438 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
15439 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
15440 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
15441 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
15442 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
15443 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
15444
15445 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
15446 HOSTCC_MODE_CLRTICK_TXBD)) {
15447 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
15448 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
15449 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
15450 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
15451 }
Michael Chand244c892005-07-05 14:42:33 -070015452
Joe Perches63c3a662011-04-26 08:12:10 +000015453 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070015454 ec->rx_coalesce_usecs_irq = 0;
15455 ec->tx_coalesce_usecs_irq = 0;
15456 ec->stats_block_coalesce_usecs = 0;
15457 }
David S. Miller15f98502005-05-18 22:49:26 -070015458}
15459
Linus Torvalds1da177e2005-04-16 15:20:36 -070015460static int __devinit tg3_init_one(struct pci_dev *pdev,
15461 const struct pci_device_id *ent)
15462{
Linus Torvalds1da177e2005-04-16 15:20:36 -070015463 struct net_device *dev;
15464 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000015465 int i, err, pm_cap;
15466 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070015467 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080015468 u64 dma_mask, persist_dma_mask;
Michał Mirosławc8f44af2011-11-15 15:29:55 +000015469 netdev_features_t features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015470
Joe Perches05dbe002010-02-17 19:44:19 +000015471 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015472
15473 err = pci_enable_device(pdev);
15474 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015475 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015476 return err;
15477 }
15478
Linus Torvalds1da177e2005-04-16 15:20:36 -070015479 err = pci_request_regions(pdev, DRV_MODULE_NAME);
15480 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015481 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015482 goto err_out_disable_pdev;
15483 }
15484
15485 pci_set_master(pdev);
15486
15487 /* Find power-management capability. */
15488 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
15489 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000015490 dev_err(&pdev->dev,
15491 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015492 err = -EIO;
15493 goto err_out_free_res;
15494 }
15495
Matt Carlson16821282011-07-13 09:27:28 +000015496 err = pci_set_power_state(pdev, PCI_D0);
15497 if (err) {
15498 dev_err(&pdev->dev, "Transition to D0 failed, aborting\n");
15499 goto err_out_free_res;
15500 }
15501
Matt Carlsonfe5f5782009-09-01 13:09:39 +000015502 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015503 if (!dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015504 err = -ENOMEM;
Matt Carlson16821282011-07-13 09:27:28 +000015505 goto err_out_power_down;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015506 }
15507
Linus Torvalds1da177e2005-04-16 15:20:36 -070015508 SET_NETDEV_DEV(dev, &pdev->dev);
15509
Linus Torvalds1da177e2005-04-16 15:20:36 -070015510 tp = netdev_priv(dev);
15511 tp->pdev = pdev;
15512 tp->dev = dev;
15513 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015514 tp->rx_mode = TG3_DEF_RX_MODE;
15515 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070015516
Linus Torvalds1da177e2005-04-16 15:20:36 -070015517 if (tg3_debug > 0)
15518 tp->msg_enable = tg3_debug;
15519 else
15520 tp->msg_enable = TG3_DEF_MSG_ENABLE;
15521
15522 /* The word/byte swap controls here control register access byte
15523 * swapping. DMA data byte swapping is controlled in the GRC_MODE
15524 * setting below.
15525 */
15526 tp->misc_host_ctrl =
15527 MISC_HOST_CTRL_MASK_PCI_INT |
15528 MISC_HOST_CTRL_WORD_SWAP |
15529 MISC_HOST_CTRL_INDIR_ACCESS |
15530 MISC_HOST_CTRL_PCISTATE_RW;
15531
15532 /* The NONFRM (non-frame) byte/word swap controls take effect
15533 * on descriptor entries, anything which isn't packet data.
15534 *
15535 * The StrongARM chips on the board (one for tx, one for rx)
15536 * are running in big-endian mode.
15537 */
15538 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
15539 GRC_MODE_WSWAP_NONFRM_DATA);
15540#ifdef __BIG_ENDIAN
15541 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
15542#endif
15543 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015544 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000015545 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015546
Matt Carlsond5fe4882008-11-21 17:20:32 -080015547 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010015548 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015549 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015550 err = -ENOMEM;
15551 goto err_out_free_dev;
15552 }
15553
Matt Carlsonc9cab242011-07-13 09:27:27 +000015554 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
15555 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761E ||
15556 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S ||
15557 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761SE ||
15558 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
15559 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
15560 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
15561 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720) {
15562 tg3_flag_set(tp, ENABLE_APE);
15563 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
15564 if (!tp->aperegs) {
15565 dev_err(&pdev->dev,
15566 "Cannot map APE registers, aborting\n");
15567 err = -ENOMEM;
15568 goto err_out_iounmap;
15569 }
15570 }
15571
Linus Torvalds1da177e2005-04-16 15:20:36 -070015572 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
15573 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015574
Linus Torvalds1da177e2005-04-16 15:20:36 -070015575 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015576 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Matt Carlson2ffcc982011-05-19 12:12:44 +000015577 dev->netdev_ops = &tg3_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015578 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015579
15580 err = tg3_get_invariants(tp);
15581 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015582 dev_err(&pdev->dev,
15583 "Problem fetching invariants of chip, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015584 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015585 }
15586
Michael Chan4a29cc22006-03-19 13:21:12 -080015587 /* The EPB bridge inside 5714, 5715, and 5780 and any
15588 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080015589 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
15590 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
15591 * do DMA address check in tg3_start_xmit().
15592 */
Joe Perches63c3a662011-04-26 08:12:10 +000015593 if (tg3_flag(tp, IS_5788))
Yang Hongyang284901a2009-04-06 19:01:15 -070015594 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Joe Perches63c3a662011-04-26 08:12:10 +000015595 else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070015596 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080015597#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070015598 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015599#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080015600 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070015601 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015602
15603 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070015604 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080015605 err = pci_set_dma_mask(pdev, dma_mask);
15606 if (!err) {
Matt Carlson0da06062011-05-19 12:12:53 +000015607 features |= NETIF_F_HIGHDMA;
Michael Chan72f2afb2006-03-06 19:28:35 -080015608 err = pci_set_consistent_dma_mask(pdev,
15609 persist_dma_mask);
15610 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015611 dev_err(&pdev->dev, "Unable to obtain 64 bit "
15612 "DMA for consistent allocations\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015613 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080015614 }
15615 }
15616 }
Yang Hongyang284901a2009-04-06 19:01:15 -070015617 if (err || dma_mask == DMA_BIT_MASK(32)) {
15618 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080015619 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015620 dev_err(&pdev->dev,
15621 "No usable DMA configuration, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015622 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080015623 }
15624 }
15625
Michael Chanfdfec1722005-07-25 12:31:48 -070015626 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015627
Matt Carlson0da06062011-05-19 12:12:53 +000015628 features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
15629
15630 /* 5700 B0 chips do not support checksumming correctly due
15631 * to hardware bugs.
15632 */
15633 if (tp->pci_chip_rev_id != CHIPREV_ID_5700_B0) {
15634 features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
15635
15636 if (tg3_flag(tp, 5755_PLUS))
15637 features |= NETIF_F_IPV6_CSUM;
15638 }
15639
Michael Chan4e3a7aa2006-03-20 17:47:44 -080015640 /* TSO is on by default on chips that support hardware TSO.
15641 * Firmware TSO on older chips gives lower performance, so it
15642 * is off by default, but can be enabled using ethtool.
15643 */
Joe Perches63c3a662011-04-26 08:12:10 +000015644 if ((tg3_flag(tp, HW_TSO_1) ||
15645 tg3_flag(tp, HW_TSO_2) ||
15646 tg3_flag(tp, HW_TSO_3)) &&
Matt Carlson0da06062011-05-19 12:12:53 +000015647 (features & NETIF_F_IP_CSUM))
15648 features |= NETIF_F_TSO;
Joe Perches63c3a662011-04-26 08:12:10 +000015649 if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
Matt Carlson0da06062011-05-19 12:12:53 +000015650 if (features & NETIF_F_IPV6_CSUM)
15651 features |= NETIF_F_TSO6;
Joe Perches63c3a662011-04-26 08:12:10 +000015652 if (tg3_flag(tp, HW_TSO_3) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000015653 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070015654 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
15655 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Joe Perches63c3a662011-04-26 08:12:10 +000015656 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Michał Mirosławdc668912011-04-07 03:35:07 +000015657 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson0da06062011-05-19 12:12:53 +000015658 features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070015659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015660
Matt Carlsond542fe22011-05-19 16:02:43 +000015661 dev->features |= features;
15662 dev->vlan_features |= features;
15663
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015664 /*
15665 * Add loopback capability only for a subset of devices that support
15666 * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
15667 * loopback for the remaining devices.
15668 */
15669 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
15670 !tg3_flag(tp, CPMU_PRESENT))
15671 /* Add the loopback capability */
Matt Carlson0da06062011-05-19 12:12:53 +000015672 features |= NETIF_F_LOOPBACK;
15673
Matt Carlson0da06062011-05-19 12:12:53 +000015674 dev->hw_features |= features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015675
Linus Torvalds1da177e2005-04-16 15:20:36 -070015676 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015677 !tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015678 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015679 tg3_flag_set(tp, MAX_RXPEND_64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015680 tp->rx_pending = 63;
15681 }
15682
Linus Torvalds1da177e2005-04-16 15:20:36 -070015683 err = tg3_get_device_address(tp);
15684 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015685 dev_err(&pdev->dev,
15686 "Could not obtain valid ethernet address, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015687 goto err_out_apeunmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070015688 }
15689
Matt Carlsonc88864d2007-11-12 21:07:01 -080015690 /*
15691 * Reset chip in case UNDI or EFI driver did not shutdown
15692 * DMA self test will enable WDMAC and we'll see (spurious)
15693 * pending DMA on the PCI bus at that point.
15694 */
15695 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
15696 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
15697 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
15698 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
15699 }
15700
15701 err = tg3_test_dma(tp);
15702 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015703 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080015704 goto err_out_apeunmap;
15705 }
15706
Matt Carlson78f90dc2009-11-13 13:03:42 +000015707 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
15708 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
15709 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000015710 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000015711 struct tg3_napi *tnapi = &tp->napi[i];
15712
15713 tnapi->tp = tp;
15714 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
15715
15716 tnapi->int_mbox = intmbx;
Matt Carlson93a700a2011-08-31 11:44:54 +000015717 if (i <= 4)
Matt Carlson78f90dc2009-11-13 13:03:42 +000015718 intmbx += 0x8;
15719 else
15720 intmbx += 0x4;
15721
15722 tnapi->consmbox = rcvmbx;
15723 tnapi->prodmbox = sndmbx;
15724
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015725 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000015726 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015727 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000015728 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000015729
Joe Perches63c3a662011-04-26 08:12:10 +000015730 if (!tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson78f90dc2009-11-13 13:03:42 +000015731 break;
15732
15733 /*
15734 * If we support MSIX, we'll be using RSS. If we're using
15735 * RSS, the first vector only handles link interrupts and the
15736 * remaining vectors handle rx and tx interrupts. Reuse the
15737 * mailbox values for the next iteration. The values we setup
15738 * above are still useful for the single vectored mode.
15739 */
15740 if (!i)
15741 continue;
15742
15743 rcvmbx += 0x8;
15744
15745 if (sndmbx & 0x4)
15746 sndmbx -= 0x4;
15747 else
15748 sndmbx += 0xc;
15749 }
15750
Matt Carlsonc88864d2007-11-12 21:07:01 -080015751 tg3_init_coal(tp);
15752
Michael Chanc49a1562006-12-17 17:07:29 -080015753 pci_set_drvdata(pdev, dev);
15754
Matt Carlsoncd0d7222011-07-13 09:27:33 +000015755 if (tg3_flag(tp, 5717_PLUS)) {
15756 /* Resume a low-power mode */
15757 tg3_frob_aux_power(tp, false);
15758 }
15759
Matt Carlson21f76382012-02-22 12:35:21 +000015760 tg3_timer_init(tp);
15761
Linus Torvalds1da177e2005-04-16 15:20:36 -070015762 err = register_netdev(dev);
15763 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015764 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070015765 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015766 }
15767
Joe Perches05dbe002010-02-17 19:44:19 +000015768 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
15769 tp->board_part_number,
15770 tp->pci_chip_rev_id,
15771 tg3_bus_string(tp, str),
15772 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015773
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015774 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000015775 struct phy_device *phydev;
15776 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000015777 netdev_info(dev,
15778 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000015779 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015780 } else {
15781 char *ethtype;
15782
15783 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
15784 ethtype = "10/100Base-TX";
15785 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
15786 ethtype = "1000Base-SX";
15787 else
15788 ethtype = "10/100/1000Base-T";
15789
Matt Carlson5129c3a2010-04-05 10:19:23 +000015790 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlson47007832011-04-20 07:57:43 +000015791 "(WireSpeed[%d], EEE[%d])\n",
15792 tg3_phy_string(tp), ethtype,
15793 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
15794 (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015795 }
Matt Carlsondf59c942008-11-03 16:52:56 -080015796
Joe Perches05dbe002010-02-17 19:44:19 +000015797 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000015798 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015799 tg3_flag(tp, USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015800 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015801 tg3_flag(tp, ENABLE_ASF) != 0,
15802 tg3_flag(tp, TSO_CAPABLE) != 0);
Joe Perches05dbe002010-02-17 19:44:19 +000015803 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
15804 tp->dma_rwctrl,
15805 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
15806 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015807
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015808 pci_save_state(pdev);
15809
Linus Torvalds1da177e2005-04-16 15:20:36 -070015810 return 0;
15811
Matt Carlson0d3031d2007-10-10 18:02:43 -070015812err_out_apeunmap:
15813 if (tp->aperegs) {
15814 iounmap(tp->aperegs);
15815 tp->aperegs = NULL;
15816 }
15817
Linus Torvalds1da177e2005-04-16 15:20:36 -070015818err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070015819 if (tp->regs) {
15820 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015821 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015823
15824err_out_free_dev:
15825 free_netdev(dev);
15826
Matt Carlson16821282011-07-13 09:27:28 +000015827err_out_power_down:
15828 pci_set_power_state(pdev, PCI_D3hot);
15829
Linus Torvalds1da177e2005-04-16 15:20:36 -070015830err_out_free_res:
15831 pci_release_regions(pdev);
15832
15833err_out_disable_pdev:
15834 pci_disable_device(pdev);
15835 pci_set_drvdata(pdev, NULL);
15836 return err;
15837}
15838
15839static void __devexit tg3_remove_one(struct pci_dev *pdev)
15840{
15841 struct net_device *dev = pci_get_drvdata(pdev);
15842
15843 if (dev) {
15844 struct tg3 *tp = netdev_priv(dev);
15845
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080015846 if (tp->fw)
15847 release_firmware(tp->fw);
15848
Matt Carlsondb219972011-11-04 09:15:03 +000015849 tg3_reset_task_cancel(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015850
David S. Miller1805b2f2011-10-24 18:18:09 -040015851 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015852 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015853 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015854 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070015855
Linus Torvalds1da177e2005-04-16 15:20:36 -070015856 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015857 if (tp->aperegs) {
15858 iounmap(tp->aperegs);
15859 tp->aperegs = NULL;
15860 }
Michael Chan68929142005-08-09 20:17:14 -070015861 if (tp->regs) {
15862 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015863 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015864 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015865 free_netdev(dev);
15866 pci_release_regions(pdev);
15867 pci_disable_device(pdev);
15868 pci_set_drvdata(pdev, NULL);
15869 }
15870}
15871
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015872#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015873static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015874{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015875 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015876 struct net_device *dev = pci_get_drvdata(pdev);
15877 struct tg3 *tp = netdev_priv(dev);
15878 int err;
15879
15880 if (!netif_running(dev))
15881 return 0;
15882
Matt Carlsondb219972011-11-04 09:15:03 +000015883 tg3_reset_task_cancel(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015884 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015885 tg3_netif_stop(tp);
15886
Matt Carlson21f76382012-02-22 12:35:21 +000015887 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015888
David S. Millerf47c11e2005-06-24 20:18:35 -070015889 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015890 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070015891 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015892
15893 netif_device_detach(dev);
15894
David S. Millerf47c11e2005-06-24 20:18:35 -070015895 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070015896 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches63c3a662011-04-26 08:12:10 +000015897 tg3_flag_clear(tp, INIT_COMPLETE);
David S. Millerf47c11e2005-06-24 20:18:35 -070015898 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015899
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015900 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015901 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015902 int err2;
15903
David S. Millerf47c11e2005-06-24 20:18:35 -070015904 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015905
Joe Perches63c3a662011-04-26 08:12:10 +000015906 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015907 err2 = tg3_restart_hw(tp, 1);
15908 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070015909 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015910
Matt Carlson21f76382012-02-22 12:35:21 +000015911 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015912
15913 netif_device_attach(dev);
15914 tg3_netif_start(tp);
15915
Michael Chanb9ec6c12006-07-25 16:37:27 -070015916out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015917 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015918
15919 if (!err2)
15920 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015921 }
15922
15923 return err;
15924}
15925
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015926static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015927{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015928 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015929 struct net_device *dev = pci_get_drvdata(pdev);
15930 struct tg3 *tp = netdev_priv(dev);
15931 int err;
15932
15933 if (!netif_running(dev))
15934 return 0;
15935
Linus Torvalds1da177e2005-04-16 15:20:36 -070015936 netif_device_attach(dev);
15937
David S. Millerf47c11e2005-06-24 20:18:35 -070015938 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015939
Joe Perches63c3a662011-04-26 08:12:10 +000015940 tg3_flag_set(tp, INIT_COMPLETE);
Michael Chanb9ec6c12006-07-25 16:37:27 -070015941 err = tg3_restart_hw(tp, 1);
15942 if (err)
15943 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015944
Matt Carlson21f76382012-02-22 12:35:21 +000015945 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015946
Linus Torvalds1da177e2005-04-16 15:20:36 -070015947 tg3_netif_start(tp);
15948
Michael Chanb9ec6c12006-07-25 16:37:27 -070015949out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015950 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015951
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015952 if (!err)
15953 tg3_phy_start(tp);
15954
Michael Chanb9ec6c12006-07-25 16:37:27 -070015955 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015956}
15957
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015958static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015959#define TG3_PM_OPS (&tg3_pm_ops)
15960
15961#else
15962
15963#define TG3_PM_OPS NULL
15964
15965#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015966
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015967/**
15968 * tg3_io_error_detected - called when PCI error is detected
15969 * @pdev: Pointer to PCI device
15970 * @state: The current pci connection state
15971 *
15972 * This function is called after a PCI bus error affecting
15973 * this device has been detected.
15974 */
15975static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
15976 pci_channel_state_t state)
15977{
15978 struct net_device *netdev = pci_get_drvdata(pdev);
15979 struct tg3 *tp = netdev_priv(netdev);
15980 pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
15981
15982 netdev_info(netdev, "PCI I/O error detected\n");
15983
15984 rtnl_lock();
15985
15986 if (!netif_running(netdev))
15987 goto done;
15988
15989 tg3_phy_stop(tp);
15990
15991 tg3_netif_stop(tp);
15992
Matt Carlson21f76382012-02-22 12:35:21 +000015993 tg3_timer_stop(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015994
15995 /* Want to make sure that the reset task doesn't run */
Matt Carlsondb219972011-11-04 09:15:03 +000015996 tg3_reset_task_cancel(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015997
15998 netif_device_detach(netdev);
15999
16000 /* Clean up software state, even if MMIO is blocked */
16001 tg3_full_lock(tp, 0);
16002 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
16003 tg3_full_unlock(tp);
16004
16005done:
16006 if (state == pci_channel_io_perm_failure)
16007 err = PCI_ERS_RESULT_DISCONNECT;
16008 else
16009 pci_disable_device(pdev);
16010
16011 rtnl_unlock();
16012
16013 return err;
16014}
16015
16016/**
16017 * tg3_io_slot_reset - called after the pci bus has been reset.
16018 * @pdev: Pointer to PCI device
16019 *
16020 * Restart the card from scratch, as if from a cold-boot.
16021 * At this point, the card has exprienced a hard reset,
16022 * followed by fixups by BIOS, and has its config space
16023 * set up identically to what it was at cold boot.
16024 */
16025static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
16026{
16027 struct net_device *netdev = pci_get_drvdata(pdev);
16028 struct tg3 *tp = netdev_priv(netdev);
16029 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
16030 int err;
16031
16032 rtnl_lock();
16033
16034 if (pci_enable_device(pdev)) {
16035 netdev_err(netdev, "Cannot re-enable PCI device after reset.\n");
16036 goto done;
16037 }
16038
16039 pci_set_master(pdev);
16040 pci_restore_state(pdev);
16041 pci_save_state(pdev);
16042
16043 if (!netif_running(netdev)) {
16044 rc = PCI_ERS_RESULT_RECOVERED;
16045 goto done;
16046 }
16047
16048 err = tg3_power_up(tp);
Matt Carlsonbed98292011-07-13 09:27:29 +000016049 if (err)
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016050 goto done;
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016051
16052 rc = PCI_ERS_RESULT_RECOVERED;
16053
16054done:
16055 rtnl_unlock();
16056
16057 return rc;
16058}
16059
16060/**
16061 * tg3_io_resume - called when traffic can start flowing again.
16062 * @pdev: Pointer to PCI device
16063 *
16064 * This callback is called when the error recovery driver tells
16065 * us that its OK to resume normal operation.
16066 */
16067static void tg3_io_resume(struct pci_dev *pdev)
16068{
16069 struct net_device *netdev = pci_get_drvdata(pdev);
16070 struct tg3 *tp = netdev_priv(netdev);
16071 int err;
16072
16073 rtnl_lock();
16074
16075 if (!netif_running(netdev))
16076 goto done;
16077
16078 tg3_full_lock(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +000016079 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016080 err = tg3_restart_hw(tp, 1);
16081 tg3_full_unlock(tp);
16082 if (err) {
16083 netdev_err(netdev, "Cannot restart hardware after reset.\n");
16084 goto done;
16085 }
16086
16087 netif_device_attach(netdev);
16088
Matt Carlson21f76382012-02-22 12:35:21 +000016089 tg3_timer_start(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016090
16091 tg3_netif_start(tp);
16092
16093 tg3_phy_start(tp);
16094
16095done:
16096 rtnl_unlock();
16097}
16098
16099static struct pci_error_handlers tg3_err_handler = {
16100 .error_detected = tg3_io_error_detected,
16101 .slot_reset = tg3_io_slot_reset,
16102 .resume = tg3_io_resume
16103};
16104
Linus Torvalds1da177e2005-04-16 15:20:36 -070016105static struct pci_driver tg3_driver = {
16106 .name = DRV_MODULE_NAME,
16107 .id_table = tg3_pci_tbl,
16108 .probe = tg3_init_one,
16109 .remove = __devexit_p(tg3_remove_one),
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016110 .err_handler = &tg3_err_handler,
Eric Dumazetaa6027c2011-01-01 05:22:46 +000016111 .driver.pm = TG3_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070016112};
16113
16114static int __init tg3_init(void)
16115{
Jeff Garzik29917622006-08-19 17:48:59 -040016116 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016117}
16118
16119static void __exit tg3_cleanup(void)
16120{
16121 pci_unregister_driver(&tg3_driver);
16122}
16123
16124module_init(tg3_init);
16125module_exit(tg3_cleanup);