blob: 9c9a4b4a9f6925160957e6eb52552303573380ef [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
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000138#define TG3_RSS_INDIR_TBL_SIZE 128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140/* Do not place this n-ring entries value into the tp struct itself,
141 * we really want to expose these constants to GCC so that modulo et
142 * al. operations are done with shifts and masks instead of with
143 * hw multiply/modulo instructions. Another solution would be to
144 * replace things like '% foo' with '& (foo - 1)'.
145 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147#define TG3_TX_RING_SIZE 512
148#define TG3_DEF_TX_RING_PENDING (TG3_TX_RING_SIZE - 1)
149
Matt Carlson2c49a442010-09-30 10:34:35 +0000150#define TG3_RX_STD_RING_BYTES(tp) \
151 (sizeof(struct tg3_rx_buffer_desc) * TG3_RX_STD_RING_SIZE(tp))
152#define TG3_RX_JMB_RING_BYTES(tp) \
153 (sizeof(struct tg3_ext_rx_buffer_desc) * TG3_RX_JMB_RING_SIZE(tp))
154#define TG3_RX_RCB_RING_BYTES(tp) \
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000155 (sizeof(struct tg3_rx_buffer_desc) * (tp->rx_ret_ring_mask + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156#define TG3_TX_RING_BYTES (sizeof(struct tg3_tx_buffer_desc) * \
157 TG3_TX_RING_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158#define NEXT_TX(N) (((N) + 1) & (TG3_TX_RING_SIZE - 1))
159
Matt Carlson287be122009-08-28 13:58:46 +0000160#define TG3_DMA_BYTE_ENAB 64
161
162#define TG3_RX_STD_DMA_SZ 1536
163#define TG3_RX_JMB_DMA_SZ 9046
164
165#define TG3_RX_DMA_TO_MAP_SZ(x) ((x) + TG3_DMA_BYTE_ENAB)
166
167#define TG3_RX_STD_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_STD_DMA_SZ)
168#define TG3_RX_JMB_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_JMB_DMA_SZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Matt Carlson2c49a442010-09-30 10:34:35 +0000170#define TG3_RX_STD_BUFF_RING_SIZE(tp) \
171 (sizeof(struct ring_info) * TG3_RX_STD_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000172
Matt Carlson2c49a442010-09-30 10:34:35 +0000173#define TG3_RX_JMB_BUFF_RING_SIZE(tp) \
174 (sizeof(struct ring_info) * TG3_RX_JMB_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000175
Matt Carlsond2757fc2010-04-12 06:58:27 +0000176/* Due to a hardware bug, the 5701 can only DMA to memory addresses
177 * that are at least dword aligned when used in PCIX mode. The driver
178 * works around this bug by double copying the packet. This workaround
179 * is built into the normal double copy length check for efficiency.
180 *
181 * However, the double copy is only necessary on those architectures
182 * where unaligned memory accesses are inefficient. For those architectures
183 * where unaligned memory accesses incur little penalty, we can reintegrate
184 * the 5701 in the normal rx path. Doing so saves a device structure
185 * dereference by hardcoding the double copy threshold in place.
186 */
187#define TG3_RX_COPY_THRESHOLD 256
188#if NET_IP_ALIGN == 0 || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
189 #define TG3_RX_COPY_THRESH(tp) TG3_RX_COPY_THRESHOLD
190#else
191 #define TG3_RX_COPY_THRESH(tp) ((tp)->rx_copy_thresh)
192#endif
193
Matt Carlson81389f52011-08-31 11:44:49 +0000194#if (NET_IP_ALIGN != 0)
195#define TG3_RX_OFFSET(tp) ((tp)->rx_offset)
196#else
Eric Dumazet9205fd92011-11-18 06:47:01 +0000197#define TG3_RX_OFFSET(tp) (NET_SKB_PAD)
Matt Carlson81389f52011-08-31 11:44:49 +0000198#endif
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200/* minimum number of free TX descriptors required to wake up TX process */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000201#define TG3_TX_WAKEUP_THRESH(tnapi) ((tnapi)->tx_pending / 4)
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 Carlsonf92d9dc2010-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 Carlsonf92d9dc2010-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 Carlsonf92d9dc2010-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 Carlsonf92d9dc2010-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 Carlsoncdd4e092009-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
1673static u16 tg3_advert_flowctrl_1000T(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_PAUSE_CAP;
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_PAUSE_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_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1683 else
1684 miireg = 0;
1685
1686 return miireg;
1687}
1688
1689static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1690{
1691 u16 miireg;
1692
Steve Glendinninge18ce342008-12-16 02:00:00 -08001693 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001694 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001695 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001696 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001697 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001698 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1699 else
1700 miireg = 0;
1701
1702 return miireg;
1703}
1704
Matt Carlson95e28692008-05-25 23:44:14 -07001705static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1706{
1707 u8 cap = 0;
1708
Matt Carlsonf3791cd2011-11-21 15:01:17 +00001709 if (lcladv & rmtadv & ADVERTISE_1000XPAUSE) {
1710 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
1711 } else if (lcladv & rmtadv & ADVERTISE_1000XPSE_ASYM) {
1712 if (lcladv & ADVERTISE_1000XPAUSE)
1713 cap = FLOW_CTRL_RX;
1714 if (rmtadv & ADVERTISE_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001715 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001716 }
1717
1718 return cap;
1719}
1720
Matt Carlsonf51f3562008-05-25 23:45:08 -07001721static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001722{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001723 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001724 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001725 u32 old_rx_mode = tp->rx_mode;
1726 u32 old_tx_mode = tp->tx_mode;
1727
Joe Perches63c3a662011-04-26 08:12:10 +00001728 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001729 autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001730 else
1731 autoneg = tp->link_config.autoneg;
1732
Joe Perches63c3a662011-04-26 08:12:10 +00001733 if (autoneg == AUTONEG_ENABLE && tg3_flag(tp, PAUSE_AUTONEG)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001734 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001735 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001736 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001737 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001738 } else
1739 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001740
Matt Carlsonf51f3562008-05-25 23:45:08 -07001741 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001742
Steve Glendinninge18ce342008-12-16 02:00:00 -08001743 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001744 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1745 else
1746 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1747
Matt Carlsonf51f3562008-05-25 23:45:08 -07001748 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001749 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001750
Steve Glendinninge18ce342008-12-16 02:00:00 -08001751 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001752 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1753 else
1754 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1755
Matt Carlsonf51f3562008-05-25 23:45:08 -07001756 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001757 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001758}
1759
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001760static void tg3_adjust_link(struct net_device *dev)
1761{
1762 u8 oldflowctrl, linkmesg = 0;
1763 u32 mac_mode, lcl_adv, rmt_adv;
1764 struct tg3 *tp = netdev_priv(dev);
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001765 struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001766
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001767 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001768
1769 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
1770 MAC_MODE_HALF_DUPLEX);
1771
1772 oldflowctrl = tp->link_config.active_flowctrl;
1773
1774 if (phydev->link) {
1775 lcl_adv = 0;
1776 rmt_adv = 0;
1777
1778 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
1779 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001780 else if (phydev->speed == SPEED_1000 ||
1781 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001782 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001783 else
1784 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001785
1786 if (phydev->duplex == DUPLEX_HALF)
1787 mac_mode |= MAC_MODE_HALF_DUPLEX;
1788 else {
1789 lcl_adv = tg3_advert_flowctrl_1000T(
1790 tp->link_config.flowctrl);
1791
1792 if (phydev->pause)
1793 rmt_adv = LPA_PAUSE_CAP;
1794 if (phydev->asym_pause)
1795 rmt_adv |= LPA_PAUSE_ASYM;
1796 }
1797
1798 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
1799 } else
1800 mac_mode |= MAC_MODE_PORT_MODE_GMII;
1801
1802 if (mac_mode != tp->mac_mode) {
1803 tp->mac_mode = mac_mode;
1804 tw32_f(MAC_MODE, tp->mac_mode);
1805 udelay(40);
1806 }
1807
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001808 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
1809 if (phydev->speed == SPEED_10)
1810 tw32(MAC_MI_STAT,
1811 MAC_MI_STAT_10MBPS_MODE |
1812 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1813 else
1814 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1815 }
1816
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001817 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
1818 tw32(MAC_TX_LENGTHS,
1819 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1820 (6 << TX_LENGTHS_IPG_SHIFT) |
1821 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
1822 else
1823 tw32(MAC_TX_LENGTHS,
1824 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1825 (6 << TX_LENGTHS_IPG_SHIFT) |
1826 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
1827
1828 if ((phydev->link && tp->link_config.active_speed == SPEED_INVALID) ||
1829 (!phydev->link && tp->link_config.active_speed != SPEED_INVALID) ||
1830 phydev->speed != tp->link_config.active_speed ||
1831 phydev->duplex != tp->link_config.active_duplex ||
1832 oldflowctrl != tp->link_config.active_flowctrl)
Matt Carlsonc6cdf432010-04-05 10:19:26 +00001833 linkmesg = 1;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001834
1835 tp->link_config.active_speed = phydev->speed;
1836 tp->link_config.active_duplex = phydev->duplex;
1837
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001838 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001839
1840 if (linkmesg)
1841 tg3_link_report(tp);
1842}
1843
1844static int tg3_phy_init(struct tg3 *tp)
1845{
1846 struct phy_device *phydev;
1847
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001848 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001849 return 0;
1850
1851 /* Bring the PHY back to a known state. */
1852 tg3_bmcr_reset(tp);
1853
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001854 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001855
1856 /* Attach the MAC to the PHY. */
Kay Sieversfb28ad32008-11-10 13:55:14 -08001857 phydev = phy_connect(tp->dev, dev_name(&phydev->dev), tg3_adjust_link,
Matt Carlsona9daf362008-05-25 23:49:44 -07001858 phydev->dev_flags, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001859 if (IS_ERR(phydev)) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001860 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001861 return PTR_ERR(phydev);
1862 }
1863
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001864 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001865 switch (phydev->interface) {
1866 case PHY_INTERFACE_MODE_GMII:
1867 case PHY_INTERFACE_MODE_RGMII:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001868 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Matt Carlson321d32a2008-11-21 17:22:19 -08001869 phydev->supported &= (PHY_GBIT_FEATURES |
1870 SUPPORTED_Pause |
1871 SUPPORTED_Asym_Pause);
1872 break;
1873 }
1874 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001875 case PHY_INTERFACE_MODE_MII:
1876 phydev->supported &= (PHY_BASIC_FEATURES |
1877 SUPPORTED_Pause |
1878 SUPPORTED_Asym_Pause);
1879 break;
1880 default:
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001881 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001882 return -EINVAL;
1883 }
1884
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001885 tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001886
1887 phydev->advertising = phydev->supported;
1888
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001889 return 0;
1890}
1891
1892static void tg3_phy_start(struct tg3 *tp)
1893{
1894 struct phy_device *phydev;
1895
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001896 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001897 return;
1898
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001899 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001900
Matt Carlson80096062010-08-02 11:26:06 +00001901 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
1902 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001903 phydev->speed = tp->link_config.orig_speed;
1904 phydev->duplex = tp->link_config.orig_duplex;
1905 phydev->autoneg = tp->link_config.orig_autoneg;
1906 phydev->advertising = tp->link_config.orig_advertising;
1907 }
1908
1909 phy_start(phydev);
1910
1911 phy_start_aneg(phydev);
1912}
1913
1914static void tg3_phy_stop(struct tg3 *tp)
1915{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001916 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001917 return;
1918
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001919 phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001920}
1921
1922static void tg3_phy_fini(struct tg3 *tp)
1923{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001924 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001925 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001926 tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001927 }
1928}
1929
Matt Carlson941ec902011-08-19 13:58:23 +00001930static int tg3_phy_set_extloopbk(struct tg3 *tp)
1931{
1932 int err;
1933 u32 val;
1934
1935 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
1936 return 0;
1937
1938 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
1939 /* Cannot do read-modify-write on 5401 */
1940 err = tg3_phy_auxctl_write(tp,
1941 MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
1942 MII_TG3_AUXCTL_ACTL_EXTLOOPBK |
1943 0x4c20);
1944 goto done;
1945 }
1946
1947 err = tg3_phy_auxctl_read(tp,
1948 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
1949 if (err)
1950 return err;
1951
1952 val |= MII_TG3_AUXCTL_ACTL_EXTLOOPBK;
1953 err = tg3_phy_auxctl_write(tp,
1954 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, val);
1955
1956done:
1957 return err;
1958}
1959
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001960static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
1961{
1962 u32 phytest;
1963
1964 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
1965 u32 phy;
1966
1967 tg3_writephy(tp, MII_TG3_FET_TEST,
1968 phytest | MII_TG3_FET_SHADOW_EN);
1969 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
1970 if (enable)
1971 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
1972 else
1973 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
1974 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
1975 }
1976 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
1977 }
1978}
1979
Matt Carlson6833c042008-11-21 17:18:59 -08001980static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
1981{
1982 u32 reg;
1983
Joe Perches63c3a662011-04-26 08:12:10 +00001984 if (!tg3_flag(tp, 5705_PLUS) ||
1985 (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001986 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08001987 return;
1988
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001989 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001990 tg3_phy_fet_toggle_apd(tp, enable);
1991 return;
1992 }
1993
Matt Carlson6833c042008-11-21 17:18:59 -08001994 reg = MII_TG3_MISC_SHDW_WREN |
1995 MII_TG3_MISC_SHDW_SCR5_SEL |
1996 MII_TG3_MISC_SHDW_SCR5_LPED |
1997 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
1998 MII_TG3_MISC_SHDW_SCR5_SDTL |
1999 MII_TG3_MISC_SHDW_SCR5_C125OE;
2000 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 || !enable)
2001 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
2002
2003 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
2004
2005
2006 reg = MII_TG3_MISC_SHDW_WREN |
2007 MII_TG3_MISC_SHDW_APD_SEL |
2008 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
2009 if (enable)
2010 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
2011
2012 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
2013}
2014
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002015static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
2016{
2017 u32 phy;
2018
Joe Perches63c3a662011-04-26 08:12:10 +00002019 if (!tg3_flag(tp, 5705_PLUS) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002020 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002021 return;
2022
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002023 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002024 u32 ephy;
2025
Matt Carlson535ef6e2009-08-25 10:09:36 +00002026 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
2027 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
2028
2029 tg3_writephy(tp, MII_TG3_FET_TEST,
2030 ephy | MII_TG3_FET_SHADOW_EN);
2031 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002032 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00002033 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002034 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00002035 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
2036 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002037 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00002038 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002039 }
2040 } else {
Matt Carlson15ee95c2011-04-20 07:57:40 +00002041 int ret;
2042
2043 ret = tg3_phy_auxctl_read(tp,
2044 MII_TG3_AUXCTL_SHDWSEL_MISC, &phy);
2045 if (!ret) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002046 if (enable)
2047 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
2048 else
2049 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002050 tg3_phy_auxctl_write(tp,
2051 MII_TG3_AUXCTL_SHDWSEL_MISC, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002052 }
2053 }
2054}
2055
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056static void tg3_phy_set_wirespeed(struct tg3 *tp)
2057{
Matt Carlson15ee95c2011-04-20 07:57:40 +00002058 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 u32 val;
2060
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002061 if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 return;
2063
Matt Carlson15ee95c2011-04-20 07:57:40 +00002064 ret = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_MISC, &val);
2065 if (!ret)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002066 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_MISC,
2067 val | MII_TG3_AUXCTL_MISC_WIRESPD_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068}
2069
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002070static void tg3_phy_apply_otp(struct tg3 *tp)
2071{
2072 u32 otp, phy;
2073
2074 if (!tp->phy_otp)
2075 return;
2076
2077 otp = tp->phy_otp;
2078
Matt Carlson1d36ba42011-04-20 07:57:42 +00002079 if (TG3_PHY_AUXCTL_SMDSP_ENABLE(tp))
2080 return;
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002081
2082 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
2083 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
2084 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
2085
2086 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
2087 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
2088 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
2089
2090 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
2091 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
2092 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
2093
2094 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
2095 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
2096
2097 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
2098 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
2099
2100 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
2101 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
2102 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
2103
Matt Carlson1d36ba42011-04-20 07:57:42 +00002104 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002105}
2106
Matt Carlson52b02d02010-10-14 10:37:41 +00002107static void tg3_phy_eee_adjust(struct tg3 *tp, u32 current_link_up)
2108{
2109 u32 val;
2110
2111 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
2112 return;
2113
2114 tp->setlpicnt = 0;
2115
2116 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
2117 current_link_up == 1 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +00002118 tp->link_config.active_duplex == DUPLEX_FULL &&
2119 (tp->link_config.active_speed == SPEED_100 ||
2120 tp->link_config.active_speed == SPEED_1000)) {
Matt Carlson52b02d02010-10-14 10:37:41 +00002121 u32 eeectl;
2122
2123 if (tp->link_config.active_speed == SPEED_1000)
2124 eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
2125 else
2126 eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
2127
2128 tw32(TG3_CPMU_EEE_CTRL, eeectl);
2129
Matt Carlson3110f5f52010-12-06 08:28:50 +00002130 tg3_phy_cl45_read(tp, MDIO_MMD_AN,
2131 TG3_CL45_D7_EEERES_STAT, &val);
Matt Carlson52b02d02010-10-14 10:37:41 +00002132
Matt Carlsonb0c59432011-05-19 12:12:48 +00002133 if (val == TG3_CL45_D7_EEERES_STAT_LP_1000T ||
2134 val == TG3_CL45_D7_EEERES_STAT_LP_100TX)
Matt Carlson52b02d02010-10-14 10:37:41 +00002135 tp->setlpicnt = 2;
2136 }
2137
2138 if (!tp->setlpicnt) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002139 if (current_link_up == 1 &&
2140 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2141 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0000);
2142 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2143 }
2144
Matt Carlson52b02d02010-10-14 10:37:41 +00002145 val = tr32(TG3_CPMU_EEE_MODE);
2146 tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
2147 }
2148}
2149
Matt Carlsonb0c59432011-05-19 12:12:48 +00002150static void tg3_phy_eee_enable(struct tg3 *tp)
2151{
2152 u32 val;
2153
2154 if (tp->link_config.active_speed == SPEED_1000 &&
2155 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2156 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
2157 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) &&
2158 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002159 val = MII_TG3_DSP_TAP26_ALNOKO |
2160 MII_TG3_DSP_TAP26_RMRXSTO;
2161 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
Matt Carlsonb0c59432011-05-19 12:12:48 +00002162 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2163 }
2164
2165 val = tr32(TG3_CPMU_EEE_MODE);
2166 tw32(TG3_CPMU_EEE_MODE, val | TG3_CPMU_EEEMD_LPI_ENABLE);
2167}
2168
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169static int tg3_wait_macro_done(struct tg3 *tp)
2170{
2171 int limit = 100;
2172
2173 while (limit--) {
2174 u32 tmp32;
2175
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002176 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 if ((tmp32 & 0x1000) == 0)
2178 break;
2179 }
2180 }
Roel Kluind4675b52009-02-12 16:33:27 -08002181 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 return -EBUSY;
2183
2184 return 0;
2185}
2186
2187static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
2188{
2189 static const u32 test_pat[4][6] = {
2190 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
2191 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
2192 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
2193 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
2194 };
2195 int chan;
2196
2197 for (chan = 0; chan < 4; chan++) {
2198 int i;
2199
2200 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2201 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002202 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
2204 for (i = 0; i < 6; i++)
2205 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
2206 test_pat[chan][i]);
2207
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002208 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 if (tg3_wait_macro_done(tp)) {
2210 *resetp = 1;
2211 return -EBUSY;
2212 }
2213
2214 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2215 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002216 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 if (tg3_wait_macro_done(tp)) {
2218 *resetp = 1;
2219 return -EBUSY;
2220 }
2221
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002222 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 if (tg3_wait_macro_done(tp)) {
2224 *resetp = 1;
2225 return -EBUSY;
2226 }
2227
2228 for (i = 0; i < 6; i += 2) {
2229 u32 low, high;
2230
2231 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
2232 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
2233 tg3_wait_macro_done(tp)) {
2234 *resetp = 1;
2235 return -EBUSY;
2236 }
2237 low &= 0x7fff;
2238 high &= 0x000f;
2239 if (low != test_pat[chan][i] ||
2240 high != test_pat[chan][i+1]) {
2241 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
2242 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
2243 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
2244
2245 return -EBUSY;
2246 }
2247 }
2248 }
2249
2250 return 0;
2251}
2252
2253static int tg3_phy_reset_chanpat(struct tg3 *tp)
2254{
2255 int chan;
2256
2257 for (chan = 0; chan < 4; chan++) {
2258 int i;
2259
2260 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2261 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002262 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 for (i = 0; i < 6; i++)
2264 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002265 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 if (tg3_wait_macro_done(tp))
2267 return -EBUSY;
2268 }
2269
2270 return 0;
2271}
2272
2273static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
2274{
2275 u32 reg32, phy9_orig;
2276 int retries, do_phy_reset, err;
2277
2278 retries = 10;
2279 do_phy_reset = 1;
2280 do {
2281 if (do_phy_reset) {
2282 err = tg3_bmcr_reset(tp);
2283 if (err)
2284 return err;
2285 do_phy_reset = 0;
2286 }
2287
2288 /* Disable transmitter and interrupt. */
2289 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
2290 continue;
2291
2292 reg32 |= 0x3000;
2293 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2294
2295 /* Set full-duplex, 1000 mbps. */
2296 tg3_writephy(tp, MII_BMCR,
Matt Carlson221c5632011-06-13 13:39:01 +00002297 BMCR_FULLDPLX | BMCR_SPEED1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298
2299 /* Set to master mode. */
Matt Carlson221c5632011-06-13 13:39:01 +00002300 if (tg3_readphy(tp, MII_CTRL1000, &phy9_orig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 continue;
2302
Matt Carlson221c5632011-06-13 13:39:01 +00002303 tg3_writephy(tp, MII_CTRL1000,
2304 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305
Matt Carlson1d36ba42011-04-20 07:57:42 +00002306 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
2307 if (err)
2308 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
2310 /* Block the PHY control access. */
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002311 tg3_phydsp_write(tp, 0x8005, 0x0800);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
2313 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
2314 if (!err)
2315 break;
2316 } while (--retries);
2317
2318 err = tg3_phy_reset_chanpat(tp);
2319 if (err)
2320 return err;
2321
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002322 tg3_phydsp_write(tp, 0x8005, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
2324 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002325 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
Matt Carlson1d36ba42011-04-20 07:57:42 +00002327 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328
Matt Carlson221c5632011-06-13 13:39:01 +00002329 tg3_writephy(tp, MII_CTRL1000, phy9_orig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330
2331 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
2332 reg32 &= ~0x3000;
2333 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2334 } else if (!err)
2335 err = -EBUSY;
2336
2337 return err;
2338}
2339
2340/* This will reset the tigon3 PHY if there is no valid
2341 * link unless the FORCE argument is non-zero.
2342 */
2343static int tg3_phy_reset(struct tg3 *tp)
2344{
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002345 u32 val, cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 int err;
2347
Michael Chan60189dd2006-12-17 17:08:07 -08002348 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002349 val = tr32(GRC_MISC_CFG);
2350 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
2351 udelay(40);
2352 }
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002353 err = tg3_readphy(tp, MII_BMSR, &val);
2354 err |= tg3_readphy(tp, MII_BMSR, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 if (err != 0)
2356 return -EBUSY;
2357
Michael Chanc8e1e822006-04-29 18:55:17 -07002358 if (netif_running(tp->dev) && netif_carrier_ok(tp->dev)) {
2359 netif_carrier_off(tp->dev);
2360 tg3_link_report(tp);
2361 }
2362
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2364 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2365 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
2366 err = tg3_phy_reset_5703_4_5(tp);
2367 if (err)
2368 return err;
2369 goto out;
2370 }
2371
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002372 cpmuctrl = 0;
2373 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
2374 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
2375 cpmuctrl = tr32(TG3_CPMU_CTRL);
2376 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
2377 tw32(TG3_CPMU_CTRL,
2378 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
2379 }
2380
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 err = tg3_bmcr_reset(tp);
2382 if (err)
2383 return err;
2384
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002385 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002386 val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
2387 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002388
2389 tw32(TG3_CPMU_CTRL, cpmuctrl);
2390 }
2391
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002392 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2393 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002394 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2395 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
2396 CPMU_LSPD_1000MB_MACCLK_12_5) {
2397 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2398 udelay(40);
2399 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2400 }
2401 }
2402
Joe Perches63c3a662011-04-26 08:12:10 +00002403 if (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002404 (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
Matt Carlsonecf14102010-01-20 16:58:05 +00002405 return 0;
2406
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002407 tg3_phy_apply_otp(tp);
2408
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002409 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -08002410 tg3_phy_toggle_apd(tp, true);
2411 else
2412 tg3_phy_toggle_apd(tp, false);
2413
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414out:
Matt Carlson1d36ba42011-04-20 07:57:42 +00002415 if ((tp->phy_flags & TG3_PHYFLG_ADC_BUG) &&
2416 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002417 tg3_phydsp_write(tp, 0x201f, 0x2aaa);
2418 tg3_phydsp_write(tp, 0x000a, 0x0323);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002419 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002421
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002422 if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002423 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2424 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002426
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002427 if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002428 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2429 tg3_phydsp_write(tp, 0x000a, 0x310b);
2430 tg3_phydsp_write(tp, 0x201f, 0x9506);
2431 tg3_phydsp_write(tp, 0x401f, 0x14e2);
2432 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2433 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002434 } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002435 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2436 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
2437 if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
2438 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2439 tg3_writephy(tp, MII_TG3_TEST1,
2440 MII_TG3_TEST1_TRIM_EN | 0x4);
2441 } else
2442 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
2443
2444 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2445 }
Michael Chanc424cb22006-04-29 18:56:34 -07002446 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002447
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 /* Set Extended packet length bit (bit 14) on all chips that */
2449 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002450 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 /* Cannot do read-modify-write on 5401 */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002452 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Joe Perches63c3a662011-04-26 08:12:10 +00002453 } else if (tg3_flag(tp, JUMBO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 /* Set bit 14 with read-modify-write to preserve other bits */
Matt Carlson15ee95c2011-04-20 07:57:40 +00002455 err = tg3_phy_auxctl_read(tp,
2456 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2457 if (!err)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002458 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2459 val | MII_TG3_AUXCTL_ACTL_EXTPKTLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 }
2461
2462 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2463 * jumbo frames transmission.
2464 */
Joe Perches63c3a662011-04-26 08:12:10 +00002465 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002466 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002467 tg3_writephy(tp, MII_TG3_EXT_CTRL,
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002468 val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 }
2470
Michael Chan715116a2006-09-27 16:09:25 -07002471 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002472 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002473 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002474 }
2475
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002476 tg3_phy_toggle_automdix(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 tg3_phy_set_wirespeed(tp);
2478 return 0;
2479}
2480
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002481#define TG3_GPIO_MSG_DRVR_PRES 0x00000001
2482#define TG3_GPIO_MSG_NEED_VAUX 0x00000002
2483#define TG3_GPIO_MSG_MASK (TG3_GPIO_MSG_DRVR_PRES | \
2484 TG3_GPIO_MSG_NEED_VAUX)
2485#define TG3_GPIO_MSG_ALL_DRVR_PRES_MASK \
2486 ((TG3_GPIO_MSG_DRVR_PRES << 0) | \
2487 (TG3_GPIO_MSG_DRVR_PRES << 4) | \
2488 (TG3_GPIO_MSG_DRVR_PRES << 8) | \
2489 (TG3_GPIO_MSG_DRVR_PRES << 12))
2490
2491#define TG3_GPIO_MSG_ALL_NEED_VAUX_MASK \
2492 ((TG3_GPIO_MSG_NEED_VAUX << 0) | \
2493 (TG3_GPIO_MSG_NEED_VAUX << 4) | \
2494 (TG3_GPIO_MSG_NEED_VAUX << 8) | \
2495 (TG3_GPIO_MSG_NEED_VAUX << 12))
2496
2497static inline u32 tg3_set_function_status(struct tg3 *tp, u32 newstat)
2498{
2499 u32 status, shift;
2500
2501 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2502 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
2503 status = tg3_ape_read32(tp, TG3_APE_GPIO_MSG);
2504 else
2505 status = tr32(TG3_CPMU_DRV_STATUS);
2506
2507 shift = TG3_APE_GPIO_MSG_SHIFT + 4 * tp->pci_fn;
2508 status &= ~(TG3_GPIO_MSG_MASK << shift);
2509 status |= (newstat << shift);
2510
2511 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2512 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
2513 tg3_ape_write32(tp, TG3_APE_GPIO_MSG, status);
2514 else
2515 tw32(TG3_CPMU_DRV_STATUS, status);
2516
2517 return status >> TG3_APE_GPIO_MSG_SHIFT;
2518}
2519
Matt Carlson520b2752011-06-13 13:39:02 +00002520static inline int tg3_pwrsrc_switch_to_vmain(struct tg3 *tp)
2521{
2522 if (!tg3_flag(tp, IS_NIC))
2523 return 0;
2524
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002525 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2526 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
2527 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
2528 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2529 return -EIO;
Matt Carlson520b2752011-06-13 13:39:02 +00002530
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002531 tg3_set_function_status(tp, TG3_GPIO_MSG_DRVR_PRES);
2532
2533 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2534 TG3_GRC_LCLCTL_PWRSW_DELAY);
2535
2536 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
2537 } else {
2538 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2539 TG3_GRC_LCLCTL_PWRSW_DELAY);
2540 }
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002541
Matt Carlson520b2752011-06-13 13:39:02 +00002542 return 0;
2543}
2544
2545static void tg3_pwrsrc_die_with_vmain(struct tg3 *tp)
2546{
2547 u32 grc_local_ctrl;
2548
2549 if (!tg3_flag(tp, IS_NIC) ||
2550 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2551 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)
2552 return;
2553
2554 grc_local_ctrl = tp->grc_local_ctrl | GRC_LCLCTRL_GPIO_OE1;
2555
2556 tw32_wait_f(GRC_LOCAL_CTRL,
2557 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2558 TG3_GRC_LCLCTL_PWRSW_DELAY);
2559
2560 tw32_wait_f(GRC_LOCAL_CTRL,
2561 grc_local_ctrl,
2562 TG3_GRC_LCLCTL_PWRSW_DELAY);
2563
2564 tw32_wait_f(GRC_LOCAL_CTRL,
2565 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2566 TG3_GRC_LCLCTL_PWRSW_DELAY);
2567}
2568
2569static void tg3_pwrsrc_switch_to_vaux(struct tg3 *tp)
2570{
2571 if (!tg3_flag(tp, IS_NIC))
2572 return;
2573
2574 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2575 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2576 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2577 (GRC_LCLCTRL_GPIO_OE0 |
2578 GRC_LCLCTRL_GPIO_OE1 |
2579 GRC_LCLCTRL_GPIO_OE2 |
2580 GRC_LCLCTRL_GPIO_OUTPUT0 |
2581 GRC_LCLCTRL_GPIO_OUTPUT1),
2582 TG3_GRC_LCLCTL_PWRSW_DELAY);
2583 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2584 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
2585 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2586 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2587 GRC_LCLCTRL_GPIO_OE1 |
2588 GRC_LCLCTRL_GPIO_OE2 |
2589 GRC_LCLCTRL_GPIO_OUTPUT0 |
2590 GRC_LCLCTRL_GPIO_OUTPUT1 |
2591 tp->grc_local_ctrl;
2592 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2593 TG3_GRC_LCLCTL_PWRSW_DELAY);
2594
2595 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2596 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2597 TG3_GRC_LCLCTL_PWRSW_DELAY);
2598
2599 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2600 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2601 TG3_GRC_LCLCTL_PWRSW_DELAY);
2602 } else {
2603 u32 no_gpio2;
2604 u32 grc_local_ctrl = 0;
2605
2606 /* Workaround to prevent overdrawing Amps. */
2607 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
2608 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
2609 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2610 grc_local_ctrl,
2611 TG3_GRC_LCLCTL_PWRSW_DELAY);
2612 }
2613
2614 /* On 5753 and variants, GPIO2 cannot be used. */
2615 no_gpio2 = tp->nic_sram_data_cfg &
2616 NIC_SRAM_DATA_CFG_NO_GPIO2;
2617
2618 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
2619 GRC_LCLCTRL_GPIO_OE1 |
2620 GRC_LCLCTRL_GPIO_OE2 |
2621 GRC_LCLCTRL_GPIO_OUTPUT1 |
2622 GRC_LCLCTRL_GPIO_OUTPUT2;
2623 if (no_gpio2) {
2624 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2625 GRC_LCLCTRL_GPIO_OUTPUT2);
2626 }
2627 tw32_wait_f(GRC_LOCAL_CTRL,
2628 tp->grc_local_ctrl | grc_local_ctrl,
2629 TG3_GRC_LCLCTL_PWRSW_DELAY);
2630
2631 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2632
2633 tw32_wait_f(GRC_LOCAL_CTRL,
2634 tp->grc_local_ctrl | grc_local_ctrl,
2635 TG3_GRC_LCLCTL_PWRSW_DELAY);
2636
2637 if (!no_gpio2) {
2638 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
2639 tw32_wait_f(GRC_LOCAL_CTRL,
2640 tp->grc_local_ctrl | grc_local_ctrl,
2641 TG3_GRC_LCLCTL_PWRSW_DELAY);
2642 }
2643 }
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002644}
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002645
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002646static void tg3_frob_aux_power_5717(struct tg3 *tp, bool wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002647{
2648 u32 msg = 0;
2649
2650 /* Serialize power state transitions */
2651 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2652 return;
2653
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002654 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE) || wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002655 msg = TG3_GPIO_MSG_NEED_VAUX;
2656
2657 msg = tg3_set_function_status(tp, msg);
2658
2659 if (msg & TG3_GPIO_MSG_ALL_DRVR_PRES_MASK)
2660 goto done;
2661
2662 if (msg & TG3_GPIO_MSG_ALL_NEED_VAUX_MASK)
2663 tg3_pwrsrc_switch_to_vaux(tp);
2664 else
2665 tg3_pwrsrc_die_with_vmain(tp);
2666
2667done:
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002668 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
Matt Carlson520b2752011-06-13 13:39:02 +00002669}
2670
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002671static void tg3_frob_aux_power(struct tg3 *tp, bool include_wol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672{
Matt Carlson683644b2011-03-09 16:58:23 +00002673 bool need_vaux = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674
Matt Carlson334355a2010-01-20 16:58:10 +00002675 /* The GPIOs do something completely different on 57765. */
Joe Perches63c3a662011-04-26 08:12:10 +00002676 if (!tg3_flag(tp, IS_NIC) ||
Matt Carlson334355a2010-01-20 16:58:10 +00002677 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 return;
2679
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002680 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2681 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
2682 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002683 tg3_frob_aux_power_5717(tp, include_wol ?
2684 tg3_flag(tp, WOL_ENABLE) != 0 : 0);
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002685 return;
2686 }
2687
2688 if (tp->pdev_peer && tp->pdev_peer != tp->pdev) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002689 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002691 dev_peer = pci_get_drvdata(tp->pdev_peer);
Matt Carlson683644b2011-03-09 16:58:23 +00002692
Michael Chanbc1c7562006-03-20 17:48:03 -08002693 /* remove_one() may have been run on the peer. */
Matt Carlson683644b2011-03-09 16:58:23 +00002694 if (dev_peer) {
2695 struct tg3 *tp_peer = netdev_priv(dev_peer);
2696
Joe Perches63c3a662011-04-26 08:12:10 +00002697 if (tg3_flag(tp_peer, INIT_COMPLETE))
Matt Carlson683644b2011-03-09 16:58:23 +00002698 return;
2699
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002700 if ((include_wol && tg3_flag(tp_peer, WOL_ENABLE)) ||
Joe Perches63c3a662011-04-26 08:12:10 +00002701 tg3_flag(tp_peer, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002702 need_vaux = true;
2703 }
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002706 if ((include_wol && tg3_flag(tp, WOL_ENABLE)) ||
2707 tg3_flag(tp, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002708 need_vaux = true;
2709
Matt Carlson520b2752011-06-13 13:39:02 +00002710 if (need_vaux)
2711 tg3_pwrsrc_switch_to_vaux(tp);
2712 else
2713 tg3_pwrsrc_die_with_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714}
2715
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002716static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2717{
2718 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2719 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00002720 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002721 if (speed != SPEED_10)
2722 return 1;
2723 } else if (speed == SPEED_10)
2724 return 1;
2725
2726 return 0;
2727}
2728
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729static int tg3_setup_phy(struct tg3 *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730static int tg3_halt_cpu(struct tg3 *, u32);
2731
Matt Carlson0a459aa2008-11-03 16:54:15 -08002732static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002733{
Matt Carlsonce057f02007-11-12 21:08:03 -08002734 u32 val;
2735
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002736 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Michael Chan51297242007-02-13 12:17:57 -08002737 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2738 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2739 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2740
2741 sg_dig_ctrl |=
2742 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2743 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2744 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2745 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002746 return;
Michael Chan51297242007-02-13 12:17:57 -08002747 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002748
Michael Chan60189dd2006-12-17 17:08:07 -08002749 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002750 tg3_bmcr_reset(tp);
2751 val = tr32(GRC_MISC_CFG);
2752 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2753 udelay(40);
2754 return;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002755 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson0e5f7842009-11-02 14:26:38 +00002756 u32 phytest;
2757 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2758 u32 phy;
2759
2760 tg3_writephy(tp, MII_ADVERTISE, 0);
2761 tg3_writephy(tp, MII_BMCR,
2762 BMCR_ANENABLE | BMCR_ANRESTART);
2763
2764 tg3_writephy(tp, MII_TG3_FET_TEST,
2765 phytest | MII_TG3_FET_SHADOW_EN);
2766 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
2767 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
2768 tg3_writephy(tp,
2769 MII_TG3_FET_SHDW_AUXMODE4,
2770 phy);
2771 }
2772 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2773 }
2774 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002775 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07002776 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2777 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002778
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002779 val = MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2780 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2781 MII_TG3_AUXCTL_PCTL_VREG_11V;
2782 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, val);
Michael Chan715116a2006-09-27 16:09:25 -07002783 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002784
Michael Chan15c3b692006-03-22 01:06:52 -08002785 /* The PHY should not be powered down on some chips because
2786 * of bugs.
2787 */
2788 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2789 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2790 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002791 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Michael Chan15c3b692006-03-22 01:06:52 -08002792 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08002793
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002794 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2795 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002796 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2797 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2798 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2799 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2800 }
2801
Michael Chan15c3b692006-03-22 01:06:52 -08002802 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2803}
2804
Matt Carlson3f007892008-11-03 16:51:36 -08002805/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002806static int tg3_nvram_lock(struct tg3 *tp)
2807{
Joe Perches63c3a662011-04-26 08:12:10 +00002808 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002809 int i;
2810
2811 if (tp->nvram_lock_cnt == 0) {
2812 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2813 for (i = 0; i < 8000; i++) {
2814 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2815 break;
2816 udelay(20);
2817 }
2818 if (i == 8000) {
2819 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2820 return -ENODEV;
2821 }
2822 }
2823 tp->nvram_lock_cnt++;
2824 }
2825 return 0;
2826}
2827
2828/* tp->lock is held. */
2829static void tg3_nvram_unlock(struct tg3 *tp)
2830{
Joe Perches63c3a662011-04-26 08:12:10 +00002831 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002832 if (tp->nvram_lock_cnt > 0)
2833 tp->nvram_lock_cnt--;
2834 if (tp->nvram_lock_cnt == 0)
2835 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
2836 }
2837}
2838
2839/* tp->lock is held. */
2840static void tg3_enable_nvram_access(struct tg3 *tp)
2841{
Joe Perches63c3a662011-04-26 08:12:10 +00002842 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002843 u32 nvaccess = tr32(NVRAM_ACCESS);
2844
2845 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
2846 }
2847}
2848
2849/* tp->lock is held. */
2850static void tg3_disable_nvram_access(struct tg3 *tp)
2851{
Joe Perches63c3a662011-04-26 08:12:10 +00002852 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002853 u32 nvaccess = tr32(NVRAM_ACCESS);
2854
2855 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
2856 }
2857}
2858
2859static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
2860 u32 offset, u32 *val)
2861{
2862 u32 tmp;
2863 int i;
2864
2865 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
2866 return -EINVAL;
2867
2868 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
2869 EEPROM_ADDR_DEVID_MASK |
2870 EEPROM_ADDR_READ);
2871 tw32(GRC_EEPROM_ADDR,
2872 tmp |
2873 (0 << EEPROM_ADDR_DEVID_SHIFT) |
2874 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
2875 EEPROM_ADDR_ADDR_MASK) |
2876 EEPROM_ADDR_READ | EEPROM_ADDR_START);
2877
2878 for (i = 0; i < 1000; i++) {
2879 tmp = tr32(GRC_EEPROM_ADDR);
2880
2881 if (tmp & EEPROM_ADDR_COMPLETE)
2882 break;
2883 msleep(1);
2884 }
2885 if (!(tmp & EEPROM_ADDR_COMPLETE))
2886 return -EBUSY;
2887
Matt Carlson62cedd12009-04-20 14:52:29 -07002888 tmp = tr32(GRC_EEPROM_DATA);
2889
2890 /*
2891 * The data will always be opposite the native endian
2892 * format. Perform a blind byteswap to compensate.
2893 */
2894 *val = swab32(tmp);
2895
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002896 return 0;
2897}
2898
2899#define NVRAM_CMD_TIMEOUT 10000
2900
2901static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
2902{
2903 int i;
2904
2905 tw32(NVRAM_CMD, nvram_cmd);
2906 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
2907 udelay(10);
2908 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
2909 udelay(10);
2910 break;
2911 }
2912 }
2913
2914 if (i == NVRAM_CMD_TIMEOUT)
2915 return -EBUSY;
2916
2917 return 0;
2918}
2919
2920static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
2921{
Joe Perches63c3a662011-04-26 08:12:10 +00002922 if (tg3_flag(tp, NVRAM) &&
2923 tg3_flag(tp, NVRAM_BUFFERED) &&
2924 tg3_flag(tp, FLASH) &&
2925 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002926 (tp->nvram_jedecnum == JEDEC_ATMEL))
2927
2928 addr = ((addr / tp->nvram_pagesize) <<
2929 ATMEL_AT45DB0X1B_PAGE_POS) +
2930 (addr % tp->nvram_pagesize);
2931
2932 return addr;
2933}
2934
2935static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
2936{
Joe Perches63c3a662011-04-26 08:12:10 +00002937 if (tg3_flag(tp, NVRAM) &&
2938 tg3_flag(tp, NVRAM_BUFFERED) &&
2939 tg3_flag(tp, FLASH) &&
2940 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002941 (tp->nvram_jedecnum == JEDEC_ATMEL))
2942
2943 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
2944 tp->nvram_pagesize) +
2945 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
2946
2947 return addr;
2948}
2949
Matt Carlsone4f34112009-02-25 14:25:00 +00002950/* NOTE: Data read in from NVRAM is byteswapped according to
2951 * the byteswapping settings for all other register accesses.
2952 * tg3 devices are BE devices, so on a BE machine, the data
2953 * returned will be exactly as it is seen in NVRAM. On a LE
2954 * machine, the 32-bit value will be byteswapped.
2955 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002956static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
2957{
2958 int ret;
2959
Joe Perches63c3a662011-04-26 08:12:10 +00002960 if (!tg3_flag(tp, NVRAM))
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002961 return tg3_nvram_read_using_eeprom(tp, offset, val);
2962
2963 offset = tg3_nvram_phys_addr(tp, offset);
2964
2965 if (offset > NVRAM_ADDR_MSK)
2966 return -EINVAL;
2967
2968 ret = tg3_nvram_lock(tp);
2969 if (ret)
2970 return ret;
2971
2972 tg3_enable_nvram_access(tp);
2973
2974 tw32(NVRAM_ADDR, offset);
2975 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
2976 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
2977
2978 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00002979 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002980
2981 tg3_disable_nvram_access(tp);
2982
2983 tg3_nvram_unlock(tp);
2984
2985 return ret;
2986}
2987
Matt Carlsona9dc5292009-02-25 14:25:30 +00002988/* Ensures NVRAM data is in bytestream format. */
2989static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002990{
2991 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00002992 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002993 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00002994 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002995 return res;
2996}
2997
Matt Carlson997b4f12011-08-31 11:44:53 +00002998#define RX_CPU_SCRATCH_BASE 0x30000
2999#define RX_CPU_SCRATCH_SIZE 0x04000
3000#define TX_CPU_SCRATCH_BASE 0x34000
3001#define TX_CPU_SCRATCH_SIZE 0x04000
3002
3003/* tp->lock is held. */
3004static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
3005{
3006 int i;
3007
3008 BUG_ON(offset == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS));
3009
3010 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
3011 u32 val = tr32(GRC_VCPU_EXT_CTRL);
3012
3013 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
3014 return 0;
3015 }
3016 if (offset == RX_CPU_BASE) {
3017 for (i = 0; i < 10000; i++) {
3018 tw32(offset + CPU_STATE, 0xffffffff);
3019 tw32(offset + CPU_MODE, CPU_MODE_HALT);
3020 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
3021 break;
3022 }
3023
3024 tw32(offset + CPU_STATE, 0xffffffff);
3025 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
3026 udelay(10);
3027 } else {
3028 for (i = 0; i < 10000; i++) {
3029 tw32(offset + CPU_STATE, 0xffffffff);
3030 tw32(offset + CPU_MODE, CPU_MODE_HALT);
3031 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
3032 break;
3033 }
3034 }
3035
3036 if (i >= 10000) {
3037 netdev_err(tp->dev, "%s timed out, %s CPU\n",
3038 __func__, offset == RX_CPU_BASE ? "RX" : "TX");
3039 return -ENODEV;
3040 }
3041
3042 /* Clear firmware's nvram arbitration. */
3043 if (tg3_flag(tp, NVRAM))
3044 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
3045 return 0;
3046}
3047
3048struct fw_info {
3049 unsigned int fw_base;
3050 unsigned int fw_len;
3051 const __be32 *fw_data;
3052};
3053
3054/* tp->lock is held. */
3055static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base,
3056 u32 cpu_scratch_base, int cpu_scratch_size,
3057 struct fw_info *info)
3058{
3059 int err, lock_err, i;
3060 void (*write_op)(struct tg3 *, u32, u32);
3061
3062 if (cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS)) {
3063 netdev_err(tp->dev,
3064 "%s: Trying to load TX cpu firmware which is 5705\n",
3065 __func__);
3066 return -EINVAL;
3067 }
3068
3069 if (tg3_flag(tp, 5705_PLUS))
3070 write_op = tg3_write_mem;
3071 else
3072 write_op = tg3_write_indirect_reg32;
3073
3074 /* It is possible that bootcode is still loading at this point.
3075 * Get the nvram lock first before halting the cpu.
3076 */
3077 lock_err = tg3_nvram_lock(tp);
3078 err = tg3_halt_cpu(tp, cpu_base);
3079 if (!lock_err)
3080 tg3_nvram_unlock(tp);
3081 if (err)
3082 goto out;
3083
3084 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
3085 write_op(tp, cpu_scratch_base + i, 0);
3086 tw32(cpu_base + CPU_STATE, 0xffffffff);
3087 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
3088 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
3089 write_op(tp, (cpu_scratch_base +
3090 (info->fw_base & 0xffff) +
3091 (i * sizeof(u32))),
3092 be32_to_cpu(info->fw_data[i]));
3093
3094 err = 0;
3095
3096out:
3097 return err;
3098}
3099
3100/* tp->lock is held. */
3101static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
3102{
3103 struct fw_info info;
3104 const __be32 *fw_data;
3105 int err, i;
3106
3107 fw_data = (void *)tp->fw->data;
3108
3109 /* Firmware blob starts with version numbers, followed by
3110 start address and length. We are setting complete length.
3111 length = end_address_of_bss - start_address_of_text.
3112 Remainder is the blob to be loaded contiguously
3113 from start address. */
3114
3115 info.fw_base = be32_to_cpu(fw_data[1]);
3116 info.fw_len = tp->fw->size - 12;
3117 info.fw_data = &fw_data[3];
3118
3119 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
3120 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
3121 &info);
3122 if (err)
3123 return err;
3124
3125 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
3126 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
3127 &info);
3128 if (err)
3129 return err;
3130
3131 /* Now startup only the RX cpu. */
3132 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3133 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
3134
3135 for (i = 0; i < 5; i++) {
3136 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
3137 break;
3138 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3139 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
3140 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
3141 udelay(1000);
3142 }
3143 if (i >= 5) {
3144 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
3145 "should be %08x\n", __func__,
3146 tr32(RX_CPU_BASE + CPU_PC), info.fw_base);
3147 return -ENODEV;
3148 }
3149 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3150 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
3151
3152 return 0;
3153}
3154
3155/* tp->lock is held. */
3156static int tg3_load_tso_firmware(struct tg3 *tp)
3157{
3158 struct fw_info info;
3159 const __be32 *fw_data;
3160 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
3161 int err, i;
3162
3163 if (tg3_flag(tp, HW_TSO_1) ||
3164 tg3_flag(tp, HW_TSO_2) ||
3165 tg3_flag(tp, HW_TSO_3))
3166 return 0;
3167
3168 fw_data = (void *)tp->fw->data;
3169
3170 /* Firmware blob starts with version numbers, followed by
3171 start address and length. We are setting complete length.
3172 length = end_address_of_bss - start_address_of_text.
3173 Remainder is the blob to be loaded contiguously
3174 from start address. */
3175
3176 info.fw_base = be32_to_cpu(fw_data[1]);
3177 cpu_scratch_size = tp->fw_len;
3178 info.fw_len = tp->fw->size - 12;
3179 info.fw_data = &fw_data[3];
3180
3181 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
3182 cpu_base = RX_CPU_BASE;
3183 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
3184 } else {
3185 cpu_base = TX_CPU_BASE;
3186 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
3187 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
3188 }
3189
3190 err = tg3_load_firmware_cpu(tp, cpu_base,
3191 cpu_scratch_base, cpu_scratch_size,
3192 &info);
3193 if (err)
3194 return err;
3195
3196 /* Now startup the cpu. */
3197 tw32(cpu_base + CPU_STATE, 0xffffffff);
3198 tw32_f(cpu_base + CPU_PC, info.fw_base);
3199
3200 for (i = 0; i < 5; i++) {
3201 if (tr32(cpu_base + CPU_PC) == info.fw_base)
3202 break;
3203 tw32(cpu_base + CPU_STATE, 0xffffffff);
3204 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
3205 tw32_f(cpu_base + CPU_PC, info.fw_base);
3206 udelay(1000);
3207 }
3208 if (i >= 5) {
3209 netdev_err(tp->dev,
3210 "%s fails to set CPU PC, is %08x should be %08x\n",
3211 __func__, tr32(cpu_base + CPU_PC), info.fw_base);
3212 return -ENODEV;
3213 }
3214 tw32(cpu_base + CPU_STATE, 0xffffffff);
3215 tw32_f(cpu_base + CPU_MODE, 0x00000000);
3216 return 0;
3217}
3218
3219
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003220/* tp->lock is held. */
Matt Carlson3f007892008-11-03 16:51:36 -08003221static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
3222{
3223 u32 addr_high, addr_low;
3224 int i;
3225
3226 addr_high = ((tp->dev->dev_addr[0] << 8) |
3227 tp->dev->dev_addr[1]);
3228 addr_low = ((tp->dev->dev_addr[2] << 24) |
3229 (tp->dev->dev_addr[3] << 16) |
3230 (tp->dev->dev_addr[4] << 8) |
3231 (tp->dev->dev_addr[5] << 0));
3232 for (i = 0; i < 4; i++) {
3233 if (i == 1 && skip_mac_1)
3234 continue;
3235 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
3236 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
3237 }
3238
3239 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3240 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
3241 for (i = 0; i < 12; i++) {
3242 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
3243 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
3244 }
3245 }
3246
3247 addr_high = (tp->dev->dev_addr[0] +
3248 tp->dev->dev_addr[1] +
3249 tp->dev->dev_addr[2] +
3250 tp->dev->dev_addr[3] +
3251 tp->dev->dev_addr[4] +
3252 tp->dev->dev_addr[5]) &
3253 TX_BACKOFF_SEED_MASK;
3254 tw32(MAC_TX_BACKOFF_SEED, addr_high);
3255}
3256
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003257static void tg3_enable_register_access(struct tg3 *tp)
3258{
3259 /*
3260 * Make sure register accesses (indirect or otherwise) will function
3261 * correctly.
3262 */
3263 pci_write_config_dword(tp->pdev,
3264 TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
3265}
3266
3267static int tg3_power_up(struct tg3 *tp)
3268{
Matt Carlsonbed98292011-07-13 09:27:29 +00003269 int err;
3270
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003271 tg3_enable_register_access(tp);
3272
Matt Carlsonbed98292011-07-13 09:27:29 +00003273 err = pci_set_power_state(tp->pdev, PCI_D0);
3274 if (!err) {
3275 /* Switch out of Vaux if it is a NIC */
3276 tg3_pwrsrc_switch_to_vmain(tp);
3277 } else {
3278 netdev_err(tp->dev, "Transition to D0 failed\n");
3279 }
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003280
Matt Carlsonbed98292011-07-13 09:27:29 +00003281 return err;
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003282}
3283
3284static int tg3_power_down_prepare(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285{
3286 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003287 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003289 tg3_enable_register_access(tp);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003290
3291 /* Restore the CLKREQ setting. */
Joe Perches63c3a662011-04-26 08:12:10 +00003292 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003293 u16 lnkctl;
3294
3295 pci_read_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00003296 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003297 &lnkctl);
3298 lnkctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
3299 pci_write_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00003300 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003301 lnkctl);
3302 }
3303
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
3305 tw32(TG3PCI_MISC_HOST_CTRL,
3306 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
3307
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003308 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00003309 tg3_flag(tp, WOL_ENABLE);
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003310
Joe Perches63c3a662011-04-26 08:12:10 +00003311 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08003312 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003313 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson80096062010-08-02 11:26:06 +00003314 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003315 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003316 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003317
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00003318 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003319
Matt Carlson80096062010-08-02 11:26:06 +00003320 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003321
3322 tp->link_config.orig_speed = phydev->speed;
3323 tp->link_config.orig_duplex = phydev->duplex;
3324 tp->link_config.orig_autoneg = phydev->autoneg;
3325 tp->link_config.orig_advertising = phydev->advertising;
3326
3327 advertising = ADVERTISED_TP |
3328 ADVERTISED_Pause |
3329 ADVERTISED_Autoneg |
3330 ADVERTISED_10baseT_Half;
3331
Joe Perches63c3a662011-04-26 08:12:10 +00003332 if (tg3_flag(tp, ENABLE_ASF) || device_should_wake) {
3333 if (tg3_flag(tp, WOL_SPEED_100MB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003334 advertising |=
3335 ADVERTISED_100baseT_Half |
3336 ADVERTISED_100baseT_Full |
3337 ADVERTISED_10baseT_Full;
3338 else
3339 advertising |= ADVERTISED_10baseT_Full;
3340 }
3341
3342 phydev->advertising = advertising;
3343
3344 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08003345
3346 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00003347 if (phyid != PHY_ID_BCMAC131) {
3348 phyid &= PHY_BCM_OUI_MASK;
3349 if (phyid == PHY_BCM_OUI_1 ||
3350 phyid == PHY_BCM_OUI_2 ||
3351 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08003352 do_low_power = true;
3353 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003354 }
Matt Carlsondd477002008-05-25 23:45:58 -07003355 } else {
Matt Carlson20232762008-12-21 20:18:56 -08003356 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003357
Matt Carlson80096062010-08-02 11:26:06 +00003358 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
3359 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07003360 tp->link_config.orig_speed = tp->link_config.speed;
3361 tp->link_config.orig_duplex = tp->link_config.duplex;
3362 tp->link_config.orig_autoneg = tp->link_config.autoneg;
3363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003365 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Matt Carlsondd477002008-05-25 23:45:58 -07003366 tp->link_config.speed = SPEED_10;
3367 tp->link_config.duplex = DUPLEX_HALF;
3368 tp->link_config.autoneg = AUTONEG_ENABLE;
3369 tg3_setup_phy(tp, 0);
3370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 }
3372
Michael Chanb5d37722006-09-27 16:06:21 -07003373 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
3374 u32 val;
3375
3376 val = tr32(GRC_VCPU_EXT_CTRL);
3377 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
Joe Perches63c3a662011-04-26 08:12:10 +00003378 } else if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08003379 int i;
3380 u32 val;
3381
3382 for (i = 0; i < 200; i++) {
3383 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
3384 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
3385 break;
3386 msleep(1);
3387 }
3388 }
Joe Perches63c3a662011-04-26 08:12:10 +00003389 if (tg3_flag(tp, WOL_CAP))
Gary Zambranoa85feb82007-05-05 11:52:19 -07003390 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
3391 WOL_DRV_STATE_SHUTDOWN |
3392 WOL_DRV_WOL |
3393 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08003394
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003395 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 u32 mac_mode;
3397
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003398 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003399 if (do_low_power &&
3400 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
3401 tg3_phy_auxctl_write(tp,
3402 MII_TG3_AUXCTL_SHDWSEL_PWRCTL,
3403 MII_TG3_AUXCTL_PCTL_WOL_EN |
3404 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
3405 MII_TG3_AUXCTL_PCTL_CL_AB_TXDAC);
Matt Carlsondd477002008-05-25 23:45:58 -07003406 udelay(40);
3407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003409 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07003410 mac_mode = MAC_MODE_PORT_MODE_GMII;
3411 else
3412 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003414 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
3415 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
3416 ASIC_REV_5700) {
Joe Perches63c3a662011-04-26 08:12:10 +00003417 u32 speed = tg3_flag(tp, WOL_SPEED_100MB) ?
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003418 SPEED_100 : SPEED_10;
3419 if (tg3_5700_link_polarity(tp, speed))
3420 mac_mode |= MAC_MODE_LINK_POLARITY;
3421 else
3422 mac_mode &= ~MAC_MODE_LINK_POLARITY;
3423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 } else {
3425 mac_mode = MAC_MODE_PORT_MODE_TBI;
3426 }
3427
Joe Perches63c3a662011-04-26 08:12:10 +00003428 if (!tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 tw32(MAC_LED_CTRL, tp->led_ctrl);
3430
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003431 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00003432 if ((tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS)) &&
3433 (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)))
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003434 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435
Joe Perches63c3a662011-04-26 08:12:10 +00003436 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00003437 mac_mode |= MAC_MODE_APE_TX_EN |
3438 MAC_MODE_APE_RX_EN |
3439 MAC_MODE_TDE_ENABLE;
Matt Carlson3bda1252008-08-15 14:08:22 -07003440
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 tw32_f(MAC_MODE, mac_mode);
3442 udelay(100);
3443
3444 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
3445 udelay(10);
3446 }
3447
Joe Perches63c3a662011-04-26 08:12:10 +00003448 if (!tg3_flag(tp, WOL_SPEED_100MB) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3450 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
3451 u32 base_val;
3452
3453 base_val = tp->pci_clock_ctrl;
3454 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
3455 CLOCK_CTRL_TXCLK_DISABLE);
3456
Michael Chanb401e9e2005-12-19 16:27:04 -08003457 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
3458 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Joe Perches63c3a662011-04-26 08:12:10 +00003459 } else if (tg3_flag(tp, 5780_CLASS) ||
3460 tg3_flag(tp, CPMU_PRESENT) ||
Matt Carlson6ff6f812011-05-19 12:12:54 +00003461 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan4cf78e42005-07-25 12:29:19 -07003462 /* do nothing */
Joe Perches63c3a662011-04-26 08:12:10 +00003463 } else if (!(tg3_flag(tp, 5750_PLUS) && tg3_flag(tp, ENABLE_ASF))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 u32 newbits1, newbits2;
3465
3466 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3467 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3468 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
3469 CLOCK_CTRL_TXCLK_DISABLE |
3470 CLOCK_CTRL_ALTCLK);
3471 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
Joe Perches63c3a662011-04-26 08:12:10 +00003472 } else if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 newbits1 = CLOCK_CTRL_625_CORE;
3474 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
3475 } else {
3476 newbits1 = CLOCK_CTRL_ALTCLK;
3477 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
3478 }
3479
Michael Chanb401e9e2005-12-19 16:27:04 -08003480 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
3481 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482
Michael Chanb401e9e2005-12-19 16:27:04 -08003483 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
3484 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485
Joe Perches63c3a662011-04-26 08:12:10 +00003486 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 u32 newbits3;
3488
3489 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3490 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3491 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
3492 CLOCK_CTRL_TXCLK_DISABLE |
3493 CLOCK_CTRL_44MHZ_CORE);
3494 } else {
3495 newbits3 = CLOCK_CTRL_44MHZ_CORE;
3496 }
3497
Michael Chanb401e9e2005-12-19 16:27:04 -08003498 tw32_wait_f(TG3PCI_CLOCK_CTRL,
3499 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 }
3501 }
3502
Joe Perches63c3a662011-04-26 08:12:10 +00003503 if (!(device_should_wake) && !tg3_flag(tp, ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08003504 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08003505
Matt Carlsoncd0d7222011-07-13 09:27:33 +00003506 tg3_frob_aux_power(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507
3508 /* Workaround for unstable PLL clock */
3509 if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
3510 (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
3511 u32 val = tr32(0x7d00);
3512
3513 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
3514 tw32(0x7d00, val);
Joe Perches63c3a662011-04-26 08:12:10 +00003515 if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08003516 int err;
3517
3518 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08003520 if (!err)
3521 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08003522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 }
3524
Michael Chanbbadf502006-04-06 21:46:34 -07003525 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
3526
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 return 0;
3528}
3529
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003530static void tg3_power_down(struct tg3 *tp)
3531{
3532 tg3_power_down_prepare(tp);
3533
Joe Perches63c3a662011-04-26 08:12:10 +00003534 pci_wake_from_d3(tp->pdev, tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003535 pci_set_power_state(tp->pdev, PCI_D3hot);
3536}
3537
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
3539{
3540 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
3541 case MII_TG3_AUX_STAT_10HALF:
3542 *speed = SPEED_10;
3543 *duplex = DUPLEX_HALF;
3544 break;
3545
3546 case MII_TG3_AUX_STAT_10FULL:
3547 *speed = SPEED_10;
3548 *duplex = DUPLEX_FULL;
3549 break;
3550
3551 case MII_TG3_AUX_STAT_100HALF:
3552 *speed = SPEED_100;
3553 *duplex = DUPLEX_HALF;
3554 break;
3555
3556 case MII_TG3_AUX_STAT_100FULL:
3557 *speed = SPEED_100;
3558 *duplex = DUPLEX_FULL;
3559 break;
3560
3561 case MII_TG3_AUX_STAT_1000HALF:
3562 *speed = SPEED_1000;
3563 *duplex = DUPLEX_HALF;
3564 break;
3565
3566 case MII_TG3_AUX_STAT_1000FULL:
3567 *speed = SPEED_1000;
3568 *duplex = DUPLEX_FULL;
3569 break;
3570
3571 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003572 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07003573 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
3574 SPEED_10;
3575 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
3576 DUPLEX_HALF;
3577 break;
3578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 *speed = SPEED_INVALID;
3580 *duplex = DUPLEX_INVALID;
3581 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583}
3584
Matt Carlson42b64a42011-05-19 12:12:49 +00003585static int tg3_phy_autoneg_cfg(struct tg3 *tp, u32 advertise, u32 flowctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586{
Matt Carlson42b64a42011-05-19 12:12:49 +00003587 int err = 0;
3588 u32 val, new_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589
Matt Carlson42b64a42011-05-19 12:12:49 +00003590 new_adv = ADVERTISE_CSMA;
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +00003591 new_adv |= ethtool_adv_to_mii_adv_t(advertise) & ADVERTISE_ALL;
Matt Carlson42b64a42011-05-19 12:12:49 +00003592 new_adv |= tg3_advert_flowctrl_1000T(flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593
Matt Carlson42b64a42011-05-19 12:12:49 +00003594 err = tg3_writephy(tp, MII_ADVERTISE, new_adv);
3595 if (err)
3596 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597
Matt Carlson4f272092011-12-14 11:09:57 +00003598 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
3599 new_adv = ethtool_adv_to_mii_ctrl1000_t(advertise);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003600
Matt Carlson4f272092011-12-14 11:09:57 +00003601 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3602 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
3603 new_adv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003604
Matt Carlson4f272092011-12-14 11:09:57 +00003605 err = tg3_writephy(tp, MII_CTRL1000, new_adv);
3606 if (err)
3607 goto done;
3608 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003609
Matt Carlson42b64a42011-05-19 12:12:49 +00003610 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
3611 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612
Matt Carlson42b64a42011-05-19 12:12:49 +00003613 tw32(TG3_CPMU_EEE_MODE,
3614 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003615
Matt Carlson42b64a42011-05-19 12:12:49 +00003616 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
3617 if (!err) {
3618 u32 err2;
Matt Carlson52b02d02010-10-14 10:37:41 +00003619
Matt Carlsona6b68da2010-12-06 08:28:52 +00003620 val = 0;
Matt Carlson42b64a42011-05-19 12:12:49 +00003621 /* Advertise 100-BaseTX EEE ability */
3622 if (advertise & ADVERTISED_100baseT_Full)
3623 val |= MDIO_AN_EEE_ADV_100TX;
3624 /* Advertise 1000-BaseT EEE ability */
3625 if (advertise & ADVERTISED_1000baseT_Full)
3626 val |= MDIO_AN_EEE_ADV_1000T;
3627 err = tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlsonb715ce92011-07-20 10:20:52 +00003628 if (err)
3629 val = 0;
3630
3631 switch (GET_ASIC_REV(tp->pci_chip_rev_id)) {
3632 case ASIC_REV_5717:
3633 case ASIC_REV_57765:
3634 case ASIC_REV_5719:
3635 /* If we advertised any eee advertisements above... */
3636 if (val)
3637 val = MII_TG3_DSP_TAP26_ALNOKO |
3638 MII_TG3_DSP_TAP26_RMRXSTO |
3639 MII_TG3_DSP_TAP26_OPCSINPT;
3640 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
3641 /* Fall through */
3642 case ASIC_REV_5720:
3643 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
3644 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
3645 MII_TG3_DSP_CH34TP2_HIBW01);
3646 }
Matt Carlson52b02d02010-10-14 10:37:41 +00003647
Matt Carlson42b64a42011-05-19 12:12:49 +00003648 err2 = TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
3649 if (!err)
3650 err = err2;
3651 }
3652
3653done:
3654 return err;
3655}
3656
3657static void tg3_phy_copper_begin(struct tg3 *tp)
3658{
3659 u32 new_adv;
3660 int i;
3661
3662 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
3663 new_adv = ADVERTISED_10baseT_Half |
3664 ADVERTISED_10baseT_Full;
3665 if (tg3_flag(tp, WOL_SPEED_100MB))
3666 new_adv |= ADVERTISED_100baseT_Half |
3667 ADVERTISED_100baseT_Full;
3668
3669 tg3_phy_autoneg_cfg(tp, new_adv,
3670 FLOW_CTRL_TX | FLOW_CTRL_RX);
3671 } else if (tp->link_config.speed == SPEED_INVALID) {
3672 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
3673 tp->link_config.advertising &=
3674 ~(ADVERTISED_1000baseT_Half |
3675 ADVERTISED_1000baseT_Full);
3676
3677 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
3678 tp->link_config.flowctrl);
3679 } else {
3680 /* Asking for a specific link mode. */
3681 if (tp->link_config.speed == SPEED_1000) {
3682 if (tp->link_config.duplex == DUPLEX_FULL)
3683 new_adv = ADVERTISED_1000baseT_Full;
3684 else
3685 new_adv = ADVERTISED_1000baseT_Half;
3686 } else if (tp->link_config.speed == SPEED_100) {
3687 if (tp->link_config.duplex == DUPLEX_FULL)
3688 new_adv = ADVERTISED_100baseT_Full;
3689 else
3690 new_adv = ADVERTISED_100baseT_Half;
3691 } else {
3692 if (tp->link_config.duplex == DUPLEX_FULL)
3693 new_adv = ADVERTISED_10baseT_Full;
3694 else
3695 new_adv = ADVERTISED_10baseT_Half;
3696 }
3697
3698 tg3_phy_autoneg_cfg(tp, new_adv,
3699 tp->link_config.flowctrl);
Matt Carlson52b02d02010-10-14 10:37:41 +00003700 }
3701
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 if (tp->link_config.autoneg == AUTONEG_DISABLE &&
3703 tp->link_config.speed != SPEED_INVALID) {
3704 u32 bmcr, orig_bmcr;
3705
3706 tp->link_config.active_speed = tp->link_config.speed;
3707 tp->link_config.active_duplex = tp->link_config.duplex;
3708
3709 bmcr = 0;
3710 switch (tp->link_config.speed) {
3711 default:
3712 case SPEED_10:
3713 break;
3714
3715 case SPEED_100:
3716 bmcr |= BMCR_SPEED100;
3717 break;
3718
3719 case SPEED_1000:
Matt Carlson221c5632011-06-13 13:39:01 +00003720 bmcr |= BMCR_SPEED1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723
3724 if (tp->link_config.duplex == DUPLEX_FULL)
3725 bmcr |= BMCR_FULLDPLX;
3726
3727 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
3728 (bmcr != orig_bmcr)) {
3729 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
3730 for (i = 0; i < 1500; i++) {
3731 u32 tmp;
3732
3733 udelay(10);
3734 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
3735 tg3_readphy(tp, MII_BMSR, &tmp))
3736 continue;
3737 if (!(tmp & BMSR_LSTATUS)) {
3738 udelay(40);
3739 break;
3740 }
3741 }
3742 tg3_writephy(tp, MII_BMCR, bmcr);
3743 udelay(40);
3744 }
3745 } else {
3746 tg3_writephy(tp, MII_BMCR,
3747 BMCR_ANENABLE | BMCR_ANRESTART);
3748 }
3749}
3750
3751static int tg3_init_5401phy_dsp(struct tg3 *tp)
3752{
3753 int err;
3754
3755 /* Turn off tap power management. */
3756 /* Set Extended packet length bit */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003757 err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00003759 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
3760 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
3761 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
3762 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
3763 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764
3765 udelay(40);
3766
3767 return err;
3768}
3769
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003770static bool tg3_phy_copper_an_config_ok(struct tg3 *tp, u32 *lcladv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771{
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003772 u32 advmsk, tgtadv, advertising;
Michael Chan3600d912006-12-07 00:21:48 -08003773
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003774 advertising = tp->link_config.advertising;
3775 tgtadv = ethtool_adv_to_mii_adv_t(advertising) & ADVERTISE_ALL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003777 advmsk = ADVERTISE_ALL;
3778 if (tp->link_config.active_duplex == DUPLEX_FULL) {
3779 tgtadv |= tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
3780 advmsk |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
3781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003783 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
3784 return false;
3785
3786 if ((*lcladv & advmsk) != tgtadv)
3787 return false;
Matt Carlsonb99d2a52011-08-31 11:44:47 +00003788
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003789 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 u32 tg3_ctrl;
3791
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003792 tgtadv = ethtool_adv_to_mii_ctrl1000_t(advertising);
Michael Chan3600d912006-12-07 00:21:48 -08003793
Matt Carlson221c5632011-06-13 13:39:01 +00003794 if (tg3_readphy(tp, MII_CTRL1000, &tg3_ctrl))
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003795 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796
Matt Carlsonb99d2a52011-08-31 11:44:47 +00003797 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL);
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003798 if (tg3_ctrl != tgtadv)
3799 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 }
Matt Carlson93a700a2011-08-31 11:44:54 +00003801
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003802 return true;
Matt Carlsonef167e22007-12-20 20:10:01 -08003803}
3804
Matt Carlson859edb22011-12-08 14:40:16 +00003805static bool tg3_phy_copper_fetch_rmtadv(struct tg3 *tp, u32 *rmtadv)
3806{
3807 u32 lpeth = 0;
3808
3809 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
3810 u32 val;
3811
3812 if (tg3_readphy(tp, MII_STAT1000, &val))
3813 return false;
3814
3815 lpeth = mii_stat1000_to_ethtool_lpa_t(val);
3816 }
3817
3818 if (tg3_readphy(tp, MII_LPA, rmtadv))
3819 return false;
3820
3821 lpeth |= mii_lpa_to_ethtool_lpa_t(*rmtadv);
3822 tp->link_config.rmt_adv = lpeth;
3823
3824 return true;
3825}
3826
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
3828{
3829 int current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003830 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08003831 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 u16 current_speed;
3833 u8 current_duplex;
3834 int i, err;
3835
3836 tw32(MAC_EVENT, 0);
3837
3838 tw32_f(MAC_STATUS,
3839 (MAC_STATUS_SYNC_CHANGED |
3840 MAC_STATUS_CFG_CHANGED |
3841 MAC_STATUS_MI_COMPLETION |
3842 MAC_STATUS_LNKSTATE_CHANGED));
3843 udelay(40);
3844
Matt Carlson8ef21422008-05-02 16:47:53 -07003845 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
3846 tw32_f(MAC_MI_MODE,
3847 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
3848 udelay(80);
3849 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003851 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852
3853 /* Some third-party PHYs need to be reset on link going
3854 * down.
3855 */
3856 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3857 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
3858 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
3859 netif_carrier_ok(tp->dev)) {
3860 tg3_readphy(tp, MII_BMSR, &bmsr);
3861 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3862 !(bmsr & BMSR_LSTATUS))
3863 force_reset = 1;
3864 }
3865 if (force_reset)
3866 tg3_phy_reset(tp);
3867
Matt Carlson79eb6902010-02-17 15:17:03 +00003868 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869 tg3_readphy(tp, MII_BMSR, &bmsr);
3870 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
Joe Perches63c3a662011-04-26 08:12:10 +00003871 !tg3_flag(tp, INIT_COMPLETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 bmsr = 0;
3873
3874 if (!(bmsr & BMSR_LSTATUS)) {
3875 err = tg3_init_5401phy_dsp(tp);
3876 if (err)
3877 return err;
3878
3879 tg3_readphy(tp, MII_BMSR, &bmsr);
3880 for (i = 0; i < 1000; i++) {
3881 udelay(10);
3882 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3883 (bmsr & BMSR_LSTATUS)) {
3884 udelay(40);
3885 break;
3886 }
3887 }
3888
Matt Carlson79eb6902010-02-17 15:17:03 +00003889 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
3890 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 !(bmsr & BMSR_LSTATUS) &&
3892 tp->link_config.active_speed == SPEED_1000) {
3893 err = tg3_phy_reset(tp);
3894 if (!err)
3895 err = tg3_init_5401phy_dsp(tp);
3896 if (err)
3897 return err;
3898 }
3899 }
3900 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3901 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
3902 /* 5701 {A0,B0} CRC bug workaround */
3903 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00003904 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
3905 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
3906 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 }
3908
3909 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003910 tg3_readphy(tp, MII_TG3_ISTAT, &val);
3911 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003913 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003915 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 tg3_writephy(tp, MII_TG3_IMASK, ~0);
3917
3918 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3919 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3920 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
3921 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3922 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
3923 else
3924 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
3925 }
3926
3927 current_link_up = 0;
3928 current_speed = SPEED_INVALID;
3929 current_duplex = DUPLEX_INVALID;
Matt Carlsone348c5e2011-11-21 15:01:20 +00003930 tp->phy_flags &= ~TG3_PHYFLG_MDIX_STATE;
Matt Carlson859edb22011-12-08 14:40:16 +00003931 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003933 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Matt Carlson15ee95c2011-04-20 07:57:40 +00003934 err = tg3_phy_auxctl_read(tp,
3935 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
3936 &val);
3937 if (!err && !(val & (1 << 10))) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003938 tg3_phy_auxctl_write(tp,
3939 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
3940 val | (1 << 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 goto relink;
3942 }
3943 }
3944
3945 bmsr = 0;
3946 for (i = 0; i < 100; i++) {
3947 tg3_readphy(tp, MII_BMSR, &bmsr);
3948 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3949 (bmsr & BMSR_LSTATUS))
3950 break;
3951 udelay(40);
3952 }
3953
3954 if (bmsr & BMSR_LSTATUS) {
3955 u32 aux_stat, bmcr;
3956
3957 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
3958 for (i = 0; i < 2000; i++) {
3959 udelay(10);
3960 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
3961 aux_stat)
3962 break;
3963 }
3964
3965 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
3966 &current_speed,
3967 &current_duplex);
3968
3969 bmcr = 0;
3970 for (i = 0; i < 200; i++) {
3971 tg3_readphy(tp, MII_BMCR, &bmcr);
3972 if (tg3_readphy(tp, MII_BMCR, &bmcr))
3973 continue;
3974 if (bmcr && bmcr != 0x7fff)
3975 break;
3976 udelay(10);
3977 }
3978
Matt Carlsonef167e22007-12-20 20:10:01 -08003979 lcl_adv = 0;
3980 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981
Matt Carlsonef167e22007-12-20 20:10:01 -08003982 tp->link_config.active_speed = current_speed;
3983 tp->link_config.active_duplex = current_duplex;
3984
3985 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3986 if ((bmcr & BMCR_ANENABLE) &&
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003987 tg3_phy_copper_an_config_ok(tp, &lcl_adv) &&
Matt Carlson859edb22011-12-08 14:40:16 +00003988 tg3_phy_copper_fetch_rmtadv(tp, &rmt_adv))
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003989 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990 } else {
3991 if (!(bmcr & BMCR_ANENABLE) &&
3992 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08003993 tp->link_config.duplex == current_duplex &&
3994 tp->link_config.flowctrl ==
3995 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 }
3998 }
3999
Matt Carlsonef167e22007-12-20 20:10:01 -08004000 if (current_link_up == 1 &&
Matt Carlsone348c5e2011-11-21 15:01:20 +00004001 tp->link_config.active_duplex == DUPLEX_FULL) {
4002 u32 reg, bit;
4003
4004 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
4005 reg = MII_TG3_FET_GEN_STAT;
4006 bit = MII_TG3_FET_GEN_STAT_MDIXSTAT;
4007 } else {
4008 reg = MII_TG3_EXT_STAT;
4009 bit = MII_TG3_EXT_STAT_MDIX;
4010 }
4011
4012 if (!tg3_readphy(tp, reg, &val) && (val & bit))
4013 tp->phy_flags |= TG3_PHYFLG_MDIX_STATE;
4014
Matt Carlsonef167e22007-12-20 20:10:01 -08004015 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Matt Carlsone348c5e2011-11-21 15:01:20 +00004016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 }
4018
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019relink:
Matt Carlson80096062010-08-02 11:26:06 +00004020 if (current_link_up == 0 || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 tg3_phy_copper_begin(tp);
4022
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004023 tg3_readphy(tp, MII_BMSR, &bmsr);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00004024 if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
4025 (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 current_link_up = 1;
4027 }
4028
4029 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
4030 if (current_link_up == 1) {
4031 if (tp->link_config.active_speed == SPEED_100 ||
4032 tp->link_config.active_speed == SPEED_10)
4033 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4034 else
4035 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004036 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00004037 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4038 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4040
4041 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4042 if (tp->link_config.active_duplex == DUPLEX_HALF)
4043 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4044
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004046 if (current_link_up == 1 &&
4047 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004049 else
4050 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 }
4052
4053 /* ??? Without this setting Netgear GA302T PHY does not
4054 * ??? send/receive packets...
4055 */
Matt Carlson79eb6902010-02-17 15:17:03 +00004056 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
4058 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
4059 tw32_f(MAC_MI_MODE, tp->mi_mode);
4060 udelay(80);
4061 }
4062
4063 tw32_f(MAC_MODE, tp->mac_mode);
4064 udelay(40);
4065
Matt Carlson52b02d02010-10-14 10:37:41 +00004066 tg3_phy_eee_adjust(tp, current_link_up);
4067
Joe Perches63c3a662011-04-26 08:12:10 +00004068 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 /* Polled via timer. */
4070 tw32_f(MAC_EVENT, 0);
4071 } else {
4072 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4073 }
4074 udelay(40);
4075
4076 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
4077 current_link_up == 1 &&
4078 tp->link_config.active_speed == SPEED_1000 &&
Joe Perches63c3a662011-04-26 08:12:10 +00004079 (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 udelay(120);
4081 tw32_f(MAC_STATUS,
4082 (MAC_STATUS_SYNC_CHANGED |
4083 MAC_STATUS_CFG_CHANGED));
4084 udelay(40);
4085 tg3_write_mem(tp,
4086 NIC_SRAM_FIRMWARE_MBOX,
4087 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
4088 }
4089
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004090 /* Prevent send BD corruption. */
Joe Perches63c3a662011-04-26 08:12:10 +00004091 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004092 u16 oldlnkctl, newlnkctl;
4093
4094 pci_read_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00004095 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004096 &oldlnkctl);
4097 if (tp->link_config.active_speed == SPEED_100 ||
4098 tp->link_config.active_speed == SPEED_10)
4099 newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
4100 else
4101 newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
4102 if (newlnkctl != oldlnkctl)
4103 pci_write_config_word(tp->pdev,
Matt Carlson93a700a2011-08-31 11:44:54 +00004104 pci_pcie_cap(tp->pdev) +
4105 PCI_EXP_LNKCTL, newlnkctl);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004106 }
4107
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 if (current_link_up != netif_carrier_ok(tp->dev)) {
4109 if (current_link_up)
4110 netif_carrier_on(tp->dev);
4111 else
4112 netif_carrier_off(tp->dev);
4113 tg3_link_report(tp);
4114 }
4115
4116 return 0;
4117}
4118
4119struct tg3_fiber_aneginfo {
4120 int state;
4121#define ANEG_STATE_UNKNOWN 0
4122#define ANEG_STATE_AN_ENABLE 1
4123#define ANEG_STATE_RESTART_INIT 2
4124#define ANEG_STATE_RESTART 3
4125#define ANEG_STATE_DISABLE_LINK_OK 4
4126#define ANEG_STATE_ABILITY_DETECT_INIT 5
4127#define ANEG_STATE_ABILITY_DETECT 6
4128#define ANEG_STATE_ACK_DETECT_INIT 7
4129#define ANEG_STATE_ACK_DETECT 8
4130#define ANEG_STATE_COMPLETE_ACK_INIT 9
4131#define ANEG_STATE_COMPLETE_ACK 10
4132#define ANEG_STATE_IDLE_DETECT_INIT 11
4133#define ANEG_STATE_IDLE_DETECT 12
4134#define ANEG_STATE_LINK_OK 13
4135#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
4136#define ANEG_STATE_NEXT_PAGE_WAIT 15
4137
4138 u32 flags;
4139#define MR_AN_ENABLE 0x00000001
4140#define MR_RESTART_AN 0x00000002
4141#define MR_AN_COMPLETE 0x00000004
4142#define MR_PAGE_RX 0x00000008
4143#define MR_NP_LOADED 0x00000010
4144#define MR_TOGGLE_TX 0x00000020
4145#define MR_LP_ADV_FULL_DUPLEX 0x00000040
4146#define MR_LP_ADV_HALF_DUPLEX 0x00000080
4147#define MR_LP_ADV_SYM_PAUSE 0x00000100
4148#define MR_LP_ADV_ASYM_PAUSE 0x00000200
4149#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
4150#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
4151#define MR_LP_ADV_NEXT_PAGE 0x00001000
4152#define MR_TOGGLE_RX 0x00002000
4153#define MR_NP_RX 0x00004000
4154
4155#define MR_LINK_OK 0x80000000
4156
4157 unsigned long link_time, cur_time;
4158
4159 u32 ability_match_cfg;
4160 int ability_match_count;
4161
4162 char ability_match, idle_match, ack_match;
4163
4164 u32 txconfig, rxconfig;
4165#define ANEG_CFG_NP 0x00000080
4166#define ANEG_CFG_ACK 0x00000040
4167#define ANEG_CFG_RF2 0x00000020
4168#define ANEG_CFG_RF1 0x00000010
4169#define ANEG_CFG_PS2 0x00000001
4170#define ANEG_CFG_PS1 0x00008000
4171#define ANEG_CFG_HD 0x00004000
4172#define ANEG_CFG_FD 0x00002000
4173#define ANEG_CFG_INVAL 0x00001f06
4174
4175};
4176#define ANEG_OK 0
4177#define ANEG_DONE 1
4178#define ANEG_TIMER_ENAB 2
4179#define ANEG_FAILED -1
4180
4181#define ANEG_STATE_SETTLE_TIME 10000
4182
4183static int tg3_fiber_aneg_smachine(struct tg3 *tp,
4184 struct tg3_fiber_aneginfo *ap)
4185{
Matt Carlson5be73b42007-12-20 20:09:29 -08004186 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 unsigned long delta;
4188 u32 rx_cfg_reg;
4189 int ret;
4190
4191 if (ap->state == ANEG_STATE_UNKNOWN) {
4192 ap->rxconfig = 0;
4193 ap->link_time = 0;
4194 ap->cur_time = 0;
4195 ap->ability_match_cfg = 0;
4196 ap->ability_match_count = 0;
4197 ap->ability_match = 0;
4198 ap->idle_match = 0;
4199 ap->ack_match = 0;
4200 }
4201 ap->cur_time++;
4202
4203 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
4204 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
4205
4206 if (rx_cfg_reg != ap->ability_match_cfg) {
4207 ap->ability_match_cfg = rx_cfg_reg;
4208 ap->ability_match = 0;
4209 ap->ability_match_count = 0;
4210 } else {
4211 if (++ap->ability_match_count > 1) {
4212 ap->ability_match = 1;
4213 ap->ability_match_cfg = rx_cfg_reg;
4214 }
4215 }
4216 if (rx_cfg_reg & ANEG_CFG_ACK)
4217 ap->ack_match = 1;
4218 else
4219 ap->ack_match = 0;
4220
4221 ap->idle_match = 0;
4222 } else {
4223 ap->idle_match = 1;
4224 ap->ability_match_cfg = 0;
4225 ap->ability_match_count = 0;
4226 ap->ability_match = 0;
4227 ap->ack_match = 0;
4228
4229 rx_cfg_reg = 0;
4230 }
4231
4232 ap->rxconfig = rx_cfg_reg;
4233 ret = ANEG_OK;
4234
Matt Carlson33f401a2010-04-05 10:19:27 +00004235 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 case ANEG_STATE_UNKNOWN:
4237 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
4238 ap->state = ANEG_STATE_AN_ENABLE;
4239
4240 /* fallthru */
4241 case ANEG_STATE_AN_ENABLE:
4242 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
4243 if (ap->flags & MR_AN_ENABLE) {
4244 ap->link_time = 0;
4245 ap->cur_time = 0;
4246 ap->ability_match_cfg = 0;
4247 ap->ability_match_count = 0;
4248 ap->ability_match = 0;
4249 ap->idle_match = 0;
4250 ap->ack_match = 0;
4251
4252 ap->state = ANEG_STATE_RESTART_INIT;
4253 } else {
4254 ap->state = ANEG_STATE_DISABLE_LINK_OK;
4255 }
4256 break;
4257
4258 case ANEG_STATE_RESTART_INIT:
4259 ap->link_time = ap->cur_time;
4260 ap->flags &= ~(MR_NP_LOADED);
4261 ap->txconfig = 0;
4262 tw32(MAC_TX_AUTO_NEG, 0);
4263 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4264 tw32_f(MAC_MODE, tp->mac_mode);
4265 udelay(40);
4266
4267 ret = ANEG_TIMER_ENAB;
4268 ap->state = ANEG_STATE_RESTART;
4269
4270 /* fallthru */
4271 case ANEG_STATE_RESTART:
4272 delta = ap->cur_time - ap->link_time;
Matt Carlson859a5882010-04-05 10:19:28 +00004273 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a5882010-04-05 10:19:28 +00004275 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 break;
4278
4279 case ANEG_STATE_DISABLE_LINK_OK:
4280 ret = ANEG_DONE;
4281 break;
4282
4283 case ANEG_STATE_ABILITY_DETECT_INIT:
4284 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08004285 ap->txconfig = ANEG_CFG_FD;
4286 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
4287 if (flowctrl & ADVERTISE_1000XPAUSE)
4288 ap->txconfig |= ANEG_CFG_PS1;
4289 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
4290 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
4292 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4293 tw32_f(MAC_MODE, tp->mac_mode);
4294 udelay(40);
4295
4296 ap->state = ANEG_STATE_ABILITY_DETECT;
4297 break;
4298
4299 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a5882010-04-05 10:19:28 +00004300 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 break;
4303
4304 case ANEG_STATE_ACK_DETECT_INIT:
4305 ap->txconfig |= ANEG_CFG_ACK;
4306 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
4307 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4308 tw32_f(MAC_MODE, tp->mac_mode);
4309 udelay(40);
4310
4311 ap->state = ANEG_STATE_ACK_DETECT;
4312
4313 /* fallthru */
4314 case ANEG_STATE_ACK_DETECT:
4315 if (ap->ack_match != 0) {
4316 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
4317 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
4318 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
4319 } else {
4320 ap->state = ANEG_STATE_AN_ENABLE;
4321 }
4322 } else if (ap->ability_match != 0 &&
4323 ap->rxconfig == 0) {
4324 ap->state = ANEG_STATE_AN_ENABLE;
4325 }
4326 break;
4327
4328 case ANEG_STATE_COMPLETE_ACK_INIT:
4329 if (ap->rxconfig & ANEG_CFG_INVAL) {
4330 ret = ANEG_FAILED;
4331 break;
4332 }
4333 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
4334 MR_LP_ADV_HALF_DUPLEX |
4335 MR_LP_ADV_SYM_PAUSE |
4336 MR_LP_ADV_ASYM_PAUSE |
4337 MR_LP_ADV_REMOTE_FAULT1 |
4338 MR_LP_ADV_REMOTE_FAULT2 |
4339 MR_LP_ADV_NEXT_PAGE |
4340 MR_TOGGLE_RX |
4341 MR_NP_RX);
4342 if (ap->rxconfig & ANEG_CFG_FD)
4343 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
4344 if (ap->rxconfig & ANEG_CFG_HD)
4345 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
4346 if (ap->rxconfig & ANEG_CFG_PS1)
4347 ap->flags |= MR_LP_ADV_SYM_PAUSE;
4348 if (ap->rxconfig & ANEG_CFG_PS2)
4349 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
4350 if (ap->rxconfig & ANEG_CFG_RF1)
4351 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
4352 if (ap->rxconfig & ANEG_CFG_RF2)
4353 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
4354 if (ap->rxconfig & ANEG_CFG_NP)
4355 ap->flags |= MR_LP_ADV_NEXT_PAGE;
4356
4357 ap->link_time = ap->cur_time;
4358
4359 ap->flags ^= (MR_TOGGLE_TX);
4360 if (ap->rxconfig & 0x0008)
4361 ap->flags |= MR_TOGGLE_RX;
4362 if (ap->rxconfig & ANEG_CFG_NP)
4363 ap->flags |= MR_NP_RX;
4364 ap->flags |= MR_PAGE_RX;
4365
4366 ap->state = ANEG_STATE_COMPLETE_ACK;
4367 ret = ANEG_TIMER_ENAB;
4368 break;
4369
4370 case ANEG_STATE_COMPLETE_ACK:
4371 if (ap->ability_match != 0 &&
4372 ap->rxconfig == 0) {
4373 ap->state = ANEG_STATE_AN_ENABLE;
4374 break;
4375 }
4376 delta = ap->cur_time - ap->link_time;
4377 if (delta > ANEG_STATE_SETTLE_TIME) {
4378 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
4379 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
4380 } else {
4381 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
4382 !(ap->flags & MR_NP_RX)) {
4383 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
4384 } else {
4385 ret = ANEG_FAILED;
4386 }
4387 }
4388 }
4389 break;
4390
4391 case ANEG_STATE_IDLE_DETECT_INIT:
4392 ap->link_time = ap->cur_time;
4393 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
4394 tw32_f(MAC_MODE, tp->mac_mode);
4395 udelay(40);
4396
4397 ap->state = ANEG_STATE_IDLE_DETECT;
4398 ret = ANEG_TIMER_ENAB;
4399 break;
4400
4401 case ANEG_STATE_IDLE_DETECT:
4402 if (ap->ability_match != 0 &&
4403 ap->rxconfig == 0) {
4404 ap->state = ANEG_STATE_AN_ENABLE;
4405 break;
4406 }
4407 delta = ap->cur_time - ap->link_time;
4408 if (delta > ANEG_STATE_SETTLE_TIME) {
4409 /* XXX another gem from the Broadcom driver :( */
4410 ap->state = ANEG_STATE_LINK_OK;
4411 }
4412 break;
4413
4414 case ANEG_STATE_LINK_OK:
4415 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
4416 ret = ANEG_DONE;
4417 break;
4418
4419 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
4420 /* ??? unimplemented */
4421 break;
4422
4423 case ANEG_STATE_NEXT_PAGE_WAIT:
4424 /* ??? unimplemented */
4425 break;
4426
4427 default:
4428 ret = ANEG_FAILED;
4429 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431
4432 return ret;
4433}
4434
Matt Carlson5be73b42007-12-20 20:09:29 -08004435static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436{
4437 int res = 0;
4438 struct tg3_fiber_aneginfo aninfo;
4439 int status = ANEG_FAILED;
4440 unsigned int tick;
4441 u32 tmp;
4442
4443 tw32_f(MAC_TX_AUTO_NEG, 0);
4444
4445 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
4446 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
4447 udelay(40);
4448
4449 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
4450 udelay(40);
4451
4452 memset(&aninfo, 0, sizeof(aninfo));
4453 aninfo.flags |= MR_AN_ENABLE;
4454 aninfo.state = ANEG_STATE_UNKNOWN;
4455 aninfo.cur_time = 0;
4456 tick = 0;
4457 while (++tick < 195000) {
4458 status = tg3_fiber_aneg_smachine(tp, &aninfo);
4459 if (status == ANEG_DONE || status == ANEG_FAILED)
4460 break;
4461
4462 udelay(1);
4463 }
4464
4465 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
4466 tw32_f(MAC_MODE, tp->mac_mode);
4467 udelay(40);
4468
Matt Carlson5be73b42007-12-20 20:09:29 -08004469 *txflags = aninfo.txconfig;
4470 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471
4472 if (status == ANEG_DONE &&
4473 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
4474 MR_LP_ADV_FULL_DUPLEX)))
4475 res = 1;
4476
4477 return res;
4478}
4479
4480static void tg3_init_bcm8002(struct tg3 *tp)
4481{
4482 u32 mac_status = tr32(MAC_STATUS);
4483 int i;
4484
4485 /* Reset when initting first time or we have a link. */
Joe Perches63c3a662011-04-26 08:12:10 +00004486 if (tg3_flag(tp, INIT_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 !(mac_status & MAC_STATUS_PCS_SYNCED))
4488 return;
4489
4490 /* Set PLL lock range. */
4491 tg3_writephy(tp, 0x16, 0x8007);
4492
4493 /* SW reset */
4494 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
4495
4496 /* Wait for reset to complete. */
4497 /* XXX schedule_timeout() ... */
4498 for (i = 0; i < 500; i++)
4499 udelay(10);
4500
4501 /* Config mode; select PMA/Ch 1 regs. */
4502 tg3_writephy(tp, 0x10, 0x8411);
4503
4504 /* Enable auto-lock and comdet, select txclk for tx. */
4505 tg3_writephy(tp, 0x11, 0x0a10);
4506
4507 tg3_writephy(tp, 0x18, 0x00a0);
4508 tg3_writephy(tp, 0x16, 0x41ff);
4509
4510 /* Assert and deassert POR. */
4511 tg3_writephy(tp, 0x13, 0x0400);
4512 udelay(40);
4513 tg3_writephy(tp, 0x13, 0x0000);
4514
4515 tg3_writephy(tp, 0x11, 0x0a50);
4516 udelay(40);
4517 tg3_writephy(tp, 0x11, 0x0a10);
4518
4519 /* Wait for signal to stabilize */
4520 /* XXX schedule_timeout() ... */
4521 for (i = 0; i < 15000; i++)
4522 udelay(10);
4523
4524 /* Deselect the channel register so we can read the PHYID
4525 * later.
4526 */
4527 tg3_writephy(tp, 0x10, 0x8011);
4528}
4529
4530static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
4531{
Matt Carlson82cd3d12007-12-20 20:09:00 -08004532 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 u32 sg_dig_ctrl, sg_dig_status;
4534 u32 serdes_cfg, expected_sg_dig_ctrl;
4535 int workaround, port_a;
4536 int current_link_up;
4537
4538 serdes_cfg = 0;
4539 expected_sg_dig_ctrl = 0;
4540 workaround = 0;
4541 port_a = 1;
4542 current_link_up = 0;
4543
4544 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
4545 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
4546 workaround = 1;
4547 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
4548 port_a = 0;
4549
4550 /* preserve bits 0-11,13,14 for signal pre-emphasis */
4551 /* preserve bits 20-23 for voltage regulator */
4552 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
4553 }
4554
4555 sg_dig_ctrl = tr32(SG_DIG_CTRL);
4556
4557 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004558 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 if (workaround) {
4560 u32 val = serdes_cfg;
4561
4562 if (port_a)
4563 val |= 0xc010000;
4564 else
4565 val |= 0x4010000;
4566 tw32_f(MAC_SERDES_CFG, val);
4567 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004568
4569 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570 }
4571 if (mac_status & MAC_STATUS_PCS_SYNCED) {
4572 tg3_setup_flow_control(tp, 0, 0);
4573 current_link_up = 1;
4574 }
4575 goto out;
4576 }
4577
4578 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004579 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580
Matt Carlson82cd3d12007-12-20 20:09:00 -08004581 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
4582 if (flowctrl & ADVERTISE_1000XPAUSE)
4583 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
4584 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
4585 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586
4587 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004588 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07004589 tp->serdes_counter &&
4590 ((mac_status & (MAC_STATUS_PCS_SYNCED |
4591 MAC_STATUS_RCVD_CFG)) ==
4592 MAC_STATUS_PCS_SYNCED)) {
4593 tp->serdes_counter--;
4594 current_link_up = 1;
4595 goto out;
4596 }
4597restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598 if (workaround)
4599 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004600 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601 udelay(5);
4602 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
4603
Michael Chan3d3ebe72006-09-27 15:59:15 -07004604 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004605 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
4607 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07004608 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 mac_status = tr32(MAC_STATUS);
4610
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004611 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08004613 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614
Matt Carlson82cd3d12007-12-20 20:09:00 -08004615 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
4616 local_adv |= ADVERTISE_1000XPAUSE;
4617 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
4618 local_adv |= ADVERTISE_1000XPSE_ASYM;
4619
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004620 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08004621 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004622 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08004623 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624
Matt Carlson859edb22011-12-08 14:40:16 +00004625 tp->link_config.rmt_adv =
4626 mii_adv_to_ethtool_adv_x(remote_adv);
4627
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628 tg3_setup_flow_control(tp, local_adv, remote_adv);
4629 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004630 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004631 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004632 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07004633 if (tp->serdes_counter)
4634 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635 else {
4636 if (workaround) {
4637 u32 val = serdes_cfg;
4638
4639 if (port_a)
4640 val |= 0xc010000;
4641 else
4642 val |= 0x4010000;
4643
4644 tw32_f(MAC_SERDES_CFG, val);
4645 }
4646
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004647 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 udelay(40);
4649
4650 /* Link parallel detection - link is up */
4651 /* only if we have PCS_SYNC and not */
4652 /* receiving config code words */
4653 mac_status = tr32(MAC_STATUS);
4654 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
4655 !(mac_status & MAC_STATUS_RCVD_CFG)) {
4656 tg3_setup_flow_control(tp, 0, 0);
4657 current_link_up = 1;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004658 tp->phy_flags |=
4659 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004660 tp->serdes_counter =
4661 SERDES_PARALLEL_DET_TIMEOUT;
4662 } else
4663 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 }
4665 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07004666 } else {
4667 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004668 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 }
4670
4671out:
4672 return current_link_up;
4673}
4674
4675static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
4676{
4677 int current_link_up = 0;
4678
Michael Chan5cf64b82007-05-05 12:11:21 -07004679 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681
4682 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08004683 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004685
Matt Carlson5be73b42007-12-20 20:09:29 -08004686 if (fiber_autoneg(tp, &txflags, &rxflags)) {
4687 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688
Matt Carlson5be73b42007-12-20 20:09:29 -08004689 if (txflags & ANEG_CFG_PS1)
4690 local_adv |= ADVERTISE_1000XPAUSE;
4691 if (txflags & ANEG_CFG_PS2)
4692 local_adv |= ADVERTISE_1000XPSE_ASYM;
4693
4694 if (rxflags & MR_LP_ADV_SYM_PAUSE)
4695 remote_adv |= LPA_1000XPAUSE;
4696 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
4697 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698
Matt Carlson859edb22011-12-08 14:40:16 +00004699 tp->link_config.rmt_adv =
4700 mii_adv_to_ethtool_adv_x(remote_adv);
4701
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702 tg3_setup_flow_control(tp, local_adv, remote_adv);
4703
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704 current_link_up = 1;
4705 }
4706 for (i = 0; i < 30; i++) {
4707 udelay(20);
4708 tw32_f(MAC_STATUS,
4709 (MAC_STATUS_SYNC_CHANGED |
4710 MAC_STATUS_CFG_CHANGED));
4711 udelay(40);
4712 if ((tr32(MAC_STATUS) &
4713 (MAC_STATUS_SYNC_CHANGED |
4714 MAC_STATUS_CFG_CHANGED)) == 0)
4715 break;
4716 }
4717
4718 mac_status = tr32(MAC_STATUS);
4719 if (current_link_up == 0 &&
4720 (mac_status & MAC_STATUS_PCS_SYNCED) &&
4721 !(mac_status & MAC_STATUS_RCVD_CFG))
4722 current_link_up = 1;
4723 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08004724 tg3_setup_flow_control(tp, 0, 0);
4725
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726 /* Forcing 1000FD link up. */
4727 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728
4729 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
4730 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004731
4732 tw32_f(MAC_MODE, tp->mac_mode);
4733 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734 }
4735
4736out:
4737 return current_link_up;
4738}
4739
4740static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
4741{
4742 u32 orig_pause_cfg;
4743 u16 orig_active_speed;
4744 u8 orig_active_duplex;
4745 u32 mac_status;
4746 int current_link_up;
4747 int i;
4748
Matt Carlson8d018622007-12-20 20:05:44 -08004749 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750 orig_active_speed = tp->link_config.active_speed;
4751 orig_active_duplex = tp->link_config.active_duplex;
4752
Joe Perches63c3a662011-04-26 08:12:10 +00004753 if (!tg3_flag(tp, HW_AUTONEG) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754 netif_carrier_ok(tp->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00004755 tg3_flag(tp, INIT_COMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756 mac_status = tr32(MAC_STATUS);
4757 mac_status &= (MAC_STATUS_PCS_SYNCED |
4758 MAC_STATUS_SIGNAL_DET |
4759 MAC_STATUS_CFG_CHANGED |
4760 MAC_STATUS_RCVD_CFG);
4761 if (mac_status == (MAC_STATUS_PCS_SYNCED |
4762 MAC_STATUS_SIGNAL_DET)) {
4763 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4764 MAC_STATUS_CFG_CHANGED));
4765 return 0;
4766 }
4767 }
4768
4769 tw32_f(MAC_TX_AUTO_NEG, 0);
4770
4771 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
4772 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
4773 tw32_f(MAC_MODE, tp->mac_mode);
4774 udelay(40);
4775
Matt Carlson79eb6902010-02-17 15:17:03 +00004776 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 tg3_init_bcm8002(tp);
4778
4779 /* Enable link change event even when serdes polling. */
4780 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4781 udelay(40);
4782
4783 current_link_up = 0;
Matt Carlson859edb22011-12-08 14:40:16 +00004784 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785 mac_status = tr32(MAC_STATUS);
4786
Joe Perches63c3a662011-04-26 08:12:10 +00004787 if (tg3_flag(tp, HW_AUTONEG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
4789 else
4790 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
4791
Matt Carlson898a56f2009-08-28 14:02:40 +00004792 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00004794 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795
4796 for (i = 0; i < 100; i++) {
4797 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4798 MAC_STATUS_CFG_CHANGED));
4799 udelay(5);
4800 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07004801 MAC_STATUS_CFG_CHANGED |
4802 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803 break;
4804 }
4805
4806 mac_status = tr32(MAC_STATUS);
4807 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
4808 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004809 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
4810 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811 tw32_f(MAC_MODE, (tp->mac_mode |
4812 MAC_MODE_SEND_CONFIGS));
4813 udelay(1);
4814 tw32_f(MAC_MODE, tp->mac_mode);
4815 }
4816 }
4817
4818 if (current_link_up == 1) {
4819 tp->link_config.active_speed = SPEED_1000;
4820 tp->link_config.active_duplex = DUPLEX_FULL;
4821 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4822 LED_CTRL_LNKLED_OVERRIDE |
4823 LED_CTRL_1000MBPS_ON));
4824 } else {
4825 tp->link_config.active_speed = SPEED_INVALID;
4826 tp->link_config.active_duplex = DUPLEX_INVALID;
4827 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4828 LED_CTRL_LNKLED_OVERRIDE |
4829 LED_CTRL_TRAFFIC_OVERRIDE));
4830 }
4831
4832 if (current_link_up != netif_carrier_ok(tp->dev)) {
4833 if (current_link_up)
4834 netif_carrier_on(tp->dev);
4835 else
4836 netif_carrier_off(tp->dev);
4837 tg3_link_report(tp);
4838 } else {
Matt Carlson8d018622007-12-20 20:05:44 -08004839 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840 if (orig_pause_cfg != now_pause_cfg ||
4841 orig_active_speed != tp->link_config.active_speed ||
4842 orig_active_duplex != tp->link_config.active_duplex)
4843 tg3_link_report(tp);
4844 }
4845
4846 return 0;
4847}
4848
Michael Chan747e8f82005-07-25 12:33:22 -07004849static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
4850{
4851 int current_link_up, err = 0;
4852 u32 bmsr, bmcr;
4853 u16 current_speed;
4854 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08004855 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07004856
4857 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4858 tw32_f(MAC_MODE, tp->mac_mode);
4859 udelay(40);
4860
4861 tw32(MAC_EVENT, 0);
4862
4863 tw32_f(MAC_STATUS,
4864 (MAC_STATUS_SYNC_CHANGED |
4865 MAC_STATUS_CFG_CHANGED |
4866 MAC_STATUS_MI_COMPLETION |
4867 MAC_STATUS_LNKSTATE_CHANGED));
4868 udelay(40);
4869
4870 if (force_reset)
4871 tg3_phy_reset(tp);
4872
4873 current_link_up = 0;
4874 current_speed = SPEED_INVALID;
4875 current_duplex = DUPLEX_INVALID;
Matt Carlson859edb22011-12-08 14:40:16 +00004876 tp->link_config.rmt_adv = 0;
Michael Chan747e8f82005-07-25 12:33:22 -07004877
4878 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4879 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004880 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
4881 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4882 bmsr |= BMSR_LSTATUS;
4883 else
4884 bmsr &= ~BMSR_LSTATUS;
4885 }
Michael Chan747e8f82005-07-25 12:33:22 -07004886
4887 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
4888
4889 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004890 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004891 /* do nothing, just check for link up at the end */
4892 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson28011cf2011-11-16 18:36:59 -05004893 u32 adv, newadv;
Michael Chan747e8f82005-07-25 12:33:22 -07004894
4895 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
Matt Carlson28011cf2011-11-16 18:36:59 -05004896 newadv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
4897 ADVERTISE_1000XPAUSE |
4898 ADVERTISE_1000XPSE_ASYM |
4899 ADVERTISE_SLCT);
Michael Chan747e8f82005-07-25 12:33:22 -07004900
Matt Carlson28011cf2011-11-16 18:36:59 -05004901 newadv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Matt Carlson37f07022011-11-17 14:30:55 +00004902 newadv |= ethtool_adv_to_mii_adv_x(tp->link_config.advertising);
Michael Chan747e8f82005-07-25 12:33:22 -07004903
Matt Carlson28011cf2011-11-16 18:36:59 -05004904 if ((newadv != adv) || !(bmcr & BMCR_ANENABLE)) {
4905 tg3_writephy(tp, MII_ADVERTISE, newadv);
Michael Chan747e8f82005-07-25 12:33:22 -07004906 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
4907 tg3_writephy(tp, MII_BMCR, bmcr);
4908
4909 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07004910 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004911 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004912
4913 return err;
4914 }
4915 } else {
4916 u32 new_bmcr;
4917
4918 bmcr &= ~BMCR_SPEED1000;
4919 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
4920
4921 if (tp->link_config.duplex == DUPLEX_FULL)
4922 new_bmcr |= BMCR_FULLDPLX;
4923
4924 if (new_bmcr != bmcr) {
4925 /* BMCR_SPEED1000 is a reserved bit that needs
4926 * to be set on write.
4927 */
4928 new_bmcr |= BMCR_SPEED1000;
4929
4930 /* Force a linkdown */
4931 if (netif_carrier_ok(tp->dev)) {
4932 u32 adv;
4933
4934 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4935 adv &= ~(ADVERTISE_1000XFULL |
4936 ADVERTISE_1000XHALF |
4937 ADVERTISE_SLCT);
4938 tg3_writephy(tp, MII_ADVERTISE, adv);
4939 tg3_writephy(tp, MII_BMCR, bmcr |
4940 BMCR_ANRESTART |
4941 BMCR_ANENABLE);
4942 udelay(10);
4943 netif_carrier_off(tp->dev);
4944 }
4945 tg3_writephy(tp, MII_BMCR, new_bmcr);
4946 bmcr = new_bmcr;
4947 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4948 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004949 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
4950 ASIC_REV_5714) {
4951 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4952 bmsr |= BMSR_LSTATUS;
4953 else
4954 bmsr &= ~BMSR_LSTATUS;
4955 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004956 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004957 }
4958 }
4959
4960 if (bmsr & BMSR_LSTATUS) {
4961 current_speed = SPEED_1000;
4962 current_link_up = 1;
4963 if (bmcr & BMCR_FULLDPLX)
4964 current_duplex = DUPLEX_FULL;
4965 else
4966 current_duplex = DUPLEX_HALF;
4967
Matt Carlsonef167e22007-12-20 20:10:01 -08004968 local_adv = 0;
4969 remote_adv = 0;
4970
Michael Chan747e8f82005-07-25 12:33:22 -07004971 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08004972 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07004973
4974 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
4975 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
4976 common = local_adv & remote_adv;
4977 if (common & (ADVERTISE_1000XHALF |
4978 ADVERTISE_1000XFULL)) {
4979 if (common & ADVERTISE_1000XFULL)
4980 current_duplex = DUPLEX_FULL;
4981 else
4982 current_duplex = DUPLEX_HALF;
Matt Carlson859edb22011-12-08 14:40:16 +00004983
4984 tp->link_config.rmt_adv =
4985 mii_adv_to_ethtool_adv_x(remote_adv);
Joe Perches63c3a662011-04-26 08:12:10 +00004986 } else if (!tg3_flag(tp, 5780_CLASS)) {
Matt Carlson57d8b882010-06-05 17:24:35 +00004987 /* Link is up via parallel detect */
Matt Carlson859a5882010-04-05 10:19:28 +00004988 } else {
Michael Chan747e8f82005-07-25 12:33:22 -07004989 current_link_up = 0;
Matt Carlson859a5882010-04-05 10:19:28 +00004990 }
Michael Chan747e8f82005-07-25 12:33:22 -07004991 }
4992 }
4993
Matt Carlsonef167e22007-12-20 20:10:01 -08004994 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
4995 tg3_setup_flow_control(tp, local_adv, remote_adv);
4996
Michael Chan747e8f82005-07-25 12:33:22 -07004997 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4998 if (tp->link_config.active_duplex == DUPLEX_HALF)
4999 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
5000
5001 tw32_f(MAC_MODE, tp->mac_mode);
5002 udelay(40);
5003
5004 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5005
5006 tp->link_config.active_speed = current_speed;
5007 tp->link_config.active_duplex = current_duplex;
5008
5009 if (current_link_up != netif_carrier_ok(tp->dev)) {
5010 if (current_link_up)
5011 netif_carrier_on(tp->dev);
5012 else {
5013 netif_carrier_off(tp->dev);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005014 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005015 }
5016 tg3_link_report(tp);
5017 }
5018 return err;
5019}
5020
5021static void tg3_serdes_parallel_detect(struct tg3 *tp)
5022{
Michael Chan3d3ebe72006-09-27 15:59:15 -07005023 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07005024 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07005025 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07005026 return;
5027 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005028
Michael Chan747e8f82005-07-25 12:33:22 -07005029 if (!netif_carrier_ok(tp->dev) &&
5030 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
5031 u32 bmcr;
5032
5033 tg3_readphy(tp, MII_BMCR, &bmcr);
5034 if (bmcr & BMCR_ANENABLE) {
5035 u32 phy1, phy2;
5036
5037 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005038 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
5039 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07005040
5041 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005042 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5043 MII_TG3_DSP_EXP1_INT_STAT);
5044 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
5045 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005046
5047 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
5048 /* We have signal detect and not receiving
5049 * config code words, link is up by parallel
5050 * detection.
5051 */
5052
5053 bmcr &= ~BMCR_ANENABLE;
5054 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
5055 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005056 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005057 }
5058 }
Matt Carlson859a5882010-04-05 10:19:28 +00005059 } else if (netif_carrier_ok(tp->dev) &&
5060 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005061 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005062 u32 phy2;
5063
5064 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005065 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5066 MII_TG3_DSP_EXP1_INT_STAT);
5067 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005068 if (phy2 & 0x20) {
5069 u32 bmcr;
5070
5071 /* Config code words received, turn on autoneg. */
5072 tg3_readphy(tp, MII_BMCR, &bmcr);
5073 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
5074
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005075 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005076
5077 }
5078 }
5079}
5080
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081static int tg3_setup_phy(struct tg3 *tp, int force_reset)
5082{
Matt Carlsonf2096f92011-04-05 14:22:48 +00005083 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084 int err;
5085
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005086 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005088 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07005089 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a5882010-04-05 10:19:28 +00005090 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092
Matt Carlsonbcb37f62008-11-03 16:52:09 -08005093 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00005094 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08005095
5096 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
5097 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
5098 scale = 65;
5099 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
5100 scale = 6;
5101 else
5102 scale = 12;
5103
5104 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
5105 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
5106 tw32(GRC_MISC_CFG, val);
5107 }
5108
Matt Carlsonf2096f92011-04-05 14:22:48 +00005109 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
5110 (6 << TX_LENGTHS_IPG_SHIFT);
5111 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
5112 val |= tr32(MAC_TX_LENGTHS) &
5113 (TX_LENGTHS_JMB_FRM_LEN_MSK |
5114 TX_LENGTHS_CNT_DWN_VAL_MSK);
5115
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116 if (tp->link_config.active_speed == SPEED_1000 &&
5117 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00005118 tw32(MAC_TX_LENGTHS, val |
5119 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00005121 tw32(MAC_TX_LENGTHS, val |
5122 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123
Joe Perches63c3a662011-04-26 08:12:10 +00005124 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125 if (netif_carrier_ok(tp->dev)) {
5126 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07005127 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128 } else {
5129 tw32(HOSTCC_STAT_COAL_TICKS, 0);
5130 }
5131 }
5132
Joe Perches63c3a662011-04-26 08:12:10 +00005133 if (tg3_flag(tp, ASPM_WORKAROUND)) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00005134 val = tr32(PCIE_PWR_MGMT_THRESH);
Matt Carlson8ed5d972007-05-07 00:25:49 -07005135 if (!netif_carrier_ok(tp->dev))
5136 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
5137 tp->pwrmgmt_thresh;
5138 else
5139 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
5140 tw32(PCIE_PWR_MGMT_THRESH, val);
5141 }
5142
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143 return err;
5144}
5145
Matt Carlson66cfd1b2010-09-30 10:34:30 +00005146static inline int tg3_irq_sync(struct tg3 *tp)
5147{
5148 return tp->irq_sync;
5149}
5150
Matt Carlson97bd8e42011-04-13 11:05:04 +00005151static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
5152{
5153 int i;
5154
5155 dst = (u32 *)((u8 *)dst + off);
5156 for (i = 0; i < len; i += sizeof(u32))
5157 *dst++ = tr32(off + i);
5158}
5159
5160static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
5161{
5162 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
5163 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
5164 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
5165 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
5166 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
5167 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
5168 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
5169 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
5170 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
5171 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
5172 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
5173 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
5174 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
5175 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
5176 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
5177 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
5178 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
5179 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
5180 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
5181
Joe Perches63c3a662011-04-26 08:12:10 +00005182 if (tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson97bd8e42011-04-13 11:05:04 +00005183 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
5184
5185 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
5186 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
5187 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
5188 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
5189 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
5190 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
5191 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
5192 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
5193
Joe Perches63c3a662011-04-26 08:12:10 +00005194 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00005195 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
5196 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
5197 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
5198 }
5199
5200 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
5201 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
5202 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
5203 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
5204 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
5205
Joe Perches63c3a662011-04-26 08:12:10 +00005206 if (tg3_flag(tp, NVRAM))
Matt Carlson97bd8e42011-04-13 11:05:04 +00005207 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
5208}
5209
5210static void tg3_dump_state(struct tg3 *tp)
5211{
5212 int i;
5213 u32 *regs;
5214
5215 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
5216 if (!regs) {
5217 netdev_err(tp->dev, "Failed allocating register dump buffer\n");
5218 return;
5219 }
5220
Joe Perches63c3a662011-04-26 08:12:10 +00005221 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00005222 /* Read up to but not including private PCI registers */
5223 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
5224 regs[i / sizeof(u32)] = tr32(i);
5225 } else
5226 tg3_dump_legacy_regs(tp, regs);
5227
5228 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
5229 if (!regs[i + 0] && !regs[i + 1] &&
5230 !regs[i + 2] && !regs[i + 3])
5231 continue;
5232
5233 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
5234 i * 4,
5235 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
5236 }
5237
5238 kfree(regs);
5239
5240 for (i = 0; i < tp->irq_cnt; i++) {
5241 struct tg3_napi *tnapi = &tp->napi[i];
5242
5243 /* SW status block */
5244 netdev_err(tp->dev,
5245 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
5246 i,
5247 tnapi->hw_status->status,
5248 tnapi->hw_status->status_tag,
5249 tnapi->hw_status->rx_jumbo_consumer,
5250 tnapi->hw_status->rx_consumer,
5251 tnapi->hw_status->rx_mini_consumer,
5252 tnapi->hw_status->idx[0].rx_producer,
5253 tnapi->hw_status->idx[0].tx_consumer);
5254
5255 netdev_err(tp->dev,
5256 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
5257 i,
5258 tnapi->last_tag, tnapi->last_irq_tag,
5259 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
5260 tnapi->rx_rcb_ptr,
5261 tnapi->prodring.rx_std_prod_idx,
5262 tnapi->prodring.rx_std_cons_idx,
5263 tnapi->prodring.rx_jmb_prod_idx,
5264 tnapi->prodring.rx_jmb_cons_idx);
5265 }
5266}
5267
Michael Chandf3e6542006-05-26 17:48:07 -07005268/* This is called whenever we suspect that the system chipset is re-
5269 * ordering the sequence of MMIO to the tx send mailbox. The symptom
5270 * is bogus tx completions. We try to recover by setting the
5271 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
5272 * in the workqueue.
5273 */
5274static void tg3_tx_recover(struct tg3 *tp)
5275{
Joe Perches63c3a662011-04-26 08:12:10 +00005276 BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
Michael Chandf3e6542006-05-26 17:48:07 -07005277 tp->write32_tx_mbox == tg3_write_indirect_mbox);
5278
Matt Carlson5129c3a2010-04-05 10:19:23 +00005279 netdev_warn(tp->dev,
5280 "The system may be re-ordering memory-mapped I/O "
5281 "cycles to the network device, attempting to recover. "
5282 "Please report the problem to the driver maintainer "
5283 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07005284
5285 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00005286 tg3_flag_set(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07005287 spin_unlock(&tp->lock);
5288}
5289
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005290static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07005291{
Matt Carlsonf65aac12010-08-02 11:26:03 +00005292 /* Tell compiler to fetch tx indices from memory. */
5293 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005294 return tnapi->tx_pending -
5295 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07005296}
5297
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298/* Tigon3 never reports partial packet sends. So we do not
5299 * need special logic to handle SKBs that have not had all
5300 * of their frags sent yet, like SunGEM does.
5301 */
Matt Carlson17375d22009-08-28 14:02:18 +00005302static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303{
Matt Carlson17375d22009-08-28 14:02:18 +00005304 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005305 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005306 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005307 struct netdev_queue *txq;
5308 int index = tnapi - tp->napi;
Tom Herbert298376d2011-11-28 16:33:30 +00005309 unsigned int pkts_compl = 0, bytes_compl = 0;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005310
Joe Perches63c3a662011-04-26 08:12:10 +00005311 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005312 index--;
5313
5314 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005315
5316 while (sw_idx != hw_idx) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00005317 struct tg3_tx_ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07005319 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320
Michael Chandf3e6542006-05-26 17:48:07 -07005321 if (unlikely(skb == NULL)) {
5322 tg3_tx_recover(tp);
5323 return;
5324 }
5325
Alexander Duyckf4188d82009-12-02 16:48:38 +00005326 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005327 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005328 skb_headlen(skb),
5329 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330
5331 ri->skb = NULL;
5332
Matt Carlsone01ee142011-07-27 14:20:50 +00005333 while (ri->fragmented) {
5334 ri->fragmented = false;
5335 sw_idx = NEXT_TX(sw_idx);
5336 ri = &tnapi->tx_buffers[sw_idx];
5337 }
5338
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339 sw_idx = NEXT_TX(sw_idx);
5340
5341 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005342 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07005343 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
5344 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005345
5346 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005347 dma_unmap_addr(ri, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00005348 skb_frag_size(&skb_shinfo(skb)->frags[i]),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005349 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00005350
5351 while (ri->fragmented) {
5352 ri->fragmented = false;
5353 sw_idx = NEXT_TX(sw_idx);
5354 ri = &tnapi->tx_buffers[sw_idx];
5355 }
5356
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357 sw_idx = NEXT_TX(sw_idx);
5358 }
5359
Tom Herbert298376d2011-11-28 16:33:30 +00005360 pkts_compl++;
5361 bytes_compl += skb->len;
5362
David S. Millerf47c11e2005-06-24 20:18:35 -07005363 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07005364
5365 if (unlikely(tx_bug)) {
5366 tg3_tx_recover(tp);
5367 return;
5368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369 }
5370
Tom Herbert298376d2011-11-28 16:33:30 +00005371 netdev_completed_queue(tp->dev, pkts_compl, bytes_compl);
5372
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005373 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374
Michael Chan1b2a7202006-08-07 21:46:02 -07005375 /* Need to make the tx_cons update visible to tg3_start_xmit()
5376 * before checking for netif_queue_stopped(). Without the
5377 * memory barrier, there is a small possibility that tg3_start_xmit()
5378 * will miss it and cause the queue to be stopped forever.
5379 */
5380 smp_mb();
5381
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005382 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005383 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005384 __netif_tx_lock(txq, smp_processor_id());
5385 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005386 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005387 netif_tx_wake_queue(txq);
5388 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07005389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390}
5391
Eric Dumazet9205fd92011-11-18 06:47:01 +00005392static void tg3_rx_data_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005393{
Eric Dumazet9205fd92011-11-18 06:47:01 +00005394 if (!ri->data)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005395 return;
5396
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005397 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005398 map_sz, PCI_DMA_FROMDEVICE);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005399 kfree(ri->data);
5400 ri->data = NULL;
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005401}
5402
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403/* Returns size of skb allocated or < 0 on error.
5404 *
5405 * We only need to fill in the address because the other members
5406 * of the RX descriptor are invariant, see tg3_init_rings.
5407 *
5408 * Note the purposeful assymetry of cpu vs. chip accesses. For
5409 * posting buffers we only dirty the first cache line of the RX
5410 * descriptor (containing the address). Whereas for the RX status
5411 * buffers the cpu only reads the last cacheline of the RX descriptor
5412 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
5413 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00005414static int tg3_alloc_rx_data(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Matt Carlsona3896162009-11-13 13:03:44 +00005415 u32 opaque_key, u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416{
5417 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00005418 struct ring_info *map;
Eric Dumazet9205fd92011-11-18 06:47:01 +00005419 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420 dma_addr_t mapping;
Eric Dumazet9205fd92011-11-18 06:47:01 +00005421 int skb_size, data_size, dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423 switch (opaque_key) {
5424 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00005425 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00005426 desc = &tpr->rx_std[dest_idx];
5427 map = &tpr->rx_std_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00005428 data_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429 break;
5430
5431 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00005432 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00005433 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00005434 map = &tpr->rx_jmb_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00005435 data_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 break;
5437
5438 default:
5439 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441
5442 /* Do not overwrite any of the map or rp information
5443 * until we are sure we can commit to a new buffer.
5444 *
5445 * Callers depend upon this behavior and assume that
5446 * we leave everything unchanged if we fail.
5447 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00005448 skb_size = SKB_DATA_ALIGN(data_size + TG3_RX_OFFSET(tp)) +
5449 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
5450 data = kmalloc(skb_size, GFP_ATOMIC);
5451 if (!data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452 return -ENOMEM;
5453
Eric Dumazet9205fd92011-11-18 06:47:01 +00005454 mapping = pci_map_single(tp->pdev,
5455 data + TG3_RX_OFFSET(tp),
5456 data_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457 PCI_DMA_FROMDEVICE);
Matt Carlsona21771d2009-11-02 14:25:31 +00005458 if (pci_dma_mapping_error(tp->pdev, mapping)) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00005459 kfree(data);
Matt Carlsona21771d2009-11-02 14:25:31 +00005460 return -EIO;
5461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462
Eric Dumazet9205fd92011-11-18 06:47:01 +00005463 map->data = data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005464 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466 desc->addr_hi = ((u64)mapping >> 32);
5467 desc->addr_lo = ((u64)mapping & 0xffffffff);
5468
Eric Dumazet9205fd92011-11-18 06:47:01 +00005469 return data_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470}
5471
5472/* We only need to move over in the address because the other
5473 * members of the RX descriptor are invariant. See notes above
Eric Dumazet9205fd92011-11-18 06:47:01 +00005474 * tg3_alloc_rx_data for full details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475 */
Matt Carlsona3896162009-11-13 13:03:44 +00005476static void tg3_recycle_rx(struct tg3_napi *tnapi,
5477 struct tg3_rx_prodring_set *dpr,
5478 u32 opaque_key, int src_idx,
5479 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480{
Matt Carlson17375d22009-08-28 14:02:18 +00005481 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
5483 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00005484 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005485 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486
5487 switch (opaque_key) {
5488 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00005489 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00005490 dest_desc = &dpr->rx_std[dest_idx];
5491 dest_map = &dpr->rx_std_buffers[dest_idx];
5492 src_desc = &spr->rx_std[src_idx];
5493 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494 break;
5495
5496 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00005497 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00005498 dest_desc = &dpr->rx_jmb[dest_idx].std;
5499 dest_map = &dpr->rx_jmb_buffers[dest_idx];
5500 src_desc = &spr->rx_jmb[src_idx].std;
5501 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 break;
5503
5504 default:
5505 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507
Eric Dumazet9205fd92011-11-18 06:47:01 +00005508 dest_map->data = src_map->data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005509 dma_unmap_addr_set(dest_map, mapping,
5510 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511 dest_desc->addr_hi = src_desc->addr_hi;
5512 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00005513
5514 /* Ensure that the update to the skb happens after the physical
5515 * addresses have been transferred to the new BD location.
5516 */
5517 smp_wmb();
5518
Eric Dumazet9205fd92011-11-18 06:47:01 +00005519 src_map->data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520}
5521
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522/* The RX ring scheme is composed of multiple rings which post fresh
5523 * buffers to the chip, and one special ring the chip uses to report
5524 * status back to the host.
5525 *
5526 * The special ring reports the status of received packets to the
5527 * host. The chip does not write into the original descriptor the
5528 * RX buffer was obtained from. The chip simply takes the original
5529 * descriptor as provided by the host, updates the status and length
5530 * field, then writes this into the next status ring entry.
5531 *
5532 * Each ring the host uses to post buffers to the chip is described
5533 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
5534 * it is first placed into the on-chip ram. When the packet's length
5535 * is known, it walks down the TG3_BDINFO entries to select the ring.
5536 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
5537 * which is within the range of the new packet's length is chosen.
5538 *
5539 * The "separate ring for rx status" scheme may sound queer, but it makes
5540 * sense from a cache coherency perspective. If only the host writes
5541 * to the buffer post rings, and only the chip writes to the rx status
5542 * rings, then cache lines never move beyond shared-modified state.
5543 * If both the host and chip were to write into the same ring, cache line
5544 * eviction could occur since both entities want it in an exclusive state.
5545 */
Matt Carlson17375d22009-08-28 14:02:18 +00005546static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547{
Matt Carlson17375d22009-08-28 14:02:18 +00005548 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07005549 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00005550 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00005551 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07005552 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00005554 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005556 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557 /*
5558 * We need to order the read of hw_idx and the read of
5559 * the opaque cookie.
5560 */
5561 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562 work_mask = 0;
5563 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00005564 std_prod_idx = tpr->rx_std_prod_idx;
5565 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00005567 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00005568 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569 unsigned int len;
5570 struct sk_buff *skb;
5571 dma_addr_t dma_addr;
5572 u32 opaque_key, desc_idx, *post_ptr;
Eric Dumazet9205fd92011-11-18 06:47:01 +00005573 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574
5575 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
5576 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
5577 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005578 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005579 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005580 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00005581 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07005582 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005584 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005585 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005586 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00005587 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00005588 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590
5591 work_mask |= opaque_key;
5592
5593 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
5594 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
5595 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00005596 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597 desc_idx, *post_ptr);
5598 drop_it_no_recycle:
5599 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00005600 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601 goto next_pkt;
5602 }
5603
Eric Dumazet9205fd92011-11-18 06:47:01 +00005604 prefetch(data + TG3_RX_OFFSET(tp));
Matt Carlsonad829262008-11-21 17:16:16 -08005605 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
5606 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607
Matt Carlsond2757fc2010-04-12 06:58:27 +00005608 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609 int skb_size;
5610
Eric Dumazet9205fd92011-11-18 06:47:01 +00005611 skb_size = tg3_alloc_rx_data(tp, tpr, opaque_key,
Matt Carlsonafc081f2009-11-13 13:03:43 +00005612 *post_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613 if (skb_size < 0)
5614 goto drop_it;
5615
Matt Carlson287be122009-08-28 13:58:46 +00005616 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617 PCI_DMA_FROMDEVICE);
5618
Eric Dumazet9205fd92011-11-18 06:47:01 +00005619 skb = build_skb(data);
5620 if (!skb) {
5621 kfree(data);
5622 goto drop_it_no_recycle;
5623 }
5624 skb_reserve(skb, TG3_RX_OFFSET(tp));
5625 /* Ensure that the update to the data happens
Matt Carlson61e800c2010-02-17 15:16:54 +00005626 * after the usage of the old DMA mapping.
5627 */
5628 smp_wmb();
5629
Eric Dumazet9205fd92011-11-18 06:47:01 +00005630 ri->data = NULL;
Matt Carlson61e800c2010-02-17 15:16:54 +00005631
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632 } else {
Matt Carlsona3896162009-11-13 13:03:44 +00005633 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634 desc_idx, *post_ptr);
5635
Eric Dumazet9205fd92011-11-18 06:47:01 +00005636 skb = netdev_alloc_skb(tp->dev,
5637 len + TG3_RAW_IP_ALIGN);
5638 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639 goto drop_it_no_recycle;
5640
Eric Dumazet9205fd92011-11-18 06:47:01 +00005641 skb_reserve(skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005643 memcpy(skb->data,
5644 data + TG3_RX_OFFSET(tp),
5645 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647 }
5648
Eric Dumazet9205fd92011-11-18 06:47:01 +00005649 skb_put(skb, len);
Michał Mirosławdc668912011-04-07 03:35:07 +00005650 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
5652 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
5653 >> RXD_TCPCSUM_SHIFT) == 0xffff))
5654 skb->ip_summed = CHECKSUM_UNNECESSARY;
5655 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005656 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657
5658 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00005659
5660 if (len > (tp->dev->mtu + ETH_HLEN) &&
5661 skb->protocol != htons(ETH_P_8021Q)) {
5662 dev_kfree_skb(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00005663 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00005664 }
5665
Matt Carlson9dc7a112010-04-12 06:58:28 +00005666 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00005667 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
5668 __vlan_hwaccel_put_tag(skb,
5669 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00005670
Matt Carlsonbf933c82011-01-25 15:58:49 +00005671 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673 received++;
5674 budget--;
5675
5676next_pkt:
5677 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07005678
5679 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005680 tpr->rx_std_prod_idx = std_prod_idx &
5681 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00005682 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5683 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07005684 work_mask &= ~RXD_OPAQUE_RING_STD;
5685 rx_std_posted = 0;
5686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07005688 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00005689 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07005690
5691 /* Refresh hw_idx to see if there is new work */
5692 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005693 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07005694 rmb();
5695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696 }
5697
5698 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00005699 tnapi->rx_rcb_ptr = sw_idx;
5700 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701
5702 /* Refill RX ring(s). */
Joe Perches63c3a662011-04-26 08:12:10 +00005703 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005704 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005705 tpr->rx_std_prod_idx = std_prod_idx &
5706 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005707 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5708 tpr->rx_std_prod_idx);
5709 }
5710 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005711 tpr->rx_jmb_prod_idx = jmb_prod_idx &
5712 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005713 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5714 tpr->rx_jmb_prod_idx);
5715 }
5716 mmiowb();
5717 } else if (work_mask) {
5718 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
5719 * updated before the producer indices can be updated.
5720 */
5721 smp_wmb();
5722
Matt Carlson2c49a442010-09-30 10:34:35 +00005723 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
5724 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005725
Matt Carlsone4af1af2010-02-12 14:47:05 +00005726 if (tnapi != &tp->napi[1])
5727 napi_schedule(&tp->napi[1].napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729
5730 return received;
5731}
5732
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005733static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735 /* handle link change and other phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00005736 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005737 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
5738
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739 if (sblk->status & SD_STATUS_LINK_CHG) {
5740 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005741 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07005742 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00005743 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsondd477002008-05-25 23:45:58 -07005744 tw32_f(MAC_STATUS,
5745 (MAC_STATUS_SYNC_CHANGED |
5746 MAC_STATUS_CFG_CHANGED |
5747 MAC_STATUS_MI_COMPLETION |
5748 MAC_STATUS_LNKSTATE_CHANGED));
5749 udelay(40);
5750 } else
5751 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07005752 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005753 }
5754 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005755}
5756
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005757static int tg3_rx_prodring_xfer(struct tg3 *tp,
5758 struct tg3_rx_prodring_set *dpr,
5759 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005760{
5761 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005762 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005763
5764 while (1) {
5765 src_prod_idx = spr->rx_std_prod_idx;
5766
5767 /* Make sure updates to the rx_std_buffers[] entries and the
5768 * standard producer index are seen in the correct order.
5769 */
5770 smp_rmb();
5771
5772 if (spr->rx_std_cons_idx == src_prod_idx)
5773 break;
5774
5775 if (spr->rx_std_cons_idx < src_prod_idx)
5776 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
5777 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005778 cpycnt = tp->rx_std_ring_mask + 1 -
5779 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005780
Matt Carlson2c49a442010-09-30 10:34:35 +00005781 cpycnt = min(cpycnt,
5782 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005783
5784 si = spr->rx_std_cons_idx;
5785 di = dpr->rx_std_prod_idx;
5786
Matt Carlsone92967b2010-02-12 14:47:06 +00005787 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00005788 if (dpr->rx_std_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00005789 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005790 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005791 break;
5792 }
5793 }
5794
5795 if (!cpycnt)
5796 break;
5797
5798 /* Ensure that updates to the rx_std_buffers ring and the
5799 * shadowed hardware producer ring from tg3_recycle_skb() are
5800 * ordered correctly WRT the skb check above.
5801 */
5802 smp_rmb();
5803
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005804 memcpy(&dpr->rx_std_buffers[di],
5805 &spr->rx_std_buffers[si],
5806 cpycnt * sizeof(struct ring_info));
5807
5808 for (i = 0; i < cpycnt; i++, di++, si++) {
5809 struct tg3_rx_buffer_desc *sbd, *dbd;
5810 sbd = &spr->rx_std[si];
5811 dbd = &dpr->rx_std[di];
5812 dbd->addr_hi = sbd->addr_hi;
5813 dbd->addr_lo = sbd->addr_lo;
5814 }
5815
Matt Carlson2c49a442010-09-30 10:34:35 +00005816 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
5817 tp->rx_std_ring_mask;
5818 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
5819 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005820 }
5821
5822 while (1) {
5823 src_prod_idx = spr->rx_jmb_prod_idx;
5824
5825 /* Make sure updates to the rx_jmb_buffers[] entries and
5826 * the jumbo producer index are seen in the correct order.
5827 */
5828 smp_rmb();
5829
5830 if (spr->rx_jmb_cons_idx == src_prod_idx)
5831 break;
5832
5833 if (spr->rx_jmb_cons_idx < src_prod_idx)
5834 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
5835 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005836 cpycnt = tp->rx_jmb_ring_mask + 1 -
5837 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005838
5839 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00005840 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005841
5842 si = spr->rx_jmb_cons_idx;
5843 di = dpr->rx_jmb_prod_idx;
5844
Matt Carlsone92967b2010-02-12 14:47:06 +00005845 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00005846 if (dpr->rx_jmb_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00005847 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005848 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005849 break;
5850 }
5851 }
5852
5853 if (!cpycnt)
5854 break;
5855
5856 /* Ensure that updates to the rx_jmb_buffers ring and the
5857 * shadowed hardware producer ring from tg3_recycle_skb() are
5858 * ordered correctly WRT the skb check above.
5859 */
5860 smp_rmb();
5861
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005862 memcpy(&dpr->rx_jmb_buffers[di],
5863 &spr->rx_jmb_buffers[si],
5864 cpycnt * sizeof(struct ring_info));
5865
5866 for (i = 0; i < cpycnt; i++, di++, si++) {
5867 struct tg3_rx_buffer_desc *sbd, *dbd;
5868 sbd = &spr->rx_jmb[si].std;
5869 dbd = &dpr->rx_jmb[di].std;
5870 dbd->addr_hi = sbd->addr_hi;
5871 dbd->addr_lo = sbd->addr_lo;
5872 }
5873
Matt Carlson2c49a442010-09-30 10:34:35 +00005874 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
5875 tp->rx_jmb_ring_mask;
5876 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
5877 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005878 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005879
5880 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005881}
5882
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005883static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
5884{
5885 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005886
5887 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005888 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00005889 tg3_tx(tnapi);
Joe Perches63c3a662011-04-26 08:12:10 +00005890 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Michael Chan4fd7ab52007-10-12 01:39:50 -07005891 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892 }
5893
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894 /* run RX thread, within the bounds set by NAPI.
5895 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005896 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07005897 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005898 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00005899 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900
Joe Perches63c3a662011-04-26 08:12:10 +00005901 if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005902 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005903 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00005904 u32 std_prod_idx = dpr->rx_std_prod_idx;
5905 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005906
Matt Carlsone4af1af2010-02-12 14:47:05 +00005907 for (i = 1; i < tp->irq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005908 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00005909 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005910
5911 wmb();
5912
Matt Carlsone4af1af2010-02-12 14:47:05 +00005913 if (std_prod_idx != dpr->rx_std_prod_idx)
5914 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5915 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005916
Matt Carlsone4af1af2010-02-12 14:47:05 +00005917 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
5918 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5919 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005920
5921 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005922
5923 if (err)
5924 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005925 }
5926
David S. Miller6f535762007-10-11 18:08:29 -07005927 return work_done;
5928}
David S. Millerf7383c22005-05-18 22:50:53 -07005929
Matt Carlsondb219972011-11-04 09:15:03 +00005930static inline void tg3_reset_task_schedule(struct tg3 *tp)
5931{
5932 if (!test_and_set_bit(TG3_FLAG_RESET_TASK_PENDING, tp->tg3_flags))
5933 schedule_work(&tp->reset_task);
5934}
5935
5936static inline void tg3_reset_task_cancel(struct tg3 *tp)
5937{
5938 cancel_work_sync(&tp->reset_task);
5939 tg3_flag_clear(tp, RESET_TASK_PENDING);
5940}
5941
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005942static int tg3_poll_msix(struct napi_struct *napi, int budget)
5943{
5944 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5945 struct tg3 *tp = tnapi->tp;
5946 int work_done = 0;
5947 struct tg3_hw_status *sblk = tnapi->hw_status;
5948
5949 while (1) {
5950 work_done = tg3_poll_work(tnapi, work_done, budget);
5951
Joe Perches63c3a662011-04-26 08:12:10 +00005952 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005953 goto tx_recovery;
5954
5955 if (unlikely(work_done >= budget))
5956 break;
5957
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005958 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005959 * to tell the hw how much work has been processed,
5960 * so we must read it before checking for more work.
5961 */
5962 tnapi->last_tag = sblk->status_tag;
5963 tnapi->last_irq_tag = tnapi->last_tag;
5964 rmb();
5965
5966 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00005967 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
5968 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005969 napi_complete(napi);
5970 /* Reenable interrupts. */
5971 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
5972 mmiowb();
5973 break;
5974 }
5975 }
5976
5977 return work_done;
5978
5979tx_recovery:
5980 /* work_done is guaranteed to be less than budget. */
5981 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00005982 tg3_reset_task_schedule(tp);
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005983 return work_done;
5984}
5985
Matt Carlsone64de4e2011-04-13 11:05:05 +00005986static void tg3_process_error(struct tg3 *tp)
5987{
5988 u32 val;
5989 bool real_error = false;
5990
Joe Perches63c3a662011-04-26 08:12:10 +00005991 if (tg3_flag(tp, ERROR_PROCESSED))
Matt Carlsone64de4e2011-04-13 11:05:05 +00005992 return;
5993
5994 /* Check Flow Attention register */
5995 val = tr32(HOSTCC_FLOW_ATTN);
5996 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
5997 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
5998 real_error = true;
5999 }
6000
6001 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
6002 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
6003 real_error = true;
6004 }
6005
6006 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
6007 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
6008 real_error = true;
6009 }
6010
6011 if (!real_error)
6012 return;
6013
6014 tg3_dump_state(tp);
6015
Joe Perches63c3a662011-04-26 08:12:10 +00006016 tg3_flag_set(tp, ERROR_PROCESSED);
Matt Carlsondb219972011-11-04 09:15:03 +00006017 tg3_reset_task_schedule(tp);
Matt Carlsone64de4e2011-04-13 11:05:05 +00006018}
6019
David S. Miller6f535762007-10-11 18:08:29 -07006020static int tg3_poll(struct napi_struct *napi, int budget)
6021{
Matt Carlson8ef04422009-08-28 14:01:37 +00006022 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
6023 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07006024 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00006025 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07006026
6027 while (1) {
Matt Carlsone64de4e2011-04-13 11:05:05 +00006028 if (sblk->status & SD_STATUS_ERROR)
6029 tg3_process_error(tp);
6030
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006031 tg3_poll_link(tp);
6032
Matt Carlson17375d22009-08-28 14:02:18 +00006033 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07006034
Joe Perches63c3a662011-04-26 08:12:10 +00006035 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
David S. Miller6f535762007-10-11 18:08:29 -07006036 goto tx_recovery;
6037
6038 if (unlikely(work_done >= budget))
6039 break;
6040
Joe Perches63c3a662011-04-26 08:12:10 +00006041 if (tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson17375d22009-08-28 14:02:18 +00006042 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07006043 * to tell the hw how much work has been processed,
6044 * so we must read it before checking for more work.
6045 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006046 tnapi->last_tag = sblk->status_tag;
6047 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07006048 rmb();
6049 } else
6050 sblk->status &= ~SD_STATUS_UPDATED;
6051
Matt Carlson17375d22009-08-28 14:02:18 +00006052 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08006053 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00006054 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07006055 break;
6056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006057 }
6058
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006059 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07006060
6061tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07006062 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08006063 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00006064 tg3_reset_task_schedule(tp);
Michael Chan4fd7ab52007-10-12 01:39:50 -07006065 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006066}
6067
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006068static void tg3_napi_disable(struct tg3 *tp)
6069{
6070 int i;
6071
6072 for (i = tp->irq_cnt - 1; i >= 0; i--)
6073 napi_disable(&tp->napi[i].napi);
6074}
6075
6076static void tg3_napi_enable(struct tg3 *tp)
6077{
6078 int i;
6079
6080 for (i = 0; i < tp->irq_cnt; i++)
6081 napi_enable(&tp->napi[i].napi);
6082}
6083
6084static void tg3_napi_init(struct tg3 *tp)
6085{
6086 int i;
6087
6088 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
6089 for (i = 1; i < tp->irq_cnt; i++)
6090 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
6091}
6092
6093static void tg3_napi_fini(struct tg3 *tp)
6094{
6095 int i;
6096
6097 for (i = 0; i < tp->irq_cnt; i++)
6098 netif_napi_del(&tp->napi[i].napi);
6099}
6100
6101static inline void tg3_netif_stop(struct tg3 *tp)
6102{
6103 tp->dev->trans_start = jiffies; /* prevent tx timeout */
6104 tg3_napi_disable(tp);
6105 netif_tx_disable(tp->dev);
6106}
6107
6108static inline void tg3_netif_start(struct tg3 *tp)
6109{
6110 /* NOTE: unconditional netif_tx_wake_all_queues is only
6111 * appropriate so long as all callers are assured to
6112 * have free tx slots (such as after tg3_init_hw)
6113 */
6114 netif_tx_wake_all_queues(tp->dev);
6115
6116 tg3_napi_enable(tp);
6117 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
6118 tg3_enable_ints(tp);
6119}
6120
David S. Millerf47c11e2005-06-24 20:18:35 -07006121static void tg3_irq_quiesce(struct tg3 *tp)
6122{
Matt Carlson4f125f42009-09-01 12:55:02 +00006123 int i;
6124
David S. Millerf47c11e2005-06-24 20:18:35 -07006125 BUG_ON(tp->irq_sync);
6126
6127 tp->irq_sync = 1;
6128 smp_mb();
6129
Matt Carlson4f125f42009-09-01 12:55:02 +00006130 for (i = 0; i < tp->irq_cnt; i++)
6131 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07006132}
6133
David S. Millerf47c11e2005-06-24 20:18:35 -07006134/* Fully shutdown all tg3 driver activity elsewhere in the system.
6135 * If irq_sync is non-zero, then the IRQ handler must be synchronized
6136 * with as well. Most of the time, this is not necessary except when
6137 * shutting down the device.
6138 */
6139static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
6140{
Michael Chan46966542007-07-11 19:47:19 -07006141 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07006142 if (irq_sync)
6143 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07006144}
6145
6146static inline void tg3_full_unlock(struct tg3 *tp)
6147{
David S. Millerf47c11e2005-06-24 20:18:35 -07006148 spin_unlock_bh(&tp->lock);
6149}
6150
Michael Chanfcfa0a32006-03-20 22:28:41 -08006151/* One-shot MSI handler - Chip automatically disables interrupt
6152 * after sending MSI so driver doesn't have to do it.
6153 */
David Howells7d12e782006-10-05 14:55:46 +01006154static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08006155{
Matt Carlson09943a12009-08-28 14:01:57 +00006156 struct tg3_napi *tnapi = dev_id;
6157 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08006158
Matt Carlson898a56f2009-08-28 14:02:40 +00006159 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006160 if (tnapi->rx_rcb)
6161 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08006162
6163 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00006164 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08006165
6166 return IRQ_HANDLED;
6167}
6168
Michael Chan88b06bc2005-04-21 17:13:25 -07006169/* MSI ISR - No need to check for interrupt sharing and no need to
6170 * flush status block and interrupt mailbox. PCI ordering rules
6171 * guarantee that MSI will arrive after the status block.
6172 */
David Howells7d12e782006-10-05 14:55:46 +01006173static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc2005-04-21 17:13:25 -07006174{
Matt Carlson09943a12009-08-28 14:01:57 +00006175 struct tg3_napi *tnapi = dev_id;
6176 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc2005-04-21 17:13:25 -07006177
Matt Carlson898a56f2009-08-28 14:02:40 +00006178 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006179 if (tnapi->rx_rcb)
6180 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc2005-04-21 17:13:25 -07006181 /*
David S. Millerfac9b832005-05-18 22:46:34 -07006182 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc2005-04-21 17:13:25 -07006183 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07006184 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc2005-04-21 17:13:25 -07006185 * NIC to stop sending us irqs, engaging "in-intr-handler"
6186 * event coalescing.
6187 */
Matt Carlson5b39de92011-08-31 11:44:50 +00006188 tw32_mailbox(tnapi->int_mbox, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07006189 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00006190 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07006191
Michael Chan88b06bc2005-04-21 17:13:25 -07006192 return IRQ_RETVAL(1);
6193}
6194
David Howells7d12e782006-10-05 14:55:46 +01006195static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006196{
Matt Carlson09943a12009-08-28 14:01:57 +00006197 struct tg3_napi *tnapi = dev_id;
6198 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006199 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006200 unsigned int handled = 1;
6201
Linus Torvalds1da177e2005-04-16 15:20:36 -07006202 /* In INTx mode, it is possible for the interrupt to arrive at
6203 * the CPU before the status block posted prior to the interrupt.
6204 * Reading the PCI State register will confirm whether the
6205 * interrupt is ours and will flush the status block.
6206 */
Michael Chand18edcb2007-03-24 20:57:11 -07006207 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
Joe Perches63c3a662011-04-26 08:12:10 +00006208 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07006209 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
6210 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07006211 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07006212 }
Michael Chand18edcb2007-03-24 20:57:11 -07006213 }
6214
6215 /*
6216 * Writing any value to intr-mbox-0 clears PCI INTA# and
6217 * chip-internal interrupt pending events.
6218 * Writing non-zero to intr-mbox-0 additional tells the
6219 * NIC to stop sending us irqs, engaging "in-intr-handler"
6220 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07006221 *
6222 * Flush the mailbox to de-assert the IRQ immediately to prevent
6223 * spurious interrupts. The flush impacts performance but
6224 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07006225 */
Michael Chanc04cb342007-05-07 00:26:15 -07006226 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07006227 if (tg3_irq_sync(tp))
6228 goto out;
6229 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00006230 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00006231 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00006232 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07006233 } else {
6234 /* No work, shared interrupt perhaps? re-enable
6235 * interrupts, and flush that PCI write
6236 */
6237 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
6238 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07006239 }
David S. Millerf47c11e2005-06-24 20:18:35 -07006240out:
David S. Millerfac9b832005-05-18 22:46:34 -07006241 return IRQ_RETVAL(handled);
6242}
6243
David Howells7d12e782006-10-05 14:55:46 +01006244static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07006245{
Matt Carlson09943a12009-08-28 14:01:57 +00006246 struct tg3_napi *tnapi = dev_id;
6247 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006248 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07006249 unsigned int handled = 1;
6250
David S. Millerfac9b832005-05-18 22:46:34 -07006251 /* In INTx mode, it is possible for the interrupt to arrive at
6252 * the CPU before the status block posted prior to the interrupt.
6253 * Reading the PCI State register will confirm whether the
6254 * interrupt is ours and will flush the status block.
6255 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006256 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Joe Perches63c3a662011-04-26 08:12:10 +00006257 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07006258 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
6259 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07006260 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006261 }
Michael Chand18edcb2007-03-24 20:57:11 -07006262 }
6263
6264 /*
6265 * writing any value to intr-mbox-0 clears PCI INTA# and
6266 * chip-internal interrupt pending events.
6267 * writing non-zero to intr-mbox-0 additional tells the
6268 * NIC to stop sending us irqs, engaging "in-intr-handler"
6269 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07006270 *
6271 * Flush the mailbox to de-assert the IRQ immediately to prevent
6272 * spurious interrupts. The flush impacts performance but
6273 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07006274 */
Michael Chanc04cb342007-05-07 00:26:15 -07006275 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00006276
6277 /*
6278 * In a shared interrupt configuration, sometimes other devices'
6279 * interrupts will scream. We record the current status tag here
6280 * so that the above check can report that the screaming interrupts
6281 * are unhandled. Eventually they will be silenced.
6282 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006283 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00006284
Michael Chand18edcb2007-03-24 20:57:11 -07006285 if (tg3_irq_sync(tp))
6286 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00006287
Matt Carlson72334482009-08-28 14:03:01 +00006288 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00006289
Matt Carlson09943a12009-08-28 14:01:57 +00006290 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00006291
David S. Millerf47c11e2005-06-24 20:18:35 -07006292out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006293 return IRQ_RETVAL(handled);
6294}
6295
Michael Chan79381092005-04-21 17:13:59 -07006296/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01006297static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07006298{
Matt Carlson09943a12009-08-28 14:01:57 +00006299 struct tg3_napi *tnapi = dev_id;
6300 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006301 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07006302
Michael Chanf9804dd2005-09-27 12:13:10 -07006303 if ((sblk->status & SD_STATUS_UPDATED) ||
6304 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07006305 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07006306 return IRQ_RETVAL(1);
6307 }
6308 return IRQ_RETVAL(0);
6309}
6310
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07006311static int tg3_init_hw(struct tg3 *, int);
Michael Chan944d9802005-05-29 14:57:48 -07006312static int tg3_halt(struct tg3 *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006313
Michael Chanb9ec6c12006-07-25 16:37:27 -07006314/* Restart hardware after configuration changes, self-test, etc.
6315 * Invoked with tp->lock held.
6316 */
6317static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
Eric Dumazet78c61462008-04-24 23:33:06 -07006318 __releases(tp->lock)
6319 __acquires(tp->lock)
Michael Chanb9ec6c12006-07-25 16:37:27 -07006320{
6321 int err;
6322
6323 err = tg3_init_hw(tp, reset_phy);
6324 if (err) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006325 netdev_err(tp->dev,
6326 "Failed to re-initialize device, aborting\n");
Michael Chanb9ec6c12006-07-25 16:37:27 -07006327 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
6328 tg3_full_unlock(tp);
6329 del_timer_sync(&tp->timer);
6330 tp->irq_sync = 0;
Matt Carlsonfed97812009-09-01 13:10:19 +00006331 tg3_napi_enable(tp);
Michael Chanb9ec6c12006-07-25 16:37:27 -07006332 dev_close(tp->dev);
6333 tg3_full_lock(tp, 0);
6334 }
6335 return err;
6336}
6337
Linus Torvalds1da177e2005-04-16 15:20:36 -07006338#ifdef CONFIG_NET_POLL_CONTROLLER
6339static void tg3_poll_controller(struct net_device *dev)
6340{
Matt Carlson4f125f42009-09-01 12:55:02 +00006341 int i;
Michael Chan88b06bc2005-04-21 17:13:25 -07006342 struct tg3 *tp = netdev_priv(dev);
6343
Matt Carlson4f125f42009-09-01 12:55:02 +00006344 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00006345 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006346}
6347#endif
6348
David Howellsc4028952006-11-22 14:57:56 +00006349static void tg3_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006350{
David Howellsc4028952006-11-22 14:57:56 +00006351 struct tg3 *tp = container_of(work, struct tg3, reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006352 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006353
Michael Chan7faa0062006-02-02 17:29:28 -08006354 tg3_full_lock(tp, 0);
Michael Chan7faa0062006-02-02 17:29:28 -08006355
6356 if (!netif_running(tp->dev)) {
Matt Carlsondb219972011-11-04 09:15:03 +00006357 tg3_flag_clear(tp, RESET_TASK_PENDING);
Michael Chan7faa0062006-02-02 17:29:28 -08006358 tg3_full_unlock(tp);
6359 return;
6360 }
6361
6362 tg3_full_unlock(tp);
6363
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006364 tg3_phy_stop(tp);
6365
Linus Torvalds1da177e2005-04-16 15:20:36 -07006366 tg3_netif_stop(tp);
6367
David S. Millerf47c11e2005-06-24 20:18:35 -07006368 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006369
Joe Perches63c3a662011-04-26 08:12:10 +00006370 if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
Michael Chandf3e6542006-05-26 17:48:07 -07006371 tp->write32_tx_mbox = tg3_write32_tx_mbox;
6372 tp->write32_rx_mbox = tg3_write_flush_reg32;
Joe Perches63c3a662011-04-26 08:12:10 +00006373 tg3_flag_set(tp, MBOX_WRITE_REORDER);
6374 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07006375 }
6376
Michael Chan944d9802005-05-29 14:57:48 -07006377 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006378 err = tg3_init_hw(tp, 1);
6379 if (err)
Michael Chanb9ec6c12006-07-25 16:37:27 -07006380 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006381
6382 tg3_netif_start(tp);
6383
Michael Chanb9ec6c12006-07-25 16:37:27 -07006384out:
Michael Chan7faa0062006-02-02 17:29:28 -08006385 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006386
6387 if (!err)
6388 tg3_phy_start(tp);
Matt Carlsondb219972011-11-04 09:15:03 +00006389
6390 tg3_flag_clear(tp, RESET_TASK_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006391}
6392
6393static void tg3_tx_timeout(struct net_device *dev)
6394{
6395 struct tg3 *tp = netdev_priv(dev);
6396
Michael Chanb0408752007-02-13 12:18:30 -08006397 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00006398 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00006399 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08006400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006401
Matt Carlsondb219972011-11-04 09:15:03 +00006402 tg3_reset_task_schedule(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006403}
6404
Michael Chanc58ec932005-09-17 00:46:27 -07006405/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
6406static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
6407{
6408 u32 base = (u32) mapping & 0xffffffff;
6409
Eric Dumazet807540b2010-09-23 05:40:09 +00006410 return (base > 0xffffdcc0) && (base + len + 8 < base);
Michael Chanc58ec932005-09-17 00:46:27 -07006411}
6412
Michael Chan72f2afb2006-03-06 19:28:35 -08006413/* Test for DMA addresses > 40-bit */
6414static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
6415 int len)
6416{
6417#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Joe Perches63c3a662011-04-26 08:12:10 +00006418 if (tg3_flag(tp, 40BIT_DMA_BUG))
Eric Dumazet807540b2010-09-23 05:40:09 +00006419 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08006420 return 0;
6421#else
6422 return 0;
6423#endif
6424}
6425
Matt Carlsond1a3b732011-07-27 14:20:51 +00006426static inline void tg3_tx_set_bd(struct tg3_tx_buffer_desc *txbd,
Matt Carlson92cd3a12011-07-27 14:20:47 +00006427 dma_addr_t mapping, u32 len, u32 flags,
6428 u32 mss, u32 vlan)
Matt Carlson2ffcc982011-05-19 12:12:44 +00006429{
Matt Carlson92cd3a12011-07-27 14:20:47 +00006430 txbd->addr_hi = ((u64) mapping >> 32);
6431 txbd->addr_lo = ((u64) mapping & 0xffffffff);
6432 txbd->len_flags = (len << TXD_LEN_SHIFT) | (flags & 0x0000ffff);
6433 txbd->vlan_tag = (mss << TXD_MSS_SHIFT) | (vlan << TXD_VLAN_TAG_SHIFT);
Matt Carlson2ffcc982011-05-19 12:12:44 +00006434}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006435
Matt Carlson84b67b22011-07-27 14:20:52 +00006436static bool tg3_tx_frag_set(struct tg3_napi *tnapi, u32 *entry, u32 *budget,
Matt Carlsond1a3b732011-07-27 14:20:51 +00006437 dma_addr_t map, u32 len, u32 flags,
6438 u32 mss, u32 vlan)
6439{
6440 struct tg3 *tp = tnapi->tp;
6441 bool hwbug = false;
6442
6443 if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
6444 hwbug = 1;
6445
6446 if (tg3_4g_overflow_test(map, len))
6447 hwbug = 1;
6448
6449 if (tg3_40bit_overflow_test(tp, map, len))
6450 hwbug = 1;
6451
Matt Carlsona4cb4282011-12-14 11:09:58 +00006452 if (tp->dma_limit) {
Matt Carlsonb9e45482011-11-04 09:14:59 +00006453 u32 prvidx = *entry;
Matt Carlsone31aa982011-07-27 14:20:53 +00006454 u32 tmp_flag = flags & ~TXD_FLAG_END;
Matt Carlsona4cb4282011-12-14 11:09:58 +00006455 while (len > tp->dma_limit && *budget) {
6456 u32 frag_len = tp->dma_limit;
6457 len -= tp->dma_limit;
Matt Carlsone31aa982011-07-27 14:20:53 +00006458
Matt Carlsonb9e45482011-11-04 09:14:59 +00006459 /* Avoid the 8byte DMA problem */
6460 if (len <= 8) {
Matt Carlsona4cb4282011-12-14 11:09:58 +00006461 len += tp->dma_limit / 2;
6462 frag_len = tp->dma_limit / 2;
Matt Carlsone31aa982011-07-27 14:20:53 +00006463 }
6464
Matt Carlsonb9e45482011-11-04 09:14:59 +00006465 tnapi->tx_buffers[*entry].fragmented = true;
6466
6467 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6468 frag_len, tmp_flag, mss, vlan);
6469 *budget -= 1;
6470 prvidx = *entry;
6471 *entry = NEXT_TX(*entry);
6472
Matt Carlsone31aa982011-07-27 14:20:53 +00006473 map += frag_len;
6474 }
6475
6476 if (len) {
6477 if (*budget) {
6478 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6479 len, flags, mss, vlan);
Matt Carlsonb9e45482011-11-04 09:14:59 +00006480 *budget -= 1;
Matt Carlsone31aa982011-07-27 14:20:53 +00006481 *entry = NEXT_TX(*entry);
6482 } else {
6483 hwbug = 1;
Matt Carlsonb9e45482011-11-04 09:14:59 +00006484 tnapi->tx_buffers[prvidx].fragmented = false;
Matt Carlsone31aa982011-07-27 14:20:53 +00006485 }
6486 }
6487 } else {
Matt Carlson84b67b22011-07-27 14:20:52 +00006488 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6489 len, flags, mss, vlan);
Matt Carlsone31aa982011-07-27 14:20:53 +00006490 *entry = NEXT_TX(*entry);
6491 }
Matt Carlsond1a3b732011-07-27 14:20:51 +00006492
6493 return hwbug;
6494}
6495
Matt Carlson0d681b22011-07-27 14:20:49 +00006496static void tg3_tx_skb_unmap(struct tg3_napi *tnapi, u32 entry, int last)
Matt Carlson432aa7e2011-05-19 12:12:45 +00006497{
6498 int i;
Matt Carlson0d681b22011-07-27 14:20:49 +00006499 struct sk_buff *skb;
Matt Carlsondf8944c2011-07-27 14:20:46 +00006500 struct tg3_tx_ring_info *txb = &tnapi->tx_buffers[entry];
Matt Carlson432aa7e2011-05-19 12:12:45 +00006501
Matt Carlson0d681b22011-07-27 14:20:49 +00006502 skb = txb->skb;
6503 txb->skb = NULL;
6504
Matt Carlson432aa7e2011-05-19 12:12:45 +00006505 pci_unmap_single(tnapi->tp->pdev,
6506 dma_unmap_addr(txb, mapping),
6507 skb_headlen(skb),
6508 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 }
6515
Matt Carlsonba1142e2011-11-04 09:15:00 +00006516 for (i = 0; i <= last; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00006517 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Matt Carlson432aa7e2011-05-19 12:12:45 +00006518
6519 entry = NEXT_TX(entry);
6520 txb = &tnapi->tx_buffers[entry];
6521
6522 pci_unmap_page(tnapi->tp->pdev,
6523 dma_unmap_addr(txb, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00006524 skb_frag_size(frag), PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00006525
6526 while (txb->fragmented) {
6527 txb->fragmented = false;
6528 entry = NEXT_TX(entry);
6529 txb = &tnapi->tx_buffers[entry];
6530 }
Matt Carlson432aa7e2011-05-19 12:12:45 +00006531 }
6532}
6533
Michael Chan72f2afb2006-03-06 19:28:35 -08006534/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006535static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
David S. Miller1805b2f2011-10-24 18:18:09 -04006536 struct sk_buff **pskb,
Matt Carlson84b67b22011-07-27 14:20:52 +00006537 u32 *entry, u32 *budget,
Matt Carlson92cd3a12011-07-27 14:20:47 +00006538 u32 base_flags, u32 mss, u32 vlan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006539{
Matt Carlson24f4efd2009-11-13 13:03:35 +00006540 struct tg3 *tp = tnapi->tp;
David S. Miller1805b2f2011-10-24 18:18:09 -04006541 struct sk_buff *new_skb, *skb = *pskb;
Michael Chanc58ec932005-09-17 00:46:27 -07006542 dma_addr_t new_addr = 0;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006543 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006544
Matt Carlson41588ba2008-04-19 18:12:33 -07006545 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
6546 new_skb = skb_copy(skb, GFP_ATOMIC);
6547 else {
6548 int more_headroom = 4 - ((unsigned long)skb->data & 3);
6549
6550 new_skb = skb_copy_expand(skb,
6551 skb_headroom(skb) + more_headroom,
6552 skb_tailroom(skb), GFP_ATOMIC);
6553 }
6554
Linus Torvalds1da177e2005-04-16 15:20:36 -07006555 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07006556 ret = -1;
6557 } else {
6558 /* New SKB is guaranteed to be linear. */
Alexander Duyckf4188d82009-12-02 16:48:38 +00006559 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
6560 PCI_DMA_TODEVICE);
6561 /* Make sure the mapping succeeded */
6562 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00006563 dev_kfree_skb(new_skb);
Michael Chanc58ec932005-09-17 00:46:27 -07006564 ret = -1;
Michael Chanc58ec932005-09-17 00:46:27 -07006565 } else {
Matt Carlsonb9e45482011-11-04 09:14:59 +00006566 u32 save_entry = *entry;
6567
Matt Carlson92cd3a12011-07-27 14:20:47 +00006568 base_flags |= TXD_FLAG_END;
6569
Matt Carlson84b67b22011-07-27 14:20:52 +00006570 tnapi->tx_buffers[*entry].skb = new_skb;
6571 dma_unmap_addr_set(&tnapi->tx_buffers[*entry],
Matt Carlson432aa7e2011-05-19 12:12:45 +00006572 mapping, new_addr);
6573
Matt Carlson84b67b22011-07-27 14:20:52 +00006574 if (tg3_tx_frag_set(tnapi, entry, budget, new_addr,
Matt Carlsond1a3b732011-07-27 14:20:51 +00006575 new_skb->len, base_flags,
6576 mss, vlan)) {
Matt Carlsonba1142e2011-11-04 09:15:00 +00006577 tg3_tx_skb_unmap(tnapi, save_entry, -1);
Matt Carlsond1a3b732011-07-27 14:20:51 +00006578 dev_kfree_skb(new_skb);
6579 ret = -1;
6580 }
Michael Chanc58ec932005-09-17 00:46:27 -07006581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006582 }
6583
Linus Torvalds1da177e2005-04-16 15:20:36 -07006584 dev_kfree_skb(skb);
David S. Miller1805b2f2011-10-24 18:18:09 -04006585 *pskb = new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07006586 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006587}
6588
Matt Carlson2ffcc982011-05-19 12:12:44 +00006589static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07006590
6591/* Use GSO to workaround a rare TSO bug that may be triggered when the
6592 * TSO header is greater than 80 bytes.
6593 */
6594static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
6595{
6596 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006597 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07006598
6599 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006600 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07006601 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006602
6603 /* netif_tx_stop_queue() must be done before checking
6604 * checking tx index in tg3_tx_avail() below, because in
6605 * tg3_tx(), we update tx index before checking for
6606 * netif_tx_queue_stopped().
6607 */
6608 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006609 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08006610 return NETDEV_TX_BUSY;
6611
6612 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07006613 }
6614
6615 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07006616 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07006617 goto tg3_tso_bug_end;
6618
6619 do {
6620 nskb = segs;
6621 segs = segs->next;
6622 nskb->next = NULL;
Matt Carlson2ffcc982011-05-19 12:12:44 +00006623 tg3_start_xmit(nskb, tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07006624 } while (segs);
6625
6626tg3_tso_bug_end:
6627 dev_kfree_skb(skb);
6628
6629 return NETDEV_TX_OK;
6630}
Michael Chan52c0fd82006-06-29 20:15:54 -07006631
Michael Chan5a6f3072006-03-20 22:28:05 -08006632/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
Joe Perches63c3a662011-04-26 08:12:10 +00006633 * support TG3_FLAG_HW_TSO_1 or firmware TSO only.
Michael Chan5a6f3072006-03-20 22:28:05 -08006634 */
Matt Carlson2ffcc982011-05-19 12:12:44 +00006635static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08006636{
6637 struct tg3 *tp = netdev_priv(dev);
Matt Carlson92cd3a12011-07-27 14:20:47 +00006638 u32 len, entry, base_flags, mss, vlan = 0;
Matt Carlson84b67b22011-07-27 14:20:52 +00006639 u32 budget;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006640 int i = -1, would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07006641 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00006642 struct tg3_napi *tnapi;
6643 struct netdev_queue *txq;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006644 unsigned int last;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006645
Matt Carlson24f4efd2009-11-13 13:03:35 +00006646 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
6647 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Joe Perches63c3a662011-04-26 08:12:10 +00006648 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006649 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006650
Matt Carlson84b67b22011-07-27 14:20:52 +00006651 budget = tg3_tx_avail(tnapi);
6652
Michael Chan00b70502006-06-17 21:58:45 -07006653 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006654 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07006655 * interrupt. Furthermore, IRQ processing runs lockless so we have
6656 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07006657 */
Matt Carlson84b67b22011-07-27 14:20:52 +00006658 if (unlikely(budget <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006659 if (!netif_tx_queue_stopped(txq)) {
6660 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08006661
6662 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00006663 netdev_err(dev,
6664 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08006665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006666 return NETDEV_TX_BUSY;
6667 }
6668
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006669 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006670 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07006671 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006672 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00006673
Matt Carlsonbe98da62010-07-11 09:31:46 +00006674 mss = skb_shinfo(skb)->gso_size;
6675 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006676 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00006677 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006678
6679 if (skb_header_cloned(skb) &&
Eric Dumazet48855432011-10-24 07:53:03 +00006680 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
6681 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006682
Matt Carlson34195c32010-07-11 09:31:42 +00006683 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07006684 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006685
Matt Carlson02e96082010-09-15 08:59:59 +00006686 if (skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00006687 hdr_len = skb_headlen(skb) - ETH_HLEN;
6688 } else {
6689 u32 ip_tcp_len;
6690
6691 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
6692 hdr_len = ip_tcp_len + tcp_opt_len;
6693
6694 iph->check = 0;
6695 iph->tot_len = htons(mss + hdr_len);
6696 }
6697
Michael Chan52c0fd82006-06-29 20:15:54 -07006698 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Joe Perches63c3a662011-04-26 08:12:10 +00006699 tg3_flag(tp, TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00006700 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07006701
Linus Torvalds1da177e2005-04-16 15:20:36 -07006702 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
6703 TXD_FLAG_CPU_POST_DMA);
6704
Joe Perches63c3a662011-04-26 08:12:10 +00006705 if (tg3_flag(tp, HW_TSO_1) ||
6706 tg3_flag(tp, HW_TSO_2) ||
6707 tg3_flag(tp, HW_TSO_3)) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006708 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006709 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006710 } else
6711 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
6712 iph->daddr, 0,
6713 IPPROTO_TCP,
6714 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006715
Joe Perches63c3a662011-04-26 08:12:10 +00006716 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlson615774f2009-11-13 13:03:39 +00006717 mss |= (hdr_len & 0xc) << 12;
6718 if (hdr_len & 0x10)
6719 base_flags |= 0x00000010;
6720 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +00006721 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006722 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +00006723 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006724 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006725 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006726 int tsflags;
6727
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006728 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006729 mss |= (tsflags << 11);
6730 }
6731 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006732 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006733 int tsflags;
6734
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006735 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006736 base_flags |= tsflags << 12;
6737 }
6738 }
6739 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00006740
Matt Carlson93a700a2011-08-31 11:44:54 +00006741 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
6742 !mss && skb->len > VLAN_ETH_FRAME_LEN)
6743 base_flags |= TXD_FLAG_JMB_PKT;
6744
Matt Carlson92cd3a12011-07-27 14:20:47 +00006745 if (vlan_tx_tag_present(skb)) {
6746 base_flags |= TXD_FLAG_VLAN;
6747 vlan = vlan_tx_tag_get(skb);
6748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006749
Alexander Duyckf4188d82009-12-02 16:48:38 +00006750 len = skb_headlen(skb);
6751
6752 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
Eric Dumazet48855432011-10-24 07:53:03 +00006753 if (pci_dma_mapping_error(tp->pdev, mapping))
6754 goto drop;
6755
David S. Miller90079ce2008-09-11 04:52:51 -07006756
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006757 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006758 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006759
6760 would_hit_hwbug = 0;
6761
Joe Perches63c3a662011-04-26 08:12:10 +00006762 if (tg3_flag(tp, 5701_DMA_BUG))
Michael Chanc58ec932005-09-17 00:46:27 -07006763 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006764
Matt Carlson84b67b22011-07-27 14:20:52 +00006765 if (tg3_tx_frag_set(tnapi, &entry, &budget, mapping, len, base_flags |
Matt Carlsond1a3b732011-07-27 14:20:51 +00006766 ((skb_shinfo(skb)->nr_frags == 0) ? TXD_FLAG_END : 0),
Matt Carlsonba1142e2011-11-04 09:15:00 +00006767 mss, vlan)) {
Matt Carlsond1a3b732011-07-27 14:20:51 +00006768 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006769 /* Now loop through additional data fragments, and queue them. */
Matt Carlsonba1142e2011-11-04 09:15:00 +00006770 } else if (skb_shinfo(skb)->nr_frags > 0) {
Matt Carlson92cd3a12011-07-27 14:20:47 +00006771 u32 tmp_mss = mss;
6772
6773 if (!tg3_flag(tp, HW_TSO_1) &&
6774 !tg3_flag(tp, HW_TSO_2) &&
6775 !tg3_flag(tp, HW_TSO_3))
6776 tmp_mss = 0;
6777
Linus Torvalds1da177e2005-04-16 15:20:36 -07006778 last = skb_shinfo(skb)->nr_frags - 1;
6779 for (i = 0; i <= last; i++) {
6780 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6781
Eric Dumazet9e903e02011-10-18 21:00:24 +00006782 len = skb_frag_size(frag);
Ian Campbelldc234d02011-08-24 22:28:11 +00006783 mapping = skb_frag_dma_map(&tp->pdev->dev, frag, 0,
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01006784 len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006785
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006786 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006787 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00006788 mapping);
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01006789 if (dma_mapping_error(&tp->pdev->dev, mapping))
Alexander Duyckf4188d82009-12-02 16:48:38 +00006790 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006791
Matt Carlsonb9e45482011-11-04 09:14:59 +00006792 if (!budget ||
6793 tg3_tx_frag_set(tnapi, &entry, &budget, mapping,
Matt Carlson84b67b22011-07-27 14:20:52 +00006794 len, base_flags |
6795 ((i == last) ? TXD_FLAG_END : 0),
Matt Carlsonb9e45482011-11-04 09:14:59 +00006796 tmp_mss, vlan)) {
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006797 would_hit_hwbug = 1;
Matt Carlsonb9e45482011-11-04 09:14:59 +00006798 break;
6799 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006800 }
6801 }
6802
6803 if (would_hit_hwbug) {
Matt Carlson0d681b22011-07-27 14:20:49 +00006804 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006805
6806 /* If the workaround fails due to memory/mapping
6807 * failure, silently drop this packet.
6808 */
Matt Carlson84b67b22011-07-27 14:20:52 +00006809 entry = tnapi->tx_prod;
6810 budget = tg3_tx_avail(tnapi);
David S. Miller1805b2f2011-10-24 18:18:09 -04006811 if (tigon3_dma_hwbug_workaround(tnapi, &skb, &entry, &budget,
Matt Carlson84b67b22011-07-27 14:20:52 +00006812 base_flags, mss, vlan))
Eric Dumazet48855432011-10-24 07:53:03 +00006813 goto drop_nofree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006814 }
6815
Richard Cochrand515b452011-06-19 03:31:41 +00006816 skb_tx_timestamp(skb);
Tom Herbert298376d2011-11-28 16:33:30 +00006817 netdev_sent_queue(tp->dev, skb->len);
Richard Cochrand515b452011-06-19 03:31:41 +00006818
Linus Torvalds1da177e2005-04-16 15:20:36 -07006819 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006820 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006821
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006822 tnapi->tx_prod = entry;
6823 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006824 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006825
6826 /* netif_tx_stop_queue() must be done before checking
6827 * checking tx index in tg3_tx_avail() below, because in
6828 * tg3_tx(), we update tx index before checking for
6829 * netif_tx_queue_stopped().
6830 */
6831 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006832 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006833 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07006834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006835
Eric Dumazetcdd0db02009-05-28 00:00:41 +00006836 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006837 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006838
6839dma_error:
Matt Carlsonba1142e2011-11-04 09:15:00 +00006840 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, --i);
Matt Carlson432aa7e2011-05-19 12:12:45 +00006841 tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
Eric Dumazet48855432011-10-24 07:53:03 +00006842drop:
6843 dev_kfree_skb(skb);
6844drop_nofree:
6845 tp->tx_dropped++;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006846 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006847}
6848
Matt Carlson6e01b202011-08-19 13:58:20 +00006849static void tg3_mac_loopback(struct tg3 *tp, bool enable)
6850{
6851 if (enable) {
6852 tp->mac_mode &= ~(MAC_MODE_HALF_DUPLEX |
6853 MAC_MODE_PORT_MODE_MASK);
6854
6855 tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
6856
6857 if (!tg3_flag(tp, 5705_PLUS))
6858 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
6859
6860 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
6861 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
6862 else
6863 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
6864 } else {
6865 tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
6866
6867 if (tg3_flag(tp, 5705_PLUS) ||
6868 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) ||
6869 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
6870 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
6871 }
6872
6873 tw32(MAC_MODE, tp->mac_mode);
6874 udelay(40);
6875}
6876
Matt Carlson941ec902011-08-19 13:58:23 +00006877static int tg3_phy_lpbk_set(struct tg3 *tp, u32 speed, bool extlpbk)
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006878{
Matt Carlson941ec902011-08-19 13:58:23 +00006879 u32 val, bmcr, mac_mode, ptest = 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006880
6881 tg3_phy_toggle_apd(tp, false);
6882 tg3_phy_toggle_automdix(tp, 0);
6883
Matt Carlson941ec902011-08-19 13:58:23 +00006884 if (extlpbk && tg3_phy_set_extloopbk(tp))
6885 return -EIO;
6886
6887 bmcr = BMCR_FULLDPLX;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006888 switch (speed) {
6889 case SPEED_10:
6890 break;
6891 case SPEED_100:
6892 bmcr |= BMCR_SPEED100;
6893 break;
6894 case SPEED_1000:
6895 default:
6896 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
6897 speed = SPEED_100;
6898 bmcr |= BMCR_SPEED100;
6899 } else {
6900 speed = SPEED_1000;
6901 bmcr |= BMCR_SPEED1000;
6902 }
6903 }
6904
Matt Carlson941ec902011-08-19 13:58:23 +00006905 if (extlpbk) {
6906 if (!(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
6907 tg3_readphy(tp, MII_CTRL1000, &val);
6908 val |= CTL1000_AS_MASTER |
6909 CTL1000_ENABLE_MASTER;
6910 tg3_writephy(tp, MII_CTRL1000, val);
6911 } else {
6912 ptest = MII_TG3_FET_PTEST_TRIM_SEL |
6913 MII_TG3_FET_PTEST_TRIM_2;
6914 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest);
6915 }
6916 } else
6917 bmcr |= BMCR_LOOPBACK;
6918
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006919 tg3_writephy(tp, MII_BMCR, bmcr);
6920
6921 /* The write needs to be flushed for the FETs */
6922 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
6923 tg3_readphy(tp, MII_BMCR, &bmcr);
6924
6925 udelay(40);
6926
6927 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
6928 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Matt Carlson941ec902011-08-19 13:58:23 +00006929 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest |
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006930 MII_TG3_FET_PTEST_FRC_TX_LINK |
6931 MII_TG3_FET_PTEST_FRC_TX_LOCK);
6932
6933 /* The write needs to be flushed for the AC131 */
6934 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
6935 }
6936
6937 /* Reset to prevent losing 1st rx packet intermittently */
6938 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
6939 tg3_flag(tp, 5780_CLASS)) {
6940 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
6941 udelay(10);
6942 tw32_f(MAC_RX_MODE, tp->rx_mode);
6943 }
6944
6945 mac_mode = tp->mac_mode &
6946 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
6947 if (speed == SPEED_1000)
6948 mac_mode |= MAC_MODE_PORT_MODE_GMII;
6949 else
6950 mac_mode |= MAC_MODE_PORT_MODE_MII;
6951
6952 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
6953 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
6954
6955 if (masked_phy_id == TG3_PHY_ID_BCM5401)
6956 mac_mode &= ~MAC_MODE_LINK_POLARITY;
6957 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
6958 mac_mode |= MAC_MODE_LINK_POLARITY;
6959
6960 tg3_writephy(tp, MII_TG3_EXT_CTRL,
6961 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
6962 }
6963
6964 tw32(MAC_MODE, mac_mode);
6965 udelay(40);
Matt Carlson941ec902011-08-19 13:58:23 +00006966
6967 return 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006968}
6969
Michał Mirosławc8f44af2011-11-15 15:29:55 +00006970static void tg3_set_loopback(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006971{
6972 struct tg3 *tp = netdev_priv(dev);
6973
6974 if (features & NETIF_F_LOOPBACK) {
6975 if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
6976 return;
6977
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006978 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00006979 tg3_mac_loopback(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006980 netif_carrier_on(tp->dev);
6981 spin_unlock_bh(&tp->lock);
6982 netdev_info(dev, "Internal MAC loopback mode enabled.\n");
6983 } else {
6984 if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
6985 return;
6986
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006987 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00006988 tg3_mac_loopback(tp, false);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006989 /* Force link status check */
6990 tg3_setup_phy(tp, 1);
6991 spin_unlock_bh(&tp->lock);
6992 netdev_info(dev, "Internal MAC loopback mode disabled.\n");
6993 }
6994}
6995
Michał Mirosławc8f44af2011-11-15 15:29:55 +00006996static netdev_features_t tg3_fix_features(struct net_device *dev,
6997 netdev_features_t features)
Michał Mirosławdc668912011-04-07 03:35:07 +00006998{
6999 struct tg3 *tp = netdev_priv(dev);
7000
Joe Perches63c3a662011-04-26 08:12:10 +00007001 if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
Michał Mirosławdc668912011-04-07 03:35:07 +00007002 features &= ~NETIF_F_ALL_TSO;
7003
7004 return features;
7005}
7006
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007007static int tg3_set_features(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007008{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007009 netdev_features_t changed = dev->features ^ features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007010
7011 if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
7012 tg3_set_loopback(dev, features);
7013
7014 return 0;
7015}
7016
Linus Torvalds1da177e2005-04-16 15:20:36 -07007017static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
7018 int new_mtu)
7019{
7020 dev->mtu = new_mtu;
7021
Michael Chanef7f5ec2005-07-25 12:32:25 -07007022 if (new_mtu > ETH_DATA_LEN) {
Joe Perches63c3a662011-04-26 08:12:10 +00007023 if (tg3_flag(tp, 5780_CLASS)) {
Michał Mirosławdc668912011-04-07 03:35:07 +00007024 netdev_update_features(dev);
Joe Perches63c3a662011-04-26 08:12:10 +00007025 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson859a5882010-04-05 10:19:28 +00007026 } else {
Joe Perches63c3a662011-04-26 08:12:10 +00007027 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Matt Carlson859a5882010-04-05 10:19:28 +00007028 }
Michael Chanef7f5ec2005-07-25 12:32:25 -07007029 } else {
Joe Perches63c3a662011-04-26 08:12:10 +00007030 if (tg3_flag(tp, 5780_CLASS)) {
7031 tg3_flag_set(tp, TSO_CAPABLE);
Michał Mirosławdc668912011-04-07 03:35:07 +00007032 netdev_update_features(dev);
7033 }
Joe Perches63c3a662011-04-26 08:12:10 +00007034 tg3_flag_clear(tp, JUMBO_RING_ENABLE);
Michael Chanef7f5ec2005-07-25 12:32:25 -07007035 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007036}
7037
7038static int tg3_change_mtu(struct net_device *dev, int new_mtu)
7039{
7040 struct tg3 *tp = netdev_priv(dev);
Michael Chanb9ec6c12006-07-25 16:37:27 -07007041 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007042
7043 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
7044 return -EINVAL;
7045
7046 if (!netif_running(dev)) {
7047 /* We'll just catch it later when the
7048 * device is up'd.
7049 */
7050 tg3_set_mtu(dev, tp, new_mtu);
7051 return 0;
7052 }
7053
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07007054 tg3_phy_stop(tp);
7055
Linus Torvalds1da177e2005-04-16 15:20:36 -07007056 tg3_netif_stop(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07007057
7058 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007059
Michael Chan944d9802005-05-29 14:57:48 -07007060 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007061
7062 tg3_set_mtu(dev, tp, new_mtu);
7063
Michael Chanb9ec6c12006-07-25 16:37:27 -07007064 err = tg3_restart_hw(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007065
Michael Chanb9ec6c12006-07-25 16:37:27 -07007066 if (!err)
7067 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007068
David S. Millerf47c11e2005-06-24 20:18:35 -07007069 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007070
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07007071 if (!err)
7072 tg3_phy_start(tp);
7073
Michael Chanb9ec6c12006-07-25 16:37:27 -07007074 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007075}
7076
Matt Carlson21f581a2009-08-28 14:00:25 +00007077static void tg3_rx_prodring_free(struct tg3 *tp,
7078 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007079{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007080 int i;
7081
Matt Carlson8fea32b2010-09-15 08:59:58 +00007082 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007083 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00007084 i = (i + 1) & tp->rx_std_ring_mask)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007085 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007086 tp->rx_pkt_map_sz);
7087
Joe Perches63c3a662011-04-26 08:12:10 +00007088 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007089 for (i = tpr->rx_jmb_cons_idx;
7090 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00007091 i = (i + 1) & tp->rx_jmb_ring_mask) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007092 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007093 TG3_RX_JMB_MAP_SZ);
7094 }
7095 }
7096
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007097 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007099
Matt Carlson2c49a442010-09-30 10:34:35 +00007100 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007101 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007102 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007103
Joe Perches63c3a662011-04-26 08:12:10 +00007104 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007105 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007106 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007107 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007108 }
7109}
7110
Matt Carlsonc6cdf432010-04-05 10:19:26 +00007111/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007112 *
7113 * The chip has been shut down and the driver detached from
7114 * the networking, so no interrupts or new tx packets will
7115 * end up in the driver. tp->{tx,}lock are held and thus
7116 * we may not sleep.
7117 */
Matt Carlson21f581a2009-08-28 14:00:25 +00007118static int tg3_rx_prodring_alloc(struct tg3 *tp,
7119 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007120{
Matt Carlson287be122009-08-28 13:58:46 +00007121 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007122
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007123 tpr->rx_std_cons_idx = 0;
7124 tpr->rx_std_prod_idx = 0;
7125 tpr->rx_jmb_cons_idx = 0;
7126 tpr->rx_jmb_prod_idx = 0;
7127
Matt Carlson8fea32b2010-09-15 08:59:58 +00007128 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007129 memset(&tpr->rx_std_buffers[0], 0,
7130 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00007131 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007132 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00007133 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007134 goto done;
7135 }
7136
Linus Torvalds1da177e2005-04-16 15:20:36 -07007137 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00007138 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007139
Matt Carlson287be122009-08-28 13:58:46 +00007140 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00007141 if (tg3_flag(tp, 5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00007142 tp->dev->mtu > ETH_DATA_LEN)
7143 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
7144 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07007145
Linus Torvalds1da177e2005-04-16 15:20:36 -07007146 /* Initialize invariants of the rings, we only set this
7147 * stuff once. This works because the card does not
7148 * write into the rx buffer posting rings.
7149 */
Matt Carlson2c49a442010-09-30 10:34:35 +00007150 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007151 struct tg3_rx_buffer_desc *rxd;
7152
Matt Carlson21f581a2009-08-28 14:00:25 +00007153 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00007154 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007155 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
7156 rxd->opaque = (RXD_OPAQUE_RING_STD |
7157 (i << RXD_OPAQUE_INDEX_SHIFT));
7158 }
7159
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007160 /* Now allocate fresh SKBs for each rx ring. */
7161 for (i = 0; i < tp->rx_pending; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007162 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_STD, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007163 netdev_warn(tp->dev,
7164 "Using a smaller RX standard ring. Only "
7165 "%d out of %d buffers were allocated "
7166 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007167 if (i == 0)
7168 goto initfail;
7169 tp->rx_pending = i;
7170 break;
7171 }
7172 }
7173
Joe Perches63c3a662011-04-26 08:12:10 +00007174 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007175 goto done;
7176
Matt Carlson2c49a442010-09-30 10:34:35 +00007177 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007178
Joe Perches63c3a662011-04-26 08:12:10 +00007179 if (!tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson0d86df82010-02-17 15:17:00 +00007180 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007181
Matt Carlson2c49a442010-09-30 10:34:35 +00007182 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00007183 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007184
Matt Carlson0d86df82010-02-17 15:17:00 +00007185 rxd = &tpr->rx_jmb[i].std;
7186 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
7187 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
7188 RXD_FLAG_JUMBO;
7189 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
7190 (i << RXD_OPAQUE_INDEX_SHIFT));
7191 }
7192
7193 for (i = 0; i < tp->rx_jumbo_pending; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007194 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_JUMBO, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007195 netdev_warn(tp->dev,
7196 "Using a smaller RX jumbo ring. Only %d "
7197 "out of %d buffers were allocated "
7198 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00007199 if (i == 0)
7200 goto initfail;
7201 tp->rx_jumbo_pending = i;
7202 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007203 }
7204 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007205
7206done:
Michael Chan32d8c572006-07-25 16:38:29 -07007207 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007208
7209initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00007210 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007211 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007212}
7213
Matt Carlson21f581a2009-08-28 14:00:25 +00007214static void tg3_rx_prodring_fini(struct tg3 *tp,
7215 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007216{
Matt Carlson21f581a2009-08-28 14:00:25 +00007217 kfree(tpr->rx_std_buffers);
7218 tpr->rx_std_buffers = NULL;
7219 kfree(tpr->rx_jmb_buffers);
7220 tpr->rx_jmb_buffers = NULL;
7221 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007222 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
7223 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00007224 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007225 }
Matt Carlson21f581a2009-08-28 14:00:25 +00007226 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007227 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
7228 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00007229 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007230 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007231}
7232
Matt Carlson21f581a2009-08-28 14:00:25 +00007233static int tg3_rx_prodring_init(struct tg3 *tp,
7234 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007235{
Matt Carlson2c49a442010-09-30 10:34:35 +00007236 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
7237 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007238 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007239 return -ENOMEM;
7240
Matt Carlson4bae65c2010-11-24 08:31:52 +00007241 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
7242 TG3_RX_STD_RING_BYTES(tp),
7243 &tpr->rx_std_mapping,
7244 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007245 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007246 goto err_out;
7247
Joe Perches63c3a662011-04-26 08:12:10 +00007248 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007249 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00007250 GFP_KERNEL);
7251 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007252 goto err_out;
7253
Matt Carlson4bae65c2010-11-24 08:31:52 +00007254 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
7255 TG3_RX_JMB_RING_BYTES(tp),
7256 &tpr->rx_jmb_mapping,
7257 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007258 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007259 goto err_out;
7260 }
7261
7262 return 0;
7263
7264err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00007265 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007266 return -ENOMEM;
7267}
7268
7269/* Free up pending packets in all rx/tx rings.
7270 *
7271 * The chip has been shut down and the driver detached from
7272 * the networking, so no interrupts or new tx packets will
7273 * end up in the driver. tp->{tx,}lock is not held and we are not
7274 * in an interrupt context and thus may sleep.
7275 */
7276static void tg3_free_rings(struct tg3 *tp)
7277{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007278 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007279
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007280 for (j = 0; j < tp->irq_cnt; j++) {
7281 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007282
Matt Carlson8fea32b2010-09-15 08:59:58 +00007283 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00007284
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007285 if (!tnapi->tx_buffers)
7286 continue;
7287
Matt Carlson0d681b22011-07-27 14:20:49 +00007288 for (i = 0; i < TG3_TX_RING_SIZE; i++) {
7289 struct sk_buff *skb = tnapi->tx_buffers[i].skb;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007290
Matt Carlson0d681b22011-07-27 14:20:49 +00007291 if (!skb)
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007292 continue;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007293
Matt Carlsonba1142e2011-11-04 09:15:00 +00007294 tg3_tx_skb_unmap(tnapi, i,
7295 skb_shinfo(skb)->nr_frags - 1);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007296
7297 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007298 }
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007299 }
Tom Herbert298376d2011-11-28 16:33:30 +00007300 netdev_reset_queue(tp->dev);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007301}
7302
7303/* Initialize tx/rx rings for packet processing.
7304 *
7305 * The chip has been shut down and the driver detached from
7306 * the networking, so no interrupts or new tx packets will
7307 * end up in the driver. tp->{tx,}lock are held and thus
7308 * we may not sleep.
7309 */
7310static int tg3_init_rings(struct tg3 *tp)
7311{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007312 int i;
Matt Carlson72334482009-08-28 14:03:01 +00007313
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007314 /* Free up all the SKBs. */
7315 tg3_free_rings(tp);
7316
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007317 for (i = 0; i < tp->irq_cnt; i++) {
7318 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007319
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007320 tnapi->last_tag = 0;
7321 tnapi->last_irq_tag = 0;
7322 tnapi->hw_status->status = 0;
7323 tnapi->hw_status->status_tag = 0;
7324 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7325
7326 tnapi->tx_prod = 0;
7327 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007328 if (tnapi->tx_ring)
7329 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007330
7331 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007332 if (tnapi->rx_rcb)
7333 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007334
Matt Carlson8fea32b2010-09-15 08:59:58 +00007335 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00007336 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007337 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00007338 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007339 }
Matt Carlson72334482009-08-28 14:03:01 +00007340
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007341 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007342}
7343
7344/*
7345 * Must not be invoked with interrupt sources disabled and
7346 * the hardware shutdown down.
7347 */
7348static void tg3_free_consistent(struct tg3 *tp)
7349{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007350 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00007351
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007352 for (i = 0; i < tp->irq_cnt; i++) {
7353 struct tg3_napi *tnapi = &tp->napi[i];
7354
7355 if (tnapi->tx_ring) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007356 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007357 tnapi->tx_ring, tnapi->tx_desc_mapping);
7358 tnapi->tx_ring = NULL;
7359 }
7360
7361 kfree(tnapi->tx_buffers);
7362 tnapi->tx_buffers = NULL;
7363
7364 if (tnapi->rx_rcb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007365 dma_free_coherent(&tp->pdev->dev,
7366 TG3_RX_RCB_RING_BYTES(tp),
7367 tnapi->rx_rcb,
7368 tnapi->rx_rcb_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007369 tnapi->rx_rcb = NULL;
7370 }
7371
Matt Carlson8fea32b2010-09-15 08:59:58 +00007372 tg3_rx_prodring_fini(tp, &tnapi->prodring);
7373
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007374 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007375 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
7376 tnapi->hw_status,
7377 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007378 tnapi->hw_status = NULL;
7379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007380 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007381
Linus Torvalds1da177e2005-04-16 15:20:36 -07007382 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007383 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
7384 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007385 tp->hw_stats = NULL;
7386 }
7387}
7388
7389/*
7390 * Must not be invoked with interrupt sources disabled and
7391 * the hardware shutdown down. Can sleep.
7392 */
7393static int tg3_alloc_consistent(struct tg3 *tp)
7394{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007395 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00007396
Matt Carlson4bae65c2010-11-24 08:31:52 +00007397 tp->hw_stats = dma_alloc_coherent(&tp->pdev->dev,
7398 sizeof(struct tg3_hw_stats),
7399 &tp->stats_mapping,
7400 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007401 if (!tp->hw_stats)
7402 goto err_out;
7403
Linus Torvalds1da177e2005-04-16 15:20:36 -07007404 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
7405
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007406 for (i = 0; i < tp->irq_cnt; i++) {
7407 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007408 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007409
Matt Carlson4bae65c2010-11-24 08:31:52 +00007410 tnapi->hw_status = dma_alloc_coherent(&tp->pdev->dev,
7411 TG3_HW_STATUS_SIZE,
7412 &tnapi->status_mapping,
7413 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007414 if (!tnapi->hw_status)
7415 goto err_out;
7416
7417 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007418 sblk = tnapi->hw_status;
7419
Matt Carlson8fea32b2010-09-15 08:59:58 +00007420 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
7421 goto err_out;
7422
Matt Carlson19cfaec2009-12-03 08:36:20 +00007423 /* If multivector TSS is enabled, vector 0 does not handle
7424 * tx interrupts. Don't allocate any resources for it.
7425 */
Joe Perches63c3a662011-04-26 08:12:10 +00007426 if ((!i && !tg3_flag(tp, ENABLE_TSS)) ||
7427 (i && tg3_flag(tp, ENABLE_TSS))) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00007428 tnapi->tx_buffers = kzalloc(
7429 sizeof(struct tg3_tx_ring_info) *
7430 TG3_TX_RING_SIZE, GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007431 if (!tnapi->tx_buffers)
7432 goto err_out;
7433
Matt Carlson4bae65c2010-11-24 08:31:52 +00007434 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
7435 TG3_TX_RING_BYTES,
7436 &tnapi->tx_desc_mapping,
7437 GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007438 if (!tnapi->tx_ring)
7439 goto err_out;
7440 }
7441
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007442 /*
7443 * When RSS is enabled, the status block format changes
7444 * slightly. The "rx_jumbo_consumer", "reserved",
7445 * and "rx_mini_consumer" members get mapped to the
7446 * other three rx return ring producer indexes.
7447 */
7448 switch (i) {
7449 default:
7450 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
7451 break;
7452 case 2:
7453 tnapi->rx_rcb_prod_idx = &sblk->rx_jumbo_consumer;
7454 break;
7455 case 3:
7456 tnapi->rx_rcb_prod_idx = &sblk->reserved;
7457 break;
7458 case 4:
7459 tnapi->rx_rcb_prod_idx = &sblk->rx_mini_consumer;
7460 break;
7461 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007462
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007463 /*
7464 * If multivector RSS is enabled, vector 0 does not handle
7465 * rx or tx interrupts. Don't allocate any resources for it.
7466 */
Joe Perches63c3a662011-04-26 08:12:10 +00007467 if (!i && tg3_flag(tp, ENABLE_RSS))
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007468 continue;
7469
Matt Carlson4bae65c2010-11-24 08:31:52 +00007470 tnapi->rx_rcb = dma_alloc_coherent(&tp->pdev->dev,
7471 TG3_RX_RCB_RING_BYTES(tp),
7472 &tnapi->rx_rcb_mapping,
7473 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007474 if (!tnapi->rx_rcb)
7475 goto err_out;
7476
7477 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007478 }
7479
Linus Torvalds1da177e2005-04-16 15:20:36 -07007480 return 0;
7481
7482err_out:
7483 tg3_free_consistent(tp);
7484 return -ENOMEM;
7485}
7486
7487#define MAX_WAIT_CNT 1000
7488
7489/* To stop a block, clear the enable bit and poll till it
7490 * clears. tp->lock is held.
7491 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007492static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007493{
7494 unsigned int i;
7495 u32 val;
7496
Joe Perches63c3a662011-04-26 08:12:10 +00007497 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007498 switch (ofs) {
7499 case RCVLSC_MODE:
7500 case DMAC_MODE:
7501 case MBFREE_MODE:
7502 case BUFMGR_MODE:
7503 case MEMARB_MODE:
7504 /* We can't enable/disable these bits of the
7505 * 5705/5750, just say success.
7506 */
7507 return 0;
7508
7509 default:
7510 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007512 }
7513
7514 val = tr32(ofs);
7515 val &= ~enable_bit;
7516 tw32_f(ofs, val);
7517
7518 for (i = 0; i < MAX_WAIT_CNT; i++) {
7519 udelay(100);
7520 val = tr32(ofs);
7521 if ((val & enable_bit) == 0)
7522 break;
7523 }
7524
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007525 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00007526 dev_err(&tp->pdev->dev,
7527 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
7528 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007529 return -ENODEV;
7530 }
7531
7532 return 0;
7533}
7534
7535/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007536static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007537{
7538 int i, err;
7539
7540 tg3_disable_ints(tp);
7541
7542 tp->rx_mode &= ~RX_MODE_ENABLE;
7543 tw32_f(MAC_RX_MODE, tp->rx_mode);
7544 udelay(10);
7545
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007546 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
7547 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
7548 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
7549 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
7550 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
7551 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007552
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007553 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
7554 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
7555 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
7556 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
7557 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
7558 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
7559 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007560
7561 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
7562 tw32_f(MAC_MODE, tp->mac_mode);
7563 udelay(40);
7564
7565 tp->tx_mode &= ~TX_MODE_ENABLE;
7566 tw32_f(MAC_TX_MODE, tp->tx_mode);
7567
7568 for (i = 0; i < MAX_WAIT_CNT; i++) {
7569 udelay(100);
7570 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
7571 break;
7572 }
7573 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00007574 dev_err(&tp->pdev->dev,
7575 "%s timed out, TX_MODE_ENABLE will not clear "
7576 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07007577 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007578 }
7579
Michael Chane6de8ad2005-05-05 14:42:41 -07007580 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007581 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
7582 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007583
7584 tw32(FTQ_RESET, 0xffffffff);
7585 tw32(FTQ_RESET, 0x00000000);
7586
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007587 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
7588 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007589
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007590 for (i = 0; i < tp->irq_cnt; i++) {
7591 struct tg3_napi *tnapi = &tp->napi[i];
7592 if (tnapi->hw_status)
7593 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007595
Linus Torvalds1da177e2005-04-16 15:20:36 -07007596 return err;
7597}
7598
Michael Chanee6a99b2007-07-18 21:49:10 -07007599/* Save PCI command register before chip reset */
7600static void tg3_save_pci_state(struct tg3 *tp)
7601{
Matt Carlson8a6eac92007-10-21 16:17:55 -07007602 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007603}
7604
7605/* Restore PCI state after chip reset */
7606static void tg3_restore_pci_state(struct tg3 *tp)
7607{
7608 u32 val;
7609
7610 /* Re-enable indirect register accesses. */
7611 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
7612 tp->misc_host_ctrl);
7613
7614 /* Set MAX PCI retry to zero. */
7615 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
7616 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007617 tg3_flag(tp, PCIX_MODE))
Michael Chanee6a99b2007-07-18 21:49:10 -07007618 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07007619 /* Allow reads and writes to the APE register and memory space. */
Joe Perches63c3a662011-04-26 08:12:10 +00007620 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07007621 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc2010-06-05 17:24:30 +00007622 PCISTATE_ALLOW_APE_SHMEM_WR |
7623 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07007624 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
7625
Matt Carlson8a6eac92007-10-21 16:17:55 -07007626 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007627
Matt Carlson2c55a3d2011-11-28 09:41:04 +00007628 if (!tg3_flag(tp, PCI_EXPRESS)) {
7629 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
7630 tp->pci_cacheline_sz);
7631 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
7632 tp->pci_lat_timer);
Michael Chan114342f2007-10-15 02:12:26 -07007633 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08007634
Michael Chanee6a99b2007-07-18 21:49:10 -07007635 /* Make sure PCI-X relaxed ordering bit is clear. */
Joe Perches63c3a662011-04-26 08:12:10 +00007636 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07007637 u16 pcix_cmd;
7638
7639 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7640 &pcix_cmd);
7641 pcix_cmd &= ~PCI_X_CMD_ERO;
7642 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7643 pcix_cmd);
7644 }
Michael Chanee6a99b2007-07-18 21:49:10 -07007645
Joe Perches63c3a662011-04-26 08:12:10 +00007646 if (tg3_flag(tp, 5780_CLASS)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007647
7648 /* Chip reset on 5780 will reset MSI enable bit,
7649 * so need to restore it.
7650 */
Joe Perches63c3a662011-04-26 08:12:10 +00007651 if (tg3_flag(tp, USING_MSI)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007652 u16 ctrl;
7653
7654 pci_read_config_word(tp->pdev,
7655 tp->msi_cap + PCI_MSI_FLAGS,
7656 &ctrl);
7657 pci_write_config_word(tp->pdev,
7658 tp->msi_cap + PCI_MSI_FLAGS,
7659 ctrl | PCI_MSI_FLAGS_ENABLE);
7660 val = tr32(MSGINT_MODE);
7661 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
7662 }
7663 }
7664}
7665
Linus Torvalds1da177e2005-04-16 15:20:36 -07007666/* tp->lock is held. */
7667static int tg3_chip_reset(struct tg3 *tp)
7668{
7669 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07007670 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00007671 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007672
David S. Millerf49639e2006-06-09 11:58:36 -07007673 tg3_nvram_lock(tp);
7674
Matt Carlson77b483f2008-08-15 14:07:24 -07007675 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
7676
David S. Millerf49639e2006-06-09 11:58:36 -07007677 /* No matching tg3_nvram_unlock() after this because
7678 * chip reset below will undo the nvram lock.
7679 */
7680 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007681
Michael Chanee6a99b2007-07-18 21:49:10 -07007682 /* GRC_MISC_CFG core clock reset will clear the memory
7683 * enable bit in PCI register 4 and the MSI enable bit
7684 * on some chips, so we save relevant registers here.
7685 */
7686 tg3_save_pci_state(tp);
7687
Michael Chand9ab5ad2006-03-20 22:27:35 -08007688 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Joe Perches63c3a662011-04-26 08:12:10 +00007689 tg3_flag(tp, 5755_PLUS))
Michael Chand9ab5ad2006-03-20 22:27:35 -08007690 tw32(GRC_FASTBOOT_PC, 0);
7691
Linus Torvalds1da177e2005-04-16 15:20:36 -07007692 /*
7693 * We must avoid the readl() that normally takes place.
7694 * It locks machines, causes machine checks, and other
7695 * fun things. So, temporarily disable the 5701
7696 * hardware workaround, while we do the reset.
7697 */
Michael Chan1ee582d2005-08-09 20:16:46 -07007698 write_op = tp->write32;
7699 if (write_op == tg3_write_flush_reg32)
7700 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007701
Michael Chand18edcb2007-03-24 20:57:11 -07007702 /* Prevent the irq handler from reading or writing PCI registers
7703 * during chip reset when the memory enable bit in the PCI command
7704 * register may be cleared. The chip does not generate interrupt
7705 * at this time, but the irq handler may still be called due to irq
7706 * sharing or irqpoll.
7707 */
Joe Perches63c3a662011-04-26 08:12:10 +00007708 tg3_flag_set(tp, CHIP_RESETTING);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007709 for (i = 0; i < tp->irq_cnt; i++) {
7710 struct tg3_napi *tnapi = &tp->napi[i];
7711 if (tnapi->hw_status) {
7712 tnapi->hw_status->status = 0;
7713 tnapi->hw_status->status_tag = 0;
7714 }
7715 tnapi->last_tag = 0;
7716 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07007717 }
Michael Chand18edcb2007-03-24 20:57:11 -07007718 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00007719
7720 for (i = 0; i < tp->irq_cnt; i++)
7721 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07007722
Matt Carlson255ca312009-08-25 10:07:27 +00007723 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7724 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7725 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
7726 }
7727
Linus Torvalds1da177e2005-04-16 15:20:36 -07007728 /* do the reset */
7729 val = GRC_MISC_CFG_CORECLK_RESET;
7730
Joe Perches63c3a662011-04-26 08:12:10 +00007731 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson88075d92010-08-02 11:25:58 +00007732 /* Force PCIe 1.0a mode */
7733 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007734 !tg3_flag(tp, 57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00007735 tr32(TG3_PCIE_PHY_TSTCTL) ==
7736 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
7737 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
7738
Linus Torvalds1da177e2005-04-16 15:20:36 -07007739 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
7740 tw32(GRC_MISC_CFG, (1 << 29));
7741 val |= (1 << 29);
7742 }
7743 }
7744
Michael Chanb5d37722006-09-27 16:06:21 -07007745 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7746 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
7747 tw32(GRC_VCPU_EXT_CTRL,
7748 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
7749 }
7750
Matt Carlsonf37500d2010-08-02 11:25:59 +00007751 /* Manage gphy power for all CPMU absent PCIe devices. */
Joe Perches63c3a662011-04-26 08:12:10 +00007752 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007753 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00007754
Linus Torvalds1da177e2005-04-16 15:20:36 -07007755 tw32(GRC_MISC_CFG, val);
7756
Michael Chan1ee582d2005-08-09 20:16:46 -07007757 /* restore 5701 hardware bug workaround write method */
7758 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007759
7760 /* Unfortunately, we have to delay before the PCI read back.
7761 * Some 575X chips even will not respond to a PCI cfg access
7762 * when the reset command is given to the chip.
7763 *
7764 * How do these hardware designers expect things to work
7765 * properly if the PCI write is posted for a long period
7766 * of time? It is always necessary to have some method by
7767 * which a register read back can occur to push the write
7768 * out which does the reset.
7769 *
7770 * For most tg3 variants the trick below was working.
7771 * Ho hum...
7772 */
7773 udelay(120);
7774
7775 /* Flush PCI posted writes. The normal MMIO registers
7776 * are inaccessible at this time so this is the only
7777 * way to make this reliably (actually, this is no longer
7778 * the case, see above). I tried to use indirect
7779 * register read/write but this upset some 5701 variants.
7780 */
7781 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
7782
7783 udelay(120);
7784
Jon Mason708ebb32011-06-27 12:56:50 +00007785 if (tg3_flag(tp, PCI_EXPRESS) && pci_pcie_cap(tp->pdev)) {
Matt Carlsone7126992009-08-25 10:08:16 +00007786 u16 val16;
7787
Linus Torvalds1da177e2005-04-16 15:20:36 -07007788 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
7789 int i;
7790 u32 cfg_val;
7791
7792 /* Wait for link training to complete. */
7793 for (i = 0; i < 5000; i++)
7794 udelay(100);
7795
7796 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
7797 pci_write_config_dword(tp->pdev, 0xc4,
7798 cfg_val | (1 << 15));
7799 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007800
Matt Carlsone7126992009-08-25 10:08:16 +00007801 /* Clear the "no snoop" and "relaxed ordering" bits. */
7802 pci_read_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00007803 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007804 &val16);
7805 val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
7806 PCI_EXP_DEVCTL_NOSNOOP_EN);
7807 /*
7808 * Older PCIe devices only support the 128 byte
7809 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007810 */
Joe Perches63c3a662011-04-26 08:12:10 +00007811 if (!tg3_flag(tp, CPMU_PRESENT))
Matt Carlsone7126992009-08-25 10:08:16 +00007812 val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007813 pci_write_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00007814 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007815 val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007816
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007817 /* Clear error status */
7818 pci_write_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00007819 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVSTA,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007820 PCI_EXP_DEVSTA_CED |
7821 PCI_EXP_DEVSTA_NFED |
7822 PCI_EXP_DEVSTA_FED |
7823 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007824 }
7825
Michael Chanee6a99b2007-07-18 21:49:10 -07007826 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007827
Joe Perches63c3a662011-04-26 08:12:10 +00007828 tg3_flag_clear(tp, CHIP_RESETTING);
7829 tg3_flag_clear(tp, ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07007830
Michael Chanee6a99b2007-07-18 21:49:10 -07007831 val = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00007832 if (tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07007833 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07007834 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007835
7836 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
7837 tg3_stop_fw(tp);
7838 tw32(0x5000, 0x400);
7839 }
7840
7841 tw32(GRC_MODE, tp->grc_mode);
7842
7843 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007844 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007845
7846 tw32(0xc4, val | (1 << 15));
7847 }
7848
7849 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
7850 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
7851 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
7852 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
7853 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
7854 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7855 }
7856
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007857 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00007858 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007859 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007860 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00007861 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007862 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007863 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007864 val = 0;
7865
7866 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007867 udelay(40);
7868
Matt Carlson77b483f2008-08-15 14:07:24 -07007869 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
7870
Michael Chan7a6f4362006-09-27 16:03:31 -07007871 err = tg3_poll_fw(tp);
7872 if (err)
7873 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007874
Matt Carlson0a9140c2009-08-28 12:27:50 +00007875 tg3_mdio_start(tp);
7876
Joe Perches63c3a662011-04-26 08:12:10 +00007877 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007878 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
7879 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007880 !tg3_flag(tp, 57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007881 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007882
7883 tw32(0x7c00, val | (1 << 25));
7884 }
7885
Matt Carlsond78b59f2011-04-05 14:22:46 +00007886 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
7887 val = tr32(TG3_CPMU_CLCK_ORIDE);
7888 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
7889 }
7890
Linus Torvalds1da177e2005-04-16 15:20:36 -07007891 /* Reprobe ASF enable state. */
Joe Perches63c3a662011-04-26 08:12:10 +00007892 tg3_flag_clear(tp, ENABLE_ASF);
7893 tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007894 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
7895 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
7896 u32 nic_cfg;
7897
7898 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
7899 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +00007900 tg3_flag_set(tp, ENABLE_ASF);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007901 tp->last_event_jiffies = jiffies;
Joe Perches63c3a662011-04-26 08:12:10 +00007902 if (tg3_flag(tp, 5750_PLUS))
7903 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007904 }
7905 }
7906
7907 return 0;
7908}
7909
Matt Carlson92feeab2011-12-08 14:40:14 +00007910static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *,
7911 struct rtnl_link_stats64 *);
7912static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *,
7913 struct tg3_ethtool_stats *);
7914
Linus Torvalds1da177e2005-04-16 15:20:36 -07007915/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07007916static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007917{
7918 int err;
7919
7920 tg3_stop_fw(tp);
7921
Michael Chan944d9802005-05-29 14:57:48 -07007922 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007923
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007924 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007925 err = tg3_chip_reset(tp);
7926
Matt Carlsondaba2a62009-04-20 06:58:52 +00007927 __tg3_set_mac_addr(tp, 0);
7928
Michael Chan944d9802005-05-29 14:57:48 -07007929 tg3_write_sig_legacy(tp, kind);
7930 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007931
Matt Carlson92feeab2011-12-08 14:40:14 +00007932 if (tp->hw_stats) {
7933 /* Save the stats across chip resets... */
7934 tg3_get_stats64(tp->dev, &tp->net_stats_prev),
7935 tg3_get_estats(tp, &tp->estats_prev);
7936
7937 /* And make sure the next sample is new data */
7938 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
7939 }
7940
Linus Torvalds1da177e2005-04-16 15:20:36 -07007941 if (err)
7942 return err;
7943
7944 return 0;
7945}
7946
Linus Torvalds1da177e2005-04-16 15:20:36 -07007947static int tg3_set_mac_addr(struct net_device *dev, void *p)
7948{
7949 struct tg3 *tp = netdev_priv(dev);
7950 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07007951 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007952
Michael Chanf9804dd2005-09-27 12:13:10 -07007953 if (!is_valid_ether_addr(addr->sa_data))
7954 return -EINVAL;
7955
Linus Torvalds1da177e2005-04-16 15:20:36 -07007956 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7957
Michael Chane75f7c92006-03-20 21:33:26 -08007958 if (!netif_running(dev))
7959 return 0;
7960
Joe Perches63c3a662011-04-26 08:12:10 +00007961 if (tg3_flag(tp, ENABLE_ASF)) {
Michael Chan986e0ae2007-05-05 12:10:20 -07007962 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07007963
Michael Chan986e0ae2007-05-05 12:10:20 -07007964 addr0_high = tr32(MAC_ADDR_0_HIGH);
7965 addr0_low = tr32(MAC_ADDR_0_LOW);
7966 addr1_high = tr32(MAC_ADDR_1_HIGH);
7967 addr1_low = tr32(MAC_ADDR_1_LOW);
7968
7969 /* Skip MAC addr 1 if ASF is using it. */
7970 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
7971 !(addr1_high == 0 && addr1_low == 0))
7972 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07007973 }
Michael Chan986e0ae2007-05-05 12:10:20 -07007974 spin_lock_bh(&tp->lock);
7975 __tg3_set_mac_addr(tp, skip_mac_1);
7976 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007977
Michael Chanb9ec6c12006-07-25 16:37:27 -07007978 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007979}
7980
7981/* tp->lock is held. */
7982static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
7983 dma_addr_t mapping, u32 maxlen_flags,
7984 u32 nic_addr)
7985{
7986 tg3_write_mem(tp,
7987 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
7988 ((u64) mapping >> 32));
7989 tg3_write_mem(tp,
7990 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
7991 ((u64) mapping & 0xffffffff));
7992 tg3_write_mem(tp,
7993 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
7994 maxlen_flags);
7995
Joe Perches63c3a662011-04-26 08:12:10 +00007996 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007997 tg3_write_mem(tp,
7998 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
7999 nic_addr);
8000}
8001
8002static void __tg3_set_rx_mode(struct net_device *);
Michael Chand244c892005-07-05 14:42:33 -07008003static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07008004{
Matt Carlsonb6080e12009-09-01 13:12:00 +00008005 int i;
8006
Joe Perches63c3a662011-04-26 08:12:10 +00008007 if (!tg3_flag(tp, ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00008008 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
8009 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
8010 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008011 } else {
8012 tw32(HOSTCC_TXCOL_TICKS, 0);
8013 tw32(HOSTCC_TXMAX_FRAMES, 0);
8014 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00008015 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008016
Joe Perches63c3a662011-04-26 08:12:10 +00008017 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008018 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
8019 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
8020 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
8021 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00008022 tw32(HOSTCC_RXCOL_TICKS, 0);
8023 tw32(HOSTCC_RXMAX_FRAMES, 0);
8024 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07008025 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008026
Joe Perches63c3a662011-04-26 08:12:10 +00008027 if (!tg3_flag(tp, 5705_PLUS)) {
David S. Miller15f98502005-05-18 22:49:26 -07008028 u32 val = ec->stats_block_coalesce_usecs;
8029
Matt Carlsonb6080e12009-09-01 13:12:00 +00008030 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
8031 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
8032
David S. Miller15f98502005-05-18 22:49:26 -07008033 if (!netif_carrier_ok(tp->dev))
8034 val = 0;
8035
8036 tw32(HOSTCC_STAT_COAL_TICKS, val);
8037 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008038
8039 for (i = 0; i < tp->irq_cnt - 1; i++) {
8040 u32 reg;
8041
8042 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
8043 tw32(reg, ec->rx_coalesce_usecs);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008044 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
8045 tw32(reg, ec->rx_max_coalesced_frames);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008046 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
8047 tw32(reg, ec->rx_max_coalesced_frames_irq);
Matt Carlson19cfaec2009-12-03 08:36:20 +00008048
Joe Perches63c3a662011-04-26 08:12:10 +00008049 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008050 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
8051 tw32(reg, ec->tx_coalesce_usecs);
8052 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
8053 tw32(reg, ec->tx_max_coalesced_frames);
8054 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
8055 tw32(reg, ec->tx_max_coalesced_frames_irq);
8056 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008057 }
8058
8059 for (; i < tp->irq_max - 1; i++) {
8060 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008061 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008062 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00008063
Joe Perches63c3a662011-04-26 08:12:10 +00008064 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008065 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
8066 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
8067 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
8068 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008069 }
David S. Miller15f98502005-05-18 22:49:26 -07008070}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008071
8072/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00008073static void tg3_rings_reset(struct tg3 *tp)
8074{
8075 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008076 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008077 struct tg3_napi *tnapi = &tp->napi[0];
8078
8079 /* Disable all transmit rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008080 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008081 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Joe Perches63c3a662011-04-26 08:12:10 +00008082 else if (tg3_flag(tp, 5717_PLUS))
Matt Carlson3d377282010-10-14 10:37:39 +00008083 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
Matt Carlsonb703df62009-12-03 08:36:21 +00008084 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8085 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008086 else
8087 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8088
8089 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8090 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
8091 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
8092 BDINFO_FLAGS_DISABLED);
8093
8094
8095 /* Disable all receive return rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008096 if (tg3_flag(tp, 5717_PLUS))
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008097 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
Joe Perches63c3a662011-04-26 08:12:10 +00008098 else if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008099 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00008100 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
8101 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson2d31eca2009-09-01 12:53:31 +00008102 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
8103 else
8104 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8105
8106 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8107 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
8108 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
8109 BDINFO_FLAGS_DISABLED);
8110
8111 /* Disable interrupts */
8112 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008113 tp->napi[0].chk_msi_cnt = 0;
8114 tp->napi[0].last_rx_cons = 0;
8115 tp->napi[0].last_tx_cons = 0;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008116
8117 /* Zero mailbox registers. */
Joe Perches63c3a662011-04-26 08:12:10 +00008118 if (tg3_flag(tp, SUPPORT_MSIX)) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00008119 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008120 tp->napi[i].tx_prod = 0;
8121 tp->napi[i].tx_cons = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00008122 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008123 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008124 tw32_rx_mbox(tp->napi[i].consmbox, 0);
8125 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
Matt Carlson7f230732011-08-31 11:44:48 +00008126 tp->napi[i].chk_msi_cnt = 0;
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008127 tp->napi[i].last_rx_cons = 0;
8128 tp->napi[i].last_tx_cons = 0;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008129 }
Joe Perches63c3a662011-04-26 08:12:10 +00008130 if (!tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008131 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008132 } else {
8133 tp->napi[0].tx_prod = 0;
8134 tp->napi[0].tx_cons = 0;
8135 tw32_mailbox(tp->napi[0].prodmbox, 0);
8136 tw32_rx_mbox(tp->napi[0].consmbox, 0);
8137 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008138
8139 /* Make sure the NIC-based send BD rings are disabled. */
Joe Perches63c3a662011-04-26 08:12:10 +00008140 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson2d31eca2009-09-01 12:53:31 +00008141 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
8142 for (i = 0; i < 16; i++)
8143 tw32_tx_mbox(mbox + i * 8, 0);
8144 }
8145
8146 txrcb = NIC_SRAM_SEND_RCB;
8147 rxrcb = NIC_SRAM_RCV_RET_RCB;
8148
8149 /* Clear status block in ram. */
8150 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8151
8152 /* Set status block DMA address */
8153 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8154 ((u64) tnapi->status_mapping >> 32));
8155 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8156 ((u64) tnapi->status_mapping & 0xffffffff));
8157
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008158 if (tnapi->tx_ring) {
8159 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8160 (TG3_TX_RING_SIZE <<
8161 BDINFO_FLAGS_MAXLEN_SHIFT),
8162 NIC_SRAM_TX_BUFFER_DESC);
8163 txrcb += TG3_BDINFO_SIZE;
8164 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008165
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008166 if (tnapi->rx_rcb) {
8167 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008168 (tp->rx_ret_ring_mask + 1) <<
8169 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008170 rxrcb += TG3_BDINFO_SIZE;
8171 }
8172
8173 stblk = HOSTCC_STATBLCK_RING1;
8174
8175 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
8176 u64 mapping = (u64)tnapi->status_mapping;
8177 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
8178 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
8179
8180 /* Clear status block in ram. */
8181 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8182
Matt Carlson19cfaec2009-12-03 08:36:20 +00008183 if (tnapi->tx_ring) {
8184 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8185 (TG3_TX_RING_SIZE <<
8186 BDINFO_FLAGS_MAXLEN_SHIFT),
8187 NIC_SRAM_TX_BUFFER_DESC);
8188 txrcb += TG3_BDINFO_SIZE;
8189 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008190
8191 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008192 ((tp->rx_ret_ring_mask + 1) <<
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008193 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
8194
8195 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008196 rxrcb += TG3_BDINFO_SIZE;
8197 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008198}
8199
Matt Carlsoneb07a942011-04-20 07:57:36 +00008200static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
8201{
8202 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
8203
Joe Perches63c3a662011-04-26 08:12:10 +00008204 if (!tg3_flag(tp, 5750_PLUS) ||
8205 tg3_flag(tp, 5780_CLASS) ||
Matt Carlsoneb07a942011-04-20 07:57:36 +00008206 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
Matt Carlson513aa6e2011-11-21 15:01:18 +00008207 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
8208 tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008209 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
8210 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
8211 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787)
8212 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
8213 else
8214 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
8215
8216 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
8217 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
8218
8219 val = min(nic_rep_thresh, host_rep_thresh);
8220 tw32(RCVBDI_STD_THRESH, val);
8221
Joe Perches63c3a662011-04-26 08:12:10 +00008222 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008223 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
8224
Joe Perches63c3a662011-04-26 08:12:10 +00008225 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008226 return;
8227
Matt Carlson513aa6e2011-11-21 15:01:18 +00008228 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
Matt Carlsoneb07a942011-04-20 07:57:36 +00008229
8230 host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
8231
8232 val = min(bdcache_maxcnt / 2, host_rep_thresh);
8233 tw32(RCVBDI_JUMBO_THRESH, val);
8234
Joe Perches63c3a662011-04-26 08:12:10 +00008235 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008236 tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
8237}
8238
Matt Carlson2d31eca2009-09-01 12:53:31 +00008239/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008240static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008241{
8242 u32 val, rdmac_mode;
8243 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00008244 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008245
8246 tg3_disable_ints(tp);
8247
8248 tg3_stop_fw(tp);
8249
8250 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
8251
Joe Perches63c3a662011-04-26 08:12:10 +00008252 if (tg3_flag(tp, INIT_COMPLETE))
Michael Chane6de8ad2005-05-05 14:42:41 -07008253 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008254
Matt Carlson699c0192010-12-06 08:28:51 +00008255 /* Enable MAC control of LPI */
8256 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
8257 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL,
8258 TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
8259 TG3_CPMU_EEE_LNKIDL_UART_IDL);
8260
8261 tw32_f(TG3_CPMU_EEE_CTRL,
8262 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
8263
Matt Carlsona386b902010-12-06 08:28:53 +00008264 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
8265 TG3_CPMU_EEEMD_LPI_IN_TX |
8266 TG3_CPMU_EEEMD_LPI_IN_RX |
8267 TG3_CPMU_EEEMD_EEE_ENABLE;
8268
8269 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8270 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
8271
Joe Perches63c3a662011-04-26 08:12:10 +00008272 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsona386b902010-12-06 08:28:53 +00008273 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
8274
8275 tw32_f(TG3_CPMU_EEE_MODE, val);
8276
8277 tw32_f(TG3_CPMU_EEE_DBTMR1,
8278 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
8279 TG3_CPMU_DBTMR1_LNKIDLE_2047US);
8280
8281 tw32_f(TG3_CPMU_EEE_DBTMR2,
Matt Carlsond7f2ab22011-01-25 15:58:56 +00008282 TG3_CPMU_DBTMR2_APE_TX_2047US |
Matt Carlsona386b902010-12-06 08:28:53 +00008283 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
Matt Carlson699c0192010-12-06 08:28:51 +00008284 }
8285
Matt Carlson603f1172010-02-12 14:47:10 +00008286 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08008287 tg3_phy_reset(tp);
8288
Linus Torvalds1da177e2005-04-16 15:20:36 -07008289 err = tg3_chip_reset(tp);
8290 if (err)
8291 return err;
8292
8293 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
8294
Matt Carlsonbcb37f62008-11-03 16:52:09 -08008295 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008296 val = tr32(TG3_CPMU_CTRL);
8297 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
8298 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08008299
8300 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8301 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8302 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8303 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
8304
8305 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
8306 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
8307 val |= CPMU_LNK_AWARE_MACCLK_6_25;
8308 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
8309
8310 val = tr32(TG3_CPMU_HST_ACC);
8311 val &= ~CPMU_HST_ACC_MACCLK_MASK;
8312 val |= CPMU_HST_ACC_MACCLK_6_25;
8313 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07008314 }
8315
Matt Carlson33466d92009-04-20 06:57:41 +00008316 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
8317 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
8318 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
8319 PCIE_PWR_MGMT_L1_THRESH_4MS;
8320 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00008321
8322 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
8323 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
8324
8325 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00008326
Matt Carlsonf40386c2009-11-02 14:24:02 +00008327 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
8328 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00008329 }
8330
Joe Perches63c3a662011-04-26 08:12:10 +00008331 if (tg3_flag(tp, L1PLLPD_EN)) {
Matt Carlson614b05902010-01-20 16:58:02 +00008332 u32 grc_mode = tr32(GRC_MODE);
8333
8334 /* Access the lower 1K of PL PCIE block registers. */
8335 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8336 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
8337
8338 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
8339 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
8340 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
8341
8342 tw32(GRC_MODE, grc_mode);
8343 }
8344
Matt Carlson5093eed2010-11-24 08:31:45 +00008345 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
8346 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
8347 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00008348
Matt Carlson5093eed2010-11-24 08:31:45 +00008349 /* Access the lower 1K of PL PCIE block registers. */
8350 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8351 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00008352
Matt Carlson5093eed2010-11-24 08:31:45 +00008353 val = tr32(TG3_PCIE_TLDLPL_PORT +
8354 TG3_PCIE_PL_LO_PHYCTL5);
8355 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
8356 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00008357
Matt Carlson5093eed2010-11-24 08:31:45 +00008358 tw32(GRC_MODE, grc_mode);
8359 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00008360
Matt Carlson1ff30a52011-05-19 12:12:46 +00008361 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_57765_AX) {
8362 u32 grc_mode = tr32(GRC_MODE);
8363
8364 /* Access the lower 1K of DL PCIE block registers. */
8365 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8366 tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
8367
8368 val = tr32(TG3_PCIE_TLDLPL_PORT +
8369 TG3_PCIE_DL_LO_FTSMAX);
8370 val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
8371 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
8372 val | TG3_PCIE_DL_LO_FTSMAX_VAL);
8373
8374 tw32(GRC_MODE, grc_mode);
8375 }
8376
Matt Carlsona977dbe2010-04-12 06:58:26 +00008377 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8378 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8379 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8380 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00008381 }
8382
Linus Torvalds1da177e2005-04-16 15:20:36 -07008383 /* This works around an issue with Athlon chipsets on
8384 * B3 tigon3 silicon. This bit has no effect on any
8385 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07008386 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008387 */
Joe Perches63c3a662011-04-26 08:12:10 +00008388 if (!tg3_flag(tp, CPMU_PRESENT)) {
8389 if (!tg3_flag(tp, PCI_EXPRESS))
Matt Carlson795d01c2007-10-07 23:28:17 -07008390 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
8391 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
8392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008393
8394 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008395 tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008396 val = tr32(TG3PCI_PCISTATE);
8397 val |= PCISTATE_RETRY_SAME_DMA;
8398 tw32(TG3PCI_PCISTATE, val);
8399 }
8400
Joe Perches63c3a662011-04-26 08:12:10 +00008401 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -07008402 /* Allow reads and writes to the
8403 * APE register and memory space.
8404 */
8405 val = tr32(TG3PCI_PCISTATE);
8406 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc2010-06-05 17:24:30 +00008407 PCISTATE_ALLOW_APE_SHMEM_WR |
8408 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008409 tw32(TG3PCI_PCISTATE, val);
8410 }
8411
Linus Torvalds1da177e2005-04-16 15:20:36 -07008412 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
8413 /* Enable some hw fixes. */
8414 val = tr32(TG3PCI_MSI_DATA);
8415 val |= (1 << 26) | (1 << 28) | (1 << 29);
8416 tw32(TG3PCI_MSI_DATA, val);
8417 }
8418
8419 /* Descriptor ring init may make accesses to the
8420 * NIC SRAM area to setup the TX descriptors, so we
8421 * can only do this after the hardware has been
8422 * successfully reset.
8423 */
Michael Chan32d8c572006-07-25 16:38:29 -07008424 err = tg3_init_rings(tp);
8425 if (err)
8426 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008427
Joe Perches63c3a662011-04-26 08:12:10 +00008428 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008429 val = tr32(TG3PCI_DMA_RW_CTRL) &
8430 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Matt Carlson1a319022010-04-12 06:58:25 +00008431 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
8432 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlson0aebff42011-04-25 12:42:45 +00008433 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57765 &&
8434 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8435 val |= DMA_RWCTRL_TAGGED_STAT_WA;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008436 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
8437 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
8438 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008439 /* This value is determined during the probe time DMA
8440 * engine test, tg3_test_dma.
8441 */
8442 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
8443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008444
8445 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
8446 GRC_MODE_4X_NIC_SEND_RINGS |
8447 GRC_MODE_NO_TX_PHDR_CSUM |
8448 GRC_MODE_NO_RX_PHDR_CSUM);
8449 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07008450
8451 /* Pseudo-header checksum is done by hardware logic and not
8452 * the offload processers, so make the chip do the pseudo-
8453 * header checksums on receive. For transmit it is more
8454 * convenient to do the pseudo-header checksum in software
8455 * as Linux does that on transmit for us in all cases.
8456 */
8457 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008458
8459 tw32(GRC_MODE,
8460 tp->grc_mode |
8461 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
8462
8463 /* Setup the timer prescalar register. Clock is always 66Mhz. */
8464 val = tr32(GRC_MISC_CFG);
8465 val &= ~0xff;
8466 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
8467 tw32(GRC_MISC_CFG, val);
8468
8469 /* Initialize MBUF/DESC pool. */
Joe Perches63c3a662011-04-26 08:12:10 +00008470 if (tg3_flag(tp, 5750_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008471 /* Do nothing. */
8472 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
8473 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
8474 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
8475 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
8476 else
8477 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
8478 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
8479 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Joe Perches63c3a662011-04-26 08:12:10 +00008480 } else if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008481 int fw_len;
8482
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08008483 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008484 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
8485 tw32(BUFMGR_MB_POOL_ADDR,
8486 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
8487 tw32(BUFMGR_MB_POOL_SIZE,
8488 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
8489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008490
Michael Chan0f893dc2005-07-25 12:30:38 -07008491 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008492 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8493 tp->bufmgr_config.mbuf_read_dma_low_water);
8494 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8495 tp->bufmgr_config.mbuf_mac_rx_low_water);
8496 tw32(BUFMGR_MB_HIGH_WATER,
8497 tp->bufmgr_config.mbuf_high_water);
8498 } else {
8499 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8500 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
8501 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8502 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
8503 tw32(BUFMGR_MB_HIGH_WATER,
8504 tp->bufmgr_config.mbuf_high_water_jumbo);
8505 }
8506 tw32(BUFMGR_DMA_LOW_WATER,
8507 tp->bufmgr_config.dma_low_water);
8508 tw32(BUFMGR_DMA_HIGH_WATER,
8509 tp->bufmgr_config.dma_high_water);
8510
Matt Carlsond309a462010-09-30 10:34:31 +00008511 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
8512 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
8513 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
Matt Carlson4d958472011-04-20 07:57:35 +00008514 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
8515 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
8516 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0)
8517 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
Matt Carlsond309a462010-09-30 10:34:31 +00008518 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008519 for (i = 0; i < 2000; i++) {
8520 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
8521 break;
8522 udelay(10);
8523 }
8524 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00008525 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008526 return -ENODEV;
8527 }
8528
Matt Carlsoneb07a942011-04-20 07:57:36 +00008529 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
8530 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
Michael Chanb5d37722006-09-27 16:06:21 -07008531
Matt Carlsoneb07a942011-04-20 07:57:36 +00008532 tg3_setup_rxbd_thresholds(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008533
8534 /* Initialize TG3_BDINFO's at:
8535 * RCVDBDI_STD_BD: standard eth size rx ring
8536 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
8537 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
8538 *
8539 * like so:
8540 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
8541 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
8542 * ring attribute flags
8543 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
8544 *
8545 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
8546 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
8547 *
8548 * The size of each ring is fixed in the firmware, but the location is
8549 * configurable.
8550 */
8551 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008552 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008553 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008554 ((u64) tpr->rx_std_mapping & 0xffffffff));
Joe Perches63c3a662011-04-26 08:12:10 +00008555 if (!tg3_flag(tp, 5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +00008556 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
8557 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008558
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008559 /* Disable the mini ring */
Joe Perches63c3a662011-04-26 08:12:10 +00008560 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008561 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
8562 BDINFO_FLAGS_DISABLED);
8563
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008564 /* Program the jumbo buffer descriptor ring control
8565 * blocks on those devices that have them.
8566 */
Matt Carlsona0512942011-07-27 14:20:54 +00008567 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008568 (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008569
Joe Perches63c3a662011-04-26 08:12:10 +00008570 if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008571 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008572 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008573 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008574 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +00008575 val = TG3_RX_JMB_RING_SIZE(tp) <<
8576 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008577 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +00008578 val | BDINFO_FLAGS_USE_EXT_RECV);
Joe Perches63c3a662011-04-26 08:12:10 +00008579 if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
Matt Carlsona50d0792010-06-05 17:24:37 +00008580 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson87668d32009-11-13 13:03:34 +00008581 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
8582 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008583 } else {
8584 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
8585 BDINFO_FLAGS_DISABLED);
8586 }
8587
Joe Perches63c3a662011-04-26 08:12:10 +00008588 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonfa6b2aa2011-11-21 15:01:19 +00008589 val = TG3_RX_STD_RING_SIZE(tp);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008590 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
8591 val |= (TG3_RX_STD_DMA_SZ << 2);
8592 } else
Matt Carlson04380d42010-04-12 06:58:29 +00008593 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008594 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008595 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008596
8597 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008598
Matt Carlson411da642009-11-13 13:03:46 +00008599 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e62009-11-13 13:03:49 +00008600 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008601
Joe Perches63c3a662011-04-26 08:12:10 +00008602 tpr->rx_jmb_prod_idx =
8603 tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
Matt Carlson66711e62009-11-13 13:03:49 +00008604 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008605
Matt Carlson2d31eca2009-09-01 12:53:31 +00008606 tg3_rings_reset(tp);
8607
Linus Torvalds1da177e2005-04-16 15:20:36 -07008608 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07008609 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008610
8611 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00008612 tw32(MAC_RX_MTU_SIZE,
8613 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008614
8615 /* The slot time is changed by tg3_setup_phy if we
8616 * run at gigabit with half duplex.
8617 */
Matt Carlsonf2096f92011-04-05 14:22:48 +00008618 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
8619 (6 << TX_LENGTHS_IPG_SHIFT) |
8620 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
8621
8622 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8623 val |= tr32(MAC_TX_LENGTHS) &
8624 (TX_LENGTHS_JMB_FRM_LEN_MSK |
8625 TX_LENGTHS_CNT_DWN_VAL_MSK);
8626
8627 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008628
8629 /* Receive rules. */
8630 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
8631 tw32(RCVLPC_CONFIG, 0x0181);
8632
8633 /* Calculate RDMAC_MODE setting early, we need it to determine
8634 * the RCVLPC_STATE_ENABLE mask.
8635 */
8636 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
8637 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
8638 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
8639 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
8640 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07008641
Matt Carlsondeabaac2010-11-24 08:31:50 +00008642 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +00008643 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
8644
Matt Carlson57e69832008-05-25 23:48:31 -07008645 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08008646 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8647 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07008648 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
8649 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
8650 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
8651
Matt Carlsonc5908932011-03-09 16:58:25 +00008652 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8653 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00008654 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07008655 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008656 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
8657 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008658 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008659 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8660 }
8661 }
8662
Joe Perches63c3a662011-04-26 08:12:10 +00008663 if (tg3_flag(tp, PCI_EXPRESS))
Michael Chan85e94ce2005-04-21 17:05:28 -07008664 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8665
Joe Perches63c3a662011-04-26 08:12:10 +00008666 if (tg3_flag(tp, HW_TSO_1) ||
8667 tg3_flag(tp, HW_TSO_2) ||
8668 tg3_flag(tp, HW_TSO_3))
Matt Carlson027455a2008-12-21 20:19:30 -08008669 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
8670
Matt Carlson108a6c12011-05-19 12:12:47 +00008671 if (tg3_flag(tp, 57765_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +00008672 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson027455a2008-12-21 20:19:30 -08008673 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
8674 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008675
Matt Carlsonf2096f92011-04-05 14:22:48 +00008676 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8677 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
8678
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008679 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
8680 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
8681 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8682 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008683 tg3_flag(tp, 57765_PLUS)) {
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008684 val = tr32(TG3_RDMA_RSRVCTRL_REG);
Matt Carlsond78b59f2011-04-05 14:22:46 +00008685 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8686 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +00008687 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
8688 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
8689 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
8690 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
8691 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
8692 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +00008693 }
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008694 tw32(TG3_RDMA_RSRVCTRL_REG,
8695 val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
8696 }
8697
Matt Carlsond78b59f2011-04-05 14:22:46 +00008698 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8699 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsond309a462010-09-30 10:34:31 +00008700 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
8701 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val |
8702 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
8703 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
8704 }
8705
Linus Torvalds1da177e2005-04-16 15:20:36 -07008706 /* Receive/send statistics. */
Joe Perches63c3a662011-04-26 08:12:10 +00008707 if (tg3_flag(tp, 5750_PLUS)) {
Michael Chan16613942006-06-29 20:15:13 -07008708 val = tr32(RCVLPC_STATS_ENABLE);
8709 val &= ~RCVLPC_STATSENAB_DACK_FIX;
8710 tw32(RCVLPC_STATS_ENABLE, val);
8711 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008712 tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008713 val = tr32(RCVLPC_STATS_ENABLE);
8714 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
8715 tw32(RCVLPC_STATS_ENABLE, val);
8716 } else {
8717 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
8718 }
8719 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
8720 tw32(SNDDATAI_STATSENAB, 0xffffff);
8721 tw32(SNDDATAI_STATSCTRL,
8722 (SNDDATAI_SCTRL_ENABLE |
8723 SNDDATAI_SCTRL_FASTUPD));
8724
8725 /* Setup host coalescing engine. */
8726 tw32(HOSTCC_MODE, 0);
8727 for (i = 0; i < 2000; i++) {
8728 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
8729 break;
8730 udelay(10);
8731 }
8732
Michael Chand244c892005-07-05 14:42:33 -07008733 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008734
Joe Perches63c3a662011-04-26 08:12:10 +00008735 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008736 /* Status/statistics block address. See tg3_timer,
8737 * the tg3_periodic_fetch_stats call there, and
8738 * tg3_get_stats to see how this works for 5705/5750 chips.
8739 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008740 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8741 ((u64) tp->stats_mapping >> 32));
8742 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8743 ((u64) tp->stats_mapping & 0xffffffff));
8744 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008745
Linus Torvalds1da177e2005-04-16 15:20:36 -07008746 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008747
8748 /* Clear statistics and status block memory areas */
8749 for (i = NIC_SRAM_STATS_BLK;
8750 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
8751 i += sizeof(u32)) {
8752 tg3_write_mem(tp, i, 0);
8753 udelay(40);
8754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008755 }
8756
8757 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
8758
8759 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
8760 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008761 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008762 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
8763
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008764 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
8765 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -07008766 /* reset to prevent losing 1st rx packet intermittently */
8767 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8768 udelay(10);
8769 }
8770
Matt Carlson3bda1252008-08-15 14:08:22 -07008771 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Matt Carlson9e975cc2011-07-20 10:20:50 +00008772 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE |
8773 MAC_MODE_FHDE_ENABLE;
8774 if (tg3_flag(tp, ENABLE_APE))
8775 tp->mac_mode |= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Joe Perches63c3a662011-04-26 08:12:10 +00008776 if (!tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008777 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07008778 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
8779 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008780 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
8781 udelay(40);
8782
Michael Chan314fba32005-04-21 17:07:04 -07008783 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Joe Perches63c3a662011-04-26 08:12:10 +00008784 * If TG3_FLAG_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07008785 * register to preserve the GPIO settings for LOMs. The GPIOs,
8786 * whether used as inputs or outputs, are set by boot code after
8787 * reset.
8788 */
Joe Perches63c3a662011-04-26 08:12:10 +00008789 if (!tg3_flag(tp, IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07008790 u32 gpio_mask;
8791
Michael Chan9d26e212006-12-07 00:21:14 -08008792 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
8793 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
8794 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07008795
8796 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
8797 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
8798 GRC_LCLCTRL_GPIO_OUTPUT3;
8799
Michael Chanaf36e6b2006-03-23 01:28:06 -08008800 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
8801 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
8802
Gary Zambranoaaf84462007-05-05 11:51:45 -07008803 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07008804 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
8805
8806 /* GPIO1 must be driven high for eeprom write protect */
Joe Perches63c3a662011-04-26 08:12:10 +00008807 if (tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan9d26e212006-12-07 00:21:14 -08008808 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
8809 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07008810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008811 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8812 udelay(100);
8813
Joe Perches63c3a662011-04-26 08:12:10 +00008814 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008815 val = tr32(MSGINT_MODE);
8816 val |= MSGINT_MODE_MULTIVEC_EN | MSGINT_MODE_ENABLE;
Matt Carlson5b39de92011-08-31 11:44:50 +00008817 if (!tg3_flag(tp, 1SHOT_MSI))
8818 val |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008819 tw32(MSGINT_MODE, val);
8820 }
8821
Joe Perches63c3a662011-04-26 08:12:10 +00008822 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008823 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
8824 udelay(40);
8825 }
8826
8827 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
8828 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
8829 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
8830 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
8831 WDMAC_MODE_LNGREAD_ENAB);
8832
Matt Carlsonc5908932011-03-09 16:58:25 +00008833 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8834 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00008835 if (tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008836 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
8837 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
8838 /* nothing */
8839 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008840 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008841 val |= WDMAC_MODE_RX_ACCEL;
8842 }
8843 }
8844
Michael Chand9ab5ad2006-03-20 22:27:35 -08008845 /* Enable host coalescing bug fix */
Joe Perches63c3a662011-04-26 08:12:10 +00008846 if (tg3_flag(tp, 5755_PLUS))
Matt Carlsonf51f3562008-05-25 23:45:08 -07008847 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad2006-03-20 22:27:35 -08008848
Matt Carlson788a0352009-11-02 14:26:03 +00008849 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
8850 val |= WDMAC_MODE_BURST_ALL_DATA;
8851
Linus Torvalds1da177e2005-04-16 15:20:36 -07008852 tw32_f(WDMAC_MODE, val);
8853 udelay(40);
8854
Joe Perches63c3a662011-04-26 08:12:10 +00008855 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07008856 u16 pcix_cmd;
8857
8858 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8859 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008860 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07008861 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
8862 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008863 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07008864 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
8865 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008866 }
Matt Carlson9974a352007-10-07 23:27:28 -07008867 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8868 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008869 }
8870
8871 tw32_f(RDMAC_MODE, rdmac_mode);
8872 udelay(40);
8873
8874 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008875 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008876 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07008877
8878 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
8879 tw32(SNDDATAC_MODE,
8880 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
8881 else
8882 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
8883
Linus Torvalds1da177e2005-04-16 15:20:36 -07008884 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
8885 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008886 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00008887 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008888 val |= RCVDBDI_MODE_LRG_RING_SZ;
8889 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008890 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008891 if (tg3_flag(tp, HW_TSO_1) ||
8892 tg3_flag(tp, HW_TSO_2) ||
8893 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008894 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008895 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00008896 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008897 val |= SNDBDI_MODE_MULTI_TXQ_EN;
8898 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008899 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
8900
8901 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
8902 err = tg3_load_5701_a0_firmware_fix(tp);
8903 if (err)
8904 return err;
8905 }
8906
Joe Perches63c3a662011-04-26 08:12:10 +00008907 if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008908 err = tg3_load_tso_firmware(tp);
8909 if (err)
8910 return err;
8911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008912
8913 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +00008914
Joe Perches63c3a662011-04-26 08:12:10 +00008915 if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsonb1d05212010-06-05 17:24:31 +00008916 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
8917 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +00008918
8919 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
8920 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
8921 tp->tx_mode &= ~val;
8922 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
8923 }
8924
Linus Torvalds1da177e2005-04-16 15:20:36 -07008925 tw32_f(MAC_TX_MODE, tp->tx_mode);
8926 udelay(100);
8927
Joe Perches63c3a662011-04-26 08:12:10 +00008928 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson9d53fa12011-07-20 10:20:54 +00008929 int i = 0;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008930 u32 reg = MAC_RSS_INDIR_TBL_0;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008931
Matt Carlson9d53fa12011-07-20 10:20:54 +00008932 if (tp->irq_cnt == 2) {
8933 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i += 8) {
8934 tw32(reg, 0x0);
8935 reg += 4;
8936 }
8937 } else {
8938 u32 val;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008939
Matt Carlson9d53fa12011-07-20 10:20:54 +00008940 while (i < TG3_RSS_INDIR_TBL_SIZE) {
8941 val = i % (tp->irq_cnt - 1);
8942 i++;
8943 for (; i % 8; i++) {
8944 val <<= 4;
8945 val |= (i % (tp->irq_cnt - 1));
8946 }
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008947 tw32(reg, val);
8948 reg += 4;
8949 }
8950 }
8951
8952 /* Setup the "secret" hash key. */
8953 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
8954 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
8955 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
8956 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
8957 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
8958 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
8959 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
8960 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
8961 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
8962 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
8963 }
8964
Linus Torvalds1da177e2005-04-16 15:20:36 -07008965 tp->rx_mode = RX_MODE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00008966 if (tg3_flag(tp, 5755_PLUS))
Michael Chanaf36e6b2006-03-23 01:28:06 -08008967 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
8968
Joe Perches63c3a662011-04-26 08:12:10 +00008969 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008970 tp->rx_mode |= RX_MODE_RSS_ENABLE |
8971 RX_MODE_RSS_ITBL_HASH_BITS_7 |
8972 RX_MODE_RSS_IPV6_HASH_EN |
8973 RX_MODE_RSS_TCP_IPV6_HASH_EN |
8974 RX_MODE_RSS_IPV4_HASH_EN |
8975 RX_MODE_RSS_TCP_IPV4_HASH_EN;
8976
Linus Torvalds1da177e2005-04-16 15:20:36 -07008977 tw32_f(MAC_RX_MODE, tp->rx_mode);
8978 udelay(10);
8979
Linus Torvalds1da177e2005-04-16 15:20:36 -07008980 tw32(MAC_LED_CTRL, tp->led_ctrl);
8981
8982 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008983 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008984 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8985 udelay(10);
8986 }
8987 tw32_f(MAC_RX_MODE, tp->rx_mode);
8988 udelay(10);
8989
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008990 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008991 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008992 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008993 /* Set drive transmission level to 1.2V */
8994 /* only if the signal pre-emphasis bit is not set */
8995 val = tr32(MAC_SERDES_CFG);
8996 val &= 0xfffff000;
8997 val |= 0x880;
8998 tw32(MAC_SERDES_CFG, val);
8999 }
9000 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
9001 tw32(MAC_SERDES_CFG, 0x616000);
9002 }
9003
9004 /* Prevent chip from dropping frames when flow control
9005 * is enabled.
9006 */
Matt Carlson666bc832010-01-20 16:58:03 +00009007 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
9008 val = 1;
9009 else
9010 val = 2;
9011 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009012
9013 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009014 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009015 /* Use hardware link auto-negotiation */
Joe Perches63c3a662011-04-26 08:12:10 +00009016 tg3_flag_set(tp, HW_AUTONEG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009017 }
9018
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009019 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +00009020 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08009021 u32 tmp;
9022
9023 tmp = tr32(SERDES_RX_CTRL);
9024 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
9025 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
9026 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
9027 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
9028 }
9029
Joe Perches63c3a662011-04-26 08:12:10 +00009030 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson80096062010-08-02 11:26:06 +00009031 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
9032 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07009033 tp->link_config.speed = tp->link_config.orig_speed;
9034 tp->link_config.duplex = tp->link_config.orig_duplex;
9035 tp->link_config.autoneg = tp->link_config.orig_autoneg;
9036 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009037
Matt Carlsondd477002008-05-25 23:45:58 -07009038 err = tg3_setup_phy(tp, 0);
9039 if (err)
9040 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009041
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009042 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
9043 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07009044 u32 tmp;
9045
9046 /* Clear CRC stats. */
9047 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
9048 tg3_writephy(tp, MII_TG3_TEST1,
9049 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009050 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -07009051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009052 }
9053 }
9054
9055 __tg3_set_rx_mode(tp->dev);
9056
9057 /* Initialize receive rules. */
9058 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
9059 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
9060 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
9061 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
9062
Joe Perches63c3a662011-04-26 08:12:10 +00009063 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009064 limit = 8;
9065 else
9066 limit = 16;
Joe Perches63c3a662011-04-26 08:12:10 +00009067 if (tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009068 limit -= 4;
9069 switch (limit) {
9070 case 16:
9071 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
9072 case 15:
9073 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
9074 case 14:
9075 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
9076 case 13:
9077 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
9078 case 12:
9079 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
9080 case 11:
9081 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
9082 case 10:
9083 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
9084 case 9:
9085 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
9086 case 8:
9087 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
9088 case 7:
9089 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
9090 case 6:
9091 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
9092 case 5:
9093 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
9094 case 4:
9095 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
9096 case 3:
9097 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
9098 case 2:
9099 case 1:
9100
9101 default:
9102 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07009103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009104
Joe Perches63c3a662011-04-26 08:12:10 +00009105 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson9ce768e2007-10-11 19:49:11 -07009106 /* Write our heartbeat update interval to APE. */
9107 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
9108 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07009109
Linus Torvalds1da177e2005-04-16 15:20:36 -07009110 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
9111
Linus Torvalds1da177e2005-04-16 15:20:36 -07009112 return 0;
9113}
9114
9115/* Called at device open time to get the chip ready for
9116 * packet processing. Invoked with tp->lock held.
9117 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009118static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009119{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009120 tg3_switch_clocks(tp);
9121
9122 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
9123
Matt Carlson2f751b62008-08-04 23:17:34 -07009124 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009125}
9126
9127#define TG3_STAT_ADD32(PSTAT, REG) \
9128do { u32 __val = tr32(REG); \
9129 (PSTAT)->low += __val; \
9130 if ((PSTAT)->low < __val) \
9131 (PSTAT)->high += 1; \
9132} while (0)
9133
9134static void tg3_periodic_fetch_stats(struct tg3 *tp)
9135{
9136 struct tg3_hw_stats *sp = tp->hw_stats;
9137
9138 if (!netif_carrier_ok(tp->dev))
9139 return;
9140
9141 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
9142 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
9143 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
9144 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
9145 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
9146 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
9147 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
9148 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
9149 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
9150 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
9151 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
9152 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
9153 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
9154
9155 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
9156 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
9157 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
9158 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
9159 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
9160 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
9161 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
9162 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
9163 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
9164 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
9165 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
9166 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
9167 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
9168 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07009169
9170 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
Matt Carlson310050f2011-05-19 12:12:55 +00009171 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
9172 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0 &&
9173 tp->pci_chip_rev_id != CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +00009174 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
9175 } else {
9176 u32 val = tr32(HOSTCC_FLOW_ATTN);
9177 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
9178 if (val) {
9179 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
9180 sp->rx_discards.low += val;
9181 if (sp->rx_discards.low < val)
9182 sp->rx_discards.high += 1;
9183 }
9184 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
9185 }
Michael Chan463d3052006-05-22 16:36:27 -07009186 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009187}
9188
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009189static void tg3_chk_missed_msi(struct tg3 *tp)
9190{
9191 u32 i;
9192
9193 for (i = 0; i < tp->irq_cnt; i++) {
9194 struct tg3_napi *tnapi = &tp->napi[i];
9195
9196 if (tg3_has_work(tnapi)) {
9197 if (tnapi->last_rx_cons == tnapi->rx_rcb_ptr &&
9198 tnapi->last_tx_cons == tnapi->tx_cons) {
9199 if (tnapi->chk_msi_cnt < 1) {
9200 tnapi->chk_msi_cnt++;
9201 return;
9202 }
Matt Carlson7f230732011-08-31 11:44:48 +00009203 tg3_msi(0, tnapi);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009204 }
9205 }
9206 tnapi->chk_msi_cnt = 0;
9207 tnapi->last_rx_cons = tnapi->rx_rcb_ptr;
9208 tnapi->last_tx_cons = tnapi->tx_cons;
9209 }
9210}
9211
Linus Torvalds1da177e2005-04-16 15:20:36 -07009212static void tg3_timer(unsigned long __opaque)
9213{
9214 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009215
Matt Carlson5b190622011-11-04 09:15:04 +00009216 if (tp->irq_sync || tg3_flag(tp, RESET_TASK_PENDING))
Michael Chanf475f162006-03-27 23:20:14 -08009217 goto restart_timer;
9218
David S. Millerf47c11e2005-06-24 20:18:35 -07009219 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009220
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009221 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
9222 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
9223 tg3_chk_missed_msi(tp);
9224
Joe Perches63c3a662011-04-26 08:12:10 +00009225 if (!tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -07009226 /* All of this garbage is because when using non-tagged
9227 * IRQ status the mailbox/status_block protocol the chip
9228 * uses with the cpu is race prone.
9229 */
Matt Carlson898a56f2009-08-28 14:02:40 +00009230 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -07009231 tw32(GRC_LOCAL_CTRL,
9232 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
9233 } else {
9234 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00009235 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -07009236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009237
David S. Millerfac9b832005-05-18 22:46:34 -07009238 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
David S. Millerf47c11e2005-06-24 20:18:35 -07009239 spin_unlock(&tp->lock);
Matt Carlsondb219972011-11-04 09:15:03 +00009240 tg3_reset_task_schedule(tp);
Matt Carlson5b190622011-11-04 09:15:04 +00009241 goto restart_timer;
David S. Millerfac9b832005-05-18 22:46:34 -07009242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009243 }
9244
Linus Torvalds1da177e2005-04-16 15:20:36 -07009245 /* This part only runs once per second. */
9246 if (!--tp->timer_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00009247 if (tg3_flag(tp, 5705_PLUS))
David S. Millerfac9b832005-05-18 22:46:34 -07009248 tg3_periodic_fetch_stats(tp);
9249
Matt Carlsonb0c59432011-05-19 12:12:48 +00009250 if (tp->setlpicnt && !--tp->setlpicnt)
9251 tg3_phy_eee_enable(tp);
Matt Carlson52b02d02010-10-14 10:37:41 +00009252
Joe Perches63c3a662011-04-26 08:12:10 +00009253 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009254 u32 mac_stat;
9255 int phy_event;
9256
9257 mac_stat = tr32(MAC_STATUS);
9258
9259 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009260 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009261 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
9262 phy_event = 1;
9263 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
9264 phy_event = 1;
9265
9266 if (phy_event)
9267 tg3_setup_phy(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +00009268 } else if (tg3_flag(tp, POLL_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009269 u32 mac_stat = tr32(MAC_STATUS);
9270 int need_setup = 0;
9271
9272 if (netif_carrier_ok(tp->dev) &&
9273 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
9274 need_setup = 1;
9275 }
Matt Carlsonbe98da62010-07-11 09:31:46 +00009276 if (!netif_carrier_ok(tp->dev) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009277 (mac_stat & (MAC_STATUS_PCS_SYNCED |
9278 MAC_STATUS_SIGNAL_DET))) {
9279 need_setup = 1;
9280 }
9281 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07009282 if (!tp->serdes_counter) {
9283 tw32_f(MAC_MODE,
9284 (tp->mac_mode &
9285 ~MAC_MODE_PORT_MODE_MASK));
9286 udelay(40);
9287 tw32_f(MAC_MODE, tp->mac_mode);
9288 udelay(40);
9289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009290 tg3_setup_phy(tp, 0);
9291 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009292 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009293 tg3_flag(tp, 5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -07009294 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +00009295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009296
9297 tp->timer_counter = tp->timer_multiplier;
9298 }
9299
Michael Chan130b8e42006-09-27 16:00:40 -07009300 /* Heartbeat is only sent once every 2 seconds.
9301 *
9302 * The heartbeat is to tell the ASF firmware that the host
9303 * driver is still alive. In the event that the OS crashes,
9304 * ASF needs to reset the hardware to free up the FIFO space
9305 * that may be filled with rx packets destined for the host.
9306 * If the FIFO is full, ASF will no longer function properly.
9307 *
9308 * Unintended resets have been reported on real time kernels
9309 * where the timer doesn't run on time. Netpoll will also have
9310 * same problem.
9311 *
9312 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
9313 * to check the ring condition when the heartbeat is expiring
9314 * before doing the reset. This will prevent most unintended
9315 * resets.
9316 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009317 if (!--tp->asf_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00009318 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07009319 tg3_wait_for_event_ack(tp);
9320
Michael Chanbbadf502006-04-06 21:46:34 -07009321 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07009322 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07009323 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +00009324 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
9325 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -07009326
9327 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009328 }
9329 tp->asf_counter = tp->asf_multiplier;
9330 }
9331
David S. Millerf47c11e2005-06-24 20:18:35 -07009332 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009333
Michael Chanf475f162006-03-27 23:20:14 -08009334restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07009335 tp->timer.expires = jiffies + tp->timer_offset;
9336 add_timer(&tp->timer);
9337}
9338
Matt Carlson4f125f42009-09-01 12:55:02 +00009339static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -08009340{
David Howells7d12e782006-10-05 14:55:46 +01009341 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009342 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +00009343 char *name;
9344 struct tg3_napi *tnapi = &tp->napi[irq_num];
9345
9346 if (tp->irq_cnt == 1)
9347 name = tp->dev->name;
9348 else {
9349 name = &tnapi->irq_lbl[0];
9350 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
9351 name[IFNAMSIZ-1] = 0;
9352 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009353
Joe Perches63c3a662011-04-26 08:12:10 +00009354 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Michael Chanfcfa0a32006-03-20 22:28:41 -08009355 fn = tg3_msi;
Joe Perches63c3a662011-04-26 08:12:10 +00009356 if (tg3_flag(tp, 1SHOT_MSI))
Michael Chanfcfa0a32006-03-20 22:28:41 -08009357 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009358 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009359 } else {
9360 fn = tg3_interrupt;
Joe Perches63c3a662011-04-26 08:12:10 +00009361 if (tg3_flag(tp, TAGGED_STATUS))
Michael Chanfcfa0a32006-03-20 22:28:41 -08009362 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009363 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009364 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009365
9366 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009367}
9368
Michael Chan79381092005-04-21 17:13:59 -07009369static int tg3_test_interrupt(struct tg3 *tp)
9370{
Matt Carlson09943a12009-08-28 14:01:57 +00009371 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -07009372 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -07009373 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009374 u32 val;
Michael Chan79381092005-04-21 17:13:59 -07009375
Michael Chand4bc3922005-05-29 14:59:20 -07009376 if (!netif_running(dev))
9377 return -ENODEV;
9378
Michael Chan79381092005-04-21 17:13:59 -07009379 tg3_disable_ints(tp);
9380
Matt Carlson4f125f42009-09-01 12:55:02 +00009381 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009382
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009383 /*
9384 * Turn off MSI one shot mode. Otherwise this test has no
9385 * observable way to know whether the interrupt was delivered.
9386 */
Matt Carlson3aa1cdf2011-07-20 10:20:55 +00009387 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009388 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
9389 tw32(MSGINT_MODE, val);
9390 }
9391
Matt Carlson4f125f42009-09-01 12:55:02 +00009392 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Matt Carlson09943a12009-08-28 14:01:57 +00009393 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009394 if (err)
9395 return err;
9396
Matt Carlson898a56f2009-08-28 14:02:40 +00009397 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -07009398 tg3_enable_ints(tp);
9399
9400 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00009401 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -07009402
9403 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -07009404 u32 int_mbox, misc_host_ctrl;
9405
Matt Carlson898a56f2009-08-28 14:02:40 +00009406 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -07009407 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
9408
9409 if ((int_mbox != 0) ||
9410 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
9411 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -07009412 break;
Michael Chanb16250e2006-09-27 16:10:14 -07009413 }
9414
Matt Carlson3aa1cdf2011-07-20 10:20:55 +00009415 if (tg3_flag(tp, 57765_PLUS) &&
9416 tnapi->hw_status->status_tag != tnapi->last_tag)
9417 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
9418
Michael Chan79381092005-04-21 17:13:59 -07009419 msleep(10);
9420 }
9421
9422 tg3_disable_ints(tp);
9423
Matt Carlson4f125f42009-09-01 12:55:02 +00009424 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009425
Matt Carlson4f125f42009-09-01 12:55:02 +00009426 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009427
9428 if (err)
9429 return err;
9430
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009431 if (intr_ok) {
9432 /* Reenable MSI one shot mode. */
Matt Carlson5b39de92011-08-31 11:44:50 +00009433 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, 1SHOT_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009434 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
9435 tw32(MSGINT_MODE, val);
9436 }
Michael Chan79381092005-04-21 17:13:59 -07009437 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009438 }
Michael Chan79381092005-04-21 17:13:59 -07009439
9440 return -EIO;
9441}
9442
9443/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
9444 * successfully restored
9445 */
9446static int tg3_test_msi(struct tg3 *tp)
9447{
Michael Chan79381092005-04-21 17:13:59 -07009448 int err;
9449 u16 pci_cmd;
9450
Joe Perches63c3a662011-04-26 08:12:10 +00009451 if (!tg3_flag(tp, USING_MSI))
Michael Chan79381092005-04-21 17:13:59 -07009452 return 0;
9453
9454 /* Turn off SERR reporting in case MSI terminates with Master
9455 * Abort.
9456 */
9457 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
9458 pci_write_config_word(tp->pdev, PCI_COMMAND,
9459 pci_cmd & ~PCI_COMMAND_SERR);
9460
9461 err = tg3_test_interrupt(tp);
9462
9463 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
9464
9465 if (!err)
9466 return 0;
9467
9468 /* other failures */
9469 if (err != -EIO)
9470 return err;
9471
9472 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009473 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
9474 "to INTx mode. Please report this failure to the PCI "
9475 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -07009476
Matt Carlson4f125f42009-09-01 12:55:02 +00009477 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +00009478
Michael Chan79381092005-04-21 17:13:59 -07009479 pci_disable_msi(tp->pdev);
9480
Joe Perches63c3a662011-04-26 08:12:10 +00009481 tg3_flag_clear(tp, USING_MSI);
Andre Detschdc8bf1b2010-04-26 07:27:07 +00009482 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -07009483
Matt Carlson4f125f42009-09-01 12:55:02 +00009484 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009485 if (err)
9486 return err;
9487
9488 /* Need to reset the chip because the MSI cycle may have terminated
9489 * with Master Abort.
9490 */
David S. Millerf47c11e2005-06-24 20:18:35 -07009491 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009492
Michael Chan944d9802005-05-29 14:57:48 -07009493 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009494 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009495
David S. Millerf47c11e2005-06-24 20:18:35 -07009496 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009497
9498 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +00009499 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -07009500
9501 return err;
9502}
9503
Matt Carlson9e9fd122009-01-19 16:57:45 -08009504static int tg3_request_firmware(struct tg3 *tp)
9505{
9506 const __be32 *fw_data;
9507
9508 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009509 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
9510 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009511 return -ENOENT;
9512 }
9513
9514 fw_data = (void *)tp->fw->data;
9515
9516 /* Firmware blob starts with version numbers, followed by
9517 * start address and _full_ length including BSS sections
9518 * (which must be longer than the actual data, of course
9519 */
9520
9521 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
9522 if (tp->fw_len < (tp->fw->size - 12)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009523 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
9524 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009525 release_firmware(tp->fw);
9526 tp->fw = NULL;
9527 return -EINVAL;
9528 }
9529
9530 /* We no longer need firmware; we have it. */
9531 tp->fw_needed = NULL;
9532 return 0;
9533}
9534
Matt Carlson679563f2009-09-01 12:55:46 +00009535static bool tg3_enable_msix(struct tg3 *tp)
9536{
9537 int i, rc, cpus = num_online_cpus();
9538 struct msix_entry msix_ent[tp->irq_max];
9539
9540 if (cpus == 1)
9541 /* Just fallback to the simpler MSI mode. */
9542 return false;
9543
9544 /*
9545 * We want as many rx rings enabled as there are cpus.
9546 * The first MSIX vector only deals with link interrupts, etc,
9547 * so we add one to the number of vectors we are requesting.
9548 */
9549 tp->irq_cnt = min_t(unsigned, cpus + 1, tp->irq_max);
9550
9551 for (i = 0; i < tp->irq_max; i++) {
9552 msix_ent[i].entry = i;
9553 msix_ent[i].vector = 0;
9554 }
9555
9556 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +00009557 if (rc < 0) {
9558 return false;
9559 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +00009560 if (pci_enable_msix(tp->pdev, msix_ent, rc))
9561 return false;
Joe Perches05dbe002010-02-17 19:44:19 +00009562 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
9563 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +00009564 tp->irq_cnt = rc;
9565 }
9566
9567 for (i = 0; i < tp->irq_max; i++)
9568 tp->napi[i].irq_vec = msix_ent[i].vector;
9569
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009570 netif_set_real_num_tx_queues(tp->dev, 1);
9571 rc = tp->irq_cnt > 1 ? tp->irq_cnt - 1 : 1;
9572 if (netif_set_real_num_rx_queues(tp->dev, rc)) {
9573 pci_disable_msix(tp->pdev);
9574 return false;
9575 }
Matt Carlsonb92b9042010-11-24 08:31:51 +00009576
9577 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +00009578 tg3_flag_set(tp, ENABLE_RSS);
Matt Carlsond78b59f2011-04-05 14:22:46 +00009579
9580 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
9581 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Joe Perches63c3a662011-04-26 08:12:10 +00009582 tg3_flag_set(tp, ENABLE_TSS);
Matt Carlsonb92b9042010-11-24 08:31:51 +00009583 netif_set_real_num_tx_queues(tp->dev, tp->irq_cnt - 1);
9584 }
9585 }
Matt Carlson2430b032010-06-05 17:24:34 +00009586
Matt Carlson679563f2009-09-01 12:55:46 +00009587 return true;
9588}
9589
Matt Carlson07b01732009-08-28 14:01:15 +00009590static void tg3_ints_init(struct tg3 *tp)
9591{
Joe Perches63c3a662011-04-26 08:12:10 +00009592 if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
9593 !tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +00009594 /* All MSI supporting chips should support tagged
9595 * status. Assert that this is the case.
9596 */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009597 netdev_warn(tp->dev,
9598 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +00009599 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +00009600 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009601
Joe Perches63c3a662011-04-26 08:12:10 +00009602 if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
9603 tg3_flag_set(tp, USING_MSIX);
9604 else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
9605 tg3_flag_set(tp, USING_MSI);
Matt Carlson679563f2009-09-01 12:55:46 +00009606
Joe Perches63c3a662011-04-26 08:12:10 +00009607 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009608 u32 msi_mode = tr32(MSGINT_MODE);
Joe Perches63c3a662011-04-26 08:12:10 +00009609 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009610 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +00009611 if (!tg3_flag(tp, 1SHOT_MSI))
9612 msi_mode |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlson679563f2009-09-01 12:55:46 +00009613 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
9614 }
9615defcfg:
Joe Perches63c3a662011-04-26 08:12:10 +00009616 if (!tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009617 tp->irq_cnt = 1;
9618 tp->napi[0].irq_vec = tp->pdev->irq;
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009619 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -07009620 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +00009621 }
Matt Carlson07b01732009-08-28 14:01:15 +00009622}
9623
9624static void tg3_ints_fini(struct tg3 *tp)
9625{
Joe Perches63c3a662011-04-26 08:12:10 +00009626 if (tg3_flag(tp, USING_MSIX))
Matt Carlson679563f2009-09-01 12:55:46 +00009627 pci_disable_msix(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009628 else if (tg3_flag(tp, USING_MSI))
Matt Carlson679563f2009-09-01 12:55:46 +00009629 pci_disable_msi(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009630 tg3_flag_clear(tp, USING_MSI);
9631 tg3_flag_clear(tp, USING_MSIX);
9632 tg3_flag_clear(tp, ENABLE_RSS);
9633 tg3_flag_clear(tp, ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +00009634}
9635
Linus Torvalds1da177e2005-04-16 15:20:36 -07009636static int tg3_open(struct net_device *dev)
9637{
9638 struct tg3 *tp = netdev_priv(dev);
Matt Carlson4f125f42009-09-01 12:55:02 +00009639 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009640
Matt Carlson9e9fd122009-01-19 16:57:45 -08009641 if (tp->fw_needed) {
9642 err = tg3_request_firmware(tp);
9643 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
9644 if (err)
9645 return err;
9646 } else if (err) {
Joe Perches05dbe002010-02-17 19:44:19 +00009647 netdev_warn(tp->dev, "TSO capability disabled\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009648 tg3_flag_clear(tp, TSO_CAPABLE);
9649 } else if (!tg3_flag(tp, TSO_CAPABLE)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009650 netdev_notice(tp->dev, "TSO capability restored\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009651 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009652 }
9653 }
9654
Michael Chanc49a1562006-12-17 17:07:29 -08009655 netif_carrier_off(tp->dev);
9656
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009657 err = tg3_power_up(tp);
Matt Carlson2f751b62008-08-04 23:17:34 -07009658 if (err)
Michael Chanbc1c7562006-03-20 17:48:03 -08009659 return err;
Matt Carlson2f751b62008-08-04 23:17:34 -07009660
9661 tg3_full_lock(tp, 0);
Michael Chanbc1c7562006-03-20 17:48:03 -08009662
Linus Torvalds1da177e2005-04-16 15:20:36 -07009663 tg3_disable_ints(tp);
Joe Perches63c3a662011-04-26 08:12:10 +00009664 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009665
David S. Millerf47c11e2005-06-24 20:18:35 -07009666 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009667
Matt Carlson679563f2009-09-01 12:55:46 +00009668 /*
9669 * Setup interrupts first so we know how
9670 * many NAPI resources to allocate
9671 */
9672 tg3_ints_init(tp);
9673
Linus Torvalds1da177e2005-04-16 15:20:36 -07009674 /* The placement of this call is tied
9675 * to the setup and use of Host TX descriptors.
9676 */
9677 err = tg3_alloc_consistent(tp);
9678 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009679 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009680
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009681 tg3_napi_init(tp);
9682
Matt Carlsonfed97812009-09-01 13:10:19 +00009683 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07009684
Matt Carlson4f125f42009-09-01 12:55:02 +00009685 for (i = 0; i < tp->irq_cnt; i++) {
9686 struct tg3_napi *tnapi = &tp->napi[i];
9687 err = tg3_request_irq(tp, i);
9688 if (err) {
Matt Carlson5bc09182011-11-04 09:15:01 +00009689 for (i--; i >= 0; i--) {
9690 tnapi = &tp->napi[i];
Matt Carlson4f125f42009-09-01 12:55:02 +00009691 free_irq(tnapi->irq_vec, tnapi);
Matt Carlson5bc09182011-11-04 09:15:01 +00009692 }
9693 goto err_out2;
Matt Carlson4f125f42009-09-01 12:55:02 +00009694 }
9695 }
Matt Carlson07b01732009-08-28 14:01:15 +00009696
David S. Millerf47c11e2005-06-24 20:18:35 -07009697 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009698
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009699 err = tg3_init_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009700 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -07009701 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009702 tg3_free_rings(tp);
9703 } else {
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009704 if (tg3_flag(tp, TAGGED_STATUS) &&
9705 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
9706 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57765)
David S. Millerfac9b832005-05-18 22:46:34 -07009707 tp->timer_offset = HZ;
9708 else
9709 tp->timer_offset = HZ / 10;
9710
9711 BUG_ON(tp->timer_offset > HZ);
9712 tp->timer_counter = tp->timer_multiplier =
9713 (HZ / tp->timer_offset);
9714 tp->asf_counter = tp->asf_multiplier =
Michael Chan28fbef72005-10-26 15:48:35 -07009715 ((HZ / tp->timer_offset) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009716
9717 init_timer(&tp->timer);
9718 tp->timer.expires = jiffies + tp->timer_offset;
9719 tp->timer.data = (unsigned long) tp;
9720 tp->timer.function = tg3_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009721 }
9722
David S. Millerf47c11e2005-06-24 20:18:35 -07009723 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009724
Matt Carlson07b01732009-08-28 14:01:15 +00009725 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009726 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009727
Joe Perches63c3a662011-04-26 08:12:10 +00009728 if (tg3_flag(tp, USING_MSI)) {
Michael Chan79381092005-04-21 17:13:59 -07009729 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -07009730
Michael Chan79381092005-04-21 17:13:59 -07009731 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -07009732 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -07009733 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -07009734 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07009735 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009736
Matt Carlson679563f2009-09-01 12:55:46 +00009737 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -07009738 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009739
Joe Perches63c3a662011-04-26 08:12:10 +00009740 if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009741 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009742
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009743 tw32(PCIE_TRANSACTION_CFG,
9744 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009745 }
Michael Chan79381092005-04-21 17:13:59 -07009746 }
9747
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009748 tg3_phy_start(tp);
9749
David S. Millerf47c11e2005-06-24 20:18:35 -07009750 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009751
Michael Chan79381092005-04-21 17:13:59 -07009752 add_timer(&tp->timer);
Joe Perches63c3a662011-04-26 08:12:10 +00009753 tg3_flag_set(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009754 tg3_enable_ints(tp);
9755
David S. Millerf47c11e2005-06-24 20:18:35 -07009756 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009757
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009758 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009759
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00009760 /*
9761 * Reset loopback feature if it was turned on while the device was down
9762 * make sure that it's installed properly now.
9763 */
9764 if (dev->features & NETIF_F_LOOPBACK)
9765 tg3_set_loopback(dev, dev->features);
9766
Linus Torvalds1da177e2005-04-16 15:20:36 -07009767 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +00009768
Matt Carlson679563f2009-09-01 12:55:46 +00009769err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +00009770 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9771 struct tg3_napi *tnapi = &tp->napi[i];
9772 free_irq(tnapi->irq_vec, tnapi);
9773 }
Matt Carlson07b01732009-08-28 14:01:15 +00009774
Matt Carlson679563f2009-09-01 12:55:46 +00009775err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +00009776 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009777 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00009778 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +00009779
9780err_out1:
9781 tg3_ints_fini(tp);
Matt Carlsoncd0d7222011-07-13 09:27:33 +00009782 tg3_frob_aux_power(tp, false);
9783 pci_set_power_state(tp->pdev, PCI_D3hot);
Matt Carlson07b01732009-08-28 14:01:15 +00009784 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009785}
9786
Linus Torvalds1da177e2005-04-16 15:20:36 -07009787static int tg3_close(struct net_device *dev)
9788{
Matt Carlson4f125f42009-09-01 12:55:02 +00009789 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009790 struct tg3 *tp = netdev_priv(dev);
9791
Matt Carlsonfed97812009-09-01 13:10:19 +00009792 tg3_napi_disable(tp);
Matt Carlsondb219972011-11-04 09:15:03 +00009793 tg3_reset_task_cancel(tp);
Michael Chan7faa0062006-02-02 17:29:28 -08009794
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009795 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009796
9797 del_timer_sync(&tp->timer);
9798
Matt Carlson24bb4fb2009-10-05 17:55:29 +00009799 tg3_phy_stop(tp);
9800
David S. Millerf47c11e2005-06-24 20:18:35 -07009801 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009802
9803 tg3_disable_ints(tp);
9804
Michael Chan944d9802005-05-29 14:57:48 -07009805 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009806 tg3_free_rings(tp);
Joe Perches63c3a662011-04-26 08:12:10 +00009807 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009808
David S. Millerf47c11e2005-06-24 20:18:35 -07009809 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009810
Matt Carlson4f125f42009-09-01 12:55:02 +00009811 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9812 struct tg3_napi *tnapi = &tp->napi[i];
9813 free_irq(tnapi->irq_vec, tnapi);
9814 }
Matt Carlson07b01732009-08-28 14:01:15 +00009815
9816 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009817
Matt Carlson92feeab2011-12-08 14:40:14 +00009818 /* Clear stats across close / open calls */
9819 memset(&tp->net_stats_prev, 0, sizeof(tp->net_stats_prev));
9820 memset(&tp->estats_prev, 0, sizeof(tp->estats_prev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07009821
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009822 tg3_napi_fini(tp);
9823
Linus Torvalds1da177e2005-04-16 15:20:36 -07009824 tg3_free_consistent(tp);
9825
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009826 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -08009827
9828 netif_carrier_off(tp->dev);
9829
Linus Torvalds1da177e2005-04-16 15:20:36 -07009830 return 0;
9831}
9832
Eric Dumazet511d2222010-07-07 20:44:24 +00009833static inline u64 get_stat64(tg3_stat64_t *val)
Stefan Buehler816f8b82008-08-15 14:10:54 -07009834{
9835 return ((u64)val->high << 32) | ((u64)val->low);
9836}
9837
Eric Dumazet511d2222010-07-07 20:44:24 +00009838static u64 calc_crc_errors(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009839{
9840 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9841
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009842 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009843 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
9844 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009845 u32 val;
9846
David S. Millerf47c11e2005-06-24 20:18:35 -07009847 spin_lock_bh(&tp->lock);
Michael Chan569a5df2007-02-13 12:18:15 -08009848 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
9849 tg3_writephy(tp, MII_TG3_TEST1,
9850 val | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009851 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009852 } else
9853 val = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07009854 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009855
9856 tp->phy_crc_errors += val;
9857
9858 return tp->phy_crc_errors;
9859 }
9860
9861 return get_stat64(&hw_stats->rx_fcs_errors);
9862}
9863
9864#define ESTAT_ADD(member) \
9865 estats->member = old_estats->member + \
Eric Dumazet511d2222010-07-07 20:44:24 +00009866 get_stat64(&hw_stats->member)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009867
Matt Carlson0e6c9da2011-12-08 14:40:13 +00009868static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *tp,
9869 struct tg3_ethtool_stats *estats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009870{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009871 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
9872 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9873
9874 if (!hw_stats)
9875 return old_estats;
9876
9877 ESTAT_ADD(rx_octets);
9878 ESTAT_ADD(rx_fragments);
9879 ESTAT_ADD(rx_ucast_packets);
9880 ESTAT_ADD(rx_mcast_packets);
9881 ESTAT_ADD(rx_bcast_packets);
9882 ESTAT_ADD(rx_fcs_errors);
9883 ESTAT_ADD(rx_align_errors);
9884 ESTAT_ADD(rx_xon_pause_rcvd);
9885 ESTAT_ADD(rx_xoff_pause_rcvd);
9886 ESTAT_ADD(rx_mac_ctrl_rcvd);
9887 ESTAT_ADD(rx_xoff_entered);
9888 ESTAT_ADD(rx_frame_too_long_errors);
9889 ESTAT_ADD(rx_jabbers);
9890 ESTAT_ADD(rx_undersize_packets);
9891 ESTAT_ADD(rx_in_length_errors);
9892 ESTAT_ADD(rx_out_length_errors);
9893 ESTAT_ADD(rx_64_or_less_octet_packets);
9894 ESTAT_ADD(rx_65_to_127_octet_packets);
9895 ESTAT_ADD(rx_128_to_255_octet_packets);
9896 ESTAT_ADD(rx_256_to_511_octet_packets);
9897 ESTAT_ADD(rx_512_to_1023_octet_packets);
9898 ESTAT_ADD(rx_1024_to_1522_octet_packets);
9899 ESTAT_ADD(rx_1523_to_2047_octet_packets);
9900 ESTAT_ADD(rx_2048_to_4095_octet_packets);
9901 ESTAT_ADD(rx_4096_to_8191_octet_packets);
9902 ESTAT_ADD(rx_8192_to_9022_octet_packets);
9903
9904 ESTAT_ADD(tx_octets);
9905 ESTAT_ADD(tx_collisions);
9906 ESTAT_ADD(tx_xon_sent);
9907 ESTAT_ADD(tx_xoff_sent);
9908 ESTAT_ADD(tx_flow_control);
9909 ESTAT_ADD(tx_mac_errors);
9910 ESTAT_ADD(tx_single_collisions);
9911 ESTAT_ADD(tx_mult_collisions);
9912 ESTAT_ADD(tx_deferred);
9913 ESTAT_ADD(tx_excessive_collisions);
9914 ESTAT_ADD(tx_late_collisions);
9915 ESTAT_ADD(tx_collide_2times);
9916 ESTAT_ADD(tx_collide_3times);
9917 ESTAT_ADD(tx_collide_4times);
9918 ESTAT_ADD(tx_collide_5times);
9919 ESTAT_ADD(tx_collide_6times);
9920 ESTAT_ADD(tx_collide_7times);
9921 ESTAT_ADD(tx_collide_8times);
9922 ESTAT_ADD(tx_collide_9times);
9923 ESTAT_ADD(tx_collide_10times);
9924 ESTAT_ADD(tx_collide_11times);
9925 ESTAT_ADD(tx_collide_12times);
9926 ESTAT_ADD(tx_collide_13times);
9927 ESTAT_ADD(tx_collide_14times);
9928 ESTAT_ADD(tx_collide_15times);
9929 ESTAT_ADD(tx_ucast_packets);
9930 ESTAT_ADD(tx_mcast_packets);
9931 ESTAT_ADD(tx_bcast_packets);
9932 ESTAT_ADD(tx_carrier_sense_errors);
9933 ESTAT_ADD(tx_discards);
9934 ESTAT_ADD(tx_errors);
9935
9936 ESTAT_ADD(dma_writeq_full);
9937 ESTAT_ADD(dma_write_prioq_full);
9938 ESTAT_ADD(rxbds_empty);
9939 ESTAT_ADD(rx_discards);
9940 ESTAT_ADD(rx_errors);
9941 ESTAT_ADD(rx_threshold_hit);
9942
9943 ESTAT_ADD(dma_readq_full);
9944 ESTAT_ADD(dma_read_prioq_full);
9945 ESTAT_ADD(tx_comp_queue_full);
9946
9947 ESTAT_ADD(ring_set_send_prod_index);
9948 ESTAT_ADD(ring_status_update);
9949 ESTAT_ADD(nic_irqs);
9950 ESTAT_ADD(nic_avoided_irqs);
9951 ESTAT_ADD(nic_tx_threshold_hit);
9952
Matt Carlson4452d092011-05-19 12:12:51 +00009953 ESTAT_ADD(mbuf_lwm_thresh_hit);
9954
Linus Torvalds1da177e2005-04-16 15:20:36 -07009955 return estats;
9956}
9957
Eric Dumazet511d2222010-07-07 20:44:24 +00009958static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
9959 struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009960{
9961 struct tg3 *tp = netdev_priv(dev);
Eric Dumazet511d2222010-07-07 20:44:24 +00009962 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009963 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9964
9965 if (!hw_stats)
9966 return old_stats;
9967
9968 stats->rx_packets = old_stats->rx_packets +
9969 get_stat64(&hw_stats->rx_ucast_packets) +
9970 get_stat64(&hw_stats->rx_mcast_packets) +
9971 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009972
Linus Torvalds1da177e2005-04-16 15:20:36 -07009973 stats->tx_packets = old_stats->tx_packets +
9974 get_stat64(&hw_stats->tx_ucast_packets) +
9975 get_stat64(&hw_stats->tx_mcast_packets) +
9976 get_stat64(&hw_stats->tx_bcast_packets);
9977
9978 stats->rx_bytes = old_stats->rx_bytes +
9979 get_stat64(&hw_stats->rx_octets);
9980 stats->tx_bytes = old_stats->tx_bytes +
9981 get_stat64(&hw_stats->tx_octets);
9982
9983 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -07009984 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009985 stats->tx_errors = old_stats->tx_errors +
9986 get_stat64(&hw_stats->tx_errors) +
9987 get_stat64(&hw_stats->tx_mac_errors) +
9988 get_stat64(&hw_stats->tx_carrier_sense_errors) +
9989 get_stat64(&hw_stats->tx_discards);
9990
9991 stats->multicast = old_stats->multicast +
9992 get_stat64(&hw_stats->rx_mcast_packets);
9993 stats->collisions = old_stats->collisions +
9994 get_stat64(&hw_stats->tx_collisions);
9995
9996 stats->rx_length_errors = old_stats->rx_length_errors +
9997 get_stat64(&hw_stats->rx_frame_too_long_errors) +
9998 get_stat64(&hw_stats->rx_undersize_packets);
9999
10000 stats->rx_over_errors = old_stats->rx_over_errors +
10001 get_stat64(&hw_stats->rxbds_empty);
10002 stats->rx_frame_errors = old_stats->rx_frame_errors +
10003 get_stat64(&hw_stats->rx_align_errors);
10004 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
10005 get_stat64(&hw_stats->tx_discards);
10006 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
10007 get_stat64(&hw_stats->tx_carrier_sense_errors);
10008
10009 stats->rx_crc_errors = old_stats->rx_crc_errors +
10010 calc_crc_errors(tp);
10011
John W. Linville4f63b872005-09-12 14:43:18 -070010012 stats->rx_missed_errors = old_stats->rx_missed_errors +
10013 get_stat64(&hw_stats->rx_discards);
10014
Eric Dumazetb0057c52010-10-10 19:55:52 +000010015 stats->rx_dropped = tp->rx_dropped;
Eric Dumazet48855432011-10-24 07:53:03 +000010016 stats->tx_dropped = tp->tx_dropped;
Eric Dumazetb0057c52010-10-10 19:55:52 +000010017
Linus Torvalds1da177e2005-04-16 15:20:36 -070010018 return stats;
10019}
10020
10021static inline u32 calc_crc(unsigned char *buf, int len)
10022{
10023 u32 reg;
10024 u32 tmp;
10025 int j, k;
10026
10027 reg = 0xffffffff;
10028
10029 for (j = 0; j < len; j++) {
10030 reg ^= buf[j];
10031
10032 for (k = 0; k < 8; k++) {
10033 tmp = reg & 0x01;
10034
10035 reg >>= 1;
10036
Matt Carlson859a5882010-04-05 10:19:28 +000010037 if (tmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010038 reg ^= 0xedb88320;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010039 }
10040 }
10041
10042 return ~reg;
10043}
10044
10045static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
10046{
10047 /* accept or reject all multicast frames */
10048 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
10049 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
10050 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
10051 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
10052}
10053
10054static void __tg3_set_rx_mode(struct net_device *dev)
10055{
10056 struct tg3 *tp = netdev_priv(dev);
10057 u32 rx_mode;
10058
10059 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
10060 RX_MODE_KEEP_VLAN_TAG);
10061
Matt Carlsonbf933c82011-01-25 15:58:49 +000010062#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010063 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
10064 * flag clear.
10065 */
Joe Perches63c3a662011-04-26 08:12:10 +000010066 if (!tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010067 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
10068#endif
10069
10070 if (dev->flags & IFF_PROMISC) {
10071 /* Promiscuous mode. */
10072 rx_mode |= RX_MODE_PROMISC;
10073 } else if (dev->flags & IFF_ALLMULTI) {
10074 /* Accept all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010075 tg3_set_multi(tp, 1);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +000010076 } else if (netdev_mc_empty(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010077 /* Reject all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010078 tg3_set_multi(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010079 } else {
10080 /* Accept one or more multicast(s). */
Jiri Pirko22bedad2010-04-01 21:22:57 +000010081 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010082 u32 mc_filter[4] = { 0, };
10083 u32 regidx;
10084 u32 bit;
10085 u32 crc;
10086
Jiri Pirko22bedad2010-04-01 21:22:57 +000010087 netdev_for_each_mc_addr(ha, dev) {
10088 crc = calc_crc(ha->addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010089 bit = ~crc & 0x7f;
10090 regidx = (bit & 0x60) >> 5;
10091 bit &= 0x1f;
10092 mc_filter[regidx] |= (1 << bit);
10093 }
10094
10095 tw32(MAC_HASH_REG_0, mc_filter[0]);
10096 tw32(MAC_HASH_REG_1, mc_filter[1]);
10097 tw32(MAC_HASH_REG_2, mc_filter[2]);
10098 tw32(MAC_HASH_REG_3, mc_filter[3]);
10099 }
10100
10101 if (rx_mode != tp->rx_mode) {
10102 tp->rx_mode = rx_mode;
10103 tw32_f(MAC_RX_MODE, rx_mode);
10104 udelay(10);
10105 }
10106}
10107
10108static void tg3_set_rx_mode(struct net_device *dev)
10109{
10110 struct tg3 *tp = netdev_priv(dev);
10111
Michael Chane75f7c92006-03-20 21:33:26 -080010112 if (!netif_running(dev))
10113 return;
10114
David S. Millerf47c11e2005-06-24 20:18:35 -070010115 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010116 __tg3_set_rx_mode(dev);
David S. Millerf47c11e2005-06-24 20:18:35 -070010117 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010118}
10119
Linus Torvalds1da177e2005-04-16 15:20:36 -070010120static int tg3_get_regs_len(struct net_device *dev)
10121{
Matt Carlson97bd8e42011-04-13 11:05:04 +000010122 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010123}
10124
10125static void tg3_get_regs(struct net_device *dev,
10126 struct ethtool_regs *regs, void *_p)
10127{
Linus Torvalds1da177e2005-04-16 15:20:36 -070010128 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010129
10130 regs->version = 0;
10131
Matt Carlson97bd8e42011-04-13 11:05:04 +000010132 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010133
Matt Carlson80096062010-08-02 11:26:06 +000010134 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010135 return;
10136
David S. Millerf47c11e2005-06-24 20:18:35 -070010137 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010138
Matt Carlson97bd8e42011-04-13 11:05:04 +000010139 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010140
David S. Millerf47c11e2005-06-24 20:18:35 -070010141 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010142}
10143
10144static int tg3_get_eeprom_len(struct net_device *dev)
10145{
10146 struct tg3 *tp = netdev_priv(dev);
10147
10148 return tp->nvram_size;
10149}
10150
Linus Torvalds1da177e2005-04-16 15:20:36 -070010151static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
10152{
10153 struct tg3 *tp = netdev_priv(dev);
10154 int ret;
10155 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -080010156 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010157 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010158
Joe Perches63c3a662011-04-26 08:12:10 +000010159 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000010160 return -EINVAL;
10161
Matt Carlson80096062010-08-02 11:26:06 +000010162 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010163 return -EAGAIN;
10164
Linus Torvalds1da177e2005-04-16 15:20:36 -070010165 offset = eeprom->offset;
10166 len = eeprom->len;
10167 eeprom->len = 0;
10168
10169 eeprom->magic = TG3_EEPROM_MAGIC;
10170
10171 if (offset & 3) {
10172 /* adjustments to start on required 4 byte boundary */
10173 b_offset = offset & 3;
10174 b_count = 4 - b_offset;
10175 if (b_count > len) {
10176 /* i.e. offset=1 len=2 */
10177 b_count = len;
10178 }
Matt Carlsona9dc5292009-02-25 14:25:30 +000010179 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010180 if (ret)
10181 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +000010182 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010183 len -= b_count;
10184 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010185 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010186 }
10187
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010188 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010189 pd = &data[eeprom->len];
10190 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010191 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010192 if (ret) {
10193 eeprom->len += i;
10194 return ret;
10195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010196 memcpy(pd + i, &val, 4);
10197 }
10198 eeprom->len += i;
10199
10200 if (len & 3) {
10201 /* read last bytes not ending on 4 byte boundary */
10202 pd = &data[eeprom->len];
10203 b_count = len & 3;
10204 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010205 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010206 if (ret)
10207 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080010208 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010209 eeprom->len += b_count;
10210 }
10211 return 0;
10212}
10213
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010214static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010215
10216static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
10217{
10218 struct tg3 *tp = netdev_priv(dev);
10219 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080010220 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010221 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010222 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010223
Matt Carlson80096062010-08-02 11:26:06 +000010224 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010225 return -EAGAIN;
10226
Joe Perches63c3a662011-04-26 08:12:10 +000010227 if (tg3_flag(tp, NO_NVRAM) ||
Matt Carlsondf259d82009-04-20 06:57:14 +000010228 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010229 return -EINVAL;
10230
10231 offset = eeprom->offset;
10232 len = eeprom->len;
10233
10234 if ((b_offset = (offset & 3))) {
10235 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010236 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010237 if (ret)
10238 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010239 len += b_offset;
10240 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -070010241 if (len < 4)
10242 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010243 }
10244
10245 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -070010246 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010247 /* adjustments to end on required 4 byte boundary */
10248 odd_len = 1;
10249 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010250 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010251 if (ret)
10252 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010253 }
10254
10255 buf = data;
10256 if (b_offset || odd_len) {
10257 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010258 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010259 return -ENOMEM;
10260 if (b_offset)
10261 memcpy(buf, &start, 4);
10262 if (odd_len)
10263 memcpy(buf+len-4, &end, 4);
10264 memcpy(buf + b_offset, data, eeprom->len);
10265 }
10266
10267 ret = tg3_nvram_write_block(tp, offset, len, buf);
10268
10269 if (buf != data)
10270 kfree(buf);
10271
10272 return ret;
10273}
10274
10275static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
10276{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010277 struct tg3 *tp = netdev_priv(dev);
10278
Joe Perches63c3a662011-04-26 08:12:10 +000010279 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010280 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010281 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010282 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010283 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10284 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010285 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010286
Linus Torvalds1da177e2005-04-16 15:20:36 -070010287 cmd->supported = (SUPPORTED_Autoneg);
10288
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010289 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010290 cmd->supported |= (SUPPORTED_1000baseT_Half |
10291 SUPPORTED_1000baseT_Full);
10292
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010293 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010294 cmd->supported |= (SUPPORTED_100baseT_Half |
10295 SUPPORTED_100baseT_Full |
10296 SUPPORTED_10baseT_Half |
10297 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -080010298 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -070010299 cmd->port = PORT_TP;
10300 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010301 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -070010302 cmd->port = PORT_FIBRE;
10303 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010304
Linus Torvalds1da177e2005-04-16 15:20:36 -070010305 cmd->advertising = tp->link_config.advertising;
Matt Carlson5bb09772011-06-13 13:39:00 +000010306 if (tg3_flag(tp, PAUSE_AUTONEG)) {
10307 if (tp->link_config.flowctrl & FLOW_CTRL_RX) {
10308 if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
10309 cmd->advertising |= ADVERTISED_Pause;
10310 } else {
10311 cmd->advertising |= ADVERTISED_Pause |
10312 ADVERTISED_Asym_Pause;
10313 }
10314 } else if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
10315 cmd->advertising |= ADVERTISED_Asym_Pause;
10316 }
10317 }
Matt Carlson859edb22011-12-08 14:40:16 +000010318 if (netif_running(dev) && netif_carrier_ok(dev)) {
David Decotigny70739492011-04-27 18:32:40 +000010319 ethtool_cmd_speed_set(cmd, tp->link_config.active_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010320 cmd->duplex = tp->link_config.active_duplex;
Matt Carlson859edb22011-12-08 14:40:16 +000010321 cmd->lp_advertising = tp->link_config.rmt_adv;
Matt Carlsone348c5e2011-11-21 15:01:20 +000010322 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
10323 if (tp->phy_flags & TG3_PHYFLG_MDIX_STATE)
10324 cmd->eth_tp_mdix = ETH_TP_MDI_X;
10325 else
10326 cmd->eth_tp_mdix = ETH_TP_MDI;
10327 }
Matt Carlson64c22182010-10-14 10:37:44 +000010328 } else {
David Decotigny70739492011-04-27 18:32:40 +000010329 ethtool_cmd_speed_set(cmd, SPEED_INVALID);
Matt Carlson64c22182010-10-14 10:37:44 +000010330 cmd->duplex = DUPLEX_INVALID;
Matt Carlsone348c5e2011-11-21 15:01:20 +000010331 cmd->eth_tp_mdix = ETH_TP_MDI_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010332 }
Matt Carlson882e9792009-09-01 13:21:36 +000010333 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +000010334 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010335 cmd->autoneg = tp->link_config.autoneg;
10336 cmd->maxtxpkt = 0;
10337 cmd->maxrxpkt = 0;
10338 return 0;
10339}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010340
Linus Torvalds1da177e2005-04-16 15:20:36 -070010341static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
10342{
10343 struct tg3 *tp = netdev_priv(dev);
David Decotigny25db0332011-04-27 18:32:39 +000010344 u32 speed = ethtool_cmd_speed(cmd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010345
Joe Perches63c3a662011-04-26 08:12:10 +000010346 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010347 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010348 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010349 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010350 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10351 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010352 }
10353
Matt Carlson7e5856b2009-02-25 14:23:01 +000010354 if (cmd->autoneg != AUTONEG_ENABLE &&
10355 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -070010356 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +000010357
10358 if (cmd->autoneg == AUTONEG_DISABLE &&
10359 cmd->duplex != DUPLEX_FULL &&
10360 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -070010361 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010362
Matt Carlson7e5856b2009-02-25 14:23:01 +000010363 if (cmd->autoneg == AUTONEG_ENABLE) {
10364 u32 mask = ADVERTISED_Autoneg |
10365 ADVERTISED_Pause |
10366 ADVERTISED_Asym_Pause;
10367
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010368 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010369 mask |= ADVERTISED_1000baseT_Half |
10370 ADVERTISED_1000baseT_Full;
10371
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010372 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010373 mask |= ADVERTISED_100baseT_Half |
10374 ADVERTISED_100baseT_Full |
10375 ADVERTISED_10baseT_Half |
10376 ADVERTISED_10baseT_Full |
10377 ADVERTISED_TP;
10378 else
10379 mask |= ADVERTISED_FIBRE;
10380
10381 if (cmd->advertising & ~mask)
10382 return -EINVAL;
10383
10384 mask &= (ADVERTISED_1000baseT_Half |
10385 ADVERTISED_1000baseT_Full |
10386 ADVERTISED_100baseT_Half |
10387 ADVERTISED_100baseT_Full |
10388 ADVERTISED_10baseT_Half |
10389 ADVERTISED_10baseT_Full);
10390
10391 cmd->advertising &= mask;
10392 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010393 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
David Decotigny25db0332011-04-27 18:32:39 +000010394 if (speed != SPEED_1000)
Matt Carlson7e5856b2009-02-25 14:23:01 +000010395 return -EINVAL;
10396
10397 if (cmd->duplex != DUPLEX_FULL)
10398 return -EINVAL;
10399 } else {
David Decotigny25db0332011-04-27 18:32:39 +000010400 if (speed != SPEED_100 &&
10401 speed != SPEED_10)
Matt Carlson7e5856b2009-02-25 14:23:01 +000010402 return -EINVAL;
10403 }
10404 }
10405
David S. Millerf47c11e2005-06-24 20:18:35 -070010406 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010407
10408 tp->link_config.autoneg = cmd->autoneg;
10409 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -070010410 tp->link_config.advertising = (cmd->advertising |
10411 ADVERTISED_Autoneg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010412 tp->link_config.speed = SPEED_INVALID;
10413 tp->link_config.duplex = DUPLEX_INVALID;
10414 } else {
10415 tp->link_config.advertising = 0;
David Decotigny25db0332011-04-27 18:32:39 +000010416 tp->link_config.speed = speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010417 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010418 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010419
Michael Chan24fcad62006-12-17 17:06:46 -080010420 tp->link_config.orig_speed = tp->link_config.speed;
10421 tp->link_config.orig_duplex = tp->link_config.duplex;
10422 tp->link_config.orig_autoneg = tp->link_config.autoneg;
10423
Linus Torvalds1da177e2005-04-16 15:20:36 -070010424 if (netif_running(dev))
10425 tg3_setup_phy(tp, 1);
10426
David S. Millerf47c11e2005-06-24 20:18:35 -070010427 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010428
Linus Torvalds1da177e2005-04-16 15:20:36 -070010429 return 0;
10430}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010431
Linus Torvalds1da177e2005-04-16 15:20:36 -070010432static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
10433{
10434 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010435
Rick Jones68aad782011-11-07 13:29:27 +000010436 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
10437 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
10438 strlcpy(info->fw_version, tp->fw_ver, sizeof(info->fw_version));
10439 strlcpy(info->bus_info, pci_name(tp->pdev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010440}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010441
Linus Torvalds1da177e2005-04-16 15:20:36 -070010442static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10443{
10444 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010445
Joe Perches63c3a662011-04-26 08:12:10 +000010446 if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070010447 wol->supported = WAKE_MAGIC;
10448 else
10449 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010450 wol->wolopts = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010451 if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010452 wol->wolopts = WAKE_MAGIC;
10453 memset(&wol->sopass, 0, sizeof(wol->sopass));
10454}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010455
Linus Torvalds1da177e2005-04-16 15:20:36 -070010456static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10457{
10458 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070010459 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010460
Linus Torvalds1da177e2005-04-16 15:20:36 -070010461 if (wol->wolopts & ~WAKE_MAGIC)
10462 return -EINVAL;
10463 if ((wol->wolopts & WAKE_MAGIC) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010464 !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010465 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010466
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010467 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
10468
David S. Millerf47c11e2005-06-24 20:18:35 -070010469 spin_lock_bh(&tp->lock);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010470 if (device_may_wakeup(dp))
Joe Perches63c3a662011-04-26 08:12:10 +000010471 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010472 else
Joe Perches63c3a662011-04-26 08:12:10 +000010473 tg3_flag_clear(tp, WOL_ENABLE);
David S. Millerf47c11e2005-06-24 20:18:35 -070010474 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010475
Linus Torvalds1da177e2005-04-16 15:20:36 -070010476 return 0;
10477}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010478
Linus Torvalds1da177e2005-04-16 15:20:36 -070010479static u32 tg3_get_msglevel(struct net_device *dev)
10480{
10481 struct tg3 *tp = netdev_priv(dev);
10482 return tp->msg_enable;
10483}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010484
Linus Torvalds1da177e2005-04-16 15:20:36 -070010485static void tg3_set_msglevel(struct net_device *dev, u32 value)
10486{
10487 struct tg3 *tp = netdev_priv(dev);
10488 tp->msg_enable = value;
10489}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010490
Linus Torvalds1da177e2005-04-16 15:20:36 -070010491static int tg3_nway_reset(struct net_device *dev)
10492{
10493 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010494 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010495
Linus Torvalds1da177e2005-04-16 15:20:36 -070010496 if (!netif_running(dev))
10497 return -EAGAIN;
10498
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010499 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070010500 return -EINVAL;
10501
Joe Perches63c3a662011-04-26 08:12:10 +000010502 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010503 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010504 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010505 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010506 } else {
10507 u32 bmcr;
10508
10509 spin_lock_bh(&tp->lock);
10510 r = -EINVAL;
10511 tg3_readphy(tp, MII_BMCR, &bmcr);
10512 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
10513 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010514 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010515 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
10516 BMCR_ANENABLE);
10517 r = 0;
10518 }
10519 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010520 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010521
Linus Torvalds1da177e2005-04-16 15:20:36 -070010522 return r;
10523}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010524
Linus Torvalds1da177e2005-04-16 15:20:36 -070010525static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10526{
10527 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010528
Matt Carlson2c49a442010-09-30 10:34:35 +000010529 ering->rx_max_pending = tp->rx_std_ring_mask;
Joe Perches63c3a662011-04-26 08:12:10 +000010530 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson2c49a442010-09-30 10:34:35 +000010531 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080010532 else
10533 ering->rx_jumbo_max_pending = 0;
10534
10535 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010536
10537 ering->rx_pending = tp->rx_pending;
Joe Perches63c3a662011-04-26 08:12:10 +000010538 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Michael Chan4f81c322006-03-20 21:33:42 -080010539 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
10540 else
10541 ering->rx_jumbo_pending = 0;
10542
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010543 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010544}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010545
Linus Torvalds1da177e2005-04-16 15:20:36 -070010546static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10547{
10548 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000010549 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010550
Matt Carlson2c49a442010-09-30 10:34:35 +000010551 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
10552 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070010553 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
10554 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Joe Perches63c3a662011-04-26 08:12:10 +000010555 (tg3_flag(tp, TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070010556 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010557 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010558
Michael Chanbbe832c2005-06-24 20:20:04 -070010559 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010560 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010561 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010562 irq_sync = 1;
10563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010564
Michael Chanbbe832c2005-06-24 20:20:04 -070010565 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010566
Linus Torvalds1da177e2005-04-16 15:20:36 -070010567 tp->rx_pending = ering->rx_pending;
10568
Joe Perches63c3a662011-04-26 08:12:10 +000010569 if (tg3_flag(tp, MAX_RXPEND_64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010570 tp->rx_pending > 63)
10571 tp->rx_pending = 63;
10572 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000010573
Matt Carlson6fd45cb2010-09-15 08:59:57 +000010574 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000010575 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010576
10577 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070010578 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -070010579 err = tg3_restart_hw(tp, 1);
10580 if (!err)
10581 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010582 }
10583
David S. Millerf47c11e2005-06-24 20:18:35 -070010584 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010585
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010586 if (irq_sync && !err)
10587 tg3_phy_start(tp);
10588
Michael Chanb9ec6c12006-07-25 16:37:27 -070010589 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010590}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010591
Linus Torvalds1da177e2005-04-16 15:20:36 -070010592static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10593{
10594 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010595
Joe Perches63c3a662011-04-26 08:12:10 +000010596 epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
Matt Carlson8d018622007-12-20 20:05:44 -080010597
Matt Carlson4a2db502011-12-08 14:40:17 +000010598 if (tp->link_config.flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080010599 epause->rx_pause = 1;
10600 else
10601 epause->rx_pause = 0;
10602
Matt Carlson4a2db502011-12-08 14:40:17 +000010603 if (tp->link_config.flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080010604 epause->tx_pause = 1;
10605 else
10606 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010607}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010608
Linus Torvalds1da177e2005-04-16 15:20:36 -070010609static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10610{
10611 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010612 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010613
Joe Perches63c3a662011-04-26 08:12:10 +000010614 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson27121682010-02-17 15:16:57 +000010615 u32 newadv;
10616 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010617
Matt Carlson27121682010-02-17 15:16:57 +000010618 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010619
Matt Carlson27121682010-02-17 15:16:57 +000010620 if (!(phydev->supported & SUPPORTED_Pause) ||
10621 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000010622 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000010623 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010624
Matt Carlson27121682010-02-17 15:16:57 +000010625 tp->link_config.flowctrl = 0;
10626 if (epause->rx_pause) {
10627 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010628
Matt Carlson27121682010-02-17 15:16:57 +000010629 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080010630 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000010631 newadv = ADVERTISED_Pause;
10632 } else
10633 newadv = ADVERTISED_Pause |
10634 ADVERTISED_Asym_Pause;
10635 } else if (epause->tx_pause) {
10636 tp->link_config.flowctrl |= FLOW_CTRL_TX;
10637 newadv = ADVERTISED_Asym_Pause;
10638 } else
10639 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010640
Matt Carlson27121682010-02-17 15:16:57 +000010641 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010642 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010643 else
Joe Perches63c3a662011-04-26 08:12:10 +000010644 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010645
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010646 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000010647 u32 oldadv = phydev->advertising &
10648 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
10649 if (oldadv != newadv) {
10650 phydev->advertising &=
10651 ~(ADVERTISED_Pause |
10652 ADVERTISED_Asym_Pause);
10653 phydev->advertising |= newadv;
10654 if (phydev->autoneg) {
10655 /*
10656 * Always renegotiate the link to
10657 * inform our link partner of our
10658 * flow control settings, even if the
10659 * flow control is forced. Let
10660 * tg3_adjust_link() do the final
10661 * flow control setup.
10662 */
10663 return phy_start_aneg(phydev);
10664 }
10665 }
10666
10667 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010668 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000010669 } else {
10670 tp->link_config.orig_advertising &=
10671 ~(ADVERTISED_Pause |
10672 ADVERTISED_Asym_Pause);
10673 tp->link_config.orig_advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010674 }
10675 } else {
10676 int irq_sync = 0;
10677
10678 if (netif_running(dev)) {
10679 tg3_netif_stop(tp);
10680 irq_sync = 1;
10681 }
10682
10683 tg3_full_lock(tp, irq_sync);
10684
10685 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010686 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010687 else
Joe Perches63c3a662011-04-26 08:12:10 +000010688 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010689 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010690 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010691 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010692 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010693 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010694 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010695 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010696 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010697
10698 if (netif_running(dev)) {
10699 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10700 err = tg3_restart_hw(tp, 1);
10701 if (!err)
10702 tg3_netif_start(tp);
10703 }
10704
10705 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010707
Michael Chanb9ec6c12006-07-25 16:37:27 -070010708 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010709}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010710
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010711static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010712{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010713 switch (sset) {
10714 case ETH_SS_TEST:
10715 return TG3_NUM_TEST;
10716 case ETH_SS_STATS:
10717 return TG3_NUM_STATS;
10718 default:
10719 return -EOPNOTSUPP;
10720 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070010721}
10722
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010723static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010724{
10725 switch (stringset) {
10726 case ETH_SS_STATS:
10727 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
10728 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070010729 case ETH_SS_TEST:
10730 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
10731 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010732 default:
10733 WARN_ON(1); /* we need a WARN() */
10734 break;
10735 }
10736}
10737
stephen hemminger81b87092011-04-04 08:43:50 +000010738static int tg3_set_phys_id(struct net_device *dev,
10739 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070010740{
10741 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070010742
10743 if (!netif_running(tp->dev))
10744 return -EAGAIN;
10745
stephen hemminger81b87092011-04-04 08:43:50 +000010746 switch (state) {
10747 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +000010748 return 1; /* cycle on/off once per second */
Michael Chan4009a932005-09-05 17:52:54 -070010749
stephen hemminger81b87092011-04-04 08:43:50 +000010750 case ETHTOOL_ID_ON:
10751 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10752 LED_CTRL_1000MBPS_ON |
10753 LED_CTRL_100MBPS_ON |
10754 LED_CTRL_10MBPS_ON |
10755 LED_CTRL_TRAFFIC_OVERRIDE |
10756 LED_CTRL_TRAFFIC_BLINK |
10757 LED_CTRL_TRAFFIC_LED);
10758 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010759
stephen hemminger81b87092011-04-04 08:43:50 +000010760 case ETHTOOL_ID_OFF:
10761 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10762 LED_CTRL_TRAFFIC_OVERRIDE);
10763 break;
Michael Chan4009a932005-09-05 17:52:54 -070010764
stephen hemminger81b87092011-04-04 08:43:50 +000010765 case ETHTOOL_ID_INACTIVE:
10766 tw32(MAC_LED_CTRL, tp->led_ctrl);
10767 break;
Michael Chan4009a932005-09-05 17:52:54 -070010768 }
stephen hemminger81b87092011-04-04 08:43:50 +000010769
Michael Chan4009a932005-09-05 17:52:54 -070010770 return 0;
10771}
10772
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010773static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010774 struct ethtool_stats *estats, u64 *tmp_stats)
10775{
10776 struct tg3 *tp = netdev_priv(dev);
Matt Carlson0e6c9da2011-12-08 14:40:13 +000010777
10778 tg3_get_estats(tp, (struct tg3_ethtool_stats *)tmp_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010779}
10780
Matt Carlson535a4902011-07-20 10:20:56 +000010781static __be32 *tg3_vpd_readblock(struct tg3 *tp, u32 *vpdlen)
Matt Carlsonc3e94502011-04-13 11:05:08 +000010782{
10783 int i;
10784 __be32 *buf;
10785 u32 offset = 0, len = 0;
10786 u32 magic, val;
10787
Joe Perches63c3a662011-04-26 08:12:10 +000010788 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
Matt Carlsonc3e94502011-04-13 11:05:08 +000010789 return NULL;
10790
10791 if (magic == TG3_EEPROM_MAGIC) {
10792 for (offset = TG3_NVM_DIR_START;
10793 offset < TG3_NVM_DIR_END;
10794 offset += TG3_NVM_DIRENT_SIZE) {
10795 if (tg3_nvram_read(tp, offset, &val))
10796 return NULL;
10797
10798 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
10799 TG3_NVM_DIRTYPE_EXTVPD)
10800 break;
10801 }
10802
10803 if (offset != TG3_NVM_DIR_END) {
10804 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
10805 if (tg3_nvram_read(tp, offset + 4, &offset))
10806 return NULL;
10807
10808 offset = tg3_nvram_logical_addr(tp, offset);
10809 }
10810 }
10811
10812 if (!offset || !len) {
10813 offset = TG3_NVM_VPD_OFF;
10814 len = TG3_NVM_VPD_LEN;
10815 }
10816
10817 buf = kmalloc(len, GFP_KERNEL);
10818 if (buf == NULL)
10819 return NULL;
10820
10821 if (magic == TG3_EEPROM_MAGIC) {
10822 for (i = 0; i < len; i += 4) {
10823 /* The data is in little-endian format in NVRAM.
10824 * Use the big-endian read routines to preserve
10825 * the byte order as it exists in NVRAM.
10826 */
10827 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
10828 goto error;
10829 }
10830 } else {
10831 u8 *ptr;
10832 ssize_t cnt;
10833 unsigned int pos = 0;
10834
10835 ptr = (u8 *)&buf[0];
10836 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
10837 cnt = pci_read_vpd(tp->pdev, pos,
10838 len - pos, ptr);
10839 if (cnt == -ETIMEDOUT || cnt == -EINTR)
10840 cnt = 0;
10841 else if (cnt < 0)
10842 goto error;
10843 }
10844 if (pos != len)
10845 goto error;
10846 }
10847
Matt Carlson535a4902011-07-20 10:20:56 +000010848 *vpdlen = len;
10849
Matt Carlsonc3e94502011-04-13 11:05:08 +000010850 return buf;
10851
10852error:
10853 kfree(buf);
10854 return NULL;
10855}
10856
Michael Chan566f86a2005-05-29 14:56:58 -070010857#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080010858#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
10859#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
10860#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Matt Carlson727a6d92011-06-13 13:38:58 +000010861#define NVRAM_SELFBOOT_FORMAT1_4_SIZE 0x20
10862#define NVRAM_SELFBOOT_FORMAT1_5_SIZE 0x24
Matt Carlsonbda18fa2011-07-20 10:20:57 +000010863#define NVRAM_SELFBOOT_FORMAT1_6_SIZE 0x50
Michael Chanb16250e2006-09-27 16:10:14 -070010864#define NVRAM_SELFBOOT_HW_SIZE 0x20
10865#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070010866
10867static int tg3_test_nvram(struct tg3 *tp)
10868{
Matt Carlson535a4902011-07-20 10:20:56 +000010869 u32 csum, magic, len;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010870 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010871 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070010872
Joe Perches63c3a662011-04-26 08:12:10 +000010873 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000010874 return 0;
10875
Matt Carlsone4f34112009-02-25 14:25:00 +000010876 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080010877 return -EIO;
10878
Michael Chan1b277772006-03-20 22:27:48 -080010879 if (magic == TG3_EEPROM_MAGIC)
10880 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070010881 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080010882 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
10883 TG3_EEPROM_SB_FORMAT_1) {
10884 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
10885 case TG3_EEPROM_SB_REVISION_0:
10886 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
10887 break;
10888 case TG3_EEPROM_SB_REVISION_2:
10889 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
10890 break;
10891 case TG3_EEPROM_SB_REVISION_3:
10892 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
10893 break;
Matt Carlson727a6d92011-06-13 13:38:58 +000010894 case TG3_EEPROM_SB_REVISION_4:
10895 size = NVRAM_SELFBOOT_FORMAT1_4_SIZE;
10896 break;
10897 case TG3_EEPROM_SB_REVISION_5:
10898 size = NVRAM_SELFBOOT_FORMAT1_5_SIZE;
10899 break;
10900 case TG3_EEPROM_SB_REVISION_6:
10901 size = NVRAM_SELFBOOT_FORMAT1_6_SIZE;
10902 break;
Matt Carlsona5767de2007-11-12 21:10:58 -080010903 default:
Matt Carlson727a6d92011-06-13 13:38:58 +000010904 return -EIO;
Matt Carlsona5767de2007-11-12 21:10:58 -080010905 }
10906 } else
Michael Chan1b277772006-03-20 22:27:48 -080010907 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070010908 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
10909 size = NVRAM_SELFBOOT_HW_SIZE;
10910 else
Michael Chan1b277772006-03-20 22:27:48 -080010911 return -EIO;
10912
10913 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070010914 if (buf == NULL)
10915 return -ENOMEM;
10916
Michael Chan1b277772006-03-20 22:27:48 -080010917 err = -EIO;
10918 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010919 err = tg3_nvram_read_be32(tp, i, &buf[j]);
10920 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070010921 break;
Michael Chan566f86a2005-05-29 14:56:58 -070010922 }
Michael Chan1b277772006-03-20 22:27:48 -080010923 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070010924 goto out;
10925
Michael Chan1b277772006-03-20 22:27:48 -080010926 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010927 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080010928 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010929 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080010930 u8 *buf8 = (u8 *) buf, csum8 = 0;
10931
Al Virob9fc7dc2007-12-17 22:59:57 -080010932 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080010933 TG3_EEPROM_SB_REVISION_2) {
10934 /* For rev 2, the csum doesn't include the MBA. */
10935 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
10936 csum8 += buf8[i];
10937 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
10938 csum8 += buf8[i];
10939 } else {
10940 for (i = 0; i < size; i++)
10941 csum8 += buf8[i];
10942 }
Michael Chan1b277772006-03-20 22:27:48 -080010943
Adrian Bunkad96b482006-04-05 22:21:04 -070010944 if (csum8 == 0) {
10945 err = 0;
10946 goto out;
10947 }
10948
10949 err = -EIO;
10950 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080010951 }
Michael Chan566f86a2005-05-29 14:56:58 -070010952
Al Virob9fc7dc2007-12-17 22:59:57 -080010953 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010954 TG3_EEPROM_MAGIC_HW) {
10955 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000010956 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070010957 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070010958
10959 /* Separate the parity bits and the data bytes. */
10960 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
10961 if ((i == 0) || (i == 8)) {
10962 int l;
10963 u8 msk;
10964
10965 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
10966 parity[k++] = buf8[i] & msk;
10967 i++;
Matt Carlson859a5882010-04-05 10:19:28 +000010968 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070010969 int l;
10970 u8 msk;
10971
10972 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
10973 parity[k++] = buf8[i] & msk;
10974 i++;
10975
10976 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
10977 parity[k++] = buf8[i] & msk;
10978 i++;
10979 }
10980 data[j++] = buf8[i];
10981 }
10982
10983 err = -EIO;
10984 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
10985 u8 hw8 = hweight8(data[i]);
10986
10987 if ((hw8 & 0x1) && parity[i])
10988 goto out;
10989 else if (!(hw8 & 0x1) && !parity[i])
10990 goto out;
10991 }
10992 err = 0;
10993 goto out;
10994 }
10995
Matt Carlson01c3a392011-03-09 16:58:20 +000010996 err = -EIO;
10997
Michael Chan566f86a2005-05-29 14:56:58 -070010998 /* Bootstrap checksum at offset 0x10 */
10999 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000011000 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070011001 goto out;
11002
11003 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
11004 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000011005 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000011006 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070011007
Matt Carlsonc3e94502011-04-13 11:05:08 +000011008 kfree(buf);
11009
Matt Carlson535a4902011-07-20 10:20:56 +000011010 buf = tg3_vpd_readblock(tp, &len);
Matt Carlsonc3e94502011-04-13 11:05:08 +000011011 if (!buf)
11012 return -ENOMEM;
Matt Carlsond4894f32011-03-09 16:58:21 +000011013
Matt Carlson535a4902011-07-20 10:20:56 +000011014 i = pci_vpd_find_tag((u8 *)buf, 0, len, PCI_VPD_LRDT_RO_DATA);
Matt Carlsond4894f32011-03-09 16:58:21 +000011015 if (i > 0) {
11016 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
11017 if (j < 0)
11018 goto out;
11019
Matt Carlson535a4902011-07-20 10:20:56 +000011020 if (i + PCI_VPD_LRDT_TAG_SIZE + j > len)
Matt Carlsond4894f32011-03-09 16:58:21 +000011021 goto out;
11022
11023 i += PCI_VPD_LRDT_TAG_SIZE;
11024 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
11025 PCI_VPD_RO_KEYWORD_CHKSUM);
11026 if (j > 0) {
11027 u8 csum8 = 0;
11028
11029 j += PCI_VPD_INFO_FLD_HDR_SIZE;
11030
11031 for (i = 0; i <= j; i++)
11032 csum8 += ((u8 *)buf)[i];
11033
11034 if (csum8)
11035 goto out;
11036 }
11037 }
11038
Michael Chan566f86a2005-05-29 14:56:58 -070011039 err = 0;
11040
11041out:
11042 kfree(buf);
11043 return err;
11044}
11045
Michael Chanca430072005-05-29 14:57:23 -070011046#define TG3_SERDES_TIMEOUT_SEC 2
11047#define TG3_COPPER_TIMEOUT_SEC 6
11048
11049static int tg3_test_link(struct tg3 *tp)
11050{
11051 int i, max;
11052
11053 if (!netif_running(tp->dev))
11054 return -ENODEV;
11055
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011056 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070011057 max = TG3_SERDES_TIMEOUT_SEC;
11058 else
11059 max = TG3_COPPER_TIMEOUT_SEC;
11060
11061 for (i = 0; i < max; i++) {
11062 if (netif_carrier_ok(tp->dev))
11063 return 0;
11064
11065 if (msleep_interruptible(1000))
11066 break;
11067 }
11068
11069 return -EIO;
11070}
11071
Michael Chana71116d2005-05-29 14:58:11 -070011072/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080011073static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070011074{
Michael Chanb16250e2006-09-27 16:10:14 -070011075 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070011076 u32 offset, read_mask, write_mask, val, save_val, read_val;
11077 static struct {
11078 u16 offset;
11079 u16 flags;
11080#define TG3_FL_5705 0x1
11081#define TG3_FL_NOT_5705 0x2
11082#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070011083#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070011084 u32 read_mask;
11085 u32 write_mask;
11086 } reg_tbl[] = {
11087 /* MAC Control Registers */
11088 { MAC_MODE, TG3_FL_NOT_5705,
11089 0x00000000, 0x00ef6f8c },
11090 { MAC_MODE, TG3_FL_5705,
11091 0x00000000, 0x01ef6b8c },
11092 { MAC_STATUS, TG3_FL_NOT_5705,
11093 0x03800107, 0x00000000 },
11094 { MAC_STATUS, TG3_FL_5705,
11095 0x03800100, 0x00000000 },
11096 { MAC_ADDR_0_HIGH, 0x0000,
11097 0x00000000, 0x0000ffff },
11098 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011099 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070011100 { MAC_RX_MTU_SIZE, 0x0000,
11101 0x00000000, 0x0000ffff },
11102 { MAC_TX_MODE, 0x0000,
11103 0x00000000, 0x00000070 },
11104 { MAC_TX_LENGTHS, 0x0000,
11105 0x00000000, 0x00003fff },
11106 { MAC_RX_MODE, TG3_FL_NOT_5705,
11107 0x00000000, 0x000007fc },
11108 { MAC_RX_MODE, TG3_FL_5705,
11109 0x00000000, 0x000007dc },
11110 { MAC_HASH_REG_0, 0x0000,
11111 0x00000000, 0xffffffff },
11112 { MAC_HASH_REG_1, 0x0000,
11113 0x00000000, 0xffffffff },
11114 { MAC_HASH_REG_2, 0x0000,
11115 0x00000000, 0xffffffff },
11116 { MAC_HASH_REG_3, 0x0000,
11117 0x00000000, 0xffffffff },
11118
11119 /* Receive Data and Receive BD Initiator Control Registers. */
11120 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
11121 0x00000000, 0xffffffff },
11122 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
11123 0x00000000, 0xffffffff },
11124 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
11125 0x00000000, 0x00000003 },
11126 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
11127 0x00000000, 0xffffffff },
11128 { RCVDBDI_STD_BD+0, 0x0000,
11129 0x00000000, 0xffffffff },
11130 { RCVDBDI_STD_BD+4, 0x0000,
11131 0x00000000, 0xffffffff },
11132 { RCVDBDI_STD_BD+8, 0x0000,
11133 0x00000000, 0xffff0002 },
11134 { RCVDBDI_STD_BD+0xc, 0x0000,
11135 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011136
Michael Chana71116d2005-05-29 14:58:11 -070011137 /* Receive BD Initiator Control Registers. */
11138 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
11139 0x00000000, 0xffffffff },
11140 { RCVBDI_STD_THRESH, TG3_FL_5705,
11141 0x00000000, 0x000003ff },
11142 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
11143 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011144
Michael Chana71116d2005-05-29 14:58:11 -070011145 /* Host Coalescing Control Registers. */
11146 { HOSTCC_MODE, TG3_FL_NOT_5705,
11147 0x00000000, 0x00000004 },
11148 { HOSTCC_MODE, TG3_FL_5705,
11149 0x00000000, 0x000000f6 },
11150 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
11151 0x00000000, 0xffffffff },
11152 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
11153 0x00000000, 0x000003ff },
11154 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
11155 0x00000000, 0xffffffff },
11156 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
11157 0x00000000, 0x000003ff },
11158 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
11159 0x00000000, 0xffffffff },
11160 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
11161 0x00000000, 0x000000ff },
11162 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
11163 0x00000000, 0xffffffff },
11164 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
11165 0x00000000, 0x000000ff },
11166 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
11167 0x00000000, 0xffffffff },
11168 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
11169 0x00000000, 0xffffffff },
11170 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
11171 0x00000000, 0xffffffff },
11172 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
11173 0x00000000, 0x000000ff },
11174 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
11175 0x00000000, 0xffffffff },
11176 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
11177 0x00000000, 0x000000ff },
11178 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
11179 0x00000000, 0xffffffff },
11180 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
11181 0x00000000, 0xffffffff },
11182 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
11183 0x00000000, 0xffffffff },
11184 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
11185 0x00000000, 0xffffffff },
11186 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
11187 0x00000000, 0xffffffff },
11188 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
11189 0xffffffff, 0x00000000 },
11190 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
11191 0xffffffff, 0x00000000 },
11192
11193 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070011194 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070011195 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070011196 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070011197 0x00000000, 0x007fffff },
11198 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
11199 0x00000000, 0x0000003f },
11200 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
11201 0x00000000, 0x000001ff },
11202 { BUFMGR_MB_HIGH_WATER, 0x0000,
11203 0x00000000, 0x000001ff },
11204 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
11205 0xffffffff, 0x00000000 },
11206 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
11207 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011208
Michael Chana71116d2005-05-29 14:58:11 -070011209 /* Mailbox Registers */
11210 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
11211 0x00000000, 0x000001ff },
11212 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
11213 0x00000000, 0x000001ff },
11214 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
11215 0x00000000, 0x000007ff },
11216 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
11217 0x00000000, 0x000001ff },
11218
11219 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
11220 };
11221
Michael Chanb16250e2006-09-27 16:10:14 -070011222 is_5705 = is_5750 = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000011223 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chana71116d2005-05-29 14:58:11 -070011224 is_5705 = 1;
Joe Perches63c3a662011-04-26 08:12:10 +000011225 if (tg3_flag(tp, 5750_PLUS))
Michael Chanb16250e2006-09-27 16:10:14 -070011226 is_5750 = 1;
11227 }
Michael Chana71116d2005-05-29 14:58:11 -070011228
11229 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
11230 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
11231 continue;
11232
11233 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
11234 continue;
11235
Joe Perches63c3a662011-04-26 08:12:10 +000011236 if (tg3_flag(tp, IS_5788) &&
Michael Chana71116d2005-05-29 14:58:11 -070011237 (reg_tbl[i].flags & TG3_FL_NOT_5788))
11238 continue;
11239
Michael Chanb16250e2006-09-27 16:10:14 -070011240 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
11241 continue;
11242
Michael Chana71116d2005-05-29 14:58:11 -070011243 offset = (u32) reg_tbl[i].offset;
11244 read_mask = reg_tbl[i].read_mask;
11245 write_mask = reg_tbl[i].write_mask;
11246
11247 /* Save the original register content */
11248 save_val = tr32(offset);
11249
11250 /* Determine the read-only value. */
11251 read_val = save_val & read_mask;
11252
11253 /* Write zero to the register, then make sure the read-only bits
11254 * are not changed and the read/write bits are all zeros.
11255 */
11256 tw32(offset, 0);
11257
11258 val = tr32(offset);
11259
11260 /* Test the read-only and read/write bits. */
11261 if (((val & read_mask) != read_val) || (val & write_mask))
11262 goto out;
11263
11264 /* Write ones to all the bits defined by RdMask and WrMask, then
11265 * make sure the read-only bits are not changed and the
11266 * read/write bits are all ones.
11267 */
11268 tw32(offset, read_mask | write_mask);
11269
11270 val = tr32(offset);
11271
11272 /* Test the read-only bits. */
11273 if ((val & read_mask) != read_val)
11274 goto out;
11275
11276 /* Test the read/write bits. */
11277 if ((val & write_mask) != write_mask)
11278 goto out;
11279
11280 tw32(offset, save_val);
11281 }
11282
11283 return 0;
11284
11285out:
Michael Chan9f88f292006-12-07 00:22:54 -080011286 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000011287 netdev_err(tp->dev,
11288 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070011289 tw32(offset, save_val);
11290 return -EIO;
11291}
11292
Michael Chan7942e1d2005-05-29 14:58:36 -070011293static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
11294{
Arjan van de Venf71e1302006-03-03 21:33:57 -050011295 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070011296 int i;
11297 u32 j;
11298
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020011299 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070011300 for (j = 0; j < len; j += 4) {
11301 u32 val;
11302
11303 tg3_write_mem(tp, offset + j, test_pattern[i]);
11304 tg3_read_mem(tp, offset + j, &val);
11305 if (val != test_pattern[i])
11306 return -EIO;
11307 }
11308 }
11309 return 0;
11310}
11311
11312static int tg3_test_memory(struct tg3 *tp)
11313{
11314 static struct mem_entry {
11315 u32 offset;
11316 u32 len;
11317 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080011318 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070011319 { 0x00002000, 0x1c000},
11320 { 0xffffffff, 0x00000}
11321 }, mem_tbl_5705[] = {
11322 { 0x00000100, 0x0000c},
11323 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070011324 { 0x00004000, 0x00800},
11325 { 0x00006000, 0x01000},
11326 { 0x00008000, 0x02000},
11327 { 0x00010000, 0x0e000},
11328 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080011329 }, mem_tbl_5755[] = {
11330 { 0x00000200, 0x00008},
11331 { 0x00004000, 0x00800},
11332 { 0x00006000, 0x00800},
11333 { 0x00008000, 0x02000},
11334 { 0x00010000, 0x0c000},
11335 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070011336 }, mem_tbl_5906[] = {
11337 { 0x00000200, 0x00008},
11338 { 0x00004000, 0x00400},
11339 { 0x00006000, 0x00400},
11340 { 0x00008000, 0x01000},
11341 { 0x00010000, 0x01000},
11342 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011343 }, mem_tbl_5717[] = {
11344 { 0x00000200, 0x00008},
11345 { 0x00010000, 0x0a000},
11346 { 0x00020000, 0x13c00},
11347 { 0xffffffff, 0x00000}
11348 }, mem_tbl_57765[] = {
11349 { 0x00000200, 0x00008},
11350 { 0x00004000, 0x00800},
11351 { 0x00006000, 0x09800},
11352 { 0x00010000, 0x0a000},
11353 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070011354 };
11355 struct mem_entry *mem_tbl;
11356 int err = 0;
11357 int i;
11358
Joe Perches63c3a662011-04-26 08:12:10 +000011359 if (tg3_flag(tp, 5717_PLUS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011360 mem_tbl = mem_tbl_5717;
11361 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
11362 mem_tbl = mem_tbl_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000011363 else if (tg3_flag(tp, 5755_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080011364 mem_tbl = mem_tbl_5755;
11365 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
11366 mem_tbl = mem_tbl_5906;
Joe Perches63c3a662011-04-26 08:12:10 +000011367 else if (tg3_flag(tp, 5705_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080011368 mem_tbl = mem_tbl_5705;
11369 else
Michael Chan7942e1d2005-05-29 14:58:36 -070011370 mem_tbl = mem_tbl_570x;
11371
11372 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000011373 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
11374 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070011375 break;
11376 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011377
Michael Chan7942e1d2005-05-29 14:58:36 -070011378 return err;
11379}
11380
Matt Carlsonbb158d62011-04-25 12:42:47 +000011381#define TG3_TSO_MSS 500
11382
11383#define TG3_TSO_IP_HDR_LEN 20
11384#define TG3_TSO_TCP_HDR_LEN 20
11385#define TG3_TSO_TCP_OPT_LEN 12
11386
11387static const u8 tg3_tso_header[] = {
113880x08, 0x00,
113890x45, 0x00, 0x00, 0x00,
113900x00, 0x00, 0x40, 0x00,
113910x40, 0x06, 0x00, 0x00,
113920x0a, 0x00, 0x00, 0x01,
113930x0a, 0x00, 0x00, 0x02,
113940x0d, 0x00, 0xe0, 0x00,
113950x00, 0x00, 0x01, 0x00,
113960x00, 0x00, 0x02, 0x00,
113970x80, 0x10, 0x10, 0x00,
113980x14, 0x09, 0x00, 0x00,
113990x01, 0x01, 0x08, 0x0a,
114000x11, 0x11, 0x11, 0x11,
114010x11, 0x11, 0x11, 0x11,
11402};
Michael Chan9f40dea2005-09-05 17:53:06 -070011403
Matt Carlson28a45952011-08-19 13:58:22 +000011404static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, bool tso_loopback)
Michael Chanc76949a2005-05-29 14:58:59 -070011405{
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011406 u32 rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011407 u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
Matt Carlson84b67b22011-07-27 14:20:52 +000011408 u32 budget;
Eric Dumazet9205fd92011-11-18 06:47:01 +000011409 struct sk_buff *skb;
11410 u8 *tx_data, *rx_data;
Michael Chanc76949a2005-05-29 14:58:59 -070011411 dma_addr_t map;
11412 int num_pkts, tx_len, rx_len, i, err;
11413 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000011414 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000011415 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070011416
Matt Carlsonc8873402010-02-12 14:47:11 +000011417 tnapi = &tp->napi[0];
11418 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011419 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +000011420 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlson1da85aa2010-09-30 10:34:34 +000011421 rnapi = &tp->napi[1];
Joe Perches63c3a662011-04-26 08:12:10 +000011422 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc8873402010-02-12 14:47:11 +000011423 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011424 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011425 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000011426
Michael Chanc76949a2005-05-29 14:58:59 -070011427 err = -EIO;
11428
Matt Carlson4852a862011-04-13 11:05:07 +000011429 tx_len = pktsz;
David S. Millera20e9c62006-07-31 22:38:16 -070011430 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070011431 if (!skb)
11432 return -ENOMEM;
11433
Michael Chanc76949a2005-05-29 14:58:59 -070011434 tx_data = skb_put(skb, tx_len);
11435 memcpy(tx_data, tp->dev->dev_addr, 6);
11436 memset(tx_data + 6, 0x0, 8);
11437
Matt Carlson4852a862011-04-13 11:05:07 +000011438 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
Michael Chanc76949a2005-05-29 14:58:59 -070011439
Matt Carlson28a45952011-08-19 13:58:22 +000011440 if (tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011441 struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
11442
11443 u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
11444 TG3_TSO_TCP_OPT_LEN;
11445
11446 memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
11447 sizeof(tg3_tso_header));
11448 mss = TG3_TSO_MSS;
11449
11450 val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
11451 num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
11452
11453 /* Set the total length field in the IP header */
11454 iph->tot_len = htons((u16)(mss + hdr_len));
11455
11456 base_flags = (TXD_FLAG_CPU_PRE_DMA |
11457 TXD_FLAG_CPU_POST_DMA);
11458
Joe Perches63c3a662011-04-26 08:12:10 +000011459 if (tg3_flag(tp, HW_TSO_1) ||
11460 tg3_flag(tp, HW_TSO_2) ||
11461 tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011462 struct tcphdr *th;
11463 val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
11464 th = (struct tcphdr *)&tx_data[val];
11465 th->check = 0;
11466 } else
11467 base_flags |= TXD_FLAG_TCPUDP_CSUM;
11468
Joe Perches63c3a662011-04-26 08:12:10 +000011469 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011470 mss |= (hdr_len & 0xc) << 12;
11471 if (hdr_len & 0x10)
11472 base_flags |= 0x00000010;
11473 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +000011474 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlsonbb158d62011-04-25 12:42:47 +000011475 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +000011476 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlsonbb158d62011-04-25 12:42:47 +000011477 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
11478 mss |= (TG3_TSO_TCP_OPT_LEN << 9);
11479 } else {
11480 base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
11481 }
11482
11483 data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
11484 } else {
11485 num_pkts = 1;
11486 data_off = ETH_HLEN;
11487 }
11488
11489 for (i = data_off; i < tx_len; i++)
Michael Chanc76949a2005-05-29 14:58:59 -070011490 tx_data[i] = (u8) (i & 0xff);
11491
Alexander Duyckf4188d82009-12-02 16:48:38 +000011492 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
11493 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000011494 dev_kfree_skb(skb);
11495 return -EIO;
11496 }
Michael Chanc76949a2005-05-29 14:58:59 -070011497
Matt Carlson0d681b22011-07-27 14:20:49 +000011498 val = tnapi->tx_prod;
11499 tnapi->tx_buffers[val].skb = skb;
11500 dma_unmap_addr_set(&tnapi->tx_buffers[val], mapping, map);
11501
Michael Chanc76949a2005-05-29 14:58:59 -070011502 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011503 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011504
11505 udelay(10);
11506
Matt Carlson898a56f2009-08-28 14:02:40 +000011507 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070011508
Matt Carlson84b67b22011-07-27 14:20:52 +000011509 budget = tg3_tx_avail(tnapi);
11510 if (tg3_tx_frag_set(tnapi, &val, &budget, map, tx_len,
Matt Carlsond1a3b732011-07-27 14:20:51 +000011511 base_flags | TXD_FLAG_END, mss, 0)) {
11512 tnapi->tx_buffers[val].skb = NULL;
11513 dev_kfree_skb(skb);
11514 return -EIO;
11515 }
Michael Chanc76949a2005-05-29 14:58:59 -070011516
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011517 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070011518
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011519 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
11520 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070011521
11522 udelay(10);
11523
Matt Carlson303fc922009-11-02 14:27:34 +000011524 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
11525 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070011526 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011527 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011528
11529 udelay(10);
11530
Matt Carlson898a56f2009-08-28 14:02:40 +000011531 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
11532 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011533 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070011534 (rx_idx == (rx_start_idx + num_pkts)))
11535 break;
11536 }
11537
Matt Carlsonba1142e2011-11-04 09:15:00 +000011538 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod - 1, -1);
Michael Chanc76949a2005-05-29 14:58:59 -070011539 dev_kfree_skb(skb);
11540
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011541 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070011542 goto out;
11543
11544 if (rx_idx != rx_start_idx + num_pkts)
11545 goto out;
11546
Matt Carlsonbb158d62011-04-25 12:42:47 +000011547 val = data_off;
11548 while (rx_idx != rx_start_idx) {
11549 desc = &rnapi->rx_rcb[rx_start_idx++];
11550 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
11551 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
Michael Chanc76949a2005-05-29 14:58:59 -070011552
Matt Carlsonbb158d62011-04-25 12:42:47 +000011553 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
11554 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
Matt Carlson4852a862011-04-13 11:05:07 +000011555 goto out;
Michael Chanc76949a2005-05-29 14:58:59 -070011556
Matt Carlsonbb158d62011-04-25 12:42:47 +000011557 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
11558 - ETH_FCS_LEN;
11559
Matt Carlson28a45952011-08-19 13:58:22 +000011560 if (!tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011561 if (rx_len != tx_len)
11562 goto out;
11563
11564 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
11565 if (opaque_key != RXD_OPAQUE_RING_STD)
11566 goto out;
11567 } else {
11568 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
11569 goto out;
11570 }
11571 } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
11572 (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
Matt Carlson54e0a672011-05-19 12:12:50 +000011573 >> RXD_TCPCSUM_SHIFT != 0xffff) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011574 goto out;
11575 }
11576
11577 if (opaque_key == RXD_OPAQUE_RING_STD) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000011578 rx_data = tpr->rx_std_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011579 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
11580 mapping);
11581 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000011582 rx_data = tpr->rx_jmb_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011583 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
11584 mapping);
11585 } else
Matt Carlson4852a862011-04-13 11:05:07 +000011586 goto out;
11587
Matt Carlsonbb158d62011-04-25 12:42:47 +000011588 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
11589 PCI_DMA_FROMDEVICE);
11590
Eric Dumazet9205fd92011-11-18 06:47:01 +000011591 rx_data += TG3_RX_OFFSET(tp);
Matt Carlsonbb158d62011-04-25 12:42:47 +000011592 for (i = data_off; i < rx_len; i++, val++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000011593 if (*(rx_data + i) != (u8) (val & 0xff))
Matt Carlsonbb158d62011-04-25 12:42:47 +000011594 goto out;
11595 }
Matt Carlson4852a862011-04-13 11:05:07 +000011596 }
11597
Michael Chanc76949a2005-05-29 14:58:59 -070011598 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011599
Eric Dumazet9205fd92011-11-18 06:47:01 +000011600 /* tg3_free_rings will unmap and free the rx_data */
Michael Chanc76949a2005-05-29 14:58:59 -070011601out:
11602 return err;
11603}
11604
Matt Carlson00c266b2011-04-25 12:42:46 +000011605#define TG3_STD_LOOPBACK_FAILED 1
11606#define TG3_JMB_LOOPBACK_FAILED 2
Matt Carlsonbb158d62011-04-25 12:42:47 +000011607#define TG3_TSO_LOOPBACK_FAILED 4
Matt Carlson28a45952011-08-19 13:58:22 +000011608#define TG3_LOOPBACK_FAILED \
11609 (TG3_STD_LOOPBACK_FAILED | \
11610 TG3_JMB_LOOPBACK_FAILED | \
11611 TG3_TSO_LOOPBACK_FAILED)
Matt Carlson00c266b2011-04-25 12:42:46 +000011612
Matt Carlson941ec902011-08-19 13:58:23 +000011613static int tg3_test_loopback(struct tg3 *tp, u64 *data, bool do_extlpbk)
Michael Chan9f40dea2005-09-05 17:53:06 -070011614{
Matt Carlson28a45952011-08-19 13:58:22 +000011615 int err = -EIO;
Matt Carlson2215e242011-08-19 13:58:19 +000011616 u32 eee_cap;
Michael Chan9f40dea2005-09-05 17:53:06 -070011617
Matt Carlsonab789042011-01-25 15:58:54 +000011618 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
11619 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
11620
Matt Carlson28a45952011-08-19 13:58:22 +000011621 if (!netif_running(tp->dev)) {
11622 data[0] = TG3_LOOPBACK_FAILED;
11623 data[1] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000011624 if (do_extlpbk)
11625 data[2] = TG3_LOOPBACK_FAILED;
Matt Carlson28a45952011-08-19 13:58:22 +000011626 goto done;
11627 }
11628
Michael Chanb9ec6c12006-07-25 16:37:27 -070011629 err = tg3_reset_hw(tp, 1);
Matt Carlsonab789042011-01-25 15:58:54 +000011630 if (err) {
Matt Carlson28a45952011-08-19 13:58:22 +000011631 data[0] = TG3_LOOPBACK_FAILED;
11632 data[1] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000011633 if (do_extlpbk)
11634 data[2] = TG3_LOOPBACK_FAILED;
Matt Carlsonab789042011-01-25 15:58:54 +000011635 goto done;
11636 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011637
Joe Perches63c3a662011-04-26 08:12:10 +000011638 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson4a85f092011-04-20 07:57:37 +000011639 int i;
11640
11641 /* Reroute all rx packets to the 1st queue */
11642 for (i = MAC_RSS_INDIR_TBL_0;
11643 i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
11644 tw32(i, 0x0);
11645 }
11646
Matt Carlson6e01b202011-08-19 13:58:20 +000011647 /* HW errata - mac loopback fails in some cases on 5780.
11648 * Normal traffic and PHY loopback are not affected by
11649 * errata. Also, the MAC loopback test is deprecated for
11650 * all newer ASIC revisions.
11651 */
11652 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
11653 !tg3_flag(tp, CPMU_PRESENT)) {
11654 tg3_mac_loopback(tp, true);
Matt Carlson9936bcf2007-10-10 18:03:07 -070011655
Matt Carlson28a45952011-08-19 13:58:22 +000011656 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
11657 data[0] |= TG3_STD_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000011658
11659 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000011660 tg3_run_loopback(tp, 9000 + ETH_HLEN, false))
11661 data[0] |= TG3_JMB_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000011662
11663 tg3_mac_loopback(tp, false);
11664 }
Matt Carlson4852a862011-04-13 11:05:07 +000011665
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011666 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000011667 !tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011668 int i;
11669
Matt Carlson941ec902011-08-19 13:58:23 +000011670 tg3_phy_lpbk_set(tp, 0, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011671
11672 /* Wait for link */
11673 for (i = 0; i < 100; i++) {
11674 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
11675 break;
11676 mdelay(1);
11677 }
11678
Matt Carlson28a45952011-08-19 13:58:22 +000011679 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
11680 data[1] |= TG3_STD_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000011681 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000011682 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
11683 data[1] |= TG3_TSO_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000011684 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000011685 tg3_run_loopback(tp, 9000 + ETH_HLEN, false))
11686 data[1] |= TG3_JMB_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070011687
Matt Carlson941ec902011-08-19 13:58:23 +000011688 if (do_extlpbk) {
11689 tg3_phy_lpbk_set(tp, 0, true);
11690
11691 /* All link indications report up, but the hardware
11692 * isn't really ready for about 20 msec. Double it
11693 * to be sure.
11694 */
11695 mdelay(40);
11696
11697 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
11698 data[2] |= TG3_STD_LOOPBACK_FAILED;
11699 if (tg3_flag(tp, TSO_CAPABLE) &&
11700 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
11701 data[2] |= TG3_TSO_LOOPBACK_FAILED;
11702 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
11703 tg3_run_loopback(tp, 9000 + ETH_HLEN, false))
11704 data[2] |= TG3_JMB_LOOPBACK_FAILED;
11705 }
11706
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011707 /* Re-enable gphy autopowerdown. */
11708 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
11709 tg3_phy_toggle_apd(tp, true);
11710 }
Matt Carlson6833c042008-11-21 17:18:59 -080011711
Matt Carlson941ec902011-08-19 13:58:23 +000011712 err = (data[0] | data[1] | data[2]) ? -EIO : 0;
Matt Carlson28a45952011-08-19 13:58:22 +000011713
Matt Carlsonab789042011-01-25 15:58:54 +000011714done:
11715 tp->phy_flags |= eee_cap;
11716
Michael Chan9f40dea2005-09-05 17:53:06 -070011717 return err;
11718}
11719
Michael Chan4cafd3f2005-05-29 14:56:34 -070011720static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
11721 u64 *data)
11722{
Michael Chan566f86a2005-05-29 14:56:58 -070011723 struct tg3 *tp = netdev_priv(dev);
Matt Carlson941ec902011-08-19 13:58:23 +000011724 bool doextlpbk = etest->flags & ETH_TEST_FL_EXTERNAL_LB;
Michael Chan566f86a2005-05-29 14:56:58 -070011725
Matt Carlsonbed98292011-07-13 09:27:29 +000011726 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
11727 tg3_power_up(tp)) {
11728 etest->flags |= ETH_TEST_FL_FAILED;
11729 memset(data, 1, sizeof(u64) * TG3_NUM_TEST);
11730 return;
11731 }
Michael Chanbc1c7562006-03-20 17:48:03 -080011732
Michael Chan566f86a2005-05-29 14:56:58 -070011733 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
11734
11735 if (tg3_test_nvram(tp) != 0) {
11736 etest->flags |= ETH_TEST_FL_FAILED;
11737 data[0] = 1;
11738 }
Matt Carlson941ec902011-08-19 13:58:23 +000011739 if (!doextlpbk && tg3_test_link(tp)) {
Michael Chanca430072005-05-29 14:57:23 -070011740 etest->flags |= ETH_TEST_FL_FAILED;
11741 data[1] = 1;
11742 }
Michael Chana71116d2005-05-29 14:58:11 -070011743 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011744 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070011745
Michael Chanbbe832c2005-06-24 20:20:04 -070011746 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011747 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070011748 tg3_netif_stop(tp);
11749 irq_sync = 1;
11750 }
11751
11752 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070011753
11754 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080011755 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011756 tg3_halt_cpu(tp, RX_CPU_BASE);
Joe Perches63c3a662011-04-26 08:12:10 +000011757 if (!tg3_flag(tp, 5705_PLUS))
Michael Chana71116d2005-05-29 14:58:11 -070011758 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080011759 if (!err)
11760 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011761
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011762 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad2006-03-20 22:27:35 -080011763 tg3_phy_reset(tp);
11764
Michael Chana71116d2005-05-29 14:58:11 -070011765 if (tg3_test_registers(tp) != 0) {
11766 etest->flags |= ETH_TEST_FL_FAILED;
11767 data[2] = 1;
11768 }
Matt Carlson28a45952011-08-19 13:58:22 +000011769
Michael Chan7942e1d2005-05-29 14:58:36 -070011770 if (tg3_test_memory(tp) != 0) {
11771 etest->flags |= ETH_TEST_FL_FAILED;
11772 data[3] = 1;
11773 }
Matt Carlson28a45952011-08-19 13:58:22 +000011774
Matt Carlson941ec902011-08-19 13:58:23 +000011775 if (doextlpbk)
11776 etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
11777
11778 if (tg3_test_loopback(tp, &data[4], doextlpbk))
Michael Chanc76949a2005-05-29 14:58:59 -070011779 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070011780
David S. Millerf47c11e2005-06-24 20:18:35 -070011781 tg3_full_unlock(tp);
11782
Michael Chand4bc3922005-05-29 14:59:20 -070011783 if (tg3_test_interrupt(tp) != 0) {
11784 etest->flags |= ETH_TEST_FL_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000011785 data[7] = 1;
Michael Chand4bc3922005-05-29 14:59:20 -070011786 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011787
11788 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070011789
Michael Chana71116d2005-05-29 14:58:11 -070011790 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
11791 if (netif_running(dev)) {
Joe Perches63c3a662011-04-26 08:12:10 +000011792 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011793 err2 = tg3_restart_hw(tp, 1);
11794 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070011795 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011796 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011797
11798 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011799
11800 if (irq_sync && !err2)
11801 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011802 }
Matt Carlson80096062010-08-02 11:26:06 +000011803 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000011804 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080011805
Michael Chan4cafd3f2005-05-29 14:56:34 -070011806}
11807
Linus Torvalds1da177e2005-04-16 15:20:36 -070011808static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
11809{
11810 struct mii_ioctl_data *data = if_mii(ifr);
11811 struct tg3 *tp = netdev_priv(dev);
11812 int err;
11813
Joe Perches63c3a662011-04-26 08:12:10 +000011814 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011815 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011816 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011817 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011818 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000011819 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011820 }
11821
Matt Carlson33f401a2010-04-05 10:19:27 +000011822 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011823 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000011824 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011825
11826 /* fallthru */
11827 case SIOCGMIIREG: {
11828 u32 mii_regval;
11829
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011830 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011831 break; /* We have no PHY */
11832
Matt Carlson34eea5a2011-04-20 07:57:38 +000011833 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080011834 return -EAGAIN;
11835
David S. Millerf47c11e2005-06-24 20:18:35 -070011836 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011837 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070011838 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011839
11840 data->val_out = mii_regval;
11841
11842 return err;
11843 }
11844
11845 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011846 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011847 break; /* We have no PHY */
11848
Matt Carlson34eea5a2011-04-20 07:57:38 +000011849 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080011850 return -EAGAIN;
11851
David S. Millerf47c11e2005-06-24 20:18:35 -070011852 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011853 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070011854 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011855
11856 return err;
11857
11858 default:
11859 /* do nothing */
11860 break;
11861 }
11862 return -EOPNOTSUPP;
11863}
11864
David S. Miller15f98502005-05-18 22:49:26 -070011865static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11866{
11867 struct tg3 *tp = netdev_priv(dev);
11868
11869 memcpy(ec, &tp->coal, sizeof(*ec));
11870 return 0;
11871}
11872
Michael Chand244c892005-07-05 14:42:33 -070011873static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11874{
11875 struct tg3 *tp = netdev_priv(dev);
11876 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
11877 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
11878
Joe Perches63c3a662011-04-26 08:12:10 +000011879 if (!tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070011880 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
11881 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
11882 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
11883 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
11884 }
11885
11886 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
11887 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
11888 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
11889 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
11890 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
11891 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
11892 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
11893 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
11894 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
11895 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
11896 return -EINVAL;
11897
11898 /* No rx interrupts will be generated if both are zero */
11899 if ((ec->rx_coalesce_usecs == 0) &&
11900 (ec->rx_max_coalesced_frames == 0))
11901 return -EINVAL;
11902
11903 /* No tx interrupts will be generated if both are zero */
11904 if ((ec->tx_coalesce_usecs == 0) &&
11905 (ec->tx_max_coalesced_frames == 0))
11906 return -EINVAL;
11907
11908 /* Only copy relevant parameters, ignore all others. */
11909 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
11910 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
11911 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
11912 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
11913 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
11914 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
11915 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
11916 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
11917 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
11918
11919 if (netif_running(dev)) {
11920 tg3_full_lock(tp, 0);
11921 __tg3_set_coalesce(tp, &tp->coal);
11922 tg3_full_unlock(tp);
11923 }
11924 return 0;
11925}
11926
Jeff Garzik7282d492006-09-13 14:30:00 -040011927static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011928 .get_settings = tg3_get_settings,
11929 .set_settings = tg3_set_settings,
11930 .get_drvinfo = tg3_get_drvinfo,
11931 .get_regs_len = tg3_get_regs_len,
11932 .get_regs = tg3_get_regs,
11933 .get_wol = tg3_get_wol,
11934 .set_wol = tg3_set_wol,
11935 .get_msglevel = tg3_get_msglevel,
11936 .set_msglevel = tg3_set_msglevel,
11937 .nway_reset = tg3_nway_reset,
11938 .get_link = ethtool_op_get_link,
11939 .get_eeprom_len = tg3_get_eeprom_len,
11940 .get_eeprom = tg3_get_eeprom,
11941 .set_eeprom = tg3_set_eeprom,
11942 .get_ringparam = tg3_get_ringparam,
11943 .set_ringparam = tg3_set_ringparam,
11944 .get_pauseparam = tg3_get_pauseparam,
11945 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070011946 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011947 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000011948 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011949 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070011950 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070011951 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070011952 .get_sset_count = tg3_get_sset_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011953};
11954
11955static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
11956{
Michael Chan1b277772006-03-20 22:27:48 -080011957 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011958
11959 tp->nvram_size = EEPROM_CHIP_SIZE;
11960
Matt Carlsone4f34112009-02-25 14:25:00 +000011961 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011962 return;
11963
Michael Chanb16250e2006-09-27 16:10:14 -070011964 if ((magic != TG3_EEPROM_MAGIC) &&
11965 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
11966 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011967 return;
11968
11969 /*
11970 * Size the chip by reading offsets at increasing powers of two.
11971 * When we encounter our validation signature, we know the addressing
11972 * has wrapped around, and thus have our chip size.
11973 */
Michael Chan1b277772006-03-20 22:27:48 -080011974 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011975
11976 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000011977 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011978 return;
11979
Michael Chan18201802006-03-20 22:29:15 -080011980 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011981 break;
11982
11983 cursize <<= 1;
11984 }
11985
11986 tp->nvram_size = cursize;
11987}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011988
Linus Torvalds1da177e2005-04-16 15:20:36 -070011989static void __devinit tg3_get_nvram_size(struct tg3 *tp)
11990{
11991 u32 val;
11992
Joe Perches63c3a662011-04-26 08:12:10 +000011993 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080011994 return;
11995
11996 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080011997 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080011998 tg3_get_eeprom_size(tp);
11999 return;
12000 }
12001
Matt Carlson6d348f22009-02-25 14:25:52 +000012002 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012003 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000012004 /* This is confusing. We want to operate on the
12005 * 16-bit value at offset 0xf2. The tg3_nvram_read()
12006 * call will read from NVRAM and byteswap the data
12007 * according to the byteswapping settings for all
12008 * other register accesses. This ensures the data we
12009 * want will always reside in the lower 16-bits.
12010 * However, the data in NVRAM is in LE format, which
12011 * means the data from the NVRAM read will always be
12012 * opposite the endianness of the CPU. The 16-bit
12013 * byteswap then brings the data to CPU endianness.
12014 */
12015 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012016 return;
12017 }
12018 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070012019 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012020}
12021
12022static void __devinit tg3_get_nvram_info(struct tg3 *tp)
12023{
12024 u32 nvcfg1;
12025
12026 nvcfg1 = tr32(NVRAM_CFG1);
12027 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
Joe Perches63c3a662011-04-26 08:12:10 +000012028 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012029 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012030 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12031 tw32(NVRAM_CFG1, nvcfg1);
12032 }
12033
Matt Carlson6ff6f812011-05-19 12:12:54 +000012034 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
Joe Perches63c3a662011-04-26 08:12:10 +000012035 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012036 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012037 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
12038 tp->nvram_jedecnum = JEDEC_ATMEL;
12039 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012040 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012041 break;
12042 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
12043 tp->nvram_jedecnum = JEDEC_ATMEL;
12044 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
12045 break;
12046 case FLASH_VENDOR_ATMEL_EEPROM:
12047 tp->nvram_jedecnum = JEDEC_ATMEL;
12048 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012049 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012050 break;
12051 case FLASH_VENDOR_ST:
12052 tp->nvram_jedecnum = JEDEC_ST;
12053 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012054 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012055 break;
12056 case FLASH_VENDOR_SAIFUN:
12057 tp->nvram_jedecnum = JEDEC_SAIFUN;
12058 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
12059 break;
12060 case FLASH_VENDOR_SST_SMALL:
12061 case FLASH_VENDOR_SST_LARGE:
12062 tp->nvram_jedecnum = JEDEC_SST;
12063 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
12064 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012065 }
Matt Carlson8590a602009-08-28 12:29:16 +000012066 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012067 tp->nvram_jedecnum = JEDEC_ATMEL;
12068 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012069 tg3_flag_set(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012070 }
12071}
12072
Matt Carlsona1b950d2009-09-01 13:20:17 +000012073static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
12074{
12075 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
12076 case FLASH_5752PAGE_SIZE_256:
12077 tp->nvram_pagesize = 256;
12078 break;
12079 case FLASH_5752PAGE_SIZE_512:
12080 tp->nvram_pagesize = 512;
12081 break;
12082 case FLASH_5752PAGE_SIZE_1K:
12083 tp->nvram_pagesize = 1024;
12084 break;
12085 case FLASH_5752PAGE_SIZE_2K:
12086 tp->nvram_pagesize = 2048;
12087 break;
12088 case FLASH_5752PAGE_SIZE_4K:
12089 tp->nvram_pagesize = 4096;
12090 break;
12091 case FLASH_5752PAGE_SIZE_264:
12092 tp->nvram_pagesize = 264;
12093 break;
12094 case FLASH_5752PAGE_SIZE_528:
12095 tp->nvram_pagesize = 528;
12096 break;
12097 }
12098}
12099
Michael Chan361b4ac2005-04-21 17:11:21 -070012100static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
12101{
12102 u32 nvcfg1;
12103
12104 nvcfg1 = tr32(NVRAM_CFG1);
12105
Michael Chane6af3012005-04-21 17:12:05 -070012106 /* NVRAM protection for TPM */
12107 if (nvcfg1 & (1 << 27))
Joe Perches63c3a662011-04-26 08:12:10 +000012108 tg3_flag_set(tp, PROTECTED_NVRAM);
Michael Chane6af3012005-04-21 17:12:05 -070012109
Michael Chan361b4ac2005-04-21 17:11:21 -070012110 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012111 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
12112 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
12113 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012114 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012115 break;
12116 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12117 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012118 tg3_flag_set(tp, NVRAM_BUFFERED);
12119 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012120 break;
12121 case FLASH_5752VENDOR_ST_M45PE10:
12122 case FLASH_5752VENDOR_ST_M45PE20:
12123 case FLASH_5752VENDOR_ST_M45PE40:
12124 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012125 tg3_flag_set(tp, NVRAM_BUFFERED);
12126 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012127 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070012128 }
12129
Joe Perches63c3a662011-04-26 08:12:10 +000012130 if (tg3_flag(tp, FLASH)) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000012131 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000012132 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070012133 /* For eeprom, set pagesize to maximum eeprom size */
12134 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12135
12136 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12137 tw32(NVRAM_CFG1, nvcfg1);
12138 }
12139}
12140
Michael Chand3c7b882006-03-23 01:28:25 -080012141static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
12142{
Matt Carlson989a9d22007-05-05 11:51:05 -070012143 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080012144
12145 nvcfg1 = tr32(NVRAM_CFG1);
12146
12147 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070012148 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012149 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson989a9d22007-05-05 11:51:05 -070012150 protect = 1;
12151 }
Michael Chand3c7b882006-03-23 01:28:25 -080012152
Matt Carlson989a9d22007-05-05 11:51:05 -070012153 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
12154 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012155 case FLASH_5755VENDOR_ATMEL_FLASH_1:
12156 case FLASH_5755VENDOR_ATMEL_FLASH_2:
12157 case FLASH_5755VENDOR_ATMEL_FLASH_3:
12158 case FLASH_5755VENDOR_ATMEL_FLASH_5:
12159 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012160 tg3_flag_set(tp, NVRAM_BUFFERED);
12161 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012162 tp->nvram_pagesize = 264;
12163 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
12164 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
12165 tp->nvram_size = (protect ? 0x3e200 :
12166 TG3_NVRAM_SIZE_512KB);
12167 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
12168 tp->nvram_size = (protect ? 0x1f200 :
12169 TG3_NVRAM_SIZE_256KB);
12170 else
12171 tp->nvram_size = (protect ? 0x1f200 :
12172 TG3_NVRAM_SIZE_128KB);
12173 break;
12174 case FLASH_5752VENDOR_ST_M45PE10:
12175 case FLASH_5752VENDOR_ST_M45PE20:
12176 case FLASH_5752VENDOR_ST_M45PE40:
12177 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012178 tg3_flag_set(tp, NVRAM_BUFFERED);
12179 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012180 tp->nvram_pagesize = 256;
12181 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
12182 tp->nvram_size = (protect ?
12183 TG3_NVRAM_SIZE_64KB :
12184 TG3_NVRAM_SIZE_128KB);
12185 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
12186 tp->nvram_size = (protect ?
12187 TG3_NVRAM_SIZE_64KB :
12188 TG3_NVRAM_SIZE_256KB);
12189 else
12190 tp->nvram_size = (protect ?
12191 TG3_NVRAM_SIZE_128KB :
12192 TG3_NVRAM_SIZE_512KB);
12193 break;
Michael Chand3c7b882006-03-23 01:28:25 -080012194 }
12195}
12196
Michael Chan1b277772006-03-20 22:27:48 -080012197static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
12198{
12199 u32 nvcfg1;
12200
12201 nvcfg1 = tr32(NVRAM_CFG1);
12202
12203 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012204 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
12205 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
12206 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
12207 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
12208 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012209 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012210 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080012211
Matt Carlson8590a602009-08-28 12:29:16 +000012212 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12213 tw32(NVRAM_CFG1, nvcfg1);
12214 break;
12215 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12216 case FLASH_5755VENDOR_ATMEL_FLASH_1:
12217 case FLASH_5755VENDOR_ATMEL_FLASH_2:
12218 case FLASH_5755VENDOR_ATMEL_FLASH_3:
12219 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012220 tg3_flag_set(tp, NVRAM_BUFFERED);
12221 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012222 tp->nvram_pagesize = 264;
12223 break;
12224 case FLASH_5752VENDOR_ST_M45PE10:
12225 case FLASH_5752VENDOR_ST_M45PE20:
12226 case FLASH_5752VENDOR_ST_M45PE40:
12227 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012228 tg3_flag_set(tp, NVRAM_BUFFERED);
12229 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012230 tp->nvram_pagesize = 256;
12231 break;
Michael Chan1b277772006-03-20 22:27:48 -080012232 }
12233}
12234
Matt Carlson6b91fa02007-10-10 18:01:09 -070012235static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
12236{
12237 u32 nvcfg1, protect = 0;
12238
12239 nvcfg1 = tr32(NVRAM_CFG1);
12240
12241 /* NVRAM protection for TPM */
12242 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012243 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson6b91fa02007-10-10 18:01:09 -070012244 protect = 1;
12245 }
12246
12247 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
12248 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012249 case FLASH_5761VENDOR_ATMEL_ADB021D:
12250 case FLASH_5761VENDOR_ATMEL_ADB041D:
12251 case FLASH_5761VENDOR_ATMEL_ADB081D:
12252 case FLASH_5761VENDOR_ATMEL_ADB161D:
12253 case FLASH_5761VENDOR_ATMEL_MDB021D:
12254 case FLASH_5761VENDOR_ATMEL_MDB041D:
12255 case FLASH_5761VENDOR_ATMEL_MDB081D:
12256 case FLASH_5761VENDOR_ATMEL_MDB161D:
12257 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012258 tg3_flag_set(tp, NVRAM_BUFFERED);
12259 tg3_flag_set(tp, FLASH);
12260 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson8590a602009-08-28 12:29:16 +000012261 tp->nvram_pagesize = 256;
12262 break;
12263 case FLASH_5761VENDOR_ST_A_M45PE20:
12264 case FLASH_5761VENDOR_ST_A_M45PE40:
12265 case FLASH_5761VENDOR_ST_A_M45PE80:
12266 case FLASH_5761VENDOR_ST_A_M45PE16:
12267 case FLASH_5761VENDOR_ST_M_M45PE20:
12268 case FLASH_5761VENDOR_ST_M_M45PE40:
12269 case FLASH_5761VENDOR_ST_M_M45PE80:
12270 case FLASH_5761VENDOR_ST_M_M45PE16:
12271 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012272 tg3_flag_set(tp, NVRAM_BUFFERED);
12273 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012274 tp->nvram_pagesize = 256;
12275 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070012276 }
12277
12278 if (protect) {
12279 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
12280 } else {
12281 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012282 case FLASH_5761VENDOR_ATMEL_ADB161D:
12283 case FLASH_5761VENDOR_ATMEL_MDB161D:
12284 case FLASH_5761VENDOR_ST_A_M45PE16:
12285 case FLASH_5761VENDOR_ST_M_M45PE16:
12286 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
12287 break;
12288 case FLASH_5761VENDOR_ATMEL_ADB081D:
12289 case FLASH_5761VENDOR_ATMEL_MDB081D:
12290 case FLASH_5761VENDOR_ST_A_M45PE80:
12291 case FLASH_5761VENDOR_ST_M_M45PE80:
12292 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12293 break;
12294 case FLASH_5761VENDOR_ATMEL_ADB041D:
12295 case FLASH_5761VENDOR_ATMEL_MDB041D:
12296 case FLASH_5761VENDOR_ST_A_M45PE40:
12297 case FLASH_5761VENDOR_ST_M_M45PE40:
12298 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12299 break;
12300 case FLASH_5761VENDOR_ATMEL_ADB021D:
12301 case FLASH_5761VENDOR_ATMEL_MDB021D:
12302 case FLASH_5761VENDOR_ST_A_M45PE20:
12303 case FLASH_5761VENDOR_ST_M_M45PE20:
12304 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12305 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070012306 }
12307 }
12308}
12309
Michael Chanb5d37722006-09-27 16:06:21 -070012310static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
12311{
12312 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012313 tg3_flag_set(tp, NVRAM_BUFFERED);
Michael Chanb5d37722006-09-27 16:06:21 -070012314 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12315}
12316
Matt Carlson321d32a2008-11-21 17:22:19 -080012317static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
12318{
12319 u32 nvcfg1;
12320
12321 nvcfg1 = tr32(NVRAM_CFG1);
12322
12323 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12324 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
12325 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
12326 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012327 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson321d32a2008-11-21 17:22:19 -080012328 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12329
12330 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12331 tw32(NVRAM_CFG1, nvcfg1);
12332 return;
12333 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12334 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
12335 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
12336 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
12337 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
12338 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
12339 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
12340 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012341 tg3_flag_set(tp, NVRAM_BUFFERED);
12342 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080012343
12344 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12345 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12346 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
12347 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
12348 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12349 break;
12350 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
12351 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
12352 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12353 break;
12354 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
12355 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
12356 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12357 break;
12358 }
12359 break;
12360 case FLASH_5752VENDOR_ST_M45PE10:
12361 case FLASH_5752VENDOR_ST_M45PE20:
12362 case FLASH_5752VENDOR_ST_M45PE40:
12363 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012364 tg3_flag_set(tp, NVRAM_BUFFERED);
12365 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080012366
12367 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12368 case FLASH_5752VENDOR_ST_M45PE10:
12369 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12370 break;
12371 case FLASH_5752VENDOR_ST_M45PE20:
12372 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12373 break;
12374 case FLASH_5752VENDOR_ST_M45PE40:
12375 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12376 break;
12377 }
12378 break;
12379 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012380 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson321d32a2008-11-21 17:22:19 -080012381 return;
12382 }
12383
Matt Carlsona1b950d2009-09-01 13:20:17 +000012384 tg3_nvram_get_pagesize(tp, nvcfg1);
12385 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012386 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012387}
12388
12389
12390static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
12391{
12392 u32 nvcfg1;
12393
12394 nvcfg1 = tr32(NVRAM_CFG1);
12395
12396 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12397 case FLASH_5717VENDOR_ATMEL_EEPROM:
12398 case FLASH_5717VENDOR_MICRO_EEPROM:
12399 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012400 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012401 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12402
12403 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12404 tw32(NVRAM_CFG1, nvcfg1);
12405 return;
12406 case FLASH_5717VENDOR_ATMEL_MDB011D:
12407 case FLASH_5717VENDOR_ATMEL_ADB011B:
12408 case FLASH_5717VENDOR_ATMEL_ADB011D:
12409 case FLASH_5717VENDOR_ATMEL_MDB021D:
12410 case FLASH_5717VENDOR_ATMEL_ADB021B:
12411 case FLASH_5717VENDOR_ATMEL_ADB021D:
12412 case FLASH_5717VENDOR_ATMEL_45USPT:
12413 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012414 tg3_flag_set(tp, NVRAM_BUFFERED);
12415 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012416
12417 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12418 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012419 /* Detect size with tg3_nvram_get_size() */
12420 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012421 case FLASH_5717VENDOR_ATMEL_ADB021B:
12422 case FLASH_5717VENDOR_ATMEL_ADB021D:
12423 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12424 break;
12425 default:
12426 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12427 break;
12428 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012429 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012430 case FLASH_5717VENDOR_ST_M_M25PE10:
12431 case FLASH_5717VENDOR_ST_A_M25PE10:
12432 case FLASH_5717VENDOR_ST_M_M45PE10:
12433 case FLASH_5717VENDOR_ST_A_M45PE10:
12434 case FLASH_5717VENDOR_ST_M_M25PE20:
12435 case FLASH_5717VENDOR_ST_A_M25PE20:
12436 case FLASH_5717VENDOR_ST_M_M45PE20:
12437 case FLASH_5717VENDOR_ST_A_M45PE20:
12438 case FLASH_5717VENDOR_ST_25USPT:
12439 case FLASH_5717VENDOR_ST_45USPT:
12440 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012441 tg3_flag_set(tp, NVRAM_BUFFERED);
12442 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012443
12444 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12445 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012446 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012447 /* Detect size with tg3_nvram_get_size() */
12448 break;
12449 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012450 case FLASH_5717VENDOR_ST_A_M45PE20:
12451 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12452 break;
12453 default:
12454 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12455 break;
12456 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012457 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012458 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012459 tg3_flag_set(tp, NO_NVRAM);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012460 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080012461 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000012462
12463 tg3_nvram_get_pagesize(tp, nvcfg1);
12464 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012465 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson321d32a2008-11-21 17:22:19 -080012466}
12467
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012468static void __devinit tg3_get_5720_nvram_info(struct tg3 *tp)
12469{
12470 u32 nvcfg1, nvmpinstrp;
12471
12472 nvcfg1 = tr32(NVRAM_CFG1);
12473 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
12474
12475 switch (nvmpinstrp) {
12476 case FLASH_5720_EEPROM_HD:
12477 case FLASH_5720_EEPROM_LD:
12478 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012479 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012480
12481 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12482 tw32(NVRAM_CFG1, nvcfg1);
12483 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
12484 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12485 else
12486 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
12487 return;
12488 case FLASH_5720VENDOR_M_ATMEL_DB011D:
12489 case FLASH_5720VENDOR_A_ATMEL_DB011B:
12490 case FLASH_5720VENDOR_A_ATMEL_DB011D:
12491 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12492 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12493 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12494 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12495 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12496 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12497 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12498 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12499 case FLASH_5720VENDOR_ATMEL_45USPT:
12500 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012501 tg3_flag_set(tp, NVRAM_BUFFERED);
12502 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012503
12504 switch (nvmpinstrp) {
12505 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12506 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12507 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12508 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12509 break;
12510 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12511 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12512 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12513 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12514 break;
12515 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12516 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12517 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12518 break;
12519 default:
12520 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12521 break;
12522 }
12523 break;
12524 case FLASH_5720VENDOR_M_ST_M25PE10:
12525 case FLASH_5720VENDOR_M_ST_M45PE10:
12526 case FLASH_5720VENDOR_A_ST_M25PE10:
12527 case FLASH_5720VENDOR_A_ST_M45PE10:
12528 case FLASH_5720VENDOR_M_ST_M25PE20:
12529 case FLASH_5720VENDOR_M_ST_M45PE20:
12530 case FLASH_5720VENDOR_A_ST_M25PE20:
12531 case FLASH_5720VENDOR_A_ST_M45PE20:
12532 case FLASH_5720VENDOR_M_ST_M25PE40:
12533 case FLASH_5720VENDOR_M_ST_M45PE40:
12534 case FLASH_5720VENDOR_A_ST_M25PE40:
12535 case FLASH_5720VENDOR_A_ST_M45PE40:
12536 case FLASH_5720VENDOR_M_ST_M25PE80:
12537 case FLASH_5720VENDOR_M_ST_M45PE80:
12538 case FLASH_5720VENDOR_A_ST_M25PE80:
12539 case FLASH_5720VENDOR_A_ST_M45PE80:
12540 case FLASH_5720VENDOR_ST_25USPT:
12541 case FLASH_5720VENDOR_ST_45USPT:
12542 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012543 tg3_flag_set(tp, NVRAM_BUFFERED);
12544 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012545
12546 switch (nvmpinstrp) {
12547 case FLASH_5720VENDOR_M_ST_M25PE20:
12548 case FLASH_5720VENDOR_M_ST_M45PE20:
12549 case FLASH_5720VENDOR_A_ST_M25PE20:
12550 case FLASH_5720VENDOR_A_ST_M45PE20:
12551 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12552 break;
12553 case FLASH_5720VENDOR_M_ST_M25PE40:
12554 case FLASH_5720VENDOR_M_ST_M45PE40:
12555 case FLASH_5720VENDOR_A_ST_M25PE40:
12556 case FLASH_5720VENDOR_A_ST_M45PE40:
12557 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12558 break;
12559 case FLASH_5720VENDOR_M_ST_M25PE80:
12560 case FLASH_5720VENDOR_M_ST_M45PE80:
12561 case FLASH_5720VENDOR_A_ST_M25PE80:
12562 case FLASH_5720VENDOR_A_ST_M45PE80:
12563 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12564 break;
12565 default:
12566 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12567 break;
12568 }
12569 break;
12570 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012571 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012572 return;
12573 }
12574
12575 tg3_nvram_get_pagesize(tp, nvcfg1);
12576 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012577 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012578}
12579
Linus Torvalds1da177e2005-04-16 15:20:36 -070012580/* Chips other than 5700/5701 use the NVRAM for fetching info. */
12581static void __devinit tg3_nvram_init(struct tg3 *tp)
12582{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012583 tw32_f(GRC_EEPROM_ADDR,
12584 (EEPROM_ADDR_FSM_RESET |
12585 (EEPROM_DEFAULT_CLOCK_PERIOD <<
12586 EEPROM_ADDR_CLKPERD_SHIFT)));
12587
Michael Chan9d57f012006-12-07 00:23:25 -080012588 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012589
12590 /* Enable seeprom accesses. */
12591 tw32_f(GRC_LOCAL_CTRL,
12592 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
12593 udelay(100);
12594
12595 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
12596 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
Joe Perches63c3a662011-04-26 08:12:10 +000012597 tg3_flag_set(tp, NVRAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012598
Michael Chanec41c7d2006-01-17 02:40:55 -080012599 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000012600 netdev_warn(tp->dev,
12601 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000012602 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080012603 return;
12604 }
Michael Chane6af3012005-04-21 17:12:05 -070012605 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012606
Matt Carlson989a9d22007-05-05 11:51:05 -070012607 tp->nvram_size = 0;
12608
Michael Chan361b4ac2005-04-21 17:11:21 -070012609 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
12610 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080012611 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
12612 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070012613 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070012614 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
12615 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080012616 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070012617 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
12618 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070012619 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12620 tg3_get_5906_nvram_info(tp);
Matt Carlsonb703df62009-12-03 08:36:21 +000012621 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
12622 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson321d32a2008-11-21 17:22:19 -080012623 tg3_get_57780_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012624 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
12625 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000012626 tg3_get_5717_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012627 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
12628 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070012629 else
12630 tg3_get_nvram_info(tp);
12631
Matt Carlson989a9d22007-05-05 11:51:05 -070012632 if (tp->nvram_size == 0)
12633 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012634
Michael Chane6af3012005-04-21 17:12:05 -070012635 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080012636 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012637
12638 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000012639 tg3_flag_clear(tp, NVRAM);
12640 tg3_flag_clear(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012641
12642 tg3_get_eeprom_size(tp);
12643 }
12644}
12645
Linus Torvalds1da177e2005-04-16 15:20:36 -070012646static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
12647 u32 offset, u32 len, u8 *buf)
12648{
12649 int i, j, rc = 0;
12650 u32 val;
12651
12652 for (i = 0; i < len; i += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012653 u32 addr;
Matt Carlsona9dc5292009-02-25 14:25:30 +000012654 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012655
12656 addr = offset + i;
12657
12658 memcpy(&data, buf + i, 4);
12659
Matt Carlson62cedd12009-04-20 14:52:29 -070012660 /*
12661 * The SEEPROM interface expects the data to always be opposite
12662 * the native endian format. We accomplish this by reversing
12663 * all the operations that would have been performed on the
12664 * data from a call to tg3_nvram_read_be32().
12665 */
12666 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012667
12668 val = tr32(GRC_EEPROM_ADDR);
12669 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
12670
12671 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
12672 EEPROM_ADDR_READ);
12673 tw32(GRC_EEPROM_ADDR, val |
12674 (0 << EEPROM_ADDR_DEVID_SHIFT) |
12675 (addr & EEPROM_ADDR_ADDR_MASK) |
12676 EEPROM_ADDR_START |
12677 EEPROM_ADDR_WRITE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012678
Michael Chan9d57f012006-12-07 00:23:25 -080012679 for (j = 0; j < 1000; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012680 val = tr32(GRC_EEPROM_ADDR);
12681
12682 if (val & EEPROM_ADDR_COMPLETE)
12683 break;
Michael Chan9d57f012006-12-07 00:23:25 -080012684 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012685 }
12686 if (!(val & EEPROM_ADDR_COMPLETE)) {
12687 rc = -EBUSY;
12688 break;
12689 }
12690 }
12691
12692 return rc;
12693}
12694
12695/* offset and length are dword aligned */
12696static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
12697 u8 *buf)
12698{
12699 int ret = 0;
12700 u32 pagesize = tp->nvram_pagesize;
12701 u32 pagemask = pagesize - 1;
12702 u32 nvram_cmd;
12703 u8 *tmp;
12704
12705 tmp = kmalloc(pagesize, GFP_KERNEL);
12706 if (tmp == NULL)
12707 return -ENOMEM;
12708
12709 while (len) {
12710 int j;
Michael Chane6af3012005-04-21 17:12:05 -070012711 u32 phy_addr, page_off, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012712
12713 phy_addr = offset & ~pagemask;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012714
Linus Torvalds1da177e2005-04-16 15:20:36 -070012715 for (j = 0; j < pagesize; j += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012716 ret = tg3_nvram_read_be32(tp, phy_addr + j,
12717 (__be32 *) (tmp + j));
12718 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012719 break;
12720 }
12721 if (ret)
12722 break;
12723
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012724 page_off = offset & pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012725 size = pagesize;
12726 if (len < size)
12727 size = len;
12728
12729 len -= size;
12730
12731 memcpy(tmp + page_off, buf, size);
12732
12733 offset = offset + (pagesize - page_off);
12734
Michael Chane6af3012005-04-21 17:12:05 -070012735 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012736
12737 /*
12738 * Before we can erase the flash page, we need
12739 * to issue a special "write enable" command.
12740 */
12741 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12742
12743 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12744 break;
12745
12746 /* Erase the target page */
12747 tw32(NVRAM_ADDR, phy_addr);
12748
12749 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
12750 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
12751
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012752 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012753 break;
12754
12755 /* Issue another write enable to start the write. */
12756 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12757
12758 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12759 break;
12760
12761 for (j = 0; j < pagesize; j += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012762 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012763
Al Virob9fc7dc2007-12-17 22:59:57 -080012764 data = *((__be32 *) (tmp + j));
Matt Carlsona9dc5292009-02-25 14:25:30 +000012765
Al Virob9fc7dc2007-12-17 22:59:57 -080012766 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012767
12768 tw32(NVRAM_ADDR, phy_addr + j);
12769
12770 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
12771 NVRAM_CMD_WR;
12772
12773 if (j == 0)
12774 nvram_cmd |= NVRAM_CMD_FIRST;
12775 else if (j == (pagesize - 4))
12776 nvram_cmd |= NVRAM_CMD_LAST;
12777
12778 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12779 break;
12780 }
12781 if (ret)
12782 break;
12783 }
12784
12785 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12786 tg3_nvram_exec_cmd(tp, nvram_cmd);
12787
12788 kfree(tmp);
12789
12790 return ret;
12791}
12792
12793/* offset and length are dword aligned */
12794static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
12795 u8 *buf)
12796{
12797 int i, ret = 0;
12798
12799 for (i = 0; i < len; i += 4, offset += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012800 u32 page_off, phy_addr, nvram_cmd;
12801 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012802
12803 memcpy(&data, buf + i, 4);
Al Virob9fc7dc2007-12-17 22:59:57 -080012804 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012805
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012806 page_off = offset % tp->nvram_pagesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012807
Michael Chan18201802006-03-20 22:29:15 -080012808 phy_addr = tg3_nvram_phys_addr(tp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012809
12810 tw32(NVRAM_ADDR, phy_addr);
12811
12812 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
12813
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012814 if (page_off == 0 || i == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012815 nvram_cmd |= NVRAM_CMD_FIRST;
Michael Chanf6d9a252006-04-29 19:00:24 -070012816 if (page_off == (tp->nvram_pagesize - 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012817 nvram_cmd |= NVRAM_CMD_LAST;
12818
12819 if (i == (len - 4))
12820 nvram_cmd |= NVRAM_CMD_LAST;
12821
Matt Carlson321d32a2008-11-21 17:22:19 -080012822 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
Joe Perches63c3a662011-04-26 08:12:10 +000012823 !tg3_flag(tp, 5755_PLUS) &&
Michael Chan4c987482005-09-05 17:52:38 -070012824 (tp->nvram_jedecnum == JEDEC_ST) &&
12825 (nvram_cmd & NVRAM_CMD_FIRST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012826
12827 if ((ret = tg3_nvram_exec_cmd(tp,
12828 NVRAM_CMD_WREN | NVRAM_CMD_GO |
12829 NVRAM_CMD_DONE)))
12830
12831 break;
12832 }
Joe Perches63c3a662011-04-26 08:12:10 +000012833 if (!tg3_flag(tp, FLASH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012834 /* We always do complete word writes to eeprom. */
12835 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
12836 }
12837
12838 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12839 break;
12840 }
12841 return ret;
12842}
12843
12844/* offset and length are dword aligned */
12845static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
12846{
12847 int ret;
12848
Joe Perches63c3a662011-04-26 08:12:10 +000012849 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
Michael Chan314fba32005-04-21 17:07:04 -070012850 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
12851 ~GRC_LCLCTRL_GPIO_OUTPUT1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012852 udelay(40);
12853 }
12854
Joe Perches63c3a662011-04-26 08:12:10 +000012855 if (!tg3_flag(tp, NVRAM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012856 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
Matt Carlson859a5882010-04-05 10:19:28 +000012857 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012858 u32 grc_mode;
12859
Michael Chanec41c7d2006-01-17 02:40:55 -080012860 ret = tg3_nvram_lock(tp);
12861 if (ret)
12862 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012863
Michael Chane6af3012005-04-21 17:12:05 -070012864 tg3_enable_nvram_access(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000012865 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012866 tw32(NVRAM_WRITE1, 0x406);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012867
12868 grc_mode = tr32(GRC_MODE);
12869 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
12870
Joe Perches63c3a662011-04-26 08:12:10 +000012871 if (tg3_flag(tp, NVRAM_BUFFERED) || !tg3_flag(tp, FLASH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012872 ret = tg3_nvram_write_block_buffered(tp, offset, len,
12873 buf);
Matt Carlson859a5882010-04-05 10:19:28 +000012874 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012875 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
12876 buf);
12877 }
12878
12879 grc_mode = tr32(GRC_MODE);
12880 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
12881
Michael Chane6af3012005-04-21 17:12:05 -070012882 tg3_disable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012883 tg3_nvram_unlock(tp);
12884 }
12885
Joe Perches63c3a662011-04-26 08:12:10 +000012886 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
Michael Chan314fba32005-04-21 17:07:04 -070012887 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012888 udelay(40);
12889 }
12890
12891 return ret;
12892}
12893
12894struct subsys_tbl_ent {
12895 u16 subsys_vendor, subsys_devid;
12896 u32 phy_id;
12897};
12898
Matt Carlson24daf2b2010-02-17 15:17:02 +000012899static struct subsys_tbl_ent subsys_id_to_phy_id[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012900 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012901 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012902 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012903 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012904 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012905 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012906 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012907 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12908 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
12909 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012910 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012911 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012912 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012913 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12914 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
12915 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012916 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012917 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012918 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012919 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012920 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012921 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012922 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012923
12924 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012925 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012926 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012927 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012928 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012929 { TG3PCI_SUBVENDOR_ID_3COM,
12930 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
12931 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012932 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012933 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012934 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012935
12936 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012937 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012938 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012939 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012940 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012941 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012942 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012943 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012944 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012945
12946 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012947 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012948 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012949 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012950 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012951 { TG3PCI_SUBVENDOR_ID_COMPAQ,
12952 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
12953 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012954 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012955 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012956 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012957
12958 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012959 { TG3PCI_SUBVENDOR_ID_IBM,
12960 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012961};
12962
Matt Carlson24daf2b2010-02-17 15:17:02 +000012963static struct subsys_tbl_ent * __devinit tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012964{
12965 int i;
12966
12967 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
12968 if ((subsys_id_to_phy_id[i].subsys_vendor ==
12969 tp->pdev->subsystem_vendor) &&
12970 (subsys_id_to_phy_id[i].subsys_devid ==
12971 tp->pdev->subsystem_device))
12972 return &subsys_id_to_phy_id[i];
12973 }
12974 return NULL;
12975}
12976
Michael Chan7d0c41e2005-04-21 17:06:20 -070012977static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012978{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012979 u32 val;
David S. Millerf49639e2006-06-09 11:58:36 -070012980
Matt Carlson79eb6902010-02-17 15:17:03 +000012981 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012982 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12983
Gary Zambranoa85feb82007-05-05 11:52:19 -070012984 /* Assume an onboard device and WOL capable by default. */
Joe Perches63c3a662011-04-26 08:12:10 +000012985 tg3_flag_set(tp, EEPROM_WRITE_PROT);
12986 tg3_flag_set(tp, WOL_CAP);
David S. Miller72b845e2006-03-14 14:11:48 -080012987
Michael Chanb5d37722006-09-27 16:06:21 -070012988 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080012989 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012990 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
12991 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080012992 }
Matt Carlson0527ba32007-10-10 18:03:30 -070012993 val = tr32(VCPU_CFGSHDW);
12994 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Joe Perches63c3a662011-04-26 08:12:10 +000012995 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson0527ba32007-10-10 18:03:30 -070012996 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000012997 (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012998 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000012999 device_set_wakeup_enable(&tp->pdev->dev, true);
13000 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080013001 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070013002 }
13003
Linus Torvalds1da177e2005-04-16 15:20:36 -070013004 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
13005 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
13006 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070013007 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070013008 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013009
13010 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
13011 tp->nic_sram_data_cfg = nic_cfg;
13012
13013 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
13014 ver >>= NIC_SRAM_DATA_VER_SHIFT;
Matt Carlson6ff6f812011-05-19 12:12:54 +000013015 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13016 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13017 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070013018 (ver > 0) && (ver < 0x100))
13019 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
13020
Matt Carlsona9daf362008-05-25 23:49:44 -070013021 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
13022 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
13023
Linus Torvalds1da177e2005-04-16 15:20:36 -070013024 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
13025 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
13026 eeprom_phy_serdes = 1;
13027
13028 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
13029 if (nic_phy_id != 0) {
13030 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
13031 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
13032
13033 eeprom_phy_id = (id1 >> 16) << 10;
13034 eeprom_phy_id |= (id2 & 0xfc00) << 16;
13035 eeprom_phy_id |= (id2 & 0x03ff) << 0;
13036 } else
13037 eeprom_phy_id = 0;
13038
Michael Chan7d0c41e2005-04-21 17:06:20 -070013039 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070013040 if (eeprom_phy_serdes) {
Joe Perches63c3a662011-04-26 08:12:10 +000013041 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013042 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000013043 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013044 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070013045 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070013046
Joe Perches63c3a662011-04-26 08:12:10 +000013047 if (tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013048 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
13049 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070013050 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070013051 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
13052
13053 switch (led_cfg) {
13054 default:
13055 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
13056 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13057 break;
13058
13059 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
13060 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
13061 break;
13062
13063 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
13064 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070013065
13066 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
13067 * read on some older 5700/5701 bootcode.
13068 */
13069 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
13070 ASIC_REV_5700 ||
13071 GET_ASIC_REV(tp->pci_chip_rev_id) ==
13072 ASIC_REV_5701)
13073 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13074
Linus Torvalds1da177e2005-04-16 15:20:36 -070013075 break;
13076
13077 case SHASTA_EXT_LED_SHARED:
13078 tp->led_ctrl = LED_CTRL_MODE_SHARED;
13079 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
13080 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
13081 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
13082 LED_CTRL_MODE_PHY_2);
13083 break;
13084
13085 case SHASTA_EXT_LED_MAC:
13086 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
13087 break;
13088
13089 case SHASTA_EXT_LED_COMBO:
13090 tp->led_ctrl = LED_CTRL_MODE_COMBO;
13091 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
13092 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
13093 LED_CTRL_MODE_PHY_2);
13094 break;
13095
Stephen Hemminger855e1112008-04-16 16:37:28 -070013096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013097
13098 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13099 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
13100 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
13101 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
13102
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013103 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
13104 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080013105
Michael Chan9d26e212006-12-07 00:21:14 -080013106 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Joe Perches63c3a662011-04-26 08:12:10 +000013107 tg3_flag_set(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080013108 if ((tp->pdev->subsystem_vendor ==
13109 PCI_VENDOR_ID_ARIMA) &&
13110 (tp->pdev->subsystem_device == 0x205a ||
13111 tp->pdev->subsystem_device == 0x2063))
Joe Perches63c3a662011-04-26 08:12:10 +000013112 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080013113 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000013114 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
13115 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080013116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013117
13118 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +000013119 tg3_flag_set(tp, ENABLE_ASF);
13120 if (tg3_flag(tp, 5750_PLUS))
13121 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013122 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080013123
13124 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013125 tg3_flag(tp, 5750_PLUS))
13126 tg3_flag_set(tp, ENABLE_APE);
Matt Carlsonb2b98d42008-11-03 16:52:32 -080013127
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013128 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070013129 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
Joe Perches63c3a662011-04-26 08:12:10 +000013130 tg3_flag_clear(tp, WOL_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013131
Joe Perches63c3a662011-04-26 08:12:10 +000013132 if (tg3_flag(tp, WOL_CAP) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013133 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013134 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013135 device_set_wakeup_enable(&tp->pdev->dev, true);
13136 }
Matt Carlson0527ba32007-10-10 18:03:30 -070013137
Linus Torvalds1da177e2005-04-16 15:20:36 -070013138 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013139 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013140
13141 /* serdes signal pre-emphasis in register 0x590 set by */
13142 /* bootcode if bit 18 is set */
13143 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013144 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070013145
Joe Perches63c3a662011-04-26 08:12:10 +000013146 if ((tg3_flag(tp, 57765_PLUS) ||
13147 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
13148 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080013149 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013150 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080013151
Joe Perches63c3a662011-04-26 08:12:10 +000013152 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson8c69b1e2010-08-02 11:26:00 +000013153 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +000013154 !tg3_flag(tp, 57765_PLUS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070013155 u32 cfg3;
13156
13157 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
13158 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
Joe Perches63c3a662011-04-26 08:12:10 +000013159 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson8ed5d972007-05-07 00:25:49 -070013160 }
Matt Carlsona9daf362008-05-25 23:49:44 -070013161
Matt Carlson14417062010-02-17 15:16:59 +000013162 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
Joe Perches63c3a662011-04-26 08:12:10 +000013163 tg3_flag_set(tp, RGMII_INBAND_DISABLE);
Matt Carlsona9daf362008-05-25 23:49:44 -070013164 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000013165 tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
Matt Carlsona9daf362008-05-25 23:49:44 -070013166 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000013167 tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013168 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080013169done:
Joe Perches63c3a662011-04-26 08:12:10 +000013170 if (tg3_flag(tp, WOL_CAP))
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000013171 device_set_wakeup_enable(&tp->pdev->dev,
Joe Perches63c3a662011-04-26 08:12:10 +000013172 tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000013173 else
13174 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070013175}
13176
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013177static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
13178{
13179 int i;
13180 u32 val;
13181
13182 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
13183 tw32(OTP_CTRL, cmd);
13184
13185 /* Wait for up to 1 ms for command to execute. */
13186 for (i = 0; i < 100; i++) {
13187 val = tr32(OTP_STATUS);
13188 if (val & OTP_STATUS_CMD_DONE)
13189 break;
13190 udelay(10);
13191 }
13192
13193 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
13194}
13195
13196/* Read the gphy configuration from the OTP region of the chip. The gphy
13197 * configuration is a 32-bit value that straddles the alignment boundary.
13198 * We do two 32-bit reads and then shift and merge the results.
13199 */
13200static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
13201{
13202 u32 bhalf_otp, thalf_otp;
13203
13204 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
13205
13206 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
13207 return 0;
13208
13209 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
13210
13211 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
13212 return 0;
13213
13214 thalf_otp = tr32(OTP_READ_DATA);
13215
13216 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
13217
13218 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
13219 return 0;
13220
13221 bhalf_otp = tr32(OTP_READ_DATA);
13222
13223 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
13224}
13225
Matt Carlsone256f8a2011-03-09 16:58:24 +000013226static void __devinit tg3_phy_init_link_config(struct tg3 *tp)
13227{
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +000013228 u32 adv = ADVERTISED_Autoneg;
Matt Carlsone256f8a2011-03-09 16:58:24 +000013229
13230 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
13231 adv |= ADVERTISED_1000baseT_Half |
13232 ADVERTISED_1000baseT_Full;
13233
13234 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
13235 adv |= ADVERTISED_100baseT_Half |
13236 ADVERTISED_100baseT_Full |
13237 ADVERTISED_10baseT_Half |
13238 ADVERTISED_10baseT_Full |
13239 ADVERTISED_TP;
13240 else
13241 adv |= ADVERTISED_FIBRE;
13242
13243 tp->link_config.advertising = adv;
13244 tp->link_config.speed = SPEED_INVALID;
13245 tp->link_config.duplex = DUPLEX_INVALID;
13246 tp->link_config.autoneg = AUTONEG_ENABLE;
13247 tp->link_config.active_speed = SPEED_INVALID;
13248 tp->link_config.active_duplex = DUPLEX_INVALID;
13249 tp->link_config.orig_speed = SPEED_INVALID;
13250 tp->link_config.orig_duplex = DUPLEX_INVALID;
13251 tp->link_config.orig_autoneg = AUTONEG_INVALID;
13252}
13253
Michael Chan7d0c41e2005-04-21 17:06:20 -070013254static int __devinit tg3_phy_probe(struct tg3 *tp)
13255{
13256 u32 hw_phy_id_1, hw_phy_id_2;
13257 u32 hw_phy_id, hw_phy_id_masked;
13258 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013259
Matt Carlsone256f8a2011-03-09 16:58:24 +000013260 /* flow control autonegotiation is default behavior */
Joe Perches63c3a662011-04-26 08:12:10 +000013261 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsone256f8a2011-03-09 16:58:24 +000013262 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
13263
Joe Perches63c3a662011-04-26 08:12:10 +000013264 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013265 return tg3_phy_init(tp);
13266
Linus Torvalds1da177e2005-04-16 15:20:36 -070013267 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010013268 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013269 */
13270 err = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000013271 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000013272 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013273 } else {
13274 /* Now read the physical PHY_ID from the chip and verify
13275 * that it is sane. If it doesn't look good, we fall back
13276 * to either the hard-coded table based PHY_ID and failing
13277 * that the value found in the eeprom area.
13278 */
13279 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
13280 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
13281
13282 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
13283 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
13284 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
13285
Matt Carlson79eb6902010-02-17 15:17:03 +000013286 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013287 }
13288
Matt Carlson79eb6902010-02-17 15:17:03 +000013289 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013290 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000013291 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013292 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070013293 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013294 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013295 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000013296 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070013297 /* Do nothing, phy ID already set up in
13298 * tg3_get_eeprom_hw_cfg().
13299 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070013300 } else {
13301 struct subsys_tbl_ent *p;
13302
13303 /* No eeprom signature? Try the hardcoded
13304 * subsys device table.
13305 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013306 p = tg3_lookup_by_subsys(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013307 if (!p)
13308 return -ENODEV;
13309
13310 tp->phy_id = p->phy_id;
13311 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000013312 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013313 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013314 }
13315 }
13316
Matt Carlsona6b68da2010-12-06 08:28:52 +000013317 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Matt Carlson5baa5e92011-07-20 10:20:53 +000013318 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13319 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720 ||
13320 (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +000013321 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0) ||
13322 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
13323 tp->pci_chip_rev_id != CHIPREV_ID_57765_A0)))
Matt Carlson52b02d02010-10-14 10:37:41 +000013324 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
13325
Matt Carlsone256f8a2011-03-09 16:58:24 +000013326 tg3_phy_init_link_config(tp);
13327
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013328 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013329 !tg3_flag(tp, ENABLE_APE) &&
13330 !tg3_flag(tp, ENABLE_ASF)) {
Matt Carlsone2bf73e2011-12-08 14:40:15 +000013331 u32 bmsr, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013332
13333 tg3_readphy(tp, MII_BMSR, &bmsr);
13334 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
13335 (bmsr & BMSR_LSTATUS))
13336 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013337
Linus Torvalds1da177e2005-04-16 15:20:36 -070013338 err = tg3_phy_reset(tp);
13339 if (err)
13340 return err;
13341
Matt Carlson42b64a42011-05-19 12:12:49 +000013342 tg3_phy_set_wirespeed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013343
Matt Carlsone2bf73e2011-12-08 14:40:15 +000013344 if (!tg3_phy_copper_an_config_ok(tp, &dummy)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000013345 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
13346 tp->link_config.flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013347
13348 tg3_writephy(tp, MII_BMCR,
13349 BMCR_ANENABLE | BMCR_ANRESTART);
13350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013351 }
13352
13353skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000013354 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013355 err = tg3_init_5401phy_dsp(tp);
13356 if (err)
13357 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013358
Linus Torvalds1da177e2005-04-16 15:20:36 -070013359 err = tg3_init_5401phy_dsp(tp);
13360 }
13361
Linus Torvalds1da177e2005-04-16 15:20:36 -070013362 return err;
13363}
13364
Matt Carlson184b8902010-04-05 10:19:25 +000013365static void __devinit tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013366{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013367 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013368 unsigned int block_end, rosize, len;
Matt Carlson535a4902011-07-20 10:20:56 +000013369 u32 vpdlen;
Matt Carlson184b8902010-04-05 10:19:25 +000013370 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013371
Matt Carlson535a4902011-07-20 10:20:56 +000013372 vpd_data = (u8 *)tg3_vpd_readblock(tp, &vpdlen);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013373 if (!vpd_data)
13374 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013375
Matt Carlson535a4902011-07-20 10:20:56 +000013376 i = pci_vpd_find_tag(vpd_data, 0, vpdlen, PCI_VPD_LRDT_RO_DATA);
Matt Carlson4181b2c2010-02-26 14:04:45 +000013377 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013378 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013379
13380 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
13381 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
13382 i += PCI_VPD_LRDT_TAG_SIZE;
13383
Matt Carlson535a4902011-07-20 10:20:56 +000013384 if (block_end > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000013385 goto out_not_found;
13386
Matt Carlson184b8902010-04-05 10:19:25 +000013387 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13388 PCI_VPD_RO_KEYWORD_MFR_ID);
13389 if (j > 0) {
13390 len = pci_vpd_info_field_size(&vpd_data[j]);
13391
13392 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13393 if (j + len > block_end || len != 4 ||
13394 memcmp(&vpd_data[j], "1028", 4))
13395 goto partno;
13396
13397 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13398 PCI_VPD_RO_KEYWORD_VENDOR0);
13399 if (j < 0)
13400 goto partno;
13401
13402 len = pci_vpd_info_field_size(&vpd_data[j]);
13403
13404 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13405 if (j + len > block_end)
13406 goto partno;
13407
13408 memcpy(tp->fw_ver, &vpd_data[j], len);
Matt Carlson535a4902011-07-20 10:20:56 +000013409 strncat(tp->fw_ver, " bc ", vpdlen - len - 1);
Matt Carlson184b8902010-04-05 10:19:25 +000013410 }
13411
13412partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000013413 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13414 PCI_VPD_RO_KEYWORD_PARTNO);
13415 if (i < 0)
13416 goto out_not_found;
13417
13418 len = pci_vpd_info_field_size(&vpd_data[i]);
13419
13420 i += PCI_VPD_INFO_FLD_HDR_SIZE;
13421 if (len > TG3_BPN_SIZE ||
Matt Carlson535a4902011-07-20 10:20:56 +000013422 (len + i) > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000013423 goto out_not_found;
13424
13425 memcpy(tp->board_part_number, &vpd_data[i], len);
13426
Linus Torvalds1da177e2005-04-16 15:20:36 -070013427out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013428 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000013429 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013430 return;
13431
13432out_no_vpd:
Matt Carlson37a949c2010-09-30 10:34:33 +000013433 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
13434 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717)
13435 strcpy(tp->board_part_number, "BCM5717");
13436 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
13437 strcpy(tp->board_part_number, "BCM5718");
13438 else
13439 goto nomatch;
13440 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
13441 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
13442 strcpy(tp->board_part_number, "BCM57780");
13443 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
13444 strcpy(tp->board_part_number, "BCM57760");
13445 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
13446 strcpy(tp->board_part_number, "BCM57790");
13447 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
13448 strcpy(tp->board_part_number, "BCM57788");
13449 else
13450 goto nomatch;
13451 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
13452 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
13453 strcpy(tp->board_part_number, "BCM57761");
13454 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
13455 strcpy(tp->board_part_number, "BCM57765");
13456 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
13457 strcpy(tp->board_part_number, "BCM57781");
13458 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
13459 strcpy(tp->board_part_number, "BCM57785");
13460 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
13461 strcpy(tp->board_part_number, "BCM57791");
13462 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13463 strcpy(tp->board_part_number, "BCM57795");
13464 else
13465 goto nomatch;
13466 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070013467 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000013468 } else {
13469nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070013470 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000013471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013472}
13473
Matt Carlson9c8a6202007-10-21 16:16:08 -070013474static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
13475{
13476 u32 val;
13477
Matt Carlsone4f34112009-02-25 14:25:00 +000013478 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013479 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013480 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013481 val != 0)
13482 return 0;
13483
13484 return 1;
13485}
13486
Matt Carlsonacd9c112009-02-25 14:26:33 +000013487static void __devinit tg3_read_bc_ver(struct tg3 *tp)
13488{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013489 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000013490 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013491 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013492
13493 if (tg3_nvram_read(tp, 0xc, &offset) ||
13494 tg3_nvram_read(tp, 0x4, &start))
13495 return;
13496
13497 offset = tg3_nvram_logical_addr(tp, offset);
13498
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013499 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013500 return;
13501
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013502 if ((val & 0xfc000000) == 0x0c000000) {
13503 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013504 return;
13505
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013506 if (val == 0)
13507 newver = true;
13508 }
13509
Matt Carlson75f99362010-04-05 10:19:24 +000013510 dst_off = strlen(tp->fw_ver);
13511
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013512 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000013513 if (TG3_VER_SIZE - dst_off < 16 ||
13514 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013515 return;
13516
13517 offset = offset + ver_offset - start;
13518 for (i = 0; i < 16; i += 4) {
13519 __be32 v;
13520 if (tg3_nvram_read_be32(tp, offset + i, &v))
13521 return;
13522
Matt Carlson75f99362010-04-05 10:19:24 +000013523 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013524 }
13525 } else {
13526 u32 major, minor;
13527
13528 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
13529 return;
13530
13531 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
13532 TG3_NVM_BCVER_MAJSFT;
13533 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000013534 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
13535 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013536 }
13537}
13538
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013539static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
13540{
13541 u32 val, major, minor;
13542
13543 /* Use native endian representation */
13544 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
13545 return;
13546
13547 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
13548 TG3_NVM_HWSB_CFG1_MAJSFT;
13549 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
13550 TG3_NVM_HWSB_CFG1_MINSFT;
13551
13552 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
13553}
13554
Matt Carlsondfe00d72008-11-21 17:19:41 -080013555static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
13556{
13557 u32 offset, major, minor, build;
13558
Matt Carlson75f99362010-04-05 10:19:24 +000013559 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013560
13561 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
13562 return;
13563
13564 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
13565 case TG3_EEPROM_SB_REVISION_0:
13566 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
13567 break;
13568 case TG3_EEPROM_SB_REVISION_2:
13569 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
13570 break;
13571 case TG3_EEPROM_SB_REVISION_3:
13572 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
13573 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000013574 case TG3_EEPROM_SB_REVISION_4:
13575 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
13576 break;
13577 case TG3_EEPROM_SB_REVISION_5:
13578 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
13579 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000013580 case TG3_EEPROM_SB_REVISION_6:
13581 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
13582 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013583 default:
13584 return;
13585 }
13586
Matt Carlsone4f34112009-02-25 14:25:00 +000013587 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080013588 return;
13589
13590 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
13591 TG3_EEPROM_SB_EDH_BLD_SHFT;
13592 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
13593 TG3_EEPROM_SB_EDH_MAJ_SHFT;
13594 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
13595
13596 if (minor > 99 || build > 26)
13597 return;
13598
Matt Carlson75f99362010-04-05 10:19:24 +000013599 offset = strlen(tp->fw_ver);
13600 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
13601 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013602
13603 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000013604 offset = strlen(tp->fw_ver);
13605 if (offset < TG3_VER_SIZE - 1)
13606 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013607 }
13608}
13609
Matt Carlsonacd9c112009-02-25 14:26:33 +000013610static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080013611{
13612 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013613 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070013614
13615 for (offset = TG3_NVM_DIR_START;
13616 offset < TG3_NVM_DIR_END;
13617 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000013618 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013619 return;
13620
13621 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
13622 break;
13623 }
13624
13625 if (offset == TG3_NVM_DIR_END)
13626 return;
13627
Joe Perches63c3a662011-04-26 08:12:10 +000013628 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013629 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000013630 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013631 return;
13632
Matt Carlsone4f34112009-02-25 14:25:00 +000013633 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013634 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013635 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013636 return;
13637
13638 offset += val - start;
13639
Matt Carlsonacd9c112009-02-25 14:26:33 +000013640 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013641
Matt Carlsonacd9c112009-02-25 14:26:33 +000013642 tp->fw_ver[vlen++] = ',';
13643 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070013644
13645 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000013646 __be32 v;
13647 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013648 return;
13649
Al Virob9fc7dc2007-12-17 22:59:57 -080013650 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013651
Matt Carlsonacd9c112009-02-25 14:26:33 +000013652 if (vlen > TG3_VER_SIZE - sizeof(v)) {
13653 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013654 break;
13655 }
13656
Matt Carlsonacd9c112009-02-25 14:26:33 +000013657 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
13658 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013659 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000013660}
13661
Matt Carlson7fd76442009-02-25 14:27:20 +000013662static void __devinit tg3_read_dash_ver(struct tg3 *tp)
13663{
13664 int vlen;
13665 u32 apedata;
Matt Carlsonecc79642010-08-02 11:26:01 +000013666 char *fwtype;
Matt Carlson7fd76442009-02-25 14:27:20 +000013667
Joe Perches63c3a662011-04-26 08:12:10 +000013668 if (!tg3_flag(tp, ENABLE_APE) || !tg3_flag(tp, ENABLE_ASF))
Matt Carlson7fd76442009-02-25 14:27:20 +000013669 return;
13670
13671 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
13672 if (apedata != APE_SEG_SIG_MAGIC)
13673 return;
13674
13675 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
13676 if (!(apedata & APE_FW_STATUS_READY))
13677 return;
13678
13679 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
13680
Matt Carlsondc6d0742010-09-15 08:59:55 +000013681 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI) {
Joe Perches63c3a662011-04-26 08:12:10 +000013682 tg3_flag_set(tp, APE_HAS_NCSI);
Matt Carlsonecc79642010-08-02 11:26:01 +000013683 fwtype = "NCSI";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013684 } else {
Matt Carlsonecc79642010-08-02 11:26:01 +000013685 fwtype = "DASH";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013686 }
Matt Carlsonecc79642010-08-02 11:26:01 +000013687
Matt Carlson7fd76442009-02-25 14:27:20 +000013688 vlen = strlen(tp->fw_ver);
13689
Matt Carlsonecc79642010-08-02 11:26:01 +000013690 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
13691 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000013692 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
13693 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
13694 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
13695 (apedata & APE_FW_VERSION_BLDMSK));
13696}
13697
Matt Carlsonacd9c112009-02-25 14:26:33 +000013698static void __devinit tg3_read_fw_ver(struct tg3 *tp)
13699{
13700 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000013701 bool vpd_vers = false;
13702
13703 if (tp->fw_ver[0] != 0)
13704 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013705
Joe Perches63c3a662011-04-26 08:12:10 +000013706 if (tg3_flag(tp, NO_NVRAM)) {
Matt Carlson75f99362010-04-05 10:19:24 +000013707 strcat(tp->fw_ver, "sb");
Matt Carlsondf259d82009-04-20 06:57:14 +000013708 return;
13709 }
13710
Matt Carlsonacd9c112009-02-25 14:26:33 +000013711 if (tg3_nvram_read(tp, 0, &val))
13712 return;
13713
13714 if (val == TG3_EEPROM_MAGIC)
13715 tg3_read_bc_ver(tp);
13716 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
13717 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013718 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
13719 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013720 else
13721 return;
13722
Matt Carlsonc9cab242011-07-13 09:27:27 +000013723 if (vpd_vers)
Matt Carlson75f99362010-04-05 10:19:24 +000013724 goto done;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013725
Matt Carlsonc9cab242011-07-13 09:27:27 +000013726 if (tg3_flag(tp, ENABLE_APE)) {
13727 if (tg3_flag(tp, ENABLE_ASF))
13728 tg3_read_dash_ver(tp);
13729 } else if (tg3_flag(tp, ENABLE_ASF)) {
13730 tg3_read_mgmtfw_ver(tp);
13731 }
Matt Carlson9c8a6202007-10-21 16:16:08 -070013732
Matt Carlson75f99362010-04-05 10:19:24 +000013733done:
Matt Carlson9c8a6202007-10-21 16:16:08 -070013734 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080013735}
13736
Michael Chan7544b092007-05-05 13:08:32 -070013737static struct pci_dev * __devinit tg3_find_peer(struct tg3 *);
13738
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013739static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
13740{
Joe Perches63c3a662011-04-26 08:12:10 +000013741 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013742 return TG3_RX_RET_MAX_SIZE_5717;
Joe Perches63c3a662011-04-26 08:12:10 +000013743 else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013744 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013745 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000013746 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013747}
13748
Matt Carlson41434702011-03-09 16:58:22 +000013749static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080013750 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
13751 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
13752 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
13753 { },
13754};
13755
Linus Torvalds1da177e2005-04-16 15:20:36 -070013756static int __devinit tg3_get_invariants(struct tg3 *tp)
13757{
Linus Torvalds1da177e2005-04-16 15:20:36 -070013758 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013759 u32 pci_state_reg, grc_misc_cfg;
13760 u32 val;
13761 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013762 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013763
Linus Torvalds1da177e2005-04-16 15:20:36 -070013764 /* Force memory write invalidate off. If we leave it on,
13765 * then on 5700_BX chips we have to enable a workaround.
13766 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
13767 * to match the cacheline size. The Broadcom driver have this
13768 * workaround but turns MWI off all the times so never uses
13769 * it. This seems to suggest that the workaround is insufficient.
13770 */
13771 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13772 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
13773 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13774
Matt Carlson16821282011-07-13 09:27:28 +000013775 /* Important! -- Make sure register accesses are byteswapped
13776 * correctly. Also, for those chips that require it, make
13777 * sure that indirect register accesses are enabled before
13778 * the first operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013779 */
13780 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13781 &misc_ctrl_reg);
Matt Carlson16821282011-07-13 09:27:28 +000013782 tp->misc_host_ctrl |= (misc_ctrl_reg &
13783 MISC_HOST_CTRL_CHIPREV);
13784 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13785 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013786
13787 tp->pci_chip_rev_id = (misc_ctrl_reg >>
13788 MISC_HOST_CTRL_CHIPREV_SHIFT);
Matt Carlson795d01c2007-10-07 23:28:17 -070013789 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
13790 u32 prod_id_asic_rev;
13791
Matt Carlson5001e2f2009-11-13 13:03:51 +000013792 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
13793 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013794 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
13795 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013796 pci_read_config_dword(tp->pdev,
13797 TG3PCI_GEN2_PRODID_ASICREV,
13798 &prod_id_asic_rev);
Matt Carlsonb703df62009-12-03 08:36:21 +000013799 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
13800 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
13801 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
13802 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
13803 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
13804 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13805 pci_read_config_dword(tp->pdev,
13806 TG3PCI_GEN15_PRODID_ASICREV,
13807 &prod_id_asic_rev);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013808 else
13809 pci_read_config_dword(tp->pdev, TG3PCI_PRODID_ASICREV,
13810 &prod_id_asic_rev);
13811
Matt Carlson321d32a2008-11-21 17:22:19 -080013812 tp->pci_chip_rev_id = prod_id_asic_rev;
Matt Carlson795d01c2007-10-07 23:28:17 -070013813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013814
Michael Chanff645be2005-04-21 17:09:53 -070013815 /* Wrong chip ID in 5752 A0. This code can be removed later
13816 * as A0 is not in production.
13817 */
13818 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
13819 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
13820
Michael Chan68929142005-08-09 20:17:14 -070013821 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
13822 * we need to disable memory and use config. cycles
13823 * only to access all registers. The 5702/03 chips
13824 * can mistakenly decode the special cycles from the
13825 * ICH chipsets as memory write cycles, causing corruption
13826 * of register and memory space. Only certain ICH bridges
13827 * will drive special cycles with non-zero data during the
13828 * address phase which can fall within the 5703's address
13829 * range. This is not an ICH bug as the PCI spec allows
13830 * non-zero address during special cycles. However, only
13831 * these ICH bridges are known to drive non-zero addresses
13832 * during special cycles.
13833 *
13834 * Since special cycles do not cross PCI bridges, we only
13835 * enable this workaround if the 5703 is on the secondary
13836 * bus of these ICH bridges.
13837 */
13838 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
13839 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
13840 static struct tg3_dev_id {
13841 u32 vendor;
13842 u32 device;
13843 u32 rev;
13844 } ich_chipsets[] = {
13845 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
13846 PCI_ANY_ID },
13847 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
13848 PCI_ANY_ID },
13849 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
13850 0xa },
13851 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
13852 PCI_ANY_ID },
13853 { },
13854 };
13855 struct tg3_dev_id *pci_id = &ich_chipsets[0];
13856 struct pci_dev *bridge = NULL;
13857
13858 while (pci_id->vendor != 0) {
13859 bridge = pci_get_device(pci_id->vendor, pci_id->device,
13860 bridge);
13861 if (!bridge) {
13862 pci_id++;
13863 continue;
13864 }
13865 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070013866 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070013867 continue;
13868 }
13869 if (bridge->subordinate &&
13870 (bridge->subordinate->number ==
13871 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013872 tg3_flag_set(tp, ICH_WORKAROUND);
Michael Chan68929142005-08-09 20:17:14 -070013873 pci_dev_put(bridge);
13874 break;
13875 }
13876 }
13877 }
13878
Matt Carlson6ff6f812011-05-19 12:12:54 +000013879 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Matt Carlson41588ba2008-04-19 18:12:33 -070013880 static struct tg3_dev_id {
13881 u32 vendor;
13882 u32 device;
13883 } bridge_chipsets[] = {
13884 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
13885 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
13886 { },
13887 };
13888 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
13889 struct pci_dev *bridge = NULL;
13890
13891 while (pci_id->vendor != 0) {
13892 bridge = pci_get_device(pci_id->vendor,
13893 pci_id->device,
13894 bridge);
13895 if (!bridge) {
13896 pci_id++;
13897 continue;
13898 }
13899 if (bridge->subordinate &&
13900 (bridge->subordinate->number <=
13901 tp->pdev->bus->number) &&
13902 (bridge->subordinate->subordinate >=
13903 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013904 tg3_flag_set(tp, 5701_DMA_BUG);
Matt Carlson41588ba2008-04-19 18:12:33 -070013905 pci_dev_put(bridge);
13906 break;
13907 }
13908 }
13909 }
13910
Michael Chan4a29cc22006-03-19 13:21:12 -080013911 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
13912 * DMA addresses > 40-bit. This bridge may have other additional
13913 * 57xx devices behind it in some 4-port NIC designs for example.
13914 * Any tg3 device found behind the bridge will also need the 40-bit
13915 * DMA workaround.
13916 */
Michael Chana4e2b342005-10-26 15:46:52 -070013917 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
13918 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Joe Perches63c3a662011-04-26 08:12:10 +000013919 tg3_flag_set(tp, 5780_CLASS);
13920 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4cf78e42005-07-25 12:29:19 -070013921 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a5882010-04-05 10:19:28 +000013922 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080013923 struct pci_dev *bridge = NULL;
13924
13925 do {
13926 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
13927 PCI_DEVICE_ID_SERVERWORKS_EPB,
13928 bridge);
13929 if (bridge && bridge->subordinate &&
13930 (bridge->subordinate->number <=
13931 tp->pdev->bus->number) &&
13932 (bridge->subordinate->subordinate >=
13933 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013934 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4a29cc22006-03-19 13:21:12 -080013935 pci_dev_put(bridge);
13936 break;
13937 }
13938 } while (bridge);
13939 }
Michael Chan4cf78e42005-07-25 12:29:19 -070013940
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013941 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Matt Carlson3a1e19d2011-07-13 09:27:32 +000013942 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)
Michael Chan7544b092007-05-05 13:08:32 -070013943 tp->pdev_peer = tg3_find_peer(tp);
13944
Matt Carlsonc885e822010-08-02 11:25:57 +000013945 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013946 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13947 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Joe Perches63c3a662011-04-26 08:12:10 +000013948 tg3_flag_set(tp, 5717_PLUS);
Matt Carlson0a58d662011-04-05 14:22:45 +000013949
13950 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013951 tg3_flag(tp, 5717_PLUS))
13952 tg3_flag_set(tp, 57765_PLUS);
Matt Carlsonc885e822010-08-02 11:25:57 +000013953
Matt Carlson321d32a2008-11-21 17:22:19 -080013954 /* Intentionally exclude ASIC_REV_5906 */
13955 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Michael Chand9ab5ad2006-03-20 22:27:35 -080013956 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013957 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013958 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013959 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013960 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013961 tg3_flag(tp, 57765_PLUS))
13962 tg3_flag_set(tp, 5755_PLUS);
Matt Carlson321d32a2008-11-21 17:22:19 -080013963
13964 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13965 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Michael Chanb5d37722006-09-27 16:06:21 -070013966 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013967 tg3_flag(tp, 5755_PLUS) ||
13968 tg3_flag(tp, 5780_CLASS))
13969 tg3_flag_set(tp, 5750_PLUS);
John W. Linville6708e5c2005-04-21 17:00:52 -070013970
Matt Carlson6ff6f812011-05-19 12:12:54 +000013971 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013972 tg3_flag(tp, 5750_PLUS))
13973 tg3_flag_set(tp, 5705_PLUS);
John W. Linville1b440c562005-04-21 17:03:18 -070013974
Matt Carlson507399f2009-11-13 13:03:37 +000013975 /* Determine TSO capabilities */
Matt Carlsona0512942011-07-27 14:20:54 +000013976 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0)
Matt Carlson4d163b72011-01-25 15:58:48 +000013977 ; /* Do nothing. HW bug. */
Joe Perches63c3a662011-04-26 08:12:10 +000013978 else if (tg3_flag(tp, 57765_PLUS))
13979 tg3_flag_set(tp, HW_TSO_3);
13980 else if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000013981 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000013982 tg3_flag_set(tp, HW_TSO_2);
13983 else if (tg3_flag(tp, 5750_PLUS)) {
13984 tg3_flag_set(tp, HW_TSO_1);
13985 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013986 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
13987 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
Joe Perches63c3a662011-04-26 08:12:10 +000013988 tg3_flag_clear(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013989 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13990 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13991 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000013992 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013993 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
13994 tp->fw_needed = FIRMWARE_TG3TSO5;
13995 else
13996 tp->fw_needed = FIRMWARE_TG3TSO;
13997 }
13998
Matt Carlsondabc5c62011-05-19 12:12:52 +000013999 /* Selectively allow TSO based on operating conditions */
Matt Carlson6ff6f812011-05-19 12:12:54 +000014000 if (tg3_flag(tp, HW_TSO_1) ||
14001 tg3_flag(tp, HW_TSO_2) ||
14002 tg3_flag(tp, HW_TSO_3) ||
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000014003 tp->fw_needed) {
14004 /* For firmware TSO, assume ASF is disabled.
14005 * We'll disable TSO later if we discover ASF
14006 * is enabled in tg3_get_eeprom_hw_cfg().
14007 */
Matt Carlsondabc5c62011-05-19 12:12:52 +000014008 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000014009 } else {
Matt Carlsondabc5c62011-05-19 12:12:52 +000014010 tg3_flag_clear(tp, TSO_CAPABLE);
14011 tg3_flag_clear(tp, TSO_BUG);
14012 tp->fw_needed = NULL;
14013 }
14014
14015 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
14016 tp->fw_needed = FIRMWARE_TG3;
14017
Matt Carlson507399f2009-11-13 13:03:37 +000014018 tp->irq_max = 1;
14019
Joe Perches63c3a662011-04-26 08:12:10 +000014020 if (tg3_flag(tp, 5750_PLUS)) {
14021 tg3_flag_set(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070014022 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
14023 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
14024 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
14025 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
14026 tp->pdev_peer == tp->pdev))
Joe Perches63c3a662011-04-26 08:12:10 +000014027 tg3_flag_clear(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070014028
Joe Perches63c3a662011-04-26 08:12:10 +000014029 if (tg3_flag(tp, 5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070014030 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000014031 tg3_flag_set(tp, 1SHOT_MSI);
Michael Chan52c0fd82006-06-29 20:15:54 -070014032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014033
Joe Perches63c3a662011-04-26 08:12:10 +000014034 if (tg3_flag(tp, 57765_PLUS)) {
14035 tg3_flag_set(tp, SUPPORT_MSIX);
Matt Carlson507399f2009-11-13 13:03:37 +000014036 tp->irq_max = TG3_IRQ_MAX_VECS;
14037 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014038 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000014039
Matt Carlson2ffcc982011-05-19 12:12:44 +000014040 if (tg3_flag(tp, 5755_PLUS))
Joe Perches63c3a662011-04-26 08:12:10 +000014041 tg3_flag_set(tp, SHORT_DMA_BUG);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014042
Matt Carlsone31aa982011-07-27 14:20:53 +000014043 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona4cb4282011-12-14 11:09:58 +000014044 tp->dma_limit = TG3_TX_BD_DMA_MAX_4K;
Matt Carlsone31aa982011-07-27 14:20:53 +000014045
Matt Carlsonfa6b2aa2011-11-21 15:01:19 +000014046 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14047 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
14048 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Joe Perches63c3a662011-04-26 08:12:10 +000014049 tg3_flag_set(tp, LRG_PROD_RING_CAP);
Matt Carlsonde9f5232011-04-05 14:22:43 +000014050
Joe Perches63c3a662011-04-26 08:12:10 +000014051 if (tg3_flag(tp, 57765_PLUS) &&
Matt Carlsona0512942011-07-27 14:20:54 +000014052 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0)
Joe Perches63c3a662011-04-26 08:12:10 +000014053 tg3_flag_set(tp, USE_JUMBO_BDFLAG);
Matt Carlsonb703df62009-12-03 08:36:21 +000014054
Joe Perches63c3a662011-04-26 08:12:10 +000014055 if (!tg3_flag(tp, 5705_PLUS) ||
14056 tg3_flag(tp, 5780_CLASS) ||
14057 tg3_flag(tp, USE_JUMBO_BDFLAG))
14058 tg3_flag_set(tp, JUMBO_CAPABLE);
Michael Chan0f893dc2005-07-25 12:30:38 -070014059
Matt Carlson52f44902008-11-21 17:17:04 -080014060 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14061 &pci_state_reg);
14062
Jon Mason708ebb32011-06-27 12:56:50 +000014063 if (pci_is_pcie(tp->pdev)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014064 u16 lnkctl;
14065
Joe Perches63c3a662011-04-26 08:12:10 +000014066 tg3_flag_set(tp, PCI_EXPRESS);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080014067
Matt Carlson2c55a3d2011-11-28 09:41:04 +000014068 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0) {
14069 int readrq = pcie_get_readrq(tp->pdev);
14070 if (readrq > 2048)
14071 pcie_set_readrq(tp->pdev, 2048);
14072 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -080014073
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014074 pci_read_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +000014075 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014076 &lnkctl);
14077 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
Matt Carlson7196cd62011-05-19 16:02:44 +000014078 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
14079 ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000014080 tg3_flag_clear(tp, HW_TSO_2);
Matt Carlsondabc5c62011-05-19 12:12:52 +000014081 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson7196cd62011-05-19 16:02:44 +000014082 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014083 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014084 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000014085 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
14086 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Joe Perches63c3a662011-04-26 08:12:10 +000014087 tg3_flag_set(tp, CLKREQ_BUG);
Matt Carlson614b05902010-01-20 16:58:02 +000014088 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000014089 tg3_flag_set(tp, L1PLLPD_EN);
Michael Chanc7835a72006-11-15 21:14:42 -080014090 }
Matt Carlson52f44902008-11-21 17:17:04 -080014091 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Jon Mason708ebb32011-06-27 12:56:50 +000014092 /* BCM5785 devices are effectively PCIe devices, and should
14093 * follow PCIe codepaths, but do not have a PCIe capabilities
14094 * section.
Matt Carlson93a700a2011-08-31 11:44:54 +000014095 */
Joe Perches63c3a662011-04-26 08:12:10 +000014096 tg3_flag_set(tp, PCI_EXPRESS);
14097 } else if (!tg3_flag(tp, 5705_PLUS) ||
14098 tg3_flag(tp, 5780_CLASS)) {
Matt Carlson52f44902008-11-21 17:17:04 -080014099 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
14100 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000014101 dev_err(&tp->pdev->dev,
14102 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080014103 return -EIO;
14104 }
14105
14106 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
Joe Perches63c3a662011-04-26 08:12:10 +000014107 tg3_flag_set(tp, PCIX_MODE);
Matt Carlson52f44902008-11-21 17:17:04 -080014108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014109
Michael Chan399de502005-10-03 14:02:39 -070014110 /* If we have an AMD 762 or VIA K8T800 chipset, write
14111 * reordering to the mailbox registers done by the host
14112 * controller can cause major troubles. We read back from
14113 * every mailbox register write to force the writes to be
14114 * posted to the chip in order.
14115 */
Matt Carlson41434702011-03-09 16:58:22 +000014116 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Joe Perches63c3a662011-04-26 08:12:10 +000014117 !tg3_flag(tp, PCI_EXPRESS))
14118 tg3_flag_set(tp, MBOX_WRITE_REORDER);
Michael Chan399de502005-10-03 14:02:39 -070014119
Matt Carlson69fc4052008-12-21 20:19:57 -080014120 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
14121 &tp->pci_cacheline_sz);
14122 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
14123 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014124 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14125 tp->pci_lat_timer < 64) {
14126 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080014127 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
14128 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014129 }
14130
Matt Carlson16821282011-07-13 09:27:28 +000014131 /* Important! -- It is critical that the PCI-X hw workaround
14132 * situation is decided before the first MMIO register access.
14133 */
Matt Carlson52f44902008-11-21 17:17:04 -080014134 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
14135 /* 5700 BX chips need to have their TX producer index
14136 * mailboxes written twice to workaround a bug.
14137 */
Joe Perches63c3a662011-04-26 08:12:10 +000014138 tg3_flag_set(tp, TXD_MBOX_HWBUG);
Matt Carlson9974a352007-10-07 23:27:28 -070014139
Matt Carlson52f44902008-11-21 17:17:04 -080014140 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014141 *
14142 * The workaround is to use indirect register accesses
14143 * for all chip writes not to mailbox registers.
14144 */
Joe Perches63c3a662011-04-26 08:12:10 +000014145 if (tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014146 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014147
Joe Perches63c3a662011-04-26 08:12:10 +000014148 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014149
14150 /* The chip can have it's power management PCI config
14151 * space registers clobbered due to this bug.
14152 * So explicitly force the chip into D0 here.
14153 */
Matt Carlson9974a352007-10-07 23:27:28 -070014154 pci_read_config_dword(tp->pdev,
14155 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014156 &pm_reg);
14157 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
14158 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070014159 pci_write_config_dword(tp->pdev,
14160 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014161 pm_reg);
14162
14163 /* Also, force SERR#/PERR# in PCI command. */
14164 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
14165 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
14166 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
14167 }
14168 }
14169
Linus Torvalds1da177e2005-04-16 15:20:36 -070014170 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000014171 tg3_flag_set(tp, PCI_HIGH_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014172 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000014173 tg3_flag_set(tp, PCI_32BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014174
14175 /* Chip-specific fixup from Broadcom driver */
14176 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
14177 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
14178 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
14179 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
14180 }
14181
Michael Chan1ee582d2005-08-09 20:16:46 -070014182 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070014183 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070014184 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070014185 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070014186 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070014187 tp->write32_tx_mbox = tg3_write32;
14188 tp->write32_rx_mbox = tg3_write32;
14189
14190 /* Various workaround register access methods */
Joe Perches63c3a662011-04-26 08:12:10 +000014191 if (tg3_flag(tp, PCIX_TARGET_HWBUG))
Michael Chan1ee582d2005-08-09 20:16:46 -070014192 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070014193 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014194 (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson98efd8a2007-05-05 12:47:25 -070014195 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
14196 /*
14197 * Back to back register writes can cause problems on these
14198 * chips, the workaround is to read back all reg writes
14199 * except those to mailbox regs.
14200 *
14201 * See tg3_write_indirect_reg32().
14202 */
Michael Chan1ee582d2005-08-09 20:16:46 -070014203 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070014204 }
14205
Joe Perches63c3a662011-04-26 08:12:10 +000014206 if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
Michael Chan1ee582d2005-08-09 20:16:46 -070014207 tp->write32_tx_mbox = tg3_write32_tx_mbox;
Joe Perches63c3a662011-04-26 08:12:10 +000014208 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Michael Chan1ee582d2005-08-09 20:16:46 -070014209 tp->write32_rx_mbox = tg3_write_flush_reg32;
14210 }
Michael Chan20094932005-08-09 20:16:32 -070014211
Joe Perches63c3a662011-04-26 08:12:10 +000014212 if (tg3_flag(tp, ICH_WORKAROUND)) {
Michael Chan68929142005-08-09 20:17:14 -070014213 tp->read32 = tg3_read_indirect_reg32;
14214 tp->write32 = tg3_write_indirect_reg32;
14215 tp->read32_mbox = tg3_read_indirect_mbox;
14216 tp->write32_mbox = tg3_write_indirect_mbox;
14217 tp->write32_tx_mbox = tg3_write_indirect_mbox;
14218 tp->write32_rx_mbox = tg3_write_indirect_mbox;
14219
14220 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070014221 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070014222
14223 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
14224 pci_cmd &= ~PCI_COMMAND_MEMORY;
14225 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
14226 }
Michael Chanb5d37722006-09-27 16:06:21 -070014227 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
14228 tp->read32_mbox = tg3_read32_mbox_5906;
14229 tp->write32_mbox = tg3_write32_mbox_5906;
14230 tp->write32_tx_mbox = tg3_write32_mbox_5906;
14231 tp->write32_rx_mbox = tg3_write32_mbox_5906;
14232 }
Michael Chan68929142005-08-09 20:17:14 -070014233
Michael Chanbbadf502006-04-06 21:46:34 -070014234 if (tp->write32 == tg3_write_indirect_reg32 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014235 (tg3_flag(tp, PCIX_MODE) &&
Michael Chanbbadf502006-04-06 21:46:34 -070014236 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070014237 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Joe Perches63c3a662011-04-26 08:12:10 +000014238 tg3_flag_set(tp, SRAM_USE_CONFIG);
Michael Chanbbadf502006-04-06 21:46:34 -070014239
Matt Carlson16821282011-07-13 09:27:28 +000014240 /* The memory arbiter has to be enabled in order for SRAM accesses
14241 * to succeed. Normally on powerup the tg3 chip firmware will make
14242 * sure it is enabled, but other entities such as system netboot
14243 * code might disable it.
14244 */
14245 val = tr32(MEMARB_MODE);
14246 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
14247
Matt Carlson9dc5e342011-11-04 09:15:02 +000014248 tp->pci_fn = PCI_FUNC(tp->pdev->devfn) & 3;
14249 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
14250 tg3_flag(tp, 5780_CLASS)) {
14251 if (tg3_flag(tp, PCIX_MODE)) {
14252 pci_read_config_dword(tp->pdev,
14253 tp->pcix_cap + PCI_X_STATUS,
14254 &val);
14255 tp->pci_fn = val & 0x7;
14256 }
14257 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
14258 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
14259 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) ==
14260 NIC_SRAM_CPMUSTAT_SIG) {
14261 tp->pci_fn = val & TG3_CPMU_STATUS_FMSK_5717;
14262 tp->pci_fn = tp->pci_fn ? 1 : 0;
14263 }
14264 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
14265 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
14266 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
14267 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) ==
14268 NIC_SRAM_CPMUSTAT_SIG) {
14269 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5719) >>
14270 TG3_CPMU_STATUS_FSHFT_5719;
14271 }
Matt Carlson69f11c92011-07-13 09:27:30 +000014272 }
14273
Michael Chan7d0c41e2005-04-21 17:06:20 -070014274 /* Get eeprom hw config before calling tg3_set_power_state().
Joe Perches63c3a662011-04-26 08:12:10 +000014275 * In particular, the TG3_FLAG_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070014276 * determined before calling tg3_set_power_state() so that
14277 * we know whether or not to switch out of Vaux power.
14278 * When the flag is set, it means that GPIO1 is used for eeprom
14279 * write protect and also implies that it is a LOM where GPIOs
14280 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040014281 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070014282 tg3_get_eeprom_hw_cfg(tp);
14283
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000014284 if (tp->fw_needed && tg3_flag(tp, ENABLE_ASF)) {
14285 tg3_flag_clear(tp, TSO_CAPABLE);
14286 tg3_flag_clear(tp, TSO_BUG);
14287 tp->fw_needed = NULL;
14288 }
14289
Joe Perches63c3a662011-04-26 08:12:10 +000014290 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070014291 /* Allow reads and writes to the
14292 * APE register and memory space.
14293 */
14294 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc2010-06-05 17:24:30 +000014295 PCISTATE_ALLOW_APE_SHMEM_WR |
14296 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070014297 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
14298 pci_state_reg);
Matt Carlsonc9cab242011-07-13 09:27:27 +000014299
14300 tg3_ape_lock_init(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070014301 }
14302
Matt Carlson9936bcf2007-10-10 18:03:07 -070014303 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson57e69832008-05-25 23:48:31 -070014304 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014305 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014306 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014307 tg3_flag(tp, 57765_PLUS))
14308 tg3_flag_set(tp, CPMU_PRESENT);
Matt Carlsond30cdd22007-10-07 23:28:35 -070014309
Matt Carlson16821282011-07-13 09:27:28 +000014310 /* Set up tp->grc_local_ctrl before calling
14311 * tg3_pwrsrc_switch_to_vmain(). GPIO1 driven high
14312 * will bring 5700's external PHY out of reset.
Michael Chan314fba32005-04-21 17:07:04 -070014313 * It is also used as eeprom write protect on LOMs.
14314 */
14315 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
Matt Carlson6ff6f812011-05-19 12:12:54 +000014316 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014317 tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan314fba32005-04-21 17:07:04 -070014318 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
14319 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070014320 /* Unused GPIO3 must be driven as output on 5752 because there
14321 * are no pull-up resistors on unused GPIO pins.
14322 */
14323 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
14324 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070014325
Matt Carlson321d32a2008-11-21 17:22:19 -080014326 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsoncb4ed1f2010-01-20 16:58:09 +000014327 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
14328 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Michael Chanaf36e6b2006-03-23 01:28:06 -080014329 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
14330
Matt Carlson8d519ab2009-04-20 06:58:01 +000014331 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
14332 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070014333 /* Turn off the debug UART. */
14334 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014335 if (tg3_flag(tp, IS_NIC))
Matt Carlson5f0c4a32008-06-09 15:41:12 -070014336 /* Keep VMain power. */
14337 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
14338 GRC_LCLCTRL_GPIO_OUTPUT0;
14339 }
14340
Matt Carlson16821282011-07-13 09:27:28 +000014341 /* Switch out of Vaux if it is a NIC */
14342 tg3_pwrsrc_switch_to_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014343
Linus Torvalds1da177e2005-04-16 15:20:36 -070014344 /* Derive initial jumbo mode from MTU assigned in
14345 * ether_setup() via the alloc_etherdev() call
14346 */
Joe Perches63c3a662011-04-26 08:12:10 +000014347 if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
14348 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014349
14350 /* Determine WakeOnLan speed to use. */
14351 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14352 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
14353 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
14354 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
Joe Perches63c3a662011-04-26 08:12:10 +000014355 tg3_flag_clear(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014356 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000014357 tg3_flag_set(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014358 }
14359
Matt Carlson7f97a4b2009-08-25 10:10:03 +000014360 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014361 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000014362
Linus Torvalds1da177e2005-04-16 15:20:36 -070014363 /* A few boards don't want Ethernet@WireSpeed phy feature */
Matt Carlson6ff6f812011-05-19 12:12:54 +000014364 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14365 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070014366 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070014367 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014368 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
14369 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
14370 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014371
14372 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
14373 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014374 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014375 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014376 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014377
Joe Perches63c3a662011-04-26 08:12:10 +000014378 if (tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014379 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080014380 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014381 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014382 !tg3_flag(tp, 57765_PLUS)) {
Michael Chanc424cb22006-04-29 18:56:34 -070014383 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070014384 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070014385 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
14386 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080014387 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
14388 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014389 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080014390 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014391 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080014392 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014393 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070014394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014395
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014396 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
14397 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
14398 tp->phy_otp = tg3_read_otp_phycfg(tp);
14399 if (tp->phy_otp == 0)
14400 tp->phy_otp = TG3_OTP_DEFAULT;
14401 }
14402
Joe Perches63c3a662011-04-26 08:12:10 +000014403 if (tg3_flag(tp, CPMU_PRESENT))
Matt Carlson8ef21422008-05-02 16:47:53 -070014404 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
14405 else
14406 tp->mi_mode = MAC_MI_MODE_BASE;
14407
Linus Torvalds1da177e2005-04-16 15:20:36 -070014408 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014409 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
14410 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
14411 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
14412
Matt Carlson4d958472011-04-20 07:57:35 +000014413 /* Set these bits to enable statistics workaround. */
14414 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14415 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
14416 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0) {
14417 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
14418 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
14419 }
14420
Matt Carlson321d32a2008-11-21 17:22:19 -080014421 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
14422 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Joe Perches63c3a662011-04-26 08:12:10 +000014423 tg3_flag_set(tp, USE_PHYLIB);
Matt Carlson57e69832008-05-25 23:48:31 -070014424
Matt Carlson158d7ab2008-05-29 01:37:54 -070014425 err = tg3_mdio_init(tp);
14426 if (err)
14427 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014428
14429 /* Initialize data/descriptor byte/word swapping. */
14430 val = tr32(GRC_MODE);
Matt Carlsonf2096f92011-04-05 14:22:48 +000014431 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
14432 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
14433 GRC_MODE_WORD_SWAP_B2HRX_DATA |
14434 GRC_MODE_B2HRX_ENABLE |
14435 GRC_MODE_HTX2B_ENABLE |
14436 GRC_MODE_HOST_STACKUP);
14437 else
14438 val &= GRC_MODE_HOST_STACKUP;
14439
Linus Torvalds1da177e2005-04-16 15:20:36 -070014440 tw32(GRC_MODE, val | tp->grc_mode);
14441
14442 tg3_switch_clocks(tp);
14443
14444 /* Clear this out for sanity. */
14445 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
14446
14447 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14448 &pci_state_reg);
14449 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014450 !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014451 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
14452
14453 if (chiprevid == CHIPREV_ID_5701_A0 ||
14454 chiprevid == CHIPREV_ID_5701_B0 ||
14455 chiprevid == CHIPREV_ID_5701_B2 ||
14456 chiprevid == CHIPREV_ID_5701_B5) {
14457 void __iomem *sram_base;
14458
14459 /* Write some dummy words into the SRAM status block
14460 * area, see if it reads back correctly. If the return
14461 * value is bad, force enable the PCIX workaround.
14462 */
14463 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
14464
14465 writel(0x00000000, sram_base);
14466 writel(0x00000000, sram_base + 4);
14467 writel(0xffffffff, sram_base + 4);
14468 if (readl(sram_base) != 0x00000000)
Joe Perches63c3a662011-04-26 08:12:10 +000014469 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014470 }
14471 }
14472
14473 udelay(50);
14474 tg3_nvram_init(tp);
14475
14476 grc_misc_cfg = tr32(GRC_MISC_CFG);
14477 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
14478
Linus Torvalds1da177e2005-04-16 15:20:36 -070014479 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14480 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
14481 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
Joe Perches63c3a662011-04-26 08:12:10 +000014482 tg3_flag_set(tp, IS_5788);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014483
Joe Perches63c3a662011-04-26 08:12:10 +000014484 if (!tg3_flag(tp, IS_5788) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +000014485 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014486 tg3_flag_set(tp, TAGGED_STATUS);
14487 if (tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070014488 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
14489 HOSTCC_MODE_CLRTICK_TXBD);
14490
14491 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
14492 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
14493 tp->misc_host_ctrl);
14494 }
14495
Matt Carlson3bda1252008-08-15 14:08:22 -070014496 /* Preserve the APE MAC_MODE bits */
Joe Perches63c3a662011-04-26 08:12:10 +000014497 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +000014498 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070014499 else
Matt Carlson6e01b202011-08-19 13:58:20 +000014500 tp->mac_mode = 0;
Matt Carlson3bda1252008-08-15 14:08:22 -070014501
Linus Torvalds1da177e2005-04-16 15:20:36 -070014502 /* these are limited to 10/100 only */
14503 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14504 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
14505 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14506 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14507 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
14508 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
14509 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
14510 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14511 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
Michael Chan676917d2006-12-07 00:20:22 -080014512 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
14513 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014514 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
Matt Carlsond1101142010-02-17 15:16:55 +000014515 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
14516 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014517 (tp->phy_flags & TG3_PHYFLG_IS_FET))
14518 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014519
14520 err = tg3_phy_probe(tp);
14521 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014522 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014523 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014524 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014525 }
14526
Matt Carlson184b8902010-04-05 10:19:25 +000014527 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080014528 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014529
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014530 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
14531 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014532 } else {
14533 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014534 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014535 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014536 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014537 }
14538
14539 /* 5700 {AX,BX} chips have a broken status block link
14540 * change bit implementation, so we must use the
14541 * status register in those cases.
14542 */
14543 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014544 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014545 else
Joe Perches63c3a662011-04-26 08:12:10 +000014546 tg3_flag_clear(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014547
14548 /* The led_ctrl is set during tg3_phy_probe, here we might
14549 * have to force the link status polling mechanism based
14550 * upon subsystem IDs.
14551 */
14552 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070014553 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014554 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
14555 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Joe Perches63c3a662011-04-26 08:12:10 +000014556 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014557 }
14558
14559 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014560 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Joe Perches63c3a662011-04-26 08:12:10 +000014561 tg3_flag_set(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014562 else
Joe Perches63c3a662011-04-26 08:12:10 +000014563 tg3_flag_clear(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014564
Eric Dumazet9205fd92011-11-18 06:47:01 +000014565 tp->rx_offset = NET_SKB_PAD + NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014566 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014567 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014568 tg3_flag(tp, PCIX_MODE)) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000014569 tp->rx_offset = NET_SKB_PAD;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014570#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000014571 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014572#endif
14573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014574
Matt Carlson2c49a442010-09-30 10:34:35 +000014575 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
14576 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000014577 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
14578
Matt Carlson2c49a442010-09-30 10:34:35 +000014579 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070014580
14581 /* Increment the rx prod index on the rx std ring by at most
14582 * 8 for these chips to workaround hw errata.
14583 */
14584 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
14585 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
14586 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
14587 tp->rx_std_max_post = 8;
14588
Joe Perches63c3a662011-04-26 08:12:10 +000014589 if (tg3_flag(tp, ASPM_WORKAROUND))
Matt Carlson8ed5d972007-05-07 00:25:49 -070014590 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
14591 PCIE_PWR_MGMT_L1_THRESH_MSK;
14592
Linus Torvalds1da177e2005-04-16 15:20:36 -070014593 return err;
14594}
14595
David S. Miller49b6e95f2007-03-29 01:38:42 -070014596#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014597static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
14598{
14599 struct net_device *dev = tp->dev;
14600 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014601 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070014602 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014603 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014604
David S. Miller49b6e95f2007-03-29 01:38:42 -070014605 addr = of_get_property(dp, "local-mac-address", &len);
14606 if (addr && len == 6) {
14607 memcpy(dev->dev_addr, addr, 6);
14608 memcpy(dev->perm_addr, dev->dev_addr, 6);
14609 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014610 }
14611 return -ENODEV;
14612}
14613
14614static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
14615{
14616 struct net_device *dev = tp->dev;
14617
14618 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070014619 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014620 return 0;
14621}
14622#endif
14623
14624static int __devinit tg3_get_device_address(struct tg3 *tp)
14625{
14626 struct net_device *dev = tp->dev;
14627 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080014628 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014629
David S. Miller49b6e95f2007-03-29 01:38:42 -070014630#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014631 if (!tg3_get_macaddr_sparc(tp))
14632 return 0;
14633#endif
14634
14635 mac_offset = 0x7c;
Matt Carlson6ff6f812011-05-19 12:12:54 +000014636 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014637 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014638 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
14639 mac_offset = 0xcc;
14640 if (tg3_nvram_lock(tp))
14641 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
14642 else
14643 tg3_nvram_unlock(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000014644 } else if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson69f11c92011-07-13 09:27:30 +000014645 if (tp->pci_fn & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014646 mac_offset = 0xcc;
Matt Carlson69f11c92011-07-13 09:27:30 +000014647 if (tp->pci_fn > 1)
Matt Carlsona50d0792010-06-05 17:24:37 +000014648 mac_offset += 0x18c;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014649 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070014650 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014651
14652 /* First try to get it from MAC address mailbox. */
14653 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
14654 if ((hi >> 16) == 0x484b) {
14655 dev->dev_addr[0] = (hi >> 8) & 0xff;
14656 dev->dev_addr[1] = (hi >> 0) & 0xff;
14657
14658 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
14659 dev->dev_addr[2] = (lo >> 24) & 0xff;
14660 dev->dev_addr[3] = (lo >> 16) & 0xff;
14661 dev->dev_addr[4] = (lo >> 8) & 0xff;
14662 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014663
Michael Chan008652b2006-03-27 23:14:53 -080014664 /* Some old bootcode may report a 0 MAC address in SRAM */
14665 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
14666 }
14667 if (!addr_ok) {
14668 /* Next, try NVRAM. */
Joe Perches63c3a662011-04-26 08:12:10 +000014669 if (!tg3_flag(tp, NO_NVRAM) &&
Matt Carlsondf259d82009-04-20 06:57:14 +000014670 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000014671 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070014672 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
14673 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080014674 }
14675 /* Finally just fetch it out of the MAC control regs. */
14676 else {
14677 hi = tr32(MAC_ADDR_0_HIGH);
14678 lo = tr32(MAC_ADDR_0_LOW);
14679
14680 dev->dev_addr[5] = lo & 0xff;
14681 dev->dev_addr[4] = (lo >> 8) & 0xff;
14682 dev->dev_addr[3] = (lo >> 16) & 0xff;
14683 dev->dev_addr[2] = (lo >> 24) & 0xff;
14684 dev->dev_addr[1] = hi & 0xff;
14685 dev->dev_addr[0] = (hi >> 8) & 0xff;
14686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014687 }
14688
14689 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070014690#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014691 if (!tg3_get_default_macaddr_sparc(tp))
14692 return 0;
14693#endif
14694 return -EINVAL;
14695 }
John W. Linville2ff43692005-09-12 14:44:20 -070014696 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014697 return 0;
14698}
14699
David S. Miller59e6b432005-05-18 22:50:10 -070014700#define BOUNDARY_SINGLE_CACHELINE 1
14701#define BOUNDARY_MULTI_CACHELINE 2
14702
14703static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
14704{
14705 int cacheline_size;
14706 u8 byte;
14707 int goal;
14708
14709 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
14710 if (byte == 0)
14711 cacheline_size = 1024;
14712 else
14713 cacheline_size = (int) byte * 4;
14714
14715 /* On 5703 and later chips, the boundary bits have no
14716 * effect.
14717 */
14718 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14719 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014720 !tg3_flag(tp, PCI_EXPRESS))
David S. Miller59e6b432005-05-18 22:50:10 -070014721 goto out;
14722
14723#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
14724 goal = BOUNDARY_MULTI_CACHELINE;
14725#else
14726#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
14727 goal = BOUNDARY_SINGLE_CACHELINE;
14728#else
14729 goal = 0;
14730#endif
14731#endif
14732
Joe Perches63c3a662011-04-26 08:12:10 +000014733 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014734 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
14735 goto out;
14736 }
14737
David S. Miller59e6b432005-05-18 22:50:10 -070014738 if (!goal)
14739 goto out;
14740
14741 /* PCI controllers on most RISC systems tend to disconnect
14742 * when a device tries to burst across a cache-line boundary.
14743 * Therefore, letting tg3 do so just wastes PCI bandwidth.
14744 *
14745 * Unfortunately, for PCI-E there are only limited
14746 * write-side controls for this, and thus for reads
14747 * we will still get the disconnects. We'll also waste
14748 * these PCI cycles for both read and write for chips
14749 * other than 5700 and 5701 which do not implement the
14750 * boundary bits.
14751 */
Joe Perches63c3a662011-04-26 08:12:10 +000014752 if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014753 switch (cacheline_size) {
14754 case 16:
14755 case 32:
14756 case 64:
14757 case 128:
14758 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14759 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
14760 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
14761 } else {
14762 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14763 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14764 }
14765 break;
14766
14767 case 256:
14768 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
14769 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
14770 break;
14771
14772 default:
14773 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14774 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14775 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014776 }
Joe Perches63c3a662011-04-26 08:12:10 +000014777 } else if (tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014778 switch (cacheline_size) {
14779 case 16:
14780 case 32:
14781 case 64:
14782 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14783 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14784 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
14785 break;
14786 }
14787 /* fallthrough */
14788 case 128:
14789 default:
14790 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14791 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
14792 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014793 }
David S. Miller59e6b432005-05-18 22:50:10 -070014794 } else {
14795 switch (cacheline_size) {
14796 case 16:
14797 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14798 val |= (DMA_RWCTRL_READ_BNDRY_16 |
14799 DMA_RWCTRL_WRITE_BNDRY_16);
14800 break;
14801 }
14802 /* fallthrough */
14803 case 32:
14804 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14805 val |= (DMA_RWCTRL_READ_BNDRY_32 |
14806 DMA_RWCTRL_WRITE_BNDRY_32);
14807 break;
14808 }
14809 /* fallthrough */
14810 case 64:
14811 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14812 val |= (DMA_RWCTRL_READ_BNDRY_64 |
14813 DMA_RWCTRL_WRITE_BNDRY_64);
14814 break;
14815 }
14816 /* fallthrough */
14817 case 128:
14818 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14819 val |= (DMA_RWCTRL_READ_BNDRY_128 |
14820 DMA_RWCTRL_WRITE_BNDRY_128);
14821 break;
14822 }
14823 /* fallthrough */
14824 case 256:
14825 val |= (DMA_RWCTRL_READ_BNDRY_256 |
14826 DMA_RWCTRL_WRITE_BNDRY_256);
14827 break;
14828 case 512:
14829 val |= (DMA_RWCTRL_READ_BNDRY_512 |
14830 DMA_RWCTRL_WRITE_BNDRY_512);
14831 break;
14832 case 1024:
14833 default:
14834 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
14835 DMA_RWCTRL_WRITE_BNDRY_1024);
14836 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014837 }
David S. Miller59e6b432005-05-18 22:50:10 -070014838 }
14839
14840out:
14841 return val;
14842}
14843
Linus Torvalds1da177e2005-04-16 15:20:36 -070014844static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
14845{
14846 struct tg3_internal_buffer_desc test_desc;
14847 u32 sram_dma_descs;
14848 int i, ret;
14849
14850 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
14851
14852 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
14853 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
14854 tw32(RDMAC_STATUS, 0);
14855 tw32(WDMAC_STATUS, 0);
14856
14857 tw32(BUFMGR_MODE, 0);
14858 tw32(FTQ_RESET, 0);
14859
14860 test_desc.addr_hi = ((u64) buf_dma) >> 32;
14861 test_desc.addr_lo = buf_dma & 0xffffffff;
14862 test_desc.nic_mbuf = 0x00002100;
14863 test_desc.len = size;
14864
14865 /*
14866 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
14867 * the *second* time the tg3 driver was getting loaded after an
14868 * initial scan.
14869 *
14870 * Broadcom tells me:
14871 * ...the DMA engine is connected to the GRC block and a DMA
14872 * reset may affect the GRC block in some unpredictable way...
14873 * The behavior of resets to individual blocks has not been tested.
14874 *
14875 * Broadcom noted the GRC reset will also reset all sub-components.
14876 */
14877 if (to_device) {
14878 test_desc.cqid_sqid = (13 << 8) | 2;
14879
14880 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
14881 udelay(40);
14882 } else {
14883 test_desc.cqid_sqid = (16 << 8) | 7;
14884
14885 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
14886 udelay(40);
14887 }
14888 test_desc.flags = 0x00000005;
14889
14890 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
14891 u32 val;
14892
14893 val = *(((u32 *)&test_desc) + i);
14894 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
14895 sram_dma_descs + (i * sizeof(u32)));
14896 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
14897 }
14898 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
14899
Matt Carlson859a5882010-04-05 10:19:28 +000014900 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014901 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a5882010-04-05 10:19:28 +000014902 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070014903 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014904
14905 ret = -ENODEV;
14906 for (i = 0; i < 40; i++) {
14907 u32 val;
14908
14909 if (to_device)
14910 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
14911 else
14912 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
14913 if ((val & 0xffff) == sram_dma_descs) {
14914 ret = 0;
14915 break;
14916 }
14917
14918 udelay(100);
14919 }
14920
14921 return ret;
14922}
14923
David S. Millerded73402005-05-23 13:59:47 -070014924#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070014925
Matt Carlson41434702011-03-09 16:58:22 +000014926static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080014927 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
14928 { },
14929};
14930
Linus Torvalds1da177e2005-04-16 15:20:36 -070014931static int __devinit tg3_test_dma(struct tg3 *tp)
14932{
14933 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070014934 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014935 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014936
Matt Carlson4bae65c2010-11-24 08:31:52 +000014937 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
14938 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014939 if (!buf) {
14940 ret = -ENOMEM;
14941 goto out_nofree;
14942 }
14943
14944 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
14945 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
14946
David S. Miller59e6b432005-05-18 22:50:10 -070014947 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014948
Joe Perches63c3a662011-04-26 08:12:10 +000014949 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014950 goto out;
14951
Joe Perches63c3a662011-04-26 08:12:10 +000014952 if (tg3_flag(tp, PCI_EXPRESS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014953 /* DMA read watermark not used on PCIE */
14954 tp->dma_rwctrl |= 0x00180000;
Joe Perches63c3a662011-04-26 08:12:10 +000014955 } else if (!tg3_flag(tp, PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070014956 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
14957 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014958 tp->dma_rwctrl |= 0x003f0000;
14959 else
14960 tp->dma_rwctrl |= 0x003f000f;
14961 } else {
14962 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14963 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
14964 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080014965 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014966
Michael Chan4a29cc22006-03-19 13:21:12 -080014967 /* If the 5704 is behind the EPB bridge, we can
14968 * do the less restrictive ONE_DMA workaround for
14969 * better performance.
14970 */
Joe Perches63c3a662011-04-26 08:12:10 +000014971 if (tg3_flag(tp, 40BIT_DMA_BUG) &&
Michael Chan4a29cc22006-03-19 13:21:12 -080014972 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14973 tp->dma_rwctrl |= 0x8000;
14974 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014975 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
14976
Michael Chan49afdeb2007-02-13 12:17:03 -080014977 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
14978 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070014979 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080014980 tp->dma_rwctrl |=
14981 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
14982 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
14983 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070014984 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
14985 /* 5780 always in PCIX mode */
14986 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070014987 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
14988 /* 5714 always in PCIX mode */
14989 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014990 } else {
14991 tp->dma_rwctrl |= 0x001b000f;
14992 }
14993 }
14994
14995 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14996 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14997 tp->dma_rwctrl &= 0xfffffff0;
14998
14999 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
15000 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
15001 /* Remove this if it causes problems for some boards. */
15002 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
15003
15004 /* On 5700/5701 chips, we need to set this bit.
15005 * Otherwise the chip will issue cacheline transactions
15006 * to streamable DMA memory with not all the byte
15007 * enables turned on. This is an error on several
15008 * RISC PCI controllers, in particular sparc64.
15009 *
15010 * On 5703/5704 chips, this bit has been reassigned
15011 * a different meaning. In particular, it is used
15012 * on those chips to enable a PCI-X workaround.
15013 */
15014 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
15015 }
15016
15017 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15018
15019#if 0
15020 /* Unneeded, already done by tg3_get_invariants. */
15021 tg3_switch_clocks(tp);
15022#endif
15023
Linus Torvalds1da177e2005-04-16 15:20:36 -070015024 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
15025 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
15026 goto out;
15027
David S. Miller59e6b432005-05-18 22:50:10 -070015028 /* It is best to perform DMA test with maximum write burst size
15029 * to expose the 5700/5701 write DMA bug.
15030 */
15031 saved_dma_rwctrl = tp->dma_rwctrl;
15032 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
15033 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15034
Linus Torvalds1da177e2005-04-16 15:20:36 -070015035 while (1) {
15036 u32 *p = buf, i;
15037
15038 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
15039 p[i] = i;
15040
15041 /* Send the buffer to the chip. */
15042 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
15043 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000015044 dev_err(&tp->pdev->dev,
15045 "%s: Buffer write failed. err = %d\n",
15046 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015047 break;
15048 }
15049
15050#if 0
15051 /* validate data reached card RAM correctly. */
15052 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
15053 u32 val;
15054 tg3_read_mem(tp, 0x2100 + (i*4), &val);
15055 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000015056 dev_err(&tp->pdev->dev,
15057 "%s: Buffer corrupted on device! "
15058 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015059 /* ret = -ENODEV here? */
15060 }
15061 p[i] = 0;
15062 }
15063#endif
15064 /* Now read it back. */
15065 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
15066 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000015067 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
15068 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015069 break;
15070 }
15071
15072 /* Verify it. */
15073 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
15074 if (p[i] == i)
15075 continue;
15076
David S. Miller59e6b432005-05-18 22:50:10 -070015077 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
15078 DMA_RWCTRL_WRITE_BNDRY_16) {
15079 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015080 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
15081 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15082 break;
15083 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000015084 dev_err(&tp->pdev->dev,
15085 "%s: Buffer corrupted on read back! "
15086 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015087 ret = -ENODEV;
15088 goto out;
15089 }
15090 }
15091
15092 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
15093 /* Success. */
15094 ret = 0;
15095 break;
15096 }
15097 }
David S. Miller59e6b432005-05-18 22:50:10 -070015098 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
15099 DMA_RWCTRL_WRITE_BNDRY_16) {
15100 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070015101 * now look for chipsets that are known to expose the
15102 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070015103 */
Matt Carlson41434702011-03-09 16:58:22 +000015104 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070015105 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
15106 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a5882010-04-05 10:19:28 +000015107 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070015108 /* Safe to use the calculated DMA boundary. */
15109 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a5882010-04-05 10:19:28 +000015110 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070015111
David S. Miller59e6b432005-05-18 22:50:10 -070015112 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15113 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015114
15115out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000015116 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015117out_nofree:
15118 return ret;
15119}
15120
Linus Torvalds1da177e2005-04-16 15:20:36 -070015121static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
15122{
Joe Perches63c3a662011-04-26 08:12:10 +000015123 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson666bc832010-01-20 16:58:03 +000015124 tp->bufmgr_config.mbuf_read_dma_low_water =
15125 DEFAULT_MB_RDMA_LOW_WATER_5705;
15126 tp->bufmgr_config.mbuf_mac_rx_low_water =
15127 DEFAULT_MB_MACRX_LOW_WATER_57765;
15128 tp->bufmgr_config.mbuf_high_water =
15129 DEFAULT_MB_HIGH_WATER_57765;
15130
15131 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15132 DEFAULT_MB_RDMA_LOW_WATER_5705;
15133 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15134 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
15135 tp->bufmgr_config.mbuf_high_water_jumbo =
15136 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000015137 } else if (tg3_flag(tp, 5705_PLUS)) {
Michael Chanfdfec172005-07-25 12:31:48 -070015138 tp->bufmgr_config.mbuf_read_dma_low_water =
15139 DEFAULT_MB_RDMA_LOW_WATER_5705;
15140 tp->bufmgr_config.mbuf_mac_rx_low_water =
15141 DEFAULT_MB_MACRX_LOW_WATER_5705;
15142 tp->bufmgr_config.mbuf_high_water =
15143 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070015144 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
15145 tp->bufmgr_config.mbuf_mac_rx_low_water =
15146 DEFAULT_MB_MACRX_LOW_WATER_5906;
15147 tp->bufmgr_config.mbuf_high_water =
15148 DEFAULT_MB_HIGH_WATER_5906;
15149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015150
Michael Chanfdfec172005-07-25 12:31:48 -070015151 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15152 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
15153 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15154 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
15155 tp->bufmgr_config.mbuf_high_water_jumbo =
15156 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
15157 } else {
15158 tp->bufmgr_config.mbuf_read_dma_low_water =
15159 DEFAULT_MB_RDMA_LOW_WATER;
15160 tp->bufmgr_config.mbuf_mac_rx_low_water =
15161 DEFAULT_MB_MACRX_LOW_WATER;
15162 tp->bufmgr_config.mbuf_high_water =
15163 DEFAULT_MB_HIGH_WATER;
15164
15165 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15166 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
15167 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15168 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
15169 tp->bufmgr_config.mbuf_high_water_jumbo =
15170 DEFAULT_MB_HIGH_WATER_JUMBO;
15171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015172
15173 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
15174 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
15175}
15176
15177static char * __devinit tg3_phy_string(struct tg3 *tp)
15178{
Matt Carlson79eb6902010-02-17 15:17:03 +000015179 switch (tp->phy_id & TG3_PHY_ID_MASK) {
15180 case TG3_PHY_ID_BCM5400: return "5400";
15181 case TG3_PHY_ID_BCM5401: return "5401";
15182 case TG3_PHY_ID_BCM5411: return "5411";
15183 case TG3_PHY_ID_BCM5701: return "5701";
15184 case TG3_PHY_ID_BCM5703: return "5703";
15185 case TG3_PHY_ID_BCM5704: return "5704";
15186 case TG3_PHY_ID_BCM5705: return "5705";
15187 case TG3_PHY_ID_BCM5750: return "5750";
15188 case TG3_PHY_ID_BCM5752: return "5752";
15189 case TG3_PHY_ID_BCM5714: return "5714";
15190 case TG3_PHY_ID_BCM5780: return "5780";
15191 case TG3_PHY_ID_BCM5755: return "5755";
15192 case TG3_PHY_ID_BCM5787: return "5787";
15193 case TG3_PHY_ID_BCM5784: return "5784";
15194 case TG3_PHY_ID_BCM5756: return "5722/5756";
15195 case TG3_PHY_ID_BCM5906: return "5906";
15196 case TG3_PHY_ID_BCM5761: return "5761";
15197 case TG3_PHY_ID_BCM5718C: return "5718C";
15198 case TG3_PHY_ID_BCM5718S: return "5718S";
15199 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000015200 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000015201 case TG3_PHY_ID_BCM5720C: return "5720C";
Matt Carlson79eb6902010-02-17 15:17:03 +000015202 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070015203 case 0: return "serdes";
15204 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070015205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015206}
15207
Michael Chanf9804dd2005-09-27 12:13:10 -070015208static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
15209{
Joe Perches63c3a662011-04-26 08:12:10 +000015210 if (tg3_flag(tp, PCI_EXPRESS)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070015211 strcpy(str, "PCI Express");
15212 return str;
Joe Perches63c3a662011-04-26 08:12:10 +000015213 } else if (tg3_flag(tp, PCIX_MODE)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070015214 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
15215
15216 strcpy(str, "PCIX:");
15217
15218 if ((clock_ctrl == 7) ||
15219 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
15220 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
15221 strcat(str, "133MHz");
15222 else if (clock_ctrl == 0)
15223 strcat(str, "33MHz");
15224 else if (clock_ctrl == 2)
15225 strcat(str, "50MHz");
15226 else if (clock_ctrl == 4)
15227 strcat(str, "66MHz");
15228 else if (clock_ctrl == 6)
15229 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070015230 } else {
15231 strcpy(str, "PCI:");
Joe Perches63c3a662011-04-26 08:12:10 +000015232 if (tg3_flag(tp, PCI_HIGH_SPEED))
Michael Chanf9804dd2005-09-27 12:13:10 -070015233 strcat(str, "66MHz");
15234 else
15235 strcat(str, "33MHz");
15236 }
Joe Perches63c3a662011-04-26 08:12:10 +000015237 if (tg3_flag(tp, PCI_32BIT))
Michael Chanf9804dd2005-09-27 12:13:10 -070015238 strcat(str, ":32-bit");
15239 else
15240 strcat(str, ":64-bit");
15241 return str;
15242}
15243
Michael Chan8c2dc7e2005-12-19 16:26:02 -080015244static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015245{
15246 struct pci_dev *peer;
15247 unsigned int func, devnr = tp->pdev->devfn & ~7;
15248
15249 for (func = 0; func < 8; func++) {
15250 peer = pci_get_slot(tp->pdev->bus, devnr | func);
15251 if (peer && peer != tp->pdev)
15252 break;
15253 pci_dev_put(peer);
15254 }
Michael Chan16fe9d72005-12-13 21:09:54 -080015255 /* 5704 can be configured in single-port mode, set peer to
15256 * tp->pdev in that case.
15257 */
15258 if (!peer) {
15259 peer = tp->pdev;
15260 return peer;
15261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015262
15263 /*
15264 * We don't need to keep the refcount elevated; there's no way
15265 * to remove one half of this device without removing the other
15266 */
15267 pci_dev_put(peer);
15268
15269 return peer;
15270}
15271
David S. Miller15f98502005-05-18 22:49:26 -070015272static void __devinit tg3_init_coal(struct tg3 *tp)
15273{
15274 struct ethtool_coalesce *ec = &tp->coal;
15275
15276 memset(ec, 0, sizeof(*ec));
15277 ec->cmd = ETHTOOL_GCOALESCE;
15278 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
15279 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
15280 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
15281 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
15282 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
15283 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
15284 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
15285 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
15286 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
15287
15288 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
15289 HOSTCC_MODE_CLRTICK_TXBD)) {
15290 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
15291 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
15292 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
15293 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
15294 }
Michael Chand244c892005-07-05 14:42:33 -070015295
Joe Perches63c3a662011-04-26 08:12:10 +000015296 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070015297 ec->rx_coalesce_usecs_irq = 0;
15298 ec->tx_coalesce_usecs_irq = 0;
15299 ec->stats_block_coalesce_usecs = 0;
15300 }
David S. Miller15f98502005-05-18 22:49:26 -070015301}
15302
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080015303static const struct net_device_ops tg3_netdev_ops = {
15304 .ndo_open = tg3_open,
15305 .ndo_stop = tg3_close,
Stephen Hemminger00829822008-11-20 20:14:53 -080015306 .ndo_start_xmit = tg3_start_xmit,
Eric Dumazet511d2222010-07-07 20:44:24 +000015307 .ndo_get_stats64 = tg3_get_stats64,
Stephen Hemminger00829822008-11-20 20:14:53 -080015308 .ndo_validate_addr = eth_validate_addr,
Jiri Pirkoafc4b132011-08-16 06:29:01 +000015309 .ndo_set_rx_mode = tg3_set_rx_mode,
Stephen Hemminger00829822008-11-20 20:14:53 -080015310 .ndo_set_mac_address = tg3_set_mac_addr,
15311 .ndo_do_ioctl = tg3_ioctl,
15312 .ndo_tx_timeout = tg3_tx_timeout,
15313 .ndo_change_mtu = tg3_change_mtu,
Michał Mirosławdc668912011-04-07 03:35:07 +000015314 .ndo_fix_features = tg3_fix_features,
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015315 .ndo_set_features = tg3_set_features,
Stephen Hemminger00829822008-11-20 20:14:53 -080015316#ifdef CONFIG_NET_POLL_CONTROLLER
15317 .ndo_poll_controller = tg3_poll_controller,
15318#endif
15319};
15320
Linus Torvalds1da177e2005-04-16 15:20:36 -070015321static int __devinit tg3_init_one(struct pci_dev *pdev,
15322 const struct pci_device_id *ent)
15323{
Linus Torvalds1da177e2005-04-16 15:20:36 -070015324 struct net_device *dev;
15325 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000015326 int i, err, pm_cap;
15327 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070015328 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080015329 u64 dma_mask, persist_dma_mask;
Michał Mirosławc8f44af2011-11-15 15:29:55 +000015330 netdev_features_t features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015331
Joe Perches05dbe002010-02-17 19:44:19 +000015332 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015333
15334 err = pci_enable_device(pdev);
15335 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015336 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015337 return err;
15338 }
15339
Linus Torvalds1da177e2005-04-16 15:20:36 -070015340 err = pci_request_regions(pdev, DRV_MODULE_NAME);
15341 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015342 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015343 goto err_out_disable_pdev;
15344 }
15345
15346 pci_set_master(pdev);
15347
15348 /* Find power-management capability. */
15349 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
15350 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000015351 dev_err(&pdev->dev,
15352 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015353 err = -EIO;
15354 goto err_out_free_res;
15355 }
15356
Matt Carlson16821282011-07-13 09:27:28 +000015357 err = pci_set_power_state(pdev, PCI_D0);
15358 if (err) {
15359 dev_err(&pdev->dev, "Transition to D0 failed, aborting\n");
15360 goto err_out_free_res;
15361 }
15362
Matt Carlsonfe5f5782009-09-01 13:09:39 +000015363 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015364 if (!dev) {
Matt Carlson2445e462010-04-05 10:19:21 +000015365 dev_err(&pdev->dev, "Etherdev alloc failed, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015366 err = -ENOMEM;
Matt Carlson16821282011-07-13 09:27:28 +000015367 goto err_out_power_down;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015368 }
15369
Linus Torvalds1da177e2005-04-16 15:20:36 -070015370 SET_NETDEV_DEV(dev, &pdev->dev);
15371
Linus Torvalds1da177e2005-04-16 15:20:36 -070015372 tp = netdev_priv(dev);
15373 tp->pdev = pdev;
15374 tp->dev = dev;
15375 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015376 tp->rx_mode = TG3_DEF_RX_MODE;
15377 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070015378
Linus Torvalds1da177e2005-04-16 15:20:36 -070015379 if (tg3_debug > 0)
15380 tp->msg_enable = tg3_debug;
15381 else
15382 tp->msg_enable = TG3_DEF_MSG_ENABLE;
15383
15384 /* The word/byte swap controls here control register access byte
15385 * swapping. DMA data byte swapping is controlled in the GRC_MODE
15386 * setting below.
15387 */
15388 tp->misc_host_ctrl =
15389 MISC_HOST_CTRL_MASK_PCI_INT |
15390 MISC_HOST_CTRL_WORD_SWAP |
15391 MISC_HOST_CTRL_INDIR_ACCESS |
15392 MISC_HOST_CTRL_PCISTATE_RW;
15393
15394 /* The NONFRM (non-frame) byte/word swap controls take effect
15395 * on descriptor entries, anything which isn't packet data.
15396 *
15397 * The StrongARM chips on the board (one for tx, one for rx)
15398 * are running in big-endian mode.
15399 */
15400 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
15401 GRC_MODE_WSWAP_NONFRM_DATA);
15402#ifdef __BIG_ENDIAN
15403 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
15404#endif
15405 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015406 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000015407 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015408
Matt Carlsond5fe4882008-11-21 17:20:32 -080015409 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010015410 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015411 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015412 err = -ENOMEM;
15413 goto err_out_free_dev;
15414 }
15415
Matt Carlsonc9cab242011-07-13 09:27:27 +000015416 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
15417 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761E ||
15418 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S ||
15419 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761SE ||
15420 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
15421 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
15422 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
15423 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720) {
15424 tg3_flag_set(tp, ENABLE_APE);
15425 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
15426 if (!tp->aperegs) {
15427 dev_err(&pdev->dev,
15428 "Cannot map APE registers, aborting\n");
15429 err = -ENOMEM;
15430 goto err_out_iounmap;
15431 }
15432 }
15433
Linus Torvalds1da177e2005-04-16 15:20:36 -070015434 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
15435 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015436
Linus Torvalds1da177e2005-04-16 15:20:36 -070015437 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015438 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Matt Carlson2ffcc982011-05-19 12:12:44 +000015439 dev->netdev_ops = &tg3_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015440 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015441
15442 err = tg3_get_invariants(tp);
15443 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015444 dev_err(&pdev->dev,
15445 "Problem fetching invariants of chip, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015446 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015447 }
15448
Michael Chan4a29cc22006-03-19 13:21:12 -080015449 /* The EPB bridge inside 5714, 5715, and 5780 and any
15450 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080015451 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
15452 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
15453 * do DMA address check in tg3_start_xmit().
15454 */
Joe Perches63c3a662011-04-26 08:12:10 +000015455 if (tg3_flag(tp, IS_5788))
Yang Hongyang284901a2009-04-06 19:01:15 -070015456 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Joe Perches63c3a662011-04-26 08:12:10 +000015457 else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070015458 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080015459#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070015460 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015461#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080015462 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070015463 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015464
15465 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070015466 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080015467 err = pci_set_dma_mask(pdev, dma_mask);
15468 if (!err) {
Matt Carlson0da06062011-05-19 12:12:53 +000015469 features |= NETIF_F_HIGHDMA;
Michael Chan72f2afb2006-03-06 19:28:35 -080015470 err = pci_set_consistent_dma_mask(pdev,
15471 persist_dma_mask);
15472 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015473 dev_err(&pdev->dev, "Unable to obtain 64 bit "
15474 "DMA for consistent allocations\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015475 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080015476 }
15477 }
15478 }
Yang Hongyang284901a2009-04-06 19:01:15 -070015479 if (err || dma_mask == DMA_BIT_MASK(32)) {
15480 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080015481 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015482 dev_err(&pdev->dev,
15483 "No usable DMA configuration, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015484 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080015485 }
15486 }
15487
Michael Chanfdfec172005-07-25 12:31:48 -070015488 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015489
Matt Carlson0da06062011-05-19 12:12:53 +000015490 features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
15491
15492 /* 5700 B0 chips do not support checksumming correctly due
15493 * to hardware bugs.
15494 */
15495 if (tp->pci_chip_rev_id != CHIPREV_ID_5700_B0) {
15496 features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
15497
15498 if (tg3_flag(tp, 5755_PLUS))
15499 features |= NETIF_F_IPV6_CSUM;
15500 }
15501
Michael Chan4e3a7aa2006-03-20 17:47:44 -080015502 /* TSO is on by default on chips that support hardware TSO.
15503 * Firmware TSO on older chips gives lower performance, so it
15504 * is off by default, but can be enabled using ethtool.
15505 */
Joe Perches63c3a662011-04-26 08:12:10 +000015506 if ((tg3_flag(tp, HW_TSO_1) ||
15507 tg3_flag(tp, HW_TSO_2) ||
15508 tg3_flag(tp, HW_TSO_3)) &&
Matt Carlson0da06062011-05-19 12:12:53 +000015509 (features & NETIF_F_IP_CSUM))
15510 features |= NETIF_F_TSO;
Joe Perches63c3a662011-04-26 08:12:10 +000015511 if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
Matt Carlson0da06062011-05-19 12:12:53 +000015512 if (features & NETIF_F_IPV6_CSUM)
15513 features |= NETIF_F_TSO6;
Joe Perches63c3a662011-04-26 08:12:10 +000015514 if (tg3_flag(tp, HW_TSO_3) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000015515 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070015516 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
15517 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Joe Perches63c3a662011-04-26 08:12:10 +000015518 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Michał Mirosławdc668912011-04-07 03:35:07 +000015519 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson0da06062011-05-19 12:12:53 +000015520 features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070015521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015522
Matt Carlsond542fe22011-05-19 16:02:43 +000015523 dev->features |= features;
15524 dev->vlan_features |= features;
15525
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015526 /*
15527 * Add loopback capability only for a subset of devices that support
15528 * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
15529 * loopback for the remaining devices.
15530 */
15531 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
15532 !tg3_flag(tp, CPMU_PRESENT))
15533 /* Add the loopback capability */
Matt Carlson0da06062011-05-19 12:12:53 +000015534 features |= NETIF_F_LOOPBACK;
15535
Matt Carlson0da06062011-05-19 12:12:53 +000015536 dev->hw_features |= features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015537
Linus Torvalds1da177e2005-04-16 15:20:36 -070015538 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015539 !tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015540 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015541 tg3_flag_set(tp, MAX_RXPEND_64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015542 tp->rx_pending = 63;
15543 }
15544
Linus Torvalds1da177e2005-04-16 15:20:36 -070015545 err = tg3_get_device_address(tp);
15546 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015547 dev_err(&pdev->dev,
15548 "Could not obtain valid ethernet address, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015549 goto err_out_apeunmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070015550 }
15551
Matt Carlsonc88864d2007-11-12 21:07:01 -080015552 /*
15553 * Reset chip in case UNDI or EFI driver did not shutdown
15554 * DMA self test will enable WDMAC and we'll see (spurious)
15555 * pending DMA on the PCI bus at that point.
15556 */
15557 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
15558 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
15559 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
15560 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
15561 }
15562
15563 err = tg3_test_dma(tp);
15564 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015565 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080015566 goto err_out_apeunmap;
15567 }
15568
Matt Carlson78f90dc2009-11-13 13:03:42 +000015569 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
15570 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
15571 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000015572 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000015573 struct tg3_napi *tnapi = &tp->napi[i];
15574
15575 tnapi->tp = tp;
15576 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
15577
15578 tnapi->int_mbox = intmbx;
Matt Carlson93a700a2011-08-31 11:44:54 +000015579 if (i <= 4)
Matt Carlson78f90dc2009-11-13 13:03:42 +000015580 intmbx += 0x8;
15581 else
15582 intmbx += 0x4;
15583
15584 tnapi->consmbox = rcvmbx;
15585 tnapi->prodmbox = sndmbx;
15586
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015587 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000015588 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015589 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000015590 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000015591
Joe Perches63c3a662011-04-26 08:12:10 +000015592 if (!tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson78f90dc2009-11-13 13:03:42 +000015593 break;
15594
15595 /*
15596 * If we support MSIX, we'll be using RSS. If we're using
15597 * RSS, the first vector only handles link interrupts and the
15598 * remaining vectors handle rx and tx interrupts. Reuse the
15599 * mailbox values for the next iteration. The values we setup
15600 * above are still useful for the single vectored mode.
15601 */
15602 if (!i)
15603 continue;
15604
15605 rcvmbx += 0x8;
15606
15607 if (sndmbx & 0x4)
15608 sndmbx -= 0x4;
15609 else
15610 sndmbx += 0xc;
15611 }
15612
Matt Carlsonc88864d2007-11-12 21:07:01 -080015613 tg3_init_coal(tp);
15614
Michael Chanc49a1562006-12-17 17:07:29 -080015615 pci_set_drvdata(pdev, dev);
15616
Matt Carlsoncd0d7222011-07-13 09:27:33 +000015617 if (tg3_flag(tp, 5717_PLUS)) {
15618 /* Resume a low-power mode */
15619 tg3_frob_aux_power(tp, false);
15620 }
15621
Linus Torvalds1da177e2005-04-16 15:20:36 -070015622 err = register_netdev(dev);
15623 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015624 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070015625 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015626 }
15627
Joe Perches05dbe002010-02-17 19:44:19 +000015628 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
15629 tp->board_part_number,
15630 tp->pci_chip_rev_id,
15631 tg3_bus_string(tp, str),
15632 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015633
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015634 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000015635 struct phy_device *phydev;
15636 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000015637 netdev_info(dev,
15638 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000015639 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015640 } else {
15641 char *ethtype;
15642
15643 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
15644 ethtype = "10/100Base-TX";
15645 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
15646 ethtype = "1000Base-SX";
15647 else
15648 ethtype = "10/100/1000Base-T";
15649
Matt Carlson5129c3a2010-04-05 10:19:23 +000015650 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlson47007832011-04-20 07:57:43 +000015651 "(WireSpeed[%d], EEE[%d])\n",
15652 tg3_phy_string(tp), ethtype,
15653 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
15654 (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015655 }
Matt Carlsondf59c942008-11-03 16:52:56 -080015656
Joe Perches05dbe002010-02-17 19:44:19 +000015657 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000015658 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015659 tg3_flag(tp, USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015660 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015661 tg3_flag(tp, ENABLE_ASF) != 0,
15662 tg3_flag(tp, TSO_CAPABLE) != 0);
Joe Perches05dbe002010-02-17 19:44:19 +000015663 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
15664 tp->dma_rwctrl,
15665 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
15666 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015667
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015668 pci_save_state(pdev);
15669
Linus Torvalds1da177e2005-04-16 15:20:36 -070015670 return 0;
15671
Matt Carlson0d3031d2007-10-10 18:02:43 -070015672err_out_apeunmap:
15673 if (tp->aperegs) {
15674 iounmap(tp->aperegs);
15675 tp->aperegs = NULL;
15676 }
15677
Linus Torvalds1da177e2005-04-16 15:20:36 -070015678err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070015679 if (tp->regs) {
15680 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015681 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015683
15684err_out_free_dev:
15685 free_netdev(dev);
15686
Matt Carlson16821282011-07-13 09:27:28 +000015687err_out_power_down:
15688 pci_set_power_state(pdev, PCI_D3hot);
15689
Linus Torvalds1da177e2005-04-16 15:20:36 -070015690err_out_free_res:
15691 pci_release_regions(pdev);
15692
15693err_out_disable_pdev:
15694 pci_disable_device(pdev);
15695 pci_set_drvdata(pdev, NULL);
15696 return err;
15697}
15698
15699static void __devexit tg3_remove_one(struct pci_dev *pdev)
15700{
15701 struct net_device *dev = pci_get_drvdata(pdev);
15702
15703 if (dev) {
15704 struct tg3 *tp = netdev_priv(dev);
15705
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080015706 if (tp->fw)
15707 release_firmware(tp->fw);
15708
Matt Carlsondb219972011-11-04 09:15:03 +000015709 tg3_reset_task_cancel(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015710
David S. Miller1805b2f2011-10-24 18:18:09 -040015711 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015712 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015713 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015714 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070015715
Linus Torvalds1da177e2005-04-16 15:20:36 -070015716 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015717 if (tp->aperegs) {
15718 iounmap(tp->aperegs);
15719 tp->aperegs = NULL;
15720 }
Michael Chan68929142005-08-09 20:17:14 -070015721 if (tp->regs) {
15722 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015723 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015725 free_netdev(dev);
15726 pci_release_regions(pdev);
15727 pci_disable_device(pdev);
15728 pci_set_drvdata(pdev, NULL);
15729 }
15730}
15731
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015732#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015733static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015734{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015735 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015736 struct net_device *dev = pci_get_drvdata(pdev);
15737 struct tg3 *tp = netdev_priv(dev);
15738 int err;
15739
15740 if (!netif_running(dev))
15741 return 0;
15742
Matt Carlsondb219972011-11-04 09:15:03 +000015743 tg3_reset_task_cancel(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015744 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015745 tg3_netif_stop(tp);
15746
15747 del_timer_sync(&tp->timer);
15748
David S. Millerf47c11e2005-06-24 20:18:35 -070015749 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015750 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070015751 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015752
15753 netif_device_detach(dev);
15754
David S. Millerf47c11e2005-06-24 20:18:35 -070015755 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070015756 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches63c3a662011-04-26 08:12:10 +000015757 tg3_flag_clear(tp, INIT_COMPLETE);
David S. Millerf47c11e2005-06-24 20:18:35 -070015758 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015759
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015760 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015761 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015762 int err2;
15763
David S. Millerf47c11e2005-06-24 20:18:35 -070015764 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015765
Joe Perches63c3a662011-04-26 08:12:10 +000015766 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015767 err2 = tg3_restart_hw(tp, 1);
15768 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070015769 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015770
15771 tp->timer.expires = jiffies + tp->timer_offset;
15772 add_timer(&tp->timer);
15773
15774 netif_device_attach(dev);
15775 tg3_netif_start(tp);
15776
Michael Chanb9ec6c12006-07-25 16:37:27 -070015777out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015778 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015779
15780 if (!err2)
15781 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015782 }
15783
15784 return err;
15785}
15786
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015787static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015788{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015789 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015790 struct net_device *dev = pci_get_drvdata(pdev);
15791 struct tg3 *tp = netdev_priv(dev);
15792 int err;
15793
15794 if (!netif_running(dev))
15795 return 0;
15796
Linus Torvalds1da177e2005-04-16 15:20:36 -070015797 netif_device_attach(dev);
15798
David S. Millerf47c11e2005-06-24 20:18:35 -070015799 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015800
Joe Perches63c3a662011-04-26 08:12:10 +000015801 tg3_flag_set(tp, INIT_COMPLETE);
Michael Chanb9ec6c12006-07-25 16:37:27 -070015802 err = tg3_restart_hw(tp, 1);
15803 if (err)
15804 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015805
15806 tp->timer.expires = jiffies + tp->timer_offset;
15807 add_timer(&tp->timer);
15808
Linus Torvalds1da177e2005-04-16 15:20:36 -070015809 tg3_netif_start(tp);
15810
Michael Chanb9ec6c12006-07-25 16:37:27 -070015811out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015812 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015813
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015814 if (!err)
15815 tg3_phy_start(tp);
15816
Michael Chanb9ec6c12006-07-25 16:37:27 -070015817 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015818}
15819
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015820static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015821#define TG3_PM_OPS (&tg3_pm_ops)
15822
15823#else
15824
15825#define TG3_PM_OPS NULL
15826
15827#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015828
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015829/**
15830 * tg3_io_error_detected - called when PCI error is detected
15831 * @pdev: Pointer to PCI device
15832 * @state: The current pci connection state
15833 *
15834 * This function is called after a PCI bus error affecting
15835 * this device has been detected.
15836 */
15837static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
15838 pci_channel_state_t state)
15839{
15840 struct net_device *netdev = pci_get_drvdata(pdev);
15841 struct tg3 *tp = netdev_priv(netdev);
15842 pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
15843
15844 netdev_info(netdev, "PCI I/O error detected\n");
15845
15846 rtnl_lock();
15847
15848 if (!netif_running(netdev))
15849 goto done;
15850
15851 tg3_phy_stop(tp);
15852
15853 tg3_netif_stop(tp);
15854
15855 del_timer_sync(&tp->timer);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015856
15857 /* Want to make sure that the reset task doesn't run */
Matt Carlsondb219972011-11-04 09:15:03 +000015858 tg3_reset_task_cancel(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000015859 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015860
15861 netif_device_detach(netdev);
15862
15863 /* Clean up software state, even if MMIO is blocked */
15864 tg3_full_lock(tp, 0);
15865 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
15866 tg3_full_unlock(tp);
15867
15868done:
15869 if (state == pci_channel_io_perm_failure)
15870 err = PCI_ERS_RESULT_DISCONNECT;
15871 else
15872 pci_disable_device(pdev);
15873
15874 rtnl_unlock();
15875
15876 return err;
15877}
15878
15879/**
15880 * tg3_io_slot_reset - called after the pci bus has been reset.
15881 * @pdev: Pointer to PCI device
15882 *
15883 * Restart the card from scratch, as if from a cold-boot.
15884 * At this point, the card has exprienced a hard reset,
15885 * followed by fixups by BIOS, and has its config space
15886 * set up identically to what it was at cold boot.
15887 */
15888static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
15889{
15890 struct net_device *netdev = pci_get_drvdata(pdev);
15891 struct tg3 *tp = netdev_priv(netdev);
15892 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
15893 int err;
15894
15895 rtnl_lock();
15896
15897 if (pci_enable_device(pdev)) {
15898 netdev_err(netdev, "Cannot re-enable PCI device after reset.\n");
15899 goto done;
15900 }
15901
15902 pci_set_master(pdev);
15903 pci_restore_state(pdev);
15904 pci_save_state(pdev);
15905
15906 if (!netif_running(netdev)) {
15907 rc = PCI_ERS_RESULT_RECOVERED;
15908 goto done;
15909 }
15910
15911 err = tg3_power_up(tp);
Matt Carlsonbed98292011-07-13 09:27:29 +000015912 if (err)
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015913 goto done;
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015914
15915 rc = PCI_ERS_RESULT_RECOVERED;
15916
15917done:
15918 rtnl_unlock();
15919
15920 return rc;
15921}
15922
15923/**
15924 * tg3_io_resume - called when traffic can start flowing again.
15925 * @pdev: Pointer to PCI device
15926 *
15927 * This callback is called when the error recovery driver tells
15928 * us that its OK to resume normal operation.
15929 */
15930static void tg3_io_resume(struct pci_dev *pdev)
15931{
15932 struct net_device *netdev = pci_get_drvdata(pdev);
15933 struct tg3 *tp = netdev_priv(netdev);
15934 int err;
15935
15936 rtnl_lock();
15937
15938 if (!netif_running(netdev))
15939 goto done;
15940
15941 tg3_full_lock(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +000015942 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015943 err = tg3_restart_hw(tp, 1);
15944 tg3_full_unlock(tp);
15945 if (err) {
15946 netdev_err(netdev, "Cannot restart hardware after reset.\n");
15947 goto done;
15948 }
15949
15950 netif_device_attach(netdev);
15951
15952 tp->timer.expires = jiffies + tp->timer_offset;
15953 add_timer(&tp->timer);
15954
15955 tg3_netif_start(tp);
15956
15957 tg3_phy_start(tp);
15958
15959done:
15960 rtnl_unlock();
15961}
15962
15963static struct pci_error_handlers tg3_err_handler = {
15964 .error_detected = tg3_io_error_detected,
15965 .slot_reset = tg3_io_slot_reset,
15966 .resume = tg3_io_resume
15967};
15968
Linus Torvalds1da177e2005-04-16 15:20:36 -070015969static struct pci_driver tg3_driver = {
15970 .name = DRV_MODULE_NAME,
15971 .id_table = tg3_pci_tbl,
15972 .probe = tg3_init_one,
15973 .remove = __devexit_p(tg3_remove_one),
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015974 .err_handler = &tg3_err_handler,
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015975 .driver.pm = TG3_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070015976};
15977
15978static int __init tg3_init(void)
15979{
Jeff Garzik29917622006-08-19 17:48:59 -040015980 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015981}
15982
15983static void __exit tg3_cleanup(void)
15984{
15985 pci_unregister_driver(&tg3_driver);
15986}
15987
15988module_init(tg3_init);
15989module_exit(tg3_cleanup);