blob: 76f33d5c72723a2f43864e9044d3e7e5cb5cd56a [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 Carlson65ec6982012-02-28 23:33:37 +00008004static void tg3_get_nstats(struct tg3 *, struct rtnl_link_stats64 *);
8005static void tg3_get_estats(struct tg3 *, struct tg3_ethtool_stats *);
Matt Carlson92feeab2011-12-08 14:40:14 +00008006
Linus Torvalds1da177e2005-04-16 15:20:36 -07008007/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07008008static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008009{
8010 int err;
8011
8012 tg3_stop_fw(tp);
8013
Michael Chan944d9802005-05-29 14:57:48 -07008014 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008015
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008016 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008017 err = tg3_chip_reset(tp);
8018
Matt Carlsondaba2a62009-04-20 06:58:52 +00008019 __tg3_set_mac_addr(tp, 0);
8020
Michael Chan944d9802005-05-29 14:57:48 -07008021 tg3_write_sig_legacy(tp, kind);
8022 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008023
Matt Carlson92feeab2011-12-08 14:40:14 +00008024 if (tp->hw_stats) {
8025 /* Save the stats across chip resets... */
David S. Millerb4017c52012-03-01 17:57:40 -05008026 tg3_get_nstats(tp, &tp->net_stats_prev);
Matt Carlson92feeab2011-12-08 14:40:14 +00008027 tg3_get_estats(tp, &tp->estats_prev);
8028
8029 /* And make sure the next sample is new data */
8030 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
8031 }
8032
Linus Torvalds1da177e2005-04-16 15:20:36 -07008033 if (err)
8034 return err;
8035
8036 return 0;
8037}
8038
Linus Torvalds1da177e2005-04-16 15:20:36 -07008039static int tg3_set_mac_addr(struct net_device *dev, void *p)
8040{
8041 struct tg3 *tp = netdev_priv(dev);
8042 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07008043 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008044
Michael Chanf9804dd2005-09-27 12:13:10 -07008045 if (!is_valid_ether_addr(addr->sa_data))
Danny Kukawka504f9b52012-02-21 02:07:49 +00008046 return -EADDRNOTAVAIL;
Michael Chanf9804dd2005-09-27 12:13:10 -07008047
Linus Torvalds1da177e2005-04-16 15:20:36 -07008048 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
8049
Michael Chane75f7c92006-03-20 21:33:26 -08008050 if (!netif_running(dev))
8051 return 0;
8052
Joe Perches63c3a662011-04-26 08:12:10 +00008053 if (tg3_flag(tp, ENABLE_ASF)) {
Michael Chan986e0ae2007-05-05 12:10:20 -07008054 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07008055
Michael Chan986e0ae2007-05-05 12:10:20 -07008056 addr0_high = tr32(MAC_ADDR_0_HIGH);
8057 addr0_low = tr32(MAC_ADDR_0_LOW);
8058 addr1_high = tr32(MAC_ADDR_1_HIGH);
8059 addr1_low = tr32(MAC_ADDR_1_LOW);
8060
8061 /* Skip MAC addr 1 if ASF is using it. */
8062 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
8063 !(addr1_high == 0 && addr1_low == 0))
8064 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07008065 }
Michael Chan986e0ae2007-05-05 12:10:20 -07008066 spin_lock_bh(&tp->lock);
8067 __tg3_set_mac_addr(tp, skip_mac_1);
8068 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008069
Michael Chanb9ec6c12006-07-25 16:37:27 -07008070 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008071}
8072
8073/* tp->lock is held. */
8074static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
8075 dma_addr_t mapping, u32 maxlen_flags,
8076 u32 nic_addr)
8077{
8078 tg3_write_mem(tp,
8079 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
8080 ((u64) mapping >> 32));
8081 tg3_write_mem(tp,
8082 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
8083 ((u64) mapping & 0xffffffff));
8084 tg3_write_mem(tp,
8085 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
8086 maxlen_flags);
8087
Joe Perches63c3a662011-04-26 08:12:10 +00008088 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008089 tg3_write_mem(tp,
8090 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
8091 nic_addr);
8092}
8093
Michael Chand244c892005-07-05 14:42:33 -07008094static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07008095{
Matt Carlsonb6080e12009-09-01 13:12:00 +00008096 int i;
8097
Joe Perches63c3a662011-04-26 08:12:10 +00008098 if (!tg3_flag(tp, ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00008099 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
8100 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
8101 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008102 } else {
8103 tw32(HOSTCC_TXCOL_TICKS, 0);
8104 tw32(HOSTCC_TXMAX_FRAMES, 0);
8105 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00008106 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008107
Joe Perches63c3a662011-04-26 08:12:10 +00008108 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008109 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
8110 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
8111 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
8112 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00008113 tw32(HOSTCC_RXCOL_TICKS, 0);
8114 tw32(HOSTCC_RXMAX_FRAMES, 0);
8115 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07008116 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008117
Joe Perches63c3a662011-04-26 08:12:10 +00008118 if (!tg3_flag(tp, 5705_PLUS)) {
David S. Miller15f98502005-05-18 22:49:26 -07008119 u32 val = ec->stats_block_coalesce_usecs;
8120
Matt Carlsonb6080e12009-09-01 13:12:00 +00008121 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
8122 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
8123
David S. Miller15f98502005-05-18 22:49:26 -07008124 if (!netif_carrier_ok(tp->dev))
8125 val = 0;
8126
8127 tw32(HOSTCC_STAT_COAL_TICKS, val);
8128 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008129
8130 for (i = 0; i < tp->irq_cnt - 1; i++) {
8131 u32 reg;
8132
8133 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
8134 tw32(reg, ec->rx_coalesce_usecs);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008135 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
8136 tw32(reg, ec->rx_max_coalesced_frames);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008137 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
8138 tw32(reg, ec->rx_max_coalesced_frames_irq);
Matt Carlson19cfaec2009-12-03 08:36:20 +00008139
Joe Perches63c3a662011-04-26 08:12:10 +00008140 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008141 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
8142 tw32(reg, ec->tx_coalesce_usecs);
8143 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
8144 tw32(reg, ec->tx_max_coalesced_frames);
8145 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
8146 tw32(reg, ec->tx_max_coalesced_frames_irq);
8147 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008148 }
8149
8150 for (; i < tp->irq_max - 1; i++) {
8151 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008152 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008153 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00008154
Joe Perches63c3a662011-04-26 08:12:10 +00008155 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008156 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
8157 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
8158 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
8159 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008160 }
David S. Miller15f98502005-05-18 22:49:26 -07008161}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008162
8163/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00008164static void tg3_rings_reset(struct tg3 *tp)
8165{
8166 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008167 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008168 struct tg3_napi *tnapi = &tp->napi[0];
8169
8170 /* Disable all transmit rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008171 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008172 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Joe Perches63c3a662011-04-26 08:12:10 +00008173 else if (tg3_flag(tp, 5717_PLUS))
Matt Carlson3d377282010-10-14 10:37:39 +00008174 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
Matt Carlson55086ad2011-12-14 11:09:59 +00008175 else if (tg3_flag(tp, 57765_CLASS))
Matt Carlsonb703df62009-12-03 08:36:21 +00008176 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008177 else
8178 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8179
8180 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8181 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
8182 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
8183 BDINFO_FLAGS_DISABLED);
8184
8185
8186 /* Disable all receive return rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008187 if (tg3_flag(tp, 5717_PLUS))
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008188 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
Joe Perches63c3a662011-04-26 08:12:10 +00008189 else if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008190 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00008191 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00008192 tg3_flag(tp, 57765_CLASS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008193 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
8194 else
8195 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8196
8197 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8198 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
8199 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
8200 BDINFO_FLAGS_DISABLED);
8201
8202 /* Disable interrupts */
8203 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008204 tp->napi[0].chk_msi_cnt = 0;
8205 tp->napi[0].last_rx_cons = 0;
8206 tp->napi[0].last_tx_cons = 0;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008207
8208 /* Zero mailbox registers. */
Joe Perches63c3a662011-04-26 08:12:10 +00008209 if (tg3_flag(tp, SUPPORT_MSIX)) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00008210 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008211 tp->napi[i].tx_prod = 0;
8212 tp->napi[i].tx_cons = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00008213 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008214 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008215 tw32_rx_mbox(tp->napi[i].consmbox, 0);
8216 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
Matt Carlson7f230732011-08-31 11:44:48 +00008217 tp->napi[i].chk_msi_cnt = 0;
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008218 tp->napi[i].last_rx_cons = 0;
8219 tp->napi[i].last_tx_cons = 0;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008220 }
Joe Perches63c3a662011-04-26 08:12:10 +00008221 if (!tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008222 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008223 } else {
8224 tp->napi[0].tx_prod = 0;
8225 tp->napi[0].tx_cons = 0;
8226 tw32_mailbox(tp->napi[0].prodmbox, 0);
8227 tw32_rx_mbox(tp->napi[0].consmbox, 0);
8228 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008229
8230 /* Make sure the NIC-based send BD rings are disabled. */
Joe Perches63c3a662011-04-26 08:12:10 +00008231 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson2d31eca2009-09-01 12:53:31 +00008232 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
8233 for (i = 0; i < 16; i++)
8234 tw32_tx_mbox(mbox + i * 8, 0);
8235 }
8236
8237 txrcb = NIC_SRAM_SEND_RCB;
8238 rxrcb = NIC_SRAM_RCV_RET_RCB;
8239
8240 /* Clear status block in ram. */
8241 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8242
8243 /* Set status block DMA address */
8244 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8245 ((u64) tnapi->status_mapping >> 32));
8246 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8247 ((u64) tnapi->status_mapping & 0xffffffff));
8248
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008249 if (tnapi->tx_ring) {
8250 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8251 (TG3_TX_RING_SIZE <<
8252 BDINFO_FLAGS_MAXLEN_SHIFT),
8253 NIC_SRAM_TX_BUFFER_DESC);
8254 txrcb += TG3_BDINFO_SIZE;
8255 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008256
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008257 if (tnapi->rx_rcb) {
8258 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008259 (tp->rx_ret_ring_mask + 1) <<
8260 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008261 rxrcb += TG3_BDINFO_SIZE;
8262 }
8263
8264 stblk = HOSTCC_STATBLCK_RING1;
8265
8266 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
8267 u64 mapping = (u64)tnapi->status_mapping;
8268 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
8269 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
8270
8271 /* Clear status block in ram. */
8272 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8273
Matt Carlson19cfaec2009-12-03 08:36:20 +00008274 if (tnapi->tx_ring) {
8275 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8276 (TG3_TX_RING_SIZE <<
8277 BDINFO_FLAGS_MAXLEN_SHIFT),
8278 NIC_SRAM_TX_BUFFER_DESC);
8279 txrcb += TG3_BDINFO_SIZE;
8280 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008281
8282 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008283 ((tp->rx_ret_ring_mask + 1) <<
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008284 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
8285
8286 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008287 rxrcb += TG3_BDINFO_SIZE;
8288 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008289}
8290
Matt Carlsoneb07a942011-04-20 07:57:36 +00008291static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
8292{
8293 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
8294
Joe Perches63c3a662011-04-26 08:12:10 +00008295 if (!tg3_flag(tp, 5750_PLUS) ||
8296 tg3_flag(tp, 5780_CLASS) ||
Matt Carlsoneb07a942011-04-20 07:57:36 +00008297 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
Matt Carlson513aa6e2011-11-21 15:01:18 +00008298 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
8299 tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008300 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
8301 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
8302 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787)
8303 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
8304 else
8305 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
8306
8307 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
8308 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
8309
8310 val = min(nic_rep_thresh, host_rep_thresh);
8311 tw32(RCVBDI_STD_THRESH, val);
8312
Joe Perches63c3a662011-04-26 08:12:10 +00008313 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008314 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
8315
Joe Perches63c3a662011-04-26 08:12:10 +00008316 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008317 return;
8318
Matt Carlson513aa6e2011-11-21 15:01:18 +00008319 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
Matt Carlsoneb07a942011-04-20 07:57:36 +00008320
8321 host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
8322
8323 val = min(bdcache_maxcnt / 2, host_rep_thresh);
8324 tw32(RCVBDI_JUMBO_THRESH, val);
8325
Joe Perches63c3a662011-04-26 08:12:10 +00008326 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008327 tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
8328}
8329
Matt Carlsonccd5ba92012-02-13 10:20:08 +00008330static inline u32 calc_crc(unsigned char *buf, int len)
8331{
8332 u32 reg;
8333 u32 tmp;
8334 int j, k;
8335
8336 reg = 0xffffffff;
8337
8338 for (j = 0; j < len; j++) {
8339 reg ^= buf[j];
8340
8341 for (k = 0; k < 8; k++) {
8342 tmp = reg & 0x01;
8343
8344 reg >>= 1;
8345
8346 if (tmp)
8347 reg ^= 0xedb88320;
8348 }
8349 }
8350
8351 return ~reg;
8352}
8353
8354static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
8355{
8356 /* accept or reject all multicast frames */
8357 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
8358 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
8359 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
8360 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
8361}
8362
8363static void __tg3_set_rx_mode(struct net_device *dev)
8364{
8365 struct tg3 *tp = netdev_priv(dev);
8366 u32 rx_mode;
8367
8368 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
8369 RX_MODE_KEEP_VLAN_TAG);
8370
8371#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
8372 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
8373 * flag clear.
8374 */
8375 if (!tg3_flag(tp, ENABLE_ASF))
8376 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
8377#endif
8378
8379 if (dev->flags & IFF_PROMISC) {
8380 /* Promiscuous mode. */
8381 rx_mode |= RX_MODE_PROMISC;
8382 } else if (dev->flags & IFF_ALLMULTI) {
8383 /* Accept all multicast. */
8384 tg3_set_multi(tp, 1);
8385 } else if (netdev_mc_empty(dev)) {
8386 /* Reject all multicast. */
8387 tg3_set_multi(tp, 0);
8388 } else {
8389 /* Accept one or more multicast(s). */
8390 struct netdev_hw_addr *ha;
8391 u32 mc_filter[4] = { 0, };
8392 u32 regidx;
8393 u32 bit;
8394 u32 crc;
8395
8396 netdev_for_each_mc_addr(ha, dev) {
8397 crc = calc_crc(ha->addr, ETH_ALEN);
8398 bit = ~crc & 0x7f;
8399 regidx = (bit & 0x60) >> 5;
8400 bit &= 0x1f;
8401 mc_filter[regidx] |= (1 << bit);
8402 }
8403
8404 tw32(MAC_HASH_REG_0, mc_filter[0]);
8405 tw32(MAC_HASH_REG_1, mc_filter[1]);
8406 tw32(MAC_HASH_REG_2, mc_filter[2]);
8407 tw32(MAC_HASH_REG_3, mc_filter[3]);
8408 }
8409
8410 if (rx_mode != tp->rx_mode) {
8411 tp->rx_mode = rx_mode;
8412 tw32_f(MAC_RX_MODE, rx_mode);
8413 udelay(10);
8414 }
8415}
8416
Matt Carlson90415472011-12-16 13:33:23 +00008417static void tg3_rss_init_dflt_indir_tbl(struct tg3 *tp)
8418{
8419 int i;
8420
8421 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
8422 tp->rss_ind_tbl[i] =
8423 ethtool_rxfh_indir_default(i, tp->irq_cnt - 1);
8424}
8425
8426static void tg3_rss_check_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008427{
8428 int i;
8429
8430 if (!tg3_flag(tp, SUPPORT_MSIX))
8431 return;
8432
Matt Carlson90415472011-12-16 13:33:23 +00008433 if (tp->irq_cnt <= 2) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008434 memset(&tp->rss_ind_tbl[0], 0, sizeof(tp->rss_ind_tbl));
Matt Carlson90415472011-12-16 13:33:23 +00008435 return;
8436 }
8437
8438 /* Validate table against current IRQ count */
8439 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
8440 if (tp->rss_ind_tbl[i] >= tp->irq_cnt - 1)
8441 break;
8442 }
8443
8444 if (i != TG3_RSS_INDIR_TBL_SIZE)
8445 tg3_rss_init_dflt_indir_tbl(tp);
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008446}
8447
Matt Carlson90415472011-12-16 13:33:23 +00008448static void tg3_rss_write_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008449{
8450 int i = 0;
8451 u32 reg = MAC_RSS_INDIR_TBL_0;
8452
8453 while (i < TG3_RSS_INDIR_TBL_SIZE) {
8454 u32 val = tp->rss_ind_tbl[i];
8455 i++;
8456 for (; i % 8; i++) {
8457 val <<= 4;
8458 val |= tp->rss_ind_tbl[i];
8459 }
8460 tw32(reg, val);
8461 reg += 4;
8462 }
8463}
8464
Matt Carlson2d31eca2009-09-01 12:53:31 +00008465/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008466static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008467{
8468 u32 val, rdmac_mode;
8469 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00008470 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008471
8472 tg3_disable_ints(tp);
8473
8474 tg3_stop_fw(tp);
8475
8476 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
8477
Joe Perches63c3a662011-04-26 08:12:10 +00008478 if (tg3_flag(tp, INIT_COMPLETE))
Michael Chane6de8ad2005-05-05 14:42:41 -07008479 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008480
Matt Carlson699c0192010-12-06 08:28:51 +00008481 /* Enable MAC control of LPI */
8482 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
8483 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL,
8484 TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
8485 TG3_CPMU_EEE_LNKIDL_UART_IDL);
8486
8487 tw32_f(TG3_CPMU_EEE_CTRL,
8488 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
8489
Matt Carlsona386b902010-12-06 08:28:53 +00008490 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
8491 TG3_CPMU_EEEMD_LPI_IN_TX |
8492 TG3_CPMU_EEEMD_LPI_IN_RX |
8493 TG3_CPMU_EEEMD_EEE_ENABLE;
8494
8495 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8496 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
8497
Joe Perches63c3a662011-04-26 08:12:10 +00008498 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsona386b902010-12-06 08:28:53 +00008499 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
8500
8501 tw32_f(TG3_CPMU_EEE_MODE, val);
8502
8503 tw32_f(TG3_CPMU_EEE_DBTMR1,
8504 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
8505 TG3_CPMU_DBTMR1_LNKIDLE_2047US);
8506
8507 tw32_f(TG3_CPMU_EEE_DBTMR2,
Matt Carlsond7f2ab22011-01-25 15:58:56 +00008508 TG3_CPMU_DBTMR2_APE_TX_2047US |
Matt Carlsona386b902010-12-06 08:28:53 +00008509 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
Matt Carlson699c0192010-12-06 08:28:51 +00008510 }
8511
Matt Carlson603f1172010-02-12 14:47:10 +00008512 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08008513 tg3_phy_reset(tp);
8514
Linus Torvalds1da177e2005-04-16 15:20:36 -07008515 err = tg3_chip_reset(tp);
8516 if (err)
8517 return err;
8518
8519 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
8520
Matt Carlsonbcb37f62008-11-03 16:52:09 -08008521 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008522 val = tr32(TG3_CPMU_CTRL);
8523 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
8524 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08008525
8526 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8527 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8528 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8529 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
8530
8531 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
8532 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
8533 val |= CPMU_LNK_AWARE_MACCLK_6_25;
8534 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
8535
8536 val = tr32(TG3_CPMU_HST_ACC);
8537 val &= ~CPMU_HST_ACC_MACCLK_MASK;
8538 val |= CPMU_HST_ACC_MACCLK_6_25;
8539 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07008540 }
8541
Matt Carlson33466d932009-04-20 06:57:41 +00008542 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
8543 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
8544 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
8545 PCIE_PWR_MGMT_L1_THRESH_4MS;
8546 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00008547
8548 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
8549 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
8550
8551 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d932009-04-20 06:57:41 +00008552
Matt Carlsonf40386c2009-11-02 14:24:02 +00008553 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
8554 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00008555 }
8556
Joe Perches63c3a662011-04-26 08:12:10 +00008557 if (tg3_flag(tp, L1PLLPD_EN)) {
Matt Carlson614b0592010-01-20 16:58:02 +00008558 u32 grc_mode = tr32(GRC_MODE);
8559
8560 /* Access the lower 1K of PL PCIE block registers. */
8561 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8562 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
8563
8564 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
8565 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
8566 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
8567
8568 tw32(GRC_MODE, grc_mode);
8569 }
8570
Matt Carlson55086ad2011-12-14 11:09:59 +00008571 if (tg3_flag(tp, 57765_CLASS)) {
Matt Carlson5093eed2010-11-24 08:31:45 +00008572 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
8573 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00008574
Matt Carlson5093eed2010-11-24 08:31:45 +00008575 /* Access the lower 1K of PL PCIE block registers. */
8576 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8577 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00008578
Matt Carlson5093eed2010-11-24 08:31:45 +00008579 val = tr32(TG3_PCIE_TLDLPL_PORT +
8580 TG3_PCIE_PL_LO_PHYCTL5);
8581 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
8582 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00008583
Matt Carlson5093eed2010-11-24 08:31:45 +00008584 tw32(GRC_MODE, grc_mode);
8585 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00008586
Matt Carlson1ff30a52011-05-19 12:12:46 +00008587 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_57765_AX) {
8588 u32 grc_mode = tr32(GRC_MODE);
8589
8590 /* Access the lower 1K of DL PCIE block registers. */
8591 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8592 tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
8593
8594 val = tr32(TG3_PCIE_TLDLPL_PORT +
8595 TG3_PCIE_DL_LO_FTSMAX);
8596 val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
8597 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
8598 val | TG3_PCIE_DL_LO_FTSMAX_VAL);
8599
8600 tw32(GRC_MODE, grc_mode);
8601 }
8602
Matt Carlsona977dbe2010-04-12 06:58:26 +00008603 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8604 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8605 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8606 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00008607 }
8608
Linus Torvalds1da177e2005-04-16 15:20:36 -07008609 /* This works around an issue with Athlon chipsets on
8610 * B3 tigon3 silicon. This bit has no effect on any
8611 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07008612 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008613 */
Joe Perches63c3a662011-04-26 08:12:10 +00008614 if (!tg3_flag(tp, CPMU_PRESENT)) {
8615 if (!tg3_flag(tp, PCI_EXPRESS))
Matt Carlson795d01c2007-10-07 23:28:17 -07008616 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
8617 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
8618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008619
8620 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008621 tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008622 val = tr32(TG3PCI_PCISTATE);
8623 val |= PCISTATE_RETRY_SAME_DMA;
8624 tw32(TG3PCI_PCISTATE, val);
8625 }
8626
Joe Perches63c3a662011-04-26 08:12:10 +00008627 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -07008628 /* Allow reads and writes to the
8629 * APE register and memory space.
8630 */
8631 val = tr32(TG3PCI_PCISTATE);
8632 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00008633 PCISTATE_ALLOW_APE_SHMEM_WR |
8634 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008635 tw32(TG3PCI_PCISTATE, val);
8636 }
8637
Linus Torvalds1da177e2005-04-16 15:20:36 -07008638 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
8639 /* Enable some hw fixes. */
8640 val = tr32(TG3PCI_MSI_DATA);
8641 val |= (1 << 26) | (1 << 28) | (1 << 29);
8642 tw32(TG3PCI_MSI_DATA, val);
8643 }
8644
8645 /* Descriptor ring init may make accesses to the
8646 * NIC SRAM area to setup the TX descriptors, so we
8647 * can only do this after the hardware has been
8648 * successfully reset.
8649 */
Michael Chan32d8c572006-07-25 16:38:29 -07008650 err = tg3_init_rings(tp);
8651 if (err)
8652 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008653
Joe Perches63c3a662011-04-26 08:12:10 +00008654 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008655 val = tr32(TG3PCI_DMA_RW_CTRL) &
8656 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Matt Carlson1a319022010-04-12 06:58:25 +00008657 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
8658 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlson55086ad2011-12-14 11:09:59 +00008659 if (!tg3_flag(tp, 57765_CLASS) &&
Matt Carlson0aebff42011-04-25 12:42:45 +00008660 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8661 val |= DMA_RWCTRL_TAGGED_STAT_WA;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008662 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
8663 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
8664 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008665 /* This value is determined during the probe time DMA
8666 * engine test, tg3_test_dma.
8667 */
8668 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
8669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008670
8671 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
8672 GRC_MODE_4X_NIC_SEND_RINGS |
8673 GRC_MODE_NO_TX_PHDR_CSUM |
8674 GRC_MODE_NO_RX_PHDR_CSUM);
8675 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07008676
8677 /* Pseudo-header checksum is done by hardware logic and not
8678 * the offload processers, so make the chip do the pseudo-
8679 * header checksums on receive. For transmit it is more
8680 * convenient to do the pseudo-header checksum in software
8681 * as Linux does that on transmit for us in all cases.
8682 */
8683 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008684
8685 tw32(GRC_MODE,
8686 tp->grc_mode |
8687 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
8688
8689 /* Setup the timer prescalar register. Clock is always 66Mhz. */
8690 val = tr32(GRC_MISC_CFG);
8691 val &= ~0xff;
8692 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
8693 tw32(GRC_MISC_CFG, val);
8694
8695 /* Initialize MBUF/DESC pool. */
Joe Perches63c3a662011-04-26 08:12:10 +00008696 if (tg3_flag(tp, 5750_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008697 /* Do nothing. */
8698 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
8699 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
8700 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
8701 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
8702 else
8703 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
8704 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
8705 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Joe Perches63c3a662011-04-26 08:12:10 +00008706 } else if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008707 int fw_len;
8708
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08008709 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008710 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
8711 tw32(BUFMGR_MB_POOL_ADDR,
8712 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
8713 tw32(BUFMGR_MB_POOL_SIZE,
8714 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
8715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008716
Michael Chan0f893dc2005-07-25 12:30:38 -07008717 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008718 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8719 tp->bufmgr_config.mbuf_read_dma_low_water);
8720 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8721 tp->bufmgr_config.mbuf_mac_rx_low_water);
8722 tw32(BUFMGR_MB_HIGH_WATER,
8723 tp->bufmgr_config.mbuf_high_water);
8724 } else {
8725 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8726 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
8727 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8728 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
8729 tw32(BUFMGR_MB_HIGH_WATER,
8730 tp->bufmgr_config.mbuf_high_water_jumbo);
8731 }
8732 tw32(BUFMGR_DMA_LOW_WATER,
8733 tp->bufmgr_config.dma_low_water);
8734 tw32(BUFMGR_DMA_HIGH_WATER,
8735 tp->bufmgr_config.dma_high_water);
8736
Matt Carlsond309a462010-09-30 10:34:31 +00008737 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
8738 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
8739 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
Matt Carlson4d958472011-04-20 07:57:35 +00008740 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
8741 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
8742 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0)
8743 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
Matt Carlsond309a462010-09-30 10:34:31 +00008744 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008745 for (i = 0; i < 2000; i++) {
8746 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
8747 break;
8748 udelay(10);
8749 }
8750 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00008751 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008752 return -ENODEV;
8753 }
8754
Matt Carlsoneb07a942011-04-20 07:57:36 +00008755 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
8756 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
Michael Chanb5d37722006-09-27 16:06:21 -07008757
Matt Carlsoneb07a942011-04-20 07:57:36 +00008758 tg3_setup_rxbd_thresholds(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008759
8760 /* Initialize TG3_BDINFO's at:
8761 * RCVDBDI_STD_BD: standard eth size rx ring
8762 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
8763 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
8764 *
8765 * like so:
8766 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
8767 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
8768 * ring attribute flags
8769 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
8770 *
8771 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
8772 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
8773 *
8774 * The size of each ring is fixed in the firmware, but the location is
8775 * configurable.
8776 */
8777 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008778 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008779 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008780 ((u64) tpr->rx_std_mapping & 0xffffffff));
Joe Perches63c3a662011-04-26 08:12:10 +00008781 if (!tg3_flag(tp, 5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +00008782 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
8783 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008784
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008785 /* Disable the mini ring */
Joe Perches63c3a662011-04-26 08:12:10 +00008786 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008787 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
8788 BDINFO_FLAGS_DISABLED);
8789
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008790 /* Program the jumbo buffer descriptor ring control
8791 * blocks on those devices that have them.
8792 */
Matt Carlsona0512942011-07-27 14:20:54 +00008793 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008794 (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008795
Joe Perches63c3a662011-04-26 08:12:10 +00008796 if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008797 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008798 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008799 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008800 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +00008801 val = TG3_RX_JMB_RING_SIZE(tp) <<
8802 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008803 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +00008804 val | BDINFO_FLAGS_USE_EXT_RECV);
Joe Perches63c3a662011-04-26 08:12:10 +00008805 if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
Matt Carlson55086ad2011-12-14 11:09:59 +00008806 tg3_flag(tp, 57765_CLASS))
Matt Carlson87668d32009-11-13 13:03:34 +00008807 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
8808 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008809 } else {
8810 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
8811 BDINFO_FLAGS_DISABLED);
8812 }
8813
Joe Perches63c3a662011-04-26 08:12:10 +00008814 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonfa6b2aa2011-11-21 15:01:19 +00008815 val = TG3_RX_STD_RING_SIZE(tp);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008816 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
8817 val |= (TG3_RX_STD_DMA_SZ << 2);
8818 } else
Matt Carlson04380d42010-04-12 06:58:29 +00008819 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008820 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008821 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008822
8823 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008824
Matt Carlson411da642009-11-13 13:03:46 +00008825 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e662009-11-13 13:03:49 +00008826 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008827
Joe Perches63c3a662011-04-26 08:12:10 +00008828 tpr->rx_jmb_prod_idx =
8829 tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
Matt Carlson66711e662009-11-13 13:03:49 +00008830 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008831
Matt Carlson2d31eca2009-09-01 12:53:31 +00008832 tg3_rings_reset(tp);
8833
Linus Torvalds1da177e2005-04-16 15:20:36 -07008834 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07008835 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008836
8837 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00008838 tw32(MAC_RX_MTU_SIZE,
8839 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008840
8841 /* The slot time is changed by tg3_setup_phy if we
8842 * run at gigabit with half duplex.
8843 */
Matt Carlsonf2096f92011-04-05 14:22:48 +00008844 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
8845 (6 << TX_LENGTHS_IPG_SHIFT) |
8846 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
8847
8848 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8849 val |= tr32(MAC_TX_LENGTHS) &
8850 (TX_LENGTHS_JMB_FRM_LEN_MSK |
8851 TX_LENGTHS_CNT_DWN_VAL_MSK);
8852
8853 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008854
8855 /* Receive rules. */
8856 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
8857 tw32(RCVLPC_CONFIG, 0x0181);
8858
8859 /* Calculate RDMAC_MODE setting early, we need it to determine
8860 * the RCVLPC_STATE_ENABLE mask.
8861 */
8862 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
8863 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
8864 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
8865 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
8866 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07008867
Matt Carlsondeabaac2010-11-24 08:31:50 +00008868 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +00008869 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
8870
Matt Carlson57e69832008-05-25 23:48:31 -07008871 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08008872 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8873 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07008874 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
8875 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
8876 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
8877
Matt Carlsonc5908932011-03-09 16:58:25 +00008878 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8879 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00008880 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07008881 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008882 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
8883 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008884 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008885 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8886 }
8887 }
8888
Joe Perches63c3a662011-04-26 08:12:10 +00008889 if (tg3_flag(tp, PCI_EXPRESS))
Michael Chan85e94ce2005-04-21 17:05:28 -07008890 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8891
Joe Perches63c3a662011-04-26 08:12:10 +00008892 if (tg3_flag(tp, HW_TSO_1) ||
8893 tg3_flag(tp, HW_TSO_2) ||
8894 tg3_flag(tp, HW_TSO_3))
Matt Carlson027455a2008-12-21 20:19:30 -08008895 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
8896
Matt Carlson108a6c12011-05-19 12:12:47 +00008897 if (tg3_flag(tp, 57765_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +00008898 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson027455a2008-12-21 20:19:30 -08008899 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
8900 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008901
Matt Carlsonf2096f92011-04-05 14:22:48 +00008902 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8903 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
8904
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008905 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
8906 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
8907 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8908 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008909 tg3_flag(tp, 57765_PLUS)) {
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008910 val = tr32(TG3_RDMA_RSRVCTRL_REG);
Matt Carlsond78b59f2011-04-05 14:22:46 +00008911 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8912 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +00008913 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
8914 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
8915 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
8916 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
8917 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
8918 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +00008919 }
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008920 tw32(TG3_RDMA_RSRVCTRL_REG,
8921 val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
8922 }
8923
Matt Carlsond78b59f2011-04-05 14:22:46 +00008924 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8925 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsond309a462010-09-30 10:34:31 +00008926 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
8927 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val |
8928 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
8929 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
8930 }
8931
Linus Torvalds1da177e2005-04-16 15:20:36 -07008932 /* Receive/send statistics. */
Joe Perches63c3a662011-04-26 08:12:10 +00008933 if (tg3_flag(tp, 5750_PLUS)) {
Michael Chan16613942006-06-29 20:15:13 -07008934 val = tr32(RCVLPC_STATS_ENABLE);
8935 val &= ~RCVLPC_STATSENAB_DACK_FIX;
8936 tw32(RCVLPC_STATS_ENABLE, val);
8937 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008938 tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008939 val = tr32(RCVLPC_STATS_ENABLE);
8940 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
8941 tw32(RCVLPC_STATS_ENABLE, val);
8942 } else {
8943 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
8944 }
8945 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
8946 tw32(SNDDATAI_STATSENAB, 0xffffff);
8947 tw32(SNDDATAI_STATSCTRL,
8948 (SNDDATAI_SCTRL_ENABLE |
8949 SNDDATAI_SCTRL_FASTUPD));
8950
8951 /* Setup host coalescing engine. */
8952 tw32(HOSTCC_MODE, 0);
8953 for (i = 0; i < 2000; i++) {
8954 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
8955 break;
8956 udelay(10);
8957 }
8958
Michael Chand244c892005-07-05 14:42:33 -07008959 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008960
Joe Perches63c3a662011-04-26 08:12:10 +00008961 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008962 /* Status/statistics block address. See tg3_timer,
8963 * the tg3_periodic_fetch_stats call there, and
8964 * tg3_get_stats to see how this works for 5705/5750 chips.
8965 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008966 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8967 ((u64) tp->stats_mapping >> 32));
8968 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8969 ((u64) tp->stats_mapping & 0xffffffff));
8970 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008971
Linus Torvalds1da177e2005-04-16 15:20:36 -07008972 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008973
8974 /* Clear statistics and status block memory areas */
8975 for (i = NIC_SRAM_STATS_BLK;
8976 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
8977 i += sizeof(u32)) {
8978 tg3_write_mem(tp, i, 0);
8979 udelay(40);
8980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008981 }
8982
8983 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
8984
8985 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
8986 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008987 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008988 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
8989
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008990 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
8991 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -07008992 /* reset to prevent losing 1st rx packet intermittently */
8993 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8994 udelay(10);
8995 }
8996
Matt Carlson3bda1252008-08-15 14:08:22 -07008997 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Matt Carlson9e975cc2011-07-20 10:20:50 +00008998 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE |
8999 MAC_MODE_FHDE_ENABLE;
9000 if (tg3_flag(tp, ENABLE_APE))
9001 tp->mac_mode |= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Joe Perches63c3a662011-04-26 08:12:10 +00009002 if (!tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009003 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07009004 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
9005 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009006 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
9007 udelay(40);
9008
Michael Chan314fba32005-04-21 17:07:04 -07009009 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Joe Perches63c3a662011-04-26 08:12:10 +00009010 * If TG3_FLAG_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07009011 * register to preserve the GPIO settings for LOMs. The GPIOs,
9012 * whether used as inputs or outputs, are set by boot code after
9013 * reset.
9014 */
Joe Perches63c3a662011-04-26 08:12:10 +00009015 if (!tg3_flag(tp, IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07009016 u32 gpio_mask;
9017
Michael Chan9d26e212006-12-07 00:21:14 -08009018 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
9019 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
9020 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07009021
9022 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
9023 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
9024 GRC_LCLCTRL_GPIO_OUTPUT3;
9025
Michael Chanaf36e6b2006-03-23 01:28:06 -08009026 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
9027 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
9028
Gary Zambranoaaf84462007-05-05 11:51:45 -07009029 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07009030 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
9031
9032 /* GPIO1 must be driven high for eeprom write protect */
Joe Perches63c3a662011-04-26 08:12:10 +00009033 if (tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan9d26e212006-12-07 00:21:14 -08009034 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
9035 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07009036 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009037 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
9038 udelay(100);
9039
Matt Carlsonc3b50032012-01-17 15:27:23 +00009040 if (tg3_flag(tp, USING_MSIX)) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009041 val = tr32(MSGINT_MODE);
Matt Carlsonc3b50032012-01-17 15:27:23 +00009042 val |= MSGINT_MODE_ENABLE;
9043 if (tp->irq_cnt > 1)
9044 val |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +00009045 if (!tg3_flag(tp, 1SHOT_MSI))
9046 val |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009047 tw32(MSGINT_MODE, val);
9048 }
9049
Joe Perches63c3a662011-04-26 08:12:10 +00009050 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009051 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
9052 udelay(40);
9053 }
9054
9055 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
9056 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
9057 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
9058 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
9059 WDMAC_MODE_LNGREAD_ENAB);
9060
Matt Carlsonc5908932011-03-09 16:58:25 +00009061 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
9062 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00009063 if (tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009064 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
9065 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
9066 /* nothing */
9067 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009068 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009069 val |= WDMAC_MODE_RX_ACCEL;
9070 }
9071 }
9072
Michael Chand9ab5ad12006-03-20 22:27:35 -08009073 /* Enable host coalescing bug fix */
Joe Perches63c3a662011-04-26 08:12:10 +00009074 if (tg3_flag(tp, 5755_PLUS))
Matt Carlsonf51f3562008-05-25 23:45:08 -07009075 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad12006-03-20 22:27:35 -08009076
Matt Carlson788a0352009-11-02 14:26:03 +00009077 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
9078 val |= WDMAC_MODE_BURST_ALL_DATA;
9079
Linus Torvalds1da177e2005-04-16 15:20:36 -07009080 tw32_f(WDMAC_MODE, val);
9081 udelay(40);
9082
Joe Perches63c3a662011-04-26 08:12:10 +00009083 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07009084 u16 pcix_cmd;
9085
9086 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
9087 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009088 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07009089 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
9090 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009091 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07009092 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
9093 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009094 }
Matt Carlson9974a352007-10-07 23:27:28 -07009095 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
9096 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009097 }
9098
9099 tw32_f(RDMAC_MODE, rdmac_mode);
9100 udelay(40);
9101
9102 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00009103 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009104 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07009105
9106 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
9107 tw32(SNDDATAC_MODE,
9108 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
9109 else
9110 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
9111
Linus Torvalds1da177e2005-04-16 15:20:36 -07009112 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
9113 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00009114 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00009115 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlson7cb32cf2010-09-30 10:34:36 +00009116 val |= RCVDBDI_MODE_LRG_RING_SZ;
9117 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009118 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00009119 if (tg3_flag(tp, HW_TSO_1) ||
9120 tg3_flag(tp, HW_TSO_2) ||
9121 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009122 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009123 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00009124 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009125 val |= SNDBDI_MODE_MULTI_TXQ_EN;
9126 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009127 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
9128
9129 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
9130 err = tg3_load_5701_a0_firmware_fix(tp);
9131 if (err)
9132 return err;
9133 }
9134
Joe Perches63c3a662011-04-26 08:12:10 +00009135 if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009136 err = tg3_load_tso_firmware(tp);
9137 if (err)
9138 return err;
9139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009140
9141 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +00009142
Joe Perches63c3a662011-04-26 08:12:10 +00009143 if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsonb1d05212010-06-05 17:24:31 +00009144 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
9145 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +00009146
9147 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
9148 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
9149 tp->tx_mode &= ~val;
9150 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
9151 }
9152
Linus Torvalds1da177e2005-04-16 15:20:36 -07009153 tw32_f(MAC_TX_MODE, tp->tx_mode);
9154 udelay(100);
9155
Joe Perches63c3a662011-04-26 08:12:10 +00009156 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009157 tg3_rss_write_indir_tbl(tp);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009158
9159 /* Setup the "secret" hash key. */
9160 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
9161 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
9162 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
9163 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
9164 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
9165 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
9166 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
9167 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
9168 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
9169 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
9170 }
9171
Linus Torvalds1da177e2005-04-16 15:20:36 -07009172 tp->rx_mode = RX_MODE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00009173 if (tg3_flag(tp, 5755_PLUS))
Michael Chanaf36e6b2006-03-23 01:28:06 -08009174 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
9175
Joe Perches63c3a662011-04-26 08:12:10 +00009176 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009177 tp->rx_mode |= RX_MODE_RSS_ENABLE |
9178 RX_MODE_RSS_ITBL_HASH_BITS_7 |
9179 RX_MODE_RSS_IPV6_HASH_EN |
9180 RX_MODE_RSS_TCP_IPV6_HASH_EN |
9181 RX_MODE_RSS_IPV4_HASH_EN |
9182 RX_MODE_RSS_TCP_IPV4_HASH_EN;
9183
Linus Torvalds1da177e2005-04-16 15:20:36 -07009184 tw32_f(MAC_RX_MODE, tp->rx_mode);
9185 udelay(10);
9186
Linus Torvalds1da177e2005-04-16 15:20:36 -07009187 tw32(MAC_LED_CTRL, tp->led_ctrl);
9188
9189 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009190 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009191 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
9192 udelay(10);
9193 }
9194 tw32_f(MAC_RX_MODE, tp->rx_mode);
9195 udelay(10);
9196
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009197 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009198 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009199 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009200 /* Set drive transmission level to 1.2V */
9201 /* only if the signal pre-emphasis bit is not set */
9202 val = tr32(MAC_SERDES_CFG);
9203 val &= 0xfffff000;
9204 val |= 0x880;
9205 tw32(MAC_SERDES_CFG, val);
9206 }
9207 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
9208 tw32(MAC_SERDES_CFG, 0x616000);
9209 }
9210
9211 /* Prevent chip from dropping frames when flow control
9212 * is enabled.
9213 */
Matt Carlson55086ad2011-12-14 11:09:59 +00009214 if (tg3_flag(tp, 57765_CLASS))
Matt Carlson666bc832010-01-20 16:58:03 +00009215 val = 1;
9216 else
9217 val = 2;
9218 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009219
9220 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009221 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009222 /* Use hardware link auto-negotiation */
Joe Perches63c3a662011-04-26 08:12:10 +00009223 tg3_flag_set(tp, HW_AUTONEG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009224 }
9225
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009226 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +00009227 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08009228 u32 tmp;
9229
9230 tmp = tr32(SERDES_RX_CTRL);
9231 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
9232 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
9233 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
9234 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
9235 }
9236
Joe Perches63c3a662011-04-26 08:12:10 +00009237 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonc6700ce2012-02-13 15:20:15 +00009238 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Matt Carlson80096062010-08-02 11:26:06 +00009239 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009240
Matt Carlsondd477002008-05-25 23:45:58 -07009241 err = tg3_setup_phy(tp, 0);
9242 if (err)
9243 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009244
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009245 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
9246 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07009247 u32 tmp;
9248
9249 /* Clear CRC stats. */
9250 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
9251 tg3_writephy(tp, MII_TG3_TEST1,
9252 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009253 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -07009254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009255 }
9256 }
9257
9258 __tg3_set_rx_mode(tp->dev);
9259
9260 /* Initialize receive rules. */
9261 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
9262 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
9263 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
9264 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
9265
Joe Perches63c3a662011-04-26 08:12:10 +00009266 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009267 limit = 8;
9268 else
9269 limit = 16;
Joe Perches63c3a662011-04-26 08:12:10 +00009270 if (tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009271 limit -= 4;
9272 switch (limit) {
9273 case 16:
9274 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
9275 case 15:
9276 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
9277 case 14:
9278 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
9279 case 13:
9280 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
9281 case 12:
9282 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
9283 case 11:
9284 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
9285 case 10:
9286 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
9287 case 9:
9288 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
9289 case 8:
9290 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
9291 case 7:
9292 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
9293 case 6:
9294 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
9295 case 5:
9296 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
9297 case 4:
9298 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
9299 case 3:
9300 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
9301 case 2:
9302 case 1:
9303
9304 default:
9305 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07009306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009307
Joe Perches63c3a662011-04-26 08:12:10 +00009308 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson9ce768e2007-10-11 19:49:11 -07009309 /* Write our heartbeat update interval to APE. */
9310 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
9311 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07009312
Linus Torvalds1da177e2005-04-16 15:20:36 -07009313 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
9314
Linus Torvalds1da177e2005-04-16 15:20:36 -07009315 return 0;
9316}
9317
9318/* Called at device open time to get the chip ready for
9319 * packet processing. Invoked with tp->lock held.
9320 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009321static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009322{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009323 tg3_switch_clocks(tp);
9324
9325 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
9326
Matt Carlson2f751b62008-08-04 23:17:34 -07009327 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009328}
9329
9330#define TG3_STAT_ADD32(PSTAT, REG) \
9331do { u32 __val = tr32(REG); \
9332 (PSTAT)->low += __val; \
9333 if ((PSTAT)->low < __val) \
9334 (PSTAT)->high += 1; \
9335} while (0)
9336
9337static void tg3_periodic_fetch_stats(struct tg3 *tp)
9338{
9339 struct tg3_hw_stats *sp = tp->hw_stats;
9340
9341 if (!netif_carrier_ok(tp->dev))
9342 return;
9343
9344 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
9345 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
9346 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
9347 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
9348 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
9349 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
9350 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
9351 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
9352 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
9353 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
9354 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
9355 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
9356 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
9357
9358 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
9359 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
9360 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
9361 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
9362 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
9363 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
9364 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
9365 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
9366 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
9367 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
9368 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
9369 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
9370 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
9371 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07009372
9373 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
Matt Carlson310050f2011-05-19 12:12:55 +00009374 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
9375 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0 &&
9376 tp->pci_chip_rev_id != CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +00009377 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
9378 } else {
9379 u32 val = tr32(HOSTCC_FLOW_ATTN);
9380 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
9381 if (val) {
9382 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
9383 sp->rx_discards.low += val;
9384 if (sp->rx_discards.low < val)
9385 sp->rx_discards.high += 1;
9386 }
9387 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
9388 }
Michael Chan463d3052006-05-22 16:36:27 -07009389 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009390}
9391
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009392static void tg3_chk_missed_msi(struct tg3 *tp)
9393{
9394 u32 i;
9395
9396 for (i = 0; i < tp->irq_cnt; i++) {
9397 struct tg3_napi *tnapi = &tp->napi[i];
9398
9399 if (tg3_has_work(tnapi)) {
9400 if (tnapi->last_rx_cons == tnapi->rx_rcb_ptr &&
9401 tnapi->last_tx_cons == tnapi->tx_cons) {
9402 if (tnapi->chk_msi_cnt < 1) {
9403 tnapi->chk_msi_cnt++;
9404 return;
9405 }
Matt Carlson7f230732011-08-31 11:44:48 +00009406 tg3_msi(0, tnapi);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009407 }
9408 }
9409 tnapi->chk_msi_cnt = 0;
9410 tnapi->last_rx_cons = tnapi->rx_rcb_ptr;
9411 tnapi->last_tx_cons = tnapi->tx_cons;
9412 }
9413}
9414
Linus Torvalds1da177e2005-04-16 15:20:36 -07009415static void tg3_timer(unsigned long __opaque)
9416{
9417 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009418
Matt Carlson5b190622011-11-04 09:15:04 +00009419 if (tp->irq_sync || tg3_flag(tp, RESET_TASK_PENDING))
Michael Chanf475f162006-03-27 23:20:14 -08009420 goto restart_timer;
9421
David S. Millerf47c11e2005-06-24 20:18:35 -07009422 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009423
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009424 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00009425 tg3_flag(tp, 57765_CLASS))
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009426 tg3_chk_missed_msi(tp);
9427
Joe Perches63c3a662011-04-26 08:12:10 +00009428 if (!tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -07009429 /* All of this garbage is because when using non-tagged
9430 * IRQ status the mailbox/status_block protocol the chip
9431 * uses with the cpu is race prone.
9432 */
Matt Carlson898a56f2009-08-28 14:02:40 +00009433 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -07009434 tw32(GRC_LOCAL_CTRL,
9435 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
9436 } else {
9437 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00009438 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -07009439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009440
David S. Millerfac9b832005-05-18 22:46:34 -07009441 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
David S. Millerf47c11e2005-06-24 20:18:35 -07009442 spin_unlock(&tp->lock);
Matt Carlsondb219972011-11-04 09:15:03 +00009443 tg3_reset_task_schedule(tp);
Matt Carlson5b190622011-11-04 09:15:04 +00009444 goto restart_timer;
David S. Millerfac9b832005-05-18 22:46:34 -07009445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009446 }
9447
Linus Torvalds1da177e2005-04-16 15:20:36 -07009448 /* This part only runs once per second. */
9449 if (!--tp->timer_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00009450 if (tg3_flag(tp, 5705_PLUS))
David S. Millerfac9b832005-05-18 22:46:34 -07009451 tg3_periodic_fetch_stats(tp);
9452
Matt Carlsonb0c59432011-05-19 12:12:48 +00009453 if (tp->setlpicnt && !--tp->setlpicnt)
9454 tg3_phy_eee_enable(tp);
Matt Carlson52b02d02010-10-14 10:37:41 +00009455
Joe Perches63c3a662011-04-26 08:12:10 +00009456 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009457 u32 mac_stat;
9458 int phy_event;
9459
9460 mac_stat = tr32(MAC_STATUS);
9461
9462 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009463 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009464 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
9465 phy_event = 1;
9466 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
9467 phy_event = 1;
9468
9469 if (phy_event)
9470 tg3_setup_phy(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +00009471 } else if (tg3_flag(tp, POLL_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009472 u32 mac_stat = tr32(MAC_STATUS);
9473 int need_setup = 0;
9474
9475 if (netif_carrier_ok(tp->dev) &&
9476 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
9477 need_setup = 1;
9478 }
Matt Carlsonbe98da62010-07-11 09:31:46 +00009479 if (!netif_carrier_ok(tp->dev) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009480 (mac_stat & (MAC_STATUS_PCS_SYNCED |
9481 MAC_STATUS_SIGNAL_DET))) {
9482 need_setup = 1;
9483 }
9484 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07009485 if (!tp->serdes_counter) {
9486 tw32_f(MAC_MODE,
9487 (tp->mac_mode &
9488 ~MAC_MODE_PORT_MODE_MASK));
9489 udelay(40);
9490 tw32_f(MAC_MODE, tp->mac_mode);
9491 udelay(40);
9492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009493 tg3_setup_phy(tp, 0);
9494 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009495 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009496 tg3_flag(tp, 5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -07009497 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +00009498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009499
9500 tp->timer_counter = tp->timer_multiplier;
9501 }
9502
Michael Chan130b8e42006-09-27 16:00:40 -07009503 /* Heartbeat is only sent once every 2 seconds.
9504 *
9505 * The heartbeat is to tell the ASF firmware that the host
9506 * driver is still alive. In the event that the OS crashes,
9507 * ASF needs to reset the hardware to free up the FIFO space
9508 * that may be filled with rx packets destined for the host.
9509 * If the FIFO is full, ASF will no longer function properly.
9510 *
9511 * Unintended resets have been reported on real time kernels
9512 * where the timer doesn't run on time. Netpoll will also have
9513 * same problem.
9514 *
9515 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
9516 * to check the ring condition when the heartbeat is expiring
9517 * before doing the reset. This will prevent most unintended
9518 * resets.
9519 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009520 if (!--tp->asf_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00009521 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07009522 tg3_wait_for_event_ack(tp);
9523
Michael Chanbbadf502006-04-06 21:46:34 -07009524 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07009525 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07009526 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +00009527 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
9528 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -07009529
9530 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009531 }
9532 tp->asf_counter = tp->asf_multiplier;
9533 }
9534
David S. Millerf47c11e2005-06-24 20:18:35 -07009535 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009536
Michael Chanf475f162006-03-27 23:20:14 -08009537restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07009538 tp->timer.expires = jiffies + tp->timer_offset;
9539 add_timer(&tp->timer);
9540}
9541
Matt Carlson21f76382012-02-22 12:35:21 +00009542static void __devinit tg3_timer_init(struct tg3 *tp)
9543{
9544 if (tg3_flag(tp, TAGGED_STATUS) &&
9545 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
9546 !tg3_flag(tp, 57765_CLASS))
9547 tp->timer_offset = HZ;
9548 else
9549 tp->timer_offset = HZ / 10;
9550
9551 BUG_ON(tp->timer_offset > HZ);
9552
9553 tp->timer_multiplier = (HZ / tp->timer_offset);
9554 tp->asf_multiplier = (HZ / tp->timer_offset) *
9555 TG3_FW_UPDATE_FREQ_SEC;
9556
9557 init_timer(&tp->timer);
9558 tp->timer.data = (unsigned long) tp;
9559 tp->timer.function = tg3_timer;
9560}
9561
9562static void tg3_timer_start(struct tg3 *tp)
9563{
9564 tp->asf_counter = tp->asf_multiplier;
9565 tp->timer_counter = tp->timer_multiplier;
9566
9567 tp->timer.expires = jiffies + tp->timer_offset;
9568 add_timer(&tp->timer);
9569}
9570
9571static void tg3_timer_stop(struct tg3 *tp)
9572{
9573 del_timer_sync(&tp->timer);
9574}
9575
9576/* Restart hardware after configuration changes, self-test, etc.
9577 * Invoked with tp->lock held.
9578 */
9579static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
9580 __releases(tp->lock)
9581 __acquires(tp->lock)
9582{
9583 int err;
9584
9585 err = tg3_init_hw(tp, reset_phy);
9586 if (err) {
9587 netdev_err(tp->dev,
9588 "Failed to re-initialize device, aborting\n");
9589 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
9590 tg3_full_unlock(tp);
9591 tg3_timer_stop(tp);
9592 tp->irq_sync = 0;
9593 tg3_napi_enable(tp);
9594 dev_close(tp->dev);
9595 tg3_full_lock(tp, 0);
9596 }
9597 return err;
9598}
9599
9600static void tg3_reset_task(struct work_struct *work)
9601{
9602 struct tg3 *tp = container_of(work, struct tg3, reset_task);
9603 int err;
9604
9605 tg3_full_lock(tp, 0);
9606
9607 if (!netif_running(tp->dev)) {
9608 tg3_flag_clear(tp, RESET_TASK_PENDING);
9609 tg3_full_unlock(tp);
9610 return;
9611 }
9612
9613 tg3_full_unlock(tp);
9614
9615 tg3_phy_stop(tp);
9616
9617 tg3_netif_stop(tp);
9618
9619 tg3_full_lock(tp, 1);
9620
9621 if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
9622 tp->write32_tx_mbox = tg3_write32_tx_mbox;
9623 tp->write32_rx_mbox = tg3_write_flush_reg32;
9624 tg3_flag_set(tp, MBOX_WRITE_REORDER);
9625 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
9626 }
9627
9628 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
9629 err = tg3_init_hw(tp, 1);
9630 if (err)
9631 goto out;
9632
9633 tg3_netif_start(tp);
9634
9635out:
9636 tg3_full_unlock(tp);
9637
9638 if (!err)
9639 tg3_phy_start(tp);
9640
9641 tg3_flag_clear(tp, RESET_TASK_PENDING);
9642}
9643
Matt Carlson4f125f42009-09-01 12:55:02 +00009644static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -08009645{
David Howells7d12e782006-10-05 14:55:46 +01009646 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009647 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +00009648 char *name;
9649 struct tg3_napi *tnapi = &tp->napi[irq_num];
9650
9651 if (tp->irq_cnt == 1)
9652 name = tp->dev->name;
9653 else {
9654 name = &tnapi->irq_lbl[0];
9655 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
9656 name[IFNAMSIZ-1] = 0;
9657 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009658
Joe Perches63c3a662011-04-26 08:12:10 +00009659 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Michael Chanfcfa0a32006-03-20 22:28:41 -08009660 fn = tg3_msi;
Joe Perches63c3a662011-04-26 08:12:10 +00009661 if (tg3_flag(tp, 1SHOT_MSI))
Michael Chanfcfa0a32006-03-20 22:28:41 -08009662 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009663 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009664 } else {
9665 fn = tg3_interrupt;
Joe Perches63c3a662011-04-26 08:12:10 +00009666 if (tg3_flag(tp, TAGGED_STATUS))
Michael Chanfcfa0a32006-03-20 22:28:41 -08009667 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009668 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009669 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009670
9671 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009672}
9673
Michael Chan79381092005-04-21 17:13:59 -07009674static int tg3_test_interrupt(struct tg3 *tp)
9675{
Matt Carlson09943a12009-08-28 14:01:57 +00009676 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -07009677 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -07009678 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009679 u32 val;
Michael Chan79381092005-04-21 17:13:59 -07009680
Michael Chand4bc3922005-05-29 14:59:20 -07009681 if (!netif_running(dev))
9682 return -ENODEV;
9683
Michael Chan79381092005-04-21 17:13:59 -07009684 tg3_disable_ints(tp);
9685
Matt Carlson4f125f42009-09-01 12:55:02 +00009686 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009687
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009688 /*
9689 * Turn off MSI one shot mode. Otherwise this test has no
9690 * observable way to know whether the interrupt was delivered.
9691 */
Matt Carlson3aa1cdf2011-07-20 10:20:55 +00009692 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009693 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
9694 tw32(MSGINT_MODE, val);
9695 }
9696
Matt Carlson4f125f42009-09-01 12:55:02 +00009697 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Davidlohr Buesof274fd92012-02-22 03:06:54 +00009698 IRQF_SHARED, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009699 if (err)
9700 return err;
9701
Matt Carlson898a56f2009-08-28 14:02:40 +00009702 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -07009703 tg3_enable_ints(tp);
9704
9705 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00009706 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -07009707
9708 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -07009709 u32 int_mbox, misc_host_ctrl;
9710
Matt Carlson898a56f2009-08-28 14:02:40 +00009711 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -07009712 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
9713
9714 if ((int_mbox != 0) ||
9715 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
9716 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -07009717 break;
Michael Chanb16250e2006-09-27 16:10:14 -07009718 }
9719
Matt Carlson3aa1cdf2011-07-20 10:20:55 +00009720 if (tg3_flag(tp, 57765_PLUS) &&
9721 tnapi->hw_status->status_tag != tnapi->last_tag)
9722 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
9723
Michael Chan79381092005-04-21 17:13:59 -07009724 msleep(10);
9725 }
9726
9727 tg3_disable_ints(tp);
9728
Matt Carlson4f125f42009-09-01 12:55:02 +00009729 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009730
Matt Carlson4f125f42009-09-01 12:55:02 +00009731 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009732
9733 if (err)
9734 return err;
9735
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009736 if (intr_ok) {
9737 /* Reenable MSI one shot mode. */
Matt Carlson5b39de92011-08-31 11:44:50 +00009738 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, 1SHOT_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009739 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
9740 tw32(MSGINT_MODE, val);
9741 }
Michael Chan79381092005-04-21 17:13:59 -07009742 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009743 }
Michael Chan79381092005-04-21 17:13:59 -07009744
9745 return -EIO;
9746}
9747
9748/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
9749 * successfully restored
9750 */
9751static int tg3_test_msi(struct tg3 *tp)
9752{
Michael Chan79381092005-04-21 17:13:59 -07009753 int err;
9754 u16 pci_cmd;
9755
Joe Perches63c3a662011-04-26 08:12:10 +00009756 if (!tg3_flag(tp, USING_MSI))
Michael Chan79381092005-04-21 17:13:59 -07009757 return 0;
9758
9759 /* Turn off SERR reporting in case MSI terminates with Master
9760 * Abort.
9761 */
9762 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
9763 pci_write_config_word(tp->pdev, PCI_COMMAND,
9764 pci_cmd & ~PCI_COMMAND_SERR);
9765
9766 err = tg3_test_interrupt(tp);
9767
9768 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
9769
9770 if (!err)
9771 return 0;
9772
9773 /* other failures */
9774 if (err != -EIO)
9775 return err;
9776
9777 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009778 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
9779 "to INTx mode. Please report this failure to the PCI "
9780 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -07009781
Matt Carlson4f125f42009-09-01 12:55:02 +00009782 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +00009783
Michael Chan79381092005-04-21 17:13:59 -07009784 pci_disable_msi(tp->pdev);
9785
Joe Perches63c3a662011-04-26 08:12:10 +00009786 tg3_flag_clear(tp, USING_MSI);
Andre Detschdc8bf1b2010-04-26 07:27:07 +00009787 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -07009788
Matt Carlson4f125f42009-09-01 12:55:02 +00009789 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009790 if (err)
9791 return err;
9792
9793 /* Need to reset the chip because the MSI cycle may have terminated
9794 * with Master Abort.
9795 */
David S. Millerf47c11e2005-06-24 20:18:35 -07009796 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009797
Michael Chan944d9802005-05-29 14:57:48 -07009798 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009799 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009800
David S. Millerf47c11e2005-06-24 20:18:35 -07009801 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009802
9803 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +00009804 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -07009805
9806 return err;
9807}
9808
Matt Carlson9e9fd122009-01-19 16:57:45 -08009809static int tg3_request_firmware(struct tg3 *tp)
9810{
9811 const __be32 *fw_data;
9812
9813 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009814 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
9815 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009816 return -ENOENT;
9817 }
9818
9819 fw_data = (void *)tp->fw->data;
9820
9821 /* Firmware blob starts with version numbers, followed by
9822 * start address and _full_ length including BSS sections
9823 * (which must be longer than the actual data, of course
9824 */
9825
9826 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
9827 if (tp->fw_len < (tp->fw->size - 12)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009828 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
9829 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009830 release_firmware(tp->fw);
9831 tp->fw = NULL;
9832 return -EINVAL;
9833 }
9834
9835 /* We no longer need firmware; we have it. */
9836 tp->fw_needed = NULL;
9837 return 0;
9838}
9839
Matt Carlson679563f2009-09-01 12:55:46 +00009840static bool tg3_enable_msix(struct tg3 *tp)
9841{
Matt Carlsonc3b50032012-01-17 15:27:23 +00009842 int i, rc;
Matt Carlson679563f2009-09-01 12:55:46 +00009843 struct msix_entry msix_ent[tp->irq_max];
9844
Matt Carlsonc3b50032012-01-17 15:27:23 +00009845 tp->irq_cnt = num_online_cpus();
9846 if (tp->irq_cnt > 1) {
9847 /* We want as many rx rings enabled as there are cpus.
9848 * In multiqueue MSI-X mode, the first MSI-X vector
9849 * only deals with link interrupts, etc, so we add
9850 * one to the number of vectors we are requesting.
9851 */
9852 tp->irq_cnt = min_t(unsigned, tp->irq_cnt + 1, tp->irq_max);
9853 }
Matt Carlson679563f2009-09-01 12:55:46 +00009854
9855 for (i = 0; i < tp->irq_max; i++) {
9856 msix_ent[i].entry = i;
9857 msix_ent[i].vector = 0;
9858 }
9859
9860 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +00009861 if (rc < 0) {
9862 return false;
9863 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +00009864 if (pci_enable_msix(tp->pdev, msix_ent, rc))
9865 return false;
Joe Perches05dbe002010-02-17 19:44:19 +00009866 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
9867 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +00009868 tp->irq_cnt = rc;
9869 }
9870
9871 for (i = 0; i < tp->irq_max; i++)
9872 tp->napi[i].irq_vec = msix_ent[i].vector;
9873
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009874 netif_set_real_num_tx_queues(tp->dev, 1);
9875 rc = tp->irq_cnt > 1 ? tp->irq_cnt - 1 : 1;
9876 if (netif_set_real_num_rx_queues(tp->dev, rc)) {
9877 pci_disable_msix(tp->pdev);
9878 return false;
9879 }
Matt Carlsonb92b9042010-11-24 08:31:51 +00009880
9881 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +00009882 tg3_flag_set(tp, ENABLE_RSS);
Matt Carlsond78b59f2011-04-05 14:22:46 +00009883
9884 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
9885 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Joe Perches63c3a662011-04-26 08:12:10 +00009886 tg3_flag_set(tp, ENABLE_TSS);
Matt Carlsonb92b9042010-11-24 08:31:51 +00009887 netif_set_real_num_tx_queues(tp->dev, tp->irq_cnt - 1);
9888 }
9889 }
Matt Carlson2430b032010-06-05 17:24:34 +00009890
Matt Carlson679563f2009-09-01 12:55:46 +00009891 return true;
9892}
9893
Matt Carlson07b01732009-08-28 14:01:15 +00009894static void tg3_ints_init(struct tg3 *tp)
9895{
Joe Perches63c3a662011-04-26 08:12:10 +00009896 if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
9897 !tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +00009898 /* All MSI supporting chips should support tagged
9899 * status. Assert that this is the case.
9900 */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009901 netdev_warn(tp->dev,
9902 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +00009903 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +00009904 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009905
Joe Perches63c3a662011-04-26 08:12:10 +00009906 if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
9907 tg3_flag_set(tp, USING_MSIX);
9908 else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
9909 tg3_flag_set(tp, USING_MSI);
Matt Carlson679563f2009-09-01 12:55:46 +00009910
Joe Perches63c3a662011-04-26 08:12:10 +00009911 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009912 u32 msi_mode = tr32(MSGINT_MODE);
Joe Perches63c3a662011-04-26 08:12:10 +00009913 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009914 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +00009915 if (!tg3_flag(tp, 1SHOT_MSI))
9916 msi_mode |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlson679563f2009-09-01 12:55:46 +00009917 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
9918 }
9919defcfg:
Joe Perches63c3a662011-04-26 08:12:10 +00009920 if (!tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009921 tp->irq_cnt = 1;
9922 tp->napi[0].irq_vec = tp->pdev->irq;
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009923 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -07009924 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +00009925 }
Matt Carlson07b01732009-08-28 14:01:15 +00009926}
9927
9928static void tg3_ints_fini(struct tg3 *tp)
9929{
Joe Perches63c3a662011-04-26 08:12:10 +00009930 if (tg3_flag(tp, USING_MSIX))
Matt Carlson679563f2009-09-01 12:55:46 +00009931 pci_disable_msix(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009932 else if (tg3_flag(tp, USING_MSI))
Matt Carlson679563f2009-09-01 12:55:46 +00009933 pci_disable_msi(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009934 tg3_flag_clear(tp, USING_MSI);
9935 tg3_flag_clear(tp, USING_MSIX);
9936 tg3_flag_clear(tp, ENABLE_RSS);
9937 tg3_flag_clear(tp, ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +00009938}
9939
Linus Torvalds1da177e2005-04-16 15:20:36 -07009940static int tg3_open(struct net_device *dev)
9941{
9942 struct tg3 *tp = netdev_priv(dev);
Matt Carlson4f125f42009-09-01 12:55:02 +00009943 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009944
Matt Carlson9e9fd122009-01-19 16:57:45 -08009945 if (tp->fw_needed) {
9946 err = tg3_request_firmware(tp);
9947 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
9948 if (err)
9949 return err;
9950 } else if (err) {
Joe Perches05dbe002010-02-17 19:44:19 +00009951 netdev_warn(tp->dev, "TSO capability disabled\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009952 tg3_flag_clear(tp, TSO_CAPABLE);
9953 } else if (!tg3_flag(tp, TSO_CAPABLE)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009954 netdev_notice(tp->dev, "TSO capability restored\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009955 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009956 }
9957 }
9958
Michael Chanc49a1562006-12-17 17:07:29 -08009959 netif_carrier_off(tp->dev);
9960
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009961 err = tg3_power_up(tp);
Matt Carlson2f751b62008-08-04 23:17:34 -07009962 if (err)
Michael Chanbc1c7562006-03-20 17:48:03 -08009963 return err;
Matt Carlson2f751b62008-08-04 23:17:34 -07009964
9965 tg3_full_lock(tp, 0);
Michael Chanbc1c7562006-03-20 17:48:03 -08009966
Linus Torvalds1da177e2005-04-16 15:20:36 -07009967 tg3_disable_ints(tp);
Joe Perches63c3a662011-04-26 08:12:10 +00009968 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009969
David S. Millerf47c11e2005-06-24 20:18:35 -07009970 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009971
Matt Carlson679563f2009-09-01 12:55:46 +00009972 /*
9973 * Setup interrupts first so we know how
9974 * many NAPI resources to allocate
9975 */
9976 tg3_ints_init(tp);
9977
Matt Carlson90415472011-12-16 13:33:23 +00009978 tg3_rss_check_indir_tbl(tp);
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009979
Linus Torvalds1da177e2005-04-16 15:20:36 -07009980 /* The placement of this call is tied
9981 * to the setup and use of Host TX descriptors.
9982 */
9983 err = tg3_alloc_consistent(tp);
9984 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009985 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009986
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009987 tg3_napi_init(tp);
9988
Matt Carlsonfed97812009-09-01 13:10:19 +00009989 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07009990
Matt Carlson4f125f42009-09-01 12:55:02 +00009991 for (i = 0; i < tp->irq_cnt; i++) {
9992 struct tg3_napi *tnapi = &tp->napi[i];
9993 err = tg3_request_irq(tp, i);
9994 if (err) {
Matt Carlson5bc09182011-11-04 09:15:01 +00009995 for (i--; i >= 0; i--) {
9996 tnapi = &tp->napi[i];
Matt Carlson4f125f42009-09-01 12:55:02 +00009997 free_irq(tnapi->irq_vec, tnapi);
Matt Carlson5bc09182011-11-04 09:15:01 +00009998 }
9999 goto err_out2;
Matt Carlson4f125f42009-09-01 12:55:02 +000010000 }
10001 }
Matt Carlson07b01732009-08-28 14:01:15 +000010002
David S. Millerf47c11e2005-06-24 20:18:35 -070010003 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010004
Gary Zambrano8e7a22e2006-04-29 18:59:13 -070010005 err = tg3_init_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010006 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -070010007 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010008 tg3_free_rings(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010009 }
10010
David S. Millerf47c11e2005-06-24 20:18:35 -070010011 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010012
Matt Carlson07b01732009-08-28 14:01:15 +000010013 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +000010014 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010015
Joe Perches63c3a662011-04-26 08:12:10 +000010016 if (tg3_flag(tp, USING_MSI)) {
Michael Chan79381092005-04-21 17:13:59 -070010017 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -070010018
Michael Chan79381092005-04-21 17:13:59 -070010019 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -070010020 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070010021 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -070010022 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070010023 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -070010024
Matt Carlson679563f2009-09-01 12:55:46 +000010025 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -070010026 }
Michael Chanfcfa0a32006-03-20 22:28:41 -080010027
Joe Perches63c3a662011-04-26 08:12:10 +000010028 if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010029 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -080010030
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010031 tw32(PCIE_TRANSACTION_CFG,
10032 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -080010033 }
Michael Chan79381092005-04-21 17:13:59 -070010034 }
10035
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010036 tg3_phy_start(tp);
10037
David S. Millerf47c11e2005-06-24 20:18:35 -070010038 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010039
Matt Carlson21f76382012-02-22 12:35:21 +000010040 tg3_timer_start(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000010041 tg3_flag_set(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010042 tg3_enable_ints(tp);
10043
David S. Millerf47c11e2005-06-24 20:18:35 -070010044 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010045
Matt Carlsonfe5f5782009-09-01 13:09:39 +000010046 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010047
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000010048 /*
10049 * Reset loopback feature if it was turned on while the device was down
10050 * make sure that it's installed properly now.
10051 */
10052 if (dev->features & NETIF_F_LOOPBACK)
10053 tg3_set_loopback(dev, dev->features);
10054
Linus Torvalds1da177e2005-04-16 15:20:36 -070010055 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +000010056
Matt Carlson679563f2009-09-01 12:55:46 +000010057err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +000010058 for (i = tp->irq_cnt - 1; i >= 0; i--) {
10059 struct tg3_napi *tnapi = &tp->napi[i];
10060 free_irq(tnapi->irq_vec, tnapi);
10061 }
Matt Carlson07b01732009-08-28 14:01:15 +000010062
Matt Carlson679563f2009-09-01 12:55:46 +000010063err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +000010064 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000010065 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +000010066 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +000010067
10068err_out1:
10069 tg3_ints_fini(tp);
Matt Carlsoncd0d7222011-07-13 09:27:33 +000010070 tg3_frob_aux_power(tp, false);
10071 pci_set_power_state(tp->pdev, PCI_D3hot);
Matt Carlson07b01732009-08-28 14:01:15 +000010072 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010073}
10074
Linus Torvalds1da177e2005-04-16 15:20:36 -070010075static int tg3_close(struct net_device *dev)
10076{
Matt Carlson4f125f42009-09-01 12:55:02 +000010077 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010078 struct tg3 *tp = netdev_priv(dev);
10079
Matt Carlsonfed97812009-09-01 13:10:19 +000010080 tg3_napi_disable(tp);
Matt Carlsondb219972011-11-04 09:15:03 +000010081 tg3_reset_task_cancel(tp);
Michael Chan7faa0062006-02-02 17:29:28 -080010082
Matt Carlsonfe5f5782009-09-01 13:09:39 +000010083 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010084
Matt Carlson21f76382012-02-22 12:35:21 +000010085 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010086
Matt Carlson24bb4fb2009-10-05 17:55:29 +000010087 tg3_phy_stop(tp);
10088
David S. Millerf47c11e2005-06-24 20:18:35 -070010089 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010090
10091 tg3_disable_ints(tp);
10092
Michael Chan944d9802005-05-29 14:57:48 -070010093 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010094 tg3_free_rings(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000010095 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010096
David S. Millerf47c11e2005-06-24 20:18:35 -070010097 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010098
Matt Carlson4f125f42009-09-01 12:55:02 +000010099 for (i = tp->irq_cnt - 1; i >= 0; i--) {
10100 struct tg3_napi *tnapi = &tp->napi[i];
10101 free_irq(tnapi->irq_vec, tnapi);
10102 }
Matt Carlson07b01732009-08-28 14:01:15 +000010103
10104 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010105
Matt Carlson92feeab2011-12-08 14:40:14 +000010106 /* Clear stats across close / open calls */
10107 memset(&tp->net_stats_prev, 0, sizeof(tp->net_stats_prev));
10108 memset(&tp->estats_prev, 0, sizeof(tp->estats_prev));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010109
Matt Carlson66cfd1b2010-09-30 10:34:30 +000010110 tg3_napi_fini(tp);
10111
Linus Torvalds1da177e2005-04-16 15:20:36 -070010112 tg3_free_consistent(tp);
10113
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000010114 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080010115
10116 netif_carrier_off(tp->dev);
10117
Linus Torvalds1da177e2005-04-16 15:20:36 -070010118 return 0;
10119}
10120
Eric Dumazet511d2222010-07-07 20:44:24 +000010121static inline u64 get_stat64(tg3_stat64_t *val)
Stefan Buehler816f8b82008-08-15 14:10:54 -070010122{
10123 return ((u64)val->high << 32) | ((u64)val->low);
10124}
10125
Matt Carlson65ec6982012-02-28 23:33:37 +000010126static u64 tg3_calc_crc_errors(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010127{
10128 struct tg3_hw_stats *hw_stats = tp->hw_stats;
10129
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010130 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010131 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
10132 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010133 u32 val;
10134
Michael Chan569a5df2007-02-13 12:18:15 -080010135 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
10136 tg3_writephy(tp, MII_TG3_TEST1,
10137 val | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +000010138 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010139 } else
10140 val = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010141
10142 tp->phy_crc_errors += val;
10143
10144 return tp->phy_crc_errors;
10145 }
10146
10147 return get_stat64(&hw_stats->rx_fcs_errors);
10148}
10149
10150#define ESTAT_ADD(member) \
10151 estats->member = old_estats->member + \
Eric Dumazet511d2222010-07-07 20:44:24 +000010152 get_stat64(&hw_stats->member)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010153
Matt Carlson65ec6982012-02-28 23:33:37 +000010154static void tg3_get_estats(struct tg3 *tp, struct tg3_ethtool_stats *estats)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010155{
Linus Torvalds1da177e2005-04-16 15:20:36 -070010156 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
10157 struct tg3_hw_stats *hw_stats = tp->hw_stats;
10158
Linus Torvalds1da177e2005-04-16 15:20:36 -070010159 ESTAT_ADD(rx_octets);
10160 ESTAT_ADD(rx_fragments);
10161 ESTAT_ADD(rx_ucast_packets);
10162 ESTAT_ADD(rx_mcast_packets);
10163 ESTAT_ADD(rx_bcast_packets);
10164 ESTAT_ADD(rx_fcs_errors);
10165 ESTAT_ADD(rx_align_errors);
10166 ESTAT_ADD(rx_xon_pause_rcvd);
10167 ESTAT_ADD(rx_xoff_pause_rcvd);
10168 ESTAT_ADD(rx_mac_ctrl_rcvd);
10169 ESTAT_ADD(rx_xoff_entered);
10170 ESTAT_ADD(rx_frame_too_long_errors);
10171 ESTAT_ADD(rx_jabbers);
10172 ESTAT_ADD(rx_undersize_packets);
10173 ESTAT_ADD(rx_in_length_errors);
10174 ESTAT_ADD(rx_out_length_errors);
10175 ESTAT_ADD(rx_64_or_less_octet_packets);
10176 ESTAT_ADD(rx_65_to_127_octet_packets);
10177 ESTAT_ADD(rx_128_to_255_octet_packets);
10178 ESTAT_ADD(rx_256_to_511_octet_packets);
10179 ESTAT_ADD(rx_512_to_1023_octet_packets);
10180 ESTAT_ADD(rx_1024_to_1522_octet_packets);
10181 ESTAT_ADD(rx_1523_to_2047_octet_packets);
10182 ESTAT_ADD(rx_2048_to_4095_octet_packets);
10183 ESTAT_ADD(rx_4096_to_8191_octet_packets);
10184 ESTAT_ADD(rx_8192_to_9022_octet_packets);
10185
10186 ESTAT_ADD(tx_octets);
10187 ESTAT_ADD(tx_collisions);
10188 ESTAT_ADD(tx_xon_sent);
10189 ESTAT_ADD(tx_xoff_sent);
10190 ESTAT_ADD(tx_flow_control);
10191 ESTAT_ADD(tx_mac_errors);
10192 ESTAT_ADD(tx_single_collisions);
10193 ESTAT_ADD(tx_mult_collisions);
10194 ESTAT_ADD(tx_deferred);
10195 ESTAT_ADD(tx_excessive_collisions);
10196 ESTAT_ADD(tx_late_collisions);
10197 ESTAT_ADD(tx_collide_2times);
10198 ESTAT_ADD(tx_collide_3times);
10199 ESTAT_ADD(tx_collide_4times);
10200 ESTAT_ADD(tx_collide_5times);
10201 ESTAT_ADD(tx_collide_6times);
10202 ESTAT_ADD(tx_collide_7times);
10203 ESTAT_ADD(tx_collide_8times);
10204 ESTAT_ADD(tx_collide_9times);
10205 ESTAT_ADD(tx_collide_10times);
10206 ESTAT_ADD(tx_collide_11times);
10207 ESTAT_ADD(tx_collide_12times);
10208 ESTAT_ADD(tx_collide_13times);
10209 ESTAT_ADD(tx_collide_14times);
10210 ESTAT_ADD(tx_collide_15times);
10211 ESTAT_ADD(tx_ucast_packets);
10212 ESTAT_ADD(tx_mcast_packets);
10213 ESTAT_ADD(tx_bcast_packets);
10214 ESTAT_ADD(tx_carrier_sense_errors);
10215 ESTAT_ADD(tx_discards);
10216 ESTAT_ADD(tx_errors);
10217
10218 ESTAT_ADD(dma_writeq_full);
10219 ESTAT_ADD(dma_write_prioq_full);
10220 ESTAT_ADD(rxbds_empty);
10221 ESTAT_ADD(rx_discards);
10222 ESTAT_ADD(rx_errors);
10223 ESTAT_ADD(rx_threshold_hit);
10224
10225 ESTAT_ADD(dma_readq_full);
10226 ESTAT_ADD(dma_read_prioq_full);
10227 ESTAT_ADD(tx_comp_queue_full);
10228
10229 ESTAT_ADD(ring_set_send_prod_index);
10230 ESTAT_ADD(ring_status_update);
10231 ESTAT_ADD(nic_irqs);
10232 ESTAT_ADD(nic_avoided_irqs);
10233 ESTAT_ADD(nic_tx_threshold_hit);
10234
Matt Carlson4452d092011-05-19 12:12:51 +000010235 ESTAT_ADD(mbuf_lwm_thresh_hit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010236}
10237
Matt Carlson65ec6982012-02-28 23:33:37 +000010238static void tg3_get_nstats(struct tg3 *tp, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010239{
Eric Dumazet511d2222010-07-07 20:44:24 +000010240 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010241 struct tg3_hw_stats *hw_stats = tp->hw_stats;
10242
Linus Torvalds1da177e2005-04-16 15:20:36 -070010243 stats->rx_packets = old_stats->rx_packets +
10244 get_stat64(&hw_stats->rx_ucast_packets) +
10245 get_stat64(&hw_stats->rx_mcast_packets) +
10246 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010247
Linus Torvalds1da177e2005-04-16 15:20:36 -070010248 stats->tx_packets = old_stats->tx_packets +
10249 get_stat64(&hw_stats->tx_ucast_packets) +
10250 get_stat64(&hw_stats->tx_mcast_packets) +
10251 get_stat64(&hw_stats->tx_bcast_packets);
10252
10253 stats->rx_bytes = old_stats->rx_bytes +
10254 get_stat64(&hw_stats->rx_octets);
10255 stats->tx_bytes = old_stats->tx_bytes +
10256 get_stat64(&hw_stats->tx_octets);
10257
10258 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -070010259 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010260 stats->tx_errors = old_stats->tx_errors +
10261 get_stat64(&hw_stats->tx_errors) +
10262 get_stat64(&hw_stats->tx_mac_errors) +
10263 get_stat64(&hw_stats->tx_carrier_sense_errors) +
10264 get_stat64(&hw_stats->tx_discards);
10265
10266 stats->multicast = old_stats->multicast +
10267 get_stat64(&hw_stats->rx_mcast_packets);
10268 stats->collisions = old_stats->collisions +
10269 get_stat64(&hw_stats->tx_collisions);
10270
10271 stats->rx_length_errors = old_stats->rx_length_errors +
10272 get_stat64(&hw_stats->rx_frame_too_long_errors) +
10273 get_stat64(&hw_stats->rx_undersize_packets);
10274
10275 stats->rx_over_errors = old_stats->rx_over_errors +
10276 get_stat64(&hw_stats->rxbds_empty);
10277 stats->rx_frame_errors = old_stats->rx_frame_errors +
10278 get_stat64(&hw_stats->rx_align_errors);
10279 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
10280 get_stat64(&hw_stats->tx_discards);
10281 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
10282 get_stat64(&hw_stats->tx_carrier_sense_errors);
10283
10284 stats->rx_crc_errors = old_stats->rx_crc_errors +
Matt Carlson65ec6982012-02-28 23:33:37 +000010285 tg3_calc_crc_errors(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010286
John W. Linville4f63b872005-09-12 14:43:18 -070010287 stats->rx_missed_errors = old_stats->rx_missed_errors +
10288 get_stat64(&hw_stats->rx_discards);
10289
Eric Dumazetb0057c52010-10-10 19:55:52 +000010290 stats->rx_dropped = tp->rx_dropped;
Eric Dumazet48855432011-10-24 07:53:03 +000010291 stats->tx_dropped = tp->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010292}
10293
Linus Torvalds1da177e2005-04-16 15:20:36 -070010294static int tg3_get_regs_len(struct net_device *dev)
10295{
Matt Carlson97bd8e42011-04-13 11:05:04 +000010296 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010297}
10298
10299static void tg3_get_regs(struct net_device *dev,
10300 struct ethtool_regs *regs, void *_p)
10301{
Linus Torvalds1da177e2005-04-16 15:20:36 -070010302 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010303
10304 regs->version = 0;
10305
Matt Carlson97bd8e42011-04-13 11:05:04 +000010306 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010307
Matt Carlson80096062010-08-02 11:26:06 +000010308 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010309 return;
10310
David S. Millerf47c11e2005-06-24 20:18:35 -070010311 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010312
Matt Carlson97bd8e42011-04-13 11:05:04 +000010313 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010314
David S. Millerf47c11e2005-06-24 20:18:35 -070010315 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010316}
10317
10318static int tg3_get_eeprom_len(struct net_device *dev)
10319{
10320 struct tg3 *tp = netdev_priv(dev);
10321
10322 return tp->nvram_size;
10323}
10324
Linus Torvalds1da177e2005-04-16 15:20:36 -070010325static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
10326{
10327 struct tg3 *tp = netdev_priv(dev);
10328 int ret;
10329 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -080010330 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010331 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010332
Joe Perches63c3a662011-04-26 08:12:10 +000010333 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000010334 return -EINVAL;
10335
Matt Carlson80096062010-08-02 11:26:06 +000010336 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010337 return -EAGAIN;
10338
Linus Torvalds1da177e2005-04-16 15:20:36 -070010339 offset = eeprom->offset;
10340 len = eeprom->len;
10341 eeprom->len = 0;
10342
10343 eeprom->magic = TG3_EEPROM_MAGIC;
10344
10345 if (offset & 3) {
10346 /* adjustments to start on required 4 byte boundary */
10347 b_offset = offset & 3;
10348 b_count = 4 - b_offset;
10349 if (b_count > len) {
10350 /* i.e. offset=1 len=2 */
10351 b_count = len;
10352 }
Matt Carlsona9dc5292009-02-25 14:25:30 +000010353 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010354 if (ret)
10355 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +000010356 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010357 len -= b_count;
10358 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010359 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010360 }
10361
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010362 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010363 pd = &data[eeprom->len];
10364 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010365 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010366 if (ret) {
10367 eeprom->len += i;
10368 return ret;
10369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010370 memcpy(pd + i, &val, 4);
10371 }
10372 eeprom->len += i;
10373
10374 if (len & 3) {
10375 /* read last bytes not ending on 4 byte boundary */
10376 pd = &data[eeprom->len];
10377 b_count = len & 3;
10378 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010379 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010380 if (ret)
10381 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080010382 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010383 eeprom->len += b_count;
10384 }
10385 return 0;
10386}
10387
Linus Torvalds1da177e2005-04-16 15:20:36 -070010388static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
10389{
10390 struct tg3 *tp = netdev_priv(dev);
10391 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080010392 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010393 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010394 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010395
Matt Carlson80096062010-08-02 11:26:06 +000010396 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010397 return -EAGAIN;
10398
Joe Perches63c3a662011-04-26 08:12:10 +000010399 if (tg3_flag(tp, NO_NVRAM) ||
Matt Carlsondf259d82009-04-20 06:57:14 +000010400 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010401 return -EINVAL;
10402
10403 offset = eeprom->offset;
10404 len = eeprom->len;
10405
10406 if ((b_offset = (offset & 3))) {
10407 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010408 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010409 if (ret)
10410 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010411 len += b_offset;
10412 offset &= ~3;
Michael Chan1c8594b42005-04-21 17:12:46 -070010413 if (len < 4)
10414 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010415 }
10416
10417 odd_len = 0;
Michael Chan1c8594b42005-04-21 17:12:46 -070010418 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010419 /* adjustments to end on required 4 byte boundary */
10420 odd_len = 1;
10421 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010422 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010423 if (ret)
10424 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010425 }
10426
10427 buf = data;
10428 if (b_offset || odd_len) {
10429 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010430 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010431 return -ENOMEM;
10432 if (b_offset)
10433 memcpy(buf, &start, 4);
10434 if (odd_len)
10435 memcpy(buf+len-4, &end, 4);
10436 memcpy(buf + b_offset, data, eeprom->len);
10437 }
10438
10439 ret = tg3_nvram_write_block(tp, offset, len, buf);
10440
10441 if (buf != data)
10442 kfree(buf);
10443
10444 return ret;
10445}
10446
10447static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
10448{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010449 struct tg3 *tp = netdev_priv(dev);
10450
Joe Perches63c3a662011-04-26 08:12:10 +000010451 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010452 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010453 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010454 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010455 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10456 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010457 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010458
Linus Torvalds1da177e2005-04-16 15:20:36 -070010459 cmd->supported = (SUPPORTED_Autoneg);
10460
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010461 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010462 cmd->supported |= (SUPPORTED_1000baseT_Half |
10463 SUPPORTED_1000baseT_Full);
10464
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010465 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010466 cmd->supported |= (SUPPORTED_100baseT_Half |
10467 SUPPORTED_100baseT_Full |
10468 SUPPORTED_10baseT_Half |
10469 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -080010470 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -070010471 cmd->port = PORT_TP;
10472 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010473 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -070010474 cmd->port = PORT_FIBRE;
10475 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010476
Linus Torvalds1da177e2005-04-16 15:20:36 -070010477 cmd->advertising = tp->link_config.advertising;
Matt Carlson5bb09772011-06-13 13:39:00 +000010478 if (tg3_flag(tp, PAUSE_AUTONEG)) {
10479 if (tp->link_config.flowctrl & FLOW_CTRL_RX) {
10480 if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
10481 cmd->advertising |= ADVERTISED_Pause;
10482 } else {
10483 cmd->advertising |= ADVERTISED_Pause |
10484 ADVERTISED_Asym_Pause;
10485 }
10486 } else if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
10487 cmd->advertising |= ADVERTISED_Asym_Pause;
10488 }
10489 }
Matt Carlson859edb22011-12-08 14:40:16 +000010490 if (netif_running(dev) && netif_carrier_ok(dev)) {
David Decotigny70739492011-04-27 18:32:40 +000010491 ethtool_cmd_speed_set(cmd, tp->link_config.active_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010492 cmd->duplex = tp->link_config.active_duplex;
Matt Carlson859edb22011-12-08 14:40:16 +000010493 cmd->lp_advertising = tp->link_config.rmt_adv;
Matt Carlsone348c5e2011-11-21 15:01:20 +000010494 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
10495 if (tp->phy_flags & TG3_PHYFLG_MDIX_STATE)
10496 cmd->eth_tp_mdix = ETH_TP_MDI_X;
10497 else
10498 cmd->eth_tp_mdix = ETH_TP_MDI;
10499 }
Matt Carlson64c22182010-10-14 10:37:44 +000010500 } else {
Matt Carlsone7405222012-02-13 15:20:16 +000010501 ethtool_cmd_speed_set(cmd, SPEED_UNKNOWN);
10502 cmd->duplex = DUPLEX_UNKNOWN;
Matt Carlsone348c5e2011-11-21 15:01:20 +000010503 cmd->eth_tp_mdix = ETH_TP_MDI_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010504 }
Matt Carlson882e9792009-09-01 13:21:36 +000010505 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +000010506 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010507 cmd->autoneg = tp->link_config.autoneg;
10508 cmd->maxtxpkt = 0;
10509 cmd->maxrxpkt = 0;
10510 return 0;
10511}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010512
Linus Torvalds1da177e2005-04-16 15:20:36 -070010513static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
10514{
10515 struct tg3 *tp = netdev_priv(dev);
David Decotigny25db0332011-04-27 18:32:39 +000010516 u32 speed = ethtool_cmd_speed(cmd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010517
Joe Perches63c3a662011-04-26 08:12:10 +000010518 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010519 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010520 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010521 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010522 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10523 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010524 }
10525
Matt Carlson7e5856b2009-02-25 14:23:01 +000010526 if (cmd->autoneg != AUTONEG_ENABLE &&
10527 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -070010528 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +000010529
10530 if (cmd->autoneg == AUTONEG_DISABLE &&
10531 cmd->duplex != DUPLEX_FULL &&
10532 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -070010533 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010534
Matt Carlson7e5856b2009-02-25 14:23:01 +000010535 if (cmd->autoneg == AUTONEG_ENABLE) {
10536 u32 mask = ADVERTISED_Autoneg |
10537 ADVERTISED_Pause |
10538 ADVERTISED_Asym_Pause;
10539
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010540 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010541 mask |= ADVERTISED_1000baseT_Half |
10542 ADVERTISED_1000baseT_Full;
10543
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010544 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010545 mask |= ADVERTISED_100baseT_Half |
10546 ADVERTISED_100baseT_Full |
10547 ADVERTISED_10baseT_Half |
10548 ADVERTISED_10baseT_Full |
10549 ADVERTISED_TP;
10550 else
10551 mask |= ADVERTISED_FIBRE;
10552
10553 if (cmd->advertising & ~mask)
10554 return -EINVAL;
10555
10556 mask &= (ADVERTISED_1000baseT_Half |
10557 ADVERTISED_1000baseT_Full |
10558 ADVERTISED_100baseT_Half |
10559 ADVERTISED_100baseT_Full |
10560 ADVERTISED_10baseT_Half |
10561 ADVERTISED_10baseT_Full);
10562
10563 cmd->advertising &= mask;
10564 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010565 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
David Decotigny25db0332011-04-27 18:32:39 +000010566 if (speed != SPEED_1000)
Matt Carlson7e5856b2009-02-25 14:23:01 +000010567 return -EINVAL;
10568
10569 if (cmd->duplex != DUPLEX_FULL)
10570 return -EINVAL;
10571 } else {
David Decotigny25db0332011-04-27 18:32:39 +000010572 if (speed != SPEED_100 &&
10573 speed != SPEED_10)
Matt Carlson7e5856b2009-02-25 14:23:01 +000010574 return -EINVAL;
10575 }
10576 }
10577
David S. Millerf47c11e2005-06-24 20:18:35 -070010578 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010579
10580 tp->link_config.autoneg = cmd->autoneg;
10581 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -070010582 tp->link_config.advertising = (cmd->advertising |
10583 ADVERTISED_Autoneg);
Matt Carlsone7405222012-02-13 15:20:16 +000010584 tp->link_config.speed = SPEED_UNKNOWN;
10585 tp->link_config.duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010586 } else {
10587 tp->link_config.advertising = 0;
David Decotigny25db0332011-04-27 18:32:39 +000010588 tp->link_config.speed = speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010589 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010590 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010591
Linus Torvalds1da177e2005-04-16 15:20:36 -070010592 if (netif_running(dev))
10593 tg3_setup_phy(tp, 1);
10594
David S. Millerf47c11e2005-06-24 20:18:35 -070010595 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010596
Linus Torvalds1da177e2005-04-16 15:20:36 -070010597 return 0;
10598}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010599
Linus Torvalds1da177e2005-04-16 15:20:36 -070010600static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
10601{
10602 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010603
Rick Jones68aad782011-11-07 13:29:27 +000010604 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
10605 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
10606 strlcpy(info->fw_version, tp->fw_ver, sizeof(info->fw_version));
10607 strlcpy(info->bus_info, pci_name(tp->pdev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010608}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010609
Linus Torvalds1da177e2005-04-16 15:20:36 -070010610static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10611{
10612 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010613
Joe Perches63c3a662011-04-26 08:12:10 +000010614 if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070010615 wol->supported = WAKE_MAGIC;
10616 else
10617 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010618 wol->wolopts = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010619 if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010620 wol->wolopts = WAKE_MAGIC;
10621 memset(&wol->sopass, 0, sizeof(wol->sopass));
10622}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010623
Linus Torvalds1da177e2005-04-16 15:20:36 -070010624static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10625{
10626 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070010627 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010628
Linus Torvalds1da177e2005-04-16 15:20:36 -070010629 if (wol->wolopts & ~WAKE_MAGIC)
10630 return -EINVAL;
10631 if ((wol->wolopts & WAKE_MAGIC) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010632 !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010633 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010634
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010635 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
10636
David S. Millerf47c11e2005-06-24 20:18:35 -070010637 spin_lock_bh(&tp->lock);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010638 if (device_may_wakeup(dp))
Joe Perches63c3a662011-04-26 08:12:10 +000010639 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010640 else
Joe Perches63c3a662011-04-26 08:12:10 +000010641 tg3_flag_clear(tp, WOL_ENABLE);
David S. Millerf47c11e2005-06-24 20:18:35 -070010642 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010643
Linus Torvalds1da177e2005-04-16 15:20:36 -070010644 return 0;
10645}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010646
Linus Torvalds1da177e2005-04-16 15:20:36 -070010647static u32 tg3_get_msglevel(struct net_device *dev)
10648{
10649 struct tg3 *tp = netdev_priv(dev);
10650 return tp->msg_enable;
10651}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010652
Linus Torvalds1da177e2005-04-16 15:20:36 -070010653static void tg3_set_msglevel(struct net_device *dev, u32 value)
10654{
10655 struct tg3 *tp = netdev_priv(dev);
10656 tp->msg_enable = value;
10657}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010658
Linus Torvalds1da177e2005-04-16 15:20:36 -070010659static int tg3_nway_reset(struct net_device *dev)
10660{
10661 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010662 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010663
Linus Torvalds1da177e2005-04-16 15:20:36 -070010664 if (!netif_running(dev))
10665 return -EAGAIN;
10666
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010667 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070010668 return -EINVAL;
10669
Joe Perches63c3a662011-04-26 08:12:10 +000010670 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010671 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010672 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010673 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010674 } else {
10675 u32 bmcr;
10676
10677 spin_lock_bh(&tp->lock);
10678 r = -EINVAL;
10679 tg3_readphy(tp, MII_BMCR, &bmcr);
10680 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
10681 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010682 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010683 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
10684 BMCR_ANENABLE);
10685 r = 0;
10686 }
10687 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010688 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010689
Linus Torvalds1da177e2005-04-16 15:20:36 -070010690 return r;
10691}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010692
Linus Torvalds1da177e2005-04-16 15:20:36 -070010693static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10694{
10695 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010696
Matt Carlson2c49a442010-09-30 10:34:35 +000010697 ering->rx_max_pending = tp->rx_std_ring_mask;
Joe Perches63c3a662011-04-26 08:12:10 +000010698 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson2c49a442010-09-30 10:34:35 +000010699 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080010700 else
10701 ering->rx_jumbo_max_pending = 0;
10702
10703 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010704
10705 ering->rx_pending = tp->rx_pending;
Joe Perches63c3a662011-04-26 08:12:10 +000010706 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Michael Chan4f81c322006-03-20 21:33:42 -080010707 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
10708 else
10709 ering->rx_jumbo_pending = 0;
10710
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010711 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010712}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010713
Linus Torvalds1da177e2005-04-16 15:20:36 -070010714static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10715{
10716 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000010717 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010718
Matt Carlson2c49a442010-09-30 10:34:35 +000010719 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
10720 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070010721 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
10722 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Joe Perches63c3a662011-04-26 08:12:10 +000010723 (tg3_flag(tp, TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070010724 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010725 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010726
Michael Chanbbe832c2005-06-24 20:20:04 -070010727 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010728 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010729 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010730 irq_sync = 1;
10731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010732
Michael Chanbbe832c2005-06-24 20:20:04 -070010733 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010734
Linus Torvalds1da177e2005-04-16 15:20:36 -070010735 tp->rx_pending = ering->rx_pending;
10736
Joe Perches63c3a662011-04-26 08:12:10 +000010737 if (tg3_flag(tp, MAX_RXPEND_64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010738 tp->rx_pending > 63)
10739 tp->rx_pending = 63;
10740 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000010741
Matt Carlson6fd45cb2010-09-15 08:59:57 +000010742 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000010743 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010744
10745 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070010746 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -070010747 err = tg3_restart_hw(tp, 1);
10748 if (!err)
10749 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010750 }
10751
David S. Millerf47c11e2005-06-24 20:18:35 -070010752 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010753
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010754 if (irq_sync && !err)
10755 tg3_phy_start(tp);
10756
Michael Chanb9ec6c12006-07-25 16:37:27 -070010757 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010758}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010759
Linus Torvalds1da177e2005-04-16 15:20:36 -070010760static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10761{
10762 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010763
Joe Perches63c3a662011-04-26 08:12:10 +000010764 epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
Matt Carlson8d018622007-12-20 20:05:44 -080010765
Matt Carlson4a2db502011-12-08 14:40:17 +000010766 if (tp->link_config.flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080010767 epause->rx_pause = 1;
10768 else
10769 epause->rx_pause = 0;
10770
Matt Carlson4a2db502011-12-08 14:40:17 +000010771 if (tp->link_config.flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080010772 epause->tx_pause = 1;
10773 else
10774 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010775}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010776
Linus Torvalds1da177e2005-04-16 15:20:36 -070010777static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10778{
10779 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010780 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010781
Joe Perches63c3a662011-04-26 08:12:10 +000010782 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson27121682010-02-17 15:16:57 +000010783 u32 newadv;
10784 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010785
Matt Carlson27121682010-02-17 15:16:57 +000010786 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010787
Matt Carlson27121682010-02-17 15:16:57 +000010788 if (!(phydev->supported & SUPPORTED_Pause) ||
10789 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000010790 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000010791 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010792
Matt Carlson27121682010-02-17 15:16:57 +000010793 tp->link_config.flowctrl = 0;
10794 if (epause->rx_pause) {
10795 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010796
Matt Carlson27121682010-02-17 15:16:57 +000010797 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080010798 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000010799 newadv = ADVERTISED_Pause;
10800 } else
10801 newadv = ADVERTISED_Pause |
10802 ADVERTISED_Asym_Pause;
10803 } else if (epause->tx_pause) {
10804 tp->link_config.flowctrl |= FLOW_CTRL_TX;
10805 newadv = ADVERTISED_Asym_Pause;
10806 } else
10807 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010808
Matt Carlson27121682010-02-17 15:16:57 +000010809 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010810 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010811 else
Joe Perches63c3a662011-04-26 08:12:10 +000010812 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010813
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010814 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000010815 u32 oldadv = phydev->advertising &
10816 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
10817 if (oldadv != newadv) {
10818 phydev->advertising &=
10819 ~(ADVERTISED_Pause |
10820 ADVERTISED_Asym_Pause);
10821 phydev->advertising |= newadv;
10822 if (phydev->autoneg) {
10823 /*
10824 * Always renegotiate the link to
10825 * inform our link partner of our
10826 * flow control settings, even if the
10827 * flow control is forced. Let
10828 * tg3_adjust_link() do the final
10829 * flow control setup.
10830 */
10831 return phy_start_aneg(phydev);
10832 }
10833 }
10834
10835 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010836 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000010837 } else {
Matt Carlsonc6700ce2012-02-13 15:20:15 +000010838 tp->link_config.advertising &=
Matt Carlson27121682010-02-17 15:16:57 +000010839 ~(ADVERTISED_Pause |
10840 ADVERTISED_Asym_Pause);
Matt Carlsonc6700ce2012-02-13 15:20:15 +000010841 tp->link_config.advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010842 }
10843 } else {
10844 int irq_sync = 0;
10845
10846 if (netif_running(dev)) {
10847 tg3_netif_stop(tp);
10848 irq_sync = 1;
10849 }
10850
10851 tg3_full_lock(tp, irq_sync);
10852
10853 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010854 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010855 else
Joe Perches63c3a662011-04-26 08:12:10 +000010856 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010857 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010858 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010859 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010860 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010861 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010862 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010863 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010864 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010865
10866 if (netif_running(dev)) {
10867 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10868 err = tg3_restart_hw(tp, 1);
10869 if (!err)
10870 tg3_netif_start(tp);
10871 }
10872
10873 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010874 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010875
Michael Chanb9ec6c12006-07-25 16:37:27 -070010876 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010877}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010878
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010879static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010880{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010881 switch (sset) {
10882 case ETH_SS_TEST:
10883 return TG3_NUM_TEST;
10884 case ETH_SS_STATS:
10885 return TG3_NUM_STATS;
10886 default:
10887 return -EOPNOTSUPP;
10888 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070010889}
10890
Matt Carlson90415472011-12-16 13:33:23 +000010891static int tg3_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
10892 u32 *rules __always_unused)
10893{
10894 struct tg3 *tp = netdev_priv(dev);
10895
10896 if (!tg3_flag(tp, SUPPORT_MSIX))
10897 return -EOPNOTSUPP;
10898
10899 switch (info->cmd) {
10900 case ETHTOOL_GRXRINGS:
10901 if (netif_running(tp->dev))
10902 info->data = tp->irq_cnt;
10903 else {
10904 info->data = num_online_cpus();
10905 if (info->data > TG3_IRQ_MAX_VECS_RSS)
10906 info->data = TG3_IRQ_MAX_VECS_RSS;
10907 }
10908
10909 /* The first interrupt vector only
10910 * handles link interrupts.
10911 */
10912 info->data -= 1;
10913 return 0;
10914
10915 default:
10916 return -EOPNOTSUPP;
10917 }
10918}
10919
10920static u32 tg3_get_rxfh_indir_size(struct net_device *dev)
10921{
10922 u32 size = 0;
10923 struct tg3 *tp = netdev_priv(dev);
10924
10925 if (tg3_flag(tp, SUPPORT_MSIX))
10926 size = TG3_RSS_INDIR_TBL_SIZE;
10927
10928 return size;
10929}
10930
10931static int tg3_get_rxfh_indir(struct net_device *dev, u32 *indir)
10932{
10933 struct tg3 *tp = netdev_priv(dev);
10934 int i;
10935
10936 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
10937 indir[i] = tp->rss_ind_tbl[i];
10938
10939 return 0;
10940}
10941
10942static int tg3_set_rxfh_indir(struct net_device *dev, const u32 *indir)
10943{
10944 struct tg3 *tp = netdev_priv(dev);
10945 size_t i;
10946
10947 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
10948 tp->rss_ind_tbl[i] = indir[i];
10949
10950 if (!netif_running(dev) || !tg3_flag(tp, ENABLE_RSS))
10951 return 0;
10952
10953 /* It is legal to write the indirection
10954 * table while the device is running.
10955 */
10956 tg3_full_lock(tp, 0);
10957 tg3_rss_write_indir_tbl(tp);
10958 tg3_full_unlock(tp);
10959
10960 return 0;
10961}
10962
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010963static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010964{
10965 switch (stringset) {
10966 case ETH_SS_STATS:
10967 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
10968 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070010969 case ETH_SS_TEST:
10970 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
10971 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010972 default:
10973 WARN_ON(1); /* we need a WARN() */
10974 break;
10975 }
10976}
10977
stephen hemminger81b87092011-04-04 08:43:50 +000010978static int tg3_set_phys_id(struct net_device *dev,
10979 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070010980{
10981 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070010982
10983 if (!netif_running(tp->dev))
10984 return -EAGAIN;
10985
stephen hemminger81b87092011-04-04 08:43:50 +000010986 switch (state) {
10987 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +000010988 return 1; /* cycle on/off once per second */
Michael Chan4009a932005-09-05 17:52:54 -070010989
stephen hemminger81b87092011-04-04 08:43:50 +000010990 case ETHTOOL_ID_ON:
10991 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10992 LED_CTRL_1000MBPS_ON |
10993 LED_CTRL_100MBPS_ON |
10994 LED_CTRL_10MBPS_ON |
10995 LED_CTRL_TRAFFIC_OVERRIDE |
10996 LED_CTRL_TRAFFIC_BLINK |
10997 LED_CTRL_TRAFFIC_LED);
10998 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010999
stephen hemminger81b87092011-04-04 08:43:50 +000011000 case ETHTOOL_ID_OFF:
11001 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
11002 LED_CTRL_TRAFFIC_OVERRIDE);
11003 break;
Michael Chan4009a932005-09-05 17:52:54 -070011004
stephen hemminger81b87092011-04-04 08:43:50 +000011005 case ETHTOOL_ID_INACTIVE:
11006 tw32(MAC_LED_CTRL, tp->led_ctrl);
11007 break;
Michael Chan4009a932005-09-05 17:52:54 -070011008 }
stephen hemminger81b87092011-04-04 08:43:50 +000011009
Michael Chan4009a932005-09-05 17:52:54 -070011010 return 0;
11011}
11012
Matt Carlsonde6f31e2010-04-12 06:58:30 +000011013static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011014 struct ethtool_stats *estats, u64 *tmp_stats)
11015{
11016 struct tg3 *tp = netdev_priv(dev);
Matt Carlson0e6c9da2011-12-08 14:40:13 +000011017
Matt Carlsonb546e462012-02-13 15:20:09 +000011018 if (tp->hw_stats)
11019 tg3_get_estats(tp, (struct tg3_ethtool_stats *)tmp_stats);
11020 else
11021 memset(tmp_stats, 0, sizeof(struct tg3_ethtool_stats));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011022}
11023
Matt Carlson535a4902011-07-20 10:20:56 +000011024static __be32 *tg3_vpd_readblock(struct tg3 *tp, u32 *vpdlen)
Matt Carlsonc3e94502011-04-13 11:05:08 +000011025{
11026 int i;
11027 __be32 *buf;
11028 u32 offset = 0, len = 0;
11029 u32 magic, val;
11030
Joe Perches63c3a662011-04-26 08:12:10 +000011031 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
Matt Carlsonc3e94502011-04-13 11:05:08 +000011032 return NULL;
11033
11034 if (magic == TG3_EEPROM_MAGIC) {
11035 for (offset = TG3_NVM_DIR_START;
11036 offset < TG3_NVM_DIR_END;
11037 offset += TG3_NVM_DIRENT_SIZE) {
11038 if (tg3_nvram_read(tp, offset, &val))
11039 return NULL;
11040
11041 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
11042 TG3_NVM_DIRTYPE_EXTVPD)
11043 break;
11044 }
11045
11046 if (offset != TG3_NVM_DIR_END) {
11047 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
11048 if (tg3_nvram_read(tp, offset + 4, &offset))
11049 return NULL;
11050
11051 offset = tg3_nvram_logical_addr(tp, offset);
11052 }
11053 }
11054
11055 if (!offset || !len) {
11056 offset = TG3_NVM_VPD_OFF;
11057 len = TG3_NVM_VPD_LEN;
11058 }
11059
11060 buf = kmalloc(len, GFP_KERNEL);
11061 if (buf == NULL)
11062 return NULL;
11063
11064 if (magic == TG3_EEPROM_MAGIC) {
11065 for (i = 0; i < len; i += 4) {
11066 /* The data is in little-endian format in NVRAM.
11067 * Use the big-endian read routines to preserve
11068 * the byte order as it exists in NVRAM.
11069 */
11070 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
11071 goto error;
11072 }
11073 } else {
11074 u8 *ptr;
11075 ssize_t cnt;
11076 unsigned int pos = 0;
11077
11078 ptr = (u8 *)&buf[0];
11079 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
11080 cnt = pci_read_vpd(tp->pdev, pos,
11081 len - pos, ptr);
11082 if (cnt == -ETIMEDOUT || cnt == -EINTR)
11083 cnt = 0;
11084 else if (cnt < 0)
11085 goto error;
11086 }
11087 if (pos != len)
11088 goto error;
11089 }
11090
Matt Carlson535a4902011-07-20 10:20:56 +000011091 *vpdlen = len;
11092
Matt Carlsonc3e94502011-04-13 11:05:08 +000011093 return buf;
11094
11095error:
11096 kfree(buf);
11097 return NULL;
11098}
11099
Michael Chan566f86a2005-05-29 14:56:58 -070011100#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080011101#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
11102#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
11103#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Matt Carlson727a6d92011-06-13 13:38:58 +000011104#define NVRAM_SELFBOOT_FORMAT1_4_SIZE 0x20
11105#define NVRAM_SELFBOOT_FORMAT1_5_SIZE 0x24
Matt Carlsonbda18fa2011-07-20 10:20:57 +000011106#define NVRAM_SELFBOOT_FORMAT1_6_SIZE 0x50
Michael Chanb16250e2006-09-27 16:10:14 -070011107#define NVRAM_SELFBOOT_HW_SIZE 0x20
11108#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070011109
11110static int tg3_test_nvram(struct tg3 *tp)
11111{
Matt Carlson535a4902011-07-20 10:20:56 +000011112 u32 csum, magic, len;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011113 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010011114 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070011115
Joe Perches63c3a662011-04-26 08:12:10 +000011116 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000011117 return 0;
11118
Matt Carlsone4f34112009-02-25 14:25:00 +000011119 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080011120 return -EIO;
11121
Michael Chan1b277772006-03-20 22:27:48 -080011122 if (magic == TG3_EEPROM_MAGIC)
11123 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070011124 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080011125 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
11126 TG3_EEPROM_SB_FORMAT_1) {
11127 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
11128 case TG3_EEPROM_SB_REVISION_0:
11129 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
11130 break;
11131 case TG3_EEPROM_SB_REVISION_2:
11132 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
11133 break;
11134 case TG3_EEPROM_SB_REVISION_3:
11135 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
11136 break;
Matt Carlson727a6d92011-06-13 13:38:58 +000011137 case TG3_EEPROM_SB_REVISION_4:
11138 size = NVRAM_SELFBOOT_FORMAT1_4_SIZE;
11139 break;
11140 case TG3_EEPROM_SB_REVISION_5:
11141 size = NVRAM_SELFBOOT_FORMAT1_5_SIZE;
11142 break;
11143 case TG3_EEPROM_SB_REVISION_6:
11144 size = NVRAM_SELFBOOT_FORMAT1_6_SIZE;
11145 break;
Matt Carlsona5767de2007-11-12 21:10:58 -080011146 default:
Matt Carlson727a6d92011-06-13 13:38:58 +000011147 return -EIO;
Matt Carlsona5767de2007-11-12 21:10:58 -080011148 }
11149 } else
Michael Chan1b277772006-03-20 22:27:48 -080011150 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070011151 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
11152 size = NVRAM_SELFBOOT_HW_SIZE;
11153 else
Michael Chan1b277772006-03-20 22:27:48 -080011154 return -EIO;
11155
11156 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070011157 if (buf == NULL)
11158 return -ENOMEM;
11159
Michael Chan1b277772006-03-20 22:27:48 -080011160 err = -EIO;
11161 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000011162 err = tg3_nvram_read_be32(tp, i, &buf[j]);
11163 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070011164 break;
Michael Chan566f86a2005-05-29 14:56:58 -070011165 }
Michael Chan1b277772006-03-20 22:27:48 -080011166 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070011167 goto out;
11168
Michael Chan1b277772006-03-20 22:27:48 -080011169 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000011170 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080011171 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070011172 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080011173 u8 *buf8 = (u8 *) buf, csum8 = 0;
11174
Al Virob9fc7dc2007-12-17 22:59:57 -080011175 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080011176 TG3_EEPROM_SB_REVISION_2) {
11177 /* For rev 2, the csum doesn't include the MBA. */
11178 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
11179 csum8 += buf8[i];
11180 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
11181 csum8 += buf8[i];
11182 } else {
11183 for (i = 0; i < size; i++)
11184 csum8 += buf8[i];
11185 }
Michael Chan1b277772006-03-20 22:27:48 -080011186
Adrian Bunkad96b482006-04-05 22:21:04 -070011187 if (csum8 == 0) {
11188 err = 0;
11189 goto out;
11190 }
11191
11192 err = -EIO;
11193 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080011194 }
Michael Chan566f86a2005-05-29 14:56:58 -070011195
Al Virob9fc7dc2007-12-17 22:59:57 -080011196 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070011197 TG3_EEPROM_MAGIC_HW) {
11198 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000011199 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070011200 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070011201
11202 /* Separate the parity bits and the data bytes. */
11203 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
11204 if ((i == 0) || (i == 8)) {
11205 int l;
11206 u8 msk;
11207
11208 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
11209 parity[k++] = buf8[i] & msk;
11210 i++;
Matt Carlson859a588792010-04-05 10:19:28 +000011211 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070011212 int l;
11213 u8 msk;
11214
11215 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
11216 parity[k++] = buf8[i] & msk;
11217 i++;
11218
11219 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
11220 parity[k++] = buf8[i] & msk;
11221 i++;
11222 }
11223 data[j++] = buf8[i];
11224 }
11225
11226 err = -EIO;
11227 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
11228 u8 hw8 = hweight8(data[i]);
11229
11230 if ((hw8 & 0x1) && parity[i])
11231 goto out;
11232 else if (!(hw8 & 0x1) && !parity[i])
11233 goto out;
11234 }
11235 err = 0;
11236 goto out;
11237 }
11238
Matt Carlson01c3a392011-03-09 16:58:20 +000011239 err = -EIO;
11240
Michael Chan566f86a2005-05-29 14:56:58 -070011241 /* Bootstrap checksum at offset 0x10 */
11242 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000011243 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070011244 goto out;
11245
11246 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
11247 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000011248 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000011249 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070011250
Matt Carlsonc3e94502011-04-13 11:05:08 +000011251 kfree(buf);
11252
Matt Carlson535a4902011-07-20 10:20:56 +000011253 buf = tg3_vpd_readblock(tp, &len);
Matt Carlsonc3e94502011-04-13 11:05:08 +000011254 if (!buf)
11255 return -ENOMEM;
Matt Carlsond4894f32011-03-09 16:58:21 +000011256
Matt Carlson535a4902011-07-20 10:20:56 +000011257 i = pci_vpd_find_tag((u8 *)buf, 0, len, PCI_VPD_LRDT_RO_DATA);
Matt Carlsond4894f32011-03-09 16:58:21 +000011258 if (i > 0) {
11259 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
11260 if (j < 0)
11261 goto out;
11262
Matt Carlson535a4902011-07-20 10:20:56 +000011263 if (i + PCI_VPD_LRDT_TAG_SIZE + j > len)
Matt Carlsond4894f32011-03-09 16:58:21 +000011264 goto out;
11265
11266 i += PCI_VPD_LRDT_TAG_SIZE;
11267 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
11268 PCI_VPD_RO_KEYWORD_CHKSUM);
11269 if (j > 0) {
11270 u8 csum8 = 0;
11271
11272 j += PCI_VPD_INFO_FLD_HDR_SIZE;
11273
11274 for (i = 0; i <= j; i++)
11275 csum8 += ((u8 *)buf)[i];
11276
11277 if (csum8)
11278 goto out;
11279 }
11280 }
11281
Michael Chan566f86a2005-05-29 14:56:58 -070011282 err = 0;
11283
11284out:
11285 kfree(buf);
11286 return err;
11287}
11288
Michael Chanca430072005-05-29 14:57:23 -070011289#define TG3_SERDES_TIMEOUT_SEC 2
11290#define TG3_COPPER_TIMEOUT_SEC 6
11291
11292static int tg3_test_link(struct tg3 *tp)
11293{
11294 int i, max;
11295
11296 if (!netif_running(tp->dev))
11297 return -ENODEV;
11298
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011299 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070011300 max = TG3_SERDES_TIMEOUT_SEC;
11301 else
11302 max = TG3_COPPER_TIMEOUT_SEC;
11303
11304 for (i = 0; i < max; i++) {
11305 if (netif_carrier_ok(tp->dev))
11306 return 0;
11307
11308 if (msleep_interruptible(1000))
11309 break;
11310 }
11311
11312 return -EIO;
11313}
11314
Michael Chana71116d2005-05-29 14:58:11 -070011315/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080011316static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070011317{
Michael Chanb16250e2006-09-27 16:10:14 -070011318 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070011319 u32 offset, read_mask, write_mask, val, save_val, read_val;
11320 static struct {
11321 u16 offset;
11322 u16 flags;
11323#define TG3_FL_5705 0x1
11324#define TG3_FL_NOT_5705 0x2
11325#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070011326#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070011327 u32 read_mask;
11328 u32 write_mask;
11329 } reg_tbl[] = {
11330 /* MAC Control Registers */
11331 { MAC_MODE, TG3_FL_NOT_5705,
11332 0x00000000, 0x00ef6f8c },
11333 { MAC_MODE, TG3_FL_5705,
11334 0x00000000, 0x01ef6b8c },
11335 { MAC_STATUS, TG3_FL_NOT_5705,
11336 0x03800107, 0x00000000 },
11337 { MAC_STATUS, TG3_FL_5705,
11338 0x03800100, 0x00000000 },
11339 { MAC_ADDR_0_HIGH, 0x0000,
11340 0x00000000, 0x0000ffff },
11341 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011342 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070011343 { MAC_RX_MTU_SIZE, 0x0000,
11344 0x00000000, 0x0000ffff },
11345 { MAC_TX_MODE, 0x0000,
11346 0x00000000, 0x00000070 },
11347 { MAC_TX_LENGTHS, 0x0000,
11348 0x00000000, 0x00003fff },
11349 { MAC_RX_MODE, TG3_FL_NOT_5705,
11350 0x00000000, 0x000007fc },
11351 { MAC_RX_MODE, TG3_FL_5705,
11352 0x00000000, 0x000007dc },
11353 { MAC_HASH_REG_0, 0x0000,
11354 0x00000000, 0xffffffff },
11355 { MAC_HASH_REG_1, 0x0000,
11356 0x00000000, 0xffffffff },
11357 { MAC_HASH_REG_2, 0x0000,
11358 0x00000000, 0xffffffff },
11359 { MAC_HASH_REG_3, 0x0000,
11360 0x00000000, 0xffffffff },
11361
11362 /* Receive Data and Receive BD Initiator Control Registers. */
11363 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
11364 0x00000000, 0xffffffff },
11365 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
11366 0x00000000, 0xffffffff },
11367 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
11368 0x00000000, 0x00000003 },
11369 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
11370 0x00000000, 0xffffffff },
11371 { RCVDBDI_STD_BD+0, 0x0000,
11372 0x00000000, 0xffffffff },
11373 { RCVDBDI_STD_BD+4, 0x0000,
11374 0x00000000, 0xffffffff },
11375 { RCVDBDI_STD_BD+8, 0x0000,
11376 0x00000000, 0xffff0002 },
11377 { RCVDBDI_STD_BD+0xc, 0x0000,
11378 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011379
Michael Chana71116d2005-05-29 14:58:11 -070011380 /* Receive BD Initiator Control Registers. */
11381 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
11382 0x00000000, 0xffffffff },
11383 { RCVBDI_STD_THRESH, TG3_FL_5705,
11384 0x00000000, 0x000003ff },
11385 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
11386 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011387
Michael Chana71116d2005-05-29 14:58:11 -070011388 /* Host Coalescing Control Registers. */
11389 { HOSTCC_MODE, TG3_FL_NOT_5705,
11390 0x00000000, 0x00000004 },
11391 { HOSTCC_MODE, TG3_FL_5705,
11392 0x00000000, 0x000000f6 },
11393 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
11394 0x00000000, 0xffffffff },
11395 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
11396 0x00000000, 0x000003ff },
11397 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
11398 0x00000000, 0xffffffff },
11399 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
11400 0x00000000, 0x000003ff },
11401 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
11402 0x00000000, 0xffffffff },
11403 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
11404 0x00000000, 0x000000ff },
11405 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
11406 0x00000000, 0xffffffff },
11407 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
11408 0x00000000, 0x000000ff },
11409 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
11410 0x00000000, 0xffffffff },
11411 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
11412 0x00000000, 0xffffffff },
11413 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
11414 0x00000000, 0xffffffff },
11415 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
11416 0x00000000, 0x000000ff },
11417 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
11418 0x00000000, 0xffffffff },
11419 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
11420 0x00000000, 0x000000ff },
11421 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
11422 0x00000000, 0xffffffff },
11423 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
11424 0x00000000, 0xffffffff },
11425 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
11426 0x00000000, 0xffffffff },
11427 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
11428 0x00000000, 0xffffffff },
11429 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
11430 0x00000000, 0xffffffff },
11431 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
11432 0xffffffff, 0x00000000 },
11433 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
11434 0xffffffff, 0x00000000 },
11435
11436 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070011437 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070011438 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070011439 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070011440 0x00000000, 0x007fffff },
11441 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
11442 0x00000000, 0x0000003f },
11443 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
11444 0x00000000, 0x000001ff },
11445 { BUFMGR_MB_HIGH_WATER, 0x0000,
11446 0x00000000, 0x000001ff },
11447 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
11448 0xffffffff, 0x00000000 },
11449 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
11450 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011451
Michael Chana71116d2005-05-29 14:58:11 -070011452 /* Mailbox Registers */
11453 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
11454 0x00000000, 0x000001ff },
11455 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
11456 0x00000000, 0x000001ff },
11457 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
11458 0x00000000, 0x000007ff },
11459 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
11460 0x00000000, 0x000001ff },
11461
11462 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
11463 };
11464
Michael Chanb16250e2006-09-27 16:10:14 -070011465 is_5705 = is_5750 = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000011466 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chana71116d2005-05-29 14:58:11 -070011467 is_5705 = 1;
Joe Perches63c3a662011-04-26 08:12:10 +000011468 if (tg3_flag(tp, 5750_PLUS))
Michael Chanb16250e2006-09-27 16:10:14 -070011469 is_5750 = 1;
11470 }
Michael Chana71116d2005-05-29 14:58:11 -070011471
11472 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
11473 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
11474 continue;
11475
11476 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
11477 continue;
11478
Joe Perches63c3a662011-04-26 08:12:10 +000011479 if (tg3_flag(tp, IS_5788) &&
Michael Chana71116d2005-05-29 14:58:11 -070011480 (reg_tbl[i].flags & TG3_FL_NOT_5788))
11481 continue;
11482
Michael Chanb16250e2006-09-27 16:10:14 -070011483 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
11484 continue;
11485
Michael Chana71116d2005-05-29 14:58:11 -070011486 offset = (u32) reg_tbl[i].offset;
11487 read_mask = reg_tbl[i].read_mask;
11488 write_mask = reg_tbl[i].write_mask;
11489
11490 /* Save the original register content */
11491 save_val = tr32(offset);
11492
11493 /* Determine the read-only value. */
11494 read_val = save_val & read_mask;
11495
11496 /* Write zero to the register, then make sure the read-only bits
11497 * are not changed and the read/write bits are all zeros.
11498 */
11499 tw32(offset, 0);
11500
11501 val = tr32(offset);
11502
11503 /* Test the read-only and read/write bits. */
11504 if (((val & read_mask) != read_val) || (val & write_mask))
11505 goto out;
11506
11507 /* Write ones to all the bits defined by RdMask and WrMask, then
11508 * make sure the read-only bits are not changed and the
11509 * read/write bits are all ones.
11510 */
11511 tw32(offset, read_mask | write_mask);
11512
11513 val = tr32(offset);
11514
11515 /* Test the read-only bits. */
11516 if ((val & read_mask) != read_val)
11517 goto out;
11518
11519 /* Test the read/write bits. */
11520 if ((val & write_mask) != write_mask)
11521 goto out;
11522
11523 tw32(offset, save_val);
11524 }
11525
11526 return 0;
11527
11528out:
Michael Chan9f88f292006-12-07 00:22:54 -080011529 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000011530 netdev_err(tp->dev,
11531 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070011532 tw32(offset, save_val);
11533 return -EIO;
11534}
11535
Michael Chan7942e1d2005-05-29 14:58:36 -070011536static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
11537{
Arjan van de Venf71e1302006-03-03 21:33:57 -050011538 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070011539 int i;
11540 u32 j;
11541
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020011542 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070011543 for (j = 0; j < len; j += 4) {
11544 u32 val;
11545
11546 tg3_write_mem(tp, offset + j, test_pattern[i]);
11547 tg3_read_mem(tp, offset + j, &val);
11548 if (val != test_pattern[i])
11549 return -EIO;
11550 }
11551 }
11552 return 0;
11553}
11554
11555static int tg3_test_memory(struct tg3 *tp)
11556{
11557 static struct mem_entry {
11558 u32 offset;
11559 u32 len;
11560 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080011561 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070011562 { 0x00002000, 0x1c000},
11563 { 0xffffffff, 0x00000}
11564 }, mem_tbl_5705[] = {
11565 { 0x00000100, 0x0000c},
11566 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070011567 { 0x00004000, 0x00800},
11568 { 0x00006000, 0x01000},
11569 { 0x00008000, 0x02000},
11570 { 0x00010000, 0x0e000},
11571 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080011572 }, mem_tbl_5755[] = {
11573 { 0x00000200, 0x00008},
11574 { 0x00004000, 0x00800},
11575 { 0x00006000, 0x00800},
11576 { 0x00008000, 0x02000},
11577 { 0x00010000, 0x0c000},
11578 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070011579 }, mem_tbl_5906[] = {
11580 { 0x00000200, 0x00008},
11581 { 0x00004000, 0x00400},
11582 { 0x00006000, 0x00400},
11583 { 0x00008000, 0x01000},
11584 { 0x00010000, 0x01000},
11585 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011586 }, mem_tbl_5717[] = {
11587 { 0x00000200, 0x00008},
11588 { 0x00010000, 0x0a000},
11589 { 0x00020000, 0x13c00},
11590 { 0xffffffff, 0x00000}
11591 }, mem_tbl_57765[] = {
11592 { 0x00000200, 0x00008},
11593 { 0x00004000, 0x00800},
11594 { 0x00006000, 0x09800},
11595 { 0x00010000, 0x0a000},
11596 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070011597 };
11598 struct mem_entry *mem_tbl;
11599 int err = 0;
11600 int i;
11601
Joe Perches63c3a662011-04-26 08:12:10 +000011602 if (tg3_flag(tp, 5717_PLUS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011603 mem_tbl = mem_tbl_5717;
Matt Carlson55086ad2011-12-14 11:09:59 +000011604 else if (tg3_flag(tp, 57765_CLASS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011605 mem_tbl = mem_tbl_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000011606 else if (tg3_flag(tp, 5755_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080011607 mem_tbl = mem_tbl_5755;
11608 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
11609 mem_tbl = mem_tbl_5906;
Joe Perches63c3a662011-04-26 08:12:10 +000011610 else if (tg3_flag(tp, 5705_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080011611 mem_tbl = mem_tbl_5705;
11612 else
Michael Chan7942e1d2005-05-29 14:58:36 -070011613 mem_tbl = mem_tbl_570x;
11614
11615 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000011616 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
11617 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070011618 break;
11619 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011620
Michael Chan7942e1d2005-05-29 14:58:36 -070011621 return err;
11622}
11623
Matt Carlsonbb158d62011-04-25 12:42:47 +000011624#define TG3_TSO_MSS 500
11625
11626#define TG3_TSO_IP_HDR_LEN 20
11627#define TG3_TSO_TCP_HDR_LEN 20
11628#define TG3_TSO_TCP_OPT_LEN 12
11629
11630static const u8 tg3_tso_header[] = {
116310x08, 0x00,
116320x45, 0x00, 0x00, 0x00,
116330x00, 0x00, 0x40, 0x00,
116340x40, 0x06, 0x00, 0x00,
116350x0a, 0x00, 0x00, 0x01,
116360x0a, 0x00, 0x00, 0x02,
116370x0d, 0x00, 0xe0, 0x00,
116380x00, 0x00, 0x01, 0x00,
116390x00, 0x00, 0x02, 0x00,
116400x80, 0x10, 0x10, 0x00,
116410x14, 0x09, 0x00, 0x00,
116420x01, 0x01, 0x08, 0x0a,
116430x11, 0x11, 0x11, 0x11,
116440x11, 0x11, 0x11, 0x11,
11645};
Michael Chan9f40dea2005-09-05 17:53:06 -070011646
Matt Carlson28a45952011-08-19 13:58:22 +000011647static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, bool tso_loopback)
Michael Chanc76949a2005-05-29 14:58:59 -070011648{
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011649 u32 rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011650 u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
Matt Carlson84b67b22011-07-27 14:20:52 +000011651 u32 budget;
Eric Dumazet9205fd92011-11-18 06:47:01 +000011652 struct sk_buff *skb;
11653 u8 *tx_data, *rx_data;
Michael Chanc76949a2005-05-29 14:58:59 -070011654 dma_addr_t map;
11655 int num_pkts, tx_len, rx_len, i, err;
11656 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000011657 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000011658 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070011659
Matt Carlsonc8873402010-02-12 14:47:11 +000011660 tnapi = &tp->napi[0];
11661 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011662 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +000011663 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlson1da85aa2010-09-30 10:34:34 +000011664 rnapi = &tp->napi[1];
Joe Perches63c3a662011-04-26 08:12:10 +000011665 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc8873402010-02-12 14:47:11 +000011666 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011667 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011668 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000011669
Michael Chanc76949a2005-05-29 14:58:59 -070011670 err = -EIO;
11671
Matt Carlson4852a862011-04-13 11:05:07 +000011672 tx_len = pktsz;
David S. Millera20e9c62006-07-31 22:38:16 -070011673 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070011674 if (!skb)
11675 return -ENOMEM;
11676
Michael Chanc76949a2005-05-29 14:58:59 -070011677 tx_data = skb_put(skb, tx_len);
11678 memcpy(tx_data, tp->dev->dev_addr, 6);
11679 memset(tx_data + 6, 0x0, 8);
11680
Matt Carlson4852a862011-04-13 11:05:07 +000011681 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
Michael Chanc76949a2005-05-29 14:58:59 -070011682
Matt Carlson28a45952011-08-19 13:58:22 +000011683 if (tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011684 struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
11685
11686 u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
11687 TG3_TSO_TCP_OPT_LEN;
11688
11689 memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
11690 sizeof(tg3_tso_header));
11691 mss = TG3_TSO_MSS;
11692
11693 val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
11694 num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
11695
11696 /* Set the total length field in the IP header */
11697 iph->tot_len = htons((u16)(mss + hdr_len));
11698
11699 base_flags = (TXD_FLAG_CPU_PRE_DMA |
11700 TXD_FLAG_CPU_POST_DMA);
11701
Joe Perches63c3a662011-04-26 08:12:10 +000011702 if (tg3_flag(tp, HW_TSO_1) ||
11703 tg3_flag(tp, HW_TSO_2) ||
11704 tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011705 struct tcphdr *th;
11706 val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
11707 th = (struct tcphdr *)&tx_data[val];
11708 th->check = 0;
11709 } else
11710 base_flags |= TXD_FLAG_TCPUDP_CSUM;
11711
Joe Perches63c3a662011-04-26 08:12:10 +000011712 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011713 mss |= (hdr_len & 0xc) << 12;
11714 if (hdr_len & 0x10)
11715 base_flags |= 0x00000010;
11716 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +000011717 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlsonbb158d62011-04-25 12:42:47 +000011718 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +000011719 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlsonbb158d62011-04-25 12:42:47 +000011720 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
11721 mss |= (TG3_TSO_TCP_OPT_LEN << 9);
11722 } else {
11723 base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
11724 }
11725
11726 data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
11727 } else {
11728 num_pkts = 1;
11729 data_off = ETH_HLEN;
11730 }
11731
11732 for (i = data_off; i < tx_len; i++)
Michael Chanc76949a2005-05-29 14:58:59 -070011733 tx_data[i] = (u8) (i & 0xff);
11734
Alexander Duyckf4188d82009-12-02 16:48:38 +000011735 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
11736 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000011737 dev_kfree_skb(skb);
11738 return -EIO;
11739 }
Michael Chanc76949a2005-05-29 14:58:59 -070011740
Matt Carlson0d681b22011-07-27 14:20:49 +000011741 val = tnapi->tx_prod;
11742 tnapi->tx_buffers[val].skb = skb;
11743 dma_unmap_addr_set(&tnapi->tx_buffers[val], mapping, map);
11744
Michael Chanc76949a2005-05-29 14:58:59 -070011745 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011746 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011747
11748 udelay(10);
11749
Matt Carlson898a56f2009-08-28 14:02:40 +000011750 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070011751
Matt Carlson84b67b22011-07-27 14:20:52 +000011752 budget = tg3_tx_avail(tnapi);
11753 if (tg3_tx_frag_set(tnapi, &val, &budget, map, tx_len,
Matt Carlsond1a3b732011-07-27 14:20:51 +000011754 base_flags | TXD_FLAG_END, mss, 0)) {
11755 tnapi->tx_buffers[val].skb = NULL;
11756 dev_kfree_skb(skb);
11757 return -EIO;
11758 }
Michael Chanc76949a2005-05-29 14:58:59 -070011759
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011760 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070011761
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011762 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
11763 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070011764
11765 udelay(10);
11766
Matt Carlson303fc922009-11-02 14:27:34 +000011767 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
11768 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070011769 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011770 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011771
11772 udelay(10);
11773
Matt Carlson898a56f2009-08-28 14:02:40 +000011774 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
11775 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011776 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070011777 (rx_idx == (rx_start_idx + num_pkts)))
11778 break;
11779 }
11780
Matt Carlsonba1142e2011-11-04 09:15:00 +000011781 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod - 1, -1);
Michael Chanc76949a2005-05-29 14:58:59 -070011782 dev_kfree_skb(skb);
11783
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011784 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070011785 goto out;
11786
11787 if (rx_idx != rx_start_idx + num_pkts)
11788 goto out;
11789
Matt Carlsonbb158d62011-04-25 12:42:47 +000011790 val = data_off;
11791 while (rx_idx != rx_start_idx) {
11792 desc = &rnapi->rx_rcb[rx_start_idx++];
11793 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
11794 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
Michael Chanc76949a2005-05-29 14:58:59 -070011795
Matt Carlsonbb158d62011-04-25 12:42:47 +000011796 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
11797 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
Matt Carlson4852a862011-04-13 11:05:07 +000011798 goto out;
Michael Chanc76949a2005-05-29 14:58:59 -070011799
Matt Carlsonbb158d62011-04-25 12:42:47 +000011800 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
11801 - ETH_FCS_LEN;
11802
Matt Carlson28a45952011-08-19 13:58:22 +000011803 if (!tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011804 if (rx_len != tx_len)
11805 goto out;
11806
11807 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
11808 if (opaque_key != RXD_OPAQUE_RING_STD)
11809 goto out;
11810 } else {
11811 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
11812 goto out;
11813 }
11814 } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
11815 (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
Matt Carlson54e0a672011-05-19 12:12:50 +000011816 >> RXD_TCPCSUM_SHIFT != 0xffff) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011817 goto out;
11818 }
11819
11820 if (opaque_key == RXD_OPAQUE_RING_STD) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000011821 rx_data = tpr->rx_std_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011822 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
11823 mapping);
11824 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000011825 rx_data = tpr->rx_jmb_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011826 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
11827 mapping);
11828 } else
Matt Carlson4852a862011-04-13 11:05:07 +000011829 goto out;
11830
Matt Carlsonbb158d62011-04-25 12:42:47 +000011831 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
11832 PCI_DMA_FROMDEVICE);
11833
Eric Dumazet9205fd92011-11-18 06:47:01 +000011834 rx_data += TG3_RX_OFFSET(tp);
Matt Carlsonbb158d62011-04-25 12:42:47 +000011835 for (i = data_off; i < rx_len; i++, val++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000011836 if (*(rx_data + i) != (u8) (val & 0xff))
Matt Carlsonbb158d62011-04-25 12:42:47 +000011837 goto out;
11838 }
Matt Carlson4852a862011-04-13 11:05:07 +000011839 }
11840
Michael Chanc76949a2005-05-29 14:58:59 -070011841 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011842
Eric Dumazet9205fd92011-11-18 06:47:01 +000011843 /* tg3_free_rings will unmap and free the rx_data */
Michael Chanc76949a2005-05-29 14:58:59 -070011844out:
11845 return err;
11846}
11847
Matt Carlson00c266b2011-04-25 12:42:46 +000011848#define TG3_STD_LOOPBACK_FAILED 1
11849#define TG3_JMB_LOOPBACK_FAILED 2
Matt Carlsonbb158d62011-04-25 12:42:47 +000011850#define TG3_TSO_LOOPBACK_FAILED 4
Matt Carlson28a45952011-08-19 13:58:22 +000011851#define TG3_LOOPBACK_FAILED \
11852 (TG3_STD_LOOPBACK_FAILED | \
11853 TG3_JMB_LOOPBACK_FAILED | \
11854 TG3_TSO_LOOPBACK_FAILED)
Matt Carlson00c266b2011-04-25 12:42:46 +000011855
Matt Carlson941ec902011-08-19 13:58:23 +000011856static int tg3_test_loopback(struct tg3 *tp, u64 *data, bool do_extlpbk)
Michael Chan9f40dea2005-09-05 17:53:06 -070011857{
Matt Carlson28a45952011-08-19 13:58:22 +000011858 int err = -EIO;
Matt Carlson2215e242011-08-19 13:58:19 +000011859 u32 eee_cap;
Michael Chan9f40dea2005-09-05 17:53:06 -070011860
Matt Carlsonab789042011-01-25 15:58:54 +000011861 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
11862 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
11863
Matt Carlson28a45952011-08-19 13:58:22 +000011864 if (!netif_running(tp->dev)) {
11865 data[0] = TG3_LOOPBACK_FAILED;
11866 data[1] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000011867 if (do_extlpbk)
11868 data[2] = TG3_LOOPBACK_FAILED;
Matt Carlson28a45952011-08-19 13:58:22 +000011869 goto done;
11870 }
11871
Michael Chanb9ec6c12006-07-25 16:37:27 -070011872 err = tg3_reset_hw(tp, 1);
Matt Carlsonab789042011-01-25 15:58:54 +000011873 if (err) {
Matt Carlson28a45952011-08-19 13:58:22 +000011874 data[0] = TG3_LOOPBACK_FAILED;
11875 data[1] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000011876 if (do_extlpbk)
11877 data[2] = TG3_LOOPBACK_FAILED;
Matt Carlsonab789042011-01-25 15:58:54 +000011878 goto done;
11879 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011880
Joe Perches63c3a662011-04-26 08:12:10 +000011881 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson4a85f092011-04-20 07:57:37 +000011882 int i;
11883
11884 /* Reroute all rx packets to the 1st queue */
11885 for (i = MAC_RSS_INDIR_TBL_0;
11886 i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
11887 tw32(i, 0x0);
11888 }
11889
Matt Carlson6e01b202011-08-19 13:58:20 +000011890 /* HW errata - mac loopback fails in some cases on 5780.
11891 * Normal traffic and PHY loopback are not affected by
11892 * errata. Also, the MAC loopback test is deprecated for
11893 * all newer ASIC revisions.
11894 */
11895 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
11896 !tg3_flag(tp, CPMU_PRESENT)) {
11897 tg3_mac_loopback(tp, true);
Matt Carlson9936bcf2007-10-10 18:03:07 -070011898
Matt Carlson28a45952011-08-19 13:58:22 +000011899 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
11900 data[0] |= TG3_STD_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000011901
11902 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000011903 tg3_run_loopback(tp, 9000 + ETH_HLEN, false))
11904 data[0] |= TG3_JMB_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000011905
11906 tg3_mac_loopback(tp, false);
11907 }
Matt Carlson4852a862011-04-13 11:05:07 +000011908
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011909 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000011910 !tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011911 int i;
11912
Matt Carlson941ec902011-08-19 13:58:23 +000011913 tg3_phy_lpbk_set(tp, 0, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011914
11915 /* Wait for link */
11916 for (i = 0; i < 100; i++) {
11917 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
11918 break;
11919 mdelay(1);
11920 }
11921
Matt Carlson28a45952011-08-19 13:58:22 +000011922 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
11923 data[1] |= TG3_STD_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000011924 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000011925 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
11926 data[1] |= TG3_TSO_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000011927 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000011928 tg3_run_loopback(tp, 9000 + ETH_HLEN, false))
11929 data[1] |= TG3_JMB_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070011930
Matt Carlson941ec902011-08-19 13:58:23 +000011931 if (do_extlpbk) {
11932 tg3_phy_lpbk_set(tp, 0, true);
11933
11934 /* All link indications report up, but the hardware
11935 * isn't really ready for about 20 msec. Double it
11936 * to be sure.
11937 */
11938 mdelay(40);
11939
11940 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
11941 data[2] |= TG3_STD_LOOPBACK_FAILED;
11942 if (tg3_flag(tp, TSO_CAPABLE) &&
11943 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
11944 data[2] |= TG3_TSO_LOOPBACK_FAILED;
11945 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
11946 tg3_run_loopback(tp, 9000 + ETH_HLEN, false))
11947 data[2] |= TG3_JMB_LOOPBACK_FAILED;
11948 }
11949
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011950 /* Re-enable gphy autopowerdown. */
11951 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
11952 tg3_phy_toggle_apd(tp, true);
11953 }
Matt Carlson6833c042008-11-21 17:18:59 -080011954
Matt Carlson941ec902011-08-19 13:58:23 +000011955 err = (data[0] | data[1] | data[2]) ? -EIO : 0;
Matt Carlson28a45952011-08-19 13:58:22 +000011956
Matt Carlsonab789042011-01-25 15:58:54 +000011957done:
11958 tp->phy_flags |= eee_cap;
11959
Michael Chan9f40dea2005-09-05 17:53:06 -070011960 return err;
11961}
11962
Michael Chan4cafd3f2005-05-29 14:56:34 -070011963static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
11964 u64 *data)
11965{
Michael Chan566f86a2005-05-29 14:56:58 -070011966 struct tg3 *tp = netdev_priv(dev);
Matt Carlson941ec902011-08-19 13:58:23 +000011967 bool doextlpbk = etest->flags & ETH_TEST_FL_EXTERNAL_LB;
Michael Chan566f86a2005-05-29 14:56:58 -070011968
Matt Carlsonbed98292011-07-13 09:27:29 +000011969 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
11970 tg3_power_up(tp)) {
11971 etest->flags |= ETH_TEST_FL_FAILED;
11972 memset(data, 1, sizeof(u64) * TG3_NUM_TEST);
11973 return;
11974 }
Michael Chanbc1c7562006-03-20 17:48:03 -080011975
Michael Chan566f86a2005-05-29 14:56:58 -070011976 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
11977
11978 if (tg3_test_nvram(tp) != 0) {
11979 etest->flags |= ETH_TEST_FL_FAILED;
11980 data[0] = 1;
11981 }
Matt Carlson941ec902011-08-19 13:58:23 +000011982 if (!doextlpbk && tg3_test_link(tp)) {
Michael Chanca430072005-05-29 14:57:23 -070011983 etest->flags |= ETH_TEST_FL_FAILED;
11984 data[1] = 1;
11985 }
Michael Chana71116d2005-05-29 14:58:11 -070011986 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011987 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070011988
Michael Chanbbe832c2005-06-24 20:20:04 -070011989 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011990 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070011991 tg3_netif_stop(tp);
11992 irq_sync = 1;
11993 }
11994
11995 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070011996
11997 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080011998 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011999 tg3_halt_cpu(tp, RX_CPU_BASE);
Joe Perches63c3a662011-04-26 08:12:10 +000012000 if (!tg3_flag(tp, 5705_PLUS))
Michael Chana71116d2005-05-29 14:58:11 -070012001 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080012002 if (!err)
12003 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012004
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012005 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad12006-03-20 22:27:35 -080012006 tg3_phy_reset(tp);
12007
Michael Chana71116d2005-05-29 14:58:11 -070012008 if (tg3_test_registers(tp) != 0) {
12009 etest->flags |= ETH_TEST_FL_FAILED;
12010 data[2] = 1;
12011 }
Matt Carlson28a45952011-08-19 13:58:22 +000012012
Michael Chan7942e1d2005-05-29 14:58:36 -070012013 if (tg3_test_memory(tp) != 0) {
12014 etest->flags |= ETH_TEST_FL_FAILED;
12015 data[3] = 1;
12016 }
Matt Carlson28a45952011-08-19 13:58:22 +000012017
Matt Carlson941ec902011-08-19 13:58:23 +000012018 if (doextlpbk)
12019 etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
12020
12021 if (tg3_test_loopback(tp, &data[4], doextlpbk))
Michael Chanc76949a2005-05-29 14:58:59 -070012022 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070012023
David S. Millerf47c11e2005-06-24 20:18:35 -070012024 tg3_full_unlock(tp);
12025
Michael Chand4bc3922005-05-29 14:59:20 -070012026 if (tg3_test_interrupt(tp) != 0) {
12027 etest->flags |= ETH_TEST_FL_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000012028 data[7] = 1;
Michael Chand4bc3922005-05-29 14:59:20 -070012029 }
David S. Millerf47c11e2005-06-24 20:18:35 -070012030
12031 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070012032
Michael Chana71116d2005-05-29 14:58:11 -070012033 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
12034 if (netif_running(dev)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012035 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012036 err2 = tg3_restart_hw(tp, 1);
12037 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070012038 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012039 }
David S. Millerf47c11e2005-06-24 20:18:35 -070012040
12041 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012042
12043 if (irq_sync && !err2)
12044 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012045 }
Matt Carlson80096062010-08-02 11:26:06 +000012046 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000012047 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080012048
Michael Chan4cafd3f2005-05-29 14:56:34 -070012049}
12050
Linus Torvalds1da177e2005-04-16 15:20:36 -070012051static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
12052{
12053 struct mii_ioctl_data *data = if_mii(ifr);
12054 struct tg3 *tp = netdev_priv(dev);
12055 int err;
12056
Joe Perches63c3a662011-04-26 08:12:10 +000012057 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000012058 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012059 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012060 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000012061 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000012062 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012063 }
12064
Matt Carlson33f401a2010-04-05 10:19:27 +000012065 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012066 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000012067 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012068
12069 /* fallthru */
12070 case SIOCGMIIREG: {
12071 u32 mii_regval;
12072
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012073 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012074 break; /* We have no PHY */
12075
Matt Carlson34eea5a2011-04-20 07:57:38 +000012076 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080012077 return -EAGAIN;
12078
David S. Millerf47c11e2005-06-24 20:18:35 -070012079 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012080 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070012081 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012082
12083 data->val_out = mii_regval;
12084
12085 return err;
12086 }
12087
12088 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012089 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012090 break; /* We have no PHY */
12091
Matt Carlson34eea5a2011-04-20 07:57:38 +000012092 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080012093 return -EAGAIN;
12094
David S. Millerf47c11e2005-06-24 20:18:35 -070012095 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012096 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070012097 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012098
12099 return err;
12100
12101 default:
12102 /* do nothing */
12103 break;
12104 }
12105 return -EOPNOTSUPP;
12106}
12107
David S. Miller15f98502005-05-18 22:49:26 -070012108static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
12109{
12110 struct tg3 *tp = netdev_priv(dev);
12111
12112 memcpy(ec, &tp->coal, sizeof(*ec));
12113 return 0;
12114}
12115
Michael Chand244c892005-07-05 14:42:33 -070012116static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
12117{
12118 struct tg3 *tp = netdev_priv(dev);
12119 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
12120 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
12121
Joe Perches63c3a662011-04-26 08:12:10 +000012122 if (!tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070012123 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
12124 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
12125 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
12126 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
12127 }
12128
12129 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
12130 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
12131 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
12132 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
12133 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
12134 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
12135 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
12136 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
12137 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
12138 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
12139 return -EINVAL;
12140
12141 /* No rx interrupts will be generated if both are zero */
12142 if ((ec->rx_coalesce_usecs == 0) &&
12143 (ec->rx_max_coalesced_frames == 0))
12144 return -EINVAL;
12145
12146 /* No tx interrupts will be generated if both are zero */
12147 if ((ec->tx_coalesce_usecs == 0) &&
12148 (ec->tx_max_coalesced_frames == 0))
12149 return -EINVAL;
12150
12151 /* Only copy relevant parameters, ignore all others. */
12152 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
12153 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
12154 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
12155 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
12156 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
12157 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
12158 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
12159 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
12160 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
12161
12162 if (netif_running(dev)) {
12163 tg3_full_lock(tp, 0);
12164 __tg3_set_coalesce(tp, &tp->coal);
12165 tg3_full_unlock(tp);
12166 }
12167 return 0;
12168}
12169
Jeff Garzik7282d492006-09-13 14:30:00 -040012170static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012171 .get_settings = tg3_get_settings,
12172 .set_settings = tg3_set_settings,
12173 .get_drvinfo = tg3_get_drvinfo,
12174 .get_regs_len = tg3_get_regs_len,
12175 .get_regs = tg3_get_regs,
12176 .get_wol = tg3_get_wol,
12177 .set_wol = tg3_set_wol,
12178 .get_msglevel = tg3_get_msglevel,
12179 .set_msglevel = tg3_set_msglevel,
12180 .nway_reset = tg3_nway_reset,
12181 .get_link = ethtool_op_get_link,
12182 .get_eeprom_len = tg3_get_eeprom_len,
12183 .get_eeprom = tg3_get_eeprom,
12184 .set_eeprom = tg3_set_eeprom,
12185 .get_ringparam = tg3_get_ringparam,
12186 .set_ringparam = tg3_set_ringparam,
12187 .get_pauseparam = tg3_get_pauseparam,
12188 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070012189 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012190 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000012191 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012192 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070012193 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070012194 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070012195 .get_sset_count = tg3_get_sset_count,
Matt Carlson90415472011-12-16 13:33:23 +000012196 .get_rxnfc = tg3_get_rxnfc,
12197 .get_rxfh_indir_size = tg3_get_rxfh_indir_size,
12198 .get_rxfh_indir = tg3_get_rxfh_indir,
12199 .set_rxfh_indir = tg3_set_rxfh_indir,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012200};
12201
David S. Millerb4017c52012-03-01 17:57:40 -050012202static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
12203 struct rtnl_link_stats64 *stats)
12204{
12205 struct tg3 *tp = netdev_priv(dev);
12206
12207 if (!tp->hw_stats)
12208 return &tp->net_stats_prev;
12209
12210 spin_lock_bh(&tp->lock);
12211 tg3_get_nstats(tp, stats);
12212 spin_unlock_bh(&tp->lock);
12213
12214 return stats;
12215}
12216
Matt Carlsonccd5ba92012-02-13 10:20:08 +000012217static void tg3_set_rx_mode(struct net_device *dev)
12218{
12219 struct tg3 *tp = netdev_priv(dev);
12220
12221 if (!netif_running(dev))
12222 return;
12223
12224 tg3_full_lock(tp, 0);
12225 __tg3_set_rx_mode(dev);
12226 tg3_full_unlock(tp);
12227}
12228
Matt Carlsonfaf16272012-02-13 10:20:07 +000012229static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
12230 int new_mtu)
12231{
12232 dev->mtu = new_mtu;
12233
12234 if (new_mtu > ETH_DATA_LEN) {
12235 if (tg3_flag(tp, 5780_CLASS)) {
12236 netdev_update_features(dev);
12237 tg3_flag_clear(tp, TSO_CAPABLE);
12238 } else {
12239 tg3_flag_set(tp, JUMBO_RING_ENABLE);
12240 }
12241 } else {
12242 if (tg3_flag(tp, 5780_CLASS)) {
12243 tg3_flag_set(tp, TSO_CAPABLE);
12244 netdev_update_features(dev);
12245 }
12246 tg3_flag_clear(tp, JUMBO_RING_ENABLE);
12247 }
12248}
12249
12250static int tg3_change_mtu(struct net_device *dev, int new_mtu)
12251{
12252 struct tg3 *tp = netdev_priv(dev);
12253 int err;
12254
12255 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
12256 return -EINVAL;
12257
12258 if (!netif_running(dev)) {
12259 /* We'll just catch it later when the
12260 * device is up'd.
12261 */
12262 tg3_set_mtu(dev, tp, new_mtu);
12263 return 0;
12264 }
12265
12266 tg3_phy_stop(tp);
12267
12268 tg3_netif_stop(tp);
12269
12270 tg3_full_lock(tp, 1);
12271
12272 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
12273
12274 tg3_set_mtu(dev, tp, new_mtu);
12275
12276 err = tg3_restart_hw(tp, 0);
12277
12278 if (!err)
12279 tg3_netif_start(tp);
12280
12281 tg3_full_unlock(tp);
12282
12283 if (!err)
12284 tg3_phy_start(tp);
12285
12286 return err;
12287}
12288
12289static const struct net_device_ops tg3_netdev_ops = {
12290 .ndo_open = tg3_open,
12291 .ndo_stop = tg3_close,
12292 .ndo_start_xmit = tg3_start_xmit,
12293 .ndo_get_stats64 = tg3_get_stats64,
12294 .ndo_validate_addr = eth_validate_addr,
12295 .ndo_set_rx_mode = tg3_set_rx_mode,
12296 .ndo_set_mac_address = tg3_set_mac_addr,
12297 .ndo_do_ioctl = tg3_ioctl,
12298 .ndo_tx_timeout = tg3_tx_timeout,
12299 .ndo_change_mtu = tg3_change_mtu,
12300 .ndo_fix_features = tg3_fix_features,
12301 .ndo_set_features = tg3_set_features,
12302#ifdef CONFIG_NET_POLL_CONTROLLER
12303 .ndo_poll_controller = tg3_poll_controller,
12304#endif
12305};
12306
Linus Torvalds1da177e2005-04-16 15:20:36 -070012307static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
12308{
Michael Chan1b277772006-03-20 22:27:48 -080012309 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012310
12311 tp->nvram_size = EEPROM_CHIP_SIZE;
12312
Matt Carlsone4f34112009-02-25 14:25:00 +000012313 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012314 return;
12315
Michael Chanb16250e2006-09-27 16:10:14 -070012316 if ((magic != TG3_EEPROM_MAGIC) &&
12317 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
12318 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012319 return;
12320
12321 /*
12322 * Size the chip by reading offsets at increasing powers of two.
12323 * When we encounter our validation signature, we know the addressing
12324 * has wrapped around, and thus have our chip size.
12325 */
Michael Chan1b277772006-03-20 22:27:48 -080012326 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012327
12328 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000012329 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012330 return;
12331
Michael Chan18201802006-03-20 22:29:15 -080012332 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012333 break;
12334
12335 cursize <<= 1;
12336 }
12337
12338 tp->nvram_size = cursize;
12339}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012340
Linus Torvalds1da177e2005-04-16 15:20:36 -070012341static void __devinit tg3_get_nvram_size(struct tg3 *tp)
12342{
12343 u32 val;
12344
Joe Perches63c3a662011-04-26 08:12:10 +000012345 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080012346 return;
12347
12348 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080012349 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080012350 tg3_get_eeprom_size(tp);
12351 return;
12352 }
12353
Matt Carlson6d348f22009-02-25 14:25:52 +000012354 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012355 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000012356 /* This is confusing. We want to operate on the
12357 * 16-bit value at offset 0xf2. The tg3_nvram_read()
12358 * call will read from NVRAM and byteswap the data
12359 * according to the byteswapping settings for all
12360 * other register accesses. This ensures the data we
12361 * want will always reside in the lower 16-bits.
12362 * However, the data in NVRAM is in LE format, which
12363 * means the data from the NVRAM read will always be
12364 * opposite the endianness of the CPU. The 16-bit
12365 * byteswap then brings the data to CPU endianness.
12366 */
12367 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012368 return;
12369 }
12370 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070012371 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012372}
12373
12374static void __devinit tg3_get_nvram_info(struct tg3 *tp)
12375{
12376 u32 nvcfg1;
12377
12378 nvcfg1 = tr32(NVRAM_CFG1);
12379 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
Joe Perches63c3a662011-04-26 08:12:10 +000012380 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012381 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012382 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12383 tw32(NVRAM_CFG1, nvcfg1);
12384 }
12385
Matt Carlson6ff6f812011-05-19 12:12:54 +000012386 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
Joe Perches63c3a662011-04-26 08:12:10 +000012387 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012388 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012389 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
12390 tp->nvram_jedecnum = JEDEC_ATMEL;
12391 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012392 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012393 break;
12394 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
12395 tp->nvram_jedecnum = JEDEC_ATMEL;
12396 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
12397 break;
12398 case FLASH_VENDOR_ATMEL_EEPROM:
12399 tp->nvram_jedecnum = JEDEC_ATMEL;
12400 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012401 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012402 break;
12403 case FLASH_VENDOR_ST:
12404 tp->nvram_jedecnum = JEDEC_ST;
12405 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012406 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012407 break;
12408 case FLASH_VENDOR_SAIFUN:
12409 tp->nvram_jedecnum = JEDEC_SAIFUN;
12410 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
12411 break;
12412 case FLASH_VENDOR_SST_SMALL:
12413 case FLASH_VENDOR_SST_LARGE:
12414 tp->nvram_jedecnum = JEDEC_SST;
12415 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
12416 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012417 }
Matt Carlson8590a602009-08-28 12:29:16 +000012418 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012419 tp->nvram_jedecnum = JEDEC_ATMEL;
12420 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012421 tg3_flag_set(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012422 }
12423}
12424
Matt Carlsona1b950d2009-09-01 13:20:17 +000012425static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
12426{
12427 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
12428 case FLASH_5752PAGE_SIZE_256:
12429 tp->nvram_pagesize = 256;
12430 break;
12431 case FLASH_5752PAGE_SIZE_512:
12432 tp->nvram_pagesize = 512;
12433 break;
12434 case FLASH_5752PAGE_SIZE_1K:
12435 tp->nvram_pagesize = 1024;
12436 break;
12437 case FLASH_5752PAGE_SIZE_2K:
12438 tp->nvram_pagesize = 2048;
12439 break;
12440 case FLASH_5752PAGE_SIZE_4K:
12441 tp->nvram_pagesize = 4096;
12442 break;
12443 case FLASH_5752PAGE_SIZE_264:
12444 tp->nvram_pagesize = 264;
12445 break;
12446 case FLASH_5752PAGE_SIZE_528:
12447 tp->nvram_pagesize = 528;
12448 break;
12449 }
12450}
12451
Michael Chan361b4ac2005-04-21 17:11:21 -070012452static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
12453{
12454 u32 nvcfg1;
12455
12456 nvcfg1 = tr32(NVRAM_CFG1);
12457
Michael Chane6af3012005-04-21 17:12:05 -070012458 /* NVRAM protection for TPM */
12459 if (nvcfg1 & (1 << 27))
Joe Perches63c3a662011-04-26 08:12:10 +000012460 tg3_flag_set(tp, PROTECTED_NVRAM);
Michael Chane6af3012005-04-21 17:12:05 -070012461
Michael Chan361b4ac2005-04-21 17:11:21 -070012462 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012463 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
12464 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
12465 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012466 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012467 break;
12468 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12469 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012470 tg3_flag_set(tp, NVRAM_BUFFERED);
12471 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012472 break;
12473 case FLASH_5752VENDOR_ST_M45PE10:
12474 case FLASH_5752VENDOR_ST_M45PE20:
12475 case FLASH_5752VENDOR_ST_M45PE40:
12476 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012477 tg3_flag_set(tp, NVRAM_BUFFERED);
12478 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012479 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070012480 }
12481
Joe Perches63c3a662011-04-26 08:12:10 +000012482 if (tg3_flag(tp, FLASH)) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000012483 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000012484 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070012485 /* For eeprom, set pagesize to maximum eeprom size */
12486 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12487
12488 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12489 tw32(NVRAM_CFG1, nvcfg1);
12490 }
12491}
12492
Michael Chand3c7b882006-03-23 01:28:25 -080012493static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
12494{
Matt Carlson989a9d22007-05-05 11:51:05 -070012495 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080012496
12497 nvcfg1 = tr32(NVRAM_CFG1);
12498
12499 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070012500 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012501 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson989a9d22007-05-05 11:51:05 -070012502 protect = 1;
12503 }
Michael Chand3c7b882006-03-23 01:28:25 -080012504
Matt Carlson989a9d22007-05-05 11:51:05 -070012505 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
12506 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012507 case FLASH_5755VENDOR_ATMEL_FLASH_1:
12508 case FLASH_5755VENDOR_ATMEL_FLASH_2:
12509 case FLASH_5755VENDOR_ATMEL_FLASH_3:
12510 case FLASH_5755VENDOR_ATMEL_FLASH_5:
12511 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012512 tg3_flag_set(tp, NVRAM_BUFFERED);
12513 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012514 tp->nvram_pagesize = 264;
12515 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
12516 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
12517 tp->nvram_size = (protect ? 0x3e200 :
12518 TG3_NVRAM_SIZE_512KB);
12519 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
12520 tp->nvram_size = (protect ? 0x1f200 :
12521 TG3_NVRAM_SIZE_256KB);
12522 else
12523 tp->nvram_size = (protect ? 0x1f200 :
12524 TG3_NVRAM_SIZE_128KB);
12525 break;
12526 case FLASH_5752VENDOR_ST_M45PE10:
12527 case FLASH_5752VENDOR_ST_M45PE20:
12528 case FLASH_5752VENDOR_ST_M45PE40:
12529 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012530 tg3_flag_set(tp, NVRAM_BUFFERED);
12531 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012532 tp->nvram_pagesize = 256;
12533 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
12534 tp->nvram_size = (protect ?
12535 TG3_NVRAM_SIZE_64KB :
12536 TG3_NVRAM_SIZE_128KB);
12537 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
12538 tp->nvram_size = (protect ?
12539 TG3_NVRAM_SIZE_64KB :
12540 TG3_NVRAM_SIZE_256KB);
12541 else
12542 tp->nvram_size = (protect ?
12543 TG3_NVRAM_SIZE_128KB :
12544 TG3_NVRAM_SIZE_512KB);
12545 break;
Michael Chand3c7b882006-03-23 01:28:25 -080012546 }
12547}
12548
Michael Chan1b277772006-03-20 22:27:48 -080012549static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
12550{
12551 u32 nvcfg1;
12552
12553 nvcfg1 = tr32(NVRAM_CFG1);
12554
12555 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012556 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
12557 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
12558 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
12559 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
12560 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012561 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012562 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080012563
Matt Carlson8590a602009-08-28 12:29:16 +000012564 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12565 tw32(NVRAM_CFG1, nvcfg1);
12566 break;
12567 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12568 case FLASH_5755VENDOR_ATMEL_FLASH_1:
12569 case FLASH_5755VENDOR_ATMEL_FLASH_2:
12570 case FLASH_5755VENDOR_ATMEL_FLASH_3:
12571 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012572 tg3_flag_set(tp, NVRAM_BUFFERED);
12573 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012574 tp->nvram_pagesize = 264;
12575 break;
12576 case FLASH_5752VENDOR_ST_M45PE10:
12577 case FLASH_5752VENDOR_ST_M45PE20:
12578 case FLASH_5752VENDOR_ST_M45PE40:
12579 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012580 tg3_flag_set(tp, NVRAM_BUFFERED);
12581 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012582 tp->nvram_pagesize = 256;
12583 break;
Michael Chan1b277772006-03-20 22:27:48 -080012584 }
12585}
12586
Matt Carlson6b91fa02007-10-10 18:01:09 -070012587static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
12588{
12589 u32 nvcfg1, protect = 0;
12590
12591 nvcfg1 = tr32(NVRAM_CFG1);
12592
12593 /* NVRAM protection for TPM */
12594 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012595 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson6b91fa02007-10-10 18:01:09 -070012596 protect = 1;
12597 }
12598
12599 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
12600 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012601 case FLASH_5761VENDOR_ATMEL_ADB021D:
12602 case FLASH_5761VENDOR_ATMEL_ADB041D:
12603 case FLASH_5761VENDOR_ATMEL_ADB081D:
12604 case FLASH_5761VENDOR_ATMEL_ADB161D:
12605 case FLASH_5761VENDOR_ATMEL_MDB021D:
12606 case FLASH_5761VENDOR_ATMEL_MDB041D:
12607 case FLASH_5761VENDOR_ATMEL_MDB081D:
12608 case FLASH_5761VENDOR_ATMEL_MDB161D:
12609 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012610 tg3_flag_set(tp, NVRAM_BUFFERED);
12611 tg3_flag_set(tp, FLASH);
12612 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson8590a602009-08-28 12:29:16 +000012613 tp->nvram_pagesize = 256;
12614 break;
12615 case FLASH_5761VENDOR_ST_A_M45PE20:
12616 case FLASH_5761VENDOR_ST_A_M45PE40:
12617 case FLASH_5761VENDOR_ST_A_M45PE80:
12618 case FLASH_5761VENDOR_ST_A_M45PE16:
12619 case FLASH_5761VENDOR_ST_M_M45PE20:
12620 case FLASH_5761VENDOR_ST_M_M45PE40:
12621 case FLASH_5761VENDOR_ST_M_M45PE80:
12622 case FLASH_5761VENDOR_ST_M_M45PE16:
12623 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012624 tg3_flag_set(tp, NVRAM_BUFFERED);
12625 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012626 tp->nvram_pagesize = 256;
12627 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070012628 }
12629
12630 if (protect) {
12631 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
12632 } else {
12633 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012634 case FLASH_5761VENDOR_ATMEL_ADB161D:
12635 case FLASH_5761VENDOR_ATMEL_MDB161D:
12636 case FLASH_5761VENDOR_ST_A_M45PE16:
12637 case FLASH_5761VENDOR_ST_M_M45PE16:
12638 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
12639 break;
12640 case FLASH_5761VENDOR_ATMEL_ADB081D:
12641 case FLASH_5761VENDOR_ATMEL_MDB081D:
12642 case FLASH_5761VENDOR_ST_A_M45PE80:
12643 case FLASH_5761VENDOR_ST_M_M45PE80:
12644 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12645 break;
12646 case FLASH_5761VENDOR_ATMEL_ADB041D:
12647 case FLASH_5761VENDOR_ATMEL_MDB041D:
12648 case FLASH_5761VENDOR_ST_A_M45PE40:
12649 case FLASH_5761VENDOR_ST_M_M45PE40:
12650 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12651 break;
12652 case FLASH_5761VENDOR_ATMEL_ADB021D:
12653 case FLASH_5761VENDOR_ATMEL_MDB021D:
12654 case FLASH_5761VENDOR_ST_A_M45PE20:
12655 case FLASH_5761VENDOR_ST_M_M45PE20:
12656 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12657 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070012658 }
12659 }
12660}
12661
Michael Chanb5d37722006-09-27 16:06:21 -070012662static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
12663{
12664 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012665 tg3_flag_set(tp, NVRAM_BUFFERED);
Michael Chanb5d37722006-09-27 16:06:21 -070012666 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12667}
12668
Matt Carlson321d32a2008-11-21 17:22:19 -080012669static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
12670{
12671 u32 nvcfg1;
12672
12673 nvcfg1 = tr32(NVRAM_CFG1);
12674
12675 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12676 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
12677 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
12678 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012679 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson321d32a2008-11-21 17:22:19 -080012680 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12681
12682 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12683 tw32(NVRAM_CFG1, nvcfg1);
12684 return;
12685 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12686 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
12687 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
12688 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
12689 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
12690 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
12691 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
12692 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012693 tg3_flag_set(tp, NVRAM_BUFFERED);
12694 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080012695
12696 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12697 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12698 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
12699 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
12700 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12701 break;
12702 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
12703 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
12704 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12705 break;
12706 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
12707 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
12708 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12709 break;
12710 }
12711 break;
12712 case FLASH_5752VENDOR_ST_M45PE10:
12713 case FLASH_5752VENDOR_ST_M45PE20:
12714 case FLASH_5752VENDOR_ST_M45PE40:
12715 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012716 tg3_flag_set(tp, NVRAM_BUFFERED);
12717 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080012718
12719 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12720 case FLASH_5752VENDOR_ST_M45PE10:
12721 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12722 break;
12723 case FLASH_5752VENDOR_ST_M45PE20:
12724 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12725 break;
12726 case FLASH_5752VENDOR_ST_M45PE40:
12727 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12728 break;
12729 }
12730 break;
12731 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012732 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson321d32a2008-11-21 17:22:19 -080012733 return;
12734 }
12735
Matt Carlsona1b950d2009-09-01 13:20:17 +000012736 tg3_nvram_get_pagesize(tp, nvcfg1);
12737 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012738 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012739}
12740
12741
12742static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
12743{
12744 u32 nvcfg1;
12745
12746 nvcfg1 = tr32(NVRAM_CFG1);
12747
12748 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12749 case FLASH_5717VENDOR_ATMEL_EEPROM:
12750 case FLASH_5717VENDOR_MICRO_EEPROM:
12751 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012752 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012753 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12754
12755 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12756 tw32(NVRAM_CFG1, nvcfg1);
12757 return;
12758 case FLASH_5717VENDOR_ATMEL_MDB011D:
12759 case FLASH_5717VENDOR_ATMEL_ADB011B:
12760 case FLASH_5717VENDOR_ATMEL_ADB011D:
12761 case FLASH_5717VENDOR_ATMEL_MDB021D:
12762 case FLASH_5717VENDOR_ATMEL_ADB021B:
12763 case FLASH_5717VENDOR_ATMEL_ADB021D:
12764 case FLASH_5717VENDOR_ATMEL_45USPT:
12765 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012766 tg3_flag_set(tp, NVRAM_BUFFERED);
12767 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012768
12769 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12770 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012771 /* Detect size with tg3_nvram_get_size() */
12772 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012773 case FLASH_5717VENDOR_ATMEL_ADB021B:
12774 case FLASH_5717VENDOR_ATMEL_ADB021D:
12775 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12776 break;
12777 default:
12778 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12779 break;
12780 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012781 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012782 case FLASH_5717VENDOR_ST_M_M25PE10:
12783 case FLASH_5717VENDOR_ST_A_M25PE10:
12784 case FLASH_5717VENDOR_ST_M_M45PE10:
12785 case FLASH_5717VENDOR_ST_A_M45PE10:
12786 case FLASH_5717VENDOR_ST_M_M25PE20:
12787 case FLASH_5717VENDOR_ST_A_M25PE20:
12788 case FLASH_5717VENDOR_ST_M_M45PE20:
12789 case FLASH_5717VENDOR_ST_A_M45PE20:
12790 case FLASH_5717VENDOR_ST_25USPT:
12791 case FLASH_5717VENDOR_ST_45USPT:
12792 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012793 tg3_flag_set(tp, NVRAM_BUFFERED);
12794 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012795
12796 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12797 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012798 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012799 /* Detect size with tg3_nvram_get_size() */
12800 break;
12801 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012802 case FLASH_5717VENDOR_ST_A_M45PE20:
12803 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12804 break;
12805 default:
12806 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12807 break;
12808 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012809 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012810 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012811 tg3_flag_set(tp, NO_NVRAM);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012812 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080012813 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000012814
12815 tg3_nvram_get_pagesize(tp, nvcfg1);
12816 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012817 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson321d32a2008-11-21 17:22:19 -080012818}
12819
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012820static void __devinit tg3_get_5720_nvram_info(struct tg3 *tp)
12821{
12822 u32 nvcfg1, nvmpinstrp;
12823
12824 nvcfg1 = tr32(NVRAM_CFG1);
12825 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
12826
12827 switch (nvmpinstrp) {
12828 case FLASH_5720_EEPROM_HD:
12829 case FLASH_5720_EEPROM_LD:
12830 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012831 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012832
12833 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12834 tw32(NVRAM_CFG1, nvcfg1);
12835 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
12836 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12837 else
12838 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
12839 return;
12840 case FLASH_5720VENDOR_M_ATMEL_DB011D:
12841 case FLASH_5720VENDOR_A_ATMEL_DB011B:
12842 case FLASH_5720VENDOR_A_ATMEL_DB011D:
12843 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12844 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12845 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12846 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12847 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12848 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12849 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12850 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12851 case FLASH_5720VENDOR_ATMEL_45USPT:
12852 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012853 tg3_flag_set(tp, NVRAM_BUFFERED);
12854 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012855
12856 switch (nvmpinstrp) {
12857 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12858 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12859 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12860 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12861 break;
12862 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12863 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12864 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12865 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12866 break;
12867 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12868 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12869 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12870 break;
12871 default:
12872 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12873 break;
12874 }
12875 break;
12876 case FLASH_5720VENDOR_M_ST_M25PE10:
12877 case FLASH_5720VENDOR_M_ST_M45PE10:
12878 case FLASH_5720VENDOR_A_ST_M25PE10:
12879 case FLASH_5720VENDOR_A_ST_M45PE10:
12880 case FLASH_5720VENDOR_M_ST_M25PE20:
12881 case FLASH_5720VENDOR_M_ST_M45PE20:
12882 case FLASH_5720VENDOR_A_ST_M25PE20:
12883 case FLASH_5720VENDOR_A_ST_M45PE20:
12884 case FLASH_5720VENDOR_M_ST_M25PE40:
12885 case FLASH_5720VENDOR_M_ST_M45PE40:
12886 case FLASH_5720VENDOR_A_ST_M25PE40:
12887 case FLASH_5720VENDOR_A_ST_M45PE40:
12888 case FLASH_5720VENDOR_M_ST_M25PE80:
12889 case FLASH_5720VENDOR_M_ST_M45PE80:
12890 case FLASH_5720VENDOR_A_ST_M25PE80:
12891 case FLASH_5720VENDOR_A_ST_M45PE80:
12892 case FLASH_5720VENDOR_ST_25USPT:
12893 case FLASH_5720VENDOR_ST_45USPT:
12894 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012895 tg3_flag_set(tp, NVRAM_BUFFERED);
12896 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012897
12898 switch (nvmpinstrp) {
12899 case FLASH_5720VENDOR_M_ST_M25PE20:
12900 case FLASH_5720VENDOR_M_ST_M45PE20:
12901 case FLASH_5720VENDOR_A_ST_M25PE20:
12902 case FLASH_5720VENDOR_A_ST_M45PE20:
12903 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12904 break;
12905 case FLASH_5720VENDOR_M_ST_M25PE40:
12906 case FLASH_5720VENDOR_M_ST_M45PE40:
12907 case FLASH_5720VENDOR_A_ST_M25PE40:
12908 case FLASH_5720VENDOR_A_ST_M45PE40:
12909 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12910 break;
12911 case FLASH_5720VENDOR_M_ST_M25PE80:
12912 case FLASH_5720VENDOR_M_ST_M45PE80:
12913 case FLASH_5720VENDOR_A_ST_M25PE80:
12914 case FLASH_5720VENDOR_A_ST_M45PE80:
12915 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12916 break;
12917 default:
12918 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12919 break;
12920 }
12921 break;
12922 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012923 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012924 return;
12925 }
12926
12927 tg3_nvram_get_pagesize(tp, nvcfg1);
12928 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012929 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012930}
12931
Linus Torvalds1da177e2005-04-16 15:20:36 -070012932/* Chips other than 5700/5701 use the NVRAM for fetching info. */
12933static void __devinit tg3_nvram_init(struct tg3 *tp)
12934{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012935 tw32_f(GRC_EEPROM_ADDR,
12936 (EEPROM_ADDR_FSM_RESET |
12937 (EEPROM_DEFAULT_CLOCK_PERIOD <<
12938 EEPROM_ADDR_CLKPERD_SHIFT)));
12939
Michael Chan9d57f012006-12-07 00:23:25 -080012940 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012941
12942 /* Enable seeprom accesses. */
12943 tw32_f(GRC_LOCAL_CTRL,
12944 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
12945 udelay(100);
12946
12947 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
12948 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
Joe Perches63c3a662011-04-26 08:12:10 +000012949 tg3_flag_set(tp, NVRAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012950
Michael Chanec41c7d2006-01-17 02:40:55 -080012951 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000012952 netdev_warn(tp->dev,
12953 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000012954 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080012955 return;
12956 }
Michael Chane6af3012005-04-21 17:12:05 -070012957 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012958
Matt Carlson989a9d22007-05-05 11:51:05 -070012959 tp->nvram_size = 0;
12960
Michael Chan361b4ac2005-04-21 17:11:21 -070012961 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
12962 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080012963 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
12964 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070012965 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070012966 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
12967 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080012968 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070012969 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
12970 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070012971 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12972 tg3_get_5906_nvram_info(tp);
Matt Carlsonb703df62009-12-03 08:36:21 +000012973 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000012974 tg3_flag(tp, 57765_CLASS))
Matt Carlson321d32a2008-11-21 17:22:19 -080012975 tg3_get_57780_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012976 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
12977 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000012978 tg3_get_5717_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012979 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
12980 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070012981 else
12982 tg3_get_nvram_info(tp);
12983
Matt Carlson989a9d22007-05-05 11:51:05 -070012984 if (tp->nvram_size == 0)
12985 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012986
Michael Chane6af3012005-04-21 17:12:05 -070012987 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080012988 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012989
12990 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000012991 tg3_flag_clear(tp, NVRAM);
12992 tg3_flag_clear(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012993
12994 tg3_get_eeprom_size(tp);
12995 }
12996}
12997
Linus Torvalds1da177e2005-04-16 15:20:36 -070012998struct subsys_tbl_ent {
12999 u16 subsys_vendor, subsys_devid;
13000 u32 phy_id;
13001};
13002
Matt Carlson24daf2b2010-02-17 15:17:02 +000013003static struct subsys_tbl_ent subsys_id_to_phy_id[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013004 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013005 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013006 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013007 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013008 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013009 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013010 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013011 { TG3PCI_SUBVENDOR_ID_BROADCOM,
13012 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
13013 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013014 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013015 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013016 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013017 { TG3PCI_SUBVENDOR_ID_BROADCOM,
13018 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
13019 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013020 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013021 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013022 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013023 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013024 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013025 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013026 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013027
13028 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013029 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013030 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013031 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013032 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013033 { TG3PCI_SUBVENDOR_ID_3COM,
13034 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
13035 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013036 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013037 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013038 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013039
13040 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013041 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013042 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013043 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013044 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013045 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013046 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013047 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013048 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013049
13050 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013051 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013052 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013053 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013054 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013055 { TG3PCI_SUBVENDOR_ID_COMPAQ,
13056 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
13057 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013058 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013059 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013060 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013061
13062 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013063 { TG3PCI_SUBVENDOR_ID_IBM,
13064 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013065};
13066
Matt Carlson24daf2b2010-02-17 15:17:02 +000013067static struct subsys_tbl_ent * __devinit tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013068{
13069 int i;
13070
13071 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
13072 if ((subsys_id_to_phy_id[i].subsys_vendor ==
13073 tp->pdev->subsystem_vendor) &&
13074 (subsys_id_to_phy_id[i].subsys_devid ==
13075 tp->pdev->subsystem_device))
13076 return &subsys_id_to_phy_id[i];
13077 }
13078 return NULL;
13079}
13080
Michael Chan7d0c41e2005-04-21 17:06:20 -070013081static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013082{
Linus Torvalds1da177e2005-04-16 15:20:36 -070013083 u32 val;
David S. Millerf49639e2006-06-09 11:58:36 -070013084
Matt Carlson79eb6902010-02-17 15:17:03 +000013085 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070013086 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13087
Gary Zambranoa85feb82007-05-05 11:52:19 -070013088 /* Assume an onboard device and WOL capable by default. */
Joe Perches63c3a662011-04-26 08:12:10 +000013089 tg3_flag_set(tp, EEPROM_WRITE_PROT);
13090 tg3_flag_set(tp, WOL_CAP);
David S. Miller72b845e2006-03-14 14:11:48 -080013091
Michael Chanb5d37722006-09-27 16:06:21 -070013092 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080013093 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013094 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
13095 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080013096 }
Matt Carlson0527ba32007-10-10 18:03:30 -070013097 val = tr32(VCPU_CFGSHDW);
13098 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Joe Perches63c3a662011-04-26 08:12:10 +000013099 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson0527ba32007-10-10 18:03:30 -070013100 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013101 (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013102 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013103 device_set_wakeup_enable(&tp->pdev->dev, true);
13104 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080013105 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070013106 }
13107
Linus Torvalds1da177e2005-04-16 15:20:36 -070013108 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
13109 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
13110 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070013111 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070013112 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013113
13114 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
13115 tp->nic_sram_data_cfg = nic_cfg;
13116
13117 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
13118 ver >>= NIC_SRAM_DATA_VER_SHIFT;
Matt Carlson6ff6f812011-05-19 12:12:54 +000013119 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13120 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13121 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070013122 (ver > 0) && (ver < 0x100))
13123 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
13124
Matt Carlsona9daf362008-05-25 23:49:44 -070013125 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
13126 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
13127
Linus Torvalds1da177e2005-04-16 15:20:36 -070013128 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
13129 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
13130 eeprom_phy_serdes = 1;
13131
13132 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
13133 if (nic_phy_id != 0) {
13134 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
13135 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
13136
13137 eeprom_phy_id = (id1 >> 16) << 10;
13138 eeprom_phy_id |= (id2 & 0xfc00) << 16;
13139 eeprom_phy_id |= (id2 & 0x03ff) << 0;
13140 } else
13141 eeprom_phy_id = 0;
13142
Michael Chan7d0c41e2005-04-21 17:06:20 -070013143 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070013144 if (eeprom_phy_serdes) {
Joe Perches63c3a662011-04-26 08:12:10 +000013145 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013146 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000013147 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013148 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070013149 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070013150
Joe Perches63c3a662011-04-26 08:12:10 +000013151 if (tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013152 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
13153 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070013154 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070013155 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
13156
13157 switch (led_cfg) {
13158 default:
13159 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
13160 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13161 break;
13162
13163 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
13164 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
13165 break;
13166
13167 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
13168 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070013169
13170 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
13171 * read on some older 5700/5701 bootcode.
13172 */
13173 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
13174 ASIC_REV_5700 ||
13175 GET_ASIC_REV(tp->pci_chip_rev_id) ==
13176 ASIC_REV_5701)
13177 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13178
Linus Torvalds1da177e2005-04-16 15:20:36 -070013179 break;
13180
13181 case SHASTA_EXT_LED_SHARED:
13182 tp->led_ctrl = LED_CTRL_MODE_SHARED;
13183 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
13184 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
13185 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
13186 LED_CTRL_MODE_PHY_2);
13187 break;
13188
13189 case SHASTA_EXT_LED_MAC:
13190 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
13191 break;
13192
13193 case SHASTA_EXT_LED_COMBO:
13194 tp->led_ctrl = LED_CTRL_MODE_COMBO;
13195 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
13196 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
13197 LED_CTRL_MODE_PHY_2);
13198 break;
13199
Stephen Hemminger855e1112008-04-16 16:37:28 -070013200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013201
13202 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13203 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
13204 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
13205 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
13206
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013207 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
13208 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080013209
Michael Chan9d26e212006-12-07 00:21:14 -080013210 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Joe Perches63c3a662011-04-26 08:12:10 +000013211 tg3_flag_set(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080013212 if ((tp->pdev->subsystem_vendor ==
13213 PCI_VENDOR_ID_ARIMA) &&
13214 (tp->pdev->subsystem_device == 0x205a ||
13215 tp->pdev->subsystem_device == 0x2063))
Joe Perches63c3a662011-04-26 08:12:10 +000013216 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080013217 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000013218 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
13219 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080013220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013221
13222 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +000013223 tg3_flag_set(tp, ENABLE_ASF);
13224 if (tg3_flag(tp, 5750_PLUS))
13225 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013226 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080013227
13228 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013229 tg3_flag(tp, 5750_PLUS))
13230 tg3_flag_set(tp, ENABLE_APE);
Matt Carlsonb2b98d42008-11-03 16:52:32 -080013231
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013232 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070013233 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
Joe Perches63c3a662011-04-26 08:12:10 +000013234 tg3_flag_clear(tp, WOL_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013235
Joe Perches63c3a662011-04-26 08:12:10 +000013236 if (tg3_flag(tp, WOL_CAP) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013237 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013238 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013239 device_set_wakeup_enable(&tp->pdev->dev, true);
13240 }
Matt Carlson0527ba32007-10-10 18:03:30 -070013241
Linus Torvalds1da177e2005-04-16 15:20:36 -070013242 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013243 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013244
13245 /* serdes signal pre-emphasis in register 0x590 set by */
13246 /* bootcode if bit 18 is set */
13247 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013248 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070013249
Joe Perches63c3a662011-04-26 08:12:10 +000013250 if ((tg3_flag(tp, 57765_PLUS) ||
13251 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
13252 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080013253 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013254 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080013255
Joe Perches63c3a662011-04-26 08:12:10 +000013256 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson8c69b1e2010-08-02 11:26:00 +000013257 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +000013258 !tg3_flag(tp, 57765_PLUS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070013259 u32 cfg3;
13260
13261 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
13262 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
Joe Perches63c3a662011-04-26 08:12:10 +000013263 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson8ed5d972007-05-07 00:25:49 -070013264 }
Matt Carlsona9daf362008-05-25 23:49:44 -070013265
Matt Carlson14417062010-02-17 15:16:59 +000013266 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
Joe Perches63c3a662011-04-26 08:12:10 +000013267 tg3_flag_set(tp, RGMII_INBAND_DISABLE);
Matt Carlsona9daf362008-05-25 23:49:44 -070013268 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000013269 tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
Matt Carlsona9daf362008-05-25 23:49:44 -070013270 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000013271 tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013272 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080013273done:
Joe Perches63c3a662011-04-26 08:12:10 +000013274 if (tg3_flag(tp, WOL_CAP))
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000013275 device_set_wakeup_enable(&tp->pdev->dev,
Joe Perches63c3a662011-04-26 08:12:10 +000013276 tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000013277 else
13278 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070013279}
13280
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013281static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
13282{
13283 int i;
13284 u32 val;
13285
13286 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
13287 tw32(OTP_CTRL, cmd);
13288
13289 /* Wait for up to 1 ms for command to execute. */
13290 for (i = 0; i < 100; i++) {
13291 val = tr32(OTP_STATUS);
13292 if (val & OTP_STATUS_CMD_DONE)
13293 break;
13294 udelay(10);
13295 }
13296
13297 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
13298}
13299
13300/* Read the gphy configuration from the OTP region of the chip. The gphy
13301 * configuration is a 32-bit value that straddles the alignment boundary.
13302 * We do two 32-bit reads and then shift and merge the results.
13303 */
13304static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
13305{
13306 u32 bhalf_otp, thalf_otp;
13307
13308 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
13309
13310 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
13311 return 0;
13312
13313 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
13314
13315 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
13316 return 0;
13317
13318 thalf_otp = tr32(OTP_READ_DATA);
13319
13320 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
13321
13322 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
13323 return 0;
13324
13325 bhalf_otp = tr32(OTP_READ_DATA);
13326
13327 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
13328}
13329
Matt Carlsone256f8a2011-03-09 16:58:24 +000013330static void __devinit tg3_phy_init_link_config(struct tg3 *tp)
13331{
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +000013332 u32 adv = ADVERTISED_Autoneg;
Matt Carlsone256f8a2011-03-09 16:58:24 +000013333
13334 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
13335 adv |= ADVERTISED_1000baseT_Half |
13336 ADVERTISED_1000baseT_Full;
13337
13338 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
13339 adv |= ADVERTISED_100baseT_Half |
13340 ADVERTISED_100baseT_Full |
13341 ADVERTISED_10baseT_Half |
13342 ADVERTISED_10baseT_Full |
13343 ADVERTISED_TP;
13344 else
13345 adv |= ADVERTISED_FIBRE;
13346
13347 tp->link_config.advertising = adv;
Matt Carlsone7405222012-02-13 15:20:16 +000013348 tp->link_config.speed = SPEED_UNKNOWN;
13349 tp->link_config.duplex = DUPLEX_UNKNOWN;
Matt Carlsone256f8a2011-03-09 16:58:24 +000013350 tp->link_config.autoneg = AUTONEG_ENABLE;
Matt Carlsone7405222012-02-13 15:20:16 +000013351 tp->link_config.active_speed = SPEED_UNKNOWN;
13352 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Matt Carlson34655ad2012-02-22 12:35:18 +000013353
13354 tp->old_link = -1;
Matt Carlsone256f8a2011-03-09 16:58:24 +000013355}
13356
Michael Chan7d0c41e2005-04-21 17:06:20 -070013357static int __devinit tg3_phy_probe(struct tg3 *tp)
13358{
13359 u32 hw_phy_id_1, hw_phy_id_2;
13360 u32 hw_phy_id, hw_phy_id_masked;
13361 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013362
Matt Carlsone256f8a2011-03-09 16:58:24 +000013363 /* flow control autonegotiation is default behavior */
Joe Perches63c3a662011-04-26 08:12:10 +000013364 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsone256f8a2011-03-09 16:58:24 +000013365 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
13366
Joe Perches63c3a662011-04-26 08:12:10 +000013367 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013368 return tg3_phy_init(tp);
13369
Linus Torvalds1da177e2005-04-16 15:20:36 -070013370 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010013371 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013372 */
13373 err = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000013374 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000013375 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013376 } else {
13377 /* Now read the physical PHY_ID from the chip and verify
13378 * that it is sane. If it doesn't look good, we fall back
13379 * to either the hard-coded table based PHY_ID and failing
13380 * that the value found in the eeprom area.
13381 */
13382 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
13383 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
13384
13385 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
13386 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
13387 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
13388
Matt Carlson79eb6902010-02-17 15:17:03 +000013389 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013390 }
13391
Matt Carlson79eb6902010-02-17 15:17:03 +000013392 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013393 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000013394 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013395 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070013396 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013397 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013398 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000013399 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070013400 /* Do nothing, phy ID already set up in
13401 * tg3_get_eeprom_hw_cfg().
13402 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070013403 } else {
13404 struct subsys_tbl_ent *p;
13405
13406 /* No eeprom signature? Try the hardcoded
13407 * subsys device table.
13408 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013409 p = tg3_lookup_by_subsys(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013410 if (!p)
13411 return -ENODEV;
13412
13413 tp->phy_id = p->phy_id;
13414 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000013415 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013416 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013417 }
13418 }
13419
Matt Carlsona6b68da2010-12-06 08:28:52 +000013420 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Matt Carlson5baa5e92011-07-20 10:20:53 +000013421 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13422 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720 ||
13423 (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +000013424 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0) ||
13425 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
13426 tp->pci_chip_rev_id != CHIPREV_ID_57765_A0)))
Matt Carlson52b02d02010-10-14 10:37:41 +000013427 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
13428
Matt Carlsone256f8a2011-03-09 16:58:24 +000013429 tg3_phy_init_link_config(tp);
13430
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013431 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013432 !tg3_flag(tp, ENABLE_APE) &&
13433 !tg3_flag(tp, ENABLE_ASF)) {
Matt Carlsone2bf73e2011-12-08 14:40:15 +000013434 u32 bmsr, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013435
13436 tg3_readphy(tp, MII_BMSR, &bmsr);
13437 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
13438 (bmsr & BMSR_LSTATUS))
13439 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013440
Linus Torvalds1da177e2005-04-16 15:20:36 -070013441 err = tg3_phy_reset(tp);
13442 if (err)
13443 return err;
13444
Matt Carlson42b64a42011-05-19 12:12:49 +000013445 tg3_phy_set_wirespeed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013446
Matt Carlsone2bf73e2011-12-08 14:40:15 +000013447 if (!tg3_phy_copper_an_config_ok(tp, &dummy)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000013448 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
13449 tp->link_config.flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013450
13451 tg3_writephy(tp, MII_BMCR,
13452 BMCR_ANENABLE | BMCR_ANRESTART);
13453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013454 }
13455
13456skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000013457 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013458 err = tg3_init_5401phy_dsp(tp);
13459 if (err)
13460 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013461
Linus Torvalds1da177e2005-04-16 15:20:36 -070013462 err = tg3_init_5401phy_dsp(tp);
13463 }
13464
Linus Torvalds1da177e2005-04-16 15:20:36 -070013465 return err;
13466}
13467
Matt Carlson184b8902010-04-05 10:19:25 +000013468static void __devinit tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013469{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013470 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013471 unsigned int block_end, rosize, len;
Matt Carlson535a4902011-07-20 10:20:56 +000013472 u32 vpdlen;
Matt Carlson184b8902010-04-05 10:19:25 +000013473 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013474
Matt Carlson535a4902011-07-20 10:20:56 +000013475 vpd_data = (u8 *)tg3_vpd_readblock(tp, &vpdlen);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013476 if (!vpd_data)
13477 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013478
Matt Carlson535a4902011-07-20 10:20:56 +000013479 i = pci_vpd_find_tag(vpd_data, 0, vpdlen, PCI_VPD_LRDT_RO_DATA);
Matt Carlson4181b2c2010-02-26 14:04:45 +000013480 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013481 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013482
13483 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
13484 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
13485 i += PCI_VPD_LRDT_TAG_SIZE;
13486
Matt Carlson535a4902011-07-20 10:20:56 +000013487 if (block_end > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000013488 goto out_not_found;
13489
Matt Carlson184b8902010-04-05 10:19:25 +000013490 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13491 PCI_VPD_RO_KEYWORD_MFR_ID);
13492 if (j > 0) {
13493 len = pci_vpd_info_field_size(&vpd_data[j]);
13494
13495 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13496 if (j + len > block_end || len != 4 ||
13497 memcmp(&vpd_data[j], "1028", 4))
13498 goto partno;
13499
13500 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13501 PCI_VPD_RO_KEYWORD_VENDOR0);
13502 if (j < 0)
13503 goto partno;
13504
13505 len = pci_vpd_info_field_size(&vpd_data[j]);
13506
13507 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13508 if (j + len > block_end)
13509 goto partno;
13510
13511 memcpy(tp->fw_ver, &vpd_data[j], len);
Matt Carlson535a4902011-07-20 10:20:56 +000013512 strncat(tp->fw_ver, " bc ", vpdlen - len - 1);
Matt Carlson184b8902010-04-05 10:19:25 +000013513 }
13514
13515partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000013516 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13517 PCI_VPD_RO_KEYWORD_PARTNO);
13518 if (i < 0)
13519 goto out_not_found;
13520
13521 len = pci_vpd_info_field_size(&vpd_data[i]);
13522
13523 i += PCI_VPD_INFO_FLD_HDR_SIZE;
13524 if (len > TG3_BPN_SIZE ||
Matt Carlson535a4902011-07-20 10:20:56 +000013525 (len + i) > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000013526 goto out_not_found;
13527
13528 memcpy(tp->board_part_number, &vpd_data[i], len);
13529
Linus Torvalds1da177e2005-04-16 15:20:36 -070013530out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013531 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000013532 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013533 return;
13534
13535out_no_vpd:
Matt Carlson37a949c2010-09-30 10:34:33 +000013536 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
13537 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717)
13538 strcpy(tp->board_part_number, "BCM5717");
13539 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
13540 strcpy(tp->board_part_number, "BCM5718");
13541 else
13542 goto nomatch;
13543 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
13544 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
13545 strcpy(tp->board_part_number, "BCM57780");
13546 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
13547 strcpy(tp->board_part_number, "BCM57760");
13548 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
13549 strcpy(tp->board_part_number, "BCM57790");
13550 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
13551 strcpy(tp->board_part_number, "BCM57788");
13552 else
13553 goto nomatch;
13554 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
13555 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
13556 strcpy(tp->board_part_number, "BCM57761");
13557 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
13558 strcpy(tp->board_part_number, "BCM57765");
13559 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
13560 strcpy(tp->board_part_number, "BCM57781");
13561 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
13562 strcpy(tp->board_part_number, "BCM57785");
13563 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
13564 strcpy(tp->board_part_number, "BCM57791");
13565 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13566 strcpy(tp->board_part_number, "BCM57795");
13567 else
13568 goto nomatch;
Matt Carlson55086ad2011-12-14 11:09:59 +000013569 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57766) {
13570 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762)
13571 strcpy(tp->board_part_number, "BCM57762");
13572 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766)
13573 strcpy(tp->board_part_number, "BCM57766");
13574 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782)
13575 strcpy(tp->board_part_number, "BCM57782");
13576 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
13577 strcpy(tp->board_part_number, "BCM57786");
13578 else
13579 goto nomatch;
Matt Carlson37a949c2010-09-30 10:34:33 +000013580 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070013581 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000013582 } else {
13583nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070013584 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000013585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013586}
13587
Matt Carlson9c8a6202007-10-21 16:16:08 -070013588static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
13589{
13590 u32 val;
13591
Matt Carlsone4f34112009-02-25 14:25:00 +000013592 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013593 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013594 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013595 val != 0)
13596 return 0;
13597
13598 return 1;
13599}
13600
Matt Carlsonacd9c112009-02-25 14:26:33 +000013601static void __devinit tg3_read_bc_ver(struct tg3 *tp)
13602{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013603 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000013604 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013605 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013606
13607 if (tg3_nvram_read(tp, 0xc, &offset) ||
13608 tg3_nvram_read(tp, 0x4, &start))
13609 return;
13610
13611 offset = tg3_nvram_logical_addr(tp, offset);
13612
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013613 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013614 return;
13615
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013616 if ((val & 0xfc000000) == 0x0c000000) {
13617 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013618 return;
13619
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013620 if (val == 0)
13621 newver = true;
13622 }
13623
Matt Carlson75f99362010-04-05 10:19:24 +000013624 dst_off = strlen(tp->fw_ver);
13625
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013626 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000013627 if (TG3_VER_SIZE - dst_off < 16 ||
13628 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013629 return;
13630
13631 offset = offset + ver_offset - start;
13632 for (i = 0; i < 16; i += 4) {
13633 __be32 v;
13634 if (tg3_nvram_read_be32(tp, offset + i, &v))
13635 return;
13636
Matt Carlson75f99362010-04-05 10:19:24 +000013637 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013638 }
13639 } else {
13640 u32 major, minor;
13641
13642 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
13643 return;
13644
13645 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
13646 TG3_NVM_BCVER_MAJSFT;
13647 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000013648 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
13649 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013650 }
13651}
13652
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013653static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
13654{
13655 u32 val, major, minor;
13656
13657 /* Use native endian representation */
13658 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
13659 return;
13660
13661 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
13662 TG3_NVM_HWSB_CFG1_MAJSFT;
13663 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
13664 TG3_NVM_HWSB_CFG1_MINSFT;
13665
13666 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
13667}
13668
Matt Carlsondfe00d72008-11-21 17:19:41 -080013669static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
13670{
13671 u32 offset, major, minor, build;
13672
Matt Carlson75f99362010-04-05 10:19:24 +000013673 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013674
13675 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
13676 return;
13677
13678 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
13679 case TG3_EEPROM_SB_REVISION_0:
13680 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
13681 break;
13682 case TG3_EEPROM_SB_REVISION_2:
13683 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
13684 break;
13685 case TG3_EEPROM_SB_REVISION_3:
13686 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
13687 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000013688 case TG3_EEPROM_SB_REVISION_4:
13689 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
13690 break;
13691 case TG3_EEPROM_SB_REVISION_5:
13692 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
13693 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000013694 case TG3_EEPROM_SB_REVISION_6:
13695 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
13696 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013697 default:
13698 return;
13699 }
13700
Matt Carlsone4f34112009-02-25 14:25:00 +000013701 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080013702 return;
13703
13704 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
13705 TG3_EEPROM_SB_EDH_BLD_SHFT;
13706 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
13707 TG3_EEPROM_SB_EDH_MAJ_SHFT;
13708 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
13709
13710 if (minor > 99 || build > 26)
13711 return;
13712
Matt Carlson75f99362010-04-05 10:19:24 +000013713 offset = strlen(tp->fw_ver);
13714 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
13715 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013716
13717 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000013718 offset = strlen(tp->fw_ver);
13719 if (offset < TG3_VER_SIZE - 1)
13720 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013721 }
13722}
13723
Matt Carlsonacd9c112009-02-25 14:26:33 +000013724static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080013725{
13726 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013727 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070013728
13729 for (offset = TG3_NVM_DIR_START;
13730 offset < TG3_NVM_DIR_END;
13731 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000013732 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013733 return;
13734
13735 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
13736 break;
13737 }
13738
13739 if (offset == TG3_NVM_DIR_END)
13740 return;
13741
Joe Perches63c3a662011-04-26 08:12:10 +000013742 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013743 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000013744 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013745 return;
13746
Matt Carlsone4f34112009-02-25 14:25:00 +000013747 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013748 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013749 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013750 return;
13751
13752 offset += val - start;
13753
Matt Carlsonacd9c112009-02-25 14:26:33 +000013754 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013755
Matt Carlsonacd9c112009-02-25 14:26:33 +000013756 tp->fw_ver[vlen++] = ',';
13757 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070013758
13759 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000013760 __be32 v;
13761 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013762 return;
13763
Al Virob9fc7dc2007-12-17 22:59:57 -080013764 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013765
Matt Carlsonacd9c112009-02-25 14:26:33 +000013766 if (vlen > TG3_VER_SIZE - sizeof(v)) {
13767 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013768 break;
13769 }
13770
Matt Carlsonacd9c112009-02-25 14:26:33 +000013771 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
13772 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013773 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000013774}
13775
Matt Carlson7fd76442009-02-25 14:27:20 +000013776static void __devinit tg3_read_dash_ver(struct tg3 *tp)
13777{
13778 int vlen;
13779 u32 apedata;
Matt Carlsonecc79642010-08-02 11:26:01 +000013780 char *fwtype;
Matt Carlson7fd76442009-02-25 14:27:20 +000013781
Joe Perches63c3a662011-04-26 08:12:10 +000013782 if (!tg3_flag(tp, ENABLE_APE) || !tg3_flag(tp, ENABLE_ASF))
Matt Carlson7fd76442009-02-25 14:27:20 +000013783 return;
13784
13785 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
13786 if (apedata != APE_SEG_SIG_MAGIC)
13787 return;
13788
13789 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
13790 if (!(apedata & APE_FW_STATUS_READY))
13791 return;
13792
13793 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
13794
Matt Carlsondc6d0742010-09-15 08:59:55 +000013795 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI) {
Joe Perches63c3a662011-04-26 08:12:10 +000013796 tg3_flag_set(tp, APE_HAS_NCSI);
Matt Carlsonecc79642010-08-02 11:26:01 +000013797 fwtype = "NCSI";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013798 } else {
Matt Carlsonecc79642010-08-02 11:26:01 +000013799 fwtype = "DASH";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013800 }
Matt Carlsonecc79642010-08-02 11:26:01 +000013801
Matt Carlson7fd76442009-02-25 14:27:20 +000013802 vlen = strlen(tp->fw_ver);
13803
Matt Carlsonecc79642010-08-02 11:26:01 +000013804 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
13805 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000013806 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
13807 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
13808 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
13809 (apedata & APE_FW_VERSION_BLDMSK));
13810}
13811
Matt Carlsonacd9c112009-02-25 14:26:33 +000013812static void __devinit tg3_read_fw_ver(struct tg3 *tp)
13813{
13814 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000013815 bool vpd_vers = false;
13816
13817 if (tp->fw_ver[0] != 0)
13818 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013819
Joe Perches63c3a662011-04-26 08:12:10 +000013820 if (tg3_flag(tp, NO_NVRAM)) {
Matt Carlson75f99362010-04-05 10:19:24 +000013821 strcat(tp->fw_ver, "sb");
Matt Carlsondf259d82009-04-20 06:57:14 +000013822 return;
13823 }
13824
Matt Carlsonacd9c112009-02-25 14:26:33 +000013825 if (tg3_nvram_read(tp, 0, &val))
13826 return;
13827
13828 if (val == TG3_EEPROM_MAGIC)
13829 tg3_read_bc_ver(tp);
13830 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
13831 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013832 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
13833 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013834 else
13835 return;
13836
Matt Carlsonc9cab242011-07-13 09:27:27 +000013837 if (vpd_vers)
Matt Carlson75f99362010-04-05 10:19:24 +000013838 goto done;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013839
Matt Carlsonc9cab242011-07-13 09:27:27 +000013840 if (tg3_flag(tp, ENABLE_APE)) {
13841 if (tg3_flag(tp, ENABLE_ASF))
13842 tg3_read_dash_ver(tp);
13843 } else if (tg3_flag(tp, ENABLE_ASF)) {
13844 tg3_read_mgmtfw_ver(tp);
13845 }
Matt Carlson9c8a6202007-10-21 16:16:08 -070013846
Matt Carlson75f99362010-04-05 10:19:24 +000013847done:
Matt Carlson9c8a6202007-10-21 16:16:08 -070013848 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080013849}
13850
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013851static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
13852{
Joe Perches63c3a662011-04-26 08:12:10 +000013853 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013854 return TG3_RX_RET_MAX_SIZE_5717;
Joe Perches63c3a662011-04-26 08:12:10 +000013855 else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013856 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013857 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000013858 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013859}
13860
Matt Carlson41434702011-03-09 16:58:22 +000013861static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080013862 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
13863 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
13864 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
13865 { },
13866};
13867
Matt Carlson16c7fa72012-02-13 10:20:10 +000013868static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
13869{
13870 struct pci_dev *peer;
13871 unsigned int func, devnr = tp->pdev->devfn & ~7;
13872
13873 for (func = 0; func < 8; func++) {
13874 peer = pci_get_slot(tp->pdev->bus, devnr | func);
13875 if (peer && peer != tp->pdev)
13876 break;
13877 pci_dev_put(peer);
13878 }
13879 /* 5704 can be configured in single-port mode, set peer to
13880 * tp->pdev in that case.
13881 */
13882 if (!peer) {
13883 peer = tp->pdev;
13884 return peer;
13885 }
13886
13887 /*
13888 * We don't need to keep the refcount elevated; there's no way
13889 * to remove one half of this device without removing the other
13890 */
13891 pci_dev_put(peer);
13892
13893 return peer;
13894}
13895
Matt Carlson42b123b2012-02-13 15:20:13 +000013896static void __devinit tg3_detect_asic_rev(struct tg3 *tp, u32 misc_ctrl_reg)
13897{
13898 tp->pci_chip_rev_id = misc_ctrl_reg >> MISC_HOST_CTRL_CHIPREV_SHIFT;
13899 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
13900 u32 reg;
13901
13902 /* All devices that use the alternate
13903 * ASIC REV location have a CPMU.
13904 */
13905 tg3_flag_set(tp, CPMU_PRESENT);
13906
13907 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
13908 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
13909 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
13910 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720)
13911 reg = TG3PCI_GEN2_PRODID_ASICREV;
13912 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
13913 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
13914 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
13915 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
13916 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
13917 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
13918 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762 ||
13919 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766 ||
13920 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782 ||
13921 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
13922 reg = TG3PCI_GEN15_PRODID_ASICREV;
13923 else
13924 reg = TG3PCI_PRODID_ASICREV;
13925
13926 pci_read_config_dword(tp->pdev, reg, &tp->pci_chip_rev_id);
13927 }
13928
13929 /* Wrong chip ID in 5752 A0. This code can be removed later
13930 * as A0 is not in production.
13931 */
13932 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
13933 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
13934
13935 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13936 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13937 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
13938 tg3_flag_set(tp, 5717_PLUS);
13939
13940 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 ||
13941 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57766)
13942 tg3_flag_set(tp, 57765_CLASS);
13943
13944 if (tg3_flag(tp, 57765_CLASS) || tg3_flag(tp, 5717_PLUS))
13945 tg3_flag_set(tp, 57765_PLUS);
13946
13947 /* Intentionally exclude ASIC_REV_5906 */
13948 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
13949 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
13950 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
13951 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
13952 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
13953 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
13954 tg3_flag(tp, 57765_PLUS))
13955 tg3_flag_set(tp, 5755_PLUS);
13956
13957 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
13958 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)
13959 tg3_flag_set(tp, 5780_CLASS);
13960
13961 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13962 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
13963 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
13964 tg3_flag(tp, 5755_PLUS) ||
13965 tg3_flag(tp, 5780_CLASS))
13966 tg3_flag_set(tp, 5750_PLUS);
13967
13968 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
13969 tg3_flag(tp, 5750_PLUS))
13970 tg3_flag_set(tp, 5705_PLUS);
13971}
13972
Linus Torvalds1da177e2005-04-16 15:20:36 -070013973static int __devinit tg3_get_invariants(struct tg3 *tp)
13974{
Linus Torvalds1da177e2005-04-16 15:20:36 -070013975 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013976 u32 pci_state_reg, grc_misc_cfg;
13977 u32 val;
13978 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013979 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013980
Linus Torvalds1da177e2005-04-16 15:20:36 -070013981 /* Force memory write invalidate off. If we leave it on,
13982 * then on 5700_BX chips we have to enable a workaround.
13983 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
13984 * to match the cacheline size. The Broadcom driver have this
13985 * workaround but turns MWI off all the times so never uses
13986 * it. This seems to suggest that the workaround is insufficient.
13987 */
13988 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13989 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
13990 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13991
Matt Carlson16821282011-07-13 09:27:28 +000013992 /* Important! -- Make sure register accesses are byteswapped
13993 * correctly. Also, for those chips that require it, make
13994 * sure that indirect register accesses are enabled before
13995 * the first operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013996 */
13997 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13998 &misc_ctrl_reg);
Matt Carlson16821282011-07-13 09:27:28 +000013999 tp->misc_host_ctrl |= (misc_ctrl_reg &
14000 MISC_HOST_CTRL_CHIPREV);
14001 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
14002 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014003
Matt Carlson42b123b2012-02-13 15:20:13 +000014004 tg3_detect_asic_rev(tp, misc_ctrl_reg);
Michael Chanff645be2005-04-21 17:09:53 -070014005
Michael Chan68929142005-08-09 20:17:14 -070014006 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
14007 * we need to disable memory and use config. cycles
14008 * only to access all registers. The 5702/03 chips
14009 * can mistakenly decode the special cycles from the
14010 * ICH chipsets as memory write cycles, causing corruption
14011 * of register and memory space. Only certain ICH bridges
14012 * will drive special cycles with non-zero data during the
14013 * address phase which can fall within the 5703's address
14014 * range. This is not an ICH bug as the PCI spec allows
14015 * non-zero address during special cycles. However, only
14016 * these ICH bridges are known to drive non-zero addresses
14017 * during special cycles.
14018 *
14019 * Since special cycles do not cross PCI bridges, we only
14020 * enable this workaround if the 5703 is on the secondary
14021 * bus of these ICH bridges.
14022 */
14023 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
14024 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
14025 static struct tg3_dev_id {
14026 u32 vendor;
14027 u32 device;
14028 u32 rev;
14029 } ich_chipsets[] = {
14030 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
14031 PCI_ANY_ID },
14032 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
14033 PCI_ANY_ID },
14034 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
14035 0xa },
14036 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
14037 PCI_ANY_ID },
14038 { },
14039 };
14040 struct tg3_dev_id *pci_id = &ich_chipsets[0];
14041 struct pci_dev *bridge = NULL;
14042
14043 while (pci_id->vendor != 0) {
14044 bridge = pci_get_device(pci_id->vendor, pci_id->device,
14045 bridge);
14046 if (!bridge) {
14047 pci_id++;
14048 continue;
14049 }
14050 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070014051 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070014052 continue;
14053 }
14054 if (bridge->subordinate &&
14055 (bridge->subordinate->number ==
14056 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014057 tg3_flag_set(tp, ICH_WORKAROUND);
Michael Chan68929142005-08-09 20:17:14 -070014058 pci_dev_put(bridge);
14059 break;
14060 }
14061 }
14062 }
14063
Matt Carlson6ff6f812011-05-19 12:12:54 +000014064 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Matt Carlson41588ba2008-04-19 18:12:33 -070014065 static struct tg3_dev_id {
14066 u32 vendor;
14067 u32 device;
14068 } bridge_chipsets[] = {
14069 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
14070 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
14071 { },
14072 };
14073 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
14074 struct pci_dev *bridge = NULL;
14075
14076 while (pci_id->vendor != 0) {
14077 bridge = pci_get_device(pci_id->vendor,
14078 pci_id->device,
14079 bridge);
14080 if (!bridge) {
14081 pci_id++;
14082 continue;
14083 }
14084 if (bridge->subordinate &&
14085 (bridge->subordinate->number <=
14086 tp->pdev->bus->number) &&
14087 (bridge->subordinate->subordinate >=
14088 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014089 tg3_flag_set(tp, 5701_DMA_BUG);
Matt Carlson41588ba2008-04-19 18:12:33 -070014090 pci_dev_put(bridge);
14091 break;
14092 }
14093 }
14094 }
14095
Michael Chan4a29cc22006-03-19 13:21:12 -080014096 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
14097 * DMA addresses > 40-bit. This bridge may have other additional
14098 * 57xx devices behind it in some 4-port NIC designs for example.
14099 * Any tg3 device found behind the bridge will also need the 40-bit
14100 * DMA workaround.
14101 */
Matt Carlson42b123b2012-02-13 15:20:13 +000014102 if (tg3_flag(tp, 5780_CLASS)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014103 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4cf78e42005-07-25 12:29:19 -070014104 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a588792010-04-05 10:19:28 +000014105 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080014106 struct pci_dev *bridge = NULL;
14107
14108 do {
14109 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
14110 PCI_DEVICE_ID_SERVERWORKS_EPB,
14111 bridge);
14112 if (bridge && bridge->subordinate &&
14113 (bridge->subordinate->number <=
14114 tp->pdev->bus->number) &&
14115 (bridge->subordinate->subordinate >=
14116 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014117 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4a29cc22006-03-19 13:21:12 -080014118 pci_dev_put(bridge);
14119 break;
14120 }
14121 } while (bridge);
14122 }
Michael Chan4cf78e42005-07-25 12:29:19 -070014123
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014124 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Matt Carlson3a1e19d2011-07-13 09:27:32 +000014125 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)
Michael Chan7544b092007-05-05 13:08:32 -070014126 tp->pdev_peer = tg3_find_peer(tp);
14127
Matt Carlson507399f2009-11-13 13:03:37 +000014128 /* Determine TSO capabilities */
Matt Carlsona0512942011-07-27 14:20:54 +000014129 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0)
Matt Carlson4d163b72011-01-25 15:58:48 +000014130 ; /* Do nothing. HW bug. */
Joe Perches63c3a662011-04-26 08:12:10 +000014131 else if (tg3_flag(tp, 57765_PLUS))
14132 tg3_flag_set(tp, HW_TSO_3);
14133 else if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000014134 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000014135 tg3_flag_set(tp, HW_TSO_2);
14136 else if (tg3_flag(tp, 5750_PLUS)) {
14137 tg3_flag_set(tp, HW_TSO_1);
14138 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000014139 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
14140 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
Joe Perches63c3a662011-04-26 08:12:10 +000014141 tg3_flag_clear(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000014142 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14143 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
14144 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000014145 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000014146 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
14147 tp->fw_needed = FIRMWARE_TG3TSO5;
14148 else
14149 tp->fw_needed = FIRMWARE_TG3TSO;
14150 }
14151
Matt Carlsondabc5c62011-05-19 12:12:52 +000014152 /* Selectively allow TSO based on operating conditions */
Matt Carlson6ff6f812011-05-19 12:12:54 +000014153 if (tg3_flag(tp, HW_TSO_1) ||
14154 tg3_flag(tp, HW_TSO_2) ||
14155 tg3_flag(tp, HW_TSO_3) ||
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000014156 tp->fw_needed) {
14157 /* For firmware TSO, assume ASF is disabled.
14158 * We'll disable TSO later if we discover ASF
14159 * is enabled in tg3_get_eeprom_hw_cfg().
14160 */
Matt Carlsondabc5c62011-05-19 12:12:52 +000014161 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000014162 } else {
Matt Carlsondabc5c62011-05-19 12:12:52 +000014163 tg3_flag_clear(tp, TSO_CAPABLE);
14164 tg3_flag_clear(tp, TSO_BUG);
14165 tp->fw_needed = NULL;
14166 }
14167
14168 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
14169 tp->fw_needed = FIRMWARE_TG3;
14170
Matt Carlson507399f2009-11-13 13:03:37 +000014171 tp->irq_max = 1;
14172
Joe Perches63c3a662011-04-26 08:12:10 +000014173 if (tg3_flag(tp, 5750_PLUS)) {
14174 tg3_flag_set(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070014175 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
14176 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
14177 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
14178 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
14179 tp->pdev_peer == tp->pdev))
Joe Perches63c3a662011-04-26 08:12:10 +000014180 tg3_flag_clear(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070014181
Joe Perches63c3a662011-04-26 08:12:10 +000014182 if (tg3_flag(tp, 5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070014183 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000014184 tg3_flag_set(tp, 1SHOT_MSI);
Michael Chan52c0fd82006-06-29 20:15:54 -070014185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014186
Joe Perches63c3a662011-04-26 08:12:10 +000014187 if (tg3_flag(tp, 57765_PLUS)) {
14188 tg3_flag_set(tp, SUPPORT_MSIX);
Matt Carlson507399f2009-11-13 13:03:37 +000014189 tp->irq_max = TG3_IRQ_MAX_VECS;
Matt Carlson90415472011-12-16 13:33:23 +000014190 tg3_rss_init_dflt_indir_tbl(tp);
Matt Carlson507399f2009-11-13 13:03:37 +000014191 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014192 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000014193
Matt Carlson2ffcc982011-05-19 12:12:44 +000014194 if (tg3_flag(tp, 5755_PLUS))
Joe Perches63c3a662011-04-26 08:12:10 +000014195 tg3_flag_set(tp, SHORT_DMA_BUG);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014196
Matt Carlsone31aa982011-07-27 14:20:53 +000014197 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona4cb4282011-12-14 11:09:58 +000014198 tp->dma_limit = TG3_TX_BD_DMA_MAX_4K;
Matt Carlsone31aa982011-07-27 14:20:53 +000014199
Matt Carlsonfa6b2aa2011-11-21 15:01:19 +000014200 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14201 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
14202 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Joe Perches63c3a662011-04-26 08:12:10 +000014203 tg3_flag_set(tp, LRG_PROD_RING_CAP);
Matt Carlsonde9f5232011-04-05 14:22:43 +000014204
Joe Perches63c3a662011-04-26 08:12:10 +000014205 if (tg3_flag(tp, 57765_PLUS) &&
Matt Carlsona0512942011-07-27 14:20:54 +000014206 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0)
Joe Perches63c3a662011-04-26 08:12:10 +000014207 tg3_flag_set(tp, USE_JUMBO_BDFLAG);
Matt Carlsonb703df62009-12-03 08:36:21 +000014208
Joe Perches63c3a662011-04-26 08:12:10 +000014209 if (!tg3_flag(tp, 5705_PLUS) ||
14210 tg3_flag(tp, 5780_CLASS) ||
14211 tg3_flag(tp, USE_JUMBO_BDFLAG))
14212 tg3_flag_set(tp, JUMBO_CAPABLE);
Michael Chan0f893dc2005-07-25 12:30:38 -070014213
Matt Carlson52f44902008-11-21 17:17:04 -080014214 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14215 &pci_state_reg);
14216
Jon Mason708ebb3a2011-06-27 12:56:50 +000014217 if (pci_is_pcie(tp->pdev)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014218 u16 lnkctl;
14219
Joe Perches63c3a662011-04-26 08:12:10 +000014220 tg3_flag_set(tp, PCI_EXPRESS);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080014221
Matt Carlson2c55a3d2011-11-28 09:41:04 +000014222 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0) {
14223 int readrq = pcie_get_readrq(tp->pdev);
14224 if (readrq > 2048)
14225 pcie_set_readrq(tp->pdev, 2048);
14226 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -080014227
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014228 pci_read_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +000014229 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014230 &lnkctl);
14231 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
Matt Carlson7196cd62011-05-19 16:02:44 +000014232 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
14233 ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000014234 tg3_flag_clear(tp, HW_TSO_2);
Matt Carlsondabc5c62011-05-19 12:12:52 +000014235 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson7196cd62011-05-19 16:02:44 +000014236 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014237 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014238 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000014239 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
14240 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Joe Perches63c3a662011-04-26 08:12:10 +000014241 tg3_flag_set(tp, CLKREQ_BUG);
Matt Carlson614b0592010-01-20 16:58:02 +000014242 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000014243 tg3_flag_set(tp, L1PLLPD_EN);
Michael Chanc7835a72006-11-15 21:14:42 -080014244 }
Matt Carlson52f44902008-11-21 17:17:04 -080014245 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Jon Mason708ebb3a2011-06-27 12:56:50 +000014246 /* BCM5785 devices are effectively PCIe devices, and should
14247 * follow PCIe codepaths, but do not have a PCIe capabilities
14248 * section.
Matt Carlson93a700a2011-08-31 11:44:54 +000014249 */
Joe Perches63c3a662011-04-26 08:12:10 +000014250 tg3_flag_set(tp, PCI_EXPRESS);
14251 } else if (!tg3_flag(tp, 5705_PLUS) ||
14252 tg3_flag(tp, 5780_CLASS)) {
Matt Carlson52f44902008-11-21 17:17:04 -080014253 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
14254 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000014255 dev_err(&tp->pdev->dev,
14256 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080014257 return -EIO;
14258 }
14259
14260 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
Joe Perches63c3a662011-04-26 08:12:10 +000014261 tg3_flag_set(tp, PCIX_MODE);
Matt Carlson52f44902008-11-21 17:17:04 -080014262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014263
Michael Chan399de502005-10-03 14:02:39 -070014264 /* If we have an AMD 762 or VIA K8T800 chipset, write
14265 * reordering to the mailbox registers done by the host
14266 * controller can cause major troubles. We read back from
14267 * every mailbox register write to force the writes to be
14268 * posted to the chip in order.
14269 */
Matt Carlson41434702011-03-09 16:58:22 +000014270 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Joe Perches63c3a662011-04-26 08:12:10 +000014271 !tg3_flag(tp, PCI_EXPRESS))
14272 tg3_flag_set(tp, MBOX_WRITE_REORDER);
Michael Chan399de502005-10-03 14:02:39 -070014273
Matt Carlson69fc4052008-12-21 20:19:57 -080014274 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
14275 &tp->pci_cacheline_sz);
14276 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
14277 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014278 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14279 tp->pci_lat_timer < 64) {
14280 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080014281 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
14282 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014283 }
14284
Matt Carlson16821282011-07-13 09:27:28 +000014285 /* Important! -- It is critical that the PCI-X hw workaround
14286 * situation is decided before the first MMIO register access.
14287 */
Matt Carlson52f44902008-11-21 17:17:04 -080014288 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
14289 /* 5700 BX chips need to have their TX producer index
14290 * mailboxes written twice to workaround a bug.
14291 */
Joe Perches63c3a662011-04-26 08:12:10 +000014292 tg3_flag_set(tp, TXD_MBOX_HWBUG);
Matt Carlson9974a352007-10-07 23:27:28 -070014293
Matt Carlson52f44902008-11-21 17:17:04 -080014294 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014295 *
14296 * The workaround is to use indirect register accesses
14297 * for all chip writes not to mailbox registers.
14298 */
Joe Perches63c3a662011-04-26 08:12:10 +000014299 if (tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014300 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014301
Joe Perches63c3a662011-04-26 08:12:10 +000014302 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014303
14304 /* The chip can have it's power management PCI config
14305 * space registers clobbered due to this bug.
14306 * So explicitly force the chip into D0 here.
14307 */
Matt Carlson9974a352007-10-07 23:27:28 -070014308 pci_read_config_dword(tp->pdev,
14309 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014310 &pm_reg);
14311 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
14312 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070014313 pci_write_config_dword(tp->pdev,
14314 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014315 pm_reg);
14316
14317 /* Also, force SERR#/PERR# in PCI command. */
14318 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
14319 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
14320 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
14321 }
14322 }
14323
Linus Torvalds1da177e2005-04-16 15:20:36 -070014324 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000014325 tg3_flag_set(tp, PCI_HIGH_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014326 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000014327 tg3_flag_set(tp, PCI_32BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014328
14329 /* Chip-specific fixup from Broadcom driver */
14330 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
14331 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
14332 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
14333 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
14334 }
14335
Michael Chan1ee582d2005-08-09 20:16:46 -070014336 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070014337 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070014338 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070014339 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070014340 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070014341 tp->write32_tx_mbox = tg3_write32;
14342 tp->write32_rx_mbox = tg3_write32;
14343
14344 /* Various workaround register access methods */
Joe Perches63c3a662011-04-26 08:12:10 +000014345 if (tg3_flag(tp, PCIX_TARGET_HWBUG))
Michael Chan1ee582d2005-08-09 20:16:46 -070014346 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070014347 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014348 (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson98efd8a2007-05-05 12:47:25 -070014349 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
14350 /*
14351 * Back to back register writes can cause problems on these
14352 * chips, the workaround is to read back all reg writes
14353 * except those to mailbox regs.
14354 *
14355 * See tg3_write_indirect_reg32().
14356 */
Michael Chan1ee582d2005-08-09 20:16:46 -070014357 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070014358 }
14359
Joe Perches63c3a662011-04-26 08:12:10 +000014360 if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
Michael Chan1ee582d2005-08-09 20:16:46 -070014361 tp->write32_tx_mbox = tg3_write32_tx_mbox;
Joe Perches63c3a662011-04-26 08:12:10 +000014362 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Michael Chan1ee582d2005-08-09 20:16:46 -070014363 tp->write32_rx_mbox = tg3_write_flush_reg32;
14364 }
Michael Chan20094932005-08-09 20:16:32 -070014365
Joe Perches63c3a662011-04-26 08:12:10 +000014366 if (tg3_flag(tp, ICH_WORKAROUND)) {
Michael Chan68929142005-08-09 20:17:14 -070014367 tp->read32 = tg3_read_indirect_reg32;
14368 tp->write32 = tg3_write_indirect_reg32;
14369 tp->read32_mbox = tg3_read_indirect_mbox;
14370 tp->write32_mbox = tg3_write_indirect_mbox;
14371 tp->write32_tx_mbox = tg3_write_indirect_mbox;
14372 tp->write32_rx_mbox = tg3_write_indirect_mbox;
14373
14374 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070014375 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070014376
14377 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
14378 pci_cmd &= ~PCI_COMMAND_MEMORY;
14379 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
14380 }
Michael Chanb5d37722006-09-27 16:06:21 -070014381 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
14382 tp->read32_mbox = tg3_read32_mbox_5906;
14383 tp->write32_mbox = tg3_write32_mbox_5906;
14384 tp->write32_tx_mbox = tg3_write32_mbox_5906;
14385 tp->write32_rx_mbox = tg3_write32_mbox_5906;
14386 }
Michael Chan68929142005-08-09 20:17:14 -070014387
Michael Chanbbadf502006-04-06 21:46:34 -070014388 if (tp->write32 == tg3_write_indirect_reg32 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014389 (tg3_flag(tp, PCIX_MODE) &&
Michael Chanbbadf502006-04-06 21:46:34 -070014390 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070014391 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Joe Perches63c3a662011-04-26 08:12:10 +000014392 tg3_flag_set(tp, SRAM_USE_CONFIG);
Michael Chanbbadf502006-04-06 21:46:34 -070014393
Matt Carlson16821282011-07-13 09:27:28 +000014394 /* The memory arbiter has to be enabled in order for SRAM accesses
14395 * to succeed. Normally on powerup the tg3 chip firmware will make
14396 * sure it is enabled, but other entities such as system netboot
14397 * code might disable it.
14398 */
14399 val = tr32(MEMARB_MODE);
14400 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
14401
Matt Carlson9dc5e342011-11-04 09:15:02 +000014402 tp->pci_fn = PCI_FUNC(tp->pdev->devfn) & 3;
14403 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
14404 tg3_flag(tp, 5780_CLASS)) {
14405 if (tg3_flag(tp, PCIX_MODE)) {
14406 pci_read_config_dword(tp->pdev,
14407 tp->pcix_cap + PCI_X_STATUS,
14408 &val);
14409 tp->pci_fn = val & 0x7;
14410 }
14411 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
14412 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
14413 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) ==
14414 NIC_SRAM_CPMUSTAT_SIG) {
14415 tp->pci_fn = val & TG3_CPMU_STATUS_FMSK_5717;
14416 tp->pci_fn = tp->pci_fn ? 1 : 0;
14417 }
14418 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
14419 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
14420 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
14421 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) ==
14422 NIC_SRAM_CPMUSTAT_SIG) {
14423 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5719) >>
14424 TG3_CPMU_STATUS_FSHFT_5719;
14425 }
Matt Carlson69f11c92011-07-13 09:27:30 +000014426 }
14427
Michael Chan7d0c41e2005-04-21 17:06:20 -070014428 /* Get eeprom hw config before calling tg3_set_power_state().
Joe Perches63c3a662011-04-26 08:12:10 +000014429 * In particular, the TG3_FLAG_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070014430 * determined before calling tg3_set_power_state() so that
14431 * we know whether or not to switch out of Vaux power.
14432 * When the flag is set, it means that GPIO1 is used for eeprom
14433 * write protect and also implies that it is a LOM where GPIOs
14434 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040014435 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070014436 tg3_get_eeprom_hw_cfg(tp);
14437
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000014438 if (tp->fw_needed && tg3_flag(tp, ENABLE_ASF)) {
14439 tg3_flag_clear(tp, TSO_CAPABLE);
14440 tg3_flag_clear(tp, TSO_BUG);
14441 tp->fw_needed = NULL;
14442 }
14443
Joe Perches63c3a662011-04-26 08:12:10 +000014444 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070014445 /* Allow reads and writes to the
14446 * APE register and memory space.
14447 */
14448 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +000014449 PCISTATE_ALLOW_APE_SHMEM_WR |
14450 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070014451 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
14452 pci_state_reg);
Matt Carlsonc9cab242011-07-13 09:27:27 +000014453
14454 tg3_ape_lock_init(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070014455 }
14456
Matt Carlson16821282011-07-13 09:27:28 +000014457 /* Set up tp->grc_local_ctrl before calling
14458 * tg3_pwrsrc_switch_to_vmain(). GPIO1 driven high
14459 * will bring 5700's external PHY out of reset.
Michael Chan314fba32005-04-21 17:07:04 -070014460 * It is also used as eeprom write protect on LOMs.
14461 */
14462 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
Matt Carlson6ff6f812011-05-19 12:12:54 +000014463 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014464 tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan314fba32005-04-21 17:07:04 -070014465 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
14466 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070014467 /* Unused GPIO3 must be driven as output on 5752 because there
14468 * are no pull-up resistors on unused GPIO pins.
14469 */
14470 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
14471 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070014472
Matt Carlson321d32a2008-11-21 17:22:19 -080014473 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsoncb4ed1f2010-01-20 16:58:09 +000014474 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000014475 tg3_flag(tp, 57765_CLASS))
Michael Chanaf36e6b2006-03-23 01:28:06 -080014476 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
14477
Matt Carlson8d519ab2009-04-20 06:58:01 +000014478 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
14479 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070014480 /* Turn off the debug UART. */
14481 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014482 if (tg3_flag(tp, IS_NIC))
Matt Carlson5f0c4a32008-06-09 15:41:12 -070014483 /* Keep VMain power. */
14484 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
14485 GRC_LCLCTRL_GPIO_OUTPUT0;
14486 }
14487
Matt Carlson16821282011-07-13 09:27:28 +000014488 /* Switch out of Vaux if it is a NIC */
14489 tg3_pwrsrc_switch_to_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014490
Linus Torvalds1da177e2005-04-16 15:20:36 -070014491 /* Derive initial jumbo mode from MTU assigned in
14492 * ether_setup() via the alloc_etherdev() call
14493 */
Joe Perches63c3a662011-04-26 08:12:10 +000014494 if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
14495 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014496
14497 /* Determine WakeOnLan speed to use. */
14498 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14499 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
14500 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
14501 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
Joe Perches63c3a662011-04-26 08:12:10 +000014502 tg3_flag_clear(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014503 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000014504 tg3_flag_set(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014505 }
14506
Matt Carlson7f97a4b2009-08-25 10:10:03 +000014507 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014508 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000014509
Linus Torvalds1da177e2005-04-16 15:20:36 -070014510 /* A few boards don't want Ethernet@WireSpeed phy feature */
Matt Carlson6ff6f812011-05-19 12:12:54 +000014511 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14512 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070014513 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070014514 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014515 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
14516 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
14517 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014518
14519 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
14520 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014521 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014522 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014523 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014524
Joe Perches63c3a662011-04-26 08:12:10 +000014525 if (tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014526 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080014527 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014528 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014529 !tg3_flag(tp, 57765_PLUS)) {
Michael Chanc424cb22006-04-29 18:56:34 -070014530 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070014531 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070014532 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
14533 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080014534 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
14535 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014536 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080014537 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014538 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080014539 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014540 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070014541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014542
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014543 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
14544 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
14545 tp->phy_otp = tg3_read_otp_phycfg(tp);
14546 if (tp->phy_otp == 0)
14547 tp->phy_otp = TG3_OTP_DEFAULT;
14548 }
14549
Joe Perches63c3a662011-04-26 08:12:10 +000014550 if (tg3_flag(tp, CPMU_PRESENT))
Matt Carlson8ef21422008-05-02 16:47:53 -070014551 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
14552 else
14553 tp->mi_mode = MAC_MI_MODE_BASE;
14554
Linus Torvalds1da177e2005-04-16 15:20:36 -070014555 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014556 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
14557 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
14558 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
14559
Matt Carlson4d958472011-04-20 07:57:35 +000014560 /* Set these bits to enable statistics workaround. */
14561 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14562 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
14563 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0) {
14564 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
14565 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
14566 }
14567
Matt Carlson321d32a2008-11-21 17:22:19 -080014568 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
14569 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Joe Perches63c3a662011-04-26 08:12:10 +000014570 tg3_flag_set(tp, USE_PHYLIB);
Matt Carlson57e69832008-05-25 23:48:31 -070014571
Matt Carlson158d7ab2008-05-29 01:37:54 -070014572 err = tg3_mdio_init(tp);
14573 if (err)
14574 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014575
14576 /* Initialize data/descriptor byte/word swapping. */
14577 val = tr32(GRC_MODE);
Matt Carlsonf2096f92011-04-05 14:22:48 +000014578 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
14579 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
14580 GRC_MODE_WORD_SWAP_B2HRX_DATA |
14581 GRC_MODE_B2HRX_ENABLE |
14582 GRC_MODE_HTX2B_ENABLE |
14583 GRC_MODE_HOST_STACKUP);
14584 else
14585 val &= GRC_MODE_HOST_STACKUP;
14586
Linus Torvalds1da177e2005-04-16 15:20:36 -070014587 tw32(GRC_MODE, val | tp->grc_mode);
14588
14589 tg3_switch_clocks(tp);
14590
14591 /* Clear this out for sanity. */
14592 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
14593
14594 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14595 &pci_state_reg);
14596 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014597 !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014598 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
14599
14600 if (chiprevid == CHIPREV_ID_5701_A0 ||
14601 chiprevid == CHIPREV_ID_5701_B0 ||
14602 chiprevid == CHIPREV_ID_5701_B2 ||
14603 chiprevid == CHIPREV_ID_5701_B5) {
14604 void __iomem *sram_base;
14605
14606 /* Write some dummy words into the SRAM status block
14607 * area, see if it reads back correctly. If the return
14608 * value is bad, force enable the PCIX workaround.
14609 */
14610 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
14611
14612 writel(0x00000000, sram_base);
14613 writel(0x00000000, sram_base + 4);
14614 writel(0xffffffff, sram_base + 4);
14615 if (readl(sram_base) != 0x00000000)
Joe Perches63c3a662011-04-26 08:12:10 +000014616 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014617 }
14618 }
14619
14620 udelay(50);
14621 tg3_nvram_init(tp);
14622
14623 grc_misc_cfg = tr32(GRC_MISC_CFG);
14624 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
14625
Linus Torvalds1da177e2005-04-16 15:20:36 -070014626 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14627 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
14628 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
Joe Perches63c3a662011-04-26 08:12:10 +000014629 tg3_flag_set(tp, IS_5788);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014630
Joe Perches63c3a662011-04-26 08:12:10 +000014631 if (!tg3_flag(tp, IS_5788) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +000014632 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014633 tg3_flag_set(tp, TAGGED_STATUS);
14634 if (tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070014635 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
14636 HOSTCC_MODE_CLRTICK_TXBD);
14637
14638 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
14639 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
14640 tp->misc_host_ctrl);
14641 }
14642
Matt Carlson3bda1252008-08-15 14:08:22 -070014643 /* Preserve the APE MAC_MODE bits */
Joe Perches63c3a662011-04-26 08:12:10 +000014644 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +000014645 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070014646 else
Matt Carlson6e01b202011-08-19 13:58:20 +000014647 tp->mac_mode = 0;
Matt Carlson3bda1252008-08-15 14:08:22 -070014648
Linus Torvalds1da177e2005-04-16 15:20:36 -070014649 /* these are limited to 10/100 only */
14650 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14651 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
14652 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14653 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14654 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
14655 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
14656 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
14657 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14658 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
Michael Chan676917d2006-12-07 00:20:22 -080014659 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
14660 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014661 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
Matt Carlsond1101142010-02-17 15:16:55 +000014662 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
14663 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014664 (tp->phy_flags & TG3_PHYFLG_IS_FET))
14665 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014666
14667 err = tg3_phy_probe(tp);
14668 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014669 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014670 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014671 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014672 }
14673
Matt Carlson184b8902010-04-05 10:19:25 +000014674 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080014675 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014676
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014677 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
14678 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014679 } else {
14680 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014681 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014682 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014683 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014684 }
14685
14686 /* 5700 {AX,BX} chips have a broken status block link
14687 * change bit implementation, so we must use the
14688 * status register in those cases.
14689 */
14690 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014691 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014692 else
Joe Perches63c3a662011-04-26 08:12:10 +000014693 tg3_flag_clear(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014694
14695 /* The led_ctrl is set during tg3_phy_probe, here we might
14696 * have to force the link status polling mechanism based
14697 * upon subsystem IDs.
14698 */
14699 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070014700 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014701 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
14702 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Joe Perches63c3a662011-04-26 08:12:10 +000014703 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014704 }
14705
14706 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014707 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Joe Perches63c3a662011-04-26 08:12:10 +000014708 tg3_flag_set(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014709 else
Joe Perches63c3a662011-04-26 08:12:10 +000014710 tg3_flag_clear(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014711
Eric Dumazet9205fd92011-11-18 06:47:01 +000014712 tp->rx_offset = NET_SKB_PAD + NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014713 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014714 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014715 tg3_flag(tp, PCIX_MODE)) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000014716 tp->rx_offset = NET_SKB_PAD;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014717#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000014718 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014719#endif
14720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014721
Matt Carlson2c49a442010-09-30 10:34:35 +000014722 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
14723 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000014724 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
14725
Matt Carlson2c49a442010-09-30 10:34:35 +000014726 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070014727
14728 /* Increment the rx prod index on the rx std ring by at most
14729 * 8 for these chips to workaround hw errata.
14730 */
14731 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
14732 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
14733 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
14734 tp->rx_std_max_post = 8;
14735
Joe Perches63c3a662011-04-26 08:12:10 +000014736 if (tg3_flag(tp, ASPM_WORKAROUND))
Matt Carlson8ed5d972007-05-07 00:25:49 -070014737 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
14738 PCIE_PWR_MGMT_L1_THRESH_MSK;
14739
Linus Torvalds1da177e2005-04-16 15:20:36 -070014740 return err;
14741}
14742
David S. Miller49b6e95f2007-03-29 01:38:42 -070014743#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014744static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
14745{
14746 struct net_device *dev = tp->dev;
14747 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014748 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070014749 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014750 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014751
David S. Miller49b6e95f2007-03-29 01:38:42 -070014752 addr = of_get_property(dp, "local-mac-address", &len);
14753 if (addr && len == 6) {
14754 memcpy(dev->dev_addr, addr, 6);
14755 memcpy(dev->perm_addr, dev->dev_addr, 6);
14756 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014757 }
14758 return -ENODEV;
14759}
14760
14761static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
14762{
14763 struct net_device *dev = tp->dev;
14764
14765 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070014766 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014767 return 0;
14768}
14769#endif
14770
14771static int __devinit tg3_get_device_address(struct tg3 *tp)
14772{
14773 struct net_device *dev = tp->dev;
14774 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080014775 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014776
David S. Miller49b6e95f2007-03-29 01:38:42 -070014777#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014778 if (!tg3_get_macaddr_sparc(tp))
14779 return 0;
14780#endif
14781
14782 mac_offset = 0x7c;
Matt Carlson6ff6f812011-05-19 12:12:54 +000014783 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014784 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014785 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
14786 mac_offset = 0xcc;
14787 if (tg3_nvram_lock(tp))
14788 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
14789 else
14790 tg3_nvram_unlock(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000014791 } else if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson69f11c92011-07-13 09:27:30 +000014792 if (tp->pci_fn & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014793 mac_offset = 0xcc;
Matt Carlson69f11c92011-07-13 09:27:30 +000014794 if (tp->pci_fn > 1)
Matt Carlsona50d0792010-06-05 17:24:37 +000014795 mac_offset += 0x18c;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014796 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070014797 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014798
14799 /* First try to get it from MAC address mailbox. */
14800 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
14801 if ((hi >> 16) == 0x484b) {
14802 dev->dev_addr[0] = (hi >> 8) & 0xff;
14803 dev->dev_addr[1] = (hi >> 0) & 0xff;
14804
14805 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
14806 dev->dev_addr[2] = (lo >> 24) & 0xff;
14807 dev->dev_addr[3] = (lo >> 16) & 0xff;
14808 dev->dev_addr[4] = (lo >> 8) & 0xff;
14809 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014810
Michael Chan008652b2006-03-27 23:14:53 -080014811 /* Some old bootcode may report a 0 MAC address in SRAM */
14812 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
14813 }
14814 if (!addr_ok) {
14815 /* Next, try NVRAM. */
Joe Perches63c3a662011-04-26 08:12:10 +000014816 if (!tg3_flag(tp, NO_NVRAM) &&
Matt Carlsondf259d82009-04-20 06:57:14 +000014817 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000014818 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070014819 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
14820 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080014821 }
14822 /* Finally just fetch it out of the MAC control regs. */
14823 else {
14824 hi = tr32(MAC_ADDR_0_HIGH);
14825 lo = tr32(MAC_ADDR_0_LOW);
14826
14827 dev->dev_addr[5] = lo & 0xff;
14828 dev->dev_addr[4] = (lo >> 8) & 0xff;
14829 dev->dev_addr[3] = (lo >> 16) & 0xff;
14830 dev->dev_addr[2] = (lo >> 24) & 0xff;
14831 dev->dev_addr[1] = hi & 0xff;
14832 dev->dev_addr[0] = (hi >> 8) & 0xff;
14833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014834 }
14835
14836 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070014837#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014838 if (!tg3_get_default_macaddr_sparc(tp))
14839 return 0;
14840#endif
14841 return -EINVAL;
14842 }
John W. Linville2ff43692005-09-12 14:44:20 -070014843 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014844 return 0;
14845}
14846
David S. Miller59e6b432005-05-18 22:50:10 -070014847#define BOUNDARY_SINGLE_CACHELINE 1
14848#define BOUNDARY_MULTI_CACHELINE 2
14849
14850static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
14851{
14852 int cacheline_size;
14853 u8 byte;
14854 int goal;
14855
14856 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
14857 if (byte == 0)
14858 cacheline_size = 1024;
14859 else
14860 cacheline_size = (int) byte * 4;
14861
14862 /* On 5703 and later chips, the boundary bits have no
14863 * effect.
14864 */
14865 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14866 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014867 !tg3_flag(tp, PCI_EXPRESS))
David S. Miller59e6b432005-05-18 22:50:10 -070014868 goto out;
14869
14870#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
14871 goal = BOUNDARY_MULTI_CACHELINE;
14872#else
14873#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
14874 goal = BOUNDARY_SINGLE_CACHELINE;
14875#else
14876 goal = 0;
14877#endif
14878#endif
14879
Joe Perches63c3a662011-04-26 08:12:10 +000014880 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014881 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
14882 goto out;
14883 }
14884
David S. Miller59e6b432005-05-18 22:50:10 -070014885 if (!goal)
14886 goto out;
14887
14888 /* PCI controllers on most RISC systems tend to disconnect
14889 * when a device tries to burst across a cache-line boundary.
14890 * Therefore, letting tg3 do so just wastes PCI bandwidth.
14891 *
14892 * Unfortunately, for PCI-E there are only limited
14893 * write-side controls for this, and thus for reads
14894 * we will still get the disconnects. We'll also waste
14895 * these PCI cycles for both read and write for chips
14896 * other than 5700 and 5701 which do not implement the
14897 * boundary bits.
14898 */
Joe Perches63c3a662011-04-26 08:12:10 +000014899 if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014900 switch (cacheline_size) {
14901 case 16:
14902 case 32:
14903 case 64:
14904 case 128:
14905 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14906 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
14907 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
14908 } else {
14909 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14910 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14911 }
14912 break;
14913
14914 case 256:
14915 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
14916 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
14917 break;
14918
14919 default:
14920 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14921 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14922 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014923 }
Joe Perches63c3a662011-04-26 08:12:10 +000014924 } else if (tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014925 switch (cacheline_size) {
14926 case 16:
14927 case 32:
14928 case 64:
14929 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14930 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14931 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
14932 break;
14933 }
14934 /* fallthrough */
14935 case 128:
14936 default:
14937 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14938 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
14939 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014940 }
David S. Miller59e6b432005-05-18 22:50:10 -070014941 } else {
14942 switch (cacheline_size) {
14943 case 16:
14944 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14945 val |= (DMA_RWCTRL_READ_BNDRY_16 |
14946 DMA_RWCTRL_WRITE_BNDRY_16);
14947 break;
14948 }
14949 /* fallthrough */
14950 case 32:
14951 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14952 val |= (DMA_RWCTRL_READ_BNDRY_32 |
14953 DMA_RWCTRL_WRITE_BNDRY_32);
14954 break;
14955 }
14956 /* fallthrough */
14957 case 64:
14958 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14959 val |= (DMA_RWCTRL_READ_BNDRY_64 |
14960 DMA_RWCTRL_WRITE_BNDRY_64);
14961 break;
14962 }
14963 /* fallthrough */
14964 case 128:
14965 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14966 val |= (DMA_RWCTRL_READ_BNDRY_128 |
14967 DMA_RWCTRL_WRITE_BNDRY_128);
14968 break;
14969 }
14970 /* fallthrough */
14971 case 256:
14972 val |= (DMA_RWCTRL_READ_BNDRY_256 |
14973 DMA_RWCTRL_WRITE_BNDRY_256);
14974 break;
14975 case 512:
14976 val |= (DMA_RWCTRL_READ_BNDRY_512 |
14977 DMA_RWCTRL_WRITE_BNDRY_512);
14978 break;
14979 case 1024:
14980 default:
14981 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
14982 DMA_RWCTRL_WRITE_BNDRY_1024);
14983 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014984 }
David S. Miller59e6b432005-05-18 22:50:10 -070014985 }
14986
14987out:
14988 return val;
14989}
14990
Linus Torvalds1da177e2005-04-16 15:20:36 -070014991static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
14992{
14993 struct tg3_internal_buffer_desc test_desc;
14994 u32 sram_dma_descs;
14995 int i, ret;
14996
14997 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
14998
14999 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
15000 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
15001 tw32(RDMAC_STATUS, 0);
15002 tw32(WDMAC_STATUS, 0);
15003
15004 tw32(BUFMGR_MODE, 0);
15005 tw32(FTQ_RESET, 0);
15006
15007 test_desc.addr_hi = ((u64) buf_dma) >> 32;
15008 test_desc.addr_lo = buf_dma & 0xffffffff;
15009 test_desc.nic_mbuf = 0x00002100;
15010 test_desc.len = size;
15011
15012 /*
15013 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
15014 * the *second* time the tg3 driver was getting loaded after an
15015 * initial scan.
15016 *
15017 * Broadcom tells me:
15018 * ...the DMA engine is connected to the GRC block and a DMA
15019 * reset may affect the GRC block in some unpredictable way...
15020 * The behavior of resets to individual blocks has not been tested.
15021 *
15022 * Broadcom noted the GRC reset will also reset all sub-components.
15023 */
15024 if (to_device) {
15025 test_desc.cqid_sqid = (13 << 8) | 2;
15026
15027 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
15028 udelay(40);
15029 } else {
15030 test_desc.cqid_sqid = (16 << 8) | 7;
15031
15032 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
15033 udelay(40);
15034 }
15035 test_desc.flags = 0x00000005;
15036
15037 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
15038 u32 val;
15039
15040 val = *(((u32 *)&test_desc) + i);
15041 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
15042 sram_dma_descs + (i * sizeof(u32)));
15043 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
15044 }
15045 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
15046
Matt Carlson859a588792010-04-05 10:19:28 +000015047 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015048 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a588792010-04-05 10:19:28 +000015049 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070015050 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015051
15052 ret = -ENODEV;
15053 for (i = 0; i < 40; i++) {
15054 u32 val;
15055
15056 if (to_device)
15057 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
15058 else
15059 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
15060 if ((val & 0xffff) == sram_dma_descs) {
15061 ret = 0;
15062 break;
15063 }
15064
15065 udelay(100);
15066 }
15067
15068 return ret;
15069}
15070
David S. Millerded73402005-05-23 13:59:47 -070015071#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070015072
Matt Carlson41434702011-03-09 16:58:22 +000015073static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080015074 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
15075 { },
15076};
15077
Linus Torvalds1da177e2005-04-16 15:20:36 -070015078static int __devinit tg3_test_dma(struct tg3 *tp)
15079{
15080 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070015081 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000015082 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015083
Matt Carlson4bae65c2010-11-24 08:31:52 +000015084 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
15085 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015086 if (!buf) {
15087 ret = -ENOMEM;
15088 goto out_nofree;
15089 }
15090
15091 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
15092 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
15093
David S. Miller59e6b432005-05-18 22:50:10 -070015094 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015095
Joe Perches63c3a662011-04-26 08:12:10 +000015096 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000015097 goto out;
15098
Joe Perches63c3a662011-04-26 08:12:10 +000015099 if (tg3_flag(tp, PCI_EXPRESS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015100 /* DMA read watermark not used on PCIE */
15101 tp->dma_rwctrl |= 0x00180000;
Joe Perches63c3a662011-04-26 08:12:10 +000015102 } else if (!tg3_flag(tp, PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070015103 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
15104 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015105 tp->dma_rwctrl |= 0x003f0000;
15106 else
15107 tp->dma_rwctrl |= 0x003f000f;
15108 } else {
15109 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
15110 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
15111 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080015112 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015113
Michael Chan4a29cc22006-03-19 13:21:12 -080015114 /* If the 5704 is behind the EPB bridge, we can
15115 * do the less restrictive ONE_DMA workaround for
15116 * better performance.
15117 */
Joe Perches63c3a662011-04-26 08:12:10 +000015118 if (tg3_flag(tp, 40BIT_DMA_BUG) &&
Michael Chan4a29cc22006-03-19 13:21:12 -080015119 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
15120 tp->dma_rwctrl |= 0x8000;
15121 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015122 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
15123
Michael Chan49afdeb2007-02-13 12:17:03 -080015124 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
15125 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070015126 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080015127 tp->dma_rwctrl |=
15128 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
15129 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
15130 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070015131 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
15132 /* 5780 always in PCIX mode */
15133 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070015134 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
15135 /* 5714 always in PCIX mode */
15136 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015137 } else {
15138 tp->dma_rwctrl |= 0x001b000f;
15139 }
15140 }
15141
15142 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
15143 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
15144 tp->dma_rwctrl &= 0xfffffff0;
15145
15146 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
15147 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
15148 /* Remove this if it causes problems for some boards. */
15149 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
15150
15151 /* On 5700/5701 chips, we need to set this bit.
15152 * Otherwise the chip will issue cacheline transactions
15153 * to streamable DMA memory with not all the byte
15154 * enables turned on. This is an error on several
15155 * RISC PCI controllers, in particular sparc64.
15156 *
15157 * On 5703/5704 chips, this bit has been reassigned
15158 * a different meaning. In particular, it is used
15159 * on those chips to enable a PCI-X workaround.
15160 */
15161 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
15162 }
15163
15164 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15165
15166#if 0
15167 /* Unneeded, already done by tg3_get_invariants. */
15168 tg3_switch_clocks(tp);
15169#endif
15170
Linus Torvalds1da177e2005-04-16 15:20:36 -070015171 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
15172 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
15173 goto out;
15174
David S. Miller59e6b432005-05-18 22:50:10 -070015175 /* It is best to perform DMA test with maximum write burst size
15176 * to expose the 5700/5701 write DMA bug.
15177 */
15178 saved_dma_rwctrl = tp->dma_rwctrl;
15179 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
15180 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15181
Linus Torvalds1da177e2005-04-16 15:20:36 -070015182 while (1) {
15183 u32 *p = buf, i;
15184
15185 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
15186 p[i] = i;
15187
15188 /* Send the buffer to the chip. */
15189 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
15190 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000015191 dev_err(&tp->pdev->dev,
15192 "%s: Buffer write failed. err = %d\n",
15193 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015194 break;
15195 }
15196
15197#if 0
15198 /* validate data reached card RAM correctly. */
15199 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
15200 u32 val;
15201 tg3_read_mem(tp, 0x2100 + (i*4), &val);
15202 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000015203 dev_err(&tp->pdev->dev,
15204 "%s: Buffer corrupted on device! "
15205 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015206 /* ret = -ENODEV here? */
15207 }
15208 p[i] = 0;
15209 }
15210#endif
15211 /* Now read it back. */
15212 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
15213 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000015214 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
15215 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015216 break;
15217 }
15218
15219 /* Verify it. */
15220 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
15221 if (p[i] == i)
15222 continue;
15223
David S. Miller59e6b432005-05-18 22:50:10 -070015224 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
15225 DMA_RWCTRL_WRITE_BNDRY_16) {
15226 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015227 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
15228 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15229 break;
15230 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000015231 dev_err(&tp->pdev->dev,
15232 "%s: Buffer corrupted on read back! "
15233 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015234 ret = -ENODEV;
15235 goto out;
15236 }
15237 }
15238
15239 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
15240 /* Success. */
15241 ret = 0;
15242 break;
15243 }
15244 }
David S. Miller59e6b432005-05-18 22:50:10 -070015245 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
15246 DMA_RWCTRL_WRITE_BNDRY_16) {
15247 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070015248 * now look for chipsets that are known to expose the
15249 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070015250 */
Matt Carlson41434702011-03-09 16:58:22 +000015251 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070015252 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
15253 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a588792010-04-05 10:19:28 +000015254 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070015255 /* Safe to use the calculated DMA boundary. */
15256 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a588792010-04-05 10:19:28 +000015257 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070015258
David S. Miller59e6b432005-05-18 22:50:10 -070015259 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015261
15262out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000015263 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015264out_nofree:
15265 return ret;
15266}
15267
Linus Torvalds1da177e2005-04-16 15:20:36 -070015268static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
15269{
Joe Perches63c3a662011-04-26 08:12:10 +000015270 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson666bc832010-01-20 16:58:03 +000015271 tp->bufmgr_config.mbuf_read_dma_low_water =
15272 DEFAULT_MB_RDMA_LOW_WATER_5705;
15273 tp->bufmgr_config.mbuf_mac_rx_low_water =
15274 DEFAULT_MB_MACRX_LOW_WATER_57765;
15275 tp->bufmgr_config.mbuf_high_water =
15276 DEFAULT_MB_HIGH_WATER_57765;
15277
15278 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15279 DEFAULT_MB_RDMA_LOW_WATER_5705;
15280 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15281 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
15282 tp->bufmgr_config.mbuf_high_water_jumbo =
15283 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000015284 } else if (tg3_flag(tp, 5705_PLUS)) {
Michael Chanfdfec1722005-07-25 12:31:48 -070015285 tp->bufmgr_config.mbuf_read_dma_low_water =
15286 DEFAULT_MB_RDMA_LOW_WATER_5705;
15287 tp->bufmgr_config.mbuf_mac_rx_low_water =
15288 DEFAULT_MB_MACRX_LOW_WATER_5705;
15289 tp->bufmgr_config.mbuf_high_water =
15290 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070015291 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
15292 tp->bufmgr_config.mbuf_mac_rx_low_water =
15293 DEFAULT_MB_MACRX_LOW_WATER_5906;
15294 tp->bufmgr_config.mbuf_high_water =
15295 DEFAULT_MB_HIGH_WATER_5906;
15296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015297
Michael Chanfdfec1722005-07-25 12:31:48 -070015298 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15299 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
15300 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15301 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
15302 tp->bufmgr_config.mbuf_high_water_jumbo =
15303 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
15304 } else {
15305 tp->bufmgr_config.mbuf_read_dma_low_water =
15306 DEFAULT_MB_RDMA_LOW_WATER;
15307 tp->bufmgr_config.mbuf_mac_rx_low_water =
15308 DEFAULT_MB_MACRX_LOW_WATER;
15309 tp->bufmgr_config.mbuf_high_water =
15310 DEFAULT_MB_HIGH_WATER;
15311
15312 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15313 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
15314 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15315 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
15316 tp->bufmgr_config.mbuf_high_water_jumbo =
15317 DEFAULT_MB_HIGH_WATER_JUMBO;
15318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015319
15320 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
15321 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
15322}
15323
15324static char * __devinit tg3_phy_string(struct tg3 *tp)
15325{
Matt Carlson79eb6902010-02-17 15:17:03 +000015326 switch (tp->phy_id & TG3_PHY_ID_MASK) {
15327 case TG3_PHY_ID_BCM5400: return "5400";
15328 case TG3_PHY_ID_BCM5401: return "5401";
15329 case TG3_PHY_ID_BCM5411: return "5411";
15330 case TG3_PHY_ID_BCM5701: return "5701";
15331 case TG3_PHY_ID_BCM5703: return "5703";
15332 case TG3_PHY_ID_BCM5704: return "5704";
15333 case TG3_PHY_ID_BCM5705: return "5705";
15334 case TG3_PHY_ID_BCM5750: return "5750";
15335 case TG3_PHY_ID_BCM5752: return "5752";
15336 case TG3_PHY_ID_BCM5714: return "5714";
15337 case TG3_PHY_ID_BCM5780: return "5780";
15338 case TG3_PHY_ID_BCM5755: return "5755";
15339 case TG3_PHY_ID_BCM5787: return "5787";
15340 case TG3_PHY_ID_BCM5784: return "5784";
15341 case TG3_PHY_ID_BCM5756: return "5722/5756";
15342 case TG3_PHY_ID_BCM5906: return "5906";
15343 case TG3_PHY_ID_BCM5761: return "5761";
15344 case TG3_PHY_ID_BCM5718C: return "5718C";
15345 case TG3_PHY_ID_BCM5718S: return "5718S";
15346 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000015347 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000015348 case TG3_PHY_ID_BCM5720C: return "5720C";
Matt Carlson79eb6902010-02-17 15:17:03 +000015349 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070015350 case 0: return "serdes";
15351 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070015352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015353}
15354
Michael Chanf9804dd2005-09-27 12:13:10 -070015355static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
15356{
Joe Perches63c3a662011-04-26 08:12:10 +000015357 if (tg3_flag(tp, PCI_EXPRESS)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070015358 strcpy(str, "PCI Express");
15359 return str;
Joe Perches63c3a662011-04-26 08:12:10 +000015360 } else if (tg3_flag(tp, PCIX_MODE)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070015361 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
15362
15363 strcpy(str, "PCIX:");
15364
15365 if ((clock_ctrl == 7) ||
15366 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
15367 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
15368 strcat(str, "133MHz");
15369 else if (clock_ctrl == 0)
15370 strcat(str, "33MHz");
15371 else if (clock_ctrl == 2)
15372 strcat(str, "50MHz");
15373 else if (clock_ctrl == 4)
15374 strcat(str, "66MHz");
15375 else if (clock_ctrl == 6)
15376 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070015377 } else {
15378 strcpy(str, "PCI:");
Joe Perches63c3a662011-04-26 08:12:10 +000015379 if (tg3_flag(tp, PCI_HIGH_SPEED))
Michael Chanf9804dd2005-09-27 12:13:10 -070015380 strcat(str, "66MHz");
15381 else
15382 strcat(str, "33MHz");
15383 }
Joe Perches63c3a662011-04-26 08:12:10 +000015384 if (tg3_flag(tp, PCI_32BIT))
Michael Chanf9804dd2005-09-27 12:13:10 -070015385 strcat(str, ":32-bit");
15386 else
15387 strcat(str, ":64-bit");
15388 return str;
15389}
15390
David S. Miller15f98502005-05-18 22:49:26 -070015391static void __devinit tg3_init_coal(struct tg3 *tp)
15392{
15393 struct ethtool_coalesce *ec = &tp->coal;
15394
15395 memset(ec, 0, sizeof(*ec));
15396 ec->cmd = ETHTOOL_GCOALESCE;
15397 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
15398 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
15399 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
15400 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
15401 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
15402 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
15403 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
15404 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
15405 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
15406
15407 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
15408 HOSTCC_MODE_CLRTICK_TXBD)) {
15409 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
15410 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
15411 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
15412 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
15413 }
Michael Chand244c892005-07-05 14:42:33 -070015414
Joe Perches63c3a662011-04-26 08:12:10 +000015415 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070015416 ec->rx_coalesce_usecs_irq = 0;
15417 ec->tx_coalesce_usecs_irq = 0;
15418 ec->stats_block_coalesce_usecs = 0;
15419 }
David S. Miller15f98502005-05-18 22:49:26 -070015420}
15421
Linus Torvalds1da177e2005-04-16 15:20:36 -070015422static int __devinit tg3_init_one(struct pci_dev *pdev,
15423 const struct pci_device_id *ent)
15424{
Linus Torvalds1da177e2005-04-16 15:20:36 -070015425 struct net_device *dev;
15426 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000015427 int i, err, pm_cap;
15428 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070015429 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080015430 u64 dma_mask, persist_dma_mask;
Michał Mirosławc8f44af2011-11-15 15:29:55 +000015431 netdev_features_t features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015432
Joe Perches05dbe002010-02-17 19:44:19 +000015433 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015434
15435 err = pci_enable_device(pdev);
15436 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015437 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015438 return err;
15439 }
15440
Linus Torvalds1da177e2005-04-16 15:20:36 -070015441 err = pci_request_regions(pdev, DRV_MODULE_NAME);
15442 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015443 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015444 goto err_out_disable_pdev;
15445 }
15446
15447 pci_set_master(pdev);
15448
15449 /* Find power-management capability. */
15450 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
15451 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000015452 dev_err(&pdev->dev,
15453 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015454 err = -EIO;
15455 goto err_out_free_res;
15456 }
15457
Matt Carlson16821282011-07-13 09:27:28 +000015458 err = pci_set_power_state(pdev, PCI_D0);
15459 if (err) {
15460 dev_err(&pdev->dev, "Transition to D0 failed, aborting\n");
15461 goto err_out_free_res;
15462 }
15463
Matt Carlsonfe5f5782009-09-01 13:09:39 +000015464 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015465 if (!dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015466 err = -ENOMEM;
Matt Carlson16821282011-07-13 09:27:28 +000015467 goto err_out_power_down;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015468 }
15469
Linus Torvalds1da177e2005-04-16 15:20:36 -070015470 SET_NETDEV_DEV(dev, &pdev->dev);
15471
Linus Torvalds1da177e2005-04-16 15:20:36 -070015472 tp = netdev_priv(dev);
15473 tp->pdev = pdev;
15474 tp->dev = dev;
15475 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015476 tp->rx_mode = TG3_DEF_RX_MODE;
15477 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070015478
Linus Torvalds1da177e2005-04-16 15:20:36 -070015479 if (tg3_debug > 0)
15480 tp->msg_enable = tg3_debug;
15481 else
15482 tp->msg_enable = TG3_DEF_MSG_ENABLE;
15483
15484 /* The word/byte swap controls here control register access byte
15485 * swapping. DMA data byte swapping is controlled in the GRC_MODE
15486 * setting below.
15487 */
15488 tp->misc_host_ctrl =
15489 MISC_HOST_CTRL_MASK_PCI_INT |
15490 MISC_HOST_CTRL_WORD_SWAP |
15491 MISC_HOST_CTRL_INDIR_ACCESS |
15492 MISC_HOST_CTRL_PCISTATE_RW;
15493
15494 /* The NONFRM (non-frame) byte/word swap controls take effect
15495 * on descriptor entries, anything which isn't packet data.
15496 *
15497 * The StrongARM chips on the board (one for tx, one for rx)
15498 * are running in big-endian mode.
15499 */
15500 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
15501 GRC_MODE_WSWAP_NONFRM_DATA);
15502#ifdef __BIG_ENDIAN
15503 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
15504#endif
15505 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015506 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000015507 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015508
Matt Carlsond5fe4882008-11-21 17:20:32 -080015509 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010015510 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015511 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015512 err = -ENOMEM;
15513 goto err_out_free_dev;
15514 }
15515
Matt Carlsonc9cab242011-07-13 09:27:27 +000015516 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
15517 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761E ||
15518 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S ||
15519 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761SE ||
15520 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
15521 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
15522 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
15523 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720) {
15524 tg3_flag_set(tp, ENABLE_APE);
15525 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
15526 if (!tp->aperegs) {
15527 dev_err(&pdev->dev,
15528 "Cannot map APE registers, aborting\n");
15529 err = -ENOMEM;
15530 goto err_out_iounmap;
15531 }
15532 }
15533
Linus Torvalds1da177e2005-04-16 15:20:36 -070015534 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
15535 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015536
Linus Torvalds1da177e2005-04-16 15:20:36 -070015537 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015538 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Matt Carlson2ffcc982011-05-19 12:12:44 +000015539 dev->netdev_ops = &tg3_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015540 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015541
15542 err = tg3_get_invariants(tp);
15543 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015544 dev_err(&pdev->dev,
15545 "Problem fetching invariants of chip, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015546 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015547 }
15548
Michael Chan4a29cc22006-03-19 13:21:12 -080015549 /* The EPB bridge inside 5714, 5715, and 5780 and any
15550 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080015551 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
15552 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
15553 * do DMA address check in tg3_start_xmit().
15554 */
Joe Perches63c3a662011-04-26 08:12:10 +000015555 if (tg3_flag(tp, IS_5788))
Yang Hongyang284901a2009-04-06 19:01:15 -070015556 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Joe Perches63c3a662011-04-26 08:12:10 +000015557 else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070015558 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080015559#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070015560 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015561#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080015562 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070015563 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015564
15565 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070015566 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080015567 err = pci_set_dma_mask(pdev, dma_mask);
15568 if (!err) {
Matt Carlson0da06062011-05-19 12:12:53 +000015569 features |= NETIF_F_HIGHDMA;
Michael Chan72f2afb2006-03-06 19:28:35 -080015570 err = pci_set_consistent_dma_mask(pdev,
15571 persist_dma_mask);
15572 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015573 dev_err(&pdev->dev, "Unable to obtain 64 bit "
15574 "DMA for consistent allocations\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015575 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080015576 }
15577 }
15578 }
Yang Hongyang284901a2009-04-06 19:01:15 -070015579 if (err || dma_mask == DMA_BIT_MASK(32)) {
15580 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080015581 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015582 dev_err(&pdev->dev,
15583 "No usable DMA configuration, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015584 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080015585 }
15586 }
15587
Michael Chanfdfec1722005-07-25 12:31:48 -070015588 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015589
Matt Carlson0da06062011-05-19 12:12:53 +000015590 features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
15591
15592 /* 5700 B0 chips do not support checksumming correctly due
15593 * to hardware bugs.
15594 */
15595 if (tp->pci_chip_rev_id != CHIPREV_ID_5700_B0) {
15596 features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
15597
15598 if (tg3_flag(tp, 5755_PLUS))
15599 features |= NETIF_F_IPV6_CSUM;
15600 }
15601
Michael Chan4e3a7aa2006-03-20 17:47:44 -080015602 /* TSO is on by default on chips that support hardware TSO.
15603 * Firmware TSO on older chips gives lower performance, so it
15604 * is off by default, but can be enabled using ethtool.
15605 */
Joe Perches63c3a662011-04-26 08:12:10 +000015606 if ((tg3_flag(tp, HW_TSO_1) ||
15607 tg3_flag(tp, HW_TSO_2) ||
15608 tg3_flag(tp, HW_TSO_3)) &&
Matt Carlson0da06062011-05-19 12:12:53 +000015609 (features & NETIF_F_IP_CSUM))
15610 features |= NETIF_F_TSO;
Joe Perches63c3a662011-04-26 08:12:10 +000015611 if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
Matt Carlson0da06062011-05-19 12:12:53 +000015612 if (features & NETIF_F_IPV6_CSUM)
15613 features |= NETIF_F_TSO6;
Joe Perches63c3a662011-04-26 08:12:10 +000015614 if (tg3_flag(tp, HW_TSO_3) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000015615 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070015616 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
15617 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Joe Perches63c3a662011-04-26 08:12:10 +000015618 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Michał Mirosławdc668912011-04-07 03:35:07 +000015619 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson0da06062011-05-19 12:12:53 +000015620 features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070015621 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015622
Matt Carlsond542fe22011-05-19 16:02:43 +000015623 dev->features |= features;
15624 dev->vlan_features |= features;
15625
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015626 /*
15627 * Add loopback capability only for a subset of devices that support
15628 * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
15629 * loopback for the remaining devices.
15630 */
15631 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
15632 !tg3_flag(tp, CPMU_PRESENT))
15633 /* Add the loopback capability */
Matt Carlson0da06062011-05-19 12:12:53 +000015634 features |= NETIF_F_LOOPBACK;
15635
Matt Carlson0da06062011-05-19 12:12:53 +000015636 dev->hw_features |= features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015637
Linus Torvalds1da177e2005-04-16 15:20:36 -070015638 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015639 !tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015640 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015641 tg3_flag_set(tp, MAX_RXPEND_64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015642 tp->rx_pending = 63;
15643 }
15644
Linus Torvalds1da177e2005-04-16 15:20:36 -070015645 err = tg3_get_device_address(tp);
15646 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015647 dev_err(&pdev->dev,
15648 "Could not obtain valid ethernet address, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015649 goto err_out_apeunmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070015650 }
15651
Matt Carlsonc88864d2007-11-12 21:07:01 -080015652 /*
15653 * Reset chip in case UNDI or EFI driver did not shutdown
15654 * DMA self test will enable WDMAC and we'll see (spurious)
15655 * pending DMA on the PCI bus at that point.
15656 */
15657 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
15658 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
15659 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
15660 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
15661 }
15662
15663 err = tg3_test_dma(tp);
15664 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015665 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080015666 goto err_out_apeunmap;
15667 }
15668
Matt Carlson78f90dc2009-11-13 13:03:42 +000015669 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
15670 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
15671 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000015672 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000015673 struct tg3_napi *tnapi = &tp->napi[i];
15674
15675 tnapi->tp = tp;
15676 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
15677
15678 tnapi->int_mbox = intmbx;
Matt Carlson93a700a2011-08-31 11:44:54 +000015679 if (i <= 4)
Matt Carlson78f90dc2009-11-13 13:03:42 +000015680 intmbx += 0x8;
15681 else
15682 intmbx += 0x4;
15683
15684 tnapi->consmbox = rcvmbx;
15685 tnapi->prodmbox = sndmbx;
15686
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015687 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000015688 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015689 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000015690 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000015691
Joe Perches63c3a662011-04-26 08:12:10 +000015692 if (!tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson78f90dc2009-11-13 13:03:42 +000015693 break;
15694
15695 /*
15696 * If we support MSIX, we'll be using RSS. If we're using
15697 * RSS, the first vector only handles link interrupts and the
15698 * remaining vectors handle rx and tx interrupts. Reuse the
15699 * mailbox values for the next iteration. The values we setup
15700 * above are still useful for the single vectored mode.
15701 */
15702 if (!i)
15703 continue;
15704
15705 rcvmbx += 0x8;
15706
15707 if (sndmbx & 0x4)
15708 sndmbx -= 0x4;
15709 else
15710 sndmbx += 0xc;
15711 }
15712
Matt Carlsonc88864d2007-11-12 21:07:01 -080015713 tg3_init_coal(tp);
15714
Michael Chanc49a1562006-12-17 17:07:29 -080015715 pci_set_drvdata(pdev, dev);
15716
Matt Carlsoncd0d7222011-07-13 09:27:33 +000015717 if (tg3_flag(tp, 5717_PLUS)) {
15718 /* Resume a low-power mode */
15719 tg3_frob_aux_power(tp, false);
15720 }
15721
Matt Carlson21f76382012-02-22 12:35:21 +000015722 tg3_timer_init(tp);
15723
Linus Torvalds1da177e2005-04-16 15:20:36 -070015724 err = register_netdev(dev);
15725 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015726 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070015727 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015728 }
15729
Joe Perches05dbe002010-02-17 19:44:19 +000015730 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
15731 tp->board_part_number,
15732 tp->pci_chip_rev_id,
15733 tg3_bus_string(tp, str),
15734 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015735
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015736 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000015737 struct phy_device *phydev;
15738 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000015739 netdev_info(dev,
15740 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000015741 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015742 } else {
15743 char *ethtype;
15744
15745 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
15746 ethtype = "10/100Base-TX";
15747 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
15748 ethtype = "1000Base-SX";
15749 else
15750 ethtype = "10/100/1000Base-T";
15751
Matt Carlson5129c3a2010-04-05 10:19:23 +000015752 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlson47007832011-04-20 07:57:43 +000015753 "(WireSpeed[%d], EEE[%d])\n",
15754 tg3_phy_string(tp), ethtype,
15755 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
15756 (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015757 }
Matt Carlsondf59c942008-11-03 16:52:56 -080015758
Joe Perches05dbe002010-02-17 19:44:19 +000015759 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000015760 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015761 tg3_flag(tp, USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015762 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015763 tg3_flag(tp, ENABLE_ASF) != 0,
15764 tg3_flag(tp, TSO_CAPABLE) != 0);
Joe Perches05dbe002010-02-17 19:44:19 +000015765 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
15766 tp->dma_rwctrl,
15767 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
15768 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015769
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015770 pci_save_state(pdev);
15771
Linus Torvalds1da177e2005-04-16 15:20:36 -070015772 return 0;
15773
Matt Carlson0d3031d2007-10-10 18:02:43 -070015774err_out_apeunmap:
15775 if (tp->aperegs) {
15776 iounmap(tp->aperegs);
15777 tp->aperegs = NULL;
15778 }
15779
Linus Torvalds1da177e2005-04-16 15:20:36 -070015780err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070015781 if (tp->regs) {
15782 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015783 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015785
15786err_out_free_dev:
15787 free_netdev(dev);
15788
Matt Carlson16821282011-07-13 09:27:28 +000015789err_out_power_down:
15790 pci_set_power_state(pdev, PCI_D3hot);
15791
Linus Torvalds1da177e2005-04-16 15:20:36 -070015792err_out_free_res:
15793 pci_release_regions(pdev);
15794
15795err_out_disable_pdev:
15796 pci_disable_device(pdev);
15797 pci_set_drvdata(pdev, NULL);
15798 return err;
15799}
15800
15801static void __devexit tg3_remove_one(struct pci_dev *pdev)
15802{
15803 struct net_device *dev = pci_get_drvdata(pdev);
15804
15805 if (dev) {
15806 struct tg3 *tp = netdev_priv(dev);
15807
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080015808 if (tp->fw)
15809 release_firmware(tp->fw);
15810
Matt Carlsondb219972011-11-04 09:15:03 +000015811 tg3_reset_task_cancel(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015812
David S. Miller1805b2f2011-10-24 18:18:09 -040015813 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015814 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015815 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015816 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070015817
Linus Torvalds1da177e2005-04-16 15:20:36 -070015818 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015819 if (tp->aperegs) {
15820 iounmap(tp->aperegs);
15821 tp->aperegs = NULL;
15822 }
Michael Chan68929142005-08-09 20:17:14 -070015823 if (tp->regs) {
15824 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015825 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015827 free_netdev(dev);
15828 pci_release_regions(pdev);
15829 pci_disable_device(pdev);
15830 pci_set_drvdata(pdev, NULL);
15831 }
15832}
15833
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015834#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015835static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015836{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015837 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015838 struct net_device *dev = pci_get_drvdata(pdev);
15839 struct tg3 *tp = netdev_priv(dev);
15840 int err;
15841
15842 if (!netif_running(dev))
15843 return 0;
15844
Matt Carlsondb219972011-11-04 09:15:03 +000015845 tg3_reset_task_cancel(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015846 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015847 tg3_netif_stop(tp);
15848
Matt Carlson21f76382012-02-22 12:35:21 +000015849 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015850
David S. Millerf47c11e2005-06-24 20:18:35 -070015851 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015852 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070015853 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015854
15855 netif_device_detach(dev);
15856
David S. Millerf47c11e2005-06-24 20:18:35 -070015857 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070015858 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches63c3a662011-04-26 08:12:10 +000015859 tg3_flag_clear(tp, INIT_COMPLETE);
David S. Millerf47c11e2005-06-24 20:18:35 -070015860 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015861
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015862 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015863 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015864 int err2;
15865
David S. Millerf47c11e2005-06-24 20:18:35 -070015866 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015867
Joe Perches63c3a662011-04-26 08:12:10 +000015868 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015869 err2 = tg3_restart_hw(tp, 1);
15870 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070015871 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015872
Matt Carlson21f76382012-02-22 12:35:21 +000015873 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015874
15875 netif_device_attach(dev);
15876 tg3_netif_start(tp);
15877
Michael Chanb9ec6c12006-07-25 16:37:27 -070015878out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015879 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015880
15881 if (!err2)
15882 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015883 }
15884
15885 return err;
15886}
15887
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015888static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015889{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015890 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015891 struct net_device *dev = pci_get_drvdata(pdev);
15892 struct tg3 *tp = netdev_priv(dev);
15893 int err;
15894
15895 if (!netif_running(dev))
15896 return 0;
15897
Linus Torvalds1da177e2005-04-16 15:20:36 -070015898 netif_device_attach(dev);
15899
David S. Millerf47c11e2005-06-24 20:18:35 -070015900 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015901
Joe Perches63c3a662011-04-26 08:12:10 +000015902 tg3_flag_set(tp, INIT_COMPLETE);
Michael Chanb9ec6c12006-07-25 16:37:27 -070015903 err = tg3_restart_hw(tp, 1);
15904 if (err)
15905 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015906
Matt Carlson21f76382012-02-22 12:35:21 +000015907 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015908
Linus Torvalds1da177e2005-04-16 15:20:36 -070015909 tg3_netif_start(tp);
15910
Michael Chanb9ec6c12006-07-25 16:37:27 -070015911out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015912 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015913
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015914 if (!err)
15915 tg3_phy_start(tp);
15916
Michael Chanb9ec6c12006-07-25 16:37:27 -070015917 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015918}
15919
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015920static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015921#define TG3_PM_OPS (&tg3_pm_ops)
15922
15923#else
15924
15925#define TG3_PM_OPS NULL
15926
15927#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015928
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015929/**
15930 * tg3_io_error_detected - called when PCI error is detected
15931 * @pdev: Pointer to PCI device
15932 * @state: The current pci connection state
15933 *
15934 * This function is called after a PCI bus error affecting
15935 * this device has been detected.
15936 */
15937static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
15938 pci_channel_state_t state)
15939{
15940 struct net_device *netdev = pci_get_drvdata(pdev);
15941 struct tg3 *tp = netdev_priv(netdev);
15942 pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
15943
15944 netdev_info(netdev, "PCI I/O error detected\n");
15945
15946 rtnl_lock();
15947
15948 if (!netif_running(netdev))
15949 goto done;
15950
15951 tg3_phy_stop(tp);
15952
15953 tg3_netif_stop(tp);
15954
Matt Carlson21f76382012-02-22 12:35:21 +000015955 tg3_timer_stop(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015956
15957 /* Want to make sure that the reset task doesn't run */
Matt Carlsondb219972011-11-04 09:15:03 +000015958 tg3_reset_task_cancel(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015959
15960 netif_device_detach(netdev);
15961
15962 /* Clean up software state, even if MMIO is blocked */
15963 tg3_full_lock(tp, 0);
15964 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
15965 tg3_full_unlock(tp);
15966
15967done:
15968 if (state == pci_channel_io_perm_failure)
15969 err = PCI_ERS_RESULT_DISCONNECT;
15970 else
15971 pci_disable_device(pdev);
15972
15973 rtnl_unlock();
15974
15975 return err;
15976}
15977
15978/**
15979 * tg3_io_slot_reset - called after the pci bus has been reset.
15980 * @pdev: Pointer to PCI device
15981 *
15982 * Restart the card from scratch, as if from a cold-boot.
15983 * At this point, the card has exprienced a hard reset,
15984 * followed by fixups by BIOS, and has its config space
15985 * set up identically to what it was at cold boot.
15986 */
15987static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
15988{
15989 struct net_device *netdev = pci_get_drvdata(pdev);
15990 struct tg3 *tp = netdev_priv(netdev);
15991 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
15992 int err;
15993
15994 rtnl_lock();
15995
15996 if (pci_enable_device(pdev)) {
15997 netdev_err(netdev, "Cannot re-enable PCI device after reset.\n");
15998 goto done;
15999 }
16000
16001 pci_set_master(pdev);
16002 pci_restore_state(pdev);
16003 pci_save_state(pdev);
16004
16005 if (!netif_running(netdev)) {
16006 rc = PCI_ERS_RESULT_RECOVERED;
16007 goto done;
16008 }
16009
16010 err = tg3_power_up(tp);
Matt Carlsonbed98292011-07-13 09:27:29 +000016011 if (err)
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016012 goto done;
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016013
16014 rc = PCI_ERS_RESULT_RECOVERED;
16015
16016done:
16017 rtnl_unlock();
16018
16019 return rc;
16020}
16021
16022/**
16023 * tg3_io_resume - called when traffic can start flowing again.
16024 * @pdev: Pointer to PCI device
16025 *
16026 * This callback is called when the error recovery driver tells
16027 * us that its OK to resume normal operation.
16028 */
16029static void tg3_io_resume(struct pci_dev *pdev)
16030{
16031 struct net_device *netdev = pci_get_drvdata(pdev);
16032 struct tg3 *tp = netdev_priv(netdev);
16033 int err;
16034
16035 rtnl_lock();
16036
16037 if (!netif_running(netdev))
16038 goto done;
16039
16040 tg3_full_lock(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +000016041 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016042 err = tg3_restart_hw(tp, 1);
16043 tg3_full_unlock(tp);
16044 if (err) {
16045 netdev_err(netdev, "Cannot restart hardware after reset.\n");
16046 goto done;
16047 }
16048
16049 netif_device_attach(netdev);
16050
Matt Carlson21f76382012-02-22 12:35:21 +000016051 tg3_timer_start(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016052
16053 tg3_netif_start(tp);
16054
16055 tg3_phy_start(tp);
16056
16057done:
16058 rtnl_unlock();
16059}
16060
16061static struct pci_error_handlers tg3_err_handler = {
16062 .error_detected = tg3_io_error_detected,
16063 .slot_reset = tg3_io_slot_reset,
16064 .resume = tg3_io_resume
16065};
16066
Linus Torvalds1da177e2005-04-16 15:20:36 -070016067static struct pci_driver tg3_driver = {
16068 .name = DRV_MODULE_NAME,
16069 .id_table = tg3_pci_tbl,
16070 .probe = tg3_init_one,
16071 .remove = __devexit_p(tg3_remove_one),
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016072 .err_handler = &tg3_err_handler,
Eric Dumazetaa6027c2011-01-01 05:22:46 +000016073 .driver.pm = TG3_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070016074};
16075
16076static int __init tg3_init(void)
16077{
Jeff Garzik29917622006-08-19 17:48:59 -040016078 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016079}
16080
16081static void __exit tg3_cleanup(void)
16082{
16083 pci_unregister_driver(&tg3_driver);
16084}
16085
16086module_init(tg3_init);
16087module_exit(tg3_cleanup);