blob: d529af99157dd6ce752fa6c3d22e0da572b2f7ae [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * tg3.c: Broadcom Tigon3 ethernet driver.
3 *
4 * Copyright (C) 2001, 2002, 2003, 2004 David S. Miller (davem@redhat.com)
5 * Copyright (C) 2001, 2002, 2003 Jeff Garzik (jgarzik@pobox.com)
6 * Copyright (C) 2004 Sun Microsystems Inc.
Matt Carlsonb86fb2c2011-01-25 15:58:57 +00007 * Copyright (C) 2005-2011 Broadcom Corporation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * Firmware is:
Michael Chan49cabf42005-06-06 15:15:17 -070010 * Derived from proprietary unpublished source code,
11 * Copyright (C) 2000-2003 Broadcom Corporation.
12 *
13 * Permission is hereby granted for the distribution of this firmware
14 * data in hexadecimal or equivalent format, provided this copyright
15 * notice is accompanying it.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 */
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#include <linux/module.h>
20#include <linux/moduleparam.h>
Matt Carlson6867c842010-07-11 09:31:44 +000021#include <linux/stringify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/kernel.h>
23#include <linux/types.h>
24#include <linux/compiler.h>
25#include <linux/slab.h>
26#include <linux/delay.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020027#include <linux/in.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/init.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000029#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/ioport.h>
31#include <linux/pci.h>
32#include <linux/netdevice.h>
33#include <linux/etherdevice.h>
34#include <linux/skbuff.h>
35#include <linux/ethtool.h>
Matt Carlson3110f5f52010-12-06 08:28:50 +000036#include <linux/mdio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/mii.h>
Matt Carlson158d7ab2008-05-29 01:37:54 -070038#include <linux/phy.h>
Matt Carlsona9daf362008-05-25 23:49:44 -070039#include <linux/brcmphy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/if_vlan.h>
41#include <linux/ip.h>
42#include <linux/tcp.h>
43#include <linux/workqueue.h>
Michael Chan61487482005-09-05 17:53:19 -070044#include <linux/prefetch.h>
Tobias Klauserf9a5f7d2005-10-29 15:09:26 +020045#include <linux/dma-mapping.h>
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080046#include <linux/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#include <net/checksum.h>
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030049#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51#include <asm/system.h>
Javier Martinez Canillas27fd9de2011-03-26 16:42:31 +000052#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <asm/byteorder.h>
Javier Martinez Canillas27fd9de2011-03-26 16:42:31 +000054#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
David S. Miller49b6e95f2007-03-29 01:38:42 -070056#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <asm/idprom.h>
David S. Miller49b6e95f2007-03-29 01:38:42 -070058#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#endif
60
Matt Carlson63532392008-11-03 16:49:57 -080061#define BAR_0 0
62#define BAR_2 2
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include "tg3.h"
65
Joe Perches63c3a662011-04-26 08:12:10 +000066/* Functions & macros to verify TG3_FLAGS types */
67
68static inline int _tg3_flag(enum TG3_FLAGS flag, unsigned long *bits)
69{
70 return test_bit(flag, bits);
71}
72
73static inline void _tg3_flag_set(enum TG3_FLAGS flag, unsigned long *bits)
74{
75 set_bit(flag, bits);
76}
77
78static inline void _tg3_flag_clear(enum TG3_FLAGS flag, unsigned long *bits)
79{
80 clear_bit(flag, bits);
81}
82
83#define tg3_flag(tp, flag) \
84 _tg3_flag(TG3_FLAG_##flag, (tp)->tg3_flags)
85#define tg3_flag_set(tp, flag) \
86 _tg3_flag_set(TG3_FLAG_##flag, (tp)->tg3_flags)
87#define tg3_flag_clear(tp, flag) \
88 _tg3_flag_clear(TG3_FLAG_##flag, (tp)->tg3_flags)
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#define DRV_MODULE_NAME "tg3"
Matt Carlson6867c842010-07-11 09:31:44 +000091#define TG3_MAJ_NUM 3
Matt Carlsonefab79c2011-12-08 14:40:18 +000092#define TG3_MIN_NUM 122
Matt Carlson6867c842010-07-11 09:31:44 +000093#define DRV_MODULE_VERSION \
94 __stringify(TG3_MAJ_NUM) "." __stringify(TG3_MIN_NUM)
Matt Carlsonefab79c2011-12-08 14:40:18 +000095#define DRV_MODULE_RELDATE "December 7, 2011"
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Matt Carlsonfd6d3f02011-08-31 11:44:52 +000097#define RESET_KIND_SHUTDOWN 0
98#define RESET_KIND_INIT 1
99#define RESET_KIND_SUSPEND 2
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101#define TG3_DEF_RX_MODE 0
102#define TG3_DEF_TX_MODE 0
103#define TG3_DEF_MSG_ENABLE \
104 (NETIF_MSG_DRV | \
105 NETIF_MSG_PROBE | \
106 NETIF_MSG_LINK | \
107 NETIF_MSG_TIMER | \
108 NETIF_MSG_IFDOWN | \
109 NETIF_MSG_IFUP | \
110 NETIF_MSG_RX_ERR | \
111 NETIF_MSG_TX_ERR)
112
Matt Carlson520b2752011-06-13 13:39:02 +0000113#define TG3_GRC_LCLCTL_PWRSW_DELAY 100
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115/* length of time before we decide the hardware is borked,
116 * and dev->tx_timeout() should be called to fix the problem
117 */
Joe Perches63c3a662011-04-26 08:12:10 +0000118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119#define TG3_TX_TIMEOUT (5 * HZ)
120
121/* hardware minimum and maximum for a single frame's data payload */
122#define TG3_MIN_MTU 60
123#define TG3_MAX_MTU(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000124 (tg3_flag(tp, JUMBO_CAPABLE) ? 9000 : 1500)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126/* These numbers seem to be hard coded in the NIC firmware somehow.
127 * You can't change the ring sizes, but you can change where you place
128 * them in the NIC onboard memory.
129 */
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000130#define TG3_RX_STD_RING_SIZE(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000131 (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000132 TG3_RX_STD_MAX_SIZE_5717 : TG3_RX_STD_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133#define TG3_DEF_RX_RING_PENDING 200
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000134#define TG3_RX_JMB_RING_SIZE(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000135 (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000136 TG3_RX_JMB_MAX_SIZE_5717 : TG3_RX_JMB_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137#define TG3_DEF_RX_JUMBO_RING_PENDING 100
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
207
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800208#define FIRMWARE_TG3 "tigon/tg3.bin"
209#define FIRMWARE_TG3TSO "tigon/tg3_tso.bin"
210#define FIRMWARE_TG3TSO5 "tigon/tg3_tso5.bin"
211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212static char version[] __devinitdata =
Joe Perches05dbe002010-02-17 19:44:19 +0000213 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215MODULE_AUTHOR("David S. Miller (davem@redhat.com) and Jeff Garzik (jgarzik@pobox.com)");
216MODULE_DESCRIPTION("Broadcom Tigon3 ethernet driver");
217MODULE_LICENSE("GPL");
218MODULE_VERSION(DRV_MODULE_VERSION);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800219MODULE_FIRMWARE(FIRMWARE_TG3);
220MODULE_FIRMWARE(FIRMWARE_TG3TSO);
221MODULE_FIRMWARE(FIRMWARE_TG3TSO5);
222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223static int tg3_debug = -1; /* -1 == use TG3_DEF_MSG_ENABLE as value */
224module_param(tg3_debug, int, 0);
225MODULE_PARM_DESC(tg3_debug, "Tigon3 bitmapped debugging message enable value");
226
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000227static DEFINE_PCI_DEVICE_TABLE(tg3_pci_tbl) = {
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700228 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5700)},
229 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5701)},
230 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702)},
231 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703)},
232 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704)},
233 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702FE)},
234 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705)},
235 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705_2)},
236 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M)},
237 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M_2)},
238 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702X)},
239 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703X)},
240 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S)},
241 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702A3)},
242 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703A3)},
243 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5782)},
244 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5788)},
245 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5789)},
246 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901)},
247 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901_2)},
248 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S_2)},
249 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705F)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700250 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5721)},
Michael Chan126a3362006-09-27 16:03:07 -0700251 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5722)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700252 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700253 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751M)},
254 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751F)},
255 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752)},
256 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752M)},
257 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753)},
258 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753M)},
259 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753F)},
260 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754)},
261 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754M)},
262 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755)},
263 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755M)},
Michael Chan126a3362006-09-27 16:03:07 -0700264 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5756)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700265 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5786)},
266 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787)},
267 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787M)},
Michael Chan676917d2006-12-07 00:20:22 -0800268 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787F)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700269 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714)},
270 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714S)},
271 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715)},
272 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715S)},
273 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780)},
274 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780S)},
275 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5781)},
Michael Chanb5d37722006-09-27 16:06:21 -0700276 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906)},
277 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906M)},
Matt Carlsond30cdd22007-10-07 23:28:35 -0700278 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5784)},
279 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5764)},
Matt Carlson6c7af272007-10-21 16:12:02 -0700280 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5723)},
Matt Carlson9936bcf2007-10-10 18:03:07 -0700281 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761)},
282 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761E)},
Matt Carlsonc88e6682008-11-03 16:49:18 -0800283 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761S)},
284 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761SE)},
Matt Carlson2befdce2009-08-28 12:28:45 +0000285 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_G)},
286 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_F)},
Matt Carlson321d32a2008-11-21 17:22:19 -0800287 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780)},
288 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57760)},
289 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57790)},
Matt Carlson5e7ccf22009-08-25 10:08:42 +0000290 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57788)},
Matt Carlson5001e2f2009-11-13 13:03:51 +0000291 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717)},
292 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5718)},
Matt Carlsonb0f75222010-01-20 16:58:11 +0000293 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57781)},
294 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57785)},
295 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57761)},
296 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57765)},
297 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57791)},
298 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57795)},
Matt Carlson302b5002010-06-05 17:24:38 +0000299 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5719)},
Matt Carlsonba1f3c72011-04-05 14:22:50 +0000300 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5720)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700301 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9DXX)},
302 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9MXX)},
303 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1000)},
304 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1001)},
305 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1003)},
306 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC9100)},
307 {PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_TIGON3)},
Meelis Roos1dcb14d2011-05-25 05:43:47 +0000308 {PCI_DEVICE(0x10cf, 0x11a2)}, /* Fujitsu 1000base-SX with BCM5703SKHB */
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700309 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310};
311
312MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
313
Andreas Mohr50da8592006-08-14 23:54:30 -0700314static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000316} ethtool_stats_keys[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 { "rx_octets" },
318 { "rx_fragments" },
319 { "rx_ucast_packets" },
320 { "rx_mcast_packets" },
321 { "rx_bcast_packets" },
322 { "rx_fcs_errors" },
323 { "rx_align_errors" },
324 { "rx_xon_pause_rcvd" },
325 { "rx_xoff_pause_rcvd" },
326 { "rx_mac_ctrl_rcvd" },
327 { "rx_xoff_entered" },
328 { "rx_frame_too_long_errors" },
329 { "rx_jabbers" },
330 { "rx_undersize_packets" },
331 { "rx_in_length_errors" },
332 { "rx_out_length_errors" },
333 { "rx_64_or_less_octet_packets" },
334 { "rx_65_to_127_octet_packets" },
335 { "rx_128_to_255_octet_packets" },
336 { "rx_256_to_511_octet_packets" },
337 { "rx_512_to_1023_octet_packets" },
338 { "rx_1024_to_1522_octet_packets" },
339 { "rx_1523_to_2047_octet_packets" },
340 { "rx_2048_to_4095_octet_packets" },
341 { "rx_4096_to_8191_octet_packets" },
342 { "rx_8192_to_9022_octet_packets" },
343
344 { "tx_octets" },
345 { "tx_collisions" },
346
347 { "tx_xon_sent" },
348 { "tx_xoff_sent" },
349 { "tx_flow_control" },
350 { "tx_mac_errors" },
351 { "tx_single_collisions" },
352 { "tx_mult_collisions" },
353 { "tx_deferred" },
354 { "tx_excessive_collisions" },
355 { "tx_late_collisions" },
356 { "tx_collide_2times" },
357 { "tx_collide_3times" },
358 { "tx_collide_4times" },
359 { "tx_collide_5times" },
360 { "tx_collide_6times" },
361 { "tx_collide_7times" },
362 { "tx_collide_8times" },
363 { "tx_collide_9times" },
364 { "tx_collide_10times" },
365 { "tx_collide_11times" },
366 { "tx_collide_12times" },
367 { "tx_collide_13times" },
368 { "tx_collide_14times" },
369 { "tx_collide_15times" },
370 { "tx_ucast_packets" },
371 { "tx_mcast_packets" },
372 { "tx_bcast_packets" },
373 { "tx_carrier_sense_errors" },
374 { "tx_discards" },
375 { "tx_errors" },
376
377 { "dma_writeq_full" },
378 { "dma_write_prioq_full" },
379 { "rxbds_empty" },
380 { "rx_discards" },
381 { "rx_errors" },
382 { "rx_threshold_hit" },
383
384 { "dma_readq_full" },
385 { "dma_read_prioq_full" },
386 { "tx_comp_queue_full" },
387
388 { "ring_set_send_prod_index" },
389 { "ring_status_update" },
390 { "nic_irqs" },
391 { "nic_avoided_irqs" },
Matt Carlson4452d092011-05-19 12:12:51 +0000392 { "nic_tx_threshold_hit" },
393
394 { "mbuf_lwm_thresh_hit" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395};
396
Matt Carlson48fa55a2011-04-13 11:05:06 +0000397#define TG3_NUM_STATS ARRAY_SIZE(ethtool_stats_keys)
398
399
Andreas Mohr50da8592006-08-14 23:54:30 -0700400static const struct {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700401 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000402} ethtool_test_keys[] = {
Matt Carlson28a45952011-08-19 13:58:22 +0000403 { "nvram test (online) " },
404 { "link test (online) " },
405 { "register test (offline)" },
406 { "memory test (offline)" },
407 { "mac loopback test (offline)" },
408 { "phy loopback test (offline)" },
Matt Carlson941ec902011-08-19 13:58:23 +0000409 { "ext loopback test (offline)" },
Matt Carlson28a45952011-08-19 13:58:22 +0000410 { "interrupt test (offline)" },
Michael Chan4cafd3f2005-05-29 14:56:34 -0700411};
412
Matt Carlson48fa55a2011-04-13 11:05:06 +0000413#define TG3_NUM_TEST ARRAY_SIZE(ethtool_test_keys)
414
415
Michael Chanb401e9e2005-12-19 16:27:04 -0800416static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
417{
418 writel(val, tp->regs + off);
419}
420
421static u32 tg3_read32(struct tg3 *tp, u32 off)
422{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000423 return readl(tp->regs + off);
Michael Chanb401e9e2005-12-19 16:27:04 -0800424}
425
Matt Carlson0d3031d2007-10-10 18:02:43 -0700426static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
427{
428 writel(val, tp->aperegs + off);
429}
430
431static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
432{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000433 return readl(tp->aperegs + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700434}
435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
437{
Michael Chan68929142005-08-09 20:17:14 -0700438 unsigned long flags;
439
440 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700441 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
442 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
Michael Chan68929142005-08-09 20:17:14 -0700443 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700444}
445
446static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
447{
448 writel(val, tp->regs + off);
449 readl(tp->regs + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
Michael Chan68929142005-08-09 20:17:14 -0700452static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
453{
454 unsigned long flags;
455 u32 val;
456
457 spin_lock_irqsave(&tp->indirect_lock, flags);
458 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
459 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
460 spin_unlock_irqrestore(&tp->indirect_lock, flags);
461 return val;
462}
463
464static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
465{
466 unsigned long flags;
467
468 if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
469 pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
470 TG3_64BIT_REG_LOW, val);
471 return;
472 }
Matt Carlson66711e62009-11-13 13:03:49 +0000473 if (off == TG3_RX_STD_PROD_IDX_REG) {
Michael Chan68929142005-08-09 20:17:14 -0700474 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
475 TG3_64BIT_REG_LOW, val);
476 return;
477 }
478
479 spin_lock_irqsave(&tp->indirect_lock, flags);
480 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
481 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
482 spin_unlock_irqrestore(&tp->indirect_lock, flags);
483
484 /* In indirect mode when disabling interrupts, we also need
485 * to clear the interrupt bit in the GRC local ctrl register.
486 */
487 if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
488 (val == 0x1)) {
489 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
490 tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
491 }
492}
493
494static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
495{
496 unsigned long flags;
497 u32 val;
498
499 spin_lock_irqsave(&tp->indirect_lock, flags);
500 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
501 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
502 spin_unlock_irqrestore(&tp->indirect_lock, flags);
503 return val;
504}
505
Michael Chanb401e9e2005-12-19 16:27:04 -0800506/* usec_wait specifies the wait time in usec when writing to certain registers
507 * where it is unsafe to read back the register without some delay.
508 * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
509 * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
510 */
511static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
Joe Perches63c3a662011-04-26 08:12:10 +0000513 if (tg3_flag(tp, PCIX_TARGET_HWBUG) || tg3_flag(tp, ICH_WORKAROUND))
Michael Chanb401e9e2005-12-19 16:27:04 -0800514 /* Non-posted methods */
515 tp->write32(tp, off, val);
516 else {
517 /* Posted method */
518 tg3_write32(tp, off, val);
519 if (usec_wait)
520 udelay(usec_wait);
521 tp->read32(tp, off);
522 }
523 /* Wait again after the read for the posted method to guarantee that
524 * the wait time is met.
525 */
526 if (usec_wait)
527 udelay(usec_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
Michael Chan09ee9292005-08-09 20:17:00 -0700530static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
531{
532 tp->write32_mbox(tp, off, val);
Joe Perches63c3a662011-04-26 08:12:10 +0000533 if (!tg3_flag(tp, MBOX_WRITE_REORDER) && !tg3_flag(tp, ICH_WORKAROUND))
Michael Chan68929142005-08-09 20:17:14 -0700534 tp->read32_mbox(tp, off);
Michael Chan09ee9292005-08-09 20:17:00 -0700535}
536
Michael Chan20094932005-08-09 20:16:32 -0700537static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
539 void __iomem *mbox = tp->regs + off;
540 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000541 if (tg3_flag(tp, TXD_MBOX_HWBUG))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000543 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 readl(mbox);
545}
546
Michael Chanb5d37722006-09-27 16:06:21 -0700547static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
548{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000549 return readl(tp->regs + off + GRCMBOX_BASE);
Michael Chanb5d37722006-09-27 16:06:21 -0700550}
551
552static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
553{
554 writel(val, tp->regs + off + GRCMBOX_BASE);
555}
556
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000557#define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700558#define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000559#define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
560#define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
561#define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
Michael Chan20094932005-08-09 20:16:32 -0700562
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000563#define tw32(reg, val) tp->write32(tp, reg, val)
564#define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
565#define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
566#define tr32(reg) tp->read32(tp, reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
569{
Michael Chan68929142005-08-09 20:17:14 -0700570 unsigned long flags;
571
Matt Carlson6ff6f812011-05-19 12:12:54 +0000572 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700573 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
574 return;
575
Michael Chan68929142005-08-09 20:17:14 -0700576 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000577 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700578 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
579 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Michael Chanbbadf502006-04-06 21:46:34 -0700581 /* Always leave this as zero. */
582 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
583 } else {
584 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
585 tw32_f(TG3PCI_MEM_WIN_DATA, val);
586
587 /* Always leave this as zero. */
588 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
589 }
Michael Chan68929142005-08-09 20:17:14 -0700590 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592
593static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
594{
Michael Chan68929142005-08-09 20:17:14 -0700595 unsigned long flags;
596
Matt Carlson6ff6f812011-05-19 12:12:54 +0000597 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700598 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
599 *val = 0;
600 return;
601 }
602
Michael Chan68929142005-08-09 20:17:14 -0700603 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000604 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700605 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
606 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Michael Chanbbadf502006-04-06 21:46:34 -0700608 /* Always leave this as zero. */
609 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
610 } else {
611 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
612 *val = tr32(TG3PCI_MEM_WIN_DATA);
613
614 /* Always leave this as zero. */
615 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
616 }
Michael Chan68929142005-08-09 20:17:14 -0700617 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618}
619
Matt Carlson0d3031d2007-10-10 18:02:43 -0700620static void tg3_ape_lock_init(struct tg3 *tp)
621{
622 int i;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000623 u32 regbase, bit;
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000624
625 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
626 regbase = TG3_APE_LOCK_GRANT;
627 else
628 regbase = TG3_APE_PER_LOCK_GRANT;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700629
630 /* Make sure the driver hasn't any stale locks. */
Matt Carlson78f94dc2011-11-04 09:14:58 +0000631 for (i = TG3_APE_LOCK_PHY0; i <= TG3_APE_LOCK_GPIO; i++) {
632 switch (i) {
633 case TG3_APE_LOCK_PHY0:
634 case TG3_APE_LOCK_PHY1:
635 case TG3_APE_LOCK_PHY2:
636 case TG3_APE_LOCK_PHY3:
637 bit = APE_LOCK_GRANT_DRIVER;
638 break;
639 default:
640 if (!tp->pci_fn)
641 bit = APE_LOCK_GRANT_DRIVER;
642 else
643 bit = 1 << tp->pci_fn;
644 }
645 tg3_ape_write32(tp, regbase + 4 * i, bit);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000646 }
647
Matt Carlson0d3031d2007-10-10 18:02:43 -0700648}
649
650static int tg3_ape_lock(struct tg3 *tp, int locknum)
651{
652 int i, off;
653 int ret = 0;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000654 u32 status, req, gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700655
Joe Perches63c3a662011-04-26 08:12:10 +0000656 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700657 return 0;
658
659 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000660 case TG3_APE_LOCK_GPIO:
661 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
662 return 0;
Matt Carlson33f401a2010-04-05 10:19:27 +0000663 case TG3_APE_LOCK_GRC:
664 case TG3_APE_LOCK_MEM:
Matt Carlson78f94dc2011-11-04 09:14:58 +0000665 if (!tp->pci_fn)
666 bit = APE_LOCK_REQ_DRIVER;
667 else
668 bit = 1 << tp->pci_fn;
Matt Carlson33f401a2010-04-05 10:19:27 +0000669 break;
670 default:
671 return -EINVAL;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700672 }
673
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000674 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
675 req = TG3_APE_LOCK_REQ;
676 gnt = TG3_APE_LOCK_GRANT;
677 } else {
678 req = TG3_APE_PER_LOCK_REQ;
679 gnt = TG3_APE_PER_LOCK_GRANT;
680 }
681
Matt Carlson0d3031d2007-10-10 18:02:43 -0700682 off = 4 * locknum;
683
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000684 tg3_ape_write32(tp, req + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700685
686 /* Wait for up to 1 millisecond to acquire lock. */
687 for (i = 0; i < 100; i++) {
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000688 status = tg3_ape_read32(tp, gnt + off);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000689 if (status == bit)
Matt Carlson0d3031d2007-10-10 18:02:43 -0700690 break;
691 udelay(10);
692 }
693
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000694 if (status != bit) {
Matt Carlson0d3031d2007-10-10 18:02:43 -0700695 /* Revoke the lock request. */
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000696 tg3_ape_write32(tp, gnt + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700697 ret = -EBUSY;
698 }
699
700 return ret;
701}
702
703static void tg3_ape_unlock(struct tg3 *tp, int locknum)
704{
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000705 u32 gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700706
Joe Perches63c3a662011-04-26 08:12:10 +0000707 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700708 return;
709
710 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000711 case TG3_APE_LOCK_GPIO:
712 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
713 return;
Matt Carlson33f401a2010-04-05 10:19:27 +0000714 case TG3_APE_LOCK_GRC:
715 case TG3_APE_LOCK_MEM:
Matt Carlson78f94dc2011-11-04 09:14:58 +0000716 if (!tp->pci_fn)
717 bit = APE_LOCK_GRANT_DRIVER;
718 else
719 bit = 1 << tp->pci_fn;
Matt Carlson33f401a2010-04-05 10:19:27 +0000720 break;
721 default:
722 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700723 }
724
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000725 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
726 gnt = TG3_APE_LOCK_GRANT;
727 else
728 gnt = TG3_APE_PER_LOCK_GRANT;
729
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000730 tg3_ape_write32(tp, gnt + 4 * locknum, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700731}
732
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000733static void tg3_ape_send_event(struct tg3 *tp, u32 event)
734{
735 int i;
736 u32 apedata;
737
738 /* NCSI does not support APE events */
739 if (tg3_flag(tp, APE_HAS_NCSI))
740 return;
741
742 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
743 if (apedata != APE_SEG_SIG_MAGIC)
744 return;
745
746 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
747 if (!(apedata & APE_FW_STATUS_READY))
748 return;
749
750 /* Wait for up to 1 millisecond for APE to service previous event. */
751 for (i = 0; i < 10; i++) {
752 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
753 return;
754
755 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
756
757 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
758 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
759 event | APE_EVENT_STATUS_EVENT_PENDING);
760
761 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
762
763 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
764 break;
765
766 udelay(100);
767 }
768
769 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
770 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
771}
772
773static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
774{
775 u32 event;
776 u32 apedata;
777
778 if (!tg3_flag(tp, ENABLE_APE))
779 return;
780
781 switch (kind) {
782 case RESET_KIND_INIT:
783 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
784 APE_HOST_SEG_SIG_MAGIC);
785 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
786 APE_HOST_SEG_LEN_MAGIC);
787 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
788 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
789 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
790 APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
791 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
792 APE_HOST_BEHAV_NO_PHYLOCK);
793 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
794 TG3_APE_HOST_DRVR_STATE_START);
795
796 event = APE_EVENT_STATUS_STATE_START;
797 break;
798 case RESET_KIND_SHUTDOWN:
799 /* With the interface we are currently using,
800 * APE does not track driver state. Wiping
801 * out the HOST SEGMENT SIGNATURE forces
802 * the APE to assume OS absent status.
803 */
804 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
805
806 if (device_may_wakeup(&tp->pdev->dev) &&
807 tg3_flag(tp, WOL_ENABLE)) {
808 tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
809 TG3_APE_HOST_WOL_SPEED_AUTO);
810 apedata = TG3_APE_HOST_DRVR_STATE_WOL;
811 } else
812 apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
813
814 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
815
816 event = APE_EVENT_STATUS_STATE_UNLOAD;
817 break;
818 case RESET_KIND_SUSPEND:
819 event = APE_EVENT_STATUS_STATE_SUSPEND;
820 break;
821 default:
822 return;
823 }
824
825 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
826
827 tg3_ape_send_event(tp, event);
828}
829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830static void tg3_disable_ints(struct tg3 *tp)
831{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000832 int i;
833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 tw32(TG3PCI_MISC_HOST_CTRL,
835 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000836 for (i = 0; i < tp->irq_max; i++)
837 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838}
839
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840static void tg3_enable_ints(struct tg3 *tp)
841{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000842 int i;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000843
Michael Chanbbe832c2005-06-24 20:20:04 -0700844 tp->irq_sync = 0;
845 wmb();
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 tw32(TG3PCI_MISC_HOST_CTRL,
848 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000849
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000850 tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000851 for (i = 0; i < tp->irq_cnt; i++) {
852 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000853
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000854 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
Joe Perches63c3a662011-04-26 08:12:10 +0000855 if (tg3_flag(tp, 1SHOT_MSI))
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000856 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
857
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000858 tp->coal_now |= tnapi->coal_now;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000859 }
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000860
861 /* Force an initial interrupt */
Joe Perches63c3a662011-04-26 08:12:10 +0000862 if (!tg3_flag(tp, TAGGED_STATUS) &&
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000863 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
864 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
865 else
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000866 tw32(HOSTCC_MODE, tp->coal_now);
867
868 tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869}
870
Matt Carlson17375d22009-08-28 14:02:18 +0000871static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
Michael Chan04237dd2005-04-25 15:17:17 -0700872{
Matt Carlson17375d22009-08-28 14:02:18 +0000873 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +0000874 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan04237dd2005-04-25 15:17:17 -0700875 unsigned int work_exists = 0;
876
877 /* check for phy events */
Joe Perches63c3a662011-04-26 08:12:10 +0000878 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Michael Chan04237dd2005-04-25 15:17:17 -0700879 if (sblk->status & SD_STATUS_LINK_CHG)
880 work_exists = 1;
881 }
882 /* check for RX/TX work to do */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000883 if (sblk->idx[0].tx_consumer != tnapi->tx_cons ||
Matt Carlson8d9d7cf2009-09-01 13:19:05 +0000884 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -0700885 work_exists = 1;
886
887 return work_exists;
888}
889
Matt Carlson17375d22009-08-28 14:02:18 +0000890/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -0700891 * similar to tg3_enable_ints, but it accurately determines whether there
892 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400893 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 */
Matt Carlson17375d22009-08-28 14:02:18 +0000895static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
Matt Carlson17375d22009-08-28 14:02:18 +0000897 struct tg3 *tp = tnapi->tp;
898
Matt Carlson898a56f2009-08-28 14:02:40 +0000899 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 mmiowb();
901
David S. Millerfac9b832005-05-18 22:46:34 -0700902 /* When doing tagged status, this work check is unnecessary.
903 * The last_tag we write above tells the chip which piece of
904 * work we've completed.
905 */
Joe Perches63c3a662011-04-26 08:12:10 +0000906 if (!tg3_flag(tp, TAGGED_STATUS) && tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -0700907 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +0000908 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909}
910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911static void tg3_switch_clocks(struct tg3 *tp)
912{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000913 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 u32 orig_clock_ctrl;
915
Joe Perches63c3a662011-04-26 08:12:10 +0000916 if (tg3_flag(tp, CPMU_PRESENT) || tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -0700917 return;
918
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000919 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
920
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 orig_clock_ctrl = clock_ctrl;
922 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
923 CLOCK_CTRL_CLKRUN_OENABLE |
924 0x1f);
925 tp->pci_clock_ctrl = clock_ctrl;
926
Joe Perches63c3a662011-04-26 08:12:10 +0000927 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800929 tw32_wait_f(TG3PCI_CLOCK_CTRL,
930 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 }
932 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800933 tw32_wait_f(TG3PCI_CLOCK_CTRL,
934 clock_ctrl |
935 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
936 40);
937 tw32_wait_f(TG3PCI_CLOCK_CTRL,
938 clock_ctrl | (CLOCK_CTRL_ALTCLK),
939 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 }
Michael Chanb401e9e2005-12-19 16:27:04 -0800941 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942}
943
944#define PHY_BUSY_LOOPS 5000
945
946static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
947{
948 u32 frame_val;
949 unsigned int loops;
950 int ret;
951
952 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
953 tw32_f(MAC_MI_MODE,
954 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
955 udelay(80);
956 }
957
958 *val = 0x0;
959
Matt Carlson882e9792009-09-01 13:21:36 +0000960 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 MI_COM_PHY_ADDR_MASK);
962 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
963 MI_COM_REG_ADDR_MASK);
964 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 tw32_f(MAC_MI_COM, frame_val);
967
968 loops = PHY_BUSY_LOOPS;
969 while (loops != 0) {
970 udelay(10);
971 frame_val = tr32(MAC_MI_COM);
972
973 if ((frame_val & MI_COM_BUSY) == 0) {
974 udelay(5);
975 frame_val = tr32(MAC_MI_COM);
976 break;
977 }
978 loops -= 1;
979 }
980
981 ret = -EBUSY;
982 if (loops != 0) {
983 *val = frame_val & MI_COM_DATA_MASK;
984 ret = 0;
985 }
986
987 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
988 tw32_f(MAC_MI_MODE, tp->mi_mode);
989 udelay(80);
990 }
991
992 return ret;
993}
994
995static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
996{
997 u32 frame_val;
998 unsigned int loops;
999 int ret;
1000
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001001 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson221c5632011-06-13 13:39:01 +00001002 (reg == MII_CTRL1000 || reg == MII_TG3_AUX_CTRL))
Michael Chanb5d37722006-09-27 16:06:21 -07001003 return 0;
1004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1006 tw32_f(MAC_MI_MODE,
1007 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
1008 udelay(80);
1009 }
1010
Matt Carlson882e9792009-09-01 13:21:36 +00001011 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 MI_COM_PHY_ADDR_MASK);
1013 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
1014 MI_COM_REG_ADDR_MASK);
1015 frame_val |= (val & MI_COM_DATA_MASK);
1016 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 tw32_f(MAC_MI_COM, frame_val);
1019
1020 loops = PHY_BUSY_LOOPS;
1021 while (loops != 0) {
1022 udelay(10);
1023 frame_val = tr32(MAC_MI_COM);
1024 if ((frame_val & MI_COM_BUSY) == 0) {
1025 udelay(5);
1026 frame_val = tr32(MAC_MI_COM);
1027 break;
1028 }
1029 loops -= 1;
1030 }
1031
1032 ret = -EBUSY;
1033 if (loops != 0)
1034 ret = 0;
1035
1036 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1037 tw32_f(MAC_MI_MODE, tp->mi_mode);
1038 udelay(80);
1039 }
1040
1041 return ret;
1042}
1043
Matt Carlsonb0988c12011-04-20 07:57:39 +00001044static int tg3_phy_cl45_write(struct tg3 *tp, u32 devad, u32 addr, u32 val)
1045{
1046 int err;
1047
1048 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1049 if (err)
1050 goto done;
1051
1052 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1053 if (err)
1054 goto done;
1055
1056 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1057 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1058 if (err)
1059 goto done;
1060
1061 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, val);
1062
1063done:
1064 return err;
1065}
1066
1067static int tg3_phy_cl45_read(struct tg3 *tp, u32 devad, u32 addr, u32 *val)
1068{
1069 int err;
1070
1071 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1072 if (err)
1073 goto done;
1074
1075 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1076 if (err)
1077 goto done;
1078
1079 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1080 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1081 if (err)
1082 goto done;
1083
1084 err = tg3_readphy(tp, MII_TG3_MMD_ADDRESS, val);
1085
1086done:
1087 return err;
1088}
1089
1090static int tg3_phydsp_read(struct tg3 *tp, u32 reg, u32 *val)
1091{
1092 int err;
1093
1094 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1095 if (!err)
1096 err = tg3_readphy(tp, MII_TG3_DSP_RW_PORT, val);
1097
1098 return err;
1099}
1100
1101static int tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
1102{
1103 int err;
1104
1105 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1106 if (!err)
1107 err = tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
1108
1109 return err;
1110}
1111
Matt Carlson15ee95c2011-04-20 07:57:40 +00001112static int tg3_phy_auxctl_read(struct tg3 *tp, int reg, u32 *val)
1113{
1114 int err;
1115
1116 err = tg3_writephy(tp, MII_TG3_AUX_CTRL,
1117 (reg << MII_TG3_AUXCTL_MISC_RDSEL_SHIFT) |
1118 MII_TG3_AUXCTL_SHDWSEL_MISC);
1119 if (!err)
1120 err = tg3_readphy(tp, MII_TG3_AUX_CTRL, val);
1121
1122 return err;
1123}
1124
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001125static int tg3_phy_auxctl_write(struct tg3 *tp, int reg, u32 set)
1126{
1127 if (reg == MII_TG3_AUXCTL_SHDWSEL_MISC)
1128 set |= MII_TG3_AUXCTL_MISC_WREN;
1129
1130 return tg3_writephy(tp, MII_TG3_AUX_CTRL, set | reg);
1131}
1132
Matt Carlson1d36ba42011-04-20 07:57:42 +00001133#define TG3_PHY_AUXCTL_SMDSP_ENABLE(tp) \
1134 tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
1135 MII_TG3_AUXCTL_ACTL_SMDSP_ENA | \
1136 MII_TG3_AUXCTL_ACTL_TX_6DB)
1137
1138#define TG3_PHY_AUXCTL_SMDSP_DISABLE(tp) \
1139 tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
1140 MII_TG3_AUXCTL_ACTL_TX_6DB);
1141
Matt Carlson95e28692008-05-25 23:44:14 -07001142static int tg3_bmcr_reset(struct tg3 *tp)
1143{
1144 u32 phy_control;
1145 int limit, err;
1146
1147 /* OK, reset it, and poll the BMCR_RESET bit until it
1148 * clears or we time out.
1149 */
1150 phy_control = BMCR_RESET;
1151 err = tg3_writephy(tp, MII_BMCR, phy_control);
1152 if (err != 0)
1153 return -EBUSY;
1154
1155 limit = 5000;
1156 while (limit--) {
1157 err = tg3_readphy(tp, MII_BMCR, &phy_control);
1158 if (err != 0)
1159 return -EBUSY;
1160
1161 if ((phy_control & BMCR_RESET) == 0) {
1162 udelay(40);
1163 break;
1164 }
1165 udelay(10);
1166 }
Roel Kluind4675b52009-02-12 16:33:27 -08001167 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -07001168 return -EBUSY;
1169
1170 return 0;
1171}
1172
Matt Carlson158d7ab2008-05-29 01:37:54 -07001173static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
1174{
Francois Romieu3d165432009-01-19 16:56:50 -08001175 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001176 u32 val;
1177
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001178 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001179
1180 if (tg3_readphy(tp, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001181 val = -EIO;
1182
1183 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001184
1185 return val;
1186}
1187
1188static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
1189{
Francois Romieu3d165432009-01-19 16:56:50 -08001190 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001191 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001192
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001193 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001194
1195 if (tg3_writephy(tp, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001196 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001197
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001198 spin_unlock_bh(&tp->lock);
1199
1200 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001201}
1202
1203static int tg3_mdio_reset(struct mii_bus *bp)
1204{
1205 return 0;
1206}
1207
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001208static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -07001209{
1210 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001211 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -07001212
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001213 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001214 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001215 case PHY_ID_BCM50610:
1216 case PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001217 val = MAC_PHYCFG2_50610_LED_MODES;
1218 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001219 case PHY_ID_BCMAC131:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001220 val = MAC_PHYCFG2_AC131_LED_MODES;
1221 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001222 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001223 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
1224 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001225 case PHY_ID_RTL8201E:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001226 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
1227 break;
1228 default:
Matt Carlsona9daf362008-05-25 23:49:44 -07001229 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001230 }
1231
1232 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
1233 tw32(MAC_PHYCFG2, val);
1234
1235 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001236 val &= ~(MAC_PHYCFG1_RGMII_INT |
1237 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
1238 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001239 tw32(MAC_PHYCFG1, val);
1240
1241 return;
1242 }
1243
Joe Perches63c3a662011-04-26 08:12:10 +00001244 if (!tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001245 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
1246 MAC_PHYCFG2_FMODE_MASK_MASK |
1247 MAC_PHYCFG2_GMODE_MASK_MASK |
1248 MAC_PHYCFG2_ACT_MASK_MASK |
1249 MAC_PHYCFG2_QUAL_MASK_MASK |
1250 MAC_PHYCFG2_INBAND_ENABLE;
1251
1252 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001253
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001254 val = tr32(MAC_PHYCFG1);
1255 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
1256 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
Joe Perches63c3a662011-04-26 08:12:10 +00001257 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1258 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001259 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
Joe Perches63c3a662011-04-26 08:12:10 +00001260 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001261 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1262 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001263 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1264 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1265 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001266
Matt Carlsona9daf362008-05-25 23:49:44 -07001267 val = tr32(MAC_EXT_RGMII_MODE);
1268 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1269 MAC_RGMII_MODE_RX_QUALITY |
1270 MAC_RGMII_MODE_RX_ACTIVITY |
1271 MAC_RGMII_MODE_RX_ENG_DET |
1272 MAC_RGMII_MODE_TX_ENABLE |
1273 MAC_RGMII_MODE_TX_LOWPWR |
1274 MAC_RGMII_MODE_TX_RESET);
Joe Perches63c3a662011-04-26 08:12:10 +00001275 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1276 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001277 val |= MAC_RGMII_MODE_RX_INT_B |
1278 MAC_RGMII_MODE_RX_QUALITY |
1279 MAC_RGMII_MODE_RX_ACTIVITY |
1280 MAC_RGMII_MODE_RX_ENG_DET;
Joe Perches63c3a662011-04-26 08:12:10 +00001281 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001282 val |= MAC_RGMII_MODE_TX_ENABLE |
1283 MAC_RGMII_MODE_TX_LOWPWR |
1284 MAC_RGMII_MODE_TX_RESET;
1285 }
1286 tw32(MAC_EXT_RGMII_MODE, val);
1287}
1288
Matt Carlson158d7ab2008-05-29 01:37:54 -07001289static void tg3_mdio_start(struct tg3 *tp)
1290{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001291 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1292 tw32_f(MAC_MI_MODE, tp->mi_mode);
1293 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001294
Joe Perches63c3a662011-04-26 08:12:10 +00001295 if (tg3_flag(tp, MDIOBUS_INITED) &&
Matt Carlson9ea48182010-02-17 15:17:01 +00001296 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1297 tg3_mdio_config_5785(tp);
1298}
1299
1300static int tg3_mdio_init(struct tg3 *tp)
1301{
1302 int i;
1303 u32 reg;
1304 struct phy_device *phydev;
1305
Joe Perches63c3a662011-04-26 08:12:10 +00001306 if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson9c7df912010-06-05 17:24:36 +00001307 u32 is_serdes;
Matt Carlson882e9792009-09-01 13:21:36 +00001308
Matt Carlson69f11c92011-07-13 09:27:30 +00001309 tp->phy_addr = tp->pci_fn + 1;
Matt Carlson882e9792009-09-01 13:21:36 +00001310
Matt Carlsond1ec96a2010-01-12 10:11:38 +00001311 if (tp->pci_chip_rev_id != CHIPREV_ID_5717_A0)
1312 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1313 else
1314 is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1315 TG3_CPMU_PHY_STRAP_IS_SERDES;
Matt Carlson882e9792009-09-01 13:21:36 +00001316 if (is_serdes)
1317 tp->phy_addr += 7;
1318 } else
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001319 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001320
Matt Carlson158d7ab2008-05-29 01:37:54 -07001321 tg3_mdio_start(tp);
1322
Joe Perches63c3a662011-04-26 08:12:10 +00001323 if (!tg3_flag(tp, USE_PHYLIB) || tg3_flag(tp, MDIOBUS_INITED))
Matt Carlson158d7ab2008-05-29 01:37:54 -07001324 return 0;
1325
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001326 tp->mdio_bus = mdiobus_alloc();
1327 if (tp->mdio_bus == NULL)
1328 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001329
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001330 tp->mdio_bus->name = "tg3 mdio bus";
1331 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001332 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001333 tp->mdio_bus->priv = tp;
1334 tp->mdio_bus->parent = &tp->pdev->dev;
1335 tp->mdio_bus->read = &tg3_mdio_read;
1336 tp->mdio_bus->write = &tg3_mdio_write;
1337 tp->mdio_bus->reset = &tg3_mdio_reset;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001338 tp->mdio_bus->phy_mask = ~(1 << TG3_PHY_MII_ADDR);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001339 tp->mdio_bus->irq = &tp->mdio_irq[0];
Matt Carlson158d7ab2008-05-29 01:37:54 -07001340
1341 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001342 tp->mdio_bus->irq[i] = PHY_POLL;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001343
1344 /* The bus registration will look for all the PHYs on the mdio bus.
1345 * Unfortunately, it does not ensure the PHY is powered up before
1346 * accessing the PHY ID registers. A chip reset is the
1347 * quickest way to bring the device back to an operational state..
1348 */
1349 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1350 tg3_bmcr_reset(tp);
1351
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001352 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001353 if (i) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001354 dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001355 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001356 return i;
1357 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001358
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001359 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsona9daf362008-05-25 23:49:44 -07001360
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001361 if (!phydev || !phydev->drv) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001362 dev_warn(&tp->pdev->dev, "No PHY devices\n");
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001363 mdiobus_unregister(tp->mdio_bus);
1364 mdiobus_free(tp->mdio_bus);
1365 return -ENODEV;
1366 }
1367
1368 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001369 case PHY_ID_BCM57780:
Matt Carlson321d32a2008-11-21 17:22:19 -08001370 phydev->interface = PHY_INTERFACE_MODE_GMII;
Matt Carlsonc704dc22009-11-02 14:32:12 +00001371 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08001372 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001373 case PHY_ID_BCM50610:
1374 case PHY_ID_BCM50610M:
Matt Carlson32e5a8d2009-11-02 14:31:39 +00001375 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001376 PHY_BRCM_RX_REFCLK_UNUSED |
Matt Carlson52fae082009-11-02 14:32:38 +00001377 PHY_BRCM_DIS_TXCRXC_NOENRGY |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001378 PHY_BRCM_AUTO_PWRDWN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001379 if (tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsona9daf362008-05-25 23:49:44 -07001380 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001381 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001382 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001383 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001384 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001385 /* fallthru */
Matt Carlson6a443a02010-02-17 15:17:04 +00001386 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001387 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001388 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001389 case PHY_ID_RTL8201E:
1390 case PHY_ID_BCMAC131:
Matt Carlsona9daf362008-05-25 23:49:44 -07001391 phydev->interface = PHY_INTERFACE_MODE_MII;
Matt Carlsoncdd4e09d2009-11-02 14:31:11 +00001392 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001393 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlsona9daf362008-05-25 23:49:44 -07001394 break;
1395 }
1396
Joe Perches63c3a662011-04-26 08:12:10 +00001397 tg3_flag_set(tp, MDIOBUS_INITED);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001398
1399 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1400 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001401
1402 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001403}
1404
1405static void tg3_mdio_fini(struct tg3 *tp)
1406{
Joe Perches63c3a662011-04-26 08:12:10 +00001407 if (tg3_flag(tp, MDIOBUS_INITED)) {
1408 tg3_flag_clear(tp, MDIOBUS_INITED);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001409 mdiobus_unregister(tp->mdio_bus);
1410 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001411 }
1412}
1413
Matt Carlson95e28692008-05-25 23:44:14 -07001414/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001415static inline void tg3_generate_fw_event(struct tg3 *tp)
1416{
1417 u32 val;
1418
1419 val = tr32(GRC_RX_CPU_EVENT);
1420 val |= GRC_RX_CPU_DRIVER_EVENT;
1421 tw32_f(GRC_RX_CPU_EVENT, val);
1422
1423 tp->last_event_jiffies = jiffies;
1424}
1425
1426#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1427
1428/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001429static void tg3_wait_for_event_ack(struct tg3 *tp)
1430{
1431 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001432 unsigned int delay_cnt;
1433 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001434
Matt Carlson4ba526c2008-08-15 14:10:04 -07001435 /* If enough time has passed, no wait is necessary. */
1436 time_remain = (long)(tp->last_event_jiffies + 1 +
1437 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1438 (long)jiffies;
1439 if (time_remain < 0)
1440 return;
1441
1442 /* Check if we can shorten the wait time. */
1443 delay_cnt = jiffies_to_usecs(time_remain);
1444 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1445 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1446 delay_cnt = (delay_cnt >> 3) + 1;
1447
1448 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001449 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1450 break;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001451 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001452 }
1453}
1454
1455/* tp->lock is held. */
1456static void tg3_ump_link_report(struct tg3 *tp)
1457{
1458 u32 reg;
1459 u32 val;
1460
Joe Perches63c3a662011-04-26 08:12:10 +00001461 if (!tg3_flag(tp, 5780_CLASS) || !tg3_flag(tp, ENABLE_ASF))
Matt Carlson95e28692008-05-25 23:44:14 -07001462 return;
1463
1464 tg3_wait_for_event_ack(tp);
1465
1466 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1467
1468 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1469
1470 val = 0;
1471 if (!tg3_readphy(tp, MII_BMCR, &reg))
1472 val = reg << 16;
1473 if (!tg3_readphy(tp, MII_BMSR, &reg))
1474 val |= (reg & 0xffff);
1475 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX, val);
1476
1477 val = 0;
1478 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1479 val = reg << 16;
1480 if (!tg3_readphy(tp, MII_LPA, &reg))
1481 val |= (reg & 0xffff);
1482 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 4, val);
1483
1484 val = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001485 if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
Matt Carlson95e28692008-05-25 23:44:14 -07001486 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1487 val = reg << 16;
1488 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1489 val |= (reg & 0xffff);
1490 }
1491 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 8, val);
1492
1493 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1494 val = reg << 16;
1495 else
1496 val = 0;
1497 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 12, val);
1498
Matt Carlson4ba526c2008-08-15 14:10:04 -07001499 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001500}
1501
Matt Carlson8d5a89b2011-08-31 11:44:51 +00001502/* tp->lock is held. */
1503static void tg3_stop_fw(struct tg3 *tp)
1504{
1505 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
1506 /* Wait for RX cpu to ACK the previous event. */
1507 tg3_wait_for_event_ack(tp);
1508
1509 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
1510
1511 tg3_generate_fw_event(tp);
1512
1513 /* Wait for RX cpu to ACK this event. */
1514 tg3_wait_for_event_ack(tp);
1515 }
1516}
1517
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001518/* tp->lock is held. */
1519static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
1520{
1521 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
1522 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
1523
1524 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1525 switch (kind) {
1526 case RESET_KIND_INIT:
1527 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1528 DRV_STATE_START);
1529 break;
1530
1531 case RESET_KIND_SHUTDOWN:
1532 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1533 DRV_STATE_UNLOAD);
1534 break;
1535
1536 case RESET_KIND_SUSPEND:
1537 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1538 DRV_STATE_SUSPEND);
1539 break;
1540
1541 default:
1542 break;
1543 }
1544 }
1545
1546 if (kind == RESET_KIND_INIT ||
1547 kind == RESET_KIND_SUSPEND)
1548 tg3_ape_driver_state_change(tp, kind);
1549}
1550
1551/* tp->lock is held. */
1552static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
1553{
1554 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1555 switch (kind) {
1556 case RESET_KIND_INIT:
1557 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1558 DRV_STATE_START_DONE);
1559 break;
1560
1561 case RESET_KIND_SHUTDOWN:
1562 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1563 DRV_STATE_UNLOAD_DONE);
1564 break;
1565
1566 default:
1567 break;
1568 }
1569 }
1570
1571 if (kind == RESET_KIND_SHUTDOWN)
1572 tg3_ape_driver_state_change(tp, kind);
1573}
1574
1575/* tp->lock is held. */
1576static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
1577{
1578 if (tg3_flag(tp, ENABLE_ASF)) {
1579 switch (kind) {
1580 case RESET_KIND_INIT:
1581 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1582 DRV_STATE_START);
1583 break;
1584
1585 case RESET_KIND_SHUTDOWN:
1586 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1587 DRV_STATE_UNLOAD);
1588 break;
1589
1590 case RESET_KIND_SUSPEND:
1591 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1592 DRV_STATE_SUSPEND);
1593 break;
1594
1595 default:
1596 break;
1597 }
1598 }
1599}
1600
1601static int tg3_poll_fw(struct tg3 *tp)
1602{
1603 int i;
1604 u32 val;
1605
1606 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
1607 /* Wait up to 20ms for init done. */
1608 for (i = 0; i < 200; i++) {
1609 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
1610 return 0;
1611 udelay(100);
1612 }
1613 return -ENODEV;
1614 }
1615
1616 /* Wait for firmware initialization to complete. */
1617 for (i = 0; i < 100000; i++) {
1618 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
1619 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
1620 break;
1621 udelay(10);
1622 }
1623
1624 /* Chip might not be fitted with firmware. Some Sun onboard
1625 * parts are configured like that. So don't signal the timeout
1626 * of the above loop as an error, but do report the lack of
1627 * running firmware once.
1628 */
1629 if (i >= 100000 && !tg3_flag(tp, NO_FWARE_REPORTED)) {
1630 tg3_flag_set(tp, NO_FWARE_REPORTED);
1631
1632 netdev_info(tp->dev, "No firmware running\n");
1633 }
1634
1635 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
1636 /* The 57765 A0 needs a little more
1637 * time to do some important work.
1638 */
1639 mdelay(10);
1640 }
1641
1642 return 0;
1643}
1644
Matt Carlson95e28692008-05-25 23:44:14 -07001645static void tg3_link_report(struct tg3 *tp)
1646{
1647 if (!netif_carrier_ok(tp->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001648 netif_info(tp, link, tp->dev, "Link is down\n");
Matt Carlson95e28692008-05-25 23:44:14 -07001649 tg3_ump_link_report(tp);
1650 } else if (netif_msg_link(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001651 netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
1652 (tp->link_config.active_speed == SPEED_1000 ?
1653 1000 :
1654 (tp->link_config.active_speed == SPEED_100 ?
1655 100 : 10)),
1656 (tp->link_config.active_duplex == DUPLEX_FULL ?
1657 "full" : "half"));
Matt Carlson95e28692008-05-25 23:44:14 -07001658
Joe Perches05dbe002010-02-17 19:44:19 +00001659 netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
1660 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
1661 "on" : "off",
1662 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
1663 "on" : "off");
Matt Carlson47007832011-04-20 07:57:43 +00001664
1665 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
1666 netdev_info(tp->dev, "EEE is %s\n",
1667 tp->setlpicnt ? "enabled" : "disabled");
1668
Matt Carlson95e28692008-05-25 23:44:14 -07001669 tg3_ump_link_report(tp);
1670 }
1671}
1672
Matt Carlson95e28692008-05-25 23:44:14 -07001673static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1674{
1675 u16 miireg;
1676
Steve Glendinninge18ce342008-12-16 02:00:00 -08001677 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001678 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001679 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001680 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001681 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001682 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1683 else
1684 miireg = 0;
1685
1686 return miireg;
1687}
1688
Matt Carlson95e28692008-05-25 23:44:14 -07001689static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1690{
1691 u8 cap = 0;
1692
Matt Carlsonf3791cd2011-11-21 15:01:17 +00001693 if (lcladv & rmtadv & ADVERTISE_1000XPAUSE) {
1694 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
1695 } else if (lcladv & rmtadv & ADVERTISE_1000XPSE_ASYM) {
1696 if (lcladv & ADVERTISE_1000XPAUSE)
1697 cap = FLOW_CTRL_RX;
1698 if (rmtadv & ADVERTISE_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001699 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001700 }
1701
1702 return cap;
1703}
1704
Matt Carlsonf51f3562008-05-25 23:45:08 -07001705static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001706{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001707 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001708 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001709 u32 old_rx_mode = tp->rx_mode;
1710 u32 old_tx_mode = tp->tx_mode;
1711
Joe Perches63c3a662011-04-26 08:12:10 +00001712 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001713 autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001714 else
1715 autoneg = tp->link_config.autoneg;
1716
Joe Perches63c3a662011-04-26 08:12:10 +00001717 if (autoneg == AUTONEG_ENABLE && tg3_flag(tp, PAUSE_AUTONEG)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001718 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001719 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001720 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001721 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001722 } else
1723 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001724
Matt Carlsonf51f3562008-05-25 23:45:08 -07001725 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001726
Steve Glendinninge18ce342008-12-16 02:00:00 -08001727 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001728 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1729 else
1730 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1731
Matt Carlsonf51f3562008-05-25 23:45:08 -07001732 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001733 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001734
Steve Glendinninge18ce342008-12-16 02:00:00 -08001735 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001736 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1737 else
1738 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1739
Matt Carlsonf51f3562008-05-25 23:45:08 -07001740 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001741 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001742}
1743
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001744static void tg3_adjust_link(struct net_device *dev)
1745{
1746 u8 oldflowctrl, linkmesg = 0;
1747 u32 mac_mode, lcl_adv, rmt_adv;
1748 struct tg3 *tp = netdev_priv(dev);
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001749 struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001750
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001751 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001752
1753 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
1754 MAC_MODE_HALF_DUPLEX);
1755
1756 oldflowctrl = tp->link_config.active_flowctrl;
1757
1758 if (phydev->link) {
1759 lcl_adv = 0;
1760 rmt_adv = 0;
1761
1762 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
1763 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001764 else if (phydev->speed == SPEED_1000 ||
1765 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001766 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001767 else
1768 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001769
1770 if (phydev->duplex == DUPLEX_HALF)
1771 mac_mode |= MAC_MODE_HALF_DUPLEX;
1772 else {
Matt Carlsonf88788f2011-12-14 11:10:00 +00001773 lcl_adv = mii_advertise_flowctrl(
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001774 tp->link_config.flowctrl);
1775
1776 if (phydev->pause)
1777 rmt_adv = LPA_PAUSE_CAP;
1778 if (phydev->asym_pause)
1779 rmt_adv |= LPA_PAUSE_ASYM;
1780 }
1781
1782 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
1783 } else
1784 mac_mode |= MAC_MODE_PORT_MODE_GMII;
1785
1786 if (mac_mode != tp->mac_mode) {
1787 tp->mac_mode = mac_mode;
1788 tw32_f(MAC_MODE, tp->mac_mode);
1789 udelay(40);
1790 }
1791
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001792 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
1793 if (phydev->speed == SPEED_10)
1794 tw32(MAC_MI_STAT,
1795 MAC_MI_STAT_10MBPS_MODE |
1796 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1797 else
1798 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1799 }
1800
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001801 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
1802 tw32(MAC_TX_LENGTHS,
1803 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1804 (6 << TX_LENGTHS_IPG_SHIFT) |
1805 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
1806 else
1807 tw32(MAC_TX_LENGTHS,
1808 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1809 (6 << TX_LENGTHS_IPG_SHIFT) |
1810 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
1811
1812 if ((phydev->link && tp->link_config.active_speed == SPEED_INVALID) ||
1813 (!phydev->link && tp->link_config.active_speed != SPEED_INVALID) ||
1814 phydev->speed != tp->link_config.active_speed ||
1815 phydev->duplex != tp->link_config.active_duplex ||
1816 oldflowctrl != tp->link_config.active_flowctrl)
Matt Carlsonc6cdf432010-04-05 10:19:26 +00001817 linkmesg = 1;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001818
1819 tp->link_config.active_speed = phydev->speed;
1820 tp->link_config.active_duplex = phydev->duplex;
1821
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001822 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001823
1824 if (linkmesg)
1825 tg3_link_report(tp);
1826}
1827
1828static int tg3_phy_init(struct tg3 *tp)
1829{
1830 struct phy_device *phydev;
1831
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001832 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001833 return 0;
1834
1835 /* Bring the PHY back to a known state. */
1836 tg3_bmcr_reset(tp);
1837
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001838 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001839
1840 /* Attach the MAC to the PHY. */
Kay Sieversfb28ad352008-11-10 13:55:14 -08001841 phydev = phy_connect(tp->dev, dev_name(&phydev->dev), tg3_adjust_link,
Matt Carlsona9daf362008-05-25 23:49:44 -07001842 phydev->dev_flags, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001843 if (IS_ERR(phydev)) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001844 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001845 return PTR_ERR(phydev);
1846 }
1847
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001848 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001849 switch (phydev->interface) {
1850 case PHY_INTERFACE_MODE_GMII:
1851 case PHY_INTERFACE_MODE_RGMII:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001852 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Matt Carlson321d32a2008-11-21 17:22:19 -08001853 phydev->supported &= (PHY_GBIT_FEATURES |
1854 SUPPORTED_Pause |
1855 SUPPORTED_Asym_Pause);
1856 break;
1857 }
1858 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001859 case PHY_INTERFACE_MODE_MII:
1860 phydev->supported &= (PHY_BASIC_FEATURES |
1861 SUPPORTED_Pause |
1862 SUPPORTED_Asym_Pause);
1863 break;
1864 default:
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001865 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001866 return -EINVAL;
1867 }
1868
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001869 tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001870
1871 phydev->advertising = phydev->supported;
1872
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001873 return 0;
1874}
1875
1876static void tg3_phy_start(struct tg3 *tp)
1877{
1878 struct phy_device *phydev;
1879
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001880 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001881 return;
1882
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001883 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001884
Matt Carlson80096062010-08-02 11:26:06 +00001885 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
1886 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001887 phydev->speed = tp->link_config.orig_speed;
1888 phydev->duplex = tp->link_config.orig_duplex;
1889 phydev->autoneg = tp->link_config.orig_autoneg;
1890 phydev->advertising = tp->link_config.orig_advertising;
1891 }
1892
1893 phy_start(phydev);
1894
1895 phy_start_aneg(phydev);
1896}
1897
1898static void tg3_phy_stop(struct tg3 *tp)
1899{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001900 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001901 return;
1902
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001903 phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001904}
1905
1906static void tg3_phy_fini(struct tg3 *tp)
1907{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001908 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001909 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001910 tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001911 }
1912}
1913
Matt Carlson941ec902011-08-19 13:58:23 +00001914static int tg3_phy_set_extloopbk(struct tg3 *tp)
1915{
1916 int err;
1917 u32 val;
1918
1919 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
1920 return 0;
1921
1922 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
1923 /* Cannot do read-modify-write on 5401 */
1924 err = tg3_phy_auxctl_write(tp,
1925 MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
1926 MII_TG3_AUXCTL_ACTL_EXTLOOPBK |
1927 0x4c20);
1928 goto done;
1929 }
1930
1931 err = tg3_phy_auxctl_read(tp,
1932 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
1933 if (err)
1934 return err;
1935
1936 val |= MII_TG3_AUXCTL_ACTL_EXTLOOPBK;
1937 err = tg3_phy_auxctl_write(tp,
1938 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, val);
1939
1940done:
1941 return err;
1942}
1943
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001944static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
1945{
1946 u32 phytest;
1947
1948 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
1949 u32 phy;
1950
1951 tg3_writephy(tp, MII_TG3_FET_TEST,
1952 phytest | MII_TG3_FET_SHADOW_EN);
1953 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
1954 if (enable)
1955 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
1956 else
1957 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
1958 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
1959 }
1960 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
1961 }
1962}
1963
Matt Carlson6833c042008-11-21 17:18:59 -08001964static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
1965{
1966 u32 reg;
1967
Joe Perches63c3a662011-04-26 08:12:10 +00001968 if (!tg3_flag(tp, 5705_PLUS) ||
1969 (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001970 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08001971 return;
1972
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001973 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001974 tg3_phy_fet_toggle_apd(tp, enable);
1975 return;
1976 }
1977
Matt Carlson6833c042008-11-21 17:18:59 -08001978 reg = MII_TG3_MISC_SHDW_WREN |
1979 MII_TG3_MISC_SHDW_SCR5_SEL |
1980 MII_TG3_MISC_SHDW_SCR5_LPED |
1981 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
1982 MII_TG3_MISC_SHDW_SCR5_SDTL |
1983 MII_TG3_MISC_SHDW_SCR5_C125OE;
1984 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 || !enable)
1985 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
1986
1987 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1988
1989
1990 reg = MII_TG3_MISC_SHDW_WREN |
1991 MII_TG3_MISC_SHDW_APD_SEL |
1992 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
1993 if (enable)
1994 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
1995
1996 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1997}
1998
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001999static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
2000{
2001 u32 phy;
2002
Joe Perches63c3a662011-04-26 08:12:10 +00002003 if (!tg3_flag(tp, 5705_PLUS) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002004 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002005 return;
2006
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002007 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002008 u32 ephy;
2009
Matt Carlson535ef6e2009-08-25 10:09:36 +00002010 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
2011 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
2012
2013 tg3_writephy(tp, MII_TG3_FET_TEST,
2014 ephy | MII_TG3_FET_SHADOW_EN);
2015 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002016 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00002017 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002018 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00002019 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
2020 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002021 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00002022 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002023 }
2024 } else {
Matt Carlson15ee95c2011-04-20 07:57:40 +00002025 int ret;
2026
2027 ret = tg3_phy_auxctl_read(tp,
2028 MII_TG3_AUXCTL_SHDWSEL_MISC, &phy);
2029 if (!ret) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002030 if (enable)
2031 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
2032 else
2033 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002034 tg3_phy_auxctl_write(tp,
2035 MII_TG3_AUXCTL_SHDWSEL_MISC, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002036 }
2037 }
2038}
2039
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040static void tg3_phy_set_wirespeed(struct tg3 *tp)
2041{
Matt Carlson15ee95c2011-04-20 07:57:40 +00002042 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 u32 val;
2044
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002045 if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 return;
2047
Matt Carlson15ee95c2011-04-20 07:57:40 +00002048 ret = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_MISC, &val);
2049 if (!ret)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002050 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_MISC,
2051 val | MII_TG3_AUXCTL_MISC_WIRESPD_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052}
2053
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002054static void tg3_phy_apply_otp(struct tg3 *tp)
2055{
2056 u32 otp, phy;
2057
2058 if (!tp->phy_otp)
2059 return;
2060
2061 otp = tp->phy_otp;
2062
Matt Carlson1d36ba42011-04-20 07:57:42 +00002063 if (TG3_PHY_AUXCTL_SMDSP_ENABLE(tp))
2064 return;
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002065
2066 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
2067 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
2068 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
2069
2070 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
2071 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
2072 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
2073
2074 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
2075 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
2076 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
2077
2078 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
2079 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
2080
2081 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
2082 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
2083
2084 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
2085 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
2086 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
2087
Matt Carlson1d36ba42011-04-20 07:57:42 +00002088 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002089}
2090
Matt Carlson52b02d02010-10-14 10:37:41 +00002091static void tg3_phy_eee_adjust(struct tg3 *tp, u32 current_link_up)
2092{
2093 u32 val;
2094
2095 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
2096 return;
2097
2098 tp->setlpicnt = 0;
2099
2100 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
2101 current_link_up == 1 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +00002102 tp->link_config.active_duplex == DUPLEX_FULL &&
2103 (tp->link_config.active_speed == SPEED_100 ||
2104 tp->link_config.active_speed == SPEED_1000)) {
Matt Carlson52b02d02010-10-14 10:37:41 +00002105 u32 eeectl;
2106
2107 if (tp->link_config.active_speed == SPEED_1000)
2108 eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
2109 else
2110 eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
2111
2112 tw32(TG3_CPMU_EEE_CTRL, eeectl);
2113
Matt Carlson3110f5f52010-12-06 08:28:50 +00002114 tg3_phy_cl45_read(tp, MDIO_MMD_AN,
2115 TG3_CL45_D7_EEERES_STAT, &val);
Matt Carlson52b02d02010-10-14 10:37:41 +00002116
Matt Carlsonb0c59432011-05-19 12:12:48 +00002117 if (val == TG3_CL45_D7_EEERES_STAT_LP_1000T ||
2118 val == TG3_CL45_D7_EEERES_STAT_LP_100TX)
Matt Carlson52b02d02010-10-14 10:37:41 +00002119 tp->setlpicnt = 2;
2120 }
2121
2122 if (!tp->setlpicnt) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002123 if (current_link_up == 1 &&
2124 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2125 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0000);
2126 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2127 }
2128
Matt Carlson52b02d02010-10-14 10:37:41 +00002129 val = tr32(TG3_CPMU_EEE_MODE);
2130 tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
2131 }
2132}
2133
Matt Carlsonb0c59432011-05-19 12:12:48 +00002134static void tg3_phy_eee_enable(struct tg3 *tp)
2135{
2136 u32 val;
2137
2138 if (tp->link_config.active_speed == SPEED_1000 &&
2139 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2140 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00002141 tg3_flag(tp, 57765_CLASS)) &&
Matt Carlsonb0c59432011-05-19 12:12:48 +00002142 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002143 val = MII_TG3_DSP_TAP26_ALNOKO |
2144 MII_TG3_DSP_TAP26_RMRXSTO;
2145 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
Matt Carlsonb0c59432011-05-19 12:12:48 +00002146 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2147 }
2148
2149 val = tr32(TG3_CPMU_EEE_MODE);
2150 tw32(TG3_CPMU_EEE_MODE, val | TG3_CPMU_EEEMD_LPI_ENABLE);
2151}
2152
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153static int tg3_wait_macro_done(struct tg3 *tp)
2154{
2155 int limit = 100;
2156
2157 while (limit--) {
2158 u32 tmp32;
2159
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002160 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 if ((tmp32 & 0x1000) == 0)
2162 break;
2163 }
2164 }
Roel Kluind4675b52009-02-12 16:33:27 -08002165 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 return -EBUSY;
2167
2168 return 0;
2169}
2170
2171static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
2172{
2173 static const u32 test_pat[4][6] = {
2174 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
2175 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
2176 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
2177 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
2178 };
2179 int chan;
2180
2181 for (chan = 0; chan < 4; chan++) {
2182 int i;
2183
2184 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2185 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002186 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
2188 for (i = 0; i < 6; i++)
2189 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
2190 test_pat[chan][i]);
2191
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002192 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 if (tg3_wait_macro_done(tp)) {
2194 *resetp = 1;
2195 return -EBUSY;
2196 }
2197
2198 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2199 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002200 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 if (tg3_wait_macro_done(tp)) {
2202 *resetp = 1;
2203 return -EBUSY;
2204 }
2205
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002206 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 if (tg3_wait_macro_done(tp)) {
2208 *resetp = 1;
2209 return -EBUSY;
2210 }
2211
2212 for (i = 0; i < 6; i += 2) {
2213 u32 low, high;
2214
2215 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
2216 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
2217 tg3_wait_macro_done(tp)) {
2218 *resetp = 1;
2219 return -EBUSY;
2220 }
2221 low &= 0x7fff;
2222 high &= 0x000f;
2223 if (low != test_pat[chan][i] ||
2224 high != test_pat[chan][i+1]) {
2225 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
2226 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
2227 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
2228
2229 return -EBUSY;
2230 }
2231 }
2232 }
2233
2234 return 0;
2235}
2236
2237static int tg3_phy_reset_chanpat(struct tg3 *tp)
2238{
2239 int chan;
2240
2241 for (chan = 0; chan < 4; chan++) {
2242 int i;
2243
2244 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2245 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002246 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 for (i = 0; i < 6; i++)
2248 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002249 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 if (tg3_wait_macro_done(tp))
2251 return -EBUSY;
2252 }
2253
2254 return 0;
2255}
2256
2257static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
2258{
2259 u32 reg32, phy9_orig;
2260 int retries, do_phy_reset, err;
2261
2262 retries = 10;
2263 do_phy_reset = 1;
2264 do {
2265 if (do_phy_reset) {
2266 err = tg3_bmcr_reset(tp);
2267 if (err)
2268 return err;
2269 do_phy_reset = 0;
2270 }
2271
2272 /* Disable transmitter and interrupt. */
2273 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
2274 continue;
2275
2276 reg32 |= 0x3000;
2277 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2278
2279 /* Set full-duplex, 1000 mbps. */
2280 tg3_writephy(tp, MII_BMCR,
Matt Carlson221c5632011-06-13 13:39:01 +00002281 BMCR_FULLDPLX | BMCR_SPEED1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
2283 /* Set to master mode. */
Matt Carlson221c5632011-06-13 13:39:01 +00002284 if (tg3_readphy(tp, MII_CTRL1000, &phy9_orig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 continue;
2286
Matt Carlson221c5632011-06-13 13:39:01 +00002287 tg3_writephy(tp, MII_CTRL1000,
2288 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289
Matt Carlson1d36ba42011-04-20 07:57:42 +00002290 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
2291 if (err)
2292 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
2294 /* Block the PHY control access. */
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002295 tg3_phydsp_write(tp, 0x8005, 0x0800);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296
2297 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
2298 if (!err)
2299 break;
2300 } while (--retries);
2301
2302 err = tg3_phy_reset_chanpat(tp);
2303 if (err)
2304 return err;
2305
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002306 tg3_phydsp_write(tp, 0x8005, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
2308 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002309 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
Matt Carlson1d36ba42011-04-20 07:57:42 +00002311 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
Matt Carlson221c5632011-06-13 13:39:01 +00002313 tg3_writephy(tp, MII_CTRL1000, phy9_orig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
2315 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
2316 reg32 &= ~0x3000;
2317 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2318 } else if (!err)
2319 err = -EBUSY;
2320
2321 return err;
2322}
2323
2324/* This will reset the tigon3 PHY if there is no valid
2325 * link unless the FORCE argument is non-zero.
2326 */
2327static int tg3_phy_reset(struct tg3 *tp)
2328{
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002329 u32 val, cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 int err;
2331
Michael Chan60189dd2006-12-17 17:08:07 -08002332 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002333 val = tr32(GRC_MISC_CFG);
2334 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
2335 udelay(40);
2336 }
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002337 err = tg3_readphy(tp, MII_BMSR, &val);
2338 err |= tg3_readphy(tp, MII_BMSR, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 if (err != 0)
2340 return -EBUSY;
2341
Michael Chanc8e1e822006-04-29 18:55:17 -07002342 if (netif_running(tp->dev) && netif_carrier_ok(tp->dev)) {
2343 netif_carrier_off(tp->dev);
2344 tg3_link_report(tp);
2345 }
2346
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2348 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2349 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
2350 err = tg3_phy_reset_5703_4_5(tp);
2351 if (err)
2352 return err;
2353 goto out;
2354 }
2355
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002356 cpmuctrl = 0;
2357 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
2358 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
2359 cpmuctrl = tr32(TG3_CPMU_CTRL);
2360 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
2361 tw32(TG3_CPMU_CTRL,
2362 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
2363 }
2364
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 err = tg3_bmcr_reset(tp);
2366 if (err)
2367 return err;
2368
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002369 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002370 val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
2371 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002372
2373 tw32(TG3_CPMU_CTRL, cpmuctrl);
2374 }
2375
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002376 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2377 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002378 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2379 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
2380 CPMU_LSPD_1000MB_MACCLK_12_5) {
2381 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2382 udelay(40);
2383 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2384 }
2385 }
2386
Joe Perches63c3a662011-04-26 08:12:10 +00002387 if (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002388 (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
Matt Carlsonecf14102010-01-20 16:58:05 +00002389 return 0;
2390
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002391 tg3_phy_apply_otp(tp);
2392
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002393 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -08002394 tg3_phy_toggle_apd(tp, true);
2395 else
2396 tg3_phy_toggle_apd(tp, false);
2397
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398out:
Matt Carlson1d36ba42011-04-20 07:57:42 +00002399 if ((tp->phy_flags & TG3_PHYFLG_ADC_BUG) &&
2400 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002401 tg3_phydsp_write(tp, 0x201f, 0x2aaa);
2402 tg3_phydsp_write(tp, 0x000a, 0x0323);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002403 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002405
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002406 if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002407 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2408 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002410
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002411 if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002412 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2413 tg3_phydsp_write(tp, 0x000a, 0x310b);
2414 tg3_phydsp_write(tp, 0x201f, 0x9506);
2415 tg3_phydsp_write(tp, 0x401f, 0x14e2);
2416 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2417 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002418 } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002419 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2420 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
2421 if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
2422 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2423 tg3_writephy(tp, MII_TG3_TEST1,
2424 MII_TG3_TEST1_TRIM_EN | 0x4);
2425 } else
2426 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
2427
2428 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2429 }
Michael Chanc424cb22006-04-29 18:56:34 -07002430 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002431
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 /* Set Extended packet length bit (bit 14) on all chips that */
2433 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002434 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 /* Cannot do read-modify-write on 5401 */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002436 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Joe Perches63c3a662011-04-26 08:12:10 +00002437 } else if (tg3_flag(tp, JUMBO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 /* Set bit 14 with read-modify-write to preserve other bits */
Matt Carlson15ee95c2011-04-20 07:57:40 +00002439 err = tg3_phy_auxctl_read(tp,
2440 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2441 if (!err)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002442 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2443 val | MII_TG3_AUXCTL_ACTL_EXTPKTLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 }
2445
2446 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2447 * jumbo frames transmission.
2448 */
Joe Perches63c3a662011-04-26 08:12:10 +00002449 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002450 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002451 tg3_writephy(tp, MII_TG3_EXT_CTRL,
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002452 val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 }
2454
Michael Chan715116a2006-09-27 16:09:25 -07002455 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002456 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002457 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002458 }
2459
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002460 tg3_phy_toggle_automdix(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 tg3_phy_set_wirespeed(tp);
2462 return 0;
2463}
2464
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002465#define TG3_GPIO_MSG_DRVR_PRES 0x00000001
2466#define TG3_GPIO_MSG_NEED_VAUX 0x00000002
2467#define TG3_GPIO_MSG_MASK (TG3_GPIO_MSG_DRVR_PRES | \
2468 TG3_GPIO_MSG_NEED_VAUX)
2469#define TG3_GPIO_MSG_ALL_DRVR_PRES_MASK \
2470 ((TG3_GPIO_MSG_DRVR_PRES << 0) | \
2471 (TG3_GPIO_MSG_DRVR_PRES << 4) | \
2472 (TG3_GPIO_MSG_DRVR_PRES << 8) | \
2473 (TG3_GPIO_MSG_DRVR_PRES << 12))
2474
2475#define TG3_GPIO_MSG_ALL_NEED_VAUX_MASK \
2476 ((TG3_GPIO_MSG_NEED_VAUX << 0) | \
2477 (TG3_GPIO_MSG_NEED_VAUX << 4) | \
2478 (TG3_GPIO_MSG_NEED_VAUX << 8) | \
2479 (TG3_GPIO_MSG_NEED_VAUX << 12))
2480
2481static inline u32 tg3_set_function_status(struct tg3 *tp, u32 newstat)
2482{
2483 u32 status, shift;
2484
2485 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2486 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
2487 status = tg3_ape_read32(tp, TG3_APE_GPIO_MSG);
2488 else
2489 status = tr32(TG3_CPMU_DRV_STATUS);
2490
2491 shift = TG3_APE_GPIO_MSG_SHIFT + 4 * tp->pci_fn;
2492 status &= ~(TG3_GPIO_MSG_MASK << shift);
2493 status |= (newstat << shift);
2494
2495 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2496 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
2497 tg3_ape_write32(tp, TG3_APE_GPIO_MSG, status);
2498 else
2499 tw32(TG3_CPMU_DRV_STATUS, status);
2500
2501 return status >> TG3_APE_GPIO_MSG_SHIFT;
2502}
2503
Matt Carlson520b2752011-06-13 13:39:02 +00002504static inline int tg3_pwrsrc_switch_to_vmain(struct tg3 *tp)
2505{
2506 if (!tg3_flag(tp, IS_NIC))
2507 return 0;
2508
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002509 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2510 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
2511 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
2512 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2513 return -EIO;
Matt Carlson520b2752011-06-13 13:39:02 +00002514
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002515 tg3_set_function_status(tp, TG3_GPIO_MSG_DRVR_PRES);
2516
2517 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2518 TG3_GRC_LCLCTL_PWRSW_DELAY);
2519
2520 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
2521 } else {
2522 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2523 TG3_GRC_LCLCTL_PWRSW_DELAY);
2524 }
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002525
Matt Carlson520b2752011-06-13 13:39:02 +00002526 return 0;
2527}
2528
2529static void tg3_pwrsrc_die_with_vmain(struct tg3 *tp)
2530{
2531 u32 grc_local_ctrl;
2532
2533 if (!tg3_flag(tp, IS_NIC) ||
2534 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2535 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)
2536 return;
2537
2538 grc_local_ctrl = tp->grc_local_ctrl | GRC_LCLCTRL_GPIO_OE1;
2539
2540 tw32_wait_f(GRC_LOCAL_CTRL,
2541 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2542 TG3_GRC_LCLCTL_PWRSW_DELAY);
2543
2544 tw32_wait_f(GRC_LOCAL_CTRL,
2545 grc_local_ctrl,
2546 TG3_GRC_LCLCTL_PWRSW_DELAY);
2547
2548 tw32_wait_f(GRC_LOCAL_CTRL,
2549 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2550 TG3_GRC_LCLCTL_PWRSW_DELAY);
2551}
2552
2553static void tg3_pwrsrc_switch_to_vaux(struct tg3 *tp)
2554{
2555 if (!tg3_flag(tp, IS_NIC))
2556 return;
2557
2558 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2559 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2560 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2561 (GRC_LCLCTRL_GPIO_OE0 |
2562 GRC_LCLCTRL_GPIO_OE1 |
2563 GRC_LCLCTRL_GPIO_OE2 |
2564 GRC_LCLCTRL_GPIO_OUTPUT0 |
2565 GRC_LCLCTRL_GPIO_OUTPUT1),
2566 TG3_GRC_LCLCTL_PWRSW_DELAY);
2567 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2568 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
2569 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2570 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2571 GRC_LCLCTRL_GPIO_OE1 |
2572 GRC_LCLCTRL_GPIO_OE2 |
2573 GRC_LCLCTRL_GPIO_OUTPUT0 |
2574 GRC_LCLCTRL_GPIO_OUTPUT1 |
2575 tp->grc_local_ctrl;
2576 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2577 TG3_GRC_LCLCTL_PWRSW_DELAY);
2578
2579 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2580 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2581 TG3_GRC_LCLCTL_PWRSW_DELAY);
2582
2583 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2584 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2585 TG3_GRC_LCLCTL_PWRSW_DELAY);
2586 } else {
2587 u32 no_gpio2;
2588 u32 grc_local_ctrl = 0;
2589
2590 /* Workaround to prevent overdrawing Amps. */
2591 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
2592 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
2593 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2594 grc_local_ctrl,
2595 TG3_GRC_LCLCTL_PWRSW_DELAY);
2596 }
2597
2598 /* On 5753 and variants, GPIO2 cannot be used. */
2599 no_gpio2 = tp->nic_sram_data_cfg &
2600 NIC_SRAM_DATA_CFG_NO_GPIO2;
2601
2602 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
2603 GRC_LCLCTRL_GPIO_OE1 |
2604 GRC_LCLCTRL_GPIO_OE2 |
2605 GRC_LCLCTRL_GPIO_OUTPUT1 |
2606 GRC_LCLCTRL_GPIO_OUTPUT2;
2607 if (no_gpio2) {
2608 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2609 GRC_LCLCTRL_GPIO_OUTPUT2);
2610 }
2611 tw32_wait_f(GRC_LOCAL_CTRL,
2612 tp->grc_local_ctrl | grc_local_ctrl,
2613 TG3_GRC_LCLCTL_PWRSW_DELAY);
2614
2615 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2616
2617 tw32_wait_f(GRC_LOCAL_CTRL,
2618 tp->grc_local_ctrl | grc_local_ctrl,
2619 TG3_GRC_LCLCTL_PWRSW_DELAY);
2620
2621 if (!no_gpio2) {
2622 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
2623 tw32_wait_f(GRC_LOCAL_CTRL,
2624 tp->grc_local_ctrl | grc_local_ctrl,
2625 TG3_GRC_LCLCTL_PWRSW_DELAY);
2626 }
2627 }
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002628}
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002629
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002630static void tg3_frob_aux_power_5717(struct tg3 *tp, bool wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002631{
2632 u32 msg = 0;
2633
2634 /* Serialize power state transitions */
2635 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2636 return;
2637
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002638 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE) || wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002639 msg = TG3_GPIO_MSG_NEED_VAUX;
2640
2641 msg = tg3_set_function_status(tp, msg);
2642
2643 if (msg & TG3_GPIO_MSG_ALL_DRVR_PRES_MASK)
2644 goto done;
2645
2646 if (msg & TG3_GPIO_MSG_ALL_NEED_VAUX_MASK)
2647 tg3_pwrsrc_switch_to_vaux(tp);
2648 else
2649 tg3_pwrsrc_die_with_vmain(tp);
2650
2651done:
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002652 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
Matt Carlson520b2752011-06-13 13:39:02 +00002653}
2654
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002655static void tg3_frob_aux_power(struct tg3 *tp, bool include_wol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656{
Matt Carlson683644b2011-03-09 16:58:23 +00002657 bool need_vaux = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658
Matt Carlson334355a2010-01-20 16:58:10 +00002659 /* The GPIOs do something completely different on 57765. */
Matt Carlson55086ad2011-12-14 11:09:59 +00002660 if (!tg3_flag(tp, IS_NIC) || tg3_flag(tp, 57765_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 return;
2662
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002663 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2664 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
2665 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002666 tg3_frob_aux_power_5717(tp, include_wol ?
2667 tg3_flag(tp, WOL_ENABLE) != 0 : 0);
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002668 return;
2669 }
2670
2671 if (tp->pdev_peer && tp->pdev_peer != tp->pdev) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002672 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002674 dev_peer = pci_get_drvdata(tp->pdev_peer);
Matt Carlson683644b2011-03-09 16:58:23 +00002675
Michael Chanbc1c7562006-03-20 17:48:03 -08002676 /* remove_one() may have been run on the peer. */
Matt Carlson683644b2011-03-09 16:58:23 +00002677 if (dev_peer) {
2678 struct tg3 *tp_peer = netdev_priv(dev_peer);
2679
Joe Perches63c3a662011-04-26 08:12:10 +00002680 if (tg3_flag(tp_peer, INIT_COMPLETE))
Matt Carlson683644b2011-03-09 16:58:23 +00002681 return;
2682
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002683 if ((include_wol && tg3_flag(tp_peer, WOL_ENABLE)) ||
Joe Perches63c3a662011-04-26 08:12:10 +00002684 tg3_flag(tp_peer, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002685 need_vaux = true;
2686 }
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002689 if ((include_wol && tg3_flag(tp, WOL_ENABLE)) ||
2690 tg3_flag(tp, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002691 need_vaux = true;
2692
Matt Carlson520b2752011-06-13 13:39:02 +00002693 if (need_vaux)
2694 tg3_pwrsrc_switch_to_vaux(tp);
2695 else
2696 tg3_pwrsrc_die_with_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697}
2698
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002699static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2700{
2701 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2702 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00002703 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002704 if (speed != SPEED_10)
2705 return 1;
2706 } else if (speed == SPEED_10)
2707 return 1;
2708
2709 return 0;
2710}
2711
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712static int tg3_setup_phy(struct tg3 *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713static int tg3_halt_cpu(struct tg3 *, u32);
2714
Matt Carlson0a459aa2008-11-03 16:54:15 -08002715static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002716{
Matt Carlsonce057f02007-11-12 21:08:03 -08002717 u32 val;
2718
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002719 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Michael Chan51297242007-02-13 12:17:57 -08002720 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2721 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2722 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2723
2724 sg_dig_ctrl |=
2725 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2726 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2727 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2728 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002729 return;
Michael Chan51297242007-02-13 12:17:57 -08002730 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002731
Michael Chan60189dd2006-12-17 17:08:07 -08002732 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002733 tg3_bmcr_reset(tp);
2734 val = tr32(GRC_MISC_CFG);
2735 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2736 udelay(40);
2737 return;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002738 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson0e5f7842009-11-02 14:26:38 +00002739 u32 phytest;
2740 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2741 u32 phy;
2742
2743 tg3_writephy(tp, MII_ADVERTISE, 0);
2744 tg3_writephy(tp, MII_BMCR,
2745 BMCR_ANENABLE | BMCR_ANRESTART);
2746
2747 tg3_writephy(tp, MII_TG3_FET_TEST,
2748 phytest | MII_TG3_FET_SHADOW_EN);
2749 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
2750 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
2751 tg3_writephy(tp,
2752 MII_TG3_FET_SHDW_AUXMODE4,
2753 phy);
2754 }
2755 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2756 }
2757 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002758 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07002759 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2760 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002761
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002762 val = MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2763 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2764 MII_TG3_AUXCTL_PCTL_VREG_11V;
2765 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, val);
Michael Chan715116a2006-09-27 16:09:25 -07002766 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002767
Michael Chan15c3b692006-03-22 01:06:52 -08002768 /* The PHY should not be powered down on some chips because
2769 * of bugs.
2770 */
2771 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2772 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2773 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002774 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Michael Chan15c3b692006-03-22 01:06:52 -08002775 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08002776
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002777 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2778 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002779 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2780 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2781 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2782 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2783 }
2784
Michael Chan15c3b692006-03-22 01:06:52 -08002785 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2786}
2787
Matt Carlson3f007892008-11-03 16:51:36 -08002788/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002789static int tg3_nvram_lock(struct tg3 *tp)
2790{
Joe Perches63c3a662011-04-26 08:12:10 +00002791 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002792 int i;
2793
2794 if (tp->nvram_lock_cnt == 0) {
2795 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2796 for (i = 0; i < 8000; i++) {
2797 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2798 break;
2799 udelay(20);
2800 }
2801 if (i == 8000) {
2802 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2803 return -ENODEV;
2804 }
2805 }
2806 tp->nvram_lock_cnt++;
2807 }
2808 return 0;
2809}
2810
2811/* tp->lock is held. */
2812static void tg3_nvram_unlock(struct tg3 *tp)
2813{
Joe Perches63c3a662011-04-26 08:12:10 +00002814 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002815 if (tp->nvram_lock_cnt > 0)
2816 tp->nvram_lock_cnt--;
2817 if (tp->nvram_lock_cnt == 0)
2818 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
2819 }
2820}
2821
2822/* tp->lock is held. */
2823static void tg3_enable_nvram_access(struct tg3 *tp)
2824{
Joe Perches63c3a662011-04-26 08:12:10 +00002825 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002826 u32 nvaccess = tr32(NVRAM_ACCESS);
2827
2828 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
2829 }
2830}
2831
2832/* tp->lock is held. */
2833static void tg3_disable_nvram_access(struct tg3 *tp)
2834{
Joe Perches63c3a662011-04-26 08:12:10 +00002835 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002836 u32 nvaccess = tr32(NVRAM_ACCESS);
2837
2838 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
2839 }
2840}
2841
2842static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
2843 u32 offset, u32 *val)
2844{
2845 u32 tmp;
2846 int i;
2847
2848 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
2849 return -EINVAL;
2850
2851 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
2852 EEPROM_ADDR_DEVID_MASK |
2853 EEPROM_ADDR_READ);
2854 tw32(GRC_EEPROM_ADDR,
2855 tmp |
2856 (0 << EEPROM_ADDR_DEVID_SHIFT) |
2857 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
2858 EEPROM_ADDR_ADDR_MASK) |
2859 EEPROM_ADDR_READ | EEPROM_ADDR_START);
2860
2861 for (i = 0; i < 1000; i++) {
2862 tmp = tr32(GRC_EEPROM_ADDR);
2863
2864 if (tmp & EEPROM_ADDR_COMPLETE)
2865 break;
2866 msleep(1);
2867 }
2868 if (!(tmp & EEPROM_ADDR_COMPLETE))
2869 return -EBUSY;
2870
Matt Carlson62cedd12009-04-20 14:52:29 -07002871 tmp = tr32(GRC_EEPROM_DATA);
2872
2873 /*
2874 * The data will always be opposite the native endian
2875 * format. Perform a blind byteswap to compensate.
2876 */
2877 *val = swab32(tmp);
2878
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002879 return 0;
2880}
2881
2882#define NVRAM_CMD_TIMEOUT 10000
2883
2884static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
2885{
2886 int i;
2887
2888 tw32(NVRAM_CMD, nvram_cmd);
2889 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
2890 udelay(10);
2891 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
2892 udelay(10);
2893 break;
2894 }
2895 }
2896
2897 if (i == NVRAM_CMD_TIMEOUT)
2898 return -EBUSY;
2899
2900 return 0;
2901}
2902
2903static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
2904{
Joe Perches63c3a662011-04-26 08:12:10 +00002905 if (tg3_flag(tp, NVRAM) &&
2906 tg3_flag(tp, NVRAM_BUFFERED) &&
2907 tg3_flag(tp, FLASH) &&
2908 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002909 (tp->nvram_jedecnum == JEDEC_ATMEL))
2910
2911 addr = ((addr / tp->nvram_pagesize) <<
2912 ATMEL_AT45DB0X1B_PAGE_POS) +
2913 (addr % tp->nvram_pagesize);
2914
2915 return addr;
2916}
2917
2918static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
2919{
Joe Perches63c3a662011-04-26 08:12:10 +00002920 if (tg3_flag(tp, NVRAM) &&
2921 tg3_flag(tp, NVRAM_BUFFERED) &&
2922 tg3_flag(tp, FLASH) &&
2923 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002924 (tp->nvram_jedecnum == JEDEC_ATMEL))
2925
2926 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
2927 tp->nvram_pagesize) +
2928 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
2929
2930 return addr;
2931}
2932
Matt Carlsone4f34112009-02-25 14:25:00 +00002933/* NOTE: Data read in from NVRAM is byteswapped according to
2934 * the byteswapping settings for all other register accesses.
2935 * tg3 devices are BE devices, so on a BE machine, the data
2936 * returned will be exactly as it is seen in NVRAM. On a LE
2937 * machine, the 32-bit value will be byteswapped.
2938 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002939static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
2940{
2941 int ret;
2942
Joe Perches63c3a662011-04-26 08:12:10 +00002943 if (!tg3_flag(tp, NVRAM))
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002944 return tg3_nvram_read_using_eeprom(tp, offset, val);
2945
2946 offset = tg3_nvram_phys_addr(tp, offset);
2947
2948 if (offset > NVRAM_ADDR_MSK)
2949 return -EINVAL;
2950
2951 ret = tg3_nvram_lock(tp);
2952 if (ret)
2953 return ret;
2954
2955 tg3_enable_nvram_access(tp);
2956
2957 tw32(NVRAM_ADDR, offset);
2958 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
2959 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
2960
2961 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00002962 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002963
2964 tg3_disable_nvram_access(tp);
2965
2966 tg3_nvram_unlock(tp);
2967
2968 return ret;
2969}
2970
Matt Carlsona9dc5292009-02-25 14:25:30 +00002971/* Ensures NVRAM data is in bytestream format. */
2972static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002973{
2974 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00002975 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002976 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00002977 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002978 return res;
2979}
2980
Matt Carlson997b4f12011-08-31 11:44:53 +00002981#define RX_CPU_SCRATCH_BASE 0x30000
2982#define RX_CPU_SCRATCH_SIZE 0x04000
2983#define TX_CPU_SCRATCH_BASE 0x34000
2984#define TX_CPU_SCRATCH_SIZE 0x04000
2985
2986/* tp->lock is held. */
2987static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
2988{
2989 int i;
2990
2991 BUG_ON(offset == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS));
2992
2993 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
2994 u32 val = tr32(GRC_VCPU_EXT_CTRL);
2995
2996 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
2997 return 0;
2998 }
2999 if (offset == RX_CPU_BASE) {
3000 for (i = 0; i < 10000; i++) {
3001 tw32(offset + CPU_STATE, 0xffffffff);
3002 tw32(offset + CPU_MODE, CPU_MODE_HALT);
3003 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
3004 break;
3005 }
3006
3007 tw32(offset + CPU_STATE, 0xffffffff);
3008 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
3009 udelay(10);
3010 } else {
3011 for (i = 0; i < 10000; i++) {
3012 tw32(offset + CPU_STATE, 0xffffffff);
3013 tw32(offset + CPU_MODE, CPU_MODE_HALT);
3014 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
3015 break;
3016 }
3017 }
3018
3019 if (i >= 10000) {
3020 netdev_err(tp->dev, "%s timed out, %s CPU\n",
3021 __func__, offset == RX_CPU_BASE ? "RX" : "TX");
3022 return -ENODEV;
3023 }
3024
3025 /* Clear firmware's nvram arbitration. */
3026 if (tg3_flag(tp, NVRAM))
3027 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
3028 return 0;
3029}
3030
3031struct fw_info {
3032 unsigned int fw_base;
3033 unsigned int fw_len;
3034 const __be32 *fw_data;
3035};
3036
3037/* tp->lock is held. */
3038static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base,
3039 u32 cpu_scratch_base, int cpu_scratch_size,
3040 struct fw_info *info)
3041{
3042 int err, lock_err, i;
3043 void (*write_op)(struct tg3 *, u32, u32);
3044
3045 if (cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS)) {
3046 netdev_err(tp->dev,
3047 "%s: Trying to load TX cpu firmware which is 5705\n",
3048 __func__);
3049 return -EINVAL;
3050 }
3051
3052 if (tg3_flag(tp, 5705_PLUS))
3053 write_op = tg3_write_mem;
3054 else
3055 write_op = tg3_write_indirect_reg32;
3056
3057 /* It is possible that bootcode is still loading at this point.
3058 * Get the nvram lock first before halting the cpu.
3059 */
3060 lock_err = tg3_nvram_lock(tp);
3061 err = tg3_halt_cpu(tp, cpu_base);
3062 if (!lock_err)
3063 tg3_nvram_unlock(tp);
3064 if (err)
3065 goto out;
3066
3067 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
3068 write_op(tp, cpu_scratch_base + i, 0);
3069 tw32(cpu_base + CPU_STATE, 0xffffffff);
3070 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
3071 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
3072 write_op(tp, (cpu_scratch_base +
3073 (info->fw_base & 0xffff) +
3074 (i * sizeof(u32))),
3075 be32_to_cpu(info->fw_data[i]));
3076
3077 err = 0;
3078
3079out:
3080 return err;
3081}
3082
3083/* tp->lock is held. */
3084static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
3085{
3086 struct fw_info info;
3087 const __be32 *fw_data;
3088 int err, i;
3089
3090 fw_data = (void *)tp->fw->data;
3091
3092 /* Firmware blob starts with version numbers, followed by
3093 start address and length. We are setting complete length.
3094 length = end_address_of_bss - start_address_of_text.
3095 Remainder is the blob to be loaded contiguously
3096 from start address. */
3097
3098 info.fw_base = be32_to_cpu(fw_data[1]);
3099 info.fw_len = tp->fw->size - 12;
3100 info.fw_data = &fw_data[3];
3101
3102 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
3103 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
3104 &info);
3105 if (err)
3106 return err;
3107
3108 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
3109 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
3110 &info);
3111 if (err)
3112 return err;
3113
3114 /* Now startup only the RX cpu. */
3115 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3116 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
3117
3118 for (i = 0; i < 5; i++) {
3119 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
3120 break;
3121 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3122 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
3123 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
3124 udelay(1000);
3125 }
3126 if (i >= 5) {
3127 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
3128 "should be %08x\n", __func__,
3129 tr32(RX_CPU_BASE + CPU_PC), info.fw_base);
3130 return -ENODEV;
3131 }
3132 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3133 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
3134
3135 return 0;
3136}
3137
3138/* tp->lock is held. */
3139static int tg3_load_tso_firmware(struct tg3 *tp)
3140{
3141 struct fw_info info;
3142 const __be32 *fw_data;
3143 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
3144 int err, i;
3145
3146 if (tg3_flag(tp, HW_TSO_1) ||
3147 tg3_flag(tp, HW_TSO_2) ||
3148 tg3_flag(tp, HW_TSO_3))
3149 return 0;
3150
3151 fw_data = (void *)tp->fw->data;
3152
3153 /* Firmware blob starts with version numbers, followed by
3154 start address and length. We are setting complete length.
3155 length = end_address_of_bss - start_address_of_text.
3156 Remainder is the blob to be loaded contiguously
3157 from start address. */
3158
3159 info.fw_base = be32_to_cpu(fw_data[1]);
3160 cpu_scratch_size = tp->fw_len;
3161 info.fw_len = tp->fw->size - 12;
3162 info.fw_data = &fw_data[3];
3163
3164 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
3165 cpu_base = RX_CPU_BASE;
3166 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
3167 } else {
3168 cpu_base = TX_CPU_BASE;
3169 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
3170 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
3171 }
3172
3173 err = tg3_load_firmware_cpu(tp, cpu_base,
3174 cpu_scratch_base, cpu_scratch_size,
3175 &info);
3176 if (err)
3177 return err;
3178
3179 /* Now startup the cpu. */
3180 tw32(cpu_base + CPU_STATE, 0xffffffff);
3181 tw32_f(cpu_base + CPU_PC, info.fw_base);
3182
3183 for (i = 0; i < 5; i++) {
3184 if (tr32(cpu_base + CPU_PC) == info.fw_base)
3185 break;
3186 tw32(cpu_base + CPU_STATE, 0xffffffff);
3187 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
3188 tw32_f(cpu_base + CPU_PC, info.fw_base);
3189 udelay(1000);
3190 }
3191 if (i >= 5) {
3192 netdev_err(tp->dev,
3193 "%s fails to set CPU PC, is %08x should be %08x\n",
3194 __func__, tr32(cpu_base + CPU_PC), info.fw_base);
3195 return -ENODEV;
3196 }
3197 tw32(cpu_base + CPU_STATE, 0xffffffff);
3198 tw32_f(cpu_base + CPU_MODE, 0x00000000);
3199 return 0;
3200}
3201
3202
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003203/* tp->lock is held. */
Matt Carlson3f007892008-11-03 16:51:36 -08003204static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
3205{
3206 u32 addr_high, addr_low;
3207 int i;
3208
3209 addr_high = ((tp->dev->dev_addr[0] << 8) |
3210 tp->dev->dev_addr[1]);
3211 addr_low = ((tp->dev->dev_addr[2] << 24) |
3212 (tp->dev->dev_addr[3] << 16) |
3213 (tp->dev->dev_addr[4] << 8) |
3214 (tp->dev->dev_addr[5] << 0));
3215 for (i = 0; i < 4; i++) {
3216 if (i == 1 && skip_mac_1)
3217 continue;
3218 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
3219 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
3220 }
3221
3222 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3223 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
3224 for (i = 0; i < 12; i++) {
3225 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
3226 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
3227 }
3228 }
3229
3230 addr_high = (tp->dev->dev_addr[0] +
3231 tp->dev->dev_addr[1] +
3232 tp->dev->dev_addr[2] +
3233 tp->dev->dev_addr[3] +
3234 tp->dev->dev_addr[4] +
3235 tp->dev->dev_addr[5]) &
3236 TX_BACKOFF_SEED_MASK;
3237 tw32(MAC_TX_BACKOFF_SEED, addr_high);
3238}
3239
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003240static void tg3_enable_register_access(struct tg3 *tp)
3241{
3242 /*
3243 * Make sure register accesses (indirect or otherwise) will function
3244 * correctly.
3245 */
3246 pci_write_config_dword(tp->pdev,
3247 TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
3248}
3249
3250static int tg3_power_up(struct tg3 *tp)
3251{
Matt Carlsonbed98292011-07-13 09:27:29 +00003252 int err;
3253
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003254 tg3_enable_register_access(tp);
3255
Matt Carlsonbed98292011-07-13 09:27:29 +00003256 err = pci_set_power_state(tp->pdev, PCI_D0);
3257 if (!err) {
3258 /* Switch out of Vaux if it is a NIC */
3259 tg3_pwrsrc_switch_to_vmain(tp);
3260 } else {
3261 netdev_err(tp->dev, "Transition to D0 failed\n");
3262 }
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003263
Matt Carlsonbed98292011-07-13 09:27:29 +00003264 return err;
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003265}
3266
3267static int tg3_power_down_prepare(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268{
3269 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003270 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003272 tg3_enable_register_access(tp);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003273
3274 /* Restore the CLKREQ setting. */
Joe Perches63c3a662011-04-26 08:12:10 +00003275 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003276 u16 lnkctl;
3277
3278 pci_read_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +00003279 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003280 &lnkctl);
3281 lnkctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
3282 pci_write_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +00003283 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003284 lnkctl);
3285 }
3286
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
3288 tw32(TG3PCI_MISC_HOST_CTRL,
3289 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
3290
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003291 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00003292 tg3_flag(tp, WOL_ENABLE);
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003293
Joe Perches63c3a662011-04-26 08:12:10 +00003294 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08003295 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003296 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson80096062010-08-02 11:26:06 +00003297 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003298 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003299 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003300
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00003301 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003302
Matt Carlson80096062010-08-02 11:26:06 +00003303 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003304
3305 tp->link_config.orig_speed = phydev->speed;
3306 tp->link_config.orig_duplex = phydev->duplex;
3307 tp->link_config.orig_autoneg = phydev->autoneg;
3308 tp->link_config.orig_advertising = phydev->advertising;
3309
3310 advertising = ADVERTISED_TP |
3311 ADVERTISED_Pause |
3312 ADVERTISED_Autoneg |
3313 ADVERTISED_10baseT_Half;
3314
Joe Perches63c3a662011-04-26 08:12:10 +00003315 if (tg3_flag(tp, ENABLE_ASF) || device_should_wake) {
3316 if (tg3_flag(tp, WOL_SPEED_100MB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003317 advertising |=
3318 ADVERTISED_100baseT_Half |
3319 ADVERTISED_100baseT_Full |
3320 ADVERTISED_10baseT_Full;
3321 else
3322 advertising |= ADVERTISED_10baseT_Full;
3323 }
3324
3325 phydev->advertising = advertising;
3326
3327 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08003328
3329 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00003330 if (phyid != PHY_ID_BCMAC131) {
3331 phyid &= PHY_BCM_OUI_MASK;
3332 if (phyid == PHY_BCM_OUI_1 ||
3333 phyid == PHY_BCM_OUI_2 ||
3334 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08003335 do_low_power = true;
3336 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003337 }
Matt Carlsondd477002008-05-25 23:45:58 -07003338 } else {
Matt Carlson20232762008-12-21 20:18:56 -08003339 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003340
Matt Carlson80096062010-08-02 11:26:06 +00003341 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
3342 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07003343 tp->link_config.orig_speed = tp->link_config.speed;
3344 tp->link_config.orig_duplex = tp->link_config.duplex;
3345 tp->link_config.orig_autoneg = tp->link_config.autoneg;
3346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003348 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Matt Carlsondd477002008-05-25 23:45:58 -07003349 tp->link_config.speed = SPEED_10;
3350 tp->link_config.duplex = DUPLEX_HALF;
3351 tp->link_config.autoneg = AUTONEG_ENABLE;
3352 tg3_setup_phy(tp, 0);
3353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 }
3355
Michael Chanb5d37722006-09-27 16:06:21 -07003356 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
3357 u32 val;
3358
3359 val = tr32(GRC_VCPU_EXT_CTRL);
3360 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
Joe Perches63c3a662011-04-26 08:12:10 +00003361 } else if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08003362 int i;
3363 u32 val;
3364
3365 for (i = 0; i < 200; i++) {
3366 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
3367 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
3368 break;
3369 msleep(1);
3370 }
3371 }
Joe Perches63c3a662011-04-26 08:12:10 +00003372 if (tg3_flag(tp, WOL_CAP))
Gary Zambranoa85feb82007-05-05 11:52:19 -07003373 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
3374 WOL_DRV_STATE_SHUTDOWN |
3375 WOL_DRV_WOL |
3376 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08003377
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003378 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 u32 mac_mode;
3380
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003381 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003382 if (do_low_power &&
3383 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
3384 tg3_phy_auxctl_write(tp,
3385 MII_TG3_AUXCTL_SHDWSEL_PWRCTL,
3386 MII_TG3_AUXCTL_PCTL_WOL_EN |
3387 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
3388 MII_TG3_AUXCTL_PCTL_CL_AB_TXDAC);
Matt Carlsondd477002008-05-25 23:45:58 -07003389 udelay(40);
3390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003392 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07003393 mac_mode = MAC_MODE_PORT_MODE_GMII;
3394 else
3395 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003397 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
3398 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
3399 ASIC_REV_5700) {
Joe Perches63c3a662011-04-26 08:12:10 +00003400 u32 speed = tg3_flag(tp, WOL_SPEED_100MB) ?
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003401 SPEED_100 : SPEED_10;
3402 if (tg3_5700_link_polarity(tp, speed))
3403 mac_mode |= MAC_MODE_LINK_POLARITY;
3404 else
3405 mac_mode &= ~MAC_MODE_LINK_POLARITY;
3406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 } else {
3408 mac_mode = MAC_MODE_PORT_MODE_TBI;
3409 }
3410
Joe Perches63c3a662011-04-26 08:12:10 +00003411 if (!tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 tw32(MAC_LED_CTRL, tp->led_ctrl);
3413
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003414 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00003415 if ((tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS)) &&
3416 (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)))
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003417 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418
Joe Perches63c3a662011-04-26 08:12:10 +00003419 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00003420 mac_mode |= MAC_MODE_APE_TX_EN |
3421 MAC_MODE_APE_RX_EN |
3422 MAC_MODE_TDE_ENABLE;
Matt Carlson3bda1252008-08-15 14:08:22 -07003423
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 tw32_f(MAC_MODE, mac_mode);
3425 udelay(100);
3426
3427 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
3428 udelay(10);
3429 }
3430
Joe Perches63c3a662011-04-26 08:12:10 +00003431 if (!tg3_flag(tp, WOL_SPEED_100MB) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3433 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
3434 u32 base_val;
3435
3436 base_val = tp->pci_clock_ctrl;
3437 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
3438 CLOCK_CTRL_TXCLK_DISABLE);
3439
Michael Chanb401e9e2005-12-19 16:27:04 -08003440 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
3441 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Joe Perches63c3a662011-04-26 08:12:10 +00003442 } else if (tg3_flag(tp, 5780_CLASS) ||
3443 tg3_flag(tp, CPMU_PRESENT) ||
Matt Carlson6ff6f812011-05-19 12:12:54 +00003444 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan4cf78e42005-07-25 12:29:19 -07003445 /* do nothing */
Joe Perches63c3a662011-04-26 08:12:10 +00003446 } else if (!(tg3_flag(tp, 5750_PLUS) && tg3_flag(tp, ENABLE_ASF))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 u32 newbits1, newbits2;
3448
3449 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3450 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3451 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
3452 CLOCK_CTRL_TXCLK_DISABLE |
3453 CLOCK_CTRL_ALTCLK);
3454 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
Joe Perches63c3a662011-04-26 08:12:10 +00003455 } else if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 newbits1 = CLOCK_CTRL_625_CORE;
3457 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
3458 } else {
3459 newbits1 = CLOCK_CTRL_ALTCLK;
3460 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
3461 }
3462
Michael Chanb401e9e2005-12-19 16:27:04 -08003463 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
3464 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465
Michael Chanb401e9e2005-12-19 16:27:04 -08003466 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
3467 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468
Joe Perches63c3a662011-04-26 08:12:10 +00003469 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 u32 newbits3;
3471
3472 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3473 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3474 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
3475 CLOCK_CTRL_TXCLK_DISABLE |
3476 CLOCK_CTRL_44MHZ_CORE);
3477 } else {
3478 newbits3 = CLOCK_CTRL_44MHZ_CORE;
3479 }
3480
Michael Chanb401e9e2005-12-19 16:27:04 -08003481 tw32_wait_f(TG3PCI_CLOCK_CTRL,
3482 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 }
3484 }
3485
Joe Perches63c3a662011-04-26 08:12:10 +00003486 if (!(device_should_wake) && !tg3_flag(tp, ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08003487 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08003488
Matt Carlsoncd0d7222011-07-13 09:27:33 +00003489 tg3_frob_aux_power(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490
3491 /* Workaround for unstable PLL clock */
3492 if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
3493 (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
3494 u32 val = tr32(0x7d00);
3495
3496 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
3497 tw32(0x7d00, val);
Joe Perches63c3a662011-04-26 08:12:10 +00003498 if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08003499 int err;
3500
3501 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08003503 if (!err)
3504 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08003505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 }
3507
Michael Chanbbadf502006-04-06 21:46:34 -07003508 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
3509
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 return 0;
3511}
3512
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003513static void tg3_power_down(struct tg3 *tp)
3514{
3515 tg3_power_down_prepare(tp);
3516
Joe Perches63c3a662011-04-26 08:12:10 +00003517 pci_wake_from_d3(tp->pdev, tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003518 pci_set_power_state(tp->pdev, PCI_D3hot);
3519}
3520
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
3522{
3523 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
3524 case MII_TG3_AUX_STAT_10HALF:
3525 *speed = SPEED_10;
3526 *duplex = DUPLEX_HALF;
3527 break;
3528
3529 case MII_TG3_AUX_STAT_10FULL:
3530 *speed = SPEED_10;
3531 *duplex = DUPLEX_FULL;
3532 break;
3533
3534 case MII_TG3_AUX_STAT_100HALF:
3535 *speed = SPEED_100;
3536 *duplex = DUPLEX_HALF;
3537 break;
3538
3539 case MII_TG3_AUX_STAT_100FULL:
3540 *speed = SPEED_100;
3541 *duplex = DUPLEX_FULL;
3542 break;
3543
3544 case MII_TG3_AUX_STAT_1000HALF:
3545 *speed = SPEED_1000;
3546 *duplex = DUPLEX_HALF;
3547 break;
3548
3549 case MII_TG3_AUX_STAT_1000FULL:
3550 *speed = SPEED_1000;
3551 *duplex = DUPLEX_FULL;
3552 break;
3553
3554 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003555 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07003556 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
3557 SPEED_10;
3558 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
3559 DUPLEX_HALF;
3560 break;
3561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 *speed = SPEED_INVALID;
3563 *duplex = DUPLEX_INVALID;
3564 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566}
3567
Matt Carlson42b64a42011-05-19 12:12:49 +00003568static int tg3_phy_autoneg_cfg(struct tg3 *tp, u32 advertise, u32 flowctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569{
Matt Carlson42b64a42011-05-19 12:12:49 +00003570 int err = 0;
3571 u32 val, new_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572
Matt Carlson42b64a42011-05-19 12:12:49 +00003573 new_adv = ADVERTISE_CSMA;
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +00003574 new_adv |= ethtool_adv_to_mii_adv_t(advertise) & ADVERTISE_ALL;
Matt Carlsonf88788f2011-12-14 11:10:00 +00003575 new_adv |= mii_advertise_flowctrl(flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576
Matt Carlson42b64a42011-05-19 12:12:49 +00003577 err = tg3_writephy(tp, MII_ADVERTISE, new_adv);
3578 if (err)
3579 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580
Matt Carlson4f272092011-12-14 11:09:57 +00003581 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
3582 new_adv = ethtool_adv_to_mii_ctrl1000_t(advertise);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003583
Matt Carlson4f272092011-12-14 11:09:57 +00003584 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3585 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
3586 new_adv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003587
Matt Carlson4f272092011-12-14 11:09:57 +00003588 err = tg3_writephy(tp, MII_CTRL1000, new_adv);
3589 if (err)
3590 goto done;
3591 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003592
Matt Carlson42b64a42011-05-19 12:12:49 +00003593 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
3594 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595
Matt Carlson42b64a42011-05-19 12:12:49 +00003596 tw32(TG3_CPMU_EEE_MODE,
3597 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003598
Matt Carlson42b64a42011-05-19 12:12:49 +00003599 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
3600 if (!err) {
3601 u32 err2;
Matt Carlson52b02d02010-10-14 10:37:41 +00003602
Matt Carlsona6b68da2010-12-06 08:28:52 +00003603 val = 0;
Matt Carlson42b64a42011-05-19 12:12:49 +00003604 /* Advertise 100-BaseTX EEE ability */
3605 if (advertise & ADVERTISED_100baseT_Full)
3606 val |= MDIO_AN_EEE_ADV_100TX;
3607 /* Advertise 1000-BaseT EEE ability */
3608 if (advertise & ADVERTISED_1000baseT_Full)
3609 val |= MDIO_AN_EEE_ADV_1000T;
3610 err = tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlsonb715ce92011-07-20 10:20:52 +00003611 if (err)
3612 val = 0;
3613
3614 switch (GET_ASIC_REV(tp->pci_chip_rev_id)) {
3615 case ASIC_REV_5717:
3616 case ASIC_REV_57765:
Matt Carlson55086ad2011-12-14 11:09:59 +00003617 case ASIC_REV_57766:
Matt Carlsonb715ce92011-07-20 10:20:52 +00003618 case ASIC_REV_5719:
3619 /* If we advertised any eee advertisements above... */
3620 if (val)
3621 val = MII_TG3_DSP_TAP26_ALNOKO |
3622 MII_TG3_DSP_TAP26_RMRXSTO |
3623 MII_TG3_DSP_TAP26_OPCSINPT;
3624 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
3625 /* Fall through */
3626 case ASIC_REV_5720:
3627 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
3628 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
3629 MII_TG3_DSP_CH34TP2_HIBW01);
3630 }
Matt Carlson52b02d02010-10-14 10:37:41 +00003631
Matt Carlson42b64a42011-05-19 12:12:49 +00003632 err2 = TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
3633 if (!err)
3634 err = err2;
3635 }
3636
3637done:
3638 return err;
3639}
3640
3641static void tg3_phy_copper_begin(struct tg3 *tp)
3642{
3643 u32 new_adv;
3644 int i;
3645
3646 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
3647 new_adv = ADVERTISED_10baseT_Half |
3648 ADVERTISED_10baseT_Full;
3649 if (tg3_flag(tp, WOL_SPEED_100MB))
3650 new_adv |= ADVERTISED_100baseT_Half |
3651 ADVERTISED_100baseT_Full;
3652
3653 tg3_phy_autoneg_cfg(tp, new_adv,
3654 FLOW_CTRL_TX | FLOW_CTRL_RX);
3655 } else if (tp->link_config.speed == SPEED_INVALID) {
3656 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
3657 tp->link_config.advertising &=
3658 ~(ADVERTISED_1000baseT_Half |
3659 ADVERTISED_1000baseT_Full);
3660
3661 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
3662 tp->link_config.flowctrl);
3663 } else {
3664 /* Asking for a specific link mode. */
3665 if (tp->link_config.speed == SPEED_1000) {
3666 if (tp->link_config.duplex == DUPLEX_FULL)
3667 new_adv = ADVERTISED_1000baseT_Full;
3668 else
3669 new_adv = ADVERTISED_1000baseT_Half;
3670 } else if (tp->link_config.speed == SPEED_100) {
3671 if (tp->link_config.duplex == DUPLEX_FULL)
3672 new_adv = ADVERTISED_100baseT_Full;
3673 else
3674 new_adv = ADVERTISED_100baseT_Half;
3675 } else {
3676 if (tp->link_config.duplex == DUPLEX_FULL)
3677 new_adv = ADVERTISED_10baseT_Full;
3678 else
3679 new_adv = ADVERTISED_10baseT_Half;
3680 }
3681
3682 tg3_phy_autoneg_cfg(tp, new_adv,
3683 tp->link_config.flowctrl);
Matt Carlson52b02d02010-10-14 10:37:41 +00003684 }
3685
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 if (tp->link_config.autoneg == AUTONEG_DISABLE &&
3687 tp->link_config.speed != SPEED_INVALID) {
3688 u32 bmcr, orig_bmcr;
3689
3690 tp->link_config.active_speed = tp->link_config.speed;
3691 tp->link_config.active_duplex = tp->link_config.duplex;
3692
3693 bmcr = 0;
3694 switch (tp->link_config.speed) {
3695 default:
3696 case SPEED_10:
3697 break;
3698
3699 case SPEED_100:
3700 bmcr |= BMCR_SPEED100;
3701 break;
3702
3703 case SPEED_1000:
Matt Carlson221c5632011-06-13 13:39:01 +00003704 bmcr |= BMCR_SPEED1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707
3708 if (tp->link_config.duplex == DUPLEX_FULL)
3709 bmcr |= BMCR_FULLDPLX;
3710
3711 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
3712 (bmcr != orig_bmcr)) {
3713 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
3714 for (i = 0; i < 1500; i++) {
3715 u32 tmp;
3716
3717 udelay(10);
3718 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
3719 tg3_readphy(tp, MII_BMSR, &tmp))
3720 continue;
3721 if (!(tmp & BMSR_LSTATUS)) {
3722 udelay(40);
3723 break;
3724 }
3725 }
3726 tg3_writephy(tp, MII_BMCR, bmcr);
3727 udelay(40);
3728 }
3729 } else {
3730 tg3_writephy(tp, MII_BMCR,
3731 BMCR_ANENABLE | BMCR_ANRESTART);
3732 }
3733}
3734
3735static int tg3_init_5401phy_dsp(struct tg3 *tp)
3736{
3737 int err;
3738
3739 /* Turn off tap power management. */
3740 /* Set Extended packet length bit */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003741 err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00003743 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
3744 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
3745 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
3746 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
3747 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748
3749 udelay(40);
3750
3751 return err;
3752}
3753
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003754static bool tg3_phy_copper_an_config_ok(struct tg3 *tp, u32 *lcladv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755{
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003756 u32 advmsk, tgtadv, advertising;
Michael Chan3600d912006-12-07 00:21:48 -08003757
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003758 advertising = tp->link_config.advertising;
3759 tgtadv = ethtool_adv_to_mii_adv_t(advertising) & ADVERTISE_ALL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003761 advmsk = ADVERTISE_ALL;
3762 if (tp->link_config.active_duplex == DUPLEX_FULL) {
Matt Carlsonf88788f2011-12-14 11:10:00 +00003763 tgtadv |= mii_advertise_flowctrl(tp->link_config.flowctrl);
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003764 advmsk |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
3765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003767 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
3768 return false;
3769
3770 if ((*lcladv & advmsk) != tgtadv)
3771 return false;
Matt Carlsonb99d2a52011-08-31 11:44:47 +00003772
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003773 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 u32 tg3_ctrl;
3775
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003776 tgtadv = ethtool_adv_to_mii_ctrl1000_t(advertising);
Michael Chan3600d912006-12-07 00:21:48 -08003777
Matt Carlson221c5632011-06-13 13:39:01 +00003778 if (tg3_readphy(tp, MII_CTRL1000, &tg3_ctrl))
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003779 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780
Matt Carlsonb99d2a52011-08-31 11:44:47 +00003781 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL);
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003782 if (tg3_ctrl != tgtadv)
3783 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 }
Matt Carlson93a700a2011-08-31 11:44:54 +00003785
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003786 return true;
Matt Carlsonef167e22007-12-20 20:10:01 -08003787}
3788
Matt Carlson859edb22011-12-08 14:40:16 +00003789static bool tg3_phy_copper_fetch_rmtadv(struct tg3 *tp, u32 *rmtadv)
3790{
3791 u32 lpeth = 0;
3792
3793 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
3794 u32 val;
3795
3796 if (tg3_readphy(tp, MII_STAT1000, &val))
3797 return false;
3798
3799 lpeth = mii_stat1000_to_ethtool_lpa_t(val);
3800 }
3801
3802 if (tg3_readphy(tp, MII_LPA, rmtadv))
3803 return false;
3804
3805 lpeth |= mii_lpa_to_ethtool_lpa_t(*rmtadv);
3806 tp->link_config.rmt_adv = lpeth;
3807
3808 return true;
3809}
3810
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
3812{
3813 int current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003814 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08003815 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 u16 current_speed;
3817 u8 current_duplex;
3818 int i, err;
3819
3820 tw32(MAC_EVENT, 0);
3821
3822 tw32_f(MAC_STATUS,
3823 (MAC_STATUS_SYNC_CHANGED |
3824 MAC_STATUS_CFG_CHANGED |
3825 MAC_STATUS_MI_COMPLETION |
3826 MAC_STATUS_LNKSTATE_CHANGED));
3827 udelay(40);
3828
Matt Carlson8ef21422008-05-02 16:47:53 -07003829 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
3830 tw32_f(MAC_MI_MODE,
3831 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
3832 udelay(80);
3833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003835 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836
3837 /* Some third-party PHYs need to be reset on link going
3838 * down.
3839 */
3840 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3841 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
3842 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
3843 netif_carrier_ok(tp->dev)) {
3844 tg3_readphy(tp, MII_BMSR, &bmsr);
3845 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3846 !(bmsr & BMSR_LSTATUS))
3847 force_reset = 1;
3848 }
3849 if (force_reset)
3850 tg3_phy_reset(tp);
3851
Matt Carlson79eb6902010-02-17 15:17:03 +00003852 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 tg3_readphy(tp, MII_BMSR, &bmsr);
3854 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
Joe Perches63c3a662011-04-26 08:12:10 +00003855 !tg3_flag(tp, INIT_COMPLETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856 bmsr = 0;
3857
3858 if (!(bmsr & BMSR_LSTATUS)) {
3859 err = tg3_init_5401phy_dsp(tp);
3860 if (err)
3861 return err;
3862
3863 tg3_readphy(tp, MII_BMSR, &bmsr);
3864 for (i = 0; i < 1000; i++) {
3865 udelay(10);
3866 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3867 (bmsr & BMSR_LSTATUS)) {
3868 udelay(40);
3869 break;
3870 }
3871 }
3872
Matt Carlson79eb6902010-02-17 15:17:03 +00003873 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
3874 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875 !(bmsr & BMSR_LSTATUS) &&
3876 tp->link_config.active_speed == SPEED_1000) {
3877 err = tg3_phy_reset(tp);
3878 if (!err)
3879 err = tg3_init_5401phy_dsp(tp);
3880 if (err)
3881 return err;
3882 }
3883 }
3884 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3885 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
3886 /* 5701 {A0,B0} CRC bug workaround */
3887 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00003888 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
3889 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
3890 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 }
3892
3893 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003894 tg3_readphy(tp, MII_TG3_ISTAT, &val);
3895 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003897 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003899 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 tg3_writephy(tp, MII_TG3_IMASK, ~0);
3901
3902 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3903 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3904 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
3905 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3906 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
3907 else
3908 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
3909 }
3910
3911 current_link_up = 0;
3912 current_speed = SPEED_INVALID;
3913 current_duplex = DUPLEX_INVALID;
Matt Carlsone348c5e2011-11-21 15:01:20 +00003914 tp->phy_flags &= ~TG3_PHYFLG_MDIX_STATE;
Matt Carlson859edb22011-12-08 14:40:16 +00003915 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003917 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Matt Carlson15ee95c2011-04-20 07:57:40 +00003918 err = tg3_phy_auxctl_read(tp,
3919 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
3920 &val);
3921 if (!err && !(val & (1 << 10))) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003922 tg3_phy_auxctl_write(tp,
3923 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
3924 val | (1 << 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 goto relink;
3926 }
3927 }
3928
3929 bmsr = 0;
3930 for (i = 0; i < 100; i++) {
3931 tg3_readphy(tp, MII_BMSR, &bmsr);
3932 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3933 (bmsr & BMSR_LSTATUS))
3934 break;
3935 udelay(40);
3936 }
3937
3938 if (bmsr & BMSR_LSTATUS) {
3939 u32 aux_stat, bmcr;
3940
3941 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
3942 for (i = 0; i < 2000; i++) {
3943 udelay(10);
3944 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
3945 aux_stat)
3946 break;
3947 }
3948
3949 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
3950 &current_speed,
3951 &current_duplex);
3952
3953 bmcr = 0;
3954 for (i = 0; i < 200; i++) {
3955 tg3_readphy(tp, MII_BMCR, &bmcr);
3956 if (tg3_readphy(tp, MII_BMCR, &bmcr))
3957 continue;
3958 if (bmcr && bmcr != 0x7fff)
3959 break;
3960 udelay(10);
3961 }
3962
Matt Carlsonef167e22007-12-20 20:10:01 -08003963 lcl_adv = 0;
3964 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965
Matt Carlsonef167e22007-12-20 20:10:01 -08003966 tp->link_config.active_speed = current_speed;
3967 tp->link_config.active_duplex = current_duplex;
3968
3969 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3970 if ((bmcr & BMCR_ANENABLE) &&
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003971 tg3_phy_copper_an_config_ok(tp, &lcl_adv) &&
Matt Carlson859edb22011-12-08 14:40:16 +00003972 tg3_phy_copper_fetch_rmtadv(tp, &rmt_adv))
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003973 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 } else {
3975 if (!(bmcr & BMCR_ANENABLE) &&
3976 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08003977 tp->link_config.duplex == current_duplex &&
3978 tp->link_config.flowctrl ==
3979 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 }
3982 }
3983
Matt Carlsonef167e22007-12-20 20:10:01 -08003984 if (current_link_up == 1 &&
Matt Carlsone348c5e2011-11-21 15:01:20 +00003985 tp->link_config.active_duplex == DUPLEX_FULL) {
3986 u32 reg, bit;
3987
3988 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
3989 reg = MII_TG3_FET_GEN_STAT;
3990 bit = MII_TG3_FET_GEN_STAT_MDIXSTAT;
3991 } else {
3992 reg = MII_TG3_EXT_STAT;
3993 bit = MII_TG3_EXT_STAT_MDIX;
3994 }
3995
3996 if (!tg3_readphy(tp, reg, &val) && (val & bit))
3997 tp->phy_flags |= TG3_PHYFLG_MDIX_STATE;
3998
Matt Carlsonef167e22007-12-20 20:10:01 -08003999 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Matt Carlsone348c5e2011-11-21 15:01:20 +00004000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 }
4002
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003relink:
Matt Carlson80096062010-08-02 11:26:06 +00004004 if (current_link_up == 0 || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005 tg3_phy_copper_begin(tp);
4006
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004007 tg3_readphy(tp, MII_BMSR, &bmsr);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00004008 if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
4009 (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 current_link_up = 1;
4011 }
4012
4013 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
4014 if (current_link_up == 1) {
4015 if (tp->link_config.active_speed == SPEED_100 ||
4016 tp->link_config.active_speed == SPEED_10)
4017 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4018 else
4019 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004020 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00004021 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4022 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4024
4025 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4026 if (tp->link_config.active_duplex == DUPLEX_HALF)
4027 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4028
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004030 if (current_link_up == 1 &&
4031 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004033 else
4034 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 }
4036
4037 /* ??? Without this setting Netgear GA302T PHY does not
4038 * ??? send/receive packets...
4039 */
Matt Carlson79eb6902010-02-17 15:17:03 +00004040 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
4042 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
4043 tw32_f(MAC_MI_MODE, tp->mi_mode);
4044 udelay(80);
4045 }
4046
4047 tw32_f(MAC_MODE, tp->mac_mode);
4048 udelay(40);
4049
Matt Carlson52b02d02010-10-14 10:37:41 +00004050 tg3_phy_eee_adjust(tp, current_link_up);
4051
Joe Perches63c3a662011-04-26 08:12:10 +00004052 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 /* Polled via timer. */
4054 tw32_f(MAC_EVENT, 0);
4055 } else {
4056 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4057 }
4058 udelay(40);
4059
4060 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
4061 current_link_up == 1 &&
4062 tp->link_config.active_speed == SPEED_1000 &&
Joe Perches63c3a662011-04-26 08:12:10 +00004063 (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 udelay(120);
4065 tw32_f(MAC_STATUS,
4066 (MAC_STATUS_SYNC_CHANGED |
4067 MAC_STATUS_CFG_CHANGED));
4068 udelay(40);
4069 tg3_write_mem(tp,
4070 NIC_SRAM_FIRMWARE_MBOX,
4071 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
4072 }
4073
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004074 /* Prevent send BD corruption. */
Joe Perches63c3a662011-04-26 08:12:10 +00004075 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004076 u16 oldlnkctl, newlnkctl;
4077
4078 pci_read_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +00004079 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004080 &oldlnkctl);
4081 if (tp->link_config.active_speed == SPEED_100 ||
4082 tp->link_config.active_speed == SPEED_10)
4083 newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
4084 else
4085 newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
4086 if (newlnkctl != oldlnkctl)
4087 pci_write_config_word(tp->pdev,
Matt Carlson93a700a2011-08-31 11:44:54 +00004088 pci_pcie_cap(tp->pdev) +
4089 PCI_EXP_LNKCTL, newlnkctl);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004090 }
4091
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 if (current_link_up != netif_carrier_ok(tp->dev)) {
4093 if (current_link_up)
4094 netif_carrier_on(tp->dev);
4095 else
4096 netif_carrier_off(tp->dev);
4097 tg3_link_report(tp);
4098 }
4099
4100 return 0;
4101}
4102
4103struct tg3_fiber_aneginfo {
4104 int state;
4105#define ANEG_STATE_UNKNOWN 0
4106#define ANEG_STATE_AN_ENABLE 1
4107#define ANEG_STATE_RESTART_INIT 2
4108#define ANEG_STATE_RESTART 3
4109#define ANEG_STATE_DISABLE_LINK_OK 4
4110#define ANEG_STATE_ABILITY_DETECT_INIT 5
4111#define ANEG_STATE_ABILITY_DETECT 6
4112#define ANEG_STATE_ACK_DETECT_INIT 7
4113#define ANEG_STATE_ACK_DETECT 8
4114#define ANEG_STATE_COMPLETE_ACK_INIT 9
4115#define ANEG_STATE_COMPLETE_ACK 10
4116#define ANEG_STATE_IDLE_DETECT_INIT 11
4117#define ANEG_STATE_IDLE_DETECT 12
4118#define ANEG_STATE_LINK_OK 13
4119#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
4120#define ANEG_STATE_NEXT_PAGE_WAIT 15
4121
4122 u32 flags;
4123#define MR_AN_ENABLE 0x00000001
4124#define MR_RESTART_AN 0x00000002
4125#define MR_AN_COMPLETE 0x00000004
4126#define MR_PAGE_RX 0x00000008
4127#define MR_NP_LOADED 0x00000010
4128#define MR_TOGGLE_TX 0x00000020
4129#define MR_LP_ADV_FULL_DUPLEX 0x00000040
4130#define MR_LP_ADV_HALF_DUPLEX 0x00000080
4131#define MR_LP_ADV_SYM_PAUSE 0x00000100
4132#define MR_LP_ADV_ASYM_PAUSE 0x00000200
4133#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
4134#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
4135#define MR_LP_ADV_NEXT_PAGE 0x00001000
4136#define MR_TOGGLE_RX 0x00002000
4137#define MR_NP_RX 0x00004000
4138
4139#define MR_LINK_OK 0x80000000
4140
4141 unsigned long link_time, cur_time;
4142
4143 u32 ability_match_cfg;
4144 int ability_match_count;
4145
4146 char ability_match, idle_match, ack_match;
4147
4148 u32 txconfig, rxconfig;
4149#define ANEG_CFG_NP 0x00000080
4150#define ANEG_CFG_ACK 0x00000040
4151#define ANEG_CFG_RF2 0x00000020
4152#define ANEG_CFG_RF1 0x00000010
4153#define ANEG_CFG_PS2 0x00000001
4154#define ANEG_CFG_PS1 0x00008000
4155#define ANEG_CFG_HD 0x00004000
4156#define ANEG_CFG_FD 0x00002000
4157#define ANEG_CFG_INVAL 0x00001f06
4158
4159};
4160#define ANEG_OK 0
4161#define ANEG_DONE 1
4162#define ANEG_TIMER_ENAB 2
4163#define ANEG_FAILED -1
4164
4165#define ANEG_STATE_SETTLE_TIME 10000
4166
4167static int tg3_fiber_aneg_smachine(struct tg3 *tp,
4168 struct tg3_fiber_aneginfo *ap)
4169{
Matt Carlson5be73b42007-12-20 20:09:29 -08004170 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 unsigned long delta;
4172 u32 rx_cfg_reg;
4173 int ret;
4174
4175 if (ap->state == ANEG_STATE_UNKNOWN) {
4176 ap->rxconfig = 0;
4177 ap->link_time = 0;
4178 ap->cur_time = 0;
4179 ap->ability_match_cfg = 0;
4180 ap->ability_match_count = 0;
4181 ap->ability_match = 0;
4182 ap->idle_match = 0;
4183 ap->ack_match = 0;
4184 }
4185 ap->cur_time++;
4186
4187 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
4188 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
4189
4190 if (rx_cfg_reg != ap->ability_match_cfg) {
4191 ap->ability_match_cfg = rx_cfg_reg;
4192 ap->ability_match = 0;
4193 ap->ability_match_count = 0;
4194 } else {
4195 if (++ap->ability_match_count > 1) {
4196 ap->ability_match = 1;
4197 ap->ability_match_cfg = rx_cfg_reg;
4198 }
4199 }
4200 if (rx_cfg_reg & ANEG_CFG_ACK)
4201 ap->ack_match = 1;
4202 else
4203 ap->ack_match = 0;
4204
4205 ap->idle_match = 0;
4206 } else {
4207 ap->idle_match = 1;
4208 ap->ability_match_cfg = 0;
4209 ap->ability_match_count = 0;
4210 ap->ability_match = 0;
4211 ap->ack_match = 0;
4212
4213 rx_cfg_reg = 0;
4214 }
4215
4216 ap->rxconfig = rx_cfg_reg;
4217 ret = ANEG_OK;
4218
Matt Carlson33f401a2010-04-05 10:19:27 +00004219 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 case ANEG_STATE_UNKNOWN:
4221 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
4222 ap->state = ANEG_STATE_AN_ENABLE;
4223
4224 /* fallthru */
4225 case ANEG_STATE_AN_ENABLE:
4226 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
4227 if (ap->flags & MR_AN_ENABLE) {
4228 ap->link_time = 0;
4229 ap->cur_time = 0;
4230 ap->ability_match_cfg = 0;
4231 ap->ability_match_count = 0;
4232 ap->ability_match = 0;
4233 ap->idle_match = 0;
4234 ap->ack_match = 0;
4235
4236 ap->state = ANEG_STATE_RESTART_INIT;
4237 } else {
4238 ap->state = ANEG_STATE_DISABLE_LINK_OK;
4239 }
4240 break;
4241
4242 case ANEG_STATE_RESTART_INIT:
4243 ap->link_time = ap->cur_time;
4244 ap->flags &= ~(MR_NP_LOADED);
4245 ap->txconfig = 0;
4246 tw32(MAC_TX_AUTO_NEG, 0);
4247 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4248 tw32_f(MAC_MODE, tp->mac_mode);
4249 udelay(40);
4250
4251 ret = ANEG_TIMER_ENAB;
4252 ap->state = ANEG_STATE_RESTART;
4253
4254 /* fallthru */
4255 case ANEG_STATE_RESTART:
4256 delta = ap->cur_time - ap->link_time;
Matt Carlson859a588792010-04-05 10:19:28 +00004257 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a588792010-04-05 10:19:28 +00004259 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 break;
4262
4263 case ANEG_STATE_DISABLE_LINK_OK:
4264 ret = ANEG_DONE;
4265 break;
4266
4267 case ANEG_STATE_ABILITY_DETECT_INIT:
4268 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08004269 ap->txconfig = ANEG_CFG_FD;
4270 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
4271 if (flowctrl & ADVERTISE_1000XPAUSE)
4272 ap->txconfig |= ANEG_CFG_PS1;
4273 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
4274 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
4276 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4277 tw32_f(MAC_MODE, tp->mac_mode);
4278 udelay(40);
4279
4280 ap->state = ANEG_STATE_ABILITY_DETECT;
4281 break;
4282
4283 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a588792010-04-05 10:19:28 +00004284 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 break;
4287
4288 case ANEG_STATE_ACK_DETECT_INIT:
4289 ap->txconfig |= ANEG_CFG_ACK;
4290 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
4291 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4292 tw32_f(MAC_MODE, tp->mac_mode);
4293 udelay(40);
4294
4295 ap->state = ANEG_STATE_ACK_DETECT;
4296
4297 /* fallthru */
4298 case ANEG_STATE_ACK_DETECT:
4299 if (ap->ack_match != 0) {
4300 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
4301 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
4302 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
4303 } else {
4304 ap->state = ANEG_STATE_AN_ENABLE;
4305 }
4306 } else if (ap->ability_match != 0 &&
4307 ap->rxconfig == 0) {
4308 ap->state = ANEG_STATE_AN_ENABLE;
4309 }
4310 break;
4311
4312 case ANEG_STATE_COMPLETE_ACK_INIT:
4313 if (ap->rxconfig & ANEG_CFG_INVAL) {
4314 ret = ANEG_FAILED;
4315 break;
4316 }
4317 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
4318 MR_LP_ADV_HALF_DUPLEX |
4319 MR_LP_ADV_SYM_PAUSE |
4320 MR_LP_ADV_ASYM_PAUSE |
4321 MR_LP_ADV_REMOTE_FAULT1 |
4322 MR_LP_ADV_REMOTE_FAULT2 |
4323 MR_LP_ADV_NEXT_PAGE |
4324 MR_TOGGLE_RX |
4325 MR_NP_RX);
4326 if (ap->rxconfig & ANEG_CFG_FD)
4327 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
4328 if (ap->rxconfig & ANEG_CFG_HD)
4329 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
4330 if (ap->rxconfig & ANEG_CFG_PS1)
4331 ap->flags |= MR_LP_ADV_SYM_PAUSE;
4332 if (ap->rxconfig & ANEG_CFG_PS2)
4333 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
4334 if (ap->rxconfig & ANEG_CFG_RF1)
4335 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
4336 if (ap->rxconfig & ANEG_CFG_RF2)
4337 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
4338 if (ap->rxconfig & ANEG_CFG_NP)
4339 ap->flags |= MR_LP_ADV_NEXT_PAGE;
4340
4341 ap->link_time = ap->cur_time;
4342
4343 ap->flags ^= (MR_TOGGLE_TX);
4344 if (ap->rxconfig & 0x0008)
4345 ap->flags |= MR_TOGGLE_RX;
4346 if (ap->rxconfig & ANEG_CFG_NP)
4347 ap->flags |= MR_NP_RX;
4348 ap->flags |= MR_PAGE_RX;
4349
4350 ap->state = ANEG_STATE_COMPLETE_ACK;
4351 ret = ANEG_TIMER_ENAB;
4352 break;
4353
4354 case ANEG_STATE_COMPLETE_ACK:
4355 if (ap->ability_match != 0 &&
4356 ap->rxconfig == 0) {
4357 ap->state = ANEG_STATE_AN_ENABLE;
4358 break;
4359 }
4360 delta = ap->cur_time - ap->link_time;
4361 if (delta > ANEG_STATE_SETTLE_TIME) {
4362 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
4363 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
4364 } else {
4365 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
4366 !(ap->flags & MR_NP_RX)) {
4367 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
4368 } else {
4369 ret = ANEG_FAILED;
4370 }
4371 }
4372 }
4373 break;
4374
4375 case ANEG_STATE_IDLE_DETECT_INIT:
4376 ap->link_time = ap->cur_time;
4377 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
4378 tw32_f(MAC_MODE, tp->mac_mode);
4379 udelay(40);
4380
4381 ap->state = ANEG_STATE_IDLE_DETECT;
4382 ret = ANEG_TIMER_ENAB;
4383 break;
4384
4385 case ANEG_STATE_IDLE_DETECT:
4386 if (ap->ability_match != 0 &&
4387 ap->rxconfig == 0) {
4388 ap->state = ANEG_STATE_AN_ENABLE;
4389 break;
4390 }
4391 delta = ap->cur_time - ap->link_time;
4392 if (delta > ANEG_STATE_SETTLE_TIME) {
4393 /* XXX another gem from the Broadcom driver :( */
4394 ap->state = ANEG_STATE_LINK_OK;
4395 }
4396 break;
4397
4398 case ANEG_STATE_LINK_OK:
4399 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
4400 ret = ANEG_DONE;
4401 break;
4402
4403 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
4404 /* ??? unimplemented */
4405 break;
4406
4407 case ANEG_STATE_NEXT_PAGE_WAIT:
4408 /* ??? unimplemented */
4409 break;
4410
4411 default:
4412 ret = ANEG_FAILED;
4413 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415
4416 return ret;
4417}
4418
Matt Carlson5be73b42007-12-20 20:09:29 -08004419static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420{
4421 int res = 0;
4422 struct tg3_fiber_aneginfo aninfo;
4423 int status = ANEG_FAILED;
4424 unsigned int tick;
4425 u32 tmp;
4426
4427 tw32_f(MAC_TX_AUTO_NEG, 0);
4428
4429 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
4430 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
4431 udelay(40);
4432
4433 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
4434 udelay(40);
4435
4436 memset(&aninfo, 0, sizeof(aninfo));
4437 aninfo.flags |= MR_AN_ENABLE;
4438 aninfo.state = ANEG_STATE_UNKNOWN;
4439 aninfo.cur_time = 0;
4440 tick = 0;
4441 while (++tick < 195000) {
4442 status = tg3_fiber_aneg_smachine(tp, &aninfo);
4443 if (status == ANEG_DONE || status == ANEG_FAILED)
4444 break;
4445
4446 udelay(1);
4447 }
4448
4449 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
4450 tw32_f(MAC_MODE, tp->mac_mode);
4451 udelay(40);
4452
Matt Carlson5be73b42007-12-20 20:09:29 -08004453 *txflags = aninfo.txconfig;
4454 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455
4456 if (status == ANEG_DONE &&
4457 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
4458 MR_LP_ADV_FULL_DUPLEX)))
4459 res = 1;
4460
4461 return res;
4462}
4463
4464static void tg3_init_bcm8002(struct tg3 *tp)
4465{
4466 u32 mac_status = tr32(MAC_STATUS);
4467 int i;
4468
4469 /* Reset when initting first time or we have a link. */
Joe Perches63c3a662011-04-26 08:12:10 +00004470 if (tg3_flag(tp, INIT_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 !(mac_status & MAC_STATUS_PCS_SYNCED))
4472 return;
4473
4474 /* Set PLL lock range. */
4475 tg3_writephy(tp, 0x16, 0x8007);
4476
4477 /* SW reset */
4478 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
4479
4480 /* Wait for reset to complete. */
4481 /* XXX schedule_timeout() ... */
4482 for (i = 0; i < 500; i++)
4483 udelay(10);
4484
4485 /* Config mode; select PMA/Ch 1 regs. */
4486 tg3_writephy(tp, 0x10, 0x8411);
4487
4488 /* Enable auto-lock and comdet, select txclk for tx. */
4489 tg3_writephy(tp, 0x11, 0x0a10);
4490
4491 tg3_writephy(tp, 0x18, 0x00a0);
4492 tg3_writephy(tp, 0x16, 0x41ff);
4493
4494 /* Assert and deassert POR. */
4495 tg3_writephy(tp, 0x13, 0x0400);
4496 udelay(40);
4497 tg3_writephy(tp, 0x13, 0x0000);
4498
4499 tg3_writephy(tp, 0x11, 0x0a50);
4500 udelay(40);
4501 tg3_writephy(tp, 0x11, 0x0a10);
4502
4503 /* Wait for signal to stabilize */
4504 /* XXX schedule_timeout() ... */
4505 for (i = 0; i < 15000; i++)
4506 udelay(10);
4507
4508 /* Deselect the channel register so we can read the PHYID
4509 * later.
4510 */
4511 tg3_writephy(tp, 0x10, 0x8011);
4512}
4513
4514static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
4515{
Matt Carlson82cd3d12007-12-20 20:09:00 -08004516 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517 u32 sg_dig_ctrl, sg_dig_status;
4518 u32 serdes_cfg, expected_sg_dig_ctrl;
4519 int workaround, port_a;
4520 int current_link_up;
4521
4522 serdes_cfg = 0;
4523 expected_sg_dig_ctrl = 0;
4524 workaround = 0;
4525 port_a = 1;
4526 current_link_up = 0;
4527
4528 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
4529 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
4530 workaround = 1;
4531 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
4532 port_a = 0;
4533
4534 /* preserve bits 0-11,13,14 for signal pre-emphasis */
4535 /* preserve bits 20-23 for voltage regulator */
4536 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
4537 }
4538
4539 sg_dig_ctrl = tr32(SG_DIG_CTRL);
4540
4541 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004542 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543 if (workaround) {
4544 u32 val = serdes_cfg;
4545
4546 if (port_a)
4547 val |= 0xc010000;
4548 else
4549 val |= 0x4010000;
4550 tw32_f(MAC_SERDES_CFG, val);
4551 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004552
4553 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554 }
4555 if (mac_status & MAC_STATUS_PCS_SYNCED) {
4556 tg3_setup_flow_control(tp, 0, 0);
4557 current_link_up = 1;
4558 }
4559 goto out;
4560 }
4561
4562 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004563 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564
Matt Carlson82cd3d12007-12-20 20:09:00 -08004565 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
4566 if (flowctrl & ADVERTISE_1000XPAUSE)
4567 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
4568 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
4569 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570
4571 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004572 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07004573 tp->serdes_counter &&
4574 ((mac_status & (MAC_STATUS_PCS_SYNCED |
4575 MAC_STATUS_RCVD_CFG)) ==
4576 MAC_STATUS_PCS_SYNCED)) {
4577 tp->serdes_counter--;
4578 current_link_up = 1;
4579 goto out;
4580 }
4581restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 if (workaround)
4583 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004584 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 udelay(5);
4586 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
4587
Michael Chan3d3ebe72006-09-27 15:59:15 -07004588 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004589 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
4591 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07004592 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 mac_status = tr32(MAC_STATUS);
4594
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004595 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08004597 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598
Matt Carlson82cd3d12007-12-20 20:09:00 -08004599 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
4600 local_adv |= ADVERTISE_1000XPAUSE;
4601 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
4602 local_adv |= ADVERTISE_1000XPSE_ASYM;
4603
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004604 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08004605 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004606 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08004607 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608
Matt Carlson859edb22011-12-08 14:40:16 +00004609 tp->link_config.rmt_adv =
4610 mii_adv_to_ethtool_adv_x(remote_adv);
4611
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 tg3_setup_flow_control(tp, local_adv, remote_adv);
4613 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004614 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004615 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004616 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07004617 if (tp->serdes_counter)
4618 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619 else {
4620 if (workaround) {
4621 u32 val = serdes_cfg;
4622
4623 if (port_a)
4624 val |= 0xc010000;
4625 else
4626 val |= 0x4010000;
4627
4628 tw32_f(MAC_SERDES_CFG, val);
4629 }
4630
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004631 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632 udelay(40);
4633
4634 /* Link parallel detection - link is up */
4635 /* only if we have PCS_SYNC and not */
4636 /* receiving config code words */
4637 mac_status = tr32(MAC_STATUS);
4638 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
4639 !(mac_status & MAC_STATUS_RCVD_CFG)) {
4640 tg3_setup_flow_control(tp, 0, 0);
4641 current_link_up = 1;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004642 tp->phy_flags |=
4643 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004644 tp->serdes_counter =
4645 SERDES_PARALLEL_DET_TIMEOUT;
4646 } else
4647 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 }
4649 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07004650 } else {
4651 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004652 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653 }
4654
4655out:
4656 return current_link_up;
4657}
4658
4659static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
4660{
4661 int current_link_up = 0;
4662
Michael Chan5cf64b8a2007-05-05 12:11:21 -07004663 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665
4666 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08004667 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004669
Matt Carlson5be73b42007-12-20 20:09:29 -08004670 if (fiber_autoneg(tp, &txflags, &rxflags)) {
4671 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672
Matt Carlson5be73b42007-12-20 20:09:29 -08004673 if (txflags & ANEG_CFG_PS1)
4674 local_adv |= ADVERTISE_1000XPAUSE;
4675 if (txflags & ANEG_CFG_PS2)
4676 local_adv |= ADVERTISE_1000XPSE_ASYM;
4677
4678 if (rxflags & MR_LP_ADV_SYM_PAUSE)
4679 remote_adv |= LPA_1000XPAUSE;
4680 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
4681 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682
Matt Carlson859edb22011-12-08 14:40:16 +00004683 tp->link_config.rmt_adv =
4684 mii_adv_to_ethtool_adv_x(remote_adv);
4685
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686 tg3_setup_flow_control(tp, local_adv, remote_adv);
4687
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 current_link_up = 1;
4689 }
4690 for (i = 0; i < 30; i++) {
4691 udelay(20);
4692 tw32_f(MAC_STATUS,
4693 (MAC_STATUS_SYNC_CHANGED |
4694 MAC_STATUS_CFG_CHANGED));
4695 udelay(40);
4696 if ((tr32(MAC_STATUS) &
4697 (MAC_STATUS_SYNC_CHANGED |
4698 MAC_STATUS_CFG_CHANGED)) == 0)
4699 break;
4700 }
4701
4702 mac_status = tr32(MAC_STATUS);
4703 if (current_link_up == 0 &&
4704 (mac_status & MAC_STATUS_PCS_SYNCED) &&
4705 !(mac_status & MAC_STATUS_RCVD_CFG))
4706 current_link_up = 1;
4707 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08004708 tg3_setup_flow_control(tp, 0, 0);
4709
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710 /* Forcing 1000FD link up. */
4711 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712
4713 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
4714 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004715
4716 tw32_f(MAC_MODE, tp->mac_mode);
4717 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718 }
4719
4720out:
4721 return current_link_up;
4722}
4723
4724static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
4725{
4726 u32 orig_pause_cfg;
4727 u16 orig_active_speed;
4728 u8 orig_active_duplex;
4729 u32 mac_status;
4730 int current_link_up;
4731 int i;
4732
Matt Carlson8d018622007-12-20 20:05:44 -08004733 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734 orig_active_speed = tp->link_config.active_speed;
4735 orig_active_duplex = tp->link_config.active_duplex;
4736
Joe Perches63c3a662011-04-26 08:12:10 +00004737 if (!tg3_flag(tp, HW_AUTONEG) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738 netif_carrier_ok(tp->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00004739 tg3_flag(tp, INIT_COMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 mac_status = tr32(MAC_STATUS);
4741 mac_status &= (MAC_STATUS_PCS_SYNCED |
4742 MAC_STATUS_SIGNAL_DET |
4743 MAC_STATUS_CFG_CHANGED |
4744 MAC_STATUS_RCVD_CFG);
4745 if (mac_status == (MAC_STATUS_PCS_SYNCED |
4746 MAC_STATUS_SIGNAL_DET)) {
4747 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4748 MAC_STATUS_CFG_CHANGED));
4749 return 0;
4750 }
4751 }
4752
4753 tw32_f(MAC_TX_AUTO_NEG, 0);
4754
4755 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
4756 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
4757 tw32_f(MAC_MODE, tp->mac_mode);
4758 udelay(40);
4759
Matt Carlson79eb6902010-02-17 15:17:03 +00004760 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761 tg3_init_bcm8002(tp);
4762
4763 /* Enable link change event even when serdes polling. */
4764 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4765 udelay(40);
4766
4767 current_link_up = 0;
Matt Carlson859edb22011-12-08 14:40:16 +00004768 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769 mac_status = tr32(MAC_STATUS);
4770
Joe Perches63c3a662011-04-26 08:12:10 +00004771 if (tg3_flag(tp, HW_AUTONEG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
4773 else
4774 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
4775
Matt Carlson898a56f2009-08-28 14:02:40 +00004776 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00004778 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779
4780 for (i = 0; i < 100; i++) {
4781 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4782 MAC_STATUS_CFG_CHANGED));
4783 udelay(5);
4784 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07004785 MAC_STATUS_CFG_CHANGED |
4786 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787 break;
4788 }
4789
4790 mac_status = tr32(MAC_STATUS);
4791 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
4792 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004793 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
4794 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795 tw32_f(MAC_MODE, (tp->mac_mode |
4796 MAC_MODE_SEND_CONFIGS));
4797 udelay(1);
4798 tw32_f(MAC_MODE, tp->mac_mode);
4799 }
4800 }
4801
4802 if (current_link_up == 1) {
4803 tp->link_config.active_speed = SPEED_1000;
4804 tp->link_config.active_duplex = DUPLEX_FULL;
4805 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4806 LED_CTRL_LNKLED_OVERRIDE |
4807 LED_CTRL_1000MBPS_ON));
4808 } else {
4809 tp->link_config.active_speed = SPEED_INVALID;
4810 tp->link_config.active_duplex = DUPLEX_INVALID;
4811 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4812 LED_CTRL_LNKLED_OVERRIDE |
4813 LED_CTRL_TRAFFIC_OVERRIDE));
4814 }
4815
4816 if (current_link_up != netif_carrier_ok(tp->dev)) {
4817 if (current_link_up)
4818 netif_carrier_on(tp->dev);
4819 else
4820 netif_carrier_off(tp->dev);
4821 tg3_link_report(tp);
4822 } else {
Matt Carlson8d018622007-12-20 20:05:44 -08004823 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824 if (orig_pause_cfg != now_pause_cfg ||
4825 orig_active_speed != tp->link_config.active_speed ||
4826 orig_active_duplex != tp->link_config.active_duplex)
4827 tg3_link_report(tp);
4828 }
4829
4830 return 0;
4831}
4832
Michael Chan747e8f82005-07-25 12:33:22 -07004833static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
4834{
4835 int current_link_up, err = 0;
4836 u32 bmsr, bmcr;
4837 u16 current_speed;
4838 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08004839 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07004840
4841 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4842 tw32_f(MAC_MODE, tp->mac_mode);
4843 udelay(40);
4844
4845 tw32(MAC_EVENT, 0);
4846
4847 tw32_f(MAC_STATUS,
4848 (MAC_STATUS_SYNC_CHANGED |
4849 MAC_STATUS_CFG_CHANGED |
4850 MAC_STATUS_MI_COMPLETION |
4851 MAC_STATUS_LNKSTATE_CHANGED));
4852 udelay(40);
4853
4854 if (force_reset)
4855 tg3_phy_reset(tp);
4856
4857 current_link_up = 0;
4858 current_speed = SPEED_INVALID;
4859 current_duplex = DUPLEX_INVALID;
Matt Carlson859edb22011-12-08 14:40:16 +00004860 tp->link_config.rmt_adv = 0;
Michael Chan747e8f82005-07-25 12:33:22 -07004861
4862 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4863 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004864 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
4865 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4866 bmsr |= BMSR_LSTATUS;
4867 else
4868 bmsr &= ~BMSR_LSTATUS;
4869 }
Michael Chan747e8f82005-07-25 12:33:22 -07004870
4871 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
4872
4873 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004874 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004875 /* do nothing, just check for link up at the end */
4876 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson28011cf2011-11-16 18:36:59 -05004877 u32 adv, newadv;
Michael Chan747e8f82005-07-25 12:33:22 -07004878
4879 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
Matt Carlson28011cf2011-11-16 18:36:59 -05004880 newadv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
4881 ADVERTISE_1000XPAUSE |
4882 ADVERTISE_1000XPSE_ASYM |
4883 ADVERTISE_SLCT);
Michael Chan747e8f82005-07-25 12:33:22 -07004884
Matt Carlson28011cf2011-11-16 18:36:59 -05004885 newadv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Matt Carlson37f07022011-11-17 14:30:55 +00004886 newadv |= ethtool_adv_to_mii_adv_x(tp->link_config.advertising);
Michael Chan747e8f82005-07-25 12:33:22 -07004887
Matt Carlson28011cf2011-11-16 18:36:59 -05004888 if ((newadv != adv) || !(bmcr & BMCR_ANENABLE)) {
4889 tg3_writephy(tp, MII_ADVERTISE, newadv);
Michael Chan747e8f82005-07-25 12:33:22 -07004890 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
4891 tg3_writephy(tp, MII_BMCR, bmcr);
4892
4893 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07004894 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004895 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004896
4897 return err;
4898 }
4899 } else {
4900 u32 new_bmcr;
4901
4902 bmcr &= ~BMCR_SPEED1000;
4903 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
4904
4905 if (tp->link_config.duplex == DUPLEX_FULL)
4906 new_bmcr |= BMCR_FULLDPLX;
4907
4908 if (new_bmcr != bmcr) {
4909 /* BMCR_SPEED1000 is a reserved bit that needs
4910 * to be set on write.
4911 */
4912 new_bmcr |= BMCR_SPEED1000;
4913
4914 /* Force a linkdown */
4915 if (netif_carrier_ok(tp->dev)) {
4916 u32 adv;
4917
4918 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4919 adv &= ~(ADVERTISE_1000XFULL |
4920 ADVERTISE_1000XHALF |
4921 ADVERTISE_SLCT);
4922 tg3_writephy(tp, MII_ADVERTISE, adv);
4923 tg3_writephy(tp, MII_BMCR, bmcr |
4924 BMCR_ANRESTART |
4925 BMCR_ANENABLE);
4926 udelay(10);
4927 netif_carrier_off(tp->dev);
4928 }
4929 tg3_writephy(tp, MII_BMCR, new_bmcr);
4930 bmcr = new_bmcr;
4931 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4932 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004933 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
4934 ASIC_REV_5714) {
4935 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4936 bmsr |= BMSR_LSTATUS;
4937 else
4938 bmsr &= ~BMSR_LSTATUS;
4939 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004940 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004941 }
4942 }
4943
4944 if (bmsr & BMSR_LSTATUS) {
4945 current_speed = SPEED_1000;
4946 current_link_up = 1;
4947 if (bmcr & BMCR_FULLDPLX)
4948 current_duplex = DUPLEX_FULL;
4949 else
4950 current_duplex = DUPLEX_HALF;
4951
Matt Carlsonef167e22007-12-20 20:10:01 -08004952 local_adv = 0;
4953 remote_adv = 0;
4954
Michael Chan747e8f82005-07-25 12:33:22 -07004955 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08004956 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07004957
4958 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
4959 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
4960 common = local_adv & remote_adv;
4961 if (common & (ADVERTISE_1000XHALF |
4962 ADVERTISE_1000XFULL)) {
4963 if (common & ADVERTISE_1000XFULL)
4964 current_duplex = DUPLEX_FULL;
4965 else
4966 current_duplex = DUPLEX_HALF;
Matt Carlson859edb22011-12-08 14:40:16 +00004967
4968 tp->link_config.rmt_adv =
4969 mii_adv_to_ethtool_adv_x(remote_adv);
Joe Perches63c3a662011-04-26 08:12:10 +00004970 } else if (!tg3_flag(tp, 5780_CLASS)) {
Matt Carlson57d8b882010-06-05 17:24:35 +00004971 /* Link is up via parallel detect */
Matt Carlson859a588792010-04-05 10:19:28 +00004972 } else {
Michael Chan747e8f82005-07-25 12:33:22 -07004973 current_link_up = 0;
Matt Carlson859a588792010-04-05 10:19:28 +00004974 }
Michael Chan747e8f82005-07-25 12:33:22 -07004975 }
4976 }
4977
Matt Carlsonef167e22007-12-20 20:10:01 -08004978 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
4979 tg3_setup_flow_control(tp, local_adv, remote_adv);
4980
Michael Chan747e8f82005-07-25 12:33:22 -07004981 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4982 if (tp->link_config.active_duplex == DUPLEX_HALF)
4983 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4984
4985 tw32_f(MAC_MODE, tp->mac_mode);
4986 udelay(40);
4987
4988 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4989
4990 tp->link_config.active_speed = current_speed;
4991 tp->link_config.active_duplex = current_duplex;
4992
4993 if (current_link_up != netif_carrier_ok(tp->dev)) {
4994 if (current_link_up)
4995 netif_carrier_on(tp->dev);
4996 else {
4997 netif_carrier_off(tp->dev);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004998 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004999 }
5000 tg3_link_report(tp);
5001 }
5002 return err;
5003}
5004
5005static void tg3_serdes_parallel_detect(struct tg3 *tp)
5006{
Michael Chan3d3ebe72006-09-27 15:59:15 -07005007 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07005008 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07005009 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07005010 return;
5011 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005012
Michael Chan747e8f82005-07-25 12:33:22 -07005013 if (!netif_carrier_ok(tp->dev) &&
5014 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
5015 u32 bmcr;
5016
5017 tg3_readphy(tp, MII_BMCR, &bmcr);
5018 if (bmcr & BMCR_ANENABLE) {
5019 u32 phy1, phy2;
5020
5021 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005022 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
5023 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07005024
5025 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005026 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5027 MII_TG3_DSP_EXP1_INT_STAT);
5028 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
5029 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005030
5031 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
5032 /* We have signal detect and not receiving
5033 * config code words, link is up by parallel
5034 * detection.
5035 */
5036
5037 bmcr &= ~BMCR_ANENABLE;
5038 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
5039 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005040 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005041 }
5042 }
Matt Carlson859a588792010-04-05 10:19:28 +00005043 } else if (netif_carrier_ok(tp->dev) &&
5044 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005045 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005046 u32 phy2;
5047
5048 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005049 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5050 MII_TG3_DSP_EXP1_INT_STAT);
5051 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005052 if (phy2 & 0x20) {
5053 u32 bmcr;
5054
5055 /* Config code words received, turn on autoneg. */
5056 tg3_readphy(tp, MII_BMCR, &bmcr);
5057 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
5058
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005059 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005060
5061 }
5062 }
5063}
5064
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065static int tg3_setup_phy(struct tg3 *tp, int force_reset)
5066{
Matt Carlsonf2096f92011-04-05 14:22:48 +00005067 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068 int err;
5069
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005070 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005072 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07005073 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a588792010-04-05 10:19:28 +00005074 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076
Matt Carlsonbcb37f62008-11-03 16:52:09 -08005077 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00005078 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08005079
5080 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
5081 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
5082 scale = 65;
5083 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
5084 scale = 6;
5085 else
5086 scale = 12;
5087
5088 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
5089 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
5090 tw32(GRC_MISC_CFG, val);
5091 }
5092
Matt Carlsonf2096f92011-04-05 14:22:48 +00005093 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
5094 (6 << TX_LENGTHS_IPG_SHIFT);
5095 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
5096 val |= tr32(MAC_TX_LENGTHS) &
5097 (TX_LENGTHS_JMB_FRM_LEN_MSK |
5098 TX_LENGTHS_CNT_DWN_VAL_MSK);
5099
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100 if (tp->link_config.active_speed == SPEED_1000 &&
5101 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00005102 tw32(MAC_TX_LENGTHS, val |
5103 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00005105 tw32(MAC_TX_LENGTHS, val |
5106 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107
Joe Perches63c3a662011-04-26 08:12:10 +00005108 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109 if (netif_carrier_ok(tp->dev)) {
5110 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07005111 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112 } else {
5113 tw32(HOSTCC_STAT_COAL_TICKS, 0);
5114 }
5115 }
5116
Joe Perches63c3a662011-04-26 08:12:10 +00005117 if (tg3_flag(tp, ASPM_WORKAROUND)) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00005118 val = tr32(PCIE_PWR_MGMT_THRESH);
Matt Carlson8ed5d972007-05-07 00:25:49 -07005119 if (!netif_carrier_ok(tp->dev))
5120 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
5121 tp->pwrmgmt_thresh;
5122 else
5123 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
5124 tw32(PCIE_PWR_MGMT_THRESH, val);
5125 }
5126
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127 return err;
5128}
5129
Matt Carlson66cfd1b2010-09-30 10:34:30 +00005130static inline int tg3_irq_sync(struct tg3 *tp)
5131{
5132 return tp->irq_sync;
5133}
5134
Matt Carlson97bd8e42011-04-13 11:05:04 +00005135static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
5136{
5137 int i;
5138
5139 dst = (u32 *)((u8 *)dst + off);
5140 for (i = 0; i < len; i += sizeof(u32))
5141 *dst++ = tr32(off + i);
5142}
5143
5144static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
5145{
5146 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
5147 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
5148 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
5149 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
5150 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
5151 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
5152 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
5153 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
5154 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
5155 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
5156 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
5157 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
5158 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
5159 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
5160 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
5161 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
5162 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
5163 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
5164 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
5165
Joe Perches63c3a662011-04-26 08:12:10 +00005166 if (tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson97bd8e42011-04-13 11:05:04 +00005167 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
5168
5169 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
5170 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
5171 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
5172 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
5173 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
5174 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
5175 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
5176 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
5177
Joe Perches63c3a662011-04-26 08:12:10 +00005178 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00005179 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
5180 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
5181 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
5182 }
5183
5184 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
5185 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
5186 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
5187 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
5188 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
5189
Joe Perches63c3a662011-04-26 08:12:10 +00005190 if (tg3_flag(tp, NVRAM))
Matt Carlson97bd8e42011-04-13 11:05:04 +00005191 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
5192}
5193
5194static void tg3_dump_state(struct tg3 *tp)
5195{
5196 int i;
5197 u32 *regs;
5198
5199 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
5200 if (!regs) {
5201 netdev_err(tp->dev, "Failed allocating register dump buffer\n");
5202 return;
5203 }
5204
Joe Perches63c3a662011-04-26 08:12:10 +00005205 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00005206 /* Read up to but not including private PCI registers */
5207 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
5208 regs[i / sizeof(u32)] = tr32(i);
5209 } else
5210 tg3_dump_legacy_regs(tp, regs);
5211
5212 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
5213 if (!regs[i + 0] && !regs[i + 1] &&
5214 !regs[i + 2] && !regs[i + 3])
5215 continue;
5216
5217 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
5218 i * 4,
5219 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
5220 }
5221
5222 kfree(regs);
5223
5224 for (i = 0; i < tp->irq_cnt; i++) {
5225 struct tg3_napi *tnapi = &tp->napi[i];
5226
5227 /* SW status block */
5228 netdev_err(tp->dev,
5229 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
5230 i,
5231 tnapi->hw_status->status,
5232 tnapi->hw_status->status_tag,
5233 tnapi->hw_status->rx_jumbo_consumer,
5234 tnapi->hw_status->rx_consumer,
5235 tnapi->hw_status->rx_mini_consumer,
5236 tnapi->hw_status->idx[0].rx_producer,
5237 tnapi->hw_status->idx[0].tx_consumer);
5238
5239 netdev_err(tp->dev,
5240 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
5241 i,
5242 tnapi->last_tag, tnapi->last_irq_tag,
5243 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
5244 tnapi->rx_rcb_ptr,
5245 tnapi->prodring.rx_std_prod_idx,
5246 tnapi->prodring.rx_std_cons_idx,
5247 tnapi->prodring.rx_jmb_prod_idx,
5248 tnapi->prodring.rx_jmb_cons_idx);
5249 }
5250}
5251
Michael Chandf3e6542006-05-26 17:48:07 -07005252/* This is called whenever we suspect that the system chipset is re-
5253 * ordering the sequence of MMIO to the tx send mailbox. The symptom
5254 * is bogus tx completions. We try to recover by setting the
5255 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
5256 * in the workqueue.
5257 */
5258static void tg3_tx_recover(struct tg3 *tp)
5259{
Joe Perches63c3a662011-04-26 08:12:10 +00005260 BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
Michael Chandf3e6542006-05-26 17:48:07 -07005261 tp->write32_tx_mbox == tg3_write_indirect_mbox);
5262
Matt Carlson5129c3a2010-04-05 10:19:23 +00005263 netdev_warn(tp->dev,
5264 "The system may be re-ordering memory-mapped I/O "
5265 "cycles to the network device, attempting to recover. "
5266 "Please report the problem to the driver maintainer "
5267 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07005268
5269 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00005270 tg3_flag_set(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07005271 spin_unlock(&tp->lock);
5272}
5273
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005274static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07005275{
Matt Carlsonf65aac12010-08-02 11:26:03 +00005276 /* Tell compiler to fetch tx indices from memory. */
5277 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005278 return tnapi->tx_pending -
5279 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07005280}
5281
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282/* Tigon3 never reports partial packet sends. So we do not
5283 * need special logic to handle SKBs that have not had all
5284 * of their frags sent yet, like SunGEM does.
5285 */
Matt Carlson17375d22009-08-28 14:02:18 +00005286static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287{
Matt Carlson17375d22009-08-28 14:02:18 +00005288 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005289 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005290 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005291 struct netdev_queue *txq;
5292 int index = tnapi - tp->napi;
Tom Herbert298376d2011-11-28 16:33:30 +00005293 unsigned int pkts_compl = 0, bytes_compl = 0;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005294
Joe Perches63c3a662011-04-26 08:12:10 +00005295 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005296 index--;
5297
5298 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299
5300 while (sw_idx != hw_idx) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00005301 struct tg3_tx_ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07005303 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304
Michael Chandf3e6542006-05-26 17:48:07 -07005305 if (unlikely(skb == NULL)) {
5306 tg3_tx_recover(tp);
5307 return;
5308 }
5309
Alexander Duyckf4188d82009-12-02 16:48:38 +00005310 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005311 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005312 skb_headlen(skb),
5313 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314
5315 ri->skb = NULL;
5316
Matt Carlsone01ee142011-07-27 14:20:50 +00005317 while (ri->fragmented) {
5318 ri->fragmented = false;
5319 sw_idx = NEXT_TX(sw_idx);
5320 ri = &tnapi->tx_buffers[sw_idx];
5321 }
5322
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323 sw_idx = NEXT_TX(sw_idx);
5324
5325 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005326 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07005327 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
5328 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005329
5330 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005331 dma_unmap_addr(ri, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00005332 skb_frag_size(&skb_shinfo(skb)->frags[i]),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005333 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00005334
5335 while (ri->fragmented) {
5336 ri->fragmented = false;
5337 sw_idx = NEXT_TX(sw_idx);
5338 ri = &tnapi->tx_buffers[sw_idx];
5339 }
5340
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341 sw_idx = NEXT_TX(sw_idx);
5342 }
5343
Tom Herbert298376d2011-11-28 16:33:30 +00005344 pkts_compl++;
5345 bytes_compl += skb->len;
5346
David S. Millerf47c11e2005-06-24 20:18:35 -07005347 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07005348
5349 if (unlikely(tx_bug)) {
5350 tg3_tx_recover(tp);
5351 return;
5352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353 }
5354
Tom Herbert298376d2011-11-28 16:33:30 +00005355 netdev_completed_queue(tp->dev, pkts_compl, bytes_compl);
5356
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005357 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358
Michael Chan1b2a7202006-08-07 21:46:02 -07005359 /* Need to make the tx_cons update visible to tg3_start_xmit()
5360 * before checking for netif_queue_stopped(). Without the
5361 * memory barrier, there is a small possibility that tg3_start_xmit()
5362 * will miss it and cause the queue to be stopped forever.
5363 */
5364 smp_mb();
5365
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005366 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005367 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005368 __netif_tx_lock(txq, smp_processor_id());
5369 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005370 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005371 netif_tx_wake_queue(txq);
5372 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07005373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374}
5375
Eric Dumazet9205fd92011-11-18 06:47:01 +00005376static void tg3_rx_data_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005377{
Eric Dumazet9205fd92011-11-18 06:47:01 +00005378 if (!ri->data)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005379 return;
5380
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005381 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005382 map_sz, PCI_DMA_FROMDEVICE);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005383 kfree(ri->data);
5384 ri->data = NULL;
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005385}
5386
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387/* Returns size of skb allocated or < 0 on error.
5388 *
5389 * We only need to fill in the address because the other members
5390 * of the RX descriptor are invariant, see tg3_init_rings.
5391 *
5392 * Note the purposeful assymetry of cpu vs. chip accesses. For
5393 * posting buffers we only dirty the first cache line of the RX
5394 * descriptor (containing the address). Whereas for the RX status
5395 * buffers the cpu only reads the last cacheline of the RX descriptor
5396 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
5397 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00005398static int tg3_alloc_rx_data(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Matt Carlsona3896162009-11-13 13:03:44 +00005399 u32 opaque_key, u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400{
5401 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00005402 struct ring_info *map;
Eric Dumazet9205fd92011-11-18 06:47:01 +00005403 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404 dma_addr_t mapping;
Eric Dumazet9205fd92011-11-18 06:47:01 +00005405 int skb_size, data_size, dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407 switch (opaque_key) {
5408 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00005409 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00005410 desc = &tpr->rx_std[dest_idx];
5411 map = &tpr->rx_std_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00005412 data_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413 break;
5414
5415 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00005416 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00005417 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00005418 map = &tpr->rx_jmb_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00005419 data_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420 break;
5421
5422 default:
5423 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425
5426 /* Do not overwrite any of the map or rp information
5427 * until we are sure we can commit to a new buffer.
5428 *
5429 * Callers depend upon this behavior and assume that
5430 * we leave everything unchanged if we fail.
5431 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00005432 skb_size = SKB_DATA_ALIGN(data_size + TG3_RX_OFFSET(tp)) +
5433 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
5434 data = kmalloc(skb_size, GFP_ATOMIC);
5435 if (!data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 return -ENOMEM;
5437
Eric Dumazet9205fd92011-11-18 06:47:01 +00005438 mapping = pci_map_single(tp->pdev,
5439 data + TG3_RX_OFFSET(tp),
5440 data_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441 PCI_DMA_FROMDEVICE);
Matt Carlsona21771d2009-11-02 14:25:31 +00005442 if (pci_dma_mapping_error(tp->pdev, mapping)) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00005443 kfree(data);
Matt Carlsona21771d2009-11-02 14:25:31 +00005444 return -EIO;
5445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446
Eric Dumazet9205fd92011-11-18 06:47:01 +00005447 map->data = data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005448 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450 desc->addr_hi = ((u64)mapping >> 32);
5451 desc->addr_lo = ((u64)mapping & 0xffffffff);
5452
Eric Dumazet9205fd92011-11-18 06:47:01 +00005453 return data_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454}
5455
5456/* We only need to move over in the address because the other
5457 * members of the RX descriptor are invariant. See notes above
Eric Dumazet9205fd92011-11-18 06:47:01 +00005458 * tg3_alloc_rx_data for full details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459 */
Matt Carlsona3896162009-11-13 13:03:44 +00005460static void tg3_recycle_rx(struct tg3_napi *tnapi,
5461 struct tg3_rx_prodring_set *dpr,
5462 u32 opaque_key, int src_idx,
5463 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464{
Matt Carlson17375d22009-08-28 14:02:18 +00005465 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
5467 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00005468 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005469 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470
5471 switch (opaque_key) {
5472 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00005473 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00005474 dest_desc = &dpr->rx_std[dest_idx];
5475 dest_map = &dpr->rx_std_buffers[dest_idx];
5476 src_desc = &spr->rx_std[src_idx];
5477 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 break;
5479
5480 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00005481 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00005482 dest_desc = &dpr->rx_jmb[dest_idx].std;
5483 dest_map = &dpr->rx_jmb_buffers[dest_idx];
5484 src_desc = &spr->rx_jmb[src_idx].std;
5485 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486 break;
5487
5488 default:
5489 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491
Eric Dumazet9205fd92011-11-18 06:47:01 +00005492 dest_map->data = src_map->data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005493 dma_unmap_addr_set(dest_map, mapping,
5494 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495 dest_desc->addr_hi = src_desc->addr_hi;
5496 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00005497
5498 /* Ensure that the update to the skb happens after the physical
5499 * addresses have been transferred to the new BD location.
5500 */
5501 smp_wmb();
5502
Eric Dumazet9205fd92011-11-18 06:47:01 +00005503 src_map->data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504}
5505
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506/* The RX ring scheme is composed of multiple rings which post fresh
5507 * buffers to the chip, and one special ring the chip uses to report
5508 * status back to the host.
5509 *
5510 * The special ring reports the status of received packets to the
5511 * host. The chip does not write into the original descriptor the
5512 * RX buffer was obtained from. The chip simply takes the original
5513 * descriptor as provided by the host, updates the status and length
5514 * field, then writes this into the next status ring entry.
5515 *
5516 * Each ring the host uses to post buffers to the chip is described
5517 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
5518 * it is first placed into the on-chip ram. When the packet's length
5519 * is known, it walks down the TG3_BDINFO entries to select the ring.
5520 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
5521 * which is within the range of the new packet's length is chosen.
5522 *
5523 * The "separate ring for rx status" scheme may sound queer, but it makes
5524 * sense from a cache coherency perspective. If only the host writes
5525 * to the buffer post rings, and only the chip writes to the rx status
5526 * rings, then cache lines never move beyond shared-modified state.
5527 * If both the host and chip were to write into the same ring, cache line
5528 * eviction could occur since both entities want it in an exclusive state.
5529 */
Matt Carlson17375d22009-08-28 14:02:18 +00005530static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531{
Matt Carlson17375d22009-08-28 14:02:18 +00005532 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07005533 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00005534 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00005535 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07005536 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00005538 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005540 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541 /*
5542 * We need to order the read of hw_idx and the read of
5543 * the opaque cookie.
5544 */
5545 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546 work_mask = 0;
5547 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00005548 std_prod_idx = tpr->rx_std_prod_idx;
5549 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00005551 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00005552 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553 unsigned int len;
5554 struct sk_buff *skb;
5555 dma_addr_t dma_addr;
5556 u32 opaque_key, desc_idx, *post_ptr;
Eric Dumazet9205fd92011-11-18 06:47:01 +00005557 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558
5559 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
5560 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
5561 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005562 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005563 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005564 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00005565 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07005566 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005568 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005569 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005570 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00005571 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00005572 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574
5575 work_mask |= opaque_key;
5576
5577 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
5578 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
5579 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00005580 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581 desc_idx, *post_ptr);
5582 drop_it_no_recycle:
5583 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00005584 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585 goto next_pkt;
5586 }
5587
Eric Dumazet9205fd92011-11-18 06:47:01 +00005588 prefetch(data + TG3_RX_OFFSET(tp));
Matt Carlsonad829262008-11-21 17:16:16 -08005589 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
5590 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591
Matt Carlsond2757fc2010-04-12 06:58:27 +00005592 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593 int skb_size;
5594
Eric Dumazet9205fd92011-11-18 06:47:01 +00005595 skb_size = tg3_alloc_rx_data(tp, tpr, opaque_key,
Matt Carlsonafc081f2009-11-13 13:03:43 +00005596 *post_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597 if (skb_size < 0)
5598 goto drop_it;
5599
Matt Carlson287be122009-08-28 13:58:46 +00005600 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601 PCI_DMA_FROMDEVICE);
5602
Eric Dumazet9205fd92011-11-18 06:47:01 +00005603 skb = build_skb(data);
5604 if (!skb) {
5605 kfree(data);
5606 goto drop_it_no_recycle;
5607 }
5608 skb_reserve(skb, TG3_RX_OFFSET(tp));
5609 /* Ensure that the update to the data happens
Matt Carlson61e800c2010-02-17 15:16:54 +00005610 * after the usage of the old DMA mapping.
5611 */
5612 smp_wmb();
5613
Eric Dumazet9205fd92011-11-18 06:47:01 +00005614 ri->data = NULL;
Matt Carlson61e800c2010-02-17 15:16:54 +00005615
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616 } else {
Matt Carlsona3896162009-11-13 13:03:44 +00005617 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618 desc_idx, *post_ptr);
5619
Eric Dumazet9205fd92011-11-18 06:47:01 +00005620 skb = netdev_alloc_skb(tp->dev,
5621 len + TG3_RAW_IP_ALIGN);
5622 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623 goto drop_it_no_recycle;
5624
Eric Dumazet9205fd92011-11-18 06:47:01 +00005625 skb_reserve(skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005627 memcpy(skb->data,
5628 data + TG3_RX_OFFSET(tp),
5629 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631 }
5632
Eric Dumazet9205fd92011-11-18 06:47:01 +00005633 skb_put(skb, len);
Michał Mirosławdc668912011-04-07 03:35:07 +00005634 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
5636 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
5637 >> RXD_TCPCSUM_SHIFT) == 0xffff))
5638 skb->ip_summed = CHECKSUM_UNNECESSARY;
5639 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005640 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641
5642 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00005643
5644 if (len > (tp->dev->mtu + ETH_HLEN) &&
5645 skb->protocol != htons(ETH_P_8021Q)) {
5646 dev_kfree_skb(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00005647 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00005648 }
5649
Matt Carlson9dc7a112010-04-12 06:58:28 +00005650 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00005651 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
5652 __vlan_hwaccel_put_tag(skb,
5653 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00005654
Matt Carlsonbf933c82011-01-25 15:58:49 +00005655 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657 received++;
5658 budget--;
5659
5660next_pkt:
5661 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07005662
5663 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005664 tpr->rx_std_prod_idx = std_prod_idx &
5665 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00005666 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5667 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07005668 work_mask &= ~RXD_OPAQUE_RING_STD;
5669 rx_std_posted = 0;
5670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07005672 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00005673 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07005674
5675 /* Refresh hw_idx to see if there is new work */
5676 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005677 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07005678 rmb();
5679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680 }
5681
5682 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00005683 tnapi->rx_rcb_ptr = sw_idx;
5684 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685
5686 /* Refill RX ring(s). */
Joe Perches63c3a662011-04-26 08:12:10 +00005687 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005688 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005689 tpr->rx_std_prod_idx = std_prod_idx &
5690 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005691 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5692 tpr->rx_std_prod_idx);
5693 }
5694 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005695 tpr->rx_jmb_prod_idx = jmb_prod_idx &
5696 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005697 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5698 tpr->rx_jmb_prod_idx);
5699 }
5700 mmiowb();
5701 } else if (work_mask) {
5702 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
5703 * updated before the producer indices can be updated.
5704 */
5705 smp_wmb();
5706
Matt Carlson2c49a442010-09-30 10:34:35 +00005707 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
5708 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005709
Matt Carlsone4af1af2010-02-12 14:47:05 +00005710 if (tnapi != &tp->napi[1])
5711 napi_schedule(&tp->napi[1].napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713
5714 return received;
5715}
5716
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005717static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719 /* handle link change and other phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00005720 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005721 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
5722
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723 if (sblk->status & SD_STATUS_LINK_CHG) {
5724 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005725 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07005726 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00005727 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsondd477002008-05-25 23:45:58 -07005728 tw32_f(MAC_STATUS,
5729 (MAC_STATUS_SYNC_CHANGED |
5730 MAC_STATUS_CFG_CHANGED |
5731 MAC_STATUS_MI_COMPLETION |
5732 MAC_STATUS_LNKSTATE_CHANGED));
5733 udelay(40);
5734 } else
5735 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07005736 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737 }
5738 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005739}
5740
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005741static int tg3_rx_prodring_xfer(struct tg3 *tp,
5742 struct tg3_rx_prodring_set *dpr,
5743 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005744{
5745 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005746 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005747
5748 while (1) {
5749 src_prod_idx = spr->rx_std_prod_idx;
5750
5751 /* Make sure updates to the rx_std_buffers[] entries and the
5752 * standard producer index are seen in the correct order.
5753 */
5754 smp_rmb();
5755
5756 if (spr->rx_std_cons_idx == src_prod_idx)
5757 break;
5758
5759 if (spr->rx_std_cons_idx < src_prod_idx)
5760 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
5761 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005762 cpycnt = tp->rx_std_ring_mask + 1 -
5763 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005764
Matt Carlson2c49a442010-09-30 10:34:35 +00005765 cpycnt = min(cpycnt,
5766 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005767
5768 si = spr->rx_std_cons_idx;
5769 di = dpr->rx_std_prod_idx;
5770
Matt Carlsone92967b2010-02-12 14:47:06 +00005771 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00005772 if (dpr->rx_std_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00005773 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005774 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005775 break;
5776 }
5777 }
5778
5779 if (!cpycnt)
5780 break;
5781
5782 /* Ensure that updates to the rx_std_buffers ring and the
5783 * shadowed hardware producer ring from tg3_recycle_skb() are
5784 * ordered correctly WRT the skb check above.
5785 */
5786 smp_rmb();
5787
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005788 memcpy(&dpr->rx_std_buffers[di],
5789 &spr->rx_std_buffers[si],
5790 cpycnt * sizeof(struct ring_info));
5791
5792 for (i = 0; i < cpycnt; i++, di++, si++) {
5793 struct tg3_rx_buffer_desc *sbd, *dbd;
5794 sbd = &spr->rx_std[si];
5795 dbd = &dpr->rx_std[di];
5796 dbd->addr_hi = sbd->addr_hi;
5797 dbd->addr_lo = sbd->addr_lo;
5798 }
5799
Matt Carlson2c49a442010-09-30 10:34:35 +00005800 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
5801 tp->rx_std_ring_mask;
5802 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
5803 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005804 }
5805
5806 while (1) {
5807 src_prod_idx = spr->rx_jmb_prod_idx;
5808
5809 /* Make sure updates to the rx_jmb_buffers[] entries and
5810 * the jumbo producer index are seen in the correct order.
5811 */
5812 smp_rmb();
5813
5814 if (spr->rx_jmb_cons_idx == src_prod_idx)
5815 break;
5816
5817 if (spr->rx_jmb_cons_idx < src_prod_idx)
5818 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
5819 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005820 cpycnt = tp->rx_jmb_ring_mask + 1 -
5821 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005822
5823 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00005824 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005825
5826 si = spr->rx_jmb_cons_idx;
5827 di = dpr->rx_jmb_prod_idx;
5828
Matt Carlsone92967b2010-02-12 14:47:06 +00005829 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00005830 if (dpr->rx_jmb_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00005831 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005832 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005833 break;
5834 }
5835 }
5836
5837 if (!cpycnt)
5838 break;
5839
5840 /* Ensure that updates to the rx_jmb_buffers ring and the
5841 * shadowed hardware producer ring from tg3_recycle_skb() are
5842 * ordered correctly WRT the skb check above.
5843 */
5844 smp_rmb();
5845
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005846 memcpy(&dpr->rx_jmb_buffers[di],
5847 &spr->rx_jmb_buffers[si],
5848 cpycnt * sizeof(struct ring_info));
5849
5850 for (i = 0; i < cpycnt; i++, di++, si++) {
5851 struct tg3_rx_buffer_desc *sbd, *dbd;
5852 sbd = &spr->rx_jmb[si].std;
5853 dbd = &dpr->rx_jmb[di].std;
5854 dbd->addr_hi = sbd->addr_hi;
5855 dbd->addr_lo = sbd->addr_lo;
5856 }
5857
Matt Carlson2c49a442010-09-30 10:34:35 +00005858 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
5859 tp->rx_jmb_ring_mask;
5860 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
5861 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005862 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005863
5864 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005865}
5866
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005867static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
5868{
5869 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870
5871 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005872 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00005873 tg3_tx(tnapi);
Joe Perches63c3a662011-04-26 08:12:10 +00005874 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Michael Chan4fd7ab52007-10-12 01:39:50 -07005875 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005876 }
5877
Linus Torvalds1da177e2005-04-16 15:20:36 -07005878 /* run RX thread, within the bounds set by NAPI.
5879 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005880 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07005881 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005882 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00005883 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005884
Joe Perches63c3a662011-04-26 08:12:10 +00005885 if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005886 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005887 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00005888 u32 std_prod_idx = dpr->rx_std_prod_idx;
5889 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005890
Matt Carlsone4af1af2010-02-12 14:47:05 +00005891 for (i = 1; i < tp->irq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005892 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00005893 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005894
5895 wmb();
5896
Matt Carlsone4af1af2010-02-12 14:47:05 +00005897 if (std_prod_idx != dpr->rx_std_prod_idx)
5898 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5899 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005900
Matt Carlsone4af1af2010-02-12 14:47:05 +00005901 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
5902 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5903 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005904
5905 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005906
5907 if (err)
5908 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005909 }
5910
David S. Miller6f535762007-10-11 18:08:29 -07005911 return work_done;
5912}
David S. Millerf7383c22005-05-18 22:50:53 -07005913
Matt Carlsondb219972011-11-04 09:15:03 +00005914static inline void tg3_reset_task_schedule(struct tg3 *tp)
5915{
5916 if (!test_and_set_bit(TG3_FLAG_RESET_TASK_PENDING, tp->tg3_flags))
5917 schedule_work(&tp->reset_task);
5918}
5919
5920static inline void tg3_reset_task_cancel(struct tg3 *tp)
5921{
5922 cancel_work_sync(&tp->reset_task);
5923 tg3_flag_clear(tp, RESET_TASK_PENDING);
5924}
5925
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005926static int tg3_poll_msix(struct napi_struct *napi, int budget)
5927{
5928 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5929 struct tg3 *tp = tnapi->tp;
5930 int work_done = 0;
5931 struct tg3_hw_status *sblk = tnapi->hw_status;
5932
5933 while (1) {
5934 work_done = tg3_poll_work(tnapi, work_done, budget);
5935
Joe Perches63c3a662011-04-26 08:12:10 +00005936 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005937 goto tx_recovery;
5938
5939 if (unlikely(work_done >= budget))
5940 break;
5941
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005942 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005943 * to tell the hw how much work has been processed,
5944 * so we must read it before checking for more work.
5945 */
5946 tnapi->last_tag = sblk->status_tag;
5947 tnapi->last_irq_tag = tnapi->last_tag;
5948 rmb();
5949
5950 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00005951 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
5952 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005953 napi_complete(napi);
5954 /* Reenable interrupts. */
5955 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
5956 mmiowb();
5957 break;
5958 }
5959 }
5960
5961 return work_done;
5962
5963tx_recovery:
5964 /* work_done is guaranteed to be less than budget. */
5965 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00005966 tg3_reset_task_schedule(tp);
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005967 return work_done;
5968}
5969
Matt Carlsone64de4e2011-04-13 11:05:05 +00005970static void tg3_process_error(struct tg3 *tp)
5971{
5972 u32 val;
5973 bool real_error = false;
5974
Joe Perches63c3a662011-04-26 08:12:10 +00005975 if (tg3_flag(tp, ERROR_PROCESSED))
Matt Carlsone64de4e2011-04-13 11:05:05 +00005976 return;
5977
5978 /* Check Flow Attention register */
5979 val = tr32(HOSTCC_FLOW_ATTN);
5980 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
5981 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
5982 real_error = true;
5983 }
5984
5985 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
5986 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
5987 real_error = true;
5988 }
5989
5990 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
5991 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
5992 real_error = true;
5993 }
5994
5995 if (!real_error)
5996 return;
5997
5998 tg3_dump_state(tp);
5999
Joe Perches63c3a662011-04-26 08:12:10 +00006000 tg3_flag_set(tp, ERROR_PROCESSED);
Matt Carlsondb219972011-11-04 09:15:03 +00006001 tg3_reset_task_schedule(tp);
Matt Carlsone64de4e2011-04-13 11:05:05 +00006002}
6003
David S. Miller6f535762007-10-11 18:08:29 -07006004static int tg3_poll(struct napi_struct *napi, int budget)
6005{
Matt Carlson8ef04422009-08-28 14:01:37 +00006006 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
6007 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07006008 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00006009 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07006010
6011 while (1) {
Matt Carlsone64de4e2011-04-13 11:05:05 +00006012 if (sblk->status & SD_STATUS_ERROR)
6013 tg3_process_error(tp);
6014
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006015 tg3_poll_link(tp);
6016
Matt Carlson17375d22009-08-28 14:02:18 +00006017 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07006018
Joe Perches63c3a662011-04-26 08:12:10 +00006019 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
David S. Miller6f535762007-10-11 18:08:29 -07006020 goto tx_recovery;
6021
6022 if (unlikely(work_done >= budget))
6023 break;
6024
Joe Perches63c3a662011-04-26 08:12:10 +00006025 if (tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson17375d22009-08-28 14:02:18 +00006026 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07006027 * to tell the hw how much work has been processed,
6028 * so we must read it before checking for more work.
6029 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006030 tnapi->last_tag = sblk->status_tag;
6031 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07006032 rmb();
6033 } else
6034 sblk->status &= ~SD_STATUS_UPDATED;
6035
Matt Carlson17375d22009-08-28 14:02:18 +00006036 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08006037 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00006038 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07006039 break;
6040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006041 }
6042
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006043 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07006044
6045tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07006046 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08006047 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00006048 tg3_reset_task_schedule(tp);
Michael Chan4fd7ab52007-10-12 01:39:50 -07006049 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006050}
6051
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006052static void tg3_napi_disable(struct tg3 *tp)
6053{
6054 int i;
6055
6056 for (i = tp->irq_cnt - 1; i >= 0; i--)
6057 napi_disable(&tp->napi[i].napi);
6058}
6059
6060static void tg3_napi_enable(struct tg3 *tp)
6061{
6062 int i;
6063
6064 for (i = 0; i < tp->irq_cnt; i++)
6065 napi_enable(&tp->napi[i].napi);
6066}
6067
6068static void tg3_napi_init(struct tg3 *tp)
6069{
6070 int i;
6071
6072 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
6073 for (i = 1; i < tp->irq_cnt; i++)
6074 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
6075}
6076
6077static void tg3_napi_fini(struct tg3 *tp)
6078{
6079 int i;
6080
6081 for (i = 0; i < tp->irq_cnt; i++)
6082 netif_napi_del(&tp->napi[i].napi);
6083}
6084
6085static inline void tg3_netif_stop(struct tg3 *tp)
6086{
6087 tp->dev->trans_start = jiffies; /* prevent tx timeout */
6088 tg3_napi_disable(tp);
6089 netif_tx_disable(tp->dev);
6090}
6091
6092static inline void tg3_netif_start(struct tg3 *tp)
6093{
6094 /* NOTE: unconditional netif_tx_wake_all_queues is only
6095 * appropriate so long as all callers are assured to
6096 * have free tx slots (such as after tg3_init_hw)
6097 */
6098 netif_tx_wake_all_queues(tp->dev);
6099
6100 tg3_napi_enable(tp);
6101 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
6102 tg3_enable_ints(tp);
6103}
6104
David S. Millerf47c11e2005-06-24 20:18:35 -07006105static void tg3_irq_quiesce(struct tg3 *tp)
6106{
Matt Carlson4f125f42009-09-01 12:55:02 +00006107 int i;
6108
David S. Millerf47c11e2005-06-24 20:18:35 -07006109 BUG_ON(tp->irq_sync);
6110
6111 tp->irq_sync = 1;
6112 smp_mb();
6113
Matt Carlson4f125f42009-09-01 12:55:02 +00006114 for (i = 0; i < tp->irq_cnt; i++)
6115 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07006116}
6117
David S. Millerf47c11e2005-06-24 20:18:35 -07006118/* Fully shutdown all tg3 driver activity elsewhere in the system.
6119 * If irq_sync is non-zero, then the IRQ handler must be synchronized
6120 * with as well. Most of the time, this is not necessary except when
6121 * shutting down the device.
6122 */
6123static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
6124{
Michael Chan46966542007-07-11 19:47:19 -07006125 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07006126 if (irq_sync)
6127 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07006128}
6129
6130static inline void tg3_full_unlock(struct tg3 *tp)
6131{
David S. Millerf47c11e2005-06-24 20:18:35 -07006132 spin_unlock_bh(&tp->lock);
6133}
6134
Michael Chanfcfa0a32006-03-20 22:28:41 -08006135/* One-shot MSI handler - Chip automatically disables interrupt
6136 * after sending MSI so driver doesn't have to do it.
6137 */
David Howells7d12e782006-10-05 14:55:46 +01006138static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08006139{
Matt Carlson09943a12009-08-28 14:01:57 +00006140 struct tg3_napi *tnapi = dev_id;
6141 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08006142
Matt Carlson898a56f2009-08-28 14:02:40 +00006143 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006144 if (tnapi->rx_rcb)
6145 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08006146
6147 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00006148 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08006149
6150 return IRQ_HANDLED;
6151}
6152
Michael Chan88b06bc22005-04-21 17:13:25 -07006153/* MSI ISR - No need to check for interrupt sharing and no need to
6154 * flush status block and interrupt mailbox. PCI ordering rules
6155 * guarantee that MSI will arrive after the status block.
6156 */
David Howells7d12e782006-10-05 14:55:46 +01006157static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07006158{
Matt Carlson09943a12009-08-28 14:01:57 +00006159 struct tg3_napi *tnapi = dev_id;
6160 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07006161
Matt Carlson898a56f2009-08-28 14:02:40 +00006162 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006163 if (tnapi->rx_rcb)
6164 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07006165 /*
David S. Millerfac9b832005-05-18 22:46:34 -07006166 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07006167 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07006168 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07006169 * NIC to stop sending us irqs, engaging "in-intr-handler"
6170 * event coalescing.
6171 */
Matt Carlson5b39de92011-08-31 11:44:50 +00006172 tw32_mailbox(tnapi->int_mbox, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07006173 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00006174 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07006175
Michael Chan88b06bc22005-04-21 17:13:25 -07006176 return IRQ_RETVAL(1);
6177}
6178
David Howells7d12e782006-10-05 14:55:46 +01006179static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006180{
Matt Carlson09943a12009-08-28 14:01:57 +00006181 struct tg3_napi *tnapi = dev_id;
6182 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006183 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006184 unsigned int handled = 1;
6185
Linus Torvalds1da177e2005-04-16 15:20:36 -07006186 /* In INTx mode, it is possible for the interrupt to arrive at
6187 * the CPU before the status block posted prior to the interrupt.
6188 * Reading the PCI State register will confirm whether the
6189 * interrupt is ours and will flush the status block.
6190 */
Michael Chand18edcb2007-03-24 20:57:11 -07006191 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
Joe Perches63c3a662011-04-26 08:12:10 +00006192 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07006193 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
6194 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07006195 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07006196 }
Michael Chand18edcb2007-03-24 20:57:11 -07006197 }
6198
6199 /*
6200 * Writing any value to intr-mbox-0 clears PCI INTA# and
6201 * chip-internal interrupt pending events.
6202 * Writing non-zero to intr-mbox-0 additional tells the
6203 * NIC to stop sending us irqs, engaging "in-intr-handler"
6204 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07006205 *
6206 * Flush the mailbox to de-assert the IRQ immediately to prevent
6207 * spurious interrupts. The flush impacts performance but
6208 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07006209 */
Michael Chanc04cb342007-05-07 00:26:15 -07006210 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07006211 if (tg3_irq_sync(tp))
6212 goto out;
6213 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00006214 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00006215 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00006216 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07006217 } else {
6218 /* No work, shared interrupt perhaps? re-enable
6219 * interrupts, and flush that PCI write
6220 */
6221 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
6222 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07006223 }
David S. Millerf47c11e2005-06-24 20:18:35 -07006224out:
David S. Millerfac9b832005-05-18 22:46:34 -07006225 return IRQ_RETVAL(handled);
6226}
6227
David Howells7d12e782006-10-05 14:55:46 +01006228static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07006229{
Matt Carlson09943a12009-08-28 14:01:57 +00006230 struct tg3_napi *tnapi = dev_id;
6231 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006232 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07006233 unsigned int handled = 1;
6234
David S. Millerfac9b832005-05-18 22:46:34 -07006235 /* In INTx mode, it is possible for the interrupt to arrive at
6236 * the CPU before the status block posted prior to the interrupt.
6237 * Reading the PCI State register will confirm whether the
6238 * interrupt is ours and will flush the status block.
6239 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006240 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Joe Perches63c3a662011-04-26 08:12:10 +00006241 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07006242 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
6243 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07006244 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006245 }
Michael Chand18edcb2007-03-24 20:57:11 -07006246 }
6247
6248 /*
6249 * writing any value to intr-mbox-0 clears PCI INTA# and
6250 * chip-internal interrupt pending events.
6251 * writing non-zero to intr-mbox-0 additional tells the
6252 * NIC to stop sending us irqs, engaging "in-intr-handler"
6253 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07006254 *
6255 * Flush the mailbox to de-assert the IRQ immediately to prevent
6256 * spurious interrupts. The flush impacts performance but
6257 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07006258 */
Michael Chanc04cb342007-05-07 00:26:15 -07006259 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00006260
6261 /*
6262 * In a shared interrupt configuration, sometimes other devices'
6263 * interrupts will scream. We record the current status tag here
6264 * so that the above check can report that the screaming interrupts
6265 * are unhandled. Eventually they will be silenced.
6266 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006267 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00006268
Michael Chand18edcb2007-03-24 20:57:11 -07006269 if (tg3_irq_sync(tp))
6270 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00006271
Matt Carlson72334482009-08-28 14:03:01 +00006272 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00006273
Matt Carlson09943a12009-08-28 14:01:57 +00006274 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00006275
David S. Millerf47c11e2005-06-24 20:18:35 -07006276out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006277 return IRQ_RETVAL(handled);
6278}
6279
Michael Chan79381092005-04-21 17:13:59 -07006280/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01006281static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07006282{
Matt Carlson09943a12009-08-28 14:01:57 +00006283 struct tg3_napi *tnapi = dev_id;
6284 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006285 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07006286
Michael Chanf9804dd2005-09-27 12:13:10 -07006287 if ((sblk->status & SD_STATUS_UPDATED) ||
6288 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07006289 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07006290 return IRQ_RETVAL(1);
6291 }
6292 return IRQ_RETVAL(0);
6293}
6294
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07006295static int tg3_init_hw(struct tg3 *, int);
Michael Chan944d9802005-05-29 14:57:48 -07006296static int tg3_halt(struct tg3 *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006297
Michael Chanb9ec6c12006-07-25 16:37:27 -07006298/* Restart hardware after configuration changes, self-test, etc.
6299 * Invoked with tp->lock held.
6300 */
6301static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
Eric Dumazet78c61462008-04-24 23:33:06 -07006302 __releases(tp->lock)
6303 __acquires(tp->lock)
Michael Chanb9ec6c12006-07-25 16:37:27 -07006304{
6305 int err;
6306
6307 err = tg3_init_hw(tp, reset_phy);
6308 if (err) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006309 netdev_err(tp->dev,
6310 "Failed to re-initialize device, aborting\n");
Michael Chanb9ec6c12006-07-25 16:37:27 -07006311 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
6312 tg3_full_unlock(tp);
6313 del_timer_sync(&tp->timer);
6314 tp->irq_sync = 0;
Matt Carlsonfed97812009-09-01 13:10:19 +00006315 tg3_napi_enable(tp);
Michael Chanb9ec6c12006-07-25 16:37:27 -07006316 dev_close(tp->dev);
6317 tg3_full_lock(tp, 0);
6318 }
6319 return err;
6320}
6321
Linus Torvalds1da177e2005-04-16 15:20:36 -07006322#ifdef CONFIG_NET_POLL_CONTROLLER
6323static void tg3_poll_controller(struct net_device *dev)
6324{
Matt Carlson4f125f42009-09-01 12:55:02 +00006325 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07006326 struct tg3 *tp = netdev_priv(dev);
6327
Matt Carlson4f125f42009-09-01 12:55:02 +00006328 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00006329 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330}
6331#endif
6332
David Howellsc4028952006-11-22 14:57:56 +00006333static void tg3_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006334{
David Howellsc4028952006-11-22 14:57:56 +00006335 struct tg3 *tp = container_of(work, struct tg3, reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006336 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006337
Michael Chan7faa0062006-02-02 17:29:28 -08006338 tg3_full_lock(tp, 0);
Michael Chan7faa0062006-02-02 17:29:28 -08006339
6340 if (!netif_running(tp->dev)) {
Matt Carlsondb219972011-11-04 09:15:03 +00006341 tg3_flag_clear(tp, RESET_TASK_PENDING);
Michael Chan7faa0062006-02-02 17:29:28 -08006342 tg3_full_unlock(tp);
6343 return;
6344 }
6345
6346 tg3_full_unlock(tp);
6347
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006348 tg3_phy_stop(tp);
6349
Linus Torvalds1da177e2005-04-16 15:20:36 -07006350 tg3_netif_stop(tp);
6351
David S. Millerf47c11e2005-06-24 20:18:35 -07006352 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006353
Joe Perches63c3a662011-04-26 08:12:10 +00006354 if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
Michael Chandf3e6542006-05-26 17:48:07 -07006355 tp->write32_tx_mbox = tg3_write32_tx_mbox;
6356 tp->write32_rx_mbox = tg3_write_flush_reg32;
Joe Perches63c3a662011-04-26 08:12:10 +00006357 tg3_flag_set(tp, MBOX_WRITE_REORDER);
6358 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07006359 }
6360
Michael Chan944d9802005-05-29 14:57:48 -07006361 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006362 err = tg3_init_hw(tp, 1);
6363 if (err)
Michael Chanb9ec6c12006-07-25 16:37:27 -07006364 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006365
6366 tg3_netif_start(tp);
6367
Michael Chanb9ec6c12006-07-25 16:37:27 -07006368out:
Michael Chan7faa0062006-02-02 17:29:28 -08006369 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006370
6371 if (!err)
6372 tg3_phy_start(tp);
Matt Carlsondb219972011-11-04 09:15:03 +00006373
6374 tg3_flag_clear(tp, RESET_TASK_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006375}
6376
6377static void tg3_tx_timeout(struct net_device *dev)
6378{
6379 struct tg3 *tp = netdev_priv(dev);
6380
Michael Chanb0408752007-02-13 12:18:30 -08006381 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00006382 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00006383 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08006384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006385
Matt Carlsondb219972011-11-04 09:15:03 +00006386 tg3_reset_task_schedule(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006387}
6388
Michael Chanc58ec932005-09-17 00:46:27 -07006389/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
6390static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
6391{
6392 u32 base = (u32) mapping & 0xffffffff;
6393
Eric Dumazet807540b2010-09-23 05:40:09 +00006394 return (base > 0xffffdcc0) && (base + len + 8 < base);
Michael Chanc58ec932005-09-17 00:46:27 -07006395}
6396
Michael Chan72f2afb2006-03-06 19:28:35 -08006397/* Test for DMA addresses > 40-bit */
6398static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
6399 int len)
6400{
6401#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Joe Perches63c3a662011-04-26 08:12:10 +00006402 if (tg3_flag(tp, 40BIT_DMA_BUG))
Eric Dumazet807540b2010-09-23 05:40:09 +00006403 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08006404 return 0;
6405#else
6406 return 0;
6407#endif
6408}
6409
Matt Carlsond1a3b732011-07-27 14:20:51 +00006410static inline void tg3_tx_set_bd(struct tg3_tx_buffer_desc *txbd,
Matt Carlson92cd3a12011-07-27 14:20:47 +00006411 dma_addr_t mapping, u32 len, u32 flags,
6412 u32 mss, u32 vlan)
Matt Carlson2ffcc982011-05-19 12:12:44 +00006413{
Matt Carlson92cd3a12011-07-27 14:20:47 +00006414 txbd->addr_hi = ((u64) mapping >> 32);
6415 txbd->addr_lo = ((u64) mapping & 0xffffffff);
6416 txbd->len_flags = (len << TXD_LEN_SHIFT) | (flags & 0x0000ffff);
6417 txbd->vlan_tag = (mss << TXD_MSS_SHIFT) | (vlan << TXD_VLAN_TAG_SHIFT);
Matt Carlson2ffcc982011-05-19 12:12:44 +00006418}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006419
Matt Carlson84b67b22011-07-27 14:20:52 +00006420static bool tg3_tx_frag_set(struct tg3_napi *tnapi, u32 *entry, u32 *budget,
Matt Carlsond1a3b732011-07-27 14:20:51 +00006421 dma_addr_t map, u32 len, u32 flags,
6422 u32 mss, u32 vlan)
6423{
6424 struct tg3 *tp = tnapi->tp;
6425 bool hwbug = false;
6426
6427 if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
Rusty Russell3db1cd52011-12-19 13:56:45 +00006428 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00006429
6430 if (tg3_4g_overflow_test(map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00006431 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00006432
6433 if (tg3_40bit_overflow_test(tp, map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00006434 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00006435
Matt Carlsona4cb4282011-12-14 11:09:58 +00006436 if (tp->dma_limit) {
Matt Carlsonb9e45482011-11-04 09:14:59 +00006437 u32 prvidx = *entry;
Matt Carlsone31aa982011-07-27 14:20:53 +00006438 u32 tmp_flag = flags & ~TXD_FLAG_END;
Matt Carlsona4cb4282011-12-14 11:09:58 +00006439 while (len > tp->dma_limit && *budget) {
6440 u32 frag_len = tp->dma_limit;
6441 len -= tp->dma_limit;
Matt Carlsone31aa982011-07-27 14:20:53 +00006442
Matt Carlsonb9e45482011-11-04 09:14:59 +00006443 /* Avoid the 8byte DMA problem */
6444 if (len <= 8) {
Matt Carlsona4cb4282011-12-14 11:09:58 +00006445 len += tp->dma_limit / 2;
6446 frag_len = tp->dma_limit / 2;
Matt Carlsone31aa982011-07-27 14:20:53 +00006447 }
6448
Matt Carlsonb9e45482011-11-04 09:14:59 +00006449 tnapi->tx_buffers[*entry].fragmented = true;
6450
6451 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6452 frag_len, tmp_flag, mss, vlan);
6453 *budget -= 1;
6454 prvidx = *entry;
6455 *entry = NEXT_TX(*entry);
6456
Matt Carlsone31aa982011-07-27 14:20:53 +00006457 map += frag_len;
6458 }
6459
6460 if (len) {
6461 if (*budget) {
6462 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6463 len, flags, mss, vlan);
Matt Carlsonb9e45482011-11-04 09:14:59 +00006464 *budget -= 1;
Matt Carlsone31aa982011-07-27 14:20:53 +00006465 *entry = NEXT_TX(*entry);
6466 } else {
Rusty Russell3db1cd52011-12-19 13:56:45 +00006467 hwbug = true;
Matt Carlsonb9e45482011-11-04 09:14:59 +00006468 tnapi->tx_buffers[prvidx].fragmented = false;
Matt Carlsone31aa982011-07-27 14:20:53 +00006469 }
6470 }
6471 } else {
Matt Carlson84b67b22011-07-27 14:20:52 +00006472 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6473 len, flags, mss, vlan);
Matt Carlsone31aa982011-07-27 14:20:53 +00006474 *entry = NEXT_TX(*entry);
6475 }
Matt Carlsond1a3b732011-07-27 14:20:51 +00006476
6477 return hwbug;
6478}
6479
Matt Carlson0d681b22011-07-27 14:20:49 +00006480static void tg3_tx_skb_unmap(struct tg3_napi *tnapi, u32 entry, int last)
Matt Carlson432aa7e2011-05-19 12:12:45 +00006481{
6482 int i;
Matt Carlson0d681b22011-07-27 14:20:49 +00006483 struct sk_buff *skb;
Matt Carlsondf8944c2011-07-27 14:20:46 +00006484 struct tg3_tx_ring_info *txb = &tnapi->tx_buffers[entry];
Matt Carlson432aa7e2011-05-19 12:12:45 +00006485
Matt Carlson0d681b22011-07-27 14:20:49 +00006486 skb = txb->skb;
6487 txb->skb = NULL;
6488
Matt Carlson432aa7e2011-05-19 12:12:45 +00006489 pci_unmap_single(tnapi->tp->pdev,
6490 dma_unmap_addr(txb, mapping),
6491 skb_headlen(skb),
6492 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00006493
6494 while (txb->fragmented) {
6495 txb->fragmented = false;
6496 entry = NEXT_TX(entry);
6497 txb = &tnapi->tx_buffers[entry];
6498 }
6499
Matt Carlsonba1142e2011-11-04 09:15:00 +00006500 for (i = 0; i <= last; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00006501 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Matt Carlson432aa7e2011-05-19 12:12:45 +00006502
6503 entry = NEXT_TX(entry);
6504 txb = &tnapi->tx_buffers[entry];
6505
6506 pci_unmap_page(tnapi->tp->pdev,
6507 dma_unmap_addr(txb, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00006508 skb_frag_size(frag), PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00006509
6510 while (txb->fragmented) {
6511 txb->fragmented = false;
6512 entry = NEXT_TX(entry);
6513 txb = &tnapi->tx_buffers[entry];
6514 }
Matt Carlson432aa7e2011-05-19 12:12:45 +00006515 }
6516}
6517
Michael Chan72f2afb2006-03-06 19:28:35 -08006518/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006519static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
David S. Miller1805b2f2011-10-24 18:18:09 -04006520 struct sk_buff **pskb,
Matt Carlson84b67b22011-07-27 14:20:52 +00006521 u32 *entry, u32 *budget,
Matt Carlson92cd3a12011-07-27 14:20:47 +00006522 u32 base_flags, u32 mss, u32 vlan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006523{
Matt Carlson24f4efd2009-11-13 13:03:35 +00006524 struct tg3 *tp = tnapi->tp;
David S. Miller1805b2f2011-10-24 18:18:09 -04006525 struct sk_buff *new_skb, *skb = *pskb;
Michael Chanc58ec932005-09-17 00:46:27 -07006526 dma_addr_t new_addr = 0;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006527 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006528
Matt Carlson41588ba2008-04-19 18:12:33 -07006529 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
6530 new_skb = skb_copy(skb, GFP_ATOMIC);
6531 else {
6532 int more_headroom = 4 - ((unsigned long)skb->data & 3);
6533
6534 new_skb = skb_copy_expand(skb,
6535 skb_headroom(skb) + more_headroom,
6536 skb_tailroom(skb), GFP_ATOMIC);
6537 }
6538
Linus Torvalds1da177e2005-04-16 15:20:36 -07006539 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07006540 ret = -1;
6541 } else {
6542 /* New SKB is guaranteed to be linear. */
Alexander Duyckf4188d82009-12-02 16:48:38 +00006543 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
6544 PCI_DMA_TODEVICE);
6545 /* Make sure the mapping succeeded */
6546 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00006547 dev_kfree_skb(new_skb);
Michael Chanc58ec932005-09-17 00:46:27 -07006548 ret = -1;
Michael Chanc58ec932005-09-17 00:46:27 -07006549 } else {
Matt Carlsonb9e45482011-11-04 09:14:59 +00006550 u32 save_entry = *entry;
6551
Matt Carlson92cd3a12011-07-27 14:20:47 +00006552 base_flags |= TXD_FLAG_END;
6553
Matt Carlson84b67b22011-07-27 14:20:52 +00006554 tnapi->tx_buffers[*entry].skb = new_skb;
6555 dma_unmap_addr_set(&tnapi->tx_buffers[*entry],
Matt Carlson432aa7e2011-05-19 12:12:45 +00006556 mapping, new_addr);
6557
Matt Carlson84b67b22011-07-27 14:20:52 +00006558 if (tg3_tx_frag_set(tnapi, entry, budget, new_addr,
Matt Carlsond1a3b732011-07-27 14:20:51 +00006559 new_skb->len, base_flags,
6560 mss, vlan)) {
Matt Carlsonba1142e2011-11-04 09:15:00 +00006561 tg3_tx_skb_unmap(tnapi, save_entry, -1);
Matt Carlsond1a3b732011-07-27 14:20:51 +00006562 dev_kfree_skb(new_skb);
6563 ret = -1;
6564 }
Michael Chanc58ec932005-09-17 00:46:27 -07006565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006566 }
6567
Linus Torvalds1da177e2005-04-16 15:20:36 -07006568 dev_kfree_skb(skb);
David S. Miller1805b2f2011-10-24 18:18:09 -04006569 *pskb = new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07006570 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006571}
6572
Matt Carlson2ffcc982011-05-19 12:12:44 +00006573static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07006574
6575/* Use GSO to workaround a rare TSO bug that may be triggered when the
6576 * TSO header is greater than 80 bytes.
6577 */
6578static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
6579{
6580 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006581 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07006582
6583 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006584 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07006585 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006586
6587 /* netif_tx_stop_queue() must be done before checking
6588 * checking tx index in tg3_tx_avail() below, because in
6589 * tg3_tx(), we update tx index before checking for
6590 * netif_tx_queue_stopped().
6591 */
6592 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006593 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08006594 return NETDEV_TX_BUSY;
6595
6596 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07006597 }
6598
6599 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07006600 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07006601 goto tg3_tso_bug_end;
6602
6603 do {
6604 nskb = segs;
6605 segs = segs->next;
6606 nskb->next = NULL;
Matt Carlson2ffcc982011-05-19 12:12:44 +00006607 tg3_start_xmit(nskb, tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07006608 } while (segs);
6609
6610tg3_tso_bug_end:
6611 dev_kfree_skb(skb);
6612
6613 return NETDEV_TX_OK;
6614}
Michael Chan52c0fd82006-06-29 20:15:54 -07006615
Michael Chan5a6f3072006-03-20 22:28:05 -08006616/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
Joe Perches63c3a662011-04-26 08:12:10 +00006617 * support TG3_FLAG_HW_TSO_1 or firmware TSO only.
Michael Chan5a6f3072006-03-20 22:28:05 -08006618 */
Matt Carlson2ffcc982011-05-19 12:12:44 +00006619static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08006620{
6621 struct tg3 *tp = netdev_priv(dev);
Matt Carlson92cd3a12011-07-27 14:20:47 +00006622 u32 len, entry, base_flags, mss, vlan = 0;
Matt Carlson84b67b22011-07-27 14:20:52 +00006623 u32 budget;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006624 int i = -1, would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07006625 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00006626 struct tg3_napi *tnapi;
6627 struct netdev_queue *txq;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006628 unsigned int last;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006629
Matt Carlson24f4efd2009-11-13 13:03:35 +00006630 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
6631 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Joe Perches63c3a662011-04-26 08:12:10 +00006632 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006633 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006634
Matt Carlson84b67b22011-07-27 14:20:52 +00006635 budget = tg3_tx_avail(tnapi);
6636
Michael Chan00b70502006-06-17 21:58:45 -07006637 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006638 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07006639 * interrupt. Furthermore, IRQ processing runs lockless so we have
6640 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07006641 */
Matt Carlson84b67b22011-07-27 14:20:52 +00006642 if (unlikely(budget <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006643 if (!netif_tx_queue_stopped(txq)) {
6644 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08006645
6646 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00006647 netdev_err(dev,
6648 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08006649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006650 return NETDEV_TX_BUSY;
6651 }
6652
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006653 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006654 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07006655 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006656 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00006657
Matt Carlsonbe98da62010-07-11 09:31:46 +00006658 mss = skb_shinfo(skb)->gso_size;
6659 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006660 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00006661 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006662
6663 if (skb_header_cloned(skb) &&
Eric Dumazet48855432011-10-24 07:53:03 +00006664 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
6665 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006666
Matt Carlson34195c32010-07-11 09:31:42 +00006667 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07006668 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006669
Matt Carlson02e96082010-09-15 08:59:59 +00006670 if (skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00006671 hdr_len = skb_headlen(skb) - ETH_HLEN;
6672 } else {
6673 u32 ip_tcp_len;
6674
6675 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
6676 hdr_len = ip_tcp_len + tcp_opt_len;
6677
6678 iph->check = 0;
6679 iph->tot_len = htons(mss + hdr_len);
6680 }
6681
Michael Chan52c0fd82006-06-29 20:15:54 -07006682 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Joe Perches63c3a662011-04-26 08:12:10 +00006683 tg3_flag(tp, TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00006684 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07006685
Linus Torvalds1da177e2005-04-16 15:20:36 -07006686 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
6687 TXD_FLAG_CPU_POST_DMA);
6688
Joe Perches63c3a662011-04-26 08:12:10 +00006689 if (tg3_flag(tp, HW_TSO_1) ||
6690 tg3_flag(tp, HW_TSO_2) ||
6691 tg3_flag(tp, HW_TSO_3)) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006692 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006693 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006694 } else
6695 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
6696 iph->daddr, 0,
6697 IPPROTO_TCP,
6698 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006699
Joe Perches63c3a662011-04-26 08:12:10 +00006700 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlson615774f2009-11-13 13:03:39 +00006701 mss |= (hdr_len & 0xc) << 12;
6702 if (hdr_len & 0x10)
6703 base_flags |= 0x00000010;
6704 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +00006705 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006706 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +00006707 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006708 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006709 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006710 int tsflags;
6711
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006712 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006713 mss |= (tsflags << 11);
6714 }
6715 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006716 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006717 int tsflags;
6718
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006719 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006720 base_flags |= tsflags << 12;
6721 }
6722 }
6723 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00006724
Matt Carlson93a700a2011-08-31 11:44:54 +00006725 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
6726 !mss && skb->len > VLAN_ETH_FRAME_LEN)
6727 base_flags |= TXD_FLAG_JMB_PKT;
6728
Matt Carlson92cd3a12011-07-27 14:20:47 +00006729 if (vlan_tx_tag_present(skb)) {
6730 base_flags |= TXD_FLAG_VLAN;
6731 vlan = vlan_tx_tag_get(skb);
6732 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006733
Alexander Duyckf4188d82009-12-02 16:48:38 +00006734 len = skb_headlen(skb);
6735
6736 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
Eric Dumazet48855432011-10-24 07:53:03 +00006737 if (pci_dma_mapping_error(tp->pdev, mapping))
6738 goto drop;
6739
David S. Miller90079ce2008-09-11 04:52:51 -07006740
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006741 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006742 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006743
6744 would_hit_hwbug = 0;
6745
Joe Perches63c3a662011-04-26 08:12:10 +00006746 if (tg3_flag(tp, 5701_DMA_BUG))
Michael Chanc58ec932005-09-17 00:46:27 -07006747 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006748
Matt Carlson84b67b22011-07-27 14:20:52 +00006749 if (tg3_tx_frag_set(tnapi, &entry, &budget, mapping, len, base_flags |
Matt Carlsond1a3b732011-07-27 14:20:51 +00006750 ((skb_shinfo(skb)->nr_frags == 0) ? TXD_FLAG_END : 0),
Matt Carlsonba1142e2011-11-04 09:15:00 +00006751 mss, vlan)) {
Matt Carlsond1a3b732011-07-27 14:20:51 +00006752 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006753 /* Now loop through additional data fragments, and queue them. */
Matt Carlsonba1142e2011-11-04 09:15:00 +00006754 } else if (skb_shinfo(skb)->nr_frags > 0) {
Matt Carlson92cd3a12011-07-27 14:20:47 +00006755 u32 tmp_mss = mss;
6756
6757 if (!tg3_flag(tp, HW_TSO_1) &&
6758 !tg3_flag(tp, HW_TSO_2) &&
6759 !tg3_flag(tp, HW_TSO_3))
6760 tmp_mss = 0;
6761
Linus Torvalds1da177e2005-04-16 15:20:36 -07006762 last = skb_shinfo(skb)->nr_frags - 1;
6763 for (i = 0; i <= last; i++) {
6764 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6765
Eric Dumazet9e903e02011-10-18 21:00:24 +00006766 len = skb_frag_size(frag);
Ian Campbelldc234d02011-08-24 22:28:11 +00006767 mapping = skb_frag_dma_map(&tp->pdev->dev, frag, 0,
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01006768 len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006769
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006770 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006771 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00006772 mapping);
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01006773 if (dma_mapping_error(&tp->pdev->dev, mapping))
Alexander Duyckf4188d82009-12-02 16:48:38 +00006774 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006775
Matt Carlsonb9e45482011-11-04 09:14:59 +00006776 if (!budget ||
6777 tg3_tx_frag_set(tnapi, &entry, &budget, mapping,
Matt Carlson84b67b22011-07-27 14:20:52 +00006778 len, base_flags |
6779 ((i == last) ? TXD_FLAG_END : 0),
Matt Carlsonb9e45482011-11-04 09:14:59 +00006780 tmp_mss, vlan)) {
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006781 would_hit_hwbug = 1;
Matt Carlsonb9e45482011-11-04 09:14:59 +00006782 break;
6783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006784 }
6785 }
6786
6787 if (would_hit_hwbug) {
Matt Carlson0d681b22011-07-27 14:20:49 +00006788 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006789
6790 /* If the workaround fails due to memory/mapping
6791 * failure, silently drop this packet.
6792 */
Matt Carlson84b67b22011-07-27 14:20:52 +00006793 entry = tnapi->tx_prod;
6794 budget = tg3_tx_avail(tnapi);
David S. Miller1805b2f2011-10-24 18:18:09 -04006795 if (tigon3_dma_hwbug_workaround(tnapi, &skb, &entry, &budget,
Matt Carlson84b67b22011-07-27 14:20:52 +00006796 base_flags, mss, vlan))
Eric Dumazet48855432011-10-24 07:53:03 +00006797 goto drop_nofree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006798 }
6799
Richard Cochrand515b452011-06-19 03:31:41 +00006800 skb_tx_timestamp(skb);
Tom Herbert298376d2011-11-28 16:33:30 +00006801 netdev_sent_queue(tp->dev, skb->len);
Richard Cochrand515b452011-06-19 03:31:41 +00006802
Linus Torvalds1da177e2005-04-16 15:20:36 -07006803 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006804 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006805
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006806 tnapi->tx_prod = entry;
6807 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006808 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006809
6810 /* netif_tx_stop_queue() must be done before checking
6811 * checking tx index in tg3_tx_avail() below, because in
6812 * tg3_tx(), we update tx index before checking for
6813 * netif_tx_queue_stopped().
6814 */
6815 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006816 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006817 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07006818 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006819
Eric Dumazetcdd0db02009-05-28 00:00:41 +00006820 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006821 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006822
6823dma_error:
Matt Carlsonba1142e2011-11-04 09:15:00 +00006824 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, --i);
Matt Carlson432aa7e2011-05-19 12:12:45 +00006825 tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
Eric Dumazet48855432011-10-24 07:53:03 +00006826drop:
6827 dev_kfree_skb(skb);
6828drop_nofree:
6829 tp->tx_dropped++;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006830 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006831}
6832
Matt Carlson6e01b202011-08-19 13:58:20 +00006833static void tg3_mac_loopback(struct tg3 *tp, bool enable)
6834{
6835 if (enable) {
6836 tp->mac_mode &= ~(MAC_MODE_HALF_DUPLEX |
6837 MAC_MODE_PORT_MODE_MASK);
6838
6839 tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
6840
6841 if (!tg3_flag(tp, 5705_PLUS))
6842 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
6843
6844 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
6845 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
6846 else
6847 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
6848 } else {
6849 tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
6850
6851 if (tg3_flag(tp, 5705_PLUS) ||
6852 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) ||
6853 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
6854 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
6855 }
6856
6857 tw32(MAC_MODE, tp->mac_mode);
6858 udelay(40);
6859}
6860
Matt Carlson941ec902011-08-19 13:58:23 +00006861static int tg3_phy_lpbk_set(struct tg3 *tp, u32 speed, bool extlpbk)
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006862{
Matt Carlson941ec902011-08-19 13:58:23 +00006863 u32 val, bmcr, mac_mode, ptest = 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006864
6865 tg3_phy_toggle_apd(tp, false);
6866 tg3_phy_toggle_automdix(tp, 0);
6867
Matt Carlson941ec902011-08-19 13:58:23 +00006868 if (extlpbk && tg3_phy_set_extloopbk(tp))
6869 return -EIO;
6870
6871 bmcr = BMCR_FULLDPLX;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006872 switch (speed) {
6873 case SPEED_10:
6874 break;
6875 case SPEED_100:
6876 bmcr |= BMCR_SPEED100;
6877 break;
6878 case SPEED_1000:
6879 default:
6880 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
6881 speed = SPEED_100;
6882 bmcr |= BMCR_SPEED100;
6883 } else {
6884 speed = SPEED_1000;
6885 bmcr |= BMCR_SPEED1000;
6886 }
6887 }
6888
Matt Carlson941ec902011-08-19 13:58:23 +00006889 if (extlpbk) {
6890 if (!(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
6891 tg3_readphy(tp, MII_CTRL1000, &val);
6892 val |= CTL1000_AS_MASTER |
6893 CTL1000_ENABLE_MASTER;
6894 tg3_writephy(tp, MII_CTRL1000, val);
6895 } else {
6896 ptest = MII_TG3_FET_PTEST_TRIM_SEL |
6897 MII_TG3_FET_PTEST_TRIM_2;
6898 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest);
6899 }
6900 } else
6901 bmcr |= BMCR_LOOPBACK;
6902
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006903 tg3_writephy(tp, MII_BMCR, bmcr);
6904
6905 /* The write needs to be flushed for the FETs */
6906 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
6907 tg3_readphy(tp, MII_BMCR, &bmcr);
6908
6909 udelay(40);
6910
6911 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
6912 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Matt Carlson941ec902011-08-19 13:58:23 +00006913 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest |
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006914 MII_TG3_FET_PTEST_FRC_TX_LINK |
6915 MII_TG3_FET_PTEST_FRC_TX_LOCK);
6916
6917 /* The write needs to be flushed for the AC131 */
6918 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
6919 }
6920
6921 /* Reset to prevent losing 1st rx packet intermittently */
6922 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
6923 tg3_flag(tp, 5780_CLASS)) {
6924 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
6925 udelay(10);
6926 tw32_f(MAC_RX_MODE, tp->rx_mode);
6927 }
6928
6929 mac_mode = tp->mac_mode &
6930 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
6931 if (speed == SPEED_1000)
6932 mac_mode |= MAC_MODE_PORT_MODE_GMII;
6933 else
6934 mac_mode |= MAC_MODE_PORT_MODE_MII;
6935
6936 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
6937 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
6938
6939 if (masked_phy_id == TG3_PHY_ID_BCM5401)
6940 mac_mode &= ~MAC_MODE_LINK_POLARITY;
6941 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
6942 mac_mode |= MAC_MODE_LINK_POLARITY;
6943
6944 tg3_writephy(tp, MII_TG3_EXT_CTRL,
6945 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
6946 }
6947
6948 tw32(MAC_MODE, mac_mode);
6949 udelay(40);
Matt Carlson941ec902011-08-19 13:58:23 +00006950
6951 return 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006952}
6953
Michał Mirosławc8f44af2011-11-15 15:29:55 +00006954static void tg3_set_loopback(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006955{
6956 struct tg3 *tp = netdev_priv(dev);
6957
6958 if (features & NETIF_F_LOOPBACK) {
6959 if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
6960 return;
6961
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006962 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00006963 tg3_mac_loopback(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006964 netif_carrier_on(tp->dev);
6965 spin_unlock_bh(&tp->lock);
6966 netdev_info(dev, "Internal MAC loopback mode enabled.\n");
6967 } else {
6968 if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
6969 return;
6970
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006971 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00006972 tg3_mac_loopback(tp, false);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006973 /* Force link status check */
6974 tg3_setup_phy(tp, 1);
6975 spin_unlock_bh(&tp->lock);
6976 netdev_info(dev, "Internal MAC loopback mode disabled.\n");
6977 }
6978}
6979
Michał Mirosławc8f44af2011-11-15 15:29:55 +00006980static netdev_features_t tg3_fix_features(struct net_device *dev,
6981 netdev_features_t features)
Michał Mirosławdc668912011-04-07 03:35:07 +00006982{
6983 struct tg3 *tp = netdev_priv(dev);
6984
Joe Perches63c3a662011-04-26 08:12:10 +00006985 if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
Michał Mirosławdc668912011-04-07 03:35:07 +00006986 features &= ~NETIF_F_ALL_TSO;
6987
6988 return features;
6989}
6990
Michał Mirosławc8f44af2011-11-15 15:29:55 +00006991static int tg3_set_features(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006992{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00006993 netdev_features_t changed = dev->features ^ features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006994
6995 if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
6996 tg3_set_loopback(dev, features);
6997
6998 return 0;
6999}
7000
Linus Torvalds1da177e2005-04-16 15:20:36 -07007001static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
7002 int new_mtu)
7003{
7004 dev->mtu = new_mtu;
7005
Michael Chanef7f5ec2005-07-25 12:32:25 -07007006 if (new_mtu > ETH_DATA_LEN) {
Joe Perches63c3a662011-04-26 08:12:10 +00007007 if (tg3_flag(tp, 5780_CLASS)) {
Michał Mirosławdc668912011-04-07 03:35:07 +00007008 netdev_update_features(dev);
Joe Perches63c3a662011-04-26 08:12:10 +00007009 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson859a588792010-04-05 10:19:28 +00007010 } else {
Joe Perches63c3a662011-04-26 08:12:10 +00007011 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Matt Carlson859a588792010-04-05 10:19:28 +00007012 }
Michael Chanef7f5ec2005-07-25 12:32:25 -07007013 } else {
Joe Perches63c3a662011-04-26 08:12:10 +00007014 if (tg3_flag(tp, 5780_CLASS)) {
7015 tg3_flag_set(tp, TSO_CAPABLE);
Michał Mirosławdc668912011-04-07 03:35:07 +00007016 netdev_update_features(dev);
7017 }
Joe Perches63c3a662011-04-26 08:12:10 +00007018 tg3_flag_clear(tp, JUMBO_RING_ENABLE);
Michael Chanef7f5ec2005-07-25 12:32:25 -07007019 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007020}
7021
7022static int tg3_change_mtu(struct net_device *dev, int new_mtu)
7023{
7024 struct tg3 *tp = netdev_priv(dev);
Michael Chanb9ec6c12006-07-25 16:37:27 -07007025 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007026
7027 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
7028 return -EINVAL;
7029
7030 if (!netif_running(dev)) {
7031 /* We'll just catch it later when the
7032 * device is up'd.
7033 */
7034 tg3_set_mtu(dev, tp, new_mtu);
7035 return 0;
7036 }
7037
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07007038 tg3_phy_stop(tp);
7039
Linus Torvalds1da177e2005-04-16 15:20:36 -07007040 tg3_netif_stop(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07007041
7042 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007043
Michael Chan944d9802005-05-29 14:57:48 -07007044 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007045
7046 tg3_set_mtu(dev, tp, new_mtu);
7047
Michael Chanb9ec6c12006-07-25 16:37:27 -07007048 err = tg3_restart_hw(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007049
Michael Chanb9ec6c12006-07-25 16:37:27 -07007050 if (!err)
7051 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007052
David S. Millerf47c11e2005-06-24 20:18:35 -07007053 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007054
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07007055 if (!err)
7056 tg3_phy_start(tp);
7057
Michael Chanb9ec6c12006-07-25 16:37:27 -07007058 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007059}
7060
Matt Carlson21f581a2009-08-28 14:00:25 +00007061static void tg3_rx_prodring_free(struct tg3 *tp,
7062 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007063{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007064 int i;
7065
Matt Carlson8fea32b2010-09-15 08:59:58 +00007066 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007067 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00007068 i = (i + 1) & tp->rx_std_ring_mask)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007069 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007070 tp->rx_pkt_map_sz);
7071
Joe Perches63c3a662011-04-26 08:12:10 +00007072 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007073 for (i = tpr->rx_jmb_cons_idx;
7074 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00007075 i = (i + 1) & tp->rx_jmb_ring_mask) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007076 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007077 TG3_RX_JMB_MAP_SZ);
7078 }
7079 }
7080
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007081 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007083
Matt Carlson2c49a442010-09-30 10:34:35 +00007084 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007085 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007086 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007087
Joe Perches63c3a662011-04-26 08:12:10 +00007088 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007089 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007090 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007091 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007092 }
7093}
7094
Matt Carlsonc6cdf432010-04-05 10:19:26 +00007095/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007096 *
7097 * The chip has been shut down and the driver detached from
7098 * the networking, so no interrupts or new tx packets will
7099 * end up in the driver. tp->{tx,}lock are held and thus
7100 * we may not sleep.
7101 */
Matt Carlson21f581a2009-08-28 14:00:25 +00007102static int tg3_rx_prodring_alloc(struct tg3 *tp,
7103 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007104{
Matt Carlson287be122009-08-28 13:58:46 +00007105 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007106
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007107 tpr->rx_std_cons_idx = 0;
7108 tpr->rx_std_prod_idx = 0;
7109 tpr->rx_jmb_cons_idx = 0;
7110 tpr->rx_jmb_prod_idx = 0;
7111
Matt Carlson8fea32b2010-09-15 08:59:58 +00007112 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007113 memset(&tpr->rx_std_buffers[0], 0,
7114 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00007115 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007116 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00007117 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007118 goto done;
7119 }
7120
Linus Torvalds1da177e2005-04-16 15:20:36 -07007121 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00007122 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007123
Matt Carlson287be122009-08-28 13:58:46 +00007124 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00007125 if (tg3_flag(tp, 5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00007126 tp->dev->mtu > ETH_DATA_LEN)
7127 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
7128 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07007129
Linus Torvalds1da177e2005-04-16 15:20:36 -07007130 /* Initialize invariants of the rings, we only set this
7131 * stuff once. This works because the card does not
7132 * write into the rx buffer posting rings.
7133 */
Matt Carlson2c49a442010-09-30 10:34:35 +00007134 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007135 struct tg3_rx_buffer_desc *rxd;
7136
Matt Carlson21f581a2009-08-28 14:00:25 +00007137 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00007138 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007139 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
7140 rxd->opaque = (RXD_OPAQUE_RING_STD |
7141 (i << RXD_OPAQUE_INDEX_SHIFT));
7142 }
7143
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007144 /* Now allocate fresh SKBs for each rx ring. */
7145 for (i = 0; i < tp->rx_pending; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007146 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_STD, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007147 netdev_warn(tp->dev,
7148 "Using a smaller RX standard ring. Only "
7149 "%d out of %d buffers were allocated "
7150 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007151 if (i == 0)
7152 goto initfail;
7153 tp->rx_pending = i;
7154 break;
7155 }
7156 }
7157
Joe Perches63c3a662011-04-26 08:12:10 +00007158 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007159 goto done;
7160
Matt Carlson2c49a442010-09-30 10:34:35 +00007161 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007162
Joe Perches63c3a662011-04-26 08:12:10 +00007163 if (!tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson0d86df82010-02-17 15:17:00 +00007164 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007165
Matt Carlson2c49a442010-09-30 10:34:35 +00007166 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00007167 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007168
Matt Carlson0d86df82010-02-17 15:17:00 +00007169 rxd = &tpr->rx_jmb[i].std;
7170 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
7171 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
7172 RXD_FLAG_JUMBO;
7173 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
7174 (i << RXD_OPAQUE_INDEX_SHIFT));
7175 }
7176
7177 for (i = 0; i < tp->rx_jumbo_pending; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007178 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_JUMBO, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007179 netdev_warn(tp->dev,
7180 "Using a smaller RX jumbo ring. Only %d "
7181 "out of %d buffers were allocated "
7182 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00007183 if (i == 0)
7184 goto initfail;
7185 tp->rx_jumbo_pending = i;
7186 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007187 }
7188 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007189
7190done:
Michael Chan32d8c572006-07-25 16:38:29 -07007191 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007192
7193initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00007194 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007195 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007196}
7197
Matt Carlson21f581a2009-08-28 14:00:25 +00007198static void tg3_rx_prodring_fini(struct tg3 *tp,
7199 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007200{
Matt Carlson21f581a2009-08-28 14:00:25 +00007201 kfree(tpr->rx_std_buffers);
7202 tpr->rx_std_buffers = NULL;
7203 kfree(tpr->rx_jmb_buffers);
7204 tpr->rx_jmb_buffers = NULL;
7205 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007206 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
7207 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00007208 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007209 }
Matt Carlson21f581a2009-08-28 14:00:25 +00007210 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007211 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
7212 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00007213 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007214 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007215}
7216
Matt Carlson21f581a2009-08-28 14:00:25 +00007217static int tg3_rx_prodring_init(struct tg3 *tp,
7218 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007219{
Matt Carlson2c49a442010-09-30 10:34:35 +00007220 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
7221 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007222 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007223 return -ENOMEM;
7224
Matt Carlson4bae65c2010-11-24 08:31:52 +00007225 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
7226 TG3_RX_STD_RING_BYTES(tp),
7227 &tpr->rx_std_mapping,
7228 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007229 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007230 goto err_out;
7231
Joe Perches63c3a662011-04-26 08:12:10 +00007232 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007233 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00007234 GFP_KERNEL);
7235 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007236 goto err_out;
7237
Matt Carlson4bae65c2010-11-24 08:31:52 +00007238 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
7239 TG3_RX_JMB_RING_BYTES(tp),
7240 &tpr->rx_jmb_mapping,
7241 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007242 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007243 goto err_out;
7244 }
7245
7246 return 0;
7247
7248err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00007249 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007250 return -ENOMEM;
7251}
7252
7253/* Free up pending packets in all rx/tx rings.
7254 *
7255 * The chip has been shut down and the driver detached from
7256 * the networking, so no interrupts or new tx packets will
7257 * end up in the driver. tp->{tx,}lock is not held and we are not
7258 * in an interrupt context and thus may sleep.
7259 */
7260static void tg3_free_rings(struct tg3 *tp)
7261{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007262 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007263
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007264 for (j = 0; j < tp->irq_cnt; j++) {
7265 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007266
Matt Carlson8fea32b2010-09-15 08:59:58 +00007267 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00007268
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007269 if (!tnapi->tx_buffers)
7270 continue;
7271
Matt Carlson0d681b22011-07-27 14:20:49 +00007272 for (i = 0; i < TG3_TX_RING_SIZE; i++) {
7273 struct sk_buff *skb = tnapi->tx_buffers[i].skb;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007274
Matt Carlson0d681b22011-07-27 14:20:49 +00007275 if (!skb)
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007276 continue;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007277
Matt Carlsonba1142e2011-11-04 09:15:00 +00007278 tg3_tx_skb_unmap(tnapi, i,
7279 skb_shinfo(skb)->nr_frags - 1);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007280
7281 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007282 }
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007283 }
Tom Herbert298376d2011-11-28 16:33:30 +00007284 netdev_reset_queue(tp->dev);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007285}
7286
7287/* Initialize tx/rx rings for packet processing.
7288 *
7289 * The chip has been shut down and the driver detached from
7290 * the networking, so no interrupts or new tx packets will
7291 * end up in the driver. tp->{tx,}lock are held and thus
7292 * we may not sleep.
7293 */
7294static int tg3_init_rings(struct tg3 *tp)
7295{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007296 int i;
Matt Carlson72334482009-08-28 14:03:01 +00007297
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007298 /* Free up all the SKBs. */
7299 tg3_free_rings(tp);
7300
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007301 for (i = 0; i < tp->irq_cnt; i++) {
7302 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007303
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007304 tnapi->last_tag = 0;
7305 tnapi->last_irq_tag = 0;
7306 tnapi->hw_status->status = 0;
7307 tnapi->hw_status->status_tag = 0;
7308 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7309
7310 tnapi->tx_prod = 0;
7311 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007312 if (tnapi->tx_ring)
7313 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007314
7315 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007316 if (tnapi->rx_rcb)
7317 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007318
Matt Carlson8fea32b2010-09-15 08:59:58 +00007319 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00007320 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007321 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00007322 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007323 }
Matt Carlson72334482009-08-28 14:03:01 +00007324
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007325 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007326}
7327
7328/*
7329 * Must not be invoked with interrupt sources disabled and
7330 * the hardware shutdown down.
7331 */
7332static void tg3_free_consistent(struct tg3 *tp)
7333{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007334 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00007335
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007336 for (i = 0; i < tp->irq_cnt; i++) {
7337 struct tg3_napi *tnapi = &tp->napi[i];
7338
7339 if (tnapi->tx_ring) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007340 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007341 tnapi->tx_ring, tnapi->tx_desc_mapping);
7342 tnapi->tx_ring = NULL;
7343 }
7344
7345 kfree(tnapi->tx_buffers);
7346 tnapi->tx_buffers = NULL;
7347
7348 if (tnapi->rx_rcb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007349 dma_free_coherent(&tp->pdev->dev,
7350 TG3_RX_RCB_RING_BYTES(tp),
7351 tnapi->rx_rcb,
7352 tnapi->rx_rcb_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007353 tnapi->rx_rcb = NULL;
7354 }
7355
Matt Carlson8fea32b2010-09-15 08:59:58 +00007356 tg3_rx_prodring_fini(tp, &tnapi->prodring);
7357
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007358 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007359 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
7360 tnapi->hw_status,
7361 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007362 tnapi->hw_status = NULL;
7363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007364 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007365
Linus Torvalds1da177e2005-04-16 15:20:36 -07007366 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007367 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
7368 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007369 tp->hw_stats = NULL;
7370 }
7371}
7372
7373/*
7374 * Must not be invoked with interrupt sources disabled and
7375 * the hardware shutdown down. Can sleep.
7376 */
7377static int tg3_alloc_consistent(struct tg3 *tp)
7378{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007379 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00007380
Matt Carlson4bae65c2010-11-24 08:31:52 +00007381 tp->hw_stats = dma_alloc_coherent(&tp->pdev->dev,
7382 sizeof(struct tg3_hw_stats),
7383 &tp->stats_mapping,
7384 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007385 if (!tp->hw_stats)
7386 goto err_out;
7387
Linus Torvalds1da177e2005-04-16 15:20:36 -07007388 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
7389
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007390 for (i = 0; i < tp->irq_cnt; i++) {
7391 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007392 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007393
Matt Carlson4bae65c2010-11-24 08:31:52 +00007394 tnapi->hw_status = dma_alloc_coherent(&tp->pdev->dev,
7395 TG3_HW_STATUS_SIZE,
7396 &tnapi->status_mapping,
7397 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007398 if (!tnapi->hw_status)
7399 goto err_out;
7400
7401 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007402 sblk = tnapi->hw_status;
7403
Matt Carlson8fea32b2010-09-15 08:59:58 +00007404 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
7405 goto err_out;
7406
Matt Carlson19cfaec2009-12-03 08:36:20 +00007407 /* If multivector TSS is enabled, vector 0 does not handle
7408 * tx interrupts. Don't allocate any resources for it.
7409 */
Joe Perches63c3a662011-04-26 08:12:10 +00007410 if ((!i && !tg3_flag(tp, ENABLE_TSS)) ||
7411 (i && tg3_flag(tp, ENABLE_TSS))) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00007412 tnapi->tx_buffers = kzalloc(
7413 sizeof(struct tg3_tx_ring_info) *
7414 TG3_TX_RING_SIZE, GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007415 if (!tnapi->tx_buffers)
7416 goto err_out;
7417
Matt Carlson4bae65c2010-11-24 08:31:52 +00007418 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
7419 TG3_TX_RING_BYTES,
7420 &tnapi->tx_desc_mapping,
7421 GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007422 if (!tnapi->tx_ring)
7423 goto err_out;
7424 }
7425
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007426 /*
7427 * When RSS is enabled, the status block format changes
7428 * slightly. The "rx_jumbo_consumer", "reserved",
7429 * and "rx_mini_consumer" members get mapped to the
7430 * other three rx return ring producer indexes.
7431 */
7432 switch (i) {
7433 default:
7434 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
7435 break;
7436 case 2:
7437 tnapi->rx_rcb_prod_idx = &sblk->rx_jumbo_consumer;
7438 break;
7439 case 3:
7440 tnapi->rx_rcb_prod_idx = &sblk->reserved;
7441 break;
7442 case 4:
7443 tnapi->rx_rcb_prod_idx = &sblk->rx_mini_consumer;
7444 break;
7445 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007446
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007447 /*
7448 * If multivector RSS is enabled, vector 0 does not handle
7449 * rx or tx interrupts. Don't allocate any resources for it.
7450 */
Joe Perches63c3a662011-04-26 08:12:10 +00007451 if (!i && tg3_flag(tp, ENABLE_RSS))
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007452 continue;
7453
Matt Carlson4bae65c2010-11-24 08:31:52 +00007454 tnapi->rx_rcb = dma_alloc_coherent(&tp->pdev->dev,
7455 TG3_RX_RCB_RING_BYTES(tp),
7456 &tnapi->rx_rcb_mapping,
7457 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007458 if (!tnapi->rx_rcb)
7459 goto err_out;
7460
7461 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007462 }
7463
Linus Torvalds1da177e2005-04-16 15:20:36 -07007464 return 0;
7465
7466err_out:
7467 tg3_free_consistent(tp);
7468 return -ENOMEM;
7469}
7470
7471#define MAX_WAIT_CNT 1000
7472
7473/* To stop a block, clear the enable bit and poll till it
7474 * clears. tp->lock is held.
7475 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007476static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007477{
7478 unsigned int i;
7479 u32 val;
7480
Joe Perches63c3a662011-04-26 08:12:10 +00007481 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007482 switch (ofs) {
7483 case RCVLSC_MODE:
7484 case DMAC_MODE:
7485 case MBFREE_MODE:
7486 case BUFMGR_MODE:
7487 case MEMARB_MODE:
7488 /* We can't enable/disable these bits of the
7489 * 5705/5750, just say success.
7490 */
7491 return 0;
7492
7493 default:
7494 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007496 }
7497
7498 val = tr32(ofs);
7499 val &= ~enable_bit;
7500 tw32_f(ofs, val);
7501
7502 for (i = 0; i < MAX_WAIT_CNT; i++) {
7503 udelay(100);
7504 val = tr32(ofs);
7505 if ((val & enable_bit) == 0)
7506 break;
7507 }
7508
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007509 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00007510 dev_err(&tp->pdev->dev,
7511 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
7512 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007513 return -ENODEV;
7514 }
7515
7516 return 0;
7517}
7518
7519/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007520static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007521{
7522 int i, err;
7523
7524 tg3_disable_ints(tp);
7525
7526 tp->rx_mode &= ~RX_MODE_ENABLE;
7527 tw32_f(MAC_RX_MODE, tp->rx_mode);
7528 udelay(10);
7529
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007530 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
7531 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
7532 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
7533 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
7534 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
7535 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007536
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007537 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
7538 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
7539 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
7540 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
7541 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
7542 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
7543 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007544
7545 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
7546 tw32_f(MAC_MODE, tp->mac_mode);
7547 udelay(40);
7548
7549 tp->tx_mode &= ~TX_MODE_ENABLE;
7550 tw32_f(MAC_TX_MODE, tp->tx_mode);
7551
7552 for (i = 0; i < MAX_WAIT_CNT; i++) {
7553 udelay(100);
7554 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
7555 break;
7556 }
7557 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00007558 dev_err(&tp->pdev->dev,
7559 "%s timed out, TX_MODE_ENABLE will not clear "
7560 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07007561 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007562 }
7563
Michael Chane6de8ad2005-05-05 14:42:41 -07007564 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007565 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
7566 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007567
7568 tw32(FTQ_RESET, 0xffffffff);
7569 tw32(FTQ_RESET, 0x00000000);
7570
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007571 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
7572 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007573
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007574 for (i = 0; i < tp->irq_cnt; i++) {
7575 struct tg3_napi *tnapi = &tp->napi[i];
7576 if (tnapi->hw_status)
7577 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007579
Linus Torvalds1da177e2005-04-16 15:20:36 -07007580 return err;
7581}
7582
Michael Chanee6a99b2007-07-18 21:49:10 -07007583/* Save PCI command register before chip reset */
7584static void tg3_save_pci_state(struct tg3 *tp)
7585{
Matt Carlson8a6eac92007-10-21 16:17:55 -07007586 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007587}
7588
7589/* Restore PCI state after chip reset */
7590static void tg3_restore_pci_state(struct tg3 *tp)
7591{
7592 u32 val;
7593
7594 /* Re-enable indirect register accesses. */
7595 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
7596 tp->misc_host_ctrl);
7597
7598 /* Set MAX PCI retry to zero. */
7599 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
7600 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007601 tg3_flag(tp, PCIX_MODE))
Michael Chanee6a99b2007-07-18 21:49:10 -07007602 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07007603 /* Allow reads and writes to the APE register and memory space. */
Joe Perches63c3a662011-04-26 08:12:10 +00007604 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07007605 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00007606 PCISTATE_ALLOW_APE_SHMEM_WR |
7607 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07007608 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
7609
Matt Carlson8a6eac92007-10-21 16:17:55 -07007610 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007611
Matt Carlson2c55a3d2011-11-28 09:41:04 +00007612 if (!tg3_flag(tp, PCI_EXPRESS)) {
7613 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
7614 tp->pci_cacheline_sz);
7615 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
7616 tp->pci_lat_timer);
Michael Chan114342f2007-10-15 02:12:26 -07007617 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08007618
Michael Chanee6a99b2007-07-18 21:49:10 -07007619 /* Make sure PCI-X relaxed ordering bit is clear. */
Joe Perches63c3a662011-04-26 08:12:10 +00007620 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07007621 u16 pcix_cmd;
7622
7623 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7624 &pcix_cmd);
7625 pcix_cmd &= ~PCI_X_CMD_ERO;
7626 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7627 pcix_cmd);
7628 }
Michael Chanee6a99b2007-07-18 21:49:10 -07007629
Joe Perches63c3a662011-04-26 08:12:10 +00007630 if (tg3_flag(tp, 5780_CLASS)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007631
7632 /* Chip reset on 5780 will reset MSI enable bit,
7633 * so need to restore it.
7634 */
Joe Perches63c3a662011-04-26 08:12:10 +00007635 if (tg3_flag(tp, USING_MSI)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007636 u16 ctrl;
7637
7638 pci_read_config_word(tp->pdev,
7639 tp->msi_cap + PCI_MSI_FLAGS,
7640 &ctrl);
7641 pci_write_config_word(tp->pdev,
7642 tp->msi_cap + PCI_MSI_FLAGS,
7643 ctrl | PCI_MSI_FLAGS_ENABLE);
7644 val = tr32(MSGINT_MODE);
7645 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
7646 }
7647 }
7648}
7649
Linus Torvalds1da177e2005-04-16 15:20:36 -07007650/* tp->lock is held. */
7651static int tg3_chip_reset(struct tg3 *tp)
7652{
7653 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07007654 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00007655 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007656
David S. Millerf49639e2006-06-09 11:58:36 -07007657 tg3_nvram_lock(tp);
7658
Matt Carlson77b483f2008-08-15 14:07:24 -07007659 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
7660
David S. Millerf49639e2006-06-09 11:58:36 -07007661 /* No matching tg3_nvram_unlock() after this because
7662 * chip reset below will undo the nvram lock.
7663 */
7664 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007665
Michael Chanee6a99b2007-07-18 21:49:10 -07007666 /* GRC_MISC_CFG core clock reset will clear the memory
7667 * enable bit in PCI register 4 and the MSI enable bit
7668 * on some chips, so we save relevant registers here.
7669 */
7670 tg3_save_pci_state(tp);
7671
Michael Chand9ab5ad12006-03-20 22:27:35 -08007672 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Joe Perches63c3a662011-04-26 08:12:10 +00007673 tg3_flag(tp, 5755_PLUS))
Michael Chand9ab5ad12006-03-20 22:27:35 -08007674 tw32(GRC_FASTBOOT_PC, 0);
7675
Linus Torvalds1da177e2005-04-16 15:20:36 -07007676 /*
7677 * We must avoid the readl() that normally takes place.
7678 * It locks machines, causes machine checks, and other
7679 * fun things. So, temporarily disable the 5701
7680 * hardware workaround, while we do the reset.
7681 */
Michael Chan1ee582d2005-08-09 20:16:46 -07007682 write_op = tp->write32;
7683 if (write_op == tg3_write_flush_reg32)
7684 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007685
Michael Chand18edcb2007-03-24 20:57:11 -07007686 /* Prevent the irq handler from reading or writing PCI registers
7687 * during chip reset when the memory enable bit in the PCI command
7688 * register may be cleared. The chip does not generate interrupt
7689 * at this time, but the irq handler may still be called due to irq
7690 * sharing or irqpoll.
7691 */
Joe Perches63c3a662011-04-26 08:12:10 +00007692 tg3_flag_set(tp, CHIP_RESETTING);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007693 for (i = 0; i < tp->irq_cnt; i++) {
7694 struct tg3_napi *tnapi = &tp->napi[i];
7695 if (tnapi->hw_status) {
7696 tnapi->hw_status->status = 0;
7697 tnapi->hw_status->status_tag = 0;
7698 }
7699 tnapi->last_tag = 0;
7700 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07007701 }
Michael Chand18edcb2007-03-24 20:57:11 -07007702 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00007703
7704 for (i = 0; i < tp->irq_cnt; i++)
7705 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07007706
Matt Carlson255ca312009-08-25 10:07:27 +00007707 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7708 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7709 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
7710 }
7711
Linus Torvalds1da177e2005-04-16 15:20:36 -07007712 /* do the reset */
7713 val = GRC_MISC_CFG_CORECLK_RESET;
7714
Joe Perches63c3a662011-04-26 08:12:10 +00007715 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson88075d92010-08-02 11:25:58 +00007716 /* Force PCIe 1.0a mode */
7717 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007718 !tg3_flag(tp, 57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00007719 tr32(TG3_PCIE_PHY_TSTCTL) ==
7720 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
7721 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
7722
Linus Torvalds1da177e2005-04-16 15:20:36 -07007723 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
7724 tw32(GRC_MISC_CFG, (1 << 29));
7725 val |= (1 << 29);
7726 }
7727 }
7728
Michael Chanb5d37722006-09-27 16:06:21 -07007729 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7730 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
7731 tw32(GRC_VCPU_EXT_CTRL,
7732 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
7733 }
7734
Matt Carlsonf37500d2010-08-02 11:25:59 +00007735 /* Manage gphy power for all CPMU absent PCIe devices. */
Joe Perches63c3a662011-04-26 08:12:10 +00007736 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007737 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00007738
Linus Torvalds1da177e2005-04-16 15:20:36 -07007739 tw32(GRC_MISC_CFG, val);
7740
Michael Chan1ee582d2005-08-09 20:16:46 -07007741 /* restore 5701 hardware bug workaround write method */
7742 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007743
7744 /* Unfortunately, we have to delay before the PCI read back.
7745 * Some 575X chips even will not respond to a PCI cfg access
7746 * when the reset command is given to the chip.
7747 *
7748 * How do these hardware designers expect things to work
7749 * properly if the PCI write is posted for a long period
7750 * of time? It is always necessary to have some method by
7751 * which a register read back can occur to push the write
7752 * out which does the reset.
7753 *
7754 * For most tg3 variants the trick below was working.
7755 * Ho hum...
7756 */
7757 udelay(120);
7758
7759 /* Flush PCI posted writes. The normal MMIO registers
7760 * are inaccessible at this time so this is the only
7761 * way to make this reliably (actually, this is no longer
7762 * the case, see above). I tried to use indirect
7763 * register read/write but this upset some 5701 variants.
7764 */
7765 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
7766
7767 udelay(120);
7768
Jon Mason708ebb3a2011-06-27 12:56:50 +00007769 if (tg3_flag(tp, PCI_EXPRESS) && pci_pcie_cap(tp->pdev)) {
Matt Carlsone7126992009-08-25 10:08:16 +00007770 u16 val16;
7771
Linus Torvalds1da177e2005-04-16 15:20:36 -07007772 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
7773 int i;
7774 u32 cfg_val;
7775
7776 /* Wait for link training to complete. */
7777 for (i = 0; i < 5000; i++)
7778 udelay(100);
7779
7780 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
7781 pci_write_config_dword(tp->pdev, 0xc4,
7782 cfg_val | (1 << 15));
7783 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007784
Matt Carlsone7126992009-08-25 10:08:16 +00007785 /* Clear the "no snoop" and "relaxed ordering" bits. */
7786 pci_read_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +00007787 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007788 &val16);
7789 val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
7790 PCI_EXP_DEVCTL_NOSNOOP_EN);
7791 /*
7792 * Older PCIe devices only support the 128 byte
7793 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007794 */
Joe Perches63c3a662011-04-26 08:12:10 +00007795 if (!tg3_flag(tp, CPMU_PRESENT))
Matt Carlsone7126992009-08-25 10:08:16 +00007796 val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007797 pci_write_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +00007798 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007799 val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007800
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007801 /* Clear error status */
7802 pci_write_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +00007803 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVSTA,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007804 PCI_EXP_DEVSTA_CED |
7805 PCI_EXP_DEVSTA_NFED |
7806 PCI_EXP_DEVSTA_FED |
7807 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007808 }
7809
Michael Chanee6a99b2007-07-18 21:49:10 -07007810 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007811
Joe Perches63c3a662011-04-26 08:12:10 +00007812 tg3_flag_clear(tp, CHIP_RESETTING);
7813 tg3_flag_clear(tp, ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07007814
Michael Chanee6a99b2007-07-18 21:49:10 -07007815 val = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00007816 if (tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07007817 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07007818 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007819
7820 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
7821 tg3_stop_fw(tp);
7822 tw32(0x5000, 0x400);
7823 }
7824
7825 tw32(GRC_MODE, tp->grc_mode);
7826
7827 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007828 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007829
7830 tw32(0xc4, val | (1 << 15));
7831 }
7832
7833 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
7834 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
7835 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
7836 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
7837 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
7838 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7839 }
7840
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007841 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00007842 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007843 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007844 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00007845 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007846 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007847 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007848 val = 0;
7849
7850 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007851 udelay(40);
7852
Matt Carlson77b483f2008-08-15 14:07:24 -07007853 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
7854
Michael Chan7a6f4362006-09-27 16:03:31 -07007855 err = tg3_poll_fw(tp);
7856 if (err)
7857 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007858
Matt Carlson0a9140c2009-08-28 12:27:50 +00007859 tg3_mdio_start(tp);
7860
Joe Perches63c3a662011-04-26 08:12:10 +00007861 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007862 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
7863 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007864 !tg3_flag(tp, 57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007865 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007866
7867 tw32(0x7c00, val | (1 << 25));
7868 }
7869
Matt Carlsond78b59f2011-04-05 14:22:46 +00007870 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
7871 val = tr32(TG3_CPMU_CLCK_ORIDE);
7872 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
7873 }
7874
Linus Torvalds1da177e2005-04-16 15:20:36 -07007875 /* Reprobe ASF enable state. */
Joe Perches63c3a662011-04-26 08:12:10 +00007876 tg3_flag_clear(tp, ENABLE_ASF);
7877 tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007878 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
7879 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
7880 u32 nic_cfg;
7881
7882 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
7883 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +00007884 tg3_flag_set(tp, ENABLE_ASF);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007885 tp->last_event_jiffies = jiffies;
Joe Perches63c3a662011-04-26 08:12:10 +00007886 if (tg3_flag(tp, 5750_PLUS))
7887 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007888 }
7889 }
7890
7891 return 0;
7892}
7893
Matt Carlson92feeab2011-12-08 14:40:14 +00007894static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *,
7895 struct rtnl_link_stats64 *);
7896static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *,
7897 struct tg3_ethtool_stats *);
7898
Linus Torvalds1da177e2005-04-16 15:20:36 -07007899/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07007900static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007901{
7902 int err;
7903
7904 tg3_stop_fw(tp);
7905
Michael Chan944d9802005-05-29 14:57:48 -07007906 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007907
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007908 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007909 err = tg3_chip_reset(tp);
7910
Matt Carlsondaba2a62009-04-20 06:58:52 +00007911 __tg3_set_mac_addr(tp, 0);
7912
Michael Chan944d9802005-05-29 14:57:48 -07007913 tg3_write_sig_legacy(tp, kind);
7914 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007915
Matt Carlson92feeab2011-12-08 14:40:14 +00007916 if (tp->hw_stats) {
7917 /* Save the stats across chip resets... */
7918 tg3_get_stats64(tp->dev, &tp->net_stats_prev),
7919 tg3_get_estats(tp, &tp->estats_prev);
7920
7921 /* And make sure the next sample is new data */
7922 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
7923 }
7924
Linus Torvalds1da177e2005-04-16 15:20:36 -07007925 if (err)
7926 return err;
7927
7928 return 0;
7929}
7930
Linus Torvalds1da177e2005-04-16 15:20:36 -07007931static int tg3_set_mac_addr(struct net_device *dev, void *p)
7932{
7933 struct tg3 *tp = netdev_priv(dev);
7934 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07007935 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007936
Michael Chanf9804dd2005-09-27 12:13:10 -07007937 if (!is_valid_ether_addr(addr->sa_data))
7938 return -EINVAL;
7939
Linus Torvalds1da177e2005-04-16 15:20:36 -07007940 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7941
Michael Chane75f7c92006-03-20 21:33:26 -08007942 if (!netif_running(dev))
7943 return 0;
7944
Joe Perches63c3a662011-04-26 08:12:10 +00007945 if (tg3_flag(tp, ENABLE_ASF)) {
Michael Chan986e0ae2007-05-05 12:10:20 -07007946 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07007947
Michael Chan986e0ae2007-05-05 12:10:20 -07007948 addr0_high = tr32(MAC_ADDR_0_HIGH);
7949 addr0_low = tr32(MAC_ADDR_0_LOW);
7950 addr1_high = tr32(MAC_ADDR_1_HIGH);
7951 addr1_low = tr32(MAC_ADDR_1_LOW);
7952
7953 /* Skip MAC addr 1 if ASF is using it. */
7954 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
7955 !(addr1_high == 0 && addr1_low == 0))
7956 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07007957 }
Michael Chan986e0ae2007-05-05 12:10:20 -07007958 spin_lock_bh(&tp->lock);
7959 __tg3_set_mac_addr(tp, skip_mac_1);
7960 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007961
Michael Chanb9ec6c12006-07-25 16:37:27 -07007962 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007963}
7964
7965/* tp->lock is held. */
7966static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
7967 dma_addr_t mapping, u32 maxlen_flags,
7968 u32 nic_addr)
7969{
7970 tg3_write_mem(tp,
7971 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
7972 ((u64) mapping >> 32));
7973 tg3_write_mem(tp,
7974 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
7975 ((u64) mapping & 0xffffffff));
7976 tg3_write_mem(tp,
7977 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
7978 maxlen_flags);
7979
Joe Perches63c3a662011-04-26 08:12:10 +00007980 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007981 tg3_write_mem(tp,
7982 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
7983 nic_addr);
7984}
7985
7986static void __tg3_set_rx_mode(struct net_device *);
Michael Chand244c892005-07-05 14:42:33 -07007987static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07007988{
Matt Carlsonb6080e12009-09-01 13:12:00 +00007989 int i;
7990
Joe Perches63c3a662011-04-26 08:12:10 +00007991 if (!tg3_flag(tp, ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007992 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
7993 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
7994 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007995 } else {
7996 tw32(HOSTCC_TXCOL_TICKS, 0);
7997 tw32(HOSTCC_TXMAX_FRAMES, 0);
7998 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007999 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008000
Joe Perches63c3a662011-04-26 08:12:10 +00008001 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008002 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
8003 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
8004 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
8005 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00008006 tw32(HOSTCC_RXCOL_TICKS, 0);
8007 tw32(HOSTCC_RXMAX_FRAMES, 0);
8008 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07008009 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008010
Joe Perches63c3a662011-04-26 08:12:10 +00008011 if (!tg3_flag(tp, 5705_PLUS)) {
David S. Miller15f98502005-05-18 22:49:26 -07008012 u32 val = ec->stats_block_coalesce_usecs;
8013
Matt Carlsonb6080e12009-09-01 13:12:00 +00008014 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
8015 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
8016
David S. Miller15f98502005-05-18 22:49:26 -07008017 if (!netif_carrier_ok(tp->dev))
8018 val = 0;
8019
8020 tw32(HOSTCC_STAT_COAL_TICKS, val);
8021 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008022
8023 for (i = 0; i < tp->irq_cnt - 1; i++) {
8024 u32 reg;
8025
8026 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
8027 tw32(reg, ec->rx_coalesce_usecs);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008028 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
8029 tw32(reg, ec->rx_max_coalesced_frames);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008030 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
8031 tw32(reg, ec->rx_max_coalesced_frames_irq);
Matt Carlson19cfaec2009-12-03 08:36:20 +00008032
Joe Perches63c3a662011-04-26 08:12:10 +00008033 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008034 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
8035 tw32(reg, ec->tx_coalesce_usecs);
8036 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
8037 tw32(reg, ec->tx_max_coalesced_frames);
8038 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
8039 tw32(reg, ec->tx_max_coalesced_frames_irq);
8040 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008041 }
8042
8043 for (; i < tp->irq_max - 1; i++) {
8044 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008045 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008046 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00008047
Joe Perches63c3a662011-04-26 08:12:10 +00008048 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008049 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
8050 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
8051 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
8052 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008053 }
David S. Miller15f98502005-05-18 22:49:26 -07008054}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008055
8056/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00008057static void tg3_rings_reset(struct tg3 *tp)
8058{
8059 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008060 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008061 struct tg3_napi *tnapi = &tp->napi[0];
8062
8063 /* Disable all transmit rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008064 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008065 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Joe Perches63c3a662011-04-26 08:12:10 +00008066 else if (tg3_flag(tp, 5717_PLUS))
Matt Carlson3d377282010-10-14 10:37:39 +00008067 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
Matt Carlson55086ad2011-12-14 11:09:59 +00008068 else if (tg3_flag(tp, 57765_CLASS))
Matt Carlsonb703df62009-12-03 08:36:21 +00008069 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008070 else
8071 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8072
8073 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8074 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
8075 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
8076 BDINFO_FLAGS_DISABLED);
8077
8078
8079 /* Disable all receive return rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008080 if (tg3_flag(tp, 5717_PLUS))
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008081 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
Joe Perches63c3a662011-04-26 08:12:10 +00008082 else if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008083 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00008084 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00008085 tg3_flag(tp, 57765_CLASS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008086 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
8087 else
8088 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8089
8090 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8091 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
8092 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
8093 BDINFO_FLAGS_DISABLED);
8094
8095 /* Disable interrupts */
8096 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008097 tp->napi[0].chk_msi_cnt = 0;
8098 tp->napi[0].last_rx_cons = 0;
8099 tp->napi[0].last_tx_cons = 0;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008100
8101 /* Zero mailbox registers. */
Joe Perches63c3a662011-04-26 08:12:10 +00008102 if (tg3_flag(tp, SUPPORT_MSIX)) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00008103 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008104 tp->napi[i].tx_prod = 0;
8105 tp->napi[i].tx_cons = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00008106 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008107 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008108 tw32_rx_mbox(tp->napi[i].consmbox, 0);
8109 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
Matt Carlson7f230732011-08-31 11:44:48 +00008110 tp->napi[i].chk_msi_cnt = 0;
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008111 tp->napi[i].last_rx_cons = 0;
8112 tp->napi[i].last_tx_cons = 0;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008113 }
Joe Perches63c3a662011-04-26 08:12:10 +00008114 if (!tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008115 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008116 } else {
8117 tp->napi[0].tx_prod = 0;
8118 tp->napi[0].tx_cons = 0;
8119 tw32_mailbox(tp->napi[0].prodmbox, 0);
8120 tw32_rx_mbox(tp->napi[0].consmbox, 0);
8121 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008122
8123 /* Make sure the NIC-based send BD rings are disabled. */
Joe Perches63c3a662011-04-26 08:12:10 +00008124 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson2d31eca2009-09-01 12:53:31 +00008125 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
8126 for (i = 0; i < 16; i++)
8127 tw32_tx_mbox(mbox + i * 8, 0);
8128 }
8129
8130 txrcb = NIC_SRAM_SEND_RCB;
8131 rxrcb = NIC_SRAM_RCV_RET_RCB;
8132
8133 /* Clear status block in ram. */
8134 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8135
8136 /* Set status block DMA address */
8137 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8138 ((u64) tnapi->status_mapping >> 32));
8139 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8140 ((u64) tnapi->status_mapping & 0xffffffff));
8141
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008142 if (tnapi->tx_ring) {
8143 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8144 (TG3_TX_RING_SIZE <<
8145 BDINFO_FLAGS_MAXLEN_SHIFT),
8146 NIC_SRAM_TX_BUFFER_DESC);
8147 txrcb += TG3_BDINFO_SIZE;
8148 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008149
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008150 if (tnapi->rx_rcb) {
8151 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008152 (tp->rx_ret_ring_mask + 1) <<
8153 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008154 rxrcb += TG3_BDINFO_SIZE;
8155 }
8156
8157 stblk = HOSTCC_STATBLCK_RING1;
8158
8159 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
8160 u64 mapping = (u64)tnapi->status_mapping;
8161 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
8162 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
8163
8164 /* Clear status block in ram. */
8165 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8166
Matt Carlson19cfaec2009-12-03 08:36:20 +00008167 if (tnapi->tx_ring) {
8168 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8169 (TG3_TX_RING_SIZE <<
8170 BDINFO_FLAGS_MAXLEN_SHIFT),
8171 NIC_SRAM_TX_BUFFER_DESC);
8172 txrcb += TG3_BDINFO_SIZE;
8173 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008174
8175 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008176 ((tp->rx_ret_ring_mask + 1) <<
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008177 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
8178
8179 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008180 rxrcb += TG3_BDINFO_SIZE;
8181 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008182}
8183
Matt Carlsoneb07a942011-04-20 07:57:36 +00008184static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
8185{
8186 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
8187
Joe Perches63c3a662011-04-26 08:12:10 +00008188 if (!tg3_flag(tp, 5750_PLUS) ||
8189 tg3_flag(tp, 5780_CLASS) ||
Matt Carlsoneb07a942011-04-20 07:57:36 +00008190 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
Matt Carlson513aa6e2011-11-21 15:01:18 +00008191 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
8192 tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008193 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
8194 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
8195 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787)
8196 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
8197 else
8198 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
8199
8200 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
8201 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
8202
8203 val = min(nic_rep_thresh, host_rep_thresh);
8204 tw32(RCVBDI_STD_THRESH, val);
8205
Joe Perches63c3a662011-04-26 08:12:10 +00008206 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008207 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
8208
Joe Perches63c3a662011-04-26 08:12:10 +00008209 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008210 return;
8211
Matt Carlson513aa6e2011-11-21 15:01:18 +00008212 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
Matt Carlsoneb07a942011-04-20 07:57:36 +00008213
8214 host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
8215
8216 val = min(bdcache_maxcnt / 2, host_rep_thresh);
8217 tw32(RCVBDI_JUMBO_THRESH, val);
8218
Joe Perches63c3a662011-04-26 08:12:10 +00008219 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008220 tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
8221}
8222
Matt Carlson90415472011-12-16 13:33:23 +00008223static void tg3_rss_init_dflt_indir_tbl(struct tg3 *tp)
8224{
8225 int i;
8226
8227 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
8228 tp->rss_ind_tbl[i] =
8229 ethtool_rxfh_indir_default(i, tp->irq_cnt - 1);
8230}
8231
8232static void tg3_rss_check_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008233{
8234 int i;
8235
8236 if (!tg3_flag(tp, SUPPORT_MSIX))
8237 return;
8238
Matt Carlson90415472011-12-16 13:33:23 +00008239 if (tp->irq_cnt <= 2) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008240 memset(&tp->rss_ind_tbl[0], 0, sizeof(tp->rss_ind_tbl));
Matt Carlson90415472011-12-16 13:33:23 +00008241 return;
8242 }
8243
8244 /* Validate table against current IRQ count */
8245 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
8246 if (tp->rss_ind_tbl[i] >= tp->irq_cnt - 1)
8247 break;
8248 }
8249
8250 if (i != TG3_RSS_INDIR_TBL_SIZE)
8251 tg3_rss_init_dflt_indir_tbl(tp);
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008252}
8253
Matt Carlson90415472011-12-16 13:33:23 +00008254static void tg3_rss_write_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008255{
8256 int i = 0;
8257 u32 reg = MAC_RSS_INDIR_TBL_0;
8258
8259 while (i < TG3_RSS_INDIR_TBL_SIZE) {
8260 u32 val = tp->rss_ind_tbl[i];
8261 i++;
8262 for (; i % 8; i++) {
8263 val <<= 4;
8264 val |= tp->rss_ind_tbl[i];
8265 }
8266 tw32(reg, val);
8267 reg += 4;
8268 }
8269}
8270
Matt Carlson2d31eca2009-09-01 12:53:31 +00008271/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008272static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008273{
8274 u32 val, rdmac_mode;
8275 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00008276 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008277
8278 tg3_disable_ints(tp);
8279
8280 tg3_stop_fw(tp);
8281
8282 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
8283
Joe Perches63c3a662011-04-26 08:12:10 +00008284 if (tg3_flag(tp, INIT_COMPLETE))
Michael Chane6de8ad2005-05-05 14:42:41 -07008285 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008286
Matt Carlson699c0192010-12-06 08:28:51 +00008287 /* Enable MAC control of LPI */
8288 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
8289 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL,
8290 TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
8291 TG3_CPMU_EEE_LNKIDL_UART_IDL);
8292
8293 tw32_f(TG3_CPMU_EEE_CTRL,
8294 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
8295
Matt Carlsona386b902010-12-06 08:28:53 +00008296 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
8297 TG3_CPMU_EEEMD_LPI_IN_TX |
8298 TG3_CPMU_EEEMD_LPI_IN_RX |
8299 TG3_CPMU_EEEMD_EEE_ENABLE;
8300
8301 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8302 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
8303
Joe Perches63c3a662011-04-26 08:12:10 +00008304 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsona386b902010-12-06 08:28:53 +00008305 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
8306
8307 tw32_f(TG3_CPMU_EEE_MODE, val);
8308
8309 tw32_f(TG3_CPMU_EEE_DBTMR1,
8310 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
8311 TG3_CPMU_DBTMR1_LNKIDLE_2047US);
8312
8313 tw32_f(TG3_CPMU_EEE_DBTMR2,
Matt Carlsond7f2ab22011-01-25 15:58:56 +00008314 TG3_CPMU_DBTMR2_APE_TX_2047US |
Matt Carlsona386b902010-12-06 08:28:53 +00008315 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
Matt Carlson699c0192010-12-06 08:28:51 +00008316 }
8317
Matt Carlson603f1172010-02-12 14:47:10 +00008318 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08008319 tg3_phy_reset(tp);
8320
Linus Torvalds1da177e2005-04-16 15:20:36 -07008321 err = tg3_chip_reset(tp);
8322 if (err)
8323 return err;
8324
8325 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
8326
Matt Carlsonbcb37f62008-11-03 16:52:09 -08008327 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008328 val = tr32(TG3_CPMU_CTRL);
8329 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
8330 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08008331
8332 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8333 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8334 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8335 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
8336
8337 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
8338 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
8339 val |= CPMU_LNK_AWARE_MACCLK_6_25;
8340 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
8341
8342 val = tr32(TG3_CPMU_HST_ACC);
8343 val &= ~CPMU_HST_ACC_MACCLK_MASK;
8344 val |= CPMU_HST_ACC_MACCLK_6_25;
8345 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07008346 }
8347
Matt Carlson33466d92009-04-20 06:57:41 +00008348 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
8349 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
8350 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
8351 PCIE_PWR_MGMT_L1_THRESH_4MS;
8352 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00008353
8354 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
8355 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
8356
8357 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00008358
Matt Carlsonf40386c2009-11-02 14:24:02 +00008359 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
8360 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00008361 }
8362
Joe Perches63c3a662011-04-26 08:12:10 +00008363 if (tg3_flag(tp, L1PLLPD_EN)) {
Matt Carlson614b0592010-01-20 16:58:02 +00008364 u32 grc_mode = tr32(GRC_MODE);
8365
8366 /* Access the lower 1K of PL PCIE block registers. */
8367 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8368 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
8369
8370 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
8371 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
8372 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
8373
8374 tw32(GRC_MODE, grc_mode);
8375 }
8376
Matt Carlson55086ad2011-12-14 11:09:59 +00008377 if (tg3_flag(tp, 57765_CLASS)) {
Matt Carlson5093eed2010-11-24 08:31:45 +00008378 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
8379 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00008380
Matt Carlson5093eed2010-11-24 08:31:45 +00008381 /* Access the lower 1K of PL PCIE block registers. */
8382 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8383 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00008384
Matt Carlson5093eed2010-11-24 08:31:45 +00008385 val = tr32(TG3_PCIE_TLDLPL_PORT +
8386 TG3_PCIE_PL_LO_PHYCTL5);
8387 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
8388 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00008389
Matt Carlson5093eed2010-11-24 08:31:45 +00008390 tw32(GRC_MODE, grc_mode);
8391 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00008392
Matt Carlson1ff30a52011-05-19 12:12:46 +00008393 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_57765_AX) {
8394 u32 grc_mode = tr32(GRC_MODE);
8395
8396 /* Access the lower 1K of DL PCIE block registers. */
8397 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8398 tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
8399
8400 val = tr32(TG3_PCIE_TLDLPL_PORT +
8401 TG3_PCIE_DL_LO_FTSMAX);
8402 val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
8403 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
8404 val | TG3_PCIE_DL_LO_FTSMAX_VAL);
8405
8406 tw32(GRC_MODE, grc_mode);
8407 }
8408
Matt Carlsona977dbe2010-04-12 06:58:26 +00008409 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8410 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8411 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8412 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00008413 }
8414
Linus Torvalds1da177e2005-04-16 15:20:36 -07008415 /* This works around an issue with Athlon chipsets on
8416 * B3 tigon3 silicon. This bit has no effect on any
8417 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07008418 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008419 */
Joe Perches63c3a662011-04-26 08:12:10 +00008420 if (!tg3_flag(tp, CPMU_PRESENT)) {
8421 if (!tg3_flag(tp, PCI_EXPRESS))
Matt Carlson795d01c2007-10-07 23:28:17 -07008422 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
8423 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
8424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008425
8426 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008427 tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008428 val = tr32(TG3PCI_PCISTATE);
8429 val |= PCISTATE_RETRY_SAME_DMA;
8430 tw32(TG3PCI_PCISTATE, val);
8431 }
8432
Joe Perches63c3a662011-04-26 08:12:10 +00008433 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -07008434 /* Allow reads and writes to the
8435 * APE register and memory space.
8436 */
8437 val = tr32(TG3PCI_PCISTATE);
8438 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00008439 PCISTATE_ALLOW_APE_SHMEM_WR |
8440 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008441 tw32(TG3PCI_PCISTATE, val);
8442 }
8443
Linus Torvalds1da177e2005-04-16 15:20:36 -07008444 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
8445 /* Enable some hw fixes. */
8446 val = tr32(TG3PCI_MSI_DATA);
8447 val |= (1 << 26) | (1 << 28) | (1 << 29);
8448 tw32(TG3PCI_MSI_DATA, val);
8449 }
8450
8451 /* Descriptor ring init may make accesses to the
8452 * NIC SRAM area to setup the TX descriptors, so we
8453 * can only do this after the hardware has been
8454 * successfully reset.
8455 */
Michael Chan32d8c572006-07-25 16:38:29 -07008456 err = tg3_init_rings(tp);
8457 if (err)
8458 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008459
Joe Perches63c3a662011-04-26 08:12:10 +00008460 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008461 val = tr32(TG3PCI_DMA_RW_CTRL) &
8462 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Matt Carlson1a319022010-04-12 06:58:25 +00008463 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
8464 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlson55086ad2011-12-14 11:09:59 +00008465 if (!tg3_flag(tp, 57765_CLASS) &&
Matt Carlson0aebff42011-04-25 12:42:45 +00008466 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8467 val |= DMA_RWCTRL_TAGGED_STAT_WA;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008468 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
8469 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
8470 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008471 /* This value is determined during the probe time DMA
8472 * engine test, tg3_test_dma.
8473 */
8474 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
8475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008476
8477 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
8478 GRC_MODE_4X_NIC_SEND_RINGS |
8479 GRC_MODE_NO_TX_PHDR_CSUM |
8480 GRC_MODE_NO_RX_PHDR_CSUM);
8481 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07008482
8483 /* Pseudo-header checksum is done by hardware logic and not
8484 * the offload processers, so make the chip do the pseudo-
8485 * header checksums on receive. For transmit it is more
8486 * convenient to do the pseudo-header checksum in software
8487 * as Linux does that on transmit for us in all cases.
8488 */
8489 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008490
8491 tw32(GRC_MODE,
8492 tp->grc_mode |
8493 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
8494
8495 /* Setup the timer prescalar register. Clock is always 66Mhz. */
8496 val = tr32(GRC_MISC_CFG);
8497 val &= ~0xff;
8498 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
8499 tw32(GRC_MISC_CFG, val);
8500
8501 /* Initialize MBUF/DESC pool. */
Joe Perches63c3a662011-04-26 08:12:10 +00008502 if (tg3_flag(tp, 5750_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008503 /* Do nothing. */
8504 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
8505 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
8506 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
8507 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
8508 else
8509 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
8510 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
8511 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Joe Perches63c3a662011-04-26 08:12:10 +00008512 } else if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008513 int fw_len;
8514
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08008515 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008516 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
8517 tw32(BUFMGR_MB_POOL_ADDR,
8518 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
8519 tw32(BUFMGR_MB_POOL_SIZE,
8520 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
8521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008522
Michael Chan0f893dc2005-07-25 12:30:38 -07008523 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008524 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8525 tp->bufmgr_config.mbuf_read_dma_low_water);
8526 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8527 tp->bufmgr_config.mbuf_mac_rx_low_water);
8528 tw32(BUFMGR_MB_HIGH_WATER,
8529 tp->bufmgr_config.mbuf_high_water);
8530 } else {
8531 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8532 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
8533 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8534 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
8535 tw32(BUFMGR_MB_HIGH_WATER,
8536 tp->bufmgr_config.mbuf_high_water_jumbo);
8537 }
8538 tw32(BUFMGR_DMA_LOW_WATER,
8539 tp->bufmgr_config.dma_low_water);
8540 tw32(BUFMGR_DMA_HIGH_WATER,
8541 tp->bufmgr_config.dma_high_water);
8542
Matt Carlsond309a462010-09-30 10:34:31 +00008543 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
8544 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
8545 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
Matt Carlson4d958472011-04-20 07:57:35 +00008546 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
8547 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
8548 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0)
8549 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
Matt Carlsond309a462010-09-30 10:34:31 +00008550 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008551 for (i = 0; i < 2000; i++) {
8552 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
8553 break;
8554 udelay(10);
8555 }
8556 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00008557 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008558 return -ENODEV;
8559 }
8560
Matt Carlsoneb07a942011-04-20 07:57:36 +00008561 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
8562 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
Michael Chanb5d37722006-09-27 16:06:21 -07008563
Matt Carlsoneb07a942011-04-20 07:57:36 +00008564 tg3_setup_rxbd_thresholds(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008565
8566 /* Initialize TG3_BDINFO's at:
8567 * RCVDBDI_STD_BD: standard eth size rx ring
8568 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
8569 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
8570 *
8571 * like so:
8572 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
8573 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
8574 * ring attribute flags
8575 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
8576 *
8577 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
8578 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
8579 *
8580 * The size of each ring is fixed in the firmware, but the location is
8581 * configurable.
8582 */
8583 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008584 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008585 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008586 ((u64) tpr->rx_std_mapping & 0xffffffff));
Joe Perches63c3a662011-04-26 08:12:10 +00008587 if (!tg3_flag(tp, 5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +00008588 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
8589 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008590
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008591 /* Disable the mini ring */
Joe Perches63c3a662011-04-26 08:12:10 +00008592 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008593 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
8594 BDINFO_FLAGS_DISABLED);
8595
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008596 /* Program the jumbo buffer descriptor ring control
8597 * blocks on those devices that have them.
8598 */
Matt Carlsona0512942011-07-27 14:20:54 +00008599 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008600 (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008601
Joe Perches63c3a662011-04-26 08:12:10 +00008602 if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008603 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008604 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008605 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008606 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +00008607 val = TG3_RX_JMB_RING_SIZE(tp) <<
8608 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008609 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +00008610 val | BDINFO_FLAGS_USE_EXT_RECV);
Joe Perches63c3a662011-04-26 08:12:10 +00008611 if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
Matt Carlson55086ad2011-12-14 11:09:59 +00008612 tg3_flag(tp, 57765_CLASS))
Matt Carlson87668d32009-11-13 13:03:34 +00008613 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
8614 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008615 } else {
8616 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
8617 BDINFO_FLAGS_DISABLED);
8618 }
8619
Joe Perches63c3a662011-04-26 08:12:10 +00008620 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonfa6b2aa2011-11-21 15:01:19 +00008621 val = TG3_RX_STD_RING_SIZE(tp);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008622 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
8623 val |= (TG3_RX_STD_DMA_SZ << 2);
8624 } else
Matt Carlson04380d42010-04-12 06:58:29 +00008625 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008626 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008627 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008628
8629 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008630
Matt Carlson411da642009-11-13 13:03:46 +00008631 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e62009-11-13 13:03:49 +00008632 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008633
Joe Perches63c3a662011-04-26 08:12:10 +00008634 tpr->rx_jmb_prod_idx =
8635 tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
Matt Carlson66711e62009-11-13 13:03:49 +00008636 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008637
Matt Carlson2d31eca2009-09-01 12:53:31 +00008638 tg3_rings_reset(tp);
8639
Linus Torvalds1da177e2005-04-16 15:20:36 -07008640 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07008641 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008642
8643 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00008644 tw32(MAC_RX_MTU_SIZE,
8645 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008646
8647 /* The slot time is changed by tg3_setup_phy if we
8648 * run at gigabit with half duplex.
8649 */
Matt Carlsonf2096f92011-04-05 14:22:48 +00008650 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
8651 (6 << TX_LENGTHS_IPG_SHIFT) |
8652 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
8653
8654 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8655 val |= tr32(MAC_TX_LENGTHS) &
8656 (TX_LENGTHS_JMB_FRM_LEN_MSK |
8657 TX_LENGTHS_CNT_DWN_VAL_MSK);
8658
8659 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008660
8661 /* Receive rules. */
8662 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
8663 tw32(RCVLPC_CONFIG, 0x0181);
8664
8665 /* Calculate RDMAC_MODE setting early, we need it to determine
8666 * the RCVLPC_STATE_ENABLE mask.
8667 */
8668 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
8669 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
8670 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
8671 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
8672 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07008673
Matt Carlsondeabaac2010-11-24 08:31:50 +00008674 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +00008675 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
8676
Matt Carlson57e69832008-05-25 23:48:31 -07008677 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08008678 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8679 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07008680 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
8681 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
8682 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
8683
Matt Carlsonc5908932011-03-09 16:58:25 +00008684 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8685 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00008686 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07008687 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008688 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
8689 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008690 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008691 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8692 }
8693 }
8694
Joe Perches63c3a662011-04-26 08:12:10 +00008695 if (tg3_flag(tp, PCI_EXPRESS))
Michael Chan85e94ce2005-04-21 17:05:28 -07008696 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8697
Matt Carlson55086ad2011-12-14 11:09:59 +00008698 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57766)
8699 rdmac_mode |= RDMAC_MODE_JMB_2K_MMRR;
8700
Joe Perches63c3a662011-04-26 08:12:10 +00008701 if (tg3_flag(tp, HW_TSO_1) ||
8702 tg3_flag(tp, HW_TSO_2) ||
8703 tg3_flag(tp, HW_TSO_3))
Matt Carlson027455a2008-12-21 20:19:30 -08008704 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
8705
Matt Carlson108a6c12011-05-19 12:12:47 +00008706 if (tg3_flag(tp, 57765_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +00008707 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson027455a2008-12-21 20:19:30 -08008708 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
8709 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008710
Matt Carlsonf2096f92011-04-05 14:22:48 +00008711 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8712 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
8713
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008714 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
8715 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
8716 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8717 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008718 tg3_flag(tp, 57765_PLUS)) {
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008719 val = tr32(TG3_RDMA_RSRVCTRL_REG);
Matt Carlsond78b59f2011-04-05 14:22:46 +00008720 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8721 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +00008722 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
8723 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
8724 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
8725 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
8726 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
8727 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +00008728 }
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008729 tw32(TG3_RDMA_RSRVCTRL_REG,
8730 val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
8731 }
8732
Matt Carlsond78b59f2011-04-05 14:22:46 +00008733 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8734 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsond309a462010-09-30 10:34:31 +00008735 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
8736 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val |
8737 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
8738 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
8739 }
8740
Linus Torvalds1da177e2005-04-16 15:20:36 -07008741 /* Receive/send statistics. */
Joe Perches63c3a662011-04-26 08:12:10 +00008742 if (tg3_flag(tp, 5750_PLUS)) {
Michael Chan16613942006-06-29 20:15:13 -07008743 val = tr32(RCVLPC_STATS_ENABLE);
8744 val &= ~RCVLPC_STATSENAB_DACK_FIX;
8745 tw32(RCVLPC_STATS_ENABLE, val);
8746 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008747 tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008748 val = tr32(RCVLPC_STATS_ENABLE);
8749 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
8750 tw32(RCVLPC_STATS_ENABLE, val);
8751 } else {
8752 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
8753 }
8754 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
8755 tw32(SNDDATAI_STATSENAB, 0xffffff);
8756 tw32(SNDDATAI_STATSCTRL,
8757 (SNDDATAI_SCTRL_ENABLE |
8758 SNDDATAI_SCTRL_FASTUPD));
8759
8760 /* Setup host coalescing engine. */
8761 tw32(HOSTCC_MODE, 0);
8762 for (i = 0; i < 2000; i++) {
8763 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
8764 break;
8765 udelay(10);
8766 }
8767
Michael Chand244c892005-07-05 14:42:33 -07008768 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008769
Joe Perches63c3a662011-04-26 08:12:10 +00008770 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008771 /* Status/statistics block address. See tg3_timer,
8772 * the tg3_periodic_fetch_stats call there, and
8773 * tg3_get_stats to see how this works for 5705/5750 chips.
8774 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008775 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8776 ((u64) tp->stats_mapping >> 32));
8777 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8778 ((u64) tp->stats_mapping & 0xffffffff));
8779 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008780
Linus Torvalds1da177e2005-04-16 15:20:36 -07008781 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008782
8783 /* Clear statistics and status block memory areas */
8784 for (i = NIC_SRAM_STATS_BLK;
8785 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
8786 i += sizeof(u32)) {
8787 tg3_write_mem(tp, i, 0);
8788 udelay(40);
8789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008790 }
8791
8792 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
8793
8794 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
8795 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008796 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008797 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
8798
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008799 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
8800 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -07008801 /* reset to prevent losing 1st rx packet intermittently */
8802 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8803 udelay(10);
8804 }
8805
Matt Carlson3bda1252008-08-15 14:08:22 -07008806 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Matt Carlson9e975cc2011-07-20 10:20:50 +00008807 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE |
8808 MAC_MODE_FHDE_ENABLE;
8809 if (tg3_flag(tp, ENABLE_APE))
8810 tp->mac_mode |= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Joe Perches63c3a662011-04-26 08:12:10 +00008811 if (!tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008812 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07008813 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
8814 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008815 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
8816 udelay(40);
8817
Michael Chan314fba32005-04-21 17:07:04 -07008818 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Joe Perches63c3a662011-04-26 08:12:10 +00008819 * If TG3_FLAG_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07008820 * register to preserve the GPIO settings for LOMs. The GPIOs,
8821 * whether used as inputs or outputs, are set by boot code after
8822 * reset.
8823 */
Joe Perches63c3a662011-04-26 08:12:10 +00008824 if (!tg3_flag(tp, IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07008825 u32 gpio_mask;
8826
Michael Chan9d26e212006-12-07 00:21:14 -08008827 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
8828 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
8829 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07008830
8831 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
8832 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
8833 GRC_LCLCTRL_GPIO_OUTPUT3;
8834
Michael Chanaf36e6b2006-03-23 01:28:06 -08008835 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
8836 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
8837
Gary Zambranoaaf84462007-05-05 11:51:45 -07008838 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07008839 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
8840
8841 /* GPIO1 must be driven high for eeprom write protect */
Joe Perches63c3a662011-04-26 08:12:10 +00008842 if (tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan9d26e212006-12-07 00:21:14 -08008843 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
8844 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07008845 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008846 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8847 udelay(100);
8848
Matt Carlsonc3b50032012-01-17 15:27:23 +00008849 if (tg3_flag(tp, USING_MSIX)) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008850 val = tr32(MSGINT_MODE);
Matt Carlsonc3b50032012-01-17 15:27:23 +00008851 val |= MSGINT_MODE_ENABLE;
8852 if (tp->irq_cnt > 1)
8853 val |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +00008854 if (!tg3_flag(tp, 1SHOT_MSI))
8855 val |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008856 tw32(MSGINT_MODE, val);
8857 }
8858
Joe Perches63c3a662011-04-26 08:12:10 +00008859 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008860 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
8861 udelay(40);
8862 }
8863
8864 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
8865 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
8866 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
8867 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
8868 WDMAC_MODE_LNGREAD_ENAB);
8869
Matt Carlsonc5908932011-03-09 16:58:25 +00008870 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8871 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00008872 if (tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008873 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
8874 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
8875 /* nothing */
8876 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008877 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008878 val |= WDMAC_MODE_RX_ACCEL;
8879 }
8880 }
8881
Michael Chand9ab5ad12006-03-20 22:27:35 -08008882 /* Enable host coalescing bug fix */
Joe Perches63c3a662011-04-26 08:12:10 +00008883 if (tg3_flag(tp, 5755_PLUS))
Matt Carlsonf51f3562008-05-25 23:45:08 -07008884 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad12006-03-20 22:27:35 -08008885
Matt Carlson788a0352009-11-02 14:26:03 +00008886 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
8887 val |= WDMAC_MODE_BURST_ALL_DATA;
8888
Linus Torvalds1da177e2005-04-16 15:20:36 -07008889 tw32_f(WDMAC_MODE, val);
8890 udelay(40);
8891
Joe Perches63c3a662011-04-26 08:12:10 +00008892 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07008893 u16 pcix_cmd;
8894
8895 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8896 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008897 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07008898 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
8899 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008900 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07008901 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
8902 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008903 }
Matt Carlson9974a352007-10-07 23:27:28 -07008904 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8905 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008906 }
8907
8908 tw32_f(RDMAC_MODE, rdmac_mode);
8909 udelay(40);
8910
8911 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008912 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008913 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07008914
8915 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
8916 tw32(SNDDATAC_MODE,
8917 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
8918 else
8919 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
8920
Linus Torvalds1da177e2005-04-16 15:20:36 -07008921 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
8922 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008923 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00008924 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008925 val |= RCVDBDI_MODE_LRG_RING_SZ;
8926 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008927 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008928 if (tg3_flag(tp, HW_TSO_1) ||
8929 tg3_flag(tp, HW_TSO_2) ||
8930 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008931 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008932 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00008933 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008934 val |= SNDBDI_MODE_MULTI_TXQ_EN;
8935 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008936 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
8937
8938 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
8939 err = tg3_load_5701_a0_firmware_fix(tp);
8940 if (err)
8941 return err;
8942 }
8943
Joe Perches63c3a662011-04-26 08:12:10 +00008944 if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008945 err = tg3_load_tso_firmware(tp);
8946 if (err)
8947 return err;
8948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008949
8950 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +00008951
Joe Perches63c3a662011-04-26 08:12:10 +00008952 if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsonb1d05212010-06-05 17:24:31 +00008953 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
8954 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +00008955
8956 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
8957 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
8958 tp->tx_mode &= ~val;
8959 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
8960 }
8961
Linus Torvalds1da177e2005-04-16 15:20:36 -07008962 tw32_f(MAC_TX_MODE, tp->tx_mode);
8963 udelay(100);
8964
Joe Perches63c3a662011-04-26 08:12:10 +00008965 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008966 tg3_rss_write_indir_tbl(tp);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008967
8968 /* Setup the "secret" hash key. */
8969 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
8970 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
8971 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
8972 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
8973 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
8974 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
8975 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
8976 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
8977 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
8978 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
8979 }
8980
Linus Torvalds1da177e2005-04-16 15:20:36 -07008981 tp->rx_mode = RX_MODE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00008982 if (tg3_flag(tp, 5755_PLUS))
Michael Chanaf36e6b2006-03-23 01:28:06 -08008983 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
8984
Joe Perches63c3a662011-04-26 08:12:10 +00008985 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008986 tp->rx_mode |= RX_MODE_RSS_ENABLE |
8987 RX_MODE_RSS_ITBL_HASH_BITS_7 |
8988 RX_MODE_RSS_IPV6_HASH_EN |
8989 RX_MODE_RSS_TCP_IPV6_HASH_EN |
8990 RX_MODE_RSS_IPV4_HASH_EN |
8991 RX_MODE_RSS_TCP_IPV4_HASH_EN;
8992
Linus Torvalds1da177e2005-04-16 15:20:36 -07008993 tw32_f(MAC_RX_MODE, tp->rx_mode);
8994 udelay(10);
8995
Linus Torvalds1da177e2005-04-16 15:20:36 -07008996 tw32(MAC_LED_CTRL, tp->led_ctrl);
8997
8998 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008999 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009000 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
9001 udelay(10);
9002 }
9003 tw32_f(MAC_RX_MODE, tp->rx_mode);
9004 udelay(10);
9005
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009006 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009007 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009008 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009009 /* Set drive transmission level to 1.2V */
9010 /* only if the signal pre-emphasis bit is not set */
9011 val = tr32(MAC_SERDES_CFG);
9012 val &= 0xfffff000;
9013 val |= 0x880;
9014 tw32(MAC_SERDES_CFG, val);
9015 }
9016 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
9017 tw32(MAC_SERDES_CFG, 0x616000);
9018 }
9019
9020 /* Prevent chip from dropping frames when flow control
9021 * is enabled.
9022 */
Matt Carlson55086ad2011-12-14 11:09:59 +00009023 if (tg3_flag(tp, 57765_CLASS))
Matt Carlson666bc832010-01-20 16:58:03 +00009024 val = 1;
9025 else
9026 val = 2;
9027 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009028
9029 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009030 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009031 /* Use hardware link auto-negotiation */
Joe Perches63c3a662011-04-26 08:12:10 +00009032 tg3_flag_set(tp, HW_AUTONEG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009033 }
9034
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009035 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +00009036 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08009037 u32 tmp;
9038
9039 tmp = tr32(SERDES_RX_CTRL);
9040 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
9041 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
9042 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
9043 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
9044 }
9045
Joe Perches63c3a662011-04-26 08:12:10 +00009046 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson80096062010-08-02 11:26:06 +00009047 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
9048 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07009049 tp->link_config.speed = tp->link_config.orig_speed;
9050 tp->link_config.duplex = tp->link_config.orig_duplex;
9051 tp->link_config.autoneg = tp->link_config.orig_autoneg;
9052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009053
Matt Carlsondd477002008-05-25 23:45:58 -07009054 err = tg3_setup_phy(tp, 0);
9055 if (err)
9056 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009057
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009058 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
9059 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07009060 u32 tmp;
9061
9062 /* Clear CRC stats. */
9063 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
9064 tg3_writephy(tp, MII_TG3_TEST1,
9065 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009066 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -07009067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009068 }
9069 }
9070
9071 __tg3_set_rx_mode(tp->dev);
9072
9073 /* Initialize receive rules. */
9074 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
9075 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
9076 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
9077 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
9078
Joe Perches63c3a662011-04-26 08:12:10 +00009079 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009080 limit = 8;
9081 else
9082 limit = 16;
Joe Perches63c3a662011-04-26 08:12:10 +00009083 if (tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009084 limit -= 4;
9085 switch (limit) {
9086 case 16:
9087 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
9088 case 15:
9089 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
9090 case 14:
9091 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
9092 case 13:
9093 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
9094 case 12:
9095 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
9096 case 11:
9097 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
9098 case 10:
9099 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
9100 case 9:
9101 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
9102 case 8:
9103 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
9104 case 7:
9105 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
9106 case 6:
9107 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
9108 case 5:
9109 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
9110 case 4:
9111 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
9112 case 3:
9113 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
9114 case 2:
9115 case 1:
9116
9117 default:
9118 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07009119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009120
Joe Perches63c3a662011-04-26 08:12:10 +00009121 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson9ce768e2007-10-11 19:49:11 -07009122 /* Write our heartbeat update interval to APE. */
9123 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
9124 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07009125
Linus Torvalds1da177e2005-04-16 15:20:36 -07009126 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
9127
Linus Torvalds1da177e2005-04-16 15:20:36 -07009128 return 0;
9129}
9130
9131/* Called at device open time to get the chip ready for
9132 * packet processing. Invoked with tp->lock held.
9133 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009134static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009135{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009136 tg3_switch_clocks(tp);
9137
9138 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
9139
Matt Carlson2f751b62008-08-04 23:17:34 -07009140 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009141}
9142
9143#define TG3_STAT_ADD32(PSTAT, REG) \
9144do { u32 __val = tr32(REG); \
9145 (PSTAT)->low += __val; \
9146 if ((PSTAT)->low < __val) \
9147 (PSTAT)->high += 1; \
9148} while (0)
9149
9150static void tg3_periodic_fetch_stats(struct tg3 *tp)
9151{
9152 struct tg3_hw_stats *sp = tp->hw_stats;
9153
9154 if (!netif_carrier_ok(tp->dev))
9155 return;
9156
9157 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
9158 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
9159 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
9160 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
9161 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
9162 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
9163 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
9164 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
9165 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
9166 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
9167 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
9168 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
9169 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
9170
9171 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
9172 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
9173 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
9174 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
9175 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
9176 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
9177 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
9178 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
9179 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
9180 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
9181 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
9182 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
9183 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
9184 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07009185
9186 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
Matt Carlson310050f2011-05-19 12:12:55 +00009187 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
9188 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0 &&
9189 tp->pci_chip_rev_id != CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +00009190 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
9191 } else {
9192 u32 val = tr32(HOSTCC_FLOW_ATTN);
9193 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
9194 if (val) {
9195 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
9196 sp->rx_discards.low += val;
9197 if (sp->rx_discards.low < val)
9198 sp->rx_discards.high += 1;
9199 }
9200 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
9201 }
Michael Chan463d3052006-05-22 16:36:27 -07009202 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009203}
9204
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009205static void tg3_chk_missed_msi(struct tg3 *tp)
9206{
9207 u32 i;
9208
9209 for (i = 0; i < tp->irq_cnt; i++) {
9210 struct tg3_napi *tnapi = &tp->napi[i];
9211
9212 if (tg3_has_work(tnapi)) {
9213 if (tnapi->last_rx_cons == tnapi->rx_rcb_ptr &&
9214 tnapi->last_tx_cons == tnapi->tx_cons) {
9215 if (tnapi->chk_msi_cnt < 1) {
9216 tnapi->chk_msi_cnt++;
9217 return;
9218 }
Matt Carlson7f230732011-08-31 11:44:48 +00009219 tg3_msi(0, tnapi);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009220 }
9221 }
9222 tnapi->chk_msi_cnt = 0;
9223 tnapi->last_rx_cons = tnapi->rx_rcb_ptr;
9224 tnapi->last_tx_cons = tnapi->tx_cons;
9225 }
9226}
9227
Linus Torvalds1da177e2005-04-16 15:20:36 -07009228static void tg3_timer(unsigned long __opaque)
9229{
9230 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009231
Matt Carlson5b190622011-11-04 09:15:04 +00009232 if (tp->irq_sync || tg3_flag(tp, RESET_TASK_PENDING))
Michael Chanf475f162006-03-27 23:20:14 -08009233 goto restart_timer;
9234
David S. Millerf47c11e2005-06-24 20:18:35 -07009235 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009236
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009237 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00009238 tg3_flag(tp, 57765_CLASS))
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009239 tg3_chk_missed_msi(tp);
9240
Joe Perches63c3a662011-04-26 08:12:10 +00009241 if (!tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -07009242 /* All of this garbage is because when using non-tagged
9243 * IRQ status the mailbox/status_block protocol the chip
9244 * uses with the cpu is race prone.
9245 */
Matt Carlson898a56f2009-08-28 14:02:40 +00009246 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -07009247 tw32(GRC_LOCAL_CTRL,
9248 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
9249 } else {
9250 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00009251 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -07009252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009253
David S. Millerfac9b832005-05-18 22:46:34 -07009254 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
David S. Millerf47c11e2005-06-24 20:18:35 -07009255 spin_unlock(&tp->lock);
Matt Carlsondb219972011-11-04 09:15:03 +00009256 tg3_reset_task_schedule(tp);
Matt Carlson5b190622011-11-04 09:15:04 +00009257 goto restart_timer;
David S. Millerfac9b832005-05-18 22:46:34 -07009258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009259 }
9260
Linus Torvalds1da177e2005-04-16 15:20:36 -07009261 /* This part only runs once per second. */
9262 if (!--tp->timer_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00009263 if (tg3_flag(tp, 5705_PLUS))
David S. Millerfac9b832005-05-18 22:46:34 -07009264 tg3_periodic_fetch_stats(tp);
9265
Matt Carlsonb0c59432011-05-19 12:12:48 +00009266 if (tp->setlpicnt && !--tp->setlpicnt)
9267 tg3_phy_eee_enable(tp);
Matt Carlson52b02d02010-10-14 10:37:41 +00009268
Joe Perches63c3a662011-04-26 08:12:10 +00009269 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009270 u32 mac_stat;
9271 int phy_event;
9272
9273 mac_stat = tr32(MAC_STATUS);
9274
9275 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009276 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009277 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
9278 phy_event = 1;
9279 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
9280 phy_event = 1;
9281
9282 if (phy_event)
9283 tg3_setup_phy(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +00009284 } else if (tg3_flag(tp, POLL_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009285 u32 mac_stat = tr32(MAC_STATUS);
9286 int need_setup = 0;
9287
9288 if (netif_carrier_ok(tp->dev) &&
9289 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
9290 need_setup = 1;
9291 }
Matt Carlsonbe98da62010-07-11 09:31:46 +00009292 if (!netif_carrier_ok(tp->dev) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009293 (mac_stat & (MAC_STATUS_PCS_SYNCED |
9294 MAC_STATUS_SIGNAL_DET))) {
9295 need_setup = 1;
9296 }
9297 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07009298 if (!tp->serdes_counter) {
9299 tw32_f(MAC_MODE,
9300 (tp->mac_mode &
9301 ~MAC_MODE_PORT_MODE_MASK));
9302 udelay(40);
9303 tw32_f(MAC_MODE, tp->mac_mode);
9304 udelay(40);
9305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009306 tg3_setup_phy(tp, 0);
9307 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009308 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009309 tg3_flag(tp, 5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -07009310 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +00009311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009312
9313 tp->timer_counter = tp->timer_multiplier;
9314 }
9315
Michael Chan130b8e42006-09-27 16:00:40 -07009316 /* Heartbeat is only sent once every 2 seconds.
9317 *
9318 * The heartbeat is to tell the ASF firmware that the host
9319 * driver is still alive. In the event that the OS crashes,
9320 * ASF needs to reset the hardware to free up the FIFO space
9321 * that may be filled with rx packets destined for the host.
9322 * If the FIFO is full, ASF will no longer function properly.
9323 *
9324 * Unintended resets have been reported on real time kernels
9325 * where the timer doesn't run on time. Netpoll will also have
9326 * same problem.
9327 *
9328 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
9329 * to check the ring condition when the heartbeat is expiring
9330 * before doing the reset. This will prevent most unintended
9331 * resets.
9332 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009333 if (!--tp->asf_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00009334 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07009335 tg3_wait_for_event_ack(tp);
9336
Michael Chanbbadf502006-04-06 21:46:34 -07009337 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07009338 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07009339 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +00009340 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
9341 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -07009342
9343 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009344 }
9345 tp->asf_counter = tp->asf_multiplier;
9346 }
9347
David S. Millerf47c11e2005-06-24 20:18:35 -07009348 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009349
Michael Chanf475f162006-03-27 23:20:14 -08009350restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07009351 tp->timer.expires = jiffies + tp->timer_offset;
9352 add_timer(&tp->timer);
9353}
9354
Matt Carlson4f125f42009-09-01 12:55:02 +00009355static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -08009356{
David Howells7d12e782006-10-05 14:55:46 +01009357 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009358 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +00009359 char *name;
9360 struct tg3_napi *tnapi = &tp->napi[irq_num];
9361
9362 if (tp->irq_cnt == 1)
9363 name = tp->dev->name;
9364 else {
9365 name = &tnapi->irq_lbl[0];
9366 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
9367 name[IFNAMSIZ-1] = 0;
9368 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009369
Joe Perches63c3a662011-04-26 08:12:10 +00009370 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Michael Chanfcfa0a32006-03-20 22:28:41 -08009371 fn = tg3_msi;
Joe Perches63c3a662011-04-26 08:12:10 +00009372 if (tg3_flag(tp, 1SHOT_MSI))
Michael Chanfcfa0a32006-03-20 22:28:41 -08009373 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009374 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009375 } else {
9376 fn = tg3_interrupt;
Joe Perches63c3a662011-04-26 08:12:10 +00009377 if (tg3_flag(tp, TAGGED_STATUS))
Michael Chanfcfa0a32006-03-20 22:28:41 -08009378 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009379 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009380 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009381
9382 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009383}
9384
Michael Chan79381092005-04-21 17:13:59 -07009385static int tg3_test_interrupt(struct tg3 *tp)
9386{
Matt Carlson09943a12009-08-28 14:01:57 +00009387 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -07009388 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -07009389 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009390 u32 val;
Michael Chan79381092005-04-21 17:13:59 -07009391
Michael Chand4bc3922005-05-29 14:59:20 -07009392 if (!netif_running(dev))
9393 return -ENODEV;
9394
Michael Chan79381092005-04-21 17:13:59 -07009395 tg3_disable_ints(tp);
9396
Matt Carlson4f125f42009-09-01 12:55:02 +00009397 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009398
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009399 /*
9400 * Turn off MSI one shot mode. Otherwise this test has no
9401 * observable way to know whether the interrupt was delivered.
9402 */
Matt Carlson3aa1cdf2011-07-20 10:20:55 +00009403 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009404 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
9405 tw32(MSGINT_MODE, val);
9406 }
9407
Matt Carlson4f125f42009-09-01 12:55:02 +00009408 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Matt Carlson09943a12009-08-28 14:01:57 +00009409 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009410 if (err)
9411 return err;
9412
Matt Carlson898a56f2009-08-28 14:02:40 +00009413 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -07009414 tg3_enable_ints(tp);
9415
9416 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00009417 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -07009418
9419 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -07009420 u32 int_mbox, misc_host_ctrl;
9421
Matt Carlson898a56f2009-08-28 14:02:40 +00009422 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -07009423 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
9424
9425 if ((int_mbox != 0) ||
9426 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
9427 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -07009428 break;
Michael Chanb16250e2006-09-27 16:10:14 -07009429 }
9430
Matt Carlson3aa1cdf2011-07-20 10:20:55 +00009431 if (tg3_flag(tp, 57765_PLUS) &&
9432 tnapi->hw_status->status_tag != tnapi->last_tag)
9433 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
9434
Michael Chan79381092005-04-21 17:13:59 -07009435 msleep(10);
9436 }
9437
9438 tg3_disable_ints(tp);
9439
Matt Carlson4f125f42009-09-01 12:55:02 +00009440 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009441
Matt Carlson4f125f42009-09-01 12:55:02 +00009442 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009443
9444 if (err)
9445 return err;
9446
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009447 if (intr_ok) {
9448 /* Reenable MSI one shot mode. */
Matt Carlson5b39de92011-08-31 11:44:50 +00009449 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, 1SHOT_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009450 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
9451 tw32(MSGINT_MODE, val);
9452 }
Michael Chan79381092005-04-21 17:13:59 -07009453 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009454 }
Michael Chan79381092005-04-21 17:13:59 -07009455
9456 return -EIO;
9457}
9458
9459/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
9460 * successfully restored
9461 */
9462static int tg3_test_msi(struct tg3 *tp)
9463{
Michael Chan79381092005-04-21 17:13:59 -07009464 int err;
9465 u16 pci_cmd;
9466
Joe Perches63c3a662011-04-26 08:12:10 +00009467 if (!tg3_flag(tp, USING_MSI))
Michael Chan79381092005-04-21 17:13:59 -07009468 return 0;
9469
9470 /* Turn off SERR reporting in case MSI terminates with Master
9471 * Abort.
9472 */
9473 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
9474 pci_write_config_word(tp->pdev, PCI_COMMAND,
9475 pci_cmd & ~PCI_COMMAND_SERR);
9476
9477 err = tg3_test_interrupt(tp);
9478
9479 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
9480
9481 if (!err)
9482 return 0;
9483
9484 /* other failures */
9485 if (err != -EIO)
9486 return err;
9487
9488 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009489 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
9490 "to INTx mode. Please report this failure to the PCI "
9491 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -07009492
Matt Carlson4f125f42009-09-01 12:55:02 +00009493 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +00009494
Michael Chan79381092005-04-21 17:13:59 -07009495 pci_disable_msi(tp->pdev);
9496
Joe Perches63c3a662011-04-26 08:12:10 +00009497 tg3_flag_clear(tp, USING_MSI);
Andre Detschdc8bf1b2010-04-26 07:27:07 +00009498 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -07009499
Matt Carlson4f125f42009-09-01 12:55:02 +00009500 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009501 if (err)
9502 return err;
9503
9504 /* Need to reset the chip because the MSI cycle may have terminated
9505 * with Master Abort.
9506 */
David S. Millerf47c11e2005-06-24 20:18:35 -07009507 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009508
Michael Chan944d9802005-05-29 14:57:48 -07009509 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009510 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009511
David S. Millerf47c11e2005-06-24 20:18:35 -07009512 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009513
9514 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +00009515 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -07009516
9517 return err;
9518}
9519
Matt Carlson9e9fd122009-01-19 16:57:45 -08009520static int tg3_request_firmware(struct tg3 *tp)
9521{
9522 const __be32 *fw_data;
9523
9524 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009525 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
9526 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009527 return -ENOENT;
9528 }
9529
9530 fw_data = (void *)tp->fw->data;
9531
9532 /* Firmware blob starts with version numbers, followed by
9533 * start address and _full_ length including BSS sections
9534 * (which must be longer than the actual data, of course
9535 */
9536
9537 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
9538 if (tp->fw_len < (tp->fw->size - 12)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009539 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
9540 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009541 release_firmware(tp->fw);
9542 tp->fw = NULL;
9543 return -EINVAL;
9544 }
9545
9546 /* We no longer need firmware; we have it. */
9547 tp->fw_needed = NULL;
9548 return 0;
9549}
9550
Matt Carlson679563f2009-09-01 12:55:46 +00009551static bool tg3_enable_msix(struct tg3 *tp)
9552{
Matt Carlsonc3b50032012-01-17 15:27:23 +00009553 int i, rc;
Matt Carlson679563f2009-09-01 12:55:46 +00009554 struct msix_entry msix_ent[tp->irq_max];
9555
Matt Carlsonc3b50032012-01-17 15:27:23 +00009556 tp->irq_cnt = num_online_cpus();
9557 if (tp->irq_cnt > 1) {
9558 /* We want as many rx rings enabled as there are cpus.
9559 * In multiqueue MSI-X mode, the first MSI-X vector
9560 * only deals with link interrupts, etc, so we add
9561 * one to the number of vectors we are requesting.
9562 */
9563 tp->irq_cnt = min_t(unsigned, tp->irq_cnt + 1, tp->irq_max);
9564 }
Matt Carlson679563f2009-09-01 12:55:46 +00009565
9566 for (i = 0; i < tp->irq_max; i++) {
9567 msix_ent[i].entry = i;
9568 msix_ent[i].vector = 0;
9569 }
9570
9571 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +00009572 if (rc < 0) {
9573 return false;
9574 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +00009575 if (pci_enable_msix(tp->pdev, msix_ent, rc))
9576 return false;
Joe Perches05dbe002010-02-17 19:44:19 +00009577 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
9578 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +00009579 tp->irq_cnt = rc;
9580 }
9581
9582 for (i = 0; i < tp->irq_max; i++)
9583 tp->napi[i].irq_vec = msix_ent[i].vector;
9584
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009585 netif_set_real_num_tx_queues(tp->dev, 1);
9586 rc = tp->irq_cnt > 1 ? tp->irq_cnt - 1 : 1;
9587 if (netif_set_real_num_rx_queues(tp->dev, rc)) {
9588 pci_disable_msix(tp->pdev);
9589 return false;
9590 }
Matt Carlsonb92b9042010-11-24 08:31:51 +00009591
9592 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +00009593 tg3_flag_set(tp, ENABLE_RSS);
Matt Carlsond78b59f2011-04-05 14:22:46 +00009594
9595 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
9596 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Joe Perches63c3a662011-04-26 08:12:10 +00009597 tg3_flag_set(tp, ENABLE_TSS);
Matt Carlsonb92b9042010-11-24 08:31:51 +00009598 netif_set_real_num_tx_queues(tp->dev, tp->irq_cnt - 1);
9599 }
9600 }
Matt Carlson2430b032010-06-05 17:24:34 +00009601
Matt Carlson679563f2009-09-01 12:55:46 +00009602 return true;
9603}
9604
Matt Carlson07b01732009-08-28 14:01:15 +00009605static void tg3_ints_init(struct tg3 *tp)
9606{
Joe Perches63c3a662011-04-26 08:12:10 +00009607 if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
9608 !tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +00009609 /* All MSI supporting chips should support tagged
9610 * status. Assert that this is the case.
9611 */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009612 netdev_warn(tp->dev,
9613 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +00009614 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +00009615 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009616
Joe Perches63c3a662011-04-26 08:12:10 +00009617 if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
9618 tg3_flag_set(tp, USING_MSIX);
9619 else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
9620 tg3_flag_set(tp, USING_MSI);
Matt Carlson679563f2009-09-01 12:55:46 +00009621
Joe Perches63c3a662011-04-26 08:12:10 +00009622 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009623 u32 msi_mode = tr32(MSGINT_MODE);
Joe Perches63c3a662011-04-26 08:12:10 +00009624 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009625 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +00009626 if (!tg3_flag(tp, 1SHOT_MSI))
9627 msi_mode |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlson679563f2009-09-01 12:55:46 +00009628 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
9629 }
9630defcfg:
Joe Perches63c3a662011-04-26 08:12:10 +00009631 if (!tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009632 tp->irq_cnt = 1;
9633 tp->napi[0].irq_vec = tp->pdev->irq;
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009634 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -07009635 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +00009636 }
Matt Carlson07b01732009-08-28 14:01:15 +00009637}
9638
9639static void tg3_ints_fini(struct tg3 *tp)
9640{
Joe Perches63c3a662011-04-26 08:12:10 +00009641 if (tg3_flag(tp, USING_MSIX))
Matt Carlson679563f2009-09-01 12:55:46 +00009642 pci_disable_msix(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009643 else if (tg3_flag(tp, USING_MSI))
Matt Carlson679563f2009-09-01 12:55:46 +00009644 pci_disable_msi(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009645 tg3_flag_clear(tp, USING_MSI);
9646 tg3_flag_clear(tp, USING_MSIX);
9647 tg3_flag_clear(tp, ENABLE_RSS);
9648 tg3_flag_clear(tp, ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +00009649}
9650
Linus Torvalds1da177e2005-04-16 15:20:36 -07009651static int tg3_open(struct net_device *dev)
9652{
9653 struct tg3 *tp = netdev_priv(dev);
Matt Carlson4f125f42009-09-01 12:55:02 +00009654 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009655
Matt Carlson9e9fd122009-01-19 16:57:45 -08009656 if (tp->fw_needed) {
9657 err = tg3_request_firmware(tp);
9658 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
9659 if (err)
9660 return err;
9661 } else if (err) {
Joe Perches05dbe002010-02-17 19:44:19 +00009662 netdev_warn(tp->dev, "TSO capability disabled\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009663 tg3_flag_clear(tp, TSO_CAPABLE);
9664 } else if (!tg3_flag(tp, TSO_CAPABLE)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009665 netdev_notice(tp->dev, "TSO capability restored\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009666 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009667 }
9668 }
9669
Michael Chanc49a1562006-12-17 17:07:29 -08009670 netif_carrier_off(tp->dev);
9671
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009672 err = tg3_power_up(tp);
Matt Carlson2f751b62008-08-04 23:17:34 -07009673 if (err)
Michael Chanbc1c7562006-03-20 17:48:03 -08009674 return err;
Matt Carlson2f751b62008-08-04 23:17:34 -07009675
9676 tg3_full_lock(tp, 0);
Michael Chanbc1c7562006-03-20 17:48:03 -08009677
Linus Torvalds1da177e2005-04-16 15:20:36 -07009678 tg3_disable_ints(tp);
Joe Perches63c3a662011-04-26 08:12:10 +00009679 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009680
David S. Millerf47c11e2005-06-24 20:18:35 -07009681 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009682
Matt Carlson679563f2009-09-01 12:55:46 +00009683 /*
9684 * Setup interrupts first so we know how
9685 * many NAPI resources to allocate
9686 */
9687 tg3_ints_init(tp);
9688
Matt Carlson90415472011-12-16 13:33:23 +00009689 tg3_rss_check_indir_tbl(tp);
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009690
Linus Torvalds1da177e2005-04-16 15:20:36 -07009691 /* The placement of this call is tied
9692 * to the setup and use of Host TX descriptors.
9693 */
9694 err = tg3_alloc_consistent(tp);
9695 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009696 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009697
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009698 tg3_napi_init(tp);
9699
Matt Carlsonfed97812009-09-01 13:10:19 +00009700 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07009701
Matt Carlson4f125f42009-09-01 12:55:02 +00009702 for (i = 0; i < tp->irq_cnt; i++) {
9703 struct tg3_napi *tnapi = &tp->napi[i];
9704 err = tg3_request_irq(tp, i);
9705 if (err) {
Matt Carlson5bc09182011-11-04 09:15:01 +00009706 for (i--; i >= 0; i--) {
9707 tnapi = &tp->napi[i];
Matt Carlson4f125f42009-09-01 12:55:02 +00009708 free_irq(tnapi->irq_vec, tnapi);
Matt Carlson5bc09182011-11-04 09:15:01 +00009709 }
9710 goto err_out2;
Matt Carlson4f125f42009-09-01 12:55:02 +00009711 }
9712 }
Matt Carlson07b01732009-08-28 14:01:15 +00009713
David S. Millerf47c11e2005-06-24 20:18:35 -07009714 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009715
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009716 err = tg3_init_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009717 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -07009718 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009719 tg3_free_rings(tp);
9720 } else {
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009721 if (tg3_flag(tp, TAGGED_STATUS) &&
Matt Carlson55086ad2011-12-14 11:09:59 +00009722 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
9723 !tg3_flag(tp, 57765_CLASS))
David S. Millerfac9b832005-05-18 22:46:34 -07009724 tp->timer_offset = HZ;
9725 else
9726 tp->timer_offset = HZ / 10;
9727
9728 BUG_ON(tp->timer_offset > HZ);
9729 tp->timer_counter = tp->timer_multiplier =
9730 (HZ / tp->timer_offset);
9731 tp->asf_counter = tp->asf_multiplier =
Michael Chan28fbef72005-10-26 15:48:35 -07009732 ((HZ / tp->timer_offset) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009733
9734 init_timer(&tp->timer);
9735 tp->timer.expires = jiffies + tp->timer_offset;
9736 tp->timer.data = (unsigned long) tp;
9737 tp->timer.function = tg3_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009738 }
9739
David S. Millerf47c11e2005-06-24 20:18:35 -07009740 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009741
Matt Carlson07b01732009-08-28 14:01:15 +00009742 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009743 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009744
Joe Perches63c3a662011-04-26 08:12:10 +00009745 if (tg3_flag(tp, USING_MSI)) {
Michael Chan79381092005-04-21 17:13:59 -07009746 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -07009747
Michael Chan79381092005-04-21 17:13:59 -07009748 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -07009749 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -07009750 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -07009751 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07009752 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009753
Matt Carlson679563f2009-09-01 12:55:46 +00009754 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -07009755 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009756
Joe Perches63c3a662011-04-26 08:12:10 +00009757 if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009758 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009759
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009760 tw32(PCIE_TRANSACTION_CFG,
9761 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009762 }
Michael Chan79381092005-04-21 17:13:59 -07009763 }
9764
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009765 tg3_phy_start(tp);
9766
David S. Millerf47c11e2005-06-24 20:18:35 -07009767 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009768
Michael Chan79381092005-04-21 17:13:59 -07009769 add_timer(&tp->timer);
Joe Perches63c3a662011-04-26 08:12:10 +00009770 tg3_flag_set(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009771 tg3_enable_ints(tp);
9772
David S. Millerf47c11e2005-06-24 20:18:35 -07009773 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009774
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009775 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009776
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00009777 /*
9778 * Reset loopback feature if it was turned on while the device was down
9779 * make sure that it's installed properly now.
9780 */
9781 if (dev->features & NETIF_F_LOOPBACK)
9782 tg3_set_loopback(dev, dev->features);
9783
Linus Torvalds1da177e2005-04-16 15:20:36 -07009784 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +00009785
Matt Carlson679563f2009-09-01 12:55:46 +00009786err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +00009787 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9788 struct tg3_napi *tnapi = &tp->napi[i];
9789 free_irq(tnapi->irq_vec, tnapi);
9790 }
Matt Carlson07b01732009-08-28 14:01:15 +00009791
Matt Carlson679563f2009-09-01 12:55:46 +00009792err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +00009793 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009794 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00009795 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +00009796
9797err_out1:
9798 tg3_ints_fini(tp);
Matt Carlsoncd0d7222011-07-13 09:27:33 +00009799 tg3_frob_aux_power(tp, false);
9800 pci_set_power_state(tp->pdev, PCI_D3hot);
Matt Carlson07b01732009-08-28 14:01:15 +00009801 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009802}
9803
Linus Torvalds1da177e2005-04-16 15:20:36 -07009804static int tg3_close(struct net_device *dev)
9805{
Matt Carlson4f125f42009-09-01 12:55:02 +00009806 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009807 struct tg3 *tp = netdev_priv(dev);
9808
Matt Carlsonfed97812009-09-01 13:10:19 +00009809 tg3_napi_disable(tp);
Matt Carlsondb219972011-11-04 09:15:03 +00009810 tg3_reset_task_cancel(tp);
Michael Chan7faa0062006-02-02 17:29:28 -08009811
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009812 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009813
9814 del_timer_sync(&tp->timer);
9815
Matt Carlson24bb4fb2009-10-05 17:55:29 +00009816 tg3_phy_stop(tp);
9817
David S. Millerf47c11e2005-06-24 20:18:35 -07009818 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009819
9820 tg3_disable_ints(tp);
9821
Michael Chan944d9802005-05-29 14:57:48 -07009822 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009823 tg3_free_rings(tp);
Joe Perches63c3a662011-04-26 08:12:10 +00009824 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009825
David S. Millerf47c11e2005-06-24 20:18:35 -07009826 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009827
Matt Carlson4f125f42009-09-01 12:55:02 +00009828 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9829 struct tg3_napi *tnapi = &tp->napi[i];
9830 free_irq(tnapi->irq_vec, tnapi);
9831 }
Matt Carlson07b01732009-08-28 14:01:15 +00009832
9833 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009834
Matt Carlson92feeab2011-12-08 14:40:14 +00009835 /* Clear stats across close / open calls */
9836 memset(&tp->net_stats_prev, 0, sizeof(tp->net_stats_prev));
9837 memset(&tp->estats_prev, 0, sizeof(tp->estats_prev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07009838
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009839 tg3_napi_fini(tp);
9840
Linus Torvalds1da177e2005-04-16 15:20:36 -07009841 tg3_free_consistent(tp);
9842
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009843 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -08009844
9845 netif_carrier_off(tp->dev);
9846
Linus Torvalds1da177e2005-04-16 15:20:36 -07009847 return 0;
9848}
9849
Eric Dumazet511d2222010-07-07 20:44:24 +00009850static inline u64 get_stat64(tg3_stat64_t *val)
Stefan Buehler816f8b82008-08-15 14:10:54 -07009851{
9852 return ((u64)val->high << 32) | ((u64)val->low);
9853}
9854
Eric Dumazet511d2222010-07-07 20:44:24 +00009855static u64 calc_crc_errors(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009856{
9857 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9858
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009859 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009860 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
9861 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009862 u32 val;
9863
David S. Millerf47c11e2005-06-24 20:18:35 -07009864 spin_lock_bh(&tp->lock);
Michael Chan569a5df2007-02-13 12:18:15 -08009865 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
9866 tg3_writephy(tp, MII_TG3_TEST1,
9867 val | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009868 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009869 } else
9870 val = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07009871 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009872
9873 tp->phy_crc_errors += val;
9874
9875 return tp->phy_crc_errors;
9876 }
9877
9878 return get_stat64(&hw_stats->rx_fcs_errors);
9879}
9880
9881#define ESTAT_ADD(member) \
9882 estats->member = old_estats->member + \
Eric Dumazet511d2222010-07-07 20:44:24 +00009883 get_stat64(&hw_stats->member)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009884
Matt Carlson0e6c9da2011-12-08 14:40:13 +00009885static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *tp,
9886 struct tg3_ethtool_stats *estats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009887{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009888 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
9889 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9890
9891 if (!hw_stats)
9892 return old_estats;
9893
9894 ESTAT_ADD(rx_octets);
9895 ESTAT_ADD(rx_fragments);
9896 ESTAT_ADD(rx_ucast_packets);
9897 ESTAT_ADD(rx_mcast_packets);
9898 ESTAT_ADD(rx_bcast_packets);
9899 ESTAT_ADD(rx_fcs_errors);
9900 ESTAT_ADD(rx_align_errors);
9901 ESTAT_ADD(rx_xon_pause_rcvd);
9902 ESTAT_ADD(rx_xoff_pause_rcvd);
9903 ESTAT_ADD(rx_mac_ctrl_rcvd);
9904 ESTAT_ADD(rx_xoff_entered);
9905 ESTAT_ADD(rx_frame_too_long_errors);
9906 ESTAT_ADD(rx_jabbers);
9907 ESTAT_ADD(rx_undersize_packets);
9908 ESTAT_ADD(rx_in_length_errors);
9909 ESTAT_ADD(rx_out_length_errors);
9910 ESTAT_ADD(rx_64_or_less_octet_packets);
9911 ESTAT_ADD(rx_65_to_127_octet_packets);
9912 ESTAT_ADD(rx_128_to_255_octet_packets);
9913 ESTAT_ADD(rx_256_to_511_octet_packets);
9914 ESTAT_ADD(rx_512_to_1023_octet_packets);
9915 ESTAT_ADD(rx_1024_to_1522_octet_packets);
9916 ESTAT_ADD(rx_1523_to_2047_octet_packets);
9917 ESTAT_ADD(rx_2048_to_4095_octet_packets);
9918 ESTAT_ADD(rx_4096_to_8191_octet_packets);
9919 ESTAT_ADD(rx_8192_to_9022_octet_packets);
9920
9921 ESTAT_ADD(tx_octets);
9922 ESTAT_ADD(tx_collisions);
9923 ESTAT_ADD(tx_xon_sent);
9924 ESTAT_ADD(tx_xoff_sent);
9925 ESTAT_ADD(tx_flow_control);
9926 ESTAT_ADD(tx_mac_errors);
9927 ESTAT_ADD(tx_single_collisions);
9928 ESTAT_ADD(tx_mult_collisions);
9929 ESTAT_ADD(tx_deferred);
9930 ESTAT_ADD(tx_excessive_collisions);
9931 ESTAT_ADD(tx_late_collisions);
9932 ESTAT_ADD(tx_collide_2times);
9933 ESTAT_ADD(tx_collide_3times);
9934 ESTAT_ADD(tx_collide_4times);
9935 ESTAT_ADD(tx_collide_5times);
9936 ESTAT_ADD(tx_collide_6times);
9937 ESTAT_ADD(tx_collide_7times);
9938 ESTAT_ADD(tx_collide_8times);
9939 ESTAT_ADD(tx_collide_9times);
9940 ESTAT_ADD(tx_collide_10times);
9941 ESTAT_ADD(tx_collide_11times);
9942 ESTAT_ADD(tx_collide_12times);
9943 ESTAT_ADD(tx_collide_13times);
9944 ESTAT_ADD(tx_collide_14times);
9945 ESTAT_ADD(tx_collide_15times);
9946 ESTAT_ADD(tx_ucast_packets);
9947 ESTAT_ADD(tx_mcast_packets);
9948 ESTAT_ADD(tx_bcast_packets);
9949 ESTAT_ADD(tx_carrier_sense_errors);
9950 ESTAT_ADD(tx_discards);
9951 ESTAT_ADD(tx_errors);
9952
9953 ESTAT_ADD(dma_writeq_full);
9954 ESTAT_ADD(dma_write_prioq_full);
9955 ESTAT_ADD(rxbds_empty);
9956 ESTAT_ADD(rx_discards);
9957 ESTAT_ADD(rx_errors);
9958 ESTAT_ADD(rx_threshold_hit);
9959
9960 ESTAT_ADD(dma_readq_full);
9961 ESTAT_ADD(dma_read_prioq_full);
9962 ESTAT_ADD(tx_comp_queue_full);
9963
9964 ESTAT_ADD(ring_set_send_prod_index);
9965 ESTAT_ADD(ring_status_update);
9966 ESTAT_ADD(nic_irqs);
9967 ESTAT_ADD(nic_avoided_irqs);
9968 ESTAT_ADD(nic_tx_threshold_hit);
9969
Matt Carlson4452d092011-05-19 12:12:51 +00009970 ESTAT_ADD(mbuf_lwm_thresh_hit);
9971
Linus Torvalds1da177e2005-04-16 15:20:36 -07009972 return estats;
9973}
9974
Eric Dumazet511d2222010-07-07 20:44:24 +00009975static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
9976 struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009977{
9978 struct tg3 *tp = netdev_priv(dev);
Eric Dumazet511d2222010-07-07 20:44:24 +00009979 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009980 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9981
9982 if (!hw_stats)
9983 return old_stats;
9984
9985 stats->rx_packets = old_stats->rx_packets +
9986 get_stat64(&hw_stats->rx_ucast_packets) +
9987 get_stat64(&hw_stats->rx_mcast_packets) +
9988 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009989
Linus Torvalds1da177e2005-04-16 15:20:36 -07009990 stats->tx_packets = old_stats->tx_packets +
9991 get_stat64(&hw_stats->tx_ucast_packets) +
9992 get_stat64(&hw_stats->tx_mcast_packets) +
9993 get_stat64(&hw_stats->tx_bcast_packets);
9994
9995 stats->rx_bytes = old_stats->rx_bytes +
9996 get_stat64(&hw_stats->rx_octets);
9997 stats->tx_bytes = old_stats->tx_bytes +
9998 get_stat64(&hw_stats->tx_octets);
9999
10000 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -070010001 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010002 stats->tx_errors = old_stats->tx_errors +
10003 get_stat64(&hw_stats->tx_errors) +
10004 get_stat64(&hw_stats->tx_mac_errors) +
10005 get_stat64(&hw_stats->tx_carrier_sense_errors) +
10006 get_stat64(&hw_stats->tx_discards);
10007
10008 stats->multicast = old_stats->multicast +
10009 get_stat64(&hw_stats->rx_mcast_packets);
10010 stats->collisions = old_stats->collisions +
10011 get_stat64(&hw_stats->tx_collisions);
10012
10013 stats->rx_length_errors = old_stats->rx_length_errors +
10014 get_stat64(&hw_stats->rx_frame_too_long_errors) +
10015 get_stat64(&hw_stats->rx_undersize_packets);
10016
10017 stats->rx_over_errors = old_stats->rx_over_errors +
10018 get_stat64(&hw_stats->rxbds_empty);
10019 stats->rx_frame_errors = old_stats->rx_frame_errors +
10020 get_stat64(&hw_stats->rx_align_errors);
10021 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
10022 get_stat64(&hw_stats->tx_discards);
10023 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
10024 get_stat64(&hw_stats->tx_carrier_sense_errors);
10025
10026 stats->rx_crc_errors = old_stats->rx_crc_errors +
10027 calc_crc_errors(tp);
10028
John W. Linville4f63b872005-09-12 14:43:18 -070010029 stats->rx_missed_errors = old_stats->rx_missed_errors +
10030 get_stat64(&hw_stats->rx_discards);
10031
Eric Dumazetb0057c52010-10-10 19:55:52 +000010032 stats->rx_dropped = tp->rx_dropped;
Eric Dumazet48855432011-10-24 07:53:03 +000010033 stats->tx_dropped = tp->tx_dropped;
Eric Dumazetb0057c52010-10-10 19:55:52 +000010034
Linus Torvalds1da177e2005-04-16 15:20:36 -070010035 return stats;
10036}
10037
10038static inline u32 calc_crc(unsigned char *buf, int len)
10039{
10040 u32 reg;
10041 u32 tmp;
10042 int j, k;
10043
10044 reg = 0xffffffff;
10045
10046 for (j = 0; j < len; j++) {
10047 reg ^= buf[j];
10048
10049 for (k = 0; k < 8; k++) {
10050 tmp = reg & 0x01;
10051
10052 reg >>= 1;
10053
Matt Carlson859a588792010-04-05 10:19:28 +000010054 if (tmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010055 reg ^= 0xedb88320;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010056 }
10057 }
10058
10059 return ~reg;
10060}
10061
10062static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
10063{
10064 /* accept or reject all multicast frames */
10065 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
10066 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
10067 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
10068 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
10069}
10070
10071static void __tg3_set_rx_mode(struct net_device *dev)
10072{
10073 struct tg3 *tp = netdev_priv(dev);
10074 u32 rx_mode;
10075
10076 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
10077 RX_MODE_KEEP_VLAN_TAG);
10078
Matt Carlsonbf933c82011-01-25 15:58:49 +000010079#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010080 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
10081 * flag clear.
10082 */
Joe Perches63c3a662011-04-26 08:12:10 +000010083 if (!tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010084 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
10085#endif
10086
10087 if (dev->flags & IFF_PROMISC) {
10088 /* Promiscuous mode. */
10089 rx_mode |= RX_MODE_PROMISC;
10090 } else if (dev->flags & IFF_ALLMULTI) {
10091 /* Accept all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010092 tg3_set_multi(tp, 1);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +000010093 } else if (netdev_mc_empty(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010094 /* Reject all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010095 tg3_set_multi(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010096 } else {
10097 /* Accept one or more multicast(s). */
Jiri Pirko22bedad32010-04-01 21:22:57 +000010098 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010099 u32 mc_filter[4] = { 0, };
10100 u32 regidx;
10101 u32 bit;
10102 u32 crc;
10103
Jiri Pirko22bedad32010-04-01 21:22:57 +000010104 netdev_for_each_mc_addr(ha, dev) {
10105 crc = calc_crc(ha->addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010106 bit = ~crc & 0x7f;
10107 regidx = (bit & 0x60) >> 5;
10108 bit &= 0x1f;
10109 mc_filter[regidx] |= (1 << bit);
10110 }
10111
10112 tw32(MAC_HASH_REG_0, mc_filter[0]);
10113 tw32(MAC_HASH_REG_1, mc_filter[1]);
10114 tw32(MAC_HASH_REG_2, mc_filter[2]);
10115 tw32(MAC_HASH_REG_3, mc_filter[3]);
10116 }
10117
10118 if (rx_mode != tp->rx_mode) {
10119 tp->rx_mode = rx_mode;
10120 tw32_f(MAC_RX_MODE, rx_mode);
10121 udelay(10);
10122 }
10123}
10124
10125static void tg3_set_rx_mode(struct net_device *dev)
10126{
10127 struct tg3 *tp = netdev_priv(dev);
10128
Michael Chane75f7c92006-03-20 21:33:26 -080010129 if (!netif_running(dev))
10130 return;
10131
David S. Millerf47c11e2005-06-24 20:18:35 -070010132 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010133 __tg3_set_rx_mode(dev);
David S. Millerf47c11e2005-06-24 20:18:35 -070010134 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010135}
10136
Linus Torvalds1da177e2005-04-16 15:20:36 -070010137static int tg3_get_regs_len(struct net_device *dev)
10138{
Matt Carlson97bd8e42011-04-13 11:05:04 +000010139 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010140}
10141
10142static void tg3_get_regs(struct net_device *dev,
10143 struct ethtool_regs *regs, void *_p)
10144{
Linus Torvalds1da177e2005-04-16 15:20:36 -070010145 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010146
10147 regs->version = 0;
10148
Matt Carlson97bd8e42011-04-13 11:05:04 +000010149 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010150
Matt Carlson80096062010-08-02 11:26:06 +000010151 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010152 return;
10153
David S. Millerf47c11e2005-06-24 20:18:35 -070010154 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010155
Matt Carlson97bd8e42011-04-13 11:05:04 +000010156 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010157
David S. Millerf47c11e2005-06-24 20:18:35 -070010158 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010159}
10160
10161static int tg3_get_eeprom_len(struct net_device *dev)
10162{
10163 struct tg3 *tp = netdev_priv(dev);
10164
10165 return tp->nvram_size;
10166}
10167
Linus Torvalds1da177e2005-04-16 15:20:36 -070010168static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
10169{
10170 struct tg3 *tp = netdev_priv(dev);
10171 int ret;
10172 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -080010173 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010174 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010175
Joe Perches63c3a662011-04-26 08:12:10 +000010176 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000010177 return -EINVAL;
10178
Matt Carlson80096062010-08-02 11:26:06 +000010179 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010180 return -EAGAIN;
10181
Linus Torvalds1da177e2005-04-16 15:20:36 -070010182 offset = eeprom->offset;
10183 len = eeprom->len;
10184 eeprom->len = 0;
10185
10186 eeprom->magic = TG3_EEPROM_MAGIC;
10187
10188 if (offset & 3) {
10189 /* adjustments to start on required 4 byte boundary */
10190 b_offset = offset & 3;
10191 b_count = 4 - b_offset;
10192 if (b_count > len) {
10193 /* i.e. offset=1 len=2 */
10194 b_count = len;
10195 }
Matt Carlsona9dc5292009-02-25 14:25:30 +000010196 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010197 if (ret)
10198 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +000010199 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010200 len -= b_count;
10201 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010202 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010203 }
10204
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010205 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010206 pd = &data[eeprom->len];
10207 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010208 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010209 if (ret) {
10210 eeprom->len += i;
10211 return ret;
10212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010213 memcpy(pd + i, &val, 4);
10214 }
10215 eeprom->len += i;
10216
10217 if (len & 3) {
10218 /* read last bytes not ending on 4 byte boundary */
10219 pd = &data[eeprom->len];
10220 b_count = len & 3;
10221 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010222 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010223 if (ret)
10224 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080010225 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010226 eeprom->len += b_count;
10227 }
10228 return 0;
10229}
10230
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010231static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010232
10233static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
10234{
10235 struct tg3 *tp = netdev_priv(dev);
10236 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080010237 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010238 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010239 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010240
Matt Carlson80096062010-08-02 11:26:06 +000010241 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010242 return -EAGAIN;
10243
Joe Perches63c3a662011-04-26 08:12:10 +000010244 if (tg3_flag(tp, NO_NVRAM) ||
Matt Carlsondf259d82009-04-20 06:57:14 +000010245 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010246 return -EINVAL;
10247
10248 offset = eeprom->offset;
10249 len = eeprom->len;
10250
10251 if ((b_offset = (offset & 3))) {
10252 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010253 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010254 if (ret)
10255 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010256 len += b_offset;
10257 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -070010258 if (len < 4)
10259 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010260 }
10261
10262 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -070010263 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010264 /* adjustments to end on required 4 byte boundary */
10265 odd_len = 1;
10266 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010267 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010268 if (ret)
10269 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010270 }
10271
10272 buf = data;
10273 if (b_offset || odd_len) {
10274 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010275 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010276 return -ENOMEM;
10277 if (b_offset)
10278 memcpy(buf, &start, 4);
10279 if (odd_len)
10280 memcpy(buf+len-4, &end, 4);
10281 memcpy(buf + b_offset, data, eeprom->len);
10282 }
10283
10284 ret = tg3_nvram_write_block(tp, offset, len, buf);
10285
10286 if (buf != data)
10287 kfree(buf);
10288
10289 return ret;
10290}
10291
10292static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
10293{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010294 struct tg3 *tp = netdev_priv(dev);
10295
Joe Perches63c3a662011-04-26 08:12:10 +000010296 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010297 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010298 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010299 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010300 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10301 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010302 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010303
Linus Torvalds1da177e2005-04-16 15:20:36 -070010304 cmd->supported = (SUPPORTED_Autoneg);
10305
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010306 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010307 cmd->supported |= (SUPPORTED_1000baseT_Half |
10308 SUPPORTED_1000baseT_Full);
10309
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010310 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010311 cmd->supported |= (SUPPORTED_100baseT_Half |
10312 SUPPORTED_100baseT_Full |
10313 SUPPORTED_10baseT_Half |
10314 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -080010315 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -070010316 cmd->port = PORT_TP;
10317 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010318 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -070010319 cmd->port = PORT_FIBRE;
10320 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010321
Linus Torvalds1da177e2005-04-16 15:20:36 -070010322 cmd->advertising = tp->link_config.advertising;
Matt Carlson5bb09772011-06-13 13:39:00 +000010323 if (tg3_flag(tp, PAUSE_AUTONEG)) {
10324 if (tp->link_config.flowctrl & FLOW_CTRL_RX) {
10325 if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
10326 cmd->advertising |= ADVERTISED_Pause;
10327 } else {
10328 cmd->advertising |= ADVERTISED_Pause |
10329 ADVERTISED_Asym_Pause;
10330 }
10331 } else if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
10332 cmd->advertising |= ADVERTISED_Asym_Pause;
10333 }
10334 }
Matt Carlson859edb22011-12-08 14:40:16 +000010335 if (netif_running(dev) && netif_carrier_ok(dev)) {
David Decotigny70739492011-04-27 18:32:40 +000010336 ethtool_cmd_speed_set(cmd, tp->link_config.active_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010337 cmd->duplex = tp->link_config.active_duplex;
Matt Carlson859edb22011-12-08 14:40:16 +000010338 cmd->lp_advertising = tp->link_config.rmt_adv;
Matt Carlsone348c5e2011-11-21 15:01:20 +000010339 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
10340 if (tp->phy_flags & TG3_PHYFLG_MDIX_STATE)
10341 cmd->eth_tp_mdix = ETH_TP_MDI_X;
10342 else
10343 cmd->eth_tp_mdix = ETH_TP_MDI;
10344 }
Matt Carlson64c22182010-10-14 10:37:44 +000010345 } else {
David Decotigny70739492011-04-27 18:32:40 +000010346 ethtool_cmd_speed_set(cmd, SPEED_INVALID);
Matt Carlson64c22182010-10-14 10:37:44 +000010347 cmd->duplex = DUPLEX_INVALID;
Matt Carlsone348c5e2011-11-21 15:01:20 +000010348 cmd->eth_tp_mdix = ETH_TP_MDI_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010349 }
Matt Carlson882e9792009-09-01 13:21:36 +000010350 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +000010351 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010352 cmd->autoneg = tp->link_config.autoneg;
10353 cmd->maxtxpkt = 0;
10354 cmd->maxrxpkt = 0;
10355 return 0;
10356}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010357
Linus Torvalds1da177e2005-04-16 15:20:36 -070010358static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
10359{
10360 struct tg3 *tp = netdev_priv(dev);
David Decotigny25db0332011-04-27 18:32:39 +000010361 u32 speed = ethtool_cmd_speed(cmd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010362
Joe Perches63c3a662011-04-26 08:12:10 +000010363 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010364 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010365 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010366 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010367 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10368 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010369 }
10370
Matt Carlson7e5856b2009-02-25 14:23:01 +000010371 if (cmd->autoneg != AUTONEG_ENABLE &&
10372 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -070010373 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +000010374
10375 if (cmd->autoneg == AUTONEG_DISABLE &&
10376 cmd->duplex != DUPLEX_FULL &&
10377 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -070010378 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010379
Matt Carlson7e5856b2009-02-25 14:23:01 +000010380 if (cmd->autoneg == AUTONEG_ENABLE) {
10381 u32 mask = ADVERTISED_Autoneg |
10382 ADVERTISED_Pause |
10383 ADVERTISED_Asym_Pause;
10384
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010385 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010386 mask |= ADVERTISED_1000baseT_Half |
10387 ADVERTISED_1000baseT_Full;
10388
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010389 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010390 mask |= ADVERTISED_100baseT_Half |
10391 ADVERTISED_100baseT_Full |
10392 ADVERTISED_10baseT_Half |
10393 ADVERTISED_10baseT_Full |
10394 ADVERTISED_TP;
10395 else
10396 mask |= ADVERTISED_FIBRE;
10397
10398 if (cmd->advertising & ~mask)
10399 return -EINVAL;
10400
10401 mask &= (ADVERTISED_1000baseT_Half |
10402 ADVERTISED_1000baseT_Full |
10403 ADVERTISED_100baseT_Half |
10404 ADVERTISED_100baseT_Full |
10405 ADVERTISED_10baseT_Half |
10406 ADVERTISED_10baseT_Full);
10407
10408 cmd->advertising &= mask;
10409 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010410 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
David Decotigny25db0332011-04-27 18:32:39 +000010411 if (speed != SPEED_1000)
Matt Carlson7e5856b2009-02-25 14:23:01 +000010412 return -EINVAL;
10413
10414 if (cmd->duplex != DUPLEX_FULL)
10415 return -EINVAL;
10416 } else {
David Decotigny25db0332011-04-27 18:32:39 +000010417 if (speed != SPEED_100 &&
10418 speed != SPEED_10)
Matt Carlson7e5856b2009-02-25 14:23:01 +000010419 return -EINVAL;
10420 }
10421 }
10422
David S. Millerf47c11e2005-06-24 20:18:35 -070010423 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010424
10425 tp->link_config.autoneg = cmd->autoneg;
10426 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -070010427 tp->link_config.advertising = (cmd->advertising |
10428 ADVERTISED_Autoneg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010429 tp->link_config.speed = SPEED_INVALID;
10430 tp->link_config.duplex = DUPLEX_INVALID;
10431 } else {
10432 tp->link_config.advertising = 0;
David Decotigny25db0332011-04-27 18:32:39 +000010433 tp->link_config.speed = speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010434 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010435 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010436
Michael Chan24fcad62006-12-17 17:06:46 -080010437 tp->link_config.orig_speed = tp->link_config.speed;
10438 tp->link_config.orig_duplex = tp->link_config.duplex;
10439 tp->link_config.orig_autoneg = tp->link_config.autoneg;
10440
Linus Torvalds1da177e2005-04-16 15:20:36 -070010441 if (netif_running(dev))
10442 tg3_setup_phy(tp, 1);
10443
David S. Millerf47c11e2005-06-24 20:18:35 -070010444 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010445
Linus Torvalds1da177e2005-04-16 15:20:36 -070010446 return 0;
10447}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010448
Linus Torvalds1da177e2005-04-16 15:20:36 -070010449static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
10450{
10451 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010452
Rick Jones68aad782011-11-07 13:29:27 +000010453 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
10454 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
10455 strlcpy(info->fw_version, tp->fw_ver, sizeof(info->fw_version));
10456 strlcpy(info->bus_info, pci_name(tp->pdev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010457}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010458
Linus Torvalds1da177e2005-04-16 15:20:36 -070010459static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10460{
10461 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010462
Joe Perches63c3a662011-04-26 08:12:10 +000010463 if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070010464 wol->supported = WAKE_MAGIC;
10465 else
10466 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010467 wol->wolopts = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010468 if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010469 wol->wolopts = WAKE_MAGIC;
10470 memset(&wol->sopass, 0, sizeof(wol->sopass));
10471}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010472
Linus Torvalds1da177e2005-04-16 15:20:36 -070010473static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10474{
10475 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070010476 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010477
Linus Torvalds1da177e2005-04-16 15:20:36 -070010478 if (wol->wolopts & ~WAKE_MAGIC)
10479 return -EINVAL;
10480 if ((wol->wolopts & WAKE_MAGIC) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010481 !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010482 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010483
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010484 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
10485
David S. Millerf47c11e2005-06-24 20:18:35 -070010486 spin_lock_bh(&tp->lock);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010487 if (device_may_wakeup(dp))
Joe Perches63c3a662011-04-26 08:12:10 +000010488 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010489 else
Joe Perches63c3a662011-04-26 08:12:10 +000010490 tg3_flag_clear(tp, WOL_ENABLE);
David S. Millerf47c11e2005-06-24 20:18:35 -070010491 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010492
Linus Torvalds1da177e2005-04-16 15:20:36 -070010493 return 0;
10494}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010495
Linus Torvalds1da177e2005-04-16 15:20:36 -070010496static u32 tg3_get_msglevel(struct net_device *dev)
10497{
10498 struct tg3 *tp = netdev_priv(dev);
10499 return tp->msg_enable;
10500}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010501
Linus Torvalds1da177e2005-04-16 15:20:36 -070010502static void tg3_set_msglevel(struct net_device *dev, u32 value)
10503{
10504 struct tg3 *tp = netdev_priv(dev);
10505 tp->msg_enable = value;
10506}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010507
Linus Torvalds1da177e2005-04-16 15:20:36 -070010508static int tg3_nway_reset(struct net_device *dev)
10509{
10510 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010511 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010512
Linus Torvalds1da177e2005-04-16 15:20:36 -070010513 if (!netif_running(dev))
10514 return -EAGAIN;
10515
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010516 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070010517 return -EINVAL;
10518
Joe Perches63c3a662011-04-26 08:12:10 +000010519 if (tg3_flag(tp, USE_PHYLIB)) {
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 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010523 } else {
10524 u32 bmcr;
10525
10526 spin_lock_bh(&tp->lock);
10527 r = -EINVAL;
10528 tg3_readphy(tp, MII_BMCR, &bmcr);
10529 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
10530 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010531 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010532 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
10533 BMCR_ANENABLE);
10534 r = 0;
10535 }
10536 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010537 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010538
Linus Torvalds1da177e2005-04-16 15:20:36 -070010539 return r;
10540}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010541
Linus Torvalds1da177e2005-04-16 15:20:36 -070010542static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10543{
10544 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010545
Matt Carlson2c49a442010-09-30 10:34:35 +000010546 ering->rx_max_pending = tp->rx_std_ring_mask;
Joe Perches63c3a662011-04-26 08:12:10 +000010547 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson2c49a442010-09-30 10:34:35 +000010548 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080010549 else
10550 ering->rx_jumbo_max_pending = 0;
10551
10552 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010553
10554 ering->rx_pending = tp->rx_pending;
Joe Perches63c3a662011-04-26 08:12:10 +000010555 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Michael Chan4f81c322006-03-20 21:33:42 -080010556 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
10557 else
10558 ering->rx_jumbo_pending = 0;
10559
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010560 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010561}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010562
Linus Torvalds1da177e2005-04-16 15:20:36 -070010563static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10564{
10565 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000010566 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010567
Matt Carlson2c49a442010-09-30 10:34:35 +000010568 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
10569 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070010570 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
10571 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Joe Perches63c3a662011-04-26 08:12:10 +000010572 (tg3_flag(tp, TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070010573 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010574 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010575
Michael Chanbbe832c2005-06-24 20:20:04 -070010576 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010577 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010578 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010579 irq_sync = 1;
10580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010581
Michael Chanbbe832c2005-06-24 20:20:04 -070010582 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010583
Linus Torvalds1da177e2005-04-16 15:20:36 -070010584 tp->rx_pending = ering->rx_pending;
10585
Joe Perches63c3a662011-04-26 08:12:10 +000010586 if (tg3_flag(tp, MAX_RXPEND_64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010587 tp->rx_pending > 63)
10588 tp->rx_pending = 63;
10589 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000010590
Matt Carlson6fd45cb2010-09-15 08:59:57 +000010591 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000010592 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010593
10594 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070010595 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -070010596 err = tg3_restart_hw(tp, 1);
10597 if (!err)
10598 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010599 }
10600
David S. Millerf47c11e2005-06-24 20:18:35 -070010601 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010602
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010603 if (irq_sync && !err)
10604 tg3_phy_start(tp);
10605
Michael Chanb9ec6c12006-07-25 16:37:27 -070010606 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010607}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010608
Linus Torvalds1da177e2005-04-16 15:20:36 -070010609static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10610{
10611 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010612
Joe Perches63c3a662011-04-26 08:12:10 +000010613 epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
Matt Carlson8d018622007-12-20 20:05:44 -080010614
Matt Carlson4a2db502011-12-08 14:40:17 +000010615 if (tp->link_config.flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080010616 epause->rx_pause = 1;
10617 else
10618 epause->rx_pause = 0;
10619
Matt Carlson4a2db502011-12-08 14:40:17 +000010620 if (tp->link_config.flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080010621 epause->tx_pause = 1;
10622 else
10623 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010624}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010625
Linus Torvalds1da177e2005-04-16 15:20:36 -070010626static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10627{
10628 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010629 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010630
Joe Perches63c3a662011-04-26 08:12:10 +000010631 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson27121682010-02-17 15:16:57 +000010632 u32 newadv;
10633 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010634
Matt Carlson27121682010-02-17 15:16:57 +000010635 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010636
Matt Carlson27121682010-02-17 15:16:57 +000010637 if (!(phydev->supported & SUPPORTED_Pause) ||
10638 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000010639 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000010640 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010641
Matt Carlson27121682010-02-17 15:16:57 +000010642 tp->link_config.flowctrl = 0;
10643 if (epause->rx_pause) {
10644 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010645
Matt Carlson27121682010-02-17 15:16:57 +000010646 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080010647 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000010648 newadv = ADVERTISED_Pause;
10649 } else
10650 newadv = ADVERTISED_Pause |
10651 ADVERTISED_Asym_Pause;
10652 } else if (epause->tx_pause) {
10653 tp->link_config.flowctrl |= FLOW_CTRL_TX;
10654 newadv = ADVERTISED_Asym_Pause;
10655 } else
10656 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010657
Matt Carlson27121682010-02-17 15:16:57 +000010658 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010659 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010660 else
Joe Perches63c3a662011-04-26 08:12:10 +000010661 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010662
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010663 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000010664 u32 oldadv = phydev->advertising &
10665 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
10666 if (oldadv != newadv) {
10667 phydev->advertising &=
10668 ~(ADVERTISED_Pause |
10669 ADVERTISED_Asym_Pause);
10670 phydev->advertising |= newadv;
10671 if (phydev->autoneg) {
10672 /*
10673 * Always renegotiate the link to
10674 * inform our link partner of our
10675 * flow control settings, even if the
10676 * flow control is forced. Let
10677 * tg3_adjust_link() do the final
10678 * flow control setup.
10679 */
10680 return phy_start_aneg(phydev);
10681 }
10682 }
10683
10684 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010685 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000010686 } else {
10687 tp->link_config.orig_advertising &=
10688 ~(ADVERTISED_Pause |
10689 ADVERTISED_Asym_Pause);
10690 tp->link_config.orig_advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010691 }
10692 } else {
10693 int irq_sync = 0;
10694
10695 if (netif_running(dev)) {
10696 tg3_netif_stop(tp);
10697 irq_sync = 1;
10698 }
10699
10700 tg3_full_lock(tp, irq_sync);
10701
10702 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010703 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010704 else
Joe Perches63c3a662011-04-26 08:12:10 +000010705 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010706 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010707 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010708 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010709 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010710 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010711 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010712 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010713 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010714
10715 if (netif_running(dev)) {
10716 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10717 err = tg3_restart_hw(tp, 1);
10718 if (!err)
10719 tg3_netif_start(tp);
10720 }
10721
10722 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010724
Michael Chanb9ec6c12006-07-25 16:37:27 -070010725 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010726}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010727
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010728static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010729{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010730 switch (sset) {
10731 case ETH_SS_TEST:
10732 return TG3_NUM_TEST;
10733 case ETH_SS_STATS:
10734 return TG3_NUM_STATS;
10735 default:
10736 return -EOPNOTSUPP;
10737 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070010738}
10739
Matt Carlson90415472011-12-16 13:33:23 +000010740static int tg3_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
10741 u32 *rules __always_unused)
10742{
10743 struct tg3 *tp = netdev_priv(dev);
10744
10745 if (!tg3_flag(tp, SUPPORT_MSIX))
10746 return -EOPNOTSUPP;
10747
10748 switch (info->cmd) {
10749 case ETHTOOL_GRXRINGS:
10750 if (netif_running(tp->dev))
10751 info->data = tp->irq_cnt;
10752 else {
10753 info->data = num_online_cpus();
10754 if (info->data > TG3_IRQ_MAX_VECS_RSS)
10755 info->data = TG3_IRQ_MAX_VECS_RSS;
10756 }
10757
10758 /* The first interrupt vector only
10759 * handles link interrupts.
10760 */
10761 info->data -= 1;
10762 return 0;
10763
10764 default:
10765 return -EOPNOTSUPP;
10766 }
10767}
10768
10769static u32 tg3_get_rxfh_indir_size(struct net_device *dev)
10770{
10771 u32 size = 0;
10772 struct tg3 *tp = netdev_priv(dev);
10773
10774 if (tg3_flag(tp, SUPPORT_MSIX))
10775 size = TG3_RSS_INDIR_TBL_SIZE;
10776
10777 return size;
10778}
10779
10780static int tg3_get_rxfh_indir(struct net_device *dev, u32 *indir)
10781{
10782 struct tg3 *tp = netdev_priv(dev);
10783 int i;
10784
10785 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
10786 indir[i] = tp->rss_ind_tbl[i];
10787
10788 return 0;
10789}
10790
10791static int tg3_set_rxfh_indir(struct net_device *dev, const u32 *indir)
10792{
10793 struct tg3 *tp = netdev_priv(dev);
10794 size_t i;
10795
10796 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
10797 tp->rss_ind_tbl[i] = indir[i];
10798
10799 if (!netif_running(dev) || !tg3_flag(tp, ENABLE_RSS))
10800 return 0;
10801
10802 /* It is legal to write the indirection
10803 * table while the device is running.
10804 */
10805 tg3_full_lock(tp, 0);
10806 tg3_rss_write_indir_tbl(tp);
10807 tg3_full_unlock(tp);
10808
10809 return 0;
10810}
10811
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010812static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010813{
10814 switch (stringset) {
10815 case ETH_SS_STATS:
10816 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
10817 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070010818 case ETH_SS_TEST:
10819 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
10820 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010821 default:
10822 WARN_ON(1); /* we need a WARN() */
10823 break;
10824 }
10825}
10826
stephen hemminger81b87092011-04-04 08:43:50 +000010827static int tg3_set_phys_id(struct net_device *dev,
10828 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070010829{
10830 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070010831
10832 if (!netif_running(tp->dev))
10833 return -EAGAIN;
10834
stephen hemminger81b87092011-04-04 08:43:50 +000010835 switch (state) {
10836 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +000010837 return 1; /* cycle on/off once per second */
Michael Chan4009a932005-09-05 17:52:54 -070010838
stephen hemminger81b87092011-04-04 08:43:50 +000010839 case ETHTOOL_ID_ON:
10840 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10841 LED_CTRL_1000MBPS_ON |
10842 LED_CTRL_100MBPS_ON |
10843 LED_CTRL_10MBPS_ON |
10844 LED_CTRL_TRAFFIC_OVERRIDE |
10845 LED_CTRL_TRAFFIC_BLINK |
10846 LED_CTRL_TRAFFIC_LED);
10847 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010848
stephen hemminger81b87092011-04-04 08:43:50 +000010849 case ETHTOOL_ID_OFF:
10850 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10851 LED_CTRL_TRAFFIC_OVERRIDE);
10852 break;
Michael Chan4009a932005-09-05 17:52:54 -070010853
stephen hemminger81b87092011-04-04 08:43:50 +000010854 case ETHTOOL_ID_INACTIVE:
10855 tw32(MAC_LED_CTRL, tp->led_ctrl);
10856 break;
Michael Chan4009a932005-09-05 17:52:54 -070010857 }
stephen hemminger81b87092011-04-04 08:43:50 +000010858
Michael Chan4009a932005-09-05 17:52:54 -070010859 return 0;
10860}
10861
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010862static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010863 struct ethtool_stats *estats, u64 *tmp_stats)
10864{
10865 struct tg3 *tp = netdev_priv(dev);
Matt Carlson0e6c9da2011-12-08 14:40:13 +000010866
10867 tg3_get_estats(tp, (struct tg3_ethtool_stats *)tmp_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010868}
10869
Matt Carlson535a4902011-07-20 10:20:56 +000010870static __be32 *tg3_vpd_readblock(struct tg3 *tp, u32 *vpdlen)
Matt Carlsonc3e94502011-04-13 11:05:08 +000010871{
10872 int i;
10873 __be32 *buf;
10874 u32 offset = 0, len = 0;
10875 u32 magic, val;
10876
Joe Perches63c3a662011-04-26 08:12:10 +000010877 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
Matt Carlsonc3e94502011-04-13 11:05:08 +000010878 return NULL;
10879
10880 if (magic == TG3_EEPROM_MAGIC) {
10881 for (offset = TG3_NVM_DIR_START;
10882 offset < TG3_NVM_DIR_END;
10883 offset += TG3_NVM_DIRENT_SIZE) {
10884 if (tg3_nvram_read(tp, offset, &val))
10885 return NULL;
10886
10887 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
10888 TG3_NVM_DIRTYPE_EXTVPD)
10889 break;
10890 }
10891
10892 if (offset != TG3_NVM_DIR_END) {
10893 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
10894 if (tg3_nvram_read(tp, offset + 4, &offset))
10895 return NULL;
10896
10897 offset = tg3_nvram_logical_addr(tp, offset);
10898 }
10899 }
10900
10901 if (!offset || !len) {
10902 offset = TG3_NVM_VPD_OFF;
10903 len = TG3_NVM_VPD_LEN;
10904 }
10905
10906 buf = kmalloc(len, GFP_KERNEL);
10907 if (buf == NULL)
10908 return NULL;
10909
10910 if (magic == TG3_EEPROM_MAGIC) {
10911 for (i = 0; i < len; i += 4) {
10912 /* The data is in little-endian format in NVRAM.
10913 * Use the big-endian read routines to preserve
10914 * the byte order as it exists in NVRAM.
10915 */
10916 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
10917 goto error;
10918 }
10919 } else {
10920 u8 *ptr;
10921 ssize_t cnt;
10922 unsigned int pos = 0;
10923
10924 ptr = (u8 *)&buf[0];
10925 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
10926 cnt = pci_read_vpd(tp->pdev, pos,
10927 len - pos, ptr);
10928 if (cnt == -ETIMEDOUT || cnt == -EINTR)
10929 cnt = 0;
10930 else if (cnt < 0)
10931 goto error;
10932 }
10933 if (pos != len)
10934 goto error;
10935 }
10936
Matt Carlson535a4902011-07-20 10:20:56 +000010937 *vpdlen = len;
10938
Matt Carlsonc3e94502011-04-13 11:05:08 +000010939 return buf;
10940
10941error:
10942 kfree(buf);
10943 return NULL;
10944}
10945
Michael Chan566f86a2005-05-29 14:56:58 -070010946#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080010947#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
10948#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
10949#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Matt Carlson727a6d92011-06-13 13:38:58 +000010950#define NVRAM_SELFBOOT_FORMAT1_4_SIZE 0x20
10951#define NVRAM_SELFBOOT_FORMAT1_5_SIZE 0x24
Matt Carlsonbda18fa2011-07-20 10:20:57 +000010952#define NVRAM_SELFBOOT_FORMAT1_6_SIZE 0x50
Michael Chanb16250e2006-09-27 16:10:14 -070010953#define NVRAM_SELFBOOT_HW_SIZE 0x20
10954#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070010955
10956static int tg3_test_nvram(struct tg3 *tp)
10957{
Matt Carlson535a4902011-07-20 10:20:56 +000010958 u32 csum, magic, len;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010959 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010960 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070010961
Joe Perches63c3a662011-04-26 08:12:10 +000010962 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000010963 return 0;
10964
Matt Carlsone4f34112009-02-25 14:25:00 +000010965 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080010966 return -EIO;
10967
Michael Chan1b277772006-03-20 22:27:48 -080010968 if (magic == TG3_EEPROM_MAGIC)
10969 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070010970 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080010971 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
10972 TG3_EEPROM_SB_FORMAT_1) {
10973 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
10974 case TG3_EEPROM_SB_REVISION_0:
10975 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
10976 break;
10977 case TG3_EEPROM_SB_REVISION_2:
10978 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
10979 break;
10980 case TG3_EEPROM_SB_REVISION_3:
10981 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
10982 break;
Matt Carlson727a6d92011-06-13 13:38:58 +000010983 case TG3_EEPROM_SB_REVISION_4:
10984 size = NVRAM_SELFBOOT_FORMAT1_4_SIZE;
10985 break;
10986 case TG3_EEPROM_SB_REVISION_5:
10987 size = NVRAM_SELFBOOT_FORMAT1_5_SIZE;
10988 break;
10989 case TG3_EEPROM_SB_REVISION_6:
10990 size = NVRAM_SELFBOOT_FORMAT1_6_SIZE;
10991 break;
Matt Carlsona5767de2007-11-12 21:10:58 -080010992 default:
Matt Carlson727a6d92011-06-13 13:38:58 +000010993 return -EIO;
Matt Carlsona5767de2007-11-12 21:10:58 -080010994 }
10995 } else
Michael Chan1b277772006-03-20 22:27:48 -080010996 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070010997 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
10998 size = NVRAM_SELFBOOT_HW_SIZE;
10999 else
Michael Chan1b277772006-03-20 22:27:48 -080011000 return -EIO;
11001
11002 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070011003 if (buf == NULL)
11004 return -ENOMEM;
11005
Michael Chan1b277772006-03-20 22:27:48 -080011006 err = -EIO;
11007 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000011008 err = tg3_nvram_read_be32(tp, i, &buf[j]);
11009 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070011010 break;
Michael Chan566f86a2005-05-29 14:56:58 -070011011 }
Michael Chan1b277772006-03-20 22:27:48 -080011012 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070011013 goto out;
11014
Michael Chan1b277772006-03-20 22:27:48 -080011015 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000011016 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080011017 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070011018 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080011019 u8 *buf8 = (u8 *) buf, csum8 = 0;
11020
Al Virob9fc7dc2007-12-17 22:59:57 -080011021 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080011022 TG3_EEPROM_SB_REVISION_2) {
11023 /* For rev 2, the csum doesn't include the MBA. */
11024 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
11025 csum8 += buf8[i];
11026 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
11027 csum8 += buf8[i];
11028 } else {
11029 for (i = 0; i < size; i++)
11030 csum8 += buf8[i];
11031 }
Michael Chan1b277772006-03-20 22:27:48 -080011032
Adrian Bunkad96b482006-04-05 22:21:04 -070011033 if (csum8 == 0) {
11034 err = 0;
11035 goto out;
11036 }
11037
11038 err = -EIO;
11039 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080011040 }
Michael Chan566f86a2005-05-29 14:56:58 -070011041
Al Virob9fc7dc2007-12-17 22:59:57 -080011042 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070011043 TG3_EEPROM_MAGIC_HW) {
11044 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000011045 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070011046 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070011047
11048 /* Separate the parity bits and the data bytes. */
11049 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
11050 if ((i == 0) || (i == 8)) {
11051 int l;
11052 u8 msk;
11053
11054 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
11055 parity[k++] = buf8[i] & msk;
11056 i++;
Matt Carlson859a588792010-04-05 10:19:28 +000011057 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070011058 int l;
11059 u8 msk;
11060
11061 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
11062 parity[k++] = buf8[i] & msk;
11063 i++;
11064
11065 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
11066 parity[k++] = buf8[i] & msk;
11067 i++;
11068 }
11069 data[j++] = buf8[i];
11070 }
11071
11072 err = -EIO;
11073 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
11074 u8 hw8 = hweight8(data[i]);
11075
11076 if ((hw8 & 0x1) && parity[i])
11077 goto out;
11078 else if (!(hw8 & 0x1) && !parity[i])
11079 goto out;
11080 }
11081 err = 0;
11082 goto out;
11083 }
11084
Matt Carlson01c3a392011-03-09 16:58:20 +000011085 err = -EIO;
11086
Michael Chan566f86a2005-05-29 14:56:58 -070011087 /* Bootstrap checksum at offset 0x10 */
11088 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000011089 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070011090 goto out;
11091
11092 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
11093 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000011094 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000011095 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070011096
Matt Carlsonc3e94502011-04-13 11:05:08 +000011097 kfree(buf);
11098
Matt Carlson535a4902011-07-20 10:20:56 +000011099 buf = tg3_vpd_readblock(tp, &len);
Matt Carlsonc3e94502011-04-13 11:05:08 +000011100 if (!buf)
11101 return -ENOMEM;
Matt Carlsond4894f32011-03-09 16:58:21 +000011102
Matt Carlson535a4902011-07-20 10:20:56 +000011103 i = pci_vpd_find_tag((u8 *)buf, 0, len, PCI_VPD_LRDT_RO_DATA);
Matt Carlsond4894f32011-03-09 16:58:21 +000011104 if (i > 0) {
11105 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
11106 if (j < 0)
11107 goto out;
11108
Matt Carlson535a4902011-07-20 10:20:56 +000011109 if (i + PCI_VPD_LRDT_TAG_SIZE + j > len)
Matt Carlsond4894f32011-03-09 16:58:21 +000011110 goto out;
11111
11112 i += PCI_VPD_LRDT_TAG_SIZE;
11113 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
11114 PCI_VPD_RO_KEYWORD_CHKSUM);
11115 if (j > 0) {
11116 u8 csum8 = 0;
11117
11118 j += PCI_VPD_INFO_FLD_HDR_SIZE;
11119
11120 for (i = 0; i <= j; i++)
11121 csum8 += ((u8 *)buf)[i];
11122
11123 if (csum8)
11124 goto out;
11125 }
11126 }
11127
Michael Chan566f86a2005-05-29 14:56:58 -070011128 err = 0;
11129
11130out:
11131 kfree(buf);
11132 return err;
11133}
11134
Michael Chanca430072005-05-29 14:57:23 -070011135#define TG3_SERDES_TIMEOUT_SEC 2
11136#define TG3_COPPER_TIMEOUT_SEC 6
11137
11138static int tg3_test_link(struct tg3 *tp)
11139{
11140 int i, max;
11141
11142 if (!netif_running(tp->dev))
11143 return -ENODEV;
11144
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011145 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070011146 max = TG3_SERDES_TIMEOUT_SEC;
11147 else
11148 max = TG3_COPPER_TIMEOUT_SEC;
11149
11150 for (i = 0; i < max; i++) {
11151 if (netif_carrier_ok(tp->dev))
11152 return 0;
11153
11154 if (msleep_interruptible(1000))
11155 break;
11156 }
11157
11158 return -EIO;
11159}
11160
Michael Chana71116d2005-05-29 14:58:11 -070011161/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080011162static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070011163{
Michael Chanb16250e2006-09-27 16:10:14 -070011164 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070011165 u32 offset, read_mask, write_mask, val, save_val, read_val;
11166 static struct {
11167 u16 offset;
11168 u16 flags;
11169#define TG3_FL_5705 0x1
11170#define TG3_FL_NOT_5705 0x2
11171#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070011172#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070011173 u32 read_mask;
11174 u32 write_mask;
11175 } reg_tbl[] = {
11176 /* MAC Control Registers */
11177 { MAC_MODE, TG3_FL_NOT_5705,
11178 0x00000000, 0x00ef6f8c },
11179 { MAC_MODE, TG3_FL_5705,
11180 0x00000000, 0x01ef6b8c },
11181 { MAC_STATUS, TG3_FL_NOT_5705,
11182 0x03800107, 0x00000000 },
11183 { MAC_STATUS, TG3_FL_5705,
11184 0x03800100, 0x00000000 },
11185 { MAC_ADDR_0_HIGH, 0x0000,
11186 0x00000000, 0x0000ffff },
11187 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011188 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070011189 { MAC_RX_MTU_SIZE, 0x0000,
11190 0x00000000, 0x0000ffff },
11191 { MAC_TX_MODE, 0x0000,
11192 0x00000000, 0x00000070 },
11193 { MAC_TX_LENGTHS, 0x0000,
11194 0x00000000, 0x00003fff },
11195 { MAC_RX_MODE, TG3_FL_NOT_5705,
11196 0x00000000, 0x000007fc },
11197 { MAC_RX_MODE, TG3_FL_5705,
11198 0x00000000, 0x000007dc },
11199 { MAC_HASH_REG_0, 0x0000,
11200 0x00000000, 0xffffffff },
11201 { MAC_HASH_REG_1, 0x0000,
11202 0x00000000, 0xffffffff },
11203 { MAC_HASH_REG_2, 0x0000,
11204 0x00000000, 0xffffffff },
11205 { MAC_HASH_REG_3, 0x0000,
11206 0x00000000, 0xffffffff },
11207
11208 /* Receive Data and Receive BD Initiator Control Registers. */
11209 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
11210 0x00000000, 0xffffffff },
11211 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
11212 0x00000000, 0xffffffff },
11213 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
11214 0x00000000, 0x00000003 },
11215 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
11216 0x00000000, 0xffffffff },
11217 { RCVDBDI_STD_BD+0, 0x0000,
11218 0x00000000, 0xffffffff },
11219 { RCVDBDI_STD_BD+4, 0x0000,
11220 0x00000000, 0xffffffff },
11221 { RCVDBDI_STD_BD+8, 0x0000,
11222 0x00000000, 0xffff0002 },
11223 { RCVDBDI_STD_BD+0xc, 0x0000,
11224 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011225
Michael Chana71116d2005-05-29 14:58:11 -070011226 /* Receive BD Initiator Control Registers. */
11227 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
11228 0x00000000, 0xffffffff },
11229 { RCVBDI_STD_THRESH, TG3_FL_5705,
11230 0x00000000, 0x000003ff },
11231 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
11232 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011233
Michael Chana71116d2005-05-29 14:58:11 -070011234 /* Host Coalescing Control Registers. */
11235 { HOSTCC_MODE, TG3_FL_NOT_5705,
11236 0x00000000, 0x00000004 },
11237 { HOSTCC_MODE, TG3_FL_5705,
11238 0x00000000, 0x000000f6 },
11239 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
11240 0x00000000, 0xffffffff },
11241 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
11242 0x00000000, 0x000003ff },
11243 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
11244 0x00000000, 0xffffffff },
11245 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
11246 0x00000000, 0x000003ff },
11247 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
11248 0x00000000, 0xffffffff },
11249 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
11250 0x00000000, 0x000000ff },
11251 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
11252 0x00000000, 0xffffffff },
11253 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
11254 0x00000000, 0x000000ff },
11255 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
11256 0x00000000, 0xffffffff },
11257 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
11258 0x00000000, 0xffffffff },
11259 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
11260 0x00000000, 0xffffffff },
11261 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
11262 0x00000000, 0x000000ff },
11263 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
11264 0x00000000, 0xffffffff },
11265 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
11266 0x00000000, 0x000000ff },
11267 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
11268 0x00000000, 0xffffffff },
11269 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
11270 0x00000000, 0xffffffff },
11271 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
11272 0x00000000, 0xffffffff },
11273 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
11274 0x00000000, 0xffffffff },
11275 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
11276 0x00000000, 0xffffffff },
11277 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
11278 0xffffffff, 0x00000000 },
11279 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
11280 0xffffffff, 0x00000000 },
11281
11282 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070011283 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070011284 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070011285 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070011286 0x00000000, 0x007fffff },
11287 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
11288 0x00000000, 0x0000003f },
11289 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
11290 0x00000000, 0x000001ff },
11291 { BUFMGR_MB_HIGH_WATER, 0x0000,
11292 0x00000000, 0x000001ff },
11293 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
11294 0xffffffff, 0x00000000 },
11295 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
11296 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011297
Michael Chana71116d2005-05-29 14:58:11 -070011298 /* Mailbox Registers */
11299 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
11300 0x00000000, 0x000001ff },
11301 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
11302 0x00000000, 0x000001ff },
11303 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
11304 0x00000000, 0x000007ff },
11305 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
11306 0x00000000, 0x000001ff },
11307
11308 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
11309 };
11310
Michael Chanb16250e2006-09-27 16:10:14 -070011311 is_5705 = is_5750 = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000011312 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chana71116d2005-05-29 14:58:11 -070011313 is_5705 = 1;
Joe Perches63c3a662011-04-26 08:12:10 +000011314 if (tg3_flag(tp, 5750_PLUS))
Michael Chanb16250e2006-09-27 16:10:14 -070011315 is_5750 = 1;
11316 }
Michael Chana71116d2005-05-29 14:58:11 -070011317
11318 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
11319 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
11320 continue;
11321
11322 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
11323 continue;
11324
Joe Perches63c3a662011-04-26 08:12:10 +000011325 if (tg3_flag(tp, IS_5788) &&
Michael Chana71116d2005-05-29 14:58:11 -070011326 (reg_tbl[i].flags & TG3_FL_NOT_5788))
11327 continue;
11328
Michael Chanb16250e2006-09-27 16:10:14 -070011329 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
11330 continue;
11331
Michael Chana71116d2005-05-29 14:58:11 -070011332 offset = (u32) reg_tbl[i].offset;
11333 read_mask = reg_tbl[i].read_mask;
11334 write_mask = reg_tbl[i].write_mask;
11335
11336 /* Save the original register content */
11337 save_val = tr32(offset);
11338
11339 /* Determine the read-only value. */
11340 read_val = save_val & read_mask;
11341
11342 /* Write zero to the register, then make sure the read-only bits
11343 * are not changed and the read/write bits are all zeros.
11344 */
11345 tw32(offset, 0);
11346
11347 val = tr32(offset);
11348
11349 /* Test the read-only and read/write bits. */
11350 if (((val & read_mask) != read_val) || (val & write_mask))
11351 goto out;
11352
11353 /* Write ones to all the bits defined by RdMask and WrMask, then
11354 * make sure the read-only bits are not changed and the
11355 * read/write bits are all ones.
11356 */
11357 tw32(offset, read_mask | write_mask);
11358
11359 val = tr32(offset);
11360
11361 /* Test the read-only bits. */
11362 if ((val & read_mask) != read_val)
11363 goto out;
11364
11365 /* Test the read/write bits. */
11366 if ((val & write_mask) != write_mask)
11367 goto out;
11368
11369 tw32(offset, save_val);
11370 }
11371
11372 return 0;
11373
11374out:
Michael Chan9f88f292006-12-07 00:22:54 -080011375 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000011376 netdev_err(tp->dev,
11377 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070011378 tw32(offset, save_val);
11379 return -EIO;
11380}
11381
Michael Chan7942e1d2005-05-29 14:58:36 -070011382static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
11383{
Arjan van de Venf71e1302006-03-03 21:33:57 -050011384 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070011385 int i;
11386 u32 j;
11387
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020011388 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070011389 for (j = 0; j < len; j += 4) {
11390 u32 val;
11391
11392 tg3_write_mem(tp, offset + j, test_pattern[i]);
11393 tg3_read_mem(tp, offset + j, &val);
11394 if (val != test_pattern[i])
11395 return -EIO;
11396 }
11397 }
11398 return 0;
11399}
11400
11401static int tg3_test_memory(struct tg3 *tp)
11402{
11403 static struct mem_entry {
11404 u32 offset;
11405 u32 len;
11406 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080011407 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070011408 { 0x00002000, 0x1c000},
11409 { 0xffffffff, 0x00000}
11410 }, mem_tbl_5705[] = {
11411 { 0x00000100, 0x0000c},
11412 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070011413 { 0x00004000, 0x00800},
11414 { 0x00006000, 0x01000},
11415 { 0x00008000, 0x02000},
11416 { 0x00010000, 0x0e000},
11417 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080011418 }, mem_tbl_5755[] = {
11419 { 0x00000200, 0x00008},
11420 { 0x00004000, 0x00800},
11421 { 0x00006000, 0x00800},
11422 { 0x00008000, 0x02000},
11423 { 0x00010000, 0x0c000},
11424 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070011425 }, mem_tbl_5906[] = {
11426 { 0x00000200, 0x00008},
11427 { 0x00004000, 0x00400},
11428 { 0x00006000, 0x00400},
11429 { 0x00008000, 0x01000},
11430 { 0x00010000, 0x01000},
11431 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011432 }, mem_tbl_5717[] = {
11433 { 0x00000200, 0x00008},
11434 { 0x00010000, 0x0a000},
11435 { 0x00020000, 0x13c00},
11436 { 0xffffffff, 0x00000}
11437 }, mem_tbl_57765[] = {
11438 { 0x00000200, 0x00008},
11439 { 0x00004000, 0x00800},
11440 { 0x00006000, 0x09800},
11441 { 0x00010000, 0x0a000},
11442 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070011443 };
11444 struct mem_entry *mem_tbl;
11445 int err = 0;
11446 int i;
11447
Joe Perches63c3a662011-04-26 08:12:10 +000011448 if (tg3_flag(tp, 5717_PLUS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011449 mem_tbl = mem_tbl_5717;
Matt Carlson55086ad2011-12-14 11:09:59 +000011450 else if (tg3_flag(tp, 57765_CLASS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011451 mem_tbl = mem_tbl_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000011452 else if (tg3_flag(tp, 5755_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080011453 mem_tbl = mem_tbl_5755;
11454 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
11455 mem_tbl = mem_tbl_5906;
Joe Perches63c3a662011-04-26 08:12:10 +000011456 else if (tg3_flag(tp, 5705_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080011457 mem_tbl = mem_tbl_5705;
11458 else
Michael Chan7942e1d2005-05-29 14:58:36 -070011459 mem_tbl = mem_tbl_570x;
11460
11461 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000011462 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
11463 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070011464 break;
11465 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011466
Michael Chan7942e1d2005-05-29 14:58:36 -070011467 return err;
11468}
11469
Matt Carlsonbb158d62011-04-25 12:42:47 +000011470#define TG3_TSO_MSS 500
11471
11472#define TG3_TSO_IP_HDR_LEN 20
11473#define TG3_TSO_TCP_HDR_LEN 20
11474#define TG3_TSO_TCP_OPT_LEN 12
11475
11476static const u8 tg3_tso_header[] = {
114770x08, 0x00,
114780x45, 0x00, 0x00, 0x00,
114790x00, 0x00, 0x40, 0x00,
114800x40, 0x06, 0x00, 0x00,
114810x0a, 0x00, 0x00, 0x01,
114820x0a, 0x00, 0x00, 0x02,
114830x0d, 0x00, 0xe0, 0x00,
114840x00, 0x00, 0x01, 0x00,
114850x00, 0x00, 0x02, 0x00,
114860x80, 0x10, 0x10, 0x00,
114870x14, 0x09, 0x00, 0x00,
114880x01, 0x01, 0x08, 0x0a,
114890x11, 0x11, 0x11, 0x11,
114900x11, 0x11, 0x11, 0x11,
11491};
Michael Chan9f40dea2005-09-05 17:53:06 -070011492
Matt Carlson28a45952011-08-19 13:58:22 +000011493static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, bool tso_loopback)
Michael Chanc76949a2005-05-29 14:58:59 -070011494{
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011495 u32 rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011496 u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
Matt Carlson84b67b22011-07-27 14:20:52 +000011497 u32 budget;
Eric Dumazet9205fd92011-11-18 06:47:01 +000011498 struct sk_buff *skb;
11499 u8 *tx_data, *rx_data;
Michael Chanc76949a2005-05-29 14:58:59 -070011500 dma_addr_t map;
11501 int num_pkts, tx_len, rx_len, i, err;
11502 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000011503 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000011504 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070011505
Matt Carlsonc8873402010-02-12 14:47:11 +000011506 tnapi = &tp->napi[0];
11507 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011508 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +000011509 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlson1da85aa2010-09-30 10:34:34 +000011510 rnapi = &tp->napi[1];
Joe Perches63c3a662011-04-26 08:12:10 +000011511 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc8873402010-02-12 14:47:11 +000011512 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011513 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011514 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000011515
Michael Chanc76949a2005-05-29 14:58:59 -070011516 err = -EIO;
11517
Matt Carlson4852a862011-04-13 11:05:07 +000011518 tx_len = pktsz;
David S. Millera20e9c62006-07-31 22:38:16 -070011519 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070011520 if (!skb)
11521 return -ENOMEM;
11522
Michael Chanc76949a2005-05-29 14:58:59 -070011523 tx_data = skb_put(skb, tx_len);
11524 memcpy(tx_data, tp->dev->dev_addr, 6);
11525 memset(tx_data + 6, 0x0, 8);
11526
Matt Carlson4852a862011-04-13 11:05:07 +000011527 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
Michael Chanc76949a2005-05-29 14:58:59 -070011528
Matt Carlson28a45952011-08-19 13:58:22 +000011529 if (tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011530 struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
11531
11532 u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
11533 TG3_TSO_TCP_OPT_LEN;
11534
11535 memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
11536 sizeof(tg3_tso_header));
11537 mss = TG3_TSO_MSS;
11538
11539 val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
11540 num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
11541
11542 /* Set the total length field in the IP header */
11543 iph->tot_len = htons((u16)(mss + hdr_len));
11544
11545 base_flags = (TXD_FLAG_CPU_PRE_DMA |
11546 TXD_FLAG_CPU_POST_DMA);
11547
Joe Perches63c3a662011-04-26 08:12:10 +000011548 if (tg3_flag(tp, HW_TSO_1) ||
11549 tg3_flag(tp, HW_TSO_2) ||
11550 tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011551 struct tcphdr *th;
11552 val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
11553 th = (struct tcphdr *)&tx_data[val];
11554 th->check = 0;
11555 } else
11556 base_flags |= TXD_FLAG_TCPUDP_CSUM;
11557
Joe Perches63c3a662011-04-26 08:12:10 +000011558 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011559 mss |= (hdr_len & 0xc) << 12;
11560 if (hdr_len & 0x10)
11561 base_flags |= 0x00000010;
11562 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +000011563 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlsonbb158d62011-04-25 12:42:47 +000011564 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +000011565 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlsonbb158d62011-04-25 12:42:47 +000011566 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
11567 mss |= (TG3_TSO_TCP_OPT_LEN << 9);
11568 } else {
11569 base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
11570 }
11571
11572 data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
11573 } else {
11574 num_pkts = 1;
11575 data_off = ETH_HLEN;
11576 }
11577
11578 for (i = data_off; i < tx_len; i++)
Michael Chanc76949a2005-05-29 14:58:59 -070011579 tx_data[i] = (u8) (i & 0xff);
11580
Alexander Duyckf4188d82009-12-02 16:48:38 +000011581 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
11582 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000011583 dev_kfree_skb(skb);
11584 return -EIO;
11585 }
Michael Chanc76949a2005-05-29 14:58:59 -070011586
Matt Carlson0d681b22011-07-27 14:20:49 +000011587 val = tnapi->tx_prod;
11588 tnapi->tx_buffers[val].skb = skb;
11589 dma_unmap_addr_set(&tnapi->tx_buffers[val], mapping, map);
11590
Michael Chanc76949a2005-05-29 14:58:59 -070011591 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011592 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011593
11594 udelay(10);
11595
Matt Carlson898a56f2009-08-28 14:02:40 +000011596 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070011597
Matt Carlson84b67b22011-07-27 14:20:52 +000011598 budget = tg3_tx_avail(tnapi);
11599 if (tg3_tx_frag_set(tnapi, &val, &budget, map, tx_len,
Matt Carlsond1a3b732011-07-27 14:20:51 +000011600 base_flags | TXD_FLAG_END, mss, 0)) {
11601 tnapi->tx_buffers[val].skb = NULL;
11602 dev_kfree_skb(skb);
11603 return -EIO;
11604 }
Michael Chanc76949a2005-05-29 14:58:59 -070011605
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011606 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070011607
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011608 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
11609 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070011610
11611 udelay(10);
11612
Matt Carlson303fc922009-11-02 14:27:34 +000011613 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
11614 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070011615 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011616 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011617
11618 udelay(10);
11619
Matt Carlson898a56f2009-08-28 14:02:40 +000011620 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
11621 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011622 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070011623 (rx_idx == (rx_start_idx + num_pkts)))
11624 break;
11625 }
11626
Matt Carlsonba1142e2011-11-04 09:15:00 +000011627 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod - 1, -1);
Michael Chanc76949a2005-05-29 14:58:59 -070011628 dev_kfree_skb(skb);
11629
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011630 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070011631 goto out;
11632
11633 if (rx_idx != rx_start_idx + num_pkts)
11634 goto out;
11635
Matt Carlsonbb158d62011-04-25 12:42:47 +000011636 val = data_off;
11637 while (rx_idx != rx_start_idx) {
11638 desc = &rnapi->rx_rcb[rx_start_idx++];
11639 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
11640 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
Michael Chanc76949a2005-05-29 14:58:59 -070011641
Matt Carlsonbb158d62011-04-25 12:42:47 +000011642 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
11643 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
Matt Carlson4852a862011-04-13 11:05:07 +000011644 goto out;
Michael Chanc76949a2005-05-29 14:58:59 -070011645
Matt Carlsonbb158d62011-04-25 12:42:47 +000011646 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
11647 - ETH_FCS_LEN;
11648
Matt Carlson28a45952011-08-19 13:58:22 +000011649 if (!tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011650 if (rx_len != tx_len)
11651 goto out;
11652
11653 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
11654 if (opaque_key != RXD_OPAQUE_RING_STD)
11655 goto out;
11656 } else {
11657 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
11658 goto out;
11659 }
11660 } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
11661 (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
Matt Carlson54e0a672011-05-19 12:12:50 +000011662 >> RXD_TCPCSUM_SHIFT != 0xffff) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011663 goto out;
11664 }
11665
11666 if (opaque_key == RXD_OPAQUE_RING_STD) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000011667 rx_data = tpr->rx_std_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011668 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
11669 mapping);
11670 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000011671 rx_data = tpr->rx_jmb_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011672 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
11673 mapping);
11674 } else
Matt Carlson4852a862011-04-13 11:05:07 +000011675 goto out;
11676
Matt Carlsonbb158d62011-04-25 12:42:47 +000011677 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
11678 PCI_DMA_FROMDEVICE);
11679
Eric Dumazet9205fd92011-11-18 06:47:01 +000011680 rx_data += TG3_RX_OFFSET(tp);
Matt Carlsonbb158d62011-04-25 12:42:47 +000011681 for (i = data_off; i < rx_len; i++, val++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000011682 if (*(rx_data + i) != (u8) (val & 0xff))
Matt Carlsonbb158d62011-04-25 12:42:47 +000011683 goto out;
11684 }
Matt Carlson4852a862011-04-13 11:05:07 +000011685 }
11686
Michael Chanc76949a2005-05-29 14:58:59 -070011687 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011688
Eric Dumazet9205fd92011-11-18 06:47:01 +000011689 /* tg3_free_rings will unmap and free the rx_data */
Michael Chanc76949a2005-05-29 14:58:59 -070011690out:
11691 return err;
11692}
11693
Matt Carlson00c266b2011-04-25 12:42:46 +000011694#define TG3_STD_LOOPBACK_FAILED 1
11695#define TG3_JMB_LOOPBACK_FAILED 2
Matt Carlsonbb158d62011-04-25 12:42:47 +000011696#define TG3_TSO_LOOPBACK_FAILED 4
Matt Carlson28a45952011-08-19 13:58:22 +000011697#define TG3_LOOPBACK_FAILED \
11698 (TG3_STD_LOOPBACK_FAILED | \
11699 TG3_JMB_LOOPBACK_FAILED | \
11700 TG3_TSO_LOOPBACK_FAILED)
Matt Carlson00c266b2011-04-25 12:42:46 +000011701
Matt Carlson941ec902011-08-19 13:58:23 +000011702static int tg3_test_loopback(struct tg3 *tp, u64 *data, bool do_extlpbk)
Michael Chan9f40dea2005-09-05 17:53:06 -070011703{
Matt Carlson28a45952011-08-19 13:58:22 +000011704 int err = -EIO;
Matt Carlson2215e242011-08-19 13:58:19 +000011705 u32 eee_cap;
Michael Chan9f40dea2005-09-05 17:53:06 -070011706
Matt Carlsonab789042011-01-25 15:58:54 +000011707 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
11708 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
11709
Matt Carlson28a45952011-08-19 13:58:22 +000011710 if (!netif_running(tp->dev)) {
11711 data[0] = TG3_LOOPBACK_FAILED;
11712 data[1] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000011713 if (do_extlpbk)
11714 data[2] = TG3_LOOPBACK_FAILED;
Matt Carlson28a45952011-08-19 13:58:22 +000011715 goto done;
11716 }
11717
Michael Chanb9ec6c12006-07-25 16:37:27 -070011718 err = tg3_reset_hw(tp, 1);
Matt Carlsonab789042011-01-25 15:58:54 +000011719 if (err) {
Matt Carlson28a45952011-08-19 13:58:22 +000011720 data[0] = TG3_LOOPBACK_FAILED;
11721 data[1] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000011722 if (do_extlpbk)
11723 data[2] = TG3_LOOPBACK_FAILED;
Matt Carlsonab789042011-01-25 15:58:54 +000011724 goto done;
11725 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011726
Joe Perches63c3a662011-04-26 08:12:10 +000011727 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson4a85f092011-04-20 07:57:37 +000011728 int i;
11729
11730 /* Reroute all rx packets to the 1st queue */
11731 for (i = MAC_RSS_INDIR_TBL_0;
11732 i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
11733 tw32(i, 0x0);
11734 }
11735
Matt Carlson6e01b202011-08-19 13:58:20 +000011736 /* HW errata - mac loopback fails in some cases on 5780.
11737 * Normal traffic and PHY loopback are not affected by
11738 * errata. Also, the MAC loopback test is deprecated for
11739 * all newer ASIC revisions.
11740 */
11741 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
11742 !tg3_flag(tp, CPMU_PRESENT)) {
11743 tg3_mac_loopback(tp, true);
Matt Carlson9936bcf2007-10-10 18:03:07 -070011744
Matt Carlson28a45952011-08-19 13:58:22 +000011745 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
11746 data[0] |= TG3_STD_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000011747
11748 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000011749 tg3_run_loopback(tp, 9000 + ETH_HLEN, false))
11750 data[0] |= TG3_JMB_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000011751
11752 tg3_mac_loopback(tp, false);
11753 }
Matt Carlson4852a862011-04-13 11:05:07 +000011754
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011755 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000011756 !tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011757 int i;
11758
Matt Carlson941ec902011-08-19 13:58:23 +000011759 tg3_phy_lpbk_set(tp, 0, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011760
11761 /* Wait for link */
11762 for (i = 0; i < 100; i++) {
11763 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
11764 break;
11765 mdelay(1);
11766 }
11767
Matt Carlson28a45952011-08-19 13:58:22 +000011768 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
11769 data[1] |= TG3_STD_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000011770 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000011771 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
11772 data[1] |= TG3_TSO_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000011773 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000011774 tg3_run_loopback(tp, 9000 + ETH_HLEN, false))
11775 data[1] |= TG3_JMB_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070011776
Matt Carlson941ec902011-08-19 13:58:23 +000011777 if (do_extlpbk) {
11778 tg3_phy_lpbk_set(tp, 0, true);
11779
11780 /* All link indications report up, but the hardware
11781 * isn't really ready for about 20 msec. Double it
11782 * to be sure.
11783 */
11784 mdelay(40);
11785
11786 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
11787 data[2] |= TG3_STD_LOOPBACK_FAILED;
11788 if (tg3_flag(tp, TSO_CAPABLE) &&
11789 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
11790 data[2] |= TG3_TSO_LOOPBACK_FAILED;
11791 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
11792 tg3_run_loopback(tp, 9000 + ETH_HLEN, false))
11793 data[2] |= TG3_JMB_LOOPBACK_FAILED;
11794 }
11795
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011796 /* Re-enable gphy autopowerdown. */
11797 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
11798 tg3_phy_toggle_apd(tp, true);
11799 }
Matt Carlson6833c042008-11-21 17:18:59 -080011800
Matt Carlson941ec902011-08-19 13:58:23 +000011801 err = (data[0] | data[1] | data[2]) ? -EIO : 0;
Matt Carlson28a45952011-08-19 13:58:22 +000011802
Matt Carlsonab789042011-01-25 15:58:54 +000011803done:
11804 tp->phy_flags |= eee_cap;
11805
Michael Chan9f40dea2005-09-05 17:53:06 -070011806 return err;
11807}
11808
Michael Chan4cafd3f2005-05-29 14:56:34 -070011809static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
11810 u64 *data)
11811{
Michael Chan566f86a2005-05-29 14:56:58 -070011812 struct tg3 *tp = netdev_priv(dev);
Matt Carlson941ec902011-08-19 13:58:23 +000011813 bool doextlpbk = etest->flags & ETH_TEST_FL_EXTERNAL_LB;
Michael Chan566f86a2005-05-29 14:56:58 -070011814
Matt Carlsonbed98292011-07-13 09:27:29 +000011815 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
11816 tg3_power_up(tp)) {
11817 etest->flags |= ETH_TEST_FL_FAILED;
11818 memset(data, 1, sizeof(u64) * TG3_NUM_TEST);
11819 return;
11820 }
Michael Chanbc1c7562006-03-20 17:48:03 -080011821
Michael Chan566f86a2005-05-29 14:56:58 -070011822 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
11823
11824 if (tg3_test_nvram(tp) != 0) {
11825 etest->flags |= ETH_TEST_FL_FAILED;
11826 data[0] = 1;
11827 }
Matt Carlson941ec902011-08-19 13:58:23 +000011828 if (!doextlpbk && tg3_test_link(tp)) {
Michael Chanca430072005-05-29 14:57:23 -070011829 etest->flags |= ETH_TEST_FL_FAILED;
11830 data[1] = 1;
11831 }
Michael Chana71116d2005-05-29 14:58:11 -070011832 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011833 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070011834
Michael Chanbbe832c2005-06-24 20:20:04 -070011835 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011836 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070011837 tg3_netif_stop(tp);
11838 irq_sync = 1;
11839 }
11840
11841 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070011842
11843 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080011844 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011845 tg3_halt_cpu(tp, RX_CPU_BASE);
Joe Perches63c3a662011-04-26 08:12:10 +000011846 if (!tg3_flag(tp, 5705_PLUS))
Michael Chana71116d2005-05-29 14:58:11 -070011847 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080011848 if (!err)
11849 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011850
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011851 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad12006-03-20 22:27:35 -080011852 tg3_phy_reset(tp);
11853
Michael Chana71116d2005-05-29 14:58:11 -070011854 if (tg3_test_registers(tp) != 0) {
11855 etest->flags |= ETH_TEST_FL_FAILED;
11856 data[2] = 1;
11857 }
Matt Carlson28a45952011-08-19 13:58:22 +000011858
Michael Chan7942e1d2005-05-29 14:58:36 -070011859 if (tg3_test_memory(tp) != 0) {
11860 etest->flags |= ETH_TEST_FL_FAILED;
11861 data[3] = 1;
11862 }
Matt Carlson28a45952011-08-19 13:58:22 +000011863
Matt Carlson941ec902011-08-19 13:58:23 +000011864 if (doextlpbk)
11865 etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
11866
11867 if (tg3_test_loopback(tp, &data[4], doextlpbk))
Michael Chanc76949a2005-05-29 14:58:59 -070011868 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070011869
David S. Millerf47c11e2005-06-24 20:18:35 -070011870 tg3_full_unlock(tp);
11871
Michael Chand4bc3922005-05-29 14:59:20 -070011872 if (tg3_test_interrupt(tp) != 0) {
11873 etest->flags |= ETH_TEST_FL_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000011874 data[7] = 1;
Michael Chand4bc3922005-05-29 14:59:20 -070011875 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011876
11877 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070011878
Michael Chana71116d2005-05-29 14:58:11 -070011879 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
11880 if (netif_running(dev)) {
Joe Perches63c3a662011-04-26 08:12:10 +000011881 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011882 err2 = tg3_restart_hw(tp, 1);
11883 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070011884 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011885 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011886
11887 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011888
11889 if (irq_sync && !err2)
11890 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011891 }
Matt Carlson80096062010-08-02 11:26:06 +000011892 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000011893 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080011894
Michael Chan4cafd3f2005-05-29 14:56:34 -070011895}
11896
Linus Torvalds1da177e2005-04-16 15:20:36 -070011897static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
11898{
11899 struct mii_ioctl_data *data = if_mii(ifr);
11900 struct tg3 *tp = netdev_priv(dev);
11901 int err;
11902
Joe Perches63c3a662011-04-26 08:12:10 +000011903 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011904 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011905 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011906 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011907 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000011908 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011909 }
11910
Matt Carlson33f401a2010-04-05 10:19:27 +000011911 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011912 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000011913 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011914
11915 /* fallthru */
11916 case SIOCGMIIREG: {
11917 u32 mii_regval;
11918
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011919 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011920 break; /* We have no PHY */
11921
Matt Carlson34eea5a2011-04-20 07:57:38 +000011922 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080011923 return -EAGAIN;
11924
David S. Millerf47c11e2005-06-24 20:18:35 -070011925 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011926 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070011927 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011928
11929 data->val_out = mii_regval;
11930
11931 return err;
11932 }
11933
11934 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011935 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011936 break; /* We have no PHY */
11937
Matt Carlson34eea5a2011-04-20 07:57:38 +000011938 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080011939 return -EAGAIN;
11940
David S. Millerf47c11e2005-06-24 20:18:35 -070011941 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011942 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070011943 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011944
11945 return err;
11946
11947 default:
11948 /* do nothing */
11949 break;
11950 }
11951 return -EOPNOTSUPP;
11952}
11953
David S. Miller15f98502005-05-18 22:49:26 -070011954static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11955{
11956 struct tg3 *tp = netdev_priv(dev);
11957
11958 memcpy(ec, &tp->coal, sizeof(*ec));
11959 return 0;
11960}
11961
Michael Chand244c892005-07-05 14:42:33 -070011962static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11963{
11964 struct tg3 *tp = netdev_priv(dev);
11965 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
11966 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
11967
Joe Perches63c3a662011-04-26 08:12:10 +000011968 if (!tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070011969 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
11970 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
11971 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
11972 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
11973 }
11974
11975 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
11976 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
11977 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
11978 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
11979 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
11980 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
11981 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
11982 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
11983 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
11984 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
11985 return -EINVAL;
11986
11987 /* No rx interrupts will be generated if both are zero */
11988 if ((ec->rx_coalesce_usecs == 0) &&
11989 (ec->rx_max_coalesced_frames == 0))
11990 return -EINVAL;
11991
11992 /* No tx interrupts will be generated if both are zero */
11993 if ((ec->tx_coalesce_usecs == 0) &&
11994 (ec->tx_max_coalesced_frames == 0))
11995 return -EINVAL;
11996
11997 /* Only copy relevant parameters, ignore all others. */
11998 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
11999 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
12000 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
12001 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
12002 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
12003 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
12004 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
12005 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
12006 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
12007
12008 if (netif_running(dev)) {
12009 tg3_full_lock(tp, 0);
12010 __tg3_set_coalesce(tp, &tp->coal);
12011 tg3_full_unlock(tp);
12012 }
12013 return 0;
12014}
12015
Jeff Garzik7282d492006-09-13 14:30:00 -040012016static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012017 .get_settings = tg3_get_settings,
12018 .set_settings = tg3_set_settings,
12019 .get_drvinfo = tg3_get_drvinfo,
12020 .get_regs_len = tg3_get_regs_len,
12021 .get_regs = tg3_get_regs,
12022 .get_wol = tg3_get_wol,
12023 .set_wol = tg3_set_wol,
12024 .get_msglevel = tg3_get_msglevel,
12025 .set_msglevel = tg3_set_msglevel,
12026 .nway_reset = tg3_nway_reset,
12027 .get_link = ethtool_op_get_link,
12028 .get_eeprom_len = tg3_get_eeprom_len,
12029 .get_eeprom = tg3_get_eeprom,
12030 .set_eeprom = tg3_set_eeprom,
12031 .get_ringparam = tg3_get_ringparam,
12032 .set_ringparam = tg3_set_ringparam,
12033 .get_pauseparam = tg3_get_pauseparam,
12034 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070012035 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012036 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000012037 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012038 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070012039 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070012040 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070012041 .get_sset_count = tg3_get_sset_count,
Matt Carlson90415472011-12-16 13:33:23 +000012042 .get_rxnfc = tg3_get_rxnfc,
12043 .get_rxfh_indir_size = tg3_get_rxfh_indir_size,
12044 .get_rxfh_indir = tg3_get_rxfh_indir,
12045 .set_rxfh_indir = tg3_set_rxfh_indir,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012046};
12047
12048static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
12049{
Michael Chan1b277772006-03-20 22:27:48 -080012050 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012051
12052 tp->nvram_size = EEPROM_CHIP_SIZE;
12053
Matt Carlsone4f34112009-02-25 14:25:00 +000012054 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012055 return;
12056
Michael Chanb16250e2006-09-27 16:10:14 -070012057 if ((magic != TG3_EEPROM_MAGIC) &&
12058 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
12059 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012060 return;
12061
12062 /*
12063 * Size the chip by reading offsets at increasing powers of two.
12064 * When we encounter our validation signature, we know the addressing
12065 * has wrapped around, and thus have our chip size.
12066 */
Michael Chan1b277772006-03-20 22:27:48 -080012067 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012068
12069 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000012070 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012071 return;
12072
Michael Chan18201802006-03-20 22:29:15 -080012073 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012074 break;
12075
12076 cursize <<= 1;
12077 }
12078
12079 tp->nvram_size = cursize;
12080}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012081
Linus Torvalds1da177e2005-04-16 15:20:36 -070012082static void __devinit tg3_get_nvram_size(struct tg3 *tp)
12083{
12084 u32 val;
12085
Joe Perches63c3a662011-04-26 08:12:10 +000012086 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080012087 return;
12088
12089 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080012090 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080012091 tg3_get_eeprom_size(tp);
12092 return;
12093 }
12094
Matt Carlson6d348f22009-02-25 14:25:52 +000012095 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012096 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000012097 /* This is confusing. We want to operate on the
12098 * 16-bit value at offset 0xf2. The tg3_nvram_read()
12099 * call will read from NVRAM and byteswap the data
12100 * according to the byteswapping settings for all
12101 * other register accesses. This ensures the data we
12102 * want will always reside in the lower 16-bits.
12103 * However, the data in NVRAM is in LE format, which
12104 * means the data from the NVRAM read will always be
12105 * opposite the endianness of the CPU. The 16-bit
12106 * byteswap then brings the data to CPU endianness.
12107 */
12108 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012109 return;
12110 }
12111 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070012112 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012113}
12114
12115static void __devinit tg3_get_nvram_info(struct tg3 *tp)
12116{
12117 u32 nvcfg1;
12118
12119 nvcfg1 = tr32(NVRAM_CFG1);
12120 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
Joe Perches63c3a662011-04-26 08:12:10 +000012121 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012122 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012123 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12124 tw32(NVRAM_CFG1, nvcfg1);
12125 }
12126
Matt Carlson6ff6f812011-05-19 12:12:54 +000012127 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
Joe Perches63c3a662011-04-26 08:12:10 +000012128 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012129 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012130 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
12131 tp->nvram_jedecnum = JEDEC_ATMEL;
12132 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012133 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012134 break;
12135 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
12136 tp->nvram_jedecnum = JEDEC_ATMEL;
12137 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
12138 break;
12139 case FLASH_VENDOR_ATMEL_EEPROM:
12140 tp->nvram_jedecnum = JEDEC_ATMEL;
12141 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012142 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012143 break;
12144 case FLASH_VENDOR_ST:
12145 tp->nvram_jedecnum = JEDEC_ST;
12146 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012147 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012148 break;
12149 case FLASH_VENDOR_SAIFUN:
12150 tp->nvram_jedecnum = JEDEC_SAIFUN;
12151 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
12152 break;
12153 case FLASH_VENDOR_SST_SMALL:
12154 case FLASH_VENDOR_SST_LARGE:
12155 tp->nvram_jedecnum = JEDEC_SST;
12156 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
12157 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012158 }
Matt Carlson8590a602009-08-28 12:29:16 +000012159 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012160 tp->nvram_jedecnum = JEDEC_ATMEL;
12161 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012162 tg3_flag_set(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012163 }
12164}
12165
Matt Carlsona1b950d2009-09-01 13:20:17 +000012166static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
12167{
12168 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
12169 case FLASH_5752PAGE_SIZE_256:
12170 tp->nvram_pagesize = 256;
12171 break;
12172 case FLASH_5752PAGE_SIZE_512:
12173 tp->nvram_pagesize = 512;
12174 break;
12175 case FLASH_5752PAGE_SIZE_1K:
12176 tp->nvram_pagesize = 1024;
12177 break;
12178 case FLASH_5752PAGE_SIZE_2K:
12179 tp->nvram_pagesize = 2048;
12180 break;
12181 case FLASH_5752PAGE_SIZE_4K:
12182 tp->nvram_pagesize = 4096;
12183 break;
12184 case FLASH_5752PAGE_SIZE_264:
12185 tp->nvram_pagesize = 264;
12186 break;
12187 case FLASH_5752PAGE_SIZE_528:
12188 tp->nvram_pagesize = 528;
12189 break;
12190 }
12191}
12192
Michael Chan361b4ac2005-04-21 17:11:21 -070012193static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
12194{
12195 u32 nvcfg1;
12196
12197 nvcfg1 = tr32(NVRAM_CFG1);
12198
Michael Chane6af3012005-04-21 17:12:05 -070012199 /* NVRAM protection for TPM */
12200 if (nvcfg1 & (1 << 27))
Joe Perches63c3a662011-04-26 08:12:10 +000012201 tg3_flag_set(tp, PROTECTED_NVRAM);
Michael Chane6af3012005-04-21 17:12:05 -070012202
Michael Chan361b4ac2005-04-21 17:11:21 -070012203 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012204 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
12205 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
12206 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012207 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012208 break;
12209 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12210 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012211 tg3_flag_set(tp, NVRAM_BUFFERED);
12212 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012213 break;
12214 case FLASH_5752VENDOR_ST_M45PE10:
12215 case FLASH_5752VENDOR_ST_M45PE20:
12216 case FLASH_5752VENDOR_ST_M45PE40:
12217 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012218 tg3_flag_set(tp, NVRAM_BUFFERED);
12219 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012220 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070012221 }
12222
Joe Perches63c3a662011-04-26 08:12:10 +000012223 if (tg3_flag(tp, FLASH)) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000012224 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000012225 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070012226 /* For eeprom, set pagesize to maximum eeprom size */
12227 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12228
12229 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12230 tw32(NVRAM_CFG1, nvcfg1);
12231 }
12232}
12233
Michael Chand3c7b882006-03-23 01:28:25 -080012234static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
12235{
Matt Carlson989a9d22007-05-05 11:51:05 -070012236 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080012237
12238 nvcfg1 = tr32(NVRAM_CFG1);
12239
12240 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070012241 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012242 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson989a9d22007-05-05 11:51:05 -070012243 protect = 1;
12244 }
Michael Chand3c7b882006-03-23 01:28:25 -080012245
Matt Carlson989a9d22007-05-05 11:51:05 -070012246 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
12247 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012248 case FLASH_5755VENDOR_ATMEL_FLASH_1:
12249 case FLASH_5755VENDOR_ATMEL_FLASH_2:
12250 case FLASH_5755VENDOR_ATMEL_FLASH_3:
12251 case FLASH_5755VENDOR_ATMEL_FLASH_5:
12252 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012253 tg3_flag_set(tp, NVRAM_BUFFERED);
12254 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012255 tp->nvram_pagesize = 264;
12256 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
12257 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
12258 tp->nvram_size = (protect ? 0x3e200 :
12259 TG3_NVRAM_SIZE_512KB);
12260 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
12261 tp->nvram_size = (protect ? 0x1f200 :
12262 TG3_NVRAM_SIZE_256KB);
12263 else
12264 tp->nvram_size = (protect ? 0x1f200 :
12265 TG3_NVRAM_SIZE_128KB);
12266 break;
12267 case FLASH_5752VENDOR_ST_M45PE10:
12268 case FLASH_5752VENDOR_ST_M45PE20:
12269 case FLASH_5752VENDOR_ST_M45PE40:
12270 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012271 tg3_flag_set(tp, NVRAM_BUFFERED);
12272 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012273 tp->nvram_pagesize = 256;
12274 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
12275 tp->nvram_size = (protect ?
12276 TG3_NVRAM_SIZE_64KB :
12277 TG3_NVRAM_SIZE_128KB);
12278 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
12279 tp->nvram_size = (protect ?
12280 TG3_NVRAM_SIZE_64KB :
12281 TG3_NVRAM_SIZE_256KB);
12282 else
12283 tp->nvram_size = (protect ?
12284 TG3_NVRAM_SIZE_128KB :
12285 TG3_NVRAM_SIZE_512KB);
12286 break;
Michael Chand3c7b882006-03-23 01:28:25 -080012287 }
12288}
12289
Michael Chan1b277772006-03-20 22:27:48 -080012290static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
12291{
12292 u32 nvcfg1;
12293
12294 nvcfg1 = tr32(NVRAM_CFG1);
12295
12296 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012297 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
12298 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
12299 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
12300 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
12301 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012302 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012303 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080012304
Matt Carlson8590a602009-08-28 12:29:16 +000012305 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12306 tw32(NVRAM_CFG1, nvcfg1);
12307 break;
12308 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12309 case FLASH_5755VENDOR_ATMEL_FLASH_1:
12310 case FLASH_5755VENDOR_ATMEL_FLASH_2:
12311 case FLASH_5755VENDOR_ATMEL_FLASH_3:
12312 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012313 tg3_flag_set(tp, NVRAM_BUFFERED);
12314 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012315 tp->nvram_pagesize = 264;
12316 break;
12317 case FLASH_5752VENDOR_ST_M45PE10:
12318 case FLASH_5752VENDOR_ST_M45PE20:
12319 case FLASH_5752VENDOR_ST_M45PE40:
12320 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012321 tg3_flag_set(tp, NVRAM_BUFFERED);
12322 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012323 tp->nvram_pagesize = 256;
12324 break;
Michael Chan1b277772006-03-20 22:27:48 -080012325 }
12326}
12327
Matt Carlson6b91fa02007-10-10 18:01:09 -070012328static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
12329{
12330 u32 nvcfg1, protect = 0;
12331
12332 nvcfg1 = tr32(NVRAM_CFG1);
12333
12334 /* NVRAM protection for TPM */
12335 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012336 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson6b91fa02007-10-10 18:01:09 -070012337 protect = 1;
12338 }
12339
12340 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
12341 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012342 case FLASH_5761VENDOR_ATMEL_ADB021D:
12343 case FLASH_5761VENDOR_ATMEL_ADB041D:
12344 case FLASH_5761VENDOR_ATMEL_ADB081D:
12345 case FLASH_5761VENDOR_ATMEL_ADB161D:
12346 case FLASH_5761VENDOR_ATMEL_MDB021D:
12347 case FLASH_5761VENDOR_ATMEL_MDB041D:
12348 case FLASH_5761VENDOR_ATMEL_MDB081D:
12349 case FLASH_5761VENDOR_ATMEL_MDB161D:
12350 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012351 tg3_flag_set(tp, NVRAM_BUFFERED);
12352 tg3_flag_set(tp, FLASH);
12353 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson8590a602009-08-28 12:29:16 +000012354 tp->nvram_pagesize = 256;
12355 break;
12356 case FLASH_5761VENDOR_ST_A_M45PE20:
12357 case FLASH_5761VENDOR_ST_A_M45PE40:
12358 case FLASH_5761VENDOR_ST_A_M45PE80:
12359 case FLASH_5761VENDOR_ST_A_M45PE16:
12360 case FLASH_5761VENDOR_ST_M_M45PE20:
12361 case FLASH_5761VENDOR_ST_M_M45PE40:
12362 case FLASH_5761VENDOR_ST_M_M45PE80:
12363 case FLASH_5761VENDOR_ST_M_M45PE16:
12364 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012365 tg3_flag_set(tp, NVRAM_BUFFERED);
12366 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012367 tp->nvram_pagesize = 256;
12368 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070012369 }
12370
12371 if (protect) {
12372 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
12373 } else {
12374 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012375 case FLASH_5761VENDOR_ATMEL_ADB161D:
12376 case FLASH_5761VENDOR_ATMEL_MDB161D:
12377 case FLASH_5761VENDOR_ST_A_M45PE16:
12378 case FLASH_5761VENDOR_ST_M_M45PE16:
12379 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
12380 break;
12381 case FLASH_5761VENDOR_ATMEL_ADB081D:
12382 case FLASH_5761VENDOR_ATMEL_MDB081D:
12383 case FLASH_5761VENDOR_ST_A_M45PE80:
12384 case FLASH_5761VENDOR_ST_M_M45PE80:
12385 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12386 break;
12387 case FLASH_5761VENDOR_ATMEL_ADB041D:
12388 case FLASH_5761VENDOR_ATMEL_MDB041D:
12389 case FLASH_5761VENDOR_ST_A_M45PE40:
12390 case FLASH_5761VENDOR_ST_M_M45PE40:
12391 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12392 break;
12393 case FLASH_5761VENDOR_ATMEL_ADB021D:
12394 case FLASH_5761VENDOR_ATMEL_MDB021D:
12395 case FLASH_5761VENDOR_ST_A_M45PE20:
12396 case FLASH_5761VENDOR_ST_M_M45PE20:
12397 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12398 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070012399 }
12400 }
12401}
12402
Michael Chanb5d37722006-09-27 16:06:21 -070012403static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
12404{
12405 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012406 tg3_flag_set(tp, NVRAM_BUFFERED);
Michael Chanb5d37722006-09-27 16:06:21 -070012407 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12408}
12409
Matt Carlson321d32a2008-11-21 17:22:19 -080012410static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
12411{
12412 u32 nvcfg1;
12413
12414 nvcfg1 = tr32(NVRAM_CFG1);
12415
12416 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12417 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
12418 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
12419 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012420 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson321d32a2008-11-21 17:22:19 -080012421 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12422
12423 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12424 tw32(NVRAM_CFG1, nvcfg1);
12425 return;
12426 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12427 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
12428 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
12429 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
12430 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
12431 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
12432 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
12433 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012434 tg3_flag_set(tp, NVRAM_BUFFERED);
12435 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080012436
12437 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12438 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12439 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
12440 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
12441 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12442 break;
12443 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
12444 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
12445 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12446 break;
12447 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
12448 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
12449 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12450 break;
12451 }
12452 break;
12453 case FLASH_5752VENDOR_ST_M45PE10:
12454 case FLASH_5752VENDOR_ST_M45PE20:
12455 case FLASH_5752VENDOR_ST_M45PE40:
12456 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012457 tg3_flag_set(tp, NVRAM_BUFFERED);
12458 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080012459
12460 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12461 case FLASH_5752VENDOR_ST_M45PE10:
12462 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12463 break;
12464 case FLASH_5752VENDOR_ST_M45PE20:
12465 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12466 break;
12467 case FLASH_5752VENDOR_ST_M45PE40:
12468 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12469 break;
12470 }
12471 break;
12472 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012473 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson321d32a2008-11-21 17:22:19 -080012474 return;
12475 }
12476
Matt Carlsona1b950d2009-09-01 13:20:17 +000012477 tg3_nvram_get_pagesize(tp, nvcfg1);
12478 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012479 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012480}
12481
12482
12483static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
12484{
12485 u32 nvcfg1;
12486
12487 nvcfg1 = tr32(NVRAM_CFG1);
12488
12489 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12490 case FLASH_5717VENDOR_ATMEL_EEPROM:
12491 case FLASH_5717VENDOR_MICRO_EEPROM:
12492 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012493 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012494 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12495
12496 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12497 tw32(NVRAM_CFG1, nvcfg1);
12498 return;
12499 case FLASH_5717VENDOR_ATMEL_MDB011D:
12500 case FLASH_5717VENDOR_ATMEL_ADB011B:
12501 case FLASH_5717VENDOR_ATMEL_ADB011D:
12502 case FLASH_5717VENDOR_ATMEL_MDB021D:
12503 case FLASH_5717VENDOR_ATMEL_ADB021B:
12504 case FLASH_5717VENDOR_ATMEL_ADB021D:
12505 case FLASH_5717VENDOR_ATMEL_45USPT:
12506 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012507 tg3_flag_set(tp, NVRAM_BUFFERED);
12508 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012509
12510 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12511 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012512 /* Detect size with tg3_nvram_get_size() */
12513 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012514 case FLASH_5717VENDOR_ATMEL_ADB021B:
12515 case FLASH_5717VENDOR_ATMEL_ADB021D:
12516 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12517 break;
12518 default:
12519 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12520 break;
12521 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012522 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012523 case FLASH_5717VENDOR_ST_M_M25PE10:
12524 case FLASH_5717VENDOR_ST_A_M25PE10:
12525 case FLASH_5717VENDOR_ST_M_M45PE10:
12526 case FLASH_5717VENDOR_ST_A_M45PE10:
12527 case FLASH_5717VENDOR_ST_M_M25PE20:
12528 case FLASH_5717VENDOR_ST_A_M25PE20:
12529 case FLASH_5717VENDOR_ST_M_M45PE20:
12530 case FLASH_5717VENDOR_ST_A_M45PE20:
12531 case FLASH_5717VENDOR_ST_25USPT:
12532 case FLASH_5717VENDOR_ST_45USPT:
12533 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012534 tg3_flag_set(tp, NVRAM_BUFFERED);
12535 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012536
12537 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12538 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012539 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012540 /* Detect size with tg3_nvram_get_size() */
12541 break;
12542 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012543 case FLASH_5717VENDOR_ST_A_M45PE20:
12544 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12545 break;
12546 default:
12547 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12548 break;
12549 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012550 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012551 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012552 tg3_flag_set(tp, NO_NVRAM);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012553 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080012554 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000012555
12556 tg3_nvram_get_pagesize(tp, nvcfg1);
12557 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012558 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson321d32a2008-11-21 17:22:19 -080012559}
12560
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012561static void __devinit tg3_get_5720_nvram_info(struct tg3 *tp)
12562{
12563 u32 nvcfg1, nvmpinstrp;
12564
12565 nvcfg1 = tr32(NVRAM_CFG1);
12566 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
12567
12568 switch (nvmpinstrp) {
12569 case FLASH_5720_EEPROM_HD:
12570 case FLASH_5720_EEPROM_LD:
12571 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012572 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012573
12574 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12575 tw32(NVRAM_CFG1, nvcfg1);
12576 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
12577 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12578 else
12579 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
12580 return;
12581 case FLASH_5720VENDOR_M_ATMEL_DB011D:
12582 case FLASH_5720VENDOR_A_ATMEL_DB011B:
12583 case FLASH_5720VENDOR_A_ATMEL_DB011D:
12584 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12585 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12586 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12587 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12588 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12589 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12590 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12591 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12592 case FLASH_5720VENDOR_ATMEL_45USPT:
12593 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012594 tg3_flag_set(tp, NVRAM_BUFFERED);
12595 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012596
12597 switch (nvmpinstrp) {
12598 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12599 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12600 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12601 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12602 break;
12603 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12604 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12605 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12606 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12607 break;
12608 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12609 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12610 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12611 break;
12612 default:
12613 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12614 break;
12615 }
12616 break;
12617 case FLASH_5720VENDOR_M_ST_M25PE10:
12618 case FLASH_5720VENDOR_M_ST_M45PE10:
12619 case FLASH_5720VENDOR_A_ST_M25PE10:
12620 case FLASH_5720VENDOR_A_ST_M45PE10:
12621 case FLASH_5720VENDOR_M_ST_M25PE20:
12622 case FLASH_5720VENDOR_M_ST_M45PE20:
12623 case FLASH_5720VENDOR_A_ST_M25PE20:
12624 case FLASH_5720VENDOR_A_ST_M45PE20:
12625 case FLASH_5720VENDOR_M_ST_M25PE40:
12626 case FLASH_5720VENDOR_M_ST_M45PE40:
12627 case FLASH_5720VENDOR_A_ST_M25PE40:
12628 case FLASH_5720VENDOR_A_ST_M45PE40:
12629 case FLASH_5720VENDOR_M_ST_M25PE80:
12630 case FLASH_5720VENDOR_M_ST_M45PE80:
12631 case FLASH_5720VENDOR_A_ST_M25PE80:
12632 case FLASH_5720VENDOR_A_ST_M45PE80:
12633 case FLASH_5720VENDOR_ST_25USPT:
12634 case FLASH_5720VENDOR_ST_45USPT:
12635 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012636 tg3_flag_set(tp, NVRAM_BUFFERED);
12637 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012638
12639 switch (nvmpinstrp) {
12640 case FLASH_5720VENDOR_M_ST_M25PE20:
12641 case FLASH_5720VENDOR_M_ST_M45PE20:
12642 case FLASH_5720VENDOR_A_ST_M25PE20:
12643 case FLASH_5720VENDOR_A_ST_M45PE20:
12644 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12645 break;
12646 case FLASH_5720VENDOR_M_ST_M25PE40:
12647 case FLASH_5720VENDOR_M_ST_M45PE40:
12648 case FLASH_5720VENDOR_A_ST_M25PE40:
12649 case FLASH_5720VENDOR_A_ST_M45PE40:
12650 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12651 break;
12652 case FLASH_5720VENDOR_M_ST_M25PE80:
12653 case FLASH_5720VENDOR_M_ST_M45PE80:
12654 case FLASH_5720VENDOR_A_ST_M25PE80:
12655 case FLASH_5720VENDOR_A_ST_M45PE80:
12656 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12657 break;
12658 default:
12659 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12660 break;
12661 }
12662 break;
12663 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012664 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012665 return;
12666 }
12667
12668 tg3_nvram_get_pagesize(tp, nvcfg1);
12669 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012670 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012671}
12672
Linus Torvalds1da177e2005-04-16 15:20:36 -070012673/* Chips other than 5700/5701 use the NVRAM for fetching info. */
12674static void __devinit tg3_nvram_init(struct tg3 *tp)
12675{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012676 tw32_f(GRC_EEPROM_ADDR,
12677 (EEPROM_ADDR_FSM_RESET |
12678 (EEPROM_DEFAULT_CLOCK_PERIOD <<
12679 EEPROM_ADDR_CLKPERD_SHIFT)));
12680
Michael Chan9d57f012006-12-07 00:23:25 -080012681 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012682
12683 /* Enable seeprom accesses. */
12684 tw32_f(GRC_LOCAL_CTRL,
12685 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
12686 udelay(100);
12687
12688 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
12689 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
Joe Perches63c3a662011-04-26 08:12:10 +000012690 tg3_flag_set(tp, NVRAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012691
Michael Chanec41c7d2006-01-17 02:40:55 -080012692 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000012693 netdev_warn(tp->dev,
12694 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000012695 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080012696 return;
12697 }
Michael Chane6af3012005-04-21 17:12:05 -070012698 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012699
Matt Carlson989a9d22007-05-05 11:51:05 -070012700 tp->nvram_size = 0;
12701
Michael Chan361b4ac2005-04-21 17:11:21 -070012702 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
12703 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080012704 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
12705 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070012706 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070012707 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
12708 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080012709 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070012710 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
12711 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070012712 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12713 tg3_get_5906_nvram_info(tp);
Matt Carlsonb703df62009-12-03 08:36:21 +000012714 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000012715 tg3_flag(tp, 57765_CLASS))
Matt Carlson321d32a2008-11-21 17:22:19 -080012716 tg3_get_57780_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012717 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
12718 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000012719 tg3_get_5717_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012720 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
12721 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070012722 else
12723 tg3_get_nvram_info(tp);
12724
Matt Carlson989a9d22007-05-05 11:51:05 -070012725 if (tp->nvram_size == 0)
12726 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012727
Michael Chane6af3012005-04-21 17:12:05 -070012728 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080012729 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012730
12731 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000012732 tg3_flag_clear(tp, NVRAM);
12733 tg3_flag_clear(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012734
12735 tg3_get_eeprom_size(tp);
12736 }
12737}
12738
Linus Torvalds1da177e2005-04-16 15:20:36 -070012739static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
12740 u32 offset, u32 len, u8 *buf)
12741{
12742 int i, j, rc = 0;
12743 u32 val;
12744
12745 for (i = 0; i < len; i += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012746 u32 addr;
Matt Carlsona9dc5292009-02-25 14:25:30 +000012747 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012748
12749 addr = offset + i;
12750
12751 memcpy(&data, buf + i, 4);
12752
Matt Carlson62cedd12009-04-20 14:52:29 -070012753 /*
12754 * The SEEPROM interface expects the data to always be opposite
12755 * the native endian format. We accomplish this by reversing
12756 * all the operations that would have been performed on the
12757 * data from a call to tg3_nvram_read_be32().
12758 */
12759 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012760
12761 val = tr32(GRC_EEPROM_ADDR);
12762 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
12763
12764 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
12765 EEPROM_ADDR_READ);
12766 tw32(GRC_EEPROM_ADDR, val |
12767 (0 << EEPROM_ADDR_DEVID_SHIFT) |
12768 (addr & EEPROM_ADDR_ADDR_MASK) |
12769 EEPROM_ADDR_START |
12770 EEPROM_ADDR_WRITE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012771
Michael Chan9d57f012006-12-07 00:23:25 -080012772 for (j = 0; j < 1000; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012773 val = tr32(GRC_EEPROM_ADDR);
12774
12775 if (val & EEPROM_ADDR_COMPLETE)
12776 break;
Michael Chan9d57f012006-12-07 00:23:25 -080012777 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012778 }
12779 if (!(val & EEPROM_ADDR_COMPLETE)) {
12780 rc = -EBUSY;
12781 break;
12782 }
12783 }
12784
12785 return rc;
12786}
12787
12788/* offset and length are dword aligned */
12789static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
12790 u8 *buf)
12791{
12792 int ret = 0;
12793 u32 pagesize = tp->nvram_pagesize;
12794 u32 pagemask = pagesize - 1;
12795 u32 nvram_cmd;
12796 u8 *tmp;
12797
12798 tmp = kmalloc(pagesize, GFP_KERNEL);
12799 if (tmp == NULL)
12800 return -ENOMEM;
12801
12802 while (len) {
12803 int j;
Michael Chane6af3012005-04-21 17:12:05 -070012804 u32 phy_addr, page_off, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012805
12806 phy_addr = offset & ~pagemask;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012807
Linus Torvalds1da177e2005-04-16 15:20:36 -070012808 for (j = 0; j < pagesize; j += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012809 ret = tg3_nvram_read_be32(tp, phy_addr + j,
12810 (__be32 *) (tmp + j));
12811 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012812 break;
12813 }
12814 if (ret)
12815 break;
12816
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012817 page_off = offset & pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012818 size = pagesize;
12819 if (len < size)
12820 size = len;
12821
12822 len -= size;
12823
12824 memcpy(tmp + page_off, buf, size);
12825
12826 offset = offset + (pagesize - page_off);
12827
Michael Chane6af3012005-04-21 17:12:05 -070012828 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012829
12830 /*
12831 * Before we can erase the flash page, we need
12832 * to issue a special "write enable" command.
12833 */
12834 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12835
12836 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12837 break;
12838
12839 /* Erase the target page */
12840 tw32(NVRAM_ADDR, phy_addr);
12841
12842 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
12843 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
12844
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012845 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012846 break;
12847
12848 /* Issue another write enable to start the write. */
12849 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12850
12851 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12852 break;
12853
12854 for (j = 0; j < pagesize; j += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012855 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012856
Al Virob9fc7dc2007-12-17 22:59:57 -080012857 data = *((__be32 *) (tmp + j));
Matt Carlsona9dc5292009-02-25 14:25:30 +000012858
Al Virob9fc7dc2007-12-17 22:59:57 -080012859 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012860
12861 tw32(NVRAM_ADDR, phy_addr + j);
12862
12863 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
12864 NVRAM_CMD_WR;
12865
12866 if (j == 0)
12867 nvram_cmd |= NVRAM_CMD_FIRST;
12868 else if (j == (pagesize - 4))
12869 nvram_cmd |= NVRAM_CMD_LAST;
12870
12871 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12872 break;
12873 }
12874 if (ret)
12875 break;
12876 }
12877
12878 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12879 tg3_nvram_exec_cmd(tp, nvram_cmd);
12880
12881 kfree(tmp);
12882
12883 return ret;
12884}
12885
12886/* offset and length are dword aligned */
12887static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
12888 u8 *buf)
12889{
12890 int i, ret = 0;
12891
12892 for (i = 0; i < len; i += 4, offset += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012893 u32 page_off, phy_addr, nvram_cmd;
12894 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012895
12896 memcpy(&data, buf + i, 4);
Al Virob9fc7dc2007-12-17 22:59:57 -080012897 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012898
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012899 page_off = offset % tp->nvram_pagesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012900
Michael Chan18201802006-03-20 22:29:15 -080012901 phy_addr = tg3_nvram_phys_addr(tp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012902
12903 tw32(NVRAM_ADDR, phy_addr);
12904
12905 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
12906
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012907 if (page_off == 0 || i == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012908 nvram_cmd |= NVRAM_CMD_FIRST;
Michael Chanf6d9a252006-04-29 19:00:24 -070012909 if (page_off == (tp->nvram_pagesize - 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012910 nvram_cmd |= NVRAM_CMD_LAST;
12911
12912 if (i == (len - 4))
12913 nvram_cmd |= NVRAM_CMD_LAST;
12914
Matt Carlson321d32a2008-11-21 17:22:19 -080012915 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
Joe Perches63c3a662011-04-26 08:12:10 +000012916 !tg3_flag(tp, 5755_PLUS) &&
Michael Chan4c987482005-09-05 17:52:38 -070012917 (tp->nvram_jedecnum == JEDEC_ST) &&
12918 (nvram_cmd & NVRAM_CMD_FIRST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012919
12920 if ((ret = tg3_nvram_exec_cmd(tp,
12921 NVRAM_CMD_WREN | NVRAM_CMD_GO |
12922 NVRAM_CMD_DONE)))
12923
12924 break;
12925 }
Joe Perches63c3a662011-04-26 08:12:10 +000012926 if (!tg3_flag(tp, FLASH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012927 /* We always do complete word writes to eeprom. */
12928 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
12929 }
12930
12931 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12932 break;
12933 }
12934 return ret;
12935}
12936
12937/* offset and length are dword aligned */
12938static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
12939{
12940 int ret;
12941
Joe Perches63c3a662011-04-26 08:12:10 +000012942 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
Michael Chan314fba32005-04-21 17:07:04 -070012943 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
12944 ~GRC_LCLCTRL_GPIO_OUTPUT1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012945 udelay(40);
12946 }
12947
Joe Perches63c3a662011-04-26 08:12:10 +000012948 if (!tg3_flag(tp, NVRAM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012949 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
Matt Carlson859a588792010-04-05 10:19:28 +000012950 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012951 u32 grc_mode;
12952
Michael Chanec41c7d2006-01-17 02:40:55 -080012953 ret = tg3_nvram_lock(tp);
12954 if (ret)
12955 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012956
Michael Chane6af3012005-04-21 17:12:05 -070012957 tg3_enable_nvram_access(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000012958 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012959 tw32(NVRAM_WRITE1, 0x406);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012960
12961 grc_mode = tr32(GRC_MODE);
12962 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
12963
Joe Perches63c3a662011-04-26 08:12:10 +000012964 if (tg3_flag(tp, NVRAM_BUFFERED) || !tg3_flag(tp, FLASH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012965 ret = tg3_nvram_write_block_buffered(tp, offset, len,
12966 buf);
Matt Carlson859a588792010-04-05 10:19:28 +000012967 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012968 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
12969 buf);
12970 }
12971
12972 grc_mode = tr32(GRC_MODE);
12973 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
12974
Michael Chane6af3012005-04-21 17:12:05 -070012975 tg3_disable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012976 tg3_nvram_unlock(tp);
12977 }
12978
Joe Perches63c3a662011-04-26 08:12:10 +000012979 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
Michael Chan314fba32005-04-21 17:07:04 -070012980 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012981 udelay(40);
12982 }
12983
12984 return ret;
12985}
12986
12987struct subsys_tbl_ent {
12988 u16 subsys_vendor, subsys_devid;
12989 u32 phy_id;
12990};
12991
Matt Carlson24daf2b2010-02-17 15:17:02 +000012992static struct subsys_tbl_ent subsys_id_to_phy_id[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012993 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012994 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012995 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012996 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012997 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012998 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012999 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013000 { TG3PCI_SUBVENDOR_ID_BROADCOM,
13001 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
13002 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013003 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013004 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013005 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013006 { TG3PCI_SUBVENDOR_ID_BROADCOM,
13007 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
13008 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013009 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013010 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013011 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013012 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013013 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013014 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013015 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013016
13017 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013018 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013019 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013020 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013021 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013022 { TG3PCI_SUBVENDOR_ID_3COM,
13023 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
13024 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013025 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013026 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013027 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013028
13029 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013030 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013031 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013032 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013033 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013034 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013035 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013036 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013037 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013038
13039 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013040 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013041 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013042 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013043 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013044 { TG3PCI_SUBVENDOR_ID_COMPAQ,
13045 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
13046 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013047 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013048 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013049 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013050
13051 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013052 { TG3PCI_SUBVENDOR_ID_IBM,
13053 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013054};
13055
Matt Carlson24daf2b2010-02-17 15:17:02 +000013056static struct subsys_tbl_ent * __devinit tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013057{
13058 int i;
13059
13060 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
13061 if ((subsys_id_to_phy_id[i].subsys_vendor ==
13062 tp->pdev->subsystem_vendor) &&
13063 (subsys_id_to_phy_id[i].subsys_devid ==
13064 tp->pdev->subsystem_device))
13065 return &subsys_id_to_phy_id[i];
13066 }
13067 return NULL;
13068}
13069
Michael Chan7d0c41e2005-04-21 17:06:20 -070013070static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013071{
Linus Torvalds1da177e2005-04-16 15:20:36 -070013072 u32 val;
David S. Millerf49639e2006-06-09 11:58:36 -070013073
Matt Carlson79eb6902010-02-17 15:17:03 +000013074 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070013075 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13076
Gary Zambranoa85feb82007-05-05 11:52:19 -070013077 /* Assume an onboard device and WOL capable by default. */
Joe Perches63c3a662011-04-26 08:12:10 +000013078 tg3_flag_set(tp, EEPROM_WRITE_PROT);
13079 tg3_flag_set(tp, WOL_CAP);
David S. Miller72b845e2006-03-14 14:11:48 -080013080
Michael Chanb5d37722006-09-27 16:06:21 -070013081 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080013082 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013083 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
13084 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080013085 }
Matt Carlson0527ba32007-10-10 18:03:30 -070013086 val = tr32(VCPU_CFGSHDW);
13087 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Joe Perches63c3a662011-04-26 08:12:10 +000013088 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson0527ba32007-10-10 18:03:30 -070013089 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013090 (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013091 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013092 device_set_wakeup_enable(&tp->pdev->dev, true);
13093 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080013094 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070013095 }
13096
Linus Torvalds1da177e2005-04-16 15:20:36 -070013097 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
13098 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
13099 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070013100 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070013101 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013102
13103 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
13104 tp->nic_sram_data_cfg = nic_cfg;
13105
13106 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
13107 ver >>= NIC_SRAM_DATA_VER_SHIFT;
Matt Carlson6ff6f812011-05-19 12:12:54 +000013108 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13109 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13110 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070013111 (ver > 0) && (ver < 0x100))
13112 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
13113
Matt Carlsona9daf362008-05-25 23:49:44 -070013114 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
13115 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
13116
Linus Torvalds1da177e2005-04-16 15:20:36 -070013117 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
13118 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
13119 eeprom_phy_serdes = 1;
13120
13121 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
13122 if (nic_phy_id != 0) {
13123 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
13124 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
13125
13126 eeprom_phy_id = (id1 >> 16) << 10;
13127 eeprom_phy_id |= (id2 & 0xfc00) << 16;
13128 eeprom_phy_id |= (id2 & 0x03ff) << 0;
13129 } else
13130 eeprom_phy_id = 0;
13131
Michael Chan7d0c41e2005-04-21 17:06:20 -070013132 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070013133 if (eeprom_phy_serdes) {
Joe Perches63c3a662011-04-26 08:12:10 +000013134 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013135 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000013136 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013137 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070013138 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070013139
Joe Perches63c3a662011-04-26 08:12:10 +000013140 if (tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013141 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
13142 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070013143 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070013144 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
13145
13146 switch (led_cfg) {
13147 default:
13148 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
13149 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13150 break;
13151
13152 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
13153 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
13154 break;
13155
13156 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
13157 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070013158
13159 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
13160 * read on some older 5700/5701 bootcode.
13161 */
13162 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
13163 ASIC_REV_5700 ||
13164 GET_ASIC_REV(tp->pci_chip_rev_id) ==
13165 ASIC_REV_5701)
13166 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13167
Linus Torvalds1da177e2005-04-16 15:20:36 -070013168 break;
13169
13170 case SHASTA_EXT_LED_SHARED:
13171 tp->led_ctrl = LED_CTRL_MODE_SHARED;
13172 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
13173 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
13174 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
13175 LED_CTRL_MODE_PHY_2);
13176 break;
13177
13178 case SHASTA_EXT_LED_MAC:
13179 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
13180 break;
13181
13182 case SHASTA_EXT_LED_COMBO:
13183 tp->led_ctrl = LED_CTRL_MODE_COMBO;
13184 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
13185 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
13186 LED_CTRL_MODE_PHY_2);
13187 break;
13188
Stephen Hemminger855e1112008-04-16 16:37:28 -070013189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013190
13191 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13192 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
13193 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
13194 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
13195
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013196 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
13197 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080013198
Michael Chan9d26e212006-12-07 00:21:14 -080013199 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Joe Perches63c3a662011-04-26 08:12:10 +000013200 tg3_flag_set(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080013201 if ((tp->pdev->subsystem_vendor ==
13202 PCI_VENDOR_ID_ARIMA) &&
13203 (tp->pdev->subsystem_device == 0x205a ||
13204 tp->pdev->subsystem_device == 0x2063))
Joe Perches63c3a662011-04-26 08:12:10 +000013205 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080013206 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000013207 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
13208 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080013209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013210
13211 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +000013212 tg3_flag_set(tp, ENABLE_ASF);
13213 if (tg3_flag(tp, 5750_PLUS))
13214 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013215 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080013216
13217 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013218 tg3_flag(tp, 5750_PLUS))
13219 tg3_flag_set(tp, ENABLE_APE);
Matt Carlsonb2b98d42008-11-03 16:52:32 -080013220
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013221 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070013222 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
Joe Perches63c3a662011-04-26 08:12:10 +000013223 tg3_flag_clear(tp, WOL_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013224
Joe Perches63c3a662011-04-26 08:12:10 +000013225 if (tg3_flag(tp, WOL_CAP) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013226 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013227 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013228 device_set_wakeup_enable(&tp->pdev->dev, true);
13229 }
Matt Carlson0527ba32007-10-10 18:03:30 -070013230
Linus Torvalds1da177e2005-04-16 15:20:36 -070013231 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013232 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013233
13234 /* serdes signal pre-emphasis in register 0x590 set by */
13235 /* bootcode if bit 18 is set */
13236 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013237 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070013238
Joe Perches63c3a662011-04-26 08:12:10 +000013239 if ((tg3_flag(tp, 57765_PLUS) ||
13240 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
13241 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080013242 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013243 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080013244
Joe Perches63c3a662011-04-26 08:12:10 +000013245 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson8c69b1e2010-08-02 11:26:00 +000013246 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +000013247 !tg3_flag(tp, 57765_PLUS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070013248 u32 cfg3;
13249
13250 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
13251 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
Joe Perches63c3a662011-04-26 08:12:10 +000013252 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson8ed5d972007-05-07 00:25:49 -070013253 }
Matt Carlsona9daf362008-05-25 23:49:44 -070013254
Matt Carlson14417062010-02-17 15:16:59 +000013255 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
Joe Perches63c3a662011-04-26 08:12:10 +000013256 tg3_flag_set(tp, RGMII_INBAND_DISABLE);
Matt Carlsona9daf362008-05-25 23:49:44 -070013257 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000013258 tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
Matt Carlsona9daf362008-05-25 23:49:44 -070013259 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000013260 tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013261 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080013262done:
Joe Perches63c3a662011-04-26 08:12:10 +000013263 if (tg3_flag(tp, WOL_CAP))
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000013264 device_set_wakeup_enable(&tp->pdev->dev,
Joe Perches63c3a662011-04-26 08:12:10 +000013265 tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000013266 else
13267 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070013268}
13269
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013270static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
13271{
13272 int i;
13273 u32 val;
13274
13275 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
13276 tw32(OTP_CTRL, cmd);
13277
13278 /* Wait for up to 1 ms for command to execute. */
13279 for (i = 0; i < 100; i++) {
13280 val = tr32(OTP_STATUS);
13281 if (val & OTP_STATUS_CMD_DONE)
13282 break;
13283 udelay(10);
13284 }
13285
13286 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
13287}
13288
13289/* Read the gphy configuration from the OTP region of the chip. The gphy
13290 * configuration is a 32-bit value that straddles the alignment boundary.
13291 * We do two 32-bit reads and then shift and merge the results.
13292 */
13293static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
13294{
13295 u32 bhalf_otp, thalf_otp;
13296
13297 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
13298
13299 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
13300 return 0;
13301
13302 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
13303
13304 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
13305 return 0;
13306
13307 thalf_otp = tr32(OTP_READ_DATA);
13308
13309 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
13310
13311 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
13312 return 0;
13313
13314 bhalf_otp = tr32(OTP_READ_DATA);
13315
13316 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
13317}
13318
Matt Carlsone256f8a2011-03-09 16:58:24 +000013319static void __devinit tg3_phy_init_link_config(struct tg3 *tp)
13320{
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +000013321 u32 adv = ADVERTISED_Autoneg;
Matt Carlsone256f8a2011-03-09 16:58:24 +000013322
13323 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
13324 adv |= ADVERTISED_1000baseT_Half |
13325 ADVERTISED_1000baseT_Full;
13326
13327 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
13328 adv |= ADVERTISED_100baseT_Half |
13329 ADVERTISED_100baseT_Full |
13330 ADVERTISED_10baseT_Half |
13331 ADVERTISED_10baseT_Full |
13332 ADVERTISED_TP;
13333 else
13334 adv |= ADVERTISED_FIBRE;
13335
13336 tp->link_config.advertising = adv;
13337 tp->link_config.speed = SPEED_INVALID;
13338 tp->link_config.duplex = DUPLEX_INVALID;
13339 tp->link_config.autoneg = AUTONEG_ENABLE;
13340 tp->link_config.active_speed = SPEED_INVALID;
13341 tp->link_config.active_duplex = DUPLEX_INVALID;
13342 tp->link_config.orig_speed = SPEED_INVALID;
13343 tp->link_config.orig_duplex = DUPLEX_INVALID;
13344 tp->link_config.orig_autoneg = AUTONEG_INVALID;
13345}
13346
Michael Chan7d0c41e2005-04-21 17:06:20 -070013347static int __devinit tg3_phy_probe(struct tg3 *tp)
13348{
13349 u32 hw_phy_id_1, hw_phy_id_2;
13350 u32 hw_phy_id, hw_phy_id_masked;
13351 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013352
Matt Carlsone256f8a2011-03-09 16:58:24 +000013353 /* flow control autonegotiation is default behavior */
Joe Perches63c3a662011-04-26 08:12:10 +000013354 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsone256f8a2011-03-09 16:58:24 +000013355 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
13356
Joe Perches63c3a662011-04-26 08:12:10 +000013357 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013358 return tg3_phy_init(tp);
13359
Linus Torvalds1da177e2005-04-16 15:20:36 -070013360 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010013361 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013362 */
13363 err = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000013364 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000013365 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013366 } else {
13367 /* Now read the physical PHY_ID from the chip and verify
13368 * that it is sane. If it doesn't look good, we fall back
13369 * to either the hard-coded table based PHY_ID and failing
13370 * that the value found in the eeprom area.
13371 */
13372 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
13373 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
13374
13375 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
13376 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
13377 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
13378
Matt Carlson79eb6902010-02-17 15:17:03 +000013379 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013380 }
13381
Matt Carlson79eb6902010-02-17 15:17:03 +000013382 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013383 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000013384 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013385 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070013386 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013387 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013388 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000013389 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070013390 /* Do nothing, phy ID already set up in
13391 * tg3_get_eeprom_hw_cfg().
13392 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070013393 } else {
13394 struct subsys_tbl_ent *p;
13395
13396 /* No eeprom signature? Try the hardcoded
13397 * subsys device table.
13398 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013399 p = tg3_lookup_by_subsys(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013400 if (!p)
13401 return -ENODEV;
13402
13403 tp->phy_id = p->phy_id;
13404 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000013405 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013406 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013407 }
13408 }
13409
Matt Carlsona6b68da2010-12-06 08:28:52 +000013410 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Matt Carlson5baa5e92011-07-20 10:20:53 +000013411 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13412 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720 ||
13413 (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +000013414 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0) ||
13415 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
13416 tp->pci_chip_rev_id != CHIPREV_ID_57765_A0)))
Matt Carlson52b02d02010-10-14 10:37:41 +000013417 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
13418
Matt Carlsone256f8a2011-03-09 16:58:24 +000013419 tg3_phy_init_link_config(tp);
13420
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013421 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013422 !tg3_flag(tp, ENABLE_APE) &&
13423 !tg3_flag(tp, ENABLE_ASF)) {
Matt Carlsone2bf73e2011-12-08 14:40:15 +000013424 u32 bmsr, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013425
13426 tg3_readphy(tp, MII_BMSR, &bmsr);
13427 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
13428 (bmsr & BMSR_LSTATUS))
13429 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013430
Linus Torvalds1da177e2005-04-16 15:20:36 -070013431 err = tg3_phy_reset(tp);
13432 if (err)
13433 return err;
13434
Matt Carlson42b64a42011-05-19 12:12:49 +000013435 tg3_phy_set_wirespeed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013436
Matt Carlsone2bf73e2011-12-08 14:40:15 +000013437 if (!tg3_phy_copper_an_config_ok(tp, &dummy)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000013438 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
13439 tp->link_config.flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013440
13441 tg3_writephy(tp, MII_BMCR,
13442 BMCR_ANENABLE | BMCR_ANRESTART);
13443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013444 }
13445
13446skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000013447 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013448 err = tg3_init_5401phy_dsp(tp);
13449 if (err)
13450 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013451
Linus Torvalds1da177e2005-04-16 15:20:36 -070013452 err = tg3_init_5401phy_dsp(tp);
13453 }
13454
Linus Torvalds1da177e2005-04-16 15:20:36 -070013455 return err;
13456}
13457
Matt Carlson184b8902010-04-05 10:19:25 +000013458static void __devinit tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013459{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013460 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013461 unsigned int block_end, rosize, len;
Matt Carlson535a4902011-07-20 10:20:56 +000013462 u32 vpdlen;
Matt Carlson184b8902010-04-05 10:19:25 +000013463 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013464
Matt Carlson535a4902011-07-20 10:20:56 +000013465 vpd_data = (u8 *)tg3_vpd_readblock(tp, &vpdlen);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013466 if (!vpd_data)
13467 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013468
Matt Carlson535a4902011-07-20 10:20:56 +000013469 i = pci_vpd_find_tag(vpd_data, 0, vpdlen, PCI_VPD_LRDT_RO_DATA);
Matt Carlson4181b2c2010-02-26 14:04:45 +000013470 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013471 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013472
13473 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
13474 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
13475 i += PCI_VPD_LRDT_TAG_SIZE;
13476
Matt Carlson535a4902011-07-20 10:20:56 +000013477 if (block_end > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000013478 goto out_not_found;
13479
Matt Carlson184b8902010-04-05 10:19:25 +000013480 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13481 PCI_VPD_RO_KEYWORD_MFR_ID);
13482 if (j > 0) {
13483 len = pci_vpd_info_field_size(&vpd_data[j]);
13484
13485 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13486 if (j + len > block_end || len != 4 ||
13487 memcmp(&vpd_data[j], "1028", 4))
13488 goto partno;
13489
13490 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13491 PCI_VPD_RO_KEYWORD_VENDOR0);
13492 if (j < 0)
13493 goto partno;
13494
13495 len = pci_vpd_info_field_size(&vpd_data[j]);
13496
13497 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13498 if (j + len > block_end)
13499 goto partno;
13500
13501 memcpy(tp->fw_ver, &vpd_data[j], len);
Matt Carlson535a4902011-07-20 10:20:56 +000013502 strncat(tp->fw_ver, " bc ", vpdlen - len - 1);
Matt Carlson184b8902010-04-05 10:19:25 +000013503 }
13504
13505partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000013506 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13507 PCI_VPD_RO_KEYWORD_PARTNO);
13508 if (i < 0)
13509 goto out_not_found;
13510
13511 len = pci_vpd_info_field_size(&vpd_data[i]);
13512
13513 i += PCI_VPD_INFO_FLD_HDR_SIZE;
13514 if (len > TG3_BPN_SIZE ||
Matt Carlson535a4902011-07-20 10:20:56 +000013515 (len + i) > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000013516 goto out_not_found;
13517
13518 memcpy(tp->board_part_number, &vpd_data[i], len);
13519
Linus Torvalds1da177e2005-04-16 15:20:36 -070013520out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013521 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000013522 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013523 return;
13524
13525out_no_vpd:
Matt Carlson37a949c2010-09-30 10:34:33 +000013526 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
13527 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717)
13528 strcpy(tp->board_part_number, "BCM5717");
13529 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
13530 strcpy(tp->board_part_number, "BCM5718");
13531 else
13532 goto nomatch;
13533 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
13534 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
13535 strcpy(tp->board_part_number, "BCM57780");
13536 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
13537 strcpy(tp->board_part_number, "BCM57760");
13538 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
13539 strcpy(tp->board_part_number, "BCM57790");
13540 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
13541 strcpy(tp->board_part_number, "BCM57788");
13542 else
13543 goto nomatch;
13544 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
13545 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
13546 strcpy(tp->board_part_number, "BCM57761");
13547 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
13548 strcpy(tp->board_part_number, "BCM57765");
13549 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
13550 strcpy(tp->board_part_number, "BCM57781");
13551 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
13552 strcpy(tp->board_part_number, "BCM57785");
13553 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
13554 strcpy(tp->board_part_number, "BCM57791");
13555 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13556 strcpy(tp->board_part_number, "BCM57795");
13557 else
13558 goto nomatch;
Matt Carlson55086ad2011-12-14 11:09:59 +000013559 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57766) {
13560 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762)
13561 strcpy(tp->board_part_number, "BCM57762");
13562 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766)
13563 strcpy(tp->board_part_number, "BCM57766");
13564 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782)
13565 strcpy(tp->board_part_number, "BCM57782");
13566 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
13567 strcpy(tp->board_part_number, "BCM57786");
13568 else
13569 goto nomatch;
Matt Carlson37a949c2010-09-30 10:34:33 +000013570 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070013571 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000013572 } else {
13573nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070013574 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000013575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013576}
13577
Matt Carlson9c8a6202007-10-21 16:16:08 -070013578static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
13579{
13580 u32 val;
13581
Matt Carlsone4f34112009-02-25 14:25:00 +000013582 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013583 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013584 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013585 val != 0)
13586 return 0;
13587
13588 return 1;
13589}
13590
Matt Carlsonacd9c112009-02-25 14:26:33 +000013591static void __devinit tg3_read_bc_ver(struct tg3 *tp)
13592{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013593 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000013594 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013595 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013596
13597 if (tg3_nvram_read(tp, 0xc, &offset) ||
13598 tg3_nvram_read(tp, 0x4, &start))
13599 return;
13600
13601 offset = tg3_nvram_logical_addr(tp, offset);
13602
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013603 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013604 return;
13605
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013606 if ((val & 0xfc000000) == 0x0c000000) {
13607 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013608 return;
13609
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013610 if (val == 0)
13611 newver = true;
13612 }
13613
Matt Carlson75f99362010-04-05 10:19:24 +000013614 dst_off = strlen(tp->fw_ver);
13615
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013616 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000013617 if (TG3_VER_SIZE - dst_off < 16 ||
13618 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013619 return;
13620
13621 offset = offset + ver_offset - start;
13622 for (i = 0; i < 16; i += 4) {
13623 __be32 v;
13624 if (tg3_nvram_read_be32(tp, offset + i, &v))
13625 return;
13626
Matt Carlson75f99362010-04-05 10:19:24 +000013627 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013628 }
13629 } else {
13630 u32 major, minor;
13631
13632 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
13633 return;
13634
13635 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
13636 TG3_NVM_BCVER_MAJSFT;
13637 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000013638 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
13639 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013640 }
13641}
13642
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013643static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
13644{
13645 u32 val, major, minor;
13646
13647 /* Use native endian representation */
13648 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
13649 return;
13650
13651 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
13652 TG3_NVM_HWSB_CFG1_MAJSFT;
13653 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
13654 TG3_NVM_HWSB_CFG1_MINSFT;
13655
13656 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
13657}
13658
Matt Carlsondfe00d72008-11-21 17:19:41 -080013659static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
13660{
13661 u32 offset, major, minor, build;
13662
Matt Carlson75f99362010-04-05 10:19:24 +000013663 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013664
13665 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
13666 return;
13667
13668 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
13669 case TG3_EEPROM_SB_REVISION_0:
13670 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
13671 break;
13672 case TG3_EEPROM_SB_REVISION_2:
13673 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
13674 break;
13675 case TG3_EEPROM_SB_REVISION_3:
13676 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
13677 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000013678 case TG3_EEPROM_SB_REVISION_4:
13679 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
13680 break;
13681 case TG3_EEPROM_SB_REVISION_5:
13682 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
13683 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000013684 case TG3_EEPROM_SB_REVISION_6:
13685 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
13686 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013687 default:
13688 return;
13689 }
13690
Matt Carlsone4f34112009-02-25 14:25:00 +000013691 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080013692 return;
13693
13694 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
13695 TG3_EEPROM_SB_EDH_BLD_SHFT;
13696 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
13697 TG3_EEPROM_SB_EDH_MAJ_SHFT;
13698 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
13699
13700 if (minor > 99 || build > 26)
13701 return;
13702
Matt Carlson75f99362010-04-05 10:19:24 +000013703 offset = strlen(tp->fw_ver);
13704 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
13705 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013706
13707 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000013708 offset = strlen(tp->fw_ver);
13709 if (offset < TG3_VER_SIZE - 1)
13710 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013711 }
13712}
13713
Matt Carlsonacd9c112009-02-25 14:26:33 +000013714static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080013715{
13716 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013717 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070013718
13719 for (offset = TG3_NVM_DIR_START;
13720 offset < TG3_NVM_DIR_END;
13721 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000013722 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013723 return;
13724
13725 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
13726 break;
13727 }
13728
13729 if (offset == TG3_NVM_DIR_END)
13730 return;
13731
Joe Perches63c3a662011-04-26 08:12:10 +000013732 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013733 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000013734 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013735 return;
13736
Matt Carlsone4f34112009-02-25 14:25:00 +000013737 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013738 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013739 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013740 return;
13741
13742 offset += val - start;
13743
Matt Carlsonacd9c112009-02-25 14:26:33 +000013744 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013745
Matt Carlsonacd9c112009-02-25 14:26:33 +000013746 tp->fw_ver[vlen++] = ',';
13747 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070013748
13749 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000013750 __be32 v;
13751 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013752 return;
13753
Al Virob9fc7dc2007-12-17 22:59:57 -080013754 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013755
Matt Carlsonacd9c112009-02-25 14:26:33 +000013756 if (vlen > TG3_VER_SIZE - sizeof(v)) {
13757 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013758 break;
13759 }
13760
Matt Carlsonacd9c112009-02-25 14:26:33 +000013761 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
13762 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013763 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000013764}
13765
Matt Carlson7fd76442009-02-25 14:27:20 +000013766static void __devinit tg3_read_dash_ver(struct tg3 *tp)
13767{
13768 int vlen;
13769 u32 apedata;
Matt Carlsonecc79642010-08-02 11:26:01 +000013770 char *fwtype;
Matt Carlson7fd76442009-02-25 14:27:20 +000013771
Joe Perches63c3a662011-04-26 08:12:10 +000013772 if (!tg3_flag(tp, ENABLE_APE) || !tg3_flag(tp, ENABLE_ASF))
Matt Carlson7fd76442009-02-25 14:27:20 +000013773 return;
13774
13775 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
13776 if (apedata != APE_SEG_SIG_MAGIC)
13777 return;
13778
13779 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
13780 if (!(apedata & APE_FW_STATUS_READY))
13781 return;
13782
13783 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
13784
Matt Carlsondc6d0742010-09-15 08:59:55 +000013785 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI) {
Joe Perches63c3a662011-04-26 08:12:10 +000013786 tg3_flag_set(tp, APE_HAS_NCSI);
Matt Carlsonecc79642010-08-02 11:26:01 +000013787 fwtype = "NCSI";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013788 } else {
Matt Carlsonecc79642010-08-02 11:26:01 +000013789 fwtype = "DASH";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013790 }
Matt Carlsonecc79642010-08-02 11:26:01 +000013791
Matt Carlson7fd76442009-02-25 14:27:20 +000013792 vlen = strlen(tp->fw_ver);
13793
Matt Carlsonecc79642010-08-02 11:26:01 +000013794 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
13795 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000013796 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
13797 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
13798 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
13799 (apedata & APE_FW_VERSION_BLDMSK));
13800}
13801
Matt Carlsonacd9c112009-02-25 14:26:33 +000013802static void __devinit tg3_read_fw_ver(struct tg3 *tp)
13803{
13804 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000013805 bool vpd_vers = false;
13806
13807 if (tp->fw_ver[0] != 0)
13808 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013809
Joe Perches63c3a662011-04-26 08:12:10 +000013810 if (tg3_flag(tp, NO_NVRAM)) {
Matt Carlson75f99362010-04-05 10:19:24 +000013811 strcat(tp->fw_ver, "sb");
Matt Carlsondf259d82009-04-20 06:57:14 +000013812 return;
13813 }
13814
Matt Carlsonacd9c112009-02-25 14:26:33 +000013815 if (tg3_nvram_read(tp, 0, &val))
13816 return;
13817
13818 if (val == TG3_EEPROM_MAGIC)
13819 tg3_read_bc_ver(tp);
13820 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
13821 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013822 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
13823 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013824 else
13825 return;
13826
Matt Carlsonc9cab242011-07-13 09:27:27 +000013827 if (vpd_vers)
Matt Carlson75f99362010-04-05 10:19:24 +000013828 goto done;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013829
Matt Carlsonc9cab242011-07-13 09:27:27 +000013830 if (tg3_flag(tp, ENABLE_APE)) {
13831 if (tg3_flag(tp, ENABLE_ASF))
13832 tg3_read_dash_ver(tp);
13833 } else if (tg3_flag(tp, ENABLE_ASF)) {
13834 tg3_read_mgmtfw_ver(tp);
13835 }
Matt Carlson9c8a6202007-10-21 16:16:08 -070013836
Matt Carlson75f99362010-04-05 10:19:24 +000013837done:
Matt Carlson9c8a6202007-10-21 16:16:08 -070013838 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080013839}
13840
Michael Chan7544b092007-05-05 13:08:32 -070013841static struct pci_dev * __devinit tg3_find_peer(struct tg3 *);
13842
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013843static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
13844{
Joe Perches63c3a662011-04-26 08:12:10 +000013845 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013846 return TG3_RX_RET_MAX_SIZE_5717;
Joe Perches63c3a662011-04-26 08:12:10 +000013847 else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013848 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013849 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000013850 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013851}
13852
Matt Carlson41434702011-03-09 16:58:22 +000013853static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080013854 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
13855 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
13856 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
13857 { },
13858};
13859
Linus Torvalds1da177e2005-04-16 15:20:36 -070013860static int __devinit tg3_get_invariants(struct tg3 *tp)
13861{
Linus Torvalds1da177e2005-04-16 15:20:36 -070013862 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013863 u32 pci_state_reg, grc_misc_cfg;
13864 u32 val;
13865 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013866 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013867
Linus Torvalds1da177e2005-04-16 15:20:36 -070013868 /* Force memory write invalidate off. If we leave it on,
13869 * then on 5700_BX chips we have to enable a workaround.
13870 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
13871 * to match the cacheline size. The Broadcom driver have this
13872 * workaround but turns MWI off all the times so never uses
13873 * it. This seems to suggest that the workaround is insufficient.
13874 */
13875 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13876 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
13877 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13878
Matt Carlson16821282011-07-13 09:27:28 +000013879 /* Important! -- Make sure register accesses are byteswapped
13880 * correctly. Also, for those chips that require it, make
13881 * sure that indirect register accesses are enabled before
13882 * the first operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013883 */
13884 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13885 &misc_ctrl_reg);
Matt Carlson16821282011-07-13 09:27:28 +000013886 tp->misc_host_ctrl |= (misc_ctrl_reg &
13887 MISC_HOST_CTRL_CHIPREV);
13888 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13889 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013890
13891 tp->pci_chip_rev_id = (misc_ctrl_reg >>
13892 MISC_HOST_CTRL_CHIPREV_SHIFT);
Matt Carlson795d01c2007-10-07 23:28:17 -070013893 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
13894 u32 prod_id_asic_rev;
13895
Matt Carlson5001e2f2009-11-13 13:03:51 +000013896 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
13897 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013898 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
13899 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013900 pci_read_config_dword(tp->pdev,
13901 TG3PCI_GEN2_PRODID_ASICREV,
13902 &prod_id_asic_rev);
Matt Carlsonb703df62009-12-03 08:36:21 +000013903 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
13904 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
13905 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
13906 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
13907 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000013908 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
13909 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762 ||
13910 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766 ||
13911 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782 ||
13912 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
Matt Carlsonb703df62009-12-03 08:36:21 +000013913 pci_read_config_dword(tp->pdev,
13914 TG3PCI_GEN15_PRODID_ASICREV,
13915 &prod_id_asic_rev);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013916 else
13917 pci_read_config_dword(tp->pdev, TG3PCI_PRODID_ASICREV,
13918 &prod_id_asic_rev);
13919
Matt Carlson321d32a2008-11-21 17:22:19 -080013920 tp->pci_chip_rev_id = prod_id_asic_rev;
Matt Carlson795d01c2007-10-07 23:28:17 -070013921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013922
Michael Chanff645be2005-04-21 17:09:53 -070013923 /* Wrong chip ID in 5752 A0. This code can be removed later
13924 * as A0 is not in production.
13925 */
13926 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
13927 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
13928
Michael Chan68929142005-08-09 20:17:14 -070013929 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
13930 * we need to disable memory and use config. cycles
13931 * only to access all registers. The 5702/03 chips
13932 * can mistakenly decode the special cycles from the
13933 * ICH chipsets as memory write cycles, causing corruption
13934 * of register and memory space. Only certain ICH bridges
13935 * will drive special cycles with non-zero data during the
13936 * address phase which can fall within the 5703's address
13937 * range. This is not an ICH bug as the PCI spec allows
13938 * non-zero address during special cycles. However, only
13939 * these ICH bridges are known to drive non-zero addresses
13940 * during special cycles.
13941 *
13942 * Since special cycles do not cross PCI bridges, we only
13943 * enable this workaround if the 5703 is on the secondary
13944 * bus of these ICH bridges.
13945 */
13946 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
13947 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
13948 static struct tg3_dev_id {
13949 u32 vendor;
13950 u32 device;
13951 u32 rev;
13952 } ich_chipsets[] = {
13953 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
13954 PCI_ANY_ID },
13955 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
13956 PCI_ANY_ID },
13957 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
13958 0xa },
13959 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
13960 PCI_ANY_ID },
13961 { },
13962 };
13963 struct tg3_dev_id *pci_id = &ich_chipsets[0];
13964 struct pci_dev *bridge = NULL;
13965
13966 while (pci_id->vendor != 0) {
13967 bridge = pci_get_device(pci_id->vendor, pci_id->device,
13968 bridge);
13969 if (!bridge) {
13970 pci_id++;
13971 continue;
13972 }
13973 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070013974 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070013975 continue;
13976 }
13977 if (bridge->subordinate &&
13978 (bridge->subordinate->number ==
13979 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013980 tg3_flag_set(tp, ICH_WORKAROUND);
Michael Chan68929142005-08-09 20:17:14 -070013981 pci_dev_put(bridge);
13982 break;
13983 }
13984 }
13985 }
13986
Matt Carlson6ff6f812011-05-19 12:12:54 +000013987 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Matt Carlson41588ba2008-04-19 18:12:33 -070013988 static struct tg3_dev_id {
13989 u32 vendor;
13990 u32 device;
13991 } bridge_chipsets[] = {
13992 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
13993 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
13994 { },
13995 };
13996 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
13997 struct pci_dev *bridge = NULL;
13998
13999 while (pci_id->vendor != 0) {
14000 bridge = pci_get_device(pci_id->vendor,
14001 pci_id->device,
14002 bridge);
14003 if (!bridge) {
14004 pci_id++;
14005 continue;
14006 }
14007 if (bridge->subordinate &&
14008 (bridge->subordinate->number <=
14009 tp->pdev->bus->number) &&
14010 (bridge->subordinate->subordinate >=
14011 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014012 tg3_flag_set(tp, 5701_DMA_BUG);
Matt Carlson41588ba2008-04-19 18:12:33 -070014013 pci_dev_put(bridge);
14014 break;
14015 }
14016 }
14017 }
14018
Michael Chan4a29cc22006-03-19 13:21:12 -080014019 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
14020 * DMA addresses > 40-bit. This bridge may have other additional
14021 * 57xx devices behind it in some 4-port NIC designs for example.
14022 * Any tg3 device found behind the bridge will also need the 40-bit
14023 * DMA workaround.
14024 */
Michael Chana4e2b342005-10-26 15:46:52 -070014025 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
14026 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Joe Perches63c3a662011-04-26 08:12:10 +000014027 tg3_flag_set(tp, 5780_CLASS);
14028 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4cf78e42005-07-25 12:29:19 -070014029 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a588792010-04-05 10:19:28 +000014030 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080014031 struct pci_dev *bridge = NULL;
14032
14033 do {
14034 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
14035 PCI_DEVICE_ID_SERVERWORKS_EPB,
14036 bridge);
14037 if (bridge && bridge->subordinate &&
14038 (bridge->subordinate->number <=
14039 tp->pdev->bus->number) &&
14040 (bridge->subordinate->subordinate >=
14041 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014042 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4a29cc22006-03-19 13:21:12 -080014043 pci_dev_put(bridge);
14044 break;
14045 }
14046 } while (bridge);
14047 }
Michael Chan4cf78e42005-07-25 12:29:19 -070014048
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014049 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Matt Carlson3a1e19d2011-07-13 09:27:32 +000014050 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)
Michael Chan7544b092007-05-05 13:08:32 -070014051 tp->pdev_peer = tg3_find_peer(tp);
14052
Matt Carlsonc885e822010-08-02 11:25:57 +000014053 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000014054 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
14055 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Joe Perches63c3a662011-04-26 08:12:10 +000014056 tg3_flag_set(tp, 5717_PLUS);
Matt Carlson0a58d662011-04-05 14:22:45 +000014057
14058 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000014059 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57766)
14060 tg3_flag_set(tp, 57765_CLASS);
14061
14062 if (tg3_flag(tp, 57765_CLASS) || tg3_flag(tp, 5717_PLUS))
Joe Perches63c3a662011-04-26 08:12:10 +000014063 tg3_flag_set(tp, 57765_PLUS);
Matt Carlsonc885e822010-08-02 11:25:57 +000014064
Matt Carlson321d32a2008-11-21 17:22:19 -080014065 /* Intentionally exclude ASIC_REV_5906 */
14066 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Michael Chand9ab5ad12006-03-20 22:27:35 -080014067 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070014068 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070014069 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070014070 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014071 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014072 tg3_flag(tp, 57765_PLUS))
14073 tg3_flag_set(tp, 5755_PLUS);
Matt Carlson321d32a2008-11-21 17:22:19 -080014074
14075 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
14076 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Michael Chanb5d37722006-09-27 16:06:21 -070014077 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014078 tg3_flag(tp, 5755_PLUS) ||
14079 tg3_flag(tp, 5780_CLASS))
14080 tg3_flag_set(tp, 5750_PLUS);
John W. Linville6708e5c2005-04-21 17:00:52 -070014081
Matt Carlson6ff6f812011-05-19 12:12:54 +000014082 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014083 tg3_flag(tp, 5750_PLUS))
14084 tg3_flag_set(tp, 5705_PLUS);
John W. Linville1b440c562005-04-21 17:03:18 -070014085
Matt Carlson507399f2009-11-13 13:03:37 +000014086 /* Determine TSO capabilities */
Matt Carlsona0512942011-07-27 14:20:54 +000014087 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0)
Matt Carlson4d163b72011-01-25 15:58:48 +000014088 ; /* Do nothing. HW bug. */
Joe Perches63c3a662011-04-26 08:12:10 +000014089 else if (tg3_flag(tp, 57765_PLUS))
14090 tg3_flag_set(tp, HW_TSO_3);
14091 else if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000014092 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000014093 tg3_flag_set(tp, HW_TSO_2);
14094 else if (tg3_flag(tp, 5750_PLUS)) {
14095 tg3_flag_set(tp, HW_TSO_1);
14096 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000014097 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
14098 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
Joe Perches63c3a662011-04-26 08:12:10 +000014099 tg3_flag_clear(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000014100 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14101 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
14102 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000014103 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000014104 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
14105 tp->fw_needed = FIRMWARE_TG3TSO5;
14106 else
14107 tp->fw_needed = FIRMWARE_TG3TSO;
14108 }
14109
Matt Carlsondabc5c62011-05-19 12:12:52 +000014110 /* Selectively allow TSO based on operating conditions */
Matt Carlson6ff6f812011-05-19 12:12:54 +000014111 if (tg3_flag(tp, HW_TSO_1) ||
14112 tg3_flag(tp, HW_TSO_2) ||
14113 tg3_flag(tp, HW_TSO_3) ||
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000014114 tp->fw_needed) {
14115 /* For firmware TSO, assume ASF is disabled.
14116 * We'll disable TSO later if we discover ASF
14117 * is enabled in tg3_get_eeprom_hw_cfg().
14118 */
Matt Carlsondabc5c62011-05-19 12:12:52 +000014119 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000014120 } else {
Matt Carlsondabc5c62011-05-19 12:12:52 +000014121 tg3_flag_clear(tp, TSO_CAPABLE);
14122 tg3_flag_clear(tp, TSO_BUG);
14123 tp->fw_needed = NULL;
14124 }
14125
14126 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
14127 tp->fw_needed = FIRMWARE_TG3;
14128
Matt Carlson507399f2009-11-13 13:03:37 +000014129 tp->irq_max = 1;
14130
Joe Perches63c3a662011-04-26 08:12:10 +000014131 if (tg3_flag(tp, 5750_PLUS)) {
14132 tg3_flag_set(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070014133 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
14134 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
14135 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
14136 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
14137 tp->pdev_peer == tp->pdev))
Joe Perches63c3a662011-04-26 08:12:10 +000014138 tg3_flag_clear(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070014139
Joe Perches63c3a662011-04-26 08:12:10 +000014140 if (tg3_flag(tp, 5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070014141 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000014142 tg3_flag_set(tp, 1SHOT_MSI);
Michael Chan52c0fd82006-06-29 20:15:54 -070014143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014144
Joe Perches63c3a662011-04-26 08:12:10 +000014145 if (tg3_flag(tp, 57765_PLUS)) {
14146 tg3_flag_set(tp, SUPPORT_MSIX);
Matt Carlson507399f2009-11-13 13:03:37 +000014147 tp->irq_max = TG3_IRQ_MAX_VECS;
Matt Carlson90415472011-12-16 13:33:23 +000014148 tg3_rss_init_dflt_indir_tbl(tp);
Matt Carlson507399f2009-11-13 13:03:37 +000014149 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014150 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000014151
Matt Carlson2ffcc982011-05-19 12:12:44 +000014152 if (tg3_flag(tp, 5755_PLUS))
Joe Perches63c3a662011-04-26 08:12:10 +000014153 tg3_flag_set(tp, SHORT_DMA_BUG);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014154
Matt Carlsone31aa982011-07-27 14:20:53 +000014155 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona4cb4282011-12-14 11:09:58 +000014156 tp->dma_limit = TG3_TX_BD_DMA_MAX_4K;
Matt Carlson55086ad2011-12-14 11:09:59 +000014157 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57766)
14158 tp->dma_limit = TG3_TX_BD_DMA_MAX_2K;
Matt Carlsone31aa982011-07-27 14:20:53 +000014159
Matt Carlsonfa6b2aa2011-11-21 15:01:19 +000014160 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14161 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
14162 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Joe Perches63c3a662011-04-26 08:12:10 +000014163 tg3_flag_set(tp, LRG_PROD_RING_CAP);
Matt Carlsonde9f5232011-04-05 14:22:43 +000014164
Joe Perches63c3a662011-04-26 08:12:10 +000014165 if (tg3_flag(tp, 57765_PLUS) &&
Matt Carlsona0512942011-07-27 14:20:54 +000014166 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0)
Joe Perches63c3a662011-04-26 08:12:10 +000014167 tg3_flag_set(tp, USE_JUMBO_BDFLAG);
Matt Carlsonb703df62009-12-03 08:36:21 +000014168
Joe Perches63c3a662011-04-26 08:12:10 +000014169 if (!tg3_flag(tp, 5705_PLUS) ||
14170 tg3_flag(tp, 5780_CLASS) ||
14171 tg3_flag(tp, USE_JUMBO_BDFLAG))
14172 tg3_flag_set(tp, JUMBO_CAPABLE);
Michael Chan0f893dc2005-07-25 12:30:38 -070014173
Matt Carlson52f44902008-11-21 17:17:04 -080014174 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14175 &pci_state_reg);
14176
Jon Mason708ebb3a2011-06-27 12:56:50 +000014177 if (pci_is_pcie(tp->pdev)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014178 u16 lnkctl;
14179
Joe Perches63c3a662011-04-26 08:12:10 +000014180 tg3_flag_set(tp, PCI_EXPRESS);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080014181
Matt Carlson2c55a3d2011-11-28 09:41:04 +000014182 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0) {
14183 int readrq = pcie_get_readrq(tp->pdev);
14184 if (readrq > 2048)
14185 pcie_set_readrq(tp->pdev, 2048);
14186 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -080014187
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014188 pci_read_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +000014189 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014190 &lnkctl);
14191 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
Matt Carlson7196cd62011-05-19 16:02:44 +000014192 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
14193 ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000014194 tg3_flag_clear(tp, HW_TSO_2);
Matt Carlsondabc5c62011-05-19 12:12:52 +000014195 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson7196cd62011-05-19 16:02:44 +000014196 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014197 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014198 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000014199 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
14200 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Joe Perches63c3a662011-04-26 08:12:10 +000014201 tg3_flag_set(tp, CLKREQ_BUG);
Matt Carlson614b0592010-01-20 16:58:02 +000014202 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000014203 tg3_flag_set(tp, L1PLLPD_EN);
Michael Chanc7835a72006-11-15 21:14:42 -080014204 }
Matt Carlson52f44902008-11-21 17:17:04 -080014205 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Jon Mason708ebb3a2011-06-27 12:56:50 +000014206 /* BCM5785 devices are effectively PCIe devices, and should
14207 * follow PCIe codepaths, but do not have a PCIe capabilities
14208 * section.
Matt Carlson93a700a2011-08-31 11:44:54 +000014209 */
Joe Perches63c3a662011-04-26 08:12:10 +000014210 tg3_flag_set(tp, PCI_EXPRESS);
14211 } else if (!tg3_flag(tp, 5705_PLUS) ||
14212 tg3_flag(tp, 5780_CLASS)) {
Matt Carlson52f44902008-11-21 17:17:04 -080014213 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
14214 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000014215 dev_err(&tp->pdev->dev,
14216 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080014217 return -EIO;
14218 }
14219
14220 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
Joe Perches63c3a662011-04-26 08:12:10 +000014221 tg3_flag_set(tp, PCIX_MODE);
Matt Carlson52f44902008-11-21 17:17:04 -080014222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014223
Michael Chan399de502005-10-03 14:02:39 -070014224 /* If we have an AMD 762 or VIA K8T800 chipset, write
14225 * reordering to the mailbox registers done by the host
14226 * controller can cause major troubles. We read back from
14227 * every mailbox register write to force the writes to be
14228 * posted to the chip in order.
14229 */
Matt Carlson41434702011-03-09 16:58:22 +000014230 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Joe Perches63c3a662011-04-26 08:12:10 +000014231 !tg3_flag(tp, PCI_EXPRESS))
14232 tg3_flag_set(tp, MBOX_WRITE_REORDER);
Michael Chan399de502005-10-03 14:02:39 -070014233
Matt Carlson69fc4052008-12-21 20:19:57 -080014234 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
14235 &tp->pci_cacheline_sz);
14236 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
14237 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014238 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14239 tp->pci_lat_timer < 64) {
14240 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080014241 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
14242 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014243 }
14244
Matt Carlson16821282011-07-13 09:27:28 +000014245 /* Important! -- It is critical that the PCI-X hw workaround
14246 * situation is decided before the first MMIO register access.
14247 */
Matt Carlson52f44902008-11-21 17:17:04 -080014248 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
14249 /* 5700 BX chips need to have their TX producer index
14250 * mailboxes written twice to workaround a bug.
14251 */
Joe Perches63c3a662011-04-26 08:12:10 +000014252 tg3_flag_set(tp, TXD_MBOX_HWBUG);
Matt Carlson9974a352007-10-07 23:27:28 -070014253
Matt Carlson52f44902008-11-21 17:17:04 -080014254 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014255 *
14256 * The workaround is to use indirect register accesses
14257 * for all chip writes not to mailbox registers.
14258 */
Joe Perches63c3a662011-04-26 08:12:10 +000014259 if (tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014260 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014261
Joe Perches63c3a662011-04-26 08:12:10 +000014262 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014263
14264 /* The chip can have it's power management PCI config
14265 * space registers clobbered due to this bug.
14266 * So explicitly force the chip into D0 here.
14267 */
Matt Carlson9974a352007-10-07 23:27:28 -070014268 pci_read_config_dword(tp->pdev,
14269 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014270 &pm_reg);
14271 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
14272 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070014273 pci_write_config_dword(tp->pdev,
14274 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014275 pm_reg);
14276
14277 /* Also, force SERR#/PERR# in PCI command. */
14278 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
14279 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
14280 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
14281 }
14282 }
14283
Linus Torvalds1da177e2005-04-16 15:20:36 -070014284 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000014285 tg3_flag_set(tp, PCI_HIGH_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014286 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000014287 tg3_flag_set(tp, PCI_32BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014288
14289 /* Chip-specific fixup from Broadcom driver */
14290 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
14291 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
14292 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
14293 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
14294 }
14295
Michael Chan1ee582d2005-08-09 20:16:46 -070014296 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070014297 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070014298 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070014299 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070014300 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070014301 tp->write32_tx_mbox = tg3_write32;
14302 tp->write32_rx_mbox = tg3_write32;
14303
14304 /* Various workaround register access methods */
Joe Perches63c3a662011-04-26 08:12:10 +000014305 if (tg3_flag(tp, PCIX_TARGET_HWBUG))
Michael Chan1ee582d2005-08-09 20:16:46 -070014306 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070014307 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014308 (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson98efd8a2007-05-05 12:47:25 -070014309 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
14310 /*
14311 * Back to back register writes can cause problems on these
14312 * chips, the workaround is to read back all reg writes
14313 * except those to mailbox regs.
14314 *
14315 * See tg3_write_indirect_reg32().
14316 */
Michael Chan1ee582d2005-08-09 20:16:46 -070014317 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070014318 }
14319
Joe Perches63c3a662011-04-26 08:12:10 +000014320 if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
Michael Chan1ee582d2005-08-09 20:16:46 -070014321 tp->write32_tx_mbox = tg3_write32_tx_mbox;
Joe Perches63c3a662011-04-26 08:12:10 +000014322 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Michael Chan1ee582d2005-08-09 20:16:46 -070014323 tp->write32_rx_mbox = tg3_write_flush_reg32;
14324 }
Michael Chan20094932005-08-09 20:16:32 -070014325
Joe Perches63c3a662011-04-26 08:12:10 +000014326 if (tg3_flag(tp, ICH_WORKAROUND)) {
Michael Chan68929142005-08-09 20:17:14 -070014327 tp->read32 = tg3_read_indirect_reg32;
14328 tp->write32 = tg3_write_indirect_reg32;
14329 tp->read32_mbox = tg3_read_indirect_mbox;
14330 tp->write32_mbox = tg3_write_indirect_mbox;
14331 tp->write32_tx_mbox = tg3_write_indirect_mbox;
14332 tp->write32_rx_mbox = tg3_write_indirect_mbox;
14333
14334 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070014335 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070014336
14337 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
14338 pci_cmd &= ~PCI_COMMAND_MEMORY;
14339 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
14340 }
Michael Chanb5d37722006-09-27 16:06:21 -070014341 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
14342 tp->read32_mbox = tg3_read32_mbox_5906;
14343 tp->write32_mbox = tg3_write32_mbox_5906;
14344 tp->write32_tx_mbox = tg3_write32_mbox_5906;
14345 tp->write32_rx_mbox = tg3_write32_mbox_5906;
14346 }
Michael Chan68929142005-08-09 20:17:14 -070014347
Michael Chanbbadf502006-04-06 21:46:34 -070014348 if (tp->write32 == tg3_write_indirect_reg32 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014349 (tg3_flag(tp, PCIX_MODE) &&
Michael Chanbbadf502006-04-06 21:46:34 -070014350 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070014351 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Joe Perches63c3a662011-04-26 08:12:10 +000014352 tg3_flag_set(tp, SRAM_USE_CONFIG);
Michael Chanbbadf502006-04-06 21:46:34 -070014353
Matt Carlson16821282011-07-13 09:27:28 +000014354 /* The memory arbiter has to be enabled in order for SRAM accesses
14355 * to succeed. Normally on powerup the tg3 chip firmware will make
14356 * sure it is enabled, but other entities such as system netboot
14357 * code might disable it.
14358 */
14359 val = tr32(MEMARB_MODE);
14360 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
14361
Matt Carlson9dc5e342011-11-04 09:15:02 +000014362 tp->pci_fn = PCI_FUNC(tp->pdev->devfn) & 3;
14363 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
14364 tg3_flag(tp, 5780_CLASS)) {
14365 if (tg3_flag(tp, PCIX_MODE)) {
14366 pci_read_config_dword(tp->pdev,
14367 tp->pcix_cap + PCI_X_STATUS,
14368 &val);
14369 tp->pci_fn = val & 0x7;
14370 }
14371 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
14372 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
14373 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) ==
14374 NIC_SRAM_CPMUSTAT_SIG) {
14375 tp->pci_fn = val & TG3_CPMU_STATUS_FMSK_5717;
14376 tp->pci_fn = tp->pci_fn ? 1 : 0;
14377 }
14378 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
14379 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
14380 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
14381 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) ==
14382 NIC_SRAM_CPMUSTAT_SIG) {
14383 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5719) >>
14384 TG3_CPMU_STATUS_FSHFT_5719;
14385 }
Matt Carlson69f11c92011-07-13 09:27:30 +000014386 }
14387
Michael Chan7d0c41e2005-04-21 17:06:20 -070014388 /* Get eeprom hw config before calling tg3_set_power_state().
Joe Perches63c3a662011-04-26 08:12:10 +000014389 * In particular, the TG3_FLAG_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070014390 * determined before calling tg3_set_power_state() so that
14391 * we know whether or not to switch out of Vaux power.
14392 * When the flag is set, it means that GPIO1 is used for eeprom
14393 * write protect and also implies that it is a LOM where GPIOs
14394 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040014395 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070014396 tg3_get_eeprom_hw_cfg(tp);
14397
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000014398 if (tp->fw_needed && tg3_flag(tp, ENABLE_ASF)) {
14399 tg3_flag_clear(tp, TSO_CAPABLE);
14400 tg3_flag_clear(tp, TSO_BUG);
14401 tp->fw_needed = NULL;
14402 }
14403
Joe Perches63c3a662011-04-26 08:12:10 +000014404 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070014405 /* Allow reads and writes to the
14406 * APE register and memory space.
14407 */
14408 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +000014409 PCISTATE_ALLOW_APE_SHMEM_WR |
14410 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070014411 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
14412 pci_state_reg);
Matt Carlsonc9cab242011-07-13 09:27:27 +000014413
14414 tg3_ape_lock_init(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070014415 }
14416
Matt Carlson9936bcf2007-10-10 18:03:07 -070014417 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson57e69832008-05-25 23:48:31 -070014418 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014419 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014420 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014421 tg3_flag(tp, 57765_PLUS))
14422 tg3_flag_set(tp, CPMU_PRESENT);
Matt Carlsond30cdd22007-10-07 23:28:35 -070014423
Matt Carlson16821282011-07-13 09:27:28 +000014424 /* Set up tp->grc_local_ctrl before calling
14425 * tg3_pwrsrc_switch_to_vmain(). GPIO1 driven high
14426 * will bring 5700's external PHY out of reset.
Michael Chan314fba32005-04-21 17:07:04 -070014427 * It is also used as eeprom write protect on LOMs.
14428 */
14429 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
Matt Carlson6ff6f812011-05-19 12:12:54 +000014430 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014431 tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan314fba32005-04-21 17:07:04 -070014432 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
14433 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070014434 /* Unused GPIO3 must be driven as output on 5752 because there
14435 * are no pull-up resistors on unused GPIO pins.
14436 */
14437 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
14438 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070014439
Matt Carlson321d32a2008-11-21 17:22:19 -080014440 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsoncb4ed1f2010-01-20 16:58:09 +000014441 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000014442 tg3_flag(tp, 57765_CLASS))
Michael Chanaf36e6b2006-03-23 01:28:06 -080014443 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
14444
Matt Carlson8d519ab2009-04-20 06:58:01 +000014445 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
14446 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070014447 /* Turn off the debug UART. */
14448 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014449 if (tg3_flag(tp, IS_NIC))
Matt Carlson5f0c4a32008-06-09 15:41:12 -070014450 /* Keep VMain power. */
14451 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
14452 GRC_LCLCTRL_GPIO_OUTPUT0;
14453 }
14454
Matt Carlson16821282011-07-13 09:27:28 +000014455 /* Switch out of Vaux if it is a NIC */
14456 tg3_pwrsrc_switch_to_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014457
Linus Torvalds1da177e2005-04-16 15:20:36 -070014458 /* Derive initial jumbo mode from MTU assigned in
14459 * ether_setup() via the alloc_etherdev() call
14460 */
Joe Perches63c3a662011-04-26 08:12:10 +000014461 if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
14462 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014463
14464 /* Determine WakeOnLan speed to use. */
14465 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14466 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
14467 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
14468 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
Joe Perches63c3a662011-04-26 08:12:10 +000014469 tg3_flag_clear(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014470 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000014471 tg3_flag_set(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014472 }
14473
Matt Carlson7f97a4b2009-08-25 10:10:03 +000014474 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014475 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000014476
Linus Torvalds1da177e2005-04-16 15:20:36 -070014477 /* A few boards don't want Ethernet@WireSpeed phy feature */
Matt Carlson6ff6f812011-05-19 12:12:54 +000014478 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14479 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070014480 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070014481 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014482 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
14483 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
14484 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014485
14486 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
14487 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014488 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014489 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014490 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014491
Joe Perches63c3a662011-04-26 08:12:10 +000014492 if (tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014493 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080014494 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014495 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014496 !tg3_flag(tp, 57765_PLUS)) {
Michael Chanc424cb22006-04-29 18:56:34 -070014497 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070014498 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070014499 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
14500 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080014501 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
14502 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014503 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080014504 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014505 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080014506 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014507 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070014508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014509
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014510 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
14511 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
14512 tp->phy_otp = tg3_read_otp_phycfg(tp);
14513 if (tp->phy_otp == 0)
14514 tp->phy_otp = TG3_OTP_DEFAULT;
14515 }
14516
Joe Perches63c3a662011-04-26 08:12:10 +000014517 if (tg3_flag(tp, CPMU_PRESENT))
Matt Carlson8ef21422008-05-02 16:47:53 -070014518 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
14519 else
14520 tp->mi_mode = MAC_MI_MODE_BASE;
14521
Linus Torvalds1da177e2005-04-16 15:20:36 -070014522 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014523 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
14524 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
14525 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
14526
Matt Carlson4d958472011-04-20 07:57:35 +000014527 /* Set these bits to enable statistics workaround. */
14528 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14529 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
14530 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0) {
14531 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
14532 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
14533 }
14534
Matt Carlson321d32a2008-11-21 17:22:19 -080014535 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
14536 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Joe Perches63c3a662011-04-26 08:12:10 +000014537 tg3_flag_set(tp, USE_PHYLIB);
Matt Carlson57e69832008-05-25 23:48:31 -070014538
Matt Carlson158d7ab2008-05-29 01:37:54 -070014539 err = tg3_mdio_init(tp);
14540 if (err)
14541 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014542
14543 /* Initialize data/descriptor byte/word swapping. */
14544 val = tr32(GRC_MODE);
Matt Carlsonf2096f92011-04-05 14:22:48 +000014545 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
14546 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
14547 GRC_MODE_WORD_SWAP_B2HRX_DATA |
14548 GRC_MODE_B2HRX_ENABLE |
14549 GRC_MODE_HTX2B_ENABLE |
14550 GRC_MODE_HOST_STACKUP);
14551 else
14552 val &= GRC_MODE_HOST_STACKUP;
14553
Linus Torvalds1da177e2005-04-16 15:20:36 -070014554 tw32(GRC_MODE, val | tp->grc_mode);
14555
14556 tg3_switch_clocks(tp);
14557
14558 /* Clear this out for sanity. */
14559 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
14560
14561 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14562 &pci_state_reg);
14563 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014564 !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014565 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
14566
14567 if (chiprevid == CHIPREV_ID_5701_A0 ||
14568 chiprevid == CHIPREV_ID_5701_B0 ||
14569 chiprevid == CHIPREV_ID_5701_B2 ||
14570 chiprevid == CHIPREV_ID_5701_B5) {
14571 void __iomem *sram_base;
14572
14573 /* Write some dummy words into the SRAM status block
14574 * area, see if it reads back correctly. If the return
14575 * value is bad, force enable the PCIX workaround.
14576 */
14577 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
14578
14579 writel(0x00000000, sram_base);
14580 writel(0x00000000, sram_base + 4);
14581 writel(0xffffffff, sram_base + 4);
14582 if (readl(sram_base) != 0x00000000)
Joe Perches63c3a662011-04-26 08:12:10 +000014583 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014584 }
14585 }
14586
14587 udelay(50);
14588 tg3_nvram_init(tp);
14589
14590 grc_misc_cfg = tr32(GRC_MISC_CFG);
14591 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
14592
Linus Torvalds1da177e2005-04-16 15:20:36 -070014593 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14594 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
14595 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
Joe Perches63c3a662011-04-26 08:12:10 +000014596 tg3_flag_set(tp, IS_5788);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014597
Joe Perches63c3a662011-04-26 08:12:10 +000014598 if (!tg3_flag(tp, IS_5788) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +000014599 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014600 tg3_flag_set(tp, TAGGED_STATUS);
14601 if (tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070014602 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
14603 HOSTCC_MODE_CLRTICK_TXBD);
14604
14605 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
14606 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
14607 tp->misc_host_ctrl);
14608 }
14609
Matt Carlson3bda1252008-08-15 14:08:22 -070014610 /* Preserve the APE MAC_MODE bits */
Joe Perches63c3a662011-04-26 08:12:10 +000014611 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +000014612 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070014613 else
Matt Carlson6e01b202011-08-19 13:58:20 +000014614 tp->mac_mode = 0;
Matt Carlson3bda1252008-08-15 14:08:22 -070014615
Linus Torvalds1da177e2005-04-16 15:20:36 -070014616 /* these are limited to 10/100 only */
14617 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14618 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
14619 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14620 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14621 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
14622 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
14623 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
14624 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14625 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
Michael Chan676917d2006-12-07 00:20:22 -080014626 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
14627 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014628 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
Matt Carlsond1101142010-02-17 15:16:55 +000014629 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
14630 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014631 (tp->phy_flags & TG3_PHYFLG_IS_FET))
14632 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014633
14634 err = tg3_phy_probe(tp);
14635 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014636 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014637 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014638 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014639 }
14640
Matt Carlson184b8902010-04-05 10:19:25 +000014641 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080014642 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014643
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014644 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
14645 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014646 } else {
14647 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014648 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014649 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014650 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014651 }
14652
14653 /* 5700 {AX,BX} chips have a broken status block link
14654 * change bit implementation, so we must use the
14655 * status register in those cases.
14656 */
14657 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014658 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014659 else
Joe Perches63c3a662011-04-26 08:12:10 +000014660 tg3_flag_clear(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014661
14662 /* The led_ctrl is set during tg3_phy_probe, here we might
14663 * have to force the link status polling mechanism based
14664 * upon subsystem IDs.
14665 */
14666 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070014667 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014668 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
14669 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Joe Perches63c3a662011-04-26 08:12:10 +000014670 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014671 }
14672
14673 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014674 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Joe Perches63c3a662011-04-26 08:12:10 +000014675 tg3_flag_set(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014676 else
Joe Perches63c3a662011-04-26 08:12:10 +000014677 tg3_flag_clear(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014678
Eric Dumazet9205fd92011-11-18 06:47:01 +000014679 tp->rx_offset = NET_SKB_PAD + NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014680 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014681 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014682 tg3_flag(tp, PCIX_MODE)) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000014683 tp->rx_offset = NET_SKB_PAD;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014684#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000014685 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014686#endif
14687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014688
Matt Carlson2c49a442010-09-30 10:34:35 +000014689 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
14690 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000014691 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
14692
Matt Carlson2c49a442010-09-30 10:34:35 +000014693 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070014694
14695 /* Increment the rx prod index on the rx std ring by at most
14696 * 8 for these chips to workaround hw errata.
14697 */
14698 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
14699 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
14700 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
14701 tp->rx_std_max_post = 8;
14702
Joe Perches63c3a662011-04-26 08:12:10 +000014703 if (tg3_flag(tp, ASPM_WORKAROUND))
Matt Carlson8ed5d972007-05-07 00:25:49 -070014704 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
14705 PCIE_PWR_MGMT_L1_THRESH_MSK;
14706
Linus Torvalds1da177e2005-04-16 15:20:36 -070014707 return err;
14708}
14709
David S. Miller49b6e95f2007-03-29 01:38:42 -070014710#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014711static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
14712{
14713 struct net_device *dev = tp->dev;
14714 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014715 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070014716 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014717 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014718
David S. Miller49b6e95f2007-03-29 01:38:42 -070014719 addr = of_get_property(dp, "local-mac-address", &len);
14720 if (addr && len == 6) {
14721 memcpy(dev->dev_addr, addr, 6);
14722 memcpy(dev->perm_addr, dev->dev_addr, 6);
14723 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014724 }
14725 return -ENODEV;
14726}
14727
14728static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
14729{
14730 struct net_device *dev = tp->dev;
14731
14732 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070014733 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014734 return 0;
14735}
14736#endif
14737
14738static int __devinit tg3_get_device_address(struct tg3 *tp)
14739{
14740 struct net_device *dev = tp->dev;
14741 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080014742 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014743
David S. Miller49b6e95f2007-03-29 01:38:42 -070014744#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014745 if (!tg3_get_macaddr_sparc(tp))
14746 return 0;
14747#endif
14748
14749 mac_offset = 0x7c;
Matt Carlson6ff6f812011-05-19 12:12:54 +000014750 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014751 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014752 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
14753 mac_offset = 0xcc;
14754 if (tg3_nvram_lock(tp))
14755 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
14756 else
14757 tg3_nvram_unlock(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000014758 } else if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson69f11c92011-07-13 09:27:30 +000014759 if (tp->pci_fn & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014760 mac_offset = 0xcc;
Matt Carlson69f11c92011-07-13 09:27:30 +000014761 if (tp->pci_fn > 1)
Matt Carlsona50d0792010-06-05 17:24:37 +000014762 mac_offset += 0x18c;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014763 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070014764 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014765
14766 /* First try to get it from MAC address mailbox. */
14767 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
14768 if ((hi >> 16) == 0x484b) {
14769 dev->dev_addr[0] = (hi >> 8) & 0xff;
14770 dev->dev_addr[1] = (hi >> 0) & 0xff;
14771
14772 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
14773 dev->dev_addr[2] = (lo >> 24) & 0xff;
14774 dev->dev_addr[3] = (lo >> 16) & 0xff;
14775 dev->dev_addr[4] = (lo >> 8) & 0xff;
14776 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014777
Michael Chan008652b2006-03-27 23:14:53 -080014778 /* Some old bootcode may report a 0 MAC address in SRAM */
14779 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
14780 }
14781 if (!addr_ok) {
14782 /* Next, try NVRAM. */
Joe Perches63c3a662011-04-26 08:12:10 +000014783 if (!tg3_flag(tp, NO_NVRAM) &&
Matt Carlsondf259d82009-04-20 06:57:14 +000014784 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000014785 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070014786 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
14787 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080014788 }
14789 /* Finally just fetch it out of the MAC control regs. */
14790 else {
14791 hi = tr32(MAC_ADDR_0_HIGH);
14792 lo = tr32(MAC_ADDR_0_LOW);
14793
14794 dev->dev_addr[5] = lo & 0xff;
14795 dev->dev_addr[4] = (lo >> 8) & 0xff;
14796 dev->dev_addr[3] = (lo >> 16) & 0xff;
14797 dev->dev_addr[2] = (lo >> 24) & 0xff;
14798 dev->dev_addr[1] = hi & 0xff;
14799 dev->dev_addr[0] = (hi >> 8) & 0xff;
14800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014801 }
14802
14803 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070014804#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014805 if (!tg3_get_default_macaddr_sparc(tp))
14806 return 0;
14807#endif
14808 return -EINVAL;
14809 }
John W. Linville2ff43692005-09-12 14:44:20 -070014810 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014811 return 0;
14812}
14813
David S. Miller59e6b432005-05-18 22:50:10 -070014814#define BOUNDARY_SINGLE_CACHELINE 1
14815#define BOUNDARY_MULTI_CACHELINE 2
14816
14817static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
14818{
14819 int cacheline_size;
14820 u8 byte;
14821 int goal;
14822
14823 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
14824 if (byte == 0)
14825 cacheline_size = 1024;
14826 else
14827 cacheline_size = (int) byte * 4;
14828
14829 /* On 5703 and later chips, the boundary bits have no
14830 * effect.
14831 */
14832 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14833 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014834 !tg3_flag(tp, PCI_EXPRESS))
David S. Miller59e6b432005-05-18 22:50:10 -070014835 goto out;
14836
14837#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
14838 goal = BOUNDARY_MULTI_CACHELINE;
14839#else
14840#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
14841 goal = BOUNDARY_SINGLE_CACHELINE;
14842#else
14843 goal = 0;
14844#endif
14845#endif
14846
Joe Perches63c3a662011-04-26 08:12:10 +000014847 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014848 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
14849 goto out;
14850 }
14851
David S. Miller59e6b432005-05-18 22:50:10 -070014852 if (!goal)
14853 goto out;
14854
14855 /* PCI controllers on most RISC systems tend to disconnect
14856 * when a device tries to burst across a cache-line boundary.
14857 * Therefore, letting tg3 do so just wastes PCI bandwidth.
14858 *
14859 * Unfortunately, for PCI-E there are only limited
14860 * write-side controls for this, and thus for reads
14861 * we will still get the disconnects. We'll also waste
14862 * these PCI cycles for both read and write for chips
14863 * other than 5700 and 5701 which do not implement the
14864 * boundary bits.
14865 */
Joe Perches63c3a662011-04-26 08:12:10 +000014866 if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014867 switch (cacheline_size) {
14868 case 16:
14869 case 32:
14870 case 64:
14871 case 128:
14872 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14873 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
14874 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
14875 } else {
14876 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14877 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14878 }
14879 break;
14880
14881 case 256:
14882 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
14883 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
14884 break;
14885
14886 default:
14887 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14888 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14889 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014890 }
Joe Perches63c3a662011-04-26 08:12:10 +000014891 } else if (tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014892 switch (cacheline_size) {
14893 case 16:
14894 case 32:
14895 case 64:
14896 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14897 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14898 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
14899 break;
14900 }
14901 /* fallthrough */
14902 case 128:
14903 default:
14904 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14905 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
14906 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014907 }
David S. Miller59e6b432005-05-18 22:50:10 -070014908 } else {
14909 switch (cacheline_size) {
14910 case 16:
14911 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14912 val |= (DMA_RWCTRL_READ_BNDRY_16 |
14913 DMA_RWCTRL_WRITE_BNDRY_16);
14914 break;
14915 }
14916 /* fallthrough */
14917 case 32:
14918 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14919 val |= (DMA_RWCTRL_READ_BNDRY_32 |
14920 DMA_RWCTRL_WRITE_BNDRY_32);
14921 break;
14922 }
14923 /* fallthrough */
14924 case 64:
14925 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14926 val |= (DMA_RWCTRL_READ_BNDRY_64 |
14927 DMA_RWCTRL_WRITE_BNDRY_64);
14928 break;
14929 }
14930 /* fallthrough */
14931 case 128:
14932 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14933 val |= (DMA_RWCTRL_READ_BNDRY_128 |
14934 DMA_RWCTRL_WRITE_BNDRY_128);
14935 break;
14936 }
14937 /* fallthrough */
14938 case 256:
14939 val |= (DMA_RWCTRL_READ_BNDRY_256 |
14940 DMA_RWCTRL_WRITE_BNDRY_256);
14941 break;
14942 case 512:
14943 val |= (DMA_RWCTRL_READ_BNDRY_512 |
14944 DMA_RWCTRL_WRITE_BNDRY_512);
14945 break;
14946 case 1024:
14947 default:
14948 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
14949 DMA_RWCTRL_WRITE_BNDRY_1024);
14950 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014951 }
David S. Miller59e6b432005-05-18 22:50:10 -070014952 }
14953
14954out:
14955 return val;
14956}
14957
Linus Torvalds1da177e2005-04-16 15:20:36 -070014958static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
14959{
14960 struct tg3_internal_buffer_desc test_desc;
14961 u32 sram_dma_descs;
14962 int i, ret;
14963
14964 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
14965
14966 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
14967 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
14968 tw32(RDMAC_STATUS, 0);
14969 tw32(WDMAC_STATUS, 0);
14970
14971 tw32(BUFMGR_MODE, 0);
14972 tw32(FTQ_RESET, 0);
14973
14974 test_desc.addr_hi = ((u64) buf_dma) >> 32;
14975 test_desc.addr_lo = buf_dma & 0xffffffff;
14976 test_desc.nic_mbuf = 0x00002100;
14977 test_desc.len = size;
14978
14979 /*
14980 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
14981 * the *second* time the tg3 driver was getting loaded after an
14982 * initial scan.
14983 *
14984 * Broadcom tells me:
14985 * ...the DMA engine is connected to the GRC block and a DMA
14986 * reset may affect the GRC block in some unpredictable way...
14987 * The behavior of resets to individual blocks has not been tested.
14988 *
14989 * Broadcom noted the GRC reset will also reset all sub-components.
14990 */
14991 if (to_device) {
14992 test_desc.cqid_sqid = (13 << 8) | 2;
14993
14994 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
14995 udelay(40);
14996 } else {
14997 test_desc.cqid_sqid = (16 << 8) | 7;
14998
14999 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
15000 udelay(40);
15001 }
15002 test_desc.flags = 0x00000005;
15003
15004 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
15005 u32 val;
15006
15007 val = *(((u32 *)&test_desc) + i);
15008 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
15009 sram_dma_descs + (i * sizeof(u32)));
15010 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
15011 }
15012 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
15013
Matt Carlson859a588792010-04-05 10:19:28 +000015014 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015015 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a588792010-04-05 10:19:28 +000015016 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070015017 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015018
15019 ret = -ENODEV;
15020 for (i = 0; i < 40; i++) {
15021 u32 val;
15022
15023 if (to_device)
15024 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
15025 else
15026 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
15027 if ((val & 0xffff) == sram_dma_descs) {
15028 ret = 0;
15029 break;
15030 }
15031
15032 udelay(100);
15033 }
15034
15035 return ret;
15036}
15037
David S. Millerded73402005-05-23 13:59:47 -070015038#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070015039
Matt Carlson41434702011-03-09 16:58:22 +000015040static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080015041 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
15042 { },
15043};
15044
Linus Torvalds1da177e2005-04-16 15:20:36 -070015045static int __devinit tg3_test_dma(struct tg3 *tp)
15046{
15047 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070015048 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000015049 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015050
Matt Carlson4bae65c2010-11-24 08:31:52 +000015051 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
15052 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015053 if (!buf) {
15054 ret = -ENOMEM;
15055 goto out_nofree;
15056 }
15057
15058 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
15059 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
15060
David S. Miller59e6b432005-05-18 22:50:10 -070015061 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015062
Joe Perches63c3a662011-04-26 08:12:10 +000015063 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000015064 goto out;
15065
Joe Perches63c3a662011-04-26 08:12:10 +000015066 if (tg3_flag(tp, PCI_EXPRESS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015067 /* DMA read watermark not used on PCIE */
15068 tp->dma_rwctrl |= 0x00180000;
Joe Perches63c3a662011-04-26 08:12:10 +000015069 } else if (!tg3_flag(tp, PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070015070 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
15071 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015072 tp->dma_rwctrl |= 0x003f0000;
15073 else
15074 tp->dma_rwctrl |= 0x003f000f;
15075 } else {
15076 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
15077 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
15078 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080015079 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015080
Michael Chan4a29cc22006-03-19 13:21:12 -080015081 /* If the 5704 is behind the EPB bridge, we can
15082 * do the less restrictive ONE_DMA workaround for
15083 * better performance.
15084 */
Joe Perches63c3a662011-04-26 08:12:10 +000015085 if (tg3_flag(tp, 40BIT_DMA_BUG) &&
Michael Chan4a29cc22006-03-19 13:21:12 -080015086 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
15087 tp->dma_rwctrl |= 0x8000;
15088 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015089 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
15090
Michael Chan49afdeb2007-02-13 12:17:03 -080015091 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
15092 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070015093 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080015094 tp->dma_rwctrl |=
15095 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
15096 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
15097 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070015098 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
15099 /* 5780 always in PCIX mode */
15100 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070015101 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
15102 /* 5714 always in PCIX mode */
15103 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015104 } else {
15105 tp->dma_rwctrl |= 0x001b000f;
15106 }
15107 }
15108
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 tp->dma_rwctrl &= 0xfffffff0;
15112
15113 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
15114 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
15115 /* Remove this if it causes problems for some boards. */
15116 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
15117
15118 /* On 5700/5701 chips, we need to set this bit.
15119 * Otherwise the chip will issue cacheline transactions
15120 * to streamable DMA memory with not all the byte
15121 * enables turned on. This is an error on several
15122 * RISC PCI controllers, in particular sparc64.
15123 *
15124 * On 5703/5704 chips, this bit has been reassigned
15125 * a different meaning. In particular, it is used
15126 * on those chips to enable a PCI-X workaround.
15127 */
15128 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
15129 }
15130
15131 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15132
15133#if 0
15134 /* Unneeded, already done by tg3_get_invariants. */
15135 tg3_switch_clocks(tp);
15136#endif
15137
Linus Torvalds1da177e2005-04-16 15:20:36 -070015138 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
15139 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
15140 goto out;
15141
David S. Miller59e6b432005-05-18 22:50:10 -070015142 /* It is best to perform DMA test with maximum write burst size
15143 * to expose the 5700/5701 write DMA bug.
15144 */
15145 saved_dma_rwctrl = tp->dma_rwctrl;
15146 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
15147 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15148
Linus Torvalds1da177e2005-04-16 15:20:36 -070015149 while (1) {
15150 u32 *p = buf, i;
15151
15152 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
15153 p[i] = i;
15154
15155 /* Send the buffer to the chip. */
15156 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
15157 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000015158 dev_err(&tp->pdev->dev,
15159 "%s: Buffer write failed. err = %d\n",
15160 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015161 break;
15162 }
15163
15164#if 0
15165 /* validate data reached card RAM correctly. */
15166 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
15167 u32 val;
15168 tg3_read_mem(tp, 0x2100 + (i*4), &val);
15169 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000015170 dev_err(&tp->pdev->dev,
15171 "%s: Buffer corrupted on device! "
15172 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015173 /* ret = -ENODEV here? */
15174 }
15175 p[i] = 0;
15176 }
15177#endif
15178 /* Now read it back. */
15179 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
15180 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000015181 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
15182 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015183 break;
15184 }
15185
15186 /* Verify it. */
15187 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
15188 if (p[i] == i)
15189 continue;
15190
David S. Miller59e6b432005-05-18 22:50:10 -070015191 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
15192 DMA_RWCTRL_WRITE_BNDRY_16) {
15193 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015194 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
15195 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15196 break;
15197 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000015198 dev_err(&tp->pdev->dev,
15199 "%s: Buffer corrupted on read back! "
15200 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015201 ret = -ENODEV;
15202 goto out;
15203 }
15204 }
15205
15206 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
15207 /* Success. */
15208 ret = 0;
15209 break;
15210 }
15211 }
David S. Miller59e6b432005-05-18 22:50:10 -070015212 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
15213 DMA_RWCTRL_WRITE_BNDRY_16) {
15214 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070015215 * now look for chipsets that are known to expose the
15216 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070015217 */
Matt Carlson41434702011-03-09 16:58:22 +000015218 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070015219 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
15220 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a588792010-04-05 10:19:28 +000015221 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070015222 /* Safe to use the calculated DMA boundary. */
15223 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a588792010-04-05 10:19:28 +000015224 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070015225
David S. Miller59e6b432005-05-18 22:50:10 -070015226 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015228
15229out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000015230 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015231out_nofree:
15232 return ret;
15233}
15234
Linus Torvalds1da177e2005-04-16 15:20:36 -070015235static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
15236{
Joe Perches63c3a662011-04-26 08:12:10 +000015237 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson666bc832010-01-20 16:58:03 +000015238 tp->bufmgr_config.mbuf_read_dma_low_water =
15239 DEFAULT_MB_RDMA_LOW_WATER_5705;
15240 tp->bufmgr_config.mbuf_mac_rx_low_water =
15241 DEFAULT_MB_MACRX_LOW_WATER_57765;
15242 tp->bufmgr_config.mbuf_high_water =
15243 DEFAULT_MB_HIGH_WATER_57765;
15244
15245 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15246 DEFAULT_MB_RDMA_LOW_WATER_5705;
15247 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15248 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
15249 tp->bufmgr_config.mbuf_high_water_jumbo =
15250 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000015251 } else if (tg3_flag(tp, 5705_PLUS)) {
Michael Chanfdfec1722005-07-25 12:31:48 -070015252 tp->bufmgr_config.mbuf_read_dma_low_water =
15253 DEFAULT_MB_RDMA_LOW_WATER_5705;
15254 tp->bufmgr_config.mbuf_mac_rx_low_water =
15255 DEFAULT_MB_MACRX_LOW_WATER_5705;
15256 tp->bufmgr_config.mbuf_high_water =
15257 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070015258 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
15259 tp->bufmgr_config.mbuf_mac_rx_low_water =
15260 DEFAULT_MB_MACRX_LOW_WATER_5906;
15261 tp->bufmgr_config.mbuf_high_water =
15262 DEFAULT_MB_HIGH_WATER_5906;
15263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015264
Michael Chanfdfec1722005-07-25 12:31:48 -070015265 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15266 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
15267 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15268 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
15269 tp->bufmgr_config.mbuf_high_water_jumbo =
15270 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
15271 } else {
15272 tp->bufmgr_config.mbuf_read_dma_low_water =
15273 DEFAULT_MB_RDMA_LOW_WATER;
15274 tp->bufmgr_config.mbuf_mac_rx_low_water =
15275 DEFAULT_MB_MACRX_LOW_WATER;
15276 tp->bufmgr_config.mbuf_high_water =
15277 DEFAULT_MB_HIGH_WATER;
15278
15279 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15280 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
15281 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15282 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
15283 tp->bufmgr_config.mbuf_high_water_jumbo =
15284 DEFAULT_MB_HIGH_WATER_JUMBO;
15285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015286
15287 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
15288 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
15289}
15290
15291static char * __devinit tg3_phy_string(struct tg3 *tp)
15292{
Matt Carlson79eb6902010-02-17 15:17:03 +000015293 switch (tp->phy_id & TG3_PHY_ID_MASK) {
15294 case TG3_PHY_ID_BCM5400: return "5400";
15295 case TG3_PHY_ID_BCM5401: return "5401";
15296 case TG3_PHY_ID_BCM5411: return "5411";
15297 case TG3_PHY_ID_BCM5701: return "5701";
15298 case TG3_PHY_ID_BCM5703: return "5703";
15299 case TG3_PHY_ID_BCM5704: return "5704";
15300 case TG3_PHY_ID_BCM5705: return "5705";
15301 case TG3_PHY_ID_BCM5750: return "5750";
15302 case TG3_PHY_ID_BCM5752: return "5752";
15303 case TG3_PHY_ID_BCM5714: return "5714";
15304 case TG3_PHY_ID_BCM5780: return "5780";
15305 case TG3_PHY_ID_BCM5755: return "5755";
15306 case TG3_PHY_ID_BCM5787: return "5787";
15307 case TG3_PHY_ID_BCM5784: return "5784";
15308 case TG3_PHY_ID_BCM5756: return "5722/5756";
15309 case TG3_PHY_ID_BCM5906: return "5906";
15310 case TG3_PHY_ID_BCM5761: return "5761";
15311 case TG3_PHY_ID_BCM5718C: return "5718C";
15312 case TG3_PHY_ID_BCM5718S: return "5718S";
15313 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000015314 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000015315 case TG3_PHY_ID_BCM5720C: return "5720C";
Matt Carlson79eb6902010-02-17 15:17:03 +000015316 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070015317 case 0: return "serdes";
15318 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070015319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015320}
15321
Michael Chanf9804dd2005-09-27 12:13:10 -070015322static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
15323{
Joe Perches63c3a662011-04-26 08:12:10 +000015324 if (tg3_flag(tp, PCI_EXPRESS)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070015325 strcpy(str, "PCI Express");
15326 return str;
Joe Perches63c3a662011-04-26 08:12:10 +000015327 } else if (tg3_flag(tp, PCIX_MODE)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070015328 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
15329
15330 strcpy(str, "PCIX:");
15331
15332 if ((clock_ctrl == 7) ||
15333 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
15334 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
15335 strcat(str, "133MHz");
15336 else if (clock_ctrl == 0)
15337 strcat(str, "33MHz");
15338 else if (clock_ctrl == 2)
15339 strcat(str, "50MHz");
15340 else if (clock_ctrl == 4)
15341 strcat(str, "66MHz");
15342 else if (clock_ctrl == 6)
15343 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070015344 } else {
15345 strcpy(str, "PCI:");
Joe Perches63c3a662011-04-26 08:12:10 +000015346 if (tg3_flag(tp, PCI_HIGH_SPEED))
Michael Chanf9804dd2005-09-27 12:13:10 -070015347 strcat(str, "66MHz");
15348 else
15349 strcat(str, "33MHz");
15350 }
Joe Perches63c3a662011-04-26 08:12:10 +000015351 if (tg3_flag(tp, PCI_32BIT))
Michael Chanf9804dd2005-09-27 12:13:10 -070015352 strcat(str, ":32-bit");
15353 else
15354 strcat(str, ":64-bit");
15355 return str;
15356}
15357
Michael Chan8c2dc7e2005-12-19 16:26:02 -080015358static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015359{
15360 struct pci_dev *peer;
15361 unsigned int func, devnr = tp->pdev->devfn & ~7;
15362
15363 for (func = 0; func < 8; func++) {
15364 peer = pci_get_slot(tp->pdev->bus, devnr | func);
15365 if (peer && peer != tp->pdev)
15366 break;
15367 pci_dev_put(peer);
15368 }
Michael Chan16fe9d72005-12-13 21:09:54 -080015369 /* 5704 can be configured in single-port mode, set peer to
15370 * tp->pdev in that case.
15371 */
15372 if (!peer) {
15373 peer = tp->pdev;
15374 return peer;
15375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015376
15377 /*
15378 * We don't need to keep the refcount elevated; there's no way
15379 * to remove one half of this device without removing the other
15380 */
15381 pci_dev_put(peer);
15382
15383 return peer;
15384}
15385
David S. Miller15f98502005-05-18 22:49:26 -070015386static void __devinit tg3_init_coal(struct tg3 *tp)
15387{
15388 struct ethtool_coalesce *ec = &tp->coal;
15389
15390 memset(ec, 0, sizeof(*ec));
15391 ec->cmd = ETHTOOL_GCOALESCE;
15392 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
15393 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
15394 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
15395 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
15396 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
15397 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
15398 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
15399 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
15400 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
15401
15402 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
15403 HOSTCC_MODE_CLRTICK_TXBD)) {
15404 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
15405 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
15406 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
15407 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
15408 }
Michael Chand244c892005-07-05 14:42:33 -070015409
Joe Perches63c3a662011-04-26 08:12:10 +000015410 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070015411 ec->rx_coalesce_usecs_irq = 0;
15412 ec->tx_coalesce_usecs_irq = 0;
15413 ec->stats_block_coalesce_usecs = 0;
15414 }
David S. Miller15f98502005-05-18 22:49:26 -070015415}
15416
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080015417static const struct net_device_ops tg3_netdev_ops = {
15418 .ndo_open = tg3_open,
15419 .ndo_stop = tg3_close,
Stephen Hemminger00829822008-11-20 20:14:53 -080015420 .ndo_start_xmit = tg3_start_xmit,
Eric Dumazet511d2222010-07-07 20:44:24 +000015421 .ndo_get_stats64 = tg3_get_stats64,
Stephen Hemminger00829822008-11-20 20:14:53 -080015422 .ndo_validate_addr = eth_validate_addr,
Jiri Pirkoafc4b132011-08-16 06:29:01 +000015423 .ndo_set_rx_mode = tg3_set_rx_mode,
Stephen Hemminger00829822008-11-20 20:14:53 -080015424 .ndo_set_mac_address = tg3_set_mac_addr,
15425 .ndo_do_ioctl = tg3_ioctl,
15426 .ndo_tx_timeout = tg3_tx_timeout,
15427 .ndo_change_mtu = tg3_change_mtu,
Michał Mirosławdc668912011-04-07 03:35:07 +000015428 .ndo_fix_features = tg3_fix_features,
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015429 .ndo_set_features = tg3_set_features,
Stephen Hemminger00829822008-11-20 20:14:53 -080015430#ifdef CONFIG_NET_POLL_CONTROLLER
15431 .ndo_poll_controller = tg3_poll_controller,
15432#endif
15433};
15434
Linus Torvalds1da177e2005-04-16 15:20:36 -070015435static int __devinit tg3_init_one(struct pci_dev *pdev,
15436 const struct pci_device_id *ent)
15437{
Linus Torvalds1da177e2005-04-16 15:20:36 -070015438 struct net_device *dev;
15439 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000015440 int i, err, pm_cap;
15441 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070015442 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080015443 u64 dma_mask, persist_dma_mask;
Michał Mirosławc8f44af2011-11-15 15:29:55 +000015444 netdev_features_t features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015445
Joe Perches05dbe002010-02-17 19:44:19 +000015446 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015447
15448 err = pci_enable_device(pdev);
15449 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015450 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015451 return err;
15452 }
15453
Linus Torvalds1da177e2005-04-16 15:20:36 -070015454 err = pci_request_regions(pdev, DRV_MODULE_NAME);
15455 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015456 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015457 goto err_out_disable_pdev;
15458 }
15459
15460 pci_set_master(pdev);
15461
15462 /* Find power-management capability. */
15463 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
15464 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000015465 dev_err(&pdev->dev,
15466 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015467 err = -EIO;
15468 goto err_out_free_res;
15469 }
15470
Matt Carlson16821282011-07-13 09:27:28 +000015471 err = pci_set_power_state(pdev, PCI_D0);
15472 if (err) {
15473 dev_err(&pdev->dev, "Transition to D0 failed, aborting\n");
15474 goto err_out_free_res;
15475 }
15476
Matt Carlsonfe5f5782009-09-01 13:09:39 +000015477 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015478 if (!dev) {
Matt Carlson2445e462010-04-05 10:19:21 +000015479 dev_err(&pdev->dev, "Etherdev alloc failed, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015480 err = -ENOMEM;
Matt Carlson16821282011-07-13 09:27:28 +000015481 goto err_out_power_down;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015482 }
15483
Linus Torvalds1da177e2005-04-16 15:20:36 -070015484 SET_NETDEV_DEV(dev, &pdev->dev);
15485
Linus Torvalds1da177e2005-04-16 15:20:36 -070015486 tp = netdev_priv(dev);
15487 tp->pdev = pdev;
15488 tp->dev = dev;
15489 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015490 tp->rx_mode = TG3_DEF_RX_MODE;
15491 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070015492
Linus Torvalds1da177e2005-04-16 15:20:36 -070015493 if (tg3_debug > 0)
15494 tp->msg_enable = tg3_debug;
15495 else
15496 tp->msg_enable = TG3_DEF_MSG_ENABLE;
15497
15498 /* The word/byte swap controls here control register access byte
15499 * swapping. DMA data byte swapping is controlled in the GRC_MODE
15500 * setting below.
15501 */
15502 tp->misc_host_ctrl =
15503 MISC_HOST_CTRL_MASK_PCI_INT |
15504 MISC_HOST_CTRL_WORD_SWAP |
15505 MISC_HOST_CTRL_INDIR_ACCESS |
15506 MISC_HOST_CTRL_PCISTATE_RW;
15507
15508 /* The NONFRM (non-frame) byte/word swap controls take effect
15509 * on descriptor entries, anything which isn't packet data.
15510 *
15511 * The StrongARM chips on the board (one for tx, one for rx)
15512 * are running in big-endian mode.
15513 */
15514 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
15515 GRC_MODE_WSWAP_NONFRM_DATA);
15516#ifdef __BIG_ENDIAN
15517 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
15518#endif
15519 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015520 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000015521 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015522
Matt Carlsond5fe4882008-11-21 17:20:32 -080015523 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010015524 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015525 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015526 err = -ENOMEM;
15527 goto err_out_free_dev;
15528 }
15529
Matt Carlsonc9cab242011-07-13 09:27:27 +000015530 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
15531 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761E ||
15532 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S ||
15533 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761SE ||
15534 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
15535 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
15536 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
15537 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720) {
15538 tg3_flag_set(tp, ENABLE_APE);
15539 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
15540 if (!tp->aperegs) {
15541 dev_err(&pdev->dev,
15542 "Cannot map APE registers, aborting\n");
15543 err = -ENOMEM;
15544 goto err_out_iounmap;
15545 }
15546 }
15547
Linus Torvalds1da177e2005-04-16 15:20:36 -070015548 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
15549 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015550
Linus Torvalds1da177e2005-04-16 15:20:36 -070015551 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015552 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Matt Carlson2ffcc982011-05-19 12:12:44 +000015553 dev->netdev_ops = &tg3_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015554 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015555
15556 err = tg3_get_invariants(tp);
15557 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015558 dev_err(&pdev->dev,
15559 "Problem fetching invariants of chip, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015560 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015561 }
15562
Michael Chan4a29cc22006-03-19 13:21:12 -080015563 /* The EPB bridge inside 5714, 5715, and 5780 and any
15564 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080015565 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
15566 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
15567 * do DMA address check in tg3_start_xmit().
15568 */
Joe Perches63c3a662011-04-26 08:12:10 +000015569 if (tg3_flag(tp, IS_5788))
Yang Hongyang284901a2009-04-06 19:01:15 -070015570 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Joe Perches63c3a662011-04-26 08:12:10 +000015571 else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070015572 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080015573#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070015574 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015575#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080015576 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070015577 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015578
15579 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070015580 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080015581 err = pci_set_dma_mask(pdev, dma_mask);
15582 if (!err) {
Matt Carlson0da06062011-05-19 12:12:53 +000015583 features |= NETIF_F_HIGHDMA;
Michael Chan72f2afb2006-03-06 19:28:35 -080015584 err = pci_set_consistent_dma_mask(pdev,
15585 persist_dma_mask);
15586 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015587 dev_err(&pdev->dev, "Unable to obtain 64 bit "
15588 "DMA for consistent allocations\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015589 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080015590 }
15591 }
15592 }
Yang Hongyang284901a2009-04-06 19:01:15 -070015593 if (err || dma_mask == DMA_BIT_MASK(32)) {
15594 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080015595 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015596 dev_err(&pdev->dev,
15597 "No usable DMA configuration, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015598 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080015599 }
15600 }
15601
Michael Chanfdfec1722005-07-25 12:31:48 -070015602 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015603
Matt Carlson0da06062011-05-19 12:12:53 +000015604 features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
15605
15606 /* 5700 B0 chips do not support checksumming correctly due
15607 * to hardware bugs.
15608 */
15609 if (tp->pci_chip_rev_id != CHIPREV_ID_5700_B0) {
15610 features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
15611
15612 if (tg3_flag(tp, 5755_PLUS))
15613 features |= NETIF_F_IPV6_CSUM;
15614 }
15615
Michael Chan4e3a7aa2006-03-20 17:47:44 -080015616 /* TSO is on by default on chips that support hardware TSO.
15617 * Firmware TSO on older chips gives lower performance, so it
15618 * is off by default, but can be enabled using ethtool.
15619 */
Joe Perches63c3a662011-04-26 08:12:10 +000015620 if ((tg3_flag(tp, HW_TSO_1) ||
15621 tg3_flag(tp, HW_TSO_2) ||
15622 tg3_flag(tp, HW_TSO_3)) &&
Matt Carlson0da06062011-05-19 12:12:53 +000015623 (features & NETIF_F_IP_CSUM))
15624 features |= NETIF_F_TSO;
Joe Perches63c3a662011-04-26 08:12:10 +000015625 if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
Matt Carlson0da06062011-05-19 12:12:53 +000015626 if (features & NETIF_F_IPV6_CSUM)
15627 features |= NETIF_F_TSO6;
Joe Perches63c3a662011-04-26 08:12:10 +000015628 if (tg3_flag(tp, HW_TSO_3) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000015629 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070015630 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
15631 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Joe Perches63c3a662011-04-26 08:12:10 +000015632 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Michał Mirosławdc668912011-04-07 03:35:07 +000015633 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson0da06062011-05-19 12:12:53 +000015634 features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070015635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015636
Matt Carlsond542fe22011-05-19 16:02:43 +000015637 dev->features |= features;
15638 dev->vlan_features |= features;
15639
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015640 /*
15641 * Add loopback capability only for a subset of devices that support
15642 * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
15643 * loopback for the remaining devices.
15644 */
15645 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
15646 !tg3_flag(tp, CPMU_PRESENT))
15647 /* Add the loopback capability */
Matt Carlson0da06062011-05-19 12:12:53 +000015648 features |= NETIF_F_LOOPBACK;
15649
Matt Carlson0da06062011-05-19 12:12:53 +000015650 dev->hw_features |= features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015651
Linus Torvalds1da177e2005-04-16 15:20:36 -070015652 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015653 !tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015654 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015655 tg3_flag_set(tp, MAX_RXPEND_64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015656 tp->rx_pending = 63;
15657 }
15658
Linus Torvalds1da177e2005-04-16 15:20:36 -070015659 err = tg3_get_device_address(tp);
15660 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015661 dev_err(&pdev->dev,
15662 "Could not obtain valid ethernet address, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015663 goto err_out_apeunmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070015664 }
15665
Matt Carlsonc88864d2007-11-12 21:07:01 -080015666 /*
15667 * Reset chip in case UNDI or EFI driver did not shutdown
15668 * DMA self test will enable WDMAC and we'll see (spurious)
15669 * pending DMA on the PCI bus at that point.
15670 */
15671 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
15672 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
15673 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
15674 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
15675 }
15676
15677 err = tg3_test_dma(tp);
15678 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015679 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080015680 goto err_out_apeunmap;
15681 }
15682
Matt Carlson78f90dc2009-11-13 13:03:42 +000015683 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
15684 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
15685 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000015686 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000015687 struct tg3_napi *tnapi = &tp->napi[i];
15688
15689 tnapi->tp = tp;
15690 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
15691
15692 tnapi->int_mbox = intmbx;
Matt Carlson93a700a2011-08-31 11:44:54 +000015693 if (i <= 4)
Matt Carlson78f90dc2009-11-13 13:03:42 +000015694 intmbx += 0x8;
15695 else
15696 intmbx += 0x4;
15697
15698 tnapi->consmbox = rcvmbx;
15699 tnapi->prodmbox = sndmbx;
15700
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015701 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000015702 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015703 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000015704 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000015705
Joe Perches63c3a662011-04-26 08:12:10 +000015706 if (!tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson78f90dc2009-11-13 13:03:42 +000015707 break;
15708
15709 /*
15710 * If we support MSIX, we'll be using RSS. If we're using
15711 * RSS, the first vector only handles link interrupts and the
15712 * remaining vectors handle rx and tx interrupts. Reuse the
15713 * mailbox values for the next iteration. The values we setup
15714 * above are still useful for the single vectored mode.
15715 */
15716 if (!i)
15717 continue;
15718
15719 rcvmbx += 0x8;
15720
15721 if (sndmbx & 0x4)
15722 sndmbx -= 0x4;
15723 else
15724 sndmbx += 0xc;
15725 }
15726
Matt Carlsonc88864d2007-11-12 21:07:01 -080015727 tg3_init_coal(tp);
15728
Michael Chanc49a1562006-12-17 17:07:29 -080015729 pci_set_drvdata(pdev, dev);
15730
Matt Carlsoncd0d7222011-07-13 09:27:33 +000015731 if (tg3_flag(tp, 5717_PLUS)) {
15732 /* Resume a low-power mode */
15733 tg3_frob_aux_power(tp, false);
15734 }
15735
Linus Torvalds1da177e2005-04-16 15:20:36 -070015736 err = register_netdev(dev);
15737 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015738 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070015739 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015740 }
15741
Joe Perches05dbe002010-02-17 19:44:19 +000015742 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
15743 tp->board_part_number,
15744 tp->pci_chip_rev_id,
15745 tg3_bus_string(tp, str),
15746 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015747
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015748 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000015749 struct phy_device *phydev;
15750 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000015751 netdev_info(dev,
15752 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000015753 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015754 } else {
15755 char *ethtype;
15756
15757 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
15758 ethtype = "10/100Base-TX";
15759 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
15760 ethtype = "1000Base-SX";
15761 else
15762 ethtype = "10/100/1000Base-T";
15763
Matt Carlson5129c3a2010-04-05 10:19:23 +000015764 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlson47007832011-04-20 07:57:43 +000015765 "(WireSpeed[%d], EEE[%d])\n",
15766 tg3_phy_string(tp), ethtype,
15767 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
15768 (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015769 }
Matt Carlsondf59c942008-11-03 16:52:56 -080015770
Joe Perches05dbe002010-02-17 19:44:19 +000015771 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000015772 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015773 tg3_flag(tp, USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015774 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015775 tg3_flag(tp, ENABLE_ASF) != 0,
15776 tg3_flag(tp, TSO_CAPABLE) != 0);
Joe Perches05dbe002010-02-17 19:44:19 +000015777 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
15778 tp->dma_rwctrl,
15779 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
15780 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015781
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015782 pci_save_state(pdev);
15783
Linus Torvalds1da177e2005-04-16 15:20:36 -070015784 return 0;
15785
Matt Carlson0d3031d2007-10-10 18:02:43 -070015786err_out_apeunmap:
15787 if (tp->aperegs) {
15788 iounmap(tp->aperegs);
15789 tp->aperegs = NULL;
15790 }
15791
Linus Torvalds1da177e2005-04-16 15:20:36 -070015792err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070015793 if (tp->regs) {
15794 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015795 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015797
15798err_out_free_dev:
15799 free_netdev(dev);
15800
Matt Carlson16821282011-07-13 09:27:28 +000015801err_out_power_down:
15802 pci_set_power_state(pdev, PCI_D3hot);
15803
Linus Torvalds1da177e2005-04-16 15:20:36 -070015804err_out_free_res:
15805 pci_release_regions(pdev);
15806
15807err_out_disable_pdev:
15808 pci_disable_device(pdev);
15809 pci_set_drvdata(pdev, NULL);
15810 return err;
15811}
15812
15813static void __devexit tg3_remove_one(struct pci_dev *pdev)
15814{
15815 struct net_device *dev = pci_get_drvdata(pdev);
15816
15817 if (dev) {
15818 struct tg3 *tp = netdev_priv(dev);
15819
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080015820 if (tp->fw)
15821 release_firmware(tp->fw);
15822
Matt Carlsondb219972011-11-04 09:15:03 +000015823 tg3_reset_task_cancel(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015824
David S. Miller1805b2f2011-10-24 18:18:09 -040015825 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015826 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015827 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015828 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070015829
Linus Torvalds1da177e2005-04-16 15:20:36 -070015830 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015831 if (tp->aperegs) {
15832 iounmap(tp->aperegs);
15833 tp->aperegs = NULL;
15834 }
Michael Chan68929142005-08-09 20:17:14 -070015835 if (tp->regs) {
15836 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015837 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015838 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015839 free_netdev(dev);
15840 pci_release_regions(pdev);
15841 pci_disable_device(pdev);
15842 pci_set_drvdata(pdev, NULL);
15843 }
15844}
15845
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015846#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015847static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015848{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015849 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015850 struct net_device *dev = pci_get_drvdata(pdev);
15851 struct tg3 *tp = netdev_priv(dev);
15852 int err;
15853
15854 if (!netif_running(dev))
15855 return 0;
15856
Matt Carlsondb219972011-11-04 09:15:03 +000015857 tg3_reset_task_cancel(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015858 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015859 tg3_netif_stop(tp);
15860
15861 del_timer_sync(&tp->timer);
15862
David S. Millerf47c11e2005-06-24 20:18:35 -070015863 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015864 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070015865 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015866
15867 netif_device_detach(dev);
15868
David S. Millerf47c11e2005-06-24 20:18:35 -070015869 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070015870 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches63c3a662011-04-26 08:12:10 +000015871 tg3_flag_clear(tp, INIT_COMPLETE);
David S. Millerf47c11e2005-06-24 20:18:35 -070015872 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015873
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015874 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015875 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015876 int err2;
15877
David S. Millerf47c11e2005-06-24 20:18:35 -070015878 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015879
Joe Perches63c3a662011-04-26 08:12:10 +000015880 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015881 err2 = tg3_restart_hw(tp, 1);
15882 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070015883 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015884
15885 tp->timer.expires = jiffies + tp->timer_offset;
15886 add_timer(&tp->timer);
15887
15888 netif_device_attach(dev);
15889 tg3_netif_start(tp);
15890
Michael Chanb9ec6c12006-07-25 16:37:27 -070015891out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015892 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015893
15894 if (!err2)
15895 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015896 }
15897
15898 return err;
15899}
15900
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015901static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015902{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015903 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015904 struct net_device *dev = pci_get_drvdata(pdev);
15905 struct tg3 *tp = netdev_priv(dev);
15906 int err;
15907
15908 if (!netif_running(dev))
15909 return 0;
15910
Linus Torvalds1da177e2005-04-16 15:20:36 -070015911 netif_device_attach(dev);
15912
David S. Millerf47c11e2005-06-24 20:18:35 -070015913 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015914
Joe Perches63c3a662011-04-26 08:12:10 +000015915 tg3_flag_set(tp, INIT_COMPLETE);
Michael Chanb9ec6c12006-07-25 16:37:27 -070015916 err = tg3_restart_hw(tp, 1);
15917 if (err)
15918 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015919
15920 tp->timer.expires = jiffies + tp->timer_offset;
15921 add_timer(&tp->timer);
15922
Linus Torvalds1da177e2005-04-16 15:20:36 -070015923 tg3_netif_start(tp);
15924
Michael Chanb9ec6c12006-07-25 16:37:27 -070015925out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015926 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015927
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015928 if (!err)
15929 tg3_phy_start(tp);
15930
Michael Chanb9ec6c12006-07-25 16:37:27 -070015931 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015932}
15933
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015934static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015935#define TG3_PM_OPS (&tg3_pm_ops)
15936
15937#else
15938
15939#define TG3_PM_OPS NULL
15940
15941#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015942
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015943/**
15944 * tg3_io_error_detected - called when PCI error is detected
15945 * @pdev: Pointer to PCI device
15946 * @state: The current pci connection state
15947 *
15948 * This function is called after a PCI bus error affecting
15949 * this device has been detected.
15950 */
15951static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
15952 pci_channel_state_t state)
15953{
15954 struct net_device *netdev = pci_get_drvdata(pdev);
15955 struct tg3 *tp = netdev_priv(netdev);
15956 pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
15957
15958 netdev_info(netdev, "PCI I/O error detected\n");
15959
15960 rtnl_lock();
15961
15962 if (!netif_running(netdev))
15963 goto done;
15964
15965 tg3_phy_stop(tp);
15966
15967 tg3_netif_stop(tp);
15968
15969 del_timer_sync(&tp->timer);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015970
15971 /* Want to make sure that the reset task doesn't run */
Matt Carlsondb219972011-11-04 09:15:03 +000015972 tg3_reset_task_cancel(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000015973 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015974
15975 netif_device_detach(netdev);
15976
15977 /* Clean up software state, even if MMIO is blocked */
15978 tg3_full_lock(tp, 0);
15979 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
15980 tg3_full_unlock(tp);
15981
15982done:
15983 if (state == pci_channel_io_perm_failure)
15984 err = PCI_ERS_RESULT_DISCONNECT;
15985 else
15986 pci_disable_device(pdev);
15987
15988 rtnl_unlock();
15989
15990 return err;
15991}
15992
15993/**
15994 * tg3_io_slot_reset - called after the pci bus has been reset.
15995 * @pdev: Pointer to PCI device
15996 *
15997 * Restart the card from scratch, as if from a cold-boot.
15998 * At this point, the card has exprienced a hard reset,
15999 * followed by fixups by BIOS, and has its config space
16000 * set up identically to what it was at cold boot.
16001 */
16002static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
16003{
16004 struct net_device *netdev = pci_get_drvdata(pdev);
16005 struct tg3 *tp = netdev_priv(netdev);
16006 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
16007 int err;
16008
16009 rtnl_lock();
16010
16011 if (pci_enable_device(pdev)) {
16012 netdev_err(netdev, "Cannot re-enable PCI device after reset.\n");
16013 goto done;
16014 }
16015
16016 pci_set_master(pdev);
16017 pci_restore_state(pdev);
16018 pci_save_state(pdev);
16019
16020 if (!netif_running(netdev)) {
16021 rc = PCI_ERS_RESULT_RECOVERED;
16022 goto done;
16023 }
16024
16025 err = tg3_power_up(tp);
Matt Carlsonbed98292011-07-13 09:27:29 +000016026 if (err)
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016027 goto done;
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016028
16029 rc = PCI_ERS_RESULT_RECOVERED;
16030
16031done:
16032 rtnl_unlock();
16033
16034 return rc;
16035}
16036
16037/**
16038 * tg3_io_resume - called when traffic can start flowing again.
16039 * @pdev: Pointer to PCI device
16040 *
16041 * This callback is called when the error recovery driver tells
16042 * us that its OK to resume normal operation.
16043 */
16044static void tg3_io_resume(struct pci_dev *pdev)
16045{
16046 struct net_device *netdev = pci_get_drvdata(pdev);
16047 struct tg3 *tp = netdev_priv(netdev);
16048 int err;
16049
16050 rtnl_lock();
16051
16052 if (!netif_running(netdev))
16053 goto done;
16054
16055 tg3_full_lock(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +000016056 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016057 err = tg3_restart_hw(tp, 1);
16058 tg3_full_unlock(tp);
16059 if (err) {
16060 netdev_err(netdev, "Cannot restart hardware after reset.\n");
16061 goto done;
16062 }
16063
16064 netif_device_attach(netdev);
16065
16066 tp->timer.expires = jiffies + tp->timer_offset;
16067 add_timer(&tp->timer);
16068
16069 tg3_netif_start(tp);
16070
16071 tg3_phy_start(tp);
16072
16073done:
16074 rtnl_unlock();
16075}
16076
16077static struct pci_error_handlers tg3_err_handler = {
16078 .error_detected = tg3_io_error_detected,
16079 .slot_reset = tg3_io_slot_reset,
16080 .resume = tg3_io_resume
16081};
16082
Linus Torvalds1da177e2005-04-16 15:20:36 -070016083static struct pci_driver tg3_driver = {
16084 .name = DRV_MODULE_NAME,
16085 .id_table = tg3_pci_tbl,
16086 .probe = tg3_init_one,
16087 .remove = __devexit_p(tg3_remove_one),
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016088 .err_handler = &tg3_err_handler,
Eric Dumazetaa6027c2011-01-01 05:22:46 +000016089 .driver.pm = TG3_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070016090};
16091
16092static int __init tg3_init(void)
16093{
Jeff Garzik29917622006-08-19 17:48:59 -040016094 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016095}
16096
16097static void __exit tg3_cleanup(void)
16098{
16099 pci_unregister_driver(&tg3_driver);
16100}
16101
16102module_init(tg3_init);
16103module_exit(tg3_cleanup);