blob: ec5bc3d04333a5ebbaeb5949c124ea93f2084883 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * tg3.c: Broadcom Tigon3 ethernet driver.
3 *
4 * Copyright (C) 2001, 2002, 2003, 2004 David S. Miller (davem@redhat.com)
5 * Copyright (C) 2001, 2002, 2003 Jeff Garzik (jgarzik@pobox.com)
6 * Copyright (C) 2004 Sun Microsystems Inc.
Matt Carlson9e056c02012-02-13 15:20:17 +00007 * Copyright (C) 2005-2012 Broadcom Corporation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * Firmware is:
Michael Chan49cabf42005-06-06 15:15:17 -070010 * Derived from proprietary unpublished source code,
11 * Copyright (C) 2000-2003 Broadcom Corporation.
12 *
13 * Permission is hereby granted for the distribution of this firmware
14 * data in hexadecimal or equivalent format, provided this copyright
15 * notice is accompanying it.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 */
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#include <linux/module.h>
20#include <linux/moduleparam.h>
Matt Carlson6867c842010-07-11 09:31:44 +000021#include <linux/stringify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/kernel.h>
23#include <linux/types.h>
24#include <linux/compiler.h>
25#include <linux/slab.h>
26#include <linux/delay.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020027#include <linux/in.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/init.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000029#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/ioport.h>
31#include <linux/pci.h>
32#include <linux/netdevice.h>
33#include <linux/etherdevice.h>
34#include <linux/skbuff.h>
35#include <linux/ethtool.h>
Matt Carlson3110f5f52010-12-06 08:28:50 +000036#include <linux/mdio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/mii.h>
Matt Carlson158d7ab2008-05-29 01:37:54 -070038#include <linux/phy.h>
Matt Carlsona9daf362008-05-25 23:49:44 -070039#include <linux/brcmphy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/if_vlan.h>
41#include <linux/ip.h>
42#include <linux/tcp.h>
43#include <linux/workqueue.h>
Michael Chan61487482005-09-05 17:53:19 -070044#include <linux/prefetch.h>
Tobias Klauserf9a5f7d2005-10-29 15:09:26 +020045#include <linux/dma-mapping.h>
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080046#include <linux/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#include <net/checksum.h>
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030049#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51#include <asm/system.h>
Javier Martinez Canillas27fd9de2011-03-26 16:42:31 +000052#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <asm/byteorder.h>
Javier Martinez Canillas27fd9de2011-03-26 16:42:31 +000054#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
David S. Miller49b6e95f2007-03-29 01:38:42 -070056#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <asm/idprom.h>
David S. Miller49b6e95f2007-03-29 01:38:42 -070058#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#endif
60
Matt Carlson63532392008-11-03 16:49:57 -080061#define BAR_0 0
62#define BAR_2 2
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include "tg3.h"
65
Joe Perches63c3a662011-04-26 08:12:10 +000066/* Functions & macros to verify TG3_FLAGS types */
67
68static inline int _tg3_flag(enum TG3_FLAGS flag, unsigned long *bits)
69{
70 return test_bit(flag, bits);
71}
72
73static inline void _tg3_flag_set(enum TG3_FLAGS flag, unsigned long *bits)
74{
75 set_bit(flag, bits);
76}
77
78static inline void _tg3_flag_clear(enum TG3_FLAGS flag, unsigned long *bits)
79{
80 clear_bit(flag, bits);
81}
82
83#define tg3_flag(tp, flag) \
84 _tg3_flag(TG3_FLAG_##flag, (tp)->tg3_flags)
85#define tg3_flag_set(tp, flag) \
86 _tg3_flag_set(TG3_FLAG_##flag, (tp)->tg3_flags)
87#define tg3_flag_clear(tp, flag) \
88 _tg3_flag_clear(TG3_FLAG_##flag, (tp)->tg3_flags)
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#define DRV_MODULE_NAME "tg3"
Matt Carlson6867c842010-07-11 09:31:44 +000091#define TG3_MAJ_NUM 3
Matt Carlsonefab79c2011-12-08 14:40:18 +000092#define TG3_MIN_NUM 122
Matt Carlson6867c842010-07-11 09:31:44 +000093#define DRV_MODULE_VERSION \
94 __stringify(TG3_MAJ_NUM) "." __stringify(TG3_MIN_NUM)
Matt Carlsonefab79c2011-12-08 14:40:18 +000095#define DRV_MODULE_RELDATE "December 7, 2011"
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Matt Carlsonfd6d3f02011-08-31 11:44:52 +000097#define RESET_KIND_SHUTDOWN 0
98#define RESET_KIND_INIT 1
99#define RESET_KIND_SUSPEND 2
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101#define TG3_DEF_RX_MODE 0
102#define TG3_DEF_TX_MODE 0
103#define TG3_DEF_MSG_ENABLE \
104 (NETIF_MSG_DRV | \
105 NETIF_MSG_PROBE | \
106 NETIF_MSG_LINK | \
107 NETIF_MSG_TIMER | \
108 NETIF_MSG_IFDOWN | \
109 NETIF_MSG_IFUP | \
110 NETIF_MSG_RX_ERR | \
111 NETIF_MSG_TX_ERR)
112
Matt Carlson520b2752011-06-13 13:39:02 +0000113#define TG3_GRC_LCLCTL_PWRSW_DELAY 100
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115/* length of time before we decide the hardware is borked,
116 * and dev->tx_timeout() should be called to fix the problem
117 */
Joe Perches63c3a662011-04-26 08:12:10 +0000118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119#define TG3_TX_TIMEOUT (5 * HZ)
120
121/* hardware minimum and maximum for a single frame's data payload */
122#define TG3_MIN_MTU 60
123#define TG3_MAX_MTU(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000124 (tg3_flag(tp, JUMBO_CAPABLE) ? 9000 : 1500)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126/* These numbers seem to be hard coded in the NIC firmware somehow.
127 * You can't change the ring sizes, but you can change where you place
128 * them in the NIC onboard memory.
129 */
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000130#define TG3_RX_STD_RING_SIZE(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000131 (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000132 TG3_RX_STD_MAX_SIZE_5717 : TG3_RX_STD_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133#define TG3_DEF_RX_RING_PENDING 200
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000134#define TG3_RX_JMB_RING_SIZE(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000135 (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000136 TG3_RX_JMB_MAX_SIZE_5717 : TG3_RX_JMB_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137#define TG3_DEF_RX_JUMBO_RING_PENDING 100
138
139/* Do not place this n-ring entries value into the tp struct itself,
140 * we really want to expose these constants to GCC so that modulo et
141 * al. operations are done with shifts and masks instead of with
142 * hw multiply/modulo instructions. Another solution would be to
143 * replace things like '% foo' with '& (foo - 1)'.
144 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146#define TG3_TX_RING_SIZE 512
147#define TG3_DEF_TX_RING_PENDING (TG3_TX_RING_SIZE - 1)
148
Matt Carlson2c49a442010-09-30 10:34:35 +0000149#define TG3_RX_STD_RING_BYTES(tp) \
150 (sizeof(struct tg3_rx_buffer_desc) * TG3_RX_STD_RING_SIZE(tp))
151#define TG3_RX_JMB_RING_BYTES(tp) \
152 (sizeof(struct tg3_ext_rx_buffer_desc) * TG3_RX_JMB_RING_SIZE(tp))
153#define TG3_RX_RCB_RING_BYTES(tp) \
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000154 (sizeof(struct tg3_rx_buffer_desc) * (tp->rx_ret_ring_mask + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155#define TG3_TX_RING_BYTES (sizeof(struct tg3_tx_buffer_desc) * \
156 TG3_TX_RING_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157#define NEXT_TX(N) (((N) + 1) & (TG3_TX_RING_SIZE - 1))
158
Matt Carlson287be122009-08-28 13:58:46 +0000159#define TG3_DMA_BYTE_ENAB 64
160
161#define TG3_RX_STD_DMA_SZ 1536
162#define TG3_RX_JMB_DMA_SZ 9046
163
164#define TG3_RX_DMA_TO_MAP_SZ(x) ((x) + TG3_DMA_BYTE_ENAB)
165
166#define TG3_RX_STD_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_STD_DMA_SZ)
167#define TG3_RX_JMB_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_JMB_DMA_SZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Matt Carlson2c49a442010-09-30 10:34:35 +0000169#define TG3_RX_STD_BUFF_RING_SIZE(tp) \
170 (sizeof(struct ring_info) * TG3_RX_STD_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000171
Matt Carlson2c49a442010-09-30 10:34:35 +0000172#define TG3_RX_JMB_BUFF_RING_SIZE(tp) \
173 (sizeof(struct ring_info) * TG3_RX_JMB_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000174
Matt Carlsond2757fc2010-04-12 06:58:27 +0000175/* Due to a hardware bug, the 5701 can only DMA to memory addresses
176 * that are at least dword aligned when used in PCIX mode. The driver
177 * works around this bug by double copying the packet. This workaround
178 * is built into the normal double copy length check for efficiency.
179 *
180 * However, the double copy is only necessary on those architectures
181 * where unaligned memory accesses are inefficient. For those architectures
182 * where unaligned memory accesses incur little penalty, we can reintegrate
183 * the 5701 in the normal rx path. Doing so saves a device structure
184 * dereference by hardcoding the double copy threshold in place.
185 */
186#define TG3_RX_COPY_THRESHOLD 256
187#if NET_IP_ALIGN == 0 || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
188 #define TG3_RX_COPY_THRESH(tp) TG3_RX_COPY_THRESHOLD
189#else
190 #define TG3_RX_COPY_THRESH(tp) ((tp)->rx_copy_thresh)
191#endif
192
Matt Carlson81389f52011-08-31 11:44:49 +0000193#if (NET_IP_ALIGN != 0)
194#define TG3_RX_OFFSET(tp) ((tp)->rx_offset)
195#else
Eric Dumazet9205fd92011-11-18 06:47:01 +0000196#define TG3_RX_OFFSET(tp) (NET_SKB_PAD)
Matt Carlson81389f52011-08-31 11:44:49 +0000197#endif
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199/* minimum number of free TX descriptors required to wake up TX process */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000200#define TG3_TX_WAKEUP_THRESH(tnapi) ((tnapi)->tx_pending / 4)
Matt Carlson55086ad2011-12-14 11:09:59 +0000201#define TG3_TX_BD_DMA_MAX_2K 2048
Matt Carlsona4cb4282011-12-14 11:09:58 +0000202#define TG3_TX_BD_DMA_MAX_4K 4096
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Matt Carlsonad829262008-11-21 17:16:16 -0800204#define TG3_RAW_IP_ALIGN 2
205
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000206#define TG3_FW_UPDATE_TIMEOUT_SEC 5
Matt Carlson21f76382012-02-22 12:35:21 +0000207#define TG3_FW_UPDATE_FREQ_SEC (TG3_FW_UPDATE_TIMEOUT_SEC / 2)
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000208
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800209#define FIRMWARE_TG3 "tigon/tg3.bin"
210#define FIRMWARE_TG3TSO "tigon/tg3_tso.bin"
211#define FIRMWARE_TG3TSO5 "tigon/tg3_tso5.bin"
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213static char version[] __devinitdata =
Joe Perches05dbe002010-02-17 19:44:19 +0000214 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216MODULE_AUTHOR("David S. Miller (davem@redhat.com) and Jeff Garzik (jgarzik@pobox.com)");
217MODULE_DESCRIPTION("Broadcom Tigon3 ethernet driver");
218MODULE_LICENSE("GPL");
219MODULE_VERSION(DRV_MODULE_VERSION);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800220MODULE_FIRMWARE(FIRMWARE_TG3);
221MODULE_FIRMWARE(FIRMWARE_TG3TSO);
222MODULE_FIRMWARE(FIRMWARE_TG3TSO5);
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224static int tg3_debug = -1; /* -1 == use TG3_DEF_MSG_ENABLE as value */
225module_param(tg3_debug, int, 0);
226MODULE_PARM_DESC(tg3_debug, "Tigon3 bitmapped debugging message enable value");
227
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000228static DEFINE_PCI_DEVICE_TABLE(tg3_pci_tbl) = {
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700229 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5700)},
230 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5701)},
231 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702)},
232 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703)},
233 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704)},
234 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702FE)},
235 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705)},
236 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705_2)},
237 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M)},
238 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M_2)},
239 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702X)},
240 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703X)},
241 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S)},
242 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702A3)},
243 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703A3)},
244 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5782)},
245 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5788)},
246 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5789)},
247 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901)},
248 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901_2)},
249 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S_2)},
250 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705F)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700251 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5721)},
Michael Chan126a3362006-09-27 16:03:07 -0700252 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5722)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700253 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700254 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751M)},
255 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751F)},
256 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752)},
257 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752M)},
258 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753)},
259 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753M)},
260 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753F)},
261 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754)},
262 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754M)},
263 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755)},
264 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755M)},
Michael Chan126a3362006-09-27 16:03:07 -0700265 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5756)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700266 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5786)},
267 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787)},
268 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787M)},
Michael Chan676917d2006-12-07 00:20:22 -0800269 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787F)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700270 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714)},
271 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714S)},
272 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715)},
273 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715S)},
274 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780)},
275 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780S)},
276 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5781)},
Michael Chanb5d37722006-09-27 16:06:21 -0700277 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906)},
278 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906M)},
Matt Carlsond30cdd22007-10-07 23:28:35 -0700279 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5784)},
280 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5764)},
Matt Carlson6c7af272007-10-21 16:12:02 -0700281 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5723)},
Matt Carlson9936bcf2007-10-10 18:03:07 -0700282 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761)},
283 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761E)},
Matt Carlsonc88e6682008-11-03 16:49:18 -0800284 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761S)},
285 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761SE)},
Matt Carlson2befdce2009-08-28 12:28:45 +0000286 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_G)},
287 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_F)},
Matt Carlson321d32a2008-11-21 17:22:19 -0800288 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780)},
289 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57760)},
290 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57790)},
Matt Carlson5e7ccf22009-08-25 10:08:42 +0000291 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57788)},
Matt Carlson5001e2f2009-11-13 13:03:51 +0000292 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717)},
293 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5718)},
Matt Carlsonb0f75222010-01-20 16:58:11 +0000294 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57781)},
295 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57785)},
296 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57761)},
297 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57765)},
298 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57791)},
299 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57795)},
Matt Carlson302b5002010-06-05 17:24:38 +0000300 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5719)},
Matt Carlsonba1f3c72011-04-05 14:22:50 +0000301 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5720)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700302 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9DXX)},
303 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9MXX)},
304 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1000)},
305 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1001)},
306 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1003)},
307 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC9100)},
308 {PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_TIGON3)},
Meelis Roos1dcb14d2011-05-25 05:43:47 +0000309 {PCI_DEVICE(0x10cf, 0x11a2)}, /* Fujitsu 1000base-SX with BCM5703SKHB */
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700310 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311};
312
313MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
314
Andreas Mohr50da8592006-08-14 23:54:30 -0700315static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000317} ethtool_stats_keys[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 { "rx_octets" },
319 { "rx_fragments" },
320 { "rx_ucast_packets" },
321 { "rx_mcast_packets" },
322 { "rx_bcast_packets" },
323 { "rx_fcs_errors" },
324 { "rx_align_errors" },
325 { "rx_xon_pause_rcvd" },
326 { "rx_xoff_pause_rcvd" },
327 { "rx_mac_ctrl_rcvd" },
328 { "rx_xoff_entered" },
329 { "rx_frame_too_long_errors" },
330 { "rx_jabbers" },
331 { "rx_undersize_packets" },
332 { "rx_in_length_errors" },
333 { "rx_out_length_errors" },
334 { "rx_64_or_less_octet_packets" },
335 { "rx_65_to_127_octet_packets" },
336 { "rx_128_to_255_octet_packets" },
337 { "rx_256_to_511_octet_packets" },
338 { "rx_512_to_1023_octet_packets" },
339 { "rx_1024_to_1522_octet_packets" },
340 { "rx_1523_to_2047_octet_packets" },
341 { "rx_2048_to_4095_octet_packets" },
342 { "rx_4096_to_8191_octet_packets" },
343 { "rx_8192_to_9022_octet_packets" },
344
345 { "tx_octets" },
346 { "tx_collisions" },
347
348 { "tx_xon_sent" },
349 { "tx_xoff_sent" },
350 { "tx_flow_control" },
351 { "tx_mac_errors" },
352 { "tx_single_collisions" },
353 { "tx_mult_collisions" },
354 { "tx_deferred" },
355 { "tx_excessive_collisions" },
356 { "tx_late_collisions" },
357 { "tx_collide_2times" },
358 { "tx_collide_3times" },
359 { "tx_collide_4times" },
360 { "tx_collide_5times" },
361 { "tx_collide_6times" },
362 { "tx_collide_7times" },
363 { "tx_collide_8times" },
364 { "tx_collide_9times" },
365 { "tx_collide_10times" },
366 { "tx_collide_11times" },
367 { "tx_collide_12times" },
368 { "tx_collide_13times" },
369 { "tx_collide_14times" },
370 { "tx_collide_15times" },
371 { "tx_ucast_packets" },
372 { "tx_mcast_packets" },
373 { "tx_bcast_packets" },
374 { "tx_carrier_sense_errors" },
375 { "tx_discards" },
376 { "tx_errors" },
377
378 { "dma_writeq_full" },
379 { "dma_write_prioq_full" },
380 { "rxbds_empty" },
381 { "rx_discards" },
382 { "rx_errors" },
383 { "rx_threshold_hit" },
384
385 { "dma_readq_full" },
386 { "dma_read_prioq_full" },
387 { "tx_comp_queue_full" },
388
389 { "ring_set_send_prod_index" },
390 { "ring_status_update" },
391 { "nic_irqs" },
392 { "nic_avoided_irqs" },
Matt Carlson4452d092011-05-19 12:12:51 +0000393 { "nic_tx_threshold_hit" },
394
395 { "mbuf_lwm_thresh_hit" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396};
397
Matt Carlson48fa55a2011-04-13 11:05:06 +0000398#define TG3_NUM_STATS ARRAY_SIZE(ethtool_stats_keys)
399
400
Andreas Mohr50da8592006-08-14 23:54:30 -0700401static const struct {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700402 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000403} ethtool_test_keys[] = {
Matt Carlson28a45952011-08-19 13:58:22 +0000404 { "nvram test (online) " },
405 { "link test (online) " },
406 { "register test (offline)" },
407 { "memory test (offline)" },
408 { "mac loopback test (offline)" },
409 { "phy loopback test (offline)" },
Matt Carlson941ec902011-08-19 13:58:23 +0000410 { "ext loopback test (offline)" },
Matt Carlson28a45952011-08-19 13:58:22 +0000411 { "interrupt test (offline)" },
Michael Chan4cafd3f2005-05-29 14:56:34 -0700412};
413
Matt Carlson48fa55a2011-04-13 11:05:06 +0000414#define TG3_NUM_TEST ARRAY_SIZE(ethtool_test_keys)
415
416
Michael Chanb401e9e2005-12-19 16:27:04 -0800417static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
418{
419 writel(val, tp->regs + off);
420}
421
422static u32 tg3_read32(struct tg3 *tp, u32 off)
423{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000424 return readl(tp->regs + off);
Michael Chanb401e9e2005-12-19 16:27:04 -0800425}
426
Matt Carlson0d3031d2007-10-10 18:02:43 -0700427static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
428{
429 writel(val, tp->aperegs + off);
430}
431
432static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
433{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000434 return readl(tp->aperegs + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700435}
436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
438{
Michael Chan68929142005-08-09 20:17:14 -0700439 unsigned long flags;
440
441 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700442 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
443 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
Michael Chan68929142005-08-09 20:17:14 -0700444 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700445}
446
447static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
448{
449 writel(val, tp->regs + off);
450 readl(tp->regs + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451}
452
Michael Chan68929142005-08-09 20:17:14 -0700453static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
454{
455 unsigned long flags;
456 u32 val;
457
458 spin_lock_irqsave(&tp->indirect_lock, flags);
459 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
460 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
461 spin_unlock_irqrestore(&tp->indirect_lock, flags);
462 return val;
463}
464
465static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
466{
467 unsigned long flags;
468
469 if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
470 pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
471 TG3_64BIT_REG_LOW, val);
472 return;
473 }
Matt 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 Carlsonf07e9af2010-08-02 11:26:07 +00002782 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Michael Chan15c3b692006-03-22 01:06:52 -08002783 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08002784
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002785 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2786 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002787 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2788 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2789 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2790 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2791 }
2792
Michael Chan15c3b692006-03-22 01:06:52 -08002793 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2794}
2795
Matt Carlson3f007892008-11-03 16:51:36 -08002796/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002797static int tg3_nvram_lock(struct tg3 *tp)
2798{
Joe Perches63c3a662011-04-26 08:12:10 +00002799 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002800 int i;
2801
2802 if (tp->nvram_lock_cnt == 0) {
2803 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2804 for (i = 0; i < 8000; i++) {
2805 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2806 break;
2807 udelay(20);
2808 }
2809 if (i == 8000) {
2810 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2811 return -ENODEV;
2812 }
2813 }
2814 tp->nvram_lock_cnt++;
2815 }
2816 return 0;
2817}
2818
2819/* tp->lock is held. */
2820static void tg3_nvram_unlock(struct tg3 *tp)
2821{
Joe Perches63c3a662011-04-26 08:12:10 +00002822 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002823 if (tp->nvram_lock_cnt > 0)
2824 tp->nvram_lock_cnt--;
2825 if (tp->nvram_lock_cnt == 0)
2826 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
2827 }
2828}
2829
2830/* tp->lock is held. */
2831static void tg3_enable_nvram_access(struct tg3 *tp)
2832{
Joe Perches63c3a662011-04-26 08:12:10 +00002833 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002834 u32 nvaccess = tr32(NVRAM_ACCESS);
2835
2836 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
2837 }
2838}
2839
2840/* tp->lock is held. */
2841static void tg3_disable_nvram_access(struct tg3 *tp)
2842{
Joe Perches63c3a662011-04-26 08:12:10 +00002843 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002844 u32 nvaccess = tr32(NVRAM_ACCESS);
2845
2846 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
2847 }
2848}
2849
2850static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
2851 u32 offset, u32 *val)
2852{
2853 u32 tmp;
2854 int i;
2855
2856 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
2857 return -EINVAL;
2858
2859 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
2860 EEPROM_ADDR_DEVID_MASK |
2861 EEPROM_ADDR_READ);
2862 tw32(GRC_EEPROM_ADDR,
2863 tmp |
2864 (0 << EEPROM_ADDR_DEVID_SHIFT) |
2865 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
2866 EEPROM_ADDR_ADDR_MASK) |
2867 EEPROM_ADDR_READ | EEPROM_ADDR_START);
2868
2869 for (i = 0; i < 1000; i++) {
2870 tmp = tr32(GRC_EEPROM_ADDR);
2871
2872 if (tmp & EEPROM_ADDR_COMPLETE)
2873 break;
2874 msleep(1);
2875 }
2876 if (!(tmp & EEPROM_ADDR_COMPLETE))
2877 return -EBUSY;
2878
Matt Carlson62cedd12009-04-20 14:52:29 -07002879 tmp = tr32(GRC_EEPROM_DATA);
2880
2881 /*
2882 * The data will always be opposite the native endian
2883 * format. Perform a blind byteswap to compensate.
2884 */
2885 *val = swab32(tmp);
2886
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002887 return 0;
2888}
2889
2890#define NVRAM_CMD_TIMEOUT 10000
2891
2892static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
2893{
2894 int i;
2895
2896 tw32(NVRAM_CMD, nvram_cmd);
2897 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
2898 udelay(10);
2899 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
2900 udelay(10);
2901 break;
2902 }
2903 }
2904
2905 if (i == NVRAM_CMD_TIMEOUT)
2906 return -EBUSY;
2907
2908 return 0;
2909}
2910
2911static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
2912{
Joe Perches63c3a662011-04-26 08:12:10 +00002913 if (tg3_flag(tp, NVRAM) &&
2914 tg3_flag(tp, NVRAM_BUFFERED) &&
2915 tg3_flag(tp, FLASH) &&
2916 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002917 (tp->nvram_jedecnum == JEDEC_ATMEL))
2918
2919 addr = ((addr / tp->nvram_pagesize) <<
2920 ATMEL_AT45DB0X1B_PAGE_POS) +
2921 (addr % tp->nvram_pagesize);
2922
2923 return addr;
2924}
2925
2926static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
2927{
Joe Perches63c3a662011-04-26 08:12:10 +00002928 if (tg3_flag(tp, NVRAM) &&
2929 tg3_flag(tp, NVRAM_BUFFERED) &&
2930 tg3_flag(tp, FLASH) &&
2931 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002932 (tp->nvram_jedecnum == JEDEC_ATMEL))
2933
2934 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
2935 tp->nvram_pagesize) +
2936 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
2937
2938 return addr;
2939}
2940
Matt Carlsone4f34112009-02-25 14:25:00 +00002941/* NOTE: Data read in from NVRAM is byteswapped according to
2942 * the byteswapping settings for all other register accesses.
2943 * tg3 devices are BE devices, so on a BE machine, the data
2944 * returned will be exactly as it is seen in NVRAM. On a LE
2945 * machine, the 32-bit value will be byteswapped.
2946 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002947static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
2948{
2949 int ret;
2950
Joe Perches63c3a662011-04-26 08:12:10 +00002951 if (!tg3_flag(tp, NVRAM))
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002952 return tg3_nvram_read_using_eeprom(tp, offset, val);
2953
2954 offset = tg3_nvram_phys_addr(tp, offset);
2955
2956 if (offset > NVRAM_ADDR_MSK)
2957 return -EINVAL;
2958
2959 ret = tg3_nvram_lock(tp);
2960 if (ret)
2961 return ret;
2962
2963 tg3_enable_nvram_access(tp);
2964
2965 tw32(NVRAM_ADDR, offset);
2966 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
2967 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
2968
2969 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00002970 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002971
2972 tg3_disable_nvram_access(tp);
2973
2974 tg3_nvram_unlock(tp);
2975
2976 return ret;
2977}
2978
Matt Carlsona9dc5292009-02-25 14:25:30 +00002979/* Ensures NVRAM data is in bytestream format. */
2980static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002981{
2982 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00002983 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002984 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00002985 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002986 return res;
2987}
2988
Matt Carlsondbe9b922012-02-13 10:20:09 +00002989static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
2990 u32 offset, u32 len, u8 *buf)
2991{
2992 int i, j, rc = 0;
2993 u32 val;
2994
2995 for (i = 0; i < len; i += 4) {
2996 u32 addr;
2997 __be32 data;
2998
2999 addr = offset + i;
3000
3001 memcpy(&data, buf + i, 4);
3002
3003 /*
3004 * The SEEPROM interface expects the data to always be opposite
3005 * the native endian format. We accomplish this by reversing
3006 * all the operations that would have been performed on the
3007 * data from a call to tg3_nvram_read_be32().
3008 */
3009 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
3010
3011 val = tr32(GRC_EEPROM_ADDR);
3012 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
3013
3014 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
3015 EEPROM_ADDR_READ);
3016 tw32(GRC_EEPROM_ADDR, val |
3017 (0 << EEPROM_ADDR_DEVID_SHIFT) |
3018 (addr & EEPROM_ADDR_ADDR_MASK) |
3019 EEPROM_ADDR_START |
3020 EEPROM_ADDR_WRITE);
3021
3022 for (j = 0; j < 1000; j++) {
3023 val = tr32(GRC_EEPROM_ADDR);
3024
3025 if (val & EEPROM_ADDR_COMPLETE)
3026 break;
3027 msleep(1);
3028 }
3029 if (!(val & EEPROM_ADDR_COMPLETE)) {
3030 rc = -EBUSY;
3031 break;
3032 }
3033 }
3034
3035 return rc;
3036}
3037
3038/* offset and length are dword aligned */
3039static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
3040 u8 *buf)
3041{
3042 int ret = 0;
3043 u32 pagesize = tp->nvram_pagesize;
3044 u32 pagemask = pagesize - 1;
3045 u32 nvram_cmd;
3046 u8 *tmp;
3047
3048 tmp = kmalloc(pagesize, GFP_KERNEL);
3049 if (tmp == NULL)
3050 return -ENOMEM;
3051
3052 while (len) {
3053 int j;
3054 u32 phy_addr, page_off, size;
3055
3056 phy_addr = offset & ~pagemask;
3057
3058 for (j = 0; j < pagesize; j += 4) {
3059 ret = tg3_nvram_read_be32(tp, phy_addr + j,
3060 (__be32 *) (tmp + j));
3061 if (ret)
3062 break;
3063 }
3064 if (ret)
3065 break;
3066
3067 page_off = offset & pagemask;
3068 size = pagesize;
3069 if (len < size)
3070 size = len;
3071
3072 len -= size;
3073
3074 memcpy(tmp + page_off, buf, size);
3075
3076 offset = offset + (pagesize - page_off);
3077
3078 tg3_enable_nvram_access(tp);
3079
3080 /*
3081 * Before we can erase the flash page, we need
3082 * to issue a special "write enable" command.
3083 */
3084 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3085
3086 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3087 break;
3088
3089 /* Erase the target page */
3090 tw32(NVRAM_ADDR, phy_addr);
3091
3092 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
3093 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
3094
3095 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3096 break;
3097
3098 /* Issue another write enable to start the write. */
3099 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3100
3101 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3102 break;
3103
3104 for (j = 0; j < pagesize; j += 4) {
3105 __be32 data;
3106
3107 data = *((__be32 *) (tmp + j));
3108
3109 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3110
3111 tw32(NVRAM_ADDR, phy_addr + j);
3112
3113 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
3114 NVRAM_CMD_WR;
3115
3116 if (j == 0)
3117 nvram_cmd |= NVRAM_CMD_FIRST;
3118 else if (j == (pagesize - 4))
3119 nvram_cmd |= NVRAM_CMD_LAST;
3120
3121 ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
3122 if (ret)
3123 break;
3124 }
3125 if (ret)
3126 break;
3127 }
3128
3129 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3130 tg3_nvram_exec_cmd(tp, nvram_cmd);
3131
3132 kfree(tmp);
3133
3134 return ret;
3135}
3136
3137/* offset and length are dword aligned */
3138static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
3139 u8 *buf)
3140{
3141 int i, ret = 0;
3142
3143 for (i = 0; i < len; i += 4, offset += 4) {
3144 u32 page_off, phy_addr, nvram_cmd;
3145 __be32 data;
3146
3147 memcpy(&data, buf + i, 4);
3148 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3149
3150 page_off = offset % tp->nvram_pagesize;
3151
3152 phy_addr = tg3_nvram_phys_addr(tp, offset);
3153
Matt Carlsondbe9b922012-02-13 10:20:09 +00003154 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
3155
3156 if (page_off == 0 || i == 0)
3157 nvram_cmd |= NVRAM_CMD_FIRST;
3158 if (page_off == (tp->nvram_pagesize - 4))
3159 nvram_cmd |= NVRAM_CMD_LAST;
3160
3161 if (i == (len - 4))
3162 nvram_cmd |= NVRAM_CMD_LAST;
3163
Matt Carlson42278222012-02-13 15:20:11 +00003164 if ((nvram_cmd & NVRAM_CMD_FIRST) ||
3165 !tg3_flag(tp, FLASH) ||
3166 !tg3_flag(tp, 57765_PLUS))
3167 tw32(NVRAM_ADDR, phy_addr);
3168
Matt Carlsondbe9b922012-02-13 10:20:09 +00003169 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
3170 !tg3_flag(tp, 5755_PLUS) &&
3171 (tp->nvram_jedecnum == JEDEC_ST) &&
3172 (nvram_cmd & NVRAM_CMD_FIRST)) {
3173 u32 cmd;
3174
3175 cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3176 ret = tg3_nvram_exec_cmd(tp, cmd);
3177 if (ret)
3178 break;
3179 }
3180 if (!tg3_flag(tp, FLASH)) {
3181 /* We always do complete word writes to eeprom. */
3182 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
3183 }
3184
3185 ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
3186 if (ret)
3187 break;
3188 }
3189 return ret;
3190}
3191
3192/* offset and length are dword aligned */
3193static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
3194{
3195 int ret;
3196
3197 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
3198 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
3199 ~GRC_LCLCTRL_GPIO_OUTPUT1);
3200 udelay(40);
3201 }
3202
3203 if (!tg3_flag(tp, NVRAM)) {
3204 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
3205 } else {
3206 u32 grc_mode;
3207
3208 ret = tg3_nvram_lock(tp);
3209 if (ret)
3210 return ret;
3211
3212 tg3_enable_nvram_access(tp);
3213 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM))
3214 tw32(NVRAM_WRITE1, 0x406);
3215
3216 grc_mode = tr32(GRC_MODE);
3217 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
3218
3219 if (tg3_flag(tp, NVRAM_BUFFERED) || !tg3_flag(tp, FLASH)) {
3220 ret = tg3_nvram_write_block_buffered(tp, offset, len,
3221 buf);
3222 } else {
3223 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
3224 buf);
3225 }
3226
3227 grc_mode = tr32(GRC_MODE);
3228 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
3229
3230 tg3_disable_nvram_access(tp);
3231 tg3_nvram_unlock(tp);
3232 }
3233
3234 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
3235 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
3236 udelay(40);
3237 }
3238
3239 return ret;
3240}
3241
Matt Carlson997b4f12011-08-31 11:44:53 +00003242#define RX_CPU_SCRATCH_BASE 0x30000
3243#define RX_CPU_SCRATCH_SIZE 0x04000
3244#define TX_CPU_SCRATCH_BASE 0x34000
3245#define TX_CPU_SCRATCH_SIZE 0x04000
3246
3247/* tp->lock is held. */
3248static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
3249{
3250 int i;
3251
3252 BUG_ON(offset == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS));
3253
3254 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
3255 u32 val = tr32(GRC_VCPU_EXT_CTRL);
3256
3257 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
3258 return 0;
3259 }
3260 if (offset == RX_CPU_BASE) {
3261 for (i = 0; i < 10000; i++) {
3262 tw32(offset + CPU_STATE, 0xffffffff);
3263 tw32(offset + CPU_MODE, CPU_MODE_HALT);
3264 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
3265 break;
3266 }
3267
3268 tw32(offset + CPU_STATE, 0xffffffff);
3269 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
3270 udelay(10);
3271 } else {
3272 for (i = 0; i < 10000; i++) {
3273 tw32(offset + CPU_STATE, 0xffffffff);
3274 tw32(offset + CPU_MODE, CPU_MODE_HALT);
3275 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
3276 break;
3277 }
3278 }
3279
3280 if (i >= 10000) {
3281 netdev_err(tp->dev, "%s timed out, %s CPU\n",
3282 __func__, offset == RX_CPU_BASE ? "RX" : "TX");
3283 return -ENODEV;
3284 }
3285
3286 /* Clear firmware's nvram arbitration. */
3287 if (tg3_flag(tp, NVRAM))
3288 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
3289 return 0;
3290}
3291
3292struct fw_info {
3293 unsigned int fw_base;
3294 unsigned int fw_len;
3295 const __be32 *fw_data;
3296};
3297
3298/* tp->lock is held. */
3299static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base,
3300 u32 cpu_scratch_base, int cpu_scratch_size,
3301 struct fw_info *info)
3302{
3303 int err, lock_err, i;
3304 void (*write_op)(struct tg3 *, u32, u32);
3305
3306 if (cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS)) {
3307 netdev_err(tp->dev,
3308 "%s: Trying to load TX cpu firmware which is 5705\n",
3309 __func__);
3310 return -EINVAL;
3311 }
3312
3313 if (tg3_flag(tp, 5705_PLUS))
3314 write_op = tg3_write_mem;
3315 else
3316 write_op = tg3_write_indirect_reg32;
3317
3318 /* It is possible that bootcode is still loading at this point.
3319 * Get the nvram lock first before halting the cpu.
3320 */
3321 lock_err = tg3_nvram_lock(tp);
3322 err = tg3_halt_cpu(tp, cpu_base);
3323 if (!lock_err)
3324 tg3_nvram_unlock(tp);
3325 if (err)
3326 goto out;
3327
3328 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
3329 write_op(tp, cpu_scratch_base + i, 0);
3330 tw32(cpu_base + CPU_STATE, 0xffffffff);
3331 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
3332 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
3333 write_op(tp, (cpu_scratch_base +
3334 (info->fw_base & 0xffff) +
3335 (i * sizeof(u32))),
3336 be32_to_cpu(info->fw_data[i]));
3337
3338 err = 0;
3339
3340out:
3341 return err;
3342}
3343
3344/* tp->lock is held. */
3345static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
3346{
3347 struct fw_info info;
3348 const __be32 *fw_data;
3349 int err, i;
3350
3351 fw_data = (void *)tp->fw->data;
3352
3353 /* Firmware blob starts with version numbers, followed by
3354 start address and length. We are setting complete length.
3355 length = end_address_of_bss - start_address_of_text.
3356 Remainder is the blob to be loaded contiguously
3357 from start address. */
3358
3359 info.fw_base = be32_to_cpu(fw_data[1]);
3360 info.fw_len = tp->fw->size - 12;
3361 info.fw_data = &fw_data[3];
3362
3363 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
3364 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
3365 &info);
3366 if (err)
3367 return err;
3368
3369 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
3370 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
3371 &info);
3372 if (err)
3373 return err;
3374
3375 /* Now startup only the RX cpu. */
3376 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3377 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
3378
3379 for (i = 0; i < 5; i++) {
3380 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
3381 break;
3382 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3383 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
3384 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
3385 udelay(1000);
3386 }
3387 if (i >= 5) {
3388 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
3389 "should be %08x\n", __func__,
3390 tr32(RX_CPU_BASE + CPU_PC), info.fw_base);
3391 return -ENODEV;
3392 }
3393 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3394 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
3395
3396 return 0;
3397}
3398
3399/* tp->lock is held. */
3400static int tg3_load_tso_firmware(struct tg3 *tp)
3401{
3402 struct fw_info info;
3403 const __be32 *fw_data;
3404 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
3405 int err, i;
3406
3407 if (tg3_flag(tp, HW_TSO_1) ||
3408 tg3_flag(tp, HW_TSO_2) ||
3409 tg3_flag(tp, HW_TSO_3))
3410 return 0;
3411
3412 fw_data = (void *)tp->fw->data;
3413
3414 /* Firmware blob starts with version numbers, followed by
3415 start address and length. We are setting complete length.
3416 length = end_address_of_bss - start_address_of_text.
3417 Remainder is the blob to be loaded contiguously
3418 from start address. */
3419
3420 info.fw_base = be32_to_cpu(fw_data[1]);
3421 cpu_scratch_size = tp->fw_len;
3422 info.fw_len = tp->fw->size - 12;
3423 info.fw_data = &fw_data[3];
3424
3425 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
3426 cpu_base = RX_CPU_BASE;
3427 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
3428 } else {
3429 cpu_base = TX_CPU_BASE;
3430 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
3431 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
3432 }
3433
3434 err = tg3_load_firmware_cpu(tp, cpu_base,
3435 cpu_scratch_base, cpu_scratch_size,
3436 &info);
3437 if (err)
3438 return err;
3439
3440 /* Now startup the cpu. */
3441 tw32(cpu_base + CPU_STATE, 0xffffffff);
3442 tw32_f(cpu_base + CPU_PC, info.fw_base);
3443
3444 for (i = 0; i < 5; i++) {
3445 if (tr32(cpu_base + CPU_PC) == info.fw_base)
3446 break;
3447 tw32(cpu_base + CPU_STATE, 0xffffffff);
3448 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
3449 tw32_f(cpu_base + CPU_PC, info.fw_base);
3450 udelay(1000);
3451 }
3452 if (i >= 5) {
3453 netdev_err(tp->dev,
3454 "%s fails to set CPU PC, is %08x should be %08x\n",
3455 __func__, tr32(cpu_base + CPU_PC), info.fw_base);
3456 return -ENODEV;
3457 }
3458 tw32(cpu_base + CPU_STATE, 0xffffffff);
3459 tw32_f(cpu_base + CPU_MODE, 0x00000000);
3460 return 0;
3461}
3462
3463
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003464/* tp->lock is held. */
Matt Carlson3f007892008-11-03 16:51:36 -08003465static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
3466{
3467 u32 addr_high, addr_low;
3468 int i;
3469
3470 addr_high = ((tp->dev->dev_addr[0] << 8) |
3471 tp->dev->dev_addr[1]);
3472 addr_low = ((tp->dev->dev_addr[2] << 24) |
3473 (tp->dev->dev_addr[3] << 16) |
3474 (tp->dev->dev_addr[4] << 8) |
3475 (tp->dev->dev_addr[5] << 0));
3476 for (i = 0; i < 4; i++) {
3477 if (i == 1 && skip_mac_1)
3478 continue;
3479 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
3480 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
3481 }
3482
3483 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3484 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
3485 for (i = 0; i < 12; i++) {
3486 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
3487 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
3488 }
3489 }
3490
3491 addr_high = (tp->dev->dev_addr[0] +
3492 tp->dev->dev_addr[1] +
3493 tp->dev->dev_addr[2] +
3494 tp->dev->dev_addr[3] +
3495 tp->dev->dev_addr[4] +
3496 tp->dev->dev_addr[5]) &
3497 TX_BACKOFF_SEED_MASK;
3498 tw32(MAC_TX_BACKOFF_SEED, addr_high);
3499}
3500
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003501static void tg3_enable_register_access(struct tg3 *tp)
3502{
3503 /*
3504 * Make sure register accesses (indirect or otherwise) will function
3505 * correctly.
3506 */
3507 pci_write_config_dword(tp->pdev,
3508 TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
3509}
3510
3511static int tg3_power_up(struct tg3 *tp)
3512{
Matt Carlsonbed98292011-07-13 09:27:29 +00003513 int err;
3514
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003515 tg3_enable_register_access(tp);
3516
Matt Carlsonbed98292011-07-13 09:27:29 +00003517 err = pci_set_power_state(tp->pdev, PCI_D0);
3518 if (!err) {
3519 /* Switch out of Vaux if it is a NIC */
3520 tg3_pwrsrc_switch_to_vmain(tp);
3521 } else {
3522 netdev_err(tp->dev, "Transition to D0 failed\n");
3523 }
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003524
Matt Carlsonbed98292011-07-13 09:27:29 +00003525 return err;
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003526}
3527
Matt Carlson4b409522012-02-13 10:20:11 +00003528static int tg3_setup_phy(struct tg3 *, int);
3529
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003530static int tg3_power_down_prepare(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531{
3532 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003533 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003535 tg3_enable_register_access(tp);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003536
3537 /* Restore the CLKREQ setting. */
Joe Perches63c3a662011-04-26 08:12:10 +00003538 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003539 u16 lnkctl;
3540
3541 pci_read_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +00003542 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003543 &lnkctl);
3544 lnkctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
3545 pci_write_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +00003546 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003547 lnkctl);
3548 }
3549
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
3551 tw32(TG3PCI_MISC_HOST_CTRL,
3552 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
3553
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003554 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00003555 tg3_flag(tp, WOL_ENABLE);
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003556
Joe Perches63c3a662011-04-26 08:12:10 +00003557 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08003558 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003559 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson80096062010-08-02 11:26:06 +00003560 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003561 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003562 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003563
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00003564 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003565
Matt Carlson80096062010-08-02 11:26:06 +00003566 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003567
Matt Carlsonc6700ce2012-02-13 15:20:15 +00003568 tp->link_config.speed = phydev->speed;
3569 tp->link_config.duplex = phydev->duplex;
3570 tp->link_config.autoneg = phydev->autoneg;
3571 tp->link_config.advertising = phydev->advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003572
3573 advertising = ADVERTISED_TP |
3574 ADVERTISED_Pause |
3575 ADVERTISED_Autoneg |
3576 ADVERTISED_10baseT_Half;
3577
Joe Perches63c3a662011-04-26 08:12:10 +00003578 if (tg3_flag(tp, ENABLE_ASF) || device_should_wake) {
3579 if (tg3_flag(tp, WOL_SPEED_100MB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003580 advertising |=
3581 ADVERTISED_100baseT_Half |
3582 ADVERTISED_100baseT_Full |
3583 ADVERTISED_10baseT_Full;
3584 else
3585 advertising |= ADVERTISED_10baseT_Full;
3586 }
3587
3588 phydev->advertising = advertising;
3589
3590 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08003591
3592 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00003593 if (phyid != PHY_ID_BCMAC131) {
3594 phyid &= PHY_BCM_OUI_MASK;
3595 if (phyid == PHY_BCM_OUI_1 ||
3596 phyid == PHY_BCM_OUI_2 ||
3597 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08003598 do_low_power = true;
3599 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003600 }
Matt Carlsondd477002008-05-25 23:45:58 -07003601 } else {
Matt Carlson20232762008-12-21 20:18:56 -08003602 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003603
Matt Carlsonc6700ce2012-02-13 15:20:15 +00003604 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER))
Matt Carlson80096062010-08-02 11:26:06 +00003605 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606
Matt Carlson2855b9f2012-02-13 15:20:14 +00003607 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlsondd477002008-05-25 23:45:58 -07003608 tg3_setup_phy(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 }
3610
Michael Chanb5d37722006-09-27 16:06:21 -07003611 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
3612 u32 val;
3613
3614 val = tr32(GRC_VCPU_EXT_CTRL);
3615 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
Joe Perches63c3a662011-04-26 08:12:10 +00003616 } else if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08003617 int i;
3618 u32 val;
3619
3620 for (i = 0; i < 200; i++) {
3621 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
3622 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
3623 break;
3624 msleep(1);
3625 }
3626 }
Joe Perches63c3a662011-04-26 08:12:10 +00003627 if (tg3_flag(tp, WOL_CAP))
Gary Zambranoa85feb82007-05-05 11:52:19 -07003628 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
3629 WOL_DRV_STATE_SHUTDOWN |
3630 WOL_DRV_WOL |
3631 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08003632
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003633 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 u32 mac_mode;
3635
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003636 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003637 if (do_low_power &&
3638 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
3639 tg3_phy_auxctl_write(tp,
3640 MII_TG3_AUXCTL_SHDWSEL_PWRCTL,
3641 MII_TG3_AUXCTL_PCTL_WOL_EN |
3642 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
3643 MII_TG3_AUXCTL_PCTL_CL_AB_TXDAC);
Matt Carlsondd477002008-05-25 23:45:58 -07003644 udelay(40);
3645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003647 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07003648 mac_mode = MAC_MODE_PORT_MODE_GMII;
3649 else
3650 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003652 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
3653 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
3654 ASIC_REV_5700) {
Joe Perches63c3a662011-04-26 08:12:10 +00003655 u32 speed = tg3_flag(tp, WOL_SPEED_100MB) ?
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003656 SPEED_100 : SPEED_10;
3657 if (tg3_5700_link_polarity(tp, speed))
3658 mac_mode |= MAC_MODE_LINK_POLARITY;
3659 else
3660 mac_mode &= ~MAC_MODE_LINK_POLARITY;
3661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 } else {
3663 mac_mode = MAC_MODE_PORT_MODE_TBI;
3664 }
3665
Joe Perches63c3a662011-04-26 08:12:10 +00003666 if (!tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 tw32(MAC_LED_CTRL, tp->led_ctrl);
3668
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003669 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00003670 if ((tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS)) &&
3671 (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)))
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003672 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673
Joe Perches63c3a662011-04-26 08:12:10 +00003674 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00003675 mac_mode |= MAC_MODE_APE_TX_EN |
3676 MAC_MODE_APE_RX_EN |
3677 MAC_MODE_TDE_ENABLE;
Matt Carlson3bda1252008-08-15 14:08:22 -07003678
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 tw32_f(MAC_MODE, mac_mode);
3680 udelay(100);
3681
3682 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
3683 udelay(10);
3684 }
3685
Joe Perches63c3a662011-04-26 08:12:10 +00003686 if (!tg3_flag(tp, WOL_SPEED_100MB) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3688 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
3689 u32 base_val;
3690
3691 base_val = tp->pci_clock_ctrl;
3692 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
3693 CLOCK_CTRL_TXCLK_DISABLE);
3694
Michael Chanb401e9e2005-12-19 16:27:04 -08003695 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
3696 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Joe Perches63c3a662011-04-26 08:12:10 +00003697 } else if (tg3_flag(tp, 5780_CLASS) ||
3698 tg3_flag(tp, CPMU_PRESENT) ||
Matt Carlson6ff6f812011-05-19 12:12:54 +00003699 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan4cf78e42005-07-25 12:29:19 -07003700 /* do nothing */
Joe Perches63c3a662011-04-26 08:12:10 +00003701 } else if (!(tg3_flag(tp, 5750_PLUS) && tg3_flag(tp, ENABLE_ASF))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 u32 newbits1, newbits2;
3703
3704 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3705 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3706 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
3707 CLOCK_CTRL_TXCLK_DISABLE |
3708 CLOCK_CTRL_ALTCLK);
3709 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
Joe Perches63c3a662011-04-26 08:12:10 +00003710 } else if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 newbits1 = CLOCK_CTRL_625_CORE;
3712 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
3713 } else {
3714 newbits1 = CLOCK_CTRL_ALTCLK;
3715 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
3716 }
3717
Michael Chanb401e9e2005-12-19 16:27:04 -08003718 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
3719 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720
Michael Chanb401e9e2005-12-19 16:27:04 -08003721 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
3722 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723
Joe Perches63c3a662011-04-26 08:12:10 +00003724 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 u32 newbits3;
3726
3727 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3728 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3729 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
3730 CLOCK_CTRL_TXCLK_DISABLE |
3731 CLOCK_CTRL_44MHZ_CORE);
3732 } else {
3733 newbits3 = CLOCK_CTRL_44MHZ_CORE;
3734 }
3735
Michael Chanb401e9e2005-12-19 16:27:04 -08003736 tw32_wait_f(TG3PCI_CLOCK_CTRL,
3737 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 }
3739 }
3740
Joe Perches63c3a662011-04-26 08:12:10 +00003741 if (!(device_should_wake) && !tg3_flag(tp, ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08003742 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08003743
Matt Carlsoncd0d7222011-07-13 09:27:33 +00003744 tg3_frob_aux_power(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745
3746 /* Workaround for unstable PLL clock */
3747 if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
3748 (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
3749 u32 val = tr32(0x7d00);
3750
3751 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
3752 tw32(0x7d00, val);
Joe Perches63c3a662011-04-26 08:12:10 +00003753 if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08003754 int err;
3755
3756 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08003758 if (!err)
3759 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08003760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 }
3762
Michael Chanbbadf502006-04-06 21:46:34 -07003763 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
3764
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 return 0;
3766}
3767
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003768static void tg3_power_down(struct tg3 *tp)
3769{
3770 tg3_power_down_prepare(tp);
3771
Joe Perches63c3a662011-04-26 08:12:10 +00003772 pci_wake_from_d3(tp->pdev, tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003773 pci_set_power_state(tp->pdev, PCI_D3hot);
3774}
3775
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
3777{
3778 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
3779 case MII_TG3_AUX_STAT_10HALF:
3780 *speed = SPEED_10;
3781 *duplex = DUPLEX_HALF;
3782 break;
3783
3784 case MII_TG3_AUX_STAT_10FULL:
3785 *speed = SPEED_10;
3786 *duplex = DUPLEX_FULL;
3787 break;
3788
3789 case MII_TG3_AUX_STAT_100HALF:
3790 *speed = SPEED_100;
3791 *duplex = DUPLEX_HALF;
3792 break;
3793
3794 case MII_TG3_AUX_STAT_100FULL:
3795 *speed = SPEED_100;
3796 *duplex = DUPLEX_FULL;
3797 break;
3798
3799 case MII_TG3_AUX_STAT_1000HALF:
3800 *speed = SPEED_1000;
3801 *duplex = DUPLEX_HALF;
3802 break;
3803
3804 case MII_TG3_AUX_STAT_1000FULL:
3805 *speed = SPEED_1000;
3806 *duplex = DUPLEX_FULL;
3807 break;
3808
3809 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003810 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07003811 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
3812 SPEED_10;
3813 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
3814 DUPLEX_HALF;
3815 break;
3816 }
Matt Carlsone7405222012-02-13 15:20:16 +00003817 *speed = SPEED_UNKNOWN;
3818 *duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821}
3822
Matt Carlson42b64a42011-05-19 12:12:49 +00003823static int tg3_phy_autoneg_cfg(struct tg3 *tp, u32 advertise, u32 flowctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824{
Matt Carlson42b64a42011-05-19 12:12:49 +00003825 int err = 0;
3826 u32 val, new_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827
Matt Carlson42b64a42011-05-19 12:12:49 +00003828 new_adv = ADVERTISE_CSMA;
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +00003829 new_adv |= ethtool_adv_to_mii_adv_t(advertise) & ADVERTISE_ALL;
Matt Carlsonf88788f2011-12-14 11:10:00 +00003830 new_adv |= mii_advertise_flowctrl(flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831
Matt Carlson42b64a42011-05-19 12:12:49 +00003832 err = tg3_writephy(tp, MII_ADVERTISE, new_adv);
3833 if (err)
3834 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835
Matt Carlson4f272092011-12-14 11:09:57 +00003836 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
3837 new_adv = ethtool_adv_to_mii_ctrl1000_t(advertise);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003838
Matt Carlson4f272092011-12-14 11:09:57 +00003839 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3840 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
3841 new_adv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003842
Matt Carlson4f272092011-12-14 11:09:57 +00003843 err = tg3_writephy(tp, MII_CTRL1000, new_adv);
3844 if (err)
3845 goto done;
3846 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003847
Matt Carlson42b64a42011-05-19 12:12:49 +00003848 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
3849 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850
Matt Carlson42b64a42011-05-19 12:12:49 +00003851 tw32(TG3_CPMU_EEE_MODE,
3852 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003853
Matt Carlson42b64a42011-05-19 12:12:49 +00003854 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
3855 if (!err) {
3856 u32 err2;
Matt Carlson52b02d02010-10-14 10:37:41 +00003857
Matt Carlsona6b68da2010-12-06 08:28:52 +00003858 val = 0;
Matt Carlson42b64a42011-05-19 12:12:49 +00003859 /* Advertise 100-BaseTX EEE ability */
3860 if (advertise & ADVERTISED_100baseT_Full)
3861 val |= MDIO_AN_EEE_ADV_100TX;
3862 /* Advertise 1000-BaseT EEE ability */
3863 if (advertise & ADVERTISED_1000baseT_Full)
3864 val |= MDIO_AN_EEE_ADV_1000T;
3865 err = tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlsonb715ce92011-07-20 10:20:52 +00003866 if (err)
3867 val = 0;
3868
3869 switch (GET_ASIC_REV(tp->pci_chip_rev_id)) {
3870 case ASIC_REV_5717:
3871 case ASIC_REV_57765:
Matt Carlson55086ad2011-12-14 11:09:59 +00003872 case ASIC_REV_57766:
Matt Carlsonb715ce92011-07-20 10:20:52 +00003873 case ASIC_REV_5719:
3874 /* If we advertised any eee advertisements above... */
3875 if (val)
3876 val = MII_TG3_DSP_TAP26_ALNOKO |
3877 MII_TG3_DSP_TAP26_RMRXSTO |
3878 MII_TG3_DSP_TAP26_OPCSINPT;
3879 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
3880 /* Fall through */
3881 case ASIC_REV_5720:
3882 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
3883 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
3884 MII_TG3_DSP_CH34TP2_HIBW01);
3885 }
Matt Carlson52b02d02010-10-14 10:37:41 +00003886
Matt Carlson42b64a42011-05-19 12:12:49 +00003887 err2 = TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
3888 if (!err)
3889 err = err2;
3890 }
3891
3892done:
3893 return err;
3894}
3895
3896static void tg3_phy_copper_begin(struct tg3 *tp)
3897{
Matt Carlsond13ba512012-02-22 12:35:19 +00003898 if (tp->link_config.autoneg == AUTONEG_ENABLE ||
3899 (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
3900 u32 adv, fc;
Matt Carlson42b64a42011-05-19 12:12:49 +00003901
Matt Carlsond13ba512012-02-22 12:35:19 +00003902 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
3903 adv = ADVERTISED_10baseT_Half |
3904 ADVERTISED_10baseT_Full;
3905 if (tg3_flag(tp, WOL_SPEED_100MB))
3906 adv |= ADVERTISED_100baseT_Half |
3907 ADVERTISED_100baseT_Full;
Matt Carlson42b64a42011-05-19 12:12:49 +00003908
Matt Carlsond13ba512012-02-22 12:35:19 +00003909 fc = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson42b64a42011-05-19 12:12:49 +00003910 } else {
Matt Carlsond13ba512012-02-22 12:35:19 +00003911 adv = tp->link_config.advertising;
3912 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
3913 adv &= ~(ADVERTISED_1000baseT_Half |
3914 ADVERTISED_1000baseT_Full);
3915
3916 fc = tp->link_config.flowctrl;
Matt Carlson42b64a42011-05-19 12:12:49 +00003917 }
3918
Matt Carlsond13ba512012-02-22 12:35:19 +00003919 tg3_phy_autoneg_cfg(tp, adv, fc);
Matt Carlson52b02d02010-10-14 10:37:41 +00003920
Matt Carlsond13ba512012-02-22 12:35:19 +00003921 tg3_writephy(tp, MII_BMCR,
3922 BMCR_ANENABLE | BMCR_ANRESTART);
3923 } else {
3924 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 u32 bmcr, orig_bmcr;
3926
3927 tp->link_config.active_speed = tp->link_config.speed;
3928 tp->link_config.active_duplex = tp->link_config.duplex;
3929
3930 bmcr = 0;
3931 switch (tp->link_config.speed) {
3932 default:
3933 case SPEED_10:
3934 break;
3935
3936 case SPEED_100:
3937 bmcr |= BMCR_SPEED100;
3938 break;
3939
3940 case SPEED_1000:
Matt Carlson221c5632011-06-13 13:39:01 +00003941 bmcr |= BMCR_SPEED1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944
3945 if (tp->link_config.duplex == DUPLEX_FULL)
3946 bmcr |= BMCR_FULLDPLX;
3947
3948 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
3949 (bmcr != orig_bmcr)) {
3950 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
3951 for (i = 0; i < 1500; i++) {
3952 u32 tmp;
3953
3954 udelay(10);
3955 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
3956 tg3_readphy(tp, MII_BMSR, &tmp))
3957 continue;
3958 if (!(tmp & BMSR_LSTATUS)) {
3959 udelay(40);
3960 break;
3961 }
3962 }
3963 tg3_writephy(tp, MII_BMCR, bmcr);
3964 udelay(40);
3965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 }
3967}
3968
3969static int tg3_init_5401phy_dsp(struct tg3 *tp)
3970{
3971 int err;
3972
3973 /* Turn off tap power management. */
3974 /* Set Extended packet length bit */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003975 err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00003977 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
3978 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
3979 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
3980 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
3981 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982
3983 udelay(40);
3984
3985 return err;
3986}
3987
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003988static bool tg3_phy_copper_an_config_ok(struct tg3 *tp, u32 *lcladv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989{
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003990 u32 advmsk, tgtadv, advertising;
Michael Chan3600d912006-12-07 00:21:48 -08003991
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003992 advertising = tp->link_config.advertising;
3993 tgtadv = ethtool_adv_to_mii_adv_t(advertising) & ADVERTISE_ALL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003995 advmsk = ADVERTISE_ALL;
3996 if (tp->link_config.active_duplex == DUPLEX_FULL) {
Matt Carlsonf88788f2011-12-14 11:10:00 +00003997 tgtadv |= mii_advertise_flowctrl(tp->link_config.flowctrl);
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003998 advmsk |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
3999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004001 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
4002 return false;
4003
4004 if ((*lcladv & advmsk) != tgtadv)
4005 return false;
Matt Carlsonb99d2a52011-08-31 11:44:47 +00004006
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004007 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 u32 tg3_ctrl;
4009
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004010 tgtadv = ethtool_adv_to_mii_ctrl1000_t(advertising);
Michael Chan3600d912006-12-07 00:21:48 -08004011
Matt Carlson221c5632011-06-13 13:39:01 +00004012 if (tg3_readphy(tp, MII_CTRL1000, &tg3_ctrl))
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004013 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014
Matt Carlson3198e072012-02-13 15:20:10 +00004015 if (tgtadv &&
4016 (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
4017 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)) {
4018 tgtadv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
4019 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL |
4020 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
4021 } else {
4022 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL);
4023 }
4024
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004025 if (tg3_ctrl != tgtadv)
4026 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 }
Matt Carlson93a700a2011-08-31 11:44:54 +00004028
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004029 return true;
Matt Carlsonef167e22007-12-20 20:10:01 -08004030}
4031
Matt Carlson859edb22011-12-08 14:40:16 +00004032static bool tg3_phy_copper_fetch_rmtadv(struct tg3 *tp, u32 *rmtadv)
4033{
4034 u32 lpeth = 0;
4035
4036 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4037 u32 val;
4038
4039 if (tg3_readphy(tp, MII_STAT1000, &val))
4040 return false;
4041
4042 lpeth = mii_stat1000_to_ethtool_lpa_t(val);
4043 }
4044
4045 if (tg3_readphy(tp, MII_LPA, rmtadv))
4046 return false;
4047
4048 lpeth |= mii_lpa_to_ethtool_lpa_t(*rmtadv);
4049 tp->link_config.rmt_adv = lpeth;
4050
4051 return true;
4052}
4053
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
4055{
4056 int current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004057 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08004058 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 u16 current_speed;
4060 u8 current_duplex;
4061 int i, err;
4062
4063 tw32(MAC_EVENT, 0);
4064
4065 tw32_f(MAC_STATUS,
4066 (MAC_STATUS_SYNC_CHANGED |
4067 MAC_STATUS_CFG_CHANGED |
4068 MAC_STATUS_MI_COMPLETION |
4069 MAC_STATUS_LNKSTATE_CHANGED));
4070 udelay(40);
4071
Matt Carlson8ef21422008-05-02 16:47:53 -07004072 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
4073 tw32_f(MAC_MI_MODE,
4074 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
4075 udelay(80);
4076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004078 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079
4080 /* Some third-party PHYs need to be reset on link going
4081 * down.
4082 */
4083 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
4084 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
4085 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
4086 netif_carrier_ok(tp->dev)) {
4087 tg3_readphy(tp, MII_BMSR, &bmsr);
4088 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4089 !(bmsr & BMSR_LSTATUS))
4090 force_reset = 1;
4091 }
4092 if (force_reset)
4093 tg3_phy_reset(tp);
4094
Matt Carlson79eb6902010-02-17 15:17:03 +00004095 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 tg3_readphy(tp, MII_BMSR, &bmsr);
4097 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
Joe Perches63c3a662011-04-26 08:12:10 +00004098 !tg3_flag(tp, INIT_COMPLETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 bmsr = 0;
4100
4101 if (!(bmsr & BMSR_LSTATUS)) {
4102 err = tg3_init_5401phy_dsp(tp);
4103 if (err)
4104 return err;
4105
4106 tg3_readphy(tp, MII_BMSR, &bmsr);
4107 for (i = 0; i < 1000; i++) {
4108 udelay(10);
4109 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4110 (bmsr & BMSR_LSTATUS)) {
4111 udelay(40);
4112 break;
4113 }
4114 }
4115
Matt Carlson79eb6902010-02-17 15:17:03 +00004116 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
4117 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 !(bmsr & BMSR_LSTATUS) &&
4119 tp->link_config.active_speed == SPEED_1000) {
4120 err = tg3_phy_reset(tp);
4121 if (!err)
4122 err = tg3_init_5401phy_dsp(tp);
4123 if (err)
4124 return err;
4125 }
4126 }
4127 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
4128 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
4129 /* 5701 {A0,B0} CRC bug workaround */
4130 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004131 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
4132 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
4133 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 }
4135
4136 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004137 tg3_readphy(tp, MII_TG3_ISTAT, &val);
4138 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004140 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004142 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 tg3_writephy(tp, MII_TG3_IMASK, ~0);
4144
4145 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
4146 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
4147 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
4148 tg3_writephy(tp, MII_TG3_EXT_CTRL,
4149 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
4150 else
4151 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
4152 }
4153
4154 current_link_up = 0;
Matt Carlsone7405222012-02-13 15:20:16 +00004155 current_speed = SPEED_UNKNOWN;
4156 current_duplex = DUPLEX_UNKNOWN;
Matt Carlsone348c5e2011-11-21 15:01:20 +00004157 tp->phy_flags &= ~TG3_PHYFLG_MDIX_STATE;
Matt Carlson859edb22011-12-08 14:40:16 +00004158 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004160 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Matt Carlson15ee95c2011-04-20 07:57:40 +00004161 err = tg3_phy_auxctl_read(tp,
4162 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4163 &val);
4164 if (!err && !(val & (1 << 10))) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004165 tg3_phy_auxctl_write(tp,
4166 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4167 val | (1 << 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 goto relink;
4169 }
4170 }
4171
4172 bmsr = 0;
4173 for (i = 0; i < 100; i++) {
4174 tg3_readphy(tp, MII_BMSR, &bmsr);
4175 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4176 (bmsr & BMSR_LSTATUS))
4177 break;
4178 udelay(40);
4179 }
4180
4181 if (bmsr & BMSR_LSTATUS) {
4182 u32 aux_stat, bmcr;
4183
4184 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
4185 for (i = 0; i < 2000; i++) {
4186 udelay(10);
4187 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
4188 aux_stat)
4189 break;
4190 }
4191
4192 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
4193 &current_speed,
4194 &current_duplex);
4195
4196 bmcr = 0;
4197 for (i = 0; i < 200; i++) {
4198 tg3_readphy(tp, MII_BMCR, &bmcr);
4199 if (tg3_readphy(tp, MII_BMCR, &bmcr))
4200 continue;
4201 if (bmcr && bmcr != 0x7fff)
4202 break;
4203 udelay(10);
4204 }
4205
Matt Carlsonef167e22007-12-20 20:10:01 -08004206 lcl_adv = 0;
4207 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208
Matt Carlsonef167e22007-12-20 20:10:01 -08004209 tp->link_config.active_speed = current_speed;
4210 tp->link_config.active_duplex = current_duplex;
4211
4212 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4213 if ((bmcr & BMCR_ANENABLE) &&
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004214 tg3_phy_copper_an_config_ok(tp, &lcl_adv) &&
Matt Carlson859edb22011-12-08 14:40:16 +00004215 tg3_phy_copper_fetch_rmtadv(tp, &rmt_adv))
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004216 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 } else {
4218 if (!(bmcr & BMCR_ANENABLE) &&
4219 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08004220 tp->link_config.duplex == current_duplex &&
4221 tp->link_config.flowctrl ==
4222 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 }
4225 }
4226
Matt Carlsonef167e22007-12-20 20:10:01 -08004227 if (current_link_up == 1 &&
Matt Carlsone348c5e2011-11-21 15:01:20 +00004228 tp->link_config.active_duplex == DUPLEX_FULL) {
4229 u32 reg, bit;
4230
4231 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
4232 reg = MII_TG3_FET_GEN_STAT;
4233 bit = MII_TG3_FET_GEN_STAT_MDIXSTAT;
4234 } else {
4235 reg = MII_TG3_EXT_STAT;
4236 bit = MII_TG3_EXT_STAT_MDIX;
4237 }
4238
4239 if (!tg3_readphy(tp, reg, &val) && (val & bit))
4240 tp->phy_flags |= TG3_PHYFLG_MDIX_STATE;
4241
Matt Carlsonef167e22007-12-20 20:10:01 -08004242 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Matt Carlsone348c5e2011-11-21 15:01:20 +00004243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244 }
4245
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246relink:
Matt Carlson80096062010-08-02 11:26:06 +00004247 if (current_link_up == 0 || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 tg3_phy_copper_begin(tp);
4249
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004250 tg3_readphy(tp, MII_BMSR, &bmsr);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00004251 if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
4252 (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 current_link_up = 1;
4254 }
4255
4256 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
4257 if (current_link_up == 1) {
4258 if (tp->link_config.active_speed == SPEED_100 ||
4259 tp->link_config.active_speed == SPEED_10)
4260 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4261 else
4262 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004263 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00004264 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4265 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4267
4268 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4269 if (tp->link_config.active_duplex == DUPLEX_HALF)
4270 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4271
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004273 if (current_link_up == 1 &&
4274 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004276 else
4277 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 }
4279
4280 /* ??? Without this setting Netgear GA302T PHY does not
4281 * ??? send/receive packets...
4282 */
Matt Carlson79eb6902010-02-17 15:17:03 +00004283 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
4285 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
4286 tw32_f(MAC_MI_MODE, tp->mi_mode);
4287 udelay(80);
4288 }
4289
4290 tw32_f(MAC_MODE, tp->mac_mode);
4291 udelay(40);
4292
Matt Carlson52b02d02010-10-14 10:37:41 +00004293 tg3_phy_eee_adjust(tp, current_link_up);
4294
Joe Perches63c3a662011-04-26 08:12:10 +00004295 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 /* Polled via timer. */
4297 tw32_f(MAC_EVENT, 0);
4298 } else {
4299 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4300 }
4301 udelay(40);
4302
4303 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
4304 current_link_up == 1 &&
4305 tp->link_config.active_speed == SPEED_1000 &&
Joe Perches63c3a662011-04-26 08:12:10 +00004306 (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 udelay(120);
4308 tw32_f(MAC_STATUS,
4309 (MAC_STATUS_SYNC_CHANGED |
4310 MAC_STATUS_CFG_CHANGED));
4311 udelay(40);
4312 tg3_write_mem(tp,
4313 NIC_SRAM_FIRMWARE_MBOX,
4314 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
4315 }
4316
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004317 /* Prevent send BD corruption. */
Joe Perches63c3a662011-04-26 08:12:10 +00004318 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004319 u16 oldlnkctl, newlnkctl;
4320
4321 pci_read_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +00004322 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004323 &oldlnkctl);
4324 if (tp->link_config.active_speed == SPEED_100 ||
4325 tp->link_config.active_speed == SPEED_10)
4326 newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
4327 else
4328 newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
4329 if (newlnkctl != oldlnkctl)
4330 pci_write_config_word(tp->pdev,
Matt Carlson93a700a2011-08-31 11:44:54 +00004331 pci_pcie_cap(tp->pdev) +
4332 PCI_EXP_LNKCTL, newlnkctl);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004333 }
4334
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 if (current_link_up != netif_carrier_ok(tp->dev)) {
4336 if (current_link_up)
4337 netif_carrier_on(tp->dev);
4338 else
4339 netif_carrier_off(tp->dev);
4340 tg3_link_report(tp);
4341 }
4342
4343 return 0;
4344}
4345
4346struct tg3_fiber_aneginfo {
4347 int state;
4348#define ANEG_STATE_UNKNOWN 0
4349#define ANEG_STATE_AN_ENABLE 1
4350#define ANEG_STATE_RESTART_INIT 2
4351#define ANEG_STATE_RESTART 3
4352#define ANEG_STATE_DISABLE_LINK_OK 4
4353#define ANEG_STATE_ABILITY_DETECT_INIT 5
4354#define ANEG_STATE_ABILITY_DETECT 6
4355#define ANEG_STATE_ACK_DETECT_INIT 7
4356#define ANEG_STATE_ACK_DETECT 8
4357#define ANEG_STATE_COMPLETE_ACK_INIT 9
4358#define ANEG_STATE_COMPLETE_ACK 10
4359#define ANEG_STATE_IDLE_DETECT_INIT 11
4360#define ANEG_STATE_IDLE_DETECT 12
4361#define ANEG_STATE_LINK_OK 13
4362#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
4363#define ANEG_STATE_NEXT_PAGE_WAIT 15
4364
4365 u32 flags;
4366#define MR_AN_ENABLE 0x00000001
4367#define MR_RESTART_AN 0x00000002
4368#define MR_AN_COMPLETE 0x00000004
4369#define MR_PAGE_RX 0x00000008
4370#define MR_NP_LOADED 0x00000010
4371#define MR_TOGGLE_TX 0x00000020
4372#define MR_LP_ADV_FULL_DUPLEX 0x00000040
4373#define MR_LP_ADV_HALF_DUPLEX 0x00000080
4374#define MR_LP_ADV_SYM_PAUSE 0x00000100
4375#define MR_LP_ADV_ASYM_PAUSE 0x00000200
4376#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
4377#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
4378#define MR_LP_ADV_NEXT_PAGE 0x00001000
4379#define MR_TOGGLE_RX 0x00002000
4380#define MR_NP_RX 0x00004000
4381
4382#define MR_LINK_OK 0x80000000
4383
4384 unsigned long link_time, cur_time;
4385
4386 u32 ability_match_cfg;
4387 int ability_match_count;
4388
4389 char ability_match, idle_match, ack_match;
4390
4391 u32 txconfig, rxconfig;
4392#define ANEG_CFG_NP 0x00000080
4393#define ANEG_CFG_ACK 0x00000040
4394#define ANEG_CFG_RF2 0x00000020
4395#define ANEG_CFG_RF1 0x00000010
4396#define ANEG_CFG_PS2 0x00000001
4397#define ANEG_CFG_PS1 0x00008000
4398#define ANEG_CFG_HD 0x00004000
4399#define ANEG_CFG_FD 0x00002000
4400#define ANEG_CFG_INVAL 0x00001f06
4401
4402};
4403#define ANEG_OK 0
4404#define ANEG_DONE 1
4405#define ANEG_TIMER_ENAB 2
4406#define ANEG_FAILED -1
4407
4408#define ANEG_STATE_SETTLE_TIME 10000
4409
4410static int tg3_fiber_aneg_smachine(struct tg3 *tp,
4411 struct tg3_fiber_aneginfo *ap)
4412{
Matt Carlson5be73b42007-12-20 20:09:29 -08004413 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 unsigned long delta;
4415 u32 rx_cfg_reg;
4416 int ret;
4417
4418 if (ap->state == ANEG_STATE_UNKNOWN) {
4419 ap->rxconfig = 0;
4420 ap->link_time = 0;
4421 ap->cur_time = 0;
4422 ap->ability_match_cfg = 0;
4423 ap->ability_match_count = 0;
4424 ap->ability_match = 0;
4425 ap->idle_match = 0;
4426 ap->ack_match = 0;
4427 }
4428 ap->cur_time++;
4429
4430 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
4431 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
4432
4433 if (rx_cfg_reg != ap->ability_match_cfg) {
4434 ap->ability_match_cfg = rx_cfg_reg;
4435 ap->ability_match = 0;
4436 ap->ability_match_count = 0;
4437 } else {
4438 if (++ap->ability_match_count > 1) {
4439 ap->ability_match = 1;
4440 ap->ability_match_cfg = rx_cfg_reg;
4441 }
4442 }
4443 if (rx_cfg_reg & ANEG_CFG_ACK)
4444 ap->ack_match = 1;
4445 else
4446 ap->ack_match = 0;
4447
4448 ap->idle_match = 0;
4449 } else {
4450 ap->idle_match = 1;
4451 ap->ability_match_cfg = 0;
4452 ap->ability_match_count = 0;
4453 ap->ability_match = 0;
4454 ap->ack_match = 0;
4455
4456 rx_cfg_reg = 0;
4457 }
4458
4459 ap->rxconfig = rx_cfg_reg;
4460 ret = ANEG_OK;
4461
Matt Carlson33f401a2010-04-05 10:19:27 +00004462 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 case ANEG_STATE_UNKNOWN:
4464 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
4465 ap->state = ANEG_STATE_AN_ENABLE;
4466
4467 /* fallthru */
4468 case ANEG_STATE_AN_ENABLE:
4469 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
4470 if (ap->flags & MR_AN_ENABLE) {
4471 ap->link_time = 0;
4472 ap->cur_time = 0;
4473 ap->ability_match_cfg = 0;
4474 ap->ability_match_count = 0;
4475 ap->ability_match = 0;
4476 ap->idle_match = 0;
4477 ap->ack_match = 0;
4478
4479 ap->state = ANEG_STATE_RESTART_INIT;
4480 } else {
4481 ap->state = ANEG_STATE_DISABLE_LINK_OK;
4482 }
4483 break;
4484
4485 case ANEG_STATE_RESTART_INIT:
4486 ap->link_time = ap->cur_time;
4487 ap->flags &= ~(MR_NP_LOADED);
4488 ap->txconfig = 0;
4489 tw32(MAC_TX_AUTO_NEG, 0);
4490 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4491 tw32_f(MAC_MODE, tp->mac_mode);
4492 udelay(40);
4493
4494 ret = ANEG_TIMER_ENAB;
4495 ap->state = ANEG_STATE_RESTART;
4496
4497 /* fallthru */
4498 case ANEG_STATE_RESTART:
4499 delta = ap->cur_time - ap->link_time;
Matt Carlson859a588792010-04-05 10:19:28 +00004500 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a588792010-04-05 10:19:28 +00004502 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504 break;
4505
4506 case ANEG_STATE_DISABLE_LINK_OK:
4507 ret = ANEG_DONE;
4508 break;
4509
4510 case ANEG_STATE_ABILITY_DETECT_INIT:
4511 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08004512 ap->txconfig = ANEG_CFG_FD;
4513 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
4514 if (flowctrl & ADVERTISE_1000XPAUSE)
4515 ap->txconfig |= ANEG_CFG_PS1;
4516 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
4517 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
4519 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4520 tw32_f(MAC_MODE, tp->mac_mode);
4521 udelay(40);
4522
4523 ap->state = ANEG_STATE_ABILITY_DETECT;
4524 break;
4525
4526 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a588792010-04-05 10:19:28 +00004527 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 break;
4530
4531 case ANEG_STATE_ACK_DETECT_INIT:
4532 ap->txconfig |= ANEG_CFG_ACK;
4533 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
4534 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4535 tw32_f(MAC_MODE, tp->mac_mode);
4536 udelay(40);
4537
4538 ap->state = ANEG_STATE_ACK_DETECT;
4539
4540 /* fallthru */
4541 case ANEG_STATE_ACK_DETECT:
4542 if (ap->ack_match != 0) {
4543 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
4544 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
4545 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
4546 } else {
4547 ap->state = ANEG_STATE_AN_ENABLE;
4548 }
4549 } else if (ap->ability_match != 0 &&
4550 ap->rxconfig == 0) {
4551 ap->state = ANEG_STATE_AN_ENABLE;
4552 }
4553 break;
4554
4555 case ANEG_STATE_COMPLETE_ACK_INIT:
4556 if (ap->rxconfig & ANEG_CFG_INVAL) {
4557 ret = ANEG_FAILED;
4558 break;
4559 }
4560 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
4561 MR_LP_ADV_HALF_DUPLEX |
4562 MR_LP_ADV_SYM_PAUSE |
4563 MR_LP_ADV_ASYM_PAUSE |
4564 MR_LP_ADV_REMOTE_FAULT1 |
4565 MR_LP_ADV_REMOTE_FAULT2 |
4566 MR_LP_ADV_NEXT_PAGE |
4567 MR_TOGGLE_RX |
4568 MR_NP_RX);
4569 if (ap->rxconfig & ANEG_CFG_FD)
4570 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
4571 if (ap->rxconfig & ANEG_CFG_HD)
4572 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
4573 if (ap->rxconfig & ANEG_CFG_PS1)
4574 ap->flags |= MR_LP_ADV_SYM_PAUSE;
4575 if (ap->rxconfig & ANEG_CFG_PS2)
4576 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
4577 if (ap->rxconfig & ANEG_CFG_RF1)
4578 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
4579 if (ap->rxconfig & ANEG_CFG_RF2)
4580 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
4581 if (ap->rxconfig & ANEG_CFG_NP)
4582 ap->flags |= MR_LP_ADV_NEXT_PAGE;
4583
4584 ap->link_time = ap->cur_time;
4585
4586 ap->flags ^= (MR_TOGGLE_TX);
4587 if (ap->rxconfig & 0x0008)
4588 ap->flags |= MR_TOGGLE_RX;
4589 if (ap->rxconfig & ANEG_CFG_NP)
4590 ap->flags |= MR_NP_RX;
4591 ap->flags |= MR_PAGE_RX;
4592
4593 ap->state = ANEG_STATE_COMPLETE_ACK;
4594 ret = ANEG_TIMER_ENAB;
4595 break;
4596
4597 case ANEG_STATE_COMPLETE_ACK:
4598 if (ap->ability_match != 0 &&
4599 ap->rxconfig == 0) {
4600 ap->state = ANEG_STATE_AN_ENABLE;
4601 break;
4602 }
4603 delta = ap->cur_time - ap->link_time;
4604 if (delta > ANEG_STATE_SETTLE_TIME) {
4605 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
4606 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
4607 } else {
4608 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
4609 !(ap->flags & MR_NP_RX)) {
4610 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
4611 } else {
4612 ret = ANEG_FAILED;
4613 }
4614 }
4615 }
4616 break;
4617
4618 case ANEG_STATE_IDLE_DETECT_INIT:
4619 ap->link_time = ap->cur_time;
4620 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
4621 tw32_f(MAC_MODE, tp->mac_mode);
4622 udelay(40);
4623
4624 ap->state = ANEG_STATE_IDLE_DETECT;
4625 ret = ANEG_TIMER_ENAB;
4626 break;
4627
4628 case ANEG_STATE_IDLE_DETECT:
4629 if (ap->ability_match != 0 &&
4630 ap->rxconfig == 0) {
4631 ap->state = ANEG_STATE_AN_ENABLE;
4632 break;
4633 }
4634 delta = ap->cur_time - ap->link_time;
4635 if (delta > ANEG_STATE_SETTLE_TIME) {
4636 /* XXX another gem from the Broadcom driver :( */
4637 ap->state = ANEG_STATE_LINK_OK;
4638 }
4639 break;
4640
4641 case ANEG_STATE_LINK_OK:
4642 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
4643 ret = ANEG_DONE;
4644 break;
4645
4646 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
4647 /* ??? unimplemented */
4648 break;
4649
4650 case ANEG_STATE_NEXT_PAGE_WAIT:
4651 /* ??? unimplemented */
4652 break;
4653
4654 default:
4655 ret = ANEG_FAILED;
4656 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658
4659 return ret;
4660}
4661
Matt Carlson5be73b42007-12-20 20:09:29 -08004662static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663{
4664 int res = 0;
4665 struct tg3_fiber_aneginfo aninfo;
4666 int status = ANEG_FAILED;
4667 unsigned int tick;
4668 u32 tmp;
4669
4670 tw32_f(MAC_TX_AUTO_NEG, 0);
4671
4672 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
4673 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
4674 udelay(40);
4675
4676 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
4677 udelay(40);
4678
4679 memset(&aninfo, 0, sizeof(aninfo));
4680 aninfo.flags |= MR_AN_ENABLE;
4681 aninfo.state = ANEG_STATE_UNKNOWN;
4682 aninfo.cur_time = 0;
4683 tick = 0;
4684 while (++tick < 195000) {
4685 status = tg3_fiber_aneg_smachine(tp, &aninfo);
4686 if (status == ANEG_DONE || status == ANEG_FAILED)
4687 break;
4688
4689 udelay(1);
4690 }
4691
4692 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
4693 tw32_f(MAC_MODE, tp->mac_mode);
4694 udelay(40);
4695
Matt Carlson5be73b42007-12-20 20:09:29 -08004696 *txflags = aninfo.txconfig;
4697 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698
4699 if (status == ANEG_DONE &&
4700 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
4701 MR_LP_ADV_FULL_DUPLEX)))
4702 res = 1;
4703
4704 return res;
4705}
4706
4707static void tg3_init_bcm8002(struct tg3 *tp)
4708{
4709 u32 mac_status = tr32(MAC_STATUS);
4710 int i;
4711
4712 /* Reset when initting first time or we have a link. */
Joe Perches63c3a662011-04-26 08:12:10 +00004713 if (tg3_flag(tp, INIT_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714 !(mac_status & MAC_STATUS_PCS_SYNCED))
4715 return;
4716
4717 /* Set PLL lock range. */
4718 tg3_writephy(tp, 0x16, 0x8007);
4719
4720 /* SW reset */
4721 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
4722
4723 /* Wait for reset to complete. */
4724 /* XXX schedule_timeout() ... */
4725 for (i = 0; i < 500; i++)
4726 udelay(10);
4727
4728 /* Config mode; select PMA/Ch 1 regs. */
4729 tg3_writephy(tp, 0x10, 0x8411);
4730
4731 /* Enable auto-lock and comdet, select txclk for tx. */
4732 tg3_writephy(tp, 0x11, 0x0a10);
4733
4734 tg3_writephy(tp, 0x18, 0x00a0);
4735 tg3_writephy(tp, 0x16, 0x41ff);
4736
4737 /* Assert and deassert POR. */
4738 tg3_writephy(tp, 0x13, 0x0400);
4739 udelay(40);
4740 tg3_writephy(tp, 0x13, 0x0000);
4741
4742 tg3_writephy(tp, 0x11, 0x0a50);
4743 udelay(40);
4744 tg3_writephy(tp, 0x11, 0x0a10);
4745
4746 /* Wait for signal to stabilize */
4747 /* XXX schedule_timeout() ... */
4748 for (i = 0; i < 15000; i++)
4749 udelay(10);
4750
4751 /* Deselect the channel register so we can read the PHYID
4752 * later.
4753 */
4754 tg3_writephy(tp, 0x10, 0x8011);
4755}
4756
4757static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
4758{
Matt Carlson82cd3d12007-12-20 20:09:00 -08004759 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 u32 sg_dig_ctrl, sg_dig_status;
4761 u32 serdes_cfg, expected_sg_dig_ctrl;
4762 int workaround, port_a;
4763 int current_link_up;
4764
4765 serdes_cfg = 0;
4766 expected_sg_dig_ctrl = 0;
4767 workaround = 0;
4768 port_a = 1;
4769 current_link_up = 0;
4770
4771 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
4772 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
4773 workaround = 1;
4774 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
4775 port_a = 0;
4776
4777 /* preserve bits 0-11,13,14 for signal pre-emphasis */
4778 /* preserve bits 20-23 for voltage regulator */
4779 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
4780 }
4781
4782 sg_dig_ctrl = tr32(SG_DIG_CTRL);
4783
4784 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004785 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786 if (workaround) {
4787 u32 val = serdes_cfg;
4788
4789 if (port_a)
4790 val |= 0xc010000;
4791 else
4792 val |= 0x4010000;
4793 tw32_f(MAC_SERDES_CFG, val);
4794 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004795
4796 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797 }
4798 if (mac_status & MAC_STATUS_PCS_SYNCED) {
4799 tg3_setup_flow_control(tp, 0, 0);
4800 current_link_up = 1;
4801 }
4802 goto out;
4803 }
4804
4805 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004806 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807
Matt Carlson82cd3d12007-12-20 20:09:00 -08004808 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
4809 if (flowctrl & ADVERTISE_1000XPAUSE)
4810 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
4811 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
4812 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813
4814 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004815 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07004816 tp->serdes_counter &&
4817 ((mac_status & (MAC_STATUS_PCS_SYNCED |
4818 MAC_STATUS_RCVD_CFG)) ==
4819 MAC_STATUS_PCS_SYNCED)) {
4820 tp->serdes_counter--;
4821 current_link_up = 1;
4822 goto out;
4823 }
4824restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825 if (workaround)
4826 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004827 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828 udelay(5);
4829 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
4830
Michael Chan3d3ebe72006-09-27 15:59:15 -07004831 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004832 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
4834 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07004835 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836 mac_status = tr32(MAC_STATUS);
4837
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004838 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08004840 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841
Matt Carlson82cd3d12007-12-20 20:09:00 -08004842 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
4843 local_adv |= ADVERTISE_1000XPAUSE;
4844 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
4845 local_adv |= ADVERTISE_1000XPSE_ASYM;
4846
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004847 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08004848 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004849 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08004850 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851
Matt Carlson859edb22011-12-08 14:40:16 +00004852 tp->link_config.rmt_adv =
4853 mii_adv_to_ethtool_adv_x(remote_adv);
4854
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855 tg3_setup_flow_control(tp, local_adv, remote_adv);
4856 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004857 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004858 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004859 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07004860 if (tp->serdes_counter)
4861 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862 else {
4863 if (workaround) {
4864 u32 val = serdes_cfg;
4865
4866 if (port_a)
4867 val |= 0xc010000;
4868 else
4869 val |= 0x4010000;
4870
4871 tw32_f(MAC_SERDES_CFG, val);
4872 }
4873
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004874 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875 udelay(40);
4876
4877 /* Link parallel detection - link is up */
4878 /* only if we have PCS_SYNC and not */
4879 /* receiving config code words */
4880 mac_status = tr32(MAC_STATUS);
4881 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
4882 !(mac_status & MAC_STATUS_RCVD_CFG)) {
4883 tg3_setup_flow_control(tp, 0, 0);
4884 current_link_up = 1;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004885 tp->phy_flags |=
4886 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004887 tp->serdes_counter =
4888 SERDES_PARALLEL_DET_TIMEOUT;
4889 } else
4890 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891 }
4892 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07004893 } else {
4894 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004895 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896 }
4897
4898out:
4899 return current_link_up;
4900}
4901
4902static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
4903{
4904 int current_link_up = 0;
4905
Michael Chan5cf64b8a2007-05-05 12:11:21 -07004906 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908
4909 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08004910 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004912
Matt Carlson5be73b42007-12-20 20:09:29 -08004913 if (fiber_autoneg(tp, &txflags, &rxflags)) {
4914 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915
Matt Carlson5be73b42007-12-20 20:09:29 -08004916 if (txflags & ANEG_CFG_PS1)
4917 local_adv |= ADVERTISE_1000XPAUSE;
4918 if (txflags & ANEG_CFG_PS2)
4919 local_adv |= ADVERTISE_1000XPSE_ASYM;
4920
4921 if (rxflags & MR_LP_ADV_SYM_PAUSE)
4922 remote_adv |= LPA_1000XPAUSE;
4923 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
4924 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925
Matt Carlson859edb22011-12-08 14:40:16 +00004926 tp->link_config.rmt_adv =
4927 mii_adv_to_ethtool_adv_x(remote_adv);
4928
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929 tg3_setup_flow_control(tp, local_adv, remote_adv);
4930
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931 current_link_up = 1;
4932 }
4933 for (i = 0; i < 30; i++) {
4934 udelay(20);
4935 tw32_f(MAC_STATUS,
4936 (MAC_STATUS_SYNC_CHANGED |
4937 MAC_STATUS_CFG_CHANGED));
4938 udelay(40);
4939 if ((tr32(MAC_STATUS) &
4940 (MAC_STATUS_SYNC_CHANGED |
4941 MAC_STATUS_CFG_CHANGED)) == 0)
4942 break;
4943 }
4944
4945 mac_status = tr32(MAC_STATUS);
4946 if (current_link_up == 0 &&
4947 (mac_status & MAC_STATUS_PCS_SYNCED) &&
4948 !(mac_status & MAC_STATUS_RCVD_CFG))
4949 current_link_up = 1;
4950 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08004951 tg3_setup_flow_control(tp, 0, 0);
4952
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953 /* Forcing 1000FD link up. */
4954 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955
4956 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
4957 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004958
4959 tw32_f(MAC_MODE, tp->mac_mode);
4960 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 }
4962
4963out:
4964 return current_link_up;
4965}
4966
4967static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
4968{
4969 u32 orig_pause_cfg;
4970 u16 orig_active_speed;
4971 u8 orig_active_duplex;
4972 u32 mac_status;
4973 int current_link_up;
4974 int i;
4975
Matt Carlson8d018622007-12-20 20:05:44 -08004976 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977 orig_active_speed = tp->link_config.active_speed;
4978 orig_active_duplex = tp->link_config.active_duplex;
4979
Joe Perches63c3a662011-04-26 08:12:10 +00004980 if (!tg3_flag(tp, HW_AUTONEG) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 netif_carrier_ok(tp->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00004982 tg3_flag(tp, INIT_COMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983 mac_status = tr32(MAC_STATUS);
4984 mac_status &= (MAC_STATUS_PCS_SYNCED |
4985 MAC_STATUS_SIGNAL_DET |
4986 MAC_STATUS_CFG_CHANGED |
4987 MAC_STATUS_RCVD_CFG);
4988 if (mac_status == (MAC_STATUS_PCS_SYNCED |
4989 MAC_STATUS_SIGNAL_DET)) {
4990 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4991 MAC_STATUS_CFG_CHANGED));
4992 return 0;
4993 }
4994 }
4995
4996 tw32_f(MAC_TX_AUTO_NEG, 0);
4997
4998 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
4999 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
5000 tw32_f(MAC_MODE, tp->mac_mode);
5001 udelay(40);
5002
Matt Carlson79eb6902010-02-17 15:17:03 +00005003 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004 tg3_init_bcm8002(tp);
5005
5006 /* Enable link change event even when serdes polling. */
5007 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5008 udelay(40);
5009
5010 current_link_up = 0;
Matt Carlson859edb22011-12-08 14:40:16 +00005011 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012 mac_status = tr32(MAC_STATUS);
5013
Joe Perches63c3a662011-04-26 08:12:10 +00005014 if (tg3_flag(tp, HW_AUTONEG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
5016 else
5017 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
5018
Matt Carlson898a56f2009-08-28 14:02:40 +00005019 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00005021 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022
5023 for (i = 0; i < 100; i++) {
5024 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
5025 MAC_STATUS_CFG_CHANGED));
5026 udelay(5);
5027 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07005028 MAC_STATUS_CFG_CHANGED |
5029 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030 break;
5031 }
5032
5033 mac_status = tr32(MAC_STATUS);
5034 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
5035 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005036 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
5037 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038 tw32_f(MAC_MODE, (tp->mac_mode |
5039 MAC_MODE_SEND_CONFIGS));
5040 udelay(1);
5041 tw32_f(MAC_MODE, tp->mac_mode);
5042 }
5043 }
5044
5045 if (current_link_up == 1) {
5046 tp->link_config.active_speed = SPEED_1000;
5047 tp->link_config.active_duplex = DUPLEX_FULL;
5048 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5049 LED_CTRL_LNKLED_OVERRIDE |
5050 LED_CTRL_1000MBPS_ON));
5051 } else {
Matt Carlsone7405222012-02-13 15:20:16 +00005052 tp->link_config.active_speed = SPEED_UNKNOWN;
5053 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5055 LED_CTRL_LNKLED_OVERRIDE |
5056 LED_CTRL_TRAFFIC_OVERRIDE));
5057 }
5058
5059 if (current_link_up != netif_carrier_ok(tp->dev)) {
5060 if (current_link_up)
5061 netif_carrier_on(tp->dev);
5062 else
5063 netif_carrier_off(tp->dev);
5064 tg3_link_report(tp);
5065 } else {
Matt Carlson8d018622007-12-20 20:05:44 -08005066 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067 if (orig_pause_cfg != now_pause_cfg ||
5068 orig_active_speed != tp->link_config.active_speed ||
5069 orig_active_duplex != tp->link_config.active_duplex)
5070 tg3_link_report(tp);
5071 }
5072
5073 return 0;
5074}
5075
Michael Chan747e8f82005-07-25 12:33:22 -07005076static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
5077{
5078 int current_link_up, err = 0;
5079 u32 bmsr, bmcr;
5080 u16 current_speed;
5081 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08005082 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07005083
5084 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5085 tw32_f(MAC_MODE, tp->mac_mode);
5086 udelay(40);
5087
5088 tw32(MAC_EVENT, 0);
5089
5090 tw32_f(MAC_STATUS,
5091 (MAC_STATUS_SYNC_CHANGED |
5092 MAC_STATUS_CFG_CHANGED |
5093 MAC_STATUS_MI_COMPLETION |
5094 MAC_STATUS_LNKSTATE_CHANGED));
5095 udelay(40);
5096
5097 if (force_reset)
5098 tg3_phy_reset(tp);
5099
5100 current_link_up = 0;
Matt Carlsone7405222012-02-13 15:20:16 +00005101 current_speed = SPEED_UNKNOWN;
5102 current_duplex = DUPLEX_UNKNOWN;
Matt Carlson859edb22011-12-08 14:40:16 +00005103 tp->link_config.rmt_adv = 0;
Michael Chan747e8f82005-07-25 12:33:22 -07005104
5105 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5106 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08005107 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
5108 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5109 bmsr |= BMSR_LSTATUS;
5110 else
5111 bmsr &= ~BMSR_LSTATUS;
5112 }
Michael Chan747e8f82005-07-25 12:33:22 -07005113
5114 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
5115
5116 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005117 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005118 /* do nothing, just check for link up at the end */
5119 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson28011cf2011-11-16 18:36:59 -05005120 u32 adv, newadv;
Michael Chan747e8f82005-07-25 12:33:22 -07005121
5122 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
Matt Carlson28011cf2011-11-16 18:36:59 -05005123 newadv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
5124 ADVERTISE_1000XPAUSE |
5125 ADVERTISE_1000XPSE_ASYM |
5126 ADVERTISE_SLCT);
Michael Chan747e8f82005-07-25 12:33:22 -07005127
Matt Carlson28011cf2011-11-16 18:36:59 -05005128 newadv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Matt Carlson37f07022011-11-17 14:30:55 +00005129 newadv |= ethtool_adv_to_mii_adv_x(tp->link_config.advertising);
Michael Chan747e8f82005-07-25 12:33:22 -07005130
Matt Carlson28011cf2011-11-16 18:36:59 -05005131 if ((newadv != adv) || !(bmcr & BMCR_ANENABLE)) {
5132 tg3_writephy(tp, MII_ADVERTISE, newadv);
Michael Chan747e8f82005-07-25 12:33:22 -07005133 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
5134 tg3_writephy(tp, MII_BMCR, bmcr);
5135
5136 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07005137 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005138 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005139
5140 return err;
5141 }
5142 } else {
5143 u32 new_bmcr;
5144
5145 bmcr &= ~BMCR_SPEED1000;
5146 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
5147
5148 if (tp->link_config.duplex == DUPLEX_FULL)
5149 new_bmcr |= BMCR_FULLDPLX;
5150
5151 if (new_bmcr != bmcr) {
5152 /* BMCR_SPEED1000 is a reserved bit that needs
5153 * to be set on write.
5154 */
5155 new_bmcr |= BMCR_SPEED1000;
5156
5157 /* Force a linkdown */
5158 if (netif_carrier_ok(tp->dev)) {
5159 u32 adv;
5160
5161 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
5162 adv &= ~(ADVERTISE_1000XFULL |
5163 ADVERTISE_1000XHALF |
5164 ADVERTISE_SLCT);
5165 tg3_writephy(tp, MII_ADVERTISE, adv);
5166 tg3_writephy(tp, MII_BMCR, bmcr |
5167 BMCR_ANRESTART |
5168 BMCR_ANENABLE);
5169 udelay(10);
5170 netif_carrier_off(tp->dev);
5171 }
5172 tg3_writephy(tp, MII_BMCR, new_bmcr);
5173 bmcr = new_bmcr;
5174 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5175 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08005176 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
5177 ASIC_REV_5714) {
5178 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5179 bmsr |= BMSR_LSTATUS;
5180 else
5181 bmsr &= ~BMSR_LSTATUS;
5182 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005183 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005184 }
5185 }
5186
5187 if (bmsr & BMSR_LSTATUS) {
5188 current_speed = SPEED_1000;
5189 current_link_up = 1;
5190 if (bmcr & BMCR_FULLDPLX)
5191 current_duplex = DUPLEX_FULL;
5192 else
5193 current_duplex = DUPLEX_HALF;
5194
Matt Carlsonef167e22007-12-20 20:10:01 -08005195 local_adv = 0;
5196 remote_adv = 0;
5197
Michael Chan747e8f82005-07-25 12:33:22 -07005198 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08005199 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07005200
5201 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
5202 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
5203 common = local_adv & remote_adv;
5204 if (common & (ADVERTISE_1000XHALF |
5205 ADVERTISE_1000XFULL)) {
5206 if (common & ADVERTISE_1000XFULL)
5207 current_duplex = DUPLEX_FULL;
5208 else
5209 current_duplex = DUPLEX_HALF;
Matt Carlson859edb22011-12-08 14:40:16 +00005210
5211 tp->link_config.rmt_adv =
5212 mii_adv_to_ethtool_adv_x(remote_adv);
Joe Perches63c3a662011-04-26 08:12:10 +00005213 } else if (!tg3_flag(tp, 5780_CLASS)) {
Matt Carlson57d8b882010-06-05 17:24:35 +00005214 /* Link is up via parallel detect */
Matt Carlson859a588792010-04-05 10:19:28 +00005215 } else {
Michael Chan747e8f82005-07-25 12:33:22 -07005216 current_link_up = 0;
Matt Carlson859a588792010-04-05 10:19:28 +00005217 }
Michael Chan747e8f82005-07-25 12:33:22 -07005218 }
5219 }
5220
Matt Carlsonef167e22007-12-20 20:10:01 -08005221 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
5222 tg3_setup_flow_control(tp, local_adv, remote_adv);
5223
Michael Chan747e8f82005-07-25 12:33:22 -07005224 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
5225 if (tp->link_config.active_duplex == DUPLEX_HALF)
5226 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
5227
5228 tw32_f(MAC_MODE, tp->mac_mode);
5229 udelay(40);
5230
5231 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5232
5233 tp->link_config.active_speed = current_speed;
5234 tp->link_config.active_duplex = current_duplex;
5235
5236 if (current_link_up != netif_carrier_ok(tp->dev)) {
5237 if (current_link_up)
5238 netif_carrier_on(tp->dev);
5239 else {
5240 netif_carrier_off(tp->dev);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005241 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005242 }
5243 tg3_link_report(tp);
5244 }
5245 return err;
5246}
5247
5248static void tg3_serdes_parallel_detect(struct tg3 *tp)
5249{
Michael Chan3d3ebe72006-09-27 15:59:15 -07005250 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07005251 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07005252 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07005253 return;
5254 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005255
Michael Chan747e8f82005-07-25 12:33:22 -07005256 if (!netif_carrier_ok(tp->dev) &&
5257 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
5258 u32 bmcr;
5259
5260 tg3_readphy(tp, MII_BMCR, &bmcr);
5261 if (bmcr & BMCR_ANENABLE) {
5262 u32 phy1, phy2;
5263
5264 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005265 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
5266 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07005267
5268 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005269 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5270 MII_TG3_DSP_EXP1_INT_STAT);
5271 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
5272 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005273
5274 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
5275 /* We have signal detect and not receiving
5276 * config code words, link is up by parallel
5277 * detection.
5278 */
5279
5280 bmcr &= ~BMCR_ANENABLE;
5281 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
5282 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005283 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005284 }
5285 }
Matt Carlson859a588792010-04-05 10:19:28 +00005286 } else if (netif_carrier_ok(tp->dev) &&
5287 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005288 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005289 u32 phy2;
5290
5291 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005292 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5293 MII_TG3_DSP_EXP1_INT_STAT);
5294 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005295 if (phy2 & 0x20) {
5296 u32 bmcr;
5297
5298 /* Config code words received, turn on autoneg. */
5299 tg3_readphy(tp, MII_BMCR, &bmcr);
5300 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
5301
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005302 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005303
5304 }
5305 }
5306}
5307
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308static int tg3_setup_phy(struct tg3 *tp, int force_reset)
5309{
Matt Carlsonf2096f92011-04-05 14:22:48 +00005310 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311 int err;
5312
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005313 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005315 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07005316 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a588792010-04-05 10:19:28 +00005317 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319
Matt Carlsonbcb37f62008-11-03 16:52:09 -08005320 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00005321 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08005322
5323 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
5324 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
5325 scale = 65;
5326 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
5327 scale = 6;
5328 else
5329 scale = 12;
5330
5331 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
5332 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
5333 tw32(GRC_MISC_CFG, val);
5334 }
5335
Matt Carlsonf2096f92011-04-05 14:22:48 +00005336 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
5337 (6 << TX_LENGTHS_IPG_SHIFT);
5338 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
5339 val |= tr32(MAC_TX_LENGTHS) &
5340 (TX_LENGTHS_JMB_FRM_LEN_MSK |
5341 TX_LENGTHS_CNT_DWN_VAL_MSK);
5342
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343 if (tp->link_config.active_speed == SPEED_1000 &&
5344 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00005345 tw32(MAC_TX_LENGTHS, val |
5346 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00005348 tw32(MAC_TX_LENGTHS, val |
5349 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350
Joe Perches63c3a662011-04-26 08:12:10 +00005351 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352 if (netif_carrier_ok(tp->dev)) {
5353 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07005354 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355 } else {
5356 tw32(HOSTCC_STAT_COAL_TICKS, 0);
5357 }
5358 }
5359
Joe Perches63c3a662011-04-26 08:12:10 +00005360 if (tg3_flag(tp, ASPM_WORKAROUND)) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00005361 val = tr32(PCIE_PWR_MGMT_THRESH);
Matt Carlson8ed5d972007-05-07 00:25:49 -07005362 if (!netif_carrier_ok(tp->dev))
5363 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
5364 tp->pwrmgmt_thresh;
5365 else
5366 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
5367 tw32(PCIE_PWR_MGMT_THRESH, val);
5368 }
5369
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370 return err;
5371}
5372
Matt Carlson66cfd1b2010-09-30 10:34:30 +00005373static inline int tg3_irq_sync(struct tg3 *tp)
5374{
5375 return tp->irq_sync;
5376}
5377
Matt Carlson97bd8e42011-04-13 11:05:04 +00005378static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
5379{
5380 int i;
5381
5382 dst = (u32 *)((u8 *)dst + off);
5383 for (i = 0; i < len; i += sizeof(u32))
5384 *dst++ = tr32(off + i);
5385}
5386
5387static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
5388{
5389 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
5390 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
5391 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
5392 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
5393 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
5394 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
5395 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
5396 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
5397 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
5398 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
5399 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
5400 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
5401 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
5402 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
5403 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
5404 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
5405 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
5406 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
5407 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
5408
Joe Perches63c3a662011-04-26 08:12:10 +00005409 if (tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson97bd8e42011-04-13 11:05:04 +00005410 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
5411
5412 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
5413 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
5414 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
5415 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
5416 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
5417 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
5418 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
5419 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
5420
Joe Perches63c3a662011-04-26 08:12:10 +00005421 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00005422 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
5423 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
5424 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
5425 }
5426
5427 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
5428 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
5429 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
5430 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
5431 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
5432
Joe Perches63c3a662011-04-26 08:12:10 +00005433 if (tg3_flag(tp, NVRAM))
Matt Carlson97bd8e42011-04-13 11:05:04 +00005434 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
5435}
5436
5437static void tg3_dump_state(struct tg3 *tp)
5438{
5439 int i;
5440 u32 *regs;
5441
5442 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
5443 if (!regs) {
5444 netdev_err(tp->dev, "Failed allocating register dump buffer\n");
5445 return;
5446 }
5447
Joe Perches63c3a662011-04-26 08:12:10 +00005448 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00005449 /* Read up to but not including private PCI registers */
5450 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
5451 regs[i / sizeof(u32)] = tr32(i);
5452 } else
5453 tg3_dump_legacy_regs(tp, regs);
5454
5455 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
5456 if (!regs[i + 0] && !regs[i + 1] &&
5457 !regs[i + 2] && !regs[i + 3])
5458 continue;
5459
5460 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
5461 i * 4,
5462 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
5463 }
5464
5465 kfree(regs);
5466
5467 for (i = 0; i < tp->irq_cnt; i++) {
5468 struct tg3_napi *tnapi = &tp->napi[i];
5469
5470 /* SW status block */
5471 netdev_err(tp->dev,
5472 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
5473 i,
5474 tnapi->hw_status->status,
5475 tnapi->hw_status->status_tag,
5476 tnapi->hw_status->rx_jumbo_consumer,
5477 tnapi->hw_status->rx_consumer,
5478 tnapi->hw_status->rx_mini_consumer,
5479 tnapi->hw_status->idx[0].rx_producer,
5480 tnapi->hw_status->idx[0].tx_consumer);
5481
5482 netdev_err(tp->dev,
5483 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
5484 i,
5485 tnapi->last_tag, tnapi->last_irq_tag,
5486 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
5487 tnapi->rx_rcb_ptr,
5488 tnapi->prodring.rx_std_prod_idx,
5489 tnapi->prodring.rx_std_cons_idx,
5490 tnapi->prodring.rx_jmb_prod_idx,
5491 tnapi->prodring.rx_jmb_cons_idx);
5492 }
5493}
5494
Michael Chandf3e6542006-05-26 17:48:07 -07005495/* This is called whenever we suspect that the system chipset is re-
5496 * ordering the sequence of MMIO to the tx send mailbox. The symptom
5497 * is bogus tx completions. We try to recover by setting the
5498 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
5499 * in the workqueue.
5500 */
5501static void tg3_tx_recover(struct tg3 *tp)
5502{
Joe Perches63c3a662011-04-26 08:12:10 +00005503 BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
Michael Chandf3e6542006-05-26 17:48:07 -07005504 tp->write32_tx_mbox == tg3_write_indirect_mbox);
5505
Matt Carlson5129c3a2010-04-05 10:19:23 +00005506 netdev_warn(tp->dev,
5507 "The system may be re-ordering memory-mapped I/O "
5508 "cycles to the network device, attempting to recover. "
5509 "Please report the problem to the driver maintainer "
5510 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07005511
5512 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00005513 tg3_flag_set(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07005514 spin_unlock(&tp->lock);
5515}
5516
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005517static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07005518{
Matt Carlsonf65aac12010-08-02 11:26:03 +00005519 /* Tell compiler to fetch tx indices from memory. */
5520 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005521 return tnapi->tx_pending -
5522 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07005523}
5524
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525/* Tigon3 never reports partial packet sends. So we do not
5526 * need special logic to handle SKBs that have not had all
5527 * of their frags sent yet, like SunGEM does.
5528 */
Matt Carlson17375d22009-08-28 14:02:18 +00005529static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530{
Matt Carlson17375d22009-08-28 14:02:18 +00005531 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005532 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005533 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005534 struct netdev_queue *txq;
5535 int index = tnapi - tp->napi;
Tom Herbert298376d2011-11-28 16:33:30 +00005536 unsigned int pkts_compl = 0, bytes_compl = 0;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005537
Joe Perches63c3a662011-04-26 08:12:10 +00005538 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005539 index--;
5540
5541 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542
5543 while (sw_idx != hw_idx) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00005544 struct tg3_tx_ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07005546 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547
Michael Chandf3e6542006-05-26 17:48:07 -07005548 if (unlikely(skb == NULL)) {
5549 tg3_tx_recover(tp);
5550 return;
5551 }
5552
Alexander Duyckf4188d82009-12-02 16:48:38 +00005553 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005554 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005555 skb_headlen(skb),
5556 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557
5558 ri->skb = NULL;
5559
Matt Carlsone01ee142011-07-27 14:20:50 +00005560 while (ri->fragmented) {
5561 ri->fragmented = false;
5562 sw_idx = NEXT_TX(sw_idx);
5563 ri = &tnapi->tx_buffers[sw_idx];
5564 }
5565
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566 sw_idx = NEXT_TX(sw_idx);
5567
5568 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005569 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07005570 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
5571 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005572
5573 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005574 dma_unmap_addr(ri, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00005575 skb_frag_size(&skb_shinfo(skb)->frags[i]),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005576 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00005577
5578 while (ri->fragmented) {
5579 ri->fragmented = false;
5580 sw_idx = NEXT_TX(sw_idx);
5581 ri = &tnapi->tx_buffers[sw_idx];
5582 }
5583
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584 sw_idx = NEXT_TX(sw_idx);
5585 }
5586
Tom Herbert298376d2011-11-28 16:33:30 +00005587 pkts_compl++;
5588 bytes_compl += skb->len;
5589
David S. Millerf47c11e2005-06-24 20:18:35 -07005590 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07005591
5592 if (unlikely(tx_bug)) {
5593 tg3_tx_recover(tp);
5594 return;
5595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005596 }
5597
Tom Herbert298376d2011-11-28 16:33:30 +00005598 netdev_completed_queue(tp->dev, pkts_compl, bytes_compl);
5599
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005600 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601
Michael Chan1b2a7202006-08-07 21:46:02 -07005602 /* Need to make the tx_cons update visible to tg3_start_xmit()
5603 * before checking for netif_queue_stopped(). Without the
5604 * memory barrier, there is a small possibility that tg3_start_xmit()
5605 * will miss it and cause the queue to be stopped forever.
5606 */
5607 smp_mb();
5608
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005609 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005610 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005611 __netif_tx_lock(txq, smp_processor_id());
5612 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005613 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005614 netif_tx_wake_queue(txq);
5615 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07005616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617}
5618
Eric Dumazet9205fd92011-11-18 06:47:01 +00005619static void tg3_rx_data_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005620{
Eric Dumazet9205fd92011-11-18 06:47:01 +00005621 if (!ri->data)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005622 return;
5623
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005624 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005625 map_sz, PCI_DMA_FROMDEVICE);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005626 kfree(ri->data);
5627 ri->data = NULL;
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005628}
5629
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630/* Returns size of skb allocated or < 0 on error.
5631 *
5632 * We only need to fill in the address because the other members
5633 * of the RX descriptor are invariant, see tg3_init_rings.
5634 *
5635 * Note the purposeful assymetry of cpu vs. chip accesses. For
5636 * posting buffers we only dirty the first cache line of the RX
5637 * descriptor (containing the address). Whereas for the RX status
5638 * buffers the cpu only reads the last cacheline of the RX descriptor
5639 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
5640 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00005641static int tg3_alloc_rx_data(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Matt Carlsona3896162009-11-13 13:03:44 +00005642 u32 opaque_key, u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643{
5644 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00005645 struct ring_info *map;
Eric Dumazet9205fd92011-11-18 06:47:01 +00005646 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647 dma_addr_t mapping;
Eric Dumazet9205fd92011-11-18 06:47:01 +00005648 int skb_size, data_size, dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650 switch (opaque_key) {
5651 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00005652 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00005653 desc = &tpr->rx_std[dest_idx];
5654 map = &tpr->rx_std_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00005655 data_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656 break;
5657
5658 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00005659 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00005660 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00005661 map = &tpr->rx_jmb_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00005662 data_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663 break;
5664
5665 default:
5666 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668
5669 /* Do not overwrite any of the map or rp information
5670 * until we are sure we can commit to a new buffer.
5671 *
5672 * Callers depend upon this behavior and assume that
5673 * we leave everything unchanged if we fail.
5674 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00005675 skb_size = SKB_DATA_ALIGN(data_size + TG3_RX_OFFSET(tp)) +
5676 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
5677 data = kmalloc(skb_size, GFP_ATOMIC);
5678 if (!data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679 return -ENOMEM;
5680
Eric Dumazet9205fd92011-11-18 06:47:01 +00005681 mapping = pci_map_single(tp->pdev,
5682 data + TG3_RX_OFFSET(tp),
5683 data_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005684 PCI_DMA_FROMDEVICE);
Matt Carlsona21771d2009-11-02 14:25:31 +00005685 if (pci_dma_mapping_error(tp->pdev, mapping)) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00005686 kfree(data);
Matt Carlsona21771d2009-11-02 14:25:31 +00005687 return -EIO;
5688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689
Eric Dumazet9205fd92011-11-18 06:47:01 +00005690 map->data = data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005691 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692
Linus Torvalds1da177e2005-04-16 15:20:36 -07005693 desc->addr_hi = ((u64)mapping >> 32);
5694 desc->addr_lo = ((u64)mapping & 0xffffffff);
5695
Eric Dumazet9205fd92011-11-18 06:47:01 +00005696 return data_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697}
5698
5699/* We only need to move over in the address because the other
5700 * members of the RX descriptor are invariant. See notes above
Eric Dumazet9205fd92011-11-18 06:47:01 +00005701 * tg3_alloc_rx_data for full details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702 */
Matt Carlsona3896162009-11-13 13:03:44 +00005703static void tg3_recycle_rx(struct tg3_napi *tnapi,
5704 struct tg3_rx_prodring_set *dpr,
5705 u32 opaque_key, int src_idx,
5706 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707{
Matt Carlson17375d22009-08-28 14:02:18 +00005708 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
5710 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00005711 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005712 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713
5714 switch (opaque_key) {
5715 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00005716 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00005717 dest_desc = &dpr->rx_std[dest_idx];
5718 dest_map = &dpr->rx_std_buffers[dest_idx];
5719 src_desc = &spr->rx_std[src_idx];
5720 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721 break;
5722
5723 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00005724 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00005725 dest_desc = &dpr->rx_jmb[dest_idx].std;
5726 dest_map = &dpr->rx_jmb_buffers[dest_idx];
5727 src_desc = &spr->rx_jmb[src_idx].std;
5728 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729 break;
5730
5731 default:
5732 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734
Eric Dumazet9205fd92011-11-18 06:47:01 +00005735 dest_map->data = src_map->data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005736 dma_unmap_addr_set(dest_map, mapping,
5737 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738 dest_desc->addr_hi = src_desc->addr_hi;
5739 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00005740
5741 /* Ensure that the update to the skb happens after the physical
5742 * addresses have been transferred to the new BD location.
5743 */
5744 smp_wmb();
5745
Eric Dumazet9205fd92011-11-18 06:47:01 +00005746 src_map->data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747}
5748
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749/* The RX ring scheme is composed of multiple rings which post fresh
5750 * buffers to the chip, and one special ring the chip uses to report
5751 * status back to the host.
5752 *
5753 * The special ring reports the status of received packets to the
5754 * host. The chip does not write into the original descriptor the
5755 * RX buffer was obtained from. The chip simply takes the original
5756 * descriptor as provided by the host, updates the status and length
5757 * field, then writes this into the next status ring entry.
5758 *
5759 * Each ring the host uses to post buffers to the chip is described
5760 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
5761 * it is first placed into the on-chip ram. When the packet's length
5762 * is known, it walks down the TG3_BDINFO entries to select the ring.
5763 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
5764 * which is within the range of the new packet's length is chosen.
5765 *
5766 * The "separate ring for rx status" scheme may sound queer, but it makes
5767 * sense from a cache coherency perspective. If only the host writes
5768 * to the buffer post rings, and only the chip writes to the rx status
5769 * rings, then cache lines never move beyond shared-modified state.
5770 * If both the host and chip were to write into the same ring, cache line
5771 * eviction could occur since both entities want it in an exclusive state.
5772 */
Matt Carlson17375d22009-08-28 14:02:18 +00005773static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774{
Matt Carlson17375d22009-08-28 14:02:18 +00005775 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07005776 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00005777 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00005778 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07005779 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00005781 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005783 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784 /*
5785 * We need to order the read of hw_idx and the read of
5786 * the opaque cookie.
5787 */
5788 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005789 work_mask = 0;
5790 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00005791 std_prod_idx = tpr->rx_std_prod_idx;
5792 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00005794 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00005795 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796 unsigned int len;
5797 struct sk_buff *skb;
5798 dma_addr_t dma_addr;
5799 u32 opaque_key, desc_idx, *post_ptr;
Eric Dumazet9205fd92011-11-18 06:47:01 +00005800 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801
5802 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
5803 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
5804 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005805 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005806 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005807 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00005808 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07005809 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005810 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005811 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005812 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005813 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00005814 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00005815 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005816 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817
5818 work_mask |= opaque_key;
5819
5820 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
5821 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
5822 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00005823 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005824 desc_idx, *post_ptr);
5825 drop_it_no_recycle:
5826 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00005827 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828 goto next_pkt;
5829 }
5830
Eric Dumazet9205fd92011-11-18 06:47:01 +00005831 prefetch(data + TG3_RX_OFFSET(tp));
Matt Carlsonad829262008-11-21 17:16:16 -08005832 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
5833 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005834
Matt Carlsond2757fc2010-04-12 06:58:27 +00005835 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836 int skb_size;
5837
Eric Dumazet9205fd92011-11-18 06:47:01 +00005838 skb_size = tg3_alloc_rx_data(tp, tpr, opaque_key,
Matt Carlsonafc081f2009-11-13 13:03:43 +00005839 *post_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840 if (skb_size < 0)
5841 goto drop_it;
5842
Matt Carlson287be122009-08-28 13:58:46 +00005843 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005844 PCI_DMA_FROMDEVICE);
5845
Eric Dumazet9205fd92011-11-18 06:47:01 +00005846 skb = build_skb(data);
5847 if (!skb) {
5848 kfree(data);
5849 goto drop_it_no_recycle;
5850 }
5851 skb_reserve(skb, TG3_RX_OFFSET(tp));
5852 /* Ensure that the update to the data happens
Matt Carlson61e800c2010-02-17 15:16:54 +00005853 * after the usage of the old DMA mapping.
5854 */
5855 smp_wmb();
5856
Eric Dumazet9205fd92011-11-18 06:47:01 +00005857 ri->data = NULL;
Matt Carlson61e800c2010-02-17 15:16:54 +00005858
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859 } else {
Matt Carlsona3896162009-11-13 13:03:44 +00005860 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005861 desc_idx, *post_ptr);
5862
Eric Dumazet9205fd92011-11-18 06:47:01 +00005863 skb = netdev_alloc_skb(tp->dev,
5864 len + TG3_RAW_IP_ALIGN);
5865 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005866 goto drop_it_no_recycle;
5867
Eric Dumazet9205fd92011-11-18 06:47:01 +00005868 skb_reserve(skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005869 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005870 memcpy(skb->data,
5871 data + TG3_RX_OFFSET(tp),
5872 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005874 }
5875
Eric Dumazet9205fd92011-11-18 06:47:01 +00005876 skb_put(skb, len);
Michał Mirosławdc668912011-04-07 03:35:07 +00005877 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005878 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
5879 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
5880 >> RXD_TCPCSUM_SHIFT) == 0xffff))
5881 skb->ip_summed = CHECKSUM_UNNECESSARY;
5882 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005883 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005884
5885 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00005886
5887 if (len > (tp->dev->mtu + ETH_HLEN) &&
5888 skb->protocol != htons(ETH_P_8021Q)) {
5889 dev_kfree_skb(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00005890 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00005891 }
5892
Matt Carlson9dc7a112010-04-12 06:58:28 +00005893 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00005894 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
5895 __vlan_hwaccel_put_tag(skb,
5896 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00005897
Matt Carlsonbf933c82011-01-25 15:58:49 +00005898 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900 received++;
5901 budget--;
5902
5903next_pkt:
5904 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07005905
5906 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005907 tpr->rx_std_prod_idx = std_prod_idx &
5908 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00005909 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5910 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07005911 work_mask &= ~RXD_OPAQUE_RING_STD;
5912 rx_std_posted = 0;
5913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07005915 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00005916 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07005917
5918 /* Refresh hw_idx to see if there is new work */
5919 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005920 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07005921 rmb();
5922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923 }
5924
5925 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00005926 tnapi->rx_rcb_ptr = sw_idx;
5927 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005928
5929 /* Refill RX ring(s). */
Joe Perches63c3a662011-04-26 08:12:10 +00005930 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005931 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005932 tpr->rx_std_prod_idx = std_prod_idx &
5933 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005934 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5935 tpr->rx_std_prod_idx);
5936 }
5937 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005938 tpr->rx_jmb_prod_idx = jmb_prod_idx &
5939 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005940 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5941 tpr->rx_jmb_prod_idx);
5942 }
5943 mmiowb();
5944 } else if (work_mask) {
5945 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
5946 * updated before the producer indices can be updated.
5947 */
5948 smp_wmb();
5949
Matt Carlson2c49a442010-09-30 10:34:35 +00005950 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
5951 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005952
Matt Carlsone4af1af2010-02-12 14:47:05 +00005953 if (tnapi != &tp->napi[1])
5954 napi_schedule(&tp->napi[1].napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005956
5957 return received;
5958}
5959
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005960static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005961{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005962 /* handle link change and other phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00005963 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005964 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
5965
Linus Torvalds1da177e2005-04-16 15:20:36 -07005966 if (sblk->status & SD_STATUS_LINK_CHG) {
5967 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005968 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07005969 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00005970 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsondd477002008-05-25 23:45:58 -07005971 tw32_f(MAC_STATUS,
5972 (MAC_STATUS_SYNC_CHANGED |
5973 MAC_STATUS_CFG_CHANGED |
5974 MAC_STATUS_MI_COMPLETION |
5975 MAC_STATUS_LNKSTATE_CHANGED));
5976 udelay(40);
5977 } else
5978 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07005979 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005980 }
5981 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005982}
5983
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005984static int tg3_rx_prodring_xfer(struct tg3 *tp,
5985 struct tg3_rx_prodring_set *dpr,
5986 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005987{
5988 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005989 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005990
5991 while (1) {
5992 src_prod_idx = spr->rx_std_prod_idx;
5993
5994 /* Make sure updates to the rx_std_buffers[] entries and the
5995 * standard producer index are seen in the correct order.
5996 */
5997 smp_rmb();
5998
5999 if (spr->rx_std_cons_idx == src_prod_idx)
6000 break;
6001
6002 if (spr->rx_std_cons_idx < src_prod_idx)
6003 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
6004 else
Matt Carlson2c49a442010-09-30 10:34:35 +00006005 cpycnt = tp->rx_std_ring_mask + 1 -
6006 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006007
Matt Carlson2c49a442010-09-30 10:34:35 +00006008 cpycnt = min(cpycnt,
6009 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006010
6011 si = spr->rx_std_cons_idx;
6012 di = dpr->rx_std_prod_idx;
6013
Matt Carlsone92967b2010-02-12 14:47:06 +00006014 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00006015 if (dpr->rx_std_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00006016 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006017 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00006018 break;
6019 }
6020 }
6021
6022 if (!cpycnt)
6023 break;
6024
6025 /* Ensure that updates to the rx_std_buffers ring and the
6026 * shadowed hardware producer ring from tg3_recycle_skb() are
6027 * ordered correctly WRT the skb check above.
6028 */
6029 smp_rmb();
6030
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006031 memcpy(&dpr->rx_std_buffers[di],
6032 &spr->rx_std_buffers[si],
6033 cpycnt * sizeof(struct ring_info));
6034
6035 for (i = 0; i < cpycnt; i++, di++, si++) {
6036 struct tg3_rx_buffer_desc *sbd, *dbd;
6037 sbd = &spr->rx_std[si];
6038 dbd = &dpr->rx_std[di];
6039 dbd->addr_hi = sbd->addr_hi;
6040 dbd->addr_lo = sbd->addr_lo;
6041 }
6042
Matt Carlson2c49a442010-09-30 10:34:35 +00006043 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
6044 tp->rx_std_ring_mask;
6045 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
6046 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006047 }
6048
6049 while (1) {
6050 src_prod_idx = spr->rx_jmb_prod_idx;
6051
6052 /* Make sure updates to the rx_jmb_buffers[] entries and
6053 * the jumbo producer index are seen in the correct order.
6054 */
6055 smp_rmb();
6056
6057 if (spr->rx_jmb_cons_idx == src_prod_idx)
6058 break;
6059
6060 if (spr->rx_jmb_cons_idx < src_prod_idx)
6061 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
6062 else
Matt Carlson2c49a442010-09-30 10:34:35 +00006063 cpycnt = tp->rx_jmb_ring_mask + 1 -
6064 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006065
6066 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00006067 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006068
6069 si = spr->rx_jmb_cons_idx;
6070 di = dpr->rx_jmb_prod_idx;
6071
Matt Carlsone92967b2010-02-12 14:47:06 +00006072 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00006073 if (dpr->rx_jmb_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00006074 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006075 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00006076 break;
6077 }
6078 }
6079
6080 if (!cpycnt)
6081 break;
6082
6083 /* Ensure that updates to the rx_jmb_buffers ring and the
6084 * shadowed hardware producer ring from tg3_recycle_skb() are
6085 * ordered correctly WRT the skb check above.
6086 */
6087 smp_rmb();
6088
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006089 memcpy(&dpr->rx_jmb_buffers[di],
6090 &spr->rx_jmb_buffers[si],
6091 cpycnt * sizeof(struct ring_info));
6092
6093 for (i = 0; i < cpycnt; i++, di++, si++) {
6094 struct tg3_rx_buffer_desc *sbd, *dbd;
6095 sbd = &spr->rx_jmb[si].std;
6096 dbd = &dpr->rx_jmb[di].std;
6097 dbd->addr_hi = sbd->addr_hi;
6098 dbd->addr_lo = sbd->addr_lo;
6099 }
6100
Matt Carlson2c49a442010-09-30 10:34:35 +00006101 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
6102 tp->rx_jmb_ring_mask;
6103 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
6104 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006105 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006106
6107 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006108}
6109
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006110static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
6111{
6112 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006113
6114 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006115 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00006116 tg3_tx(tnapi);
Joe Perches63c3a662011-04-26 08:12:10 +00006117 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Michael Chan4fd7ab52007-10-12 01:39:50 -07006118 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006119 }
6120
Linus Torvalds1da177e2005-04-16 15:20:36 -07006121 /* run RX thread, within the bounds set by NAPI.
6122 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006123 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07006124 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006125 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00006126 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006127
Joe Perches63c3a662011-04-26 08:12:10 +00006128 if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006129 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006130 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006131 u32 std_prod_idx = dpr->rx_std_prod_idx;
6132 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006133
Matt Carlsone4af1af2010-02-12 14:47:05 +00006134 for (i = 1; i < tp->irq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006135 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00006136 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006137
6138 wmb();
6139
Matt Carlsone4af1af2010-02-12 14:47:05 +00006140 if (std_prod_idx != dpr->rx_std_prod_idx)
6141 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6142 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006143
Matt Carlsone4af1af2010-02-12 14:47:05 +00006144 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
6145 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
6146 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006147
6148 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006149
6150 if (err)
6151 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006152 }
6153
David S. Miller6f535762007-10-11 18:08:29 -07006154 return work_done;
6155}
David S. Millerf7383c22005-05-18 22:50:53 -07006156
Matt Carlsondb219972011-11-04 09:15:03 +00006157static inline void tg3_reset_task_schedule(struct tg3 *tp)
6158{
6159 if (!test_and_set_bit(TG3_FLAG_RESET_TASK_PENDING, tp->tg3_flags))
6160 schedule_work(&tp->reset_task);
6161}
6162
6163static inline void tg3_reset_task_cancel(struct tg3 *tp)
6164{
6165 cancel_work_sync(&tp->reset_task);
6166 tg3_flag_clear(tp, RESET_TASK_PENDING);
Matt Carlsonc7101352012-02-22 12:35:20 +00006167 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Matt Carlsondb219972011-11-04 09:15:03 +00006168}
6169
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006170static int tg3_poll_msix(struct napi_struct *napi, int budget)
6171{
6172 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
6173 struct tg3 *tp = tnapi->tp;
6174 int work_done = 0;
6175 struct tg3_hw_status *sblk = tnapi->hw_status;
6176
6177 while (1) {
6178 work_done = tg3_poll_work(tnapi, work_done, budget);
6179
Joe Perches63c3a662011-04-26 08:12:10 +00006180 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006181 goto tx_recovery;
6182
6183 if (unlikely(work_done >= budget))
6184 break;
6185
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006186 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006187 * to tell the hw how much work has been processed,
6188 * so we must read it before checking for more work.
6189 */
6190 tnapi->last_tag = sblk->status_tag;
6191 tnapi->last_irq_tag = tnapi->last_tag;
6192 rmb();
6193
6194 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00006195 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
6196 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006197 napi_complete(napi);
6198 /* Reenable interrupts. */
6199 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
6200 mmiowb();
6201 break;
6202 }
6203 }
6204
6205 return work_done;
6206
6207tx_recovery:
6208 /* work_done is guaranteed to be less than budget. */
6209 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00006210 tg3_reset_task_schedule(tp);
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006211 return work_done;
6212}
6213
Matt Carlsone64de4e2011-04-13 11:05:05 +00006214static void tg3_process_error(struct tg3 *tp)
6215{
6216 u32 val;
6217 bool real_error = false;
6218
Joe Perches63c3a662011-04-26 08:12:10 +00006219 if (tg3_flag(tp, ERROR_PROCESSED))
Matt Carlsone64de4e2011-04-13 11:05:05 +00006220 return;
6221
6222 /* Check Flow Attention register */
6223 val = tr32(HOSTCC_FLOW_ATTN);
6224 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
6225 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
6226 real_error = true;
6227 }
6228
6229 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
6230 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
6231 real_error = true;
6232 }
6233
6234 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
6235 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
6236 real_error = true;
6237 }
6238
6239 if (!real_error)
6240 return;
6241
6242 tg3_dump_state(tp);
6243
Joe Perches63c3a662011-04-26 08:12:10 +00006244 tg3_flag_set(tp, ERROR_PROCESSED);
Matt Carlsondb219972011-11-04 09:15:03 +00006245 tg3_reset_task_schedule(tp);
Matt Carlsone64de4e2011-04-13 11:05:05 +00006246}
6247
David S. Miller6f535762007-10-11 18:08:29 -07006248static int tg3_poll(struct napi_struct *napi, int budget)
6249{
Matt Carlson8ef04422009-08-28 14:01:37 +00006250 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
6251 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07006252 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00006253 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07006254
6255 while (1) {
Matt Carlsone64de4e2011-04-13 11:05:05 +00006256 if (sblk->status & SD_STATUS_ERROR)
6257 tg3_process_error(tp);
6258
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006259 tg3_poll_link(tp);
6260
Matt Carlson17375d22009-08-28 14:02:18 +00006261 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07006262
Joe Perches63c3a662011-04-26 08:12:10 +00006263 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
David S. Miller6f535762007-10-11 18:08:29 -07006264 goto tx_recovery;
6265
6266 if (unlikely(work_done >= budget))
6267 break;
6268
Joe Perches63c3a662011-04-26 08:12:10 +00006269 if (tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson17375d22009-08-28 14:02:18 +00006270 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07006271 * to tell the hw how much work has been processed,
6272 * so we must read it before checking for more work.
6273 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006274 tnapi->last_tag = sblk->status_tag;
6275 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07006276 rmb();
6277 } else
6278 sblk->status &= ~SD_STATUS_UPDATED;
6279
Matt Carlson17375d22009-08-28 14:02:18 +00006280 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08006281 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00006282 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07006283 break;
6284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006285 }
6286
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006287 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07006288
6289tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07006290 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08006291 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00006292 tg3_reset_task_schedule(tp);
Michael Chan4fd7ab52007-10-12 01:39:50 -07006293 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006294}
6295
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006296static void tg3_napi_disable(struct tg3 *tp)
6297{
6298 int i;
6299
6300 for (i = tp->irq_cnt - 1; i >= 0; i--)
6301 napi_disable(&tp->napi[i].napi);
6302}
6303
6304static void tg3_napi_enable(struct tg3 *tp)
6305{
6306 int i;
6307
6308 for (i = 0; i < tp->irq_cnt; i++)
6309 napi_enable(&tp->napi[i].napi);
6310}
6311
6312static void tg3_napi_init(struct tg3 *tp)
6313{
6314 int i;
6315
6316 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
6317 for (i = 1; i < tp->irq_cnt; i++)
6318 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
6319}
6320
6321static void tg3_napi_fini(struct tg3 *tp)
6322{
6323 int i;
6324
6325 for (i = 0; i < tp->irq_cnt; i++)
6326 netif_napi_del(&tp->napi[i].napi);
6327}
6328
6329static inline void tg3_netif_stop(struct tg3 *tp)
6330{
6331 tp->dev->trans_start = jiffies; /* prevent tx timeout */
6332 tg3_napi_disable(tp);
6333 netif_tx_disable(tp->dev);
6334}
6335
6336static inline void tg3_netif_start(struct tg3 *tp)
6337{
6338 /* NOTE: unconditional netif_tx_wake_all_queues is only
6339 * appropriate so long as all callers are assured to
6340 * have free tx slots (such as after tg3_init_hw)
6341 */
6342 netif_tx_wake_all_queues(tp->dev);
6343
6344 tg3_napi_enable(tp);
6345 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
6346 tg3_enable_ints(tp);
6347}
6348
David S. Millerf47c11e2005-06-24 20:18:35 -07006349static void tg3_irq_quiesce(struct tg3 *tp)
6350{
Matt Carlson4f125f42009-09-01 12:55:02 +00006351 int i;
6352
David S. Millerf47c11e2005-06-24 20:18:35 -07006353 BUG_ON(tp->irq_sync);
6354
6355 tp->irq_sync = 1;
6356 smp_mb();
6357
Matt Carlson4f125f42009-09-01 12:55:02 +00006358 for (i = 0; i < tp->irq_cnt; i++)
6359 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07006360}
6361
David S. Millerf47c11e2005-06-24 20:18:35 -07006362/* Fully shutdown all tg3 driver activity elsewhere in the system.
6363 * If irq_sync is non-zero, then the IRQ handler must be synchronized
6364 * with as well. Most of the time, this is not necessary except when
6365 * shutting down the device.
6366 */
6367static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
6368{
Michael Chan46966542007-07-11 19:47:19 -07006369 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07006370 if (irq_sync)
6371 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07006372}
6373
6374static inline void tg3_full_unlock(struct tg3 *tp)
6375{
David S. Millerf47c11e2005-06-24 20:18:35 -07006376 spin_unlock_bh(&tp->lock);
6377}
6378
Michael Chanfcfa0a32006-03-20 22:28:41 -08006379/* One-shot MSI handler - Chip automatically disables interrupt
6380 * after sending MSI so driver doesn't have to do it.
6381 */
David Howells7d12e782006-10-05 14:55:46 +01006382static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08006383{
Matt Carlson09943a12009-08-28 14:01:57 +00006384 struct tg3_napi *tnapi = dev_id;
6385 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08006386
Matt Carlson898a56f2009-08-28 14:02:40 +00006387 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006388 if (tnapi->rx_rcb)
6389 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08006390
6391 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00006392 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08006393
6394 return IRQ_HANDLED;
6395}
6396
Michael Chan88b06bc22005-04-21 17:13:25 -07006397/* MSI ISR - No need to check for interrupt sharing and no need to
6398 * flush status block and interrupt mailbox. PCI ordering rules
6399 * guarantee that MSI will arrive after the status block.
6400 */
David Howells7d12e782006-10-05 14:55:46 +01006401static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07006402{
Matt Carlson09943a12009-08-28 14:01:57 +00006403 struct tg3_napi *tnapi = dev_id;
6404 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07006405
Matt Carlson898a56f2009-08-28 14:02:40 +00006406 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006407 if (tnapi->rx_rcb)
6408 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07006409 /*
David S. Millerfac9b832005-05-18 22:46:34 -07006410 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07006411 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07006412 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07006413 * NIC to stop sending us irqs, engaging "in-intr-handler"
6414 * event coalescing.
6415 */
Matt Carlson5b39de92011-08-31 11:44:50 +00006416 tw32_mailbox(tnapi->int_mbox, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07006417 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00006418 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07006419
Michael Chan88b06bc22005-04-21 17:13:25 -07006420 return IRQ_RETVAL(1);
6421}
6422
David Howells7d12e782006-10-05 14:55:46 +01006423static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006424{
Matt Carlson09943a12009-08-28 14:01:57 +00006425 struct tg3_napi *tnapi = dev_id;
6426 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006427 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006428 unsigned int handled = 1;
6429
Linus Torvalds1da177e2005-04-16 15:20:36 -07006430 /* In INTx mode, it is possible for the interrupt to arrive at
6431 * the CPU before the status block posted prior to the interrupt.
6432 * Reading the PCI State register will confirm whether the
6433 * interrupt is ours and will flush the status block.
6434 */
Michael Chand18edcb2007-03-24 20:57:11 -07006435 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
Joe Perches63c3a662011-04-26 08:12:10 +00006436 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07006437 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
6438 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07006439 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07006440 }
Michael Chand18edcb2007-03-24 20:57:11 -07006441 }
6442
6443 /*
6444 * Writing any value to intr-mbox-0 clears PCI INTA# and
6445 * chip-internal interrupt pending events.
6446 * Writing non-zero to intr-mbox-0 additional tells the
6447 * NIC to stop sending us irqs, engaging "in-intr-handler"
6448 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07006449 *
6450 * Flush the mailbox to de-assert the IRQ immediately to prevent
6451 * spurious interrupts. The flush impacts performance but
6452 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07006453 */
Michael Chanc04cb342007-05-07 00:26:15 -07006454 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07006455 if (tg3_irq_sync(tp))
6456 goto out;
6457 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00006458 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00006459 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00006460 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07006461 } else {
6462 /* No work, shared interrupt perhaps? re-enable
6463 * interrupts, and flush that PCI write
6464 */
6465 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
6466 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07006467 }
David S. Millerf47c11e2005-06-24 20:18:35 -07006468out:
David S. Millerfac9b832005-05-18 22:46:34 -07006469 return IRQ_RETVAL(handled);
6470}
6471
David Howells7d12e782006-10-05 14:55:46 +01006472static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07006473{
Matt Carlson09943a12009-08-28 14:01:57 +00006474 struct tg3_napi *tnapi = dev_id;
6475 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006476 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07006477 unsigned int handled = 1;
6478
David S. Millerfac9b832005-05-18 22:46:34 -07006479 /* In INTx mode, it is possible for the interrupt to arrive at
6480 * the CPU before the status block posted prior to the interrupt.
6481 * Reading the PCI State register will confirm whether the
6482 * interrupt is ours and will flush the status block.
6483 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006484 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Joe Perches63c3a662011-04-26 08:12:10 +00006485 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07006486 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
6487 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07006488 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006489 }
Michael Chand18edcb2007-03-24 20:57:11 -07006490 }
6491
6492 /*
6493 * writing any value to intr-mbox-0 clears PCI INTA# and
6494 * chip-internal interrupt pending events.
6495 * writing non-zero to intr-mbox-0 additional tells the
6496 * NIC to stop sending us irqs, engaging "in-intr-handler"
6497 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07006498 *
6499 * Flush the mailbox to de-assert the IRQ immediately to prevent
6500 * spurious interrupts. The flush impacts performance but
6501 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07006502 */
Michael Chanc04cb342007-05-07 00:26:15 -07006503 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00006504
6505 /*
6506 * In a shared interrupt configuration, sometimes other devices'
6507 * interrupts will scream. We record the current status tag here
6508 * so that the above check can report that the screaming interrupts
6509 * are unhandled. Eventually they will be silenced.
6510 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006511 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00006512
Michael Chand18edcb2007-03-24 20:57:11 -07006513 if (tg3_irq_sync(tp))
6514 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00006515
Matt Carlson72334482009-08-28 14:03:01 +00006516 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00006517
Matt Carlson09943a12009-08-28 14:01:57 +00006518 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00006519
David S. Millerf47c11e2005-06-24 20:18:35 -07006520out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006521 return IRQ_RETVAL(handled);
6522}
6523
Michael Chan79381092005-04-21 17:13:59 -07006524/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01006525static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07006526{
Matt Carlson09943a12009-08-28 14:01:57 +00006527 struct tg3_napi *tnapi = dev_id;
6528 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006529 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07006530
Michael Chanf9804dd2005-09-27 12:13:10 -07006531 if ((sblk->status & SD_STATUS_UPDATED) ||
6532 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07006533 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07006534 return IRQ_RETVAL(1);
6535 }
6536 return IRQ_RETVAL(0);
6537}
6538
Linus Torvalds1da177e2005-04-16 15:20:36 -07006539#ifdef CONFIG_NET_POLL_CONTROLLER
6540static void tg3_poll_controller(struct net_device *dev)
6541{
Matt Carlson4f125f42009-09-01 12:55:02 +00006542 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07006543 struct tg3 *tp = netdev_priv(dev);
6544
Matt Carlson4f125f42009-09-01 12:55:02 +00006545 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00006546 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006547}
6548#endif
6549
Linus Torvalds1da177e2005-04-16 15:20:36 -07006550static void tg3_tx_timeout(struct net_device *dev)
6551{
6552 struct tg3 *tp = netdev_priv(dev);
6553
Michael Chanb0408752007-02-13 12:18:30 -08006554 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00006555 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00006556 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08006557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006558
Matt Carlsondb219972011-11-04 09:15:03 +00006559 tg3_reset_task_schedule(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006560}
6561
Michael Chanc58ec932005-09-17 00:46:27 -07006562/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
6563static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
6564{
6565 u32 base = (u32) mapping & 0xffffffff;
6566
Eric Dumazet807540b2010-09-23 05:40:09 +00006567 return (base > 0xffffdcc0) && (base + len + 8 < base);
Michael Chanc58ec932005-09-17 00:46:27 -07006568}
6569
Michael Chan72f2afb2006-03-06 19:28:35 -08006570/* Test for DMA addresses > 40-bit */
6571static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
6572 int len)
6573{
6574#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Joe Perches63c3a662011-04-26 08:12:10 +00006575 if (tg3_flag(tp, 40BIT_DMA_BUG))
Eric Dumazet807540b2010-09-23 05:40:09 +00006576 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08006577 return 0;
6578#else
6579 return 0;
6580#endif
6581}
6582
Matt Carlsond1a3b732011-07-27 14:20:51 +00006583static inline void tg3_tx_set_bd(struct tg3_tx_buffer_desc *txbd,
Matt Carlson92cd3a12011-07-27 14:20:47 +00006584 dma_addr_t mapping, u32 len, u32 flags,
6585 u32 mss, u32 vlan)
Matt Carlson2ffcc982011-05-19 12:12:44 +00006586{
Matt Carlson92cd3a12011-07-27 14:20:47 +00006587 txbd->addr_hi = ((u64) mapping >> 32);
6588 txbd->addr_lo = ((u64) mapping & 0xffffffff);
6589 txbd->len_flags = (len << TXD_LEN_SHIFT) | (flags & 0x0000ffff);
6590 txbd->vlan_tag = (mss << TXD_MSS_SHIFT) | (vlan << TXD_VLAN_TAG_SHIFT);
Matt Carlson2ffcc982011-05-19 12:12:44 +00006591}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006592
Matt Carlson84b67b22011-07-27 14:20:52 +00006593static bool tg3_tx_frag_set(struct tg3_napi *tnapi, u32 *entry, u32 *budget,
Matt Carlsond1a3b732011-07-27 14:20:51 +00006594 dma_addr_t map, u32 len, u32 flags,
6595 u32 mss, u32 vlan)
6596{
6597 struct tg3 *tp = tnapi->tp;
6598 bool hwbug = false;
6599
6600 if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
Rusty Russell3db1cd52011-12-19 13:56:45 +00006601 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00006602
6603 if (tg3_4g_overflow_test(map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00006604 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00006605
6606 if (tg3_40bit_overflow_test(tp, map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00006607 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00006608
Matt Carlsona4cb4282011-12-14 11:09:58 +00006609 if (tp->dma_limit) {
Matt Carlsonb9e45482011-11-04 09:14:59 +00006610 u32 prvidx = *entry;
Matt Carlsone31aa982011-07-27 14:20:53 +00006611 u32 tmp_flag = flags & ~TXD_FLAG_END;
Matt Carlsona4cb4282011-12-14 11:09:58 +00006612 while (len > tp->dma_limit && *budget) {
6613 u32 frag_len = tp->dma_limit;
6614 len -= tp->dma_limit;
Matt Carlsone31aa982011-07-27 14:20:53 +00006615
Matt Carlsonb9e45482011-11-04 09:14:59 +00006616 /* Avoid the 8byte DMA problem */
6617 if (len <= 8) {
Matt Carlsona4cb4282011-12-14 11:09:58 +00006618 len += tp->dma_limit / 2;
6619 frag_len = tp->dma_limit / 2;
Matt Carlsone31aa982011-07-27 14:20:53 +00006620 }
6621
Matt Carlsonb9e45482011-11-04 09:14:59 +00006622 tnapi->tx_buffers[*entry].fragmented = true;
6623
6624 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6625 frag_len, tmp_flag, mss, vlan);
6626 *budget -= 1;
6627 prvidx = *entry;
6628 *entry = NEXT_TX(*entry);
6629
Matt Carlsone31aa982011-07-27 14:20:53 +00006630 map += frag_len;
6631 }
6632
6633 if (len) {
6634 if (*budget) {
6635 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6636 len, flags, mss, vlan);
Matt Carlsonb9e45482011-11-04 09:14:59 +00006637 *budget -= 1;
Matt Carlsone31aa982011-07-27 14:20:53 +00006638 *entry = NEXT_TX(*entry);
6639 } else {
Rusty Russell3db1cd52011-12-19 13:56:45 +00006640 hwbug = true;
Matt Carlsonb9e45482011-11-04 09:14:59 +00006641 tnapi->tx_buffers[prvidx].fragmented = false;
Matt Carlsone31aa982011-07-27 14:20:53 +00006642 }
6643 }
6644 } else {
Matt Carlson84b67b22011-07-27 14:20:52 +00006645 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6646 len, flags, mss, vlan);
Matt Carlsone31aa982011-07-27 14:20:53 +00006647 *entry = NEXT_TX(*entry);
6648 }
Matt Carlsond1a3b732011-07-27 14:20:51 +00006649
6650 return hwbug;
6651}
6652
Matt Carlson0d681b22011-07-27 14:20:49 +00006653static void tg3_tx_skb_unmap(struct tg3_napi *tnapi, u32 entry, int last)
Matt Carlson432aa7e2011-05-19 12:12:45 +00006654{
6655 int i;
Matt Carlson0d681b22011-07-27 14:20:49 +00006656 struct sk_buff *skb;
Matt Carlsondf8944c2011-07-27 14:20:46 +00006657 struct tg3_tx_ring_info *txb = &tnapi->tx_buffers[entry];
Matt Carlson432aa7e2011-05-19 12:12:45 +00006658
Matt Carlson0d681b22011-07-27 14:20:49 +00006659 skb = txb->skb;
6660 txb->skb = NULL;
6661
Matt Carlson432aa7e2011-05-19 12:12:45 +00006662 pci_unmap_single(tnapi->tp->pdev,
6663 dma_unmap_addr(txb, mapping),
6664 skb_headlen(skb),
6665 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00006666
6667 while (txb->fragmented) {
6668 txb->fragmented = false;
6669 entry = NEXT_TX(entry);
6670 txb = &tnapi->tx_buffers[entry];
6671 }
6672
Matt Carlsonba1142e2011-11-04 09:15:00 +00006673 for (i = 0; i <= last; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00006674 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Matt Carlson432aa7e2011-05-19 12:12:45 +00006675
6676 entry = NEXT_TX(entry);
6677 txb = &tnapi->tx_buffers[entry];
6678
6679 pci_unmap_page(tnapi->tp->pdev,
6680 dma_unmap_addr(txb, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00006681 skb_frag_size(frag), PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00006682
6683 while (txb->fragmented) {
6684 txb->fragmented = false;
6685 entry = NEXT_TX(entry);
6686 txb = &tnapi->tx_buffers[entry];
6687 }
Matt Carlson432aa7e2011-05-19 12:12:45 +00006688 }
6689}
6690
Michael Chan72f2afb2006-03-06 19:28:35 -08006691/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006692static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
David S. Miller1805b2f2011-10-24 18:18:09 -04006693 struct sk_buff **pskb,
Matt Carlson84b67b22011-07-27 14:20:52 +00006694 u32 *entry, u32 *budget,
Matt Carlson92cd3a12011-07-27 14:20:47 +00006695 u32 base_flags, u32 mss, u32 vlan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006696{
Matt Carlson24f4efd2009-11-13 13:03:35 +00006697 struct tg3 *tp = tnapi->tp;
David S. Miller1805b2f2011-10-24 18:18:09 -04006698 struct sk_buff *new_skb, *skb = *pskb;
Michael Chanc58ec932005-09-17 00:46:27 -07006699 dma_addr_t new_addr = 0;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006700 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006701
Matt Carlson41588ba2008-04-19 18:12:33 -07006702 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
6703 new_skb = skb_copy(skb, GFP_ATOMIC);
6704 else {
6705 int more_headroom = 4 - ((unsigned long)skb->data & 3);
6706
6707 new_skb = skb_copy_expand(skb,
6708 skb_headroom(skb) + more_headroom,
6709 skb_tailroom(skb), GFP_ATOMIC);
6710 }
6711
Linus Torvalds1da177e2005-04-16 15:20:36 -07006712 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07006713 ret = -1;
6714 } else {
6715 /* New SKB is guaranteed to be linear. */
Alexander Duyckf4188d82009-12-02 16:48:38 +00006716 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
6717 PCI_DMA_TODEVICE);
6718 /* Make sure the mapping succeeded */
6719 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00006720 dev_kfree_skb(new_skb);
Michael Chanc58ec932005-09-17 00:46:27 -07006721 ret = -1;
Michael Chanc58ec932005-09-17 00:46:27 -07006722 } else {
Matt Carlsonb9e45482011-11-04 09:14:59 +00006723 u32 save_entry = *entry;
6724
Matt Carlson92cd3a12011-07-27 14:20:47 +00006725 base_flags |= TXD_FLAG_END;
6726
Matt Carlson84b67b22011-07-27 14:20:52 +00006727 tnapi->tx_buffers[*entry].skb = new_skb;
6728 dma_unmap_addr_set(&tnapi->tx_buffers[*entry],
Matt Carlson432aa7e2011-05-19 12:12:45 +00006729 mapping, new_addr);
6730
Matt Carlson84b67b22011-07-27 14:20:52 +00006731 if (tg3_tx_frag_set(tnapi, entry, budget, new_addr,
Matt Carlsond1a3b732011-07-27 14:20:51 +00006732 new_skb->len, base_flags,
6733 mss, vlan)) {
Matt Carlsonba1142e2011-11-04 09:15:00 +00006734 tg3_tx_skb_unmap(tnapi, save_entry, -1);
Matt Carlsond1a3b732011-07-27 14:20:51 +00006735 dev_kfree_skb(new_skb);
6736 ret = -1;
6737 }
Michael Chanc58ec932005-09-17 00:46:27 -07006738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006739 }
6740
Linus Torvalds1da177e2005-04-16 15:20:36 -07006741 dev_kfree_skb(skb);
David S. Miller1805b2f2011-10-24 18:18:09 -04006742 *pskb = new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07006743 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006744}
6745
Matt Carlson2ffcc982011-05-19 12:12:44 +00006746static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07006747
6748/* Use GSO to workaround a rare TSO bug that may be triggered when the
6749 * TSO header is greater than 80 bytes.
6750 */
6751static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
6752{
6753 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006754 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07006755
6756 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006757 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07006758 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006759
6760 /* netif_tx_stop_queue() must be done before checking
6761 * checking tx index in tg3_tx_avail() below, because in
6762 * tg3_tx(), we update tx index before checking for
6763 * netif_tx_queue_stopped().
6764 */
6765 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006766 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08006767 return NETDEV_TX_BUSY;
6768
6769 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07006770 }
6771
6772 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07006773 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07006774 goto tg3_tso_bug_end;
6775
6776 do {
6777 nskb = segs;
6778 segs = segs->next;
6779 nskb->next = NULL;
Matt Carlson2ffcc982011-05-19 12:12:44 +00006780 tg3_start_xmit(nskb, tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07006781 } while (segs);
6782
6783tg3_tso_bug_end:
6784 dev_kfree_skb(skb);
6785
6786 return NETDEV_TX_OK;
6787}
Michael Chan52c0fd82006-06-29 20:15:54 -07006788
Michael Chan5a6f3072006-03-20 22:28:05 -08006789/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
Joe Perches63c3a662011-04-26 08:12:10 +00006790 * support TG3_FLAG_HW_TSO_1 or firmware TSO only.
Michael Chan5a6f3072006-03-20 22:28:05 -08006791 */
Matt Carlson2ffcc982011-05-19 12:12:44 +00006792static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08006793{
6794 struct tg3 *tp = netdev_priv(dev);
Matt Carlson92cd3a12011-07-27 14:20:47 +00006795 u32 len, entry, base_flags, mss, vlan = 0;
Matt Carlson84b67b22011-07-27 14:20:52 +00006796 u32 budget;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006797 int i = -1, would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07006798 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00006799 struct tg3_napi *tnapi;
6800 struct netdev_queue *txq;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006801 unsigned int last;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006802
Matt Carlson24f4efd2009-11-13 13:03:35 +00006803 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
6804 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Joe Perches63c3a662011-04-26 08:12:10 +00006805 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006806 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006807
Matt Carlson84b67b22011-07-27 14:20:52 +00006808 budget = tg3_tx_avail(tnapi);
6809
Michael Chan00b70502006-06-17 21:58:45 -07006810 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006811 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07006812 * interrupt. Furthermore, IRQ processing runs lockless so we have
6813 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07006814 */
Matt Carlson84b67b22011-07-27 14:20:52 +00006815 if (unlikely(budget <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006816 if (!netif_tx_queue_stopped(txq)) {
6817 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08006818
6819 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00006820 netdev_err(dev,
6821 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08006822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006823 return NETDEV_TX_BUSY;
6824 }
6825
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006826 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006827 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07006828 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006829 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00006830
Matt Carlsonbe98da62010-07-11 09:31:46 +00006831 mss = skb_shinfo(skb)->gso_size;
6832 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006833 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00006834 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006835
6836 if (skb_header_cloned(skb) &&
Eric Dumazet48855432011-10-24 07:53:03 +00006837 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
6838 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006839
Matt Carlson34195c32010-07-11 09:31:42 +00006840 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07006841 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006842
Eric Dumazeta5a11952012-01-23 01:22:09 +00006843 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb) - ETH_HLEN;
Matt Carlson34195c32010-07-11 09:31:42 +00006844
Eric Dumazeta5a11952012-01-23 01:22:09 +00006845 if (!skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00006846 iph->check = 0;
6847 iph->tot_len = htons(mss + hdr_len);
6848 }
6849
Michael Chan52c0fd82006-06-29 20:15:54 -07006850 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Joe Perches63c3a662011-04-26 08:12:10 +00006851 tg3_flag(tp, TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00006852 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07006853
Linus Torvalds1da177e2005-04-16 15:20:36 -07006854 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
6855 TXD_FLAG_CPU_POST_DMA);
6856
Joe Perches63c3a662011-04-26 08:12:10 +00006857 if (tg3_flag(tp, HW_TSO_1) ||
6858 tg3_flag(tp, HW_TSO_2) ||
6859 tg3_flag(tp, HW_TSO_3)) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006860 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006861 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006862 } else
6863 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
6864 iph->daddr, 0,
6865 IPPROTO_TCP,
6866 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006867
Joe Perches63c3a662011-04-26 08:12:10 +00006868 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlson615774f2009-11-13 13:03:39 +00006869 mss |= (hdr_len & 0xc) << 12;
6870 if (hdr_len & 0x10)
6871 base_flags |= 0x00000010;
6872 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +00006873 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006874 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +00006875 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006876 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006877 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006878 int tsflags;
6879
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006880 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006881 mss |= (tsflags << 11);
6882 }
6883 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006884 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006885 int tsflags;
6886
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006887 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006888 base_flags |= tsflags << 12;
6889 }
6890 }
6891 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00006892
Matt Carlson93a700a2011-08-31 11:44:54 +00006893 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
6894 !mss && skb->len > VLAN_ETH_FRAME_LEN)
6895 base_flags |= TXD_FLAG_JMB_PKT;
6896
Matt Carlson92cd3a12011-07-27 14:20:47 +00006897 if (vlan_tx_tag_present(skb)) {
6898 base_flags |= TXD_FLAG_VLAN;
6899 vlan = vlan_tx_tag_get(skb);
6900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006901
Alexander Duyckf4188d82009-12-02 16:48:38 +00006902 len = skb_headlen(skb);
6903
6904 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
Eric Dumazet48855432011-10-24 07:53:03 +00006905 if (pci_dma_mapping_error(tp->pdev, mapping))
6906 goto drop;
6907
David S. Miller90079ce2008-09-11 04:52:51 -07006908
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006909 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006910 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006911
6912 would_hit_hwbug = 0;
6913
Joe Perches63c3a662011-04-26 08:12:10 +00006914 if (tg3_flag(tp, 5701_DMA_BUG))
Michael Chanc58ec932005-09-17 00:46:27 -07006915 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006916
Matt Carlson84b67b22011-07-27 14:20:52 +00006917 if (tg3_tx_frag_set(tnapi, &entry, &budget, mapping, len, base_flags |
Matt Carlsond1a3b732011-07-27 14:20:51 +00006918 ((skb_shinfo(skb)->nr_frags == 0) ? TXD_FLAG_END : 0),
Matt Carlsonba1142e2011-11-04 09:15:00 +00006919 mss, vlan)) {
Matt Carlsond1a3b732011-07-27 14:20:51 +00006920 would_hit_hwbug = 1;
Matt Carlsonba1142e2011-11-04 09:15:00 +00006921 } else if (skb_shinfo(skb)->nr_frags > 0) {
Matt Carlson92cd3a12011-07-27 14:20:47 +00006922 u32 tmp_mss = mss;
6923
6924 if (!tg3_flag(tp, HW_TSO_1) &&
6925 !tg3_flag(tp, HW_TSO_2) &&
6926 !tg3_flag(tp, HW_TSO_3))
6927 tmp_mss = 0;
6928
Matt Carlsonc5665a52012-02-13 10:20:12 +00006929 /* Now loop through additional data
6930 * fragments, and queue them.
6931 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006932 last = skb_shinfo(skb)->nr_frags - 1;
6933 for (i = 0; i <= last; i++) {
6934 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6935
Eric Dumazet9e903e02011-10-18 21:00:24 +00006936 len = skb_frag_size(frag);
Ian Campbelldc234d02011-08-24 22:28:11 +00006937 mapping = skb_frag_dma_map(&tp->pdev->dev, frag, 0,
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01006938 len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006939
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006940 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006941 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00006942 mapping);
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01006943 if (dma_mapping_error(&tp->pdev->dev, mapping))
Alexander Duyckf4188d82009-12-02 16:48:38 +00006944 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006945
Matt Carlsonb9e45482011-11-04 09:14:59 +00006946 if (!budget ||
6947 tg3_tx_frag_set(tnapi, &entry, &budget, mapping,
Matt Carlson84b67b22011-07-27 14:20:52 +00006948 len, base_flags |
6949 ((i == last) ? TXD_FLAG_END : 0),
Matt Carlsonb9e45482011-11-04 09:14:59 +00006950 tmp_mss, vlan)) {
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006951 would_hit_hwbug = 1;
Matt Carlsonb9e45482011-11-04 09:14:59 +00006952 break;
6953 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006954 }
6955 }
6956
6957 if (would_hit_hwbug) {
Matt Carlson0d681b22011-07-27 14:20:49 +00006958 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006959
6960 /* If the workaround fails due to memory/mapping
6961 * failure, silently drop this packet.
6962 */
Matt Carlson84b67b22011-07-27 14:20:52 +00006963 entry = tnapi->tx_prod;
6964 budget = tg3_tx_avail(tnapi);
David S. Miller1805b2f2011-10-24 18:18:09 -04006965 if (tigon3_dma_hwbug_workaround(tnapi, &skb, &entry, &budget,
Matt Carlson84b67b22011-07-27 14:20:52 +00006966 base_flags, mss, vlan))
Eric Dumazet48855432011-10-24 07:53:03 +00006967 goto drop_nofree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006968 }
6969
Richard Cochrand515b452011-06-19 03:31:41 +00006970 skb_tx_timestamp(skb);
Tom Herbert298376d2011-11-28 16:33:30 +00006971 netdev_sent_queue(tp->dev, skb->len);
Richard Cochrand515b452011-06-19 03:31:41 +00006972
Linus Torvalds1da177e2005-04-16 15:20:36 -07006973 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006974 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006975
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006976 tnapi->tx_prod = entry;
6977 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006978 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006979
6980 /* netif_tx_stop_queue() must be done before checking
6981 * checking tx index in tg3_tx_avail() below, because in
6982 * tg3_tx(), we update tx index before checking for
6983 * netif_tx_queue_stopped().
6984 */
6985 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006986 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006987 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07006988 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006989
Eric Dumazetcdd0db02009-05-28 00:00:41 +00006990 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006991 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006992
6993dma_error:
Matt Carlsonba1142e2011-11-04 09:15:00 +00006994 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, --i);
Matt Carlson432aa7e2011-05-19 12:12:45 +00006995 tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
Eric Dumazet48855432011-10-24 07:53:03 +00006996drop:
6997 dev_kfree_skb(skb);
6998drop_nofree:
6999 tp->tx_dropped++;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007000 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007001}
7002
Matt Carlson6e01b202011-08-19 13:58:20 +00007003static void tg3_mac_loopback(struct tg3 *tp, bool enable)
7004{
7005 if (enable) {
7006 tp->mac_mode &= ~(MAC_MODE_HALF_DUPLEX |
7007 MAC_MODE_PORT_MODE_MASK);
7008
7009 tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
7010
7011 if (!tg3_flag(tp, 5705_PLUS))
7012 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
7013
7014 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
7015 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
7016 else
7017 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
7018 } else {
7019 tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
7020
7021 if (tg3_flag(tp, 5705_PLUS) ||
7022 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) ||
7023 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
7024 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
7025 }
7026
7027 tw32(MAC_MODE, tp->mac_mode);
7028 udelay(40);
7029}
7030
Matt Carlson941ec902011-08-19 13:58:23 +00007031static int tg3_phy_lpbk_set(struct tg3 *tp, u32 speed, bool extlpbk)
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007032{
Matt Carlson941ec902011-08-19 13:58:23 +00007033 u32 val, bmcr, mac_mode, ptest = 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007034
7035 tg3_phy_toggle_apd(tp, false);
7036 tg3_phy_toggle_automdix(tp, 0);
7037
Matt Carlson941ec902011-08-19 13:58:23 +00007038 if (extlpbk && tg3_phy_set_extloopbk(tp))
7039 return -EIO;
7040
7041 bmcr = BMCR_FULLDPLX;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007042 switch (speed) {
7043 case SPEED_10:
7044 break;
7045 case SPEED_100:
7046 bmcr |= BMCR_SPEED100;
7047 break;
7048 case SPEED_1000:
7049 default:
7050 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
7051 speed = SPEED_100;
7052 bmcr |= BMCR_SPEED100;
7053 } else {
7054 speed = SPEED_1000;
7055 bmcr |= BMCR_SPEED1000;
7056 }
7057 }
7058
Matt Carlson941ec902011-08-19 13:58:23 +00007059 if (extlpbk) {
7060 if (!(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
7061 tg3_readphy(tp, MII_CTRL1000, &val);
7062 val |= CTL1000_AS_MASTER |
7063 CTL1000_ENABLE_MASTER;
7064 tg3_writephy(tp, MII_CTRL1000, val);
7065 } else {
7066 ptest = MII_TG3_FET_PTEST_TRIM_SEL |
7067 MII_TG3_FET_PTEST_TRIM_2;
7068 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest);
7069 }
7070 } else
7071 bmcr |= BMCR_LOOPBACK;
7072
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007073 tg3_writephy(tp, MII_BMCR, bmcr);
7074
7075 /* The write needs to be flushed for the FETs */
7076 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
7077 tg3_readphy(tp, MII_BMCR, &bmcr);
7078
7079 udelay(40);
7080
7081 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
7082 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Matt Carlson941ec902011-08-19 13:58:23 +00007083 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest |
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007084 MII_TG3_FET_PTEST_FRC_TX_LINK |
7085 MII_TG3_FET_PTEST_FRC_TX_LOCK);
7086
7087 /* The write needs to be flushed for the AC131 */
7088 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
7089 }
7090
7091 /* Reset to prevent losing 1st rx packet intermittently */
7092 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
7093 tg3_flag(tp, 5780_CLASS)) {
7094 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
7095 udelay(10);
7096 tw32_f(MAC_RX_MODE, tp->rx_mode);
7097 }
7098
7099 mac_mode = tp->mac_mode &
7100 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
7101 if (speed == SPEED_1000)
7102 mac_mode |= MAC_MODE_PORT_MODE_GMII;
7103 else
7104 mac_mode |= MAC_MODE_PORT_MODE_MII;
7105
7106 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
7107 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
7108
7109 if (masked_phy_id == TG3_PHY_ID_BCM5401)
7110 mac_mode &= ~MAC_MODE_LINK_POLARITY;
7111 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
7112 mac_mode |= MAC_MODE_LINK_POLARITY;
7113
7114 tg3_writephy(tp, MII_TG3_EXT_CTRL,
7115 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
7116 }
7117
7118 tw32(MAC_MODE, mac_mode);
7119 udelay(40);
Matt Carlson941ec902011-08-19 13:58:23 +00007120
7121 return 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007122}
7123
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007124static void tg3_set_loopback(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007125{
7126 struct tg3 *tp = netdev_priv(dev);
7127
7128 if (features & NETIF_F_LOOPBACK) {
7129 if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
7130 return;
7131
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007132 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00007133 tg3_mac_loopback(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007134 netif_carrier_on(tp->dev);
7135 spin_unlock_bh(&tp->lock);
7136 netdev_info(dev, "Internal MAC loopback mode enabled.\n");
7137 } else {
7138 if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
7139 return;
7140
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007141 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00007142 tg3_mac_loopback(tp, false);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007143 /* Force link status check */
7144 tg3_setup_phy(tp, 1);
7145 spin_unlock_bh(&tp->lock);
7146 netdev_info(dev, "Internal MAC loopback mode disabled.\n");
7147 }
7148}
7149
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007150static netdev_features_t tg3_fix_features(struct net_device *dev,
7151 netdev_features_t features)
Michał Mirosławdc668912011-04-07 03:35:07 +00007152{
7153 struct tg3 *tp = netdev_priv(dev);
7154
Joe Perches63c3a662011-04-26 08:12:10 +00007155 if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
Michał Mirosławdc668912011-04-07 03:35:07 +00007156 features &= ~NETIF_F_ALL_TSO;
7157
7158 return features;
7159}
7160
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007161static int tg3_set_features(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007162{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007163 netdev_features_t changed = dev->features ^ features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007164
7165 if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
7166 tg3_set_loopback(dev, features);
7167
7168 return 0;
7169}
7170
Matt Carlson21f581a2009-08-28 14:00:25 +00007171static void tg3_rx_prodring_free(struct tg3 *tp,
7172 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007173{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007174 int i;
7175
Matt Carlson8fea32b2010-09-15 08:59:58 +00007176 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007177 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00007178 i = (i + 1) & tp->rx_std_ring_mask)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007179 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007180 tp->rx_pkt_map_sz);
7181
Joe Perches63c3a662011-04-26 08:12:10 +00007182 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007183 for (i = tpr->rx_jmb_cons_idx;
7184 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00007185 i = (i + 1) & tp->rx_jmb_ring_mask) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007186 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007187 TG3_RX_JMB_MAP_SZ);
7188 }
7189 }
7190
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007191 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007193
Matt Carlson2c49a442010-09-30 10:34:35 +00007194 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007195 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007196 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007197
Joe Perches63c3a662011-04-26 08:12:10 +00007198 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007199 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007200 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007201 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007202 }
7203}
7204
Matt Carlsonc6cdf432010-04-05 10:19:26 +00007205/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007206 *
7207 * The chip has been shut down and the driver detached from
7208 * the networking, so no interrupts or new tx packets will
7209 * end up in the driver. tp->{tx,}lock are held and thus
7210 * we may not sleep.
7211 */
Matt Carlson21f581a2009-08-28 14:00:25 +00007212static int tg3_rx_prodring_alloc(struct tg3 *tp,
7213 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007214{
Matt Carlson287be122009-08-28 13:58:46 +00007215 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007216
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007217 tpr->rx_std_cons_idx = 0;
7218 tpr->rx_std_prod_idx = 0;
7219 tpr->rx_jmb_cons_idx = 0;
7220 tpr->rx_jmb_prod_idx = 0;
7221
Matt Carlson8fea32b2010-09-15 08:59:58 +00007222 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007223 memset(&tpr->rx_std_buffers[0], 0,
7224 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00007225 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007226 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00007227 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007228 goto done;
7229 }
7230
Linus Torvalds1da177e2005-04-16 15:20:36 -07007231 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00007232 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007233
Matt Carlson287be122009-08-28 13:58:46 +00007234 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00007235 if (tg3_flag(tp, 5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00007236 tp->dev->mtu > ETH_DATA_LEN)
7237 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
7238 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07007239
Linus Torvalds1da177e2005-04-16 15:20:36 -07007240 /* Initialize invariants of the rings, we only set this
7241 * stuff once. This works because the card does not
7242 * write into the rx buffer posting rings.
7243 */
Matt Carlson2c49a442010-09-30 10:34:35 +00007244 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007245 struct tg3_rx_buffer_desc *rxd;
7246
Matt Carlson21f581a2009-08-28 14:00:25 +00007247 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00007248 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007249 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
7250 rxd->opaque = (RXD_OPAQUE_RING_STD |
7251 (i << RXD_OPAQUE_INDEX_SHIFT));
7252 }
7253
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007254 /* Now allocate fresh SKBs for each rx ring. */
7255 for (i = 0; i < tp->rx_pending; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007256 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_STD, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007257 netdev_warn(tp->dev,
7258 "Using a smaller RX standard ring. Only "
7259 "%d out of %d buffers were allocated "
7260 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007261 if (i == 0)
7262 goto initfail;
7263 tp->rx_pending = i;
7264 break;
7265 }
7266 }
7267
Joe Perches63c3a662011-04-26 08:12:10 +00007268 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007269 goto done;
7270
Matt Carlson2c49a442010-09-30 10:34:35 +00007271 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007272
Joe Perches63c3a662011-04-26 08:12:10 +00007273 if (!tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson0d86df82010-02-17 15:17:00 +00007274 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007275
Matt Carlson2c49a442010-09-30 10:34:35 +00007276 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00007277 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007278
Matt Carlson0d86df82010-02-17 15:17:00 +00007279 rxd = &tpr->rx_jmb[i].std;
7280 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
7281 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
7282 RXD_FLAG_JUMBO;
7283 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
7284 (i << RXD_OPAQUE_INDEX_SHIFT));
7285 }
7286
7287 for (i = 0; i < tp->rx_jumbo_pending; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007288 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_JUMBO, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007289 netdev_warn(tp->dev,
7290 "Using a smaller RX jumbo ring. Only %d "
7291 "out of %d buffers were allocated "
7292 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00007293 if (i == 0)
7294 goto initfail;
7295 tp->rx_jumbo_pending = i;
7296 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007297 }
7298 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007299
7300done:
Michael Chan32d8c572006-07-25 16:38:29 -07007301 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007302
7303initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00007304 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007305 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007306}
7307
Matt Carlson21f581a2009-08-28 14:00:25 +00007308static void tg3_rx_prodring_fini(struct tg3 *tp,
7309 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007310{
Matt Carlson21f581a2009-08-28 14:00:25 +00007311 kfree(tpr->rx_std_buffers);
7312 tpr->rx_std_buffers = NULL;
7313 kfree(tpr->rx_jmb_buffers);
7314 tpr->rx_jmb_buffers = NULL;
7315 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007316 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
7317 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00007318 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007319 }
Matt Carlson21f581a2009-08-28 14:00:25 +00007320 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007321 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
7322 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00007323 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007324 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007325}
7326
Matt Carlson21f581a2009-08-28 14:00:25 +00007327static int tg3_rx_prodring_init(struct tg3 *tp,
7328 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007329{
Matt Carlson2c49a442010-09-30 10:34:35 +00007330 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
7331 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007332 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007333 return -ENOMEM;
7334
Matt Carlson4bae65c2010-11-24 08:31:52 +00007335 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
7336 TG3_RX_STD_RING_BYTES(tp),
7337 &tpr->rx_std_mapping,
7338 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007339 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007340 goto err_out;
7341
Joe Perches63c3a662011-04-26 08:12:10 +00007342 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007343 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00007344 GFP_KERNEL);
7345 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007346 goto err_out;
7347
Matt Carlson4bae65c2010-11-24 08:31:52 +00007348 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
7349 TG3_RX_JMB_RING_BYTES(tp),
7350 &tpr->rx_jmb_mapping,
7351 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007352 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007353 goto err_out;
7354 }
7355
7356 return 0;
7357
7358err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00007359 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007360 return -ENOMEM;
7361}
7362
7363/* Free up pending packets in all rx/tx rings.
7364 *
7365 * The chip has been shut down and the driver detached from
7366 * the networking, so no interrupts or new tx packets will
7367 * end up in the driver. tp->{tx,}lock is not held and we are not
7368 * in an interrupt context and thus may sleep.
7369 */
7370static void tg3_free_rings(struct tg3 *tp)
7371{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007372 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007373
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007374 for (j = 0; j < tp->irq_cnt; j++) {
7375 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007376
Matt Carlson8fea32b2010-09-15 08:59:58 +00007377 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00007378
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007379 if (!tnapi->tx_buffers)
7380 continue;
7381
Matt Carlson0d681b22011-07-27 14:20:49 +00007382 for (i = 0; i < TG3_TX_RING_SIZE; i++) {
7383 struct sk_buff *skb = tnapi->tx_buffers[i].skb;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007384
Matt Carlson0d681b22011-07-27 14:20:49 +00007385 if (!skb)
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007386 continue;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007387
Matt Carlsonba1142e2011-11-04 09:15:00 +00007388 tg3_tx_skb_unmap(tnapi, i,
7389 skb_shinfo(skb)->nr_frags - 1);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007390
7391 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007392 }
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007393 }
Tom Herbert298376d2011-11-28 16:33:30 +00007394 netdev_reset_queue(tp->dev);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007395}
7396
7397/* Initialize tx/rx rings for packet processing.
7398 *
7399 * The chip has been shut down and the driver detached from
7400 * the networking, so no interrupts or new tx packets will
7401 * end up in the driver. tp->{tx,}lock are held and thus
7402 * we may not sleep.
7403 */
7404static int tg3_init_rings(struct tg3 *tp)
7405{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007406 int i;
Matt Carlson72334482009-08-28 14:03:01 +00007407
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007408 /* Free up all the SKBs. */
7409 tg3_free_rings(tp);
7410
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007411 for (i = 0; i < tp->irq_cnt; i++) {
7412 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007413
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007414 tnapi->last_tag = 0;
7415 tnapi->last_irq_tag = 0;
7416 tnapi->hw_status->status = 0;
7417 tnapi->hw_status->status_tag = 0;
7418 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7419
7420 tnapi->tx_prod = 0;
7421 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007422 if (tnapi->tx_ring)
7423 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007424
7425 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007426 if (tnapi->rx_rcb)
7427 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007428
Matt Carlson8fea32b2010-09-15 08:59:58 +00007429 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00007430 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007431 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00007432 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007433 }
Matt Carlson72334482009-08-28 14:03:01 +00007434
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007435 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007436}
7437
7438/*
7439 * Must not be invoked with interrupt sources disabled and
7440 * the hardware shutdown down.
7441 */
7442static void tg3_free_consistent(struct tg3 *tp)
7443{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007444 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00007445
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007446 for (i = 0; i < tp->irq_cnt; i++) {
7447 struct tg3_napi *tnapi = &tp->napi[i];
7448
7449 if (tnapi->tx_ring) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007450 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007451 tnapi->tx_ring, tnapi->tx_desc_mapping);
7452 tnapi->tx_ring = NULL;
7453 }
7454
7455 kfree(tnapi->tx_buffers);
7456 tnapi->tx_buffers = NULL;
7457
7458 if (tnapi->rx_rcb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007459 dma_free_coherent(&tp->pdev->dev,
7460 TG3_RX_RCB_RING_BYTES(tp),
7461 tnapi->rx_rcb,
7462 tnapi->rx_rcb_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007463 tnapi->rx_rcb = NULL;
7464 }
7465
Matt Carlson8fea32b2010-09-15 08:59:58 +00007466 tg3_rx_prodring_fini(tp, &tnapi->prodring);
7467
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007468 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007469 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
7470 tnapi->hw_status,
7471 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007472 tnapi->hw_status = NULL;
7473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007474 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007475
Linus Torvalds1da177e2005-04-16 15:20:36 -07007476 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007477 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
7478 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007479 tp->hw_stats = NULL;
7480 }
7481}
7482
7483/*
7484 * Must not be invoked with interrupt sources disabled and
7485 * the hardware shutdown down. Can sleep.
7486 */
7487static int tg3_alloc_consistent(struct tg3 *tp)
7488{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007489 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00007490
Matt Carlson4bae65c2010-11-24 08:31:52 +00007491 tp->hw_stats = dma_alloc_coherent(&tp->pdev->dev,
7492 sizeof(struct tg3_hw_stats),
7493 &tp->stats_mapping,
7494 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007495 if (!tp->hw_stats)
7496 goto err_out;
7497
Linus Torvalds1da177e2005-04-16 15:20:36 -07007498 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
7499
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007500 for (i = 0; i < tp->irq_cnt; i++) {
7501 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007502 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007503
Matt Carlson4bae65c2010-11-24 08:31:52 +00007504 tnapi->hw_status = dma_alloc_coherent(&tp->pdev->dev,
7505 TG3_HW_STATUS_SIZE,
7506 &tnapi->status_mapping,
7507 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007508 if (!tnapi->hw_status)
7509 goto err_out;
7510
7511 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007512 sblk = tnapi->hw_status;
7513
Matt Carlson8fea32b2010-09-15 08:59:58 +00007514 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
7515 goto err_out;
7516
Matt Carlson19cfaec2009-12-03 08:36:20 +00007517 /* If multivector TSS is enabled, vector 0 does not handle
7518 * tx interrupts. Don't allocate any resources for it.
7519 */
Joe Perches63c3a662011-04-26 08:12:10 +00007520 if ((!i && !tg3_flag(tp, ENABLE_TSS)) ||
7521 (i && tg3_flag(tp, ENABLE_TSS))) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00007522 tnapi->tx_buffers = kzalloc(
7523 sizeof(struct tg3_tx_ring_info) *
7524 TG3_TX_RING_SIZE, GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007525 if (!tnapi->tx_buffers)
7526 goto err_out;
7527
Matt Carlson4bae65c2010-11-24 08:31:52 +00007528 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
7529 TG3_TX_RING_BYTES,
7530 &tnapi->tx_desc_mapping,
7531 GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007532 if (!tnapi->tx_ring)
7533 goto err_out;
7534 }
7535
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007536 /*
7537 * When RSS is enabled, the status block format changes
7538 * slightly. The "rx_jumbo_consumer", "reserved",
7539 * and "rx_mini_consumer" members get mapped to the
7540 * other three rx return ring producer indexes.
7541 */
7542 switch (i) {
7543 default:
7544 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
7545 break;
7546 case 2:
7547 tnapi->rx_rcb_prod_idx = &sblk->rx_jumbo_consumer;
7548 break;
7549 case 3:
7550 tnapi->rx_rcb_prod_idx = &sblk->reserved;
7551 break;
7552 case 4:
7553 tnapi->rx_rcb_prod_idx = &sblk->rx_mini_consumer;
7554 break;
7555 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007556
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007557 /*
7558 * If multivector RSS is enabled, vector 0 does not handle
7559 * rx or tx interrupts. Don't allocate any resources for it.
7560 */
Joe Perches63c3a662011-04-26 08:12:10 +00007561 if (!i && tg3_flag(tp, ENABLE_RSS))
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007562 continue;
7563
Matt Carlson4bae65c2010-11-24 08:31:52 +00007564 tnapi->rx_rcb = dma_alloc_coherent(&tp->pdev->dev,
7565 TG3_RX_RCB_RING_BYTES(tp),
7566 &tnapi->rx_rcb_mapping,
7567 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007568 if (!tnapi->rx_rcb)
7569 goto err_out;
7570
7571 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007572 }
7573
Linus Torvalds1da177e2005-04-16 15:20:36 -07007574 return 0;
7575
7576err_out:
7577 tg3_free_consistent(tp);
7578 return -ENOMEM;
7579}
7580
7581#define MAX_WAIT_CNT 1000
7582
7583/* To stop a block, clear the enable bit and poll till it
7584 * clears. tp->lock is held.
7585 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007586static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007587{
7588 unsigned int i;
7589 u32 val;
7590
Joe Perches63c3a662011-04-26 08:12:10 +00007591 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007592 switch (ofs) {
7593 case RCVLSC_MODE:
7594 case DMAC_MODE:
7595 case MBFREE_MODE:
7596 case BUFMGR_MODE:
7597 case MEMARB_MODE:
7598 /* We can't enable/disable these bits of the
7599 * 5705/5750, just say success.
7600 */
7601 return 0;
7602
7603 default:
7604 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007606 }
7607
7608 val = tr32(ofs);
7609 val &= ~enable_bit;
7610 tw32_f(ofs, val);
7611
7612 for (i = 0; i < MAX_WAIT_CNT; i++) {
7613 udelay(100);
7614 val = tr32(ofs);
7615 if ((val & enable_bit) == 0)
7616 break;
7617 }
7618
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007619 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00007620 dev_err(&tp->pdev->dev,
7621 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
7622 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007623 return -ENODEV;
7624 }
7625
7626 return 0;
7627}
7628
7629/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007630static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007631{
7632 int i, err;
7633
7634 tg3_disable_ints(tp);
7635
7636 tp->rx_mode &= ~RX_MODE_ENABLE;
7637 tw32_f(MAC_RX_MODE, tp->rx_mode);
7638 udelay(10);
7639
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007640 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
7641 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
7642 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
7643 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
7644 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
7645 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007646
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007647 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
7648 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
7649 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
7650 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
7651 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
7652 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
7653 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007654
7655 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
7656 tw32_f(MAC_MODE, tp->mac_mode);
7657 udelay(40);
7658
7659 tp->tx_mode &= ~TX_MODE_ENABLE;
7660 tw32_f(MAC_TX_MODE, tp->tx_mode);
7661
7662 for (i = 0; i < MAX_WAIT_CNT; i++) {
7663 udelay(100);
7664 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
7665 break;
7666 }
7667 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00007668 dev_err(&tp->pdev->dev,
7669 "%s timed out, TX_MODE_ENABLE will not clear "
7670 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07007671 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007672 }
7673
Michael Chane6de8ad2005-05-05 14:42:41 -07007674 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007675 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
7676 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007677
7678 tw32(FTQ_RESET, 0xffffffff);
7679 tw32(FTQ_RESET, 0x00000000);
7680
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007681 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
7682 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007683
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007684 for (i = 0; i < tp->irq_cnt; i++) {
7685 struct tg3_napi *tnapi = &tp->napi[i];
7686 if (tnapi->hw_status)
7687 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007689
Linus Torvalds1da177e2005-04-16 15:20:36 -07007690 return err;
7691}
7692
Michael Chanee6a99b2007-07-18 21:49:10 -07007693/* Save PCI command register before chip reset */
7694static void tg3_save_pci_state(struct tg3 *tp)
7695{
Matt Carlson8a6eac92007-10-21 16:17:55 -07007696 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007697}
7698
7699/* Restore PCI state after chip reset */
7700static void tg3_restore_pci_state(struct tg3 *tp)
7701{
7702 u32 val;
7703
7704 /* Re-enable indirect register accesses. */
7705 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
7706 tp->misc_host_ctrl);
7707
7708 /* Set MAX PCI retry to zero. */
7709 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
7710 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007711 tg3_flag(tp, PCIX_MODE))
Michael Chanee6a99b2007-07-18 21:49:10 -07007712 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07007713 /* Allow reads and writes to the APE register and memory space. */
Joe Perches63c3a662011-04-26 08:12:10 +00007714 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07007715 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00007716 PCISTATE_ALLOW_APE_SHMEM_WR |
7717 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07007718 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
7719
Matt Carlson8a6eac92007-10-21 16:17:55 -07007720 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007721
Matt Carlson2c55a3d2011-11-28 09:41:04 +00007722 if (!tg3_flag(tp, PCI_EXPRESS)) {
7723 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
7724 tp->pci_cacheline_sz);
7725 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
7726 tp->pci_lat_timer);
Michael Chan114342f2007-10-15 02:12:26 -07007727 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08007728
Michael Chanee6a99b2007-07-18 21:49:10 -07007729 /* Make sure PCI-X relaxed ordering bit is clear. */
Joe Perches63c3a662011-04-26 08:12:10 +00007730 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07007731 u16 pcix_cmd;
7732
7733 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7734 &pcix_cmd);
7735 pcix_cmd &= ~PCI_X_CMD_ERO;
7736 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7737 pcix_cmd);
7738 }
Michael Chanee6a99b2007-07-18 21:49:10 -07007739
Joe Perches63c3a662011-04-26 08:12:10 +00007740 if (tg3_flag(tp, 5780_CLASS)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007741
7742 /* Chip reset on 5780 will reset MSI enable bit,
7743 * so need to restore it.
7744 */
Joe Perches63c3a662011-04-26 08:12:10 +00007745 if (tg3_flag(tp, USING_MSI)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007746 u16 ctrl;
7747
7748 pci_read_config_word(tp->pdev,
7749 tp->msi_cap + PCI_MSI_FLAGS,
7750 &ctrl);
7751 pci_write_config_word(tp->pdev,
7752 tp->msi_cap + PCI_MSI_FLAGS,
7753 ctrl | PCI_MSI_FLAGS_ENABLE);
7754 val = tr32(MSGINT_MODE);
7755 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
7756 }
7757 }
7758}
7759
Linus Torvalds1da177e2005-04-16 15:20:36 -07007760/* tp->lock is held. */
7761static int tg3_chip_reset(struct tg3 *tp)
7762{
7763 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07007764 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00007765 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007766
David S. Millerf49639e2006-06-09 11:58:36 -07007767 tg3_nvram_lock(tp);
7768
Matt Carlson77b483f2008-08-15 14:07:24 -07007769 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
7770
David S. Millerf49639e2006-06-09 11:58:36 -07007771 /* No matching tg3_nvram_unlock() after this because
7772 * chip reset below will undo the nvram lock.
7773 */
7774 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007775
Michael Chanee6a99b2007-07-18 21:49:10 -07007776 /* GRC_MISC_CFG core clock reset will clear the memory
7777 * enable bit in PCI register 4 and the MSI enable bit
7778 * on some chips, so we save relevant registers here.
7779 */
7780 tg3_save_pci_state(tp);
7781
Michael Chand9ab5ad12006-03-20 22:27:35 -08007782 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Joe Perches63c3a662011-04-26 08:12:10 +00007783 tg3_flag(tp, 5755_PLUS))
Michael Chand9ab5ad12006-03-20 22:27:35 -08007784 tw32(GRC_FASTBOOT_PC, 0);
7785
Linus Torvalds1da177e2005-04-16 15:20:36 -07007786 /*
7787 * We must avoid the readl() that normally takes place.
7788 * It locks machines, causes machine checks, and other
7789 * fun things. So, temporarily disable the 5701
7790 * hardware workaround, while we do the reset.
7791 */
Michael Chan1ee582d2005-08-09 20:16:46 -07007792 write_op = tp->write32;
7793 if (write_op == tg3_write_flush_reg32)
7794 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007795
Michael Chand18edcb2007-03-24 20:57:11 -07007796 /* Prevent the irq handler from reading or writing PCI registers
7797 * during chip reset when the memory enable bit in the PCI command
7798 * register may be cleared. The chip does not generate interrupt
7799 * at this time, but the irq handler may still be called due to irq
7800 * sharing or irqpoll.
7801 */
Joe Perches63c3a662011-04-26 08:12:10 +00007802 tg3_flag_set(tp, CHIP_RESETTING);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007803 for (i = 0; i < tp->irq_cnt; i++) {
7804 struct tg3_napi *tnapi = &tp->napi[i];
7805 if (tnapi->hw_status) {
7806 tnapi->hw_status->status = 0;
7807 tnapi->hw_status->status_tag = 0;
7808 }
7809 tnapi->last_tag = 0;
7810 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07007811 }
Michael Chand18edcb2007-03-24 20:57:11 -07007812 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00007813
7814 for (i = 0; i < tp->irq_cnt; i++)
7815 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07007816
Matt Carlson255ca312009-08-25 10:07:27 +00007817 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7818 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7819 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
7820 }
7821
Linus Torvalds1da177e2005-04-16 15:20:36 -07007822 /* do the reset */
7823 val = GRC_MISC_CFG_CORECLK_RESET;
7824
Joe Perches63c3a662011-04-26 08:12:10 +00007825 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson88075d92010-08-02 11:25:58 +00007826 /* Force PCIe 1.0a mode */
7827 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007828 !tg3_flag(tp, 57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00007829 tr32(TG3_PCIE_PHY_TSTCTL) ==
7830 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
7831 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
7832
Linus Torvalds1da177e2005-04-16 15:20:36 -07007833 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
7834 tw32(GRC_MISC_CFG, (1 << 29));
7835 val |= (1 << 29);
7836 }
7837 }
7838
Michael Chanb5d37722006-09-27 16:06:21 -07007839 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7840 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
7841 tw32(GRC_VCPU_EXT_CTRL,
7842 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
7843 }
7844
Matt Carlsonf37500d2010-08-02 11:25:59 +00007845 /* Manage gphy power for all CPMU absent PCIe devices. */
Joe Perches63c3a662011-04-26 08:12:10 +00007846 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007847 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00007848
Linus Torvalds1da177e2005-04-16 15:20:36 -07007849 tw32(GRC_MISC_CFG, val);
7850
Michael Chan1ee582d2005-08-09 20:16:46 -07007851 /* restore 5701 hardware bug workaround write method */
7852 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007853
7854 /* Unfortunately, we have to delay before the PCI read back.
7855 * Some 575X chips even will not respond to a PCI cfg access
7856 * when the reset command is given to the chip.
7857 *
7858 * How do these hardware designers expect things to work
7859 * properly if the PCI write is posted for a long period
7860 * of time? It is always necessary to have some method by
7861 * which a register read back can occur to push the write
7862 * out which does the reset.
7863 *
7864 * For most tg3 variants the trick below was working.
7865 * Ho hum...
7866 */
7867 udelay(120);
7868
7869 /* Flush PCI posted writes. The normal MMIO registers
7870 * are inaccessible at this time so this is the only
7871 * way to make this reliably (actually, this is no longer
7872 * the case, see above). I tried to use indirect
7873 * register read/write but this upset some 5701 variants.
7874 */
7875 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
7876
7877 udelay(120);
7878
Jon Mason708ebb3a2011-06-27 12:56:50 +00007879 if (tg3_flag(tp, PCI_EXPRESS) && pci_pcie_cap(tp->pdev)) {
Matt Carlsone7126992009-08-25 10:08:16 +00007880 u16 val16;
7881
Linus Torvalds1da177e2005-04-16 15:20:36 -07007882 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
7883 int i;
7884 u32 cfg_val;
7885
7886 /* Wait for link training to complete. */
7887 for (i = 0; i < 5000; i++)
7888 udelay(100);
7889
7890 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
7891 pci_write_config_dword(tp->pdev, 0xc4,
7892 cfg_val | (1 << 15));
7893 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007894
Matt Carlsone7126992009-08-25 10:08:16 +00007895 /* Clear the "no snoop" and "relaxed ordering" bits. */
7896 pci_read_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +00007897 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007898 &val16);
7899 val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
7900 PCI_EXP_DEVCTL_NOSNOOP_EN);
7901 /*
7902 * Older PCIe devices only support the 128 byte
7903 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007904 */
Joe Perches63c3a662011-04-26 08:12:10 +00007905 if (!tg3_flag(tp, CPMU_PRESENT))
Matt Carlsone7126992009-08-25 10:08:16 +00007906 val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007907 pci_write_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +00007908 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007909 val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007910
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007911 /* Clear error status */
7912 pci_write_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +00007913 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVSTA,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007914 PCI_EXP_DEVSTA_CED |
7915 PCI_EXP_DEVSTA_NFED |
7916 PCI_EXP_DEVSTA_FED |
7917 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007918 }
7919
Michael Chanee6a99b2007-07-18 21:49:10 -07007920 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007921
Joe Perches63c3a662011-04-26 08:12:10 +00007922 tg3_flag_clear(tp, CHIP_RESETTING);
7923 tg3_flag_clear(tp, ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07007924
Michael Chanee6a99b2007-07-18 21:49:10 -07007925 val = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00007926 if (tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07007927 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07007928 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007929
7930 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
7931 tg3_stop_fw(tp);
7932 tw32(0x5000, 0x400);
7933 }
7934
7935 tw32(GRC_MODE, tp->grc_mode);
7936
7937 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007938 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007939
7940 tw32(0xc4, val | (1 << 15));
7941 }
7942
7943 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
7944 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
7945 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
7946 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
7947 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
7948 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7949 }
7950
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007951 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00007952 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007953 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007954 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00007955 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007956 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007957 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007958 val = 0;
7959
7960 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007961 udelay(40);
7962
Matt Carlson77b483f2008-08-15 14:07:24 -07007963 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
7964
Michael Chan7a6f4362006-09-27 16:03:31 -07007965 err = tg3_poll_fw(tp);
7966 if (err)
7967 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007968
Matt Carlson0a9140c2009-08-28 12:27:50 +00007969 tg3_mdio_start(tp);
7970
Joe Perches63c3a662011-04-26 08:12:10 +00007971 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007972 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
7973 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007974 !tg3_flag(tp, 57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007975 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007976
7977 tw32(0x7c00, val | (1 << 25));
7978 }
7979
Matt Carlsond78b59f2011-04-05 14:22:46 +00007980 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
7981 val = tr32(TG3_CPMU_CLCK_ORIDE);
7982 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
7983 }
7984
Linus Torvalds1da177e2005-04-16 15:20:36 -07007985 /* Reprobe ASF enable state. */
Joe Perches63c3a662011-04-26 08:12:10 +00007986 tg3_flag_clear(tp, ENABLE_ASF);
7987 tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007988 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
7989 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
7990 u32 nic_cfg;
7991
7992 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
7993 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +00007994 tg3_flag_set(tp, ENABLE_ASF);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007995 tp->last_event_jiffies = jiffies;
Joe Perches63c3a662011-04-26 08:12:10 +00007996 if (tg3_flag(tp, 5750_PLUS))
7997 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007998 }
7999 }
8000
8001 return 0;
8002}
8003
Matt Carlson92feeab2011-12-08 14:40:14 +00008004static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *,
8005 struct rtnl_link_stats64 *);
8006static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *,
8007 struct tg3_ethtool_stats *);
8008
Linus Torvalds1da177e2005-04-16 15:20:36 -07008009/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07008010static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008011{
8012 int err;
8013
8014 tg3_stop_fw(tp);
8015
Michael Chan944d9802005-05-29 14:57:48 -07008016 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008017
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008018 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008019 err = tg3_chip_reset(tp);
8020
Matt Carlsondaba2a62009-04-20 06:58:52 +00008021 __tg3_set_mac_addr(tp, 0);
8022
Michael Chan944d9802005-05-29 14:57:48 -07008023 tg3_write_sig_legacy(tp, kind);
8024 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008025
Matt Carlson92feeab2011-12-08 14:40:14 +00008026 if (tp->hw_stats) {
8027 /* Save the stats across chip resets... */
8028 tg3_get_stats64(tp->dev, &tp->net_stats_prev),
8029 tg3_get_estats(tp, &tp->estats_prev);
8030
8031 /* And make sure the next sample is new data */
8032 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
8033 }
8034
Linus Torvalds1da177e2005-04-16 15:20:36 -07008035 if (err)
8036 return err;
8037
8038 return 0;
8039}
8040
Linus Torvalds1da177e2005-04-16 15:20:36 -07008041static int tg3_set_mac_addr(struct net_device *dev, void *p)
8042{
8043 struct tg3 *tp = netdev_priv(dev);
8044 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07008045 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008046
Michael Chanf9804dd2005-09-27 12:13:10 -07008047 if (!is_valid_ether_addr(addr->sa_data))
8048 return -EINVAL;
8049
Linus Torvalds1da177e2005-04-16 15:20:36 -07008050 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
8051
Michael Chane75f7c92006-03-20 21:33:26 -08008052 if (!netif_running(dev))
8053 return 0;
8054
Joe Perches63c3a662011-04-26 08:12:10 +00008055 if (tg3_flag(tp, ENABLE_ASF)) {
Michael Chan986e0ae2007-05-05 12:10:20 -07008056 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07008057
Michael Chan986e0ae2007-05-05 12:10:20 -07008058 addr0_high = tr32(MAC_ADDR_0_HIGH);
8059 addr0_low = tr32(MAC_ADDR_0_LOW);
8060 addr1_high = tr32(MAC_ADDR_1_HIGH);
8061 addr1_low = tr32(MAC_ADDR_1_LOW);
8062
8063 /* Skip MAC addr 1 if ASF is using it. */
8064 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
8065 !(addr1_high == 0 && addr1_low == 0))
8066 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07008067 }
Michael Chan986e0ae2007-05-05 12:10:20 -07008068 spin_lock_bh(&tp->lock);
8069 __tg3_set_mac_addr(tp, skip_mac_1);
8070 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008071
Michael Chanb9ec6c12006-07-25 16:37:27 -07008072 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008073}
8074
8075/* tp->lock is held. */
8076static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
8077 dma_addr_t mapping, u32 maxlen_flags,
8078 u32 nic_addr)
8079{
8080 tg3_write_mem(tp,
8081 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
8082 ((u64) mapping >> 32));
8083 tg3_write_mem(tp,
8084 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
8085 ((u64) mapping & 0xffffffff));
8086 tg3_write_mem(tp,
8087 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
8088 maxlen_flags);
8089
Joe Perches63c3a662011-04-26 08:12:10 +00008090 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008091 tg3_write_mem(tp,
8092 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
8093 nic_addr);
8094}
8095
Michael Chand244c892005-07-05 14:42:33 -07008096static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07008097{
Matt Carlsonb6080e12009-09-01 13:12:00 +00008098 int i;
8099
Joe Perches63c3a662011-04-26 08:12:10 +00008100 if (!tg3_flag(tp, ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00008101 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
8102 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
8103 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008104 } else {
8105 tw32(HOSTCC_TXCOL_TICKS, 0);
8106 tw32(HOSTCC_TXMAX_FRAMES, 0);
8107 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00008108 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008109
Joe Perches63c3a662011-04-26 08:12:10 +00008110 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008111 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
8112 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
8113 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
8114 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00008115 tw32(HOSTCC_RXCOL_TICKS, 0);
8116 tw32(HOSTCC_RXMAX_FRAMES, 0);
8117 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07008118 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008119
Joe Perches63c3a662011-04-26 08:12:10 +00008120 if (!tg3_flag(tp, 5705_PLUS)) {
David S. Miller15f98502005-05-18 22:49:26 -07008121 u32 val = ec->stats_block_coalesce_usecs;
8122
Matt Carlsonb6080e12009-09-01 13:12:00 +00008123 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
8124 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
8125
David S. Miller15f98502005-05-18 22:49:26 -07008126 if (!netif_carrier_ok(tp->dev))
8127 val = 0;
8128
8129 tw32(HOSTCC_STAT_COAL_TICKS, val);
8130 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008131
8132 for (i = 0; i < tp->irq_cnt - 1; i++) {
8133 u32 reg;
8134
8135 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
8136 tw32(reg, ec->rx_coalesce_usecs);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008137 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
8138 tw32(reg, ec->rx_max_coalesced_frames);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008139 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
8140 tw32(reg, ec->rx_max_coalesced_frames_irq);
Matt Carlson19cfaec2009-12-03 08:36:20 +00008141
Joe Perches63c3a662011-04-26 08:12:10 +00008142 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008143 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
8144 tw32(reg, ec->tx_coalesce_usecs);
8145 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
8146 tw32(reg, ec->tx_max_coalesced_frames);
8147 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
8148 tw32(reg, ec->tx_max_coalesced_frames_irq);
8149 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008150 }
8151
8152 for (; i < tp->irq_max - 1; i++) {
8153 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008154 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008155 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00008156
Joe Perches63c3a662011-04-26 08:12:10 +00008157 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008158 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
8159 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
8160 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
8161 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008162 }
David S. Miller15f98502005-05-18 22:49:26 -07008163}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008164
8165/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00008166static void tg3_rings_reset(struct tg3 *tp)
8167{
8168 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008169 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008170 struct tg3_napi *tnapi = &tp->napi[0];
8171
8172 /* Disable all transmit rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008173 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008174 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Joe Perches63c3a662011-04-26 08:12:10 +00008175 else if (tg3_flag(tp, 5717_PLUS))
Matt Carlson3d377282010-10-14 10:37:39 +00008176 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
Matt Carlson55086ad2011-12-14 11:09:59 +00008177 else if (tg3_flag(tp, 57765_CLASS))
Matt Carlsonb703df62009-12-03 08:36:21 +00008178 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008179 else
8180 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8181
8182 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8183 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
8184 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
8185 BDINFO_FLAGS_DISABLED);
8186
8187
8188 /* Disable all receive return rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008189 if (tg3_flag(tp, 5717_PLUS))
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008190 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
Joe Perches63c3a662011-04-26 08:12:10 +00008191 else if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008192 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00008193 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00008194 tg3_flag(tp, 57765_CLASS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008195 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
8196 else
8197 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8198
8199 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8200 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
8201 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
8202 BDINFO_FLAGS_DISABLED);
8203
8204 /* Disable interrupts */
8205 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008206 tp->napi[0].chk_msi_cnt = 0;
8207 tp->napi[0].last_rx_cons = 0;
8208 tp->napi[0].last_tx_cons = 0;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008209
8210 /* Zero mailbox registers. */
Joe Perches63c3a662011-04-26 08:12:10 +00008211 if (tg3_flag(tp, SUPPORT_MSIX)) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00008212 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008213 tp->napi[i].tx_prod = 0;
8214 tp->napi[i].tx_cons = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00008215 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008216 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008217 tw32_rx_mbox(tp->napi[i].consmbox, 0);
8218 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
Matt Carlson7f230732011-08-31 11:44:48 +00008219 tp->napi[i].chk_msi_cnt = 0;
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008220 tp->napi[i].last_rx_cons = 0;
8221 tp->napi[i].last_tx_cons = 0;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008222 }
Joe Perches63c3a662011-04-26 08:12:10 +00008223 if (!tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008224 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008225 } else {
8226 tp->napi[0].tx_prod = 0;
8227 tp->napi[0].tx_cons = 0;
8228 tw32_mailbox(tp->napi[0].prodmbox, 0);
8229 tw32_rx_mbox(tp->napi[0].consmbox, 0);
8230 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008231
8232 /* Make sure the NIC-based send BD rings are disabled. */
Joe Perches63c3a662011-04-26 08:12:10 +00008233 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson2d31eca2009-09-01 12:53:31 +00008234 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
8235 for (i = 0; i < 16; i++)
8236 tw32_tx_mbox(mbox + i * 8, 0);
8237 }
8238
8239 txrcb = NIC_SRAM_SEND_RCB;
8240 rxrcb = NIC_SRAM_RCV_RET_RCB;
8241
8242 /* Clear status block in ram. */
8243 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8244
8245 /* Set status block DMA address */
8246 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8247 ((u64) tnapi->status_mapping >> 32));
8248 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8249 ((u64) tnapi->status_mapping & 0xffffffff));
8250
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008251 if (tnapi->tx_ring) {
8252 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8253 (TG3_TX_RING_SIZE <<
8254 BDINFO_FLAGS_MAXLEN_SHIFT),
8255 NIC_SRAM_TX_BUFFER_DESC);
8256 txrcb += TG3_BDINFO_SIZE;
8257 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008258
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008259 if (tnapi->rx_rcb) {
8260 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008261 (tp->rx_ret_ring_mask + 1) <<
8262 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008263 rxrcb += TG3_BDINFO_SIZE;
8264 }
8265
8266 stblk = HOSTCC_STATBLCK_RING1;
8267
8268 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
8269 u64 mapping = (u64)tnapi->status_mapping;
8270 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
8271 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
8272
8273 /* Clear status block in ram. */
8274 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8275
Matt Carlson19cfaec2009-12-03 08:36:20 +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 Carlsonf77a6a82009-09-01 13:04:37 +00008283
8284 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008285 ((tp->rx_ret_ring_mask + 1) <<
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008286 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
8287
8288 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008289 rxrcb += TG3_BDINFO_SIZE;
8290 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008291}
8292
Matt Carlsoneb07a942011-04-20 07:57:36 +00008293static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
8294{
8295 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
8296
Joe Perches63c3a662011-04-26 08:12:10 +00008297 if (!tg3_flag(tp, 5750_PLUS) ||
8298 tg3_flag(tp, 5780_CLASS) ||
Matt Carlsoneb07a942011-04-20 07:57:36 +00008299 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
Matt Carlson513aa6e2011-11-21 15:01:18 +00008300 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
8301 tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008302 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
8303 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
8304 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787)
8305 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
8306 else
8307 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
8308
8309 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
8310 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
8311
8312 val = min(nic_rep_thresh, host_rep_thresh);
8313 tw32(RCVBDI_STD_THRESH, val);
8314
Joe Perches63c3a662011-04-26 08:12:10 +00008315 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008316 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
8317
Joe Perches63c3a662011-04-26 08:12:10 +00008318 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008319 return;
8320
Matt Carlson513aa6e2011-11-21 15:01:18 +00008321 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
Matt Carlsoneb07a942011-04-20 07:57:36 +00008322
8323 host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
8324
8325 val = min(bdcache_maxcnt / 2, host_rep_thresh);
8326 tw32(RCVBDI_JUMBO_THRESH, val);
8327
Joe Perches63c3a662011-04-26 08:12:10 +00008328 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008329 tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
8330}
8331
Matt Carlsonccd5ba92012-02-13 10:20:08 +00008332static inline u32 calc_crc(unsigned char *buf, int len)
8333{
8334 u32 reg;
8335 u32 tmp;
8336 int j, k;
8337
8338 reg = 0xffffffff;
8339
8340 for (j = 0; j < len; j++) {
8341 reg ^= buf[j];
8342
8343 for (k = 0; k < 8; k++) {
8344 tmp = reg & 0x01;
8345
8346 reg >>= 1;
8347
8348 if (tmp)
8349 reg ^= 0xedb88320;
8350 }
8351 }
8352
8353 return ~reg;
8354}
8355
8356static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
8357{
8358 /* accept or reject all multicast frames */
8359 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
8360 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
8361 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
8362 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
8363}
8364
8365static void __tg3_set_rx_mode(struct net_device *dev)
8366{
8367 struct tg3 *tp = netdev_priv(dev);
8368 u32 rx_mode;
8369
8370 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
8371 RX_MODE_KEEP_VLAN_TAG);
8372
8373#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
8374 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
8375 * flag clear.
8376 */
8377 if (!tg3_flag(tp, ENABLE_ASF))
8378 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
8379#endif
8380
8381 if (dev->flags & IFF_PROMISC) {
8382 /* Promiscuous mode. */
8383 rx_mode |= RX_MODE_PROMISC;
8384 } else if (dev->flags & IFF_ALLMULTI) {
8385 /* Accept all multicast. */
8386 tg3_set_multi(tp, 1);
8387 } else if (netdev_mc_empty(dev)) {
8388 /* Reject all multicast. */
8389 tg3_set_multi(tp, 0);
8390 } else {
8391 /* Accept one or more multicast(s). */
8392 struct netdev_hw_addr *ha;
8393 u32 mc_filter[4] = { 0, };
8394 u32 regidx;
8395 u32 bit;
8396 u32 crc;
8397
8398 netdev_for_each_mc_addr(ha, dev) {
8399 crc = calc_crc(ha->addr, ETH_ALEN);
8400 bit = ~crc & 0x7f;
8401 regidx = (bit & 0x60) >> 5;
8402 bit &= 0x1f;
8403 mc_filter[regidx] |= (1 << bit);
8404 }
8405
8406 tw32(MAC_HASH_REG_0, mc_filter[0]);
8407 tw32(MAC_HASH_REG_1, mc_filter[1]);
8408 tw32(MAC_HASH_REG_2, mc_filter[2]);
8409 tw32(MAC_HASH_REG_3, mc_filter[3]);
8410 }
8411
8412 if (rx_mode != tp->rx_mode) {
8413 tp->rx_mode = rx_mode;
8414 tw32_f(MAC_RX_MODE, rx_mode);
8415 udelay(10);
8416 }
8417}
8418
Matt Carlson90415472011-12-16 13:33:23 +00008419static void tg3_rss_init_dflt_indir_tbl(struct tg3 *tp)
8420{
8421 int i;
8422
8423 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
8424 tp->rss_ind_tbl[i] =
8425 ethtool_rxfh_indir_default(i, tp->irq_cnt - 1);
8426}
8427
8428static void tg3_rss_check_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008429{
8430 int i;
8431
8432 if (!tg3_flag(tp, SUPPORT_MSIX))
8433 return;
8434
Matt Carlson90415472011-12-16 13:33:23 +00008435 if (tp->irq_cnt <= 2) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008436 memset(&tp->rss_ind_tbl[0], 0, sizeof(tp->rss_ind_tbl));
Matt Carlson90415472011-12-16 13:33:23 +00008437 return;
8438 }
8439
8440 /* Validate table against current IRQ count */
8441 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
8442 if (tp->rss_ind_tbl[i] >= tp->irq_cnt - 1)
8443 break;
8444 }
8445
8446 if (i != TG3_RSS_INDIR_TBL_SIZE)
8447 tg3_rss_init_dflt_indir_tbl(tp);
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008448}
8449
Matt Carlson90415472011-12-16 13:33:23 +00008450static void tg3_rss_write_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008451{
8452 int i = 0;
8453 u32 reg = MAC_RSS_INDIR_TBL_0;
8454
8455 while (i < TG3_RSS_INDIR_TBL_SIZE) {
8456 u32 val = tp->rss_ind_tbl[i];
8457 i++;
8458 for (; i % 8; i++) {
8459 val <<= 4;
8460 val |= tp->rss_ind_tbl[i];
8461 }
8462 tw32(reg, val);
8463 reg += 4;
8464 }
8465}
8466
Matt Carlson2d31eca2009-09-01 12:53:31 +00008467/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008468static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008469{
8470 u32 val, rdmac_mode;
8471 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00008472 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008473
8474 tg3_disable_ints(tp);
8475
8476 tg3_stop_fw(tp);
8477
8478 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
8479
Joe Perches63c3a662011-04-26 08:12:10 +00008480 if (tg3_flag(tp, INIT_COMPLETE))
Michael Chane6de8ad2005-05-05 14:42:41 -07008481 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008482
Matt Carlson699c0192010-12-06 08:28:51 +00008483 /* Enable MAC control of LPI */
8484 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
8485 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL,
8486 TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
8487 TG3_CPMU_EEE_LNKIDL_UART_IDL);
8488
8489 tw32_f(TG3_CPMU_EEE_CTRL,
8490 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
8491
Matt Carlsona386b902010-12-06 08:28:53 +00008492 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
8493 TG3_CPMU_EEEMD_LPI_IN_TX |
8494 TG3_CPMU_EEEMD_LPI_IN_RX |
8495 TG3_CPMU_EEEMD_EEE_ENABLE;
8496
8497 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8498 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
8499
Joe Perches63c3a662011-04-26 08:12:10 +00008500 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsona386b902010-12-06 08:28:53 +00008501 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
8502
8503 tw32_f(TG3_CPMU_EEE_MODE, val);
8504
8505 tw32_f(TG3_CPMU_EEE_DBTMR1,
8506 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
8507 TG3_CPMU_DBTMR1_LNKIDLE_2047US);
8508
8509 tw32_f(TG3_CPMU_EEE_DBTMR2,
Matt Carlsond7f2ab22011-01-25 15:58:56 +00008510 TG3_CPMU_DBTMR2_APE_TX_2047US |
Matt Carlsona386b902010-12-06 08:28:53 +00008511 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
Matt Carlson699c0192010-12-06 08:28:51 +00008512 }
8513
Matt Carlson603f1172010-02-12 14:47:10 +00008514 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08008515 tg3_phy_reset(tp);
8516
Linus Torvalds1da177e2005-04-16 15:20:36 -07008517 err = tg3_chip_reset(tp);
8518 if (err)
8519 return err;
8520
8521 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
8522
Matt Carlsonbcb37f62008-11-03 16:52:09 -08008523 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008524 val = tr32(TG3_CPMU_CTRL);
8525 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
8526 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08008527
8528 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8529 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8530 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8531 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
8532
8533 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
8534 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
8535 val |= CPMU_LNK_AWARE_MACCLK_6_25;
8536 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
8537
8538 val = tr32(TG3_CPMU_HST_ACC);
8539 val &= ~CPMU_HST_ACC_MACCLK_MASK;
8540 val |= CPMU_HST_ACC_MACCLK_6_25;
8541 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07008542 }
8543
Matt Carlson33466d932009-04-20 06:57:41 +00008544 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
8545 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
8546 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
8547 PCIE_PWR_MGMT_L1_THRESH_4MS;
8548 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00008549
8550 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
8551 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
8552
8553 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d932009-04-20 06:57:41 +00008554
Matt Carlsonf40386c2009-11-02 14:24:02 +00008555 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
8556 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00008557 }
8558
Joe Perches63c3a662011-04-26 08:12:10 +00008559 if (tg3_flag(tp, L1PLLPD_EN)) {
Matt Carlson614b0592010-01-20 16:58:02 +00008560 u32 grc_mode = tr32(GRC_MODE);
8561
8562 /* Access the lower 1K of PL PCIE block registers. */
8563 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8564 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
8565
8566 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
8567 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
8568 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
8569
8570 tw32(GRC_MODE, grc_mode);
8571 }
8572
Matt Carlson55086ad2011-12-14 11:09:59 +00008573 if (tg3_flag(tp, 57765_CLASS)) {
Matt Carlson5093eed2010-11-24 08:31:45 +00008574 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
8575 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00008576
Matt Carlson5093eed2010-11-24 08:31:45 +00008577 /* Access the lower 1K of PL PCIE block registers. */
8578 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8579 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00008580
Matt Carlson5093eed2010-11-24 08:31:45 +00008581 val = tr32(TG3_PCIE_TLDLPL_PORT +
8582 TG3_PCIE_PL_LO_PHYCTL5);
8583 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
8584 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00008585
Matt Carlson5093eed2010-11-24 08:31:45 +00008586 tw32(GRC_MODE, grc_mode);
8587 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00008588
Matt Carlson1ff30a52011-05-19 12:12:46 +00008589 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_57765_AX) {
8590 u32 grc_mode = tr32(GRC_MODE);
8591
8592 /* Access the lower 1K of DL PCIE block registers. */
8593 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8594 tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
8595
8596 val = tr32(TG3_PCIE_TLDLPL_PORT +
8597 TG3_PCIE_DL_LO_FTSMAX);
8598 val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
8599 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
8600 val | TG3_PCIE_DL_LO_FTSMAX_VAL);
8601
8602 tw32(GRC_MODE, grc_mode);
8603 }
8604
Matt Carlsona977dbe2010-04-12 06:58:26 +00008605 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8606 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8607 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8608 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00008609 }
8610
Linus Torvalds1da177e2005-04-16 15:20:36 -07008611 /* This works around an issue with Athlon chipsets on
8612 * B3 tigon3 silicon. This bit has no effect on any
8613 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07008614 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008615 */
Joe Perches63c3a662011-04-26 08:12:10 +00008616 if (!tg3_flag(tp, CPMU_PRESENT)) {
8617 if (!tg3_flag(tp, PCI_EXPRESS))
Matt Carlson795d01c2007-10-07 23:28:17 -07008618 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
8619 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
8620 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008621
8622 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008623 tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008624 val = tr32(TG3PCI_PCISTATE);
8625 val |= PCISTATE_RETRY_SAME_DMA;
8626 tw32(TG3PCI_PCISTATE, val);
8627 }
8628
Joe Perches63c3a662011-04-26 08:12:10 +00008629 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -07008630 /* Allow reads and writes to the
8631 * APE register and memory space.
8632 */
8633 val = tr32(TG3PCI_PCISTATE);
8634 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00008635 PCISTATE_ALLOW_APE_SHMEM_WR |
8636 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008637 tw32(TG3PCI_PCISTATE, val);
8638 }
8639
Linus Torvalds1da177e2005-04-16 15:20:36 -07008640 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
8641 /* Enable some hw fixes. */
8642 val = tr32(TG3PCI_MSI_DATA);
8643 val |= (1 << 26) | (1 << 28) | (1 << 29);
8644 tw32(TG3PCI_MSI_DATA, val);
8645 }
8646
8647 /* Descriptor ring init may make accesses to the
8648 * NIC SRAM area to setup the TX descriptors, so we
8649 * can only do this after the hardware has been
8650 * successfully reset.
8651 */
Michael Chan32d8c572006-07-25 16:38:29 -07008652 err = tg3_init_rings(tp);
8653 if (err)
8654 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008655
Joe Perches63c3a662011-04-26 08:12:10 +00008656 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008657 val = tr32(TG3PCI_DMA_RW_CTRL) &
8658 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Matt Carlson1a319022010-04-12 06:58:25 +00008659 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
8660 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlson55086ad2011-12-14 11:09:59 +00008661 if (!tg3_flag(tp, 57765_CLASS) &&
Matt Carlson0aebff42011-04-25 12:42:45 +00008662 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8663 val |= DMA_RWCTRL_TAGGED_STAT_WA;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008664 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
8665 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
8666 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008667 /* This value is determined during the probe time DMA
8668 * engine test, tg3_test_dma.
8669 */
8670 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
8671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008672
8673 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
8674 GRC_MODE_4X_NIC_SEND_RINGS |
8675 GRC_MODE_NO_TX_PHDR_CSUM |
8676 GRC_MODE_NO_RX_PHDR_CSUM);
8677 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07008678
8679 /* Pseudo-header checksum is done by hardware logic and not
8680 * the offload processers, so make the chip do the pseudo-
8681 * header checksums on receive. For transmit it is more
8682 * convenient to do the pseudo-header checksum in software
8683 * as Linux does that on transmit for us in all cases.
8684 */
8685 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008686
8687 tw32(GRC_MODE,
8688 tp->grc_mode |
8689 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
8690
8691 /* Setup the timer prescalar register. Clock is always 66Mhz. */
8692 val = tr32(GRC_MISC_CFG);
8693 val &= ~0xff;
8694 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
8695 tw32(GRC_MISC_CFG, val);
8696
8697 /* Initialize MBUF/DESC pool. */
Joe Perches63c3a662011-04-26 08:12:10 +00008698 if (tg3_flag(tp, 5750_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008699 /* Do nothing. */
8700 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
8701 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
8702 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
8703 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
8704 else
8705 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
8706 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
8707 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Joe Perches63c3a662011-04-26 08:12:10 +00008708 } else if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008709 int fw_len;
8710
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08008711 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008712 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
8713 tw32(BUFMGR_MB_POOL_ADDR,
8714 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
8715 tw32(BUFMGR_MB_POOL_SIZE,
8716 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
8717 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008718
Michael Chan0f893dc2005-07-25 12:30:38 -07008719 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008720 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8721 tp->bufmgr_config.mbuf_read_dma_low_water);
8722 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8723 tp->bufmgr_config.mbuf_mac_rx_low_water);
8724 tw32(BUFMGR_MB_HIGH_WATER,
8725 tp->bufmgr_config.mbuf_high_water);
8726 } else {
8727 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8728 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
8729 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8730 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
8731 tw32(BUFMGR_MB_HIGH_WATER,
8732 tp->bufmgr_config.mbuf_high_water_jumbo);
8733 }
8734 tw32(BUFMGR_DMA_LOW_WATER,
8735 tp->bufmgr_config.dma_low_water);
8736 tw32(BUFMGR_DMA_HIGH_WATER,
8737 tp->bufmgr_config.dma_high_water);
8738
Matt Carlsond309a462010-09-30 10:34:31 +00008739 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
8740 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
8741 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
Matt Carlson4d958472011-04-20 07:57:35 +00008742 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
8743 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
8744 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0)
8745 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
Matt Carlsond309a462010-09-30 10:34:31 +00008746 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008747 for (i = 0; i < 2000; i++) {
8748 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
8749 break;
8750 udelay(10);
8751 }
8752 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00008753 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008754 return -ENODEV;
8755 }
8756
Matt Carlsoneb07a942011-04-20 07:57:36 +00008757 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
8758 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
Michael Chanb5d37722006-09-27 16:06:21 -07008759
Matt Carlsoneb07a942011-04-20 07:57:36 +00008760 tg3_setup_rxbd_thresholds(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008761
8762 /* Initialize TG3_BDINFO's at:
8763 * RCVDBDI_STD_BD: standard eth size rx ring
8764 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
8765 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
8766 *
8767 * like so:
8768 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
8769 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
8770 * ring attribute flags
8771 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
8772 *
8773 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
8774 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
8775 *
8776 * The size of each ring is fixed in the firmware, but the location is
8777 * configurable.
8778 */
8779 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008780 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008781 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008782 ((u64) tpr->rx_std_mapping & 0xffffffff));
Joe Perches63c3a662011-04-26 08:12:10 +00008783 if (!tg3_flag(tp, 5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +00008784 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
8785 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008786
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008787 /* Disable the mini ring */
Joe Perches63c3a662011-04-26 08:12:10 +00008788 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008789 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
8790 BDINFO_FLAGS_DISABLED);
8791
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008792 /* Program the jumbo buffer descriptor ring control
8793 * blocks on those devices that have them.
8794 */
Matt Carlsona0512942011-07-27 14:20:54 +00008795 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008796 (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008797
Joe Perches63c3a662011-04-26 08:12:10 +00008798 if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008799 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008800 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008801 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008802 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +00008803 val = TG3_RX_JMB_RING_SIZE(tp) <<
8804 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008805 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +00008806 val | BDINFO_FLAGS_USE_EXT_RECV);
Joe Perches63c3a662011-04-26 08:12:10 +00008807 if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
Matt Carlson55086ad2011-12-14 11:09:59 +00008808 tg3_flag(tp, 57765_CLASS))
Matt Carlson87668d32009-11-13 13:03:34 +00008809 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
8810 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008811 } else {
8812 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
8813 BDINFO_FLAGS_DISABLED);
8814 }
8815
Joe Perches63c3a662011-04-26 08:12:10 +00008816 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonfa6b2aa2011-11-21 15:01:19 +00008817 val = TG3_RX_STD_RING_SIZE(tp);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008818 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
8819 val |= (TG3_RX_STD_DMA_SZ << 2);
8820 } else
Matt Carlson04380d42010-04-12 06:58:29 +00008821 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008822 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008823 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008824
8825 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008826
Matt Carlson411da642009-11-13 13:03:46 +00008827 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e662009-11-13 13:03:49 +00008828 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008829
Joe Perches63c3a662011-04-26 08:12:10 +00008830 tpr->rx_jmb_prod_idx =
8831 tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
Matt Carlson66711e662009-11-13 13:03:49 +00008832 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008833
Matt Carlson2d31eca2009-09-01 12:53:31 +00008834 tg3_rings_reset(tp);
8835
Linus Torvalds1da177e2005-04-16 15:20:36 -07008836 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07008837 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008838
8839 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00008840 tw32(MAC_RX_MTU_SIZE,
8841 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008842
8843 /* The slot time is changed by tg3_setup_phy if we
8844 * run at gigabit with half duplex.
8845 */
Matt Carlsonf2096f92011-04-05 14:22:48 +00008846 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
8847 (6 << TX_LENGTHS_IPG_SHIFT) |
8848 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
8849
8850 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8851 val |= tr32(MAC_TX_LENGTHS) &
8852 (TX_LENGTHS_JMB_FRM_LEN_MSK |
8853 TX_LENGTHS_CNT_DWN_VAL_MSK);
8854
8855 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008856
8857 /* Receive rules. */
8858 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
8859 tw32(RCVLPC_CONFIG, 0x0181);
8860
8861 /* Calculate RDMAC_MODE setting early, we need it to determine
8862 * the RCVLPC_STATE_ENABLE mask.
8863 */
8864 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
8865 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
8866 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
8867 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
8868 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07008869
Matt Carlsondeabaac2010-11-24 08:31:50 +00008870 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +00008871 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
8872
Matt Carlson57e69832008-05-25 23:48:31 -07008873 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08008874 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8875 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07008876 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
8877 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
8878 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
8879
Matt Carlsonc5908932011-03-09 16:58:25 +00008880 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8881 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00008882 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07008883 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008884 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
8885 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008886 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008887 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8888 }
8889 }
8890
Joe Perches63c3a662011-04-26 08:12:10 +00008891 if (tg3_flag(tp, PCI_EXPRESS))
Michael Chan85e94ce2005-04-21 17:05:28 -07008892 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8893
Joe Perches63c3a662011-04-26 08:12:10 +00008894 if (tg3_flag(tp, HW_TSO_1) ||
8895 tg3_flag(tp, HW_TSO_2) ||
8896 tg3_flag(tp, HW_TSO_3))
Matt Carlson027455a2008-12-21 20:19:30 -08008897 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
8898
Matt Carlson108a6c12011-05-19 12:12:47 +00008899 if (tg3_flag(tp, 57765_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +00008900 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson027455a2008-12-21 20:19:30 -08008901 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
8902 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008903
Matt Carlsonf2096f92011-04-05 14:22:48 +00008904 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8905 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
8906
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008907 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
8908 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
8909 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8910 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008911 tg3_flag(tp, 57765_PLUS)) {
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008912 val = tr32(TG3_RDMA_RSRVCTRL_REG);
Matt Carlsond78b59f2011-04-05 14:22:46 +00008913 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8914 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +00008915 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
8916 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
8917 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
8918 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
8919 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
8920 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +00008921 }
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008922 tw32(TG3_RDMA_RSRVCTRL_REG,
8923 val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
8924 }
8925
Matt Carlsond78b59f2011-04-05 14:22:46 +00008926 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8927 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsond309a462010-09-30 10:34:31 +00008928 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
8929 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val |
8930 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
8931 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
8932 }
8933
Linus Torvalds1da177e2005-04-16 15:20:36 -07008934 /* Receive/send statistics. */
Joe Perches63c3a662011-04-26 08:12:10 +00008935 if (tg3_flag(tp, 5750_PLUS)) {
Michael Chan16613942006-06-29 20:15:13 -07008936 val = tr32(RCVLPC_STATS_ENABLE);
8937 val &= ~RCVLPC_STATSENAB_DACK_FIX;
8938 tw32(RCVLPC_STATS_ENABLE, val);
8939 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008940 tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008941 val = tr32(RCVLPC_STATS_ENABLE);
8942 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
8943 tw32(RCVLPC_STATS_ENABLE, val);
8944 } else {
8945 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
8946 }
8947 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
8948 tw32(SNDDATAI_STATSENAB, 0xffffff);
8949 tw32(SNDDATAI_STATSCTRL,
8950 (SNDDATAI_SCTRL_ENABLE |
8951 SNDDATAI_SCTRL_FASTUPD));
8952
8953 /* Setup host coalescing engine. */
8954 tw32(HOSTCC_MODE, 0);
8955 for (i = 0; i < 2000; i++) {
8956 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
8957 break;
8958 udelay(10);
8959 }
8960
Michael Chand244c892005-07-05 14:42:33 -07008961 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008962
Joe Perches63c3a662011-04-26 08:12:10 +00008963 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008964 /* Status/statistics block address. See tg3_timer,
8965 * the tg3_periodic_fetch_stats call there, and
8966 * tg3_get_stats to see how this works for 5705/5750 chips.
8967 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008968 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8969 ((u64) tp->stats_mapping >> 32));
8970 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8971 ((u64) tp->stats_mapping & 0xffffffff));
8972 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008973
Linus Torvalds1da177e2005-04-16 15:20:36 -07008974 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008975
8976 /* Clear statistics and status block memory areas */
8977 for (i = NIC_SRAM_STATS_BLK;
8978 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
8979 i += sizeof(u32)) {
8980 tg3_write_mem(tp, i, 0);
8981 udelay(40);
8982 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008983 }
8984
8985 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
8986
8987 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
8988 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008989 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008990 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
8991
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008992 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
8993 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -07008994 /* reset to prevent losing 1st rx packet intermittently */
8995 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8996 udelay(10);
8997 }
8998
Matt Carlson3bda1252008-08-15 14:08:22 -07008999 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Matt Carlson9e975cc2011-07-20 10:20:50 +00009000 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE |
9001 MAC_MODE_FHDE_ENABLE;
9002 if (tg3_flag(tp, ENABLE_APE))
9003 tp->mac_mode |= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Joe Perches63c3a662011-04-26 08:12:10 +00009004 if (!tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009005 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07009006 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
9007 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009008 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
9009 udelay(40);
9010
Michael Chan314fba32005-04-21 17:07:04 -07009011 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Joe Perches63c3a662011-04-26 08:12:10 +00009012 * If TG3_FLAG_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07009013 * register to preserve the GPIO settings for LOMs. The GPIOs,
9014 * whether used as inputs or outputs, are set by boot code after
9015 * reset.
9016 */
Joe Perches63c3a662011-04-26 08:12:10 +00009017 if (!tg3_flag(tp, IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07009018 u32 gpio_mask;
9019
Michael Chan9d26e212006-12-07 00:21:14 -08009020 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
9021 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
9022 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07009023
9024 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
9025 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
9026 GRC_LCLCTRL_GPIO_OUTPUT3;
9027
Michael Chanaf36e6b2006-03-23 01:28:06 -08009028 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
9029 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
9030
Gary Zambranoaaf84462007-05-05 11:51:45 -07009031 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07009032 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
9033
9034 /* GPIO1 must be driven high for eeprom write protect */
Joe Perches63c3a662011-04-26 08:12:10 +00009035 if (tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan9d26e212006-12-07 00:21:14 -08009036 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
9037 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07009038 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009039 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
9040 udelay(100);
9041
Matt Carlsonc3b50032012-01-17 15:27:23 +00009042 if (tg3_flag(tp, USING_MSIX)) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009043 val = tr32(MSGINT_MODE);
Matt Carlsonc3b50032012-01-17 15:27:23 +00009044 val |= MSGINT_MODE_ENABLE;
9045 if (tp->irq_cnt > 1)
9046 val |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +00009047 if (!tg3_flag(tp, 1SHOT_MSI))
9048 val |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009049 tw32(MSGINT_MODE, val);
9050 }
9051
Joe Perches63c3a662011-04-26 08:12:10 +00009052 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009053 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
9054 udelay(40);
9055 }
9056
9057 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
9058 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
9059 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
9060 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
9061 WDMAC_MODE_LNGREAD_ENAB);
9062
Matt Carlsonc5908932011-03-09 16:58:25 +00009063 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
9064 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00009065 if (tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009066 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
9067 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
9068 /* nothing */
9069 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009070 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009071 val |= WDMAC_MODE_RX_ACCEL;
9072 }
9073 }
9074
Michael Chand9ab5ad12006-03-20 22:27:35 -08009075 /* Enable host coalescing bug fix */
Joe Perches63c3a662011-04-26 08:12:10 +00009076 if (tg3_flag(tp, 5755_PLUS))
Matt Carlsonf51f3562008-05-25 23:45:08 -07009077 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad12006-03-20 22:27:35 -08009078
Matt Carlson788a0352009-11-02 14:26:03 +00009079 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
9080 val |= WDMAC_MODE_BURST_ALL_DATA;
9081
Linus Torvalds1da177e2005-04-16 15:20:36 -07009082 tw32_f(WDMAC_MODE, val);
9083 udelay(40);
9084
Joe Perches63c3a662011-04-26 08:12:10 +00009085 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07009086 u16 pcix_cmd;
9087
9088 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
9089 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009090 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07009091 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
9092 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009093 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07009094 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
9095 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009096 }
Matt Carlson9974a352007-10-07 23:27:28 -07009097 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
9098 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009099 }
9100
9101 tw32_f(RDMAC_MODE, rdmac_mode);
9102 udelay(40);
9103
9104 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00009105 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009106 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07009107
9108 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
9109 tw32(SNDDATAC_MODE,
9110 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
9111 else
9112 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
9113
Linus Torvalds1da177e2005-04-16 15:20:36 -07009114 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
9115 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00009116 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00009117 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlson7cb32cf2010-09-30 10:34:36 +00009118 val |= RCVDBDI_MODE_LRG_RING_SZ;
9119 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009120 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00009121 if (tg3_flag(tp, HW_TSO_1) ||
9122 tg3_flag(tp, HW_TSO_2) ||
9123 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009124 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009125 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00009126 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009127 val |= SNDBDI_MODE_MULTI_TXQ_EN;
9128 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009129 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
9130
9131 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
9132 err = tg3_load_5701_a0_firmware_fix(tp);
9133 if (err)
9134 return err;
9135 }
9136
Joe Perches63c3a662011-04-26 08:12:10 +00009137 if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009138 err = tg3_load_tso_firmware(tp);
9139 if (err)
9140 return err;
9141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009142
9143 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +00009144
Joe Perches63c3a662011-04-26 08:12:10 +00009145 if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsonb1d05212010-06-05 17:24:31 +00009146 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
9147 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +00009148
9149 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
9150 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
9151 tp->tx_mode &= ~val;
9152 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
9153 }
9154
Linus Torvalds1da177e2005-04-16 15:20:36 -07009155 tw32_f(MAC_TX_MODE, tp->tx_mode);
9156 udelay(100);
9157
Joe Perches63c3a662011-04-26 08:12:10 +00009158 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009159 tg3_rss_write_indir_tbl(tp);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009160
9161 /* Setup the "secret" hash key. */
9162 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
9163 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
9164 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
9165 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
9166 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
9167 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
9168 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
9169 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
9170 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
9171 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
9172 }
9173
Linus Torvalds1da177e2005-04-16 15:20:36 -07009174 tp->rx_mode = RX_MODE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00009175 if (tg3_flag(tp, 5755_PLUS))
Michael Chanaf36e6b2006-03-23 01:28:06 -08009176 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
9177
Joe Perches63c3a662011-04-26 08:12:10 +00009178 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009179 tp->rx_mode |= RX_MODE_RSS_ENABLE |
9180 RX_MODE_RSS_ITBL_HASH_BITS_7 |
9181 RX_MODE_RSS_IPV6_HASH_EN |
9182 RX_MODE_RSS_TCP_IPV6_HASH_EN |
9183 RX_MODE_RSS_IPV4_HASH_EN |
9184 RX_MODE_RSS_TCP_IPV4_HASH_EN;
9185
Linus Torvalds1da177e2005-04-16 15:20:36 -07009186 tw32_f(MAC_RX_MODE, tp->rx_mode);
9187 udelay(10);
9188
Linus Torvalds1da177e2005-04-16 15:20:36 -07009189 tw32(MAC_LED_CTRL, tp->led_ctrl);
9190
9191 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009192 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009193 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
9194 udelay(10);
9195 }
9196 tw32_f(MAC_RX_MODE, tp->rx_mode);
9197 udelay(10);
9198
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009199 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009200 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009201 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009202 /* Set drive transmission level to 1.2V */
9203 /* only if the signal pre-emphasis bit is not set */
9204 val = tr32(MAC_SERDES_CFG);
9205 val &= 0xfffff000;
9206 val |= 0x880;
9207 tw32(MAC_SERDES_CFG, val);
9208 }
9209 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
9210 tw32(MAC_SERDES_CFG, 0x616000);
9211 }
9212
9213 /* Prevent chip from dropping frames when flow control
9214 * is enabled.
9215 */
Matt Carlson55086ad2011-12-14 11:09:59 +00009216 if (tg3_flag(tp, 57765_CLASS))
Matt Carlson666bc832010-01-20 16:58:03 +00009217 val = 1;
9218 else
9219 val = 2;
9220 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009221
9222 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009223 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009224 /* Use hardware link auto-negotiation */
Joe Perches63c3a662011-04-26 08:12:10 +00009225 tg3_flag_set(tp, HW_AUTONEG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009226 }
9227
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009228 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +00009229 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08009230 u32 tmp;
9231
9232 tmp = tr32(SERDES_RX_CTRL);
9233 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
9234 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
9235 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
9236 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
9237 }
9238
Joe Perches63c3a662011-04-26 08:12:10 +00009239 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonc6700ce2012-02-13 15:20:15 +00009240 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Matt Carlson80096062010-08-02 11:26:06 +00009241 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009242
Matt Carlsondd477002008-05-25 23:45:58 -07009243 err = tg3_setup_phy(tp, 0);
9244 if (err)
9245 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009246
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009247 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
9248 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07009249 u32 tmp;
9250
9251 /* Clear CRC stats. */
9252 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
9253 tg3_writephy(tp, MII_TG3_TEST1,
9254 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009255 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -07009256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009257 }
9258 }
9259
9260 __tg3_set_rx_mode(tp->dev);
9261
9262 /* Initialize receive rules. */
9263 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
9264 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
9265 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
9266 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
9267
Joe Perches63c3a662011-04-26 08:12:10 +00009268 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009269 limit = 8;
9270 else
9271 limit = 16;
Joe Perches63c3a662011-04-26 08:12:10 +00009272 if (tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009273 limit -= 4;
9274 switch (limit) {
9275 case 16:
9276 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
9277 case 15:
9278 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
9279 case 14:
9280 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
9281 case 13:
9282 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
9283 case 12:
9284 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
9285 case 11:
9286 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
9287 case 10:
9288 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
9289 case 9:
9290 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
9291 case 8:
9292 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
9293 case 7:
9294 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
9295 case 6:
9296 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
9297 case 5:
9298 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
9299 case 4:
9300 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
9301 case 3:
9302 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
9303 case 2:
9304 case 1:
9305
9306 default:
9307 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07009308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009309
Joe Perches63c3a662011-04-26 08:12:10 +00009310 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson9ce768e2007-10-11 19:49:11 -07009311 /* Write our heartbeat update interval to APE. */
9312 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
9313 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07009314
Linus Torvalds1da177e2005-04-16 15:20:36 -07009315 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
9316
Linus Torvalds1da177e2005-04-16 15:20:36 -07009317 return 0;
9318}
9319
9320/* Called at device open time to get the chip ready for
9321 * packet processing. Invoked with tp->lock held.
9322 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009323static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009324{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009325 tg3_switch_clocks(tp);
9326
9327 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
9328
Matt Carlson2f751b62008-08-04 23:17:34 -07009329 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009330}
9331
9332#define TG3_STAT_ADD32(PSTAT, REG) \
9333do { u32 __val = tr32(REG); \
9334 (PSTAT)->low += __val; \
9335 if ((PSTAT)->low < __val) \
9336 (PSTAT)->high += 1; \
9337} while (0)
9338
9339static void tg3_periodic_fetch_stats(struct tg3 *tp)
9340{
9341 struct tg3_hw_stats *sp = tp->hw_stats;
9342
9343 if (!netif_carrier_ok(tp->dev))
9344 return;
9345
9346 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
9347 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
9348 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
9349 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
9350 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
9351 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
9352 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
9353 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
9354 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
9355 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
9356 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
9357 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
9358 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
9359
9360 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
9361 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
9362 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
9363 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
9364 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
9365 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
9366 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
9367 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
9368 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
9369 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
9370 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
9371 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
9372 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
9373 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07009374
9375 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
Matt Carlson310050f2011-05-19 12:12:55 +00009376 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
9377 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0 &&
9378 tp->pci_chip_rev_id != CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +00009379 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
9380 } else {
9381 u32 val = tr32(HOSTCC_FLOW_ATTN);
9382 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
9383 if (val) {
9384 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
9385 sp->rx_discards.low += val;
9386 if (sp->rx_discards.low < val)
9387 sp->rx_discards.high += 1;
9388 }
9389 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
9390 }
Michael Chan463d3052006-05-22 16:36:27 -07009391 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009392}
9393
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009394static void tg3_chk_missed_msi(struct tg3 *tp)
9395{
9396 u32 i;
9397
9398 for (i = 0; i < tp->irq_cnt; i++) {
9399 struct tg3_napi *tnapi = &tp->napi[i];
9400
9401 if (tg3_has_work(tnapi)) {
9402 if (tnapi->last_rx_cons == tnapi->rx_rcb_ptr &&
9403 tnapi->last_tx_cons == tnapi->tx_cons) {
9404 if (tnapi->chk_msi_cnt < 1) {
9405 tnapi->chk_msi_cnt++;
9406 return;
9407 }
Matt Carlson7f230732011-08-31 11:44:48 +00009408 tg3_msi(0, tnapi);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009409 }
9410 }
9411 tnapi->chk_msi_cnt = 0;
9412 tnapi->last_rx_cons = tnapi->rx_rcb_ptr;
9413 tnapi->last_tx_cons = tnapi->tx_cons;
9414 }
9415}
9416
Linus Torvalds1da177e2005-04-16 15:20:36 -07009417static void tg3_timer(unsigned long __opaque)
9418{
9419 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009420
Matt Carlson5b190622011-11-04 09:15:04 +00009421 if (tp->irq_sync || tg3_flag(tp, RESET_TASK_PENDING))
Michael Chanf475f162006-03-27 23:20:14 -08009422 goto restart_timer;
9423
David S. Millerf47c11e2005-06-24 20:18:35 -07009424 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009425
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009426 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00009427 tg3_flag(tp, 57765_CLASS))
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009428 tg3_chk_missed_msi(tp);
9429
Joe Perches63c3a662011-04-26 08:12:10 +00009430 if (!tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -07009431 /* All of this garbage is because when using non-tagged
9432 * IRQ status the mailbox/status_block protocol the chip
9433 * uses with the cpu is race prone.
9434 */
Matt Carlson898a56f2009-08-28 14:02:40 +00009435 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -07009436 tw32(GRC_LOCAL_CTRL,
9437 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
9438 } else {
9439 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00009440 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -07009441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009442
David S. Millerfac9b832005-05-18 22:46:34 -07009443 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
David S. Millerf47c11e2005-06-24 20:18:35 -07009444 spin_unlock(&tp->lock);
Matt Carlsondb219972011-11-04 09:15:03 +00009445 tg3_reset_task_schedule(tp);
Matt Carlson5b190622011-11-04 09:15:04 +00009446 goto restart_timer;
David S. Millerfac9b832005-05-18 22:46:34 -07009447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009448 }
9449
Linus Torvalds1da177e2005-04-16 15:20:36 -07009450 /* This part only runs once per second. */
9451 if (!--tp->timer_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00009452 if (tg3_flag(tp, 5705_PLUS))
David S. Millerfac9b832005-05-18 22:46:34 -07009453 tg3_periodic_fetch_stats(tp);
9454
Matt Carlsonb0c59432011-05-19 12:12:48 +00009455 if (tp->setlpicnt && !--tp->setlpicnt)
9456 tg3_phy_eee_enable(tp);
Matt Carlson52b02d02010-10-14 10:37:41 +00009457
Joe Perches63c3a662011-04-26 08:12:10 +00009458 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009459 u32 mac_stat;
9460 int phy_event;
9461
9462 mac_stat = tr32(MAC_STATUS);
9463
9464 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009465 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009466 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
9467 phy_event = 1;
9468 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
9469 phy_event = 1;
9470
9471 if (phy_event)
9472 tg3_setup_phy(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +00009473 } else if (tg3_flag(tp, POLL_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009474 u32 mac_stat = tr32(MAC_STATUS);
9475 int need_setup = 0;
9476
9477 if (netif_carrier_ok(tp->dev) &&
9478 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
9479 need_setup = 1;
9480 }
Matt Carlsonbe98da62010-07-11 09:31:46 +00009481 if (!netif_carrier_ok(tp->dev) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009482 (mac_stat & (MAC_STATUS_PCS_SYNCED |
9483 MAC_STATUS_SIGNAL_DET))) {
9484 need_setup = 1;
9485 }
9486 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07009487 if (!tp->serdes_counter) {
9488 tw32_f(MAC_MODE,
9489 (tp->mac_mode &
9490 ~MAC_MODE_PORT_MODE_MASK));
9491 udelay(40);
9492 tw32_f(MAC_MODE, tp->mac_mode);
9493 udelay(40);
9494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009495 tg3_setup_phy(tp, 0);
9496 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009497 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009498 tg3_flag(tp, 5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -07009499 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +00009500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009501
9502 tp->timer_counter = tp->timer_multiplier;
9503 }
9504
Michael Chan130b8e42006-09-27 16:00:40 -07009505 /* Heartbeat is only sent once every 2 seconds.
9506 *
9507 * The heartbeat is to tell the ASF firmware that the host
9508 * driver is still alive. In the event that the OS crashes,
9509 * ASF needs to reset the hardware to free up the FIFO space
9510 * that may be filled with rx packets destined for the host.
9511 * If the FIFO is full, ASF will no longer function properly.
9512 *
9513 * Unintended resets have been reported on real time kernels
9514 * where the timer doesn't run on time. Netpoll will also have
9515 * same problem.
9516 *
9517 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
9518 * to check the ring condition when the heartbeat is expiring
9519 * before doing the reset. This will prevent most unintended
9520 * resets.
9521 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009522 if (!--tp->asf_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00009523 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07009524 tg3_wait_for_event_ack(tp);
9525
Michael Chanbbadf502006-04-06 21:46:34 -07009526 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07009527 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07009528 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +00009529 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
9530 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -07009531
9532 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009533 }
9534 tp->asf_counter = tp->asf_multiplier;
9535 }
9536
David S. Millerf47c11e2005-06-24 20:18:35 -07009537 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009538
Michael Chanf475f162006-03-27 23:20:14 -08009539restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07009540 tp->timer.expires = jiffies + tp->timer_offset;
9541 add_timer(&tp->timer);
9542}
9543
Matt Carlson21f76382012-02-22 12:35:21 +00009544static void __devinit tg3_timer_init(struct tg3 *tp)
9545{
9546 if (tg3_flag(tp, TAGGED_STATUS) &&
9547 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
9548 !tg3_flag(tp, 57765_CLASS))
9549 tp->timer_offset = HZ;
9550 else
9551 tp->timer_offset = HZ / 10;
9552
9553 BUG_ON(tp->timer_offset > HZ);
9554
9555 tp->timer_multiplier = (HZ / tp->timer_offset);
9556 tp->asf_multiplier = (HZ / tp->timer_offset) *
9557 TG3_FW_UPDATE_FREQ_SEC;
9558
9559 init_timer(&tp->timer);
9560 tp->timer.data = (unsigned long) tp;
9561 tp->timer.function = tg3_timer;
9562}
9563
9564static void tg3_timer_start(struct tg3 *tp)
9565{
9566 tp->asf_counter = tp->asf_multiplier;
9567 tp->timer_counter = tp->timer_multiplier;
9568
9569 tp->timer.expires = jiffies + tp->timer_offset;
9570 add_timer(&tp->timer);
9571}
9572
9573static void tg3_timer_stop(struct tg3 *tp)
9574{
9575 del_timer_sync(&tp->timer);
9576}
9577
9578/* Restart hardware after configuration changes, self-test, etc.
9579 * Invoked with tp->lock held.
9580 */
9581static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
9582 __releases(tp->lock)
9583 __acquires(tp->lock)
9584{
9585 int err;
9586
9587 err = tg3_init_hw(tp, reset_phy);
9588 if (err) {
9589 netdev_err(tp->dev,
9590 "Failed to re-initialize device, aborting\n");
9591 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
9592 tg3_full_unlock(tp);
9593 tg3_timer_stop(tp);
9594 tp->irq_sync = 0;
9595 tg3_napi_enable(tp);
9596 dev_close(tp->dev);
9597 tg3_full_lock(tp, 0);
9598 }
9599 return err;
9600}
9601
9602static void tg3_reset_task(struct work_struct *work)
9603{
9604 struct tg3 *tp = container_of(work, struct tg3, reset_task);
9605 int err;
9606
9607 tg3_full_lock(tp, 0);
9608
9609 if (!netif_running(tp->dev)) {
9610 tg3_flag_clear(tp, RESET_TASK_PENDING);
9611 tg3_full_unlock(tp);
9612 return;
9613 }
9614
9615 tg3_full_unlock(tp);
9616
9617 tg3_phy_stop(tp);
9618
9619 tg3_netif_stop(tp);
9620
9621 tg3_full_lock(tp, 1);
9622
9623 if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
9624 tp->write32_tx_mbox = tg3_write32_tx_mbox;
9625 tp->write32_rx_mbox = tg3_write_flush_reg32;
9626 tg3_flag_set(tp, MBOX_WRITE_REORDER);
9627 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
9628 }
9629
9630 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
9631 err = tg3_init_hw(tp, 1);
9632 if (err)
9633 goto out;
9634
9635 tg3_netif_start(tp);
9636
9637out:
9638 tg3_full_unlock(tp);
9639
9640 if (!err)
9641 tg3_phy_start(tp);
9642
9643 tg3_flag_clear(tp, RESET_TASK_PENDING);
9644}
9645
Matt Carlson4f125f42009-09-01 12:55:02 +00009646static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -08009647{
David Howells7d12e782006-10-05 14:55:46 +01009648 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009649 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +00009650 char *name;
9651 struct tg3_napi *tnapi = &tp->napi[irq_num];
9652
9653 if (tp->irq_cnt == 1)
9654 name = tp->dev->name;
9655 else {
9656 name = &tnapi->irq_lbl[0];
9657 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
9658 name[IFNAMSIZ-1] = 0;
9659 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009660
Joe Perches63c3a662011-04-26 08:12:10 +00009661 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Michael Chanfcfa0a32006-03-20 22:28:41 -08009662 fn = tg3_msi;
Joe Perches63c3a662011-04-26 08:12:10 +00009663 if (tg3_flag(tp, 1SHOT_MSI))
Michael Chanfcfa0a32006-03-20 22:28:41 -08009664 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009665 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009666 } else {
9667 fn = tg3_interrupt;
Joe Perches63c3a662011-04-26 08:12:10 +00009668 if (tg3_flag(tp, TAGGED_STATUS))
Michael Chanfcfa0a32006-03-20 22:28:41 -08009669 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009670 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009671 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009672
9673 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009674}
9675
Michael Chan79381092005-04-21 17:13:59 -07009676static int tg3_test_interrupt(struct tg3 *tp)
9677{
Matt Carlson09943a12009-08-28 14:01:57 +00009678 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -07009679 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -07009680 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009681 u32 val;
Michael Chan79381092005-04-21 17:13:59 -07009682
Michael Chand4bc3922005-05-29 14:59:20 -07009683 if (!netif_running(dev))
9684 return -ENODEV;
9685
Michael Chan79381092005-04-21 17:13:59 -07009686 tg3_disable_ints(tp);
9687
Matt Carlson4f125f42009-09-01 12:55:02 +00009688 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009689
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009690 /*
9691 * Turn off MSI one shot mode. Otherwise this test has no
9692 * observable way to know whether the interrupt was delivered.
9693 */
Matt Carlson3aa1cdf2011-07-20 10:20:55 +00009694 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009695 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
9696 tw32(MSGINT_MODE, val);
9697 }
9698
Matt Carlson4f125f42009-09-01 12:55:02 +00009699 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Davidlohr Buesof274fd92012-02-22 03:06:54 +00009700 IRQF_SHARED, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009701 if (err)
9702 return err;
9703
Matt Carlson898a56f2009-08-28 14:02:40 +00009704 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -07009705 tg3_enable_ints(tp);
9706
9707 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00009708 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -07009709
9710 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -07009711 u32 int_mbox, misc_host_ctrl;
9712
Matt Carlson898a56f2009-08-28 14:02:40 +00009713 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -07009714 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
9715
9716 if ((int_mbox != 0) ||
9717 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
9718 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -07009719 break;
Michael Chanb16250e2006-09-27 16:10:14 -07009720 }
9721
Matt Carlson3aa1cdf2011-07-20 10:20:55 +00009722 if (tg3_flag(tp, 57765_PLUS) &&
9723 tnapi->hw_status->status_tag != tnapi->last_tag)
9724 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
9725
Michael Chan79381092005-04-21 17:13:59 -07009726 msleep(10);
9727 }
9728
9729 tg3_disable_ints(tp);
9730
Matt Carlson4f125f42009-09-01 12:55:02 +00009731 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009732
Matt Carlson4f125f42009-09-01 12:55:02 +00009733 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009734
9735 if (err)
9736 return err;
9737
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009738 if (intr_ok) {
9739 /* Reenable MSI one shot mode. */
Matt Carlson5b39de92011-08-31 11:44:50 +00009740 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, 1SHOT_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009741 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
9742 tw32(MSGINT_MODE, val);
9743 }
Michael Chan79381092005-04-21 17:13:59 -07009744 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009745 }
Michael Chan79381092005-04-21 17:13:59 -07009746
9747 return -EIO;
9748}
9749
9750/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
9751 * successfully restored
9752 */
9753static int tg3_test_msi(struct tg3 *tp)
9754{
Michael Chan79381092005-04-21 17:13:59 -07009755 int err;
9756 u16 pci_cmd;
9757
Joe Perches63c3a662011-04-26 08:12:10 +00009758 if (!tg3_flag(tp, USING_MSI))
Michael Chan79381092005-04-21 17:13:59 -07009759 return 0;
9760
9761 /* Turn off SERR reporting in case MSI terminates with Master
9762 * Abort.
9763 */
9764 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
9765 pci_write_config_word(tp->pdev, PCI_COMMAND,
9766 pci_cmd & ~PCI_COMMAND_SERR);
9767
9768 err = tg3_test_interrupt(tp);
9769
9770 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
9771
9772 if (!err)
9773 return 0;
9774
9775 /* other failures */
9776 if (err != -EIO)
9777 return err;
9778
9779 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009780 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
9781 "to INTx mode. Please report this failure to the PCI "
9782 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -07009783
Matt Carlson4f125f42009-09-01 12:55:02 +00009784 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +00009785
Michael Chan79381092005-04-21 17:13:59 -07009786 pci_disable_msi(tp->pdev);
9787
Joe Perches63c3a662011-04-26 08:12:10 +00009788 tg3_flag_clear(tp, USING_MSI);
Andre Detschdc8bf1b2010-04-26 07:27:07 +00009789 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -07009790
Matt Carlson4f125f42009-09-01 12:55:02 +00009791 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009792 if (err)
9793 return err;
9794
9795 /* Need to reset the chip because the MSI cycle may have terminated
9796 * with Master Abort.
9797 */
David S. Millerf47c11e2005-06-24 20:18:35 -07009798 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009799
Michael Chan944d9802005-05-29 14:57:48 -07009800 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009801 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009802
David S. Millerf47c11e2005-06-24 20:18:35 -07009803 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009804
9805 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +00009806 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -07009807
9808 return err;
9809}
9810
Matt Carlson9e9fd122009-01-19 16:57:45 -08009811static int tg3_request_firmware(struct tg3 *tp)
9812{
9813 const __be32 *fw_data;
9814
9815 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009816 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
9817 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009818 return -ENOENT;
9819 }
9820
9821 fw_data = (void *)tp->fw->data;
9822
9823 /* Firmware blob starts with version numbers, followed by
9824 * start address and _full_ length including BSS sections
9825 * (which must be longer than the actual data, of course
9826 */
9827
9828 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
9829 if (tp->fw_len < (tp->fw->size - 12)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009830 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
9831 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009832 release_firmware(tp->fw);
9833 tp->fw = NULL;
9834 return -EINVAL;
9835 }
9836
9837 /* We no longer need firmware; we have it. */
9838 tp->fw_needed = NULL;
9839 return 0;
9840}
9841
Matt Carlson679563f2009-09-01 12:55:46 +00009842static bool tg3_enable_msix(struct tg3 *tp)
9843{
Matt Carlsonc3b50032012-01-17 15:27:23 +00009844 int i, rc;
Matt Carlson679563f2009-09-01 12:55:46 +00009845 struct msix_entry msix_ent[tp->irq_max];
9846
Matt Carlsonc3b50032012-01-17 15:27:23 +00009847 tp->irq_cnt = num_online_cpus();
9848 if (tp->irq_cnt > 1) {
9849 /* We want as many rx rings enabled as there are cpus.
9850 * In multiqueue MSI-X mode, the first MSI-X vector
9851 * only deals with link interrupts, etc, so we add
9852 * one to the number of vectors we are requesting.
9853 */
9854 tp->irq_cnt = min_t(unsigned, tp->irq_cnt + 1, tp->irq_max);
9855 }
Matt Carlson679563f2009-09-01 12:55:46 +00009856
9857 for (i = 0; i < tp->irq_max; i++) {
9858 msix_ent[i].entry = i;
9859 msix_ent[i].vector = 0;
9860 }
9861
9862 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +00009863 if (rc < 0) {
9864 return false;
9865 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +00009866 if (pci_enable_msix(tp->pdev, msix_ent, rc))
9867 return false;
Joe Perches05dbe002010-02-17 19:44:19 +00009868 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
9869 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +00009870 tp->irq_cnt = rc;
9871 }
9872
9873 for (i = 0; i < tp->irq_max; i++)
9874 tp->napi[i].irq_vec = msix_ent[i].vector;
9875
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009876 netif_set_real_num_tx_queues(tp->dev, 1);
9877 rc = tp->irq_cnt > 1 ? tp->irq_cnt - 1 : 1;
9878 if (netif_set_real_num_rx_queues(tp->dev, rc)) {
9879 pci_disable_msix(tp->pdev);
9880 return false;
9881 }
Matt Carlsonb92b9042010-11-24 08:31:51 +00009882
9883 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +00009884 tg3_flag_set(tp, ENABLE_RSS);
Matt Carlsond78b59f2011-04-05 14:22:46 +00009885
9886 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
9887 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Joe Perches63c3a662011-04-26 08:12:10 +00009888 tg3_flag_set(tp, ENABLE_TSS);
Matt Carlsonb92b9042010-11-24 08:31:51 +00009889 netif_set_real_num_tx_queues(tp->dev, tp->irq_cnt - 1);
9890 }
9891 }
Matt Carlson2430b032010-06-05 17:24:34 +00009892
Matt Carlson679563f2009-09-01 12:55:46 +00009893 return true;
9894}
9895
Matt Carlson07b01732009-08-28 14:01:15 +00009896static void tg3_ints_init(struct tg3 *tp)
9897{
Joe Perches63c3a662011-04-26 08:12:10 +00009898 if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
9899 !tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +00009900 /* All MSI supporting chips should support tagged
9901 * status. Assert that this is the case.
9902 */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009903 netdev_warn(tp->dev,
9904 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +00009905 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +00009906 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009907
Joe Perches63c3a662011-04-26 08:12:10 +00009908 if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
9909 tg3_flag_set(tp, USING_MSIX);
9910 else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
9911 tg3_flag_set(tp, USING_MSI);
Matt Carlson679563f2009-09-01 12:55:46 +00009912
Joe Perches63c3a662011-04-26 08:12:10 +00009913 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009914 u32 msi_mode = tr32(MSGINT_MODE);
Joe Perches63c3a662011-04-26 08:12:10 +00009915 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009916 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +00009917 if (!tg3_flag(tp, 1SHOT_MSI))
9918 msi_mode |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlson679563f2009-09-01 12:55:46 +00009919 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
9920 }
9921defcfg:
Joe Perches63c3a662011-04-26 08:12:10 +00009922 if (!tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009923 tp->irq_cnt = 1;
9924 tp->napi[0].irq_vec = tp->pdev->irq;
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009925 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -07009926 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +00009927 }
Matt Carlson07b01732009-08-28 14:01:15 +00009928}
9929
9930static void tg3_ints_fini(struct tg3 *tp)
9931{
Joe Perches63c3a662011-04-26 08:12:10 +00009932 if (tg3_flag(tp, USING_MSIX))
Matt Carlson679563f2009-09-01 12:55:46 +00009933 pci_disable_msix(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009934 else if (tg3_flag(tp, USING_MSI))
Matt Carlson679563f2009-09-01 12:55:46 +00009935 pci_disable_msi(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009936 tg3_flag_clear(tp, USING_MSI);
9937 tg3_flag_clear(tp, USING_MSIX);
9938 tg3_flag_clear(tp, ENABLE_RSS);
9939 tg3_flag_clear(tp, ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +00009940}
9941
Linus Torvalds1da177e2005-04-16 15:20:36 -07009942static int tg3_open(struct net_device *dev)
9943{
9944 struct tg3 *tp = netdev_priv(dev);
Matt Carlson4f125f42009-09-01 12:55:02 +00009945 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009946
Matt Carlson9e9fd122009-01-19 16:57:45 -08009947 if (tp->fw_needed) {
9948 err = tg3_request_firmware(tp);
9949 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
9950 if (err)
9951 return err;
9952 } else if (err) {
Joe Perches05dbe002010-02-17 19:44:19 +00009953 netdev_warn(tp->dev, "TSO capability disabled\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009954 tg3_flag_clear(tp, TSO_CAPABLE);
9955 } else if (!tg3_flag(tp, TSO_CAPABLE)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009956 netdev_notice(tp->dev, "TSO capability restored\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009957 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009958 }
9959 }
9960
Michael Chanc49a1562006-12-17 17:07:29 -08009961 netif_carrier_off(tp->dev);
9962
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009963 err = tg3_power_up(tp);
Matt Carlson2f751b62008-08-04 23:17:34 -07009964 if (err)
Michael Chanbc1c7562006-03-20 17:48:03 -08009965 return err;
Matt Carlson2f751b62008-08-04 23:17:34 -07009966
9967 tg3_full_lock(tp, 0);
Michael Chanbc1c7562006-03-20 17:48:03 -08009968
Linus Torvalds1da177e2005-04-16 15:20:36 -07009969 tg3_disable_ints(tp);
Joe Perches63c3a662011-04-26 08:12:10 +00009970 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009971
David S. Millerf47c11e2005-06-24 20:18:35 -07009972 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009973
Matt Carlson679563f2009-09-01 12:55:46 +00009974 /*
9975 * Setup interrupts first so we know how
9976 * many NAPI resources to allocate
9977 */
9978 tg3_ints_init(tp);
9979
Matt Carlson90415472011-12-16 13:33:23 +00009980 tg3_rss_check_indir_tbl(tp);
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009981
Linus Torvalds1da177e2005-04-16 15:20:36 -07009982 /* The placement of this call is tied
9983 * to the setup and use of Host TX descriptors.
9984 */
9985 err = tg3_alloc_consistent(tp);
9986 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009987 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009988
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009989 tg3_napi_init(tp);
9990
Matt Carlsonfed97812009-09-01 13:10:19 +00009991 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07009992
Matt Carlson4f125f42009-09-01 12:55:02 +00009993 for (i = 0; i < tp->irq_cnt; i++) {
9994 struct tg3_napi *tnapi = &tp->napi[i];
9995 err = tg3_request_irq(tp, i);
9996 if (err) {
Matt Carlson5bc09182011-11-04 09:15:01 +00009997 for (i--; i >= 0; i--) {
9998 tnapi = &tp->napi[i];
Matt Carlson4f125f42009-09-01 12:55:02 +00009999 free_irq(tnapi->irq_vec, tnapi);
Matt Carlson5bc09182011-11-04 09:15:01 +000010000 }
10001 goto err_out2;
Matt Carlson4f125f42009-09-01 12:55:02 +000010002 }
10003 }
Matt Carlson07b01732009-08-28 14:01:15 +000010004
David S. Millerf47c11e2005-06-24 20:18:35 -070010005 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010006
Gary Zambrano8e7a22e2006-04-29 18:59:13 -070010007 err = tg3_init_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010008 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -070010009 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010010 tg3_free_rings(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010011 }
10012
David S. Millerf47c11e2005-06-24 20:18:35 -070010013 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010014
Matt Carlson07b01732009-08-28 14:01:15 +000010015 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +000010016 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010017
Joe Perches63c3a662011-04-26 08:12:10 +000010018 if (tg3_flag(tp, USING_MSI)) {
Michael Chan79381092005-04-21 17:13:59 -070010019 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -070010020
Michael Chan79381092005-04-21 17:13:59 -070010021 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -070010022 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070010023 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -070010024 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070010025 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -070010026
Matt Carlson679563f2009-09-01 12:55:46 +000010027 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -070010028 }
Michael Chanfcfa0a32006-03-20 22:28:41 -080010029
Joe Perches63c3a662011-04-26 08:12:10 +000010030 if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010031 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -080010032
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010033 tw32(PCIE_TRANSACTION_CFG,
10034 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -080010035 }
Michael Chan79381092005-04-21 17:13:59 -070010036 }
10037
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010038 tg3_phy_start(tp);
10039
David S. Millerf47c11e2005-06-24 20:18:35 -070010040 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010041
Matt Carlson21f76382012-02-22 12:35:21 +000010042 tg3_timer_start(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000010043 tg3_flag_set(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010044 tg3_enable_ints(tp);
10045
David S. Millerf47c11e2005-06-24 20:18:35 -070010046 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010047
Matt Carlsonfe5f5782009-09-01 13:09:39 +000010048 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010049
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000010050 /*
10051 * Reset loopback feature if it was turned on while the device was down
10052 * make sure that it's installed properly now.
10053 */
10054 if (dev->features & NETIF_F_LOOPBACK)
10055 tg3_set_loopback(dev, dev->features);
10056
Linus Torvalds1da177e2005-04-16 15:20:36 -070010057 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +000010058
Matt Carlson679563f2009-09-01 12:55:46 +000010059err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +000010060 for (i = tp->irq_cnt - 1; i >= 0; i--) {
10061 struct tg3_napi *tnapi = &tp->napi[i];
10062 free_irq(tnapi->irq_vec, tnapi);
10063 }
Matt Carlson07b01732009-08-28 14:01:15 +000010064
Matt Carlson679563f2009-09-01 12:55:46 +000010065err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +000010066 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000010067 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +000010068 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +000010069
10070err_out1:
10071 tg3_ints_fini(tp);
Matt Carlsoncd0d7222011-07-13 09:27:33 +000010072 tg3_frob_aux_power(tp, false);
10073 pci_set_power_state(tp->pdev, PCI_D3hot);
Matt Carlson07b01732009-08-28 14:01:15 +000010074 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010075}
10076
Linus Torvalds1da177e2005-04-16 15:20:36 -070010077static int tg3_close(struct net_device *dev)
10078{
Matt Carlson4f125f42009-09-01 12:55:02 +000010079 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010080 struct tg3 *tp = netdev_priv(dev);
10081
Matt Carlsonfed97812009-09-01 13:10:19 +000010082 tg3_napi_disable(tp);
Matt Carlsondb219972011-11-04 09:15:03 +000010083 tg3_reset_task_cancel(tp);
Michael Chan7faa0062006-02-02 17:29:28 -080010084
Matt Carlsonfe5f5782009-09-01 13:09:39 +000010085 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010086
Matt Carlson21f76382012-02-22 12:35:21 +000010087 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010088
Matt Carlson24bb4fb2009-10-05 17:55:29 +000010089 tg3_phy_stop(tp);
10090
David S. Millerf47c11e2005-06-24 20:18:35 -070010091 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010092
10093 tg3_disable_ints(tp);
10094
Michael Chan944d9802005-05-29 14:57:48 -070010095 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010096 tg3_free_rings(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000010097 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010098
David S. Millerf47c11e2005-06-24 20:18:35 -070010099 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010100
Matt Carlson4f125f42009-09-01 12:55:02 +000010101 for (i = tp->irq_cnt - 1; i >= 0; i--) {
10102 struct tg3_napi *tnapi = &tp->napi[i];
10103 free_irq(tnapi->irq_vec, tnapi);
10104 }
Matt Carlson07b01732009-08-28 14:01:15 +000010105
10106 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010107
Matt Carlson92feeab2011-12-08 14:40:14 +000010108 /* Clear stats across close / open calls */
10109 memset(&tp->net_stats_prev, 0, sizeof(tp->net_stats_prev));
10110 memset(&tp->estats_prev, 0, sizeof(tp->estats_prev));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010111
Matt Carlson66cfd1b2010-09-30 10:34:30 +000010112 tg3_napi_fini(tp);
10113
Linus Torvalds1da177e2005-04-16 15:20:36 -070010114 tg3_free_consistent(tp);
10115
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000010116 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080010117
10118 netif_carrier_off(tp->dev);
10119
Linus Torvalds1da177e2005-04-16 15:20:36 -070010120 return 0;
10121}
10122
Eric Dumazet511d2222010-07-07 20:44:24 +000010123static inline u64 get_stat64(tg3_stat64_t *val)
Stefan Buehler816f8b82008-08-15 14:10:54 -070010124{
10125 return ((u64)val->high << 32) | ((u64)val->low);
10126}
10127
Eric Dumazet511d2222010-07-07 20:44:24 +000010128static u64 calc_crc_errors(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010129{
10130 struct tg3_hw_stats *hw_stats = tp->hw_stats;
10131
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010132 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010133 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
10134 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010135 u32 val;
10136
David S. Millerf47c11e2005-06-24 20:18:35 -070010137 spin_lock_bh(&tp->lock);
Michael Chan569a5df2007-02-13 12:18:15 -080010138 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
10139 tg3_writephy(tp, MII_TG3_TEST1,
10140 val | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +000010141 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010142 } else
10143 val = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -070010144 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010145
10146 tp->phy_crc_errors += val;
10147
10148 return tp->phy_crc_errors;
10149 }
10150
10151 return get_stat64(&hw_stats->rx_fcs_errors);
10152}
10153
10154#define ESTAT_ADD(member) \
10155 estats->member = old_estats->member + \
Eric Dumazet511d2222010-07-07 20:44:24 +000010156 get_stat64(&hw_stats->member)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010157
Matt Carlson0e6c9da2011-12-08 14:40:13 +000010158static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *tp,
10159 struct tg3_ethtool_stats *estats)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010160{
Linus Torvalds1da177e2005-04-16 15:20:36 -070010161 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
10162 struct tg3_hw_stats *hw_stats = tp->hw_stats;
10163
Linus Torvalds1da177e2005-04-16 15:20:36 -070010164 ESTAT_ADD(rx_octets);
10165 ESTAT_ADD(rx_fragments);
10166 ESTAT_ADD(rx_ucast_packets);
10167 ESTAT_ADD(rx_mcast_packets);
10168 ESTAT_ADD(rx_bcast_packets);
10169 ESTAT_ADD(rx_fcs_errors);
10170 ESTAT_ADD(rx_align_errors);
10171 ESTAT_ADD(rx_xon_pause_rcvd);
10172 ESTAT_ADD(rx_xoff_pause_rcvd);
10173 ESTAT_ADD(rx_mac_ctrl_rcvd);
10174 ESTAT_ADD(rx_xoff_entered);
10175 ESTAT_ADD(rx_frame_too_long_errors);
10176 ESTAT_ADD(rx_jabbers);
10177 ESTAT_ADD(rx_undersize_packets);
10178 ESTAT_ADD(rx_in_length_errors);
10179 ESTAT_ADD(rx_out_length_errors);
10180 ESTAT_ADD(rx_64_or_less_octet_packets);
10181 ESTAT_ADD(rx_65_to_127_octet_packets);
10182 ESTAT_ADD(rx_128_to_255_octet_packets);
10183 ESTAT_ADD(rx_256_to_511_octet_packets);
10184 ESTAT_ADD(rx_512_to_1023_octet_packets);
10185 ESTAT_ADD(rx_1024_to_1522_octet_packets);
10186 ESTAT_ADD(rx_1523_to_2047_octet_packets);
10187 ESTAT_ADD(rx_2048_to_4095_octet_packets);
10188 ESTAT_ADD(rx_4096_to_8191_octet_packets);
10189 ESTAT_ADD(rx_8192_to_9022_octet_packets);
10190
10191 ESTAT_ADD(tx_octets);
10192 ESTAT_ADD(tx_collisions);
10193 ESTAT_ADD(tx_xon_sent);
10194 ESTAT_ADD(tx_xoff_sent);
10195 ESTAT_ADD(tx_flow_control);
10196 ESTAT_ADD(tx_mac_errors);
10197 ESTAT_ADD(tx_single_collisions);
10198 ESTAT_ADD(tx_mult_collisions);
10199 ESTAT_ADD(tx_deferred);
10200 ESTAT_ADD(tx_excessive_collisions);
10201 ESTAT_ADD(tx_late_collisions);
10202 ESTAT_ADD(tx_collide_2times);
10203 ESTAT_ADD(tx_collide_3times);
10204 ESTAT_ADD(tx_collide_4times);
10205 ESTAT_ADD(tx_collide_5times);
10206 ESTAT_ADD(tx_collide_6times);
10207 ESTAT_ADD(tx_collide_7times);
10208 ESTAT_ADD(tx_collide_8times);
10209 ESTAT_ADD(tx_collide_9times);
10210 ESTAT_ADD(tx_collide_10times);
10211 ESTAT_ADD(tx_collide_11times);
10212 ESTAT_ADD(tx_collide_12times);
10213 ESTAT_ADD(tx_collide_13times);
10214 ESTAT_ADD(tx_collide_14times);
10215 ESTAT_ADD(tx_collide_15times);
10216 ESTAT_ADD(tx_ucast_packets);
10217 ESTAT_ADD(tx_mcast_packets);
10218 ESTAT_ADD(tx_bcast_packets);
10219 ESTAT_ADD(tx_carrier_sense_errors);
10220 ESTAT_ADD(tx_discards);
10221 ESTAT_ADD(tx_errors);
10222
10223 ESTAT_ADD(dma_writeq_full);
10224 ESTAT_ADD(dma_write_prioq_full);
10225 ESTAT_ADD(rxbds_empty);
10226 ESTAT_ADD(rx_discards);
10227 ESTAT_ADD(rx_errors);
10228 ESTAT_ADD(rx_threshold_hit);
10229
10230 ESTAT_ADD(dma_readq_full);
10231 ESTAT_ADD(dma_read_prioq_full);
10232 ESTAT_ADD(tx_comp_queue_full);
10233
10234 ESTAT_ADD(ring_set_send_prod_index);
10235 ESTAT_ADD(ring_status_update);
10236 ESTAT_ADD(nic_irqs);
10237 ESTAT_ADD(nic_avoided_irqs);
10238 ESTAT_ADD(nic_tx_threshold_hit);
10239
Matt Carlson4452d092011-05-19 12:12:51 +000010240 ESTAT_ADD(mbuf_lwm_thresh_hit);
10241
Linus Torvalds1da177e2005-04-16 15:20:36 -070010242 return estats;
10243}
10244
Eric Dumazet511d2222010-07-07 20:44:24 +000010245static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
10246 struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010247{
10248 struct tg3 *tp = netdev_priv(dev);
Eric Dumazet511d2222010-07-07 20:44:24 +000010249 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010250 struct tg3_hw_stats *hw_stats = tp->hw_stats;
10251
10252 if (!hw_stats)
10253 return old_stats;
10254
10255 stats->rx_packets = old_stats->rx_packets +
10256 get_stat64(&hw_stats->rx_ucast_packets) +
10257 get_stat64(&hw_stats->rx_mcast_packets) +
10258 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010259
Linus Torvalds1da177e2005-04-16 15:20:36 -070010260 stats->tx_packets = old_stats->tx_packets +
10261 get_stat64(&hw_stats->tx_ucast_packets) +
10262 get_stat64(&hw_stats->tx_mcast_packets) +
10263 get_stat64(&hw_stats->tx_bcast_packets);
10264
10265 stats->rx_bytes = old_stats->rx_bytes +
10266 get_stat64(&hw_stats->rx_octets);
10267 stats->tx_bytes = old_stats->tx_bytes +
10268 get_stat64(&hw_stats->tx_octets);
10269
10270 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -070010271 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010272 stats->tx_errors = old_stats->tx_errors +
10273 get_stat64(&hw_stats->tx_errors) +
10274 get_stat64(&hw_stats->tx_mac_errors) +
10275 get_stat64(&hw_stats->tx_carrier_sense_errors) +
10276 get_stat64(&hw_stats->tx_discards);
10277
10278 stats->multicast = old_stats->multicast +
10279 get_stat64(&hw_stats->rx_mcast_packets);
10280 stats->collisions = old_stats->collisions +
10281 get_stat64(&hw_stats->tx_collisions);
10282
10283 stats->rx_length_errors = old_stats->rx_length_errors +
10284 get_stat64(&hw_stats->rx_frame_too_long_errors) +
10285 get_stat64(&hw_stats->rx_undersize_packets);
10286
10287 stats->rx_over_errors = old_stats->rx_over_errors +
10288 get_stat64(&hw_stats->rxbds_empty);
10289 stats->rx_frame_errors = old_stats->rx_frame_errors +
10290 get_stat64(&hw_stats->rx_align_errors);
10291 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
10292 get_stat64(&hw_stats->tx_discards);
10293 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
10294 get_stat64(&hw_stats->tx_carrier_sense_errors);
10295
10296 stats->rx_crc_errors = old_stats->rx_crc_errors +
10297 calc_crc_errors(tp);
10298
John W. Linville4f63b872005-09-12 14:43:18 -070010299 stats->rx_missed_errors = old_stats->rx_missed_errors +
10300 get_stat64(&hw_stats->rx_discards);
10301
Eric Dumazetb0057c52010-10-10 19:55:52 +000010302 stats->rx_dropped = tp->rx_dropped;
Eric Dumazet48855432011-10-24 07:53:03 +000010303 stats->tx_dropped = tp->tx_dropped;
Eric Dumazetb0057c52010-10-10 19:55:52 +000010304
Linus Torvalds1da177e2005-04-16 15:20:36 -070010305 return stats;
10306}
10307
Linus Torvalds1da177e2005-04-16 15:20:36 -070010308static int tg3_get_regs_len(struct net_device *dev)
10309{
Matt Carlson97bd8e42011-04-13 11:05:04 +000010310 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010311}
10312
10313static void tg3_get_regs(struct net_device *dev,
10314 struct ethtool_regs *regs, void *_p)
10315{
Linus Torvalds1da177e2005-04-16 15:20:36 -070010316 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010317
10318 regs->version = 0;
10319
Matt Carlson97bd8e42011-04-13 11:05:04 +000010320 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010321
Matt Carlson80096062010-08-02 11:26:06 +000010322 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010323 return;
10324
David S. Millerf47c11e2005-06-24 20:18:35 -070010325 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010326
Matt Carlson97bd8e42011-04-13 11:05:04 +000010327 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010328
David S. Millerf47c11e2005-06-24 20:18:35 -070010329 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010330}
10331
10332static int tg3_get_eeprom_len(struct net_device *dev)
10333{
10334 struct tg3 *tp = netdev_priv(dev);
10335
10336 return tp->nvram_size;
10337}
10338
Linus Torvalds1da177e2005-04-16 15:20:36 -070010339static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
10340{
10341 struct tg3 *tp = netdev_priv(dev);
10342 int ret;
10343 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -080010344 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010345 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010346
Joe Perches63c3a662011-04-26 08:12:10 +000010347 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000010348 return -EINVAL;
10349
Matt Carlson80096062010-08-02 11:26:06 +000010350 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010351 return -EAGAIN;
10352
Linus Torvalds1da177e2005-04-16 15:20:36 -070010353 offset = eeprom->offset;
10354 len = eeprom->len;
10355 eeprom->len = 0;
10356
10357 eeprom->magic = TG3_EEPROM_MAGIC;
10358
10359 if (offset & 3) {
10360 /* adjustments to start on required 4 byte boundary */
10361 b_offset = offset & 3;
10362 b_count = 4 - b_offset;
10363 if (b_count > len) {
10364 /* i.e. offset=1 len=2 */
10365 b_count = len;
10366 }
Matt Carlsona9dc5292009-02-25 14:25:30 +000010367 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010368 if (ret)
10369 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +000010370 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010371 len -= b_count;
10372 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010373 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010374 }
10375
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010376 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010377 pd = &data[eeprom->len];
10378 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010379 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010380 if (ret) {
10381 eeprom->len += i;
10382 return ret;
10383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010384 memcpy(pd + i, &val, 4);
10385 }
10386 eeprom->len += i;
10387
10388 if (len & 3) {
10389 /* read last bytes not ending on 4 byte boundary */
10390 pd = &data[eeprom->len];
10391 b_count = len & 3;
10392 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010393 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010394 if (ret)
10395 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080010396 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010397 eeprom->len += b_count;
10398 }
10399 return 0;
10400}
10401
Linus Torvalds1da177e2005-04-16 15:20:36 -070010402static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
10403{
10404 struct tg3 *tp = netdev_priv(dev);
10405 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080010406 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010407 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010408 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010409
Matt Carlson80096062010-08-02 11:26:06 +000010410 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010411 return -EAGAIN;
10412
Joe Perches63c3a662011-04-26 08:12:10 +000010413 if (tg3_flag(tp, NO_NVRAM) ||
Matt Carlsondf259d82009-04-20 06:57:14 +000010414 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010415 return -EINVAL;
10416
10417 offset = eeprom->offset;
10418 len = eeprom->len;
10419
10420 if ((b_offset = (offset & 3))) {
10421 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010422 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010423 if (ret)
10424 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010425 len += b_offset;
10426 offset &= ~3;
Michael Chan1c8594b42005-04-21 17:12:46 -070010427 if (len < 4)
10428 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010429 }
10430
10431 odd_len = 0;
Michael Chan1c8594b42005-04-21 17:12:46 -070010432 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010433 /* adjustments to end on required 4 byte boundary */
10434 odd_len = 1;
10435 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010436 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010437 if (ret)
10438 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010439 }
10440
10441 buf = data;
10442 if (b_offset || odd_len) {
10443 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010444 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010445 return -ENOMEM;
10446 if (b_offset)
10447 memcpy(buf, &start, 4);
10448 if (odd_len)
10449 memcpy(buf+len-4, &end, 4);
10450 memcpy(buf + b_offset, data, eeprom->len);
10451 }
10452
10453 ret = tg3_nvram_write_block(tp, offset, len, buf);
10454
10455 if (buf != data)
10456 kfree(buf);
10457
10458 return ret;
10459}
10460
10461static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
10462{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010463 struct tg3 *tp = netdev_priv(dev);
10464
Joe Perches63c3a662011-04-26 08:12:10 +000010465 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010466 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010467 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010468 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010469 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10470 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010471 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010472
Linus Torvalds1da177e2005-04-16 15:20:36 -070010473 cmd->supported = (SUPPORTED_Autoneg);
10474
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010475 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010476 cmd->supported |= (SUPPORTED_1000baseT_Half |
10477 SUPPORTED_1000baseT_Full);
10478
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010479 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010480 cmd->supported |= (SUPPORTED_100baseT_Half |
10481 SUPPORTED_100baseT_Full |
10482 SUPPORTED_10baseT_Half |
10483 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -080010484 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -070010485 cmd->port = PORT_TP;
10486 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010487 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -070010488 cmd->port = PORT_FIBRE;
10489 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010490
Linus Torvalds1da177e2005-04-16 15:20:36 -070010491 cmd->advertising = tp->link_config.advertising;
Matt Carlson5bb09772011-06-13 13:39:00 +000010492 if (tg3_flag(tp, PAUSE_AUTONEG)) {
10493 if (tp->link_config.flowctrl & FLOW_CTRL_RX) {
10494 if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
10495 cmd->advertising |= ADVERTISED_Pause;
10496 } else {
10497 cmd->advertising |= ADVERTISED_Pause |
10498 ADVERTISED_Asym_Pause;
10499 }
10500 } else if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
10501 cmd->advertising |= ADVERTISED_Asym_Pause;
10502 }
10503 }
Matt Carlson859edb22011-12-08 14:40:16 +000010504 if (netif_running(dev) && netif_carrier_ok(dev)) {
David Decotigny70739492011-04-27 18:32:40 +000010505 ethtool_cmd_speed_set(cmd, tp->link_config.active_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010506 cmd->duplex = tp->link_config.active_duplex;
Matt Carlson859edb22011-12-08 14:40:16 +000010507 cmd->lp_advertising = tp->link_config.rmt_adv;
Matt Carlsone348c5e2011-11-21 15:01:20 +000010508 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
10509 if (tp->phy_flags & TG3_PHYFLG_MDIX_STATE)
10510 cmd->eth_tp_mdix = ETH_TP_MDI_X;
10511 else
10512 cmd->eth_tp_mdix = ETH_TP_MDI;
10513 }
Matt Carlson64c22182010-10-14 10:37:44 +000010514 } else {
Matt Carlsone7405222012-02-13 15:20:16 +000010515 ethtool_cmd_speed_set(cmd, SPEED_UNKNOWN);
10516 cmd->duplex = DUPLEX_UNKNOWN;
Matt Carlsone348c5e2011-11-21 15:01:20 +000010517 cmd->eth_tp_mdix = ETH_TP_MDI_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010518 }
Matt Carlson882e9792009-09-01 13:21:36 +000010519 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +000010520 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010521 cmd->autoneg = tp->link_config.autoneg;
10522 cmd->maxtxpkt = 0;
10523 cmd->maxrxpkt = 0;
10524 return 0;
10525}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010526
Linus Torvalds1da177e2005-04-16 15:20:36 -070010527static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
10528{
10529 struct tg3 *tp = netdev_priv(dev);
David Decotigny25db0332011-04-27 18:32:39 +000010530 u32 speed = ethtool_cmd_speed(cmd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010531
Joe Perches63c3a662011-04-26 08:12:10 +000010532 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010533 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010534 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010535 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010536 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10537 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010538 }
10539
Matt Carlson7e5856b2009-02-25 14:23:01 +000010540 if (cmd->autoneg != AUTONEG_ENABLE &&
10541 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -070010542 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +000010543
10544 if (cmd->autoneg == AUTONEG_DISABLE &&
10545 cmd->duplex != DUPLEX_FULL &&
10546 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -070010547 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010548
Matt Carlson7e5856b2009-02-25 14:23:01 +000010549 if (cmd->autoneg == AUTONEG_ENABLE) {
10550 u32 mask = ADVERTISED_Autoneg |
10551 ADVERTISED_Pause |
10552 ADVERTISED_Asym_Pause;
10553
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010554 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010555 mask |= ADVERTISED_1000baseT_Half |
10556 ADVERTISED_1000baseT_Full;
10557
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010558 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010559 mask |= ADVERTISED_100baseT_Half |
10560 ADVERTISED_100baseT_Full |
10561 ADVERTISED_10baseT_Half |
10562 ADVERTISED_10baseT_Full |
10563 ADVERTISED_TP;
10564 else
10565 mask |= ADVERTISED_FIBRE;
10566
10567 if (cmd->advertising & ~mask)
10568 return -EINVAL;
10569
10570 mask &= (ADVERTISED_1000baseT_Half |
10571 ADVERTISED_1000baseT_Full |
10572 ADVERTISED_100baseT_Half |
10573 ADVERTISED_100baseT_Full |
10574 ADVERTISED_10baseT_Half |
10575 ADVERTISED_10baseT_Full);
10576
10577 cmd->advertising &= mask;
10578 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010579 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
David Decotigny25db0332011-04-27 18:32:39 +000010580 if (speed != SPEED_1000)
Matt Carlson7e5856b2009-02-25 14:23:01 +000010581 return -EINVAL;
10582
10583 if (cmd->duplex != DUPLEX_FULL)
10584 return -EINVAL;
10585 } else {
David Decotigny25db0332011-04-27 18:32:39 +000010586 if (speed != SPEED_100 &&
10587 speed != SPEED_10)
Matt Carlson7e5856b2009-02-25 14:23:01 +000010588 return -EINVAL;
10589 }
10590 }
10591
David S. Millerf47c11e2005-06-24 20:18:35 -070010592 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010593
10594 tp->link_config.autoneg = cmd->autoneg;
10595 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -070010596 tp->link_config.advertising = (cmd->advertising |
10597 ADVERTISED_Autoneg);
Matt Carlsone7405222012-02-13 15:20:16 +000010598 tp->link_config.speed = SPEED_UNKNOWN;
10599 tp->link_config.duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010600 } else {
10601 tp->link_config.advertising = 0;
David Decotigny25db0332011-04-27 18:32:39 +000010602 tp->link_config.speed = speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010603 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010604 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010605
Linus Torvalds1da177e2005-04-16 15:20:36 -070010606 if (netif_running(dev))
10607 tg3_setup_phy(tp, 1);
10608
David S. Millerf47c11e2005-06-24 20:18:35 -070010609 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010610
Linus Torvalds1da177e2005-04-16 15:20:36 -070010611 return 0;
10612}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010613
Linus Torvalds1da177e2005-04-16 15:20:36 -070010614static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
10615{
10616 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010617
Rick Jones68aad782011-11-07 13:29:27 +000010618 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
10619 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
10620 strlcpy(info->fw_version, tp->fw_ver, sizeof(info->fw_version));
10621 strlcpy(info->bus_info, pci_name(tp->pdev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010622}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010623
Linus Torvalds1da177e2005-04-16 15:20:36 -070010624static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10625{
10626 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010627
Joe Perches63c3a662011-04-26 08:12:10 +000010628 if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070010629 wol->supported = WAKE_MAGIC;
10630 else
10631 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010632 wol->wolopts = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010633 if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010634 wol->wolopts = WAKE_MAGIC;
10635 memset(&wol->sopass, 0, sizeof(wol->sopass));
10636}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010637
Linus Torvalds1da177e2005-04-16 15:20:36 -070010638static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10639{
10640 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070010641 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010642
Linus Torvalds1da177e2005-04-16 15:20:36 -070010643 if (wol->wolopts & ~WAKE_MAGIC)
10644 return -EINVAL;
10645 if ((wol->wolopts & WAKE_MAGIC) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010646 !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010647 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010648
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010649 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
10650
David S. Millerf47c11e2005-06-24 20:18:35 -070010651 spin_lock_bh(&tp->lock);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010652 if (device_may_wakeup(dp))
Joe Perches63c3a662011-04-26 08:12:10 +000010653 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010654 else
Joe Perches63c3a662011-04-26 08:12:10 +000010655 tg3_flag_clear(tp, WOL_ENABLE);
David S. Millerf47c11e2005-06-24 20:18:35 -070010656 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010657
Linus Torvalds1da177e2005-04-16 15:20:36 -070010658 return 0;
10659}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010660
Linus Torvalds1da177e2005-04-16 15:20:36 -070010661static u32 tg3_get_msglevel(struct net_device *dev)
10662{
10663 struct tg3 *tp = netdev_priv(dev);
10664 return tp->msg_enable;
10665}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010666
Linus Torvalds1da177e2005-04-16 15:20:36 -070010667static void tg3_set_msglevel(struct net_device *dev, u32 value)
10668{
10669 struct tg3 *tp = netdev_priv(dev);
10670 tp->msg_enable = value;
10671}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010672
Linus Torvalds1da177e2005-04-16 15:20:36 -070010673static int tg3_nway_reset(struct net_device *dev)
10674{
10675 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010676 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010677
Linus Torvalds1da177e2005-04-16 15:20:36 -070010678 if (!netif_running(dev))
10679 return -EAGAIN;
10680
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010681 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070010682 return -EINVAL;
10683
Joe Perches63c3a662011-04-26 08:12:10 +000010684 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010685 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010686 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010687 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010688 } else {
10689 u32 bmcr;
10690
10691 spin_lock_bh(&tp->lock);
10692 r = -EINVAL;
10693 tg3_readphy(tp, MII_BMCR, &bmcr);
10694 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
10695 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010696 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010697 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
10698 BMCR_ANENABLE);
10699 r = 0;
10700 }
10701 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010702 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010703
Linus Torvalds1da177e2005-04-16 15:20:36 -070010704 return r;
10705}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010706
Linus Torvalds1da177e2005-04-16 15:20:36 -070010707static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10708{
10709 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010710
Matt Carlson2c49a442010-09-30 10:34:35 +000010711 ering->rx_max_pending = tp->rx_std_ring_mask;
Joe Perches63c3a662011-04-26 08:12:10 +000010712 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson2c49a442010-09-30 10:34:35 +000010713 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080010714 else
10715 ering->rx_jumbo_max_pending = 0;
10716
10717 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010718
10719 ering->rx_pending = tp->rx_pending;
Joe Perches63c3a662011-04-26 08:12:10 +000010720 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Michael Chan4f81c322006-03-20 21:33:42 -080010721 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
10722 else
10723 ering->rx_jumbo_pending = 0;
10724
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010725 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010726}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010727
Linus Torvalds1da177e2005-04-16 15:20:36 -070010728static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10729{
10730 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000010731 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010732
Matt Carlson2c49a442010-09-30 10:34:35 +000010733 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
10734 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070010735 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
10736 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Joe Perches63c3a662011-04-26 08:12:10 +000010737 (tg3_flag(tp, TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070010738 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010739 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010740
Michael Chanbbe832c2005-06-24 20:20:04 -070010741 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010742 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010743 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010744 irq_sync = 1;
10745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010746
Michael Chanbbe832c2005-06-24 20:20:04 -070010747 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010748
Linus Torvalds1da177e2005-04-16 15:20:36 -070010749 tp->rx_pending = ering->rx_pending;
10750
Joe Perches63c3a662011-04-26 08:12:10 +000010751 if (tg3_flag(tp, MAX_RXPEND_64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010752 tp->rx_pending > 63)
10753 tp->rx_pending = 63;
10754 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000010755
Matt Carlson6fd45cb2010-09-15 08:59:57 +000010756 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000010757 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010758
10759 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070010760 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -070010761 err = tg3_restart_hw(tp, 1);
10762 if (!err)
10763 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010764 }
10765
David S. Millerf47c11e2005-06-24 20:18:35 -070010766 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010767
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010768 if (irq_sync && !err)
10769 tg3_phy_start(tp);
10770
Michael Chanb9ec6c12006-07-25 16:37:27 -070010771 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010772}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010773
Linus Torvalds1da177e2005-04-16 15:20:36 -070010774static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10775{
10776 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010777
Joe Perches63c3a662011-04-26 08:12:10 +000010778 epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
Matt Carlson8d018622007-12-20 20:05:44 -080010779
Matt Carlson4a2db502011-12-08 14:40:17 +000010780 if (tp->link_config.flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080010781 epause->rx_pause = 1;
10782 else
10783 epause->rx_pause = 0;
10784
Matt Carlson4a2db502011-12-08 14:40:17 +000010785 if (tp->link_config.flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080010786 epause->tx_pause = 1;
10787 else
10788 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010789}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010790
Linus Torvalds1da177e2005-04-16 15:20:36 -070010791static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10792{
10793 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010794 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010795
Joe Perches63c3a662011-04-26 08:12:10 +000010796 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson27121682010-02-17 15:16:57 +000010797 u32 newadv;
10798 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010799
Matt Carlson27121682010-02-17 15:16:57 +000010800 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010801
Matt Carlson27121682010-02-17 15:16:57 +000010802 if (!(phydev->supported & SUPPORTED_Pause) ||
10803 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000010804 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000010805 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010806
Matt Carlson27121682010-02-17 15:16:57 +000010807 tp->link_config.flowctrl = 0;
10808 if (epause->rx_pause) {
10809 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010810
Matt Carlson27121682010-02-17 15:16:57 +000010811 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080010812 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000010813 newadv = ADVERTISED_Pause;
10814 } else
10815 newadv = ADVERTISED_Pause |
10816 ADVERTISED_Asym_Pause;
10817 } else if (epause->tx_pause) {
10818 tp->link_config.flowctrl |= FLOW_CTRL_TX;
10819 newadv = ADVERTISED_Asym_Pause;
10820 } else
10821 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010822
Matt Carlson27121682010-02-17 15:16:57 +000010823 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010824 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010825 else
Joe Perches63c3a662011-04-26 08:12:10 +000010826 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010827
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010828 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000010829 u32 oldadv = phydev->advertising &
10830 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
10831 if (oldadv != newadv) {
10832 phydev->advertising &=
10833 ~(ADVERTISED_Pause |
10834 ADVERTISED_Asym_Pause);
10835 phydev->advertising |= newadv;
10836 if (phydev->autoneg) {
10837 /*
10838 * Always renegotiate the link to
10839 * inform our link partner of our
10840 * flow control settings, even if the
10841 * flow control is forced. Let
10842 * tg3_adjust_link() do the final
10843 * flow control setup.
10844 */
10845 return phy_start_aneg(phydev);
10846 }
10847 }
10848
10849 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010850 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000010851 } else {
Matt Carlsonc6700ce2012-02-13 15:20:15 +000010852 tp->link_config.advertising &=
Matt Carlson27121682010-02-17 15:16:57 +000010853 ~(ADVERTISED_Pause |
10854 ADVERTISED_Asym_Pause);
Matt Carlsonc6700ce2012-02-13 15:20:15 +000010855 tp->link_config.advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010856 }
10857 } else {
10858 int irq_sync = 0;
10859
10860 if (netif_running(dev)) {
10861 tg3_netif_stop(tp);
10862 irq_sync = 1;
10863 }
10864
10865 tg3_full_lock(tp, irq_sync);
10866
10867 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010868 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010869 else
Joe Perches63c3a662011-04-26 08:12:10 +000010870 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010871 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010872 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010873 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010874 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010875 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010876 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010877 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010878 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010879
10880 if (netif_running(dev)) {
10881 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10882 err = tg3_restart_hw(tp, 1);
10883 if (!err)
10884 tg3_netif_start(tp);
10885 }
10886
10887 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010889
Michael Chanb9ec6c12006-07-25 16:37:27 -070010890 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010891}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010892
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010893static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010894{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010895 switch (sset) {
10896 case ETH_SS_TEST:
10897 return TG3_NUM_TEST;
10898 case ETH_SS_STATS:
10899 return TG3_NUM_STATS;
10900 default:
10901 return -EOPNOTSUPP;
10902 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070010903}
10904
Matt Carlson90415472011-12-16 13:33:23 +000010905static int tg3_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
10906 u32 *rules __always_unused)
10907{
10908 struct tg3 *tp = netdev_priv(dev);
10909
10910 if (!tg3_flag(tp, SUPPORT_MSIX))
10911 return -EOPNOTSUPP;
10912
10913 switch (info->cmd) {
10914 case ETHTOOL_GRXRINGS:
10915 if (netif_running(tp->dev))
10916 info->data = tp->irq_cnt;
10917 else {
10918 info->data = num_online_cpus();
10919 if (info->data > TG3_IRQ_MAX_VECS_RSS)
10920 info->data = TG3_IRQ_MAX_VECS_RSS;
10921 }
10922
10923 /* The first interrupt vector only
10924 * handles link interrupts.
10925 */
10926 info->data -= 1;
10927 return 0;
10928
10929 default:
10930 return -EOPNOTSUPP;
10931 }
10932}
10933
10934static u32 tg3_get_rxfh_indir_size(struct net_device *dev)
10935{
10936 u32 size = 0;
10937 struct tg3 *tp = netdev_priv(dev);
10938
10939 if (tg3_flag(tp, SUPPORT_MSIX))
10940 size = TG3_RSS_INDIR_TBL_SIZE;
10941
10942 return size;
10943}
10944
10945static int tg3_get_rxfh_indir(struct net_device *dev, u32 *indir)
10946{
10947 struct tg3 *tp = netdev_priv(dev);
10948 int i;
10949
10950 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
10951 indir[i] = tp->rss_ind_tbl[i];
10952
10953 return 0;
10954}
10955
10956static int tg3_set_rxfh_indir(struct net_device *dev, const u32 *indir)
10957{
10958 struct tg3 *tp = netdev_priv(dev);
10959 size_t i;
10960
10961 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
10962 tp->rss_ind_tbl[i] = indir[i];
10963
10964 if (!netif_running(dev) || !tg3_flag(tp, ENABLE_RSS))
10965 return 0;
10966
10967 /* It is legal to write the indirection
10968 * table while the device is running.
10969 */
10970 tg3_full_lock(tp, 0);
10971 tg3_rss_write_indir_tbl(tp);
10972 tg3_full_unlock(tp);
10973
10974 return 0;
10975}
10976
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010977static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010978{
10979 switch (stringset) {
10980 case ETH_SS_STATS:
10981 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
10982 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070010983 case ETH_SS_TEST:
10984 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
10985 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010986 default:
10987 WARN_ON(1); /* we need a WARN() */
10988 break;
10989 }
10990}
10991
stephen hemminger81b87092011-04-04 08:43:50 +000010992static int tg3_set_phys_id(struct net_device *dev,
10993 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070010994{
10995 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070010996
10997 if (!netif_running(tp->dev))
10998 return -EAGAIN;
10999
stephen hemminger81b87092011-04-04 08:43:50 +000011000 switch (state) {
11001 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +000011002 return 1; /* cycle on/off once per second */
Michael Chan4009a932005-09-05 17:52:54 -070011003
stephen hemminger81b87092011-04-04 08:43:50 +000011004 case ETHTOOL_ID_ON:
11005 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
11006 LED_CTRL_1000MBPS_ON |
11007 LED_CTRL_100MBPS_ON |
11008 LED_CTRL_10MBPS_ON |
11009 LED_CTRL_TRAFFIC_OVERRIDE |
11010 LED_CTRL_TRAFFIC_BLINK |
11011 LED_CTRL_TRAFFIC_LED);
11012 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011013
stephen hemminger81b87092011-04-04 08:43:50 +000011014 case ETHTOOL_ID_OFF:
11015 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
11016 LED_CTRL_TRAFFIC_OVERRIDE);
11017 break;
Michael Chan4009a932005-09-05 17:52:54 -070011018
stephen hemminger81b87092011-04-04 08:43:50 +000011019 case ETHTOOL_ID_INACTIVE:
11020 tw32(MAC_LED_CTRL, tp->led_ctrl);
11021 break;
Michael Chan4009a932005-09-05 17:52:54 -070011022 }
stephen hemminger81b87092011-04-04 08:43:50 +000011023
Michael Chan4009a932005-09-05 17:52:54 -070011024 return 0;
11025}
11026
Matt Carlsonde6f31e2010-04-12 06:58:30 +000011027static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011028 struct ethtool_stats *estats, u64 *tmp_stats)
11029{
11030 struct tg3 *tp = netdev_priv(dev);
Matt Carlson0e6c9da2011-12-08 14:40:13 +000011031
Matt Carlsonb546e462012-02-13 15:20:09 +000011032 if (tp->hw_stats)
11033 tg3_get_estats(tp, (struct tg3_ethtool_stats *)tmp_stats);
11034 else
11035 memset(tmp_stats, 0, sizeof(struct tg3_ethtool_stats));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011036}
11037
Matt Carlson535a4902011-07-20 10:20:56 +000011038static __be32 *tg3_vpd_readblock(struct tg3 *tp, u32 *vpdlen)
Matt Carlsonc3e94502011-04-13 11:05:08 +000011039{
11040 int i;
11041 __be32 *buf;
11042 u32 offset = 0, len = 0;
11043 u32 magic, val;
11044
Joe Perches63c3a662011-04-26 08:12:10 +000011045 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
Matt Carlsonc3e94502011-04-13 11:05:08 +000011046 return NULL;
11047
11048 if (magic == TG3_EEPROM_MAGIC) {
11049 for (offset = TG3_NVM_DIR_START;
11050 offset < TG3_NVM_DIR_END;
11051 offset += TG3_NVM_DIRENT_SIZE) {
11052 if (tg3_nvram_read(tp, offset, &val))
11053 return NULL;
11054
11055 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
11056 TG3_NVM_DIRTYPE_EXTVPD)
11057 break;
11058 }
11059
11060 if (offset != TG3_NVM_DIR_END) {
11061 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
11062 if (tg3_nvram_read(tp, offset + 4, &offset))
11063 return NULL;
11064
11065 offset = tg3_nvram_logical_addr(tp, offset);
11066 }
11067 }
11068
11069 if (!offset || !len) {
11070 offset = TG3_NVM_VPD_OFF;
11071 len = TG3_NVM_VPD_LEN;
11072 }
11073
11074 buf = kmalloc(len, GFP_KERNEL);
11075 if (buf == NULL)
11076 return NULL;
11077
11078 if (magic == TG3_EEPROM_MAGIC) {
11079 for (i = 0; i < len; i += 4) {
11080 /* The data is in little-endian format in NVRAM.
11081 * Use the big-endian read routines to preserve
11082 * the byte order as it exists in NVRAM.
11083 */
11084 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
11085 goto error;
11086 }
11087 } else {
11088 u8 *ptr;
11089 ssize_t cnt;
11090 unsigned int pos = 0;
11091
11092 ptr = (u8 *)&buf[0];
11093 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
11094 cnt = pci_read_vpd(tp->pdev, pos,
11095 len - pos, ptr);
11096 if (cnt == -ETIMEDOUT || cnt == -EINTR)
11097 cnt = 0;
11098 else if (cnt < 0)
11099 goto error;
11100 }
11101 if (pos != len)
11102 goto error;
11103 }
11104
Matt Carlson535a4902011-07-20 10:20:56 +000011105 *vpdlen = len;
11106
Matt Carlsonc3e94502011-04-13 11:05:08 +000011107 return buf;
11108
11109error:
11110 kfree(buf);
11111 return NULL;
11112}
11113
Michael Chan566f86a2005-05-29 14:56:58 -070011114#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080011115#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
11116#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
11117#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Matt Carlson727a6d92011-06-13 13:38:58 +000011118#define NVRAM_SELFBOOT_FORMAT1_4_SIZE 0x20
11119#define NVRAM_SELFBOOT_FORMAT1_5_SIZE 0x24
Matt Carlsonbda18fa2011-07-20 10:20:57 +000011120#define NVRAM_SELFBOOT_FORMAT1_6_SIZE 0x50
Michael Chanb16250e2006-09-27 16:10:14 -070011121#define NVRAM_SELFBOOT_HW_SIZE 0x20
11122#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070011123
11124static int tg3_test_nvram(struct tg3 *tp)
11125{
Matt Carlson535a4902011-07-20 10:20:56 +000011126 u32 csum, magic, len;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011127 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010011128 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070011129
Joe Perches63c3a662011-04-26 08:12:10 +000011130 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000011131 return 0;
11132
Matt Carlsone4f34112009-02-25 14:25:00 +000011133 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080011134 return -EIO;
11135
Michael Chan1b277772006-03-20 22:27:48 -080011136 if (magic == TG3_EEPROM_MAGIC)
11137 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070011138 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080011139 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
11140 TG3_EEPROM_SB_FORMAT_1) {
11141 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
11142 case TG3_EEPROM_SB_REVISION_0:
11143 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
11144 break;
11145 case TG3_EEPROM_SB_REVISION_2:
11146 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
11147 break;
11148 case TG3_EEPROM_SB_REVISION_3:
11149 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
11150 break;
Matt Carlson727a6d92011-06-13 13:38:58 +000011151 case TG3_EEPROM_SB_REVISION_4:
11152 size = NVRAM_SELFBOOT_FORMAT1_4_SIZE;
11153 break;
11154 case TG3_EEPROM_SB_REVISION_5:
11155 size = NVRAM_SELFBOOT_FORMAT1_5_SIZE;
11156 break;
11157 case TG3_EEPROM_SB_REVISION_6:
11158 size = NVRAM_SELFBOOT_FORMAT1_6_SIZE;
11159 break;
Matt Carlsona5767de2007-11-12 21:10:58 -080011160 default:
Matt Carlson727a6d92011-06-13 13:38:58 +000011161 return -EIO;
Matt Carlsona5767de2007-11-12 21:10:58 -080011162 }
11163 } else
Michael Chan1b277772006-03-20 22:27:48 -080011164 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070011165 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
11166 size = NVRAM_SELFBOOT_HW_SIZE;
11167 else
Michael Chan1b277772006-03-20 22:27:48 -080011168 return -EIO;
11169
11170 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070011171 if (buf == NULL)
11172 return -ENOMEM;
11173
Michael Chan1b277772006-03-20 22:27:48 -080011174 err = -EIO;
11175 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000011176 err = tg3_nvram_read_be32(tp, i, &buf[j]);
11177 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070011178 break;
Michael Chan566f86a2005-05-29 14:56:58 -070011179 }
Michael Chan1b277772006-03-20 22:27:48 -080011180 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070011181 goto out;
11182
Michael Chan1b277772006-03-20 22:27:48 -080011183 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000011184 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080011185 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070011186 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080011187 u8 *buf8 = (u8 *) buf, csum8 = 0;
11188
Al Virob9fc7dc2007-12-17 22:59:57 -080011189 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080011190 TG3_EEPROM_SB_REVISION_2) {
11191 /* For rev 2, the csum doesn't include the MBA. */
11192 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
11193 csum8 += buf8[i];
11194 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
11195 csum8 += buf8[i];
11196 } else {
11197 for (i = 0; i < size; i++)
11198 csum8 += buf8[i];
11199 }
Michael Chan1b277772006-03-20 22:27:48 -080011200
Adrian Bunkad96b482006-04-05 22:21:04 -070011201 if (csum8 == 0) {
11202 err = 0;
11203 goto out;
11204 }
11205
11206 err = -EIO;
11207 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080011208 }
Michael Chan566f86a2005-05-29 14:56:58 -070011209
Al Virob9fc7dc2007-12-17 22:59:57 -080011210 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070011211 TG3_EEPROM_MAGIC_HW) {
11212 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000011213 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070011214 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070011215
11216 /* Separate the parity bits and the data bytes. */
11217 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
11218 if ((i == 0) || (i == 8)) {
11219 int l;
11220 u8 msk;
11221
11222 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
11223 parity[k++] = buf8[i] & msk;
11224 i++;
Matt Carlson859a588792010-04-05 10:19:28 +000011225 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070011226 int l;
11227 u8 msk;
11228
11229 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
11230 parity[k++] = buf8[i] & msk;
11231 i++;
11232
11233 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
11234 parity[k++] = buf8[i] & msk;
11235 i++;
11236 }
11237 data[j++] = buf8[i];
11238 }
11239
11240 err = -EIO;
11241 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
11242 u8 hw8 = hweight8(data[i]);
11243
11244 if ((hw8 & 0x1) && parity[i])
11245 goto out;
11246 else if (!(hw8 & 0x1) && !parity[i])
11247 goto out;
11248 }
11249 err = 0;
11250 goto out;
11251 }
11252
Matt Carlson01c3a392011-03-09 16:58:20 +000011253 err = -EIO;
11254
Michael Chan566f86a2005-05-29 14:56:58 -070011255 /* Bootstrap checksum at offset 0x10 */
11256 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000011257 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070011258 goto out;
11259
11260 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
11261 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000011262 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000011263 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070011264
Matt Carlsonc3e94502011-04-13 11:05:08 +000011265 kfree(buf);
11266
Matt Carlson535a4902011-07-20 10:20:56 +000011267 buf = tg3_vpd_readblock(tp, &len);
Matt Carlsonc3e94502011-04-13 11:05:08 +000011268 if (!buf)
11269 return -ENOMEM;
Matt Carlsond4894f32011-03-09 16:58:21 +000011270
Matt Carlson535a4902011-07-20 10:20:56 +000011271 i = pci_vpd_find_tag((u8 *)buf, 0, len, PCI_VPD_LRDT_RO_DATA);
Matt Carlsond4894f32011-03-09 16:58:21 +000011272 if (i > 0) {
11273 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
11274 if (j < 0)
11275 goto out;
11276
Matt Carlson535a4902011-07-20 10:20:56 +000011277 if (i + PCI_VPD_LRDT_TAG_SIZE + j > len)
Matt Carlsond4894f32011-03-09 16:58:21 +000011278 goto out;
11279
11280 i += PCI_VPD_LRDT_TAG_SIZE;
11281 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
11282 PCI_VPD_RO_KEYWORD_CHKSUM);
11283 if (j > 0) {
11284 u8 csum8 = 0;
11285
11286 j += PCI_VPD_INFO_FLD_HDR_SIZE;
11287
11288 for (i = 0; i <= j; i++)
11289 csum8 += ((u8 *)buf)[i];
11290
11291 if (csum8)
11292 goto out;
11293 }
11294 }
11295
Michael Chan566f86a2005-05-29 14:56:58 -070011296 err = 0;
11297
11298out:
11299 kfree(buf);
11300 return err;
11301}
11302
Michael Chanca430072005-05-29 14:57:23 -070011303#define TG3_SERDES_TIMEOUT_SEC 2
11304#define TG3_COPPER_TIMEOUT_SEC 6
11305
11306static int tg3_test_link(struct tg3 *tp)
11307{
11308 int i, max;
11309
11310 if (!netif_running(tp->dev))
11311 return -ENODEV;
11312
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011313 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070011314 max = TG3_SERDES_TIMEOUT_SEC;
11315 else
11316 max = TG3_COPPER_TIMEOUT_SEC;
11317
11318 for (i = 0; i < max; i++) {
11319 if (netif_carrier_ok(tp->dev))
11320 return 0;
11321
11322 if (msleep_interruptible(1000))
11323 break;
11324 }
11325
11326 return -EIO;
11327}
11328
Michael Chana71116d2005-05-29 14:58:11 -070011329/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080011330static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070011331{
Michael Chanb16250e2006-09-27 16:10:14 -070011332 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070011333 u32 offset, read_mask, write_mask, val, save_val, read_val;
11334 static struct {
11335 u16 offset;
11336 u16 flags;
11337#define TG3_FL_5705 0x1
11338#define TG3_FL_NOT_5705 0x2
11339#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070011340#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070011341 u32 read_mask;
11342 u32 write_mask;
11343 } reg_tbl[] = {
11344 /* MAC Control Registers */
11345 { MAC_MODE, TG3_FL_NOT_5705,
11346 0x00000000, 0x00ef6f8c },
11347 { MAC_MODE, TG3_FL_5705,
11348 0x00000000, 0x01ef6b8c },
11349 { MAC_STATUS, TG3_FL_NOT_5705,
11350 0x03800107, 0x00000000 },
11351 { MAC_STATUS, TG3_FL_5705,
11352 0x03800100, 0x00000000 },
11353 { MAC_ADDR_0_HIGH, 0x0000,
11354 0x00000000, 0x0000ffff },
11355 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011356 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070011357 { MAC_RX_MTU_SIZE, 0x0000,
11358 0x00000000, 0x0000ffff },
11359 { MAC_TX_MODE, 0x0000,
11360 0x00000000, 0x00000070 },
11361 { MAC_TX_LENGTHS, 0x0000,
11362 0x00000000, 0x00003fff },
11363 { MAC_RX_MODE, TG3_FL_NOT_5705,
11364 0x00000000, 0x000007fc },
11365 { MAC_RX_MODE, TG3_FL_5705,
11366 0x00000000, 0x000007dc },
11367 { MAC_HASH_REG_0, 0x0000,
11368 0x00000000, 0xffffffff },
11369 { MAC_HASH_REG_1, 0x0000,
11370 0x00000000, 0xffffffff },
11371 { MAC_HASH_REG_2, 0x0000,
11372 0x00000000, 0xffffffff },
11373 { MAC_HASH_REG_3, 0x0000,
11374 0x00000000, 0xffffffff },
11375
11376 /* Receive Data and Receive BD Initiator Control Registers. */
11377 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
11378 0x00000000, 0xffffffff },
11379 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
11380 0x00000000, 0xffffffff },
11381 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
11382 0x00000000, 0x00000003 },
11383 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
11384 0x00000000, 0xffffffff },
11385 { RCVDBDI_STD_BD+0, 0x0000,
11386 0x00000000, 0xffffffff },
11387 { RCVDBDI_STD_BD+4, 0x0000,
11388 0x00000000, 0xffffffff },
11389 { RCVDBDI_STD_BD+8, 0x0000,
11390 0x00000000, 0xffff0002 },
11391 { RCVDBDI_STD_BD+0xc, 0x0000,
11392 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011393
Michael Chana71116d2005-05-29 14:58:11 -070011394 /* Receive BD Initiator Control Registers. */
11395 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
11396 0x00000000, 0xffffffff },
11397 { RCVBDI_STD_THRESH, TG3_FL_5705,
11398 0x00000000, 0x000003ff },
11399 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
11400 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011401
Michael Chana71116d2005-05-29 14:58:11 -070011402 /* Host Coalescing Control Registers. */
11403 { HOSTCC_MODE, TG3_FL_NOT_5705,
11404 0x00000000, 0x00000004 },
11405 { HOSTCC_MODE, TG3_FL_5705,
11406 0x00000000, 0x000000f6 },
11407 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
11408 0x00000000, 0xffffffff },
11409 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
11410 0x00000000, 0x000003ff },
11411 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
11412 0x00000000, 0xffffffff },
11413 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
11414 0x00000000, 0x000003ff },
11415 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
11416 0x00000000, 0xffffffff },
11417 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
11418 0x00000000, 0x000000ff },
11419 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
11420 0x00000000, 0xffffffff },
11421 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
11422 0x00000000, 0x000000ff },
11423 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
11424 0x00000000, 0xffffffff },
11425 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
11426 0x00000000, 0xffffffff },
11427 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
11428 0x00000000, 0xffffffff },
11429 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
11430 0x00000000, 0x000000ff },
11431 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
11432 0x00000000, 0xffffffff },
11433 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
11434 0x00000000, 0x000000ff },
11435 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
11436 0x00000000, 0xffffffff },
11437 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
11438 0x00000000, 0xffffffff },
11439 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
11440 0x00000000, 0xffffffff },
11441 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
11442 0x00000000, 0xffffffff },
11443 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
11444 0x00000000, 0xffffffff },
11445 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
11446 0xffffffff, 0x00000000 },
11447 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
11448 0xffffffff, 0x00000000 },
11449
11450 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070011451 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070011452 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070011453 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070011454 0x00000000, 0x007fffff },
11455 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
11456 0x00000000, 0x0000003f },
11457 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
11458 0x00000000, 0x000001ff },
11459 { BUFMGR_MB_HIGH_WATER, 0x0000,
11460 0x00000000, 0x000001ff },
11461 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
11462 0xffffffff, 0x00000000 },
11463 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
11464 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011465
Michael Chana71116d2005-05-29 14:58:11 -070011466 /* Mailbox Registers */
11467 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
11468 0x00000000, 0x000001ff },
11469 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
11470 0x00000000, 0x000001ff },
11471 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
11472 0x00000000, 0x000007ff },
11473 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
11474 0x00000000, 0x000001ff },
11475
11476 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
11477 };
11478
Michael Chanb16250e2006-09-27 16:10:14 -070011479 is_5705 = is_5750 = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000011480 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chana71116d2005-05-29 14:58:11 -070011481 is_5705 = 1;
Joe Perches63c3a662011-04-26 08:12:10 +000011482 if (tg3_flag(tp, 5750_PLUS))
Michael Chanb16250e2006-09-27 16:10:14 -070011483 is_5750 = 1;
11484 }
Michael Chana71116d2005-05-29 14:58:11 -070011485
11486 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
11487 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
11488 continue;
11489
11490 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
11491 continue;
11492
Joe Perches63c3a662011-04-26 08:12:10 +000011493 if (tg3_flag(tp, IS_5788) &&
Michael Chana71116d2005-05-29 14:58:11 -070011494 (reg_tbl[i].flags & TG3_FL_NOT_5788))
11495 continue;
11496
Michael Chanb16250e2006-09-27 16:10:14 -070011497 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
11498 continue;
11499
Michael Chana71116d2005-05-29 14:58:11 -070011500 offset = (u32) reg_tbl[i].offset;
11501 read_mask = reg_tbl[i].read_mask;
11502 write_mask = reg_tbl[i].write_mask;
11503
11504 /* Save the original register content */
11505 save_val = tr32(offset);
11506
11507 /* Determine the read-only value. */
11508 read_val = save_val & read_mask;
11509
11510 /* Write zero to the register, then make sure the read-only bits
11511 * are not changed and the read/write bits are all zeros.
11512 */
11513 tw32(offset, 0);
11514
11515 val = tr32(offset);
11516
11517 /* Test the read-only and read/write bits. */
11518 if (((val & read_mask) != read_val) || (val & write_mask))
11519 goto out;
11520
11521 /* Write ones to all the bits defined by RdMask and WrMask, then
11522 * make sure the read-only bits are not changed and the
11523 * read/write bits are all ones.
11524 */
11525 tw32(offset, read_mask | write_mask);
11526
11527 val = tr32(offset);
11528
11529 /* Test the read-only bits. */
11530 if ((val & read_mask) != read_val)
11531 goto out;
11532
11533 /* Test the read/write bits. */
11534 if ((val & write_mask) != write_mask)
11535 goto out;
11536
11537 tw32(offset, save_val);
11538 }
11539
11540 return 0;
11541
11542out:
Michael Chan9f88f292006-12-07 00:22:54 -080011543 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000011544 netdev_err(tp->dev,
11545 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070011546 tw32(offset, save_val);
11547 return -EIO;
11548}
11549
Michael Chan7942e1d2005-05-29 14:58:36 -070011550static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
11551{
Arjan van de Venf71e1302006-03-03 21:33:57 -050011552 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070011553 int i;
11554 u32 j;
11555
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020011556 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070011557 for (j = 0; j < len; j += 4) {
11558 u32 val;
11559
11560 tg3_write_mem(tp, offset + j, test_pattern[i]);
11561 tg3_read_mem(tp, offset + j, &val);
11562 if (val != test_pattern[i])
11563 return -EIO;
11564 }
11565 }
11566 return 0;
11567}
11568
11569static int tg3_test_memory(struct tg3 *tp)
11570{
11571 static struct mem_entry {
11572 u32 offset;
11573 u32 len;
11574 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080011575 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070011576 { 0x00002000, 0x1c000},
11577 { 0xffffffff, 0x00000}
11578 }, mem_tbl_5705[] = {
11579 { 0x00000100, 0x0000c},
11580 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070011581 { 0x00004000, 0x00800},
11582 { 0x00006000, 0x01000},
11583 { 0x00008000, 0x02000},
11584 { 0x00010000, 0x0e000},
11585 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080011586 }, mem_tbl_5755[] = {
11587 { 0x00000200, 0x00008},
11588 { 0x00004000, 0x00800},
11589 { 0x00006000, 0x00800},
11590 { 0x00008000, 0x02000},
11591 { 0x00010000, 0x0c000},
11592 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070011593 }, mem_tbl_5906[] = {
11594 { 0x00000200, 0x00008},
11595 { 0x00004000, 0x00400},
11596 { 0x00006000, 0x00400},
11597 { 0x00008000, 0x01000},
11598 { 0x00010000, 0x01000},
11599 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011600 }, mem_tbl_5717[] = {
11601 { 0x00000200, 0x00008},
11602 { 0x00010000, 0x0a000},
11603 { 0x00020000, 0x13c00},
11604 { 0xffffffff, 0x00000}
11605 }, mem_tbl_57765[] = {
11606 { 0x00000200, 0x00008},
11607 { 0x00004000, 0x00800},
11608 { 0x00006000, 0x09800},
11609 { 0x00010000, 0x0a000},
11610 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070011611 };
11612 struct mem_entry *mem_tbl;
11613 int err = 0;
11614 int i;
11615
Joe Perches63c3a662011-04-26 08:12:10 +000011616 if (tg3_flag(tp, 5717_PLUS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011617 mem_tbl = mem_tbl_5717;
Matt Carlson55086ad2011-12-14 11:09:59 +000011618 else if (tg3_flag(tp, 57765_CLASS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011619 mem_tbl = mem_tbl_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000011620 else if (tg3_flag(tp, 5755_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080011621 mem_tbl = mem_tbl_5755;
11622 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
11623 mem_tbl = mem_tbl_5906;
Joe Perches63c3a662011-04-26 08:12:10 +000011624 else if (tg3_flag(tp, 5705_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080011625 mem_tbl = mem_tbl_5705;
11626 else
Michael Chan7942e1d2005-05-29 14:58:36 -070011627 mem_tbl = mem_tbl_570x;
11628
11629 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000011630 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
11631 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070011632 break;
11633 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011634
Michael Chan7942e1d2005-05-29 14:58:36 -070011635 return err;
11636}
11637
Matt Carlsonbb158d62011-04-25 12:42:47 +000011638#define TG3_TSO_MSS 500
11639
11640#define TG3_TSO_IP_HDR_LEN 20
11641#define TG3_TSO_TCP_HDR_LEN 20
11642#define TG3_TSO_TCP_OPT_LEN 12
11643
11644static const u8 tg3_tso_header[] = {
116450x08, 0x00,
116460x45, 0x00, 0x00, 0x00,
116470x00, 0x00, 0x40, 0x00,
116480x40, 0x06, 0x00, 0x00,
116490x0a, 0x00, 0x00, 0x01,
116500x0a, 0x00, 0x00, 0x02,
116510x0d, 0x00, 0xe0, 0x00,
116520x00, 0x00, 0x01, 0x00,
116530x00, 0x00, 0x02, 0x00,
116540x80, 0x10, 0x10, 0x00,
116550x14, 0x09, 0x00, 0x00,
116560x01, 0x01, 0x08, 0x0a,
116570x11, 0x11, 0x11, 0x11,
116580x11, 0x11, 0x11, 0x11,
11659};
Michael Chan9f40dea2005-09-05 17:53:06 -070011660
Matt Carlson28a45952011-08-19 13:58:22 +000011661static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, bool tso_loopback)
Michael Chanc76949a2005-05-29 14:58:59 -070011662{
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011663 u32 rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011664 u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
Matt Carlson84b67b22011-07-27 14:20:52 +000011665 u32 budget;
Eric Dumazet9205fd92011-11-18 06:47:01 +000011666 struct sk_buff *skb;
11667 u8 *tx_data, *rx_data;
Michael Chanc76949a2005-05-29 14:58:59 -070011668 dma_addr_t map;
11669 int num_pkts, tx_len, rx_len, i, err;
11670 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000011671 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000011672 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070011673
Matt Carlsonc8873402010-02-12 14:47:11 +000011674 tnapi = &tp->napi[0];
11675 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011676 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +000011677 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlson1da85aa2010-09-30 10:34:34 +000011678 rnapi = &tp->napi[1];
Joe Perches63c3a662011-04-26 08:12:10 +000011679 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc8873402010-02-12 14:47:11 +000011680 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011681 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011682 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000011683
Michael Chanc76949a2005-05-29 14:58:59 -070011684 err = -EIO;
11685
Matt Carlson4852a862011-04-13 11:05:07 +000011686 tx_len = pktsz;
David S. Millera20e9c62006-07-31 22:38:16 -070011687 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070011688 if (!skb)
11689 return -ENOMEM;
11690
Michael Chanc76949a2005-05-29 14:58:59 -070011691 tx_data = skb_put(skb, tx_len);
11692 memcpy(tx_data, tp->dev->dev_addr, 6);
11693 memset(tx_data + 6, 0x0, 8);
11694
Matt Carlson4852a862011-04-13 11:05:07 +000011695 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
Michael Chanc76949a2005-05-29 14:58:59 -070011696
Matt Carlson28a45952011-08-19 13:58:22 +000011697 if (tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011698 struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
11699
11700 u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
11701 TG3_TSO_TCP_OPT_LEN;
11702
11703 memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
11704 sizeof(tg3_tso_header));
11705 mss = TG3_TSO_MSS;
11706
11707 val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
11708 num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
11709
11710 /* Set the total length field in the IP header */
11711 iph->tot_len = htons((u16)(mss + hdr_len));
11712
11713 base_flags = (TXD_FLAG_CPU_PRE_DMA |
11714 TXD_FLAG_CPU_POST_DMA);
11715
Joe Perches63c3a662011-04-26 08:12:10 +000011716 if (tg3_flag(tp, HW_TSO_1) ||
11717 tg3_flag(tp, HW_TSO_2) ||
11718 tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011719 struct tcphdr *th;
11720 val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
11721 th = (struct tcphdr *)&tx_data[val];
11722 th->check = 0;
11723 } else
11724 base_flags |= TXD_FLAG_TCPUDP_CSUM;
11725
Joe Perches63c3a662011-04-26 08:12:10 +000011726 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011727 mss |= (hdr_len & 0xc) << 12;
11728 if (hdr_len & 0x10)
11729 base_flags |= 0x00000010;
11730 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +000011731 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlsonbb158d62011-04-25 12:42:47 +000011732 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +000011733 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlsonbb158d62011-04-25 12:42:47 +000011734 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
11735 mss |= (TG3_TSO_TCP_OPT_LEN << 9);
11736 } else {
11737 base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
11738 }
11739
11740 data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
11741 } else {
11742 num_pkts = 1;
11743 data_off = ETH_HLEN;
11744 }
11745
11746 for (i = data_off; i < tx_len; i++)
Michael Chanc76949a2005-05-29 14:58:59 -070011747 tx_data[i] = (u8) (i & 0xff);
11748
Alexander Duyckf4188d82009-12-02 16:48:38 +000011749 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
11750 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000011751 dev_kfree_skb(skb);
11752 return -EIO;
11753 }
Michael Chanc76949a2005-05-29 14:58:59 -070011754
Matt Carlson0d681b22011-07-27 14:20:49 +000011755 val = tnapi->tx_prod;
11756 tnapi->tx_buffers[val].skb = skb;
11757 dma_unmap_addr_set(&tnapi->tx_buffers[val], mapping, map);
11758
Michael Chanc76949a2005-05-29 14:58:59 -070011759 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011760 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011761
11762 udelay(10);
11763
Matt Carlson898a56f2009-08-28 14:02:40 +000011764 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070011765
Matt Carlson84b67b22011-07-27 14:20:52 +000011766 budget = tg3_tx_avail(tnapi);
11767 if (tg3_tx_frag_set(tnapi, &val, &budget, map, tx_len,
Matt Carlsond1a3b732011-07-27 14:20:51 +000011768 base_flags | TXD_FLAG_END, mss, 0)) {
11769 tnapi->tx_buffers[val].skb = NULL;
11770 dev_kfree_skb(skb);
11771 return -EIO;
11772 }
Michael Chanc76949a2005-05-29 14:58:59 -070011773
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011774 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070011775
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011776 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
11777 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070011778
11779 udelay(10);
11780
Matt Carlson303fc922009-11-02 14:27:34 +000011781 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
11782 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070011783 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011784 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011785
11786 udelay(10);
11787
Matt Carlson898a56f2009-08-28 14:02:40 +000011788 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
11789 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011790 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070011791 (rx_idx == (rx_start_idx + num_pkts)))
11792 break;
11793 }
11794
Matt Carlsonba1142e2011-11-04 09:15:00 +000011795 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod - 1, -1);
Michael Chanc76949a2005-05-29 14:58:59 -070011796 dev_kfree_skb(skb);
11797
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011798 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070011799 goto out;
11800
11801 if (rx_idx != rx_start_idx + num_pkts)
11802 goto out;
11803
Matt Carlsonbb158d62011-04-25 12:42:47 +000011804 val = data_off;
11805 while (rx_idx != rx_start_idx) {
11806 desc = &rnapi->rx_rcb[rx_start_idx++];
11807 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
11808 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
Michael Chanc76949a2005-05-29 14:58:59 -070011809
Matt Carlsonbb158d62011-04-25 12:42:47 +000011810 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
11811 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
Matt Carlson4852a862011-04-13 11:05:07 +000011812 goto out;
Michael Chanc76949a2005-05-29 14:58:59 -070011813
Matt Carlsonbb158d62011-04-25 12:42:47 +000011814 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
11815 - ETH_FCS_LEN;
11816
Matt Carlson28a45952011-08-19 13:58:22 +000011817 if (!tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011818 if (rx_len != tx_len)
11819 goto out;
11820
11821 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
11822 if (opaque_key != RXD_OPAQUE_RING_STD)
11823 goto out;
11824 } else {
11825 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
11826 goto out;
11827 }
11828 } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
11829 (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
Matt Carlson54e0a672011-05-19 12:12:50 +000011830 >> RXD_TCPCSUM_SHIFT != 0xffff) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011831 goto out;
11832 }
11833
11834 if (opaque_key == RXD_OPAQUE_RING_STD) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000011835 rx_data = tpr->rx_std_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011836 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
11837 mapping);
11838 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000011839 rx_data = tpr->rx_jmb_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011840 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
11841 mapping);
11842 } else
Matt Carlson4852a862011-04-13 11:05:07 +000011843 goto out;
11844
Matt Carlsonbb158d62011-04-25 12:42:47 +000011845 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
11846 PCI_DMA_FROMDEVICE);
11847
Eric Dumazet9205fd92011-11-18 06:47:01 +000011848 rx_data += TG3_RX_OFFSET(tp);
Matt Carlsonbb158d62011-04-25 12:42:47 +000011849 for (i = data_off; i < rx_len; i++, val++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000011850 if (*(rx_data + i) != (u8) (val & 0xff))
Matt Carlsonbb158d62011-04-25 12:42:47 +000011851 goto out;
11852 }
Matt Carlson4852a862011-04-13 11:05:07 +000011853 }
11854
Michael Chanc76949a2005-05-29 14:58:59 -070011855 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011856
Eric Dumazet9205fd92011-11-18 06:47:01 +000011857 /* tg3_free_rings will unmap and free the rx_data */
Michael Chanc76949a2005-05-29 14:58:59 -070011858out:
11859 return err;
11860}
11861
Matt Carlson00c266b2011-04-25 12:42:46 +000011862#define TG3_STD_LOOPBACK_FAILED 1
11863#define TG3_JMB_LOOPBACK_FAILED 2
Matt Carlsonbb158d62011-04-25 12:42:47 +000011864#define TG3_TSO_LOOPBACK_FAILED 4
Matt Carlson28a45952011-08-19 13:58:22 +000011865#define TG3_LOOPBACK_FAILED \
11866 (TG3_STD_LOOPBACK_FAILED | \
11867 TG3_JMB_LOOPBACK_FAILED | \
11868 TG3_TSO_LOOPBACK_FAILED)
Matt Carlson00c266b2011-04-25 12:42:46 +000011869
Matt Carlson941ec902011-08-19 13:58:23 +000011870static int tg3_test_loopback(struct tg3 *tp, u64 *data, bool do_extlpbk)
Michael Chan9f40dea2005-09-05 17:53:06 -070011871{
Matt Carlson28a45952011-08-19 13:58:22 +000011872 int err = -EIO;
Matt Carlson2215e242011-08-19 13:58:19 +000011873 u32 eee_cap;
Michael Chan9f40dea2005-09-05 17:53:06 -070011874
Matt Carlsonab789042011-01-25 15:58:54 +000011875 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
11876 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
11877
Matt Carlson28a45952011-08-19 13:58:22 +000011878 if (!netif_running(tp->dev)) {
11879 data[0] = TG3_LOOPBACK_FAILED;
11880 data[1] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000011881 if (do_extlpbk)
11882 data[2] = TG3_LOOPBACK_FAILED;
Matt Carlson28a45952011-08-19 13:58:22 +000011883 goto done;
11884 }
11885
Michael Chanb9ec6c12006-07-25 16:37:27 -070011886 err = tg3_reset_hw(tp, 1);
Matt Carlsonab789042011-01-25 15:58:54 +000011887 if (err) {
Matt Carlson28a45952011-08-19 13:58:22 +000011888 data[0] = TG3_LOOPBACK_FAILED;
11889 data[1] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000011890 if (do_extlpbk)
11891 data[2] = TG3_LOOPBACK_FAILED;
Matt Carlsonab789042011-01-25 15:58:54 +000011892 goto done;
11893 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011894
Joe Perches63c3a662011-04-26 08:12:10 +000011895 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson4a85f092011-04-20 07:57:37 +000011896 int i;
11897
11898 /* Reroute all rx packets to the 1st queue */
11899 for (i = MAC_RSS_INDIR_TBL_0;
11900 i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
11901 tw32(i, 0x0);
11902 }
11903
Matt Carlson6e01b202011-08-19 13:58:20 +000011904 /* HW errata - mac loopback fails in some cases on 5780.
11905 * Normal traffic and PHY loopback are not affected by
11906 * errata. Also, the MAC loopback test is deprecated for
11907 * all newer ASIC revisions.
11908 */
11909 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
11910 !tg3_flag(tp, CPMU_PRESENT)) {
11911 tg3_mac_loopback(tp, true);
Matt Carlson9936bcf2007-10-10 18:03:07 -070011912
Matt Carlson28a45952011-08-19 13:58:22 +000011913 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
11914 data[0] |= TG3_STD_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000011915
11916 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000011917 tg3_run_loopback(tp, 9000 + ETH_HLEN, false))
11918 data[0] |= TG3_JMB_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000011919
11920 tg3_mac_loopback(tp, false);
11921 }
Matt Carlson4852a862011-04-13 11:05:07 +000011922
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011923 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000011924 !tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011925 int i;
11926
Matt Carlson941ec902011-08-19 13:58:23 +000011927 tg3_phy_lpbk_set(tp, 0, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011928
11929 /* Wait for link */
11930 for (i = 0; i < 100; i++) {
11931 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
11932 break;
11933 mdelay(1);
11934 }
11935
Matt Carlson28a45952011-08-19 13:58:22 +000011936 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
11937 data[1] |= TG3_STD_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000011938 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000011939 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
11940 data[1] |= TG3_TSO_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000011941 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000011942 tg3_run_loopback(tp, 9000 + ETH_HLEN, false))
11943 data[1] |= TG3_JMB_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070011944
Matt Carlson941ec902011-08-19 13:58:23 +000011945 if (do_extlpbk) {
11946 tg3_phy_lpbk_set(tp, 0, true);
11947
11948 /* All link indications report up, but the hardware
11949 * isn't really ready for about 20 msec. Double it
11950 * to be sure.
11951 */
11952 mdelay(40);
11953
11954 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
11955 data[2] |= TG3_STD_LOOPBACK_FAILED;
11956 if (tg3_flag(tp, TSO_CAPABLE) &&
11957 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
11958 data[2] |= TG3_TSO_LOOPBACK_FAILED;
11959 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
11960 tg3_run_loopback(tp, 9000 + ETH_HLEN, false))
11961 data[2] |= TG3_JMB_LOOPBACK_FAILED;
11962 }
11963
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011964 /* Re-enable gphy autopowerdown. */
11965 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
11966 tg3_phy_toggle_apd(tp, true);
11967 }
Matt Carlson6833c042008-11-21 17:18:59 -080011968
Matt Carlson941ec902011-08-19 13:58:23 +000011969 err = (data[0] | data[1] | data[2]) ? -EIO : 0;
Matt Carlson28a45952011-08-19 13:58:22 +000011970
Matt Carlsonab789042011-01-25 15:58:54 +000011971done:
11972 tp->phy_flags |= eee_cap;
11973
Michael Chan9f40dea2005-09-05 17:53:06 -070011974 return err;
11975}
11976
Michael Chan4cafd3f2005-05-29 14:56:34 -070011977static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
11978 u64 *data)
11979{
Michael Chan566f86a2005-05-29 14:56:58 -070011980 struct tg3 *tp = netdev_priv(dev);
Matt Carlson941ec902011-08-19 13:58:23 +000011981 bool doextlpbk = etest->flags & ETH_TEST_FL_EXTERNAL_LB;
Michael Chan566f86a2005-05-29 14:56:58 -070011982
Matt Carlsonbed98292011-07-13 09:27:29 +000011983 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
11984 tg3_power_up(tp)) {
11985 etest->flags |= ETH_TEST_FL_FAILED;
11986 memset(data, 1, sizeof(u64) * TG3_NUM_TEST);
11987 return;
11988 }
Michael Chanbc1c7562006-03-20 17:48:03 -080011989
Michael Chan566f86a2005-05-29 14:56:58 -070011990 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
11991
11992 if (tg3_test_nvram(tp) != 0) {
11993 etest->flags |= ETH_TEST_FL_FAILED;
11994 data[0] = 1;
11995 }
Matt Carlson941ec902011-08-19 13:58:23 +000011996 if (!doextlpbk && tg3_test_link(tp)) {
Michael Chanca430072005-05-29 14:57:23 -070011997 etest->flags |= ETH_TEST_FL_FAILED;
11998 data[1] = 1;
11999 }
Michael Chana71116d2005-05-29 14:58:11 -070012000 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012001 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070012002
Michael Chanbbe832c2005-06-24 20:20:04 -070012003 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012004 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070012005 tg3_netif_stop(tp);
12006 irq_sync = 1;
12007 }
12008
12009 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070012010
12011 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080012012 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012013 tg3_halt_cpu(tp, RX_CPU_BASE);
Joe Perches63c3a662011-04-26 08:12:10 +000012014 if (!tg3_flag(tp, 5705_PLUS))
Michael Chana71116d2005-05-29 14:58:11 -070012015 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080012016 if (!err)
12017 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012018
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012019 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad12006-03-20 22:27:35 -080012020 tg3_phy_reset(tp);
12021
Michael Chana71116d2005-05-29 14:58:11 -070012022 if (tg3_test_registers(tp) != 0) {
12023 etest->flags |= ETH_TEST_FL_FAILED;
12024 data[2] = 1;
12025 }
Matt Carlson28a45952011-08-19 13:58:22 +000012026
Michael Chan7942e1d2005-05-29 14:58:36 -070012027 if (tg3_test_memory(tp) != 0) {
12028 etest->flags |= ETH_TEST_FL_FAILED;
12029 data[3] = 1;
12030 }
Matt Carlson28a45952011-08-19 13:58:22 +000012031
Matt Carlson941ec902011-08-19 13:58:23 +000012032 if (doextlpbk)
12033 etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
12034
12035 if (tg3_test_loopback(tp, &data[4], doextlpbk))
Michael Chanc76949a2005-05-29 14:58:59 -070012036 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070012037
David S. Millerf47c11e2005-06-24 20:18:35 -070012038 tg3_full_unlock(tp);
12039
Michael Chand4bc3922005-05-29 14:59:20 -070012040 if (tg3_test_interrupt(tp) != 0) {
12041 etest->flags |= ETH_TEST_FL_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000012042 data[7] = 1;
Michael Chand4bc3922005-05-29 14:59:20 -070012043 }
David S. Millerf47c11e2005-06-24 20:18:35 -070012044
12045 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070012046
Michael Chana71116d2005-05-29 14:58:11 -070012047 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
12048 if (netif_running(dev)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012049 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012050 err2 = tg3_restart_hw(tp, 1);
12051 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070012052 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012053 }
David S. Millerf47c11e2005-06-24 20:18:35 -070012054
12055 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012056
12057 if (irq_sync && !err2)
12058 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012059 }
Matt Carlson80096062010-08-02 11:26:06 +000012060 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000012061 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080012062
Michael Chan4cafd3f2005-05-29 14:56:34 -070012063}
12064
Linus Torvalds1da177e2005-04-16 15:20:36 -070012065static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
12066{
12067 struct mii_ioctl_data *data = if_mii(ifr);
12068 struct tg3 *tp = netdev_priv(dev);
12069 int err;
12070
Joe Perches63c3a662011-04-26 08:12:10 +000012071 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000012072 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012073 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012074 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000012075 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000012076 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012077 }
12078
Matt Carlson33f401a2010-04-05 10:19:27 +000012079 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012080 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000012081 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012082
12083 /* fallthru */
12084 case SIOCGMIIREG: {
12085 u32 mii_regval;
12086
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012087 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012088 break; /* We have no PHY */
12089
Matt Carlson34eea5a2011-04-20 07:57:38 +000012090 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080012091 return -EAGAIN;
12092
David S. Millerf47c11e2005-06-24 20:18:35 -070012093 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012094 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070012095 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012096
12097 data->val_out = mii_regval;
12098
12099 return err;
12100 }
12101
12102 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012103 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012104 break; /* We have no PHY */
12105
Matt Carlson34eea5a2011-04-20 07:57:38 +000012106 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080012107 return -EAGAIN;
12108
David S. Millerf47c11e2005-06-24 20:18:35 -070012109 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012110 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070012111 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012112
12113 return err;
12114
12115 default:
12116 /* do nothing */
12117 break;
12118 }
12119 return -EOPNOTSUPP;
12120}
12121
David S. Miller15f98502005-05-18 22:49:26 -070012122static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
12123{
12124 struct tg3 *tp = netdev_priv(dev);
12125
12126 memcpy(ec, &tp->coal, sizeof(*ec));
12127 return 0;
12128}
12129
Michael Chand244c892005-07-05 14:42:33 -070012130static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
12131{
12132 struct tg3 *tp = netdev_priv(dev);
12133 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
12134 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
12135
Joe Perches63c3a662011-04-26 08:12:10 +000012136 if (!tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070012137 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
12138 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
12139 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
12140 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
12141 }
12142
12143 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
12144 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
12145 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
12146 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
12147 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
12148 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
12149 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
12150 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
12151 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
12152 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
12153 return -EINVAL;
12154
12155 /* No rx interrupts will be generated if both are zero */
12156 if ((ec->rx_coalesce_usecs == 0) &&
12157 (ec->rx_max_coalesced_frames == 0))
12158 return -EINVAL;
12159
12160 /* No tx interrupts will be generated if both are zero */
12161 if ((ec->tx_coalesce_usecs == 0) &&
12162 (ec->tx_max_coalesced_frames == 0))
12163 return -EINVAL;
12164
12165 /* Only copy relevant parameters, ignore all others. */
12166 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
12167 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
12168 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
12169 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
12170 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
12171 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
12172 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
12173 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
12174 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
12175
12176 if (netif_running(dev)) {
12177 tg3_full_lock(tp, 0);
12178 __tg3_set_coalesce(tp, &tp->coal);
12179 tg3_full_unlock(tp);
12180 }
12181 return 0;
12182}
12183
Jeff Garzik7282d492006-09-13 14:30:00 -040012184static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012185 .get_settings = tg3_get_settings,
12186 .set_settings = tg3_set_settings,
12187 .get_drvinfo = tg3_get_drvinfo,
12188 .get_regs_len = tg3_get_regs_len,
12189 .get_regs = tg3_get_regs,
12190 .get_wol = tg3_get_wol,
12191 .set_wol = tg3_set_wol,
12192 .get_msglevel = tg3_get_msglevel,
12193 .set_msglevel = tg3_set_msglevel,
12194 .nway_reset = tg3_nway_reset,
12195 .get_link = ethtool_op_get_link,
12196 .get_eeprom_len = tg3_get_eeprom_len,
12197 .get_eeprom = tg3_get_eeprom,
12198 .set_eeprom = tg3_set_eeprom,
12199 .get_ringparam = tg3_get_ringparam,
12200 .set_ringparam = tg3_set_ringparam,
12201 .get_pauseparam = tg3_get_pauseparam,
12202 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070012203 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012204 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000012205 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012206 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070012207 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070012208 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070012209 .get_sset_count = tg3_get_sset_count,
Matt Carlson90415472011-12-16 13:33:23 +000012210 .get_rxnfc = tg3_get_rxnfc,
12211 .get_rxfh_indir_size = tg3_get_rxfh_indir_size,
12212 .get_rxfh_indir = tg3_get_rxfh_indir,
12213 .set_rxfh_indir = tg3_set_rxfh_indir,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012214};
12215
Matt Carlsonccd5ba92012-02-13 10:20:08 +000012216static void tg3_set_rx_mode(struct net_device *dev)
12217{
12218 struct tg3 *tp = netdev_priv(dev);
12219
12220 if (!netif_running(dev))
12221 return;
12222
12223 tg3_full_lock(tp, 0);
12224 __tg3_set_rx_mode(dev);
12225 tg3_full_unlock(tp);
12226}
12227
Matt Carlsonfaf16272012-02-13 10:20:07 +000012228static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
12229 int new_mtu)
12230{
12231 dev->mtu = new_mtu;
12232
12233 if (new_mtu > ETH_DATA_LEN) {
12234 if (tg3_flag(tp, 5780_CLASS)) {
12235 netdev_update_features(dev);
12236 tg3_flag_clear(tp, TSO_CAPABLE);
12237 } else {
12238 tg3_flag_set(tp, JUMBO_RING_ENABLE);
12239 }
12240 } else {
12241 if (tg3_flag(tp, 5780_CLASS)) {
12242 tg3_flag_set(tp, TSO_CAPABLE);
12243 netdev_update_features(dev);
12244 }
12245 tg3_flag_clear(tp, JUMBO_RING_ENABLE);
12246 }
12247}
12248
12249static int tg3_change_mtu(struct net_device *dev, int new_mtu)
12250{
12251 struct tg3 *tp = netdev_priv(dev);
12252 int err;
12253
12254 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
12255 return -EINVAL;
12256
12257 if (!netif_running(dev)) {
12258 /* We'll just catch it later when the
12259 * device is up'd.
12260 */
12261 tg3_set_mtu(dev, tp, new_mtu);
12262 return 0;
12263 }
12264
12265 tg3_phy_stop(tp);
12266
12267 tg3_netif_stop(tp);
12268
12269 tg3_full_lock(tp, 1);
12270
12271 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
12272
12273 tg3_set_mtu(dev, tp, new_mtu);
12274
12275 err = tg3_restart_hw(tp, 0);
12276
12277 if (!err)
12278 tg3_netif_start(tp);
12279
12280 tg3_full_unlock(tp);
12281
12282 if (!err)
12283 tg3_phy_start(tp);
12284
12285 return err;
12286}
12287
12288static const struct net_device_ops tg3_netdev_ops = {
12289 .ndo_open = tg3_open,
12290 .ndo_stop = tg3_close,
12291 .ndo_start_xmit = tg3_start_xmit,
12292 .ndo_get_stats64 = tg3_get_stats64,
12293 .ndo_validate_addr = eth_validate_addr,
12294 .ndo_set_rx_mode = tg3_set_rx_mode,
12295 .ndo_set_mac_address = tg3_set_mac_addr,
12296 .ndo_do_ioctl = tg3_ioctl,
12297 .ndo_tx_timeout = tg3_tx_timeout,
12298 .ndo_change_mtu = tg3_change_mtu,
12299 .ndo_fix_features = tg3_fix_features,
12300 .ndo_set_features = tg3_set_features,
12301#ifdef CONFIG_NET_POLL_CONTROLLER
12302 .ndo_poll_controller = tg3_poll_controller,
12303#endif
12304};
12305
Linus Torvalds1da177e2005-04-16 15:20:36 -070012306static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
12307{
Michael Chan1b277772006-03-20 22:27:48 -080012308 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012309
12310 tp->nvram_size = EEPROM_CHIP_SIZE;
12311
Matt Carlsone4f34112009-02-25 14:25:00 +000012312 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012313 return;
12314
Michael Chanb16250e2006-09-27 16:10:14 -070012315 if ((magic != TG3_EEPROM_MAGIC) &&
12316 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
12317 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012318 return;
12319
12320 /*
12321 * Size the chip by reading offsets at increasing powers of two.
12322 * When we encounter our validation signature, we know the addressing
12323 * has wrapped around, and thus have our chip size.
12324 */
Michael Chan1b277772006-03-20 22:27:48 -080012325 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012326
12327 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000012328 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012329 return;
12330
Michael Chan18201802006-03-20 22:29:15 -080012331 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012332 break;
12333
12334 cursize <<= 1;
12335 }
12336
12337 tp->nvram_size = cursize;
12338}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012339
Linus Torvalds1da177e2005-04-16 15:20:36 -070012340static void __devinit tg3_get_nvram_size(struct tg3 *tp)
12341{
12342 u32 val;
12343
Joe Perches63c3a662011-04-26 08:12:10 +000012344 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080012345 return;
12346
12347 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080012348 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080012349 tg3_get_eeprom_size(tp);
12350 return;
12351 }
12352
Matt Carlson6d348f22009-02-25 14:25:52 +000012353 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012354 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000012355 /* This is confusing. We want to operate on the
12356 * 16-bit value at offset 0xf2. The tg3_nvram_read()
12357 * call will read from NVRAM and byteswap the data
12358 * according to the byteswapping settings for all
12359 * other register accesses. This ensures the data we
12360 * want will always reside in the lower 16-bits.
12361 * However, the data in NVRAM is in LE format, which
12362 * means the data from the NVRAM read will always be
12363 * opposite the endianness of the CPU. The 16-bit
12364 * byteswap then brings the data to CPU endianness.
12365 */
12366 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012367 return;
12368 }
12369 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070012370 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012371}
12372
12373static void __devinit tg3_get_nvram_info(struct tg3 *tp)
12374{
12375 u32 nvcfg1;
12376
12377 nvcfg1 = tr32(NVRAM_CFG1);
12378 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
Joe Perches63c3a662011-04-26 08:12:10 +000012379 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012380 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012381 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12382 tw32(NVRAM_CFG1, nvcfg1);
12383 }
12384
Matt Carlson6ff6f812011-05-19 12:12:54 +000012385 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
Joe Perches63c3a662011-04-26 08:12:10 +000012386 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012387 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012388 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
12389 tp->nvram_jedecnum = JEDEC_ATMEL;
12390 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012391 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012392 break;
12393 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
12394 tp->nvram_jedecnum = JEDEC_ATMEL;
12395 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
12396 break;
12397 case FLASH_VENDOR_ATMEL_EEPROM:
12398 tp->nvram_jedecnum = JEDEC_ATMEL;
12399 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012400 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012401 break;
12402 case FLASH_VENDOR_ST:
12403 tp->nvram_jedecnum = JEDEC_ST;
12404 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012405 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012406 break;
12407 case FLASH_VENDOR_SAIFUN:
12408 tp->nvram_jedecnum = JEDEC_SAIFUN;
12409 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
12410 break;
12411 case FLASH_VENDOR_SST_SMALL:
12412 case FLASH_VENDOR_SST_LARGE:
12413 tp->nvram_jedecnum = JEDEC_SST;
12414 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
12415 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012416 }
Matt Carlson8590a602009-08-28 12:29:16 +000012417 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012418 tp->nvram_jedecnum = JEDEC_ATMEL;
12419 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012420 tg3_flag_set(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012421 }
12422}
12423
Matt Carlsona1b950d2009-09-01 13:20:17 +000012424static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
12425{
12426 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
12427 case FLASH_5752PAGE_SIZE_256:
12428 tp->nvram_pagesize = 256;
12429 break;
12430 case FLASH_5752PAGE_SIZE_512:
12431 tp->nvram_pagesize = 512;
12432 break;
12433 case FLASH_5752PAGE_SIZE_1K:
12434 tp->nvram_pagesize = 1024;
12435 break;
12436 case FLASH_5752PAGE_SIZE_2K:
12437 tp->nvram_pagesize = 2048;
12438 break;
12439 case FLASH_5752PAGE_SIZE_4K:
12440 tp->nvram_pagesize = 4096;
12441 break;
12442 case FLASH_5752PAGE_SIZE_264:
12443 tp->nvram_pagesize = 264;
12444 break;
12445 case FLASH_5752PAGE_SIZE_528:
12446 tp->nvram_pagesize = 528;
12447 break;
12448 }
12449}
12450
Michael Chan361b4ac2005-04-21 17:11:21 -070012451static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
12452{
12453 u32 nvcfg1;
12454
12455 nvcfg1 = tr32(NVRAM_CFG1);
12456
Michael Chane6af3012005-04-21 17:12:05 -070012457 /* NVRAM protection for TPM */
12458 if (nvcfg1 & (1 << 27))
Joe Perches63c3a662011-04-26 08:12:10 +000012459 tg3_flag_set(tp, PROTECTED_NVRAM);
Michael Chane6af3012005-04-21 17:12:05 -070012460
Michael Chan361b4ac2005-04-21 17:11:21 -070012461 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012462 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
12463 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
12464 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012465 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012466 break;
12467 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12468 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012469 tg3_flag_set(tp, NVRAM_BUFFERED);
12470 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012471 break;
12472 case FLASH_5752VENDOR_ST_M45PE10:
12473 case FLASH_5752VENDOR_ST_M45PE20:
12474 case FLASH_5752VENDOR_ST_M45PE40:
12475 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012476 tg3_flag_set(tp, NVRAM_BUFFERED);
12477 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012478 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070012479 }
12480
Joe Perches63c3a662011-04-26 08:12:10 +000012481 if (tg3_flag(tp, FLASH)) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000012482 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000012483 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070012484 /* For eeprom, set pagesize to maximum eeprom size */
12485 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12486
12487 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12488 tw32(NVRAM_CFG1, nvcfg1);
12489 }
12490}
12491
Michael Chand3c7b882006-03-23 01:28:25 -080012492static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
12493{
Matt Carlson989a9d22007-05-05 11:51:05 -070012494 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080012495
12496 nvcfg1 = tr32(NVRAM_CFG1);
12497
12498 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070012499 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012500 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson989a9d22007-05-05 11:51:05 -070012501 protect = 1;
12502 }
Michael Chand3c7b882006-03-23 01:28:25 -080012503
Matt Carlson989a9d22007-05-05 11:51:05 -070012504 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
12505 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012506 case FLASH_5755VENDOR_ATMEL_FLASH_1:
12507 case FLASH_5755VENDOR_ATMEL_FLASH_2:
12508 case FLASH_5755VENDOR_ATMEL_FLASH_3:
12509 case FLASH_5755VENDOR_ATMEL_FLASH_5:
12510 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012511 tg3_flag_set(tp, NVRAM_BUFFERED);
12512 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012513 tp->nvram_pagesize = 264;
12514 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
12515 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
12516 tp->nvram_size = (protect ? 0x3e200 :
12517 TG3_NVRAM_SIZE_512KB);
12518 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
12519 tp->nvram_size = (protect ? 0x1f200 :
12520 TG3_NVRAM_SIZE_256KB);
12521 else
12522 tp->nvram_size = (protect ? 0x1f200 :
12523 TG3_NVRAM_SIZE_128KB);
12524 break;
12525 case FLASH_5752VENDOR_ST_M45PE10:
12526 case FLASH_5752VENDOR_ST_M45PE20:
12527 case FLASH_5752VENDOR_ST_M45PE40:
12528 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012529 tg3_flag_set(tp, NVRAM_BUFFERED);
12530 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012531 tp->nvram_pagesize = 256;
12532 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
12533 tp->nvram_size = (protect ?
12534 TG3_NVRAM_SIZE_64KB :
12535 TG3_NVRAM_SIZE_128KB);
12536 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
12537 tp->nvram_size = (protect ?
12538 TG3_NVRAM_SIZE_64KB :
12539 TG3_NVRAM_SIZE_256KB);
12540 else
12541 tp->nvram_size = (protect ?
12542 TG3_NVRAM_SIZE_128KB :
12543 TG3_NVRAM_SIZE_512KB);
12544 break;
Michael Chand3c7b882006-03-23 01:28:25 -080012545 }
12546}
12547
Michael Chan1b277772006-03-20 22:27:48 -080012548static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
12549{
12550 u32 nvcfg1;
12551
12552 nvcfg1 = tr32(NVRAM_CFG1);
12553
12554 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012555 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
12556 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
12557 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
12558 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
12559 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012560 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012561 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080012562
Matt Carlson8590a602009-08-28 12:29:16 +000012563 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12564 tw32(NVRAM_CFG1, nvcfg1);
12565 break;
12566 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12567 case FLASH_5755VENDOR_ATMEL_FLASH_1:
12568 case FLASH_5755VENDOR_ATMEL_FLASH_2:
12569 case FLASH_5755VENDOR_ATMEL_FLASH_3:
12570 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012571 tg3_flag_set(tp, NVRAM_BUFFERED);
12572 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012573 tp->nvram_pagesize = 264;
12574 break;
12575 case FLASH_5752VENDOR_ST_M45PE10:
12576 case FLASH_5752VENDOR_ST_M45PE20:
12577 case FLASH_5752VENDOR_ST_M45PE40:
12578 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012579 tg3_flag_set(tp, NVRAM_BUFFERED);
12580 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012581 tp->nvram_pagesize = 256;
12582 break;
Michael Chan1b277772006-03-20 22:27:48 -080012583 }
12584}
12585
Matt Carlson6b91fa02007-10-10 18:01:09 -070012586static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
12587{
12588 u32 nvcfg1, protect = 0;
12589
12590 nvcfg1 = tr32(NVRAM_CFG1);
12591
12592 /* NVRAM protection for TPM */
12593 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012594 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson6b91fa02007-10-10 18:01:09 -070012595 protect = 1;
12596 }
12597
12598 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
12599 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012600 case FLASH_5761VENDOR_ATMEL_ADB021D:
12601 case FLASH_5761VENDOR_ATMEL_ADB041D:
12602 case FLASH_5761VENDOR_ATMEL_ADB081D:
12603 case FLASH_5761VENDOR_ATMEL_ADB161D:
12604 case FLASH_5761VENDOR_ATMEL_MDB021D:
12605 case FLASH_5761VENDOR_ATMEL_MDB041D:
12606 case FLASH_5761VENDOR_ATMEL_MDB081D:
12607 case FLASH_5761VENDOR_ATMEL_MDB161D:
12608 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012609 tg3_flag_set(tp, NVRAM_BUFFERED);
12610 tg3_flag_set(tp, FLASH);
12611 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson8590a602009-08-28 12:29:16 +000012612 tp->nvram_pagesize = 256;
12613 break;
12614 case FLASH_5761VENDOR_ST_A_M45PE20:
12615 case FLASH_5761VENDOR_ST_A_M45PE40:
12616 case FLASH_5761VENDOR_ST_A_M45PE80:
12617 case FLASH_5761VENDOR_ST_A_M45PE16:
12618 case FLASH_5761VENDOR_ST_M_M45PE20:
12619 case FLASH_5761VENDOR_ST_M_M45PE40:
12620 case FLASH_5761VENDOR_ST_M_M45PE80:
12621 case FLASH_5761VENDOR_ST_M_M45PE16:
12622 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012623 tg3_flag_set(tp, NVRAM_BUFFERED);
12624 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012625 tp->nvram_pagesize = 256;
12626 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070012627 }
12628
12629 if (protect) {
12630 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
12631 } else {
12632 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012633 case FLASH_5761VENDOR_ATMEL_ADB161D:
12634 case FLASH_5761VENDOR_ATMEL_MDB161D:
12635 case FLASH_5761VENDOR_ST_A_M45PE16:
12636 case FLASH_5761VENDOR_ST_M_M45PE16:
12637 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
12638 break;
12639 case FLASH_5761VENDOR_ATMEL_ADB081D:
12640 case FLASH_5761VENDOR_ATMEL_MDB081D:
12641 case FLASH_5761VENDOR_ST_A_M45PE80:
12642 case FLASH_5761VENDOR_ST_M_M45PE80:
12643 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12644 break;
12645 case FLASH_5761VENDOR_ATMEL_ADB041D:
12646 case FLASH_5761VENDOR_ATMEL_MDB041D:
12647 case FLASH_5761VENDOR_ST_A_M45PE40:
12648 case FLASH_5761VENDOR_ST_M_M45PE40:
12649 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12650 break;
12651 case FLASH_5761VENDOR_ATMEL_ADB021D:
12652 case FLASH_5761VENDOR_ATMEL_MDB021D:
12653 case FLASH_5761VENDOR_ST_A_M45PE20:
12654 case FLASH_5761VENDOR_ST_M_M45PE20:
12655 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12656 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070012657 }
12658 }
12659}
12660
Michael Chanb5d37722006-09-27 16:06:21 -070012661static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
12662{
12663 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012664 tg3_flag_set(tp, NVRAM_BUFFERED);
Michael Chanb5d37722006-09-27 16:06:21 -070012665 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12666}
12667
Matt Carlson321d32a2008-11-21 17:22:19 -080012668static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
12669{
12670 u32 nvcfg1;
12671
12672 nvcfg1 = tr32(NVRAM_CFG1);
12673
12674 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12675 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
12676 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
12677 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012678 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson321d32a2008-11-21 17:22:19 -080012679 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12680
12681 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12682 tw32(NVRAM_CFG1, nvcfg1);
12683 return;
12684 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12685 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
12686 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
12687 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
12688 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
12689 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
12690 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
12691 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012692 tg3_flag_set(tp, NVRAM_BUFFERED);
12693 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080012694
12695 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12696 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12697 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
12698 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
12699 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12700 break;
12701 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
12702 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
12703 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12704 break;
12705 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
12706 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
12707 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12708 break;
12709 }
12710 break;
12711 case FLASH_5752VENDOR_ST_M45PE10:
12712 case FLASH_5752VENDOR_ST_M45PE20:
12713 case FLASH_5752VENDOR_ST_M45PE40:
12714 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012715 tg3_flag_set(tp, NVRAM_BUFFERED);
12716 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080012717
12718 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12719 case FLASH_5752VENDOR_ST_M45PE10:
12720 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12721 break;
12722 case FLASH_5752VENDOR_ST_M45PE20:
12723 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12724 break;
12725 case FLASH_5752VENDOR_ST_M45PE40:
12726 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12727 break;
12728 }
12729 break;
12730 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012731 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson321d32a2008-11-21 17:22:19 -080012732 return;
12733 }
12734
Matt Carlsona1b950d2009-09-01 13:20:17 +000012735 tg3_nvram_get_pagesize(tp, nvcfg1);
12736 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012737 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012738}
12739
12740
12741static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
12742{
12743 u32 nvcfg1;
12744
12745 nvcfg1 = tr32(NVRAM_CFG1);
12746
12747 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12748 case FLASH_5717VENDOR_ATMEL_EEPROM:
12749 case FLASH_5717VENDOR_MICRO_EEPROM:
12750 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012751 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012752 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12753
12754 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12755 tw32(NVRAM_CFG1, nvcfg1);
12756 return;
12757 case FLASH_5717VENDOR_ATMEL_MDB011D:
12758 case FLASH_5717VENDOR_ATMEL_ADB011B:
12759 case FLASH_5717VENDOR_ATMEL_ADB011D:
12760 case FLASH_5717VENDOR_ATMEL_MDB021D:
12761 case FLASH_5717VENDOR_ATMEL_ADB021B:
12762 case FLASH_5717VENDOR_ATMEL_ADB021D:
12763 case FLASH_5717VENDOR_ATMEL_45USPT:
12764 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012765 tg3_flag_set(tp, NVRAM_BUFFERED);
12766 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012767
12768 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12769 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012770 /* Detect size with tg3_nvram_get_size() */
12771 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012772 case FLASH_5717VENDOR_ATMEL_ADB021B:
12773 case FLASH_5717VENDOR_ATMEL_ADB021D:
12774 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12775 break;
12776 default:
12777 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12778 break;
12779 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012780 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012781 case FLASH_5717VENDOR_ST_M_M25PE10:
12782 case FLASH_5717VENDOR_ST_A_M25PE10:
12783 case FLASH_5717VENDOR_ST_M_M45PE10:
12784 case FLASH_5717VENDOR_ST_A_M45PE10:
12785 case FLASH_5717VENDOR_ST_M_M25PE20:
12786 case FLASH_5717VENDOR_ST_A_M25PE20:
12787 case FLASH_5717VENDOR_ST_M_M45PE20:
12788 case FLASH_5717VENDOR_ST_A_M45PE20:
12789 case FLASH_5717VENDOR_ST_25USPT:
12790 case FLASH_5717VENDOR_ST_45USPT:
12791 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012792 tg3_flag_set(tp, NVRAM_BUFFERED);
12793 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012794
12795 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12796 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012797 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012798 /* Detect size with tg3_nvram_get_size() */
12799 break;
12800 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012801 case FLASH_5717VENDOR_ST_A_M45PE20:
12802 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12803 break;
12804 default:
12805 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12806 break;
12807 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012808 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012809 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012810 tg3_flag_set(tp, NO_NVRAM);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012811 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080012812 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000012813
12814 tg3_nvram_get_pagesize(tp, nvcfg1);
12815 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012816 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson321d32a2008-11-21 17:22:19 -080012817}
12818
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012819static void __devinit tg3_get_5720_nvram_info(struct tg3 *tp)
12820{
12821 u32 nvcfg1, nvmpinstrp;
12822
12823 nvcfg1 = tr32(NVRAM_CFG1);
12824 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
12825
12826 switch (nvmpinstrp) {
12827 case FLASH_5720_EEPROM_HD:
12828 case FLASH_5720_EEPROM_LD:
12829 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012830 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012831
12832 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12833 tw32(NVRAM_CFG1, nvcfg1);
12834 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
12835 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12836 else
12837 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
12838 return;
12839 case FLASH_5720VENDOR_M_ATMEL_DB011D:
12840 case FLASH_5720VENDOR_A_ATMEL_DB011B:
12841 case FLASH_5720VENDOR_A_ATMEL_DB011D:
12842 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12843 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12844 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12845 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12846 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12847 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12848 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12849 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12850 case FLASH_5720VENDOR_ATMEL_45USPT:
12851 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012852 tg3_flag_set(tp, NVRAM_BUFFERED);
12853 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012854
12855 switch (nvmpinstrp) {
12856 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12857 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12858 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12859 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12860 break;
12861 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12862 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12863 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12864 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12865 break;
12866 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12867 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12868 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12869 break;
12870 default:
12871 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12872 break;
12873 }
12874 break;
12875 case FLASH_5720VENDOR_M_ST_M25PE10:
12876 case FLASH_5720VENDOR_M_ST_M45PE10:
12877 case FLASH_5720VENDOR_A_ST_M25PE10:
12878 case FLASH_5720VENDOR_A_ST_M45PE10:
12879 case FLASH_5720VENDOR_M_ST_M25PE20:
12880 case FLASH_5720VENDOR_M_ST_M45PE20:
12881 case FLASH_5720VENDOR_A_ST_M25PE20:
12882 case FLASH_5720VENDOR_A_ST_M45PE20:
12883 case FLASH_5720VENDOR_M_ST_M25PE40:
12884 case FLASH_5720VENDOR_M_ST_M45PE40:
12885 case FLASH_5720VENDOR_A_ST_M25PE40:
12886 case FLASH_5720VENDOR_A_ST_M45PE40:
12887 case FLASH_5720VENDOR_M_ST_M25PE80:
12888 case FLASH_5720VENDOR_M_ST_M45PE80:
12889 case FLASH_5720VENDOR_A_ST_M25PE80:
12890 case FLASH_5720VENDOR_A_ST_M45PE80:
12891 case FLASH_5720VENDOR_ST_25USPT:
12892 case FLASH_5720VENDOR_ST_45USPT:
12893 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012894 tg3_flag_set(tp, NVRAM_BUFFERED);
12895 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012896
12897 switch (nvmpinstrp) {
12898 case FLASH_5720VENDOR_M_ST_M25PE20:
12899 case FLASH_5720VENDOR_M_ST_M45PE20:
12900 case FLASH_5720VENDOR_A_ST_M25PE20:
12901 case FLASH_5720VENDOR_A_ST_M45PE20:
12902 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12903 break;
12904 case FLASH_5720VENDOR_M_ST_M25PE40:
12905 case FLASH_5720VENDOR_M_ST_M45PE40:
12906 case FLASH_5720VENDOR_A_ST_M25PE40:
12907 case FLASH_5720VENDOR_A_ST_M45PE40:
12908 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12909 break;
12910 case FLASH_5720VENDOR_M_ST_M25PE80:
12911 case FLASH_5720VENDOR_M_ST_M45PE80:
12912 case FLASH_5720VENDOR_A_ST_M25PE80:
12913 case FLASH_5720VENDOR_A_ST_M45PE80:
12914 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12915 break;
12916 default:
12917 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12918 break;
12919 }
12920 break;
12921 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012922 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012923 return;
12924 }
12925
12926 tg3_nvram_get_pagesize(tp, nvcfg1);
12927 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012928 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012929}
12930
Linus Torvalds1da177e2005-04-16 15:20:36 -070012931/* Chips other than 5700/5701 use the NVRAM for fetching info. */
12932static void __devinit tg3_nvram_init(struct tg3 *tp)
12933{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012934 tw32_f(GRC_EEPROM_ADDR,
12935 (EEPROM_ADDR_FSM_RESET |
12936 (EEPROM_DEFAULT_CLOCK_PERIOD <<
12937 EEPROM_ADDR_CLKPERD_SHIFT)));
12938
Michael Chan9d57f012006-12-07 00:23:25 -080012939 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012940
12941 /* Enable seeprom accesses. */
12942 tw32_f(GRC_LOCAL_CTRL,
12943 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
12944 udelay(100);
12945
12946 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
12947 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
Joe Perches63c3a662011-04-26 08:12:10 +000012948 tg3_flag_set(tp, NVRAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012949
Michael Chanec41c7d2006-01-17 02:40:55 -080012950 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000012951 netdev_warn(tp->dev,
12952 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000012953 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080012954 return;
12955 }
Michael Chane6af3012005-04-21 17:12:05 -070012956 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012957
Matt Carlson989a9d22007-05-05 11:51:05 -070012958 tp->nvram_size = 0;
12959
Michael Chan361b4ac2005-04-21 17:11:21 -070012960 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
12961 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080012962 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
12963 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070012964 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070012965 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
12966 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080012967 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070012968 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
12969 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070012970 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12971 tg3_get_5906_nvram_info(tp);
Matt Carlsonb703df62009-12-03 08:36:21 +000012972 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000012973 tg3_flag(tp, 57765_CLASS))
Matt Carlson321d32a2008-11-21 17:22:19 -080012974 tg3_get_57780_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012975 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
12976 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000012977 tg3_get_5717_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012978 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
12979 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070012980 else
12981 tg3_get_nvram_info(tp);
12982
Matt Carlson989a9d22007-05-05 11:51:05 -070012983 if (tp->nvram_size == 0)
12984 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012985
Michael Chane6af3012005-04-21 17:12:05 -070012986 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080012987 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012988
12989 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000012990 tg3_flag_clear(tp, NVRAM);
12991 tg3_flag_clear(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012992
12993 tg3_get_eeprom_size(tp);
12994 }
12995}
12996
Linus Torvalds1da177e2005-04-16 15:20:36 -070012997struct subsys_tbl_ent {
12998 u16 subsys_vendor, subsys_devid;
12999 u32 phy_id;
13000};
13001
Matt Carlson24daf2b2010-02-17 15:17:02 +000013002static struct subsys_tbl_ent subsys_id_to_phy_id[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013003 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013004 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013005 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013006 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013007 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013008 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013009 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013010 { TG3PCI_SUBVENDOR_ID_BROADCOM,
13011 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
13012 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013013 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013014 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013015 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013016 { TG3PCI_SUBVENDOR_ID_BROADCOM,
13017 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
13018 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013019 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013020 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013021 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013022 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013023 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013024 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013025 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013026
13027 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013028 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013029 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013030 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013031 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013032 { TG3PCI_SUBVENDOR_ID_3COM,
13033 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
13034 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013035 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013036 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013037 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013038
13039 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013040 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013041 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013042 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013043 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013044 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013045 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013046 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013047 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013048
13049 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013050 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013051 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013052 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013053 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013054 { TG3PCI_SUBVENDOR_ID_COMPAQ,
13055 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
13056 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013057 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013058 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013059 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013060
13061 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013062 { TG3PCI_SUBVENDOR_ID_IBM,
13063 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013064};
13065
Matt Carlson24daf2b2010-02-17 15:17:02 +000013066static struct subsys_tbl_ent * __devinit tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013067{
13068 int i;
13069
13070 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
13071 if ((subsys_id_to_phy_id[i].subsys_vendor ==
13072 tp->pdev->subsystem_vendor) &&
13073 (subsys_id_to_phy_id[i].subsys_devid ==
13074 tp->pdev->subsystem_device))
13075 return &subsys_id_to_phy_id[i];
13076 }
13077 return NULL;
13078}
13079
Michael Chan7d0c41e2005-04-21 17:06:20 -070013080static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013081{
Linus Torvalds1da177e2005-04-16 15:20:36 -070013082 u32 val;
David S. Millerf49639e2006-06-09 11:58:36 -070013083
Matt Carlson79eb6902010-02-17 15:17:03 +000013084 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070013085 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13086
Gary Zambranoa85feb82007-05-05 11:52:19 -070013087 /* Assume an onboard device and WOL capable by default. */
Joe Perches63c3a662011-04-26 08:12:10 +000013088 tg3_flag_set(tp, EEPROM_WRITE_PROT);
13089 tg3_flag_set(tp, WOL_CAP);
David S. Miller72b845e2006-03-14 14:11:48 -080013090
Michael Chanb5d37722006-09-27 16:06:21 -070013091 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080013092 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013093 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
13094 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080013095 }
Matt Carlson0527ba32007-10-10 18:03:30 -070013096 val = tr32(VCPU_CFGSHDW);
13097 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Joe Perches63c3a662011-04-26 08:12:10 +000013098 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson0527ba32007-10-10 18:03:30 -070013099 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013100 (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013101 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013102 device_set_wakeup_enable(&tp->pdev->dev, true);
13103 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080013104 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070013105 }
13106
Linus Torvalds1da177e2005-04-16 15:20:36 -070013107 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
13108 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
13109 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070013110 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070013111 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013112
13113 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
13114 tp->nic_sram_data_cfg = nic_cfg;
13115
13116 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
13117 ver >>= NIC_SRAM_DATA_VER_SHIFT;
Matt Carlson6ff6f812011-05-19 12:12:54 +000013118 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13119 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13120 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070013121 (ver > 0) && (ver < 0x100))
13122 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
13123
Matt Carlsona9daf362008-05-25 23:49:44 -070013124 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
13125 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
13126
Linus Torvalds1da177e2005-04-16 15:20:36 -070013127 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
13128 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
13129 eeprom_phy_serdes = 1;
13130
13131 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
13132 if (nic_phy_id != 0) {
13133 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
13134 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
13135
13136 eeprom_phy_id = (id1 >> 16) << 10;
13137 eeprom_phy_id |= (id2 & 0xfc00) << 16;
13138 eeprom_phy_id |= (id2 & 0x03ff) << 0;
13139 } else
13140 eeprom_phy_id = 0;
13141
Michael Chan7d0c41e2005-04-21 17:06:20 -070013142 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070013143 if (eeprom_phy_serdes) {
Joe Perches63c3a662011-04-26 08:12:10 +000013144 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013145 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000013146 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013147 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070013148 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070013149
Joe Perches63c3a662011-04-26 08:12:10 +000013150 if (tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013151 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
13152 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070013153 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070013154 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
13155
13156 switch (led_cfg) {
13157 default:
13158 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
13159 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13160 break;
13161
13162 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
13163 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
13164 break;
13165
13166 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
13167 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070013168
13169 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
13170 * read on some older 5700/5701 bootcode.
13171 */
13172 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
13173 ASIC_REV_5700 ||
13174 GET_ASIC_REV(tp->pci_chip_rev_id) ==
13175 ASIC_REV_5701)
13176 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13177
Linus Torvalds1da177e2005-04-16 15:20:36 -070013178 break;
13179
13180 case SHASTA_EXT_LED_SHARED:
13181 tp->led_ctrl = LED_CTRL_MODE_SHARED;
13182 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
13183 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
13184 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
13185 LED_CTRL_MODE_PHY_2);
13186 break;
13187
13188 case SHASTA_EXT_LED_MAC:
13189 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
13190 break;
13191
13192 case SHASTA_EXT_LED_COMBO:
13193 tp->led_ctrl = LED_CTRL_MODE_COMBO;
13194 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
13195 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
13196 LED_CTRL_MODE_PHY_2);
13197 break;
13198
Stephen Hemminger855e1112008-04-16 16:37:28 -070013199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013200
13201 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13202 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
13203 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
13204 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
13205
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013206 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
13207 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080013208
Michael Chan9d26e212006-12-07 00:21:14 -080013209 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Joe Perches63c3a662011-04-26 08:12:10 +000013210 tg3_flag_set(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080013211 if ((tp->pdev->subsystem_vendor ==
13212 PCI_VENDOR_ID_ARIMA) &&
13213 (tp->pdev->subsystem_device == 0x205a ||
13214 tp->pdev->subsystem_device == 0x2063))
Joe Perches63c3a662011-04-26 08:12:10 +000013215 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080013216 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000013217 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
13218 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080013219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013220
13221 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +000013222 tg3_flag_set(tp, ENABLE_ASF);
13223 if (tg3_flag(tp, 5750_PLUS))
13224 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013225 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080013226
13227 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013228 tg3_flag(tp, 5750_PLUS))
13229 tg3_flag_set(tp, ENABLE_APE);
Matt Carlsonb2b98d42008-11-03 16:52:32 -080013230
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013231 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070013232 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
Joe Perches63c3a662011-04-26 08:12:10 +000013233 tg3_flag_clear(tp, WOL_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013234
Joe Perches63c3a662011-04-26 08:12:10 +000013235 if (tg3_flag(tp, WOL_CAP) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013236 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013237 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013238 device_set_wakeup_enable(&tp->pdev->dev, true);
13239 }
Matt Carlson0527ba32007-10-10 18:03:30 -070013240
Linus Torvalds1da177e2005-04-16 15:20:36 -070013241 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013242 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013243
13244 /* serdes signal pre-emphasis in register 0x590 set by */
13245 /* bootcode if bit 18 is set */
13246 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013247 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070013248
Joe Perches63c3a662011-04-26 08:12:10 +000013249 if ((tg3_flag(tp, 57765_PLUS) ||
13250 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
13251 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080013252 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013253 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080013254
Joe Perches63c3a662011-04-26 08:12:10 +000013255 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson8c69b1e2010-08-02 11:26:00 +000013256 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +000013257 !tg3_flag(tp, 57765_PLUS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070013258 u32 cfg3;
13259
13260 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
13261 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
Joe Perches63c3a662011-04-26 08:12:10 +000013262 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson8ed5d972007-05-07 00:25:49 -070013263 }
Matt Carlsona9daf362008-05-25 23:49:44 -070013264
Matt Carlson14417062010-02-17 15:16:59 +000013265 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
Joe Perches63c3a662011-04-26 08:12:10 +000013266 tg3_flag_set(tp, RGMII_INBAND_DISABLE);
Matt Carlsona9daf362008-05-25 23:49:44 -070013267 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000013268 tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
Matt Carlsona9daf362008-05-25 23:49:44 -070013269 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000013270 tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013271 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080013272done:
Joe Perches63c3a662011-04-26 08:12:10 +000013273 if (tg3_flag(tp, WOL_CAP))
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000013274 device_set_wakeup_enable(&tp->pdev->dev,
Joe Perches63c3a662011-04-26 08:12:10 +000013275 tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000013276 else
13277 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070013278}
13279
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013280static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
13281{
13282 int i;
13283 u32 val;
13284
13285 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
13286 tw32(OTP_CTRL, cmd);
13287
13288 /* Wait for up to 1 ms for command to execute. */
13289 for (i = 0; i < 100; i++) {
13290 val = tr32(OTP_STATUS);
13291 if (val & OTP_STATUS_CMD_DONE)
13292 break;
13293 udelay(10);
13294 }
13295
13296 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
13297}
13298
13299/* Read the gphy configuration from the OTP region of the chip. The gphy
13300 * configuration is a 32-bit value that straddles the alignment boundary.
13301 * We do two 32-bit reads and then shift and merge the results.
13302 */
13303static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
13304{
13305 u32 bhalf_otp, thalf_otp;
13306
13307 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
13308
13309 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
13310 return 0;
13311
13312 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
13313
13314 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
13315 return 0;
13316
13317 thalf_otp = tr32(OTP_READ_DATA);
13318
13319 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
13320
13321 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
13322 return 0;
13323
13324 bhalf_otp = tr32(OTP_READ_DATA);
13325
13326 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
13327}
13328
Matt Carlsone256f8a2011-03-09 16:58:24 +000013329static void __devinit tg3_phy_init_link_config(struct tg3 *tp)
13330{
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +000013331 u32 adv = ADVERTISED_Autoneg;
Matt Carlsone256f8a2011-03-09 16:58:24 +000013332
13333 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
13334 adv |= ADVERTISED_1000baseT_Half |
13335 ADVERTISED_1000baseT_Full;
13336
13337 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
13338 adv |= ADVERTISED_100baseT_Half |
13339 ADVERTISED_100baseT_Full |
13340 ADVERTISED_10baseT_Half |
13341 ADVERTISED_10baseT_Full |
13342 ADVERTISED_TP;
13343 else
13344 adv |= ADVERTISED_FIBRE;
13345
13346 tp->link_config.advertising = adv;
Matt Carlsone7405222012-02-13 15:20:16 +000013347 tp->link_config.speed = SPEED_UNKNOWN;
13348 tp->link_config.duplex = DUPLEX_UNKNOWN;
Matt Carlsone256f8a2011-03-09 16:58:24 +000013349 tp->link_config.autoneg = AUTONEG_ENABLE;
Matt Carlsone7405222012-02-13 15:20:16 +000013350 tp->link_config.active_speed = SPEED_UNKNOWN;
13351 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Matt Carlson34655ad2012-02-22 12:35:18 +000013352
13353 tp->old_link = -1;
Matt Carlsone256f8a2011-03-09 16:58:24 +000013354}
13355
Michael Chan7d0c41e2005-04-21 17:06:20 -070013356static int __devinit tg3_phy_probe(struct tg3 *tp)
13357{
13358 u32 hw_phy_id_1, hw_phy_id_2;
13359 u32 hw_phy_id, hw_phy_id_masked;
13360 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013361
Matt Carlsone256f8a2011-03-09 16:58:24 +000013362 /* flow control autonegotiation is default behavior */
Joe Perches63c3a662011-04-26 08:12:10 +000013363 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsone256f8a2011-03-09 16:58:24 +000013364 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
13365
Joe Perches63c3a662011-04-26 08:12:10 +000013366 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013367 return tg3_phy_init(tp);
13368
Linus Torvalds1da177e2005-04-16 15:20:36 -070013369 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010013370 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013371 */
13372 err = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000013373 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000013374 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013375 } else {
13376 /* Now read the physical PHY_ID from the chip and verify
13377 * that it is sane. If it doesn't look good, we fall back
13378 * to either the hard-coded table based PHY_ID and failing
13379 * that the value found in the eeprom area.
13380 */
13381 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
13382 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
13383
13384 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
13385 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
13386 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
13387
Matt Carlson79eb6902010-02-17 15:17:03 +000013388 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013389 }
13390
Matt Carlson79eb6902010-02-17 15:17:03 +000013391 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013392 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000013393 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013394 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070013395 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013396 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013397 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000013398 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070013399 /* Do nothing, phy ID already set up in
13400 * tg3_get_eeprom_hw_cfg().
13401 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070013402 } else {
13403 struct subsys_tbl_ent *p;
13404
13405 /* No eeprom signature? Try the hardcoded
13406 * subsys device table.
13407 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013408 p = tg3_lookup_by_subsys(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013409 if (!p)
13410 return -ENODEV;
13411
13412 tp->phy_id = p->phy_id;
13413 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000013414 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013415 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013416 }
13417 }
13418
Matt Carlsona6b68da2010-12-06 08:28:52 +000013419 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Matt Carlson5baa5e92011-07-20 10:20:53 +000013420 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13421 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720 ||
13422 (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +000013423 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0) ||
13424 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
13425 tp->pci_chip_rev_id != CHIPREV_ID_57765_A0)))
Matt Carlson52b02d02010-10-14 10:37:41 +000013426 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
13427
Matt Carlsone256f8a2011-03-09 16:58:24 +000013428 tg3_phy_init_link_config(tp);
13429
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013430 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013431 !tg3_flag(tp, ENABLE_APE) &&
13432 !tg3_flag(tp, ENABLE_ASF)) {
Matt Carlsone2bf73e2011-12-08 14:40:15 +000013433 u32 bmsr, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013434
13435 tg3_readphy(tp, MII_BMSR, &bmsr);
13436 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
13437 (bmsr & BMSR_LSTATUS))
13438 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013439
Linus Torvalds1da177e2005-04-16 15:20:36 -070013440 err = tg3_phy_reset(tp);
13441 if (err)
13442 return err;
13443
Matt Carlson42b64a42011-05-19 12:12:49 +000013444 tg3_phy_set_wirespeed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013445
Matt Carlsone2bf73e2011-12-08 14:40:15 +000013446 if (!tg3_phy_copper_an_config_ok(tp, &dummy)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000013447 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
13448 tp->link_config.flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013449
13450 tg3_writephy(tp, MII_BMCR,
13451 BMCR_ANENABLE | BMCR_ANRESTART);
13452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013453 }
13454
13455skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000013456 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013457 err = tg3_init_5401phy_dsp(tp);
13458 if (err)
13459 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013460
Linus Torvalds1da177e2005-04-16 15:20:36 -070013461 err = tg3_init_5401phy_dsp(tp);
13462 }
13463
Linus Torvalds1da177e2005-04-16 15:20:36 -070013464 return err;
13465}
13466
Matt Carlson184b8902010-04-05 10:19:25 +000013467static void __devinit tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013468{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013469 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013470 unsigned int block_end, rosize, len;
Matt Carlson535a4902011-07-20 10:20:56 +000013471 u32 vpdlen;
Matt Carlson184b8902010-04-05 10:19:25 +000013472 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013473
Matt Carlson535a4902011-07-20 10:20:56 +000013474 vpd_data = (u8 *)tg3_vpd_readblock(tp, &vpdlen);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013475 if (!vpd_data)
13476 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013477
Matt Carlson535a4902011-07-20 10:20:56 +000013478 i = pci_vpd_find_tag(vpd_data, 0, vpdlen, PCI_VPD_LRDT_RO_DATA);
Matt Carlson4181b2c2010-02-26 14:04:45 +000013479 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013480 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013481
13482 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
13483 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
13484 i += PCI_VPD_LRDT_TAG_SIZE;
13485
Matt Carlson535a4902011-07-20 10:20:56 +000013486 if (block_end > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000013487 goto out_not_found;
13488
Matt Carlson184b8902010-04-05 10:19:25 +000013489 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13490 PCI_VPD_RO_KEYWORD_MFR_ID);
13491 if (j > 0) {
13492 len = pci_vpd_info_field_size(&vpd_data[j]);
13493
13494 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13495 if (j + len > block_end || len != 4 ||
13496 memcmp(&vpd_data[j], "1028", 4))
13497 goto partno;
13498
13499 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13500 PCI_VPD_RO_KEYWORD_VENDOR0);
13501 if (j < 0)
13502 goto partno;
13503
13504 len = pci_vpd_info_field_size(&vpd_data[j]);
13505
13506 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13507 if (j + len > block_end)
13508 goto partno;
13509
13510 memcpy(tp->fw_ver, &vpd_data[j], len);
Matt Carlson535a4902011-07-20 10:20:56 +000013511 strncat(tp->fw_ver, " bc ", vpdlen - len - 1);
Matt Carlson184b8902010-04-05 10:19:25 +000013512 }
13513
13514partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000013515 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13516 PCI_VPD_RO_KEYWORD_PARTNO);
13517 if (i < 0)
13518 goto out_not_found;
13519
13520 len = pci_vpd_info_field_size(&vpd_data[i]);
13521
13522 i += PCI_VPD_INFO_FLD_HDR_SIZE;
13523 if (len > TG3_BPN_SIZE ||
Matt Carlson535a4902011-07-20 10:20:56 +000013524 (len + i) > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000013525 goto out_not_found;
13526
13527 memcpy(tp->board_part_number, &vpd_data[i], len);
13528
Linus Torvalds1da177e2005-04-16 15:20:36 -070013529out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013530 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000013531 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013532 return;
13533
13534out_no_vpd:
Matt Carlson37a949c2010-09-30 10:34:33 +000013535 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
13536 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717)
13537 strcpy(tp->board_part_number, "BCM5717");
13538 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
13539 strcpy(tp->board_part_number, "BCM5718");
13540 else
13541 goto nomatch;
13542 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
13543 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
13544 strcpy(tp->board_part_number, "BCM57780");
13545 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
13546 strcpy(tp->board_part_number, "BCM57760");
13547 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
13548 strcpy(tp->board_part_number, "BCM57790");
13549 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
13550 strcpy(tp->board_part_number, "BCM57788");
13551 else
13552 goto nomatch;
13553 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
13554 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
13555 strcpy(tp->board_part_number, "BCM57761");
13556 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
13557 strcpy(tp->board_part_number, "BCM57765");
13558 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
13559 strcpy(tp->board_part_number, "BCM57781");
13560 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
13561 strcpy(tp->board_part_number, "BCM57785");
13562 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
13563 strcpy(tp->board_part_number, "BCM57791");
13564 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13565 strcpy(tp->board_part_number, "BCM57795");
13566 else
13567 goto nomatch;
Matt Carlson55086ad2011-12-14 11:09:59 +000013568 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57766) {
13569 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762)
13570 strcpy(tp->board_part_number, "BCM57762");
13571 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766)
13572 strcpy(tp->board_part_number, "BCM57766");
13573 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782)
13574 strcpy(tp->board_part_number, "BCM57782");
13575 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
13576 strcpy(tp->board_part_number, "BCM57786");
13577 else
13578 goto nomatch;
Matt Carlson37a949c2010-09-30 10:34:33 +000013579 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070013580 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000013581 } else {
13582nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070013583 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000013584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013585}
13586
Matt Carlson9c8a6202007-10-21 16:16:08 -070013587static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
13588{
13589 u32 val;
13590
Matt Carlsone4f34112009-02-25 14:25:00 +000013591 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013592 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013593 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013594 val != 0)
13595 return 0;
13596
13597 return 1;
13598}
13599
Matt Carlsonacd9c112009-02-25 14:26:33 +000013600static void __devinit tg3_read_bc_ver(struct tg3 *tp)
13601{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013602 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000013603 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013604 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013605
13606 if (tg3_nvram_read(tp, 0xc, &offset) ||
13607 tg3_nvram_read(tp, 0x4, &start))
13608 return;
13609
13610 offset = tg3_nvram_logical_addr(tp, offset);
13611
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013612 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013613 return;
13614
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013615 if ((val & 0xfc000000) == 0x0c000000) {
13616 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013617 return;
13618
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013619 if (val == 0)
13620 newver = true;
13621 }
13622
Matt Carlson75f99362010-04-05 10:19:24 +000013623 dst_off = strlen(tp->fw_ver);
13624
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013625 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000013626 if (TG3_VER_SIZE - dst_off < 16 ||
13627 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013628 return;
13629
13630 offset = offset + ver_offset - start;
13631 for (i = 0; i < 16; i += 4) {
13632 __be32 v;
13633 if (tg3_nvram_read_be32(tp, offset + i, &v))
13634 return;
13635
Matt Carlson75f99362010-04-05 10:19:24 +000013636 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013637 }
13638 } else {
13639 u32 major, minor;
13640
13641 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
13642 return;
13643
13644 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
13645 TG3_NVM_BCVER_MAJSFT;
13646 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000013647 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
13648 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013649 }
13650}
13651
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013652static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
13653{
13654 u32 val, major, minor;
13655
13656 /* Use native endian representation */
13657 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
13658 return;
13659
13660 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
13661 TG3_NVM_HWSB_CFG1_MAJSFT;
13662 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
13663 TG3_NVM_HWSB_CFG1_MINSFT;
13664
13665 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
13666}
13667
Matt Carlsondfe00d72008-11-21 17:19:41 -080013668static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
13669{
13670 u32 offset, major, minor, build;
13671
Matt Carlson75f99362010-04-05 10:19:24 +000013672 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013673
13674 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
13675 return;
13676
13677 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
13678 case TG3_EEPROM_SB_REVISION_0:
13679 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
13680 break;
13681 case TG3_EEPROM_SB_REVISION_2:
13682 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
13683 break;
13684 case TG3_EEPROM_SB_REVISION_3:
13685 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
13686 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000013687 case TG3_EEPROM_SB_REVISION_4:
13688 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
13689 break;
13690 case TG3_EEPROM_SB_REVISION_5:
13691 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
13692 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000013693 case TG3_EEPROM_SB_REVISION_6:
13694 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
13695 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013696 default:
13697 return;
13698 }
13699
Matt Carlsone4f34112009-02-25 14:25:00 +000013700 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080013701 return;
13702
13703 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
13704 TG3_EEPROM_SB_EDH_BLD_SHFT;
13705 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
13706 TG3_EEPROM_SB_EDH_MAJ_SHFT;
13707 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
13708
13709 if (minor > 99 || build > 26)
13710 return;
13711
Matt Carlson75f99362010-04-05 10:19:24 +000013712 offset = strlen(tp->fw_ver);
13713 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
13714 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013715
13716 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000013717 offset = strlen(tp->fw_ver);
13718 if (offset < TG3_VER_SIZE - 1)
13719 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013720 }
13721}
13722
Matt Carlsonacd9c112009-02-25 14:26:33 +000013723static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080013724{
13725 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013726 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070013727
13728 for (offset = TG3_NVM_DIR_START;
13729 offset < TG3_NVM_DIR_END;
13730 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000013731 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013732 return;
13733
13734 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
13735 break;
13736 }
13737
13738 if (offset == TG3_NVM_DIR_END)
13739 return;
13740
Joe Perches63c3a662011-04-26 08:12:10 +000013741 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013742 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000013743 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013744 return;
13745
Matt Carlsone4f34112009-02-25 14:25:00 +000013746 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013747 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013748 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013749 return;
13750
13751 offset += val - start;
13752
Matt Carlsonacd9c112009-02-25 14:26:33 +000013753 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013754
Matt Carlsonacd9c112009-02-25 14:26:33 +000013755 tp->fw_ver[vlen++] = ',';
13756 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070013757
13758 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000013759 __be32 v;
13760 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013761 return;
13762
Al Virob9fc7dc2007-12-17 22:59:57 -080013763 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013764
Matt Carlsonacd9c112009-02-25 14:26:33 +000013765 if (vlen > TG3_VER_SIZE - sizeof(v)) {
13766 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013767 break;
13768 }
13769
Matt Carlsonacd9c112009-02-25 14:26:33 +000013770 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
13771 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013772 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000013773}
13774
Matt Carlson7fd76442009-02-25 14:27:20 +000013775static void __devinit tg3_read_dash_ver(struct tg3 *tp)
13776{
13777 int vlen;
13778 u32 apedata;
Matt Carlsonecc79642010-08-02 11:26:01 +000013779 char *fwtype;
Matt Carlson7fd76442009-02-25 14:27:20 +000013780
Joe Perches63c3a662011-04-26 08:12:10 +000013781 if (!tg3_flag(tp, ENABLE_APE) || !tg3_flag(tp, ENABLE_ASF))
Matt Carlson7fd76442009-02-25 14:27:20 +000013782 return;
13783
13784 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
13785 if (apedata != APE_SEG_SIG_MAGIC)
13786 return;
13787
13788 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
13789 if (!(apedata & APE_FW_STATUS_READY))
13790 return;
13791
13792 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
13793
Matt Carlsondc6d0742010-09-15 08:59:55 +000013794 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI) {
Joe Perches63c3a662011-04-26 08:12:10 +000013795 tg3_flag_set(tp, APE_HAS_NCSI);
Matt Carlsonecc79642010-08-02 11:26:01 +000013796 fwtype = "NCSI";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013797 } else {
Matt Carlsonecc79642010-08-02 11:26:01 +000013798 fwtype = "DASH";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013799 }
Matt Carlsonecc79642010-08-02 11:26:01 +000013800
Matt Carlson7fd76442009-02-25 14:27:20 +000013801 vlen = strlen(tp->fw_ver);
13802
Matt Carlsonecc79642010-08-02 11:26:01 +000013803 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
13804 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000013805 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
13806 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
13807 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
13808 (apedata & APE_FW_VERSION_BLDMSK));
13809}
13810
Matt Carlsonacd9c112009-02-25 14:26:33 +000013811static void __devinit tg3_read_fw_ver(struct tg3 *tp)
13812{
13813 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000013814 bool vpd_vers = false;
13815
13816 if (tp->fw_ver[0] != 0)
13817 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013818
Joe Perches63c3a662011-04-26 08:12:10 +000013819 if (tg3_flag(tp, NO_NVRAM)) {
Matt Carlson75f99362010-04-05 10:19:24 +000013820 strcat(tp->fw_ver, "sb");
Matt Carlsondf259d82009-04-20 06:57:14 +000013821 return;
13822 }
13823
Matt Carlsonacd9c112009-02-25 14:26:33 +000013824 if (tg3_nvram_read(tp, 0, &val))
13825 return;
13826
13827 if (val == TG3_EEPROM_MAGIC)
13828 tg3_read_bc_ver(tp);
13829 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
13830 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013831 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
13832 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013833 else
13834 return;
13835
Matt Carlsonc9cab242011-07-13 09:27:27 +000013836 if (vpd_vers)
Matt Carlson75f99362010-04-05 10:19:24 +000013837 goto done;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013838
Matt Carlsonc9cab242011-07-13 09:27:27 +000013839 if (tg3_flag(tp, ENABLE_APE)) {
13840 if (tg3_flag(tp, ENABLE_ASF))
13841 tg3_read_dash_ver(tp);
13842 } else if (tg3_flag(tp, ENABLE_ASF)) {
13843 tg3_read_mgmtfw_ver(tp);
13844 }
Matt Carlson9c8a6202007-10-21 16:16:08 -070013845
Matt Carlson75f99362010-04-05 10:19:24 +000013846done:
Matt Carlson9c8a6202007-10-21 16:16:08 -070013847 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080013848}
13849
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013850static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
13851{
Joe Perches63c3a662011-04-26 08:12:10 +000013852 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013853 return TG3_RX_RET_MAX_SIZE_5717;
Joe Perches63c3a662011-04-26 08:12:10 +000013854 else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013855 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013856 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000013857 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013858}
13859
Matt Carlson41434702011-03-09 16:58:22 +000013860static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080013861 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
13862 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
13863 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
13864 { },
13865};
13866
Matt Carlson16c7fa72012-02-13 10:20:10 +000013867static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
13868{
13869 struct pci_dev *peer;
13870 unsigned int func, devnr = tp->pdev->devfn & ~7;
13871
13872 for (func = 0; func < 8; func++) {
13873 peer = pci_get_slot(tp->pdev->bus, devnr | func);
13874 if (peer && peer != tp->pdev)
13875 break;
13876 pci_dev_put(peer);
13877 }
13878 /* 5704 can be configured in single-port mode, set peer to
13879 * tp->pdev in that case.
13880 */
13881 if (!peer) {
13882 peer = tp->pdev;
13883 return peer;
13884 }
13885
13886 /*
13887 * We don't need to keep the refcount elevated; there's no way
13888 * to remove one half of this device without removing the other
13889 */
13890 pci_dev_put(peer);
13891
13892 return peer;
13893}
13894
Matt Carlson42b123b2012-02-13 15:20:13 +000013895static void __devinit tg3_detect_asic_rev(struct tg3 *tp, u32 misc_ctrl_reg)
13896{
13897 tp->pci_chip_rev_id = misc_ctrl_reg >> MISC_HOST_CTRL_CHIPREV_SHIFT;
13898 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
13899 u32 reg;
13900
13901 /* All devices that use the alternate
13902 * ASIC REV location have a CPMU.
13903 */
13904 tg3_flag_set(tp, CPMU_PRESENT);
13905
13906 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
13907 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
13908 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
13909 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720)
13910 reg = TG3PCI_GEN2_PRODID_ASICREV;
13911 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
13912 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
13913 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
13914 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
13915 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
13916 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
13917 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762 ||
13918 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766 ||
13919 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782 ||
13920 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
13921 reg = TG3PCI_GEN15_PRODID_ASICREV;
13922 else
13923 reg = TG3PCI_PRODID_ASICREV;
13924
13925 pci_read_config_dword(tp->pdev, reg, &tp->pci_chip_rev_id);
13926 }
13927
13928 /* Wrong chip ID in 5752 A0. This code can be removed later
13929 * as A0 is not in production.
13930 */
13931 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
13932 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
13933
13934 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13935 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13936 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
13937 tg3_flag_set(tp, 5717_PLUS);
13938
13939 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 ||
13940 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57766)
13941 tg3_flag_set(tp, 57765_CLASS);
13942
13943 if (tg3_flag(tp, 57765_CLASS) || tg3_flag(tp, 5717_PLUS))
13944 tg3_flag_set(tp, 57765_PLUS);
13945
13946 /* Intentionally exclude ASIC_REV_5906 */
13947 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
13948 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
13949 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
13950 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
13951 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
13952 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
13953 tg3_flag(tp, 57765_PLUS))
13954 tg3_flag_set(tp, 5755_PLUS);
13955
13956 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
13957 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)
13958 tg3_flag_set(tp, 5780_CLASS);
13959
13960 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13961 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
13962 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
13963 tg3_flag(tp, 5755_PLUS) ||
13964 tg3_flag(tp, 5780_CLASS))
13965 tg3_flag_set(tp, 5750_PLUS);
13966
13967 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
13968 tg3_flag(tp, 5750_PLUS))
13969 tg3_flag_set(tp, 5705_PLUS);
13970}
13971
Linus Torvalds1da177e2005-04-16 15:20:36 -070013972static int __devinit tg3_get_invariants(struct tg3 *tp)
13973{
Linus Torvalds1da177e2005-04-16 15:20:36 -070013974 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013975 u32 pci_state_reg, grc_misc_cfg;
13976 u32 val;
13977 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013978 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013979
Linus Torvalds1da177e2005-04-16 15:20:36 -070013980 /* Force memory write invalidate off. If we leave it on,
13981 * then on 5700_BX chips we have to enable a workaround.
13982 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
13983 * to match the cacheline size. The Broadcom driver have this
13984 * workaround but turns MWI off all the times so never uses
13985 * it. This seems to suggest that the workaround is insufficient.
13986 */
13987 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13988 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
13989 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13990
Matt Carlson16821282011-07-13 09:27:28 +000013991 /* Important! -- Make sure register accesses are byteswapped
13992 * correctly. Also, for those chips that require it, make
13993 * sure that indirect register accesses are enabled before
13994 * the first operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013995 */
13996 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13997 &misc_ctrl_reg);
Matt Carlson16821282011-07-13 09:27:28 +000013998 tp->misc_host_ctrl |= (misc_ctrl_reg &
13999 MISC_HOST_CTRL_CHIPREV);
14000 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
14001 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014002
Matt Carlson42b123b2012-02-13 15:20:13 +000014003 tg3_detect_asic_rev(tp, misc_ctrl_reg);
Michael Chanff645be2005-04-21 17:09:53 -070014004
Michael Chan68929142005-08-09 20:17:14 -070014005 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
14006 * we need to disable memory and use config. cycles
14007 * only to access all registers. The 5702/03 chips
14008 * can mistakenly decode the special cycles from the
14009 * ICH chipsets as memory write cycles, causing corruption
14010 * of register and memory space. Only certain ICH bridges
14011 * will drive special cycles with non-zero data during the
14012 * address phase which can fall within the 5703's address
14013 * range. This is not an ICH bug as the PCI spec allows
14014 * non-zero address during special cycles. However, only
14015 * these ICH bridges are known to drive non-zero addresses
14016 * during special cycles.
14017 *
14018 * Since special cycles do not cross PCI bridges, we only
14019 * enable this workaround if the 5703 is on the secondary
14020 * bus of these ICH bridges.
14021 */
14022 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
14023 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
14024 static struct tg3_dev_id {
14025 u32 vendor;
14026 u32 device;
14027 u32 rev;
14028 } ich_chipsets[] = {
14029 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
14030 PCI_ANY_ID },
14031 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
14032 PCI_ANY_ID },
14033 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
14034 0xa },
14035 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
14036 PCI_ANY_ID },
14037 { },
14038 };
14039 struct tg3_dev_id *pci_id = &ich_chipsets[0];
14040 struct pci_dev *bridge = NULL;
14041
14042 while (pci_id->vendor != 0) {
14043 bridge = pci_get_device(pci_id->vendor, pci_id->device,
14044 bridge);
14045 if (!bridge) {
14046 pci_id++;
14047 continue;
14048 }
14049 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070014050 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070014051 continue;
14052 }
14053 if (bridge->subordinate &&
14054 (bridge->subordinate->number ==
14055 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014056 tg3_flag_set(tp, ICH_WORKAROUND);
Michael Chan68929142005-08-09 20:17:14 -070014057 pci_dev_put(bridge);
14058 break;
14059 }
14060 }
14061 }
14062
Matt Carlson6ff6f812011-05-19 12:12:54 +000014063 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Matt Carlson41588ba2008-04-19 18:12:33 -070014064 static struct tg3_dev_id {
14065 u32 vendor;
14066 u32 device;
14067 } bridge_chipsets[] = {
14068 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
14069 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
14070 { },
14071 };
14072 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
14073 struct pci_dev *bridge = NULL;
14074
14075 while (pci_id->vendor != 0) {
14076 bridge = pci_get_device(pci_id->vendor,
14077 pci_id->device,
14078 bridge);
14079 if (!bridge) {
14080 pci_id++;
14081 continue;
14082 }
14083 if (bridge->subordinate &&
14084 (bridge->subordinate->number <=
14085 tp->pdev->bus->number) &&
14086 (bridge->subordinate->subordinate >=
14087 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014088 tg3_flag_set(tp, 5701_DMA_BUG);
Matt Carlson41588ba2008-04-19 18:12:33 -070014089 pci_dev_put(bridge);
14090 break;
14091 }
14092 }
14093 }
14094
Michael Chan4a29cc22006-03-19 13:21:12 -080014095 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
14096 * DMA addresses > 40-bit. This bridge may have other additional
14097 * 57xx devices behind it in some 4-port NIC designs for example.
14098 * Any tg3 device found behind the bridge will also need the 40-bit
14099 * DMA workaround.
14100 */
Matt Carlson42b123b2012-02-13 15:20:13 +000014101 if (tg3_flag(tp, 5780_CLASS)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014102 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4cf78e42005-07-25 12:29:19 -070014103 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a588792010-04-05 10:19:28 +000014104 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080014105 struct pci_dev *bridge = NULL;
14106
14107 do {
14108 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
14109 PCI_DEVICE_ID_SERVERWORKS_EPB,
14110 bridge);
14111 if (bridge && bridge->subordinate &&
14112 (bridge->subordinate->number <=
14113 tp->pdev->bus->number) &&
14114 (bridge->subordinate->subordinate >=
14115 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014116 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4a29cc22006-03-19 13:21:12 -080014117 pci_dev_put(bridge);
14118 break;
14119 }
14120 } while (bridge);
14121 }
Michael Chan4cf78e42005-07-25 12:29:19 -070014122
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014123 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Matt Carlson3a1e19d2011-07-13 09:27:32 +000014124 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)
Michael Chan7544b092007-05-05 13:08:32 -070014125 tp->pdev_peer = tg3_find_peer(tp);
14126
Matt Carlson507399f2009-11-13 13:03:37 +000014127 /* Determine TSO capabilities */
Matt Carlsona0512942011-07-27 14:20:54 +000014128 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0)
Matt Carlson4d163b72011-01-25 15:58:48 +000014129 ; /* Do nothing. HW bug. */
Joe Perches63c3a662011-04-26 08:12:10 +000014130 else if (tg3_flag(tp, 57765_PLUS))
14131 tg3_flag_set(tp, HW_TSO_3);
14132 else if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000014133 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000014134 tg3_flag_set(tp, HW_TSO_2);
14135 else if (tg3_flag(tp, 5750_PLUS)) {
14136 tg3_flag_set(tp, HW_TSO_1);
14137 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000014138 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
14139 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
Joe Perches63c3a662011-04-26 08:12:10 +000014140 tg3_flag_clear(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000014141 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14142 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
14143 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000014144 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000014145 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
14146 tp->fw_needed = FIRMWARE_TG3TSO5;
14147 else
14148 tp->fw_needed = FIRMWARE_TG3TSO;
14149 }
14150
Matt Carlsondabc5c62011-05-19 12:12:52 +000014151 /* Selectively allow TSO based on operating conditions */
Matt Carlson6ff6f812011-05-19 12:12:54 +000014152 if (tg3_flag(tp, HW_TSO_1) ||
14153 tg3_flag(tp, HW_TSO_2) ||
14154 tg3_flag(tp, HW_TSO_3) ||
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000014155 tp->fw_needed) {
14156 /* For firmware TSO, assume ASF is disabled.
14157 * We'll disable TSO later if we discover ASF
14158 * is enabled in tg3_get_eeprom_hw_cfg().
14159 */
Matt Carlsondabc5c62011-05-19 12:12:52 +000014160 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000014161 } else {
Matt Carlsondabc5c62011-05-19 12:12:52 +000014162 tg3_flag_clear(tp, TSO_CAPABLE);
14163 tg3_flag_clear(tp, TSO_BUG);
14164 tp->fw_needed = NULL;
14165 }
14166
14167 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
14168 tp->fw_needed = FIRMWARE_TG3;
14169
Matt Carlson507399f2009-11-13 13:03:37 +000014170 tp->irq_max = 1;
14171
Joe Perches63c3a662011-04-26 08:12:10 +000014172 if (tg3_flag(tp, 5750_PLUS)) {
14173 tg3_flag_set(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070014174 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
14175 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
14176 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
14177 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
14178 tp->pdev_peer == tp->pdev))
Joe Perches63c3a662011-04-26 08:12:10 +000014179 tg3_flag_clear(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070014180
Joe Perches63c3a662011-04-26 08:12:10 +000014181 if (tg3_flag(tp, 5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070014182 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000014183 tg3_flag_set(tp, 1SHOT_MSI);
Michael Chan52c0fd82006-06-29 20:15:54 -070014184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014185
Joe Perches63c3a662011-04-26 08:12:10 +000014186 if (tg3_flag(tp, 57765_PLUS)) {
14187 tg3_flag_set(tp, SUPPORT_MSIX);
Matt Carlson507399f2009-11-13 13:03:37 +000014188 tp->irq_max = TG3_IRQ_MAX_VECS;
Matt Carlson90415472011-12-16 13:33:23 +000014189 tg3_rss_init_dflt_indir_tbl(tp);
Matt Carlson507399f2009-11-13 13:03:37 +000014190 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014191 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000014192
Matt Carlson2ffcc982011-05-19 12:12:44 +000014193 if (tg3_flag(tp, 5755_PLUS))
Joe Perches63c3a662011-04-26 08:12:10 +000014194 tg3_flag_set(tp, SHORT_DMA_BUG);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014195
Matt Carlsone31aa982011-07-27 14:20:53 +000014196 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona4cb4282011-12-14 11:09:58 +000014197 tp->dma_limit = TG3_TX_BD_DMA_MAX_4K;
Matt Carlsone31aa982011-07-27 14:20:53 +000014198
Matt Carlsonfa6b2aa2011-11-21 15:01:19 +000014199 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14200 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
14201 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Joe Perches63c3a662011-04-26 08:12:10 +000014202 tg3_flag_set(tp, LRG_PROD_RING_CAP);
Matt Carlsonde9f5232011-04-05 14:22:43 +000014203
Joe Perches63c3a662011-04-26 08:12:10 +000014204 if (tg3_flag(tp, 57765_PLUS) &&
Matt Carlsona0512942011-07-27 14:20:54 +000014205 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0)
Joe Perches63c3a662011-04-26 08:12:10 +000014206 tg3_flag_set(tp, USE_JUMBO_BDFLAG);
Matt Carlsonb703df62009-12-03 08:36:21 +000014207
Joe Perches63c3a662011-04-26 08:12:10 +000014208 if (!tg3_flag(tp, 5705_PLUS) ||
14209 tg3_flag(tp, 5780_CLASS) ||
14210 tg3_flag(tp, USE_JUMBO_BDFLAG))
14211 tg3_flag_set(tp, JUMBO_CAPABLE);
Michael Chan0f893dc2005-07-25 12:30:38 -070014212
Matt Carlson52f44902008-11-21 17:17:04 -080014213 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14214 &pci_state_reg);
14215
Jon Mason708ebb3a2011-06-27 12:56:50 +000014216 if (pci_is_pcie(tp->pdev)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014217 u16 lnkctl;
14218
Joe Perches63c3a662011-04-26 08:12:10 +000014219 tg3_flag_set(tp, PCI_EXPRESS);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080014220
Matt Carlson2c55a3d2011-11-28 09:41:04 +000014221 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0) {
14222 int readrq = pcie_get_readrq(tp->pdev);
14223 if (readrq > 2048)
14224 pcie_set_readrq(tp->pdev, 2048);
14225 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -080014226
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014227 pci_read_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +000014228 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014229 &lnkctl);
14230 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
Matt Carlson7196cd62011-05-19 16:02:44 +000014231 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
14232 ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000014233 tg3_flag_clear(tp, HW_TSO_2);
Matt Carlsondabc5c62011-05-19 12:12:52 +000014234 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson7196cd62011-05-19 16:02:44 +000014235 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014236 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014237 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000014238 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
14239 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Joe Perches63c3a662011-04-26 08:12:10 +000014240 tg3_flag_set(tp, CLKREQ_BUG);
Matt Carlson614b0592010-01-20 16:58:02 +000014241 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000014242 tg3_flag_set(tp, L1PLLPD_EN);
Michael Chanc7835a72006-11-15 21:14:42 -080014243 }
Matt Carlson52f44902008-11-21 17:17:04 -080014244 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Jon Mason708ebb3a2011-06-27 12:56:50 +000014245 /* BCM5785 devices are effectively PCIe devices, and should
14246 * follow PCIe codepaths, but do not have a PCIe capabilities
14247 * section.
Matt Carlson93a700a2011-08-31 11:44:54 +000014248 */
Joe Perches63c3a662011-04-26 08:12:10 +000014249 tg3_flag_set(tp, PCI_EXPRESS);
14250 } else if (!tg3_flag(tp, 5705_PLUS) ||
14251 tg3_flag(tp, 5780_CLASS)) {
Matt Carlson52f44902008-11-21 17:17:04 -080014252 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
14253 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000014254 dev_err(&tp->pdev->dev,
14255 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080014256 return -EIO;
14257 }
14258
14259 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
Joe Perches63c3a662011-04-26 08:12:10 +000014260 tg3_flag_set(tp, PCIX_MODE);
Matt Carlson52f44902008-11-21 17:17:04 -080014261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014262
Michael Chan399de502005-10-03 14:02:39 -070014263 /* If we have an AMD 762 or VIA K8T800 chipset, write
14264 * reordering to the mailbox registers done by the host
14265 * controller can cause major troubles. We read back from
14266 * every mailbox register write to force the writes to be
14267 * posted to the chip in order.
14268 */
Matt Carlson41434702011-03-09 16:58:22 +000014269 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Joe Perches63c3a662011-04-26 08:12:10 +000014270 !tg3_flag(tp, PCI_EXPRESS))
14271 tg3_flag_set(tp, MBOX_WRITE_REORDER);
Michael Chan399de502005-10-03 14:02:39 -070014272
Matt Carlson69fc4052008-12-21 20:19:57 -080014273 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
14274 &tp->pci_cacheline_sz);
14275 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
14276 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014277 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14278 tp->pci_lat_timer < 64) {
14279 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080014280 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
14281 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014282 }
14283
Matt Carlson16821282011-07-13 09:27:28 +000014284 /* Important! -- It is critical that the PCI-X hw workaround
14285 * situation is decided before the first MMIO register access.
14286 */
Matt Carlson52f44902008-11-21 17:17:04 -080014287 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
14288 /* 5700 BX chips need to have their TX producer index
14289 * mailboxes written twice to workaround a bug.
14290 */
Joe Perches63c3a662011-04-26 08:12:10 +000014291 tg3_flag_set(tp, TXD_MBOX_HWBUG);
Matt Carlson9974a352007-10-07 23:27:28 -070014292
Matt Carlson52f44902008-11-21 17:17:04 -080014293 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014294 *
14295 * The workaround is to use indirect register accesses
14296 * for all chip writes not to mailbox registers.
14297 */
Joe Perches63c3a662011-04-26 08:12:10 +000014298 if (tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014299 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014300
Joe Perches63c3a662011-04-26 08:12:10 +000014301 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014302
14303 /* The chip can have it's power management PCI config
14304 * space registers clobbered due to this bug.
14305 * So explicitly force the chip into D0 here.
14306 */
Matt Carlson9974a352007-10-07 23:27:28 -070014307 pci_read_config_dword(tp->pdev,
14308 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014309 &pm_reg);
14310 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
14311 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070014312 pci_write_config_dword(tp->pdev,
14313 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014314 pm_reg);
14315
14316 /* Also, force SERR#/PERR# in PCI command. */
14317 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
14318 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
14319 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
14320 }
14321 }
14322
Linus Torvalds1da177e2005-04-16 15:20:36 -070014323 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000014324 tg3_flag_set(tp, PCI_HIGH_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014325 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000014326 tg3_flag_set(tp, PCI_32BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014327
14328 /* Chip-specific fixup from Broadcom driver */
14329 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
14330 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
14331 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
14332 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
14333 }
14334
Michael Chan1ee582d2005-08-09 20:16:46 -070014335 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070014336 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070014337 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070014338 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070014339 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070014340 tp->write32_tx_mbox = tg3_write32;
14341 tp->write32_rx_mbox = tg3_write32;
14342
14343 /* Various workaround register access methods */
Joe Perches63c3a662011-04-26 08:12:10 +000014344 if (tg3_flag(tp, PCIX_TARGET_HWBUG))
Michael Chan1ee582d2005-08-09 20:16:46 -070014345 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070014346 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014347 (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson98efd8a2007-05-05 12:47:25 -070014348 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
14349 /*
14350 * Back to back register writes can cause problems on these
14351 * chips, the workaround is to read back all reg writes
14352 * except those to mailbox regs.
14353 *
14354 * See tg3_write_indirect_reg32().
14355 */
Michael Chan1ee582d2005-08-09 20:16:46 -070014356 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070014357 }
14358
Joe Perches63c3a662011-04-26 08:12:10 +000014359 if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
Michael Chan1ee582d2005-08-09 20:16:46 -070014360 tp->write32_tx_mbox = tg3_write32_tx_mbox;
Joe Perches63c3a662011-04-26 08:12:10 +000014361 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Michael Chan1ee582d2005-08-09 20:16:46 -070014362 tp->write32_rx_mbox = tg3_write_flush_reg32;
14363 }
Michael Chan20094932005-08-09 20:16:32 -070014364
Joe Perches63c3a662011-04-26 08:12:10 +000014365 if (tg3_flag(tp, ICH_WORKAROUND)) {
Michael Chan68929142005-08-09 20:17:14 -070014366 tp->read32 = tg3_read_indirect_reg32;
14367 tp->write32 = tg3_write_indirect_reg32;
14368 tp->read32_mbox = tg3_read_indirect_mbox;
14369 tp->write32_mbox = tg3_write_indirect_mbox;
14370 tp->write32_tx_mbox = tg3_write_indirect_mbox;
14371 tp->write32_rx_mbox = tg3_write_indirect_mbox;
14372
14373 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070014374 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070014375
14376 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
14377 pci_cmd &= ~PCI_COMMAND_MEMORY;
14378 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
14379 }
Michael Chanb5d37722006-09-27 16:06:21 -070014380 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
14381 tp->read32_mbox = tg3_read32_mbox_5906;
14382 tp->write32_mbox = tg3_write32_mbox_5906;
14383 tp->write32_tx_mbox = tg3_write32_mbox_5906;
14384 tp->write32_rx_mbox = tg3_write32_mbox_5906;
14385 }
Michael Chan68929142005-08-09 20:17:14 -070014386
Michael Chanbbadf502006-04-06 21:46:34 -070014387 if (tp->write32 == tg3_write_indirect_reg32 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014388 (tg3_flag(tp, PCIX_MODE) &&
Michael Chanbbadf502006-04-06 21:46:34 -070014389 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070014390 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Joe Perches63c3a662011-04-26 08:12:10 +000014391 tg3_flag_set(tp, SRAM_USE_CONFIG);
Michael Chanbbadf502006-04-06 21:46:34 -070014392
Matt Carlson16821282011-07-13 09:27:28 +000014393 /* The memory arbiter has to be enabled in order for SRAM accesses
14394 * to succeed. Normally on powerup the tg3 chip firmware will make
14395 * sure it is enabled, but other entities such as system netboot
14396 * code might disable it.
14397 */
14398 val = tr32(MEMARB_MODE);
14399 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
14400
Matt Carlson9dc5e342011-11-04 09:15:02 +000014401 tp->pci_fn = PCI_FUNC(tp->pdev->devfn) & 3;
14402 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
14403 tg3_flag(tp, 5780_CLASS)) {
14404 if (tg3_flag(tp, PCIX_MODE)) {
14405 pci_read_config_dword(tp->pdev,
14406 tp->pcix_cap + PCI_X_STATUS,
14407 &val);
14408 tp->pci_fn = val & 0x7;
14409 }
14410 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
14411 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
14412 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) ==
14413 NIC_SRAM_CPMUSTAT_SIG) {
14414 tp->pci_fn = val & TG3_CPMU_STATUS_FMSK_5717;
14415 tp->pci_fn = tp->pci_fn ? 1 : 0;
14416 }
14417 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
14418 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
14419 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
14420 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) ==
14421 NIC_SRAM_CPMUSTAT_SIG) {
14422 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5719) >>
14423 TG3_CPMU_STATUS_FSHFT_5719;
14424 }
Matt Carlson69f11c92011-07-13 09:27:30 +000014425 }
14426
Michael Chan7d0c41e2005-04-21 17:06:20 -070014427 /* Get eeprom hw config before calling tg3_set_power_state().
Joe Perches63c3a662011-04-26 08:12:10 +000014428 * In particular, the TG3_FLAG_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070014429 * determined before calling tg3_set_power_state() so that
14430 * we know whether or not to switch out of Vaux power.
14431 * When the flag is set, it means that GPIO1 is used for eeprom
14432 * write protect and also implies that it is a LOM where GPIOs
14433 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040014434 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070014435 tg3_get_eeprom_hw_cfg(tp);
14436
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000014437 if (tp->fw_needed && tg3_flag(tp, ENABLE_ASF)) {
14438 tg3_flag_clear(tp, TSO_CAPABLE);
14439 tg3_flag_clear(tp, TSO_BUG);
14440 tp->fw_needed = NULL;
14441 }
14442
Joe Perches63c3a662011-04-26 08:12:10 +000014443 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070014444 /* Allow reads and writes to the
14445 * APE register and memory space.
14446 */
14447 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +000014448 PCISTATE_ALLOW_APE_SHMEM_WR |
14449 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070014450 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
14451 pci_state_reg);
Matt Carlsonc9cab242011-07-13 09:27:27 +000014452
14453 tg3_ape_lock_init(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070014454 }
14455
Matt Carlson16821282011-07-13 09:27:28 +000014456 /* Set up tp->grc_local_ctrl before calling
14457 * tg3_pwrsrc_switch_to_vmain(). GPIO1 driven high
14458 * will bring 5700's external PHY out of reset.
Michael Chan314fba32005-04-21 17:07:04 -070014459 * It is also used as eeprom write protect on LOMs.
14460 */
14461 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
Matt Carlson6ff6f812011-05-19 12:12:54 +000014462 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014463 tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan314fba32005-04-21 17:07:04 -070014464 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
14465 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070014466 /* Unused GPIO3 must be driven as output on 5752 because there
14467 * are no pull-up resistors on unused GPIO pins.
14468 */
14469 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
14470 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070014471
Matt Carlson321d32a2008-11-21 17:22:19 -080014472 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsoncb4ed1f2010-01-20 16:58:09 +000014473 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000014474 tg3_flag(tp, 57765_CLASS))
Michael Chanaf36e6b2006-03-23 01:28:06 -080014475 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
14476
Matt Carlson8d519ab2009-04-20 06:58:01 +000014477 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
14478 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070014479 /* Turn off the debug UART. */
14480 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014481 if (tg3_flag(tp, IS_NIC))
Matt Carlson5f0c4a32008-06-09 15:41:12 -070014482 /* Keep VMain power. */
14483 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
14484 GRC_LCLCTRL_GPIO_OUTPUT0;
14485 }
14486
Matt Carlson16821282011-07-13 09:27:28 +000014487 /* Switch out of Vaux if it is a NIC */
14488 tg3_pwrsrc_switch_to_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014489
Linus Torvalds1da177e2005-04-16 15:20:36 -070014490 /* Derive initial jumbo mode from MTU assigned in
14491 * ether_setup() via the alloc_etherdev() call
14492 */
Joe Perches63c3a662011-04-26 08:12:10 +000014493 if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
14494 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014495
14496 /* Determine WakeOnLan speed to use. */
14497 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14498 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
14499 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
14500 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
Joe Perches63c3a662011-04-26 08:12:10 +000014501 tg3_flag_clear(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014502 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000014503 tg3_flag_set(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014504 }
14505
Matt Carlson7f97a4b2009-08-25 10:10:03 +000014506 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014507 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000014508
Linus Torvalds1da177e2005-04-16 15:20:36 -070014509 /* A few boards don't want Ethernet@WireSpeed phy feature */
Matt Carlson6ff6f812011-05-19 12:12:54 +000014510 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14511 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070014512 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070014513 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014514 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
14515 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
14516 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014517
14518 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
14519 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014520 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014521 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014522 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014523
Joe Perches63c3a662011-04-26 08:12:10 +000014524 if (tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014525 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080014526 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014527 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014528 !tg3_flag(tp, 57765_PLUS)) {
Michael Chanc424cb22006-04-29 18:56:34 -070014529 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070014530 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070014531 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
14532 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080014533 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
14534 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014535 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080014536 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014537 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080014538 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014539 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070014540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014541
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014542 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
14543 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
14544 tp->phy_otp = tg3_read_otp_phycfg(tp);
14545 if (tp->phy_otp == 0)
14546 tp->phy_otp = TG3_OTP_DEFAULT;
14547 }
14548
Joe Perches63c3a662011-04-26 08:12:10 +000014549 if (tg3_flag(tp, CPMU_PRESENT))
Matt Carlson8ef21422008-05-02 16:47:53 -070014550 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
14551 else
14552 tp->mi_mode = MAC_MI_MODE_BASE;
14553
Linus Torvalds1da177e2005-04-16 15:20:36 -070014554 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014555 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
14556 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
14557 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
14558
Matt Carlson4d958472011-04-20 07:57:35 +000014559 /* Set these bits to enable statistics workaround. */
14560 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14561 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
14562 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0) {
14563 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
14564 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
14565 }
14566
Matt Carlson321d32a2008-11-21 17:22:19 -080014567 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
14568 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Joe Perches63c3a662011-04-26 08:12:10 +000014569 tg3_flag_set(tp, USE_PHYLIB);
Matt Carlson57e69832008-05-25 23:48:31 -070014570
Matt Carlson158d7ab2008-05-29 01:37:54 -070014571 err = tg3_mdio_init(tp);
14572 if (err)
14573 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014574
14575 /* Initialize data/descriptor byte/word swapping. */
14576 val = tr32(GRC_MODE);
Matt Carlsonf2096f92011-04-05 14:22:48 +000014577 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
14578 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
14579 GRC_MODE_WORD_SWAP_B2HRX_DATA |
14580 GRC_MODE_B2HRX_ENABLE |
14581 GRC_MODE_HTX2B_ENABLE |
14582 GRC_MODE_HOST_STACKUP);
14583 else
14584 val &= GRC_MODE_HOST_STACKUP;
14585
Linus Torvalds1da177e2005-04-16 15:20:36 -070014586 tw32(GRC_MODE, val | tp->grc_mode);
14587
14588 tg3_switch_clocks(tp);
14589
14590 /* Clear this out for sanity. */
14591 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
14592
14593 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14594 &pci_state_reg);
14595 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014596 !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014597 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
14598
14599 if (chiprevid == CHIPREV_ID_5701_A0 ||
14600 chiprevid == CHIPREV_ID_5701_B0 ||
14601 chiprevid == CHIPREV_ID_5701_B2 ||
14602 chiprevid == CHIPREV_ID_5701_B5) {
14603 void __iomem *sram_base;
14604
14605 /* Write some dummy words into the SRAM status block
14606 * area, see if it reads back correctly. If the return
14607 * value is bad, force enable the PCIX workaround.
14608 */
14609 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
14610
14611 writel(0x00000000, sram_base);
14612 writel(0x00000000, sram_base + 4);
14613 writel(0xffffffff, sram_base + 4);
14614 if (readl(sram_base) != 0x00000000)
Joe Perches63c3a662011-04-26 08:12:10 +000014615 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014616 }
14617 }
14618
14619 udelay(50);
14620 tg3_nvram_init(tp);
14621
14622 grc_misc_cfg = tr32(GRC_MISC_CFG);
14623 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
14624
Linus Torvalds1da177e2005-04-16 15:20:36 -070014625 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14626 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
14627 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
Joe Perches63c3a662011-04-26 08:12:10 +000014628 tg3_flag_set(tp, IS_5788);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014629
Joe Perches63c3a662011-04-26 08:12:10 +000014630 if (!tg3_flag(tp, IS_5788) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +000014631 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014632 tg3_flag_set(tp, TAGGED_STATUS);
14633 if (tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070014634 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
14635 HOSTCC_MODE_CLRTICK_TXBD);
14636
14637 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
14638 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
14639 tp->misc_host_ctrl);
14640 }
14641
Matt Carlson3bda1252008-08-15 14:08:22 -070014642 /* Preserve the APE MAC_MODE bits */
Joe Perches63c3a662011-04-26 08:12:10 +000014643 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +000014644 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070014645 else
Matt Carlson6e01b202011-08-19 13:58:20 +000014646 tp->mac_mode = 0;
Matt Carlson3bda1252008-08-15 14:08:22 -070014647
Linus Torvalds1da177e2005-04-16 15:20:36 -070014648 /* these are limited to 10/100 only */
14649 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14650 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
14651 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14652 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14653 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
14654 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
14655 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
14656 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14657 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
Michael Chan676917d2006-12-07 00:20:22 -080014658 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
14659 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014660 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
Matt Carlsond1101142010-02-17 15:16:55 +000014661 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
14662 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014663 (tp->phy_flags & TG3_PHYFLG_IS_FET))
14664 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014665
14666 err = tg3_phy_probe(tp);
14667 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014668 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014669 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014670 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014671 }
14672
Matt Carlson184b8902010-04-05 10:19:25 +000014673 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080014674 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014675
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014676 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
14677 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014678 } else {
14679 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014680 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014681 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014682 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014683 }
14684
14685 /* 5700 {AX,BX} chips have a broken status block link
14686 * change bit implementation, so we must use the
14687 * status register in those cases.
14688 */
14689 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014690 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014691 else
Joe Perches63c3a662011-04-26 08:12:10 +000014692 tg3_flag_clear(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014693
14694 /* The led_ctrl is set during tg3_phy_probe, here we might
14695 * have to force the link status polling mechanism based
14696 * upon subsystem IDs.
14697 */
14698 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070014699 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014700 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
14701 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Joe Perches63c3a662011-04-26 08:12:10 +000014702 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014703 }
14704
14705 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014706 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Joe Perches63c3a662011-04-26 08:12:10 +000014707 tg3_flag_set(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014708 else
Joe Perches63c3a662011-04-26 08:12:10 +000014709 tg3_flag_clear(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014710
Eric Dumazet9205fd92011-11-18 06:47:01 +000014711 tp->rx_offset = NET_SKB_PAD + NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014712 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014713 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014714 tg3_flag(tp, PCIX_MODE)) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000014715 tp->rx_offset = NET_SKB_PAD;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014716#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000014717 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014718#endif
14719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014720
Matt Carlson2c49a442010-09-30 10:34:35 +000014721 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
14722 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000014723 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
14724
Matt Carlson2c49a442010-09-30 10:34:35 +000014725 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070014726
14727 /* Increment the rx prod index on the rx std ring by at most
14728 * 8 for these chips to workaround hw errata.
14729 */
14730 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
14731 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
14732 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
14733 tp->rx_std_max_post = 8;
14734
Joe Perches63c3a662011-04-26 08:12:10 +000014735 if (tg3_flag(tp, ASPM_WORKAROUND))
Matt Carlson8ed5d972007-05-07 00:25:49 -070014736 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
14737 PCIE_PWR_MGMT_L1_THRESH_MSK;
14738
Linus Torvalds1da177e2005-04-16 15:20:36 -070014739 return err;
14740}
14741
David S. Miller49b6e95f2007-03-29 01:38:42 -070014742#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014743static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
14744{
14745 struct net_device *dev = tp->dev;
14746 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014747 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070014748 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014749 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014750
David S. Miller49b6e95f2007-03-29 01:38:42 -070014751 addr = of_get_property(dp, "local-mac-address", &len);
14752 if (addr && len == 6) {
14753 memcpy(dev->dev_addr, addr, 6);
14754 memcpy(dev->perm_addr, dev->dev_addr, 6);
14755 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014756 }
14757 return -ENODEV;
14758}
14759
14760static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
14761{
14762 struct net_device *dev = tp->dev;
14763
14764 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070014765 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014766 return 0;
14767}
14768#endif
14769
14770static int __devinit tg3_get_device_address(struct tg3 *tp)
14771{
14772 struct net_device *dev = tp->dev;
14773 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080014774 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014775
David S. Miller49b6e95f2007-03-29 01:38:42 -070014776#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014777 if (!tg3_get_macaddr_sparc(tp))
14778 return 0;
14779#endif
14780
14781 mac_offset = 0x7c;
Matt Carlson6ff6f812011-05-19 12:12:54 +000014782 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014783 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014784 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
14785 mac_offset = 0xcc;
14786 if (tg3_nvram_lock(tp))
14787 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
14788 else
14789 tg3_nvram_unlock(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000014790 } else if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson69f11c92011-07-13 09:27:30 +000014791 if (tp->pci_fn & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014792 mac_offset = 0xcc;
Matt Carlson69f11c92011-07-13 09:27:30 +000014793 if (tp->pci_fn > 1)
Matt Carlsona50d0792010-06-05 17:24:37 +000014794 mac_offset += 0x18c;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014795 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070014796 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014797
14798 /* First try to get it from MAC address mailbox. */
14799 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
14800 if ((hi >> 16) == 0x484b) {
14801 dev->dev_addr[0] = (hi >> 8) & 0xff;
14802 dev->dev_addr[1] = (hi >> 0) & 0xff;
14803
14804 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
14805 dev->dev_addr[2] = (lo >> 24) & 0xff;
14806 dev->dev_addr[3] = (lo >> 16) & 0xff;
14807 dev->dev_addr[4] = (lo >> 8) & 0xff;
14808 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014809
Michael Chan008652b2006-03-27 23:14:53 -080014810 /* Some old bootcode may report a 0 MAC address in SRAM */
14811 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
14812 }
14813 if (!addr_ok) {
14814 /* Next, try NVRAM. */
Joe Perches63c3a662011-04-26 08:12:10 +000014815 if (!tg3_flag(tp, NO_NVRAM) &&
Matt Carlsondf259d82009-04-20 06:57:14 +000014816 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000014817 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070014818 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
14819 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080014820 }
14821 /* Finally just fetch it out of the MAC control regs. */
14822 else {
14823 hi = tr32(MAC_ADDR_0_HIGH);
14824 lo = tr32(MAC_ADDR_0_LOW);
14825
14826 dev->dev_addr[5] = lo & 0xff;
14827 dev->dev_addr[4] = (lo >> 8) & 0xff;
14828 dev->dev_addr[3] = (lo >> 16) & 0xff;
14829 dev->dev_addr[2] = (lo >> 24) & 0xff;
14830 dev->dev_addr[1] = hi & 0xff;
14831 dev->dev_addr[0] = (hi >> 8) & 0xff;
14832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014833 }
14834
14835 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070014836#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014837 if (!tg3_get_default_macaddr_sparc(tp))
14838 return 0;
14839#endif
14840 return -EINVAL;
14841 }
John W. Linville2ff43692005-09-12 14:44:20 -070014842 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014843 return 0;
14844}
14845
David S. Miller59e6b432005-05-18 22:50:10 -070014846#define BOUNDARY_SINGLE_CACHELINE 1
14847#define BOUNDARY_MULTI_CACHELINE 2
14848
14849static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
14850{
14851 int cacheline_size;
14852 u8 byte;
14853 int goal;
14854
14855 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
14856 if (byte == 0)
14857 cacheline_size = 1024;
14858 else
14859 cacheline_size = (int) byte * 4;
14860
14861 /* On 5703 and later chips, the boundary bits have no
14862 * effect.
14863 */
14864 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14865 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014866 !tg3_flag(tp, PCI_EXPRESS))
David S. Miller59e6b432005-05-18 22:50:10 -070014867 goto out;
14868
14869#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
14870 goal = BOUNDARY_MULTI_CACHELINE;
14871#else
14872#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
14873 goal = BOUNDARY_SINGLE_CACHELINE;
14874#else
14875 goal = 0;
14876#endif
14877#endif
14878
Joe Perches63c3a662011-04-26 08:12:10 +000014879 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014880 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
14881 goto out;
14882 }
14883
David S. Miller59e6b432005-05-18 22:50:10 -070014884 if (!goal)
14885 goto out;
14886
14887 /* PCI controllers on most RISC systems tend to disconnect
14888 * when a device tries to burst across a cache-line boundary.
14889 * Therefore, letting tg3 do so just wastes PCI bandwidth.
14890 *
14891 * Unfortunately, for PCI-E there are only limited
14892 * write-side controls for this, and thus for reads
14893 * we will still get the disconnects. We'll also waste
14894 * these PCI cycles for both read and write for chips
14895 * other than 5700 and 5701 which do not implement the
14896 * boundary bits.
14897 */
Joe Perches63c3a662011-04-26 08:12:10 +000014898 if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014899 switch (cacheline_size) {
14900 case 16:
14901 case 32:
14902 case 64:
14903 case 128:
14904 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14905 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
14906 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
14907 } else {
14908 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14909 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14910 }
14911 break;
14912
14913 case 256:
14914 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
14915 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
14916 break;
14917
14918 default:
14919 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14920 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14921 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014922 }
Joe Perches63c3a662011-04-26 08:12:10 +000014923 } else if (tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014924 switch (cacheline_size) {
14925 case 16:
14926 case 32:
14927 case 64:
14928 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14929 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14930 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
14931 break;
14932 }
14933 /* fallthrough */
14934 case 128:
14935 default:
14936 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14937 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
14938 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014939 }
David S. Miller59e6b432005-05-18 22:50:10 -070014940 } else {
14941 switch (cacheline_size) {
14942 case 16:
14943 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14944 val |= (DMA_RWCTRL_READ_BNDRY_16 |
14945 DMA_RWCTRL_WRITE_BNDRY_16);
14946 break;
14947 }
14948 /* fallthrough */
14949 case 32:
14950 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14951 val |= (DMA_RWCTRL_READ_BNDRY_32 |
14952 DMA_RWCTRL_WRITE_BNDRY_32);
14953 break;
14954 }
14955 /* fallthrough */
14956 case 64:
14957 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14958 val |= (DMA_RWCTRL_READ_BNDRY_64 |
14959 DMA_RWCTRL_WRITE_BNDRY_64);
14960 break;
14961 }
14962 /* fallthrough */
14963 case 128:
14964 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14965 val |= (DMA_RWCTRL_READ_BNDRY_128 |
14966 DMA_RWCTRL_WRITE_BNDRY_128);
14967 break;
14968 }
14969 /* fallthrough */
14970 case 256:
14971 val |= (DMA_RWCTRL_READ_BNDRY_256 |
14972 DMA_RWCTRL_WRITE_BNDRY_256);
14973 break;
14974 case 512:
14975 val |= (DMA_RWCTRL_READ_BNDRY_512 |
14976 DMA_RWCTRL_WRITE_BNDRY_512);
14977 break;
14978 case 1024:
14979 default:
14980 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
14981 DMA_RWCTRL_WRITE_BNDRY_1024);
14982 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014983 }
David S. Miller59e6b432005-05-18 22:50:10 -070014984 }
14985
14986out:
14987 return val;
14988}
14989
Linus Torvalds1da177e2005-04-16 15:20:36 -070014990static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
14991{
14992 struct tg3_internal_buffer_desc test_desc;
14993 u32 sram_dma_descs;
14994 int i, ret;
14995
14996 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
14997
14998 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
14999 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
15000 tw32(RDMAC_STATUS, 0);
15001 tw32(WDMAC_STATUS, 0);
15002
15003 tw32(BUFMGR_MODE, 0);
15004 tw32(FTQ_RESET, 0);
15005
15006 test_desc.addr_hi = ((u64) buf_dma) >> 32;
15007 test_desc.addr_lo = buf_dma & 0xffffffff;
15008 test_desc.nic_mbuf = 0x00002100;
15009 test_desc.len = size;
15010
15011 /*
15012 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
15013 * the *second* time the tg3 driver was getting loaded after an
15014 * initial scan.
15015 *
15016 * Broadcom tells me:
15017 * ...the DMA engine is connected to the GRC block and a DMA
15018 * reset may affect the GRC block in some unpredictable way...
15019 * The behavior of resets to individual blocks has not been tested.
15020 *
15021 * Broadcom noted the GRC reset will also reset all sub-components.
15022 */
15023 if (to_device) {
15024 test_desc.cqid_sqid = (13 << 8) | 2;
15025
15026 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
15027 udelay(40);
15028 } else {
15029 test_desc.cqid_sqid = (16 << 8) | 7;
15030
15031 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
15032 udelay(40);
15033 }
15034 test_desc.flags = 0x00000005;
15035
15036 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
15037 u32 val;
15038
15039 val = *(((u32 *)&test_desc) + i);
15040 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
15041 sram_dma_descs + (i * sizeof(u32)));
15042 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
15043 }
15044 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
15045
Matt Carlson859a588792010-04-05 10:19:28 +000015046 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015047 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a588792010-04-05 10:19:28 +000015048 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070015049 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015050
15051 ret = -ENODEV;
15052 for (i = 0; i < 40; i++) {
15053 u32 val;
15054
15055 if (to_device)
15056 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
15057 else
15058 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
15059 if ((val & 0xffff) == sram_dma_descs) {
15060 ret = 0;
15061 break;
15062 }
15063
15064 udelay(100);
15065 }
15066
15067 return ret;
15068}
15069
David S. Millerded73402005-05-23 13:59:47 -070015070#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070015071
Matt Carlson41434702011-03-09 16:58:22 +000015072static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080015073 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
15074 { },
15075};
15076
Linus Torvalds1da177e2005-04-16 15:20:36 -070015077static int __devinit tg3_test_dma(struct tg3 *tp)
15078{
15079 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070015080 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000015081 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015082
Matt Carlson4bae65c2010-11-24 08:31:52 +000015083 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
15084 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015085 if (!buf) {
15086 ret = -ENOMEM;
15087 goto out_nofree;
15088 }
15089
15090 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
15091 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
15092
David S. Miller59e6b432005-05-18 22:50:10 -070015093 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015094
Joe Perches63c3a662011-04-26 08:12:10 +000015095 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000015096 goto out;
15097
Joe Perches63c3a662011-04-26 08:12:10 +000015098 if (tg3_flag(tp, PCI_EXPRESS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015099 /* DMA read watermark not used on PCIE */
15100 tp->dma_rwctrl |= 0x00180000;
Joe Perches63c3a662011-04-26 08:12:10 +000015101 } else if (!tg3_flag(tp, PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070015102 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
15103 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015104 tp->dma_rwctrl |= 0x003f0000;
15105 else
15106 tp->dma_rwctrl |= 0x003f000f;
15107 } else {
15108 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
15109 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
15110 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080015111 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015112
Michael Chan4a29cc22006-03-19 13:21:12 -080015113 /* If the 5704 is behind the EPB bridge, we can
15114 * do the less restrictive ONE_DMA workaround for
15115 * better performance.
15116 */
Joe Perches63c3a662011-04-26 08:12:10 +000015117 if (tg3_flag(tp, 40BIT_DMA_BUG) &&
Michael Chan4a29cc22006-03-19 13:21:12 -080015118 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
15119 tp->dma_rwctrl |= 0x8000;
15120 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015121 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
15122
Michael Chan49afdeb2007-02-13 12:17:03 -080015123 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
15124 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070015125 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080015126 tp->dma_rwctrl |=
15127 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
15128 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
15129 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070015130 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
15131 /* 5780 always in PCIX mode */
15132 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070015133 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
15134 /* 5714 always in PCIX mode */
15135 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015136 } else {
15137 tp->dma_rwctrl |= 0x001b000f;
15138 }
15139 }
15140
15141 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
15142 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
15143 tp->dma_rwctrl &= 0xfffffff0;
15144
15145 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
15146 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
15147 /* Remove this if it causes problems for some boards. */
15148 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
15149
15150 /* On 5700/5701 chips, we need to set this bit.
15151 * Otherwise the chip will issue cacheline transactions
15152 * to streamable DMA memory with not all the byte
15153 * enables turned on. This is an error on several
15154 * RISC PCI controllers, in particular sparc64.
15155 *
15156 * On 5703/5704 chips, this bit has been reassigned
15157 * a different meaning. In particular, it is used
15158 * on those chips to enable a PCI-X workaround.
15159 */
15160 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
15161 }
15162
15163 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15164
15165#if 0
15166 /* Unneeded, already done by tg3_get_invariants. */
15167 tg3_switch_clocks(tp);
15168#endif
15169
Linus Torvalds1da177e2005-04-16 15:20:36 -070015170 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
15171 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
15172 goto out;
15173
David S. Miller59e6b432005-05-18 22:50:10 -070015174 /* It is best to perform DMA test with maximum write burst size
15175 * to expose the 5700/5701 write DMA bug.
15176 */
15177 saved_dma_rwctrl = tp->dma_rwctrl;
15178 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
15179 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15180
Linus Torvalds1da177e2005-04-16 15:20:36 -070015181 while (1) {
15182 u32 *p = buf, i;
15183
15184 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
15185 p[i] = i;
15186
15187 /* Send the buffer to the chip. */
15188 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
15189 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000015190 dev_err(&tp->pdev->dev,
15191 "%s: Buffer write failed. err = %d\n",
15192 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015193 break;
15194 }
15195
15196#if 0
15197 /* validate data reached card RAM correctly. */
15198 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
15199 u32 val;
15200 tg3_read_mem(tp, 0x2100 + (i*4), &val);
15201 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000015202 dev_err(&tp->pdev->dev,
15203 "%s: Buffer corrupted on device! "
15204 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015205 /* ret = -ENODEV here? */
15206 }
15207 p[i] = 0;
15208 }
15209#endif
15210 /* Now read it back. */
15211 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
15212 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000015213 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
15214 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015215 break;
15216 }
15217
15218 /* Verify it. */
15219 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
15220 if (p[i] == i)
15221 continue;
15222
David S. Miller59e6b432005-05-18 22:50:10 -070015223 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
15224 DMA_RWCTRL_WRITE_BNDRY_16) {
15225 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015226 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
15227 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15228 break;
15229 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000015230 dev_err(&tp->pdev->dev,
15231 "%s: Buffer corrupted on read back! "
15232 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015233 ret = -ENODEV;
15234 goto out;
15235 }
15236 }
15237
15238 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
15239 /* Success. */
15240 ret = 0;
15241 break;
15242 }
15243 }
David S. Miller59e6b432005-05-18 22:50:10 -070015244 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
15245 DMA_RWCTRL_WRITE_BNDRY_16) {
15246 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070015247 * now look for chipsets that are known to expose the
15248 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070015249 */
Matt Carlson41434702011-03-09 16:58:22 +000015250 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070015251 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
15252 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a588792010-04-05 10:19:28 +000015253 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070015254 /* Safe to use the calculated DMA boundary. */
15255 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a588792010-04-05 10:19:28 +000015256 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070015257
David S. Miller59e6b432005-05-18 22:50:10 -070015258 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015260
15261out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000015262 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015263out_nofree:
15264 return ret;
15265}
15266
Linus Torvalds1da177e2005-04-16 15:20:36 -070015267static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
15268{
Joe Perches63c3a662011-04-26 08:12:10 +000015269 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson666bc832010-01-20 16:58:03 +000015270 tp->bufmgr_config.mbuf_read_dma_low_water =
15271 DEFAULT_MB_RDMA_LOW_WATER_5705;
15272 tp->bufmgr_config.mbuf_mac_rx_low_water =
15273 DEFAULT_MB_MACRX_LOW_WATER_57765;
15274 tp->bufmgr_config.mbuf_high_water =
15275 DEFAULT_MB_HIGH_WATER_57765;
15276
15277 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15278 DEFAULT_MB_RDMA_LOW_WATER_5705;
15279 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15280 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
15281 tp->bufmgr_config.mbuf_high_water_jumbo =
15282 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000015283 } else if (tg3_flag(tp, 5705_PLUS)) {
Michael Chanfdfec1722005-07-25 12:31:48 -070015284 tp->bufmgr_config.mbuf_read_dma_low_water =
15285 DEFAULT_MB_RDMA_LOW_WATER_5705;
15286 tp->bufmgr_config.mbuf_mac_rx_low_water =
15287 DEFAULT_MB_MACRX_LOW_WATER_5705;
15288 tp->bufmgr_config.mbuf_high_water =
15289 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070015290 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
15291 tp->bufmgr_config.mbuf_mac_rx_low_water =
15292 DEFAULT_MB_MACRX_LOW_WATER_5906;
15293 tp->bufmgr_config.mbuf_high_water =
15294 DEFAULT_MB_HIGH_WATER_5906;
15295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015296
Michael Chanfdfec1722005-07-25 12:31:48 -070015297 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15298 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
15299 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15300 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
15301 tp->bufmgr_config.mbuf_high_water_jumbo =
15302 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
15303 } else {
15304 tp->bufmgr_config.mbuf_read_dma_low_water =
15305 DEFAULT_MB_RDMA_LOW_WATER;
15306 tp->bufmgr_config.mbuf_mac_rx_low_water =
15307 DEFAULT_MB_MACRX_LOW_WATER;
15308 tp->bufmgr_config.mbuf_high_water =
15309 DEFAULT_MB_HIGH_WATER;
15310
15311 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15312 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
15313 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15314 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
15315 tp->bufmgr_config.mbuf_high_water_jumbo =
15316 DEFAULT_MB_HIGH_WATER_JUMBO;
15317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015318
15319 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
15320 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
15321}
15322
15323static char * __devinit tg3_phy_string(struct tg3 *tp)
15324{
Matt Carlson79eb6902010-02-17 15:17:03 +000015325 switch (tp->phy_id & TG3_PHY_ID_MASK) {
15326 case TG3_PHY_ID_BCM5400: return "5400";
15327 case TG3_PHY_ID_BCM5401: return "5401";
15328 case TG3_PHY_ID_BCM5411: return "5411";
15329 case TG3_PHY_ID_BCM5701: return "5701";
15330 case TG3_PHY_ID_BCM5703: return "5703";
15331 case TG3_PHY_ID_BCM5704: return "5704";
15332 case TG3_PHY_ID_BCM5705: return "5705";
15333 case TG3_PHY_ID_BCM5750: return "5750";
15334 case TG3_PHY_ID_BCM5752: return "5752";
15335 case TG3_PHY_ID_BCM5714: return "5714";
15336 case TG3_PHY_ID_BCM5780: return "5780";
15337 case TG3_PHY_ID_BCM5755: return "5755";
15338 case TG3_PHY_ID_BCM5787: return "5787";
15339 case TG3_PHY_ID_BCM5784: return "5784";
15340 case TG3_PHY_ID_BCM5756: return "5722/5756";
15341 case TG3_PHY_ID_BCM5906: return "5906";
15342 case TG3_PHY_ID_BCM5761: return "5761";
15343 case TG3_PHY_ID_BCM5718C: return "5718C";
15344 case TG3_PHY_ID_BCM5718S: return "5718S";
15345 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000015346 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000015347 case TG3_PHY_ID_BCM5720C: return "5720C";
Matt Carlson79eb6902010-02-17 15:17:03 +000015348 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070015349 case 0: return "serdes";
15350 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070015351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015352}
15353
Michael Chanf9804dd2005-09-27 12:13:10 -070015354static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
15355{
Joe Perches63c3a662011-04-26 08:12:10 +000015356 if (tg3_flag(tp, PCI_EXPRESS)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070015357 strcpy(str, "PCI Express");
15358 return str;
Joe Perches63c3a662011-04-26 08:12:10 +000015359 } else if (tg3_flag(tp, PCIX_MODE)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070015360 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
15361
15362 strcpy(str, "PCIX:");
15363
15364 if ((clock_ctrl == 7) ||
15365 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
15366 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
15367 strcat(str, "133MHz");
15368 else if (clock_ctrl == 0)
15369 strcat(str, "33MHz");
15370 else if (clock_ctrl == 2)
15371 strcat(str, "50MHz");
15372 else if (clock_ctrl == 4)
15373 strcat(str, "66MHz");
15374 else if (clock_ctrl == 6)
15375 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070015376 } else {
15377 strcpy(str, "PCI:");
Joe Perches63c3a662011-04-26 08:12:10 +000015378 if (tg3_flag(tp, PCI_HIGH_SPEED))
Michael Chanf9804dd2005-09-27 12:13:10 -070015379 strcat(str, "66MHz");
15380 else
15381 strcat(str, "33MHz");
15382 }
Joe Perches63c3a662011-04-26 08:12:10 +000015383 if (tg3_flag(tp, PCI_32BIT))
Michael Chanf9804dd2005-09-27 12:13:10 -070015384 strcat(str, ":32-bit");
15385 else
15386 strcat(str, ":64-bit");
15387 return str;
15388}
15389
David S. Miller15f98502005-05-18 22:49:26 -070015390static void __devinit tg3_init_coal(struct tg3 *tp)
15391{
15392 struct ethtool_coalesce *ec = &tp->coal;
15393
15394 memset(ec, 0, sizeof(*ec));
15395 ec->cmd = ETHTOOL_GCOALESCE;
15396 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
15397 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
15398 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
15399 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
15400 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
15401 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
15402 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
15403 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
15404 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
15405
15406 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
15407 HOSTCC_MODE_CLRTICK_TXBD)) {
15408 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
15409 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
15410 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
15411 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
15412 }
Michael Chand244c892005-07-05 14:42:33 -070015413
Joe Perches63c3a662011-04-26 08:12:10 +000015414 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070015415 ec->rx_coalesce_usecs_irq = 0;
15416 ec->tx_coalesce_usecs_irq = 0;
15417 ec->stats_block_coalesce_usecs = 0;
15418 }
David S. Miller15f98502005-05-18 22:49:26 -070015419}
15420
Linus Torvalds1da177e2005-04-16 15:20:36 -070015421static int __devinit tg3_init_one(struct pci_dev *pdev,
15422 const struct pci_device_id *ent)
15423{
Linus Torvalds1da177e2005-04-16 15:20:36 -070015424 struct net_device *dev;
15425 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000015426 int i, err, pm_cap;
15427 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070015428 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080015429 u64 dma_mask, persist_dma_mask;
Michał Mirosławc8f44af2011-11-15 15:29:55 +000015430 netdev_features_t features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015431
Joe Perches05dbe002010-02-17 19:44:19 +000015432 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015433
15434 err = pci_enable_device(pdev);
15435 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015436 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015437 return err;
15438 }
15439
Linus Torvalds1da177e2005-04-16 15:20:36 -070015440 err = pci_request_regions(pdev, DRV_MODULE_NAME);
15441 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015442 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015443 goto err_out_disable_pdev;
15444 }
15445
15446 pci_set_master(pdev);
15447
15448 /* Find power-management capability. */
15449 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
15450 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000015451 dev_err(&pdev->dev,
15452 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015453 err = -EIO;
15454 goto err_out_free_res;
15455 }
15456
Matt Carlson16821282011-07-13 09:27:28 +000015457 err = pci_set_power_state(pdev, PCI_D0);
15458 if (err) {
15459 dev_err(&pdev->dev, "Transition to D0 failed, aborting\n");
15460 goto err_out_free_res;
15461 }
15462
Matt Carlsonfe5f5782009-09-01 13:09:39 +000015463 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015464 if (!dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015465 err = -ENOMEM;
Matt Carlson16821282011-07-13 09:27:28 +000015466 goto err_out_power_down;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015467 }
15468
Linus Torvalds1da177e2005-04-16 15:20:36 -070015469 SET_NETDEV_DEV(dev, &pdev->dev);
15470
Linus Torvalds1da177e2005-04-16 15:20:36 -070015471 tp = netdev_priv(dev);
15472 tp->pdev = pdev;
15473 tp->dev = dev;
15474 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015475 tp->rx_mode = TG3_DEF_RX_MODE;
15476 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070015477
Linus Torvalds1da177e2005-04-16 15:20:36 -070015478 if (tg3_debug > 0)
15479 tp->msg_enable = tg3_debug;
15480 else
15481 tp->msg_enable = TG3_DEF_MSG_ENABLE;
15482
15483 /* The word/byte swap controls here control register access byte
15484 * swapping. DMA data byte swapping is controlled in the GRC_MODE
15485 * setting below.
15486 */
15487 tp->misc_host_ctrl =
15488 MISC_HOST_CTRL_MASK_PCI_INT |
15489 MISC_HOST_CTRL_WORD_SWAP |
15490 MISC_HOST_CTRL_INDIR_ACCESS |
15491 MISC_HOST_CTRL_PCISTATE_RW;
15492
15493 /* The NONFRM (non-frame) byte/word swap controls take effect
15494 * on descriptor entries, anything which isn't packet data.
15495 *
15496 * The StrongARM chips on the board (one for tx, one for rx)
15497 * are running in big-endian mode.
15498 */
15499 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
15500 GRC_MODE_WSWAP_NONFRM_DATA);
15501#ifdef __BIG_ENDIAN
15502 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
15503#endif
15504 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015505 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000015506 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015507
Matt Carlsond5fe4882008-11-21 17:20:32 -080015508 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010015509 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015510 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015511 err = -ENOMEM;
15512 goto err_out_free_dev;
15513 }
15514
Matt Carlsonc9cab242011-07-13 09:27:27 +000015515 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
15516 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761E ||
15517 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S ||
15518 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761SE ||
15519 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
15520 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
15521 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
15522 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720) {
15523 tg3_flag_set(tp, ENABLE_APE);
15524 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
15525 if (!tp->aperegs) {
15526 dev_err(&pdev->dev,
15527 "Cannot map APE registers, aborting\n");
15528 err = -ENOMEM;
15529 goto err_out_iounmap;
15530 }
15531 }
15532
Linus Torvalds1da177e2005-04-16 15:20:36 -070015533 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
15534 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015535
Linus Torvalds1da177e2005-04-16 15:20:36 -070015536 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015537 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Matt Carlson2ffcc982011-05-19 12:12:44 +000015538 dev->netdev_ops = &tg3_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015539 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015540
15541 err = tg3_get_invariants(tp);
15542 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015543 dev_err(&pdev->dev,
15544 "Problem fetching invariants of chip, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015545 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015546 }
15547
Michael Chan4a29cc22006-03-19 13:21:12 -080015548 /* The EPB bridge inside 5714, 5715, and 5780 and any
15549 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080015550 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
15551 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
15552 * do DMA address check in tg3_start_xmit().
15553 */
Joe Perches63c3a662011-04-26 08:12:10 +000015554 if (tg3_flag(tp, IS_5788))
Yang Hongyang284901a2009-04-06 19:01:15 -070015555 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Joe Perches63c3a662011-04-26 08:12:10 +000015556 else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070015557 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080015558#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070015559 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015560#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080015561 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070015562 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015563
15564 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070015565 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080015566 err = pci_set_dma_mask(pdev, dma_mask);
15567 if (!err) {
Matt Carlson0da06062011-05-19 12:12:53 +000015568 features |= NETIF_F_HIGHDMA;
Michael Chan72f2afb2006-03-06 19:28:35 -080015569 err = pci_set_consistent_dma_mask(pdev,
15570 persist_dma_mask);
15571 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015572 dev_err(&pdev->dev, "Unable to obtain 64 bit "
15573 "DMA for consistent allocations\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015574 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080015575 }
15576 }
15577 }
Yang Hongyang284901a2009-04-06 19:01:15 -070015578 if (err || dma_mask == DMA_BIT_MASK(32)) {
15579 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080015580 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015581 dev_err(&pdev->dev,
15582 "No usable DMA configuration, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015583 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080015584 }
15585 }
15586
Michael Chanfdfec1722005-07-25 12:31:48 -070015587 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015588
Matt Carlson0da06062011-05-19 12:12:53 +000015589 features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
15590
15591 /* 5700 B0 chips do not support checksumming correctly due
15592 * to hardware bugs.
15593 */
15594 if (tp->pci_chip_rev_id != CHIPREV_ID_5700_B0) {
15595 features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
15596
15597 if (tg3_flag(tp, 5755_PLUS))
15598 features |= NETIF_F_IPV6_CSUM;
15599 }
15600
Michael Chan4e3a7aa2006-03-20 17:47:44 -080015601 /* TSO is on by default on chips that support hardware TSO.
15602 * Firmware TSO on older chips gives lower performance, so it
15603 * is off by default, but can be enabled using ethtool.
15604 */
Joe Perches63c3a662011-04-26 08:12:10 +000015605 if ((tg3_flag(tp, HW_TSO_1) ||
15606 tg3_flag(tp, HW_TSO_2) ||
15607 tg3_flag(tp, HW_TSO_3)) &&
Matt Carlson0da06062011-05-19 12:12:53 +000015608 (features & NETIF_F_IP_CSUM))
15609 features |= NETIF_F_TSO;
Joe Perches63c3a662011-04-26 08:12:10 +000015610 if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
Matt Carlson0da06062011-05-19 12:12:53 +000015611 if (features & NETIF_F_IPV6_CSUM)
15612 features |= NETIF_F_TSO6;
Joe Perches63c3a662011-04-26 08:12:10 +000015613 if (tg3_flag(tp, HW_TSO_3) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000015614 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070015615 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
15616 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Joe Perches63c3a662011-04-26 08:12:10 +000015617 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Michał Mirosławdc668912011-04-07 03:35:07 +000015618 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson0da06062011-05-19 12:12:53 +000015619 features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070015620 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015621
Matt Carlsond542fe22011-05-19 16:02:43 +000015622 dev->features |= features;
15623 dev->vlan_features |= features;
15624
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015625 /*
15626 * Add loopback capability only for a subset of devices that support
15627 * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
15628 * loopback for the remaining devices.
15629 */
15630 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
15631 !tg3_flag(tp, CPMU_PRESENT))
15632 /* Add the loopback capability */
Matt Carlson0da06062011-05-19 12:12:53 +000015633 features |= NETIF_F_LOOPBACK;
15634
Matt Carlson0da06062011-05-19 12:12:53 +000015635 dev->hw_features |= features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015636
Linus Torvalds1da177e2005-04-16 15:20:36 -070015637 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015638 !tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015639 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015640 tg3_flag_set(tp, MAX_RXPEND_64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015641 tp->rx_pending = 63;
15642 }
15643
Linus Torvalds1da177e2005-04-16 15:20:36 -070015644 err = tg3_get_device_address(tp);
15645 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015646 dev_err(&pdev->dev,
15647 "Could not obtain valid ethernet address, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015648 goto err_out_apeunmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070015649 }
15650
Matt Carlsonc88864d2007-11-12 21:07:01 -080015651 /*
15652 * Reset chip in case UNDI or EFI driver did not shutdown
15653 * DMA self test will enable WDMAC and we'll see (spurious)
15654 * pending DMA on the PCI bus at that point.
15655 */
15656 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
15657 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
15658 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
15659 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
15660 }
15661
15662 err = tg3_test_dma(tp);
15663 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015664 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080015665 goto err_out_apeunmap;
15666 }
15667
Matt Carlson78f90dc2009-11-13 13:03:42 +000015668 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
15669 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
15670 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000015671 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000015672 struct tg3_napi *tnapi = &tp->napi[i];
15673
15674 tnapi->tp = tp;
15675 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
15676
15677 tnapi->int_mbox = intmbx;
Matt Carlson93a700a2011-08-31 11:44:54 +000015678 if (i <= 4)
Matt Carlson78f90dc2009-11-13 13:03:42 +000015679 intmbx += 0x8;
15680 else
15681 intmbx += 0x4;
15682
15683 tnapi->consmbox = rcvmbx;
15684 tnapi->prodmbox = sndmbx;
15685
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015686 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000015687 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015688 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000015689 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000015690
Joe Perches63c3a662011-04-26 08:12:10 +000015691 if (!tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson78f90dc2009-11-13 13:03:42 +000015692 break;
15693
15694 /*
15695 * If we support MSIX, we'll be using RSS. If we're using
15696 * RSS, the first vector only handles link interrupts and the
15697 * remaining vectors handle rx and tx interrupts. Reuse the
15698 * mailbox values for the next iteration. The values we setup
15699 * above are still useful for the single vectored mode.
15700 */
15701 if (!i)
15702 continue;
15703
15704 rcvmbx += 0x8;
15705
15706 if (sndmbx & 0x4)
15707 sndmbx -= 0x4;
15708 else
15709 sndmbx += 0xc;
15710 }
15711
Matt Carlsonc88864d2007-11-12 21:07:01 -080015712 tg3_init_coal(tp);
15713
Michael Chanc49a1562006-12-17 17:07:29 -080015714 pci_set_drvdata(pdev, dev);
15715
Matt Carlsoncd0d7222011-07-13 09:27:33 +000015716 if (tg3_flag(tp, 5717_PLUS)) {
15717 /* Resume a low-power mode */
15718 tg3_frob_aux_power(tp, false);
15719 }
15720
Matt Carlson21f76382012-02-22 12:35:21 +000015721 tg3_timer_init(tp);
15722
Linus Torvalds1da177e2005-04-16 15:20:36 -070015723 err = register_netdev(dev);
15724 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015725 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070015726 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015727 }
15728
Joe Perches05dbe002010-02-17 19:44:19 +000015729 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
15730 tp->board_part_number,
15731 tp->pci_chip_rev_id,
15732 tg3_bus_string(tp, str),
15733 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015734
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015735 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000015736 struct phy_device *phydev;
15737 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000015738 netdev_info(dev,
15739 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000015740 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015741 } else {
15742 char *ethtype;
15743
15744 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
15745 ethtype = "10/100Base-TX";
15746 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
15747 ethtype = "1000Base-SX";
15748 else
15749 ethtype = "10/100/1000Base-T";
15750
Matt Carlson5129c3a2010-04-05 10:19:23 +000015751 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlson47007832011-04-20 07:57:43 +000015752 "(WireSpeed[%d], EEE[%d])\n",
15753 tg3_phy_string(tp), ethtype,
15754 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
15755 (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015756 }
Matt Carlsondf59c942008-11-03 16:52:56 -080015757
Joe Perches05dbe002010-02-17 19:44:19 +000015758 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000015759 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015760 tg3_flag(tp, USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015761 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015762 tg3_flag(tp, ENABLE_ASF) != 0,
15763 tg3_flag(tp, TSO_CAPABLE) != 0);
Joe Perches05dbe002010-02-17 19:44:19 +000015764 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
15765 tp->dma_rwctrl,
15766 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
15767 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015768
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015769 pci_save_state(pdev);
15770
Linus Torvalds1da177e2005-04-16 15:20:36 -070015771 return 0;
15772
Matt Carlson0d3031d2007-10-10 18:02:43 -070015773err_out_apeunmap:
15774 if (tp->aperegs) {
15775 iounmap(tp->aperegs);
15776 tp->aperegs = NULL;
15777 }
15778
Linus Torvalds1da177e2005-04-16 15:20:36 -070015779err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070015780 if (tp->regs) {
15781 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015782 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015784
15785err_out_free_dev:
15786 free_netdev(dev);
15787
Matt Carlson16821282011-07-13 09:27:28 +000015788err_out_power_down:
15789 pci_set_power_state(pdev, PCI_D3hot);
15790
Linus Torvalds1da177e2005-04-16 15:20:36 -070015791err_out_free_res:
15792 pci_release_regions(pdev);
15793
15794err_out_disable_pdev:
15795 pci_disable_device(pdev);
15796 pci_set_drvdata(pdev, NULL);
15797 return err;
15798}
15799
15800static void __devexit tg3_remove_one(struct pci_dev *pdev)
15801{
15802 struct net_device *dev = pci_get_drvdata(pdev);
15803
15804 if (dev) {
15805 struct tg3 *tp = netdev_priv(dev);
15806
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080015807 if (tp->fw)
15808 release_firmware(tp->fw);
15809
Matt Carlsondb219972011-11-04 09:15:03 +000015810 tg3_reset_task_cancel(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015811
David S. Miller1805b2f2011-10-24 18:18:09 -040015812 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015813 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015814 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015815 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070015816
Linus Torvalds1da177e2005-04-16 15:20:36 -070015817 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015818 if (tp->aperegs) {
15819 iounmap(tp->aperegs);
15820 tp->aperegs = NULL;
15821 }
Michael Chan68929142005-08-09 20:17:14 -070015822 if (tp->regs) {
15823 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015824 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015826 free_netdev(dev);
15827 pci_release_regions(pdev);
15828 pci_disable_device(pdev);
15829 pci_set_drvdata(pdev, NULL);
15830 }
15831}
15832
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015833#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015834static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015835{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015836 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015837 struct net_device *dev = pci_get_drvdata(pdev);
15838 struct tg3 *tp = netdev_priv(dev);
15839 int err;
15840
15841 if (!netif_running(dev))
15842 return 0;
15843
Matt Carlsondb219972011-11-04 09:15:03 +000015844 tg3_reset_task_cancel(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015845 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015846 tg3_netif_stop(tp);
15847
Matt Carlson21f76382012-02-22 12:35:21 +000015848 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015849
David S. Millerf47c11e2005-06-24 20:18:35 -070015850 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015851 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070015852 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015853
15854 netif_device_detach(dev);
15855
David S. Millerf47c11e2005-06-24 20:18:35 -070015856 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070015857 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches63c3a662011-04-26 08:12:10 +000015858 tg3_flag_clear(tp, INIT_COMPLETE);
David S. Millerf47c11e2005-06-24 20:18:35 -070015859 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015860
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015861 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015862 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015863 int err2;
15864
David S. Millerf47c11e2005-06-24 20:18:35 -070015865 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015866
Joe Perches63c3a662011-04-26 08:12:10 +000015867 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015868 err2 = tg3_restart_hw(tp, 1);
15869 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070015870 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015871
Matt Carlson21f76382012-02-22 12:35:21 +000015872 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015873
15874 netif_device_attach(dev);
15875 tg3_netif_start(tp);
15876
Michael Chanb9ec6c12006-07-25 16:37:27 -070015877out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015878 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015879
15880 if (!err2)
15881 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015882 }
15883
15884 return err;
15885}
15886
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015887static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015888{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015889 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015890 struct net_device *dev = pci_get_drvdata(pdev);
15891 struct tg3 *tp = netdev_priv(dev);
15892 int err;
15893
15894 if (!netif_running(dev))
15895 return 0;
15896
Linus Torvalds1da177e2005-04-16 15:20:36 -070015897 netif_device_attach(dev);
15898
David S. Millerf47c11e2005-06-24 20:18:35 -070015899 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015900
Joe Perches63c3a662011-04-26 08:12:10 +000015901 tg3_flag_set(tp, INIT_COMPLETE);
Michael Chanb9ec6c12006-07-25 16:37:27 -070015902 err = tg3_restart_hw(tp, 1);
15903 if (err)
15904 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015905
Matt Carlson21f76382012-02-22 12:35:21 +000015906 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015907
Linus Torvalds1da177e2005-04-16 15:20:36 -070015908 tg3_netif_start(tp);
15909
Michael Chanb9ec6c12006-07-25 16:37:27 -070015910out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015911 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015912
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015913 if (!err)
15914 tg3_phy_start(tp);
15915
Michael Chanb9ec6c12006-07-25 16:37:27 -070015916 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015917}
15918
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015919static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015920#define TG3_PM_OPS (&tg3_pm_ops)
15921
15922#else
15923
15924#define TG3_PM_OPS NULL
15925
15926#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015927
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015928/**
15929 * tg3_io_error_detected - called when PCI error is detected
15930 * @pdev: Pointer to PCI device
15931 * @state: The current pci connection state
15932 *
15933 * This function is called after a PCI bus error affecting
15934 * this device has been detected.
15935 */
15936static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
15937 pci_channel_state_t state)
15938{
15939 struct net_device *netdev = pci_get_drvdata(pdev);
15940 struct tg3 *tp = netdev_priv(netdev);
15941 pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
15942
15943 netdev_info(netdev, "PCI I/O error detected\n");
15944
15945 rtnl_lock();
15946
15947 if (!netif_running(netdev))
15948 goto done;
15949
15950 tg3_phy_stop(tp);
15951
15952 tg3_netif_stop(tp);
15953
Matt Carlson21f76382012-02-22 12:35:21 +000015954 tg3_timer_stop(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015955
15956 /* Want to make sure that the reset task doesn't run */
Matt Carlsondb219972011-11-04 09:15:03 +000015957 tg3_reset_task_cancel(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015958
15959 netif_device_detach(netdev);
15960
15961 /* Clean up software state, even if MMIO is blocked */
15962 tg3_full_lock(tp, 0);
15963 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
15964 tg3_full_unlock(tp);
15965
15966done:
15967 if (state == pci_channel_io_perm_failure)
15968 err = PCI_ERS_RESULT_DISCONNECT;
15969 else
15970 pci_disable_device(pdev);
15971
15972 rtnl_unlock();
15973
15974 return err;
15975}
15976
15977/**
15978 * tg3_io_slot_reset - called after the pci bus has been reset.
15979 * @pdev: Pointer to PCI device
15980 *
15981 * Restart the card from scratch, as if from a cold-boot.
15982 * At this point, the card has exprienced a hard reset,
15983 * followed by fixups by BIOS, and has its config space
15984 * set up identically to what it was at cold boot.
15985 */
15986static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
15987{
15988 struct net_device *netdev = pci_get_drvdata(pdev);
15989 struct tg3 *tp = netdev_priv(netdev);
15990 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
15991 int err;
15992
15993 rtnl_lock();
15994
15995 if (pci_enable_device(pdev)) {
15996 netdev_err(netdev, "Cannot re-enable PCI device after reset.\n");
15997 goto done;
15998 }
15999
16000 pci_set_master(pdev);
16001 pci_restore_state(pdev);
16002 pci_save_state(pdev);
16003
16004 if (!netif_running(netdev)) {
16005 rc = PCI_ERS_RESULT_RECOVERED;
16006 goto done;
16007 }
16008
16009 err = tg3_power_up(tp);
Matt Carlsonbed98292011-07-13 09:27:29 +000016010 if (err)
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016011 goto done;
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016012
16013 rc = PCI_ERS_RESULT_RECOVERED;
16014
16015done:
16016 rtnl_unlock();
16017
16018 return rc;
16019}
16020
16021/**
16022 * tg3_io_resume - called when traffic can start flowing again.
16023 * @pdev: Pointer to PCI device
16024 *
16025 * This callback is called when the error recovery driver tells
16026 * us that its OK to resume normal operation.
16027 */
16028static void tg3_io_resume(struct pci_dev *pdev)
16029{
16030 struct net_device *netdev = pci_get_drvdata(pdev);
16031 struct tg3 *tp = netdev_priv(netdev);
16032 int err;
16033
16034 rtnl_lock();
16035
16036 if (!netif_running(netdev))
16037 goto done;
16038
16039 tg3_full_lock(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +000016040 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016041 err = tg3_restart_hw(tp, 1);
16042 tg3_full_unlock(tp);
16043 if (err) {
16044 netdev_err(netdev, "Cannot restart hardware after reset.\n");
16045 goto done;
16046 }
16047
16048 netif_device_attach(netdev);
16049
Matt Carlson21f76382012-02-22 12:35:21 +000016050 tg3_timer_start(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016051
16052 tg3_netif_start(tp);
16053
16054 tg3_phy_start(tp);
16055
16056done:
16057 rtnl_unlock();
16058}
16059
16060static struct pci_error_handlers tg3_err_handler = {
16061 .error_detected = tg3_io_error_detected,
16062 .slot_reset = tg3_io_slot_reset,
16063 .resume = tg3_io_resume
16064};
16065
Linus Torvalds1da177e2005-04-16 15:20:36 -070016066static struct pci_driver tg3_driver = {
16067 .name = DRV_MODULE_NAME,
16068 .id_table = tg3_pci_tbl,
16069 .probe = tg3_init_one,
16070 .remove = __devexit_p(tg3_remove_one),
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016071 .err_handler = &tg3_err_handler,
Eric Dumazetaa6027c2011-01-01 05:22:46 +000016072 .driver.pm = TG3_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070016073};
16074
16075static int __init tg3_init(void)
16076{
Jeff Garzik29917622006-08-19 17:48:59 -040016077 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016078}
16079
16080static void __exit tg3_cleanup(void)
16081{
16082 pci_unregister_driver(&tg3_driver);
16083}
16084
16085module_init(tg3_init);
16086module_exit(tg3_cleanup);