blob: cf36312ac5ac508007df5150461d5c5fa2c948bf [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 Carlson5ae7fa02011-11-04 09:15:05 +000092#define TG3_MIN_NUM 121
Matt Carlson6867c842010-07-11 09:31:44 +000093#define DRV_MODULE_VERSION \
94 __stringify(TG3_MAJ_NUM) "." __stringify(TG3_MIN_NUM)
Matt Carlson5ae7fa02011-11-04 09:15:05 +000095#define DRV_MODULE_RELDATE "November 2, 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 Carlsone31aa982011-07-27 14:20:53 +0000202#define TG3_TX_BD_DMA_MAX 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 Carlson66711e662009-11-13 13:03:49 +0000473 if (off == TG3_RX_STD_PROD_IDX_REG) {
Michael Chan68929142005-08-09 20:17:14 -0700474 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
475 TG3_64BIT_REG_LOW, val);
476 return;
477 }
478
479 spin_lock_irqsave(&tp->indirect_lock, flags);
480 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
481 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
482 spin_unlock_irqrestore(&tp->indirect_lock, flags);
483
484 /* In indirect mode when disabling interrupts, we also need
485 * to clear the interrupt bit in the GRC local ctrl register.
486 */
487 if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
488 (val == 0x1)) {
489 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
490 tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
491 }
492}
493
494static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
495{
496 unsigned long flags;
497 u32 val;
498
499 spin_lock_irqsave(&tp->indirect_lock, flags);
500 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
501 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
502 spin_unlock_irqrestore(&tp->indirect_lock, flags);
503 return val;
504}
505
Michael Chanb401e9e2005-12-19 16:27:04 -0800506/* usec_wait specifies the wait time in usec when writing to certain registers
507 * where it is unsafe to read back the register without some delay.
508 * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
509 * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
510 */
511static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
Joe Perches63c3a662011-04-26 08:12:10 +0000513 if (tg3_flag(tp, PCIX_TARGET_HWBUG) || tg3_flag(tp, ICH_WORKAROUND))
Michael Chanb401e9e2005-12-19 16:27:04 -0800514 /* Non-posted methods */
515 tp->write32(tp, off, val);
516 else {
517 /* Posted method */
518 tg3_write32(tp, off, val);
519 if (usec_wait)
520 udelay(usec_wait);
521 tp->read32(tp, off);
522 }
523 /* Wait again after the read for the posted method to guarantee that
524 * the wait time is met.
525 */
526 if (usec_wait)
527 udelay(usec_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
Michael Chan09ee9292005-08-09 20:17:00 -0700530static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
531{
532 tp->write32_mbox(tp, off, val);
Joe Perches63c3a662011-04-26 08:12:10 +0000533 if (!tg3_flag(tp, MBOX_WRITE_REORDER) && !tg3_flag(tp, ICH_WORKAROUND))
Michael Chan68929142005-08-09 20:17:14 -0700534 tp->read32_mbox(tp, off);
Michael Chan09ee9292005-08-09 20:17:00 -0700535}
536
Michael Chan20094932005-08-09 20:16:32 -0700537static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
539 void __iomem *mbox = tp->regs + off;
540 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000541 if (tg3_flag(tp, TXD_MBOX_HWBUG))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000543 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 readl(mbox);
545}
546
Michael Chanb5d37722006-09-27 16:06:21 -0700547static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
548{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000549 return readl(tp->regs + off + GRCMBOX_BASE);
Michael Chanb5d37722006-09-27 16:06:21 -0700550}
551
552static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
553{
554 writel(val, tp->regs + off + GRCMBOX_BASE);
555}
556
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000557#define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700558#define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000559#define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
560#define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
561#define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
Michael Chan20094932005-08-09 20:16:32 -0700562
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000563#define tw32(reg, val) tp->write32(tp, reg, val)
564#define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
565#define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
566#define tr32(reg) tp->read32(tp, reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
569{
Michael Chan68929142005-08-09 20:17:14 -0700570 unsigned long flags;
571
Matt Carlson6ff6f812011-05-19 12:12:54 +0000572 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700573 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
574 return;
575
Michael Chan68929142005-08-09 20:17:14 -0700576 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000577 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700578 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
579 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Michael Chanbbadf502006-04-06 21:46:34 -0700581 /* Always leave this as zero. */
582 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
583 } else {
584 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
585 tw32_f(TG3PCI_MEM_WIN_DATA, val);
586
587 /* Always leave this as zero. */
588 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
589 }
Michael Chan68929142005-08-09 20:17:14 -0700590 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592
593static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
594{
Michael Chan68929142005-08-09 20:17:14 -0700595 unsigned long flags;
596
Matt Carlson6ff6f812011-05-19 12:12:54 +0000597 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700598 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
599 *val = 0;
600 return;
601 }
602
Michael Chan68929142005-08-09 20:17:14 -0700603 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000604 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700605 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
606 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Michael Chanbbadf502006-04-06 21:46:34 -0700608 /* Always leave this as zero. */
609 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
610 } else {
611 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
612 *val = tr32(TG3PCI_MEM_WIN_DATA);
613
614 /* Always leave this as zero. */
615 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
616 }
Michael Chan68929142005-08-09 20:17:14 -0700617 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618}
619
Matt Carlson0d3031d2007-10-10 18:02:43 -0700620static void tg3_ape_lock_init(struct tg3 *tp)
621{
622 int i;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000623 u32 regbase, bit;
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000624
625 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
626 regbase = TG3_APE_LOCK_GRANT;
627 else
628 regbase = TG3_APE_PER_LOCK_GRANT;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700629
630 /* Make sure the driver hasn't any stale locks. */
Matt Carlson78f94dc2011-11-04 09:14:58 +0000631 for (i = TG3_APE_LOCK_PHY0; i <= TG3_APE_LOCK_GPIO; i++) {
632 switch (i) {
633 case TG3_APE_LOCK_PHY0:
634 case TG3_APE_LOCK_PHY1:
635 case TG3_APE_LOCK_PHY2:
636 case TG3_APE_LOCK_PHY3:
637 bit = APE_LOCK_GRANT_DRIVER;
638 break;
639 default:
640 if (!tp->pci_fn)
641 bit = APE_LOCK_GRANT_DRIVER;
642 else
643 bit = 1 << tp->pci_fn;
644 }
645 tg3_ape_write32(tp, regbase + 4 * i, bit);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000646 }
647
Matt Carlson0d3031d2007-10-10 18:02:43 -0700648}
649
650static int tg3_ape_lock(struct tg3 *tp, int locknum)
651{
652 int i, off;
653 int ret = 0;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000654 u32 status, req, gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700655
Joe Perches63c3a662011-04-26 08:12:10 +0000656 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700657 return 0;
658
659 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000660 case TG3_APE_LOCK_GPIO:
661 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
662 return 0;
Matt Carlson33f401a2010-04-05 10:19:27 +0000663 case TG3_APE_LOCK_GRC:
664 case TG3_APE_LOCK_MEM:
Matt Carlson78f94dc2011-11-04 09:14:58 +0000665 if (!tp->pci_fn)
666 bit = APE_LOCK_REQ_DRIVER;
667 else
668 bit = 1 << tp->pci_fn;
Matt Carlson33f401a2010-04-05 10:19:27 +0000669 break;
670 default:
671 return -EINVAL;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700672 }
673
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000674 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
675 req = TG3_APE_LOCK_REQ;
676 gnt = TG3_APE_LOCK_GRANT;
677 } else {
678 req = TG3_APE_PER_LOCK_REQ;
679 gnt = TG3_APE_PER_LOCK_GRANT;
680 }
681
Matt Carlson0d3031d2007-10-10 18:02:43 -0700682 off = 4 * locknum;
683
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000684 tg3_ape_write32(tp, req + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700685
686 /* Wait for up to 1 millisecond to acquire lock. */
687 for (i = 0; i < 100; i++) {
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000688 status = tg3_ape_read32(tp, gnt + off);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000689 if (status == bit)
Matt Carlson0d3031d2007-10-10 18:02:43 -0700690 break;
691 udelay(10);
692 }
693
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000694 if (status != bit) {
Matt Carlson0d3031d2007-10-10 18:02:43 -0700695 /* Revoke the lock request. */
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000696 tg3_ape_write32(tp, gnt + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700697 ret = -EBUSY;
698 }
699
700 return ret;
701}
702
703static void tg3_ape_unlock(struct tg3 *tp, int locknum)
704{
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000705 u32 gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700706
Joe Perches63c3a662011-04-26 08:12:10 +0000707 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700708 return;
709
710 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000711 case TG3_APE_LOCK_GPIO:
712 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
713 return;
Matt Carlson33f401a2010-04-05 10:19:27 +0000714 case TG3_APE_LOCK_GRC:
715 case TG3_APE_LOCK_MEM:
Matt Carlson78f94dc2011-11-04 09:14:58 +0000716 if (!tp->pci_fn)
717 bit = APE_LOCK_GRANT_DRIVER;
718 else
719 bit = 1 << tp->pci_fn;
Matt Carlson33f401a2010-04-05 10:19:27 +0000720 break;
721 default:
722 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700723 }
724
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000725 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
726 gnt = TG3_APE_LOCK_GRANT;
727 else
728 gnt = TG3_APE_PER_LOCK_GRANT;
729
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000730 tg3_ape_write32(tp, gnt + 4 * locknum, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700731}
732
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000733static void tg3_ape_send_event(struct tg3 *tp, u32 event)
734{
735 int i;
736 u32 apedata;
737
738 /* NCSI does not support APE events */
739 if (tg3_flag(tp, APE_HAS_NCSI))
740 return;
741
742 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
743 if (apedata != APE_SEG_SIG_MAGIC)
744 return;
745
746 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
747 if (!(apedata & APE_FW_STATUS_READY))
748 return;
749
750 /* Wait for up to 1 millisecond for APE to service previous event. */
751 for (i = 0; i < 10; i++) {
752 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
753 return;
754
755 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
756
757 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
758 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
759 event | APE_EVENT_STATUS_EVENT_PENDING);
760
761 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
762
763 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
764 break;
765
766 udelay(100);
767 }
768
769 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
770 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
771}
772
773static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
774{
775 u32 event;
776 u32 apedata;
777
778 if (!tg3_flag(tp, ENABLE_APE))
779 return;
780
781 switch (kind) {
782 case RESET_KIND_INIT:
783 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
784 APE_HOST_SEG_SIG_MAGIC);
785 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
786 APE_HOST_SEG_LEN_MAGIC);
787 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
788 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
789 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
790 APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
791 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
792 APE_HOST_BEHAV_NO_PHYLOCK);
793 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
794 TG3_APE_HOST_DRVR_STATE_START);
795
796 event = APE_EVENT_STATUS_STATE_START;
797 break;
798 case RESET_KIND_SHUTDOWN:
799 /* With the interface we are currently using,
800 * APE does not track driver state. Wiping
801 * out the HOST SEGMENT SIGNATURE forces
802 * the APE to assume OS absent status.
803 */
804 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
805
806 if (device_may_wakeup(&tp->pdev->dev) &&
807 tg3_flag(tp, WOL_ENABLE)) {
808 tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
809 TG3_APE_HOST_WOL_SPEED_AUTO);
810 apedata = TG3_APE_HOST_DRVR_STATE_WOL;
811 } else
812 apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
813
814 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
815
816 event = APE_EVENT_STATUS_STATE_UNLOAD;
817 break;
818 case RESET_KIND_SUSPEND:
819 event = APE_EVENT_STATUS_STATE_SUSPEND;
820 break;
821 default:
822 return;
823 }
824
825 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
826
827 tg3_ape_send_event(tp, event);
828}
829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830static void tg3_disable_ints(struct tg3 *tp)
831{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000832 int i;
833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 tw32(TG3PCI_MISC_HOST_CTRL,
835 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000836 for (i = 0; i < tp->irq_max; i++)
837 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838}
839
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840static void tg3_enable_ints(struct tg3 *tp)
841{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000842 int i;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000843
Michael Chanbbe832c2005-06-24 20:20:04 -0700844 tp->irq_sync = 0;
845 wmb();
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 tw32(TG3PCI_MISC_HOST_CTRL,
848 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000849
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000850 tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000851 for (i = 0; i < tp->irq_cnt; i++) {
852 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000853
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000854 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
Joe Perches63c3a662011-04-26 08:12:10 +0000855 if (tg3_flag(tp, 1SHOT_MSI))
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000856 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
857
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000858 tp->coal_now |= tnapi->coal_now;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000859 }
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000860
861 /* Force an initial interrupt */
Joe Perches63c3a662011-04-26 08:12:10 +0000862 if (!tg3_flag(tp, TAGGED_STATUS) &&
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000863 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
864 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
865 else
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000866 tw32(HOSTCC_MODE, tp->coal_now);
867
868 tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869}
870
Matt Carlson17375d22009-08-28 14:02:18 +0000871static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
Michael Chan04237dd2005-04-25 15:17:17 -0700872{
Matt Carlson17375d22009-08-28 14:02:18 +0000873 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +0000874 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan04237dd2005-04-25 15:17:17 -0700875 unsigned int work_exists = 0;
876
877 /* check for phy events */
Joe Perches63c3a662011-04-26 08:12:10 +0000878 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Michael Chan04237dd2005-04-25 15:17:17 -0700879 if (sblk->status & SD_STATUS_LINK_CHG)
880 work_exists = 1;
881 }
882 /* check for RX/TX work to do */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000883 if (sblk->idx[0].tx_consumer != tnapi->tx_cons ||
Matt Carlson8d9d7cf2009-09-01 13:19:05 +0000884 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -0700885 work_exists = 1;
886
887 return work_exists;
888}
889
Matt Carlson17375d22009-08-28 14:02:18 +0000890/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -0700891 * similar to tg3_enable_ints, but it accurately determines whether there
892 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400893 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 */
Matt Carlson17375d22009-08-28 14:02:18 +0000895static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
Matt Carlson17375d22009-08-28 14:02:18 +0000897 struct tg3 *tp = tnapi->tp;
898
Matt Carlson898a56f2009-08-28 14:02:40 +0000899 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 mmiowb();
901
David S. Millerfac9b832005-05-18 22:46:34 -0700902 /* When doing tagged status, this work check is unnecessary.
903 * The last_tag we write above tells the chip which piece of
904 * work we've completed.
905 */
Joe Perches63c3a662011-04-26 08:12:10 +0000906 if (!tg3_flag(tp, TAGGED_STATUS) && tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -0700907 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +0000908 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909}
910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911static void tg3_switch_clocks(struct tg3 *tp)
912{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000913 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 u32 orig_clock_ctrl;
915
Joe Perches63c3a662011-04-26 08:12:10 +0000916 if (tg3_flag(tp, CPMU_PRESENT) || tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -0700917 return;
918
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000919 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
920
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 orig_clock_ctrl = clock_ctrl;
922 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
923 CLOCK_CTRL_CLKRUN_OENABLE |
924 0x1f);
925 tp->pci_clock_ctrl = clock_ctrl;
926
Joe Perches63c3a662011-04-26 08:12:10 +0000927 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800929 tw32_wait_f(TG3PCI_CLOCK_CTRL,
930 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 }
932 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800933 tw32_wait_f(TG3PCI_CLOCK_CTRL,
934 clock_ctrl |
935 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
936 40);
937 tw32_wait_f(TG3PCI_CLOCK_CTRL,
938 clock_ctrl | (CLOCK_CTRL_ALTCLK),
939 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 }
Michael Chanb401e9e2005-12-19 16:27:04 -0800941 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942}
943
944#define PHY_BUSY_LOOPS 5000
945
946static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
947{
948 u32 frame_val;
949 unsigned int loops;
950 int ret;
951
952 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
953 tw32_f(MAC_MI_MODE,
954 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
955 udelay(80);
956 }
957
958 *val = 0x0;
959
Matt Carlson882e9792009-09-01 13:21:36 +0000960 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 MI_COM_PHY_ADDR_MASK);
962 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
963 MI_COM_REG_ADDR_MASK);
964 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 tw32_f(MAC_MI_COM, frame_val);
967
968 loops = PHY_BUSY_LOOPS;
969 while (loops != 0) {
970 udelay(10);
971 frame_val = tr32(MAC_MI_COM);
972
973 if ((frame_val & MI_COM_BUSY) == 0) {
974 udelay(5);
975 frame_val = tr32(MAC_MI_COM);
976 break;
977 }
978 loops -= 1;
979 }
980
981 ret = -EBUSY;
982 if (loops != 0) {
983 *val = frame_val & MI_COM_DATA_MASK;
984 ret = 0;
985 }
986
987 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
988 tw32_f(MAC_MI_MODE, tp->mi_mode);
989 udelay(80);
990 }
991
992 return ret;
993}
994
995static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
996{
997 u32 frame_val;
998 unsigned int loops;
999 int ret;
1000
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001001 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson221c5632011-06-13 13:39:01 +00001002 (reg == MII_CTRL1000 || reg == MII_TG3_AUX_CTRL))
Michael Chanb5d37722006-09-27 16:06:21 -07001003 return 0;
1004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1006 tw32_f(MAC_MI_MODE,
1007 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
1008 udelay(80);
1009 }
1010
Matt Carlson882e9792009-09-01 13:21:36 +00001011 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 MI_COM_PHY_ADDR_MASK);
1013 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
1014 MI_COM_REG_ADDR_MASK);
1015 frame_val |= (val & MI_COM_DATA_MASK);
1016 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 tw32_f(MAC_MI_COM, frame_val);
1019
1020 loops = PHY_BUSY_LOOPS;
1021 while (loops != 0) {
1022 udelay(10);
1023 frame_val = tr32(MAC_MI_COM);
1024 if ((frame_val & MI_COM_BUSY) == 0) {
1025 udelay(5);
1026 frame_val = tr32(MAC_MI_COM);
1027 break;
1028 }
1029 loops -= 1;
1030 }
1031
1032 ret = -EBUSY;
1033 if (loops != 0)
1034 ret = 0;
1035
1036 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1037 tw32_f(MAC_MI_MODE, tp->mi_mode);
1038 udelay(80);
1039 }
1040
1041 return ret;
1042}
1043
Matt Carlsonb0988c12011-04-20 07:57:39 +00001044static int tg3_phy_cl45_write(struct tg3 *tp, u32 devad, u32 addr, u32 val)
1045{
1046 int err;
1047
1048 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1049 if (err)
1050 goto done;
1051
1052 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1053 if (err)
1054 goto done;
1055
1056 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1057 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1058 if (err)
1059 goto done;
1060
1061 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, val);
1062
1063done:
1064 return err;
1065}
1066
1067static int tg3_phy_cl45_read(struct tg3 *tp, u32 devad, u32 addr, u32 *val)
1068{
1069 int err;
1070
1071 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1072 if (err)
1073 goto done;
1074
1075 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1076 if (err)
1077 goto done;
1078
1079 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1080 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1081 if (err)
1082 goto done;
1083
1084 err = tg3_readphy(tp, MII_TG3_MMD_ADDRESS, val);
1085
1086done:
1087 return err;
1088}
1089
1090static int tg3_phydsp_read(struct tg3 *tp, u32 reg, u32 *val)
1091{
1092 int err;
1093
1094 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1095 if (!err)
1096 err = tg3_readphy(tp, MII_TG3_DSP_RW_PORT, val);
1097
1098 return err;
1099}
1100
1101static int tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
1102{
1103 int err;
1104
1105 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1106 if (!err)
1107 err = tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
1108
1109 return err;
1110}
1111
Matt Carlson15ee95c2011-04-20 07:57:40 +00001112static int tg3_phy_auxctl_read(struct tg3 *tp, int reg, u32 *val)
1113{
1114 int err;
1115
1116 err = tg3_writephy(tp, MII_TG3_AUX_CTRL,
1117 (reg << MII_TG3_AUXCTL_MISC_RDSEL_SHIFT) |
1118 MII_TG3_AUXCTL_SHDWSEL_MISC);
1119 if (!err)
1120 err = tg3_readphy(tp, MII_TG3_AUX_CTRL, val);
1121
1122 return err;
1123}
1124
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001125static int tg3_phy_auxctl_write(struct tg3 *tp, int reg, u32 set)
1126{
1127 if (reg == MII_TG3_AUXCTL_SHDWSEL_MISC)
1128 set |= MII_TG3_AUXCTL_MISC_WREN;
1129
1130 return tg3_writephy(tp, MII_TG3_AUX_CTRL, set | reg);
1131}
1132
Matt Carlson1d36ba42011-04-20 07:57:42 +00001133#define TG3_PHY_AUXCTL_SMDSP_ENABLE(tp) \
1134 tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
1135 MII_TG3_AUXCTL_ACTL_SMDSP_ENA | \
1136 MII_TG3_AUXCTL_ACTL_TX_6DB)
1137
1138#define TG3_PHY_AUXCTL_SMDSP_DISABLE(tp) \
1139 tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
1140 MII_TG3_AUXCTL_ACTL_TX_6DB);
1141
Matt Carlson95e28692008-05-25 23:44:14 -07001142static int tg3_bmcr_reset(struct tg3 *tp)
1143{
1144 u32 phy_control;
1145 int limit, err;
1146
1147 /* OK, reset it, and poll the BMCR_RESET bit until it
1148 * clears or we time out.
1149 */
1150 phy_control = BMCR_RESET;
1151 err = tg3_writephy(tp, MII_BMCR, phy_control);
1152 if (err != 0)
1153 return -EBUSY;
1154
1155 limit = 5000;
1156 while (limit--) {
1157 err = tg3_readphy(tp, MII_BMCR, &phy_control);
1158 if (err != 0)
1159 return -EBUSY;
1160
1161 if ((phy_control & BMCR_RESET) == 0) {
1162 udelay(40);
1163 break;
1164 }
1165 udelay(10);
1166 }
Roel Kluind4675b52009-02-12 16:33:27 -08001167 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -07001168 return -EBUSY;
1169
1170 return 0;
1171}
1172
Matt Carlson158d7ab2008-05-29 01:37:54 -07001173static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
1174{
Francois Romieu3d165432009-01-19 16:56:50 -08001175 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001176 u32 val;
1177
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001178 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001179
1180 if (tg3_readphy(tp, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001181 val = -EIO;
1182
1183 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001184
1185 return val;
1186}
1187
1188static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
1189{
Francois Romieu3d165432009-01-19 16:56:50 -08001190 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001191 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001192
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001193 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001194
1195 if (tg3_writephy(tp, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001196 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001197
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001198 spin_unlock_bh(&tp->lock);
1199
1200 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001201}
1202
1203static int tg3_mdio_reset(struct mii_bus *bp)
1204{
1205 return 0;
1206}
1207
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001208static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -07001209{
1210 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001211 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -07001212
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001213 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001214 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001215 case PHY_ID_BCM50610:
1216 case PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001217 val = MAC_PHYCFG2_50610_LED_MODES;
1218 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001219 case PHY_ID_BCMAC131:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001220 val = MAC_PHYCFG2_AC131_LED_MODES;
1221 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001222 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001223 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
1224 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001225 case PHY_ID_RTL8201E:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001226 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
1227 break;
1228 default:
Matt Carlsona9daf362008-05-25 23:49:44 -07001229 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001230 }
1231
1232 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
1233 tw32(MAC_PHYCFG2, val);
1234
1235 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001236 val &= ~(MAC_PHYCFG1_RGMII_INT |
1237 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
1238 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001239 tw32(MAC_PHYCFG1, val);
1240
1241 return;
1242 }
1243
Joe Perches63c3a662011-04-26 08:12:10 +00001244 if (!tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001245 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
1246 MAC_PHYCFG2_FMODE_MASK_MASK |
1247 MAC_PHYCFG2_GMODE_MASK_MASK |
1248 MAC_PHYCFG2_ACT_MASK_MASK |
1249 MAC_PHYCFG2_QUAL_MASK_MASK |
1250 MAC_PHYCFG2_INBAND_ENABLE;
1251
1252 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001253
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001254 val = tr32(MAC_PHYCFG1);
1255 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
1256 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
Joe Perches63c3a662011-04-26 08:12:10 +00001257 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1258 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001259 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
Joe Perches63c3a662011-04-26 08:12:10 +00001260 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001261 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1262 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001263 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1264 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1265 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001266
Matt Carlsona9daf362008-05-25 23:49:44 -07001267 val = tr32(MAC_EXT_RGMII_MODE);
1268 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1269 MAC_RGMII_MODE_RX_QUALITY |
1270 MAC_RGMII_MODE_RX_ACTIVITY |
1271 MAC_RGMII_MODE_RX_ENG_DET |
1272 MAC_RGMII_MODE_TX_ENABLE |
1273 MAC_RGMII_MODE_TX_LOWPWR |
1274 MAC_RGMII_MODE_TX_RESET);
Joe Perches63c3a662011-04-26 08:12:10 +00001275 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1276 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001277 val |= MAC_RGMII_MODE_RX_INT_B |
1278 MAC_RGMII_MODE_RX_QUALITY |
1279 MAC_RGMII_MODE_RX_ACTIVITY |
1280 MAC_RGMII_MODE_RX_ENG_DET;
Joe Perches63c3a662011-04-26 08:12:10 +00001281 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001282 val |= MAC_RGMII_MODE_TX_ENABLE |
1283 MAC_RGMII_MODE_TX_LOWPWR |
1284 MAC_RGMII_MODE_TX_RESET;
1285 }
1286 tw32(MAC_EXT_RGMII_MODE, val);
1287}
1288
Matt Carlson158d7ab2008-05-29 01:37:54 -07001289static void tg3_mdio_start(struct tg3 *tp)
1290{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001291 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1292 tw32_f(MAC_MI_MODE, tp->mi_mode);
1293 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001294
Joe Perches63c3a662011-04-26 08:12:10 +00001295 if (tg3_flag(tp, MDIOBUS_INITED) &&
Matt Carlson9ea48182010-02-17 15:17:01 +00001296 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1297 tg3_mdio_config_5785(tp);
1298}
1299
1300static int tg3_mdio_init(struct tg3 *tp)
1301{
1302 int i;
1303 u32 reg;
1304 struct phy_device *phydev;
1305
Joe Perches63c3a662011-04-26 08:12:10 +00001306 if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson9c7df912010-06-05 17:24:36 +00001307 u32 is_serdes;
Matt Carlson882e9792009-09-01 13:21:36 +00001308
Matt Carlson69f11c92011-07-13 09:27:30 +00001309 tp->phy_addr = tp->pci_fn + 1;
Matt Carlson882e9792009-09-01 13:21:36 +00001310
Matt Carlsond1ec96a2010-01-12 10:11:38 +00001311 if (tp->pci_chip_rev_id != CHIPREV_ID_5717_A0)
1312 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1313 else
1314 is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1315 TG3_CPMU_PHY_STRAP_IS_SERDES;
Matt Carlson882e9792009-09-01 13:21:36 +00001316 if (is_serdes)
1317 tp->phy_addr += 7;
1318 } else
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001319 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001320
Matt Carlson158d7ab2008-05-29 01:37:54 -07001321 tg3_mdio_start(tp);
1322
Joe Perches63c3a662011-04-26 08:12:10 +00001323 if (!tg3_flag(tp, USE_PHYLIB) || tg3_flag(tp, MDIOBUS_INITED))
Matt Carlson158d7ab2008-05-29 01:37:54 -07001324 return 0;
1325
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001326 tp->mdio_bus = mdiobus_alloc();
1327 if (tp->mdio_bus == NULL)
1328 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001329
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001330 tp->mdio_bus->name = "tg3 mdio bus";
1331 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001332 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001333 tp->mdio_bus->priv = tp;
1334 tp->mdio_bus->parent = &tp->pdev->dev;
1335 tp->mdio_bus->read = &tg3_mdio_read;
1336 tp->mdio_bus->write = &tg3_mdio_write;
1337 tp->mdio_bus->reset = &tg3_mdio_reset;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001338 tp->mdio_bus->phy_mask = ~(1 << TG3_PHY_MII_ADDR);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001339 tp->mdio_bus->irq = &tp->mdio_irq[0];
Matt Carlson158d7ab2008-05-29 01:37:54 -07001340
1341 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001342 tp->mdio_bus->irq[i] = PHY_POLL;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001343
1344 /* The bus registration will look for all the PHYs on the mdio bus.
1345 * Unfortunately, it does not ensure the PHY is powered up before
1346 * accessing the PHY ID registers. A chip reset is the
1347 * quickest way to bring the device back to an operational state..
1348 */
1349 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1350 tg3_bmcr_reset(tp);
1351
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001352 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001353 if (i) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001354 dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001355 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001356 return i;
1357 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001358
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001359 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsona9daf362008-05-25 23:49:44 -07001360
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001361 if (!phydev || !phydev->drv) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001362 dev_warn(&tp->pdev->dev, "No PHY devices\n");
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001363 mdiobus_unregister(tp->mdio_bus);
1364 mdiobus_free(tp->mdio_bus);
1365 return -ENODEV;
1366 }
1367
1368 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001369 case PHY_ID_BCM57780:
Matt Carlson321d32a2008-11-21 17:22:19 -08001370 phydev->interface = PHY_INTERFACE_MODE_GMII;
Matt Carlsonc704dc22009-11-02 14:32:12 +00001371 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08001372 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001373 case PHY_ID_BCM50610:
1374 case PHY_ID_BCM50610M:
Matt Carlson32e5a8d2009-11-02 14:31:39 +00001375 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001376 PHY_BRCM_RX_REFCLK_UNUSED |
Matt Carlson52fae082009-11-02 14:32:38 +00001377 PHY_BRCM_DIS_TXCRXC_NOENRGY |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001378 PHY_BRCM_AUTO_PWRDWN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001379 if (tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsona9daf362008-05-25 23:49:44 -07001380 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001381 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001382 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001383 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001384 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001385 /* fallthru */
Matt Carlson6a443a02010-02-17 15:17:04 +00001386 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001387 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001388 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001389 case PHY_ID_RTL8201E:
1390 case PHY_ID_BCMAC131:
Matt Carlsona9daf362008-05-25 23:49:44 -07001391 phydev->interface = PHY_INTERFACE_MODE_MII;
Matt Carlsoncdd4e09d2009-11-02 14:31:11 +00001392 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001393 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlsona9daf362008-05-25 23:49:44 -07001394 break;
1395 }
1396
Joe Perches63c3a662011-04-26 08:12:10 +00001397 tg3_flag_set(tp, MDIOBUS_INITED);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001398
1399 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1400 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001401
1402 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001403}
1404
1405static void tg3_mdio_fini(struct tg3 *tp)
1406{
Joe Perches63c3a662011-04-26 08:12:10 +00001407 if (tg3_flag(tp, MDIOBUS_INITED)) {
1408 tg3_flag_clear(tp, MDIOBUS_INITED);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001409 mdiobus_unregister(tp->mdio_bus);
1410 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001411 }
1412}
1413
Matt Carlson95e28692008-05-25 23:44:14 -07001414/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001415static inline void tg3_generate_fw_event(struct tg3 *tp)
1416{
1417 u32 val;
1418
1419 val = tr32(GRC_RX_CPU_EVENT);
1420 val |= GRC_RX_CPU_DRIVER_EVENT;
1421 tw32_f(GRC_RX_CPU_EVENT, val);
1422
1423 tp->last_event_jiffies = jiffies;
1424}
1425
1426#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1427
1428/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001429static void tg3_wait_for_event_ack(struct tg3 *tp)
1430{
1431 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001432 unsigned int delay_cnt;
1433 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001434
Matt Carlson4ba526c2008-08-15 14:10:04 -07001435 /* If enough time has passed, no wait is necessary. */
1436 time_remain = (long)(tp->last_event_jiffies + 1 +
1437 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1438 (long)jiffies;
1439 if (time_remain < 0)
1440 return;
1441
1442 /* Check if we can shorten the wait time. */
1443 delay_cnt = jiffies_to_usecs(time_remain);
1444 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1445 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1446 delay_cnt = (delay_cnt >> 3) + 1;
1447
1448 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001449 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1450 break;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001451 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001452 }
1453}
1454
1455/* tp->lock is held. */
1456static void tg3_ump_link_report(struct tg3 *tp)
1457{
1458 u32 reg;
1459 u32 val;
1460
Joe Perches63c3a662011-04-26 08:12:10 +00001461 if (!tg3_flag(tp, 5780_CLASS) || !tg3_flag(tp, ENABLE_ASF))
Matt Carlson95e28692008-05-25 23:44:14 -07001462 return;
1463
1464 tg3_wait_for_event_ack(tp);
1465
1466 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1467
1468 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1469
1470 val = 0;
1471 if (!tg3_readphy(tp, MII_BMCR, &reg))
1472 val = reg << 16;
1473 if (!tg3_readphy(tp, MII_BMSR, &reg))
1474 val |= (reg & 0xffff);
1475 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX, val);
1476
1477 val = 0;
1478 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1479 val = reg << 16;
1480 if (!tg3_readphy(tp, MII_LPA, &reg))
1481 val |= (reg & 0xffff);
1482 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 4, val);
1483
1484 val = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001485 if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
Matt Carlson95e28692008-05-25 23:44:14 -07001486 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1487 val = reg << 16;
1488 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1489 val |= (reg & 0xffff);
1490 }
1491 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 8, val);
1492
1493 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1494 val = reg << 16;
1495 else
1496 val = 0;
1497 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 12, val);
1498
Matt Carlson4ba526c2008-08-15 14:10:04 -07001499 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001500}
1501
Matt Carlson8d5a89b2011-08-31 11:44:51 +00001502/* tp->lock is held. */
1503static void tg3_stop_fw(struct tg3 *tp)
1504{
1505 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
1506 /* Wait for RX cpu to ACK the previous event. */
1507 tg3_wait_for_event_ack(tp);
1508
1509 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
1510
1511 tg3_generate_fw_event(tp);
1512
1513 /* Wait for RX cpu to ACK this event. */
1514 tg3_wait_for_event_ack(tp);
1515 }
1516}
1517
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001518/* tp->lock is held. */
1519static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
1520{
1521 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
1522 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
1523
1524 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1525 switch (kind) {
1526 case RESET_KIND_INIT:
1527 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1528 DRV_STATE_START);
1529 break;
1530
1531 case RESET_KIND_SHUTDOWN:
1532 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1533 DRV_STATE_UNLOAD);
1534 break;
1535
1536 case RESET_KIND_SUSPEND:
1537 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1538 DRV_STATE_SUSPEND);
1539 break;
1540
1541 default:
1542 break;
1543 }
1544 }
1545
1546 if (kind == RESET_KIND_INIT ||
1547 kind == RESET_KIND_SUSPEND)
1548 tg3_ape_driver_state_change(tp, kind);
1549}
1550
1551/* tp->lock is held. */
1552static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
1553{
1554 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1555 switch (kind) {
1556 case RESET_KIND_INIT:
1557 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1558 DRV_STATE_START_DONE);
1559 break;
1560
1561 case RESET_KIND_SHUTDOWN:
1562 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1563 DRV_STATE_UNLOAD_DONE);
1564 break;
1565
1566 default:
1567 break;
1568 }
1569 }
1570
1571 if (kind == RESET_KIND_SHUTDOWN)
1572 tg3_ape_driver_state_change(tp, kind);
1573}
1574
1575/* tp->lock is held. */
1576static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
1577{
1578 if (tg3_flag(tp, ENABLE_ASF)) {
1579 switch (kind) {
1580 case RESET_KIND_INIT:
1581 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1582 DRV_STATE_START);
1583 break;
1584
1585 case RESET_KIND_SHUTDOWN:
1586 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1587 DRV_STATE_UNLOAD);
1588 break;
1589
1590 case RESET_KIND_SUSPEND:
1591 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1592 DRV_STATE_SUSPEND);
1593 break;
1594
1595 default:
1596 break;
1597 }
1598 }
1599}
1600
1601static int tg3_poll_fw(struct tg3 *tp)
1602{
1603 int i;
1604 u32 val;
1605
1606 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
1607 /* Wait up to 20ms for init done. */
1608 for (i = 0; i < 200; i++) {
1609 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
1610 return 0;
1611 udelay(100);
1612 }
1613 return -ENODEV;
1614 }
1615
1616 /* Wait for firmware initialization to complete. */
1617 for (i = 0; i < 100000; i++) {
1618 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
1619 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
1620 break;
1621 udelay(10);
1622 }
1623
1624 /* Chip might not be fitted with firmware. Some Sun onboard
1625 * parts are configured like that. So don't signal the timeout
1626 * of the above loop as an error, but do report the lack of
1627 * running firmware once.
1628 */
1629 if (i >= 100000 && !tg3_flag(tp, NO_FWARE_REPORTED)) {
1630 tg3_flag_set(tp, NO_FWARE_REPORTED);
1631
1632 netdev_info(tp->dev, "No firmware running\n");
1633 }
1634
1635 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
1636 /* The 57765 A0 needs a little more
1637 * time to do some important work.
1638 */
1639 mdelay(10);
1640 }
1641
1642 return 0;
1643}
1644
Matt Carlson95e28692008-05-25 23:44:14 -07001645static void tg3_link_report(struct tg3 *tp)
1646{
1647 if (!netif_carrier_ok(tp->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001648 netif_info(tp, link, tp->dev, "Link is down\n");
Matt Carlson95e28692008-05-25 23:44:14 -07001649 tg3_ump_link_report(tp);
1650 } else if (netif_msg_link(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001651 netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
1652 (tp->link_config.active_speed == SPEED_1000 ?
1653 1000 :
1654 (tp->link_config.active_speed == SPEED_100 ?
1655 100 : 10)),
1656 (tp->link_config.active_duplex == DUPLEX_FULL ?
1657 "full" : "half"));
Matt Carlson95e28692008-05-25 23:44:14 -07001658
Joe Perches05dbe002010-02-17 19:44:19 +00001659 netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
1660 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
1661 "on" : "off",
1662 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
1663 "on" : "off");
Matt Carlson47007832011-04-20 07:57:43 +00001664
1665 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
1666 netdev_info(tp->dev, "EEE is %s\n",
1667 tp->setlpicnt ? "enabled" : "disabled");
1668
Matt Carlson95e28692008-05-25 23:44:14 -07001669 tg3_ump_link_report(tp);
1670 }
1671}
1672
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 Mason708ebb3a2011-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 Mason708ebb3a2011-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 Carlson42b64a42011-05-19 12:12:49 +00003598 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
3599 goto done;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003600
Matt Carlson37f07022011-11-17 14:30:55 +00003601 new_adv = ethtool_adv_to_mii_ctrl1000_t(advertise);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003602
Matt Carlson42b64a42011-05-19 12:12:49 +00003603 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3604 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
Matt Carlson221c5632011-06-13 13:39:01 +00003605 new_adv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606
Matt Carlson221c5632011-06-13 13:39:01 +00003607 err = tg3_writephy(tp, MII_CTRL1000, new_adv);
Matt Carlson42b64a42011-05-19 12:12:49 +00003608 if (err)
3609 goto done;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003610
Matt Carlson42b64a42011-05-19 12:12:49 +00003611 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
3612 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613
Matt Carlson42b64a42011-05-19 12:12:49 +00003614 tw32(TG3_CPMU_EEE_MODE,
3615 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003616
Matt Carlson42b64a42011-05-19 12:12:49 +00003617 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
3618 if (!err) {
3619 u32 err2;
Matt Carlson52b02d02010-10-14 10:37:41 +00003620
Matt Carlsona6b68da2010-12-06 08:28:52 +00003621 val = 0;
Matt Carlson42b64a42011-05-19 12:12:49 +00003622 /* Advertise 100-BaseTX EEE ability */
3623 if (advertise & ADVERTISED_100baseT_Full)
3624 val |= MDIO_AN_EEE_ADV_100TX;
3625 /* Advertise 1000-BaseT EEE ability */
3626 if (advertise & ADVERTISED_1000baseT_Full)
3627 val |= MDIO_AN_EEE_ADV_1000T;
3628 err = tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlsonb715ce92011-07-20 10:20:52 +00003629 if (err)
3630 val = 0;
3631
3632 switch (GET_ASIC_REV(tp->pci_chip_rev_id)) {
3633 case ASIC_REV_5717:
3634 case ASIC_REV_57765:
3635 case ASIC_REV_5719:
3636 /* If we advertised any eee advertisements above... */
3637 if (val)
3638 val = MII_TG3_DSP_TAP26_ALNOKO |
3639 MII_TG3_DSP_TAP26_RMRXSTO |
3640 MII_TG3_DSP_TAP26_OPCSINPT;
3641 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
3642 /* Fall through */
3643 case ASIC_REV_5720:
3644 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
3645 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
3646 MII_TG3_DSP_CH34TP2_HIBW01);
3647 }
Matt Carlson52b02d02010-10-14 10:37:41 +00003648
Matt Carlson42b64a42011-05-19 12:12:49 +00003649 err2 = TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
3650 if (!err)
3651 err = err2;
3652 }
3653
3654done:
3655 return err;
3656}
3657
3658static void tg3_phy_copper_begin(struct tg3 *tp)
3659{
3660 u32 new_adv;
3661 int i;
3662
3663 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
3664 new_adv = ADVERTISED_10baseT_Half |
3665 ADVERTISED_10baseT_Full;
3666 if (tg3_flag(tp, WOL_SPEED_100MB))
3667 new_adv |= ADVERTISED_100baseT_Half |
3668 ADVERTISED_100baseT_Full;
3669
3670 tg3_phy_autoneg_cfg(tp, new_adv,
3671 FLOW_CTRL_TX | FLOW_CTRL_RX);
3672 } else if (tp->link_config.speed == SPEED_INVALID) {
3673 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
3674 tp->link_config.advertising &=
3675 ~(ADVERTISED_1000baseT_Half |
3676 ADVERTISED_1000baseT_Full);
3677
3678 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
3679 tp->link_config.flowctrl);
3680 } else {
3681 /* Asking for a specific link mode. */
3682 if (tp->link_config.speed == SPEED_1000) {
3683 if (tp->link_config.duplex == DUPLEX_FULL)
3684 new_adv = ADVERTISED_1000baseT_Full;
3685 else
3686 new_adv = ADVERTISED_1000baseT_Half;
3687 } else if (tp->link_config.speed == SPEED_100) {
3688 if (tp->link_config.duplex == DUPLEX_FULL)
3689 new_adv = ADVERTISED_100baseT_Full;
3690 else
3691 new_adv = ADVERTISED_100baseT_Half;
3692 } else {
3693 if (tp->link_config.duplex == DUPLEX_FULL)
3694 new_adv = ADVERTISED_10baseT_Full;
3695 else
3696 new_adv = ADVERTISED_10baseT_Half;
3697 }
3698
3699 tg3_phy_autoneg_cfg(tp, new_adv,
3700 tp->link_config.flowctrl);
Matt Carlson52b02d02010-10-14 10:37:41 +00003701 }
3702
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 if (tp->link_config.autoneg == AUTONEG_DISABLE &&
3704 tp->link_config.speed != SPEED_INVALID) {
3705 u32 bmcr, orig_bmcr;
3706
3707 tp->link_config.active_speed = tp->link_config.speed;
3708 tp->link_config.active_duplex = tp->link_config.duplex;
3709
3710 bmcr = 0;
3711 switch (tp->link_config.speed) {
3712 default:
3713 case SPEED_10:
3714 break;
3715
3716 case SPEED_100:
3717 bmcr |= BMCR_SPEED100;
3718 break;
3719
3720 case SPEED_1000:
Matt Carlson221c5632011-06-13 13:39:01 +00003721 bmcr |= BMCR_SPEED1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724
3725 if (tp->link_config.duplex == DUPLEX_FULL)
3726 bmcr |= BMCR_FULLDPLX;
3727
3728 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
3729 (bmcr != orig_bmcr)) {
3730 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
3731 for (i = 0; i < 1500; i++) {
3732 u32 tmp;
3733
3734 udelay(10);
3735 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
3736 tg3_readphy(tp, MII_BMSR, &tmp))
3737 continue;
3738 if (!(tmp & BMSR_LSTATUS)) {
3739 udelay(40);
3740 break;
3741 }
3742 }
3743 tg3_writephy(tp, MII_BMCR, bmcr);
3744 udelay(40);
3745 }
3746 } else {
3747 tg3_writephy(tp, MII_BMCR,
3748 BMCR_ANENABLE | BMCR_ANRESTART);
3749 }
3750}
3751
3752static int tg3_init_5401phy_dsp(struct tg3 *tp)
3753{
3754 int err;
3755
3756 /* Turn off tap power management. */
3757 /* Set Extended packet length bit */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003758 err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00003760 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
3761 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
3762 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
3763 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
3764 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765
3766 udelay(40);
3767
3768 return err;
3769}
3770
Michael Chan3600d912006-12-07 00:21:48 -08003771static int tg3_copper_is_advertising_all(struct tg3 *tp, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772{
Michael Chan3600d912006-12-07 00:21:48 -08003773 u32 adv_reg, all_mask = 0;
3774
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +00003775 all_mask = ethtool_adv_to_mii_adv_t(mask) & ADVERTISE_ALL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776
3777 if (tg3_readphy(tp, MII_ADVERTISE, &adv_reg))
3778 return 0;
3779
Matt Carlsonb99d2a52011-08-31 11:44:47 +00003780 if ((adv_reg & ADVERTISE_ALL) != all_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 return 0;
Matt Carlsonb99d2a52011-08-31 11:44:47 +00003782
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003783 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 u32 tg3_ctrl;
3785
Matt Carlson37f07022011-11-17 14:30:55 +00003786 all_mask = ethtool_adv_to_mii_ctrl1000_t(mask);
Michael Chan3600d912006-12-07 00:21:48 -08003787
Matt Carlson221c5632011-06-13 13:39:01 +00003788 if (tg3_readphy(tp, MII_CTRL1000, &tg3_ctrl))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 return 0;
3790
Matt Carlsonb99d2a52011-08-31 11:44:47 +00003791 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL);
3792 if (tg3_ctrl != all_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 return 0;
3794 }
Matt Carlson93a700a2011-08-31 11:44:54 +00003795
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 return 1;
3797}
3798
Matt Carlsonef167e22007-12-20 20:10:01 -08003799static int tg3_adv_1000T_flowctrl_ok(struct tg3 *tp, u32 *lcladv, u32 *rmtadv)
3800{
3801 u32 curadv, reqadv;
3802
3803 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
3804 return 1;
3805
3806 curadv = *lcladv & (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
3807 reqadv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
3808
3809 if (tp->link_config.active_duplex == DUPLEX_FULL) {
3810 if (curadv != reqadv)
3811 return 0;
3812
Joe Perches63c3a662011-04-26 08:12:10 +00003813 if (tg3_flag(tp, PAUSE_AUTONEG))
Matt Carlsonef167e22007-12-20 20:10:01 -08003814 tg3_readphy(tp, MII_LPA, rmtadv);
3815 } else {
3816 /* Reprogram the advertisement register, even if it
3817 * does not affect the current link. If the link
3818 * gets renegotiated in the future, we can save an
3819 * additional renegotiation cycle by advertising
3820 * it correctly in the first place.
3821 */
3822 if (curadv != reqadv) {
3823 *lcladv &= ~(ADVERTISE_PAUSE_CAP |
3824 ADVERTISE_PAUSE_ASYM);
3825 tg3_writephy(tp, MII_ADVERTISE, *lcladv | reqadv);
3826 }
3827 }
3828
3829 return 1;
3830}
3831
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
3833{
3834 int current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003835 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08003836 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 u16 current_speed;
3838 u8 current_duplex;
3839 int i, err;
3840
3841 tw32(MAC_EVENT, 0);
3842
3843 tw32_f(MAC_STATUS,
3844 (MAC_STATUS_SYNC_CHANGED |
3845 MAC_STATUS_CFG_CHANGED |
3846 MAC_STATUS_MI_COMPLETION |
3847 MAC_STATUS_LNKSTATE_CHANGED));
3848 udelay(40);
3849
Matt Carlson8ef21422008-05-02 16:47:53 -07003850 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
3851 tw32_f(MAC_MI_MODE,
3852 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
3853 udelay(80);
3854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003856 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857
3858 /* Some third-party PHYs need to be reset on link going
3859 * down.
3860 */
3861 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3862 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
3863 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
3864 netif_carrier_ok(tp->dev)) {
3865 tg3_readphy(tp, MII_BMSR, &bmsr);
3866 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3867 !(bmsr & BMSR_LSTATUS))
3868 force_reset = 1;
3869 }
3870 if (force_reset)
3871 tg3_phy_reset(tp);
3872
Matt Carlson79eb6902010-02-17 15:17:03 +00003873 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874 tg3_readphy(tp, MII_BMSR, &bmsr);
3875 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
Joe Perches63c3a662011-04-26 08:12:10 +00003876 !tg3_flag(tp, INIT_COMPLETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877 bmsr = 0;
3878
3879 if (!(bmsr & BMSR_LSTATUS)) {
3880 err = tg3_init_5401phy_dsp(tp);
3881 if (err)
3882 return err;
3883
3884 tg3_readphy(tp, MII_BMSR, &bmsr);
3885 for (i = 0; i < 1000; i++) {
3886 udelay(10);
3887 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3888 (bmsr & BMSR_LSTATUS)) {
3889 udelay(40);
3890 break;
3891 }
3892 }
3893
Matt Carlson79eb6902010-02-17 15:17:03 +00003894 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
3895 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 !(bmsr & BMSR_LSTATUS) &&
3897 tp->link_config.active_speed == SPEED_1000) {
3898 err = tg3_phy_reset(tp);
3899 if (!err)
3900 err = tg3_init_5401phy_dsp(tp);
3901 if (err)
3902 return err;
3903 }
3904 }
3905 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3906 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
3907 /* 5701 {A0,B0} CRC bug workaround */
3908 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00003909 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
3910 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
3911 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 }
3913
3914 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003915 tg3_readphy(tp, MII_TG3_ISTAT, &val);
3916 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003918 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003920 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 tg3_writephy(tp, MII_TG3_IMASK, ~0);
3922
3923 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3924 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3925 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
3926 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3927 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
3928 else
3929 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
3930 }
3931
3932 current_link_up = 0;
3933 current_speed = SPEED_INVALID;
3934 current_duplex = DUPLEX_INVALID;
Matt Carlsone348c5e2011-11-21 15:01:20 +00003935 tp->phy_flags &= ~TG3_PHYFLG_MDIX_STATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003937 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Matt Carlson15ee95c2011-04-20 07:57:40 +00003938 err = tg3_phy_auxctl_read(tp,
3939 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
3940 &val);
3941 if (!err && !(val & (1 << 10))) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003942 tg3_phy_auxctl_write(tp,
3943 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
3944 val | (1 << 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945 goto relink;
3946 }
3947 }
3948
3949 bmsr = 0;
3950 for (i = 0; i < 100; i++) {
3951 tg3_readphy(tp, MII_BMSR, &bmsr);
3952 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3953 (bmsr & BMSR_LSTATUS))
3954 break;
3955 udelay(40);
3956 }
3957
3958 if (bmsr & BMSR_LSTATUS) {
3959 u32 aux_stat, bmcr;
3960
3961 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
3962 for (i = 0; i < 2000; i++) {
3963 udelay(10);
3964 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
3965 aux_stat)
3966 break;
3967 }
3968
3969 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
3970 &current_speed,
3971 &current_duplex);
3972
3973 bmcr = 0;
3974 for (i = 0; i < 200; i++) {
3975 tg3_readphy(tp, MII_BMCR, &bmcr);
3976 if (tg3_readphy(tp, MII_BMCR, &bmcr))
3977 continue;
3978 if (bmcr && bmcr != 0x7fff)
3979 break;
3980 udelay(10);
3981 }
3982
Matt Carlsonef167e22007-12-20 20:10:01 -08003983 lcl_adv = 0;
3984 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985
Matt Carlsonef167e22007-12-20 20:10:01 -08003986 tp->link_config.active_speed = current_speed;
3987 tp->link_config.active_duplex = current_duplex;
3988
3989 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3990 if ((bmcr & BMCR_ANENABLE) &&
3991 tg3_copper_is_advertising_all(tp,
3992 tp->link_config.advertising)) {
3993 if (tg3_adv_1000T_flowctrl_ok(tp, &lcl_adv,
3994 &rmt_adv))
3995 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 }
3997 } else {
3998 if (!(bmcr & BMCR_ANENABLE) &&
3999 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08004000 tp->link_config.duplex == current_duplex &&
4001 tp->link_config.flowctrl ==
4002 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 }
4005 }
4006
Matt Carlsonef167e22007-12-20 20:10:01 -08004007 if (current_link_up == 1 &&
Matt Carlsone348c5e2011-11-21 15:01:20 +00004008 tp->link_config.active_duplex == DUPLEX_FULL) {
4009 u32 reg, bit;
4010
4011 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
4012 reg = MII_TG3_FET_GEN_STAT;
4013 bit = MII_TG3_FET_GEN_STAT_MDIXSTAT;
4014 } else {
4015 reg = MII_TG3_EXT_STAT;
4016 bit = MII_TG3_EXT_STAT_MDIX;
4017 }
4018
4019 if (!tg3_readphy(tp, reg, &val) && (val & bit))
4020 tp->phy_flags |= TG3_PHYFLG_MDIX_STATE;
4021
Matt Carlsonef167e22007-12-20 20:10:01 -08004022 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Matt Carlsone348c5e2011-11-21 15:01:20 +00004023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 }
4025
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026relink:
Matt Carlson80096062010-08-02 11:26:06 +00004027 if (current_link_up == 0 || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 tg3_phy_copper_begin(tp);
4029
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004030 tg3_readphy(tp, MII_BMSR, &bmsr);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00004031 if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
4032 (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033 current_link_up = 1;
4034 }
4035
4036 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
4037 if (current_link_up == 1) {
4038 if (tp->link_config.active_speed == SPEED_100 ||
4039 tp->link_config.active_speed == SPEED_10)
4040 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4041 else
4042 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004043 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00004044 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4045 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4047
4048 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4049 if (tp->link_config.active_duplex == DUPLEX_HALF)
4050 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4051
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004053 if (current_link_up == 1 &&
4054 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004056 else
4057 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058 }
4059
4060 /* ??? Without this setting Netgear GA302T PHY does not
4061 * ??? send/receive packets...
4062 */
Matt Carlson79eb6902010-02-17 15:17:03 +00004063 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
4065 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
4066 tw32_f(MAC_MI_MODE, tp->mi_mode);
4067 udelay(80);
4068 }
4069
4070 tw32_f(MAC_MODE, tp->mac_mode);
4071 udelay(40);
4072
Matt Carlson52b02d02010-10-14 10:37:41 +00004073 tg3_phy_eee_adjust(tp, current_link_up);
4074
Joe Perches63c3a662011-04-26 08:12:10 +00004075 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 /* Polled via timer. */
4077 tw32_f(MAC_EVENT, 0);
4078 } else {
4079 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4080 }
4081 udelay(40);
4082
4083 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
4084 current_link_up == 1 &&
4085 tp->link_config.active_speed == SPEED_1000 &&
Joe Perches63c3a662011-04-26 08:12:10 +00004086 (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 udelay(120);
4088 tw32_f(MAC_STATUS,
4089 (MAC_STATUS_SYNC_CHANGED |
4090 MAC_STATUS_CFG_CHANGED));
4091 udelay(40);
4092 tg3_write_mem(tp,
4093 NIC_SRAM_FIRMWARE_MBOX,
4094 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
4095 }
4096
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004097 /* Prevent send BD corruption. */
Joe Perches63c3a662011-04-26 08:12:10 +00004098 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004099 u16 oldlnkctl, newlnkctl;
4100
4101 pci_read_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +00004102 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004103 &oldlnkctl);
4104 if (tp->link_config.active_speed == SPEED_100 ||
4105 tp->link_config.active_speed == SPEED_10)
4106 newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
4107 else
4108 newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
4109 if (newlnkctl != oldlnkctl)
4110 pci_write_config_word(tp->pdev,
Matt Carlson93a700a2011-08-31 11:44:54 +00004111 pci_pcie_cap(tp->pdev) +
4112 PCI_EXP_LNKCTL, newlnkctl);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004113 }
4114
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 if (current_link_up != netif_carrier_ok(tp->dev)) {
4116 if (current_link_up)
4117 netif_carrier_on(tp->dev);
4118 else
4119 netif_carrier_off(tp->dev);
4120 tg3_link_report(tp);
4121 }
4122
4123 return 0;
4124}
4125
4126struct tg3_fiber_aneginfo {
4127 int state;
4128#define ANEG_STATE_UNKNOWN 0
4129#define ANEG_STATE_AN_ENABLE 1
4130#define ANEG_STATE_RESTART_INIT 2
4131#define ANEG_STATE_RESTART 3
4132#define ANEG_STATE_DISABLE_LINK_OK 4
4133#define ANEG_STATE_ABILITY_DETECT_INIT 5
4134#define ANEG_STATE_ABILITY_DETECT 6
4135#define ANEG_STATE_ACK_DETECT_INIT 7
4136#define ANEG_STATE_ACK_DETECT 8
4137#define ANEG_STATE_COMPLETE_ACK_INIT 9
4138#define ANEG_STATE_COMPLETE_ACK 10
4139#define ANEG_STATE_IDLE_DETECT_INIT 11
4140#define ANEG_STATE_IDLE_DETECT 12
4141#define ANEG_STATE_LINK_OK 13
4142#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
4143#define ANEG_STATE_NEXT_PAGE_WAIT 15
4144
4145 u32 flags;
4146#define MR_AN_ENABLE 0x00000001
4147#define MR_RESTART_AN 0x00000002
4148#define MR_AN_COMPLETE 0x00000004
4149#define MR_PAGE_RX 0x00000008
4150#define MR_NP_LOADED 0x00000010
4151#define MR_TOGGLE_TX 0x00000020
4152#define MR_LP_ADV_FULL_DUPLEX 0x00000040
4153#define MR_LP_ADV_HALF_DUPLEX 0x00000080
4154#define MR_LP_ADV_SYM_PAUSE 0x00000100
4155#define MR_LP_ADV_ASYM_PAUSE 0x00000200
4156#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
4157#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
4158#define MR_LP_ADV_NEXT_PAGE 0x00001000
4159#define MR_TOGGLE_RX 0x00002000
4160#define MR_NP_RX 0x00004000
4161
4162#define MR_LINK_OK 0x80000000
4163
4164 unsigned long link_time, cur_time;
4165
4166 u32 ability_match_cfg;
4167 int ability_match_count;
4168
4169 char ability_match, idle_match, ack_match;
4170
4171 u32 txconfig, rxconfig;
4172#define ANEG_CFG_NP 0x00000080
4173#define ANEG_CFG_ACK 0x00000040
4174#define ANEG_CFG_RF2 0x00000020
4175#define ANEG_CFG_RF1 0x00000010
4176#define ANEG_CFG_PS2 0x00000001
4177#define ANEG_CFG_PS1 0x00008000
4178#define ANEG_CFG_HD 0x00004000
4179#define ANEG_CFG_FD 0x00002000
4180#define ANEG_CFG_INVAL 0x00001f06
4181
4182};
4183#define ANEG_OK 0
4184#define ANEG_DONE 1
4185#define ANEG_TIMER_ENAB 2
4186#define ANEG_FAILED -1
4187
4188#define ANEG_STATE_SETTLE_TIME 10000
4189
4190static int tg3_fiber_aneg_smachine(struct tg3 *tp,
4191 struct tg3_fiber_aneginfo *ap)
4192{
Matt Carlson5be73b42007-12-20 20:09:29 -08004193 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 unsigned long delta;
4195 u32 rx_cfg_reg;
4196 int ret;
4197
4198 if (ap->state == ANEG_STATE_UNKNOWN) {
4199 ap->rxconfig = 0;
4200 ap->link_time = 0;
4201 ap->cur_time = 0;
4202 ap->ability_match_cfg = 0;
4203 ap->ability_match_count = 0;
4204 ap->ability_match = 0;
4205 ap->idle_match = 0;
4206 ap->ack_match = 0;
4207 }
4208 ap->cur_time++;
4209
4210 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
4211 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
4212
4213 if (rx_cfg_reg != ap->ability_match_cfg) {
4214 ap->ability_match_cfg = rx_cfg_reg;
4215 ap->ability_match = 0;
4216 ap->ability_match_count = 0;
4217 } else {
4218 if (++ap->ability_match_count > 1) {
4219 ap->ability_match = 1;
4220 ap->ability_match_cfg = rx_cfg_reg;
4221 }
4222 }
4223 if (rx_cfg_reg & ANEG_CFG_ACK)
4224 ap->ack_match = 1;
4225 else
4226 ap->ack_match = 0;
4227
4228 ap->idle_match = 0;
4229 } else {
4230 ap->idle_match = 1;
4231 ap->ability_match_cfg = 0;
4232 ap->ability_match_count = 0;
4233 ap->ability_match = 0;
4234 ap->ack_match = 0;
4235
4236 rx_cfg_reg = 0;
4237 }
4238
4239 ap->rxconfig = rx_cfg_reg;
4240 ret = ANEG_OK;
4241
Matt Carlson33f401a2010-04-05 10:19:27 +00004242 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 case ANEG_STATE_UNKNOWN:
4244 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
4245 ap->state = ANEG_STATE_AN_ENABLE;
4246
4247 /* fallthru */
4248 case ANEG_STATE_AN_ENABLE:
4249 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
4250 if (ap->flags & MR_AN_ENABLE) {
4251 ap->link_time = 0;
4252 ap->cur_time = 0;
4253 ap->ability_match_cfg = 0;
4254 ap->ability_match_count = 0;
4255 ap->ability_match = 0;
4256 ap->idle_match = 0;
4257 ap->ack_match = 0;
4258
4259 ap->state = ANEG_STATE_RESTART_INIT;
4260 } else {
4261 ap->state = ANEG_STATE_DISABLE_LINK_OK;
4262 }
4263 break;
4264
4265 case ANEG_STATE_RESTART_INIT:
4266 ap->link_time = ap->cur_time;
4267 ap->flags &= ~(MR_NP_LOADED);
4268 ap->txconfig = 0;
4269 tw32(MAC_TX_AUTO_NEG, 0);
4270 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4271 tw32_f(MAC_MODE, tp->mac_mode);
4272 udelay(40);
4273
4274 ret = ANEG_TIMER_ENAB;
4275 ap->state = ANEG_STATE_RESTART;
4276
4277 /* fallthru */
4278 case ANEG_STATE_RESTART:
4279 delta = ap->cur_time - ap->link_time;
Matt Carlson859a588792010-04-05 10:19:28 +00004280 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a588792010-04-05 10:19:28 +00004282 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 break;
4285
4286 case ANEG_STATE_DISABLE_LINK_OK:
4287 ret = ANEG_DONE;
4288 break;
4289
4290 case ANEG_STATE_ABILITY_DETECT_INIT:
4291 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08004292 ap->txconfig = ANEG_CFG_FD;
4293 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
4294 if (flowctrl & ADVERTISE_1000XPAUSE)
4295 ap->txconfig |= ANEG_CFG_PS1;
4296 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
4297 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
4299 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4300 tw32_f(MAC_MODE, tp->mac_mode);
4301 udelay(40);
4302
4303 ap->state = ANEG_STATE_ABILITY_DETECT;
4304 break;
4305
4306 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a588792010-04-05 10:19:28 +00004307 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 break;
4310
4311 case ANEG_STATE_ACK_DETECT_INIT:
4312 ap->txconfig |= ANEG_CFG_ACK;
4313 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
4314 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4315 tw32_f(MAC_MODE, tp->mac_mode);
4316 udelay(40);
4317
4318 ap->state = ANEG_STATE_ACK_DETECT;
4319
4320 /* fallthru */
4321 case ANEG_STATE_ACK_DETECT:
4322 if (ap->ack_match != 0) {
4323 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
4324 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
4325 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
4326 } else {
4327 ap->state = ANEG_STATE_AN_ENABLE;
4328 }
4329 } else if (ap->ability_match != 0 &&
4330 ap->rxconfig == 0) {
4331 ap->state = ANEG_STATE_AN_ENABLE;
4332 }
4333 break;
4334
4335 case ANEG_STATE_COMPLETE_ACK_INIT:
4336 if (ap->rxconfig & ANEG_CFG_INVAL) {
4337 ret = ANEG_FAILED;
4338 break;
4339 }
4340 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
4341 MR_LP_ADV_HALF_DUPLEX |
4342 MR_LP_ADV_SYM_PAUSE |
4343 MR_LP_ADV_ASYM_PAUSE |
4344 MR_LP_ADV_REMOTE_FAULT1 |
4345 MR_LP_ADV_REMOTE_FAULT2 |
4346 MR_LP_ADV_NEXT_PAGE |
4347 MR_TOGGLE_RX |
4348 MR_NP_RX);
4349 if (ap->rxconfig & ANEG_CFG_FD)
4350 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
4351 if (ap->rxconfig & ANEG_CFG_HD)
4352 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
4353 if (ap->rxconfig & ANEG_CFG_PS1)
4354 ap->flags |= MR_LP_ADV_SYM_PAUSE;
4355 if (ap->rxconfig & ANEG_CFG_PS2)
4356 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
4357 if (ap->rxconfig & ANEG_CFG_RF1)
4358 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
4359 if (ap->rxconfig & ANEG_CFG_RF2)
4360 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
4361 if (ap->rxconfig & ANEG_CFG_NP)
4362 ap->flags |= MR_LP_ADV_NEXT_PAGE;
4363
4364 ap->link_time = ap->cur_time;
4365
4366 ap->flags ^= (MR_TOGGLE_TX);
4367 if (ap->rxconfig & 0x0008)
4368 ap->flags |= MR_TOGGLE_RX;
4369 if (ap->rxconfig & ANEG_CFG_NP)
4370 ap->flags |= MR_NP_RX;
4371 ap->flags |= MR_PAGE_RX;
4372
4373 ap->state = ANEG_STATE_COMPLETE_ACK;
4374 ret = ANEG_TIMER_ENAB;
4375 break;
4376
4377 case ANEG_STATE_COMPLETE_ACK:
4378 if (ap->ability_match != 0 &&
4379 ap->rxconfig == 0) {
4380 ap->state = ANEG_STATE_AN_ENABLE;
4381 break;
4382 }
4383 delta = ap->cur_time - ap->link_time;
4384 if (delta > ANEG_STATE_SETTLE_TIME) {
4385 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
4386 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
4387 } else {
4388 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
4389 !(ap->flags & MR_NP_RX)) {
4390 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
4391 } else {
4392 ret = ANEG_FAILED;
4393 }
4394 }
4395 }
4396 break;
4397
4398 case ANEG_STATE_IDLE_DETECT_INIT:
4399 ap->link_time = ap->cur_time;
4400 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
4401 tw32_f(MAC_MODE, tp->mac_mode);
4402 udelay(40);
4403
4404 ap->state = ANEG_STATE_IDLE_DETECT;
4405 ret = ANEG_TIMER_ENAB;
4406 break;
4407
4408 case ANEG_STATE_IDLE_DETECT:
4409 if (ap->ability_match != 0 &&
4410 ap->rxconfig == 0) {
4411 ap->state = ANEG_STATE_AN_ENABLE;
4412 break;
4413 }
4414 delta = ap->cur_time - ap->link_time;
4415 if (delta > ANEG_STATE_SETTLE_TIME) {
4416 /* XXX another gem from the Broadcom driver :( */
4417 ap->state = ANEG_STATE_LINK_OK;
4418 }
4419 break;
4420
4421 case ANEG_STATE_LINK_OK:
4422 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
4423 ret = ANEG_DONE;
4424 break;
4425
4426 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
4427 /* ??? unimplemented */
4428 break;
4429
4430 case ANEG_STATE_NEXT_PAGE_WAIT:
4431 /* ??? unimplemented */
4432 break;
4433
4434 default:
4435 ret = ANEG_FAILED;
4436 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438
4439 return ret;
4440}
4441
Matt Carlson5be73b42007-12-20 20:09:29 -08004442static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443{
4444 int res = 0;
4445 struct tg3_fiber_aneginfo aninfo;
4446 int status = ANEG_FAILED;
4447 unsigned int tick;
4448 u32 tmp;
4449
4450 tw32_f(MAC_TX_AUTO_NEG, 0);
4451
4452 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
4453 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
4454 udelay(40);
4455
4456 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
4457 udelay(40);
4458
4459 memset(&aninfo, 0, sizeof(aninfo));
4460 aninfo.flags |= MR_AN_ENABLE;
4461 aninfo.state = ANEG_STATE_UNKNOWN;
4462 aninfo.cur_time = 0;
4463 tick = 0;
4464 while (++tick < 195000) {
4465 status = tg3_fiber_aneg_smachine(tp, &aninfo);
4466 if (status == ANEG_DONE || status == ANEG_FAILED)
4467 break;
4468
4469 udelay(1);
4470 }
4471
4472 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
4473 tw32_f(MAC_MODE, tp->mac_mode);
4474 udelay(40);
4475
Matt Carlson5be73b42007-12-20 20:09:29 -08004476 *txflags = aninfo.txconfig;
4477 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478
4479 if (status == ANEG_DONE &&
4480 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
4481 MR_LP_ADV_FULL_DUPLEX)))
4482 res = 1;
4483
4484 return res;
4485}
4486
4487static void tg3_init_bcm8002(struct tg3 *tp)
4488{
4489 u32 mac_status = tr32(MAC_STATUS);
4490 int i;
4491
4492 /* Reset when initting first time or we have a link. */
Joe Perches63c3a662011-04-26 08:12:10 +00004493 if (tg3_flag(tp, INIT_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494 !(mac_status & MAC_STATUS_PCS_SYNCED))
4495 return;
4496
4497 /* Set PLL lock range. */
4498 tg3_writephy(tp, 0x16, 0x8007);
4499
4500 /* SW reset */
4501 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
4502
4503 /* Wait for reset to complete. */
4504 /* XXX schedule_timeout() ... */
4505 for (i = 0; i < 500; i++)
4506 udelay(10);
4507
4508 /* Config mode; select PMA/Ch 1 regs. */
4509 tg3_writephy(tp, 0x10, 0x8411);
4510
4511 /* Enable auto-lock and comdet, select txclk for tx. */
4512 tg3_writephy(tp, 0x11, 0x0a10);
4513
4514 tg3_writephy(tp, 0x18, 0x00a0);
4515 tg3_writephy(tp, 0x16, 0x41ff);
4516
4517 /* Assert and deassert POR. */
4518 tg3_writephy(tp, 0x13, 0x0400);
4519 udelay(40);
4520 tg3_writephy(tp, 0x13, 0x0000);
4521
4522 tg3_writephy(tp, 0x11, 0x0a50);
4523 udelay(40);
4524 tg3_writephy(tp, 0x11, 0x0a10);
4525
4526 /* Wait for signal to stabilize */
4527 /* XXX schedule_timeout() ... */
4528 for (i = 0; i < 15000; i++)
4529 udelay(10);
4530
4531 /* Deselect the channel register so we can read the PHYID
4532 * later.
4533 */
4534 tg3_writephy(tp, 0x10, 0x8011);
4535}
4536
4537static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
4538{
Matt Carlson82cd3d12007-12-20 20:09:00 -08004539 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540 u32 sg_dig_ctrl, sg_dig_status;
4541 u32 serdes_cfg, expected_sg_dig_ctrl;
4542 int workaround, port_a;
4543 int current_link_up;
4544
4545 serdes_cfg = 0;
4546 expected_sg_dig_ctrl = 0;
4547 workaround = 0;
4548 port_a = 1;
4549 current_link_up = 0;
4550
4551 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
4552 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
4553 workaround = 1;
4554 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
4555 port_a = 0;
4556
4557 /* preserve bits 0-11,13,14 for signal pre-emphasis */
4558 /* preserve bits 20-23 for voltage regulator */
4559 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
4560 }
4561
4562 sg_dig_ctrl = tr32(SG_DIG_CTRL);
4563
4564 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004565 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 if (workaround) {
4567 u32 val = serdes_cfg;
4568
4569 if (port_a)
4570 val |= 0xc010000;
4571 else
4572 val |= 0x4010000;
4573 tw32_f(MAC_SERDES_CFG, val);
4574 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004575
4576 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 }
4578 if (mac_status & MAC_STATUS_PCS_SYNCED) {
4579 tg3_setup_flow_control(tp, 0, 0);
4580 current_link_up = 1;
4581 }
4582 goto out;
4583 }
4584
4585 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004586 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587
Matt Carlson82cd3d12007-12-20 20:09:00 -08004588 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
4589 if (flowctrl & ADVERTISE_1000XPAUSE)
4590 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
4591 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
4592 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593
4594 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004595 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07004596 tp->serdes_counter &&
4597 ((mac_status & (MAC_STATUS_PCS_SYNCED |
4598 MAC_STATUS_RCVD_CFG)) ==
4599 MAC_STATUS_PCS_SYNCED)) {
4600 tp->serdes_counter--;
4601 current_link_up = 1;
4602 goto out;
4603 }
4604restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605 if (workaround)
4606 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004607 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 udelay(5);
4609 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
4610
Michael Chan3d3ebe72006-09-27 15:59:15 -07004611 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004612 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
4614 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07004615 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616 mac_status = tr32(MAC_STATUS);
4617
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004618 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08004620 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621
Matt Carlson82cd3d12007-12-20 20:09:00 -08004622 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
4623 local_adv |= ADVERTISE_1000XPAUSE;
4624 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
4625 local_adv |= ADVERTISE_1000XPSE_ASYM;
4626
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004627 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08004628 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004629 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08004630 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631
4632 tg3_setup_flow_control(tp, local_adv, remote_adv);
4633 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004634 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004635 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004636 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07004637 if (tp->serdes_counter)
4638 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 else {
4640 if (workaround) {
4641 u32 val = serdes_cfg;
4642
4643 if (port_a)
4644 val |= 0xc010000;
4645 else
4646 val |= 0x4010000;
4647
4648 tw32_f(MAC_SERDES_CFG, val);
4649 }
4650
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004651 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652 udelay(40);
4653
4654 /* Link parallel detection - link is up */
4655 /* only if we have PCS_SYNC and not */
4656 /* receiving config code words */
4657 mac_status = tr32(MAC_STATUS);
4658 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
4659 !(mac_status & MAC_STATUS_RCVD_CFG)) {
4660 tg3_setup_flow_control(tp, 0, 0);
4661 current_link_up = 1;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004662 tp->phy_flags |=
4663 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004664 tp->serdes_counter =
4665 SERDES_PARALLEL_DET_TIMEOUT;
4666 } else
4667 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668 }
4669 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07004670 } else {
4671 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004672 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 }
4674
4675out:
4676 return current_link_up;
4677}
4678
4679static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
4680{
4681 int current_link_up = 0;
4682
Michael Chan5cf64b8a2007-05-05 12:11:21 -07004683 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685
4686 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08004687 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004689
Matt Carlson5be73b42007-12-20 20:09:29 -08004690 if (fiber_autoneg(tp, &txflags, &rxflags)) {
4691 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692
Matt Carlson5be73b42007-12-20 20:09:29 -08004693 if (txflags & ANEG_CFG_PS1)
4694 local_adv |= ADVERTISE_1000XPAUSE;
4695 if (txflags & ANEG_CFG_PS2)
4696 local_adv |= ADVERTISE_1000XPSE_ASYM;
4697
4698 if (rxflags & MR_LP_ADV_SYM_PAUSE)
4699 remote_adv |= LPA_1000XPAUSE;
4700 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
4701 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702
4703 tg3_setup_flow_control(tp, local_adv, remote_adv);
4704
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705 current_link_up = 1;
4706 }
4707 for (i = 0; i < 30; i++) {
4708 udelay(20);
4709 tw32_f(MAC_STATUS,
4710 (MAC_STATUS_SYNC_CHANGED |
4711 MAC_STATUS_CFG_CHANGED));
4712 udelay(40);
4713 if ((tr32(MAC_STATUS) &
4714 (MAC_STATUS_SYNC_CHANGED |
4715 MAC_STATUS_CFG_CHANGED)) == 0)
4716 break;
4717 }
4718
4719 mac_status = tr32(MAC_STATUS);
4720 if (current_link_up == 0 &&
4721 (mac_status & MAC_STATUS_PCS_SYNCED) &&
4722 !(mac_status & MAC_STATUS_RCVD_CFG))
4723 current_link_up = 1;
4724 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08004725 tg3_setup_flow_control(tp, 0, 0);
4726
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727 /* Forcing 1000FD link up. */
4728 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729
4730 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
4731 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004732
4733 tw32_f(MAC_MODE, tp->mac_mode);
4734 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735 }
4736
4737out:
4738 return current_link_up;
4739}
4740
4741static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
4742{
4743 u32 orig_pause_cfg;
4744 u16 orig_active_speed;
4745 u8 orig_active_duplex;
4746 u32 mac_status;
4747 int current_link_up;
4748 int i;
4749
Matt Carlson8d018622007-12-20 20:05:44 -08004750 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751 orig_active_speed = tp->link_config.active_speed;
4752 orig_active_duplex = tp->link_config.active_duplex;
4753
Joe Perches63c3a662011-04-26 08:12:10 +00004754 if (!tg3_flag(tp, HW_AUTONEG) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 netif_carrier_ok(tp->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00004756 tg3_flag(tp, INIT_COMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757 mac_status = tr32(MAC_STATUS);
4758 mac_status &= (MAC_STATUS_PCS_SYNCED |
4759 MAC_STATUS_SIGNAL_DET |
4760 MAC_STATUS_CFG_CHANGED |
4761 MAC_STATUS_RCVD_CFG);
4762 if (mac_status == (MAC_STATUS_PCS_SYNCED |
4763 MAC_STATUS_SIGNAL_DET)) {
4764 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4765 MAC_STATUS_CFG_CHANGED));
4766 return 0;
4767 }
4768 }
4769
4770 tw32_f(MAC_TX_AUTO_NEG, 0);
4771
4772 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
4773 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
4774 tw32_f(MAC_MODE, tp->mac_mode);
4775 udelay(40);
4776
Matt Carlson79eb6902010-02-17 15:17:03 +00004777 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778 tg3_init_bcm8002(tp);
4779
4780 /* Enable link change event even when serdes polling. */
4781 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4782 udelay(40);
4783
4784 current_link_up = 0;
4785 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;
4876
4877 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4878 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004879 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
4880 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4881 bmsr |= BMSR_LSTATUS;
4882 else
4883 bmsr &= ~BMSR_LSTATUS;
4884 }
Michael Chan747e8f82005-07-25 12:33:22 -07004885
4886 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
4887
4888 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004889 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004890 /* do nothing, just check for link up at the end */
4891 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson28011cf2011-11-16 18:36:59 -05004892 u32 adv, newadv;
Michael Chan747e8f82005-07-25 12:33:22 -07004893
4894 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
Matt Carlson28011cf2011-11-16 18:36:59 -05004895 newadv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
4896 ADVERTISE_1000XPAUSE |
4897 ADVERTISE_1000XPSE_ASYM |
4898 ADVERTISE_SLCT);
Michael Chan747e8f82005-07-25 12:33:22 -07004899
Matt Carlson28011cf2011-11-16 18:36:59 -05004900 newadv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Matt Carlson37f07022011-11-17 14:30:55 +00004901 newadv |= ethtool_adv_to_mii_adv_x(tp->link_config.advertising);
Michael Chan747e8f82005-07-25 12:33:22 -07004902
Matt Carlson28011cf2011-11-16 18:36:59 -05004903 if ((newadv != adv) || !(bmcr & BMCR_ANENABLE)) {
4904 tg3_writephy(tp, MII_ADVERTISE, newadv);
Michael Chan747e8f82005-07-25 12:33:22 -07004905 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
4906 tg3_writephy(tp, MII_BMCR, bmcr);
4907
4908 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07004909 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004910 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004911
4912 return err;
4913 }
4914 } else {
4915 u32 new_bmcr;
4916
4917 bmcr &= ~BMCR_SPEED1000;
4918 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
4919
4920 if (tp->link_config.duplex == DUPLEX_FULL)
4921 new_bmcr |= BMCR_FULLDPLX;
4922
4923 if (new_bmcr != bmcr) {
4924 /* BMCR_SPEED1000 is a reserved bit that needs
4925 * to be set on write.
4926 */
4927 new_bmcr |= BMCR_SPEED1000;
4928
4929 /* Force a linkdown */
4930 if (netif_carrier_ok(tp->dev)) {
4931 u32 adv;
4932
4933 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4934 adv &= ~(ADVERTISE_1000XFULL |
4935 ADVERTISE_1000XHALF |
4936 ADVERTISE_SLCT);
4937 tg3_writephy(tp, MII_ADVERTISE, adv);
4938 tg3_writephy(tp, MII_BMCR, bmcr |
4939 BMCR_ANRESTART |
4940 BMCR_ANENABLE);
4941 udelay(10);
4942 netif_carrier_off(tp->dev);
4943 }
4944 tg3_writephy(tp, MII_BMCR, new_bmcr);
4945 bmcr = new_bmcr;
4946 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4947 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004948 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
4949 ASIC_REV_5714) {
4950 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4951 bmsr |= BMSR_LSTATUS;
4952 else
4953 bmsr &= ~BMSR_LSTATUS;
4954 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004955 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004956 }
4957 }
4958
4959 if (bmsr & BMSR_LSTATUS) {
4960 current_speed = SPEED_1000;
4961 current_link_up = 1;
4962 if (bmcr & BMCR_FULLDPLX)
4963 current_duplex = DUPLEX_FULL;
4964 else
4965 current_duplex = DUPLEX_HALF;
4966
Matt Carlsonef167e22007-12-20 20:10:01 -08004967 local_adv = 0;
4968 remote_adv = 0;
4969
Michael Chan747e8f82005-07-25 12:33:22 -07004970 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08004971 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07004972
4973 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
4974 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
4975 common = local_adv & remote_adv;
4976 if (common & (ADVERTISE_1000XHALF |
4977 ADVERTISE_1000XFULL)) {
4978 if (common & ADVERTISE_1000XFULL)
4979 current_duplex = DUPLEX_FULL;
4980 else
4981 current_duplex = DUPLEX_HALF;
Joe Perches63c3a662011-04-26 08:12:10 +00004982 } else if (!tg3_flag(tp, 5780_CLASS)) {
Matt Carlson57d8b882010-06-05 17:24:35 +00004983 /* Link is up via parallel detect */
Matt Carlson859a588792010-04-05 10:19:28 +00004984 } else {
Michael Chan747e8f82005-07-25 12:33:22 -07004985 current_link_up = 0;
Matt Carlson859a588792010-04-05 10:19:28 +00004986 }
Michael Chan747e8f82005-07-25 12:33:22 -07004987 }
4988 }
4989
Matt Carlsonef167e22007-12-20 20:10:01 -08004990 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
4991 tg3_setup_flow_control(tp, local_adv, remote_adv);
4992
Michael Chan747e8f82005-07-25 12:33:22 -07004993 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4994 if (tp->link_config.active_duplex == DUPLEX_HALF)
4995 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4996
4997 tw32_f(MAC_MODE, tp->mac_mode);
4998 udelay(40);
4999
5000 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5001
5002 tp->link_config.active_speed = current_speed;
5003 tp->link_config.active_duplex = current_duplex;
5004
5005 if (current_link_up != netif_carrier_ok(tp->dev)) {
5006 if (current_link_up)
5007 netif_carrier_on(tp->dev);
5008 else {
5009 netif_carrier_off(tp->dev);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005010 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005011 }
5012 tg3_link_report(tp);
5013 }
5014 return err;
5015}
5016
5017static void tg3_serdes_parallel_detect(struct tg3 *tp)
5018{
Michael Chan3d3ebe72006-09-27 15:59:15 -07005019 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07005020 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07005021 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07005022 return;
5023 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005024
Michael Chan747e8f82005-07-25 12:33:22 -07005025 if (!netif_carrier_ok(tp->dev) &&
5026 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
5027 u32 bmcr;
5028
5029 tg3_readphy(tp, MII_BMCR, &bmcr);
5030 if (bmcr & BMCR_ANENABLE) {
5031 u32 phy1, phy2;
5032
5033 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005034 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
5035 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07005036
5037 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005038 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5039 MII_TG3_DSP_EXP1_INT_STAT);
5040 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
5041 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005042
5043 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
5044 /* We have signal detect and not receiving
5045 * config code words, link is up by parallel
5046 * detection.
5047 */
5048
5049 bmcr &= ~BMCR_ANENABLE;
5050 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
5051 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005052 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005053 }
5054 }
Matt Carlson859a588792010-04-05 10:19:28 +00005055 } else if (netif_carrier_ok(tp->dev) &&
5056 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005057 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005058 u32 phy2;
5059
5060 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005061 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5062 MII_TG3_DSP_EXP1_INT_STAT);
5063 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005064 if (phy2 & 0x20) {
5065 u32 bmcr;
5066
5067 /* Config code words received, turn on autoneg. */
5068 tg3_readphy(tp, MII_BMCR, &bmcr);
5069 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
5070
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005071 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005072
5073 }
5074 }
5075}
5076
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077static int tg3_setup_phy(struct tg3 *tp, int force_reset)
5078{
Matt Carlsonf2096f92011-04-05 14:22:48 +00005079 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080 int err;
5081
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005082 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005084 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07005085 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a588792010-04-05 10:19:28 +00005086 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088
Matt Carlsonbcb37f62008-11-03 16:52:09 -08005089 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00005090 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08005091
5092 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
5093 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
5094 scale = 65;
5095 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
5096 scale = 6;
5097 else
5098 scale = 12;
5099
5100 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
5101 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
5102 tw32(GRC_MISC_CFG, val);
5103 }
5104
Matt Carlsonf2096f92011-04-05 14:22:48 +00005105 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
5106 (6 << TX_LENGTHS_IPG_SHIFT);
5107 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
5108 val |= tr32(MAC_TX_LENGTHS) &
5109 (TX_LENGTHS_JMB_FRM_LEN_MSK |
5110 TX_LENGTHS_CNT_DWN_VAL_MSK);
5111
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112 if (tp->link_config.active_speed == SPEED_1000 &&
5113 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00005114 tw32(MAC_TX_LENGTHS, val |
5115 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00005117 tw32(MAC_TX_LENGTHS, val |
5118 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119
Joe Perches63c3a662011-04-26 08:12:10 +00005120 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121 if (netif_carrier_ok(tp->dev)) {
5122 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07005123 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124 } else {
5125 tw32(HOSTCC_STAT_COAL_TICKS, 0);
5126 }
5127 }
5128
Joe Perches63c3a662011-04-26 08:12:10 +00005129 if (tg3_flag(tp, ASPM_WORKAROUND)) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00005130 val = tr32(PCIE_PWR_MGMT_THRESH);
Matt Carlson8ed5d972007-05-07 00:25:49 -07005131 if (!netif_carrier_ok(tp->dev))
5132 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
5133 tp->pwrmgmt_thresh;
5134 else
5135 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
5136 tw32(PCIE_PWR_MGMT_THRESH, val);
5137 }
5138
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139 return err;
5140}
5141
Matt Carlson66cfd1b2010-09-30 10:34:30 +00005142static inline int tg3_irq_sync(struct tg3 *tp)
5143{
5144 return tp->irq_sync;
5145}
5146
Matt Carlson97bd8e42011-04-13 11:05:04 +00005147static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
5148{
5149 int i;
5150
5151 dst = (u32 *)((u8 *)dst + off);
5152 for (i = 0; i < len; i += sizeof(u32))
5153 *dst++ = tr32(off + i);
5154}
5155
5156static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
5157{
5158 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
5159 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
5160 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
5161 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
5162 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
5163 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
5164 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
5165 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
5166 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
5167 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
5168 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
5169 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
5170 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
5171 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
5172 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
5173 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
5174 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
5175 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
5176 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
5177
Joe Perches63c3a662011-04-26 08:12:10 +00005178 if (tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson97bd8e42011-04-13 11:05:04 +00005179 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
5180
5181 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
5182 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
5183 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
5184 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
5185 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
5186 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
5187 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
5188 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
5189
Joe Perches63c3a662011-04-26 08:12:10 +00005190 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00005191 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
5192 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
5193 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
5194 }
5195
5196 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
5197 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
5198 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
5199 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
5200 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
5201
Joe Perches63c3a662011-04-26 08:12:10 +00005202 if (tg3_flag(tp, NVRAM))
Matt Carlson97bd8e42011-04-13 11:05:04 +00005203 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
5204}
5205
5206static void tg3_dump_state(struct tg3 *tp)
5207{
5208 int i;
5209 u32 *regs;
5210
5211 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
5212 if (!regs) {
5213 netdev_err(tp->dev, "Failed allocating register dump buffer\n");
5214 return;
5215 }
5216
Joe Perches63c3a662011-04-26 08:12:10 +00005217 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00005218 /* Read up to but not including private PCI registers */
5219 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
5220 regs[i / sizeof(u32)] = tr32(i);
5221 } else
5222 tg3_dump_legacy_regs(tp, regs);
5223
5224 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
5225 if (!regs[i + 0] && !regs[i + 1] &&
5226 !regs[i + 2] && !regs[i + 3])
5227 continue;
5228
5229 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
5230 i * 4,
5231 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
5232 }
5233
5234 kfree(regs);
5235
5236 for (i = 0; i < tp->irq_cnt; i++) {
5237 struct tg3_napi *tnapi = &tp->napi[i];
5238
5239 /* SW status block */
5240 netdev_err(tp->dev,
5241 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
5242 i,
5243 tnapi->hw_status->status,
5244 tnapi->hw_status->status_tag,
5245 tnapi->hw_status->rx_jumbo_consumer,
5246 tnapi->hw_status->rx_consumer,
5247 tnapi->hw_status->rx_mini_consumer,
5248 tnapi->hw_status->idx[0].rx_producer,
5249 tnapi->hw_status->idx[0].tx_consumer);
5250
5251 netdev_err(tp->dev,
5252 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
5253 i,
5254 tnapi->last_tag, tnapi->last_irq_tag,
5255 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
5256 tnapi->rx_rcb_ptr,
5257 tnapi->prodring.rx_std_prod_idx,
5258 tnapi->prodring.rx_std_cons_idx,
5259 tnapi->prodring.rx_jmb_prod_idx,
5260 tnapi->prodring.rx_jmb_cons_idx);
5261 }
5262}
5263
Michael Chandf3e6542006-05-26 17:48:07 -07005264/* This is called whenever we suspect that the system chipset is re-
5265 * ordering the sequence of MMIO to the tx send mailbox. The symptom
5266 * is bogus tx completions. We try to recover by setting the
5267 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
5268 * in the workqueue.
5269 */
5270static void tg3_tx_recover(struct tg3 *tp)
5271{
Joe Perches63c3a662011-04-26 08:12:10 +00005272 BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
Michael Chandf3e6542006-05-26 17:48:07 -07005273 tp->write32_tx_mbox == tg3_write_indirect_mbox);
5274
Matt Carlson5129c3a2010-04-05 10:19:23 +00005275 netdev_warn(tp->dev,
5276 "The system may be re-ordering memory-mapped I/O "
5277 "cycles to the network device, attempting to recover. "
5278 "Please report the problem to the driver maintainer "
5279 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07005280
5281 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00005282 tg3_flag_set(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07005283 spin_unlock(&tp->lock);
5284}
5285
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005286static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07005287{
Matt Carlsonf65aac12010-08-02 11:26:03 +00005288 /* Tell compiler to fetch tx indices from memory. */
5289 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005290 return tnapi->tx_pending -
5291 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07005292}
5293
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294/* Tigon3 never reports partial packet sends. So we do not
5295 * need special logic to handle SKBs that have not had all
5296 * of their frags sent yet, like SunGEM does.
5297 */
Matt Carlson17375d22009-08-28 14:02:18 +00005298static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299{
Matt Carlson17375d22009-08-28 14:02:18 +00005300 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005301 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005302 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005303 struct netdev_queue *txq;
5304 int index = tnapi - tp->napi;
Tom Herbert298376d2011-11-28 16:33:30 +00005305 unsigned int pkts_compl = 0, bytes_compl = 0;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005306
Joe Perches63c3a662011-04-26 08:12:10 +00005307 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005308 index--;
5309
5310 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311
5312 while (sw_idx != hw_idx) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00005313 struct tg3_tx_ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07005315 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316
Michael Chandf3e6542006-05-26 17:48:07 -07005317 if (unlikely(skb == NULL)) {
5318 tg3_tx_recover(tp);
5319 return;
5320 }
5321
Alexander Duyckf4188d82009-12-02 16:48:38 +00005322 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005323 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005324 skb_headlen(skb),
5325 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326
5327 ri->skb = NULL;
5328
Matt Carlsone01ee142011-07-27 14:20:50 +00005329 while (ri->fragmented) {
5330 ri->fragmented = false;
5331 sw_idx = NEXT_TX(sw_idx);
5332 ri = &tnapi->tx_buffers[sw_idx];
5333 }
5334
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335 sw_idx = NEXT_TX(sw_idx);
5336
5337 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005338 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07005339 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
5340 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005341
5342 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005343 dma_unmap_addr(ri, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00005344 skb_frag_size(&skb_shinfo(skb)->frags[i]),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005345 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00005346
5347 while (ri->fragmented) {
5348 ri->fragmented = false;
5349 sw_idx = NEXT_TX(sw_idx);
5350 ri = &tnapi->tx_buffers[sw_idx];
5351 }
5352
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353 sw_idx = NEXT_TX(sw_idx);
5354 }
5355
Tom Herbert298376d2011-11-28 16:33:30 +00005356 pkts_compl++;
5357 bytes_compl += skb->len;
5358
David S. Millerf47c11e2005-06-24 20:18:35 -07005359 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07005360
5361 if (unlikely(tx_bug)) {
5362 tg3_tx_recover(tp);
5363 return;
5364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365 }
5366
Tom Herbert298376d2011-11-28 16:33:30 +00005367 netdev_completed_queue(tp->dev, pkts_compl, bytes_compl);
5368
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005369 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370
Michael Chan1b2a7202006-08-07 21:46:02 -07005371 /* Need to make the tx_cons update visible to tg3_start_xmit()
5372 * before checking for netif_queue_stopped(). Without the
5373 * memory barrier, there is a small possibility that tg3_start_xmit()
5374 * will miss it and cause the queue to be stopped forever.
5375 */
5376 smp_mb();
5377
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005378 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005379 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005380 __netif_tx_lock(txq, smp_processor_id());
5381 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005382 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005383 netif_tx_wake_queue(txq);
5384 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07005385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386}
5387
Eric Dumazet9205fd92011-11-18 06:47:01 +00005388static void tg3_rx_data_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005389{
Eric Dumazet9205fd92011-11-18 06:47:01 +00005390 if (!ri->data)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005391 return;
5392
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005393 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005394 map_sz, PCI_DMA_FROMDEVICE);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005395 kfree(ri->data);
5396 ri->data = NULL;
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005397}
5398
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399/* Returns size of skb allocated or < 0 on error.
5400 *
5401 * We only need to fill in the address because the other members
5402 * of the RX descriptor are invariant, see tg3_init_rings.
5403 *
5404 * Note the purposeful assymetry of cpu vs. chip accesses. For
5405 * posting buffers we only dirty the first cache line of the RX
5406 * descriptor (containing the address). Whereas for the RX status
5407 * buffers the cpu only reads the last cacheline of the RX descriptor
5408 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
5409 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00005410static int tg3_alloc_rx_data(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Matt Carlsona3896162009-11-13 13:03:44 +00005411 u32 opaque_key, u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412{
5413 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00005414 struct ring_info *map;
Eric Dumazet9205fd92011-11-18 06:47:01 +00005415 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416 dma_addr_t mapping;
Eric Dumazet9205fd92011-11-18 06:47:01 +00005417 int skb_size, data_size, dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419 switch (opaque_key) {
5420 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00005421 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00005422 desc = &tpr->rx_std[dest_idx];
5423 map = &tpr->rx_std_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00005424 data_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425 break;
5426
5427 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00005428 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00005429 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00005430 map = &tpr->rx_jmb_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00005431 data_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432 break;
5433
5434 default:
5435 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437
5438 /* Do not overwrite any of the map or rp information
5439 * until we are sure we can commit to a new buffer.
5440 *
5441 * Callers depend upon this behavior and assume that
5442 * we leave everything unchanged if we fail.
5443 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00005444 skb_size = SKB_DATA_ALIGN(data_size + TG3_RX_OFFSET(tp)) +
5445 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
5446 data = kmalloc(skb_size, GFP_ATOMIC);
5447 if (!data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448 return -ENOMEM;
5449
Eric Dumazet9205fd92011-11-18 06:47:01 +00005450 mapping = pci_map_single(tp->pdev,
5451 data + TG3_RX_OFFSET(tp),
5452 data_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453 PCI_DMA_FROMDEVICE);
Matt Carlsona21771d2009-11-02 14:25:31 +00005454 if (pci_dma_mapping_error(tp->pdev, mapping)) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00005455 kfree(data);
Matt Carlsona21771d2009-11-02 14:25:31 +00005456 return -EIO;
5457 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458
Eric Dumazet9205fd92011-11-18 06:47:01 +00005459 map->data = data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005460 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462 desc->addr_hi = ((u64)mapping >> 32);
5463 desc->addr_lo = ((u64)mapping & 0xffffffff);
5464
Eric Dumazet9205fd92011-11-18 06:47:01 +00005465 return data_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466}
5467
5468/* We only need to move over in the address because the other
5469 * members of the RX descriptor are invariant. See notes above
Eric Dumazet9205fd92011-11-18 06:47:01 +00005470 * tg3_alloc_rx_data for full details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005471 */
Matt Carlsona3896162009-11-13 13:03:44 +00005472static void tg3_recycle_rx(struct tg3_napi *tnapi,
5473 struct tg3_rx_prodring_set *dpr,
5474 u32 opaque_key, int src_idx,
5475 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476{
Matt Carlson17375d22009-08-28 14:02:18 +00005477 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
5479 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00005480 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005481 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482
5483 switch (opaque_key) {
5484 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00005485 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00005486 dest_desc = &dpr->rx_std[dest_idx];
5487 dest_map = &dpr->rx_std_buffers[dest_idx];
5488 src_desc = &spr->rx_std[src_idx];
5489 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490 break;
5491
5492 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00005493 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00005494 dest_desc = &dpr->rx_jmb[dest_idx].std;
5495 dest_map = &dpr->rx_jmb_buffers[dest_idx];
5496 src_desc = &spr->rx_jmb[src_idx].std;
5497 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498 break;
5499
5500 default:
5501 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005502 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503
Eric Dumazet9205fd92011-11-18 06:47:01 +00005504 dest_map->data = src_map->data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005505 dma_unmap_addr_set(dest_map, mapping,
5506 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507 dest_desc->addr_hi = src_desc->addr_hi;
5508 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00005509
5510 /* Ensure that the update to the skb happens after the physical
5511 * addresses have been transferred to the new BD location.
5512 */
5513 smp_wmb();
5514
Eric Dumazet9205fd92011-11-18 06:47:01 +00005515 src_map->data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516}
5517
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518/* The RX ring scheme is composed of multiple rings which post fresh
5519 * buffers to the chip, and one special ring the chip uses to report
5520 * status back to the host.
5521 *
5522 * The special ring reports the status of received packets to the
5523 * host. The chip does not write into the original descriptor the
5524 * RX buffer was obtained from. The chip simply takes the original
5525 * descriptor as provided by the host, updates the status and length
5526 * field, then writes this into the next status ring entry.
5527 *
5528 * Each ring the host uses to post buffers to the chip is described
5529 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
5530 * it is first placed into the on-chip ram. When the packet's length
5531 * is known, it walks down the TG3_BDINFO entries to select the ring.
5532 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
5533 * which is within the range of the new packet's length is chosen.
5534 *
5535 * The "separate ring for rx status" scheme may sound queer, but it makes
5536 * sense from a cache coherency perspective. If only the host writes
5537 * to the buffer post rings, and only the chip writes to the rx status
5538 * rings, then cache lines never move beyond shared-modified state.
5539 * If both the host and chip were to write into the same ring, cache line
5540 * eviction could occur since both entities want it in an exclusive state.
5541 */
Matt Carlson17375d22009-08-28 14:02:18 +00005542static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543{
Matt Carlson17375d22009-08-28 14:02:18 +00005544 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07005545 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00005546 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00005547 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07005548 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00005550 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005552 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553 /*
5554 * We need to order the read of hw_idx and the read of
5555 * the opaque cookie.
5556 */
5557 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558 work_mask = 0;
5559 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00005560 std_prod_idx = tpr->rx_std_prod_idx;
5561 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00005563 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00005564 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565 unsigned int len;
5566 struct sk_buff *skb;
5567 dma_addr_t dma_addr;
5568 u32 opaque_key, desc_idx, *post_ptr;
Eric Dumazet9205fd92011-11-18 06:47:01 +00005569 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570
5571 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
5572 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
5573 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005574 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005575 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005576 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00005577 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07005578 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005580 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005581 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005582 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00005583 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00005584 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586
5587 work_mask |= opaque_key;
5588
5589 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
5590 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
5591 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00005592 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593 desc_idx, *post_ptr);
5594 drop_it_no_recycle:
5595 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00005596 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597 goto next_pkt;
5598 }
5599
Eric Dumazet9205fd92011-11-18 06:47:01 +00005600 prefetch(data + TG3_RX_OFFSET(tp));
Matt Carlsonad829262008-11-21 17:16:16 -08005601 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
5602 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603
Matt Carlsond2757fc2010-04-12 06:58:27 +00005604 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605 int skb_size;
5606
Eric Dumazet9205fd92011-11-18 06:47:01 +00005607 skb_size = tg3_alloc_rx_data(tp, tpr, opaque_key,
Matt Carlsonafc081f2009-11-13 13:03:43 +00005608 *post_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609 if (skb_size < 0)
5610 goto drop_it;
5611
Matt Carlson287be122009-08-28 13:58:46 +00005612 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613 PCI_DMA_FROMDEVICE);
5614
Eric Dumazet9205fd92011-11-18 06:47:01 +00005615 skb = build_skb(data);
5616 if (!skb) {
5617 kfree(data);
5618 goto drop_it_no_recycle;
5619 }
5620 skb_reserve(skb, TG3_RX_OFFSET(tp));
5621 /* Ensure that the update to the data happens
Matt Carlson61e800c2010-02-17 15:16:54 +00005622 * after the usage of the old DMA mapping.
5623 */
5624 smp_wmb();
5625
Eric Dumazet9205fd92011-11-18 06:47:01 +00005626 ri->data = NULL;
Matt Carlson61e800c2010-02-17 15:16:54 +00005627
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628 } else {
Matt Carlsona3896162009-11-13 13:03:44 +00005629 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630 desc_idx, *post_ptr);
5631
Eric Dumazet9205fd92011-11-18 06:47:01 +00005632 skb = netdev_alloc_skb(tp->dev,
5633 len + TG3_RAW_IP_ALIGN);
5634 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635 goto drop_it_no_recycle;
5636
Eric Dumazet9205fd92011-11-18 06:47:01 +00005637 skb_reserve(skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005639 memcpy(skb->data,
5640 data + TG3_RX_OFFSET(tp),
5641 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643 }
5644
Eric Dumazet9205fd92011-11-18 06:47:01 +00005645 skb_put(skb, len);
Michał Mirosławdc668912011-04-07 03:35:07 +00005646 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
5648 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
5649 >> RXD_TCPCSUM_SHIFT) == 0xffff))
5650 skb->ip_summed = CHECKSUM_UNNECESSARY;
5651 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005652 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653
5654 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00005655
5656 if (len > (tp->dev->mtu + ETH_HLEN) &&
5657 skb->protocol != htons(ETH_P_8021Q)) {
5658 dev_kfree_skb(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00005659 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00005660 }
5661
Matt Carlson9dc7a112010-04-12 06:58:28 +00005662 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00005663 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
5664 __vlan_hwaccel_put_tag(skb,
5665 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00005666
Matt Carlsonbf933c82011-01-25 15:58:49 +00005667 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669 received++;
5670 budget--;
5671
5672next_pkt:
5673 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07005674
5675 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005676 tpr->rx_std_prod_idx = std_prod_idx &
5677 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00005678 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5679 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07005680 work_mask &= ~RXD_OPAQUE_RING_STD;
5681 rx_std_posted = 0;
5682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07005684 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00005685 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07005686
5687 /* Refresh hw_idx to see if there is new work */
5688 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005689 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07005690 rmb();
5691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692 }
5693
5694 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00005695 tnapi->rx_rcb_ptr = sw_idx;
5696 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697
5698 /* Refill RX ring(s). */
Joe Perches63c3a662011-04-26 08:12:10 +00005699 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005700 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005701 tpr->rx_std_prod_idx = std_prod_idx &
5702 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005703 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5704 tpr->rx_std_prod_idx);
5705 }
5706 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005707 tpr->rx_jmb_prod_idx = jmb_prod_idx &
5708 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005709 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5710 tpr->rx_jmb_prod_idx);
5711 }
5712 mmiowb();
5713 } else if (work_mask) {
5714 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
5715 * updated before the producer indices can be updated.
5716 */
5717 smp_wmb();
5718
Matt Carlson2c49a442010-09-30 10:34:35 +00005719 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
5720 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005721
Matt Carlsone4af1af2010-02-12 14:47:05 +00005722 if (tnapi != &tp->napi[1])
5723 napi_schedule(&tp->napi[1].napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725
5726 return received;
5727}
5728
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005729static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731 /* handle link change and other phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00005732 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005733 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
5734
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735 if (sblk->status & SD_STATUS_LINK_CHG) {
5736 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005737 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07005738 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00005739 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsondd477002008-05-25 23:45:58 -07005740 tw32_f(MAC_STATUS,
5741 (MAC_STATUS_SYNC_CHANGED |
5742 MAC_STATUS_CFG_CHANGED |
5743 MAC_STATUS_MI_COMPLETION |
5744 MAC_STATUS_LNKSTATE_CHANGED));
5745 udelay(40);
5746 } else
5747 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07005748 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749 }
5750 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005751}
5752
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005753static int tg3_rx_prodring_xfer(struct tg3 *tp,
5754 struct tg3_rx_prodring_set *dpr,
5755 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005756{
5757 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005758 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005759
5760 while (1) {
5761 src_prod_idx = spr->rx_std_prod_idx;
5762
5763 /* Make sure updates to the rx_std_buffers[] entries and the
5764 * standard producer index are seen in the correct order.
5765 */
5766 smp_rmb();
5767
5768 if (spr->rx_std_cons_idx == src_prod_idx)
5769 break;
5770
5771 if (spr->rx_std_cons_idx < src_prod_idx)
5772 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
5773 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005774 cpycnt = tp->rx_std_ring_mask + 1 -
5775 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005776
Matt Carlson2c49a442010-09-30 10:34:35 +00005777 cpycnt = min(cpycnt,
5778 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005779
5780 si = spr->rx_std_cons_idx;
5781 di = dpr->rx_std_prod_idx;
5782
Matt Carlsone92967b2010-02-12 14:47:06 +00005783 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00005784 if (dpr->rx_std_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00005785 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005786 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005787 break;
5788 }
5789 }
5790
5791 if (!cpycnt)
5792 break;
5793
5794 /* Ensure that updates to the rx_std_buffers ring and the
5795 * shadowed hardware producer ring from tg3_recycle_skb() are
5796 * ordered correctly WRT the skb check above.
5797 */
5798 smp_rmb();
5799
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005800 memcpy(&dpr->rx_std_buffers[di],
5801 &spr->rx_std_buffers[si],
5802 cpycnt * sizeof(struct ring_info));
5803
5804 for (i = 0; i < cpycnt; i++, di++, si++) {
5805 struct tg3_rx_buffer_desc *sbd, *dbd;
5806 sbd = &spr->rx_std[si];
5807 dbd = &dpr->rx_std[di];
5808 dbd->addr_hi = sbd->addr_hi;
5809 dbd->addr_lo = sbd->addr_lo;
5810 }
5811
Matt Carlson2c49a442010-09-30 10:34:35 +00005812 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
5813 tp->rx_std_ring_mask;
5814 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
5815 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005816 }
5817
5818 while (1) {
5819 src_prod_idx = spr->rx_jmb_prod_idx;
5820
5821 /* Make sure updates to the rx_jmb_buffers[] entries and
5822 * the jumbo producer index are seen in the correct order.
5823 */
5824 smp_rmb();
5825
5826 if (spr->rx_jmb_cons_idx == src_prod_idx)
5827 break;
5828
5829 if (spr->rx_jmb_cons_idx < src_prod_idx)
5830 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
5831 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005832 cpycnt = tp->rx_jmb_ring_mask + 1 -
5833 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005834
5835 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00005836 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005837
5838 si = spr->rx_jmb_cons_idx;
5839 di = dpr->rx_jmb_prod_idx;
5840
Matt Carlsone92967b2010-02-12 14:47:06 +00005841 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00005842 if (dpr->rx_jmb_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00005843 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005844 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005845 break;
5846 }
5847 }
5848
5849 if (!cpycnt)
5850 break;
5851
5852 /* Ensure that updates to the rx_jmb_buffers ring and the
5853 * shadowed hardware producer ring from tg3_recycle_skb() are
5854 * ordered correctly WRT the skb check above.
5855 */
5856 smp_rmb();
5857
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005858 memcpy(&dpr->rx_jmb_buffers[di],
5859 &spr->rx_jmb_buffers[si],
5860 cpycnt * sizeof(struct ring_info));
5861
5862 for (i = 0; i < cpycnt; i++, di++, si++) {
5863 struct tg3_rx_buffer_desc *sbd, *dbd;
5864 sbd = &spr->rx_jmb[si].std;
5865 dbd = &dpr->rx_jmb[di].std;
5866 dbd->addr_hi = sbd->addr_hi;
5867 dbd->addr_lo = sbd->addr_lo;
5868 }
5869
Matt Carlson2c49a442010-09-30 10:34:35 +00005870 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
5871 tp->rx_jmb_ring_mask;
5872 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
5873 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005874 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005875
5876 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005877}
5878
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005879static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
5880{
5881 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882
5883 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005884 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00005885 tg3_tx(tnapi);
Joe Perches63c3a662011-04-26 08:12:10 +00005886 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Michael Chan4fd7ab52007-10-12 01:39:50 -07005887 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888 }
5889
Linus Torvalds1da177e2005-04-16 15:20:36 -07005890 /* run RX thread, within the bounds set by NAPI.
5891 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005892 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07005893 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005894 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00005895 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005896
Joe Perches63c3a662011-04-26 08:12:10 +00005897 if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005898 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005899 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00005900 u32 std_prod_idx = dpr->rx_std_prod_idx;
5901 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005902
Matt Carlsone4af1af2010-02-12 14:47:05 +00005903 for (i = 1; i < tp->irq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005904 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00005905 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005906
5907 wmb();
5908
Matt Carlsone4af1af2010-02-12 14:47:05 +00005909 if (std_prod_idx != dpr->rx_std_prod_idx)
5910 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5911 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005912
Matt Carlsone4af1af2010-02-12 14:47:05 +00005913 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
5914 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5915 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005916
5917 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005918
5919 if (err)
5920 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005921 }
5922
David S. Miller6f535762007-10-11 18:08:29 -07005923 return work_done;
5924}
David S. Millerf7383c222005-05-18 22:50:53 -07005925
Matt Carlsondb219972011-11-04 09:15:03 +00005926static inline void tg3_reset_task_schedule(struct tg3 *tp)
5927{
5928 if (!test_and_set_bit(TG3_FLAG_RESET_TASK_PENDING, tp->tg3_flags))
5929 schedule_work(&tp->reset_task);
5930}
5931
5932static inline void tg3_reset_task_cancel(struct tg3 *tp)
5933{
5934 cancel_work_sync(&tp->reset_task);
5935 tg3_flag_clear(tp, RESET_TASK_PENDING);
5936}
5937
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005938static int tg3_poll_msix(struct napi_struct *napi, int budget)
5939{
5940 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5941 struct tg3 *tp = tnapi->tp;
5942 int work_done = 0;
5943 struct tg3_hw_status *sblk = tnapi->hw_status;
5944
5945 while (1) {
5946 work_done = tg3_poll_work(tnapi, work_done, budget);
5947
Joe Perches63c3a662011-04-26 08:12:10 +00005948 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005949 goto tx_recovery;
5950
5951 if (unlikely(work_done >= budget))
5952 break;
5953
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005954 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005955 * to tell the hw how much work has been processed,
5956 * so we must read it before checking for more work.
5957 */
5958 tnapi->last_tag = sblk->status_tag;
5959 tnapi->last_irq_tag = tnapi->last_tag;
5960 rmb();
5961
5962 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00005963 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
5964 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005965 napi_complete(napi);
5966 /* Reenable interrupts. */
5967 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
5968 mmiowb();
5969 break;
5970 }
5971 }
5972
5973 return work_done;
5974
5975tx_recovery:
5976 /* work_done is guaranteed to be less than budget. */
5977 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00005978 tg3_reset_task_schedule(tp);
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005979 return work_done;
5980}
5981
Matt Carlsone64de4e2011-04-13 11:05:05 +00005982static void tg3_process_error(struct tg3 *tp)
5983{
5984 u32 val;
5985 bool real_error = false;
5986
Joe Perches63c3a662011-04-26 08:12:10 +00005987 if (tg3_flag(tp, ERROR_PROCESSED))
Matt Carlsone64de4e2011-04-13 11:05:05 +00005988 return;
5989
5990 /* Check Flow Attention register */
5991 val = tr32(HOSTCC_FLOW_ATTN);
5992 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
5993 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
5994 real_error = true;
5995 }
5996
5997 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
5998 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
5999 real_error = true;
6000 }
6001
6002 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
6003 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
6004 real_error = true;
6005 }
6006
6007 if (!real_error)
6008 return;
6009
6010 tg3_dump_state(tp);
6011
Joe Perches63c3a662011-04-26 08:12:10 +00006012 tg3_flag_set(tp, ERROR_PROCESSED);
Matt Carlsondb219972011-11-04 09:15:03 +00006013 tg3_reset_task_schedule(tp);
Matt Carlsone64de4e2011-04-13 11:05:05 +00006014}
6015
David S. Miller6f535762007-10-11 18:08:29 -07006016static int tg3_poll(struct napi_struct *napi, int budget)
6017{
Matt Carlson8ef04422009-08-28 14:01:37 +00006018 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
6019 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07006020 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00006021 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07006022
6023 while (1) {
Matt Carlsone64de4e2011-04-13 11:05:05 +00006024 if (sblk->status & SD_STATUS_ERROR)
6025 tg3_process_error(tp);
6026
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006027 tg3_poll_link(tp);
6028
Matt Carlson17375d22009-08-28 14:02:18 +00006029 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07006030
Joe Perches63c3a662011-04-26 08:12:10 +00006031 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
David S. Miller6f535762007-10-11 18:08:29 -07006032 goto tx_recovery;
6033
6034 if (unlikely(work_done >= budget))
6035 break;
6036
Joe Perches63c3a662011-04-26 08:12:10 +00006037 if (tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson17375d22009-08-28 14:02:18 +00006038 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07006039 * to tell the hw how much work has been processed,
6040 * so we must read it before checking for more work.
6041 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006042 tnapi->last_tag = sblk->status_tag;
6043 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07006044 rmb();
6045 } else
6046 sblk->status &= ~SD_STATUS_UPDATED;
6047
Matt Carlson17375d22009-08-28 14:02:18 +00006048 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08006049 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00006050 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07006051 break;
6052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053 }
6054
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006055 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07006056
6057tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07006058 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08006059 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00006060 tg3_reset_task_schedule(tp);
Michael Chan4fd7ab52007-10-12 01:39:50 -07006061 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062}
6063
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006064static void tg3_napi_disable(struct tg3 *tp)
6065{
6066 int i;
6067
6068 for (i = tp->irq_cnt - 1; i >= 0; i--)
6069 napi_disable(&tp->napi[i].napi);
6070}
6071
6072static void tg3_napi_enable(struct tg3 *tp)
6073{
6074 int i;
6075
6076 for (i = 0; i < tp->irq_cnt; i++)
6077 napi_enable(&tp->napi[i].napi);
6078}
6079
6080static void tg3_napi_init(struct tg3 *tp)
6081{
6082 int i;
6083
6084 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
6085 for (i = 1; i < tp->irq_cnt; i++)
6086 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
6087}
6088
6089static void tg3_napi_fini(struct tg3 *tp)
6090{
6091 int i;
6092
6093 for (i = 0; i < tp->irq_cnt; i++)
6094 netif_napi_del(&tp->napi[i].napi);
6095}
6096
6097static inline void tg3_netif_stop(struct tg3 *tp)
6098{
6099 tp->dev->trans_start = jiffies; /* prevent tx timeout */
6100 tg3_napi_disable(tp);
6101 netif_tx_disable(tp->dev);
6102}
6103
6104static inline void tg3_netif_start(struct tg3 *tp)
6105{
6106 /* NOTE: unconditional netif_tx_wake_all_queues is only
6107 * appropriate so long as all callers are assured to
6108 * have free tx slots (such as after tg3_init_hw)
6109 */
6110 netif_tx_wake_all_queues(tp->dev);
6111
6112 tg3_napi_enable(tp);
6113 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
6114 tg3_enable_ints(tp);
6115}
6116
David S. Millerf47c11e2005-06-24 20:18:35 -07006117static void tg3_irq_quiesce(struct tg3 *tp)
6118{
Matt Carlson4f125f42009-09-01 12:55:02 +00006119 int i;
6120
David S. Millerf47c11e2005-06-24 20:18:35 -07006121 BUG_ON(tp->irq_sync);
6122
6123 tp->irq_sync = 1;
6124 smp_mb();
6125
Matt Carlson4f125f42009-09-01 12:55:02 +00006126 for (i = 0; i < tp->irq_cnt; i++)
6127 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07006128}
6129
David S. Millerf47c11e2005-06-24 20:18:35 -07006130/* Fully shutdown all tg3 driver activity elsewhere in the system.
6131 * If irq_sync is non-zero, then the IRQ handler must be synchronized
6132 * with as well. Most of the time, this is not necessary except when
6133 * shutting down the device.
6134 */
6135static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
6136{
Michael Chan46966542007-07-11 19:47:19 -07006137 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07006138 if (irq_sync)
6139 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07006140}
6141
6142static inline void tg3_full_unlock(struct tg3 *tp)
6143{
David S. Millerf47c11e2005-06-24 20:18:35 -07006144 spin_unlock_bh(&tp->lock);
6145}
6146
Michael Chanfcfa0a32006-03-20 22:28:41 -08006147/* One-shot MSI handler - Chip automatically disables interrupt
6148 * after sending MSI so driver doesn't have to do it.
6149 */
David Howells7d12e782006-10-05 14:55:46 +01006150static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08006151{
Matt Carlson09943a12009-08-28 14:01:57 +00006152 struct tg3_napi *tnapi = dev_id;
6153 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08006154
Matt Carlson898a56f2009-08-28 14:02:40 +00006155 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006156 if (tnapi->rx_rcb)
6157 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08006158
6159 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00006160 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08006161
6162 return IRQ_HANDLED;
6163}
6164
Michael Chan88b06bc22005-04-21 17:13:25 -07006165/* MSI ISR - No need to check for interrupt sharing and no need to
6166 * flush status block and interrupt mailbox. PCI ordering rules
6167 * guarantee that MSI will arrive after the status block.
6168 */
David Howells7d12e782006-10-05 14:55:46 +01006169static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07006170{
Matt Carlson09943a12009-08-28 14:01:57 +00006171 struct tg3_napi *tnapi = dev_id;
6172 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07006173
Matt Carlson898a56f2009-08-28 14:02:40 +00006174 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006175 if (tnapi->rx_rcb)
6176 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07006177 /*
David S. Millerfac9b832005-05-18 22:46:34 -07006178 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07006179 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07006180 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07006181 * NIC to stop sending us irqs, engaging "in-intr-handler"
6182 * event coalescing.
6183 */
Matt Carlson5b39de92011-08-31 11:44:50 +00006184 tw32_mailbox(tnapi->int_mbox, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07006185 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00006186 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07006187
Michael Chan88b06bc22005-04-21 17:13:25 -07006188 return IRQ_RETVAL(1);
6189}
6190
David Howells7d12e782006-10-05 14:55:46 +01006191static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006192{
Matt Carlson09943a12009-08-28 14:01:57 +00006193 struct tg3_napi *tnapi = dev_id;
6194 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006195 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006196 unsigned int handled = 1;
6197
Linus Torvalds1da177e2005-04-16 15:20:36 -07006198 /* In INTx mode, it is possible for the interrupt to arrive at
6199 * the CPU before the status block posted prior to the interrupt.
6200 * Reading the PCI State register will confirm whether the
6201 * interrupt is ours and will flush the status block.
6202 */
Michael Chand18edcb2007-03-24 20:57:11 -07006203 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
Joe Perches63c3a662011-04-26 08:12:10 +00006204 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07006205 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
6206 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07006207 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07006208 }
Michael Chand18edcb2007-03-24 20:57:11 -07006209 }
6210
6211 /*
6212 * Writing any value to intr-mbox-0 clears PCI INTA# and
6213 * chip-internal interrupt pending events.
6214 * Writing non-zero to intr-mbox-0 additional tells the
6215 * NIC to stop sending us irqs, engaging "in-intr-handler"
6216 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07006217 *
6218 * Flush the mailbox to de-assert the IRQ immediately to prevent
6219 * spurious interrupts. The flush impacts performance but
6220 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07006221 */
Michael Chanc04cb342007-05-07 00:26:15 -07006222 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07006223 if (tg3_irq_sync(tp))
6224 goto out;
6225 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00006226 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00006227 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00006228 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07006229 } else {
6230 /* No work, shared interrupt perhaps? re-enable
6231 * interrupts, and flush that PCI write
6232 */
6233 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
6234 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07006235 }
David S. Millerf47c11e2005-06-24 20:18:35 -07006236out:
David S. Millerfac9b832005-05-18 22:46:34 -07006237 return IRQ_RETVAL(handled);
6238}
6239
David Howells7d12e782006-10-05 14:55:46 +01006240static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07006241{
Matt Carlson09943a12009-08-28 14:01:57 +00006242 struct tg3_napi *tnapi = dev_id;
6243 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006244 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07006245 unsigned int handled = 1;
6246
David S. Millerfac9b832005-05-18 22:46:34 -07006247 /* In INTx mode, it is possible for the interrupt to arrive at
6248 * the CPU before the status block posted prior to the interrupt.
6249 * Reading the PCI State register will confirm whether the
6250 * interrupt is ours and will flush the status block.
6251 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006252 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Joe Perches63c3a662011-04-26 08:12:10 +00006253 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07006254 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
6255 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07006256 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006257 }
Michael Chand18edcb2007-03-24 20:57:11 -07006258 }
6259
6260 /*
6261 * writing any value to intr-mbox-0 clears PCI INTA# and
6262 * chip-internal interrupt pending events.
6263 * writing non-zero to intr-mbox-0 additional tells the
6264 * NIC to stop sending us irqs, engaging "in-intr-handler"
6265 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07006266 *
6267 * Flush the mailbox to de-assert the IRQ immediately to prevent
6268 * spurious interrupts. The flush impacts performance but
6269 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07006270 */
Michael Chanc04cb342007-05-07 00:26:15 -07006271 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00006272
6273 /*
6274 * In a shared interrupt configuration, sometimes other devices'
6275 * interrupts will scream. We record the current status tag here
6276 * so that the above check can report that the screaming interrupts
6277 * are unhandled. Eventually they will be silenced.
6278 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006279 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00006280
Michael Chand18edcb2007-03-24 20:57:11 -07006281 if (tg3_irq_sync(tp))
6282 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00006283
Matt Carlson72334482009-08-28 14:03:01 +00006284 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00006285
Matt Carlson09943a12009-08-28 14:01:57 +00006286 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00006287
David S. Millerf47c11e2005-06-24 20:18:35 -07006288out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006289 return IRQ_RETVAL(handled);
6290}
6291
Michael Chan79381092005-04-21 17:13:59 -07006292/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01006293static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07006294{
Matt Carlson09943a12009-08-28 14:01:57 +00006295 struct tg3_napi *tnapi = dev_id;
6296 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006297 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07006298
Michael Chanf9804dd2005-09-27 12:13:10 -07006299 if ((sblk->status & SD_STATUS_UPDATED) ||
6300 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07006301 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07006302 return IRQ_RETVAL(1);
6303 }
6304 return IRQ_RETVAL(0);
6305}
6306
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07006307static int tg3_init_hw(struct tg3 *, int);
Michael Chan944d9802005-05-29 14:57:48 -07006308static int tg3_halt(struct tg3 *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006309
Michael Chanb9ec6c12006-07-25 16:37:27 -07006310/* Restart hardware after configuration changes, self-test, etc.
6311 * Invoked with tp->lock held.
6312 */
6313static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
Eric Dumazet78c61462008-04-24 23:33:06 -07006314 __releases(tp->lock)
6315 __acquires(tp->lock)
Michael Chanb9ec6c12006-07-25 16:37:27 -07006316{
6317 int err;
6318
6319 err = tg3_init_hw(tp, reset_phy);
6320 if (err) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006321 netdev_err(tp->dev,
6322 "Failed to re-initialize device, aborting\n");
Michael Chanb9ec6c12006-07-25 16:37:27 -07006323 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
6324 tg3_full_unlock(tp);
6325 del_timer_sync(&tp->timer);
6326 tp->irq_sync = 0;
Matt Carlsonfed97812009-09-01 13:10:19 +00006327 tg3_napi_enable(tp);
Michael Chanb9ec6c12006-07-25 16:37:27 -07006328 dev_close(tp->dev);
6329 tg3_full_lock(tp, 0);
6330 }
6331 return err;
6332}
6333
Linus Torvalds1da177e2005-04-16 15:20:36 -07006334#ifdef CONFIG_NET_POLL_CONTROLLER
6335static void tg3_poll_controller(struct net_device *dev)
6336{
Matt Carlson4f125f42009-09-01 12:55:02 +00006337 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07006338 struct tg3 *tp = netdev_priv(dev);
6339
Matt Carlson4f125f42009-09-01 12:55:02 +00006340 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00006341 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006342}
6343#endif
6344
David Howellsc4028952006-11-22 14:57:56 +00006345static void tg3_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006346{
David Howellsc4028952006-11-22 14:57:56 +00006347 struct tg3 *tp = container_of(work, struct tg3, reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006348 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006349
Michael Chan7faa0062006-02-02 17:29:28 -08006350 tg3_full_lock(tp, 0);
Michael Chan7faa0062006-02-02 17:29:28 -08006351
6352 if (!netif_running(tp->dev)) {
Matt Carlsondb219972011-11-04 09:15:03 +00006353 tg3_flag_clear(tp, RESET_TASK_PENDING);
Michael Chan7faa0062006-02-02 17:29:28 -08006354 tg3_full_unlock(tp);
6355 return;
6356 }
6357
6358 tg3_full_unlock(tp);
6359
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006360 tg3_phy_stop(tp);
6361
Linus Torvalds1da177e2005-04-16 15:20:36 -07006362 tg3_netif_stop(tp);
6363
David S. Millerf47c11e2005-06-24 20:18:35 -07006364 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006365
Joe Perches63c3a662011-04-26 08:12:10 +00006366 if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
Michael Chandf3e6542006-05-26 17:48:07 -07006367 tp->write32_tx_mbox = tg3_write32_tx_mbox;
6368 tp->write32_rx_mbox = tg3_write_flush_reg32;
Joe Perches63c3a662011-04-26 08:12:10 +00006369 tg3_flag_set(tp, MBOX_WRITE_REORDER);
6370 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07006371 }
6372
Michael Chan944d9802005-05-29 14:57:48 -07006373 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006374 err = tg3_init_hw(tp, 1);
6375 if (err)
Michael Chanb9ec6c12006-07-25 16:37:27 -07006376 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377
6378 tg3_netif_start(tp);
6379
Michael Chanb9ec6c12006-07-25 16:37:27 -07006380out:
Michael Chan7faa0062006-02-02 17:29:28 -08006381 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006382
6383 if (!err)
6384 tg3_phy_start(tp);
Matt Carlsondb219972011-11-04 09:15:03 +00006385
6386 tg3_flag_clear(tp, RESET_TASK_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006387}
6388
6389static void tg3_tx_timeout(struct net_device *dev)
6390{
6391 struct tg3 *tp = netdev_priv(dev);
6392
Michael Chanb0408752007-02-13 12:18:30 -08006393 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00006394 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00006395 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08006396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006397
Matt Carlsondb219972011-11-04 09:15:03 +00006398 tg3_reset_task_schedule(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006399}
6400
Michael Chanc58ec932005-09-17 00:46:27 -07006401/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
6402static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
6403{
6404 u32 base = (u32) mapping & 0xffffffff;
6405
Eric Dumazet807540b2010-09-23 05:40:09 +00006406 return (base > 0xffffdcc0) && (base + len + 8 < base);
Michael Chanc58ec932005-09-17 00:46:27 -07006407}
6408
Michael Chan72f2afb2006-03-06 19:28:35 -08006409/* Test for DMA addresses > 40-bit */
6410static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
6411 int len)
6412{
6413#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Joe Perches63c3a662011-04-26 08:12:10 +00006414 if (tg3_flag(tp, 40BIT_DMA_BUG))
Eric Dumazet807540b2010-09-23 05:40:09 +00006415 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08006416 return 0;
6417#else
6418 return 0;
6419#endif
6420}
6421
Matt Carlsond1a3b732011-07-27 14:20:51 +00006422static inline void tg3_tx_set_bd(struct tg3_tx_buffer_desc *txbd,
Matt Carlson92cd3a12011-07-27 14:20:47 +00006423 dma_addr_t mapping, u32 len, u32 flags,
6424 u32 mss, u32 vlan)
Matt Carlson2ffcc982011-05-19 12:12:44 +00006425{
Matt Carlson92cd3a12011-07-27 14:20:47 +00006426 txbd->addr_hi = ((u64) mapping >> 32);
6427 txbd->addr_lo = ((u64) mapping & 0xffffffff);
6428 txbd->len_flags = (len << TXD_LEN_SHIFT) | (flags & 0x0000ffff);
6429 txbd->vlan_tag = (mss << TXD_MSS_SHIFT) | (vlan << TXD_VLAN_TAG_SHIFT);
Matt Carlson2ffcc982011-05-19 12:12:44 +00006430}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006431
Matt Carlson84b67b22011-07-27 14:20:52 +00006432static bool tg3_tx_frag_set(struct tg3_napi *tnapi, u32 *entry, u32 *budget,
Matt Carlsond1a3b732011-07-27 14:20:51 +00006433 dma_addr_t map, u32 len, u32 flags,
6434 u32 mss, u32 vlan)
6435{
6436 struct tg3 *tp = tnapi->tp;
6437 bool hwbug = false;
6438
6439 if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
6440 hwbug = 1;
6441
6442 if (tg3_4g_overflow_test(map, len))
6443 hwbug = 1;
6444
6445 if (tg3_40bit_overflow_test(tp, map, len))
6446 hwbug = 1;
6447
Matt Carlsone31aa982011-07-27 14:20:53 +00006448 if (tg3_flag(tp, 4K_FIFO_LIMIT)) {
Matt Carlsonb9e45482011-11-04 09:14:59 +00006449 u32 prvidx = *entry;
Matt Carlsone31aa982011-07-27 14:20:53 +00006450 u32 tmp_flag = flags & ~TXD_FLAG_END;
Matt Carlsonb9e45482011-11-04 09:14:59 +00006451 while (len > TG3_TX_BD_DMA_MAX && *budget) {
Matt Carlsone31aa982011-07-27 14:20:53 +00006452 u32 frag_len = TG3_TX_BD_DMA_MAX;
6453 len -= TG3_TX_BD_DMA_MAX;
6454
Matt Carlsonb9e45482011-11-04 09:14:59 +00006455 /* Avoid the 8byte DMA problem */
6456 if (len <= 8) {
6457 len += TG3_TX_BD_DMA_MAX / 2;
6458 frag_len = TG3_TX_BD_DMA_MAX / 2;
Matt Carlsone31aa982011-07-27 14:20:53 +00006459 }
6460
Matt Carlsonb9e45482011-11-04 09:14:59 +00006461 tnapi->tx_buffers[*entry].fragmented = true;
6462
6463 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6464 frag_len, tmp_flag, mss, vlan);
6465 *budget -= 1;
6466 prvidx = *entry;
6467 *entry = NEXT_TX(*entry);
6468
Matt Carlsone31aa982011-07-27 14:20:53 +00006469 map += frag_len;
6470 }
6471
6472 if (len) {
6473 if (*budget) {
6474 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6475 len, flags, mss, vlan);
Matt Carlsonb9e45482011-11-04 09:14:59 +00006476 *budget -= 1;
Matt Carlsone31aa982011-07-27 14:20:53 +00006477 *entry = NEXT_TX(*entry);
6478 } else {
6479 hwbug = 1;
Matt Carlsonb9e45482011-11-04 09:14:59 +00006480 tnapi->tx_buffers[prvidx].fragmented = false;
Matt Carlsone31aa982011-07-27 14:20:53 +00006481 }
6482 }
6483 } else {
Matt Carlson84b67b22011-07-27 14:20:52 +00006484 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6485 len, flags, mss, vlan);
Matt Carlsone31aa982011-07-27 14:20:53 +00006486 *entry = NEXT_TX(*entry);
6487 }
Matt Carlsond1a3b732011-07-27 14:20:51 +00006488
6489 return hwbug;
6490}
6491
Matt Carlson0d681b22011-07-27 14:20:49 +00006492static void tg3_tx_skb_unmap(struct tg3_napi *tnapi, u32 entry, int last)
Matt Carlson432aa7e2011-05-19 12:12:45 +00006493{
6494 int i;
Matt Carlson0d681b22011-07-27 14:20:49 +00006495 struct sk_buff *skb;
Matt Carlsondf8944c2011-07-27 14:20:46 +00006496 struct tg3_tx_ring_info *txb = &tnapi->tx_buffers[entry];
Matt Carlson432aa7e2011-05-19 12:12:45 +00006497
Matt Carlson0d681b22011-07-27 14:20:49 +00006498 skb = txb->skb;
6499 txb->skb = NULL;
6500
Matt Carlson432aa7e2011-05-19 12:12:45 +00006501 pci_unmap_single(tnapi->tp->pdev,
6502 dma_unmap_addr(txb, mapping),
6503 skb_headlen(skb),
6504 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00006505
6506 while (txb->fragmented) {
6507 txb->fragmented = false;
6508 entry = NEXT_TX(entry);
6509 txb = &tnapi->tx_buffers[entry];
6510 }
6511
Matt Carlsonba1142e2011-11-04 09:15:00 +00006512 for (i = 0; i <= last; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00006513 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Matt Carlson432aa7e2011-05-19 12:12:45 +00006514
6515 entry = NEXT_TX(entry);
6516 txb = &tnapi->tx_buffers[entry];
6517
6518 pci_unmap_page(tnapi->tp->pdev,
6519 dma_unmap_addr(txb, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00006520 skb_frag_size(frag), PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00006521
6522 while (txb->fragmented) {
6523 txb->fragmented = false;
6524 entry = NEXT_TX(entry);
6525 txb = &tnapi->tx_buffers[entry];
6526 }
Matt Carlson432aa7e2011-05-19 12:12:45 +00006527 }
6528}
6529
Michael Chan72f2afb2006-03-06 19:28:35 -08006530/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006531static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
David S. Miller1805b2f2011-10-24 18:18:09 -04006532 struct sk_buff **pskb,
Matt Carlson84b67b22011-07-27 14:20:52 +00006533 u32 *entry, u32 *budget,
Matt Carlson92cd3a12011-07-27 14:20:47 +00006534 u32 base_flags, u32 mss, u32 vlan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006535{
Matt Carlson24f4efd2009-11-13 13:03:35 +00006536 struct tg3 *tp = tnapi->tp;
David S. Miller1805b2f2011-10-24 18:18:09 -04006537 struct sk_buff *new_skb, *skb = *pskb;
Michael Chanc58ec932005-09-17 00:46:27 -07006538 dma_addr_t new_addr = 0;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006539 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006540
Matt Carlson41588ba12008-04-19 18:12:33 -07006541 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
6542 new_skb = skb_copy(skb, GFP_ATOMIC);
6543 else {
6544 int more_headroom = 4 - ((unsigned long)skb->data & 3);
6545
6546 new_skb = skb_copy_expand(skb,
6547 skb_headroom(skb) + more_headroom,
6548 skb_tailroom(skb), GFP_ATOMIC);
6549 }
6550
Linus Torvalds1da177e2005-04-16 15:20:36 -07006551 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07006552 ret = -1;
6553 } else {
6554 /* New SKB is guaranteed to be linear. */
Alexander Duyckf4188d82009-12-02 16:48:38 +00006555 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
6556 PCI_DMA_TODEVICE);
6557 /* Make sure the mapping succeeded */
6558 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00006559 dev_kfree_skb(new_skb);
Michael Chanc58ec932005-09-17 00:46:27 -07006560 ret = -1;
Michael Chanc58ec932005-09-17 00:46:27 -07006561 } else {
Matt Carlsonb9e45482011-11-04 09:14:59 +00006562 u32 save_entry = *entry;
6563
Matt Carlson92cd3a12011-07-27 14:20:47 +00006564 base_flags |= TXD_FLAG_END;
6565
Matt Carlson84b67b22011-07-27 14:20:52 +00006566 tnapi->tx_buffers[*entry].skb = new_skb;
6567 dma_unmap_addr_set(&tnapi->tx_buffers[*entry],
Matt Carlson432aa7e2011-05-19 12:12:45 +00006568 mapping, new_addr);
6569
Matt Carlson84b67b22011-07-27 14:20:52 +00006570 if (tg3_tx_frag_set(tnapi, entry, budget, new_addr,
Matt Carlsond1a3b732011-07-27 14:20:51 +00006571 new_skb->len, base_flags,
6572 mss, vlan)) {
Matt Carlsonba1142e2011-11-04 09:15:00 +00006573 tg3_tx_skb_unmap(tnapi, save_entry, -1);
Matt Carlsond1a3b732011-07-27 14:20:51 +00006574 dev_kfree_skb(new_skb);
6575 ret = -1;
6576 }
Michael Chanc58ec932005-09-17 00:46:27 -07006577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006578 }
6579
Linus Torvalds1da177e2005-04-16 15:20:36 -07006580 dev_kfree_skb(skb);
David S. Miller1805b2f2011-10-24 18:18:09 -04006581 *pskb = new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07006582 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006583}
6584
Matt Carlson2ffcc982011-05-19 12:12:44 +00006585static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07006586
6587/* Use GSO to workaround a rare TSO bug that may be triggered when the
6588 * TSO header is greater than 80 bytes.
6589 */
6590static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
6591{
6592 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006593 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07006594
6595 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006596 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07006597 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006598
6599 /* netif_tx_stop_queue() must be done before checking
6600 * checking tx index in tg3_tx_avail() below, because in
6601 * tg3_tx(), we update tx index before checking for
6602 * netif_tx_queue_stopped().
6603 */
6604 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006605 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08006606 return NETDEV_TX_BUSY;
6607
6608 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07006609 }
6610
6611 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07006612 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07006613 goto tg3_tso_bug_end;
6614
6615 do {
6616 nskb = segs;
6617 segs = segs->next;
6618 nskb->next = NULL;
Matt Carlson2ffcc982011-05-19 12:12:44 +00006619 tg3_start_xmit(nskb, tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07006620 } while (segs);
6621
6622tg3_tso_bug_end:
6623 dev_kfree_skb(skb);
6624
6625 return NETDEV_TX_OK;
6626}
Michael Chan52c0fd82006-06-29 20:15:54 -07006627
Michael Chan5a6f3072006-03-20 22:28:05 -08006628/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
Joe Perches63c3a662011-04-26 08:12:10 +00006629 * support TG3_FLAG_HW_TSO_1 or firmware TSO only.
Michael Chan5a6f3072006-03-20 22:28:05 -08006630 */
Matt Carlson2ffcc982011-05-19 12:12:44 +00006631static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08006632{
6633 struct tg3 *tp = netdev_priv(dev);
Matt Carlson92cd3a12011-07-27 14:20:47 +00006634 u32 len, entry, base_flags, mss, vlan = 0;
Matt Carlson84b67b22011-07-27 14:20:52 +00006635 u32 budget;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006636 int i = -1, would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07006637 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00006638 struct tg3_napi *tnapi;
6639 struct netdev_queue *txq;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006640 unsigned int last;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006641
Matt Carlson24f4efd2009-11-13 13:03:35 +00006642 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
6643 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Joe Perches63c3a662011-04-26 08:12:10 +00006644 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006645 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006646
Matt Carlson84b67b22011-07-27 14:20:52 +00006647 budget = tg3_tx_avail(tnapi);
6648
Michael Chan00b70502006-06-17 21:58:45 -07006649 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006650 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07006651 * interrupt. Furthermore, IRQ processing runs lockless so we have
6652 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07006653 */
Matt Carlson84b67b22011-07-27 14:20:52 +00006654 if (unlikely(budget <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006655 if (!netif_tx_queue_stopped(txq)) {
6656 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08006657
6658 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00006659 netdev_err(dev,
6660 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08006661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006662 return NETDEV_TX_BUSY;
6663 }
6664
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006665 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006666 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07006667 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006668 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00006669
Matt Carlsonbe98da62010-07-11 09:31:46 +00006670 mss = skb_shinfo(skb)->gso_size;
6671 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006672 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00006673 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006674
6675 if (skb_header_cloned(skb) &&
Eric Dumazet48855432011-10-24 07:53:03 +00006676 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
6677 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006678
Matt Carlson34195c32010-07-11 09:31:42 +00006679 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07006680 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006681
Matt Carlson02e96082010-09-15 08:59:59 +00006682 if (skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00006683 hdr_len = skb_headlen(skb) - ETH_HLEN;
6684 } else {
6685 u32 ip_tcp_len;
6686
6687 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
6688 hdr_len = ip_tcp_len + tcp_opt_len;
6689
6690 iph->check = 0;
6691 iph->tot_len = htons(mss + hdr_len);
6692 }
6693
Michael Chan52c0fd82006-06-29 20:15:54 -07006694 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Joe Perches63c3a662011-04-26 08:12:10 +00006695 tg3_flag(tp, TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00006696 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07006697
Linus Torvalds1da177e2005-04-16 15:20:36 -07006698 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
6699 TXD_FLAG_CPU_POST_DMA);
6700
Joe Perches63c3a662011-04-26 08:12:10 +00006701 if (tg3_flag(tp, HW_TSO_1) ||
6702 tg3_flag(tp, HW_TSO_2) ||
6703 tg3_flag(tp, HW_TSO_3)) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006704 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006705 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006706 } else
6707 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
6708 iph->daddr, 0,
6709 IPPROTO_TCP,
6710 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006711
Joe Perches63c3a662011-04-26 08:12:10 +00006712 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlson615774f2009-11-13 13:03:39 +00006713 mss |= (hdr_len & 0xc) << 12;
6714 if (hdr_len & 0x10)
6715 base_flags |= 0x00000010;
6716 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +00006717 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006718 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +00006719 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006720 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006721 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006722 int tsflags;
6723
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006724 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006725 mss |= (tsflags << 11);
6726 }
6727 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006728 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006729 int tsflags;
6730
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006731 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006732 base_flags |= tsflags << 12;
6733 }
6734 }
6735 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00006736
Matt Carlson93a700a2011-08-31 11:44:54 +00006737 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
6738 !mss && skb->len > VLAN_ETH_FRAME_LEN)
6739 base_flags |= TXD_FLAG_JMB_PKT;
6740
Matt Carlson92cd3a12011-07-27 14:20:47 +00006741 if (vlan_tx_tag_present(skb)) {
6742 base_flags |= TXD_FLAG_VLAN;
6743 vlan = vlan_tx_tag_get(skb);
6744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006745
Alexander Duyckf4188d82009-12-02 16:48:38 +00006746 len = skb_headlen(skb);
6747
6748 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
Eric Dumazet48855432011-10-24 07:53:03 +00006749 if (pci_dma_mapping_error(tp->pdev, mapping))
6750 goto drop;
6751
David S. Miller90079ce2008-09-11 04:52:51 -07006752
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006753 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006754 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006755
6756 would_hit_hwbug = 0;
6757
Joe Perches63c3a662011-04-26 08:12:10 +00006758 if (tg3_flag(tp, 5701_DMA_BUG))
Michael Chanc58ec932005-09-17 00:46:27 -07006759 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006760
Matt Carlson84b67b22011-07-27 14:20:52 +00006761 if (tg3_tx_frag_set(tnapi, &entry, &budget, mapping, len, base_flags |
Matt Carlsond1a3b732011-07-27 14:20:51 +00006762 ((skb_shinfo(skb)->nr_frags == 0) ? TXD_FLAG_END : 0),
Matt Carlsonba1142e2011-11-04 09:15:00 +00006763 mss, vlan)) {
Matt Carlsond1a3b732011-07-27 14:20:51 +00006764 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006765 /* Now loop through additional data fragments, and queue them. */
Matt Carlsonba1142e2011-11-04 09:15:00 +00006766 } else if (skb_shinfo(skb)->nr_frags > 0) {
Matt Carlson92cd3a12011-07-27 14:20:47 +00006767 u32 tmp_mss = mss;
6768
6769 if (!tg3_flag(tp, HW_TSO_1) &&
6770 !tg3_flag(tp, HW_TSO_2) &&
6771 !tg3_flag(tp, HW_TSO_3))
6772 tmp_mss = 0;
6773
Linus Torvalds1da177e2005-04-16 15:20:36 -07006774 last = skb_shinfo(skb)->nr_frags - 1;
6775 for (i = 0; i <= last; i++) {
6776 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6777
Eric Dumazet9e903e02011-10-18 21:00:24 +00006778 len = skb_frag_size(frag);
Ian Campbelldc234d02011-08-24 22:28:11 +00006779 mapping = skb_frag_dma_map(&tp->pdev->dev, frag, 0,
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01006780 len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006781
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006782 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006783 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00006784 mapping);
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01006785 if (dma_mapping_error(&tp->pdev->dev, mapping))
Alexander Duyckf4188d82009-12-02 16:48:38 +00006786 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006787
Matt Carlsonb9e45482011-11-04 09:14:59 +00006788 if (!budget ||
6789 tg3_tx_frag_set(tnapi, &entry, &budget, mapping,
Matt Carlson84b67b22011-07-27 14:20:52 +00006790 len, base_flags |
6791 ((i == last) ? TXD_FLAG_END : 0),
Matt Carlsonb9e45482011-11-04 09:14:59 +00006792 tmp_mss, vlan)) {
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006793 would_hit_hwbug = 1;
Matt Carlsonb9e45482011-11-04 09:14:59 +00006794 break;
6795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006796 }
6797 }
6798
6799 if (would_hit_hwbug) {
Matt Carlson0d681b22011-07-27 14:20:49 +00006800 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006801
6802 /* If the workaround fails due to memory/mapping
6803 * failure, silently drop this packet.
6804 */
Matt Carlson84b67b22011-07-27 14:20:52 +00006805 entry = tnapi->tx_prod;
6806 budget = tg3_tx_avail(tnapi);
David S. Miller1805b2f2011-10-24 18:18:09 -04006807 if (tigon3_dma_hwbug_workaround(tnapi, &skb, &entry, &budget,
Matt Carlson84b67b22011-07-27 14:20:52 +00006808 base_flags, mss, vlan))
Eric Dumazet48855432011-10-24 07:53:03 +00006809 goto drop_nofree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006810 }
6811
Richard Cochrand515b452011-06-19 03:31:41 +00006812 skb_tx_timestamp(skb);
Tom Herbert298376d2011-11-28 16:33:30 +00006813 netdev_sent_queue(tp->dev, skb->len);
Richard Cochrand515b452011-06-19 03:31:41 +00006814
Linus Torvalds1da177e2005-04-16 15:20:36 -07006815 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006816 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006817
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006818 tnapi->tx_prod = entry;
6819 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006820 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006821
6822 /* netif_tx_stop_queue() must be done before checking
6823 * checking tx index in tg3_tx_avail() below, because in
6824 * tg3_tx(), we update tx index before checking for
6825 * netif_tx_queue_stopped().
6826 */
6827 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006828 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006829 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07006830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006831
Eric Dumazetcdd0db02009-05-28 00:00:41 +00006832 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006833 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006834
6835dma_error:
Matt Carlsonba1142e2011-11-04 09:15:00 +00006836 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, --i);
Matt Carlson432aa7e2011-05-19 12:12:45 +00006837 tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
Eric Dumazet48855432011-10-24 07:53:03 +00006838drop:
6839 dev_kfree_skb(skb);
6840drop_nofree:
6841 tp->tx_dropped++;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006842 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006843}
6844
Matt Carlson6e01b202011-08-19 13:58:20 +00006845static void tg3_mac_loopback(struct tg3 *tp, bool enable)
6846{
6847 if (enable) {
6848 tp->mac_mode &= ~(MAC_MODE_HALF_DUPLEX |
6849 MAC_MODE_PORT_MODE_MASK);
6850
6851 tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
6852
6853 if (!tg3_flag(tp, 5705_PLUS))
6854 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
6855
6856 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
6857 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
6858 else
6859 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
6860 } else {
6861 tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
6862
6863 if (tg3_flag(tp, 5705_PLUS) ||
6864 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) ||
6865 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
6866 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
6867 }
6868
6869 tw32(MAC_MODE, tp->mac_mode);
6870 udelay(40);
6871}
6872
Matt Carlson941ec902011-08-19 13:58:23 +00006873static int tg3_phy_lpbk_set(struct tg3 *tp, u32 speed, bool extlpbk)
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006874{
Matt Carlson941ec902011-08-19 13:58:23 +00006875 u32 val, bmcr, mac_mode, ptest = 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006876
6877 tg3_phy_toggle_apd(tp, false);
6878 tg3_phy_toggle_automdix(tp, 0);
6879
Matt Carlson941ec902011-08-19 13:58:23 +00006880 if (extlpbk && tg3_phy_set_extloopbk(tp))
6881 return -EIO;
6882
6883 bmcr = BMCR_FULLDPLX;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006884 switch (speed) {
6885 case SPEED_10:
6886 break;
6887 case SPEED_100:
6888 bmcr |= BMCR_SPEED100;
6889 break;
6890 case SPEED_1000:
6891 default:
6892 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
6893 speed = SPEED_100;
6894 bmcr |= BMCR_SPEED100;
6895 } else {
6896 speed = SPEED_1000;
6897 bmcr |= BMCR_SPEED1000;
6898 }
6899 }
6900
Matt Carlson941ec902011-08-19 13:58:23 +00006901 if (extlpbk) {
6902 if (!(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
6903 tg3_readphy(tp, MII_CTRL1000, &val);
6904 val |= CTL1000_AS_MASTER |
6905 CTL1000_ENABLE_MASTER;
6906 tg3_writephy(tp, MII_CTRL1000, val);
6907 } else {
6908 ptest = MII_TG3_FET_PTEST_TRIM_SEL |
6909 MII_TG3_FET_PTEST_TRIM_2;
6910 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest);
6911 }
6912 } else
6913 bmcr |= BMCR_LOOPBACK;
6914
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006915 tg3_writephy(tp, MII_BMCR, bmcr);
6916
6917 /* The write needs to be flushed for the FETs */
6918 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
6919 tg3_readphy(tp, MII_BMCR, &bmcr);
6920
6921 udelay(40);
6922
6923 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
6924 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Matt Carlson941ec902011-08-19 13:58:23 +00006925 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest |
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006926 MII_TG3_FET_PTEST_FRC_TX_LINK |
6927 MII_TG3_FET_PTEST_FRC_TX_LOCK);
6928
6929 /* The write needs to be flushed for the AC131 */
6930 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
6931 }
6932
6933 /* Reset to prevent losing 1st rx packet intermittently */
6934 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
6935 tg3_flag(tp, 5780_CLASS)) {
6936 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
6937 udelay(10);
6938 tw32_f(MAC_RX_MODE, tp->rx_mode);
6939 }
6940
6941 mac_mode = tp->mac_mode &
6942 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
6943 if (speed == SPEED_1000)
6944 mac_mode |= MAC_MODE_PORT_MODE_GMII;
6945 else
6946 mac_mode |= MAC_MODE_PORT_MODE_MII;
6947
6948 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
6949 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
6950
6951 if (masked_phy_id == TG3_PHY_ID_BCM5401)
6952 mac_mode &= ~MAC_MODE_LINK_POLARITY;
6953 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
6954 mac_mode |= MAC_MODE_LINK_POLARITY;
6955
6956 tg3_writephy(tp, MII_TG3_EXT_CTRL,
6957 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
6958 }
6959
6960 tw32(MAC_MODE, mac_mode);
6961 udelay(40);
Matt Carlson941ec902011-08-19 13:58:23 +00006962
6963 return 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006964}
6965
Michał Mirosławc8f44af2011-11-15 15:29:55 +00006966static void tg3_set_loopback(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006967{
6968 struct tg3 *tp = netdev_priv(dev);
6969
6970 if (features & NETIF_F_LOOPBACK) {
6971 if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
6972 return;
6973
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006974 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00006975 tg3_mac_loopback(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006976 netif_carrier_on(tp->dev);
6977 spin_unlock_bh(&tp->lock);
6978 netdev_info(dev, "Internal MAC loopback mode enabled.\n");
6979 } else {
6980 if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
6981 return;
6982
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006983 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00006984 tg3_mac_loopback(tp, false);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006985 /* Force link status check */
6986 tg3_setup_phy(tp, 1);
6987 spin_unlock_bh(&tp->lock);
6988 netdev_info(dev, "Internal MAC loopback mode disabled.\n");
6989 }
6990}
6991
Michał Mirosławc8f44af2011-11-15 15:29:55 +00006992static netdev_features_t tg3_fix_features(struct net_device *dev,
6993 netdev_features_t features)
Michał Mirosławdc668912011-04-07 03:35:07 +00006994{
6995 struct tg3 *tp = netdev_priv(dev);
6996
Joe Perches63c3a662011-04-26 08:12:10 +00006997 if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
Michał Mirosławdc668912011-04-07 03:35:07 +00006998 features &= ~NETIF_F_ALL_TSO;
6999
7000 return features;
7001}
7002
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007003static int tg3_set_features(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007004{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007005 netdev_features_t changed = dev->features ^ features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007006
7007 if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
7008 tg3_set_loopback(dev, features);
7009
7010 return 0;
7011}
7012
Linus Torvalds1da177e2005-04-16 15:20:36 -07007013static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
7014 int new_mtu)
7015{
7016 dev->mtu = new_mtu;
7017
Michael Chanef7f5ec2005-07-25 12:32:25 -07007018 if (new_mtu > ETH_DATA_LEN) {
Joe Perches63c3a662011-04-26 08:12:10 +00007019 if (tg3_flag(tp, 5780_CLASS)) {
Michał Mirosławdc668912011-04-07 03:35:07 +00007020 netdev_update_features(dev);
Joe Perches63c3a662011-04-26 08:12:10 +00007021 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson859a588792010-04-05 10:19:28 +00007022 } else {
Joe Perches63c3a662011-04-26 08:12:10 +00007023 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Matt Carlson859a588792010-04-05 10:19:28 +00007024 }
Michael Chanef7f5ec2005-07-25 12:32:25 -07007025 } else {
Joe Perches63c3a662011-04-26 08:12:10 +00007026 if (tg3_flag(tp, 5780_CLASS)) {
7027 tg3_flag_set(tp, TSO_CAPABLE);
Michał Mirosławdc668912011-04-07 03:35:07 +00007028 netdev_update_features(dev);
7029 }
Joe Perches63c3a662011-04-26 08:12:10 +00007030 tg3_flag_clear(tp, JUMBO_RING_ENABLE);
Michael Chanef7f5ec2005-07-25 12:32:25 -07007031 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007032}
7033
7034static int tg3_change_mtu(struct net_device *dev, int new_mtu)
7035{
7036 struct tg3 *tp = netdev_priv(dev);
Michael Chanb9ec6c12006-07-25 16:37:27 -07007037 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007038
7039 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
7040 return -EINVAL;
7041
7042 if (!netif_running(dev)) {
7043 /* We'll just catch it later when the
7044 * device is up'd.
7045 */
7046 tg3_set_mtu(dev, tp, new_mtu);
7047 return 0;
7048 }
7049
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07007050 tg3_phy_stop(tp);
7051
Linus Torvalds1da177e2005-04-16 15:20:36 -07007052 tg3_netif_stop(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07007053
7054 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007055
Michael Chan944d9802005-05-29 14:57:48 -07007056 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007057
7058 tg3_set_mtu(dev, tp, new_mtu);
7059
Michael Chanb9ec6c12006-07-25 16:37:27 -07007060 err = tg3_restart_hw(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007061
Michael Chanb9ec6c12006-07-25 16:37:27 -07007062 if (!err)
7063 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007064
David S. Millerf47c11e2005-06-24 20:18:35 -07007065 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007066
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07007067 if (!err)
7068 tg3_phy_start(tp);
7069
Michael Chanb9ec6c12006-07-25 16:37:27 -07007070 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007071}
7072
Matt Carlson21f581a2009-08-28 14:00:25 +00007073static void tg3_rx_prodring_free(struct tg3 *tp,
7074 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007075{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007076 int i;
7077
Matt Carlson8fea32b2010-09-15 08:59:58 +00007078 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007079 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00007080 i = (i + 1) & tp->rx_std_ring_mask)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007081 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007082 tp->rx_pkt_map_sz);
7083
Joe Perches63c3a662011-04-26 08:12:10 +00007084 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007085 for (i = tpr->rx_jmb_cons_idx;
7086 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00007087 i = (i + 1) & tp->rx_jmb_ring_mask) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007088 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007089 TG3_RX_JMB_MAP_SZ);
7090 }
7091 }
7092
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007093 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007095
Matt Carlson2c49a442010-09-30 10:34:35 +00007096 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007097 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007098 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007099
Joe Perches63c3a662011-04-26 08:12:10 +00007100 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007101 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007102 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007103 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007104 }
7105}
7106
Matt Carlsonc6cdf432010-04-05 10:19:26 +00007107/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007108 *
7109 * The chip has been shut down and the driver detached from
7110 * the networking, so no interrupts or new tx packets will
7111 * end up in the driver. tp->{tx,}lock are held and thus
7112 * we may not sleep.
7113 */
Matt Carlson21f581a2009-08-28 14:00:25 +00007114static int tg3_rx_prodring_alloc(struct tg3 *tp,
7115 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007116{
Matt Carlson287be122009-08-28 13:58:46 +00007117 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007118
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007119 tpr->rx_std_cons_idx = 0;
7120 tpr->rx_std_prod_idx = 0;
7121 tpr->rx_jmb_cons_idx = 0;
7122 tpr->rx_jmb_prod_idx = 0;
7123
Matt Carlson8fea32b2010-09-15 08:59:58 +00007124 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007125 memset(&tpr->rx_std_buffers[0], 0,
7126 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00007127 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007128 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00007129 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007130 goto done;
7131 }
7132
Linus Torvalds1da177e2005-04-16 15:20:36 -07007133 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00007134 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007135
Matt Carlson287be122009-08-28 13:58:46 +00007136 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00007137 if (tg3_flag(tp, 5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00007138 tp->dev->mtu > ETH_DATA_LEN)
7139 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
7140 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad42005-07-25 12:31:17 -07007141
Linus Torvalds1da177e2005-04-16 15:20:36 -07007142 /* Initialize invariants of the rings, we only set this
7143 * stuff once. This works because the card does not
7144 * write into the rx buffer posting rings.
7145 */
Matt Carlson2c49a442010-09-30 10:34:35 +00007146 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007147 struct tg3_rx_buffer_desc *rxd;
7148
Matt Carlson21f581a2009-08-28 14:00:25 +00007149 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00007150 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007151 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
7152 rxd->opaque = (RXD_OPAQUE_RING_STD |
7153 (i << RXD_OPAQUE_INDEX_SHIFT));
7154 }
7155
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007156 /* Now allocate fresh SKBs for each rx ring. */
7157 for (i = 0; i < tp->rx_pending; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007158 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_STD, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007159 netdev_warn(tp->dev,
7160 "Using a smaller RX standard ring. Only "
7161 "%d out of %d buffers were allocated "
7162 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007163 if (i == 0)
7164 goto initfail;
7165 tp->rx_pending = i;
7166 break;
7167 }
7168 }
7169
Joe Perches63c3a662011-04-26 08:12:10 +00007170 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007171 goto done;
7172
Matt Carlson2c49a442010-09-30 10:34:35 +00007173 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007174
Joe Perches63c3a662011-04-26 08:12:10 +00007175 if (!tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson0d86df82010-02-17 15:17:00 +00007176 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007177
Matt Carlson2c49a442010-09-30 10:34:35 +00007178 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00007179 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007180
Matt Carlson0d86df82010-02-17 15:17:00 +00007181 rxd = &tpr->rx_jmb[i].std;
7182 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
7183 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
7184 RXD_FLAG_JUMBO;
7185 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
7186 (i << RXD_OPAQUE_INDEX_SHIFT));
7187 }
7188
7189 for (i = 0; i < tp->rx_jumbo_pending; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007190 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_JUMBO, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007191 netdev_warn(tp->dev,
7192 "Using a smaller RX jumbo ring. Only %d "
7193 "out of %d buffers were allocated "
7194 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00007195 if (i == 0)
7196 goto initfail;
7197 tp->rx_jumbo_pending = i;
7198 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007199 }
7200 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007201
7202done:
Michael Chan32d8c572006-07-25 16:38:29 -07007203 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007204
7205initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00007206 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007207 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007208}
7209
Matt Carlson21f581a2009-08-28 14:00:25 +00007210static void tg3_rx_prodring_fini(struct tg3 *tp,
7211 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007212{
Matt Carlson21f581a2009-08-28 14:00:25 +00007213 kfree(tpr->rx_std_buffers);
7214 tpr->rx_std_buffers = NULL;
7215 kfree(tpr->rx_jmb_buffers);
7216 tpr->rx_jmb_buffers = NULL;
7217 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007218 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
7219 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00007220 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007221 }
Matt Carlson21f581a2009-08-28 14:00:25 +00007222 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007223 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
7224 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00007225 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007226 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007227}
7228
Matt Carlson21f581a2009-08-28 14:00:25 +00007229static int tg3_rx_prodring_init(struct tg3 *tp,
7230 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007231{
Matt Carlson2c49a442010-09-30 10:34:35 +00007232 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
7233 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007234 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007235 return -ENOMEM;
7236
Matt Carlson4bae65c2010-11-24 08:31:52 +00007237 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
7238 TG3_RX_STD_RING_BYTES(tp),
7239 &tpr->rx_std_mapping,
7240 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007241 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007242 goto err_out;
7243
Joe Perches63c3a662011-04-26 08:12:10 +00007244 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007245 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00007246 GFP_KERNEL);
7247 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007248 goto err_out;
7249
Matt Carlson4bae65c2010-11-24 08:31:52 +00007250 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
7251 TG3_RX_JMB_RING_BYTES(tp),
7252 &tpr->rx_jmb_mapping,
7253 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007254 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007255 goto err_out;
7256 }
7257
7258 return 0;
7259
7260err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00007261 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007262 return -ENOMEM;
7263}
7264
7265/* Free up pending packets in all rx/tx rings.
7266 *
7267 * The chip has been shut down and the driver detached from
7268 * the networking, so no interrupts or new tx packets will
7269 * end up in the driver. tp->{tx,}lock is not held and we are not
7270 * in an interrupt context and thus may sleep.
7271 */
7272static void tg3_free_rings(struct tg3 *tp)
7273{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007274 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007275
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007276 for (j = 0; j < tp->irq_cnt; j++) {
7277 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007278
Matt Carlson8fea32b2010-09-15 08:59:58 +00007279 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00007280
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007281 if (!tnapi->tx_buffers)
7282 continue;
7283
Matt Carlson0d681b22011-07-27 14:20:49 +00007284 for (i = 0; i < TG3_TX_RING_SIZE; i++) {
7285 struct sk_buff *skb = tnapi->tx_buffers[i].skb;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007286
Matt Carlson0d681b22011-07-27 14:20:49 +00007287 if (!skb)
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007288 continue;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007289
Matt Carlsonba1142e2011-11-04 09:15:00 +00007290 tg3_tx_skb_unmap(tnapi, i,
7291 skb_shinfo(skb)->nr_frags - 1);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007292
7293 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007294 }
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007295 }
Tom Herbert298376d2011-11-28 16:33:30 +00007296 netdev_reset_queue(tp->dev);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007297}
7298
7299/* Initialize tx/rx rings for packet processing.
7300 *
7301 * The chip has been shut down and the driver detached from
7302 * the networking, so no interrupts or new tx packets will
7303 * end up in the driver. tp->{tx,}lock are held and thus
7304 * we may not sleep.
7305 */
7306static int tg3_init_rings(struct tg3 *tp)
7307{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007308 int i;
Matt Carlson72334482009-08-28 14:03:01 +00007309
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007310 /* Free up all the SKBs. */
7311 tg3_free_rings(tp);
7312
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007313 for (i = 0; i < tp->irq_cnt; i++) {
7314 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007315
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007316 tnapi->last_tag = 0;
7317 tnapi->last_irq_tag = 0;
7318 tnapi->hw_status->status = 0;
7319 tnapi->hw_status->status_tag = 0;
7320 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7321
7322 tnapi->tx_prod = 0;
7323 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007324 if (tnapi->tx_ring)
7325 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007326
7327 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007328 if (tnapi->rx_rcb)
7329 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007330
Matt Carlson8fea32b2010-09-15 08:59:58 +00007331 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00007332 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007333 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00007334 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007335 }
Matt Carlson72334482009-08-28 14:03:01 +00007336
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007337 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007338}
7339
7340/*
7341 * Must not be invoked with interrupt sources disabled and
7342 * the hardware shutdown down.
7343 */
7344static void tg3_free_consistent(struct tg3 *tp)
7345{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007346 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00007347
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007348 for (i = 0; i < tp->irq_cnt; i++) {
7349 struct tg3_napi *tnapi = &tp->napi[i];
7350
7351 if (tnapi->tx_ring) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007352 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007353 tnapi->tx_ring, tnapi->tx_desc_mapping);
7354 tnapi->tx_ring = NULL;
7355 }
7356
7357 kfree(tnapi->tx_buffers);
7358 tnapi->tx_buffers = NULL;
7359
7360 if (tnapi->rx_rcb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007361 dma_free_coherent(&tp->pdev->dev,
7362 TG3_RX_RCB_RING_BYTES(tp),
7363 tnapi->rx_rcb,
7364 tnapi->rx_rcb_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007365 tnapi->rx_rcb = NULL;
7366 }
7367
Matt Carlson8fea32b2010-09-15 08:59:58 +00007368 tg3_rx_prodring_fini(tp, &tnapi->prodring);
7369
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007370 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007371 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
7372 tnapi->hw_status,
7373 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007374 tnapi->hw_status = NULL;
7375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007376 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007377
Linus Torvalds1da177e2005-04-16 15:20:36 -07007378 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007379 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
7380 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007381 tp->hw_stats = NULL;
7382 }
7383}
7384
7385/*
7386 * Must not be invoked with interrupt sources disabled and
7387 * the hardware shutdown down. Can sleep.
7388 */
7389static int tg3_alloc_consistent(struct tg3 *tp)
7390{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007391 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00007392
Matt Carlson4bae65c2010-11-24 08:31:52 +00007393 tp->hw_stats = dma_alloc_coherent(&tp->pdev->dev,
7394 sizeof(struct tg3_hw_stats),
7395 &tp->stats_mapping,
7396 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007397 if (!tp->hw_stats)
7398 goto err_out;
7399
Linus Torvalds1da177e2005-04-16 15:20:36 -07007400 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
7401
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007402 for (i = 0; i < tp->irq_cnt; i++) {
7403 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007404 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007405
Matt Carlson4bae65c2010-11-24 08:31:52 +00007406 tnapi->hw_status = dma_alloc_coherent(&tp->pdev->dev,
7407 TG3_HW_STATUS_SIZE,
7408 &tnapi->status_mapping,
7409 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007410 if (!tnapi->hw_status)
7411 goto err_out;
7412
7413 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007414 sblk = tnapi->hw_status;
7415
Matt Carlson8fea32b2010-09-15 08:59:58 +00007416 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
7417 goto err_out;
7418
Matt Carlson19cfaec2009-12-03 08:36:20 +00007419 /* If multivector TSS is enabled, vector 0 does not handle
7420 * tx interrupts. Don't allocate any resources for it.
7421 */
Joe Perches63c3a662011-04-26 08:12:10 +00007422 if ((!i && !tg3_flag(tp, ENABLE_TSS)) ||
7423 (i && tg3_flag(tp, ENABLE_TSS))) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00007424 tnapi->tx_buffers = kzalloc(
7425 sizeof(struct tg3_tx_ring_info) *
7426 TG3_TX_RING_SIZE, GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007427 if (!tnapi->tx_buffers)
7428 goto err_out;
7429
Matt Carlson4bae65c2010-11-24 08:31:52 +00007430 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
7431 TG3_TX_RING_BYTES,
7432 &tnapi->tx_desc_mapping,
7433 GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007434 if (!tnapi->tx_ring)
7435 goto err_out;
7436 }
7437
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007438 /*
7439 * When RSS is enabled, the status block format changes
7440 * slightly. The "rx_jumbo_consumer", "reserved",
7441 * and "rx_mini_consumer" members get mapped to the
7442 * other three rx return ring producer indexes.
7443 */
7444 switch (i) {
7445 default:
7446 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
7447 break;
7448 case 2:
7449 tnapi->rx_rcb_prod_idx = &sblk->rx_jumbo_consumer;
7450 break;
7451 case 3:
7452 tnapi->rx_rcb_prod_idx = &sblk->reserved;
7453 break;
7454 case 4:
7455 tnapi->rx_rcb_prod_idx = &sblk->rx_mini_consumer;
7456 break;
7457 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007458
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007459 /*
7460 * If multivector RSS is enabled, vector 0 does not handle
7461 * rx or tx interrupts. Don't allocate any resources for it.
7462 */
Joe Perches63c3a662011-04-26 08:12:10 +00007463 if (!i && tg3_flag(tp, ENABLE_RSS))
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007464 continue;
7465
Matt Carlson4bae65c2010-11-24 08:31:52 +00007466 tnapi->rx_rcb = dma_alloc_coherent(&tp->pdev->dev,
7467 TG3_RX_RCB_RING_BYTES(tp),
7468 &tnapi->rx_rcb_mapping,
7469 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007470 if (!tnapi->rx_rcb)
7471 goto err_out;
7472
7473 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007474 }
7475
Linus Torvalds1da177e2005-04-16 15:20:36 -07007476 return 0;
7477
7478err_out:
7479 tg3_free_consistent(tp);
7480 return -ENOMEM;
7481}
7482
7483#define MAX_WAIT_CNT 1000
7484
7485/* To stop a block, clear the enable bit and poll till it
7486 * clears. tp->lock is held.
7487 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007488static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007489{
7490 unsigned int i;
7491 u32 val;
7492
Joe Perches63c3a662011-04-26 08:12:10 +00007493 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007494 switch (ofs) {
7495 case RCVLSC_MODE:
7496 case DMAC_MODE:
7497 case MBFREE_MODE:
7498 case BUFMGR_MODE:
7499 case MEMARB_MODE:
7500 /* We can't enable/disable these bits of the
7501 * 5705/5750, just say success.
7502 */
7503 return 0;
7504
7505 default:
7506 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007508 }
7509
7510 val = tr32(ofs);
7511 val &= ~enable_bit;
7512 tw32_f(ofs, val);
7513
7514 for (i = 0; i < MAX_WAIT_CNT; i++) {
7515 udelay(100);
7516 val = tr32(ofs);
7517 if ((val & enable_bit) == 0)
7518 break;
7519 }
7520
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007521 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00007522 dev_err(&tp->pdev->dev,
7523 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
7524 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007525 return -ENODEV;
7526 }
7527
7528 return 0;
7529}
7530
7531/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007532static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007533{
7534 int i, err;
7535
7536 tg3_disable_ints(tp);
7537
7538 tp->rx_mode &= ~RX_MODE_ENABLE;
7539 tw32_f(MAC_RX_MODE, tp->rx_mode);
7540 udelay(10);
7541
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007542 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
7543 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
7544 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
7545 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
7546 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
7547 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007548
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007549 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
7550 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
7551 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
7552 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
7553 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
7554 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
7555 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007556
7557 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
7558 tw32_f(MAC_MODE, tp->mac_mode);
7559 udelay(40);
7560
7561 tp->tx_mode &= ~TX_MODE_ENABLE;
7562 tw32_f(MAC_TX_MODE, tp->tx_mode);
7563
7564 for (i = 0; i < MAX_WAIT_CNT; i++) {
7565 udelay(100);
7566 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
7567 break;
7568 }
7569 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00007570 dev_err(&tp->pdev->dev,
7571 "%s timed out, TX_MODE_ENABLE will not clear "
7572 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07007573 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007574 }
7575
Michael Chane6de8ad2005-05-05 14:42:41 -07007576 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007577 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
7578 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007579
7580 tw32(FTQ_RESET, 0xffffffff);
7581 tw32(FTQ_RESET, 0x00000000);
7582
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007583 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
7584 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007585
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007586 for (i = 0; i < tp->irq_cnt; i++) {
7587 struct tg3_napi *tnapi = &tp->napi[i];
7588 if (tnapi->hw_status)
7589 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007591 if (tp->hw_stats)
7592 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
7593
Linus Torvalds1da177e2005-04-16 15:20:36 -07007594 return err;
7595}
7596
Michael Chanee6a99b2007-07-18 21:49:10 -07007597/* Save PCI command register before chip reset */
7598static void tg3_save_pci_state(struct tg3 *tp)
7599{
Matt Carlson8a6eac92007-10-21 16:17:55 -07007600 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007601}
7602
7603/* Restore PCI state after chip reset */
7604static void tg3_restore_pci_state(struct tg3 *tp)
7605{
7606 u32 val;
7607
7608 /* Re-enable indirect register accesses. */
7609 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
7610 tp->misc_host_ctrl);
7611
7612 /* Set MAX PCI retry to zero. */
7613 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
7614 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007615 tg3_flag(tp, PCIX_MODE))
Michael Chanee6a99b2007-07-18 21:49:10 -07007616 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07007617 /* Allow reads and writes to the APE register and memory space. */
Joe Perches63c3a662011-04-26 08:12:10 +00007618 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07007619 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00007620 PCISTATE_ALLOW_APE_SHMEM_WR |
7621 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07007622 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
7623
Matt Carlson8a6eac92007-10-21 16:17:55 -07007624 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007625
Matt Carlson2c55a3d2011-11-28 09:41:04 +00007626 if (!tg3_flag(tp, PCI_EXPRESS)) {
7627 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
7628 tp->pci_cacheline_sz);
7629 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
7630 tp->pci_lat_timer);
Michael Chan114342f2007-10-15 02:12:26 -07007631 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08007632
Michael Chanee6a99b2007-07-18 21:49:10 -07007633 /* Make sure PCI-X relaxed ordering bit is clear. */
Joe Perches63c3a662011-04-26 08:12:10 +00007634 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07007635 u16 pcix_cmd;
7636
7637 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7638 &pcix_cmd);
7639 pcix_cmd &= ~PCI_X_CMD_ERO;
7640 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7641 pcix_cmd);
7642 }
Michael Chanee6a99b2007-07-18 21:49:10 -07007643
Joe Perches63c3a662011-04-26 08:12:10 +00007644 if (tg3_flag(tp, 5780_CLASS)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007645
7646 /* Chip reset on 5780 will reset MSI enable bit,
7647 * so need to restore it.
7648 */
Joe Perches63c3a662011-04-26 08:12:10 +00007649 if (tg3_flag(tp, USING_MSI)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007650 u16 ctrl;
7651
7652 pci_read_config_word(tp->pdev,
7653 tp->msi_cap + PCI_MSI_FLAGS,
7654 &ctrl);
7655 pci_write_config_word(tp->pdev,
7656 tp->msi_cap + PCI_MSI_FLAGS,
7657 ctrl | PCI_MSI_FLAGS_ENABLE);
7658 val = tr32(MSGINT_MODE);
7659 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
7660 }
7661 }
7662}
7663
Linus Torvalds1da177e2005-04-16 15:20:36 -07007664/* tp->lock is held. */
7665static int tg3_chip_reset(struct tg3 *tp)
7666{
7667 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07007668 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00007669 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007670
David S. Millerf49639e2006-06-09 11:58:36 -07007671 tg3_nvram_lock(tp);
7672
Matt Carlson77b483f2008-08-15 14:07:24 -07007673 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
7674
David S. Millerf49639e2006-06-09 11:58:36 -07007675 /* No matching tg3_nvram_unlock() after this because
7676 * chip reset below will undo the nvram lock.
7677 */
7678 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007679
Michael Chanee6a99b2007-07-18 21:49:10 -07007680 /* GRC_MISC_CFG core clock reset will clear the memory
7681 * enable bit in PCI register 4 and the MSI enable bit
7682 * on some chips, so we save relevant registers here.
7683 */
7684 tg3_save_pci_state(tp);
7685
Michael Chand9ab5ad12006-03-20 22:27:35 -08007686 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Joe Perches63c3a662011-04-26 08:12:10 +00007687 tg3_flag(tp, 5755_PLUS))
Michael Chand9ab5ad12006-03-20 22:27:35 -08007688 tw32(GRC_FASTBOOT_PC, 0);
7689
Linus Torvalds1da177e2005-04-16 15:20:36 -07007690 /*
7691 * We must avoid the readl() that normally takes place.
7692 * It locks machines, causes machine checks, and other
7693 * fun things. So, temporarily disable the 5701
7694 * hardware workaround, while we do the reset.
7695 */
Michael Chan1ee582d2005-08-09 20:16:46 -07007696 write_op = tp->write32;
7697 if (write_op == tg3_write_flush_reg32)
7698 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007699
Michael Chand18edcb2007-03-24 20:57:11 -07007700 /* Prevent the irq handler from reading or writing PCI registers
7701 * during chip reset when the memory enable bit in the PCI command
7702 * register may be cleared. The chip does not generate interrupt
7703 * at this time, but the irq handler may still be called due to irq
7704 * sharing or irqpoll.
7705 */
Joe Perches63c3a662011-04-26 08:12:10 +00007706 tg3_flag_set(tp, CHIP_RESETTING);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007707 for (i = 0; i < tp->irq_cnt; i++) {
7708 struct tg3_napi *tnapi = &tp->napi[i];
7709 if (tnapi->hw_status) {
7710 tnapi->hw_status->status = 0;
7711 tnapi->hw_status->status_tag = 0;
7712 }
7713 tnapi->last_tag = 0;
7714 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07007715 }
Michael Chand18edcb2007-03-24 20:57:11 -07007716 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00007717
7718 for (i = 0; i < tp->irq_cnt; i++)
7719 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07007720
Matt Carlson255ca312009-08-25 10:07:27 +00007721 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7722 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7723 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
7724 }
7725
Linus Torvalds1da177e2005-04-16 15:20:36 -07007726 /* do the reset */
7727 val = GRC_MISC_CFG_CORECLK_RESET;
7728
Joe Perches63c3a662011-04-26 08:12:10 +00007729 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson88075d92010-08-02 11:25:58 +00007730 /* Force PCIe 1.0a mode */
7731 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007732 !tg3_flag(tp, 57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00007733 tr32(TG3_PCIE_PHY_TSTCTL) ==
7734 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
7735 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
7736
Linus Torvalds1da177e2005-04-16 15:20:36 -07007737 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
7738 tw32(GRC_MISC_CFG, (1 << 29));
7739 val |= (1 << 29);
7740 }
7741 }
7742
Michael Chanb5d37722006-09-27 16:06:21 -07007743 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7744 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
7745 tw32(GRC_VCPU_EXT_CTRL,
7746 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
7747 }
7748
Matt Carlsonf37500d2010-08-02 11:25:59 +00007749 /* Manage gphy power for all CPMU absent PCIe devices. */
Joe Perches63c3a662011-04-26 08:12:10 +00007750 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007751 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00007752
Linus Torvalds1da177e2005-04-16 15:20:36 -07007753 tw32(GRC_MISC_CFG, val);
7754
Michael Chan1ee582d2005-08-09 20:16:46 -07007755 /* restore 5701 hardware bug workaround write method */
7756 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007757
7758 /* Unfortunately, we have to delay before the PCI read back.
7759 * Some 575X chips even will not respond to a PCI cfg access
7760 * when the reset command is given to the chip.
7761 *
7762 * How do these hardware designers expect things to work
7763 * properly if the PCI write is posted for a long period
7764 * of time? It is always necessary to have some method by
7765 * which a register read back can occur to push the write
7766 * out which does the reset.
7767 *
7768 * For most tg3 variants the trick below was working.
7769 * Ho hum...
7770 */
7771 udelay(120);
7772
7773 /* Flush PCI posted writes. The normal MMIO registers
7774 * are inaccessible at this time so this is the only
7775 * way to make this reliably (actually, this is no longer
7776 * the case, see above). I tried to use indirect
7777 * register read/write but this upset some 5701 variants.
7778 */
7779 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
7780
7781 udelay(120);
7782
Jon Mason708ebb3a2011-06-27 12:56:50 +00007783 if (tg3_flag(tp, PCI_EXPRESS) && pci_pcie_cap(tp->pdev)) {
Matt Carlsone7126992009-08-25 10:08:16 +00007784 u16 val16;
7785
Linus Torvalds1da177e2005-04-16 15:20:36 -07007786 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
7787 int i;
7788 u32 cfg_val;
7789
7790 /* Wait for link training to complete. */
7791 for (i = 0; i < 5000; i++)
7792 udelay(100);
7793
7794 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
7795 pci_write_config_dword(tp->pdev, 0xc4,
7796 cfg_val | (1 << 15));
7797 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007798
Matt Carlsone7126992009-08-25 10:08:16 +00007799 /* Clear the "no snoop" and "relaxed ordering" bits. */
7800 pci_read_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +00007801 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007802 &val16);
7803 val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
7804 PCI_EXP_DEVCTL_NOSNOOP_EN);
7805 /*
7806 * Older PCIe devices only support the 128 byte
7807 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007808 */
Joe Perches63c3a662011-04-26 08:12:10 +00007809 if (!tg3_flag(tp, CPMU_PRESENT))
Matt Carlsone7126992009-08-25 10:08:16 +00007810 val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007811 pci_write_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +00007812 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007813 val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007814
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007815 /* Clear error status */
7816 pci_write_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +00007817 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVSTA,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007818 PCI_EXP_DEVSTA_CED |
7819 PCI_EXP_DEVSTA_NFED |
7820 PCI_EXP_DEVSTA_FED |
7821 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007822 }
7823
Michael Chanee6a99b2007-07-18 21:49:10 -07007824 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007825
Joe Perches63c3a662011-04-26 08:12:10 +00007826 tg3_flag_clear(tp, CHIP_RESETTING);
7827 tg3_flag_clear(tp, ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07007828
Michael Chanee6a99b2007-07-18 21:49:10 -07007829 val = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00007830 if (tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07007831 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07007832 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007833
7834 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
7835 tg3_stop_fw(tp);
7836 tw32(0x5000, 0x400);
7837 }
7838
7839 tw32(GRC_MODE, tp->grc_mode);
7840
7841 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007842 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007843
7844 tw32(0xc4, val | (1 << 15));
7845 }
7846
7847 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
7848 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
7849 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
7850 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
7851 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
7852 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7853 }
7854
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007855 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00007856 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007857 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007858 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00007859 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007860 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007861 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007862 val = 0;
7863
7864 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007865 udelay(40);
7866
Matt Carlson77b483f2008-08-15 14:07:24 -07007867 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
7868
Michael Chan7a6f4362006-09-27 16:03:31 -07007869 err = tg3_poll_fw(tp);
7870 if (err)
7871 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007872
Matt Carlson0a9140c2009-08-28 12:27:50 +00007873 tg3_mdio_start(tp);
7874
Joe Perches63c3a662011-04-26 08:12:10 +00007875 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007876 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
7877 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007878 !tg3_flag(tp, 57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007879 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007880
7881 tw32(0x7c00, val | (1 << 25));
7882 }
7883
Matt Carlsond78b59f2011-04-05 14:22:46 +00007884 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
7885 val = tr32(TG3_CPMU_CLCK_ORIDE);
7886 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
7887 }
7888
Linus Torvalds1da177e2005-04-16 15:20:36 -07007889 /* Reprobe ASF enable state. */
Joe Perches63c3a662011-04-26 08:12:10 +00007890 tg3_flag_clear(tp, ENABLE_ASF);
7891 tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007892 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
7893 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
7894 u32 nic_cfg;
7895
7896 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
7897 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +00007898 tg3_flag_set(tp, ENABLE_ASF);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007899 tp->last_event_jiffies = jiffies;
Joe Perches63c3a662011-04-26 08:12:10 +00007900 if (tg3_flag(tp, 5750_PLUS))
7901 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007902 }
7903 }
7904
7905 return 0;
7906}
7907
7908/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07007909static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007910{
7911 int err;
7912
7913 tg3_stop_fw(tp);
7914
Michael Chan944d9802005-05-29 14:57:48 -07007915 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007916
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007917 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007918 err = tg3_chip_reset(tp);
7919
Matt Carlsondaba2a62009-04-20 06:58:52 +00007920 __tg3_set_mac_addr(tp, 0);
7921
Michael Chan944d9802005-05-29 14:57:48 -07007922 tg3_write_sig_legacy(tp, kind);
7923 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007924
7925 if (err)
7926 return err;
7927
7928 return 0;
7929}
7930
Linus Torvalds1da177e2005-04-16 15:20:36 -07007931static int tg3_set_mac_addr(struct net_device *dev, void *p)
7932{
7933 struct tg3 *tp = netdev_priv(dev);
7934 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07007935 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007936
Michael Chanf9804dd2005-09-27 12:13:10 -07007937 if (!is_valid_ether_addr(addr->sa_data))
7938 return -EINVAL;
7939
Linus Torvalds1da177e2005-04-16 15:20:36 -07007940 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7941
Michael Chane75f7c92006-03-20 21:33:26 -08007942 if (!netif_running(dev))
7943 return 0;
7944
Joe Perches63c3a662011-04-26 08:12:10 +00007945 if (tg3_flag(tp, ENABLE_ASF)) {
Michael Chan986e0ae2007-05-05 12:10:20 -07007946 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07007947
Michael Chan986e0ae2007-05-05 12:10:20 -07007948 addr0_high = tr32(MAC_ADDR_0_HIGH);
7949 addr0_low = tr32(MAC_ADDR_0_LOW);
7950 addr1_high = tr32(MAC_ADDR_1_HIGH);
7951 addr1_low = tr32(MAC_ADDR_1_LOW);
7952
7953 /* Skip MAC addr 1 if ASF is using it. */
7954 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
7955 !(addr1_high == 0 && addr1_low == 0))
7956 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07007957 }
Michael Chan986e0ae2007-05-05 12:10:20 -07007958 spin_lock_bh(&tp->lock);
7959 __tg3_set_mac_addr(tp, skip_mac_1);
7960 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007961
Michael Chanb9ec6c12006-07-25 16:37:27 -07007962 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007963}
7964
7965/* tp->lock is held. */
7966static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
7967 dma_addr_t mapping, u32 maxlen_flags,
7968 u32 nic_addr)
7969{
7970 tg3_write_mem(tp,
7971 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
7972 ((u64) mapping >> 32));
7973 tg3_write_mem(tp,
7974 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
7975 ((u64) mapping & 0xffffffff));
7976 tg3_write_mem(tp,
7977 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
7978 maxlen_flags);
7979
Joe Perches63c3a662011-04-26 08:12:10 +00007980 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007981 tg3_write_mem(tp,
7982 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
7983 nic_addr);
7984}
7985
7986static void __tg3_set_rx_mode(struct net_device *);
Michael Chand244c892005-07-05 14:42:33 -07007987static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07007988{
Matt Carlsonb6080e12009-09-01 13:12:00 +00007989 int i;
7990
Joe Perches63c3a662011-04-26 08:12:10 +00007991 if (!tg3_flag(tp, ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007992 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
7993 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
7994 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007995 } else {
7996 tw32(HOSTCC_TXCOL_TICKS, 0);
7997 tw32(HOSTCC_TXMAX_FRAMES, 0);
7998 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007999 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008000
Joe Perches63c3a662011-04-26 08:12:10 +00008001 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008002 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
8003 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
8004 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
8005 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00008006 tw32(HOSTCC_RXCOL_TICKS, 0);
8007 tw32(HOSTCC_RXMAX_FRAMES, 0);
8008 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07008009 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008010
Joe Perches63c3a662011-04-26 08:12:10 +00008011 if (!tg3_flag(tp, 5705_PLUS)) {
David S. Miller15f98502005-05-18 22:49:26 -07008012 u32 val = ec->stats_block_coalesce_usecs;
8013
Matt Carlsonb6080e12009-09-01 13:12:00 +00008014 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
8015 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
8016
David S. Miller15f98502005-05-18 22:49:26 -07008017 if (!netif_carrier_ok(tp->dev))
8018 val = 0;
8019
8020 tw32(HOSTCC_STAT_COAL_TICKS, val);
8021 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008022
8023 for (i = 0; i < tp->irq_cnt - 1; i++) {
8024 u32 reg;
8025
8026 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
8027 tw32(reg, ec->rx_coalesce_usecs);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008028 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
8029 tw32(reg, ec->rx_max_coalesced_frames);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008030 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
8031 tw32(reg, ec->rx_max_coalesced_frames_irq);
Matt Carlson19cfaec2009-12-03 08:36:20 +00008032
Joe Perches63c3a662011-04-26 08:12:10 +00008033 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008034 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
8035 tw32(reg, ec->tx_coalesce_usecs);
8036 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
8037 tw32(reg, ec->tx_max_coalesced_frames);
8038 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
8039 tw32(reg, ec->tx_max_coalesced_frames_irq);
8040 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008041 }
8042
8043 for (; i < tp->irq_max - 1; i++) {
8044 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008045 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008046 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00008047
Joe Perches63c3a662011-04-26 08:12:10 +00008048 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008049 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
8050 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
8051 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
8052 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008053 }
David S. Miller15f98502005-05-18 22:49:26 -07008054}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008055
8056/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00008057static void tg3_rings_reset(struct tg3 *tp)
8058{
8059 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008060 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008061 struct tg3_napi *tnapi = &tp->napi[0];
8062
8063 /* Disable all transmit rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008064 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008065 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Joe Perches63c3a662011-04-26 08:12:10 +00008066 else if (tg3_flag(tp, 5717_PLUS))
Matt Carlson3d377282010-10-14 10:37:39 +00008067 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
Matt Carlsonb703df62009-12-03 08:36:21 +00008068 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8069 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008070 else
8071 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8072
8073 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8074 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
8075 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
8076 BDINFO_FLAGS_DISABLED);
8077
8078
8079 /* Disable all receive return rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008080 if (tg3_flag(tp, 5717_PLUS))
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008081 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
Joe Perches63c3a662011-04-26 08:12:10 +00008082 else if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008083 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00008084 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
8085 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson2d31eca2009-09-01 12:53:31 +00008086 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
8087 else
8088 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8089
8090 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8091 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
8092 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
8093 BDINFO_FLAGS_DISABLED);
8094
8095 /* Disable interrupts */
8096 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008097 tp->napi[0].chk_msi_cnt = 0;
8098 tp->napi[0].last_rx_cons = 0;
8099 tp->napi[0].last_tx_cons = 0;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008100
8101 /* Zero mailbox registers. */
Joe Perches63c3a662011-04-26 08:12:10 +00008102 if (tg3_flag(tp, SUPPORT_MSIX)) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00008103 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008104 tp->napi[i].tx_prod = 0;
8105 tp->napi[i].tx_cons = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00008106 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008107 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008108 tw32_rx_mbox(tp->napi[i].consmbox, 0);
8109 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
Matt Carlson7f230732011-08-31 11:44:48 +00008110 tp->napi[i].chk_msi_cnt = 0;
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008111 tp->napi[i].last_rx_cons = 0;
8112 tp->napi[i].last_tx_cons = 0;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008113 }
Joe Perches63c3a662011-04-26 08:12:10 +00008114 if (!tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008115 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008116 } else {
8117 tp->napi[0].tx_prod = 0;
8118 tp->napi[0].tx_cons = 0;
8119 tw32_mailbox(tp->napi[0].prodmbox, 0);
8120 tw32_rx_mbox(tp->napi[0].consmbox, 0);
8121 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008122
8123 /* Make sure the NIC-based send BD rings are disabled. */
Joe Perches63c3a662011-04-26 08:12:10 +00008124 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson2d31eca2009-09-01 12:53:31 +00008125 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
8126 for (i = 0; i < 16; i++)
8127 tw32_tx_mbox(mbox + i * 8, 0);
8128 }
8129
8130 txrcb = NIC_SRAM_SEND_RCB;
8131 rxrcb = NIC_SRAM_RCV_RET_RCB;
8132
8133 /* Clear status block in ram. */
8134 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8135
8136 /* Set status block DMA address */
8137 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8138 ((u64) tnapi->status_mapping >> 32));
8139 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8140 ((u64) tnapi->status_mapping & 0xffffffff));
8141
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008142 if (tnapi->tx_ring) {
8143 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8144 (TG3_TX_RING_SIZE <<
8145 BDINFO_FLAGS_MAXLEN_SHIFT),
8146 NIC_SRAM_TX_BUFFER_DESC);
8147 txrcb += TG3_BDINFO_SIZE;
8148 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008149
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008150 if (tnapi->rx_rcb) {
8151 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008152 (tp->rx_ret_ring_mask + 1) <<
8153 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008154 rxrcb += TG3_BDINFO_SIZE;
8155 }
8156
8157 stblk = HOSTCC_STATBLCK_RING1;
8158
8159 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
8160 u64 mapping = (u64)tnapi->status_mapping;
8161 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
8162 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
8163
8164 /* Clear status block in ram. */
8165 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8166
Matt Carlson19cfaec2009-12-03 08:36:20 +00008167 if (tnapi->tx_ring) {
8168 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8169 (TG3_TX_RING_SIZE <<
8170 BDINFO_FLAGS_MAXLEN_SHIFT),
8171 NIC_SRAM_TX_BUFFER_DESC);
8172 txrcb += TG3_BDINFO_SIZE;
8173 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008174
8175 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008176 ((tp->rx_ret_ring_mask + 1) <<
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008177 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
8178
8179 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008180 rxrcb += TG3_BDINFO_SIZE;
8181 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008182}
8183
Matt Carlsoneb07a942011-04-20 07:57:36 +00008184static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
8185{
8186 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
8187
Joe Perches63c3a662011-04-26 08:12:10 +00008188 if (!tg3_flag(tp, 5750_PLUS) ||
8189 tg3_flag(tp, 5780_CLASS) ||
Matt Carlsoneb07a942011-04-20 07:57:36 +00008190 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
Matt Carlson513aa6e2011-11-21 15:01:18 +00008191 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
8192 tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008193 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
8194 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
8195 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787)
8196 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
8197 else
8198 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
8199
8200 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
8201 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
8202
8203 val = min(nic_rep_thresh, host_rep_thresh);
8204 tw32(RCVBDI_STD_THRESH, val);
8205
Joe Perches63c3a662011-04-26 08:12:10 +00008206 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008207 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
8208
Joe Perches63c3a662011-04-26 08:12:10 +00008209 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008210 return;
8211
Matt Carlson513aa6e2011-11-21 15:01:18 +00008212 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
Matt Carlsoneb07a942011-04-20 07:57:36 +00008213
8214 host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
8215
8216 val = min(bdcache_maxcnt / 2, host_rep_thresh);
8217 tw32(RCVBDI_JUMBO_THRESH, val);
8218
Joe Perches63c3a662011-04-26 08:12:10 +00008219 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008220 tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
8221}
8222
Matt Carlson2d31eca2009-09-01 12:53:31 +00008223/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008224static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008225{
8226 u32 val, rdmac_mode;
8227 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00008228 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008229
8230 tg3_disable_ints(tp);
8231
8232 tg3_stop_fw(tp);
8233
8234 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
8235
Joe Perches63c3a662011-04-26 08:12:10 +00008236 if (tg3_flag(tp, INIT_COMPLETE))
Michael Chane6de8ad2005-05-05 14:42:41 -07008237 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008238
Matt Carlson699c0192010-12-06 08:28:51 +00008239 /* Enable MAC control of LPI */
8240 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
8241 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL,
8242 TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
8243 TG3_CPMU_EEE_LNKIDL_UART_IDL);
8244
8245 tw32_f(TG3_CPMU_EEE_CTRL,
8246 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
8247
Matt Carlsona386b902010-12-06 08:28:53 +00008248 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
8249 TG3_CPMU_EEEMD_LPI_IN_TX |
8250 TG3_CPMU_EEEMD_LPI_IN_RX |
8251 TG3_CPMU_EEEMD_EEE_ENABLE;
8252
8253 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8254 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
8255
Joe Perches63c3a662011-04-26 08:12:10 +00008256 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsona386b902010-12-06 08:28:53 +00008257 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
8258
8259 tw32_f(TG3_CPMU_EEE_MODE, val);
8260
8261 tw32_f(TG3_CPMU_EEE_DBTMR1,
8262 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
8263 TG3_CPMU_DBTMR1_LNKIDLE_2047US);
8264
8265 tw32_f(TG3_CPMU_EEE_DBTMR2,
Matt Carlsond7f2ab22011-01-25 15:58:56 +00008266 TG3_CPMU_DBTMR2_APE_TX_2047US |
Matt Carlsona386b902010-12-06 08:28:53 +00008267 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
Matt Carlson699c0192010-12-06 08:28:51 +00008268 }
8269
Matt Carlson603f1172010-02-12 14:47:10 +00008270 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08008271 tg3_phy_reset(tp);
8272
Linus Torvalds1da177e2005-04-16 15:20:36 -07008273 err = tg3_chip_reset(tp);
8274 if (err)
8275 return err;
8276
8277 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
8278
Matt Carlsonbcb37f62008-11-03 16:52:09 -08008279 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008280 val = tr32(TG3_CPMU_CTRL);
8281 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
8282 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08008283
8284 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8285 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8286 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8287 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
8288
8289 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
8290 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
8291 val |= CPMU_LNK_AWARE_MACCLK_6_25;
8292 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
8293
8294 val = tr32(TG3_CPMU_HST_ACC);
8295 val &= ~CPMU_HST_ACC_MACCLK_MASK;
8296 val |= CPMU_HST_ACC_MACCLK_6_25;
8297 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07008298 }
8299
Matt Carlson33466d932009-04-20 06:57:41 +00008300 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
8301 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
8302 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
8303 PCIE_PWR_MGMT_L1_THRESH_4MS;
8304 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00008305
8306 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
8307 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
8308
8309 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d932009-04-20 06:57:41 +00008310
Matt Carlsonf40386c2009-11-02 14:24:02 +00008311 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
8312 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00008313 }
8314
Joe Perches63c3a662011-04-26 08:12:10 +00008315 if (tg3_flag(tp, L1PLLPD_EN)) {
Matt Carlson614b0592010-01-20 16:58:02 +00008316 u32 grc_mode = tr32(GRC_MODE);
8317
8318 /* Access the lower 1K of PL PCIE block registers. */
8319 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8320 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
8321
8322 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
8323 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
8324 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
8325
8326 tw32(GRC_MODE, grc_mode);
8327 }
8328
Matt Carlson5093eed2010-11-24 08:31:45 +00008329 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
8330 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
8331 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00008332
Matt Carlson5093eed2010-11-24 08:31:45 +00008333 /* Access the lower 1K of PL PCIE block registers. */
8334 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8335 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00008336
Matt Carlson5093eed2010-11-24 08:31:45 +00008337 val = tr32(TG3_PCIE_TLDLPL_PORT +
8338 TG3_PCIE_PL_LO_PHYCTL5);
8339 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
8340 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00008341
Matt Carlson5093eed2010-11-24 08:31:45 +00008342 tw32(GRC_MODE, grc_mode);
8343 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00008344
Matt Carlson1ff30a52011-05-19 12:12:46 +00008345 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_57765_AX) {
8346 u32 grc_mode = tr32(GRC_MODE);
8347
8348 /* Access the lower 1K of DL PCIE block registers. */
8349 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8350 tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
8351
8352 val = tr32(TG3_PCIE_TLDLPL_PORT +
8353 TG3_PCIE_DL_LO_FTSMAX);
8354 val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
8355 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
8356 val | TG3_PCIE_DL_LO_FTSMAX_VAL);
8357
8358 tw32(GRC_MODE, grc_mode);
8359 }
8360
Matt Carlsona977dbe2010-04-12 06:58:26 +00008361 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8362 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8363 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8364 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00008365 }
8366
Linus Torvalds1da177e2005-04-16 15:20:36 -07008367 /* This works around an issue with Athlon chipsets on
8368 * B3 tigon3 silicon. This bit has no effect on any
8369 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07008370 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008371 */
Joe Perches63c3a662011-04-26 08:12:10 +00008372 if (!tg3_flag(tp, CPMU_PRESENT)) {
8373 if (!tg3_flag(tp, PCI_EXPRESS))
Matt Carlson795d01c2007-10-07 23:28:17 -07008374 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
8375 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
8376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008377
8378 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008379 tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008380 val = tr32(TG3PCI_PCISTATE);
8381 val |= PCISTATE_RETRY_SAME_DMA;
8382 tw32(TG3PCI_PCISTATE, val);
8383 }
8384
Joe Perches63c3a662011-04-26 08:12:10 +00008385 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -07008386 /* Allow reads and writes to the
8387 * APE register and memory space.
8388 */
8389 val = tr32(TG3PCI_PCISTATE);
8390 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00008391 PCISTATE_ALLOW_APE_SHMEM_WR |
8392 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008393 tw32(TG3PCI_PCISTATE, val);
8394 }
8395
Linus Torvalds1da177e2005-04-16 15:20:36 -07008396 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
8397 /* Enable some hw fixes. */
8398 val = tr32(TG3PCI_MSI_DATA);
8399 val |= (1 << 26) | (1 << 28) | (1 << 29);
8400 tw32(TG3PCI_MSI_DATA, val);
8401 }
8402
8403 /* Descriptor ring init may make accesses to the
8404 * NIC SRAM area to setup the TX descriptors, so we
8405 * can only do this after the hardware has been
8406 * successfully reset.
8407 */
Michael Chan32d8c572006-07-25 16:38:29 -07008408 err = tg3_init_rings(tp);
8409 if (err)
8410 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008411
Joe Perches63c3a662011-04-26 08:12:10 +00008412 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008413 val = tr32(TG3PCI_DMA_RW_CTRL) &
8414 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Matt Carlson1a319022010-04-12 06:58:25 +00008415 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
8416 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlson0aebff42011-04-25 12:42:45 +00008417 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57765 &&
8418 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8419 val |= DMA_RWCTRL_TAGGED_STAT_WA;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008420 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
8421 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
8422 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008423 /* This value is determined during the probe time DMA
8424 * engine test, tg3_test_dma.
8425 */
8426 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
8427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008428
8429 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
8430 GRC_MODE_4X_NIC_SEND_RINGS |
8431 GRC_MODE_NO_TX_PHDR_CSUM |
8432 GRC_MODE_NO_RX_PHDR_CSUM);
8433 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07008434
8435 /* Pseudo-header checksum is done by hardware logic and not
8436 * the offload processers, so make the chip do the pseudo-
8437 * header checksums on receive. For transmit it is more
8438 * convenient to do the pseudo-header checksum in software
8439 * as Linux does that on transmit for us in all cases.
8440 */
8441 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008442
8443 tw32(GRC_MODE,
8444 tp->grc_mode |
8445 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
8446
8447 /* Setup the timer prescalar register. Clock is always 66Mhz. */
8448 val = tr32(GRC_MISC_CFG);
8449 val &= ~0xff;
8450 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
8451 tw32(GRC_MISC_CFG, val);
8452
8453 /* Initialize MBUF/DESC pool. */
Joe Perches63c3a662011-04-26 08:12:10 +00008454 if (tg3_flag(tp, 5750_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008455 /* Do nothing. */
8456 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
8457 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
8458 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
8459 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
8460 else
8461 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
8462 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
8463 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Joe Perches63c3a662011-04-26 08:12:10 +00008464 } else if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008465 int fw_len;
8466
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08008467 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008468 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
8469 tw32(BUFMGR_MB_POOL_ADDR,
8470 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
8471 tw32(BUFMGR_MB_POOL_SIZE,
8472 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
8473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008474
Michael Chan0f893dc2005-07-25 12:30:38 -07008475 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008476 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8477 tp->bufmgr_config.mbuf_read_dma_low_water);
8478 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8479 tp->bufmgr_config.mbuf_mac_rx_low_water);
8480 tw32(BUFMGR_MB_HIGH_WATER,
8481 tp->bufmgr_config.mbuf_high_water);
8482 } else {
8483 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8484 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
8485 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8486 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
8487 tw32(BUFMGR_MB_HIGH_WATER,
8488 tp->bufmgr_config.mbuf_high_water_jumbo);
8489 }
8490 tw32(BUFMGR_DMA_LOW_WATER,
8491 tp->bufmgr_config.dma_low_water);
8492 tw32(BUFMGR_DMA_HIGH_WATER,
8493 tp->bufmgr_config.dma_high_water);
8494
Matt Carlsond309a462010-09-30 10:34:31 +00008495 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
8496 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
8497 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
Matt Carlson4d958472011-04-20 07:57:35 +00008498 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
8499 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
8500 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0)
8501 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
Matt Carlsond309a462010-09-30 10:34:31 +00008502 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008503 for (i = 0; i < 2000; i++) {
8504 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
8505 break;
8506 udelay(10);
8507 }
8508 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00008509 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008510 return -ENODEV;
8511 }
8512
Matt Carlsoneb07a942011-04-20 07:57:36 +00008513 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
8514 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
Michael Chanb5d37722006-09-27 16:06:21 -07008515
Matt Carlsoneb07a942011-04-20 07:57:36 +00008516 tg3_setup_rxbd_thresholds(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008517
8518 /* Initialize TG3_BDINFO's at:
8519 * RCVDBDI_STD_BD: standard eth size rx ring
8520 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
8521 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
8522 *
8523 * like so:
8524 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
8525 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
8526 * ring attribute flags
8527 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
8528 *
8529 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
8530 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
8531 *
8532 * The size of each ring is fixed in the firmware, but the location is
8533 * configurable.
8534 */
8535 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008536 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008537 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008538 ((u64) tpr->rx_std_mapping & 0xffffffff));
Joe Perches63c3a662011-04-26 08:12:10 +00008539 if (!tg3_flag(tp, 5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +00008540 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
8541 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008542
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008543 /* Disable the mini ring */
Joe Perches63c3a662011-04-26 08:12:10 +00008544 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008545 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
8546 BDINFO_FLAGS_DISABLED);
8547
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008548 /* Program the jumbo buffer descriptor ring control
8549 * blocks on those devices that have them.
8550 */
Matt Carlsona0512942011-07-27 14:20:54 +00008551 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008552 (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008553
Joe Perches63c3a662011-04-26 08:12:10 +00008554 if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008555 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008556 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008557 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008558 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +00008559 val = TG3_RX_JMB_RING_SIZE(tp) <<
8560 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008561 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +00008562 val | BDINFO_FLAGS_USE_EXT_RECV);
Joe Perches63c3a662011-04-26 08:12:10 +00008563 if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
Matt Carlsona50d0792010-06-05 17:24:37 +00008564 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson87668d32009-11-13 13:03:34 +00008565 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
8566 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008567 } else {
8568 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
8569 BDINFO_FLAGS_DISABLED);
8570 }
8571
Joe Perches63c3a662011-04-26 08:12:10 +00008572 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonfa6b2aa2011-11-21 15:01:19 +00008573 val = TG3_RX_STD_RING_SIZE(tp);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008574 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
8575 val |= (TG3_RX_STD_DMA_SZ << 2);
8576 } else
Matt Carlson04380d42010-04-12 06:58:29 +00008577 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008578 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008579 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008580
8581 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008582
Matt Carlson411da642009-11-13 13:03:46 +00008583 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e662009-11-13 13:03:49 +00008584 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008585
Joe Perches63c3a662011-04-26 08:12:10 +00008586 tpr->rx_jmb_prod_idx =
8587 tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
Matt Carlson66711e662009-11-13 13:03:49 +00008588 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008589
Matt Carlson2d31eca2009-09-01 12:53:31 +00008590 tg3_rings_reset(tp);
8591
Linus Torvalds1da177e2005-04-16 15:20:36 -07008592 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07008593 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008594
8595 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00008596 tw32(MAC_RX_MTU_SIZE,
8597 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008598
8599 /* The slot time is changed by tg3_setup_phy if we
8600 * run at gigabit with half duplex.
8601 */
Matt Carlsonf2096f92011-04-05 14:22:48 +00008602 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
8603 (6 << TX_LENGTHS_IPG_SHIFT) |
8604 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
8605
8606 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8607 val |= tr32(MAC_TX_LENGTHS) &
8608 (TX_LENGTHS_JMB_FRM_LEN_MSK |
8609 TX_LENGTHS_CNT_DWN_VAL_MSK);
8610
8611 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008612
8613 /* Receive rules. */
8614 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
8615 tw32(RCVLPC_CONFIG, 0x0181);
8616
8617 /* Calculate RDMAC_MODE setting early, we need it to determine
8618 * the RCVLPC_STATE_ENABLE mask.
8619 */
8620 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
8621 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
8622 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
8623 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
8624 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07008625
Matt Carlsondeabaac2010-11-24 08:31:50 +00008626 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +00008627 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
8628
Matt Carlson57e69832008-05-25 23:48:31 -07008629 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08008630 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8631 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07008632 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
8633 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
8634 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
8635
Matt Carlsonc5908932011-03-09 16:58:25 +00008636 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8637 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00008638 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07008639 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008640 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
8641 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008642 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008643 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8644 }
8645 }
8646
Joe Perches63c3a662011-04-26 08:12:10 +00008647 if (tg3_flag(tp, PCI_EXPRESS))
Michael Chan85e94ce2005-04-21 17:05:28 -07008648 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8649
Joe Perches63c3a662011-04-26 08:12:10 +00008650 if (tg3_flag(tp, HW_TSO_1) ||
8651 tg3_flag(tp, HW_TSO_2) ||
8652 tg3_flag(tp, HW_TSO_3))
Matt Carlson027455a2008-12-21 20:19:30 -08008653 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
8654
Matt Carlson108a6c12011-05-19 12:12:47 +00008655 if (tg3_flag(tp, 57765_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +00008656 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson027455a2008-12-21 20:19:30 -08008657 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
8658 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008659
Matt Carlsonf2096f92011-04-05 14:22:48 +00008660 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8661 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
8662
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008663 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
8664 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
8665 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8666 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008667 tg3_flag(tp, 57765_PLUS)) {
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008668 val = tr32(TG3_RDMA_RSRVCTRL_REG);
Matt Carlsond78b59f2011-04-05 14:22:46 +00008669 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8670 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +00008671 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
8672 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
8673 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
8674 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
8675 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
8676 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +00008677 }
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008678 tw32(TG3_RDMA_RSRVCTRL_REG,
8679 val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
8680 }
8681
Matt Carlsond78b59f2011-04-05 14:22:46 +00008682 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8683 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsond309a462010-09-30 10:34:31 +00008684 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
8685 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val |
8686 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
8687 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
8688 }
8689
Linus Torvalds1da177e2005-04-16 15:20:36 -07008690 /* Receive/send statistics. */
Joe Perches63c3a662011-04-26 08:12:10 +00008691 if (tg3_flag(tp, 5750_PLUS)) {
Michael Chan16613942006-06-29 20:15:13 -07008692 val = tr32(RCVLPC_STATS_ENABLE);
8693 val &= ~RCVLPC_STATSENAB_DACK_FIX;
8694 tw32(RCVLPC_STATS_ENABLE, val);
8695 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008696 tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008697 val = tr32(RCVLPC_STATS_ENABLE);
8698 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
8699 tw32(RCVLPC_STATS_ENABLE, val);
8700 } else {
8701 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
8702 }
8703 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
8704 tw32(SNDDATAI_STATSENAB, 0xffffff);
8705 tw32(SNDDATAI_STATSCTRL,
8706 (SNDDATAI_SCTRL_ENABLE |
8707 SNDDATAI_SCTRL_FASTUPD));
8708
8709 /* Setup host coalescing engine. */
8710 tw32(HOSTCC_MODE, 0);
8711 for (i = 0; i < 2000; i++) {
8712 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
8713 break;
8714 udelay(10);
8715 }
8716
Michael Chand244c892005-07-05 14:42:33 -07008717 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008718
Joe Perches63c3a662011-04-26 08:12:10 +00008719 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008720 /* Status/statistics block address. See tg3_timer,
8721 * the tg3_periodic_fetch_stats call there, and
8722 * tg3_get_stats to see how this works for 5705/5750 chips.
8723 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008724 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8725 ((u64) tp->stats_mapping >> 32));
8726 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8727 ((u64) tp->stats_mapping & 0xffffffff));
8728 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008729
Linus Torvalds1da177e2005-04-16 15:20:36 -07008730 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008731
8732 /* Clear statistics and status block memory areas */
8733 for (i = NIC_SRAM_STATS_BLK;
8734 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
8735 i += sizeof(u32)) {
8736 tg3_write_mem(tp, i, 0);
8737 udelay(40);
8738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008739 }
8740
8741 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
8742
8743 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
8744 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008745 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008746 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
8747
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008748 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
8749 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -07008750 /* reset to prevent losing 1st rx packet intermittently */
8751 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8752 udelay(10);
8753 }
8754
Matt Carlson3bda1252008-08-15 14:08:22 -07008755 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Matt Carlson9e975cc2011-07-20 10:20:50 +00008756 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE |
8757 MAC_MODE_FHDE_ENABLE;
8758 if (tg3_flag(tp, ENABLE_APE))
8759 tp->mac_mode |= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Joe Perches63c3a662011-04-26 08:12:10 +00008760 if (!tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008761 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07008762 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
8763 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008764 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
8765 udelay(40);
8766
Michael Chan314fba32005-04-21 17:07:04 -07008767 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Joe Perches63c3a662011-04-26 08:12:10 +00008768 * If TG3_FLAG_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07008769 * register to preserve the GPIO settings for LOMs. The GPIOs,
8770 * whether used as inputs or outputs, are set by boot code after
8771 * reset.
8772 */
Joe Perches63c3a662011-04-26 08:12:10 +00008773 if (!tg3_flag(tp, IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07008774 u32 gpio_mask;
8775
Michael Chan9d26e212006-12-07 00:21:14 -08008776 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
8777 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
8778 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07008779
8780 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
8781 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
8782 GRC_LCLCTRL_GPIO_OUTPUT3;
8783
Michael Chanaf36e6b2006-03-23 01:28:06 -08008784 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
8785 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
8786
Gary Zambranoaaf84462007-05-05 11:51:45 -07008787 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07008788 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
8789
8790 /* GPIO1 must be driven high for eeprom write protect */
Joe Perches63c3a662011-04-26 08:12:10 +00008791 if (tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan9d26e212006-12-07 00:21:14 -08008792 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
8793 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07008794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008795 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8796 udelay(100);
8797
Joe Perches63c3a662011-04-26 08:12:10 +00008798 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008799 val = tr32(MSGINT_MODE);
8800 val |= MSGINT_MODE_MULTIVEC_EN | MSGINT_MODE_ENABLE;
Matt Carlson5b39de92011-08-31 11:44:50 +00008801 if (!tg3_flag(tp, 1SHOT_MSI))
8802 val |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008803 tw32(MSGINT_MODE, val);
8804 }
8805
Joe Perches63c3a662011-04-26 08:12:10 +00008806 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008807 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
8808 udelay(40);
8809 }
8810
8811 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
8812 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
8813 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
8814 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
8815 WDMAC_MODE_LNGREAD_ENAB);
8816
Matt Carlsonc5908932011-03-09 16:58:25 +00008817 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8818 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00008819 if (tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008820 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
8821 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
8822 /* nothing */
8823 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008824 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008825 val |= WDMAC_MODE_RX_ACCEL;
8826 }
8827 }
8828
Michael Chand9ab5ad12006-03-20 22:27:35 -08008829 /* Enable host coalescing bug fix */
Joe Perches63c3a662011-04-26 08:12:10 +00008830 if (tg3_flag(tp, 5755_PLUS))
Matt Carlsonf51f3562008-05-25 23:45:08 -07008831 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad12006-03-20 22:27:35 -08008832
Matt Carlson788a0352009-11-02 14:26:03 +00008833 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
8834 val |= WDMAC_MODE_BURST_ALL_DATA;
8835
Linus Torvalds1da177e2005-04-16 15:20:36 -07008836 tw32_f(WDMAC_MODE, val);
8837 udelay(40);
8838
Joe Perches63c3a662011-04-26 08:12:10 +00008839 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07008840 u16 pcix_cmd;
8841
8842 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8843 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008844 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07008845 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
8846 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008847 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07008848 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
8849 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008850 }
Matt Carlson9974a352007-10-07 23:27:28 -07008851 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8852 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008853 }
8854
8855 tw32_f(RDMAC_MODE, rdmac_mode);
8856 udelay(40);
8857
8858 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008859 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008860 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07008861
8862 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
8863 tw32(SNDDATAC_MODE,
8864 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
8865 else
8866 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
8867
Linus Torvalds1da177e2005-04-16 15:20:36 -07008868 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
8869 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008870 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00008871 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008872 val |= RCVDBDI_MODE_LRG_RING_SZ;
8873 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008874 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008875 if (tg3_flag(tp, HW_TSO_1) ||
8876 tg3_flag(tp, HW_TSO_2) ||
8877 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008878 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008879 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00008880 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008881 val |= SNDBDI_MODE_MULTI_TXQ_EN;
8882 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008883 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
8884
8885 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
8886 err = tg3_load_5701_a0_firmware_fix(tp);
8887 if (err)
8888 return err;
8889 }
8890
Joe Perches63c3a662011-04-26 08:12:10 +00008891 if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008892 err = tg3_load_tso_firmware(tp);
8893 if (err)
8894 return err;
8895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008896
8897 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +00008898
Joe Perches63c3a662011-04-26 08:12:10 +00008899 if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsonb1d05212010-06-05 17:24:31 +00008900 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
8901 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +00008902
8903 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
8904 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
8905 tp->tx_mode &= ~val;
8906 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
8907 }
8908
Linus Torvalds1da177e2005-04-16 15:20:36 -07008909 tw32_f(MAC_TX_MODE, tp->tx_mode);
8910 udelay(100);
8911
Joe Perches63c3a662011-04-26 08:12:10 +00008912 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson9d53fa12011-07-20 10:20:54 +00008913 int i = 0;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008914 u32 reg = MAC_RSS_INDIR_TBL_0;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008915
Matt Carlson9d53fa12011-07-20 10:20:54 +00008916 if (tp->irq_cnt == 2) {
8917 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i += 8) {
8918 tw32(reg, 0x0);
8919 reg += 4;
8920 }
8921 } else {
8922 u32 val;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008923
Matt Carlson9d53fa12011-07-20 10:20:54 +00008924 while (i < TG3_RSS_INDIR_TBL_SIZE) {
8925 val = i % (tp->irq_cnt - 1);
8926 i++;
8927 for (; i % 8; i++) {
8928 val <<= 4;
8929 val |= (i % (tp->irq_cnt - 1));
8930 }
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008931 tw32(reg, val);
8932 reg += 4;
8933 }
8934 }
8935
8936 /* Setup the "secret" hash key. */
8937 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
8938 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
8939 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
8940 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
8941 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
8942 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
8943 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
8944 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
8945 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
8946 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
8947 }
8948
Linus Torvalds1da177e2005-04-16 15:20:36 -07008949 tp->rx_mode = RX_MODE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00008950 if (tg3_flag(tp, 5755_PLUS))
Michael Chanaf36e6b2006-03-23 01:28:06 -08008951 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
8952
Joe Perches63c3a662011-04-26 08:12:10 +00008953 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008954 tp->rx_mode |= RX_MODE_RSS_ENABLE |
8955 RX_MODE_RSS_ITBL_HASH_BITS_7 |
8956 RX_MODE_RSS_IPV6_HASH_EN |
8957 RX_MODE_RSS_TCP_IPV6_HASH_EN |
8958 RX_MODE_RSS_IPV4_HASH_EN |
8959 RX_MODE_RSS_TCP_IPV4_HASH_EN;
8960
Linus Torvalds1da177e2005-04-16 15:20:36 -07008961 tw32_f(MAC_RX_MODE, tp->rx_mode);
8962 udelay(10);
8963
Linus Torvalds1da177e2005-04-16 15:20:36 -07008964 tw32(MAC_LED_CTRL, tp->led_ctrl);
8965
8966 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008967 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008968 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8969 udelay(10);
8970 }
8971 tw32_f(MAC_RX_MODE, tp->rx_mode);
8972 udelay(10);
8973
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008974 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008975 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008976 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008977 /* Set drive transmission level to 1.2V */
8978 /* only if the signal pre-emphasis bit is not set */
8979 val = tr32(MAC_SERDES_CFG);
8980 val &= 0xfffff000;
8981 val |= 0x880;
8982 tw32(MAC_SERDES_CFG, val);
8983 }
8984 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
8985 tw32(MAC_SERDES_CFG, 0x616000);
8986 }
8987
8988 /* Prevent chip from dropping frames when flow control
8989 * is enabled.
8990 */
Matt Carlson666bc832010-01-20 16:58:03 +00008991 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8992 val = 1;
8993 else
8994 val = 2;
8995 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008996
8997 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008998 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008999 /* Use hardware link auto-negotiation */
Joe Perches63c3a662011-04-26 08:12:10 +00009000 tg3_flag_set(tp, HW_AUTONEG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009001 }
9002
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009003 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +00009004 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08009005 u32 tmp;
9006
9007 tmp = tr32(SERDES_RX_CTRL);
9008 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
9009 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
9010 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
9011 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
9012 }
9013
Joe Perches63c3a662011-04-26 08:12:10 +00009014 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson80096062010-08-02 11:26:06 +00009015 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
9016 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07009017 tp->link_config.speed = tp->link_config.orig_speed;
9018 tp->link_config.duplex = tp->link_config.orig_duplex;
9019 tp->link_config.autoneg = tp->link_config.orig_autoneg;
9020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009021
Matt Carlsondd477002008-05-25 23:45:58 -07009022 err = tg3_setup_phy(tp, 0);
9023 if (err)
9024 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009025
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009026 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
9027 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07009028 u32 tmp;
9029
9030 /* Clear CRC stats. */
9031 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
9032 tg3_writephy(tp, MII_TG3_TEST1,
9033 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009034 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -07009035 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009036 }
9037 }
9038
9039 __tg3_set_rx_mode(tp->dev);
9040
9041 /* Initialize receive rules. */
9042 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
9043 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
9044 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
9045 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
9046
Joe Perches63c3a662011-04-26 08:12:10 +00009047 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009048 limit = 8;
9049 else
9050 limit = 16;
Joe Perches63c3a662011-04-26 08:12:10 +00009051 if (tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009052 limit -= 4;
9053 switch (limit) {
9054 case 16:
9055 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
9056 case 15:
9057 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
9058 case 14:
9059 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
9060 case 13:
9061 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
9062 case 12:
9063 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
9064 case 11:
9065 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
9066 case 10:
9067 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
9068 case 9:
9069 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
9070 case 8:
9071 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
9072 case 7:
9073 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
9074 case 6:
9075 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
9076 case 5:
9077 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
9078 case 4:
9079 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
9080 case 3:
9081 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
9082 case 2:
9083 case 1:
9084
9085 default:
9086 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07009087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009088
Joe Perches63c3a662011-04-26 08:12:10 +00009089 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson9ce768e2007-10-11 19:49:11 -07009090 /* Write our heartbeat update interval to APE. */
9091 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
9092 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07009093
Linus Torvalds1da177e2005-04-16 15:20:36 -07009094 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
9095
Linus Torvalds1da177e2005-04-16 15:20:36 -07009096 return 0;
9097}
9098
9099/* Called at device open time to get the chip ready for
9100 * packet processing. Invoked with tp->lock held.
9101 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009102static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009103{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009104 tg3_switch_clocks(tp);
9105
9106 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
9107
Matt Carlson2f751b62008-08-04 23:17:34 -07009108 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009109}
9110
9111#define TG3_STAT_ADD32(PSTAT, REG) \
9112do { u32 __val = tr32(REG); \
9113 (PSTAT)->low += __val; \
9114 if ((PSTAT)->low < __val) \
9115 (PSTAT)->high += 1; \
9116} while (0)
9117
9118static void tg3_periodic_fetch_stats(struct tg3 *tp)
9119{
9120 struct tg3_hw_stats *sp = tp->hw_stats;
9121
9122 if (!netif_carrier_ok(tp->dev))
9123 return;
9124
9125 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
9126 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
9127 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
9128 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
9129 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
9130 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
9131 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
9132 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
9133 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
9134 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
9135 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
9136 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
9137 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
9138
9139 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
9140 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
9141 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
9142 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
9143 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
9144 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
9145 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
9146 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
9147 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
9148 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
9149 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
9150 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
9151 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
9152 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07009153
9154 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
Matt Carlson310050f2011-05-19 12:12:55 +00009155 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
9156 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0 &&
9157 tp->pci_chip_rev_id != CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +00009158 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
9159 } else {
9160 u32 val = tr32(HOSTCC_FLOW_ATTN);
9161 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
9162 if (val) {
9163 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
9164 sp->rx_discards.low += val;
9165 if (sp->rx_discards.low < val)
9166 sp->rx_discards.high += 1;
9167 }
9168 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
9169 }
Michael Chan463d3052006-05-22 16:36:27 -07009170 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009171}
9172
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009173static void tg3_chk_missed_msi(struct tg3 *tp)
9174{
9175 u32 i;
9176
9177 for (i = 0; i < tp->irq_cnt; i++) {
9178 struct tg3_napi *tnapi = &tp->napi[i];
9179
9180 if (tg3_has_work(tnapi)) {
9181 if (tnapi->last_rx_cons == tnapi->rx_rcb_ptr &&
9182 tnapi->last_tx_cons == tnapi->tx_cons) {
9183 if (tnapi->chk_msi_cnt < 1) {
9184 tnapi->chk_msi_cnt++;
9185 return;
9186 }
Matt Carlson7f230732011-08-31 11:44:48 +00009187 tg3_msi(0, tnapi);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009188 }
9189 }
9190 tnapi->chk_msi_cnt = 0;
9191 tnapi->last_rx_cons = tnapi->rx_rcb_ptr;
9192 tnapi->last_tx_cons = tnapi->tx_cons;
9193 }
9194}
9195
Linus Torvalds1da177e2005-04-16 15:20:36 -07009196static void tg3_timer(unsigned long __opaque)
9197{
9198 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009199
Matt Carlson5b190622011-11-04 09:15:04 +00009200 if (tp->irq_sync || tg3_flag(tp, RESET_TASK_PENDING))
Michael Chanf475f162006-03-27 23:20:14 -08009201 goto restart_timer;
9202
David S. Millerf47c11e2005-06-24 20:18:35 -07009203 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009204
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009205 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
9206 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
9207 tg3_chk_missed_msi(tp);
9208
Joe Perches63c3a662011-04-26 08:12:10 +00009209 if (!tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -07009210 /* All of this garbage is because when using non-tagged
9211 * IRQ status the mailbox/status_block protocol the chip
9212 * uses with the cpu is race prone.
9213 */
Matt Carlson898a56f2009-08-28 14:02:40 +00009214 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -07009215 tw32(GRC_LOCAL_CTRL,
9216 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
9217 } else {
9218 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00009219 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -07009220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009221
David S. Millerfac9b832005-05-18 22:46:34 -07009222 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
David S. Millerf47c11e2005-06-24 20:18:35 -07009223 spin_unlock(&tp->lock);
Matt Carlsondb219972011-11-04 09:15:03 +00009224 tg3_reset_task_schedule(tp);
Matt Carlson5b190622011-11-04 09:15:04 +00009225 goto restart_timer;
David S. Millerfac9b832005-05-18 22:46:34 -07009226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009227 }
9228
Linus Torvalds1da177e2005-04-16 15:20:36 -07009229 /* This part only runs once per second. */
9230 if (!--tp->timer_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00009231 if (tg3_flag(tp, 5705_PLUS))
David S. Millerfac9b832005-05-18 22:46:34 -07009232 tg3_periodic_fetch_stats(tp);
9233
Matt Carlsonb0c59432011-05-19 12:12:48 +00009234 if (tp->setlpicnt && !--tp->setlpicnt)
9235 tg3_phy_eee_enable(tp);
Matt Carlson52b02d02010-10-14 10:37:41 +00009236
Joe Perches63c3a662011-04-26 08:12:10 +00009237 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009238 u32 mac_stat;
9239 int phy_event;
9240
9241 mac_stat = tr32(MAC_STATUS);
9242
9243 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009244 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009245 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
9246 phy_event = 1;
9247 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
9248 phy_event = 1;
9249
9250 if (phy_event)
9251 tg3_setup_phy(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +00009252 } else if (tg3_flag(tp, POLL_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009253 u32 mac_stat = tr32(MAC_STATUS);
9254 int need_setup = 0;
9255
9256 if (netif_carrier_ok(tp->dev) &&
9257 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
9258 need_setup = 1;
9259 }
Matt Carlsonbe98da62010-07-11 09:31:46 +00009260 if (!netif_carrier_ok(tp->dev) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009261 (mac_stat & (MAC_STATUS_PCS_SYNCED |
9262 MAC_STATUS_SIGNAL_DET))) {
9263 need_setup = 1;
9264 }
9265 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07009266 if (!tp->serdes_counter) {
9267 tw32_f(MAC_MODE,
9268 (tp->mac_mode &
9269 ~MAC_MODE_PORT_MODE_MASK));
9270 udelay(40);
9271 tw32_f(MAC_MODE, tp->mac_mode);
9272 udelay(40);
9273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009274 tg3_setup_phy(tp, 0);
9275 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009276 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009277 tg3_flag(tp, 5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -07009278 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +00009279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009280
9281 tp->timer_counter = tp->timer_multiplier;
9282 }
9283
Michael Chan130b8e42006-09-27 16:00:40 -07009284 /* Heartbeat is only sent once every 2 seconds.
9285 *
9286 * The heartbeat is to tell the ASF firmware that the host
9287 * driver is still alive. In the event that the OS crashes,
9288 * ASF needs to reset the hardware to free up the FIFO space
9289 * that may be filled with rx packets destined for the host.
9290 * If the FIFO is full, ASF will no longer function properly.
9291 *
9292 * Unintended resets have been reported on real time kernels
9293 * where the timer doesn't run on time. Netpoll will also have
9294 * same problem.
9295 *
9296 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
9297 * to check the ring condition when the heartbeat is expiring
9298 * before doing the reset. This will prevent most unintended
9299 * resets.
9300 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009301 if (!--tp->asf_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00009302 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07009303 tg3_wait_for_event_ack(tp);
9304
Michael Chanbbadf502006-04-06 21:46:34 -07009305 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07009306 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07009307 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +00009308 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
9309 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -07009310
9311 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009312 }
9313 tp->asf_counter = tp->asf_multiplier;
9314 }
9315
David S. Millerf47c11e2005-06-24 20:18:35 -07009316 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009317
Michael Chanf475f162006-03-27 23:20:14 -08009318restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07009319 tp->timer.expires = jiffies + tp->timer_offset;
9320 add_timer(&tp->timer);
9321}
9322
Matt Carlson4f125f42009-09-01 12:55:02 +00009323static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -08009324{
David Howells7d12e782006-10-05 14:55:46 +01009325 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009326 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +00009327 char *name;
9328 struct tg3_napi *tnapi = &tp->napi[irq_num];
9329
9330 if (tp->irq_cnt == 1)
9331 name = tp->dev->name;
9332 else {
9333 name = &tnapi->irq_lbl[0];
9334 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
9335 name[IFNAMSIZ-1] = 0;
9336 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009337
Joe Perches63c3a662011-04-26 08:12:10 +00009338 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Michael Chanfcfa0a32006-03-20 22:28:41 -08009339 fn = tg3_msi;
Joe Perches63c3a662011-04-26 08:12:10 +00009340 if (tg3_flag(tp, 1SHOT_MSI))
Michael Chanfcfa0a32006-03-20 22:28:41 -08009341 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009342 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009343 } else {
9344 fn = tg3_interrupt;
Joe Perches63c3a662011-04-26 08:12:10 +00009345 if (tg3_flag(tp, TAGGED_STATUS))
Michael Chanfcfa0a32006-03-20 22:28:41 -08009346 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009347 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009348 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009349
9350 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009351}
9352
Michael Chan79381092005-04-21 17:13:59 -07009353static int tg3_test_interrupt(struct tg3 *tp)
9354{
Matt Carlson09943a12009-08-28 14:01:57 +00009355 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -07009356 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -07009357 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009358 u32 val;
Michael Chan79381092005-04-21 17:13:59 -07009359
Michael Chand4bc3922005-05-29 14:59:20 -07009360 if (!netif_running(dev))
9361 return -ENODEV;
9362
Michael Chan79381092005-04-21 17:13:59 -07009363 tg3_disable_ints(tp);
9364
Matt Carlson4f125f42009-09-01 12:55:02 +00009365 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009366
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009367 /*
9368 * Turn off MSI one shot mode. Otherwise this test has no
9369 * observable way to know whether the interrupt was delivered.
9370 */
Matt Carlson3aa1cdf2011-07-20 10:20:55 +00009371 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009372 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
9373 tw32(MSGINT_MODE, val);
9374 }
9375
Matt Carlson4f125f42009-09-01 12:55:02 +00009376 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Matt Carlson09943a12009-08-28 14:01:57 +00009377 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009378 if (err)
9379 return err;
9380
Matt Carlson898a56f2009-08-28 14:02:40 +00009381 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -07009382 tg3_enable_ints(tp);
9383
9384 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00009385 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -07009386
9387 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -07009388 u32 int_mbox, misc_host_ctrl;
9389
Matt Carlson898a56f2009-08-28 14:02:40 +00009390 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -07009391 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
9392
9393 if ((int_mbox != 0) ||
9394 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
9395 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -07009396 break;
Michael Chanb16250e2006-09-27 16:10:14 -07009397 }
9398
Matt Carlson3aa1cdf2011-07-20 10:20:55 +00009399 if (tg3_flag(tp, 57765_PLUS) &&
9400 tnapi->hw_status->status_tag != tnapi->last_tag)
9401 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
9402
Michael Chan79381092005-04-21 17:13:59 -07009403 msleep(10);
9404 }
9405
9406 tg3_disable_ints(tp);
9407
Matt Carlson4f125f42009-09-01 12:55:02 +00009408 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009409
Matt Carlson4f125f42009-09-01 12:55:02 +00009410 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009411
9412 if (err)
9413 return err;
9414
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009415 if (intr_ok) {
9416 /* Reenable MSI one shot mode. */
Matt Carlson5b39de92011-08-31 11:44:50 +00009417 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, 1SHOT_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009418 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
9419 tw32(MSGINT_MODE, val);
9420 }
Michael Chan79381092005-04-21 17:13:59 -07009421 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009422 }
Michael Chan79381092005-04-21 17:13:59 -07009423
9424 return -EIO;
9425}
9426
9427/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
9428 * successfully restored
9429 */
9430static int tg3_test_msi(struct tg3 *tp)
9431{
Michael Chan79381092005-04-21 17:13:59 -07009432 int err;
9433 u16 pci_cmd;
9434
Joe Perches63c3a662011-04-26 08:12:10 +00009435 if (!tg3_flag(tp, USING_MSI))
Michael Chan79381092005-04-21 17:13:59 -07009436 return 0;
9437
9438 /* Turn off SERR reporting in case MSI terminates with Master
9439 * Abort.
9440 */
9441 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
9442 pci_write_config_word(tp->pdev, PCI_COMMAND,
9443 pci_cmd & ~PCI_COMMAND_SERR);
9444
9445 err = tg3_test_interrupt(tp);
9446
9447 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
9448
9449 if (!err)
9450 return 0;
9451
9452 /* other failures */
9453 if (err != -EIO)
9454 return err;
9455
9456 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009457 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
9458 "to INTx mode. Please report this failure to the PCI "
9459 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -07009460
Matt Carlson4f125f42009-09-01 12:55:02 +00009461 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +00009462
Michael Chan79381092005-04-21 17:13:59 -07009463 pci_disable_msi(tp->pdev);
9464
Joe Perches63c3a662011-04-26 08:12:10 +00009465 tg3_flag_clear(tp, USING_MSI);
Andre Detschdc8bf1b2010-04-26 07:27:07 +00009466 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -07009467
Matt Carlson4f125f42009-09-01 12:55:02 +00009468 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009469 if (err)
9470 return err;
9471
9472 /* Need to reset the chip because the MSI cycle may have terminated
9473 * with Master Abort.
9474 */
David S. Millerf47c11e2005-06-24 20:18:35 -07009475 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009476
Michael Chan944d9802005-05-29 14:57:48 -07009477 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009478 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009479
David S. Millerf47c11e2005-06-24 20:18:35 -07009480 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009481
9482 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +00009483 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -07009484
9485 return err;
9486}
9487
Matt Carlson9e9fd122009-01-19 16:57:45 -08009488static int tg3_request_firmware(struct tg3 *tp)
9489{
9490 const __be32 *fw_data;
9491
9492 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009493 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
9494 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009495 return -ENOENT;
9496 }
9497
9498 fw_data = (void *)tp->fw->data;
9499
9500 /* Firmware blob starts with version numbers, followed by
9501 * start address and _full_ length including BSS sections
9502 * (which must be longer than the actual data, of course
9503 */
9504
9505 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
9506 if (tp->fw_len < (tp->fw->size - 12)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009507 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
9508 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009509 release_firmware(tp->fw);
9510 tp->fw = NULL;
9511 return -EINVAL;
9512 }
9513
9514 /* We no longer need firmware; we have it. */
9515 tp->fw_needed = NULL;
9516 return 0;
9517}
9518
Matt Carlson679563f2009-09-01 12:55:46 +00009519static bool tg3_enable_msix(struct tg3 *tp)
9520{
9521 int i, rc, cpus = num_online_cpus();
9522 struct msix_entry msix_ent[tp->irq_max];
9523
9524 if (cpus == 1)
9525 /* Just fallback to the simpler MSI mode. */
9526 return false;
9527
9528 /*
9529 * We want as many rx rings enabled as there are cpus.
9530 * The first MSIX vector only deals with link interrupts, etc,
9531 * so we add one to the number of vectors we are requesting.
9532 */
9533 tp->irq_cnt = min_t(unsigned, cpus + 1, tp->irq_max);
9534
9535 for (i = 0; i < tp->irq_max; i++) {
9536 msix_ent[i].entry = i;
9537 msix_ent[i].vector = 0;
9538 }
9539
9540 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +00009541 if (rc < 0) {
9542 return false;
9543 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +00009544 if (pci_enable_msix(tp->pdev, msix_ent, rc))
9545 return false;
Joe Perches05dbe002010-02-17 19:44:19 +00009546 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
9547 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +00009548 tp->irq_cnt = rc;
9549 }
9550
9551 for (i = 0; i < tp->irq_max; i++)
9552 tp->napi[i].irq_vec = msix_ent[i].vector;
9553
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009554 netif_set_real_num_tx_queues(tp->dev, 1);
9555 rc = tp->irq_cnt > 1 ? tp->irq_cnt - 1 : 1;
9556 if (netif_set_real_num_rx_queues(tp->dev, rc)) {
9557 pci_disable_msix(tp->pdev);
9558 return false;
9559 }
Matt Carlsonb92b9042010-11-24 08:31:51 +00009560
9561 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +00009562 tg3_flag_set(tp, ENABLE_RSS);
Matt Carlsond78b59f2011-04-05 14:22:46 +00009563
9564 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
9565 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Joe Perches63c3a662011-04-26 08:12:10 +00009566 tg3_flag_set(tp, ENABLE_TSS);
Matt Carlsonb92b9042010-11-24 08:31:51 +00009567 netif_set_real_num_tx_queues(tp->dev, tp->irq_cnt - 1);
9568 }
9569 }
Matt Carlson2430b032010-06-05 17:24:34 +00009570
Matt Carlson679563f2009-09-01 12:55:46 +00009571 return true;
9572}
9573
Matt Carlson07b01732009-08-28 14:01:15 +00009574static void tg3_ints_init(struct tg3 *tp)
9575{
Joe Perches63c3a662011-04-26 08:12:10 +00009576 if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
9577 !tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +00009578 /* All MSI supporting chips should support tagged
9579 * status. Assert that this is the case.
9580 */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009581 netdev_warn(tp->dev,
9582 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +00009583 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +00009584 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009585
Joe Perches63c3a662011-04-26 08:12:10 +00009586 if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
9587 tg3_flag_set(tp, USING_MSIX);
9588 else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
9589 tg3_flag_set(tp, USING_MSI);
Matt Carlson679563f2009-09-01 12:55:46 +00009590
Joe Perches63c3a662011-04-26 08:12:10 +00009591 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009592 u32 msi_mode = tr32(MSGINT_MODE);
Joe Perches63c3a662011-04-26 08:12:10 +00009593 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009594 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +00009595 if (!tg3_flag(tp, 1SHOT_MSI))
9596 msi_mode |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlson679563f2009-09-01 12:55:46 +00009597 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
9598 }
9599defcfg:
Joe Perches63c3a662011-04-26 08:12:10 +00009600 if (!tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009601 tp->irq_cnt = 1;
9602 tp->napi[0].irq_vec = tp->pdev->irq;
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009603 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -07009604 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +00009605 }
Matt Carlson07b01732009-08-28 14:01:15 +00009606}
9607
9608static void tg3_ints_fini(struct tg3 *tp)
9609{
Joe Perches63c3a662011-04-26 08:12:10 +00009610 if (tg3_flag(tp, USING_MSIX))
Matt Carlson679563f2009-09-01 12:55:46 +00009611 pci_disable_msix(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009612 else if (tg3_flag(tp, USING_MSI))
Matt Carlson679563f2009-09-01 12:55:46 +00009613 pci_disable_msi(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009614 tg3_flag_clear(tp, USING_MSI);
9615 tg3_flag_clear(tp, USING_MSIX);
9616 tg3_flag_clear(tp, ENABLE_RSS);
9617 tg3_flag_clear(tp, ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +00009618}
9619
Linus Torvalds1da177e2005-04-16 15:20:36 -07009620static int tg3_open(struct net_device *dev)
9621{
9622 struct tg3 *tp = netdev_priv(dev);
Matt Carlson4f125f42009-09-01 12:55:02 +00009623 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009624
Matt Carlson9e9fd122009-01-19 16:57:45 -08009625 if (tp->fw_needed) {
9626 err = tg3_request_firmware(tp);
9627 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
9628 if (err)
9629 return err;
9630 } else if (err) {
Joe Perches05dbe002010-02-17 19:44:19 +00009631 netdev_warn(tp->dev, "TSO capability disabled\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009632 tg3_flag_clear(tp, TSO_CAPABLE);
9633 } else if (!tg3_flag(tp, TSO_CAPABLE)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009634 netdev_notice(tp->dev, "TSO capability restored\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009635 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009636 }
9637 }
9638
Michael Chanc49a1562006-12-17 17:07:29 -08009639 netif_carrier_off(tp->dev);
9640
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009641 err = tg3_power_up(tp);
Matt Carlson2f751b62008-08-04 23:17:34 -07009642 if (err)
Michael Chanbc1c7562006-03-20 17:48:03 -08009643 return err;
Matt Carlson2f751b62008-08-04 23:17:34 -07009644
9645 tg3_full_lock(tp, 0);
Michael Chanbc1c7562006-03-20 17:48:03 -08009646
Linus Torvalds1da177e2005-04-16 15:20:36 -07009647 tg3_disable_ints(tp);
Joe Perches63c3a662011-04-26 08:12:10 +00009648 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009649
David S. Millerf47c11e2005-06-24 20:18:35 -07009650 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009651
Matt Carlson679563f2009-09-01 12:55:46 +00009652 /*
9653 * Setup interrupts first so we know how
9654 * many NAPI resources to allocate
9655 */
9656 tg3_ints_init(tp);
9657
Linus Torvalds1da177e2005-04-16 15:20:36 -07009658 /* The placement of this call is tied
9659 * to the setup and use of Host TX descriptors.
9660 */
9661 err = tg3_alloc_consistent(tp);
9662 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009663 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009664
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009665 tg3_napi_init(tp);
9666
Matt Carlsonfed97812009-09-01 13:10:19 +00009667 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07009668
Matt Carlson4f125f42009-09-01 12:55:02 +00009669 for (i = 0; i < tp->irq_cnt; i++) {
9670 struct tg3_napi *tnapi = &tp->napi[i];
9671 err = tg3_request_irq(tp, i);
9672 if (err) {
Matt Carlson5bc09182011-11-04 09:15:01 +00009673 for (i--; i >= 0; i--) {
9674 tnapi = &tp->napi[i];
Matt Carlson4f125f42009-09-01 12:55:02 +00009675 free_irq(tnapi->irq_vec, tnapi);
Matt Carlson5bc09182011-11-04 09:15:01 +00009676 }
9677 goto err_out2;
Matt Carlson4f125f42009-09-01 12:55:02 +00009678 }
9679 }
Matt Carlson07b01732009-08-28 14:01:15 +00009680
David S. Millerf47c11e2005-06-24 20:18:35 -07009681 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009682
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009683 err = tg3_init_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009684 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -07009685 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009686 tg3_free_rings(tp);
9687 } else {
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009688 if (tg3_flag(tp, TAGGED_STATUS) &&
9689 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
9690 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57765)
David S. Millerfac9b832005-05-18 22:46:34 -07009691 tp->timer_offset = HZ;
9692 else
9693 tp->timer_offset = HZ / 10;
9694
9695 BUG_ON(tp->timer_offset > HZ);
9696 tp->timer_counter = tp->timer_multiplier =
9697 (HZ / tp->timer_offset);
9698 tp->asf_counter = tp->asf_multiplier =
Michael Chan28fbef72005-10-26 15:48:35 -07009699 ((HZ / tp->timer_offset) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009700
9701 init_timer(&tp->timer);
9702 tp->timer.expires = jiffies + tp->timer_offset;
9703 tp->timer.data = (unsigned long) tp;
9704 tp->timer.function = tg3_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009705 }
9706
David S. Millerf47c11e2005-06-24 20:18:35 -07009707 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009708
Matt Carlson07b01732009-08-28 14:01:15 +00009709 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009710 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009711
Joe Perches63c3a662011-04-26 08:12:10 +00009712 if (tg3_flag(tp, USING_MSI)) {
Michael Chan79381092005-04-21 17:13:59 -07009713 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -07009714
Michael Chan79381092005-04-21 17:13:59 -07009715 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -07009716 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -07009717 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -07009718 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07009719 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009720
Matt Carlson679563f2009-09-01 12:55:46 +00009721 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -07009722 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009723
Joe Perches63c3a662011-04-26 08:12:10 +00009724 if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009725 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009726
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009727 tw32(PCIE_TRANSACTION_CFG,
9728 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009729 }
Michael Chan79381092005-04-21 17:13:59 -07009730 }
9731
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009732 tg3_phy_start(tp);
9733
David S. Millerf47c11e2005-06-24 20:18:35 -07009734 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009735
Michael Chan79381092005-04-21 17:13:59 -07009736 add_timer(&tp->timer);
Joe Perches63c3a662011-04-26 08:12:10 +00009737 tg3_flag_set(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009738 tg3_enable_ints(tp);
9739
David S. Millerf47c11e2005-06-24 20:18:35 -07009740 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009741
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009742 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009743
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00009744 /*
9745 * Reset loopback feature if it was turned on while the device was down
9746 * make sure that it's installed properly now.
9747 */
9748 if (dev->features & NETIF_F_LOOPBACK)
9749 tg3_set_loopback(dev, dev->features);
9750
Linus Torvalds1da177e2005-04-16 15:20:36 -07009751 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +00009752
Matt Carlson679563f2009-09-01 12:55:46 +00009753err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +00009754 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9755 struct tg3_napi *tnapi = &tp->napi[i];
9756 free_irq(tnapi->irq_vec, tnapi);
9757 }
Matt Carlson07b01732009-08-28 14:01:15 +00009758
Matt Carlson679563f2009-09-01 12:55:46 +00009759err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +00009760 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009761 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00009762 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +00009763
9764err_out1:
9765 tg3_ints_fini(tp);
Matt Carlsoncd0d7222011-07-13 09:27:33 +00009766 tg3_frob_aux_power(tp, false);
9767 pci_set_power_state(tp->pdev, PCI_D3hot);
Matt Carlson07b01732009-08-28 14:01:15 +00009768 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009769}
9770
Eric Dumazet511d2222010-07-07 20:44:24 +00009771static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *,
9772 struct rtnl_link_stats64 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009773static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *);
9774
9775static int tg3_close(struct net_device *dev)
9776{
Matt Carlson4f125f42009-09-01 12:55:02 +00009777 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009778 struct tg3 *tp = netdev_priv(dev);
9779
Matt Carlsonfed97812009-09-01 13:10:19 +00009780 tg3_napi_disable(tp);
Matt Carlsondb219972011-11-04 09:15:03 +00009781 tg3_reset_task_cancel(tp);
Michael Chan7faa0062006-02-02 17:29:28 -08009782
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009783 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009784
9785 del_timer_sync(&tp->timer);
9786
Matt Carlson24bb4fb2009-10-05 17:55:29 +00009787 tg3_phy_stop(tp);
9788
David S. Millerf47c11e2005-06-24 20:18:35 -07009789 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009790
9791 tg3_disable_ints(tp);
9792
Michael Chan944d9802005-05-29 14:57:48 -07009793 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009794 tg3_free_rings(tp);
Joe Perches63c3a662011-04-26 08:12:10 +00009795 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009796
David S. Millerf47c11e2005-06-24 20:18:35 -07009797 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009798
Matt Carlson4f125f42009-09-01 12:55:02 +00009799 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9800 struct tg3_napi *tnapi = &tp->napi[i];
9801 free_irq(tnapi->irq_vec, tnapi);
9802 }
Matt Carlson07b01732009-08-28 14:01:15 +00009803
9804 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009805
Eric Dumazet511d2222010-07-07 20:44:24 +00009806 tg3_get_stats64(tp->dev, &tp->net_stats_prev);
9807
Linus Torvalds1da177e2005-04-16 15:20:36 -07009808 memcpy(&tp->estats_prev, tg3_get_estats(tp),
9809 sizeof(tp->estats_prev));
9810
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009811 tg3_napi_fini(tp);
9812
Linus Torvalds1da177e2005-04-16 15:20:36 -07009813 tg3_free_consistent(tp);
9814
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009815 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -08009816
9817 netif_carrier_off(tp->dev);
9818
Linus Torvalds1da177e2005-04-16 15:20:36 -07009819 return 0;
9820}
9821
Eric Dumazet511d2222010-07-07 20:44:24 +00009822static inline u64 get_stat64(tg3_stat64_t *val)
Stefan Buehler816f8b82008-08-15 14:10:54 -07009823{
9824 return ((u64)val->high << 32) | ((u64)val->low);
9825}
9826
Eric Dumazet511d2222010-07-07 20:44:24 +00009827static u64 calc_crc_errors(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009828{
9829 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9830
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009831 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009832 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
9833 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009834 u32 val;
9835
David S. Millerf47c11e2005-06-24 20:18:35 -07009836 spin_lock_bh(&tp->lock);
Michael Chan569a5df2007-02-13 12:18:15 -08009837 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
9838 tg3_writephy(tp, MII_TG3_TEST1,
9839 val | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009840 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009841 } else
9842 val = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07009843 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009844
9845 tp->phy_crc_errors += val;
9846
9847 return tp->phy_crc_errors;
9848 }
9849
9850 return get_stat64(&hw_stats->rx_fcs_errors);
9851}
9852
9853#define ESTAT_ADD(member) \
9854 estats->member = old_estats->member + \
Eric Dumazet511d2222010-07-07 20:44:24 +00009855 get_stat64(&hw_stats->member)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009856
9857static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *tp)
9858{
9859 struct tg3_ethtool_stats *estats = &tp->estats;
9860 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
9861 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9862
9863 if (!hw_stats)
9864 return old_estats;
9865
9866 ESTAT_ADD(rx_octets);
9867 ESTAT_ADD(rx_fragments);
9868 ESTAT_ADD(rx_ucast_packets);
9869 ESTAT_ADD(rx_mcast_packets);
9870 ESTAT_ADD(rx_bcast_packets);
9871 ESTAT_ADD(rx_fcs_errors);
9872 ESTAT_ADD(rx_align_errors);
9873 ESTAT_ADD(rx_xon_pause_rcvd);
9874 ESTAT_ADD(rx_xoff_pause_rcvd);
9875 ESTAT_ADD(rx_mac_ctrl_rcvd);
9876 ESTAT_ADD(rx_xoff_entered);
9877 ESTAT_ADD(rx_frame_too_long_errors);
9878 ESTAT_ADD(rx_jabbers);
9879 ESTAT_ADD(rx_undersize_packets);
9880 ESTAT_ADD(rx_in_length_errors);
9881 ESTAT_ADD(rx_out_length_errors);
9882 ESTAT_ADD(rx_64_or_less_octet_packets);
9883 ESTAT_ADD(rx_65_to_127_octet_packets);
9884 ESTAT_ADD(rx_128_to_255_octet_packets);
9885 ESTAT_ADD(rx_256_to_511_octet_packets);
9886 ESTAT_ADD(rx_512_to_1023_octet_packets);
9887 ESTAT_ADD(rx_1024_to_1522_octet_packets);
9888 ESTAT_ADD(rx_1523_to_2047_octet_packets);
9889 ESTAT_ADD(rx_2048_to_4095_octet_packets);
9890 ESTAT_ADD(rx_4096_to_8191_octet_packets);
9891 ESTAT_ADD(rx_8192_to_9022_octet_packets);
9892
9893 ESTAT_ADD(tx_octets);
9894 ESTAT_ADD(tx_collisions);
9895 ESTAT_ADD(tx_xon_sent);
9896 ESTAT_ADD(tx_xoff_sent);
9897 ESTAT_ADD(tx_flow_control);
9898 ESTAT_ADD(tx_mac_errors);
9899 ESTAT_ADD(tx_single_collisions);
9900 ESTAT_ADD(tx_mult_collisions);
9901 ESTAT_ADD(tx_deferred);
9902 ESTAT_ADD(tx_excessive_collisions);
9903 ESTAT_ADD(tx_late_collisions);
9904 ESTAT_ADD(tx_collide_2times);
9905 ESTAT_ADD(tx_collide_3times);
9906 ESTAT_ADD(tx_collide_4times);
9907 ESTAT_ADD(tx_collide_5times);
9908 ESTAT_ADD(tx_collide_6times);
9909 ESTAT_ADD(tx_collide_7times);
9910 ESTAT_ADD(tx_collide_8times);
9911 ESTAT_ADD(tx_collide_9times);
9912 ESTAT_ADD(tx_collide_10times);
9913 ESTAT_ADD(tx_collide_11times);
9914 ESTAT_ADD(tx_collide_12times);
9915 ESTAT_ADD(tx_collide_13times);
9916 ESTAT_ADD(tx_collide_14times);
9917 ESTAT_ADD(tx_collide_15times);
9918 ESTAT_ADD(tx_ucast_packets);
9919 ESTAT_ADD(tx_mcast_packets);
9920 ESTAT_ADD(tx_bcast_packets);
9921 ESTAT_ADD(tx_carrier_sense_errors);
9922 ESTAT_ADD(tx_discards);
9923 ESTAT_ADD(tx_errors);
9924
9925 ESTAT_ADD(dma_writeq_full);
9926 ESTAT_ADD(dma_write_prioq_full);
9927 ESTAT_ADD(rxbds_empty);
9928 ESTAT_ADD(rx_discards);
9929 ESTAT_ADD(rx_errors);
9930 ESTAT_ADD(rx_threshold_hit);
9931
9932 ESTAT_ADD(dma_readq_full);
9933 ESTAT_ADD(dma_read_prioq_full);
9934 ESTAT_ADD(tx_comp_queue_full);
9935
9936 ESTAT_ADD(ring_set_send_prod_index);
9937 ESTAT_ADD(ring_status_update);
9938 ESTAT_ADD(nic_irqs);
9939 ESTAT_ADD(nic_avoided_irqs);
9940 ESTAT_ADD(nic_tx_threshold_hit);
9941
Matt Carlson4452d092011-05-19 12:12:51 +00009942 ESTAT_ADD(mbuf_lwm_thresh_hit);
9943
Linus Torvalds1da177e2005-04-16 15:20:36 -07009944 return estats;
9945}
9946
Eric Dumazet511d2222010-07-07 20:44:24 +00009947static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
9948 struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009949{
9950 struct tg3 *tp = netdev_priv(dev);
Eric Dumazet511d2222010-07-07 20:44:24 +00009951 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009952 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9953
9954 if (!hw_stats)
9955 return old_stats;
9956
9957 stats->rx_packets = old_stats->rx_packets +
9958 get_stat64(&hw_stats->rx_ucast_packets) +
9959 get_stat64(&hw_stats->rx_mcast_packets) +
9960 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009961
Linus Torvalds1da177e2005-04-16 15:20:36 -07009962 stats->tx_packets = old_stats->tx_packets +
9963 get_stat64(&hw_stats->tx_ucast_packets) +
9964 get_stat64(&hw_stats->tx_mcast_packets) +
9965 get_stat64(&hw_stats->tx_bcast_packets);
9966
9967 stats->rx_bytes = old_stats->rx_bytes +
9968 get_stat64(&hw_stats->rx_octets);
9969 stats->tx_bytes = old_stats->tx_bytes +
9970 get_stat64(&hw_stats->tx_octets);
9971
9972 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -07009973 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009974 stats->tx_errors = old_stats->tx_errors +
9975 get_stat64(&hw_stats->tx_errors) +
9976 get_stat64(&hw_stats->tx_mac_errors) +
9977 get_stat64(&hw_stats->tx_carrier_sense_errors) +
9978 get_stat64(&hw_stats->tx_discards);
9979
9980 stats->multicast = old_stats->multicast +
9981 get_stat64(&hw_stats->rx_mcast_packets);
9982 stats->collisions = old_stats->collisions +
9983 get_stat64(&hw_stats->tx_collisions);
9984
9985 stats->rx_length_errors = old_stats->rx_length_errors +
9986 get_stat64(&hw_stats->rx_frame_too_long_errors) +
9987 get_stat64(&hw_stats->rx_undersize_packets);
9988
9989 stats->rx_over_errors = old_stats->rx_over_errors +
9990 get_stat64(&hw_stats->rxbds_empty);
9991 stats->rx_frame_errors = old_stats->rx_frame_errors +
9992 get_stat64(&hw_stats->rx_align_errors);
9993 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
9994 get_stat64(&hw_stats->tx_discards);
9995 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
9996 get_stat64(&hw_stats->tx_carrier_sense_errors);
9997
9998 stats->rx_crc_errors = old_stats->rx_crc_errors +
9999 calc_crc_errors(tp);
10000
John W. Linville4f63b872005-09-12 14:43:18 -070010001 stats->rx_missed_errors = old_stats->rx_missed_errors +
10002 get_stat64(&hw_stats->rx_discards);
10003
Eric Dumazetb0057c52010-10-10 19:55:52 +000010004 stats->rx_dropped = tp->rx_dropped;
Eric Dumazet48855432011-10-24 07:53:03 +000010005 stats->tx_dropped = tp->tx_dropped;
Eric Dumazetb0057c52010-10-10 19:55:52 +000010006
Linus Torvalds1da177e2005-04-16 15:20:36 -070010007 return stats;
10008}
10009
10010static inline u32 calc_crc(unsigned char *buf, int len)
10011{
10012 u32 reg;
10013 u32 tmp;
10014 int j, k;
10015
10016 reg = 0xffffffff;
10017
10018 for (j = 0; j < len; j++) {
10019 reg ^= buf[j];
10020
10021 for (k = 0; k < 8; k++) {
10022 tmp = reg & 0x01;
10023
10024 reg >>= 1;
10025
Matt Carlson859a588792010-04-05 10:19:28 +000010026 if (tmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010027 reg ^= 0xedb88320;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010028 }
10029 }
10030
10031 return ~reg;
10032}
10033
10034static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
10035{
10036 /* accept or reject all multicast frames */
10037 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
10038 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
10039 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
10040 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
10041}
10042
10043static void __tg3_set_rx_mode(struct net_device *dev)
10044{
10045 struct tg3 *tp = netdev_priv(dev);
10046 u32 rx_mode;
10047
10048 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
10049 RX_MODE_KEEP_VLAN_TAG);
10050
Matt Carlsonbf933c82011-01-25 15:58:49 +000010051#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010052 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
10053 * flag clear.
10054 */
Joe Perches63c3a662011-04-26 08:12:10 +000010055 if (!tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010056 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
10057#endif
10058
10059 if (dev->flags & IFF_PROMISC) {
10060 /* Promiscuous mode. */
10061 rx_mode |= RX_MODE_PROMISC;
10062 } else if (dev->flags & IFF_ALLMULTI) {
10063 /* Accept all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010064 tg3_set_multi(tp, 1);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +000010065 } else if (netdev_mc_empty(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010066 /* Reject all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010067 tg3_set_multi(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010068 } else {
10069 /* Accept one or more multicast(s). */
Jiri Pirko22bedad32010-04-01 21:22:57 +000010070 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010071 u32 mc_filter[4] = { 0, };
10072 u32 regidx;
10073 u32 bit;
10074 u32 crc;
10075
Jiri Pirko22bedad32010-04-01 21:22:57 +000010076 netdev_for_each_mc_addr(ha, dev) {
10077 crc = calc_crc(ha->addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010078 bit = ~crc & 0x7f;
10079 regidx = (bit & 0x60) >> 5;
10080 bit &= 0x1f;
10081 mc_filter[regidx] |= (1 << bit);
10082 }
10083
10084 tw32(MAC_HASH_REG_0, mc_filter[0]);
10085 tw32(MAC_HASH_REG_1, mc_filter[1]);
10086 tw32(MAC_HASH_REG_2, mc_filter[2]);
10087 tw32(MAC_HASH_REG_3, mc_filter[3]);
10088 }
10089
10090 if (rx_mode != tp->rx_mode) {
10091 tp->rx_mode = rx_mode;
10092 tw32_f(MAC_RX_MODE, rx_mode);
10093 udelay(10);
10094 }
10095}
10096
10097static void tg3_set_rx_mode(struct net_device *dev)
10098{
10099 struct tg3 *tp = netdev_priv(dev);
10100
Michael Chane75f7c92006-03-20 21:33:26 -080010101 if (!netif_running(dev))
10102 return;
10103
David S. Millerf47c11e2005-06-24 20:18:35 -070010104 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010105 __tg3_set_rx_mode(dev);
David S. Millerf47c11e2005-06-24 20:18:35 -070010106 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010107}
10108
Linus Torvalds1da177e2005-04-16 15:20:36 -070010109static int tg3_get_regs_len(struct net_device *dev)
10110{
Matt Carlson97bd8e42011-04-13 11:05:04 +000010111 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010112}
10113
10114static void tg3_get_regs(struct net_device *dev,
10115 struct ethtool_regs *regs, void *_p)
10116{
Linus Torvalds1da177e2005-04-16 15:20:36 -070010117 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010118
10119 regs->version = 0;
10120
Matt Carlson97bd8e42011-04-13 11:05:04 +000010121 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010122
Matt Carlson80096062010-08-02 11:26:06 +000010123 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010124 return;
10125
David S. Millerf47c11e2005-06-24 20:18:35 -070010126 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010127
Matt Carlson97bd8e42011-04-13 11:05:04 +000010128 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010129
David S. Millerf47c11e2005-06-24 20:18:35 -070010130 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010131}
10132
10133static int tg3_get_eeprom_len(struct net_device *dev)
10134{
10135 struct tg3 *tp = netdev_priv(dev);
10136
10137 return tp->nvram_size;
10138}
10139
Linus Torvalds1da177e2005-04-16 15:20:36 -070010140static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
10141{
10142 struct tg3 *tp = netdev_priv(dev);
10143 int ret;
10144 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -080010145 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010146 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010147
Joe Perches63c3a662011-04-26 08:12:10 +000010148 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000010149 return -EINVAL;
10150
Matt Carlson80096062010-08-02 11:26:06 +000010151 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010152 return -EAGAIN;
10153
Linus Torvalds1da177e2005-04-16 15:20:36 -070010154 offset = eeprom->offset;
10155 len = eeprom->len;
10156 eeprom->len = 0;
10157
10158 eeprom->magic = TG3_EEPROM_MAGIC;
10159
10160 if (offset & 3) {
10161 /* adjustments to start on required 4 byte boundary */
10162 b_offset = offset & 3;
10163 b_count = 4 - b_offset;
10164 if (b_count > len) {
10165 /* i.e. offset=1 len=2 */
10166 b_count = len;
10167 }
Matt Carlsona9dc5292009-02-25 14:25:30 +000010168 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010169 if (ret)
10170 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +000010171 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010172 len -= b_count;
10173 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010174 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010175 }
10176
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010177 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010178 pd = &data[eeprom->len];
10179 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010180 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010181 if (ret) {
10182 eeprom->len += i;
10183 return ret;
10184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010185 memcpy(pd + i, &val, 4);
10186 }
10187 eeprom->len += i;
10188
10189 if (len & 3) {
10190 /* read last bytes not ending on 4 byte boundary */
10191 pd = &data[eeprom->len];
10192 b_count = len & 3;
10193 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010194 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010195 if (ret)
10196 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080010197 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010198 eeprom->len += b_count;
10199 }
10200 return 0;
10201}
10202
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010203static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010204
10205static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
10206{
10207 struct tg3 *tp = netdev_priv(dev);
10208 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080010209 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010210 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010211 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010212
Matt Carlson80096062010-08-02 11:26:06 +000010213 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010214 return -EAGAIN;
10215
Joe Perches63c3a662011-04-26 08:12:10 +000010216 if (tg3_flag(tp, NO_NVRAM) ||
Matt Carlsondf259d82009-04-20 06:57:14 +000010217 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010218 return -EINVAL;
10219
10220 offset = eeprom->offset;
10221 len = eeprom->len;
10222
10223 if ((b_offset = (offset & 3))) {
10224 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010225 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010226 if (ret)
10227 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010228 len += b_offset;
10229 offset &= ~3;
Michael Chan1c8594b42005-04-21 17:12:46 -070010230 if (len < 4)
10231 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010232 }
10233
10234 odd_len = 0;
Michael Chan1c8594b42005-04-21 17:12:46 -070010235 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010236 /* adjustments to end on required 4 byte boundary */
10237 odd_len = 1;
10238 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010239 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010240 if (ret)
10241 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010242 }
10243
10244 buf = data;
10245 if (b_offset || odd_len) {
10246 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010247 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010248 return -ENOMEM;
10249 if (b_offset)
10250 memcpy(buf, &start, 4);
10251 if (odd_len)
10252 memcpy(buf+len-4, &end, 4);
10253 memcpy(buf + b_offset, data, eeprom->len);
10254 }
10255
10256 ret = tg3_nvram_write_block(tp, offset, len, buf);
10257
10258 if (buf != data)
10259 kfree(buf);
10260
10261 return ret;
10262}
10263
10264static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
10265{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010266 struct tg3 *tp = netdev_priv(dev);
10267
Joe Perches63c3a662011-04-26 08:12:10 +000010268 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010269 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010270 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010271 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010272 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10273 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010274 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010275
Linus Torvalds1da177e2005-04-16 15:20:36 -070010276 cmd->supported = (SUPPORTED_Autoneg);
10277
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010278 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010279 cmd->supported |= (SUPPORTED_1000baseT_Half |
10280 SUPPORTED_1000baseT_Full);
10281
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010282 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010283 cmd->supported |= (SUPPORTED_100baseT_Half |
10284 SUPPORTED_100baseT_Full |
10285 SUPPORTED_10baseT_Half |
10286 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -080010287 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -070010288 cmd->port = PORT_TP;
10289 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010290 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -070010291 cmd->port = PORT_FIBRE;
10292 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010293
Linus Torvalds1da177e2005-04-16 15:20:36 -070010294 cmd->advertising = tp->link_config.advertising;
Matt Carlson5bb09772011-06-13 13:39:00 +000010295 if (tg3_flag(tp, PAUSE_AUTONEG)) {
10296 if (tp->link_config.flowctrl & FLOW_CTRL_RX) {
10297 if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
10298 cmd->advertising |= ADVERTISED_Pause;
10299 } else {
10300 cmd->advertising |= ADVERTISED_Pause |
10301 ADVERTISED_Asym_Pause;
10302 }
10303 } else if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
10304 cmd->advertising |= ADVERTISED_Asym_Pause;
10305 }
10306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010307 if (netif_running(dev)) {
David Decotigny70739492011-04-27 18:32:40 +000010308 ethtool_cmd_speed_set(cmd, tp->link_config.active_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010309 cmd->duplex = tp->link_config.active_duplex;
Matt Carlsone348c5e2011-11-21 15:01:20 +000010310 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
10311 if (tp->phy_flags & TG3_PHYFLG_MDIX_STATE)
10312 cmd->eth_tp_mdix = ETH_TP_MDI_X;
10313 else
10314 cmd->eth_tp_mdix = ETH_TP_MDI;
10315 }
Matt Carlson64c22182010-10-14 10:37:44 +000010316 } else {
David Decotigny70739492011-04-27 18:32:40 +000010317 ethtool_cmd_speed_set(cmd, SPEED_INVALID);
Matt Carlson64c22182010-10-14 10:37:44 +000010318 cmd->duplex = DUPLEX_INVALID;
Matt Carlsone348c5e2011-11-21 15:01:20 +000010319 cmd->eth_tp_mdix = ETH_TP_MDI_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010320 }
Matt Carlson882e9792009-09-01 13:21:36 +000010321 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +000010322 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010323 cmd->autoneg = tp->link_config.autoneg;
10324 cmd->maxtxpkt = 0;
10325 cmd->maxrxpkt = 0;
10326 return 0;
10327}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010328
Linus Torvalds1da177e2005-04-16 15:20:36 -070010329static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
10330{
10331 struct tg3 *tp = netdev_priv(dev);
David Decotigny25db0332011-04-27 18:32:39 +000010332 u32 speed = ethtool_cmd_speed(cmd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010333
Joe Perches63c3a662011-04-26 08:12:10 +000010334 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010335 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010336 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010337 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010338 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10339 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010340 }
10341
Matt Carlson7e5856b2009-02-25 14:23:01 +000010342 if (cmd->autoneg != AUTONEG_ENABLE &&
10343 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -070010344 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +000010345
10346 if (cmd->autoneg == AUTONEG_DISABLE &&
10347 cmd->duplex != DUPLEX_FULL &&
10348 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -070010349 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010350
Matt Carlson7e5856b2009-02-25 14:23:01 +000010351 if (cmd->autoneg == AUTONEG_ENABLE) {
10352 u32 mask = ADVERTISED_Autoneg |
10353 ADVERTISED_Pause |
10354 ADVERTISED_Asym_Pause;
10355
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010356 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010357 mask |= ADVERTISED_1000baseT_Half |
10358 ADVERTISED_1000baseT_Full;
10359
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010360 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010361 mask |= ADVERTISED_100baseT_Half |
10362 ADVERTISED_100baseT_Full |
10363 ADVERTISED_10baseT_Half |
10364 ADVERTISED_10baseT_Full |
10365 ADVERTISED_TP;
10366 else
10367 mask |= ADVERTISED_FIBRE;
10368
10369 if (cmd->advertising & ~mask)
10370 return -EINVAL;
10371
10372 mask &= (ADVERTISED_1000baseT_Half |
10373 ADVERTISED_1000baseT_Full |
10374 ADVERTISED_100baseT_Half |
10375 ADVERTISED_100baseT_Full |
10376 ADVERTISED_10baseT_Half |
10377 ADVERTISED_10baseT_Full);
10378
10379 cmd->advertising &= mask;
10380 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010381 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
David Decotigny25db0332011-04-27 18:32:39 +000010382 if (speed != SPEED_1000)
Matt Carlson7e5856b2009-02-25 14:23:01 +000010383 return -EINVAL;
10384
10385 if (cmd->duplex != DUPLEX_FULL)
10386 return -EINVAL;
10387 } else {
David Decotigny25db0332011-04-27 18:32:39 +000010388 if (speed != SPEED_100 &&
10389 speed != SPEED_10)
Matt Carlson7e5856b2009-02-25 14:23:01 +000010390 return -EINVAL;
10391 }
10392 }
10393
David S. Millerf47c11e2005-06-24 20:18:35 -070010394 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010395
10396 tp->link_config.autoneg = cmd->autoneg;
10397 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -070010398 tp->link_config.advertising = (cmd->advertising |
10399 ADVERTISED_Autoneg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010400 tp->link_config.speed = SPEED_INVALID;
10401 tp->link_config.duplex = DUPLEX_INVALID;
10402 } else {
10403 tp->link_config.advertising = 0;
David Decotigny25db0332011-04-27 18:32:39 +000010404 tp->link_config.speed = speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010405 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010406 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010407
Michael Chan24fcad62006-12-17 17:06:46 -080010408 tp->link_config.orig_speed = tp->link_config.speed;
10409 tp->link_config.orig_duplex = tp->link_config.duplex;
10410 tp->link_config.orig_autoneg = tp->link_config.autoneg;
10411
Linus Torvalds1da177e2005-04-16 15:20:36 -070010412 if (netif_running(dev))
10413 tg3_setup_phy(tp, 1);
10414
David S. Millerf47c11e2005-06-24 20:18:35 -070010415 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010416
Linus Torvalds1da177e2005-04-16 15:20:36 -070010417 return 0;
10418}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010419
Linus Torvalds1da177e2005-04-16 15:20:36 -070010420static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
10421{
10422 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010423
Rick Jones68aad782011-11-07 13:29:27 +000010424 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
10425 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
10426 strlcpy(info->fw_version, tp->fw_ver, sizeof(info->fw_version));
10427 strlcpy(info->bus_info, pci_name(tp->pdev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010428}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010429
Linus Torvalds1da177e2005-04-16 15:20:36 -070010430static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10431{
10432 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010433
Joe Perches63c3a662011-04-26 08:12:10 +000010434 if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070010435 wol->supported = WAKE_MAGIC;
10436 else
10437 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010438 wol->wolopts = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010439 if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010440 wol->wolopts = WAKE_MAGIC;
10441 memset(&wol->sopass, 0, sizeof(wol->sopass));
10442}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010443
Linus Torvalds1da177e2005-04-16 15:20:36 -070010444static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10445{
10446 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070010447 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010448
Linus Torvalds1da177e2005-04-16 15:20:36 -070010449 if (wol->wolopts & ~WAKE_MAGIC)
10450 return -EINVAL;
10451 if ((wol->wolopts & WAKE_MAGIC) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010452 !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010453 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010454
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010455 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
10456
David S. Millerf47c11e2005-06-24 20:18:35 -070010457 spin_lock_bh(&tp->lock);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010458 if (device_may_wakeup(dp))
Joe Perches63c3a662011-04-26 08:12:10 +000010459 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010460 else
Joe Perches63c3a662011-04-26 08:12:10 +000010461 tg3_flag_clear(tp, WOL_ENABLE);
David S. Millerf47c11e2005-06-24 20:18:35 -070010462 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010463
Linus Torvalds1da177e2005-04-16 15:20:36 -070010464 return 0;
10465}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010466
Linus Torvalds1da177e2005-04-16 15:20:36 -070010467static u32 tg3_get_msglevel(struct net_device *dev)
10468{
10469 struct tg3 *tp = netdev_priv(dev);
10470 return tp->msg_enable;
10471}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010472
Linus Torvalds1da177e2005-04-16 15:20:36 -070010473static void tg3_set_msglevel(struct net_device *dev, u32 value)
10474{
10475 struct tg3 *tp = netdev_priv(dev);
10476 tp->msg_enable = value;
10477}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010478
Linus Torvalds1da177e2005-04-16 15:20:36 -070010479static int tg3_nway_reset(struct net_device *dev)
10480{
10481 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010482 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010483
Linus Torvalds1da177e2005-04-16 15:20:36 -070010484 if (!netif_running(dev))
10485 return -EAGAIN;
10486
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010487 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070010488 return -EINVAL;
10489
Joe Perches63c3a662011-04-26 08:12:10 +000010490 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010491 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010492 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010493 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010494 } else {
10495 u32 bmcr;
10496
10497 spin_lock_bh(&tp->lock);
10498 r = -EINVAL;
10499 tg3_readphy(tp, MII_BMCR, &bmcr);
10500 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
10501 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010502 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010503 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
10504 BMCR_ANENABLE);
10505 r = 0;
10506 }
10507 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010508 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010509
Linus Torvalds1da177e2005-04-16 15:20:36 -070010510 return r;
10511}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010512
Linus Torvalds1da177e2005-04-16 15:20:36 -070010513static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10514{
10515 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010516
Matt Carlson2c49a442010-09-30 10:34:35 +000010517 ering->rx_max_pending = tp->rx_std_ring_mask;
Joe Perches63c3a662011-04-26 08:12:10 +000010518 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson2c49a442010-09-30 10:34:35 +000010519 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080010520 else
10521 ering->rx_jumbo_max_pending = 0;
10522
10523 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010524
10525 ering->rx_pending = tp->rx_pending;
Joe Perches63c3a662011-04-26 08:12:10 +000010526 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Michael Chan4f81c322006-03-20 21:33:42 -080010527 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
10528 else
10529 ering->rx_jumbo_pending = 0;
10530
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010531 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010532}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010533
Linus Torvalds1da177e2005-04-16 15:20:36 -070010534static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10535{
10536 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000010537 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010538
Matt Carlson2c49a442010-09-30 10:34:35 +000010539 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
10540 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070010541 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
10542 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Joe Perches63c3a662011-04-26 08:12:10 +000010543 (tg3_flag(tp, TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070010544 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010545 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010546
Michael Chanbbe832c2005-06-24 20:20:04 -070010547 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010548 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010549 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010550 irq_sync = 1;
10551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010552
Michael Chanbbe832c2005-06-24 20:20:04 -070010553 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010554
Linus Torvalds1da177e2005-04-16 15:20:36 -070010555 tp->rx_pending = ering->rx_pending;
10556
Joe Perches63c3a662011-04-26 08:12:10 +000010557 if (tg3_flag(tp, MAX_RXPEND_64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010558 tp->rx_pending > 63)
10559 tp->rx_pending = 63;
10560 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000010561
Matt Carlson6fd45cb2010-09-15 08:59:57 +000010562 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000010563 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010564
10565 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070010566 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -070010567 err = tg3_restart_hw(tp, 1);
10568 if (!err)
10569 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010570 }
10571
David S. Millerf47c11e2005-06-24 20:18:35 -070010572 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010573
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010574 if (irq_sync && !err)
10575 tg3_phy_start(tp);
10576
Michael Chanb9ec6c12006-07-25 16:37:27 -070010577 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010578}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010579
Linus Torvalds1da177e2005-04-16 15:20:36 -070010580static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10581{
10582 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010583
Joe Perches63c3a662011-04-26 08:12:10 +000010584 epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
Matt Carlson8d018622007-12-20 20:05:44 -080010585
Steve Glendinninge18ce342008-12-16 02:00:00 -080010586 if (tp->link_config.active_flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080010587 epause->rx_pause = 1;
10588 else
10589 epause->rx_pause = 0;
10590
Steve Glendinninge18ce342008-12-16 02:00:00 -080010591 if (tp->link_config.active_flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080010592 epause->tx_pause = 1;
10593 else
10594 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010595}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010596
Linus Torvalds1da177e2005-04-16 15:20:36 -070010597static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10598{
10599 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010600 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010601
Joe Perches63c3a662011-04-26 08:12:10 +000010602 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson27121682010-02-17 15:16:57 +000010603 u32 newadv;
10604 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010605
Matt Carlson27121682010-02-17 15:16:57 +000010606 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010607
Matt Carlson27121682010-02-17 15:16:57 +000010608 if (!(phydev->supported & SUPPORTED_Pause) ||
10609 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000010610 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000010611 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010612
Matt Carlson27121682010-02-17 15:16:57 +000010613 tp->link_config.flowctrl = 0;
10614 if (epause->rx_pause) {
10615 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010616
Matt Carlson27121682010-02-17 15:16:57 +000010617 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080010618 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000010619 newadv = ADVERTISED_Pause;
10620 } else
10621 newadv = ADVERTISED_Pause |
10622 ADVERTISED_Asym_Pause;
10623 } else if (epause->tx_pause) {
10624 tp->link_config.flowctrl |= FLOW_CTRL_TX;
10625 newadv = ADVERTISED_Asym_Pause;
10626 } else
10627 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010628
Matt Carlson27121682010-02-17 15:16:57 +000010629 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010630 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010631 else
Joe Perches63c3a662011-04-26 08:12:10 +000010632 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010633
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010634 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000010635 u32 oldadv = phydev->advertising &
10636 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
10637 if (oldadv != newadv) {
10638 phydev->advertising &=
10639 ~(ADVERTISED_Pause |
10640 ADVERTISED_Asym_Pause);
10641 phydev->advertising |= newadv;
10642 if (phydev->autoneg) {
10643 /*
10644 * Always renegotiate the link to
10645 * inform our link partner of our
10646 * flow control settings, even if the
10647 * flow control is forced. Let
10648 * tg3_adjust_link() do the final
10649 * flow control setup.
10650 */
10651 return phy_start_aneg(phydev);
10652 }
10653 }
10654
10655 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010656 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000010657 } else {
10658 tp->link_config.orig_advertising &=
10659 ~(ADVERTISED_Pause |
10660 ADVERTISED_Asym_Pause);
10661 tp->link_config.orig_advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010662 }
10663 } else {
10664 int irq_sync = 0;
10665
10666 if (netif_running(dev)) {
10667 tg3_netif_stop(tp);
10668 irq_sync = 1;
10669 }
10670
10671 tg3_full_lock(tp, irq_sync);
10672
10673 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010674 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010675 else
Joe Perches63c3a662011-04-26 08:12:10 +000010676 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010677 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010678 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010679 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010680 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010681 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010682 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010683 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010684 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010685
10686 if (netif_running(dev)) {
10687 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10688 err = tg3_restart_hw(tp, 1);
10689 if (!err)
10690 tg3_netif_start(tp);
10691 }
10692
10693 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010695
Michael Chanb9ec6c12006-07-25 16:37:27 -070010696 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010697}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010698
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010699static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010700{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010701 switch (sset) {
10702 case ETH_SS_TEST:
10703 return TG3_NUM_TEST;
10704 case ETH_SS_STATS:
10705 return TG3_NUM_STATS;
10706 default:
10707 return -EOPNOTSUPP;
10708 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070010709}
10710
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010711static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010712{
10713 switch (stringset) {
10714 case ETH_SS_STATS:
10715 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
10716 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070010717 case ETH_SS_TEST:
10718 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
10719 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010720 default:
10721 WARN_ON(1); /* we need a WARN() */
10722 break;
10723 }
10724}
10725
stephen hemminger81b87092011-04-04 08:43:50 +000010726static int tg3_set_phys_id(struct net_device *dev,
10727 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070010728{
10729 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070010730
10731 if (!netif_running(tp->dev))
10732 return -EAGAIN;
10733
stephen hemminger81b87092011-04-04 08:43:50 +000010734 switch (state) {
10735 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +000010736 return 1; /* cycle on/off once per second */
Michael Chan4009a932005-09-05 17:52:54 -070010737
stephen hemminger81b87092011-04-04 08:43:50 +000010738 case ETHTOOL_ID_ON:
10739 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10740 LED_CTRL_1000MBPS_ON |
10741 LED_CTRL_100MBPS_ON |
10742 LED_CTRL_10MBPS_ON |
10743 LED_CTRL_TRAFFIC_OVERRIDE |
10744 LED_CTRL_TRAFFIC_BLINK |
10745 LED_CTRL_TRAFFIC_LED);
10746 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010747
stephen hemminger81b87092011-04-04 08:43:50 +000010748 case ETHTOOL_ID_OFF:
10749 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10750 LED_CTRL_TRAFFIC_OVERRIDE);
10751 break;
Michael Chan4009a932005-09-05 17:52:54 -070010752
stephen hemminger81b87092011-04-04 08:43:50 +000010753 case ETHTOOL_ID_INACTIVE:
10754 tw32(MAC_LED_CTRL, tp->led_ctrl);
10755 break;
Michael Chan4009a932005-09-05 17:52:54 -070010756 }
stephen hemminger81b87092011-04-04 08:43:50 +000010757
Michael Chan4009a932005-09-05 17:52:54 -070010758 return 0;
10759}
10760
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010761static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010762 struct ethtool_stats *estats, u64 *tmp_stats)
10763{
10764 struct tg3 *tp = netdev_priv(dev);
10765 memcpy(tmp_stats, tg3_get_estats(tp), sizeof(tp->estats));
10766}
10767
Matt Carlson535a4902011-07-20 10:20:56 +000010768static __be32 *tg3_vpd_readblock(struct tg3 *tp, u32 *vpdlen)
Matt Carlsonc3e94502011-04-13 11:05:08 +000010769{
10770 int i;
10771 __be32 *buf;
10772 u32 offset = 0, len = 0;
10773 u32 magic, val;
10774
Joe Perches63c3a662011-04-26 08:12:10 +000010775 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
Matt Carlsonc3e94502011-04-13 11:05:08 +000010776 return NULL;
10777
10778 if (magic == TG3_EEPROM_MAGIC) {
10779 for (offset = TG3_NVM_DIR_START;
10780 offset < TG3_NVM_DIR_END;
10781 offset += TG3_NVM_DIRENT_SIZE) {
10782 if (tg3_nvram_read(tp, offset, &val))
10783 return NULL;
10784
10785 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
10786 TG3_NVM_DIRTYPE_EXTVPD)
10787 break;
10788 }
10789
10790 if (offset != TG3_NVM_DIR_END) {
10791 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
10792 if (tg3_nvram_read(tp, offset + 4, &offset))
10793 return NULL;
10794
10795 offset = tg3_nvram_logical_addr(tp, offset);
10796 }
10797 }
10798
10799 if (!offset || !len) {
10800 offset = TG3_NVM_VPD_OFF;
10801 len = TG3_NVM_VPD_LEN;
10802 }
10803
10804 buf = kmalloc(len, GFP_KERNEL);
10805 if (buf == NULL)
10806 return NULL;
10807
10808 if (magic == TG3_EEPROM_MAGIC) {
10809 for (i = 0; i < len; i += 4) {
10810 /* The data is in little-endian format in NVRAM.
10811 * Use the big-endian read routines to preserve
10812 * the byte order as it exists in NVRAM.
10813 */
10814 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
10815 goto error;
10816 }
10817 } else {
10818 u8 *ptr;
10819 ssize_t cnt;
10820 unsigned int pos = 0;
10821
10822 ptr = (u8 *)&buf[0];
10823 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
10824 cnt = pci_read_vpd(tp->pdev, pos,
10825 len - pos, ptr);
10826 if (cnt == -ETIMEDOUT || cnt == -EINTR)
10827 cnt = 0;
10828 else if (cnt < 0)
10829 goto error;
10830 }
10831 if (pos != len)
10832 goto error;
10833 }
10834
Matt Carlson535a4902011-07-20 10:20:56 +000010835 *vpdlen = len;
10836
Matt Carlsonc3e94502011-04-13 11:05:08 +000010837 return buf;
10838
10839error:
10840 kfree(buf);
10841 return NULL;
10842}
10843
Michael Chan566f86a2005-05-29 14:56:58 -070010844#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080010845#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
10846#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
10847#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Matt Carlson727a6d92011-06-13 13:38:58 +000010848#define NVRAM_SELFBOOT_FORMAT1_4_SIZE 0x20
10849#define NVRAM_SELFBOOT_FORMAT1_5_SIZE 0x24
Matt Carlsonbda18fa2011-07-20 10:20:57 +000010850#define NVRAM_SELFBOOT_FORMAT1_6_SIZE 0x50
Michael Chanb16250e2006-09-27 16:10:14 -070010851#define NVRAM_SELFBOOT_HW_SIZE 0x20
10852#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070010853
10854static int tg3_test_nvram(struct tg3 *tp)
10855{
Matt Carlson535a4902011-07-20 10:20:56 +000010856 u32 csum, magic, len;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010857 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010858 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070010859
Joe Perches63c3a662011-04-26 08:12:10 +000010860 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000010861 return 0;
10862
Matt Carlsone4f34112009-02-25 14:25:00 +000010863 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080010864 return -EIO;
10865
Michael Chan1b277772006-03-20 22:27:48 -080010866 if (magic == TG3_EEPROM_MAGIC)
10867 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070010868 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080010869 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
10870 TG3_EEPROM_SB_FORMAT_1) {
10871 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
10872 case TG3_EEPROM_SB_REVISION_0:
10873 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
10874 break;
10875 case TG3_EEPROM_SB_REVISION_2:
10876 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
10877 break;
10878 case TG3_EEPROM_SB_REVISION_3:
10879 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
10880 break;
Matt Carlson727a6d92011-06-13 13:38:58 +000010881 case TG3_EEPROM_SB_REVISION_4:
10882 size = NVRAM_SELFBOOT_FORMAT1_4_SIZE;
10883 break;
10884 case TG3_EEPROM_SB_REVISION_5:
10885 size = NVRAM_SELFBOOT_FORMAT1_5_SIZE;
10886 break;
10887 case TG3_EEPROM_SB_REVISION_6:
10888 size = NVRAM_SELFBOOT_FORMAT1_6_SIZE;
10889 break;
Matt Carlsona5767de2007-11-12 21:10:58 -080010890 default:
Matt Carlson727a6d92011-06-13 13:38:58 +000010891 return -EIO;
Matt Carlsona5767de2007-11-12 21:10:58 -080010892 }
10893 } else
Michael Chan1b277772006-03-20 22:27:48 -080010894 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070010895 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
10896 size = NVRAM_SELFBOOT_HW_SIZE;
10897 else
Michael Chan1b277772006-03-20 22:27:48 -080010898 return -EIO;
10899
10900 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070010901 if (buf == NULL)
10902 return -ENOMEM;
10903
Michael Chan1b277772006-03-20 22:27:48 -080010904 err = -EIO;
10905 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010906 err = tg3_nvram_read_be32(tp, i, &buf[j]);
10907 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070010908 break;
Michael Chan566f86a2005-05-29 14:56:58 -070010909 }
Michael Chan1b277772006-03-20 22:27:48 -080010910 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070010911 goto out;
10912
Michael Chan1b277772006-03-20 22:27:48 -080010913 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010914 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080010915 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010916 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080010917 u8 *buf8 = (u8 *) buf, csum8 = 0;
10918
Al Virob9fc7dc2007-12-17 22:59:57 -080010919 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080010920 TG3_EEPROM_SB_REVISION_2) {
10921 /* For rev 2, the csum doesn't include the MBA. */
10922 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
10923 csum8 += buf8[i];
10924 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
10925 csum8 += buf8[i];
10926 } else {
10927 for (i = 0; i < size; i++)
10928 csum8 += buf8[i];
10929 }
Michael Chan1b277772006-03-20 22:27:48 -080010930
Adrian Bunkad96b482006-04-05 22:21:04 -070010931 if (csum8 == 0) {
10932 err = 0;
10933 goto out;
10934 }
10935
10936 err = -EIO;
10937 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080010938 }
Michael Chan566f86a2005-05-29 14:56:58 -070010939
Al Virob9fc7dc2007-12-17 22:59:57 -080010940 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010941 TG3_EEPROM_MAGIC_HW) {
10942 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000010943 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070010944 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070010945
10946 /* Separate the parity bits and the data bytes. */
10947 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
10948 if ((i == 0) || (i == 8)) {
10949 int l;
10950 u8 msk;
10951
10952 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
10953 parity[k++] = buf8[i] & msk;
10954 i++;
Matt Carlson859a588792010-04-05 10:19:28 +000010955 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070010956 int l;
10957 u8 msk;
10958
10959 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
10960 parity[k++] = buf8[i] & msk;
10961 i++;
10962
10963 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
10964 parity[k++] = buf8[i] & msk;
10965 i++;
10966 }
10967 data[j++] = buf8[i];
10968 }
10969
10970 err = -EIO;
10971 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
10972 u8 hw8 = hweight8(data[i]);
10973
10974 if ((hw8 & 0x1) && parity[i])
10975 goto out;
10976 else if (!(hw8 & 0x1) && !parity[i])
10977 goto out;
10978 }
10979 err = 0;
10980 goto out;
10981 }
10982
Matt Carlson01c3a392011-03-09 16:58:20 +000010983 err = -EIO;
10984
Michael Chan566f86a2005-05-29 14:56:58 -070010985 /* Bootstrap checksum at offset 0x10 */
10986 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000010987 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070010988 goto out;
10989
10990 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
10991 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000010992 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000010993 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070010994
Matt Carlsonc3e94502011-04-13 11:05:08 +000010995 kfree(buf);
10996
Matt Carlson535a4902011-07-20 10:20:56 +000010997 buf = tg3_vpd_readblock(tp, &len);
Matt Carlsonc3e94502011-04-13 11:05:08 +000010998 if (!buf)
10999 return -ENOMEM;
Matt Carlsond4894f32011-03-09 16:58:21 +000011000
Matt Carlson535a4902011-07-20 10:20:56 +000011001 i = pci_vpd_find_tag((u8 *)buf, 0, len, PCI_VPD_LRDT_RO_DATA);
Matt Carlsond4894f32011-03-09 16:58:21 +000011002 if (i > 0) {
11003 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
11004 if (j < 0)
11005 goto out;
11006
Matt Carlson535a4902011-07-20 10:20:56 +000011007 if (i + PCI_VPD_LRDT_TAG_SIZE + j > len)
Matt Carlsond4894f32011-03-09 16:58:21 +000011008 goto out;
11009
11010 i += PCI_VPD_LRDT_TAG_SIZE;
11011 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
11012 PCI_VPD_RO_KEYWORD_CHKSUM);
11013 if (j > 0) {
11014 u8 csum8 = 0;
11015
11016 j += PCI_VPD_INFO_FLD_HDR_SIZE;
11017
11018 for (i = 0; i <= j; i++)
11019 csum8 += ((u8 *)buf)[i];
11020
11021 if (csum8)
11022 goto out;
11023 }
11024 }
11025
Michael Chan566f86a2005-05-29 14:56:58 -070011026 err = 0;
11027
11028out:
11029 kfree(buf);
11030 return err;
11031}
11032
Michael Chanca430072005-05-29 14:57:23 -070011033#define TG3_SERDES_TIMEOUT_SEC 2
11034#define TG3_COPPER_TIMEOUT_SEC 6
11035
11036static int tg3_test_link(struct tg3 *tp)
11037{
11038 int i, max;
11039
11040 if (!netif_running(tp->dev))
11041 return -ENODEV;
11042
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011043 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070011044 max = TG3_SERDES_TIMEOUT_SEC;
11045 else
11046 max = TG3_COPPER_TIMEOUT_SEC;
11047
11048 for (i = 0; i < max; i++) {
11049 if (netif_carrier_ok(tp->dev))
11050 return 0;
11051
11052 if (msleep_interruptible(1000))
11053 break;
11054 }
11055
11056 return -EIO;
11057}
11058
Michael Chana71116d2005-05-29 14:58:11 -070011059/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080011060static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070011061{
Michael Chanb16250e2006-09-27 16:10:14 -070011062 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070011063 u32 offset, read_mask, write_mask, val, save_val, read_val;
11064 static struct {
11065 u16 offset;
11066 u16 flags;
11067#define TG3_FL_5705 0x1
11068#define TG3_FL_NOT_5705 0x2
11069#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070011070#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070011071 u32 read_mask;
11072 u32 write_mask;
11073 } reg_tbl[] = {
11074 /* MAC Control Registers */
11075 { MAC_MODE, TG3_FL_NOT_5705,
11076 0x00000000, 0x00ef6f8c },
11077 { MAC_MODE, TG3_FL_5705,
11078 0x00000000, 0x01ef6b8c },
11079 { MAC_STATUS, TG3_FL_NOT_5705,
11080 0x03800107, 0x00000000 },
11081 { MAC_STATUS, TG3_FL_5705,
11082 0x03800100, 0x00000000 },
11083 { MAC_ADDR_0_HIGH, 0x0000,
11084 0x00000000, 0x0000ffff },
11085 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011086 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070011087 { MAC_RX_MTU_SIZE, 0x0000,
11088 0x00000000, 0x0000ffff },
11089 { MAC_TX_MODE, 0x0000,
11090 0x00000000, 0x00000070 },
11091 { MAC_TX_LENGTHS, 0x0000,
11092 0x00000000, 0x00003fff },
11093 { MAC_RX_MODE, TG3_FL_NOT_5705,
11094 0x00000000, 0x000007fc },
11095 { MAC_RX_MODE, TG3_FL_5705,
11096 0x00000000, 0x000007dc },
11097 { MAC_HASH_REG_0, 0x0000,
11098 0x00000000, 0xffffffff },
11099 { MAC_HASH_REG_1, 0x0000,
11100 0x00000000, 0xffffffff },
11101 { MAC_HASH_REG_2, 0x0000,
11102 0x00000000, 0xffffffff },
11103 { MAC_HASH_REG_3, 0x0000,
11104 0x00000000, 0xffffffff },
11105
11106 /* Receive Data and Receive BD Initiator Control Registers. */
11107 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
11108 0x00000000, 0xffffffff },
11109 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
11110 0x00000000, 0xffffffff },
11111 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
11112 0x00000000, 0x00000003 },
11113 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
11114 0x00000000, 0xffffffff },
11115 { RCVDBDI_STD_BD+0, 0x0000,
11116 0x00000000, 0xffffffff },
11117 { RCVDBDI_STD_BD+4, 0x0000,
11118 0x00000000, 0xffffffff },
11119 { RCVDBDI_STD_BD+8, 0x0000,
11120 0x00000000, 0xffff0002 },
11121 { RCVDBDI_STD_BD+0xc, 0x0000,
11122 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011123
Michael Chana71116d2005-05-29 14:58:11 -070011124 /* Receive BD Initiator Control Registers. */
11125 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
11126 0x00000000, 0xffffffff },
11127 { RCVBDI_STD_THRESH, TG3_FL_5705,
11128 0x00000000, 0x000003ff },
11129 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
11130 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011131
Michael Chana71116d2005-05-29 14:58:11 -070011132 /* Host Coalescing Control Registers. */
11133 { HOSTCC_MODE, TG3_FL_NOT_5705,
11134 0x00000000, 0x00000004 },
11135 { HOSTCC_MODE, TG3_FL_5705,
11136 0x00000000, 0x000000f6 },
11137 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
11138 0x00000000, 0xffffffff },
11139 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
11140 0x00000000, 0x000003ff },
11141 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
11142 0x00000000, 0xffffffff },
11143 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
11144 0x00000000, 0x000003ff },
11145 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
11146 0x00000000, 0xffffffff },
11147 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
11148 0x00000000, 0x000000ff },
11149 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
11150 0x00000000, 0xffffffff },
11151 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
11152 0x00000000, 0x000000ff },
11153 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
11154 0x00000000, 0xffffffff },
11155 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
11156 0x00000000, 0xffffffff },
11157 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
11158 0x00000000, 0xffffffff },
11159 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
11160 0x00000000, 0x000000ff },
11161 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
11162 0x00000000, 0xffffffff },
11163 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
11164 0x00000000, 0x000000ff },
11165 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
11166 0x00000000, 0xffffffff },
11167 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
11168 0x00000000, 0xffffffff },
11169 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
11170 0x00000000, 0xffffffff },
11171 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
11172 0x00000000, 0xffffffff },
11173 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
11174 0x00000000, 0xffffffff },
11175 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
11176 0xffffffff, 0x00000000 },
11177 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
11178 0xffffffff, 0x00000000 },
11179
11180 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070011181 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070011182 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070011183 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070011184 0x00000000, 0x007fffff },
11185 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
11186 0x00000000, 0x0000003f },
11187 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
11188 0x00000000, 0x000001ff },
11189 { BUFMGR_MB_HIGH_WATER, 0x0000,
11190 0x00000000, 0x000001ff },
11191 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
11192 0xffffffff, 0x00000000 },
11193 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
11194 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011195
Michael Chana71116d2005-05-29 14:58:11 -070011196 /* Mailbox Registers */
11197 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
11198 0x00000000, 0x000001ff },
11199 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
11200 0x00000000, 0x000001ff },
11201 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
11202 0x00000000, 0x000007ff },
11203 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
11204 0x00000000, 0x000001ff },
11205
11206 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
11207 };
11208
Michael Chanb16250e2006-09-27 16:10:14 -070011209 is_5705 = is_5750 = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000011210 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chana71116d2005-05-29 14:58:11 -070011211 is_5705 = 1;
Joe Perches63c3a662011-04-26 08:12:10 +000011212 if (tg3_flag(tp, 5750_PLUS))
Michael Chanb16250e2006-09-27 16:10:14 -070011213 is_5750 = 1;
11214 }
Michael Chana71116d2005-05-29 14:58:11 -070011215
11216 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
11217 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
11218 continue;
11219
11220 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
11221 continue;
11222
Joe Perches63c3a662011-04-26 08:12:10 +000011223 if (tg3_flag(tp, IS_5788) &&
Michael Chana71116d2005-05-29 14:58:11 -070011224 (reg_tbl[i].flags & TG3_FL_NOT_5788))
11225 continue;
11226
Michael Chanb16250e2006-09-27 16:10:14 -070011227 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
11228 continue;
11229
Michael Chana71116d2005-05-29 14:58:11 -070011230 offset = (u32) reg_tbl[i].offset;
11231 read_mask = reg_tbl[i].read_mask;
11232 write_mask = reg_tbl[i].write_mask;
11233
11234 /* Save the original register content */
11235 save_val = tr32(offset);
11236
11237 /* Determine the read-only value. */
11238 read_val = save_val & read_mask;
11239
11240 /* Write zero to the register, then make sure the read-only bits
11241 * are not changed and the read/write bits are all zeros.
11242 */
11243 tw32(offset, 0);
11244
11245 val = tr32(offset);
11246
11247 /* Test the read-only and read/write bits. */
11248 if (((val & read_mask) != read_val) || (val & write_mask))
11249 goto out;
11250
11251 /* Write ones to all the bits defined by RdMask and WrMask, then
11252 * make sure the read-only bits are not changed and the
11253 * read/write bits are all ones.
11254 */
11255 tw32(offset, read_mask | write_mask);
11256
11257 val = tr32(offset);
11258
11259 /* Test the read-only bits. */
11260 if ((val & read_mask) != read_val)
11261 goto out;
11262
11263 /* Test the read/write bits. */
11264 if ((val & write_mask) != write_mask)
11265 goto out;
11266
11267 tw32(offset, save_val);
11268 }
11269
11270 return 0;
11271
11272out:
Michael Chan9f88f292006-12-07 00:22:54 -080011273 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000011274 netdev_err(tp->dev,
11275 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070011276 tw32(offset, save_val);
11277 return -EIO;
11278}
11279
Michael Chan7942e1d2005-05-29 14:58:36 -070011280static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
11281{
Arjan van de Venf71e1302006-03-03 21:33:57 -050011282 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070011283 int i;
11284 u32 j;
11285
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020011286 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070011287 for (j = 0; j < len; j += 4) {
11288 u32 val;
11289
11290 tg3_write_mem(tp, offset + j, test_pattern[i]);
11291 tg3_read_mem(tp, offset + j, &val);
11292 if (val != test_pattern[i])
11293 return -EIO;
11294 }
11295 }
11296 return 0;
11297}
11298
11299static int tg3_test_memory(struct tg3 *tp)
11300{
11301 static struct mem_entry {
11302 u32 offset;
11303 u32 len;
11304 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080011305 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070011306 { 0x00002000, 0x1c000},
11307 { 0xffffffff, 0x00000}
11308 }, mem_tbl_5705[] = {
11309 { 0x00000100, 0x0000c},
11310 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070011311 { 0x00004000, 0x00800},
11312 { 0x00006000, 0x01000},
11313 { 0x00008000, 0x02000},
11314 { 0x00010000, 0x0e000},
11315 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080011316 }, mem_tbl_5755[] = {
11317 { 0x00000200, 0x00008},
11318 { 0x00004000, 0x00800},
11319 { 0x00006000, 0x00800},
11320 { 0x00008000, 0x02000},
11321 { 0x00010000, 0x0c000},
11322 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070011323 }, mem_tbl_5906[] = {
11324 { 0x00000200, 0x00008},
11325 { 0x00004000, 0x00400},
11326 { 0x00006000, 0x00400},
11327 { 0x00008000, 0x01000},
11328 { 0x00010000, 0x01000},
11329 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011330 }, mem_tbl_5717[] = {
11331 { 0x00000200, 0x00008},
11332 { 0x00010000, 0x0a000},
11333 { 0x00020000, 0x13c00},
11334 { 0xffffffff, 0x00000}
11335 }, mem_tbl_57765[] = {
11336 { 0x00000200, 0x00008},
11337 { 0x00004000, 0x00800},
11338 { 0x00006000, 0x09800},
11339 { 0x00010000, 0x0a000},
11340 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070011341 };
11342 struct mem_entry *mem_tbl;
11343 int err = 0;
11344 int i;
11345
Joe Perches63c3a662011-04-26 08:12:10 +000011346 if (tg3_flag(tp, 5717_PLUS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011347 mem_tbl = mem_tbl_5717;
11348 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
11349 mem_tbl = mem_tbl_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000011350 else if (tg3_flag(tp, 5755_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080011351 mem_tbl = mem_tbl_5755;
11352 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
11353 mem_tbl = mem_tbl_5906;
Joe Perches63c3a662011-04-26 08:12:10 +000011354 else if (tg3_flag(tp, 5705_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080011355 mem_tbl = mem_tbl_5705;
11356 else
Michael Chan7942e1d2005-05-29 14:58:36 -070011357 mem_tbl = mem_tbl_570x;
11358
11359 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000011360 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
11361 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070011362 break;
11363 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011364
Michael Chan7942e1d2005-05-29 14:58:36 -070011365 return err;
11366}
11367
Matt Carlsonbb158d62011-04-25 12:42:47 +000011368#define TG3_TSO_MSS 500
11369
11370#define TG3_TSO_IP_HDR_LEN 20
11371#define TG3_TSO_TCP_HDR_LEN 20
11372#define TG3_TSO_TCP_OPT_LEN 12
11373
11374static const u8 tg3_tso_header[] = {
113750x08, 0x00,
113760x45, 0x00, 0x00, 0x00,
113770x00, 0x00, 0x40, 0x00,
113780x40, 0x06, 0x00, 0x00,
113790x0a, 0x00, 0x00, 0x01,
113800x0a, 0x00, 0x00, 0x02,
113810x0d, 0x00, 0xe0, 0x00,
113820x00, 0x00, 0x01, 0x00,
113830x00, 0x00, 0x02, 0x00,
113840x80, 0x10, 0x10, 0x00,
113850x14, 0x09, 0x00, 0x00,
113860x01, 0x01, 0x08, 0x0a,
113870x11, 0x11, 0x11, 0x11,
113880x11, 0x11, 0x11, 0x11,
11389};
Michael Chan9f40dea2005-09-05 17:53:06 -070011390
Matt Carlson28a45952011-08-19 13:58:22 +000011391static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, bool tso_loopback)
Michael Chanc76949a2005-05-29 14:58:59 -070011392{
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011393 u32 rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011394 u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
Matt Carlson84b67b22011-07-27 14:20:52 +000011395 u32 budget;
Eric Dumazet9205fd92011-11-18 06:47:01 +000011396 struct sk_buff *skb;
11397 u8 *tx_data, *rx_data;
Michael Chanc76949a2005-05-29 14:58:59 -070011398 dma_addr_t map;
11399 int num_pkts, tx_len, rx_len, i, err;
11400 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000011401 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000011402 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070011403
Matt Carlsonc8873402010-02-12 14:47:11 +000011404 tnapi = &tp->napi[0];
11405 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011406 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +000011407 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlson1da85aa2010-09-30 10:34:34 +000011408 rnapi = &tp->napi[1];
Joe Perches63c3a662011-04-26 08:12:10 +000011409 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc8873402010-02-12 14:47:11 +000011410 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011411 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011412 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000011413
Michael Chanc76949a2005-05-29 14:58:59 -070011414 err = -EIO;
11415
Matt Carlson4852a862011-04-13 11:05:07 +000011416 tx_len = pktsz;
David S. Millera20e9c62006-07-31 22:38:16 -070011417 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070011418 if (!skb)
11419 return -ENOMEM;
11420
Michael Chanc76949a2005-05-29 14:58:59 -070011421 tx_data = skb_put(skb, tx_len);
11422 memcpy(tx_data, tp->dev->dev_addr, 6);
11423 memset(tx_data + 6, 0x0, 8);
11424
Matt Carlson4852a862011-04-13 11:05:07 +000011425 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
Michael Chanc76949a2005-05-29 14:58:59 -070011426
Matt Carlson28a45952011-08-19 13:58:22 +000011427 if (tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011428 struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
11429
11430 u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
11431 TG3_TSO_TCP_OPT_LEN;
11432
11433 memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
11434 sizeof(tg3_tso_header));
11435 mss = TG3_TSO_MSS;
11436
11437 val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
11438 num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
11439
11440 /* Set the total length field in the IP header */
11441 iph->tot_len = htons((u16)(mss + hdr_len));
11442
11443 base_flags = (TXD_FLAG_CPU_PRE_DMA |
11444 TXD_FLAG_CPU_POST_DMA);
11445
Joe Perches63c3a662011-04-26 08:12:10 +000011446 if (tg3_flag(tp, HW_TSO_1) ||
11447 tg3_flag(tp, HW_TSO_2) ||
11448 tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011449 struct tcphdr *th;
11450 val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
11451 th = (struct tcphdr *)&tx_data[val];
11452 th->check = 0;
11453 } else
11454 base_flags |= TXD_FLAG_TCPUDP_CSUM;
11455
Joe Perches63c3a662011-04-26 08:12:10 +000011456 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011457 mss |= (hdr_len & 0xc) << 12;
11458 if (hdr_len & 0x10)
11459 base_flags |= 0x00000010;
11460 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +000011461 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlsonbb158d62011-04-25 12:42:47 +000011462 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +000011463 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlsonbb158d62011-04-25 12:42:47 +000011464 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
11465 mss |= (TG3_TSO_TCP_OPT_LEN << 9);
11466 } else {
11467 base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
11468 }
11469
11470 data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
11471 } else {
11472 num_pkts = 1;
11473 data_off = ETH_HLEN;
11474 }
11475
11476 for (i = data_off; i < tx_len; i++)
Michael Chanc76949a2005-05-29 14:58:59 -070011477 tx_data[i] = (u8) (i & 0xff);
11478
Alexander Duyckf4188d82009-12-02 16:48:38 +000011479 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
11480 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000011481 dev_kfree_skb(skb);
11482 return -EIO;
11483 }
Michael Chanc76949a2005-05-29 14:58:59 -070011484
Matt Carlson0d681b22011-07-27 14:20:49 +000011485 val = tnapi->tx_prod;
11486 tnapi->tx_buffers[val].skb = skb;
11487 dma_unmap_addr_set(&tnapi->tx_buffers[val], mapping, map);
11488
Michael Chanc76949a2005-05-29 14:58:59 -070011489 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011490 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011491
11492 udelay(10);
11493
Matt Carlson898a56f2009-08-28 14:02:40 +000011494 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070011495
Matt Carlson84b67b22011-07-27 14:20:52 +000011496 budget = tg3_tx_avail(tnapi);
11497 if (tg3_tx_frag_set(tnapi, &val, &budget, map, tx_len,
Matt Carlsond1a3b732011-07-27 14:20:51 +000011498 base_flags | TXD_FLAG_END, mss, 0)) {
11499 tnapi->tx_buffers[val].skb = NULL;
11500 dev_kfree_skb(skb);
11501 return -EIO;
11502 }
Michael Chanc76949a2005-05-29 14:58:59 -070011503
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011504 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070011505
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011506 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
11507 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070011508
11509 udelay(10);
11510
Matt Carlson303fc922009-11-02 14:27:34 +000011511 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
11512 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070011513 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011514 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011515
11516 udelay(10);
11517
Matt Carlson898a56f2009-08-28 14:02:40 +000011518 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
11519 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011520 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070011521 (rx_idx == (rx_start_idx + num_pkts)))
11522 break;
11523 }
11524
Matt Carlsonba1142e2011-11-04 09:15:00 +000011525 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod - 1, -1);
Michael Chanc76949a2005-05-29 14:58:59 -070011526 dev_kfree_skb(skb);
11527
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011528 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070011529 goto out;
11530
11531 if (rx_idx != rx_start_idx + num_pkts)
11532 goto out;
11533
Matt Carlsonbb158d62011-04-25 12:42:47 +000011534 val = data_off;
11535 while (rx_idx != rx_start_idx) {
11536 desc = &rnapi->rx_rcb[rx_start_idx++];
11537 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
11538 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
Michael Chanc76949a2005-05-29 14:58:59 -070011539
Matt Carlsonbb158d62011-04-25 12:42:47 +000011540 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
11541 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
Matt Carlson4852a862011-04-13 11:05:07 +000011542 goto out;
Michael Chanc76949a2005-05-29 14:58:59 -070011543
Matt Carlsonbb158d62011-04-25 12:42:47 +000011544 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
11545 - ETH_FCS_LEN;
11546
Matt Carlson28a45952011-08-19 13:58:22 +000011547 if (!tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011548 if (rx_len != tx_len)
11549 goto out;
11550
11551 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
11552 if (opaque_key != RXD_OPAQUE_RING_STD)
11553 goto out;
11554 } else {
11555 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
11556 goto out;
11557 }
11558 } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
11559 (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
Matt Carlson54e0a672011-05-19 12:12:50 +000011560 >> RXD_TCPCSUM_SHIFT != 0xffff) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011561 goto out;
11562 }
11563
11564 if (opaque_key == RXD_OPAQUE_RING_STD) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000011565 rx_data = tpr->rx_std_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011566 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
11567 mapping);
11568 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000011569 rx_data = tpr->rx_jmb_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011570 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
11571 mapping);
11572 } else
Matt Carlson4852a862011-04-13 11:05:07 +000011573 goto out;
11574
Matt Carlsonbb158d62011-04-25 12:42:47 +000011575 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
11576 PCI_DMA_FROMDEVICE);
11577
Eric Dumazet9205fd92011-11-18 06:47:01 +000011578 rx_data += TG3_RX_OFFSET(tp);
Matt Carlsonbb158d62011-04-25 12:42:47 +000011579 for (i = data_off; i < rx_len; i++, val++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000011580 if (*(rx_data + i) != (u8) (val & 0xff))
Matt Carlsonbb158d62011-04-25 12:42:47 +000011581 goto out;
11582 }
Matt Carlson4852a862011-04-13 11:05:07 +000011583 }
11584
Michael Chanc76949a2005-05-29 14:58:59 -070011585 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011586
Eric Dumazet9205fd92011-11-18 06:47:01 +000011587 /* tg3_free_rings will unmap and free the rx_data */
Michael Chanc76949a2005-05-29 14:58:59 -070011588out:
11589 return err;
11590}
11591
Matt Carlson00c266b2011-04-25 12:42:46 +000011592#define TG3_STD_LOOPBACK_FAILED 1
11593#define TG3_JMB_LOOPBACK_FAILED 2
Matt Carlsonbb158d62011-04-25 12:42:47 +000011594#define TG3_TSO_LOOPBACK_FAILED 4
Matt Carlson28a45952011-08-19 13:58:22 +000011595#define TG3_LOOPBACK_FAILED \
11596 (TG3_STD_LOOPBACK_FAILED | \
11597 TG3_JMB_LOOPBACK_FAILED | \
11598 TG3_TSO_LOOPBACK_FAILED)
Matt Carlson00c266b2011-04-25 12:42:46 +000011599
Matt Carlson941ec902011-08-19 13:58:23 +000011600static int tg3_test_loopback(struct tg3 *tp, u64 *data, bool do_extlpbk)
Michael Chan9f40dea2005-09-05 17:53:06 -070011601{
Matt Carlson28a45952011-08-19 13:58:22 +000011602 int err = -EIO;
Matt Carlson2215e242011-08-19 13:58:19 +000011603 u32 eee_cap;
Michael Chan9f40dea2005-09-05 17:53:06 -070011604
Matt Carlsonab789042011-01-25 15:58:54 +000011605 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
11606 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
11607
Matt Carlson28a45952011-08-19 13:58:22 +000011608 if (!netif_running(tp->dev)) {
11609 data[0] = TG3_LOOPBACK_FAILED;
11610 data[1] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000011611 if (do_extlpbk)
11612 data[2] = TG3_LOOPBACK_FAILED;
Matt Carlson28a45952011-08-19 13:58:22 +000011613 goto done;
11614 }
11615
Michael Chanb9ec6c12006-07-25 16:37:27 -070011616 err = tg3_reset_hw(tp, 1);
Matt Carlsonab789042011-01-25 15:58:54 +000011617 if (err) {
Matt Carlson28a45952011-08-19 13:58:22 +000011618 data[0] = TG3_LOOPBACK_FAILED;
11619 data[1] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000011620 if (do_extlpbk)
11621 data[2] = TG3_LOOPBACK_FAILED;
Matt Carlsonab789042011-01-25 15:58:54 +000011622 goto done;
11623 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011624
Joe Perches63c3a662011-04-26 08:12:10 +000011625 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson4a85f092011-04-20 07:57:37 +000011626 int i;
11627
11628 /* Reroute all rx packets to the 1st queue */
11629 for (i = MAC_RSS_INDIR_TBL_0;
11630 i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
11631 tw32(i, 0x0);
11632 }
11633
Matt Carlson6e01b202011-08-19 13:58:20 +000011634 /* HW errata - mac loopback fails in some cases on 5780.
11635 * Normal traffic and PHY loopback are not affected by
11636 * errata. Also, the MAC loopback test is deprecated for
11637 * all newer ASIC revisions.
11638 */
11639 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
11640 !tg3_flag(tp, CPMU_PRESENT)) {
11641 tg3_mac_loopback(tp, true);
Matt Carlson9936bcf2007-10-10 18:03:07 -070011642
Matt Carlson28a45952011-08-19 13:58:22 +000011643 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
11644 data[0] |= TG3_STD_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000011645
11646 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000011647 tg3_run_loopback(tp, 9000 + ETH_HLEN, false))
11648 data[0] |= TG3_JMB_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000011649
11650 tg3_mac_loopback(tp, false);
11651 }
Matt Carlson4852a862011-04-13 11:05:07 +000011652
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011653 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000011654 !tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011655 int i;
11656
Matt Carlson941ec902011-08-19 13:58:23 +000011657 tg3_phy_lpbk_set(tp, 0, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011658
11659 /* Wait for link */
11660 for (i = 0; i < 100; i++) {
11661 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
11662 break;
11663 mdelay(1);
11664 }
11665
Matt Carlson28a45952011-08-19 13:58:22 +000011666 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
11667 data[1] |= TG3_STD_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000011668 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000011669 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
11670 data[1] |= TG3_TSO_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000011671 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000011672 tg3_run_loopback(tp, 9000 + ETH_HLEN, false))
11673 data[1] |= TG3_JMB_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070011674
Matt Carlson941ec902011-08-19 13:58:23 +000011675 if (do_extlpbk) {
11676 tg3_phy_lpbk_set(tp, 0, true);
11677
11678 /* All link indications report up, but the hardware
11679 * isn't really ready for about 20 msec. Double it
11680 * to be sure.
11681 */
11682 mdelay(40);
11683
11684 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
11685 data[2] |= TG3_STD_LOOPBACK_FAILED;
11686 if (tg3_flag(tp, TSO_CAPABLE) &&
11687 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
11688 data[2] |= TG3_TSO_LOOPBACK_FAILED;
11689 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
11690 tg3_run_loopback(tp, 9000 + ETH_HLEN, false))
11691 data[2] |= TG3_JMB_LOOPBACK_FAILED;
11692 }
11693
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011694 /* Re-enable gphy autopowerdown. */
11695 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
11696 tg3_phy_toggle_apd(tp, true);
11697 }
Matt Carlson6833c042008-11-21 17:18:59 -080011698
Matt Carlson941ec902011-08-19 13:58:23 +000011699 err = (data[0] | data[1] | data[2]) ? -EIO : 0;
Matt Carlson28a45952011-08-19 13:58:22 +000011700
Matt Carlsonab789042011-01-25 15:58:54 +000011701done:
11702 tp->phy_flags |= eee_cap;
11703
Michael Chan9f40dea2005-09-05 17:53:06 -070011704 return err;
11705}
11706
Michael Chan4cafd3f2005-05-29 14:56:34 -070011707static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
11708 u64 *data)
11709{
Michael Chan566f86a2005-05-29 14:56:58 -070011710 struct tg3 *tp = netdev_priv(dev);
Matt Carlson941ec902011-08-19 13:58:23 +000011711 bool doextlpbk = etest->flags & ETH_TEST_FL_EXTERNAL_LB;
Michael Chan566f86a2005-05-29 14:56:58 -070011712
Matt Carlsonbed98292011-07-13 09:27:29 +000011713 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
11714 tg3_power_up(tp)) {
11715 etest->flags |= ETH_TEST_FL_FAILED;
11716 memset(data, 1, sizeof(u64) * TG3_NUM_TEST);
11717 return;
11718 }
Michael Chanbc1c7562006-03-20 17:48:03 -080011719
Michael Chan566f86a2005-05-29 14:56:58 -070011720 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
11721
11722 if (tg3_test_nvram(tp) != 0) {
11723 etest->flags |= ETH_TEST_FL_FAILED;
11724 data[0] = 1;
11725 }
Matt Carlson941ec902011-08-19 13:58:23 +000011726 if (!doextlpbk && tg3_test_link(tp)) {
Michael Chanca430072005-05-29 14:57:23 -070011727 etest->flags |= ETH_TEST_FL_FAILED;
11728 data[1] = 1;
11729 }
Michael Chana71116d2005-05-29 14:58:11 -070011730 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011731 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070011732
Michael Chanbbe832c2005-06-24 20:20:04 -070011733 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011734 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070011735 tg3_netif_stop(tp);
11736 irq_sync = 1;
11737 }
11738
11739 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070011740
11741 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080011742 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011743 tg3_halt_cpu(tp, RX_CPU_BASE);
Joe Perches63c3a662011-04-26 08:12:10 +000011744 if (!tg3_flag(tp, 5705_PLUS))
Michael Chana71116d2005-05-29 14:58:11 -070011745 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080011746 if (!err)
11747 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011748
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011749 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad12006-03-20 22:27:35 -080011750 tg3_phy_reset(tp);
11751
Michael Chana71116d2005-05-29 14:58:11 -070011752 if (tg3_test_registers(tp) != 0) {
11753 etest->flags |= ETH_TEST_FL_FAILED;
11754 data[2] = 1;
11755 }
Matt Carlson28a45952011-08-19 13:58:22 +000011756
Michael Chan7942e1d2005-05-29 14:58:36 -070011757 if (tg3_test_memory(tp) != 0) {
11758 etest->flags |= ETH_TEST_FL_FAILED;
11759 data[3] = 1;
11760 }
Matt Carlson28a45952011-08-19 13:58:22 +000011761
Matt Carlson941ec902011-08-19 13:58:23 +000011762 if (doextlpbk)
11763 etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
11764
11765 if (tg3_test_loopback(tp, &data[4], doextlpbk))
Michael Chanc76949a2005-05-29 14:58:59 -070011766 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070011767
David S. Millerf47c11e2005-06-24 20:18:35 -070011768 tg3_full_unlock(tp);
11769
Michael Chand4bc3922005-05-29 14:59:20 -070011770 if (tg3_test_interrupt(tp) != 0) {
11771 etest->flags |= ETH_TEST_FL_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000011772 data[7] = 1;
Michael Chand4bc3922005-05-29 14:59:20 -070011773 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011774
11775 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070011776
Michael Chana71116d2005-05-29 14:58:11 -070011777 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
11778 if (netif_running(dev)) {
Joe Perches63c3a662011-04-26 08:12:10 +000011779 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011780 err2 = tg3_restart_hw(tp, 1);
11781 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070011782 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011783 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011784
11785 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011786
11787 if (irq_sync && !err2)
11788 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011789 }
Matt Carlson80096062010-08-02 11:26:06 +000011790 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000011791 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080011792
Michael Chan4cafd3f2005-05-29 14:56:34 -070011793}
11794
Linus Torvalds1da177e2005-04-16 15:20:36 -070011795static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
11796{
11797 struct mii_ioctl_data *data = if_mii(ifr);
11798 struct tg3 *tp = netdev_priv(dev);
11799 int err;
11800
Joe Perches63c3a662011-04-26 08:12:10 +000011801 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011802 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011803 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011804 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011805 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000011806 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011807 }
11808
Matt Carlson33f401a2010-04-05 10:19:27 +000011809 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011810 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000011811 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011812
11813 /* fallthru */
11814 case SIOCGMIIREG: {
11815 u32 mii_regval;
11816
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011817 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011818 break; /* We have no PHY */
11819
Matt Carlson34eea5a2011-04-20 07:57:38 +000011820 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080011821 return -EAGAIN;
11822
David S. Millerf47c11e2005-06-24 20:18:35 -070011823 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011824 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070011825 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011826
11827 data->val_out = mii_regval;
11828
11829 return err;
11830 }
11831
11832 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011833 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011834 break; /* We have no PHY */
11835
Matt Carlson34eea5a2011-04-20 07:57:38 +000011836 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080011837 return -EAGAIN;
11838
David S. Millerf47c11e2005-06-24 20:18:35 -070011839 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011840 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070011841 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011842
11843 return err;
11844
11845 default:
11846 /* do nothing */
11847 break;
11848 }
11849 return -EOPNOTSUPP;
11850}
11851
David S. Miller15f98502005-05-18 22:49:26 -070011852static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11853{
11854 struct tg3 *tp = netdev_priv(dev);
11855
11856 memcpy(ec, &tp->coal, sizeof(*ec));
11857 return 0;
11858}
11859
Michael Chand244c892005-07-05 14:42:33 -070011860static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11861{
11862 struct tg3 *tp = netdev_priv(dev);
11863 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
11864 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
11865
Joe Perches63c3a662011-04-26 08:12:10 +000011866 if (!tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070011867 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
11868 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
11869 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
11870 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
11871 }
11872
11873 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
11874 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
11875 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
11876 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
11877 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
11878 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
11879 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
11880 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
11881 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
11882 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
11883 return -EINVAL;
11884
11885 /* No rx interrupts will be generated if both are zero */
11886 if ((ec->rx_coalesce_usecs == 0) &&
11887 (ec->rx_max_coalesced_frames == 0))
11888 return -EINVAL;
11889
11890 /* No tx interrupts will be generated if both are zero */
11891 if ((ec->tx_coalesce_usecs == 0) &&
11892 (ec->tx_max_coalesced_frames == 0))
11893 return -EINVAL;
11894
11895 /* Only copy relevant parameters, ignore all others. */
11896 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
11897 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
11898 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
11899 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
11900 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
11901 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
11902 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
11903 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
11904 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
11905
11906 if (netif_running(dev)) {
11907 tg3_full_lock(tp, 0);
11908 __tg3_set_coalesce(tp, &tp->coal);
11909 tg3_full_unlock(tp);
11910 }
11911 return 0;
11912}
11913
Jeff Garzik7282d492006-09-13 14:30:00 -040011914static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011915 .get_settings = tg3_get_settings,
11916 .set_settings = tg3_set_settings,
11917 .get_drvinfo = tg3_get_drvinfo,
11918 .get_regs_len = tg3_get_regs_len,
11919 .get_regs = tg3_get_regs,
11920 .get_wol = tg3_get_wol,
11921 .set_wol = tg3_set_wol,
11922 .get_msglevel = tg3_get_msglevel,
11923 .set_msglevel = tg3_set_msglevel,
11924 .nway_reset = tg3_nway_reset,
11925 .get_link = ethtool_op_get_link,
11926 .get_eeprom_len = tg3_get_eeprom_len,
11927 .get_eeprom = tg3_get_eeprom,
11928 .set_eeprom = tg3_set_eeprom,
11929 .get_ringparam = tg3_get_ringparam,
11930 .set_ringparam = tg3_set_ringparam,
11931 .get_pauseparam = tg3_get_pauseparam,
11932 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070011933 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011934 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000011935 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011936 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070011937 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070011938 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070011939 .get_sset_count = tg3_get_sset_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011940};
11941
11942static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
11943{
Michael Chan1b277772006-03-20 22:27:48 -080011944 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011945
11946 tp->nvram_size = EEPROM_CHIP_SIZE;
11947
Matt Carlsone4f34112009-02-25 14:25:00 +000011948 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011949 return;
11950
Michael Chanb16250e2006-09-27 16:10:14 -070011951 if ((magic != TG3_EEPROM_MAGIC) &&
11952 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
11953 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011954 return;
11955
11956 /*
11957 * Size the chip by reading offsets at increasing powers of two.
11958 * When we encounter our validation signature, we know the addressing
11959 * has wrapped around, and thus have our chip size.
11960 */
Michael Chan1b277772006-03-20 22:27:48 -080011961 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011962
11963 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000011964 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011965 return;
11966
Michael Chan18201802006-03-20 22:29:15 -080011967 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011968 break;
11969
11970 cursize <<= 1;
11971 }
11972
11973 tp->nvram_size = cursize;
11974}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011975
Linus Torvalds1da177e2005-04-16 15:20:36 -070011976static void __devinit tg3_get_nvram_size(struct tg3 *tp)
11977{
11978 u32 val;
11979
Joe Perches63c3a662011-04-26 08:12:10 +000011980 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080011981 return;
11982
11983 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080011984 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080011985 tg3_get_eeprom_size(tp);
11986 return;
11987 }
11988
Matt Carlson6d348f22009-02-25 14:25:52 +000011989 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011990 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000011991 /* This is confusing. We want to operate on the
11992 * 16-bit value at offset 0xf2. The tg3_nvram_read()
11993 * call will read from NVRAM and byteswap the data
11994 * according to the byteswapping settings for all
11995 * other register accesses. This ensures the data we
11996 * want will always reside in the lower 16-bits.
11997 * However, the data in NVRAM is in LE format, which
11998 * means the data from the NVRAM read will always be
11999 * opposite the endianness of the CPU. The 16-bit
12000 * byteswap then brings the data to CPU endianness.
12001 */
12002 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012003 return;
12004 }
12005 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070012006 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012007}
12008
12009static void __devinit tg3_get_nvram_info(struct tg3 *tp)
12010{
12011 u32 nvcfg1;
12012
12013 nvcfg1 = tr32(NVRAM_CFG1);
12014 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
Joe Perches63c3a662011-04-26 08:12:10 +000012015 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012016 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012017 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12018 tw32(NVRAM_CFG1, nvcfg1);
12019 }
12020
Matt Carlson6ff6f812011-05-19 12:12:54 +000012021 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
Joe Perches63c3a662011-04-26 08:12:10 +000012022 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012023 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012024 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
12025 tp->nvram_jedecnum = JEDEC_ATMEL;
12026 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012027 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012028 break;
12029 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
12030 tp->nvram_jedecnum = JEDEC_ATMEL;
12031 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
12032 break;
12033 case FLASH_VENDOR_ATMEL_EEPROM:
12034 tp->nvram_jedecnum = JEDEC_ATMEL;
12035 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012036 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012037 break;
12038 case FLASH_VENDOR_ST:
12039 tp->nvram_jedecnum = JEDEC_ST;
12040 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012041 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012042 break;
12043 case FLASH_VENDOR_SAIFUN:
12044 tp->nvram_jedecnum = JEDEC_SAIFUN;
12045 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
12046 break;
12047 case FLASH_VENDOR_SST_SMALL:
12048 case FLASH_VENDOR_SST_LARGE:
12049 tp->nvram_jedecnum = JEDEC_SST;
12050 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
12051 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012052 }
Matt Carlson8590a602009-08-28 12:29:16 +000012053 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012054 tp->nvram_jedecnum = JEDEC_ATMEL;
12055 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012056 tg3_flag_set(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012057 }
12058}
12059
Matt Carlsona1b950d2009-09-01 13:20:17 +000012060static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
12061{
12062 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
12063 case FLASH_5752PAGE_SIZE_256:
12064 tp->nvram_pagesize = 256;
12065 break;
12066 case FLASH_5752PAGE_SIZE_512:
12067 tp->nvram_pagesize = 512;
12068 break;
12069 case FLASH_5752PAGE_SIZE_1K:
12070 tp->nvram_pagesize = 1024;
12071 break;
12072 case FLASH_5752PAGE_SIZE_2K:
12073 tp->nvram_pagesize = 2048;
12074 break;
12075 case FLASH_5752PAGE_SIZE_4K:
12076 tp->nvram_pagesize = 4096;
12077 break;
12078 case FLASH_5752PAGE_SIZE_264:
12079 tp->nvram_pagesize = 264;
12080 break;
12081 case FLASH_5752PAGE_SIZE_528:
12082 tp->nvram_pagesize = 528;
12083 break;
12084 }
12085}
12086
Michael Chan361b4ac2005-04-21 17:11:21 -070012087static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
12088{
12089 u32 nvcfg1;
12090
12091 nvcfg1 = tr32(NVRAM_CFG1);
12092
Michael Chane6af3012005-04-21 17:12:05 -070012093 /* NVRAM protection for TPM */
12094 if (nvcfg1 & (1 << 27))
Joe Perches63c3a662011-04-26 08:12:10 +000012095 tg3_flag_set(tp, PROTECTED_NVRAM);
Michael Chane6af3012005-04-21 17:12:05 -070012096
Michael Chan361b4ac2005-04-21 17:11:21 -070012097 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012098 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
12099 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
12100 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012101 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012102 break;
12103 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12104 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012105 tg3_flag_set(tp, NVRAM_BUFFERED);
12106 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012107 break;
12108 case FLASH_5752VENDOR_ST_M45PE10:
12109 case FLASH_5752VENDOR_ST_M45PE20:
12110 case FLASH_5752VENDOR_ST_M45PE40:
12111 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012112 tg3_flag_set(tp, NVRAM_BUFFERED);
12113 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012114 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070012115 }
12116
Joe Perches63c3a662011-04-26 08:12:10 +000012117 if (tg3_flag(tp, FLASH)) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000012118 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000012119 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070012120 /* For eeprom, set pagesize to maximum eeprom size */
12121 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12122
12123 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12124 tw32(NVRAM_CFG1, nvcfg1);
12125 }
12126}
12127
Michael Chand3c7b882006-03-23 01:28:25 -080012128static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
12129{
Matt Carlson989a9d22007-05-05 11:51:05 -070012130 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080012131
12132 nvcfg1 = tr32(NVRAM_CFG1);
12133
12134 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070012135 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012136 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson989a9d22007-05-05 11:51:05 -070012137 protect = 1;
12138 }
Michael Chand3c7b882006-03-23 01:28:25 -080012139
Matt Carlson989a9d22007-05-05 11:51:05 -070012140 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
12141 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012142 case FLASH_5755VENDOR_ATMEL_FLASH_1:
12143 case FLASH_5755VENDOR_ATMEL_FLASH_2:
12144 case FLASH_5755VENDOR_ATMEL_FLASH_3:
12145 case FLASH_5755VENDOR_ATMEL_FLASH_5:
12146 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012147 tg3_flag_set(tp, NVRAM_BUFFERED);
12148 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012149 tp->nvram_pagesize = 264;
12150 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
12151 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
12152 tp->nvram_size = (protect ? 0x3e200 :
12153 TG3_NVRAM_SIZE_512KB);
12154 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
12155 tp->nvram_size = (protect ? 0x1f200 :
12156 TG3_NVRAM_SIZE_256KB);
12157 else
12158 tp->nvram_size = (protect ? 0x1f200 :
12159 TG3_NVRAM_SIZE_128KB);
12160 break;
12161 case FLASH_5752VENDOR_ST_M45PE10:
12162 case FLASH_5752VENDOR_ST_M45PE20:
12163 case FLASH_5752VENDOR_ST_M45PE40:
12164 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012165 tg3_flag_set(tp, NVRAM_BUFFERED);
12166 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012167 tp->nvram_pagesize = 256;
12168 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
12169 tp->nvram_size = (protect ?
12170 TG3_NVRAM_SIZE_64KB :
12171 TG3_NVRAM_SIZE_128KB);
12172 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
12173 tp->nvram_size = (protect ?
12174 TG3_NVRAM_SIZE_64KB :
12175 TG3_NVRAM_SIZE_256KB);
12176 else
12177 tp->nvram_size = (protect ?
12178 TG3_NVRAM_SIZE_128KB :
12179 TG3_NVRAM_SIZE_512KB);
12180 break;
Michael Chand3c7b882006-03-23 01:28:25 -080012181 }
12182}
12183
Michael Chan1b277772006-03-20 22:27:48 -080012184static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
12185{
12186 u32 nvcfg1;
12187
12188 nvcfg1 = tr32(NVRAM_CFG1);
12189
12190 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012191 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
12192 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
12193 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
12194 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
12195 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012196 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012197 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080012198
Matt Carlson8590a602009-08-28 12:29:16 +000012199 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12200 tw32(NVRAM_CFG1, nvcfg1);
12201 break;
12202 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12203 case FLASH_5755VENDOR_ATMEL_FLASH_1:
12204 case FLASH_5755VENDOR_ATMEL_FLASH_2:
12205 case FLASH_5755VENDOR_ATMEL_FLASH_3:
12206 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012207 tg3_flag_set(tp, NVRAM_BUFFERED);
12208 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012209 tp->nvram_pagesize = 264;
12210 break;
12211 case FLASH_5752VENDOR_ST_M45PE10:
12212 case FLASH_5752VENDOR_ST_M45PE20:
12213 case FLASH_5752VENDOR_ST_M45PE40:
12214 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012215 tg3_flag_set(tp, NVRAM_BUFFERED);
12216 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012217 tp->nvram_pagesize = 256;
12218 break;
Michael Chan1b277772006-03-20 22:27:48 -080012219 }
12220}
12221
Matt Carlson6b91fa02007-10-10 18:01:09 -070012222static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
12223{
12224 u32 nvcfg1, protect = 0;
12225
12226 nvcfg1 = tr32(NVRAM_CFG1);
12227
12228 /* NVRAM protection for TPM */
12229 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012230 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson6b91fa02007-10-10 18:01:09 -070012231 protect = 1;
12232 }
12233
12234 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
12235 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012236 case FLASH_5761VENDOR_ATMEL_ADB021D:
12237 case FLASH_5761VENDOR_ATMEL_ADB041D:
12238 case FLASH_5761VENDOR_ATMEL_ADB081D:
12239 case FLASH_5761VENDOR_ATMEL_ADB161D:
12240 case FLASH_5761VENDOR_ATMEL_MDB021D:
12241 case FLASH_5761VENDOR_ATMEL_MDB041D:
12242 case FLASH_5761VENDOR_ATMEL_MDB081D:
12243 case FLASH_5761VENDOR_ATMEL_MDB161D:
12244 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012245 tg3_flag_set(tp, NVRAM_BUFFERED);
12246 tg3_flag_set(tp, FLASH);
12247 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson8590a602009-08-28 12:29:16 +000012248 tp->nvram_pagesize = 256;
12249 break;
12250 case FLASH_5761VENDOR_ST_A_M45PE20:
12251 case FLASH_5761VENDOR_ST_A_M45PE40:
12252 case FLASH_5761VENDOR_ST_A_M45PE80:
12253 case FLASH_5761VENDOR_ST_A_M45PE16:
12254 case FLASH_5761VENDOR_ST_M_M45PE20:
12255 case FLASH_5761VENDOR_ST_M_M45PE40:
12256 case FLASH_5761VENDOR_ST_M_M45PE80:
12257 case FLASH_5761VENDOR_ST_M_M45PE16:
12258 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012259 tg3_flag_set(tp, NVRAM_BUFFERED);
12260 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012261 tp->nvram_pagesize = 256;
12262 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070012263 }
12264
12265 if (protect) {
12266 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
12267 } else {
12268 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012269 case FLASH_5761VENDOR_ATMEL_ADB161D:
12270 case FLASH_5761VENDOR_ATMEL_MDB161D:
12271 case FLASH_5761VENDOR_ST_A_M45PE16:
12272 case FLASH_5761VENDOR_ST_M_M45PE16:
12273 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
12274 break;
12275 case FLASH_5761VENDOR_ATMEL_ADB081D:
12276 case FLASH_5761VENDOR_ATMEL_MDB081D:
12277 case FLASH_5761VENDOR_ST_A_M45PE80:
12278 case FLASH_5761VENDOR_ST_M_M45PE80:
12279 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12280 break;
12281 case FLASH_5761VENDOR_ATMEL_ADB041D:
12282 case FLASH_5761VENDOR_ATMEL_MDB041D:
12283 case FLASH_5761VENDOR_ST_A_M45PE40:
12284 case FLASH_5761VENDOR_ST_M_M45PE40:
12285 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12286 break;
12287 case FLASH_5761VENDOR_ATMEL_ADB021D:
12288 case FLASH_5761VENDOR_ATMEL_MDB021D:
12289 case FLASH_5761VENDOR_ST_A_M45PE20:
12290 case FLASH_5761VENDOR_ST_M_M45PE20:
12291 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12292 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070012293 }
12294 }
12295}
12296
Michael Chanb5d37722006-09-27 16:06:21 -070012297static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
12298{
12299 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012300 tg3_flag_set(tp, NVRAM_BUFFERED);
Michael Chanb5d37722006-09-27 16:06:21 -070012301 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12302}
12303
Matt Carlson321d32a2008-11-21 17:22:19 -080012304static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
12305{
12306 u32 nvcfg1;
12307
12308 nvcfg1 = tr32(NVRAM_CFG1);
12309
12310 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12311 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
12312 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
12313 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012314 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson321d32a2008-11-21 17:22:19 -080012315 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12316
12317 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12318 tw32(NVRAM_CFG1, nvcfg1);
12319 return;
12320 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12321 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
12322 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
12323 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
12324 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
12325 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
12326 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
12327 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012328 tg3_flag_set(tp, NVRAM_BUFFERED);
12329 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080012330
12331 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12332 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12333 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
12334 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
12335 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12336 break;
12337 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
12338 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
12339 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12340 break;
12341 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
12342 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
12343 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12344 break;
12345 }
12346 break;
12347 case FLASH_5752VENDOR_ST_M45PE10:
12348 case FLASH_5752VENDOR_ST_M45PE20:
12349 case FLASH_5752VENDOR_ST_M45PE40:
12350 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012351 tg3_flag_set(tp, NVRAM_BUFFERED);
12352 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080012353
12354 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12355 case FLASH_5752VENDOR_ST_M45PE10:
12356 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12357 break;
12358 case FLASH_5752VENDOR_ST_M45PE20:
12359 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12360 break;
12361 case FLASH_5752VENDOR_ST_M45PE40:
12362 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12363 break;
12364 }
12365 break;
12366 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012367 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson321d32a2008-11-21 17:22:19 -080012368 return;
12369 }
12370
Matt Carlsona1b950d2009-09-01 13:20:17 +000012371 tg3_nvram_get_pagesize(tp, nvcfg1);
12372 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012373 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012374}
12375
12376
12377static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
12378{
12379 u32 nvcfg1;
12380
12381 nvcfg1 = tr32(NVRAM_CFG1);
12382
12383 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12384 case FLASH_5717VENDOR_ATMEL_EEPROM:
12385 case FLASH_5717VENDOR_MICRO_EEPROM:
12386 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012387 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012388 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12389
12390 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12391 tw32(NVRAM_CFG1, nvcfg1);
12392 return;
12393 case FLASH_5717VENDOR_ATMEL_MDB011D:
12394 case FLASH_5717VENDOR_ATMEL_ADB011B:
12395 case FLASH_5717VENDOR_ATMEL_ADB011D:
12396 case FLASH_5717VENDOR_ATMEL_MDB021D:
12397 case FLASH_5717VENDOR_ATMEL_ADB021B:
12398 case FLASH_5717VENDOR_ATMEL_ADB021D:
12399 case FLASH_5717VENDOR_ATMEL_45USPT:
12400 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012401 tg3_flag_set(tp, NVRAM_BUFFERED);
12402 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012403
12404 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12405 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012406 /* Detect size with tg3_nvram_get_size() */
12407 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012408 case FLASH_5717VENDOR_ATMEL_ADB021B:
12409 case FLASH_5717VENDOR_ATMEL_ADB021D:
12410 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12411 break;
12412 default:
12413 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12414 break;
12415 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012416 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012417 case FLASH_5717VENDOR_ST_M_M25PE10:
12418 case FLASH_5717VENDOR_ST_A_M25PE10:
12419 case FLASH_5717VENDOR_ST_M_M45PE10:
12420 case FLASH_5717VENDOR_ST_A_M45PE10:
12421 case FLASH_5717VENDOR_ST_M_M25PE20:
12422 case FLASH_5717VENDOR_ST_A_M25PE20:
12423 case FLASH_5717VENDOR_ST_M_M45PE20:
12424 case FLASH_5717VENDOR_ST_A_M45PE20:
12425 case FLASH_5717VENDOR_ST_25USPT:
12426 case FLASH_5717VENDOR_ST_45USPT:
12427 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012428 tg3_flag_set(tp, NVRAM_BUFFERED);
12429 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012430
12431 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12432 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012433 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012434 /* Detect size with tg3_nvram_get_size() */
12435 break;
12436 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012437 case FLASH_5717VENDOR_ST_A_M45PE20:
12438 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12439 break;
12440 default:
12441 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12442 break;
12443 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012444 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012445 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012446 tg3_flag_set(tp, NO_NVRAM);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012447 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080012448 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000012449
12450 tg3_nvram_get_pagesize(tp, nvcfg1);
12451 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012452 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson321d32a2008-11-21 17:22:19 -080012453}
12454
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012455static void __devinit tg3_get_5720_nvram_info(struct tg3 *tp)
12456{
12457 u32 nvcfg1, nvmpinstrp;
12458
12459 nvcfg1 = tr32(NVRAM_CFG1);
12460 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
12461
12462 switch (nvmpinstrp) {
12463 case FLASH_5720_EEPROM_HD:
12464 case FLASH_5720_EEPROM_LD:
12465 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012466 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012467
12468 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12469 tw32(NVRAM_CFG1, nvcfg1);
12470 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
12471 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12472 else
12473 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
12474 return;
12475 case FLASH_5720VENDOR_M_ATMEL_DB011D:
12476 case FLASH_5720VENDOR_A_ATMEL_DB011B:
12477 case FLASH_5720VENDOR_A_ATMEL_DB011D:
12478 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12479 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12480 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12481 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12482 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12483 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12484 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12485 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12486 case FLASH_5720VENDOR_ATMEL_45USPT:
12487 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012488 tg3_flag_set(tp, NVRAM_BUFFERED);
12489 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012490
12491 switch (nvmpinstrp) {
12492 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12493 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12494 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12495 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12496 break;
12497 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12498 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12499 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12500 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12501 break;
12502 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12503 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12504 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12505 break;
12506 default:
12507 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12508 break;
12509 }
12510 break;
12511 case FLASH_5720VENDOR_M_ST_M25PE10:
12512 case FLASH_5720VENDOR_M_ST_M45PE10:
12513 case FLASH_5720VENDOR_A_ST_M25PE10:
12514 case FLASH_5720VENDOR_A_ST_M45PE10:
12515 case FLASH_5720VENDOR_M_ST_M25PE20:
12516 case FLASH_5720VENDOR_M_ST_M45PE20:
12517 case FLASH_5720VENDOR_A_ST_M25PE20:
12518 case FLASH_5720VENDOR_A_ST_M45PE20:
12519 case FLASH_5720VENDOR_M_ST_M25PE40:
12520 case FLASH_5720VENDOR_M_ST_M45PE40:
12521 case FLASH_5720VENDOR_A_ST_M25PE40:
12522 case FLASH_5720VENDOR_A_ST_M45PE40:
12523 case FLASH_5720VENDOR_M_ST_M25PE80:
12524 case FLASH_5720VENDOR_M_ST_M45PE80:
12525 case FLASH_5720VENDOR_A_ST_M25PE80:
12526 case FLASH_5720VENDOR_A_ST_M45PE80:
12527 case FLASH_5720VENDOR_ST_25USPT:
12528 case FLASH_5720VENDOR_ST_45USPT:
12529 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012530 tg3_flag_set(tp, NVRAM_BUFFERED);
12531 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012532
12533 switch (nvmpinstrp) {
12534 case FLASH_5720VENDOR_M_ST_M25PE20:
12535 case FLASH_5720VENDOR_M_ST_M45PE20:
12536 case FLASH_5720VENDOR_A_ST_M25PE20:
12537 case FLASH_5720VENDOR_A_ST_M45PE20:
12538 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12539 break;
12540 case FLASH_5720VENDOR_M_ST_M25PE40:
12541 case FLASH_5720VENDOR_M_ST_M45PE40:
12542 case FLASH_5720VENDOR_A_ST_M25PE40:
12543 case FLASH_5720VENDOR_A_ST_M45PE40:
12544 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12545 break;
12546 case FLASH_5720VENDOR_M_ST_M25PE80:
12547 case FLASH_5720VENDOR_M_ST_M45PE80:
12548 case FLASH_5720VENDOR_A_ST_M25PE80:
12549 case FLASH_5720VENDOR_A_ST_M45PE80:
12550 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12551 break;
12552 default:
12553 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12554 break;
12555 }
12556 break;
12557 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012558 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012559 return;
12560 }
12561
12562 tg3_nvram_get_pagesize(tp, nvcfg1);
12563 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012564 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012565}
12566
Linus Torvalds1da177e2005-04-16 15:20:36 -070012567/* Chips other than 5700/5701 use the NVRAM for fetching info. */
12568static void __devinit tg3_nvram_init(struct tg3 *tp)
12569{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012570 tw32_f(GRC_EEPROM_ADDR,
12571 (EEPROM_ADDR_FSM_RESET |
12572 (EEPROM_DEFAULT_CLOCK_PERIOD <<
12573 EEPROM_ADDR_CLKPERD_SHIFT)));
12574
Michael Chan9d57f012006-12-07 00:23:25 -080012575 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012576
12577 /* Enable seeprom accesses. */
12578 tw32_f(GRC_LOCAL_CTRL,
12579 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
12580 udelay(100);
12581
12582 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
12583 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
Joe Perches63c3a662011-04-26 08:12:10 +000012584 tg3_flag_set(tp, NVRAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012585
Michael Chanec41c7d2006-01-17 02:40:55 -080012586 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000012587 netdev_warn(tp->dev,
12588 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000012589 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080012590 return;
12591 }
Michael Chane6af3012005-04-21 17:12:05 -070012592 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012593
Matt Carlson989a9d22007-05-05 11:51:05 -070012594 tp->nvram_size = 0;
12595
Michael Chan361b4ac2005-04-21 17:11:21 -070012596 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
12597 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080012598 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
12599 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070012600 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070012601 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
12602 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080012603 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070012604 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
12605 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070012606 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12607 tg3_get_5906_nvram_info(tp);
Matt Carlsonb703df62009-12-03 08:36:21 +000012608 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
12609 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson321d32a2008-11-21 17:22:19 -080012610 tg3_get_57780_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012611 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
12612 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000012613 tg3_get_5717_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012614 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
12615 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070012616 else
12617 tg3_get_nvram_info(tp);
12618
Matt Carlson989a9d22007-05-05 11:51:05 -070012619 if (tp->nvram_size == 0)
12620 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012621
Michael Chane6af3012005-04-21 17:12:05 -070012622 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080012623 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012624
12625 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000012626 tg3_flag_clear(tp, NVRAM);
12627 tg3_flag_clear(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012628
12629 tg3_get_eeprom_size(tp);
12630 }
12631}
12632
Linus Torvalds1da177e2005-04-16 15:20:36 -070012633static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
12634 u32 offset, u32 len, u8 *buf)
12635{
12636 int i, j, rc = 0;
12637 u32 val;
12638
12639 for (i = 0; i < len; i += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012640 u32 addr;
Matt Carlsona9dc5292009-02-25 14:25:30 +000012641 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012642
12643 addr = offset + i;
12644
12645 memcpy(&data, buf + i, 4);
12646
Matt Carlson62cedd12009-04-20 14:52:29 -070012647 /*
12648 * The SEEPROM interface expects the data to always be opposite
12649 * the native endian format. We accomplish this by reversing
12650 * all the operations that would have been performed on the
12651 * data from a call to tg3_nvram_read_be32().
12652 */
12653 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012654
12655 val = tr32(GRC_EEPROM_ADDR);
12656 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
12657
12658 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
12659 EEPROM_ADDR_READ);
12660 tw32(GRC_EEPROM_ADDR, val |
12661 (0 << EEPROM_ADDR_DEVID_SHIFT) |
12662 (addr & EEPROM_ADDR_ADDR_MASK) |
12663 EEPROM_ADDR_START |
12664 EEPROM_ADDR_WRITE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012665
Michael Chan9d57f012006-12-07 00:23:25 -080012666 for (j = 0; j < 1000; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012667 val = tr32(GRC_EEPROM_ADDR);
12668
12669 if (val & EEPROM_ADDR_COMPLETE)
12670 break;
Michael Chan9d57f012006-12-07 00:23:25 -080012671 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012672 }
12673 if (!(val & EEPROM_ADDR_COMPLETE)) {
12674 rc = -EBUSY;
12675 break;
12676 }
12677 }
12678
12679 return rc;
12680}
12681
12682/* offset and length are dword aligned */
12683static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
12684 u8 *buf)
12685{
12686 int ret = 0;
12687 u32 pagesize = tp->nvram_pagesize;
12688 u32 pagemask = pagesize - 1;
12689 u32 nvram_cmd;
12690 u8 *tmp;
12691
12692 tmp = kmalloc(pagesize, GFP_KERNEL);
12693 if (tmp == NULL)
12694 return -ENOMEM;
12695
12696 while (len) {
12697 int j;
Michael Chane6af3012005-04-21 17:12:05 -070012698 u32 phy_addr, page_off, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012699
12700 phy_addr = offset & ~pagemask;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012701
Linus Torvalds1da177e2005-04-16 15:20:36 -070012702 for (j = 0; j < pagesize; j += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012703 ret = tg3_nvram_read_be32(tp, phy_addr + j,
12704 (__be32 *) (tmp + j));
12705 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012706 break;
12707 }
12708 if (ret)
12709 break;
12710
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012711 page_off = offset & pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012712 size = pagesize;
12713 if (len < size)
12714 size = len;
12715
12716 len -= size;
12717
12718 memcpy(tmp + page_off, buf, size);
12719
12720 offset = offset + (pagesize - page_off);
12721
Michael Chane6af3012005-04-21 17:12:05 -070012722 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012723
12724 /*
12725 * Before we can erase the flash page, we need
12726 * to issue a special "write enable" command.
12727 */
12728 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12729
12730 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12731 break;
12732
12733 /* Erase the target page */
12734 tw32(NVRAM_ADDR, phy_addr);
12735
12736 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
12737 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
12738
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012739 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012740 break;
12741
12742 /* Issue another write enable to start the write. */
12743 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12744
12745 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12746 break;
12747
12748 for (j = 0; j < pagesize; j += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012749 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012750
Al Virob9fc7dc2007-12-17 22:59:57 -080012751 data = *((__be32 *) (tmp + j));
Matt Carlsona9dc5292009-02-25 14:25:30 +000012752
Al Virob9fc7dc2007-12-17 22:59:57 -080012753 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012754
12755 tw32(NVRAM_ADDR, phy_addr + j);
12756
12757 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
12758 NVRAM_CMD_WR;
12759
12760 if (j == 0)
12761 nvram_cmd |= NVRAM_CMD_FIRST;
12762 else if (j == (pagesize - 4))
12763 nvram_cmd |= NVRAM_CMD_LAST;
12764
12765 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12766 break;
12767 }
12768 if (ret)
12769 break;
12770 }
12771
12772 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12773 tg3_nvram_exec_cmd(tp, nvram_cmd);
12774
12775 kfree(tmp);
12776
12777 return ret;
12778}
12779
12780/* offset and length are dword aligned */
12781static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
12782 u8 *buf)
12783{
12784 int i, ret = 0;
12785
12786 for (i = 0; i < len; i += 4, offset += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012787 u32 page_off, phy_addr, nvram_cmd;
12788 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012789
12790 memcpy(&data, buf + i, 4);
Al Virob9fc7dc2007-12-17 22:59:57 -080012791 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012792
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012793 page_off = offset % tp->nvram_pagesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012794
Michael Chan18201802006-03-20 22:29:15 -080012795 phy_addr = tg3_nvram_phys_addr(tp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012796
12797 tw32(NVRAM_ADDR, phy_addr);
12798
12799 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
12800
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012801 if (page_off == 0 || i == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012802 nvram_cmd |= NVRAM_CMD_FIRST;
Michael Chanf6d9a252006-04-29 19:00:24 -070012803 if (page_off == (tp->nvram_pagesize - 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012804 nvram_cmd |= NVRAM_CMD_LAST;
12805
12806 if (i == (len - 4))
12807 nvram_cmd |= NVRAM_CMD_LAST;
12808
Matt Carlson321d32a2008-11-21 17:22:19 -080012809 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
Joe Perches63c3a662011-04-26 08:12:10 +000012810 !tg3_flag(tp, 5755_PLUS) &&
Michael Chan4c987482005-09-05 17:52:38 -070012811 (tp->nvram_jedecnum == JEDEC_ST) &&
12812 (nvram_cmd & NVRAM_CMD_FIRST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012813
12814 if ((ret = tg3_nvram_exec_cmd(tp,
12815 NVRAM_CMD_WREN | NVRAM_CMD_GO |
12816 NVRAM_CMD_DONE)))
12817
12818 break;
12819 }
Joe Perches63c3a662011-04-26 08:12:10 +000012820 if (!tg3_flag(tp, FLASH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012821 /* We always do complete word writes to eeprom. */
12822 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
12823 }
12824
12825 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12826 break;
12827 }
12828 return ret;
12829}
12830
12831/* offset and length are dword aligned */
12832static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
12833{
12834 int ret;
12835
Joe Perches63c3a662011-04-26 08:12:10 +000012836 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
Michael Chan314fba32005-04-21 17:07:04 -070012837 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
12838 ~GRC_LCLCTRL_GPIO_OUTPUT1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012839 udelay(40);
12840 }
12841
Joe Perches63c3a662011-04-26 08:12:10 +000012842 if (!tg3_flag(tp, NVRAM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012843 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
Matt Carlson859a588792010-04-05 10:19:28 +000012844 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012845 u32 grc_mode;
12846
Michael Chanec41c7d2006-01-17 02:40:55 -080012847 ret = tg3_nvram_lock(tp);
12848 if (ret)
12849 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012850
Michael Chane6af3012005-04-21 17:12:05 -070012851 tg3_enable_nvram_access(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000012852 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012853 tw32(NVRAM_WRITE1, 0x406);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012854
12855 grc_mode = tr32(GRC_MODE);
12856 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
12857
Joe Perches63c3a662011-04-26 08:12:10 +000012858 if (tg3_flag(tp, NVRAM_BUFFERED) || !tg3_flag(tp, FLASH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012859 ret = tg3_nvram_write_block_buffered(tp, offset, len,
12860 buf);
Matt Carlson859a588792010-04-05 10:19:28 +000012861 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012862 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
12863 buf);
12864 }
12865
12866 grc_mode = tr32(GRC_MODE);
12867 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
12868
Michael Chane6af3012005-04-21 17:12:05 -070012869 tg3_disable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012870 tg3_nvram_unlock(tp);
12871 }
12872
Joe Perches63c3a662011-04-26 08:12:10 +000012873 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
Michael Chan314fba32005-04-21 17:07:04 -070012874 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012875 udelay(40);
12876 }
12877
12878 return ret;
12879}
12880
12881struct subsys_tbl_ent {
12882 u16 subsys_vendor, subsys_devid;
12883 u32 phy_id;
12884};
12885
Matt Carlson24daf2b2010-02-17 15:17:02 +000012886static struct subsys_tbl_ent subsys_id_to_phy_id[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012887 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012888 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012889 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012890 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012891 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012892 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012893 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012894 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12895 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
12896 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012897 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012898 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012899 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012900 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12901 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
12902 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012903 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012904 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012905 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012906 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012907 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012908 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012909 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012910
12911 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012912 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012913 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012914 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012915 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012916 { TG3PCI_SUBVENDOR_ID_3COM,
12917 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
12918 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012919 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012920 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012921 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012922
12923 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012924 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012925 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012926 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012927 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012928 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012929 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012930 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012931 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012932
12933 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012934 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012935 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012936 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012937 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012938 { TG3PCI_SUBVENDOR_ID_COMPAQ,
12939 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
12940 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012941 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012942 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012943 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012944
12945 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012946 { TG3PCI_SUBVENDOR_ID_IBM,
12947 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012948};
12949
Matt Carlson24daf2b2010-02-17 15:17:02 +000012950static struct subsys_tbl_ent * __devinit tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012951{
12952 int i;
12953
12954 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
12955 if ((subsys_id_to_phy_id[i].subsys_vendor ==
12956 tp->pdev->subsystem_vendor) &&
12957 (subsys_id_to_phy_id[i].subsys_devid ==
12958 tp->pdev->subsystem_device))
12959 return &subsys_id_to_phy_id[i];
12960 }
12961 return NULL;
12962}
12963
Michael Chan7d0c41e2005-04-21 17:06:20 -070012964static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012965{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012966 u32 val;
David S. Millerf49639e2006-06-09 11:58:36 -070012967
Matt Carlson79eb6902010-02-17 15:17:03 +000012968 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012969 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12970
Gary Zambranoa85feb82007-05-05 11:52:19 -070012971 /* Assume an onboard device and WOL capable by default. */
Joe Perches63c3a662011-04-26 08:12:10 +000012972 tg3_flag_set(tp, EEPROM_WRITE_PROT);
12973 tg3_flag_set(tp, WOL_CAP);
David S. Miller72b845e2006-03-14 14:11:48 -080012974
Michael Chanb5d37722006-09-27 16:06:21 -070012975 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080012976 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012977 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
12978 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080012979 }
Matt Carlson0527ba32007-10-10 18:03:30 -070012980 val = tr32(VCPU_CFGSHDW);
12981 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Joe Perches63c3a662011-04-26 08:12:10 +000012982 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson0527ba32007-10-10 18:03:30 -070012983 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000012984 (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012985 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000012986 device_set_wakeup_enable(&tp->pdev->dev, true);
12987 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012988 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070012989 }
12990
Linus Torvalds1da177e2005-04-16 15:20:36 -070012991 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
12992 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
12993 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070012994 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012995 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012996
12997 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
12998 tp->nic_sram_data_cfg = nic_cfg;
12999
13000 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
13001 ver >>= NIC_SRAM_DATA_VER_SHIFT;
Matt Carlson6ff6f812011-05-19 12:12:54 +000013002 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13003 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13004 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070013005 (ver > 0) && (ver < 0x100))
13006 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
13007
Matt Carlsona9daf362008-05-25 23:49:44 -070013008 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
13009 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
13010
Linus Torvalds1da177e2005-04-16 15:20:36 -070013011 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
13012 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
13013 eeprom_phy_serdes = 1;
13014
13015 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
13016 if (nic_phy_id != 0) {
13017 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
13018 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
13019
13020 eeprom_phy_id = (id1 >> 16) << 10;
13021 eeprom_phy_id |= (id2 & 0xfc00) << 16;
13022 eeprom_phy_id |= (id2 & 0x03ff) << 0;
13023 } else
13024 eeprom_phy_id = 0;
13025
Michael Chan7d0c41e2005-04-21 17:06:20 -070013026 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070013027 if (eeprom_phy_serdes) {
Joe Perches63c3a662011-04-26 08:12:10 +000013028 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013029 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000013030 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013031 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070013032 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070013033
Joe Perches63c3a662011-04-26 08:12:10 +000013034 if (tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013035 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
13036 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070013037 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070013038 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
13039
13040 switch (led_cfg) {
13041 default:
13042 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
13043 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13044 break;
13045
13046 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
13047 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
13048 break;
13049
13050 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
13051 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070013052
13053 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
13054 * read on some older 5700/5701 bootcode.
13055 */
13056 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
13057 ASIC_REV_5700 ||
13058 GET_ASIC_REV(tp->pci_chip_rev_id) ==
13059 ASIC_REV_5701)
13060 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13061
Linus Torvalds1da177e2005-04-16 15:20:36 -070013062 break;
13063
13064 case SHASTA_EXT_LED_SHARED:
13065 tp->led_ctrl = LED_CTRL_MODE_SHARED;
13066 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
13067 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
13068 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
13069 LED_CTRL_MODE_PHY_2);
13070 break;
13071
13072 case SHASTA_EXT_LED_MAC:
13073 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
13074 break;
13075
13076 case SHASTA_EXT_LED_COMBO:
13077 tp->led_ctrl = LED_CTRL_MODE_COMBO;
13078 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
13079 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
13080 LED_CTRL_MODE_PHY_2);
13081 break;
13082
Stephen Hemminger855e1112008-04-16 16:37:28 -070013083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013084
13085 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13086 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
13087 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
13088 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
13089
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013090 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
13091 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080013092
Michael Chan9d26e212006-12-07 00:21:14 -080013093 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Joe Perches63c3a662011-04-26 08:12:10 +000013094 tg3_flag_set(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080013095 if ((tp->pdev->subsystem_vendor ==
13096 PCI_VENDOR_ID_ARIMA) &&
13097 (tp->pdev->subsystem_device == 0x205a ||
13098 tp->pdev->subsystem_device == 0x2063))
Joe Perches63c3a662011-04-26 08:12:10 +000013099 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080013100 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000013101 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
13102 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080013103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013104
13105 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +000013106 tg3_flag_set(tp, ENABLE_ASF);
13107 if (tg3_flag(tp, 5750_PLUS))
13108 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013109 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080013110
13111 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013112 tg3_flag(tp, 5750_PLUS))
13113 tg3_flag_set(tp, ENABLE_APE);
Matt Carlsonb2b98d42008-11-03 16:52:32 -080013114
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013115 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070013116 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
Joe Perches63c3a662011-04-26 08:12:10 +000013117 tg3_flag_clear(tp, WOL_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013118
Joe Perches63c3a662011-04-26 08:12:10 +000013119 if (tg3_flag(tp, WOL_CAP) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013120 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013121 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013122 device_set_wakeup_enable(&tp->pdev->dev, true);
13123 }
Matt Carlson0527ba32007-10-10 18:03:30 -070013124
Linus Torvalds1da177e2005-04-16 15:20:36 -070013125 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013126 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013127
13128 /* serdes signal pre-emphasis in register 0x590 set by */
13129 /* bootcode if bit 18 is set */
13130 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013131 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070013132
Joe Perches63c3a662011-04-26 08:12:10 +000013133 if ((tg3_flag(tp, 57765_PLUS) ||
13134 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
13135 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080013136 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013137 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080013138
Joe Perches63c3a662011-04-26 08:12:10 +000013139 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson8c69b1e2010-08-02 11:26:00 +000013140 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +000013141 !tg3_flag(tp, 57765_PLUS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070013142 u32 cfg3;
13143
13144 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
13145 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
Joe Perches63c3a662011-04-26 08:12:10 +000013146 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson8ed5d972007-05-07 00:25:49 -070013147 }
Matt Carlsona9daf362008-05-25 23:49:44 -070013148
Matt Carlson14417062010-02-17 15:16:59 +000013149 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
Joe Perches63c3a662011-04-26 08:12:10 +000013150 tg3_flag_set(tp, RGMII_INBAND_DISABLE);
Matt Carlsona9daf362008-05-25 23:49:44 -070013151 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000013152 tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
Matt Carlsona9daf362008-05-25 23:49:44 -070013153 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000013154 tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013155 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080013156done:
Joe Perches63c3a662011-04-26 08:12:10 +000013157 if (tg3_flag(tp, WOL_CAP))
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000013158 device_set_wakeup_enable(&tp->pdev->dev,
Joe Perches63c3a662011-04-26 08:12:10 +000013159 tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000013160 else
13161 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070013162}
13163
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013164static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
13165{
13166 int i;
13167 u32 val;
13168
13169 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
13170 tw32(OTP_CTRL, cmd);
13171
13172 /* Wait for up to 1 ms for command to execute. */
13173 for (i = 0; i < 100; i++) {
13174 val = tr32(OTP_STATUS);
13175 if (val & OTP_STATUS_CMD_DONE)
13176 break;
13177 udelay(10);
13178 }
13179
13180 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
13181}
13182
13183/* Read the gphy configuration from the OTP region of the chip. The gphy
13184 * configuration is a 32-bit value that straddles the alignment boundary.
13185 * We do two 32-bit reads and then shift and merge the results.
13186 */
13187static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
13188{
13189 u32 bhalf_otp, thalf_otp;
13190
13191 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
13192
13193 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
13194 return 0;
13195
13196 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
13197
13198 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
13199 return 0;
13200
13201 thalf_otp = tr32(OTP_READ_DATA);
13202
13203 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
13204
13205 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
13206 return 0;
13207
13208 bhalf_otp = tr32(OTP_READ_DATA);
13209
13210 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
13211}
13212
Matt Carlsone256f8a2011-03-09 16:58:24 +000013213static void __devinit tg3_phy_init_link_config(struct tg3 *tp)
13214{
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +000013215 u32 adv = ADVERTISED_Autoneg;
Matt Carlsone256f8a2011-03-09 16:58:24 +000013216
13217 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
13218 adv |= ADVERTISED_1000baseT_Half |
13219 ADVERTISED_1000baseT_Full;
13220
13221 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
13222 adv |= ADVERTISED_100baseT_Half |
13223 ADVERTISED_100baseT_Full |
13224 ADVERTISED_10baseT_Half |
13225 ADVERTISED_10baseT_Full |
13226 ADVERTISED_TP;
13227 else
13228 adv |= ADVERTISED_FIBRE;
13229
13230 tp->link_config.advertising = adv;
13231 tp->link_config.speed = SPEED_INVALID;
13232 tp->link_config.duplex = DUPLEX_INVALID;
13233 tp->link_config.autoneg = AUTONEG_ENABLE;
13234 tp->link_config.active_speed = SPEED_INVALID;
13235 tp->link_config.active_duplex = DUPLEX_INVALID;
13236 tp->link_config.orig_speed = SPEED_INVALID;
13237 tp->link_config.orig_duplex = DUPLEX_INVALID;
13238 tp->link_config.orig_autoneg = AUTONEG_INVALID;
13239}
13240
Michael Chan7d0c41e2005-04-21 17:06:20 -070013241static int __devinit tg3_phy_probe(struct tg3 *tp)
13242{
13243 u32 hw_phy_id_1, hw_phy_id_2;
13244 u32 hw_phy_id, hw_phy_id_masked;
13245 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013246
Matt Carlsone256f8a2011-03-09 16:58:24 +000013247 /* flow control autonegotiation is default behavior */
Joe Perches63c3a662011-04-26 08:12:10 +000013248 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsone256f8a2011-03-09 16:58:24 +000013249 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
13250
Joe Perches63c3a662011-04-26 08:12:10 +000013251 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013252 return tg3_phy_init(tp);
13253
Linus Torvalds1da177e2005-04-16 15:20:36 -070013254 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010013255 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013256 */
13257 err = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000013258 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000013259 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013260 } else {
13261 /* Now read the physical PHY_ID from the chip and verify
13262 * that it is sane. If it doesn't look good, we fall back
13263 * to either the hard-coded table based PHY_ID and failing
13264 * that the value found in the eeprom area.
13265 */
13266 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
13267 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
13268
13269 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
13270 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
13271 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
13272
Matt Carlson79eb6902010-02-17 15:17:03 +000013273 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013274 }
13275
Matt Carlson79eb6902010-02-17 15:17:03 +000013276 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013277 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000013278 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013279 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070013280 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013281 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013282 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000013283 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070013284 /* Do nothing, phy ID already set up in
13285 * tg3_get_eeprom_hw_cfg().
13286 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070013287 } else {
13288 struct subsys_tbl_ent *p;
13289
13290 /* No eeprom signature? Try the hardcoded
13291 * subsys device table.
13292 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013293 p = tg3_lookup_by_subsys(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013294 if (!p)
13295 return -ENODEV;
13296
13297 tp->phy_id = p->phy_id;
13298 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000013299 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013300 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013301 }
13302 }
13303
Matt Carlsona6b68da2010-12-06 08:28:52 +000013304 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Matt Carlson5baa5e92011-07-20 10:20:53 +000013305 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13306 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720 ||
13307 (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +000013308 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0) ||
13309 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
13310 tp->pci_chip_rev_id != CHIPREV_ID_57765_A0)))
Matt Carlson52b02d02010-10-14 10:37:41 +000013311 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
13312
Matt Carlsone256f8a2011-03-09 16:58:24 +000013313 tg3_phy_init_link_config(tp);
13314
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013315 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013316 !tg3_flag(tp, ENABLE_APE) &&
13317 !tg3_flag(tp, ENABLE_ASF)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000013318 u32 bmsr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013319
13320 tg3_readphy(tp, MII_BMSR, &bmsr);
13321 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
13322 (bmsr & BMSR_LSTATUS))
13323 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013324
Linus Torvalds1da177e2005-04-16 15:20:36 -070013325 err = tg3_phy_reset(tp);
13326 if (err)
13327 return err;
13328
Matt Carlson42b64a42011-05-19 12:12:49 +000013329 tg3_phy_set_wirespeed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013330
Michael Chan3600d912006-12-07 00:21:48 -080013331 mask = (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
13332 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
13333 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full);
13334 if (!tg3_copper_is_advertising_all(tp, mask)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000013335 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
13336 tp->link_config.flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013337
13338 tg3_writephy(tp, MII_BMCR,
13339 BMCR_ANENABLE | BMCR_ANRESTART);
13340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013341 }
13342
13343skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000013344 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013345 err = tg3_init_5401phy_dsp(tp);
13346 if (err)
13347 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013348
Linus Torvalds1da177e2005-04-16 15:20:36 -070013349 err = tg3_init_5401phy_dsp(tp);
13350 }
13351
Linus Torvalds1da177e2005-04-16 15:20:36 -070013352 return err;
13353}
13354
Matt Carlson184b8902010-04-05 10:19:25 +000013355static void __devinit tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013356{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013357 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013358 unsigned int block_end, rosize, len;
Matt Carlson535a4902011-07-20 10:20:56 +000013359 u32 vpdlen;
Matt Carlson184b8902010-04-05 10:19:25 +000013360 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013361
Matt Carlson535a4902011-07-20 10:20:56 +000013362 vpd_data = (u8 *)tg3_vpd_readblock(tp, &vpdlen);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013363 if (!vpd_data)
13364 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013365
Matt Carlson535a4902011-07-20 10:20:56 +000013366 i = pci_vpd_find_tag(vpd_data, 0, vpdlen, PCI_VPD_LRDT_RO_DATA);
Matt Carlson4181b2c2010-02-26 14:04:45 +000013367 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013368 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013369
13370 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
13371 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
13372 i += PCI_VPD_LRDT_TAG_SIZE;
13373
Matt Carlson535a4902011-07-20 10:20:56 +000013374 if (block_end > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000013375 goto out_not_found;
13376
Matt Carlson184b8902010-04-05 10:19:25 +000013377 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13378 PCI_VPD_RO_KEYWORD_MFR_ID);
13379 if (j > 0) {
13380 len = pci_vpd_info_field_size(&vpd_data[j]);
13381
13382 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13383 if (j + len > block_end || len != 4 ||
13384 memcmp(&vpd_data[j], "1028", 4))
13385 goto partno;
13386
13387 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13388 PCI_VPD_RO_KEYWORD_VENDOR0);
13389 if (j < 0)
13390 goto partno;
13391
13392 len = pci_vpd_info_field_size(&vpd_data[j]);
13393
13394 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13395 if (j + len > block_end)
13396 goto partno;
13397
13398 memcpy(tp->fw_ver, &vpd_data[j], len);
Matt Carlson535a4902011-07-20 10:20:56 +000013399 strncat(tp->fw_ver, " bc ", vpdlen - len - 1);
Matt Carlson184b8902010-04-05 10:19:25 +000013400 }
13401
13402partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000013403 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13404 PCI_VPD_RO_KEYWORD_PARTNO);
13405 if (i < 0)
13406 goto out_not_found;
13407
13408 len = pci_vpd_info_field_size(&vpd_data[i]);
13409
13410 i += PCI_VPD_INFO_FLD_HDR_SIZE;
13411 if (len > TG3_BPN_SIZE ||
Matt Carlson535a4902011-07-20 10:20:56 +000013412 (len + i) > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000013413 goto out_not_found;
13414
13415 memcpy(tp->board_part_number, &vpd_data[i], len);
13416
Linus Torvalds1da177e2005-04-16 15:20:36 -070013417out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013418 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000013419 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013420 return;
13421
13422out_no_vpd:
Matt Carlson37a949c2010-09-30 10:34:33 +000013423 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
13424 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717)
13425 strcpy(tp->board_part_number, "BCM5717");
13426 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
13427 strcpy(tp->board_part_number, "BCM5718");
13428 else
13429 goto nomatch;
13430 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
13431 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
13432 strcpy(tp->board_part_number, "BCM57780");
13433 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
13434 strcpy(tp->board_part_number, "BCM57760");
13435 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
13436 strcpy(tp->board_part_number, "BCM57790");
13437 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
13438 strcpy(tp->board_part_number, "BCM57788");
13439 else
13440 goto nomatch;
13441 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
13442 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
13443 strcpy(tp->board_part_number, "BCM57761");
13444 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
13445 strcpy(tp->board_part_number, "BCM57765");
13446 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
13447 strcpy(tp->board_part_number, "BCM57781");
13448 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
13449 strcpy(tp->board_part_number, "BCM57785");
13450 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
13451 strcpy(tp->board_part_number, "BCM57791");
13452 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13453 strcpy(tp->board_part_number, "BCM57795");
13454 else
13455 goto nomatch;
13456 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070013457 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000013458 } else {
13459nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070013460 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000013461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013462}
13463
Matt Carlson9c8a6202007-10-21 16:16:08 -070013464static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
13465{
13466 u32 val;
13467
Matt Carlsone4f34112009-02-25 14:25:00 +000013468 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013469 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013470 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013471 val != 0)
13472 return 0;
13473
13474 return 1;
13475}
13476
Matt Carlsonacd9c112009-02-25 14:26:33 +000013477static void __devinit tg3_read_bc_ver(struct tg3 *tp)
13478{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013479 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000013480 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013481 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013482
13483 if (tg3_nvram_read(tp, 0xc, &offset) ||
13484 tg3_nvram_read(tp, 0x4, &start))
13485 return;
13486
13487 offset = tg3_nvram_logical_addr(tp, offset);
13488
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013489 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013490 return;
13491
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013492 if ((val & 0xfc000000) == 0x0c000000) {
13493 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013494 return;
13495
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013496 if (val == 0)
13497 newver = true;
13498 }
13499
Matt Carlson75f99362010-04-05 10:19:24 +000013500 dst_off = strlen(tp->fw_ver);
13501
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013502 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000013503 if (TG3_VER_SIZE - dst_off < 16 ||
13504 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013505 return;
13506
13507 offset = offset + ver_offset - start;
13508 for (i = 0; i < 16; i += 4) {
13509 __be32 v;
13510 if (tg3_nvram_read_be32(tp, offset + i, &v))
13511 return;
13512
Matt Carlson75f99362010-04-05 10:19:24 +000013513 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013514 }
13515 } else {
13516 u32 major, minor;
13517
13518 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
13519 return;
13520
13521 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
13522 TG3_NVM_BCVER_MAJSFT;
13523 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000013524 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
13525 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013526 }
13527}
13528
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013529static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
13530{
13531 u32 val, major, minor;
13532
13533 /* Use native endian representation */
13534 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
13535 return;
13536
13537 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
13538 TG3_NVM_HWSB_CFG1_MAJSFT;
13539 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
13540 TG3_NVM_HWSB_CFG1_MINSFT;
13541
13542 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
13543}
13544
Matt Carlsondfe00d72008-11-21 17:19:41 -080013545static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
13546{
13547 u32 offset, major, minor, build;
13548
Matt Carlson75f99362010-04-05 10:19:24 +000013549 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013550
13551 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
13552 return;
13553
13554 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
13555 case TG3_EEPROM_SB_REVISION_0:
13556 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
13557 break;
13558 case TG3_EEPROM_SB_REVISION_2:
13559 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
13560 break;
13561 case TG3_EEPROM_SB_REVISION_3:
13562 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
13563 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000013564 case TG3_EEPROM_SB_REVISION_4:
13565 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
13566 break;
13567 case TG3_EEPROM_SB_REVISION_5:
13568 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
13569 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000013570 case TG3_EEPROM_SB_REVISION_6:
13571 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
13572 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013573 default:
13574 return;
13575 }
13576
Matt Carlsone4f34112009-02-25 14:25:00 +000013577 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080013578 return;
13579
13580 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
13581 TG3_EEPROM_SB_EDH_BLD_SHFT;
13582 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
13583 TG3_EEPROM_SB_EDH_MAJ_SHFT;
13584 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
13585
13586 if (minor > 99 || build > 26)
13587 return;
13588
Matt Carlson75f99362010-04-05 10:19:24 +000013589 offset = strlen(tp->fw_ver);
13590 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
13591 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013592
13593 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000013594 offset = strlen(tp->fw_ver);
13595 if (offset < TG3_VER_SIZE - 1)
13596 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013597 }
13598}
13599
Matt Carlsonacd9c112009-02-25 14:26:33 +000013600static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080013601{
13602 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013603 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070013604
13605 for (offset = TG3_NVM_DIR_START;
13606 offset < TG3_NVM_DIR_END;
13607 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000013608 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013609 return;
13610
13611 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
13612 break;
13613 }
13614
13615 if (offset == TG3_NVM_DIR_END)
13616 return;
13617
Joe Perches63c3a662011-04-26 08:12:10 +000013618 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013619 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000013620 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013621 return;
13622
Matt Carlsone4f34112009-02-25 14:25:00 +000013623 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013624 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013625 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013626 return;
13627
13628 offset += val - start;
13629
Matt Carlsonacd9c112009-02-25 14:26:33 +000013630 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013631
Matt Carlsonacd9c112009-02-25 14:26:33 +000013632 tp->fw_ver[vlen++] = ',';
13633 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070013634
13635 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000013636 __be32 v;
13637 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013638 return;
13639
Al Virob9fc7dc2007-12-17 22:59:57 -080013640 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013641
Matt Carlsonacd9c112009-02-25 14:26:33 +000013642 if (vlen > TG3_VER_SIZE - sizeof(v)) {
13643 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013644 break;
13645 }
13646
Matt Carlsonacd9c112009-02-25 14:26:33 +000013647 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
13648 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013649 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000013650}
13651
Matt Carlson7fd76442009-02-25 14:27:20 +000013652static void __devinit tg3_read_dash_ver(struct tg3 *tp)
13653{
13654 int vlen;
13655 u32 apedata;
Matt Carlsonecc79642010-08-02 11:26:01 +000013656 char *fwtype;
Matt Carlson7fd76442009-02-25 14:27:20 +000013657
Joe Perches63c3a662011-04-26 08:12:10 +000013658 if (!tg3_flag(tp, ENABLE_APE) || !tg3_flag(tp, ENABLE_ASF))
Matt Carlson7fd76442009-02-25 14:27:20 +000013659 return;
13660
13661 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
13662 if (apedata != APE_SEG_SIG_MAGIC)
13663 return;
13664
13665 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
13666 if (!(apedata & APE_FW_STATUS_READY))
13667 return;
13668
13669 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
13670
Matt Carlsondc6d0742010-09-15 08:59:55 +000013671 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI) {
Joe Perches63c3a662011-04-26 08:12:10 +000013672 tg3_flag_set(tp, APE_HAS_NCSI);
Matt Carlsonecc79642010-08-02 11:26:01 +000013673 fwtype = "NCSI";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013674 } else {
Matt Carlsonecc79642010-08-02 11:26:01 +000013675 fwtype = "DASH";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013676 }
Matt Carlsonecc79642010-08-02 11:26:01 +000013677
Matt Carlson7fd76442009-02-25 14:27:20 +000013678 vlen = strlen(tp->fw_ver);
13679
Matt Carlsonecc79642010-08-02 11:26:01 +000013680 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
13681 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000013682 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
13683 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
13684 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
13685 (apedata & APE_FW_VERSION_BLDMSK));
13686}
13687
Matt Carlsonacd9c112009-02-25 14:26:33 +000013688static void __devinit tg3_read_fw_ver(struct tg3 *tp)
13689{
13690 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000013691 bool vpd_vers = false;
13692
13693 if (tp->fw_ver[0] != 0)
13694 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013695
Joe Perches63c3a662011-04-26 08:12:10 +000013696 if (tg3_flag(tp, NO_NVRAM)) {
Matt Carlson75f99362010-04-05 10:19:24 +000013697 strcat(tp->fw_ver, "sb");
Matt Carlsondf259d82009-04-20 06:57:14 +000013698 return;
13699 }
13700
Matt Carlsonacd9c112009-02-25 14:26:33 +000013701 if (tg3_nvram_read(tp, 0, &val))
13702 return;
13703
13704 if (val == TG3_EEPROM_MAGIC)
13705 tg3_read_bc_ver(tp);
13706 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
13707 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013708 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
13709 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013710 else
13711 return;
13712
Matt Carlsonc9cab242011-07-13 09:27:27 +000013713 if (vpd_vers)
Matt Carlson75f99362010-04-05 10:19:24 +000013714 goto done;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013715
Matt Carlsonc9cab242011-07-13 09:27:27 +000013716 if (tg3_flag(tp, ENABLE_APE)) {
13717 if (tg3_flag(tp, ENABLE_ASF))
13718 tg3_read_dash_ver(tp);
13719 } else if (tg3_flag(tp, ENABLE_ASF)) {
13720 tg3_read_mgmtfw_ver(tp);
13721 }
Matt Carlson9c8a6202007-10-21 16:16:08 -070013722
Matt Carlson75f99362010-04-05 10:19:24 +000013723done:
Matt Carlson9c8a6202007-10-21 16:16:08 -070013724 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080013725}
13726
Michael Chan7544b092007-05-05 13:08:32 -070013727static struct pci_dev * __devinit tg3_find_peer(struct tg3 *);
13728
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013729static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
13730{
Joe Perches63c3a662011-04-26 08:12:10 +000013731 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013732 return TG3_RX_RET_MAX_SIZE_5717;
Joe Perches63c3a662011-04-26 08:12:10 +000013733 else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013734 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013735 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000013736 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013737}
13738
Matt Carlson41434702011-03-09 16:58:22 +000013739static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080013740 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
13741 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
13742 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
13743 { },
13744};
13745
Linus Torvalds1da177e2005-04-16 15:20:36 -070013746static int __devinit tg3_get_invariants(struct tg3 *tp)
13747{
Linus Torvalds1da177e2005-04-16 15:20:36 -070013748 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013749 u32 pci_state_reg, grc_misc_cfg;
13750 u32 val;
13751 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013752 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013753
Linus Torvalds1da177e2005-04-16 15:20:36 -070013754 /* Force memory write invalidate off. If we leave it on,
13755 * then on 5700_BX chips we have to enable a workaround.
13756 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
13757 * to match the cacheline size. The Broadcom driver have this
13758 * workaround but turns MWI off all the times so never uses
13759 * it. This seems to suggest that the workaround is insufficient.
13760 */
13761 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13762 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
13763 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13764
Matt Carlson16821282011-07-13 09:27:28 +000013765 /* Important! -- Make sure register accesses are byteswapped
13766 * correctly. Also, for those chips that require it, make
13767 * sure that indirect register accesses are enabled before
13768 * the first operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013769 */
13770 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13771 &misc_ctrl_reg);
Matt Carlson16821282011-07-13 09:27:28 +000013772 tp->misc_host_ctrl |= (misc_ctrl_reg &
13773 MISC_HOST_CTRL_CHIPREV);
13774 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13775 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013776
13777 tp->pci_chip_rev_id = (misc_ctrl_reg >>
13778 MISC_HOST_CTRL_CHIPREV_SHIFT);
Matt Carlson795d01c2007-10-07 23:28:17 -070013779 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
13780 u32 prod_id_asic_rev;
13781
Matt Carlson5001e2f2009-11-13 13:03:51 +000013782 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
13783 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013784 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
13785 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013786 pci_read_config_dword(tp->pdev,
13787 TG3PCI_GEN2_PRODID_ASICREV,
13788 &prod_id_asic_rev);
Matt Carlsonb703df62009-12-03 08:36:21 +000013789 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
13790 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
13791 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
13792 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
13793 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
13794 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13795 pci_read_config_dword(tp->pdev,
13796 TG3PCI_GEN15_PRODID_ASICREV,
13797 &prod_id_asic_rev);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013798 else
13799 pci_read_config_dword(tp->pdev, TG3PCI_PRODID_ASICREV,
13800 &prod_id_asic_rev);
13801
Matt Carlson321d32a2008-11-21 17:22:19 -080013802 tp->pci_chip_rev_id = prod_id_asic_rev;
Matt Carlson795d01c2007-10-07 23:28:17 -070013803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013804
Michael Chanff645be2005-04-21 17:09:53 -070013805 /* Wrong chip ID in 5752 A0. This code can be removed later
13806 * as A0 is not in production.
13807 */
13808 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
13809 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
13810
Michael Chan68929142005-08-09 20:17:14 -070013811 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
13812 * we need to disable memory and use config. cycles
13813 * only to access all registers. The 5702/03 chips
13814 * can mistakenly decode the special cycles from the
13815 * ICH chipsets as memory write cycles, causing corruption
13816 * of register and memory space. Only certain ICH bridges
13817 * will drive special cycles with non-zero data during the
13818 * address phase which can fall within the 5703's address
13819 * range. This is not an ICH bug as the PCI spec allows
13820 * non-zero address during special cycles. However, only
13821 * these ICH bridges are known to drive non-zero addresses
13822 * during special cycles.
13823 *
13824 * Since special cycles do not cross PCI bridges, we only
13825 * enable this workaround if the 5703 is on the secondary
13826 * bus of these ICH bridges.
13827 */
13828 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
13829 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
13830 static struct tg3_dev_id {
13831 u32 vendor;
13832 u32 device;
13833 u32 rev;
13834 } ich_chipsets[] = {
13835 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
13836 PCI_ANY_ID },
13837 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
13838 PCI_ANY_ID },
13839 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
13840 0xa },
13841 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
13842 PCI_ANY_ID },
13843 { },
13844 };
13845 struct tg3_dev_id *pci_id = &ich_chipsets[0];
13846 struct pci_dev *bridge = NULL;
13847
13848 while (pci_id->vendor != 0) {
13849 bridge = pci_get_device(pci_id->vendor, pci_id->device,
13850 bridge);
13851 if (!bridge) {
13852 pci_id++;
13853 continue;
13854 }
13855 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070013856 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070013857 continue;
13858 }
13859 if (bridge->subordinate &&
13860 (bridge->subordinate->number ==
13861 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013862 tg3_flag_set(tp, ICH_WORKAROUND);
Michael Chan68929142005-08-09 20:17:14 -070013863 pci_dev_put(bridge);
13864 break;
13865 }
13866 }
13867 }
13868
Matt Carlson6ff6f812011-05-19 12:12:54 +000013869 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Matt Carlson41588ba12008-04-19 18:12:33 -070013870 static struct tg3_dev_id {
13871 u32 vendor;
13872 u32 device;
13873 } bridge_chipsets[] = {
13874 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
13875 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
13876 { },
13877 };
13878 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
13879 struct pci_dev *bridge = NULL;
13880
13881 while (pci_id->vendor != 0) {
13882 bridge = pci_get_device(pci_id->vendor,
13883 pci_id->device,
13884 bridge);
13885 if (!bridge) {
13886 pci_id++;
13887 continue;
13888 }
13889 if (bridge->subordinate &&
13890 (bridge->subordinate->number <=
13891 tp->pdev->bus->number) &&
13892 (bridge->subordinate->subordinate >=
13893 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013894 tg3_flag_set(tp, 5701_DMA_BUG);
Matt Carlson41588ba12008-04-19 18:12:33 -070013895 pci_dev_put(bridge);
13896 break;
13897 }
13898 }
13899 }
13900
Michael Chan4a29cc22006-03-19 13:21:12 -080013901 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
13902 * DMA addresses > 40-bit. This bridge may have other additional
13903 * 57xx devices behind it in some 4-port NIC designs for example.
13904 * Any tg3 device found behind the bridge will also need the 40-bit
13905 * DMA workaround.
13906 */
Michael Chana4e2b342005-10-26 15:46:52 -070013907 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
13908 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Joe Perches63c3a662011-04-26 08:12:10 +000013909 tg3_flag_set(tp, 5780_CLASS);
13910 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4cf78e42005-07-25 12:29:19 -070013911 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a588792010-04-05 10:19:28 +000013912 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080013913 struct pci_dev *bridge = NULL;
13914
13915 do {
13916 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
13917 PCI_DEVICE_ID_SERVERWORKS_EPB,
13918 bridge);
13919 if (bridge && bridge->subordinate &&
13920 (bridge->subordinate->number <=
13921 tp->pdev->bus->number) &&
13922 (bridge->subordinate->subordinate >=
13923 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013924 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4a29cc22006-03-19 13:21:12 -080013925 pci_dev_put(bridge);
13926 break;
13927 }
13928 } while (bridge);
13929 }
Michael Chan4cf78e42005-07-25 12:29:19 -070013930
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013931 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Matt Carlson3a1e19d2011-07-13 09:27:32 +000013932 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)
Michael Chan7544b092007-05-05 13:08:32 -070013933 tp->pdev_peer = tg3_find_peer(tp);
13934
Matt Carlsonc885e822010-08-02 11:25:57 +000013935 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013936 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13937 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Joe Perches63c3a662011-04-26 08:12:10 +000013938 tg3_flag_set(tp, 5717_PLUS);
Matt Carlson0a58d662011-04-05 14:22:45 +000013939
13940 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013941 tg3_flag(tp, 5717_PLUS))
13942 tg3_flag_set(tp, 57765_PLUS);
Matt Carlsonc885e822010-08-02 11:25:57 +000013943
Matt Carlson321d32a2008-11-21 17:22:19 -080013944 /* Intentionally exclude ASIC_REV_5906 */
13945 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Michael Chand9ab5ad12006-03-20 22:27:35 -080013946 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013947 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013948 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013949 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013950 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013951 tg3_flag(tp, 57765_PLUS))
13952 tg3_flag_set(tp, 5755_PLUS);
Matt Carlson321d32a2008-11-21 17:22:19 -080013953
13954 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13955 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Michael Chanb5d37722006-09-27 16:06:21 -070013956 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013957 tg3_flag(tp, 5755_PLUS) ||
13958 tg3_flag(tp, 5780_CLASS))
13959 tg3_flag_set(tp, 5750_PLUS);
John W. Linville6708e5c2005-04-21 17:00:52 -070013960
Matt Carlson6ff6f812011-05-19 12:12:54 +000013961 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013962 tg3_flag(tp, 5750_PLUS))
13963 tg3_flag_set(tp, 5705_PLUS);
John W. Linville1b440c562005-04-21 17:03:18 -070013964
Matt Carlson507399f2009-11-13 13:03:37 +000013965 /* Determine TSO capabilities */
Matt Carlsona0512942011-07-27 14:20:54 +000013966 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0)
Matt Carlson4d163b72011-01-25 15:58:48 +000013967 ; /* Do nothing. HW bug. */
Joe Perches63c3a662011-04-26 08:12:10 +000013968 else if (tg3_flag(tp, 57765_PLUS))
13969 tg3_flag_set(tp, HW_TSO_3);
13970 else if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000013971 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000013972 tg3_flag_set(tp, HW_TSO_2);
13973 else if (tg3_flag(tp, 5750_PLUS)) {
13974 tg3_flag_set(tp, HW_TSO_1);
13975 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013976 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
13977 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
Joe Perches63c3a662011-04-26 08:12:10 +000013978 tg3_flag_clear(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013979 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13980 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13981 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000013982 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013983 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
13984 tp->fw_needed = FIRMWARE_TG3TSO5;
13985 else
13986 tp->fw_needed = FIRMWARE_TG3TSO;
13987 }
13988
Matt Carlsondabc5c62011-05-19 12:12:52 +000013989 /* Selectively allow TSO based on operating conditions */
Matt Carlson6ff6f812011-05-19 12:12:54 +000013990 if (tg3_flag(tp, HW_TSO_1) ||
13991 tg3_flag(tp, HW_TSO_2) ||
13992 tg3_flag(tp, HW_TSO_3) ||
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000013993 tp->fw_needed) {
13994 /* For firmware TSO, assume ASF is disabled.
13995 * We'll disable TSO later if we discover ASF
13996 * is enabled in tg3_get_eeprom_hw_cfg().
13997 */
Matt Carlsondabc5c62011-05-19 12:12:52 +000013998 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000013999 } else {
Matt Carlsondabc5c62011-05-19 12:12:52 +000014000 tg3_flag_clear(tp, TSO_CAPABLE);
14001 tg3_flag_clear(tp, TSO_BUG);
14002 tp->fw_needed = NULL;
14003 }
14004
14005 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
14006 tp->fw_needed = FIRMWARE_TG3;
14007
Matt Carlson507399f2009-11-13 13:03:37 +000014008 tp->irq_max = 1;
14009
Joe Perches63c3a662011-04-26 08:12:10 +000014010 if (tg3_flag(tp, 5750_PLUS)) {
14011 tg3_flag_set(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070014012 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
14013 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
14014 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
14015 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
14016 tp->pdev_peer == tp->pdev))
Joe Perches63c3a662011-04-26 08:12:10 +000014017 tg3_flag_clear(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070014018
Joe Perches63c3a662011-04-26 08:12:10 +000014019 if (tg3_flag(tp, 5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070014020 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000014021 tg3_flag_set(tp, 1SHOT_MSI);
Michael Chan52c0fd82006-06-29 20:15:54 -070014022 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014023
Joe Perches63c3a662011-04-26 08:12:10 +000014024 if (tg3_flag(tp, 57765_PLUS)) {
14025 tg3_flag_set(tp, SUPPORT_MSIX);
Matt Carlson507399f2009-11-13 13:03:37 +000014026 tp->irq_max = TG3_IRQ_MAX_VECS;
14027 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014028 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000014029
Matt Carlson2ffcc982011-05-19 12:12:44 +000014030 if (tg3_flag(tp, 5755_PLUS))
Joe Perches63c3a662011-04-26 08:12:10 +000014031 tg3_flag_set(tp, SHORT_DMA_BUG);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014032
Matt Carlsone31aa982011-07-27 14:20:53 +000014033 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
14034 tg3_flag_set(tp, 4K_FIFO_LIMIT);
14035
Matt Carlsonfa6b2aa2011-11-21 15:01:19 +000014036 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14037 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
14038 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Joe Perches63c3a662011-04-26 08:12:10 +000014039 tg3_flag_set(tp, LRG_PROD_RING_CAP);
Matt Carlsonde9f5232011-04-05 14:22:43 +000014040
Joe Perches63c3a662011-04-26 08:12:10 +000014041 if (tg3_flag(tp, 57765_PLUS) &&
Matt Carlsona0512942011-07-27 14:20:54 +000014042 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0)
Joe Perches63c3a662011-04-26 08:12:10 +000014043 tg3_flag_set(tp, USE_JUMBO_BDFLAG);
Matt Carlsonb703df62009-12-03 08:36:21 +000014044
Joe Perches63c3a662011-04-26 08:12:10 +000014045 if (!tg3_flag(tp, 5705_PLUS) ||
14046 tg3_flag(tp, 5780_CLASS) ||
14047 tg3_flag(tp, USE_JUMBO_BDFLAG))
14048 tg3_flag_set(tp, JUMBO_CAPABLE);
Michael Chan0f893dc2005-07-25 12:30:38 -070014049
Matt Carlson52f44902008-11-21 17:17:04 -080014050 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14051 &pci_state_reg);
14052
Jon Mason708ebb3a2011-06-27 12:56:50 +000014053 if (pci_is_pcie(tp->pdev)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014054 u16 lnkctl;
14055
Joe Perches63c3a662011-04-26 08:12:10 +000014056 tg3_flag_set(tp, PCI_EXPRESS);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080014057
Matt Carlson2c55a3d2011-11-28 09:41:04 +000014058 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0) {
14059 int readrq = pcie_get_readrq(tp->pdev);
14060 if (readrq > 2048)
14061 pcie_set_readrq(tp->pdev, 2048);
14062 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -080014063
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014064 pci_read_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +000014065 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014066 &lnkctl);
14067 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
Matt Carlson7196cd62011-05-19 16:02:44 +000014068 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
14069 ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000014070 tg3_flag_clear(tp, HW_TSO_2);
Matt Carlsondabc5c62011-05-19 12:12:52 +000014071 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson7196cd62011-05-19 16:02:44 +000014072 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014073 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014074 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000014075 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
14076 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Joe Perches63c3a662011-04-26 08:12:10 +000014077 tg3_flag_set(tp, CLKREQ_BUG);
Matt Carlson614b0592010-01-20 16:58:02 +000014078 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000014079 tg3_flag_set(tp, L1PLLPD_EN);
Michael Chanc7835a72006-11-15 21:14:42 -080014080 }
Matt Carlson52f44902008-11-21 17:17:04 -080014081 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Jon Mason708ebb3a2011-06-27 12:56:50 +000014082 /* BCM5785 devices are effectively PCIe devices, and should
14083 * follow PCIe codepaths, but do not have a PCIe capabilities
14084 * section.
Matt Carlson93a700a2011-08-31 11:44:54 +000014085 */
Joe Perches63c3a662011-04-26 08:12:10 +000014086 tg3_flag_set(tp, PCI_EXPRESS);
14087 } else if (!tg3_flag(tp, 5705_PLUS) ||
14088 tg3_flag(tp, 5780_CLASS)) {
Matt Carlson52f44902008-11-21 17:17:04 -080014089 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
14090 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000014091 dev_err(&tp->pdev->dev,
14092 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080014093 return -EIO;
14094 }
14095
14096 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
Joe Perches63c3a662011-04-26 08:12:10 +000014097 tg3_flag_set(tp, PCIX_MODE);
Matt Carlson52f44902008-11-21 17:17:04 -080014098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014099
Michael Chan399de502005-10-03 14:02:39 -070014100 /* If we have an AMD 762 or VIA K8T800 chipset, write
14101 * reordering to the mailbox registers done by the host
14102 * controller can cause major troubles. We read back from
14103 * every mailbox register write to force the writes to be
14104 * posted to the chip in order.
14105 */
Matt Carlson41434702011-03-09 16:58:22 +000014106 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Joe Perches63c3a662011-04-26 08:12:10 +000014107 !tg3_flag(tp, PCI_EXPRESS))
14108 tg3_flag_set(tp, MBOX_WRITE_REORDER);
Michael Chan399de502005-10-03 14:02:39 -070014109
Matt Carlson69fc4052008-12-21 20:19:57 -080014110 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
14111 &tp->pci_cacheline_sz);
14112 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
14113 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014114 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14115 tp->pci_lat_timer < 64) {
14116 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080014117 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
14118 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014119 }
14120
Matt Carlson16821282011-07-13 09:27:28 +000014121 /* Important! -- It is critical that the PCI-X hw workaround
14122 * situation is decided before the first MMIO register access.
14123 */
Matt Carlson52f44902008-11-21 17:17:04 -080014124 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
14125 /* 5700 BX chips need to have their TX producer index
14126 * mailboxes written twice to workaround a bug.
14127 */
Joe Perches63c3a662011-04-26 08:12:10 +000014128 tg3_flag_set(tp, TXD_MBOX_HWBUG);
Matt Carlson9974a352007-10-07 23:27:28 -070014129
Matt Carlson52f44902008-11-21 17:17:04 -080014130 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014131 *
14132 * The workaround is to use indirect register accesses
14133 * for all chip writes not to mailbox registers.
14134 */
Joe Perches63c3a662011-04-26 08:12:10 +000014135 if (tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014136 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014137
Joe Perches63c3a662011-04-26 08:12:10 +000014138 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014139
14140 /* The chip can have it's power management PCI config
14141 * space registers clobbered due to this bug.
14142 * So explicitly force the chip into D0 here.
14143 */
Matt Carlson9974a352007-10-07 23:27:28 -070014144 pci_read_config_dword(tp->pdev,
14145 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014146 &pm_reg);
14147 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
14148 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070014149 pci_write_config_dword(tp->pdev,
14150 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014151 pm_reg);
14152
14153 /* Also, force SERR#/PERR# in PCI command. */
14154 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
14155 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
14156 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
14157 }
14158 }
14159
Linus Torvalds1da177e2005-04-16 15:20:36 -070014160 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000014161 tg3_flag_set(tp, PCI_HIGH_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014162 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000014163 tg3_flag_set(tp, PCI_32BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014164
14165 /* Chip-specific fixup from Broadcom driver */
14166 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
14167 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
14168 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
14169 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
14170 }
14171
Michael Chan1ee582d2005-08-09 20:16:46 -070014172 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070014173 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070014174 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070014175 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070014176 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070014177 tp->write32_tx_mbox = tg3_write32;
14178 tp->write32_rx_mbox = tg3_write32;
14179
14180 /* Various workaround register access methods */
Joe Perches63c3a662011-04-26 08:12:10 +000014181 if (tg3_flag(tp, PCIX_TARGET_HWBUG))
Michael Chan1ee582d2005-08-09 20:16:46 -070014182 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070014183 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014184 (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson98efd8a2007-05-05 12:47:25 -070014185 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
14186 /*
14187 * Back to back register writes can cause problems on these
14188 * chips, the workaround is to read back all reg writes
14189 * except those to mailbox regs.
14190 *
14191 * See tg3_write_indirect_reg32().
14192 */
Michael Chan1ee582d2005-08-09 20:16:46 -070014193 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070014194 }
14195
Joe Perches63c3a662011-04-26 08:12:10 +000014196 if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
Michael Chan1ee582d2005-08-09 20:16:46 -070014197 tp->write32_tx_mbox = tg3_write32_tx_mbox;
Joe Perches63c3a662011-04-26 08:12:10 +000014198 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Michael Chan1ee582d2005-08-09 20:16:46 -070014199 tp->write32_rx_mbox = tg3_write_flush_reg32;
14200 }
Michael Chan20094932005-08-09 20:16:32 -070014201
Joe Perches63c3a662011-04-26 08:12:10 +000014202 if (tg3_flag(tp, ICH_WORKAROUND)) {
Michael Chan68929142005-08-09 20:17:14 -070014203 tp->read32 = tg3_read_indirect_reg32;
14204 tp->write32 = tg3_write_indirect_reg32;
14205 tp->read32_mbox = tg3_read_indirect_mbox;
14206 tp->write32_mbox = tg3_write_indirect_mbox;
14207 tp->write32_tx_mbox = tg3_write_indirect_mbox;
14208 tp->write32_rx_mbox = tg3_write_indirect_mbox;
14209
14210 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070014211 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070014212
14213 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
14214 pci_cmd &= ~PCI_COMMAND_MEMORY;
14215 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
14216 }
Michael Chanb5d37722006-09-27 16:06:21 -070014217 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
14218 tp->read32_mbox = tg3_read32_mbox_5906;
14219 tp->write32_mbox = tg3_write32_mbox_5906;
14220 tp->write32_tx_mbox = tg3_write32_mbox_5906;
14221 tp->write32_rx_mbox = tg3_write32_mbox_5906;
14222 }
Michael Chan68929142005-08-09 20:17:14 -070014223
Michael Chanbbadf502006-04-06 21:46:34 -070014224 if (tp->write32 == tg3_write_indirect_reg32 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014225 (tg3_flag(tp, PCIX_MODE) &&
Michael Chanbbadf502006-04-06 21:46:34 -070014226 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070014227 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Joe Perches63c3a662011-04-26 08:12:10 +000014228 tg3_flag_set(tp, SRAM_USE_CONFIG);
Michael Chanbbadf502006-04-06 21:46:34 -070014229
Matt Carlson16821282011-07-13 09:27:28 +000014230 /* The memory arbiter has to be enabled in order for SRAM accesses
14231 * to succeed. Normally on powerup the tg3 chip firmware will make
14232 * sure it is enabled, but other entities such as system netboot
14233 * code might disable it.
14234 */
14235 val = tr32(MEMARB_MODE);
14236 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
14237
Matt Carlson9dc5e342011-11-04 09:15:02 +000014238 tp->pci_fn = PCI_FUNC(tp->pdev->devfn) & 3;
14239 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
14240 tg3_flag(tp, 5780_CLASS)) {
14241 if (tg3_flag(tp, PCIX_MODE)) {
14242 pci_read_config_dword(tp->pdev,
14243 tp->pcix_cap + PCI_X_STATUS,
14244 &val);
14245 tp->pci_fn = val & 0x7;
14246 }
14247 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
14248 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
14249 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) ==
14250 NIC_SRAM_CPMUSTAT_SIG) {
14251 tp->pci_fn = val & TG3_CPMU_STATUS_FMSK_5717;
14252 tp->pci_fn = tp->pci_fn ? 1 : 0;
14253 }
14254 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
14255 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
14256 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
14257 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) ==
14258 NIC_SRAM_CPMUSTAT_SIG) {
14259 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5719) >>
14260 TG3_CPMU_STATUS_FSHFT_5719;
14261 }
Matt Carlson69f11c92011-07-13 09:27:30 +000014262 }
14263
Michael Chan7d0c41e2005-04-21 17:06:20 -070014264 /* Get eeprom hw config before calling tg3_set_power_state().
Joe Perches63c3a662011-04-26 08:12:10 +000014265 * In particular, the TG3_FLAG_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070014266 * determined before calling tg3_set_power_state() so that
14267 * we know whether or not to switch out of Vaux power.
14268 * When the flag is set, it means that GPIO1 is used for eeprom
14269 * write protect and also implies that it is a LOM where GPIOs
14270 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040014271 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070014272 tg3_get_eeprom_hw_cfg(tp);
14273
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000014274 if (tp->fw_needed && tg3_flag(tp, ENABLE_ASF)) {
14275 tg3_flag_clear(tp, TSO_CAPABLE);
14276 tg3_flag_clear(tp, TSO_BUG);
14277 tp->fw_needed = NULL;
14278 }
14279
Joe Perches63c3a662011-04-26 08:12:10 +000014280 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070014281 /* Allow reads and writes to the
14282 * APE register and memory space.
14283 */
14284 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +000014285 PCISTATE_ALLOW_APE_SHMEM_WR |
14286 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070014287 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
14288 pci_state_reg);
Matt Carlsonc9cab242011-07-13 09:27:27 +000014289
14290 tg3_ape_lock_init(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070014291 }
14292
Matt Carlson9936bcf2007-10-10 18:03:07 -070014293 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson57e69832008-05-25 23:48:31 -070014294 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014295 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014296 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014297 tg3_flag(tp, 57765_PLUS))
14298 tg3_flag_set(tp, CPMU_PRESENT);
Matt Carlsond30cdd22007-10-07 23:28:35 -070014299
Matt Carlson16821282011-07-13 09:27:28 +000014300 /* Set up tp->grc_local_ctrl before calling
14301 * tg3_pwrsrc_switch_to_vmain(). GPIO1 driven high
14302 * will bring 5700's external PHY out of reset.
Michael Chan314fba32005-04-21 17:07:04 -070014303 * It is also used as eeprom write protect on LOMs.
14304 */
14305 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
Matt Carlson6ff6f812011-05-19 12:12:54 +000014306 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014307 tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan314fba32005-04-21 17:07:04 -070014308 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
14309 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070014310 /* Unused GPIO3 must be driven as output on 5752 because there
14311 * are no pull-up resistors on unused GPIO pins.
14312 */
14313 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
14314 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070014315
Matt Carlson321d32a2008-11-21 17:22:19 -080014316 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsoncb4ed1f2010-01-20 16:58:09 +000014317 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
14318 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Michael Chanaf36e6b2006-03-23 01:28:06 -080014319 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
14320
Matt Carlson8d519ab2009-04-20 06:58:01 +000014321 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
14322 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070014323 /* Turn off the debug UART. */
14324 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014325 if (tg3_flag(tp, IS_NIC))
Matt Carlson5f0c4a32008-06-09 15:41:12 -070014326 /* Keep VMain power. */
14327 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
14328 GRC_LCLCTRL_GPIO_OUTPUT0;
14329 }
14330
Matt Carlson16821282011-07-13 09:27:28 +000014331 /* Switch out of Vaux if it is a NIC */
14332 tg3_pwrsrc_switch_to_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014333
Linus Torvalds1da177e2005-04-16 15:20:36 -070014334 /* Derive initial jumbo mode from MTU assigned in
14335 * ether_setup() via the alloc_etherdev() call
14336 */
Joe Perches63c3a662011-04-26 08:12:10 +000014337 if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
14338 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014339
14340 /* Determine WakeOnLan speed to use. */
14341 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14342 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
14343 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
14344 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
Joe Perches63c3a662011-04-26 08:12:10 +000014345 tg3_flag_clear(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014346 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000014347 tg3_flag_set(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014348 }
14349
Matt Carlson7f97a4b2009-08-25 10:10:03 +000014350 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014351 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000014352
Linus Torvalds1da177e2005-04-16 15:20:36 -070014353 /* A few boards don't want Ethernet@WireSpeed phy feature */
Matt Carlson6ff6f812011-05-19 12:12:54 +000014354 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14355 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070014356 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070014357 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014358 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
14359 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
14360 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014361
14362 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
14363 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014364 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014365 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014366 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014367
Joe Perches63c3a662011-04-26 08:12:10 +000014368 if (tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014369 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080014370 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014371 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014372 !tg3_flag(tp, 57765_PLUS)) {
Michael Chanc424cb22006-04-29 18:56:34 -070014373 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070014374 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070014375 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
14376 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080014377 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
14378 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014379 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080014380 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014381 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080014382 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014383 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070014384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014385
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014386 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
14387 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
14388 tp->phy_otp = tg3_read_otp_phycfg(tp);
14389 if (tp->phy_otp == 0)
14390 tp->phy_otp = TG3_OTP_DEFAULT;
14391 }
14392
Joe Perches63c3a662011-04-26 08:12:10 +000014393 if (tg3_flag(tp, CPMU_PRESENT))
Matt Carlson8ef21422008-05-02 16:47:53 -070014394 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
14395 else
14396 tp->mi_mode = MAC_MI_MODE_BASE;
14397
Linus Torvalds1da177e2005-04-16 15:20:36 -070014398 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014399 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
14400 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
14401 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
14402
Matt Carlson4d958472011-04-20 07:57:35 +000014403 /* Set these bits to enable statistics workaround. */
14404 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14405 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
14406 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0) {
14407 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
14408 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
14409 }
14410
Matt Carlson321d32a2008-11-21 17:22:19 -080014411 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
14412 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Joe Perches63c3a662011-04-26 08:12:10 +000014413 tg3_flag_set(tp, USE_PHYLIB);
Matt Carlson57e69832008-05-25 23:48:31 -070014414
Matt Carlson158d7ab2008-05-29 01:37:54 -070014415 err = tg3_mdio_init(tp);
14416 if (err)
14417 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014418
14419 /* Initialize data/descriptor byte/word swapping. */
14420 val = tr32(GRC_MODE);
Matt Carlsonf2096f92011-04-05 14:22:48 +000014421 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
14422 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
14423 GRC_MODE_WORD_SWAP_B2HRX_DATA |
14424 GRC_MODE_B2HRX_ENABLE |
14425 GRC_MODE_HTX2B_ENABLE |
14426 GRC_MODE_HOST_STACKUP);
14427 else
14428 val &= GRC_MODE_HOST_STACKUP;
14429
Linus Torvalds1da177e2005-04-16 15:20:36 -070014430 tw32(GRC_MODE, val | tp->grc_mode);
14431
14432 tg3_switch_clocks(tp);
14433
14434 /* Clear this out for sanity. */
14435 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
14436
14437 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14438 &pci_state_reg);
14439 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014440 !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014441 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
14442
14443 if (chiprevid == CHIPREV_ID_5701_A0 ||
14444 chiprevid == CHIPREV_ID_5701_B0 ||
14445 chiprevid == CHIPREV_ID_5701_B2 ||
14446 chiprevid == CHIPREV_ID_5701_B5) {
14447 void __iomem *sram_base;
14448
14449 /* Write some dummy words into the SRAM status block
14450 * area, see if it reads back correctly. If the return
14451 * value is bad, force enable the PCIX workaround.
14452 */
14453 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
14454
14455 writel(0x00000000, sram_base);
14456 writel(0x00000000, sram_base + 4);
14457 writel(0xffffffff, sram_base + 4);
14458 if (readl(sram_base) != 0x00000000)
Joe Perches63c3a662011-04-26 08:12:10 +000014459 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014460 }
14461 }
14462
14463 udelay(50);
14464 tg3_nvram_init(tp);
14465
14466 grc_misc_cfg = tr32(GRC_MISC_CFG);
14467 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
14468
Linus Torvalds1da177e2005-04-16 15:20:36 -070014469 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14470 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
14471 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
Joe Perches63c3a662011-04-26 08:12:10 +000014472 tg3_flag_set(tp, IS_5788);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014473
Joe Perches63c3a662011-04-26 08:12:10 +000014474 if (!tg3_flag(tp, IS_5788) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +000014475 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014476 tg3_flag_set(tp, TAGGED_STATUS);
14477 if (tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070014478 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
14479 HOSTCC_MODE_CLRTICK_TXBD);
14480
14481 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
14482 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
14483 tp->misc_host_ctrl);
14484 }
14485
Matt Carlson3bda1252008-08-15 14:08:22 -070014486 /* Preserve the APE MAC_MODE bits */
Joe Perches63c3a662011-04-26 08:12:10 +000014487 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +000014488 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070014489 else
Matt Carlson6e01b202011-08-19 13:58:20 +000014490 tp->mac_mode = 0;
Matt Carlson3bda1252008-08-15 14:08:22 -070014491
Linus Torvalds1da177e2005-04-16 15:20:36 -070014492 /* these are limited to 10/100 only */
14493 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14494 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
14495 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14496 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14497 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
14498 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
14499 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
14500 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14501 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
Michael Chan676917d2006-12-07 00:20:22 -080014502 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
14503 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014504 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
Matt Carlsond1101142010-02-17 15:16:55 +000014505 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
14506 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014507 (tp->phy_flags & TG3_PHYFLG_IS_FET))
14508 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014509
14510 err = tg3_phy_probe(tp);
14511 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014512 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014513 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014514 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014515 }
14516
Matt Carlson184b8902010-04-05 10:19:25 +000014517 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080014518 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014519
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014520 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
14521 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014522 } else {
14523 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014524 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014525 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014526 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014527 }
14528
14529 /* 5700 {AX,BX} chips have a broken status block link
14530 * change bit implementation, so we must use the
14531 * status register in those cases.
14532 */
14533 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014534 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014535 else
Joe Perches63c3a662011-04-26 08:12:10 +000014536 tg3_flag_clear(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014537
14538 /* The led_ctrl is set during tg3_phy_probe, here we might
14539 * have to force the link status polling mechanism based
14540 * upon subsystem IDs.
14541 */
14542 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070014543 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014544 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
14545 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Joe Perches63c3a662011-04-26 08:12:10 +000014546 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014547 }
14548
14549 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014550 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Joe Perches63c3a662011-04-26 08:12:10 +000014551 tg3_flag_set(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014552 else
Joe Perches63c3a662011-04-26 08:12:10 +000014553 tg3_flag_clear(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014554
Eric Dumazet9205fd92011-11-18 06:47:01 +000014555 tp->rx_offset = NET_SKB_PAD + NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014556 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014557 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014558 tg3_flag(tp, PCIX_MODE)) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000014559 tp->rx_offset = NET_SKB_PAD;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014560#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000014561 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014562#endif
14563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014564
Matt Carlson2c49a442010-09-30 10:34:35 +000014565 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
14566 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000014567 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
14568
Matt Carlson2c49a442010-09-30 10:34:35 +000014569 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070014570
14571 /* Increment the rx prod index on the rx std ring by at most
14572 * 8 for these chips to workaround hw errata.
14573 */
14574 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
14575 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
14576 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
14577 tp->rx_std_max_post = 8;
14578
Joe Perches63c3a662011-04-26 08:12:10 +000014579 if (tg3_flag(tp, ASPM_WORKAROUND))
Matt Carlson8ed5d972007-05-07 00:25:49 -070014580 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
14581 PCIE_PWR_MGMT_L1_THRESH_MSK;
14582
Linus Torvalds1da177e2005-04-16 15:20:36 -070014583 return err;
14584}
14585
David S. Miller49b6e95f2007-03-29 01:38:42 -070014586#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014587static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
14588{
14589 struct net_device *dev = tp->dev;
14590 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014591 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070014592 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014593 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014594
David S. Miller49b6e95f2007-03-29 01:38:42 -070014595 addr = of_get_property(dp, "local-mac-address", &len);
14596 if (addr && len == 6) {
14597 memcpy(dev->dev_addr, addr, 6);
14598 memcpy(dev->perm_addr, dev->dev_addr, 6);
14599 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014600 }
14601 return -ENODEV;
14602}
14603
14604static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
14605{
14606 struct net_device *dev = tp->dev;
14607
14608 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070014609 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014610 return 0;
14611}
14612#endif
14613
14614static int __devinit tg3_get_device_address(struct tg3 *tp)
14615{
14616 struct net_device *dev = tp->dev;
14617 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080014618 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014619
David S. Miller49b6e95f2007-03-29 01:38:42 -070014620#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014621 if (!tg3_get_macaddr_sparc(tp))
14622 return 0;
14623#endif
14624
14625 mac_offset = 0x7c;
Matt Carlson6ff6f812011-05-19 12:12:54 +000014626 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014627 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014628 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
14629 mac_offset = 0xcc;
14630 if (tg3_nvram_lock(tp))
14631 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
14632 else
14633 tg3_nvram_unlock(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000014634 } else if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson69f11c92011-07-13 09:27:30 +000014635 if (tp->pci_fn & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014636 mac_offset = 0xcc;
Matt Carlson69f11c92011-07-13 09:27:30 +000014637 if (tp->pci_fn > 1)
Matt Carlsona50d0792010-06-05 17:24:37 +000014638 mac_offset += 0x18c;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014639 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070014640 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014641
14642 /* First try to get it from MAC address mailbox. */
14643 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
14644 if ((hi >> 16) == 0x484b) {
14645 dev->dev_addr[0] = (hi >> 8) & 0xff;
14646 dev->dev_addr[1] = (hi >> 0) & 0xff;
14647
14648 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
14649 dev->dev_addr[2] = (lo >> 24) & 0xff;
14650 dev->dev_addr[3] = (lo >> 16) & 0xff;
14651 dev->dev_addr[4] = (lo >> 8) & 0xff;
14652 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014653
Michael Chan008652b2006-03-27 23:14:53 -080014654 /* Some old bootcode may report a 0 MAC address in SRAM */
14655 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
14656 }
14657 if (!addr_ok) {
14658 /* Next, try NVRAM. */
Joe Perches63c3a662011-04-26 08:12:10 +000014659 if (!tg3_flag(tp, NO_NVRAM) &&
Matt Carlsondf259d82009-04-20 06:57:14 +000014660 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000014661 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070014662 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
14663 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080014664 }
14665 /* Finally just fetch it out of the MAC control regs. */
14666 else {
14667 hi = tr32(MAC_ADDR_0_HIGH);
14668 lo = tr32(MAC_ADDR_0_LOW);
14669
14670 dev->dev_addr[5] = lo & 0xff;
14671 dev->dev_addr[4] = (lo >> 8) & 0xff;
14672 dev->dev_addr[3] = (lo >> 16) & 0xff;
14673 dev->dev_addr[2] = (lo >> 24) & 0xff;
14674 dev->dev_addr[1] = hi & 0xff;
14675 dev->dev_addr[0] = (hi >> 8) & 0xff;
14676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014677 }
14678
14679 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070014680#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014681 if (!tg3_get_default_macaddr_sparc(tp))
14682 return 0;
14683#endif
14684 return -EINVAL;
14685 }
John W. Linville2ff43692005-09-12 14:44:20 -070014686 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014687 return 0;
14688}
14689
David S. Miller59e6b432005-05-18 22:50:10 -070014690#define BOUNDARY_SINGLE_CACHELINE 1
14691#define BOUNDARY_MULTI_CACHELINE 2
14692
14693static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
14694{
14695 int cacheline_size;
14696 u8 byte;
14697 int goal;
14698
14699 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
14700 if (byte == 0)
14701 cacheline_size = 1024;
14702 else
14703 cacheline_size = (int) byte * 4;
14704
14705 /* On 5703 and later chips, the boundary bits have no
14706 * effect.
14707 */
14708 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14709 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014710 !tg3_flag(tp, PCI_EXPRESS))
David S. Miller59e6b432005-05-18 22:50:10 -070014711 goto out;
14712
14713#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
14714 goal = BOUNDARY_MULTI_CACHELINE;
14715#else
14716#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
14717 goal = BOUNDARY_SINGLE_CACHELINE;
14718#else
14719 goal = 0;
14720#endif
14721#endif
14722
Joe Perches63c3a662011-04-26 08:12:10 +000014723 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014724 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
14725 goto out;
14726 }
14727
David S. Miller59e6b432005-05-18 22:50:10 -070014728 if (!goal)
14729 goto out;
14730
14731 /* PCI controllers on most RISC systems tend to disconnect
14732 * when a device tries to burst across a cache-line boundary.
14733 * Therefore, letting tg3 do so just wastes PCI bandwidth.
14734 *
14735 * Unfortunately, for PCI-E there are only limited
14736 * write-side controls for this, and thus for reads
14737 * we will still get the disconnects. We'll also waste
14738 * these PCI cycles for both read and write for chips
14739 * other than 5700 and 5701 which do not implement the
14740 * boundary bits.
14741 */
Joe Perches63c3a662011-04-26 08:12:10 +000014742 if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014743 switch (cacheline_size) {
14744 case 16:
14745 case 32:
14746 case 64:
14747 case 128:
14748 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14749 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
14750 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
14751 } else {
14752 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14753 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14754 }
14755 break;
14756
14757 case 256:
14758 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
14759 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
14760 break;
14761
14762 default:
14763 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14764 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14765 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014766 }
Joe Perches63c3a662011-04-26 08:12:10 +000014767 } else if (tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014768 switch (cacheline_size) {
14769 case 16:
14770 case 32:
14771 case 64:
14772 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14773 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14774 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
14775 break;
14776 }
14777 /* fallthrough */
14778 case 128:
14779 default:
14780 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14781 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
14782 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014783 }
David S. Miller59e6b432005-05-18 22:50:10 -070014784 } else {
14785 switch (cacheline_size) {
14786 case 16:
14787 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14788 val |= (DMA_RWCTRL_READ_BNDRY_16 |
14789 DMA_RWCTRL_WRITE_BNDRY_16);
14790 break;
14791 }
14792 /* fallthrough */
14793 case 32:
14794 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14795 val |= (DMA_RWCTRL_READ_BNDRY_32 |
14796 DMA_RWCTRL_WRITE_BNDRY_32);
14797 break;
14798 }
14799 /* fallthrough */
14800 case 64:
14801 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14802 val |= (DMA_RWCTRL_READ_BNDRY_64 |
14803 DMA_RWCTRL_WRITE_BNDRY_64);
14804 break;
14805 }
14806 /* fallthrough */
14807 case 128:
14808 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14809 val |= (DMA_RWCTRL_READ_BNDRY_128 |
14810 DMA_RWCTRL_WRITE_BNDRY_128);
14811 break;
14812 }
14813 /* fallthrough */
14814 case 256:
14815 val |= (DMA_RWCTRL_READ_BNDRY_256 |
14816 DMA_RWCTRL_WRITE_BNDRY_256);
14817 break;
14818 case 512:
14819 val |= (DMA_RWCTRL_READ_BNDRY_512 |
14820 DMA_RWCTRL_WRITE_BNDRY_512);
14821 break;
14822 case 1024:
14823 default:
14824 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
14825 DMA_RWCTRL_WRITE_BNDRY_1024);
14826 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014827 }
David S. Miller59e6b432005-05-18 22:50:10 -070014828 }
14829
14830out:
14831 return val;
14832}
14833
Linus Torvalds1da177e2005-04-16 15:20:36 -070014834static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
14835{
14836 struct tg3_internal_buffer_desc test_desc;
14837 u32 sram_dma_descs;
14838 int i, ret;
14839
14840 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
14841
14842 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
14843 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
14844 tw32(RDMAC_STATUS, 0);
14845 tw32(WDMAC_STATUS, 0);
14846
14847 tw32(BUFMGR_MODE, 0);
14848 tw32(FTQ_RESET, 0);
14849
14850 test_desc.addr_hi = ((u64) buf_dma) >> 32;
14851 test_desc.addr_lo = buf_dma & 0xffffffff;
14852 test_desc.nic_mbuf = 0x00002100;
14853 test_desc.len = size;
14854
14855 /*
14856 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
14857 * the *second* time the tg3 driver was getting loaded after an
14858 * initial scan.
14859 *
14860 * Broadcom tells me:
14861 * ...the DMA engine is connected to the GRC block and a DMA
14862 * reset may affect the GRC block in some unpredictable way...
14863 * The behavior of resets to individual blocks has not been tested.
14864 *
14865 * Broadcom noted the GRC reset will also reset all sub-components.
14866 */
14867 if (to_device) {
14868 test_desc.cqid_sqid = (13 << 8) | 2;
14869
14870 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
14871 udelay(40);
14872 } else {
14873 test_desc.cqid_sqid = (16 << 8) | 7;
14874
14875 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
14876 udelay(40);
14877 }
14878 test_desc.flags = 0x00000005;
14879
14880 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
14881 u32 val;
14882
14883 val = *(((u32 *)&test_desc) + i);
14884 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
14885 sram_dma_descs + (i * sizeof(u32)));
14886 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
14887 }
14888 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
14889
Matt Carlson859a588792010-04-05 10:19:28 +000014890 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014891 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a588792010-04-05 10:19:28 +000014892 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070014893 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014894
14895 ret = -ENODEV;
14896 for (i = 0; i < 40; i++) {
14897 u32 val;
14898
14899 if (to_device)
14900 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
14901 else
14902 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
14903 if ((val & 0xffff) == sram_dma_descs) {
14904 ret = 0;
14905 break;
14906 }
14907
14908 udelay(100);
14909 }
14910
14911 return ret;
14912}
14913
David S. Millerded73402005-05-23 13:59:47 -070014914#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070014915
Matt Carlson41434702011-03-09 16:58:22 +000014916static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080014917 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
14918 { },
14919};
14920
Linus Torvalds1da177e2005-04-16 15:20:36 -070014921static int __devinit tg3_test_dma(struct tg3 *tp)
14922{
14923 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070014924 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014925 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014926
Matt Carlson4bae65c2010-11-24 08:31:52 +000014927 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
14928 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014929 if (!buf) {
14930 ret = -ENOMEM;
14931 goto out_nofree;
14932 }
14933
14934 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
14935 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
14936
David S. Miller59e6b432005-05-18 22:50:10 -070014937 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014938
Joe Perches63c3a662011-04-26 08:12:10 +000014939 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014940 goto out;
14941
Joe Perches63c3a662011-04-26 08:12:10 +000014942 if (tg3_flag(tp, PCI_EXPRESS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014943 /* DMA read watermark not used on PCIE */
14944 tp->dma_rwctrl |= 0x00180000;
Joe Perches63c3a662011-04-26 08:12:10 +000014945 } else if (!tg3_flag(tp, PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070014946 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
14947 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014948 tp->dma_rwctrl |= 0x003f0000;
14949 else
14950 tp->dma_rwctrl |= 0x003f000f;
14951 } else {
14952 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14953 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
14954 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080014955 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014956
Michael Chan4a29cc22006-03-19 13:21:12 -080014957 /* If the 5704 is behind the EPB bridge, we can
14958 * do the less restrictive ONE_DMA workaround for
14959 * better performance.
14960 */
Joe Perches63c3a662011-04-26 08:12:10 +000014961 if (tg3_flag(tp, 40BIT_DMA_BUG) &&
Michael Chan4a29cc22006-03-19 13:21:12 -080014962 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14963 tp->dma_rwctrl |= 0x8000;
14964 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014965 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
14966
Michael Chan49afdeb2007-02-13 12:17:03 -080014967 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
14968 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070014969 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080014970 tp->dma_rwctrl |=
14971 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
14972 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
14973 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070014974 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
14975 /* 5780 always in PCIX mode */
14976 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070014977 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
14978 /* 5714 always in PCIX mode */
14979 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014980 } else {
14981 tp->dma_rwctrl |= 0x001b000f;
14982 }
14983 }
14984
14985 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14986 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14987 tp->dma_rwctrl &= 0xfffffff0;
14988
14989 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14990 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
14991 /* Remove this if it causes problems for some boards. */
14992 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
14993
14994 /* On 5700/5701 chips, we need to set this bit.
14995 * Otherwise the chip will issue cacheline transactions
14996 * to streamable DMA memory with not all the byte
14997 * enables turned on. This is an error on several
14998 * RISC PCI controllers, in particular sparc64.
14999 *
15000 * On 5703/5704 chips, this bit has been reassigned
15001 * a different meaning. In particular, it is used
15002 * on those chips to enable a PCI-X workaround.
15003 */
15004 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
15005 }
15006
15007 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15008
15009#if 0
15010 /* Unneeded, already done by tg3_get_invariants. */
15011 tg3_switch_clocks(tp);
15012#endif
15013
Linus Torvalds1da177e2005-04-16 15:20:36 -070015014 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
15015 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
15016 goto out;
15017
David S. Miller59e6b432005-05-18 22:50:10 -070015018 /* It is best to perform DMA test with maximum write burst size
15019 * to expose the 5700/5701 write DMA bug.
15020 */
15021 saved_dma_rwctrl = tp->dma_rwctrl;
15022 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
15023 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15024
Linus Torvalds1da177e2005-04-16 15:20:36 -070015025 while (1) {
15026 u32 *p = buf, i;
15027
15028 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
15029 p[i] = i;
15030
15031 /* Send the buffer to the chip. */
15032 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
15033 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000015034 dev_err(&tp->pdev->dev,
15035 "%s: Buffer write failed. err = %d\n",
15036 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015037 break;
15038 }
15039
15040#if 0
15041 /* validate data reached card RAM correctly. */
15042 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
15043 u32 val;
15044 tg3_read_mem(tp, 0x2100 + (i*4), &val);
15045 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000015046 dev_err(&tp->pdev->dev,
15047 "%s: Buffer corrupted on device! "
15048 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015049 /* ret = -ENODEV here? */
15050 }
15051 p[i] = 0;
15052 }
15053#endif
15054 /* Now read it back. */
15055 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
15056 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000015057 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
15058 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015059 break;
15060 }
15061
15062 /* Verify it. */
15063 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
15064 if (p[i] == i)
15065 continue;
15066
David S. Miller59e6b432005-05-18 22:50:10 -070015067 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
15068 DMA_RWCTRL_WRITE_BNDRY_16) {
15069 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015070 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
15071 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15072 break;
15073 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000015074 dev_err(&tp->pdev->dev,
15075 "%s: Buffer corrupted on read back! "
15076 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015077 ret = -ENODEV;
15078 goto out;
15079 }
15080 }
15081
15082 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
15083 /* Success. */
15084 ret = 0;
15085 break;
15086 }
15087 }
David S. Miller59e6b432005-05-18 22:50:10 -070015088 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
15089 DMA_RWCTRL_WRITE_BNDRY_16) {
15090 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070015091 * now look for chipsets that are known to expose the
15092 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070015093 */
Matt Carlson41434702011-03-09 16:58:22 +000015094 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070015095 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
15096 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a588792010-04-05 10:19:28 +000015097 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070015098 /* Safe to use the calculated DMA boundary. */
15099 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a588792010-04-05 10:19:28 +000015100 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070015101
David S. Miller59e6b432005-05-18 22:50:10 -070015102 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015104
15105out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000015106 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015107out_nofree:
15108 return ret;
15109}
15110
Linus Torvalds1da177e2005-04-16 15:20:36 -070015111static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
15112{
Joe Perches63c3a662011-04-26 08:12:10 +000015113 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson666bc832010-01-20 16:58:03 +000015114 tp->bufmgr_config.mbuf_read_dma_low_water =
15115 DEFAULT_MB_RDMA_LOW_WATER_5705;
15116 tp->bufmgr_config.mbuf_mac_rx_low_water =
15117 DEFAULT_MB_MACRX_LOW_WATER_57765;
15118 tp->bufmgr_config.mbuf_high_water =
15119 DEFAULT_MB_HIGH_WATER_57765;
15120
15121 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15122 DEFAULT_MB_RDMA_LOW_WATER_5705;
15123 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15124 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
15125 tp->bufmgr_config.mbuf_high_water_jumbo =
15126 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000015127 } else if (tg3_flag(tp, 5705_PLUS)) {
Michael Chanfdfec1722005-07-25 12:31:48 -070015128 tp->bufmgr_config.mbuf_read_dma_low_water =
15129 DEFAULT_MB_RDMA_LOW_WATER_5705;
15130 tp->bufmgr_config.mbuf_mac_rx_low_water =
15131 DEFAULT_MB_MACRX_LOW_WATER_5705;
15132 tp->bufmgr_config.mbuf_high_water =
15133 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070015134 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
15135 tp->bufmgr_config.mbuf_mac_rx_low_water =
15136 DEFAULT_MB_MACRX_LOW_WATER_5906;
15137 tp->bufmgr_config.mbuf_high_water =
15138 DEFAULT_MB_HIGH_WATER_5906;
15139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015140
Michael Chanfdfec1722005-07-25 12:31:48 -070015141 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15142 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
15143 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15144 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
15145 tp->bufmgr_config.mbuf_high_water_jumbo =
15146 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
15147 } else {
15148 tp->bufmgr_config.mbuf_read_dma_low_water =
15149 DEFAULT_MB_RDMA_LOW_WATER;
15150 tp->bufmgr_config.mbuf_mac_rx_low_water =
15151 DEFAULT_MB_MACRX_LOW_WATER;
15152 tp->bufmgr_config.mbuf_high_water =
15153 DEFAULT_MB_HIGH_WATER;
15154
15155 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15156 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
15157 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15158 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
15159 tp->bufmgr_config.mbuf_high_water_jumbo =
15160 DEFAULT_MB_HIGH_WATER_JUMBO;
15161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015162
15163 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
15164 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
15165}
15166
15167static char * __devinit tg3_phy_string(struct tg3 *tp)
15168{
Matt Carlson79eb6902010-02-17 15:17:03 +000015169 switch (tp->phy_id & TG3_PHY_ID_MASK) {
15170 case TG3_PHY_ID_BCM5400: return "5400";
15171 case TG3_PHY_ID_BCM5401: return "5401";
15172 case TG3_PHY_ID_BCM5411: return "5411";
15173 case TG3_PHY_ID_BCM5701: return "5701";
15174 case TG3_PHY_ID_BCM5703: return "5703";
15175 case TG3_PHY_ID_BCM5704: return "5704";
15176 case TG3_PHY_ID_BCM5705: return "5705";
15177 case TG3_PHY_ID_BCM5750: return "5750";
15178 case TG3_PHY_ID_BCM5752: return "5752";
15179 case TG3_PHY_ID_BCM5714: return "5714";
15180 case TG3_PHY_ID_BCM5780: return "5780";
15181 case TG3_PHY_ID_BCM5755: return "5755";
15182 case TG3_PHY_ID_BCM5787: return "5787";
15183 case TG3_PHY_ID_BCM5784: return "5784";
15184 case TG3_PHY_ID_BCM5756: return "5722/5756";
15185 case TG3_PHY_ID_BCM5906: return "5906";
15186 case TG3_PHY_ID_BCM5761: return "5761";
15187 case TG3_PHY_ID_BCM5718C: return "5718C";
15188 case TG3_PHY_ID_BCM5718S: return "5718S";
15189 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000015190 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000015191 case TG3_PHY_ID_BCM5720C: return "5720C";
Matt Carlson79eb6902010-02-17 15:17:03 +000015192 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070015193 case 0: return "serdes";
15194 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070015195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015196}
15197
Michael Chanf9804dd2005-09-27 12:13:10 -070015198static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
15199{
Joe Perches63c3a662011-04-26 08:12:10 +000015200 if (tg3_flag(tp, PCI_EXPRESS)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070015201 strcpy(str, "PCI Express");
15202 return str;
Joe Perches63c3a662011-04-26 08:12:10 +000015203 } else if (tg3_flag(tp, PCIX_MODE)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070015204 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
15205
15206 strcpy(str, "PCIX:");
15207
15208 if ((clock_ctrl == 7) ||
15209 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
15210 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
15211 strcat(str, "133MHz");
15212 else if (clock_ctrl == 0)
15213 strcat(str, "33MHz");
15214 else if (clock_ctrl == 2)
15215 strcat(str, "50MHz");
15216 else if (clock_ctrl == 4)
15217 strcat(str, "66MHz");
15218 else if (clock_ctrl == 6)
15219 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070015220 } else {
15221 strcpy(str, "PCI:");
Joe Perches63c3a662011-04-26 08:12:10 +000015222 if (tg3_flag(tp, PCI_HIGH_SPEED))
Michael Chanf9804dd2005-09-27 12:13:10 -070015223 strcat(str, "66MHz");
15224 else
15225 strcat(str, "33MHz");
15226 }
Joe Perches63c3a662011-04-26 08:12:10 +000015227 if (tg3_flag(tp, PCI_32BIT))
Michael Chanf9804dd2005-09-27 12:13:10 -070015228 strcat(str, ":32-bit");
15229 else
15230 strcat(str, ":64-bit");
15231 return str;
15232}
15233
Michael Chan8c2dc7e2005-12-19 16:26:02 -080015234static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015235{
15236 struct pci_dev *peer;
15237 unsigned int func, devnr = tp->pdev->devfn & ~7;
15238
15239 for (func = 0; func < 8; func++) {
15240 peer = pci_get_slot(tp->pdev->bus, devnr | func);
15241 if (peer && peer != tp->pdev)
15242 break;
15243 pci_dev_put(peer);
15244 }
Michael Chan16fe9d72005-12-13 21:09:54 -080015245 /* 5704 can be configured in single-port mode, set peer to
15246 * tp->pdev in that case.
15247 */
15248 if (!peer) {
15249 peer = tp->pdev;
15250 return peer;
15251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015252
15253 /*
15254 * We don't need to keep the refcount elevated; there's no way
15255 * to remove one half of this device without removing the other
15256 */
15257 pci_dev_put(peer);
15258
15259 return peer;
15260}
15261
David S. Miller15f98502005-05-18 22:49:26 -070015262static void __devinit tg3_init_coal(struct tg3 *tp)
15263{
15264 struct ethtool_coalesce *ec = &tp->coal;
15265
15266 memset(ec, 0, sizeof(*ec));
15267 ec->cmd = ETHTOOL_GCOALESCE;
15268 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
15269 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
15270 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
15271 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
15272 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
15273 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
15274 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
15275 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
15276 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
15277
15278 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
15279 HOSTCC_MODE_CLRTICK_TXBD)) {
15280 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
15281 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
15282 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
15283 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
15284 }
Michael Chand244c892005-07-05 14:42:33 -070015285
Joe Perches63c3a662011-04-26 08:12:10 +000015286 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070015287 ec->rx_coalesce_usecs_irq = 0;
15288 ec->tx_coalesce_usecs_irq = 0;
15289 ec->stats_block_coalesce_usecs = 0;
15290 }
David S. Miller15f98502005-05-18 22:49:26 -070015291}
15292
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080015293static const struct net_device_ops tg3_netdev_ops = {
15294 .ndo_open = tg3_open,
15295 .ndo_stop = tg3_close,
Stephen Hemminger00829822008-11-20 20:14:53 -080015296 .ndo_start_xmit = tg3_start_xmit,
Eric Dumazet511d2222010-07-07 20:44:24 +000015297 .ndo_get_stats64 = tg3_get_stats64,
Stephen Hemminger00829822008-11-20 20:14:53 -080015298 .ndo_validate_addr = eth_validate_addr,
Jiri Pirkoafc4b132011-08-16 06:29:01 +000015299 .ndo_set_rx_mode = tg3_set_rx_mode,
Stephen Hemminger00829822008-11-20 20:14:53 -080015300 .ndo_set_mac_address = tg3_set_mac_addr,
15301 .ndo_do_ioctl = tg3_ioctl,
15302 .ndo_tx_timeout = tg3_tx_timeout,
15303 .ndo_change_mtu = tg3_change_mtu,
Michał Mirosławdc668912011-04-07 03:35:07 +000015304 .ndo_fix_features = tg3_fix_features,
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015305 .ndo_set_features = tg3_set_features,
Stephen Hemminger00829822008-11-20 20:14:53 -080015306#ifdef CONFIG_NET_POLL_CONTROLLER
15307 .ndo_poll_controller = tg3_poll_controller,
15308#endif
15309};
15310
Linus Torvalds1da177e2005-04-16 15:20:36 -070015311static int __devinit tg3_init_one(struct pci_dev *pdev,
15312 const struct pci_device_id *ent)
15313{
Linus Torvalds1da177e2005-04-16 15:20:36 -070015314 struct net_device *dev;
15315 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000015316 int i, err, pm_cap;
15317 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070015318 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080015319 u64 dma_mask, persist_dma_mask;
Michał Mirosławc8f44af2011-11-15 15:29:55 +000015320 netdev_features_t features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015321
Joe Perches05dbe002010-02-17 19:44:19 +000015322 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015323
15324 err = pci_enable_device(pdev);
15325 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015326 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015327 return err;
15328 }
15329
Linus Torvalds1da177e2005-04-16 15:20:36 -070015330 err = pci_request_regions(pdev, DRV_MODULE_NAME);
15331 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015332 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015333 goto err_out_disable_pdev;
15334 }
15335
15336 pci_set_master(pdev);
15337
15338 /* Find power-management capability. */
15339 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
15340 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000015341 dev_err(&pdev->dev,
15342 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015343 err = -EIO;
15344 goto err_out_free_res;
15345 }
15346
Matt Carlson16821282011-07-13 09:27:28 +000015347 err = pci_set_power_state(pdev, PCI_D0);
15348 if (err) {
15349 dev_err(&pdev->dev, "Transition to D0 failed, aborting\n");
15350 goto err_out_free_res;
15351 }
15352
Matt Carlsonfe5f5782009-09-01 13:09:39 +000015353 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015354 if (!dev) {
Matt Carlson2445e462010-04-05 10:19:21 +000015355 dev_err(&pdev->dev, "Etherdev alloc failed, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015356 err = -ENOMEM;
Matt Carlson16821282011-07-13 09:27:28 +000015357 goto err_out_power_down;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015358 }
15359
Linus Torvalds1da177e2005-04-16 15:20:36 -070015360 SET_NETDEV_DEV(dev, &pdev->dev);
15361
Linus Torvalds1da177e2005-04-16 15:20:36 -070015362 tp = netdev_priv(dev);
15363 tp->pdev = pdev;
15364 tp->dev = dev;
15365 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015366 tp->rx_mode = TG3_DEF_RX_MODE;
15367 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070015368
Linus Torvalds1da177e2005-04-16 15:20:36 -070015369 if (tg3_debug > 0)
15370 tp->msg_enable = tg3_debug;
15371 else
15372 tp->msg_enable = TG3_DEF_MSG_ENABLE;
15373
15374 /* The word/byte swap controls here control register access byte
15375 * swapping. DMA data byte swapping is controlled in the GRC_MODE
15376 * setting below.
15377 */
15378 tp->misc_host_ctrl =
15379 MISC_HOST_CTRL_MASK_PCI_INT |
15380 MISC_HOST_CTRL_WORD_SWAP |
15381 MISC_HOST_CTRL_INDIR_ACCESS |
15382 MISC_HOST_CTRL_PCISTATE_RW;
15383
15384 /* The NONFRM (non-frame) byte/word swap controls take effect
15385 * on descriptor entries, anything which isn't packet data.
15386 *
15387 * The StrongARM chips on the board (one for tx, one for rx)
15388 * are running in big-endian mode.
15389 */
15390 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
15391 GRC_MODE_WSWAP_NONFRM_DATA);
15392#ifdef __BIG_ENDIAN
15393 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
15394#endif
15395 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015396 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000015397 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015398
Matt Carlsond5fe4882008-11-21 17:20:32 -080015399 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010015400 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015401 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015402 err = -ENOMEM;
15403 goto err_out_free_dev;
15404 }
15405
Matt Carlsonc9cab242011-07-13 09:27:27 +000015406 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
15407 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761E ||
15408 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S ||
15409 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761SE ||
15410 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
15411 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
15412 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
15413 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720) {
15414 tg3_flag_set(tp, ENABLE_APE);
15415 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
15416 if (!tp->aperegs) {
15417 dev_err(&pdev->dev,
15418 "Cannot map APE registers, aborting\n");
15419 err = -ENOMEM;
15420 goto err_out_iounmap;
15421 }
15422 }
15423
Linus Torvalds1da177e2005-04-16 15:20:36 -070015424 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
15425 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015426
Linus Torvalds1da177e2005-04-16 15:20:36 -070015427 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015428 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Matt Carlson2ffcc982011-05-19 12:12:44 +000015429 dev->netdev_ops = &tg3_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015430 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015431
15432 err = tg3_get_invariants(tp);
15433 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015434 dev_err(&pdev->dev,
15435 "Problem fetching invariants of chip, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015436 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015437 }
15438
Michael Chan4a29cc22006-03-19 13:21:12 -080015439 /* The EPB bridge inside 5714, 5715, and 5780 and any
15440 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080015441 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
15442 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
15443 * do DMA address check in tg3_start_xmit().
15444 */
Joe Perches63c3a662011-04-26 08:12:10 +000015445 if (tg3_flag(tp, IS_5788))
Yang Hongyang284901a2009-04-06 19:01:15 -070015446 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Joe Perches63c3a662011-04-26 08:12:10 +000015447 else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070015448 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080015449#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070015450 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015451#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080015452 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070015453 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015454
15455 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070015456 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080015457 err = pci_set_dma_mask(pdev, dma_mask);
15458 if (!err) {
Matt Carlson0da06062011-05-19 12:12:53 +000015459 features |= NETIF_F_HIGHDMA;
Michael Chan72f2afb2006-03-06 19:28:35 -080015460 err = pci_set_consistent_dma_mask(pdev,
15461 persist_dma_mask);
15462 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015463 dev_err(&pdev->dev, "Unable to obtain 64 bit "
15464 "DMA for consistent allocations\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015465 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080015466 }
15467 }
15468 }
Yang Hongyang284901a2009-04-06 19:01:15 -070015469 if (err || dma_mask == DMA_BIT_MASK(32)) {
15470 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080015471 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015472 dev_err(&pdev->dev,
15473 "No usable DMA configuration, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015474 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080015475 }
15476 }
15477
Michael Chanfdfec1722005-07-25 12:31:48 -070015478 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015479
Matt Carlson0da06062011-05-19 12:12:53 +000015480 features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
15481
15482 /* 5700 B0 chips do not support checksumming correctly due
15483 * to hardware bugs.
15484 */
15485 if (tp->pci_chip_rev_id != CHIPREV_ID_5700_B0) {
15486 features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
15487
15488 if (tg3_flag(tp, 5755_PLUS))
15489 features |= NETIF_F_IPV6_CSUM;
15490 }
15491
Michael Chan4e3a7aa2006-03-20 17:47:44 -080015492 /* TSO is on by default on chips that support hardware TSO.
15493 * Firmware TSO on older chips gives lower performance, so it
15494 * is off by default, but can be enabled using ethtool.
15495 */
Joe Perches63c3a662011-04-26 08:12:10 +000015496 if ((tg3_flag(tp, HW_TSO_1) ||
15497 tg3_flag(tp, HW_TSO_2) ||
15498 tg3_flag(tp, HW_TSO_3)) &&
Matt Carlson0da06062011-05-19 12:12:53 +000015499 (features & NETIF_F_IP_CSUM))
15500 features |= NETIF_F_TSO;
Joe Perches63c3a662011-04-26 08:12:10 +000015501 if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
Matt Carlson0da06062011-05-19 12:12:53 +000015502 if (features & NETIF_F_IPV6_CSUM)
15503 features |= NETIF_F_TSO6;
Joe Perches63c3a662011-04-26 08:12:10 +000015504 if (tg3_flag(tp, HW_TSO_3) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000015505 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070015506 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
15507 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Joe Perches63c3a662011-04-26 08:12:10 +000015508 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Michał Mirosławdc668912011-04-07 03:35:07 +000015509 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson0da06062011-05-19 12:12:53 +000015510 features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070015511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015512
Matt Carlsond542fe22011-05-19 16:02:43 +000015513 dev->features |= features;
15514 dev->vlan_features |= features;
15515
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015516 /*
15517 * Add loopback capability only for a subset of devices that support
15518 * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
15519 * loopback for the remaining devices.
15520 */
15521 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
15522 !tg3_flag(tp, CPMU_PRESENT))
15523 /* Add the loopback capability */
Matt Carlson0da06062011-05-19 12:12:53 +000015524 features |= NETIF_F_LOOPBACK;
15525
Matt Carlson0da06062011-05-19 12:12:53 +000015526 dev->hw_features |= features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015527
Linus Torvalds1da177e2005-04-16 15:20:36 -070015528 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015529 !tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015530 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015531 tg3_flag_set(tp, MAX_RXPEND_64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015532 tp->rx_pending = 63;
15533 }
15534
Linus Torvalds1da177e2005-04-16 15:20:36 -070015535 err = tg3_get_device_address(tp);
15536 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015537 dev_err(&pdev->dev,
15538 "Could not obtain valid ethernet address, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015539 goto err_out_apeunmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070015540 }
15541
Matt Carlsonc88864d2007-11-12 21:07:01 -080015542 /*
15543 * Reset chip in case UNDI or EFI driver did not shutdown
15544 * DMA self test will enable WDMAC and we'll see (spurious)
15545 * pending DMA on the PCI bus at that point.
15546 */
15547 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
15548 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
15549 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
15550 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
15551 }
15552
15553 err = tg3_test_dma(tp);
15554 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015555 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080015556 goto err_out_apeunmap;
15557 }
15558
Matt Carlson78f90dc2009-11-13 13:03:42 +000015559 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
15560 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
15561 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000015562 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000015563 struct tg3_napi *tnapi = &tp->napi[i];
15564
15565 tnapi->tp = tp;
15566 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
15567
15568 tnapi->int_mbox = intmbx;
Matt Carlson93a700a2011-08-31 11:44:54 +000015569 if (i <= 4)
Matt Carlson78f90dc2009-11-13 13:03:42 +000015570 intmbx += 0x8;
15571 else
15572 intmbx += 0x4;
15573
15574 tnapi->consmbox = rcvmbx;
15575 tnapi->prodmbox = sndmbx;
15576
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015577 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000015578 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015579 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000015580 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000015581
Joe Perches63c3a662011-04-26 08:12:10 +000015582 if (!tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson78f90dc2009-11-13 13:03:42 +000015583 break;
15584
15585 /*
15586 * If we support MSIX, we'll be using RSS. If we're using
15587 * RSS, the first vector only handles link interrupts and the
15588 * remaining vectors handle rx and tx interrupts. Reuse the
15589 * mailbox values for the next iteration. The values we setup
15590 * above are still useful for the single vectored mode.
15591 */
15592 if (!i)
15593 continue;
15594
15595 rcvmbx += 0x8;
15596
15597 if (sndmbx & 0x4)
15598 sndmbx -= 0x4;
15599 else
15600 sndmbx += 0xc;
15601 }
15602
Matt Carlsonc88864d2007-11-12 21:07:01 -080015603 tg3_init_coal(tp);
15604
Michael Chanc49a1562006-12-17 17:07:29 -080015605 pci_set_drvdata(pdev, dev);
15606
Matt Carlsoncd0d7222011-07-13 09:27:33 +000015607 if (tg3_flag(tp, 5717_PLUS)) {
15608 /* Resume a low-power mode */
15609 tg3_frob_aux_power(tp, false);
15610 }
15611
Linus Torvalds1da177e2005-04-16 15:20:36 -070015612 err = register_netdev(dev);
15613 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015614 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070015615 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015616 }
15617
Joe Perches05dbe002010-02-17 19:44:19 +000015618 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
15619 tp->board_part_number,
15620 tp->pci_chip_rev_id,
15621 tg3_bus_string(tp, str),
15622 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015623
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015624 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000015625 struct phy_device *phydev;
15626 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000015627 netdev_info(dev,
15628 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000015629 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015630 } else {
15631 char *ethtype;
15632
15633 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
15634 ethtype = "10/100Base-TX";
15635 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
15636 ethtype = "1000Base-SX";
15637 else
15638 ethtype = "10/100/1000Base-T";
15639
Matt Carlson5129c3a2010-04-05 10:19:23 +000015640 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlson47007832011-04-20 07:57:43 +000015641 "(WireSpeed[%d], EEE[%d])\n",
15642 tg3_phy_string(tp), ethtype,
15643 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
15644 (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015645 }
Matt Carlsondf59c942008-11-03 16:52:56 -080015646
Joe Perches05dbe002010-02-17 19:44:19 +000015647 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000015648 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015649 tg3_flag(tp, USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015650 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015651 tg3_flag(tp, ENABLE_ASF) != 0,
15652 tg3_flag(tp, TSO_CAPABLE) != 0);
Joe Perches05dbe002010-02-17 19:44:19 +000015653 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
15654 tp->dma_rwctrl,
15655 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
15656 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015657
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015658 pci_save_state(pdev);
15659
Linus Torvalds1da177e2005-04-16 15:20:36 -070015660 return 0;
15661
Matt Carlson0d3031d2007-10-10 18:02:43 -070015662err_out_apeunmap:
15663 if (tp->aperegs) {
15664 iounmap(tp->aperegs);
15665 tp->aperegs = NULL;
15666 }
15667
Linus Torvalds1da177e2005-04-16 15:20:36 -070015668err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070015669 if (tp->regs) {
15670 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015671 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015673
15674err_out_free_dev:
15675 free_netdev(dev);
15676
Matt Carlson16821282011-07-13 09:27:28 +000015677err_out_power_down:
15678 pci_set_power_state(pdev, PCI_D3hot);
15679
Linus Torvalds1da177e2005-04-16 15:20:36 -070015680err_out_free_res:
15681 pci_release_regions(pdev);
15682
15683err_out_disable_pdev:
15684 pci_disable_device(pdev);
15685 pci_set_drvdata(pdev, NULL);
15686 return err;
15687}
15688
15689static void __devexit tg3_remove_one(struct pci_dev *pdev)
15690{
15691 struct net_device *dev = pci_get_drvdata(pdev);
15692
15693 if (dev) {
15694 struct tg3 *tp = netdev_priv(dev);
15695
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080015696 if (tp->fw)
15697 release_firmware(tp->fw);
15698
Matt Carlsondb219972011-11-04 09:15:03 +000015699 tg3_reset_task_cancel(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015700
David S. Miller1805b2f2011-10-24 18:18:09 -040015701 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015702 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015703 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015704 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070015705
Linus Torvalds1da177e2005-04-16 15:20:36 -070015706 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015707 if (tp->aperegs) {
15708 iounmap(tp->aperegs);
15709 tp->aperegs = NULL;
15710 }
Michael Chan68929142005-08-09 20:17:14 -070015711 if (tp->regs) {
15712 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015713 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015714 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015715 free_netdev(dev);
15716 pci_release_regions(pdev);
15717 pci_disable_device(pdev);
15718 pci_set_drvdata(pdev, NULL);
15719 }
15720}
15721
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015722#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015723static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015724{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015725 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015726 struct net_device *dev = pci_get_drvdata(pdev);
15727 struct tg3 *tp = netdev_priv(dev);
15728 int err;
15729
15730 if (!netif_running(dev))
15731 return 0;
15732
Matt Carlsondb219972011-11-04 09:15:03 +000015733 tg3_reset_task_cancel(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015734 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015735 tg3_netif_stop(tp);
15736
15737 del_timer_sync(&tp->timer);
15738
David S. Millerf47c11e2005-06-24 20:18:35 -070015739 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015740 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070015741 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015742
15743 netif_device_detach(dev);
15744
David S. Millerf47c11e2005-06-24 20:18:35 -070015745 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070015746 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches63c3a662011-04-26 08:12:10 +000015747 tg3_flag_clear(tp, INIT_COMPLETE);
David S. Millerf47c11e2005-06-24 20:18:35 -070015748 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015749
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015750 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015751 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015752 int err2;
15753
David S. Millerf47c11e2005-06-24 20:18:35 -070015754 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015755
Joe Perches63c3a662011-04-26 08:12:10 +000015756 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015757 err2 = tg3_restart_hw(tp, 1);
15758 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070015759 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015760
15761 tp->timer.expires = jiffies + tp->timer_offset;
15762 add_timer(&tp->timer);
15763
15764 netif_device_attach(dev);
15765 tg3_netif_start(tp);
15766
Michael Chanb9ec6c12006-07-25 16:37:27 -070015767out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015768 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015769
15770 if (!err2)
15771 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015772 }
15773
15774 return err;
15775}
15776
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015777static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015778{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015779 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015780 struct net_device *dev = pci_get_drvdata(pdev);
15781 struct tg3 *tp = netdev_priv(dev);
15782 int err;
15783
15784 if (!netif_running(dev))
15785 return 0;
15786
Linus Torvalds1da177e2005-04-16 15:20:36 -070015787 netif_device_attach(dev);
15788
David S. Millerf47c11e2005-06-24 20:18:35 -070015789 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015790
Joe Perches63c3a662011-04-26 08:12:10 +000015791 tg3_flag_set(tp, INIT_COMPLETE);
Michael Chanb9ec6c12006-07-25 16:37:27 -070015792 err = tg3_restart_hw(tp, 1);
15793 if (err)
15794 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015795
15796 tp->timer.expires = jiffies + tp->timer_offset;
15797 add_timer(&tp->timer);
15798
Linus Torvalds1da177e2005-04-16 15:20:36 -070015799 tg3_netif_start(tp);
15800
Michael Chanb9ec6c12006-07-25 16:37:27 -070015801out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015802 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015803
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015804 if (!err)
15805 tg3_phy_start(tp);
15806
Michael Chanb9ec6c12006-07-25 16:37:27 -070015807 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015808}
15809
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015810static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015811#define TG3_PM_OPS (&tg3_pm_ops)
15812
15813#else
15814
15815#define TG3_PM_OPS NULL
15816
15817#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015818
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015819/**
15820 * tg3_io_error_detected - called when PCI error is detected
15821 * @pdev: Pointer to PCI device
15822 * @state: The current pci connection state
15823 *
15824 * This function is called after a PCI bus error affecting
15825 * this device has been detected.
15826 */
15827static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
15828 pci_channel_state_t state)
15829{
15830 struct net_device *netdev = pci_get_drvdata(pdev);
15831 struct tg3 *tp = netdev_priv(netdev);
15832 pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
15833
15834 netdev_info(netdev, "PCI I/O error detected\n");
15835
15836 rtnl_lock();
15837
15838 if (!netif_running(netdev))
15839 goto done;
15840
15841 tg3_phy_stop(tp);
15842
15843 tg3_netif_stop(tp);
15844
15845 del_timer_sync(&tp->timer);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015846
15847 /* Want to make sure that the reset task doesn't run */
Matt Carlsondb219972011-11-04 09:15:03 +000015848 tg3_reset_task_cancel(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000015849 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015850
15851 netif_device_detach(netdev);
15852
15853 /* Clean up software state, even if MMIO is blocked */
15854 tg3_full_lock(tp, 0);
15855 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
15856 tg3_full_unlock(tp);
15857
15858done:
15859 if (state == pci_channel_io_perm_failure)
15860 err = PCI_ERS_RESULT_DISCONNECT;
15861 else
15862 pci_disable_device(pdev);
15863
15864 rtnl_unlock();
15865
15866 return err;
15867}
15868
15869/**
15870 * tg3_io_slot_reset - called after the pci bus has been reset.
15871 * @pdev: Pointer to PCI device
15872 *
15873 * Restart the card from scratch, as if from a cold-boot.
15874 * At this point, the card has exprienced a hard reset,
15875 * followed by fixups by BIOS, and has its config space
15876 * set up identically to what it was at cold boot.
15877 */
15878static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
15879{
15880 struct net_device *netdev = pci_get_drvdata(pdev);
15881 struct tg3 *tp = netdev_priv(netdev);
15882 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
15883 int err;
15884
15885 rtnl_lock();
15886
15887 if (pci_enable_device(pdev)) {
15888 netdev_err(netdev, "Cannot re-enable PCI device after reset.\n");
15889 goto done;
15890 }
15891
15892 pci_set_master(pdev);
15893 pci_restore_state(pdev);
15894 pci_save_state(pdev);
15895
15896 if (!netif_running(netdev)) {
15897 rc = PCI_ERS_RESULT_RECOVERED;
15898 goto done;
15899 }
15900
15901 err = tg3_power_up(tp);
Matt Carlsonbed98292011-07-13 09:27:29 +000015902 if (err)
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015903 goto done;
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015904
15905 rc = PCI_ERS_RESULT_RECOVERED;
15906
15907done:
15908 rtnl_unlock();
15909
15910 return rc;
15911}
15912
15913/**
15914 * tg3_io_resume - called when traffic can start flowing again.
15915 * @pdev: Pointer to PCI device
15916 *
15917 * This callback is called when the error recovery driver tells
15918 * us that its OK to resume normal operation.
15919 */
15920static void tg3_io_resume(struct pci_dev *pdev)
15921{
15922 struct net_device *netdev = pci_get_drvdata(pdev);
15923 struct tg3 *tp = netdev_priv(netdev);
15924 int err;
15925
15926 rtnl_lock();
15927
15928 if (!netif_running(netdev))
15929 goto done;
15930
15931 tg3_full_lock(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +000015932 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015933 err = tg3_restart_hw(tp, 1);
15934 tg3_full_unlock(tp);
15935 if (err) {
15936 netdev_err(netdev, "Cannot restart hardware after reset.\n");
15937 goto done;
15938 }
15939
15940 netif_device_attach(netdev);
15941
15942 tp->timer.expires = jiffies + tp->timer_offset;
15943 add_timer(&tp->timer);
15944
15945 tg3_netif_start(tp);
15946
15947 tg3_phy_start(tp);
15948
15949done:
15950 rtnl_unlock();
15951}
15952
15953static struct pci_error_handlers tg3_err_handler = {
15954 .error_detected = tg3_io_error_detected,
15955 .slot_reset = tg3_io_slot_reset,
15956 .resume = tg3_io_resume
15957};
15958
Linus Torvalds1da177e2005-04-16 15:20:36 -070015959static struct pci_driver tg3_driver = {
15960 .name = DRV_MODULE_NAME,
15961 .id_table = tg3_pci_tbl,
15962 .probe = tg3_init_one,
15963 .remove = __devexit_p(tg3_remove_one),
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015964 .err_handler = &tg3_err_handler,
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015965 .driver.pm = TG3_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070015966};
15967
15968static int __init tg3_init(void)
15969{
Jeff Garzik29917622006-08-19 17:48:59 -040015970 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015971}
15972
15973static void __exit tg3_cleanup(void)
15974{
15975 pci_unregister_driver(&tg3_driver);
15976}
15977
15978module_init(tg3_init);
15979module_exit(tg3_cleanup);