blob: 161cbbb4814ad1ae99fa75f3473a28fac8bcf768 [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 Carlsoneaa36662011-08-19 13:58:24 +000092#define TG3_MIN_NUM 120
Matt Carlson6867c842010-07-11 09:31:44 +000093#define DRV_MODULE_VERSION \
94 __stringify(TG3_MAJ_NUM) "." __stringify(TG3_MIN_NUM)
Matt Carlsoneaa36662011-08-19 13:58:24 +000095#define DRV_MODULE_RELDATE "August 18, 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
197#define TG3_RX_OFFSET(tp) 0
198#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 Carlson66711e62009-11-13 13:03:49 +0000473 if (off == TG3_RX_STD_PROD_IDX_REG) {
Michael Chan68929142005-08-09 20:17:14 -0700474 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
475 TG3_64BIT_REG_LOW, val);
476 return;
477 }
478
479 spin_lock_irqsave(&tp->indirect_lock, flags);
480 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
481 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
482 spin_unlock_irqrestore(&tp->indirect_lock, flags);
483
484 /* In indirect mode when disabling interrupts, we also need
485 * to clear the interrupt bit in the GRC local ctrl register.
486 */
487 if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
488 (val == 0x1)) {
489 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
490 tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
491 }
492}
493
494static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
495{
496 unsigned long flags;
497 u32 val;
498
499 spin_lock_irqsave(&tp->indirect_lock, flags);
500 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
501 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
502 spin_unlock_irqrestore(&tp->indirect_lock, flags);
503 return val;
504}
505
Michael Chanb401e9e2005-12-19 16:27:04 -0800506/* usec_wait specifies the wait time in usec when writing to certain registers
507 * where it is unsafe to read back the register without some delay.
508 * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
509 * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
510 */
511static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
Joe Perches63c3a662011-04-26 08:12:10 +0000513 if (tg3_flag(tp, PCIX_TARGET_HWBUG) || tg3_flag(tp, ICH_WORKAROUND))
Michael Chanb401e9e2005-12-19 16:27:04 -0800514 /* Non-posted methods */
515 tp->write32(tp, off, val);
516 else {
517 /* Posted method */
518 tg3_write32(tp, off, val);
519 if (usec_wait)
520 udelay(usec_wait);
521 tp->read32(tp, off);
522 }
523 /* Wait again after the read for the posted method to guarantee that
524 * the wait time is met.
525 */
526 if (usec_wait)
527 udelay(usec_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
Michael Chan09ee9292005-08-09 20:17:00 -0700530static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
531{
532 tp->write32_mbox(tp, off, val);
Joe Perches63c3a662011-04-26 08:12:10 +0000533 if (!tg3_flag(tp, MBOX_WRITE_REORDER) && !tg3_flag(tp, ICH_WORKAROUND))
Michael Chan68929142005-08-09 20:17:14 -0700534 tp->read32_mbox(tp, off);
Michael Chan09ee9292005-08-09 20:17:00 -0700535}
536
Michael Chan20094932005-08-09 20:16:32 -0700537static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
539 void __iomem *mbox = tp->regs + off;
540 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000541 if (tg3_flag(tp, TXD_MBOX_HWBUG))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000543 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 readl(mbox);
545}
546
Michael Chanb5d37722006-09-27 16:06:21 -0700547static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
548{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000549 return readl(tp->regs + off + GRCMBOX_BASE);
Michael Chanb5d37722006-09-27 16:06:21 -0700550}
551
552static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
553{
554 writel(val, tp->regs + off + GRCMBOX_BASE);
555}
556
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000557#define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700558#define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000559#define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
560#define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
561#define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
Michael Chan20094932005-08-09 20:16:32 -0700562
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000563#define tw32(reg, val) tp->write32(tp, reg, val)
564#define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
565#define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
566#define tr32(reg) tp->read32(tp, reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
569{
Michael Chan68929142005-08-09 20:17:14 -0700570 unsigned long flags;
571
Matt Carlson6ff6f812011-05-19 12:12:54 +0000572 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700573 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
574 return;
575
Michael Chan68929142005-08-09 20:17:14 -0700576 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000577 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700578 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
579 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Michael Chanbbadf502006-04-06 21:46:34 -0700581 /* Always leave this as zero. */
582 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
583 } else {
584 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
585 tw32_f(TG3PCI_MEM_WIN_DATA, val);
586
587 /* Always leave this as zero. */
588 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
589 }
Michael Chan68929142005-08-09 20:17:14 -0700590 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592
593static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
594{
Michael Chan68929142005-08-09 20:17:14 -0700595 unsigned long flags;
596
Matt Carlson6ff6f812011-05-19 12:12:54 +0000597 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700598 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
599 *val = 0;
600 return;
601 }
602
Michael Chan68929142005-08-09 20:17:14 -0700603 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000604 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700605 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
606 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Michael Chanbbadf502006-04-06 21:46:34 -0700608 /* Always leave this as zero. */
609 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
610 } else {
611 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
612 *val = tr32(TG3PCI_MEM_WIN_DATA);
613
614 /* Always leave this as zero. */
615 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
616 }
Michael Chan68929142005-08-09 20:17:14 -0700617 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618}
619
Matt Carlson0d3031d2007-10-10 18:02:43 -0700620static void tg3_ape_lock_init(struct tg3 *tp)
621{
622 int i;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000623 u32 regbase, bit;
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000624
625 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
626 regbase = TG3_APE_LOCK_GRANT;
627 else
628 regbase = TG3_APE_PER_LOCK_GRANT;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700629
630 /* Make sure the driver hasn't any stale locks. */
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000631 for (i = 0; i < 8; i++) {
632 if (i == TG3_APE_LOCK_GPIO)
633 continue;
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000634 tg3_ape_write32(tp, regbase + 4 * i, APE_LOCK_GRANT_DRIVER);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000635 }
636
637 /* Clear the correct bit of the GPIO lock too. */
638 if (!tp->pci_fn)
639 bit = APE_LOCK_GRANT_DRIVER;
640 else
641 bit = 1 << tp->pci_fn;
642
643 tg3_ape_write32(tp, regbase + 4 * TG3_APE_LOCK_GPIO, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700644}
645
646static int tg3_ape_lock(struct tg3 *tp, int locknum)
647{
648 int i, off;
649 int ret = 0;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000650 u32 status, req, gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700651
Joe Perches63c3a662011-04-26 08:12:10 +0000652 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700653 return 0;
654
655 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000656 case TG3_APE_LOCK_GPIO:
657 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
658 return 0;
Matt Carlson33f401a2010-04-05 10:19:27 +0000659 case TG3_APE_LOCK_GRC:
660 case TG3_APE_LOCK_MEM:
661 break;
662 default:
663 return -EINVAL;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700664 }
665
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000666 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
667 req = TG3_APE_LOCK_REQ;
668 gnt = TG3_APE_LOCK_GRANT;
669 } else {
670 req = TG3_APE_PER_LOCK_REQ;
671 gnt = TG3_APE_PER_LOCK_GRANT;
672 }
673
Matt Carlson0d3031d2007-10-10 18:02:43 -0700674 off = 4 * locknum;
675
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000676 if (locknum != TG3_APE_LOCK_GPIO || !tp->pci_fn)
677 bit = APE_LOCK_REQ_DRIVER;
678 else
679 bit = 1 << tp->pci_fn;
680
681 tg3_ape_write32(tp, req + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700682
683 /* Wait for up to 1 millisecond to acquire lock. */
684 for (i = 0; i < 100; i++) {
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000685 status = tg3_ape_read32(tp, gnt + off);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000686 if (status == bit)
Matt Carlson0d3031d2007-10-10 18:02:43 -0700687 break;
688 udelay(10);
689 }
690
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000691 if (status != bit) {
Matt Carlson0d3031d2007-10-10 18:02:43 -0700692 /* Revoke the lock request. */
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000693 tg3_ape_write32(tp, gnt + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700694 ret = -EBUSY;
695 }
696
697 return ret;
698}
699
700static void tg3_ape_unlock(struct tg3 *tp, int locknum)
701{
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000702 u32 gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700703
Joe Perches63c3a662011-04-26 08:12:10 +0000704 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700705 return;
706
707 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000708 case TG3_APE_LOCK_GPIO:
709 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
710 return;
Matt Carlson33f401a2010-04-05 10:19:27 +0000711 case TG3_APE_LOCK_GRC:
712 case TG3_APE_LOCK_MEM:
713 break;
714 default:
715 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700716 }
717
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000718 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
719 gnt = TG3_APE_LOCK_GRANT;
720 else
721 gnt = TG3_APE_PER_LOCK_GRANT;
722
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000723 if (locknum != TG3_APE_LOCK_GPIO || !tp->pci_fn)
724 bit = APE_LOCK_GRANT_DRIVER;
725 else
726 bit = 1 << tp->pci_fn;
727
728 tg3_ape_write32(tp, gnt + 4 * locknum, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700729}
730
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000731static void tg3_ape_send_event(struct tg3 *tp, u32 event)
732{
733 int i;
734 u32 apedata;
735
736 /* NCSI does not support APE events */
737 if (tg3_flag(tp, APE_HAS_NCSI))
738 return;
739
740 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
741 if (apedata != APE_SEG_SIG_MAGIC)
742 return;
743
744 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
745 if (!(apedata & APE_FW_STATUS_READY))
746 return;
747
748 /* Wait for up to 1 millisecond for APE to service previous event. */
749 for (i = 0; i < 10; i++) {
750 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
751 return;
752
753 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
754
755 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
756 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
757 event | APE_EVENT_STATUS_EVENT_PENDING);
758
759 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
760
761 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
762 break;
763
764 udelay(100);
765 }
766
767 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
768 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
769}
770
771static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
772{
773 u32 event;
774 u32 apedata;
775
776 if (!tg3_flag(tp, ENABLE_APE))
777 return;
778
779 switch (kind) {
780 case RESET_KIND_INIT:
781 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
782 APE_HOST_SEG_SIG_MAGIC);
783 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
784 APE_HOST_SEG_LEN_MAGIC);
785 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
786 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
787 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
788 APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
789 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
790 APE_HOST_BEHAV_NO_PHYLOCK);
791 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
792 TG3_APE_HOST_DRVR_STATE_START);
793
794 event = APE_EVENT_STATUS_STATE_START;
795 break;
796 case RESET_KIND_SHUTDOWN:
797 /* With the interface we are currently using,
798 * APE does not track driver state. Wiping
799 * out the HOST SEGMENT SIGNATURE forces
800 * the APE to assume OS absent status.
801 */
802 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
803
804 if (device_may_wakeup(&tp->pdev->dev) &&
805 tg3_flag(tp, WOL_ENABLE)) {
806 tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
807 TG3_APE_HOST_WOL_SPEED_AUTO);
808 apedata = TG3_APE_HOST_DRVR_STATE_WOL;
809 } else
810 apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
811
812 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
813
814 event = APE_EVENT_STATUS_STATE_UNLOAD;
815 break;
816 case RESET_KIND_SUSPEND:
817 event = APE_EVENT_STATUS_STATE_SUSPEND;
818 break;
819 default:
820 return;
821 }
822
823 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
824
825 tg3_ape_send_event(tp, event);
826}
827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828static void tg3_disable_ints(struct tg3 *tp)
829{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000830 int i;
831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 tw32(TG3PCI_MISC_HOST_CTRL,
833 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000834 for (i = 0; i < tp->irq_max; i++)
835 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836}
837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838static void tg3_enable_ints(struct tg3 *tp)
839{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000840 int i;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000841
Michael Chanbbe832c2005-06-24 20:20:04 -0700842 tp->irq_sync = 0;
843 wmb();
844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 tw32(TG3PCI_MISC_HOST_CTRL,
846 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000847
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000848 tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000849 for (i = 0; i < tp->irq_cnt; i++) {
850 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000851
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000852 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
Joe Perches63c3a662011-04-26 08:12:10 +0000853 if (tg3_flag(tp, 1SHOT_MSI))
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000854 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
855
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000856 tp->coal_now |= tnapi->coal_now;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000857 }
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000858
859 /* Force an initial interrupt */
Joe Perches63c3a662011-04-26 08:12:10 +0000860 if (!tg3_flag(tp, TAGGED_STATUS) &&
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000861 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
862 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
863 else
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000864 tw32(HOSTCC_MODE, tp->coal_now);
865
866 tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867}
868
Matt Carlson17375d22009-08-28 14:02:18 +0000869static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
Michael Chan04237dd2005-04-25 15:17:17 -0700870{
Matt Carlson17375d22009-08-28 14:02:18 +0000871 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +0000872 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan04237dd2005-04-25 15:17:17 -0700873 unsigned int work_exists = 0;
874
875 /* check for phy events */
Joe Perches63c3a662011-04-26 08:12:10 +0000876 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Michael Chan04237dd2005-04-25 15:17:17 -0700877 if (sblk->status & SD_STATUS_LINK_CHG)
878 work_exists = 1;
879 }
880 /* check for RX/TX work to do */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000881 if (sblk->idx[0].tx_consumer != tnapi->tx_cons ||
Matt Carlson8d9d7cf2009-09-01 13:19:05 +0000882 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -0700883 work_exists = 1;
884
885 return work_exists;
886}
887
Matt Carlson17375d22009-08-28 14:02:18 +0000888/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -0700889 * similar to tg3_enable_ints, but it accurately determines whether there
890 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400891 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 */
Matt Carlson17375d22009-08-28 14:02:18 +0000893static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
Matt Carlson17375d22009-08-28 14:02:18 +0000895 struct tg3 *tp = tnapi->tp;
896
Matt Carlson898a56f2009-08-28 14:02:40 +0000897 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 mmiowb();
899
David S. Millerfac9b832005-05-18 22:46:34 -0700900 /* When doing tagged status, this work check is unnecessary.
901 * The last_tag we write above tells the chip which piece of
902 * work we've completed.
903 */
Joe Perches63c3a662011-04-26 08:12:10 +0000904 if (!tg3_flag(tp, TAGGED_STATUS) && tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -0700905 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +0000906 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907}
908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909static void tg3_switch_clocks(struct tg3 *tp)
910{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000911 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 u32 orig_clock_ctrl;
913
Joe Perches63c3a662011-04-26 08:12:10 +0000914 if (tg3_flag(tp, CPMU_PRESENT) || tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -0700915 return;
916
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000917 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
918
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 orig_clock_ctrl = clock_ctrl;
920 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
921 CLOCK_CTRL_CLKRUN_OENABLE |
922 0x1f);
923 tp->pci_clock_ctrl = clock_ctrl;
924
Joe Perches63c3a662011-04-26 08:12:10 +0000925 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800927 tw32_wait_f(TG3PCI_CLOCK_CTRL,
928 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 }
930 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800931 tw32_wait_f(TG3PCI_CLOCK_CTRL,
932 clock_ctrl |
933 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
934 40);
935 tw32_wait_f(TG3PCI_CLOCK_CTRL,
936 clock_ctrl | (CLOCK_CTRL_ALTCLK),
937 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 }
Michael Chanb401e9e2005-12-19 16:27:04 -0800939 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940}
941
942#define PHY_BUSY_LOOPS 5000
943
944static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
945{
946 u32 frame_val;
947 unsigned int loops;
948 int ret;
949
950 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
951 tw32_f(MAC_MI_MODE,
952 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
953 udelay(80);
954 }
955
956 *val = 0x0;
957
Matt Carlson882e9792009-09-01 13:21:36 +0000958 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 MI_COM_PHY_ADDR_MASK);
960 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
961 MI_COM_REG_ADDR_MASK);
962 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 tw32_f(MAC_MI_COM, frame_val);
965
966 loops = PHY_BUSY_LOOPS;
967 while (loops != 0) {
968 udelay(10);
969 frame_val = tr32(MAC_MI_COM);
970
971 if ((frame_val & MI_COM_BUSY) == 0) {
972 udelay(5);
973 frame_val = tr32(MAC_MI_COM);
974 break;
975 }
976 loops -= 1;
977 }
978
979 ret = -EBUSY;
980 if (loops != 0) {
981 *val = frame_val & MI_COM_DATA_MASK;
982 ret = 0;
983 }
984
985 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
986 tw32_f(MAC_MI_MODE, tp->mi_mode);
987 udelay(80);
988 }
989
990 return ret;
991}
992
993static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
994{
995 u32 frame_val;
996 unsigned int loops;
997 int ret;
998
Matt Carlsonf07e9af2010-08-02 11:26:07 +0000999 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson221c5632011-06-13 13:39:01 +00001000 (reg == MII_CTRL1000 || reg == MII_TG3_AUX_CTRL))
Michael Chanb5d37722006-09-27 16:06:21 -07001001 return 0;
1002
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1004 tw32_f(MAC_MI_MODE,
1005 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
1006 udelay(80);
1007 }
1008
Matt Carlson882e9792009-09-01 13:21:36 +00001009 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 MI_COM_PHY_ADDR_MASK);
1011 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
1012 MI_COM_REG_ADDR_MASK);
1013 frame_val |= (val & MI_COM_DATA_MASK);
1014 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 tw32_f(MAC_MI_COM, frame_val);
1017
1018 loops = PHY_BUSY_LOOPS;
1019 while (loops != 0) {
1020 udelay(10);
1021 frame_val = tr32(MAC_MI_COM);
1022 if ((frame_val & MI_COM_BUSY) == 0) {
1023 udelay(5);
1024 frame_val = tr32(MAC_MI_COM);
1025 break;
1026 }
1027 loops -= 1;
1028 }
1029
1030 ret = -EBUSY;
1031 if (loops != 0)
1032 ret = 0;
1033
1034 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1035 tw32_f(MAC_MI_MODE, tp->mi_mode);
1036 udelay(80);
1037 }
1038
1039 return ret;
1040}
1041
Matt Carlsonb0988c12011-04-20 07:57:39 +00001042static int tg3_phy_cl45_write(struct tg3 *tp, u32 devad, u32 addr, u32 val)
1043{
1044 int err;
1045
1046 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1047 if (err)
1048 goto done;
1049
1050 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1051 if (err)
1052 goto done;
1053
1054 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1055 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1056 if (err)
1057 goto done;
1058
1059 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, val);
1060
1061done:
1062 return err;
1063}
1064
1065static int tg3_phy_cl45_read(struct tg3 *tp, u32 devad, u32 addr, u32 *val)
1066{
1067 int err;
1068
1069 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1070 if (err)
1071 goto done;
1072
1073 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1074 if (err)
1075 goto done;
1076
1077 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1078 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1079 if (err)
1080 goto done;
1081
1082 err = tg3_readphy(tp, MII_TG3_MMD_ADDRESS, val);
1083
1084done:
1085 return err;
1086}
1087
1088static int tg3_phydsp_read(struct tg3 *tp, u32 reg, u32 *val)
1089{
1090 int err;
1091
1092 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1093 if (!err)
1094 err = tg3_readphy(tp, MII_TG3_DSP_RW_PORT, val);
1095
1096 return err;
1097}
1098
1099static int tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
1100{
1101 int err;
1102
1103 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1104 if (!err)
1105 err = tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
1106
1107 return err;
1108}
1109
Matt Carlson15ee95c2011-04-20 07:57:40 +00001110static int tg3_phy_auxctl_read(struct tg3 *tp, int reg, u32 *val)
1111{
1112 int err;
1113
1114 err = tg3_writephy(tp, MII_TG3_AUX_CTRL,
1115 (reg << MII_TG3_AUXCTL_MISC_RDSEL_SHIFT) |
1116 MII_TG3_AUXCTL_SHDWSEL_MISC);
1117 if (!err)
1118 err = tg3_readphy(tp, MII_TG3_AUX_CTRL, val);
1119
1120 return err;
1121}
1122
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001123static int tg3_phy_auxctl_write(struct tg3 *tp, int reg, u32 set)
1124{
1125 if (reg == MII_TG3_AUXCTL_SHDWSEL_MISC)
1126 set |= MII_TG3_AUXCTL_MISC_WREN;
1127
1128 return tg3_writephy(tp, MII_TG3_AUX_CTRL, set | reg);
1129}
1130
Matt Carlson1d36ba42011-04-20 07:57:42 +00001131#define TG3_PHY_AUXCTL_SMDSP_ENABLE(tp) \
1132 tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
1133 MII_TG3_AUXCTL_ACTL_SMDSP_ENA | \
1134 MII_TG3_AUXCTL_ACTL_TX_6DB)
1135
1136#define TG3_PHY_AUXCTL_SMDSP_DISABLE(tp) \
1137 tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
1138 MII_TG3_AUXCTL_ACTL_TX_6DB);
1139
Matt Carlson95e28692008-05-25 23:44:14 -07001140static int tg3_bmcr_reset(struct tg3 *tp)
1141{
1142 u32 phy_control;
1143 int limit, err;
1144
1145 /* OK, reset it, and poll the BMCR_RESET bit until it
1146 * clears or we time out.
1147 */
1148 phy_control = BMCR_RESET;
1149 err = tg3_writephy(tp, MII_BMCR, phy_control);
1150 if (err != 0)
1151 return -EBUSY;
1152
1153 limit = 5000;
1154 while (limit--) {
1155 err = tg3_readphy(tp, MII_BMCR, &phy_control);
1156 if (err != 0)
1157 return -EBUSY;
1158
1159 if ((phy_control & BMCR_RESET) == 0) {
1160 udelay(40);
1161 break;
1162 }
1163 udelay(10);
1164 }
Roel Kluind4675b52009-02-12 16:33:27 -08001165 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -07001166 return -EBUSY;
1167
1168 return 0;
1169}
1170
Matt Carlson158d7ab2008-05-29 01:37:54 -07001171static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
1172{
Francois Romieu3d165432009-01-19 16:56:50 -08001173 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001174 u32 val;
1175
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001176 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001177
1178 if (tg3_readphy(tp, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001179 val = -EIO;
1180
1181 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001182
1183 return val;
1184}
1185
1186static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
1187{
Francois Romieu3d165432009-01-19 16:56:50 -08001188 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001189 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001190
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001191 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001192
1193 if (tg3_writephy(tp, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001194 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001195
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001196 spin_unlock_bh(&tp->lock);
1197
1198 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001199}
1200
1201static int tg3_mdio_reset(struct mii_bus *bp)
1202{
1203 return 0;
1204}
1205
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001206static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -07001207{
1208 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001209 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -07001210
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001211 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001212 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001213 case PHY_ID_BCM50610:
1214 case PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001215 val = MAC_PHYCFG2_50610_LED_MODES;
1216 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001217 case PHY_ID_BCMAC131:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001218 val = MAC_PHYCFG2_AC131_LED_MODES;
1219 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001220 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001221 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
1222 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001223 case PHY_ID_RTL8201E:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001224 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
1225 break;
1226 default:
Matt Carlsona9daf362008-05-25 23:49:44 -07001227 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001228 }
1229
1230 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
1231 tw32(MAC_PHYCFG2, val);
1232
1233 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001234 val &= ~(MAC_PHYCFG1_RGMII_INT |
1235 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
1236 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001237 tw32(MAC_PHYCFG1, val);
1238
1239 return;
1240 }
1241
Joe Perches63c3a662011-04-26 08:12:10 +00001242 if (!tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001243 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
1244 MAC_PHYCFG2_FMODE_MASK_MASK |
1245 MAC_PHYCFG2_GMODE_MASK_MASK |
1246 MAC_PHYCFG2_ACT_MASK_MASK |
1247 MAC_PHYCFG2_QUAL_MASK_MASK |
1248 MAC_PHYCFG2_INBAND_ENABLE;
1249
1250 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001251
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001252 val = tr32(MAC_PHYCFG1);
1253 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
1254 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
Joe Perches63c3a662011-04-26 08:12:10 +00001255 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1256 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001257 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
Joe Perches63c3a662011-04-26 08:12:10 +00001258 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001259 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1260 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001261 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1262 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1263 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001264
Matt Carlsona9daf362008-05-25 23:49:44 -07001265 val = tr32(MAC_EXT_RGMII_MODE);
1266 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1267 MAC_RGMII_MODE_RX_QUALITY |
1268 MAC_RGMII_MODE_RX_ACTIVITY |
1269 MAC_RGMII_MODE_RX_ENG_DET |
1270 MAC_RGMII_MODE_TX_ENABLE |
1271 MAC_RGMII_MODE_TX_LOWPWR |
1272 MAC_RGMII_MODE_TX_RESET);
Joe Perches63c3a662011-04-26 08:12:10 +00001273 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1274 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001275 val |= MAC_RGMII_MODE_RX_INT_B |
1276 MAC_RGMII_MODE_RX_QUALITY |
1277 MAC_RGMII_MODE_RX_ACTIVITY |
1278 MAC_RGMII_MODE_RX_ENG_DET;
Joe Perches63c3a662011-04-26 08:12:10 +00001279 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001280 val |= MAC_RGMII_MODE_TX_ENABLE |
1281 MAC_RGMII_MODE_TX_LOWPWR |
1282 MAC_RGMII_MODE_TX_RESET;
1283 }
1284 tw32(MAC_EXT_RGMII_MODE, val);
1285}
1286
Matt Carlson158d7ab2008-05-29 01:37:54 -07001287static void tg3_mdio_start(struct tg3 *tp)
1288{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001289 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1290 tw32_f(MAC_MI_MODE, tp->mi_mode);
1291 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001292
Joe Perches63c3a662011-04-26 08:12:10 +00001293 if (tg3_flag(tp, MDIOBUS_INITED) &&
Matt Carlson9ea48182010-02-17 15:17:01 +00001294 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1295 tg3_mdio_config_5785(tp);
1296}
1297
1298static int tg3_mdio_init(struct tg3 *tp)
1299{
1300 int i;
1301 u32 reg;
1302 struct phy_device *phydev;
1303
Joe Perches63c3a662011-04-26 08:12:10 +00001304 if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson9c7df912010-06-05 17:24:36 +00001305 u32 is_serdes;
Matt Carlson882e9792009-09-01 13:21:36 +00001306
Matt Carlson69f11c92011-07-13 09:27:30 +00001307 tp->phy_addr = tp->pci_fn + 1;
Matt Carlson882e9792009-09-01 13:21:36 +00001308
Matt Carlsond1ec96a2010-01-12 10:11:38 +00001309 if (tp->pci_chip_rev_id != CHIPREV_ID_5717_A0)
1310 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1311 else
1312 is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1313 TG3_CPMU_PHY_STRAP_IS_SERDES;
Matt Carlson882e9792009-09-01 13:21:36 +00001314 if (is_serdes)
1315 tp->phy_addr += 7;
1316 } else
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001317 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001318
Matt Carlson158d7ab2008-05-29 01:37:54 -07001319 tg3_mdio_start(tp);
1320
Joe Perches63c3a662011-04-26 08:12:10 +00001321 if (!tg3_flag(tp, USE_PHYLIB) || tg3_flag(tp, MDIOBUS_INITED))
Matt Carlson158d7ab2008-05-29 01:37:54 -07001322 return 0;
1323
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001324 tp->mdio_bus = mdiobus_alloc();
1325 if (tp->mdio_bus == NULL)
1326 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001327
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001328 tp->mdio_bus->name = "tg3 mdio bus";
1329 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001330 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001331 tp->mdio_bus->priv = tp;
1332 tp->mdio_bus->parent = &tp->pdev->dev;
1333 tp->mdio_bus->read = &tg3_mdio_read;
1334 tp->mdio_bus->write = &tg3_mdio_write;
1335 tp->mdio_bus->reset = &tg3_mdio_reset;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001336 tp->mdio_bus->phy_mask = ~(1 << TG3_PHY_MII_ADDR);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001337 tp->mdio_bus->irq = &tp->mdio_irq[0];
Matt Carlson158d7ab2008-05-29 01:37:54 -07001338
1339 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001340 tp->mdio_bus->irq[i] = PHY_POLL;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001341
1342 /* The bus registration will look for all the PHYs on the mdio bus.
1343 * Unfortunately, it does not ensure the PHY is powered up before
1344 * accessing the PHY ID registers. A chip reset is the
1345 * quickest way to bring the device back to an operational state..
1346 */
1347 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1348 tg3_bmcr_reset(tp);
1349
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001350 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001351 if (i) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001352 dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001353 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001354 return i;
1355 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001356
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001357 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsona9daf362008-05-25 23:49:44 -07001358
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001359 if (!phydev || !phydev->drv) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001360 dev_warn(&tp->pdev->dev, "No PHY devices\n");
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001361 mdiobus_unregister(tp->mdio_bus);
1362 mdiobus_free(tp->mdio_bus);
1363 return -ENODEV;
1364 }
1365
1366 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001367 case PHY_ID_BCM57780:
Matt Carlson321d32a2008-11-21 17:22:19 -08001368 phydev->interface = PHY_INTERFACE_MODE_GMII;
Matt Carlsonc704dc22009-11-02 14:32:12 +00001369 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08001370 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001371 case PHY_ID_BCM50610:
1372 case PHY_ID_BCM50610M:
Matt Carlson32e5a8d2009-11-02 14:31:39 +00001373 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001374 PHY_BRCM_RX_REFCLK_UNUSED |
Matt Carlson52fae082009-11-02 14:32:38 +00001375 PHY_BRCM_DIS_TXCRXC_NOENRGY |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001376 PHY_BRCM_AUTO_PWRDWN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001377 if (tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsona9daf362008-05-25 23:49:44 -07001378 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001379 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001380 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001381 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001382 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001383 /* fallthru */
Matt Carlson6a443a02010-02-17 15:17:04 +00001384 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001385 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001386 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001387 case PHY_ID_RTL8201E:
1388 case PHY_ID_BCMAC131:
Matt Carlsona9daf362008-05-25 23:49:44 -07001389 phydev->interface = PHY_INTERFACE_MODE_MII;
Matt Carlsoncdd4e092009-11-02 14:31:11 +00001390 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001391 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlsona9daf362008-05-25 23:49:44 -07001392 break;
1393 }
1394
Joe Perches63c3a662011-04-26 08:12:10 +00001395 tg3_flag_set(tp, MDIOBUS_INITED);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001396
1397 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1398 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001399
1400 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001401}
1402
1403static void tg3_mdio_fini(struct tg3 *tp)
1404{
Joe Perches63c3a662011-04-26 08:12:10 +00001405 if (tg3_flag(tp, MDIOBUS_INITED)) {
1406 tg3_flag_clear(tp, MDIOBUS_INITED);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001407 mdiobus_unregister(tp->mdio_bus);
1408 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001409 }
1410}
1411
Matt Carlson95e28692008-05-25 23:44:14 -07001412/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001413static inline void tg3_generate_fw_event(struct tg3 *tp)
1414{
1415 u32 val;
1416
1417 val = tr32(GRC_RX_CPU_EVENT);
1418 val |= GRC_RX_CPU_DRIVER_EVENT;
1419 tw32_f(GRC_RX_CPU_EVENT, val);
1420
1421 tp->last_event_jiffies = jiffies;
1422}
1423
1424#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1425
1426/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001427static void tg3_wait_for_event_ack(struct tg3 *tp)
1428{
1429 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001430 unsigned int delay_cnt;
1431 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001432
Matt Carlson4ba526c2008-08-15 14:10:04 -07001433 /* If enough time has passed, no wait is necessary. */
1434 time_remain = (long)(tp->last_event_jiffies + 1 +
1435 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1436 (long)jiffies;
1437 if (time_remain < 0)
1438 return;
1439
1440 /* Check if we can shorten the wait time. */
1441 delay_cnt = jiffies_to_usecs(time_remain);
1442 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1443 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1444 delay_cnt = (delay_cnt >> 3) + 1;
1445
1446 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001447 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1448 break;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001449 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001450 }
1451}
1452
1453/* tp->lock is held. */
1454static void tg3_ump_link_report(struct tg3 *tp)
1455{
1456 u32 reg;
1457 u32 val;
1458
Joe Perches63c3a662011-04-26 08:12:10 +00001459 if (!tg3_flag(tp, 5780_CLASS) || !tg3_flag(tp, ENABLE_ASF))
Matt Carlson95e28692008-05-25 23:44:14 -07001460 return;
1461
1462 tg3_wait_for_event_ack(tp);
1463
1464 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1465
1466 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1467
1468 val = 0;
1469 if (!tg3_readphy(tp, MII_BMCR, &reg))
1470 val = reg << 16;
1471 if (!tg3_readphy(tp, MII_BMSR, &reg))
1472 val |= (reg & 0xffff);
1473 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX, val);
1474
1475 val = 0;
1476 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1477 val = reg << 16;
1478 if (!tg3_readphy(tp, MII_LPA, &reg))
1479 val |= (reg & 0xffff);
1480 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 4, val);
1481
1482 val = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001483 if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
Matt Carlson95e28692008-05-25 23:44:14 -07001484 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1485 val = reg << 16;
1486 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1487 val |= (reg & 0xffff);
1488 }
1489 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 8, val);
1490
1491 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1492 val = reg << 16;
1493 else
1494 val = 0;
1495 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 12, val);
1496
Matt Carlson4ba526c2008-08-15 14:10:04 -07001497 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001498}
1499
Matt Carlson8d5a89b2011-08-31 11:44:51 +00001500/* tp->lock is held. */
1501static void tg3_stop_fw(struct tg3 *tp)
1502{
1503 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
1504 /* Wait for RX cpu to ACK the previous event. */
1505 tg3_wait_for_event_ack(tp);
1506
1507 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
1508
1509 tg3_generate_fw_event(tp);
1510
1511 /* Wait for RX cpu to ACK this event. */
1512 tg3_wait_for_event_ack(tp);
1513 }
1514}
1515
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001516/* tp->lock is held. */
1517static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
1518{
1519 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
1520 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
1521
1522 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1523 switch (kind) {
1524 case RESET_KIND_INIT:
1525 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1526 DRV_STATE_START);
1527 break;
1528
1529 case RESET_KIND_SHUTDOWN:
1530 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1531 DRV_STATE_UNLOAD);
1532 break;
1533
1534 case RESET_KIND_SUSPEND:
1535 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1536 DRV_STATE_SUSPEND);
1537 break;
1538
1539 default:
1540 break;
1541 }
1542 }
1543
1544 if (kind == RESET_KIND_INIT ||
1545 kind == RESET_KIND_SUSPEND)
1546 tg3_ape_driver_state_change(tp, kind);
1547}
1548
1549/* tp->lock is held. */
1550static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
1551{
1552 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1553 switch (kind) {
1554 case RESET_KIND_INIT:
1555 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1556 DRV_STATE_START_DONE);
1557 break;
1558
1559 case RESET_KIND_SHUTDOWN:
1560 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1561 DRV_STATE_UNLOAD_DONE);
1562 break;
1563
1564 default:
1565 break;
1566 }
1567 }
1568
1569 if (kind == RESET_KIND_SHUTDOWN)
1570 tg3_ape_driver_state_change(tp, kind);
1571}
1572
1573/* tp->lock is held. */
1574static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
1575{
1576 if (tg3_flag(tp, ENABLE_ASF)) {
1577 switch (kind) {
1578 case RESET_KIND_INIT:
1579 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1580 DRV_STATE_START);
1581 break;
1582
1583 case RESET_KIND_SHUTDOWN:
1584 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1585 DRV_STATE_UNLOAD);
1586 break;
1587
1588 case RESET_KIND_SUSPEND:
1589 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1590 DRV_STATE_SUSPEND);
1591 break;
1592
1593 default:
1594 break;
1595 }
1596 }
1597}
1598
1599static int tg3_poll_fw(struct tg3 *tp)
1600{
1601 int i;
1602 u32 val;
1603
1604 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
1605 /* Wait up to 20ms for init done. */
1606 for (i = 0; i < 200; i++) {
1607 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
1608 return 0;
1609 udelay(100);
1610 }
1611 return -ENODEV;
1612 }
1613
1614 /* Wait for firmware initialization to complete. */
1615 for (i = 0; i < 100000; i++) {
1616 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
1617 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
1618 break;
1619 udelay(10);
1620 }
1621
1622 /* Chip might not be fitted with firmware. Some Sun onboard
1623 * parts are configured like that. So don't signal the timeout
1624 * of the above loop as an error, but do report the lack of
1625 * running firmware once.
1626 */
1627 if (i >= 100000 && !tg3_flag(tp, NO_FWARE_REPORTED)) {
1628 tg3_flag_set(tp, NO_FWARE_REPORTED);
1629
1630 netdev_info(tp->dev, "No firmware running\n");
1631 }
1632
1633 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
1634 /* The 57765 A0 needs a little more
1635 * time to do some important work.
1636 */
1637 mdelay(10);
1638 }
1639
1640 return 0;
1641}
1642
Matt Carlson95e28692008-05-25 23:44:14 -07001643static void tg3_link_report(struct tg3 *tp)
1644{
1645 if (!netif_carrier_ok(tp->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001646 netif_info(tp, link, tp->dev, "Link is down\n");
Matt Carlson95e28692008-05-25 23:44:14 -07001647 tg3_ump_link_report(tp);
1648 } else if (netif_msg_link(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001649 netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
1650 (tp->link_config.active_speed == SPEED_1000 ?
1651 1000 :
1652 (tp->link_config.active_speed == SPEED_100 ?
1653 100 : 10)),
1654 (tp->link_config.active_duplex == DUPLEX_FULL ?
1655 "full" : "half"));
Matt Carlson95e28692008-05-25 23:44:14 -07001656
Joe Perches05dbe002010-02-17 19:44:19 +00001657 netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
1658 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
1659 "on" : "off",
1660 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
1661 "on" : "off");
Matt Carlson47007832011-04-20 07:57:43 +00001662
1663 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
1664 netdev_info(tp->dev, "EEE is %s\n",
1665 tp->setlpicnt ? "enabled" : "disabled");
1666
Matt Carlson95e28692008-05-25 23:44:14 -07001667 tg3_ump_link_report(tp);
1668 }
1669}
1670
1671static u16 tg3_advert_flowctrl_1000T(u8 flow_ctrl)
1672{
1673 u16 miireg;
1674
Steve Glendinninge18ce342008-12-16 02:00:00 -08001675 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001676 miireg = ADVERTISE_PAUSE_CAP;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001677 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001678 miireg = ADVERTISE_PAUSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001679 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001680 miireg = ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1681 else
1682 miireg = 0;
1683
1684 return miireg;
1685}
1686
1687static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1688{
1689 u16 miireg;
1690
Steve Glendinninge18ce342008-12-16 02:00:00 -08001691 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001692 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001693 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001694 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001695 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001696 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1697 else
1698 miireg = 0;
1699
1700 return miireg;
1701}
1702
Matt Carlson95e28692008-05-25 23:44:14 -07001703static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1704{
1705 u8 cap = 0;
1706
1707 if (lcladv & ADVERTISE_1000XPAUSE) {
1708 if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1709 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001710 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001711 else if (rmtadv & LPA_1000XPAUSE_ASYM)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001712 cap = FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001713 } else {
1714 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001715 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001716 }
1717 } else if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1718 if ((rmtadv & LPA_1000XPAUSE) && (rmtadv & LPA_1000XPAUSE_ASYM))
Steve Glendinninge18ce342008-12-16 02:00:00 -08001719 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001720 }
1721
1722 return cap;
1723}
1724
Matt Carlsonf51f3562008-05-25 23:45:08 -07001725static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001726{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001727 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001728 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001729 u32 old_rx_mode = tp->rx_mode;
1730 u32 old_tx_mode = tp->tx_mode;
1731
Joe Perches63c3a662011-04-26 08:12:10 +00001732 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001733 autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001734 else
1735 autoneg = tp->link_config.autoneg;
1736
Joe Perches63c3a662011-04-26 08:12:10 +00001737 if (autoneg == AUTONEG_ENABLE && tg3_flag(tp, PAUSE_AUTONEG)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001738 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001739 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001740 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001741 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001742 } else
1743 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001744
Matt Carlsonf51f3562008-05-25 23:45:08 -07001745 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001746
Steve Glendinninge18ce342008-12-16 02:00:00 -08001747 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001748 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1749 else
1750 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1751
Matt Carlsonf51f3562008-05-25 23:45:08 -07001752 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001753 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001754
Steve Glendinninge18ce342008-12-16 02:00:00 -08001755 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001756 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1757 else
1758 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1759
Matt Carlsonf51f3562008-05-25 23:45:08 -07001760 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001761 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001762}
1763
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001764static void tg3_adjust_link(struct net_device *dev)
1765{
1766 u8 oldflowctrl, linkmesg = 0;
1767 u32 mac_mode, lcl_adv, rmt_adv;
1768 struct tg3 *tp = netdev_priv(dev);
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001769 struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001770
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001771 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001772
1773 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
1774 MAC_MODE_HALF_DUPLEX);
1775
1776 oldflowctrl = tp->link_config.active_flowctrl;
1777
1778 if (phydev->link) {
1779 lcl_adv = 0;
1780 rmt_adv = 0;
1781
1782 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
1783 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001784 else if (phydev->speed == SPEED_1000 ||
1785 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001786 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001787 else
1788 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001789
1790 if (phydev->duplex == DUPLEX_HALF)
1791 mac_mode |= MAC_MODE_HALF_DUPLEX;
1792 else {
1793 lcl_adv = tg3_advert_flowctrl_1000T(
1794 tp->link_config.flowctrl);
1795
1796 if (phydev->pause)
1797 rmt_adv = LPA_PAUSE_CAP;
1798 if (phydev->asym_pause)
1799 rmt_adv |= LPA_PAUSE_ASYM;
1800 }
1801
1802 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
1803 } else
1804 mac_mode |= MAC_MODE_PORT_MODE_GMII;
1805
1806 if (mac_mode != tp->mac_mode) {
1807 tp->mac_mode = mac_mode;
1808 tw32_f(MAC_MODE, tp->mac_mode);
1809 udelay(40);
1810 }
1811
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001812 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
1813 if (phydev->speed == SPEED_10)
1814 tw32(MAC_MI_STAT,
1815 MAC_MI_STAT_10MBPS_MODE |
1816 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1817 else
1818 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1819 }
1820
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001821 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
1822 tw32(MAC_TX_LENGTHS,
1823 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1824 (6 << TX_LENGTHS_IPG_SHIFT) |
1825 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
1826 else
1827 tw32(MAC_TX_LENGTHS,
1828 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1829 (6 << TX_LENGTHS_IPG_SHIFT) |
1830 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
1831
1832 if ((phydev->link && tp->link_config.active_speed == SPEED_INVALID) ||
1833 (!phydev->link && tp->link_config.active_speed != SPEED_INVALID) ||
1834 phydev->speed != tp->link_config.active_speed ||
1835 phydev->duplex != tp->link_config.active_duplex ||
1836 oldflowctrl != tp->link_config.active_flowctrl)
Matt Carlsonc6cdf432010-04-05 10:19:26 +00001837 linkmesg = 1;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001838
1839 tp->link_config.active_speed = phydev->speed;
1840 tp->link_config.active_duplex = phydev->duplex;
1841
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001842 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001843
1844 if (linkmesg)
1845 tg3_link_report(tp);
1846}
1847
1848static int tg3_phy_init(struct tg3 *tp)
1849{
1850 struct phy_device *phydev;
1851
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001852 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001853 return 0;
1854
1855 /* Bring the PHY back to a known state. */
1856 tg3_bmcr_reset(tp);
1857
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001858 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001859
1860 /* Attach the MAC to the PHY. */
Kay Sieversfb28ad32008-11-10 13:55:14 -08001861 phydev = phy_connect(tp->dev, dev_name(&phydev->dev), tg3_adjust_link,
Matt Carlsona9daf362008-05-25 23:49:44 -07001862 phydev->dev_flags, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001863 if (IS_ERR(phydev)) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001864 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001865 return PTR_ERR(phydev);
1866 }
1867
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001868 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001869 switch (phydev->interface) {
1870 case PHY_INTERFACE_MODE_GMII:
1871 case PHY_INTERFACE_MODE_RGMII:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001872 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Matt Carlson321d32a2008-11-21 17:22:19 -08001873 phydev->supported &= (PHY_GBIT_FEATURES |
1874 SUPPORTED_Pause |
1875 SUPPORTED_Asym_Pause);
1876 break;
1877 }
1878 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001879 case PHY_INTERFACE_MODE_MII:
1880 phydev->supported &= (PHY_BASIC_FEATURES |
1881 SUPPORTED_Pause |
1882 SUPPORTED_Asym_Pause);
1883 break;
1884 default:
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001885 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001886 return -EINVAL;
1887 }
1888
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001889 tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001890
1891 phydev->advertising = phydev->supported;
1892
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001893 return 0;
1894}
1895
1896static void tg3_phy_start(struct tg3 *tp)
1897{
1898 struct phy_device *phydev;
1899
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001900 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001901 return;
1902
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001903 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001904
Matt Carlson80096062010-08-02 11:26:06 +00001905 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
1906 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001907 phydev->speed = tp->link_config.orig_speed;
1908 phydev->duplex = tp->link_config.orig_duplex;
1909 phydev->autoneg = tp->link_config.orig_autoneg;
1910 phydev->advertising = tp->link_config.orig_advertising;
1911 }
1912
1913 phy_start(phydev);
1914
1915 phy_start_aneg(phydev);
1916}
1917
1918static void tg3_phy_stop(struct tg3 *tp)
1919{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001920 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001921 return;
1922
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001923 phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001924}
1925
1926static void tg3_phy_fini(struct tg3 *tp)
1927{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001928 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001929 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001930 tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001931 }
1932}
1933
Matt Carlson941ec902011-08-19 13:58:23 +00001934static int tg3_phy_set_extloopbk(struct tg3 *tp)
1935{
1936 int err;
1937 u32 val;
1938
1939 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
1940 return 0;
1941
1942 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
1943 /* Cannot do read-modify-write on 5401 */
1944 err = tg3_phy_auxctl_write(tp,
1945 MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
1946 MII_TG3_AUXCTL_ACTL_EXTLOOPBK |
1947 0x4c20);
1948 goto done;
1949 }
1950
1951 err = tg3_phy_auxctl_read(tp,
1952 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
1953 if (err)
1954 return err;
1955
1956 val |= MII_TG3_AUXCTL_ACTL_EXTLOOPBK;
1957 err = tg3_phy_auxctl_write(tp,
1958 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, val);
1959
1960done:
1961 return err;
1962}
1963
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001964static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
1965{
1966 u32 phytest;
1967
1968 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
1969 u32 phy;
1970
1971 tg3_writephy(tp, MII_TG3_FET_TEST,
1972 phytest | MII_TG3_FET_SHADOW_EN);
1973 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
1974 if (enable)
1975 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
1976 else
1977 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
1978 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
1979 }
1980 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
1981 }
1982}
1983
Matt Carlson6833c042008-11-21 17:18:59 -08001984static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
1985{
1986 u32 reg;
1987
Joe Perches63c3a662011-04-26 08:12:10 +00001988 if (!tg3_flag(tp, 5705_PLUS) ||
1989 (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001990 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08001991 return;
1992
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001993 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001994 tg3_phy_fet_toggle_apd(tp, enable);
1995 return;
1996 }
1997
Matt Carlson6833c042008-11-21 17:18:59 -08001998 reg = MII_TG3_MISC_SHDW_WREN |
1999 MII_TG3_MISC_SHDW_SCR5_SEL |
2000 MII_TG3_MISC_SHDW_SCR5_LPED |
2001 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
2002 MII_TG3_MISC_SHDW_SCR5_SDTL |
2003 MII_TG3_MISC_SHDW_SCR5_C125OE;
2004 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 || !enable)
2005 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
2006
2007 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
2008
2009
2010 reg = MII_TG3_MISC_SHDW_WREN |
2011 MII_TG3_MISC_SHDW_APD_SEL |
2012 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
2013 if (enable)
2014 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
2015
2016 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
2017}
2018
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002019static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
2020{
2021 u32 phy;
2022
Joe Perches63c3a662011-04-26 08:12:10 +00002023 if (!tg3_flag(tp, 5705_PLUS) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002024 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002025 return;
2026
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002027 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002028 u32 ephy;
2029
Matt Carlson535ef6e2009-08-25 10:09:36 +00002030 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
2031 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
2032
2033 tg3_writephy(tp, MII_TG3_FET_TEST,
2034 ephy | MII_TG3_FET_SHADOW_EN);
2035 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002036 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00002037 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002038 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00002039 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
2040 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002041 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00002042 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002043 }
2044 } else {
Matt Carlson15ee95c2011-04-20 07:57:40 +00002045 int ret;
2046
2047 ret = tg3_phy_auxctl_read(tp,
2048 MII_TG3_AUXCTL_SHDWSEL_MISC, &phy);
2049 if (!ret) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002050 if (enable)
2051 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
2052 else
2053 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002054 tg3_phy_auxctl_write(tp,
2055 MII_TG3_AUXCTL_SHDWSEL_MISC, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002056 }
2057 }
2058}
2059
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060static void tg3_phy_set_wirespeed(struct tg3 *tp)
2061{
Matt Carlson15ee95c2011-04-20 07:57:40 +00002062 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 u32 val;
2064
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002065 if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 return;
2067
Matt Carlson15ee95c2011-04-20 07:57:40 +00002068 ret = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_MISC, &val);
2069 if (!ret)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002070 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_MISC,
2071 val | MII_TG3_AUXCTL_MISC_WIRESPD_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072}
2073
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002074static void tg3_phy_apply_otp(struct tg3 *tp)
2075{
2076 u32 otp, phy;
2077
2078 if (!tp->phy_otp)
2079 return;
2080
2081 otp = tp->phy_otp;
2082
Matt Carlson1d36ba42011-04-20 07:57:42 +00002083 if (TG3_PHY_AUXCTL_SMDSP_ENABLE(tp))
2084 return;
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002085
2086 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
2087 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
2088 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
2089
2090 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
2091 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
2092 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
2093
2094 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
2095 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
2096 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
2097
2098 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
2099 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
2100
2101 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
2102 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
2103
2104 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
2105 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
2106 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
2107
Matt Carlson1d36ba42011-04-20 07:57:42 +00002108 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002109}
2110
Matt Carlson52b02d02010-10-14 10:37:41 +00002111static void tg3_phy_eee_adjust(struct tg3 *tp, u32 current_link_up)
2112{
2113 u32 val;
2114
2115 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
2116 return;
2117
2118 tp->setlpicnt = 0;
2119
2120 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
2121 current_link_up == 1 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +00002122 tp->link_config.active_duplex == DUPLEX_FULL &&
2123 (tp->link_config.active_speed == SPEED_100 ||
2124 tp->link_config.active_speed == SPEED_1000)) {
Matt Carlson52b02d02010-10-14 10:37:41 +00002125 u32 eeectl;
2126
2127 if (tp->link_config.active_speed == SPEED_1000)
2128 eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
2129 else
2130 eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
2131
2132 tw32(TG3_CPMU_EEE_CTRL, eeectl);
2133
Matt Carlson3110f5f52010-12-06 08:28:50 +00002134 tg3_phy_cl45_read(tp, MDIO_MMD_AN,
2135 TG3_CL45_D7_EEERES_STAT, &val);
Matt Carlson52b02d02010-10-14 10:37:41 +00002136
Matt Carlsonb0c59432011-05-19 12:12:48 +00002137 if (val == TG3_CL45_D7_EEERES_STAT_LP_1000T ||
2138 val == TG3_CL45_D7_EEERES_STAT_LP_100TX)
Matt Carlson52b02d02010-10-14 10:37:41 +00002139 tp->setlpicnt = 2;
2140 }
2141
2142 if (!tp->setlpicnt) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002143 if (current_link_up == 1 &&
2144 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2145 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0000);
2146 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2147 }
2148
Matt Carlson52b02d02010-10-14 10:37:41 +00002149 val = tr32(TG3_CPMU_EEE_MODE);
2150 tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
2151 }
2152}
2153
Matt Carlsonb0c59432011-05-19 12:12:48 +00002154static void tg3_phy_eee_enable(struct tg3 *tp)
2155{
2156 u32 val;
2157
2158 if (tp->link_config.active_speed == SPEED_1000 &&
2159 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2160 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
2161 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) &&
2162 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002163 val = MII_TG3_DSP_TAP26_ALNOKO |
2164 MII_TG3_DSP_TAP26_RMRXSTO;
2165 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
Matt Carlsonb0c59432011-05-19 12:12:48 +00002166 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2167 }
2168
2169 val = tr32(TG3_CPMU_EEE_MODE);
2170 tw32(TG3_CPMU_EEE_MODE, val | TG3_CPMU_EEEMD_LPI_ENABLE);
2171}
2172
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173static int tg3_wait_macro_done(struct tg3 *tp)
2174{
2175 int limit = 100;
2176
2177 while (limit--) {
2178 u32 tmp32;
2179
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002180 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 if ((tmp32 & 0x1000) == 0)
2182 break;
2183 }
2184 }
Roel Kluind4675b52009-02-12 16:33:27 -08002185 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 return -EBUSY;
2187
2188 return 0;
2189}
2190
2191static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
2192{
2193 static const u32 test_pat[4][6] = {
2194 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
2195 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
2196 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
2197 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
2198 };
2199 int chan;
2200
2201 for (chan = 0; chan < 4; chan++) {
2202 int i;
2203
2204 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2205 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002206 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
2208 for (i = 0; i < 6; i++)
2209 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
2210 test_pat[chan][i]);
2211
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002212 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 if (tg3_wait_macro_done(tp)) {
2214 *resetp = 1;
2215 return -EBUSY;
2216 }
2217
2218 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2219 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002220 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 if (tg3_wait_macro_done(tp)) {
2222 *resetp = 1;
2223 return -EBUSY;
2224 }
2225
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002226 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 if (tg3_wait_macro_done(tp)) {
2228 *resetp = 1;
2229 return -EBUSY;
2230 }
2231
2232 for (i = 0; i < 6; i += 2) {
2233 u32 low, high;
2234
2235 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
2236 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
2237 tg3_wait_macro_done(tp)) {
2238 *resetp = 1;
2239 return -EBUSY;
2240 }
2241 low &= 0x7fff;
2242 high &= 0x000f;
2243 if (low != test_pat[chan][i] ||
2244 high != test_pat[chan][i+1]) {
2245 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
2246 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
2247 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
2248
2249 return -EBUSY;
2250 }
2251 }
2252 }
2253
2254 return 0;
2255}
2256
2257static int tg3_phy_reset_chanpat(struct tg3 *tp)
2258{
2259 int chan;
2260
2261 for (chan = 0; chan < 4; chan++) {
2262 int i;
2263
2264 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2265 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002266 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 for (i = 0; i < 6; i++)
2268 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002269 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 if (tg3_wait_macro_done(tp))
2271 return -EBUSY;
2272 }
2273
2274 return 0;
2275}
2276
2277static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
2278{
2279 u32 reg32, phy9_orig;
2280 int retries, do_phy_reset, err;
2281
2282 retries = 10;
2283 do_phy_reset = 1;
2284 do {
2285 if (do_phy_reset) {
2286 err = tg3_bmcr_reset(tp);
2287 if (err)
2288 return err;
2289 do_phy_reset = 0;
2290 }
2291
2292 /* Disable transmitter and interrupt. */
2293 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
2294 continue;
2295
2296 reg32 |= 0x3000;
2297 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2298
2299 /* Set full-duplex, 1000 mbps. */
2300 tg3_writephy(tp, MII_BMCR,
Matt Carlson221c5632011-06-13 13:39:01 +00002301 BMCR_FULLDPLX | BMCR_SPEED1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
2303 /* Set to master mode. */
Matt Carlson221c5632011-06-13 13:39:01 +00002304 if (tg3_readphy(tp, MII_CTRL1000, &phy9_orig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 continue;
2306
Matt Carlson221c5632011-06-13 13:39:01 +00002307 tg3_writephy(tp, MII_CTRL1000,
2308 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
Matt Carlson1d36ba42011-04-20 07:57:42 +00002310 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
2311 if (err)
2312 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
2314 /* Block the PHY control access. */
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002315 tg3_phydsp_write(tp, 0x8005, 0x0800);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
2317 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
2318 if (!err)
2319 break;
2320 } while (--retries);
2321
2322 err = tg3_phy_reset_chanpat(tp);
2323 if (err)
2324 return err;
2325
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002326 tg3_phydsp_write(tp, 0x8005, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
2328 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002329 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330
Matt Carlson1d36ba42011-04-20 07:57:42 +00002331 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332
Matt Carlson221c5632011-06-13 13:39:01 +00002333 tg3_writephy(tp, MII_CTRL1000, phy9_orig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
2335 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
2336 reg32 &= ~0x3000;
2337 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2338 } else if (!err)
2339 err = -EBUSY;
2340
2341 return err;
2342}
2343
2344/* This will reset the tigon3 PHY if there is no valid
2345 * link unless the FORCE argument is non-zero.
2346 */
2347static int tg3_phy_reset(struct tg3 *tp)
2348{
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002349 u32 val, cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 int err;
2351
Michael Chan60189dd2006-12-17 17:08:07 -08002352 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002353 val = tr32(GRC_MISC_CFG);
2354 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
2355 udelay(40);
2356 }
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002357 err = tg3_readphy(tp, MII_BMSR, &val);
2358 err |= tg3_readphy(tp, MII_BMSR, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 if (err != 0)
2360 return -EBUSY;
2361
Michael Chanc8e1e822006-04-29 18:55:17 -07002362 if (netif_running(tp->dev) && netif_carrier_ok(tp->dev)) {
2363 netif_carrier_off(tp->dev);
2364 tg3_link_report(tp);
2365 }
2366
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2368 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2369 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
2370 err = tg3_phy_reset_5703_4_5(tp);
2371 if (err)
2372 return err;
2373 goto out;
2374 }
2375
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002376 cpmuctrl = 0;
2377 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
2378 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
2379 cpmuctrl = tr32(TG3_CPMU_CTRL);
2380 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
2381 tw32(TG3_CPMU_CTRL,
2382 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
2383 }
2384
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 err = tg3_bmcr_reset(tp);
2386 if (err)
2387 return err;
2388
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002389 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002390 val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
2391 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002392
2393 tw32(TG3_CPMU_CTRL, cpmuctrl);
2394 }
2395
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002396 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2397 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002398 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2399 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
2400 CPMU_LSPD_1000MB_MACCLK_12_5) {
2401 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2402 udelay(40);
2403 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2404 }
2405 }
2406
Joe Perches63c3a662011-04-26 08:12:10 +00002407 if (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002408 (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
Matt Carlsonecf14102010-01-20 16:58:05 +00002409 return 0;
2410
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002411 tg3_phy_apply_otp(tp);
2412
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002413 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -08002414 tg3_phy_toggle_apd(tp, true);
2415 else
2416 tg3_phy_toggle_apd(tp, false);
2417
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418out:
Matt Carlson1d36ba42011-04-20 07:57:42 +00002419 if ((tp->phy_flags & TG3_PHYFLG_ADC_BUG) &&
2420 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002421 tg3_phydsp_write(tp, 0x201f, 0x2aaa);
2422 tg3_phydsp_write(tp, 0x000a, 0x0323);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002423 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002425
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002426 if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002427 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2428 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002430
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002431 if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002432 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2433 tg3_phydsp_write(tp, 0x000a, 0x310b);
2434 tg3_phydsp_write(tp, 0x201f, 0x9506);
2435 tg3_phydsp_write(tp, 0x401f, 0x14e2);
2436 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2437 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002438 } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002439 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2440 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
2441 if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
2442 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2443 tg3_writephy(tp, MII_TG3_TEST1,
2444 MII_TG3_TEST1_TRIM_EN | 0x4);
2445 } else
2446 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
2447
2448 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2449 }
Michael Chanc424cb22006-04-29 18:56:34 -07002450 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002451
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 /* Set Extended packet length bit (bit 14) on all chips that */
2453 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002454 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 /* Cannot do read-modify-write on 5401 */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002456 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Joe Perches63c3a662011-04-26 08:12:10 +00002457 } else if (tg3_flag(tp, JUMBO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 /* Set bit 14 with read-modify-write to preserve other bits */
Matt Carlson15ee95c2011-04-20 07:57:40 +00002459 err = tg3_phy_auxctl_read(tp,
2460 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2461 if (!err)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002462 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2463 val | MII_TG3_AUXCTL_ACTL_EXTPKTLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 }
2465
2466 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2467 * jumbo frames transmission.
2468 */
Joe Perches63c3a662011-04-26 08:12:10 +00002469 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002470 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002471 tg3_writephy(tp, MII_TG3_EXT_CTRL,
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002472 val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 }
2474
Michael Chan715116a2006-09-27 16:09:25 -07002475 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002476 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002477 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002478 }
2479
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002480 tg3_phy_toggle_automdix(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 tg3_phy_set_wirespeed(tp);
2482 return 0;
2483}
2484
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002485#define TG3_GPIO_MSG_DRVR_PRES 0x00000001
2486#define TG3_GPIO_MSG_NEED_VAUX 0x00000002
2487#define TG3_GPIO_MSG_MASK (TG3_GPIO_MSG_DRVR_PRES | \
2488 TG3_GPIO_MSG_NEED_VAUX)
2489#define TG3_GPIO_MSG_ALL_DRVR_PRES_MASK \
2490 ((TG3_GPIO_MSG_DRVR_PRES << 0) | \
2491 (TG3_GPIO_MSG_DRVR_PRES << 4) | \
2492 (TG3_GPIO_MSG_DRVR_PRES << 8) | \
2493 (TG3_GPIO_MSG_DRVR_PRES << 12))
2494
2495#define TG3_GPIO_MSG_ALL_NEED_VAUX_MASK \
2496 ((TG3_GPIO_MSG_NEED_VAUX << 0) | \
2497 (TG3_GPIO_MSG_NEED_VAUX << 4) | \
2498 (TG3_GPIO_MSG_NEED_VAUX << 8) | \
2499 (TG3_GPIO_MSG_NEED_VAUX << 12))
2500
2501static inline u32 tg3_set_function_status(struct tg3 *tp, u32 newstat)
2502{
2503 u32 status, shift;
2504
2505 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2506 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
2507 status = tg3_ape_read32(tp, TG3_APE_GPIO_MSG);
2508 else
2509 status = tr32(TG3_CPMU_DRV_STATUS);
2510
2511 shift = TG3_APE_GPIO_MSG_SHIFT + 4 * tp->pci_fn;
2512 status &= ~(TG3_GPIO_MSG_MASK << shift);
2513 status |= (newstat << shift);
2514
2515 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2516 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
2517 tg3_ape_write32(tp, TG3_APE_GPIO_MSG, status);
2518 else
2519 tw32(TG3_CPMU_DRV_STATUS, status);
2520
2521 return status >> TG3_APE_GPIO_MSG_SHIFT;
2522}
2523
Matt Carlson520b2752011-06-13 13:39:02 +00002524static inline int tg3_pwrsrc_switch_to_vmain(struct tg3 *tp)
2525{
2526 if (!tg3_flag(tp, IS_NIC))
2527 return 0;
2528
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002529 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2530 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
2531 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
2532 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2533 return -EIO;
Matt Carlson520b2752011-06-13 13:39:02 +00002534
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002535 tg3_set_function_status(tp, TG3_GPIO_MSG_DRVR_PRES);
2536
2537 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2538 TG3_GRC_LCLCTL_PWRSW_DELAY);
2539
2540 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
2541 } else {
2542 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2543 TG3_GRC_LCLCTL_PWRSW_DELAY);
2544 }
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002545
Matt Carlson520b2752011-06-13 13:39:02 +00002546 return 0;
2547}
2548
2549static void tg3_pwrsrc_die_with_vmain(struct tg3 *tp)
2550{
2551 u32 grc_local_ctrl;
2552
2553 if (!tg3_flag(tp, IS_NIC) ||
2554 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2555 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)
2556 return;
2557
2558 grc_local_ctrl = tp->grc_local_ctrl | GRC_LCLCTRL_GPIO_OE1;
2559
2560 tw32_wait_f(GRC_LOCAL_CTRL,
2561 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2562 TG3_GRC_LCLCTL_PWRSW_DELAY);
2563
2564 tw32_wait_f(GRC_LOCAL_CTRL,
2565 grc_local_ctrl,
2566 TG3_GRC_LCLCTL_PWRSW_DELAY);
2567
2568 tw32_wait_f(GRC_LOCAL_CTRL,
2569 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2570 TG3_GRC_LCLCTL_PWRSW_DELAY);
2571}
2572
2573static void tg3_pwrsrc_switch_to_vaux(struct tg3 *tp)
2574{
2575 if (!tg3_flag(tp, IS_NIC))
2576 return;
2577
2578 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2579 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2580 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2581 (GRC_LCLCTRL_GPIO_OE0 |
2582 GRC_LCLCTRL_GPIO_OE1 |
2583 GRC_LCLCTRL_GPIO_OE2 |
2584 GRC_LCLCTRL_GPIO_OUTPUT0 |
2585 GRC_LCLCTRL_GPIO_OUTPUT1),
2586 TG3_GRC_LCLCTL_PWRSW_DELAY);
2587 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2588 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
2589 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2590 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2591 GRC_LCLCTRL_GPIO_OE1 |
2592 GRC_LCLCTRL_GPIO_OE2 |
2593 GRC_LCLCTRL_GPIO_OUTPUT0 |
2594 GRC_LCLCTRL_GPIO_OUTPUT1 |
2595 tp->grc_local_ctrl;
2596 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2597 TG3_GRC_LCLCTL_PWRSW_DELAY);
2598
2599 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2600 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2601 TG3_GRC_LCLCTL_PWRSW_DELAY);
2602
2603 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2604 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2605 TG3_GRC_LCLCTL_PWRSW_DELAY);
2606 } else {
2607 u32 no_gpio2;
2608 u32 grc_local_ctrl = 0;
2609
2610 /* Workaround to prevent overdrawing Amps. */
2611 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
2612 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
2613 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2614 grc_local_ctrl,
2615 TG3_GRC_LCLCTL_PWRSW_DELAY);
2616 }
2617
2618 /* On 5753 and variants, GPIO2 cannot be used. */
2619 no_gpio2 = tp->nic_sram_data_cfg &
2620 NIC_SRAM_DATA_CFG_NO_GPIO2;
2621
2622 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
2623 GRC_LCLCTRL_GPIO_OE1 |
2624 GRC_LCLCTRL_GPIO_OE2 |
2625 GRC_LCLCTRL_GPIO_OUTPUT1 |
2626 GRC_LCLCTRL_GPIO_OUTPUT2;
2627 if (no_gpio2) {
2628 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2629 GRC_LCLCTRL_GPIO_OUTPUT2);
2630 }
2631 tw32_wait_f(GRC_LOCAL_CTRL,
2632 tp->grc_local_ctrl | grc_local_ctrl,
2633 TG3_GRC_LCLCTL_PWRSW_DELAY);
2634
2635 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2636
2637 tw32_wait_f(GRC_LOCAL_CTRL,
2638 tp->grc_local_ctrl | grc_local_ctrl,
2639 TG3_GRC_LCLCTL_PWRSW_DELAY);
2640
2641 if (!no_gpio2) {
2642 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
2643 tw32_wait_f(GRC_LOCAL_CTRL,
2644 tp->grc_local_ctrl | grc_local_ctrl,
2645 TG3_GRC_LCLCTL_PWRSW_DELAY);
2646 }
2647 }
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002648}
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002649
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002650static void tg3_frob_aux_power_5717(struct tg3 *tp, bool wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002651{
2652 u32 msg = 0;
2653
2654 /* Serialize power state transitions */
2655 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2656 return;
2657
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002658 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE) || wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002659 msg = TG3_GPIO_MSG_NEED_VAUX;
2660
2661 msg = tg3_set_function_status(tp, msg);
2662
2663 if (msg & TG3_GPIO_MSG_ALL_DRVR_PRES_MASK)
2664 goto done;
2665
2666 if (msg & TG3_GPIO_MSG_ALL_NEED_VAUX_MASK)
2667 tg3_pwrsrc_switch_to_vaux(tp);
2668 else
2669 tg3_pwrsrc_die_with_vmain(tp);
2670
2671done:
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002672 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
Matt Carlson520b2752011-06-13 13:39:02 +00002673}
2674
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002675static void tg3_frob_aux_power(struct tg3 *tp, bool include_wol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676{
Matt Carlson683644b2011-03-09 16:58:23 +00002677 bool need_vaux = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678
Matt Carlson334355a2010-01-20 16:58:10 +00002679 /* The GPIOs do something completely different on 57765. */
Joe Perches63c3a662011-04-26 08:12:10 +00002680 if (!tg3_flag(tp, IS_NIC) ||
Matt Carlson334355a2010-01-20 16:58:10 +00002681 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 return;
2683
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002684 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2685 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
2686 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002687 tg3_frob_aux_power_5717(tp, include_wol ?
2688 tg3_flag(tp, WOL_ENABLE) != 0 : 0);
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002689 return;
2690 }
2691
2692 if (tp->pdev_peer && tp->pdev_peer != tp->pdev) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002693 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002695 dev_peer = pci_get_drvdata(tp->pdev_peer);
Matt Carlson683644b2011-03-09 16:58:23 +00002696
Michael Chanbc1c7562006-03-20 17:48:03 -08002697 /* remove_one() may have been run on the peer. */
Matt Carlson683644b2011-03-09 16:58:23 +00002698 if (dev_peer) {
2699 struct tg3 *tp_peer = netdev_priv(dev_peer);
2700
Joe Perches63c3a662011-04-26 08:12:10 +00002701 if (tg3_flag(tp_peer, INIT_COMPLETE))
Matt Carlson683644b2011-03-09 16:58:23 +00002702 return;
2703
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002704 if ((include_wol && tg3_flag(tp_peer, WOL_ENABLE)) ||
Joe Perches63c3a662011-04-26 08:12:10 +00002705 tg3_flag(tp_peer, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002706 need_vaux = true;
2707 }
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002710 if ((include_wol && tg3_flag(tp, WOL_ENABLE)) ||
2711 tg3_flag(tp, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002712 need_vaux = true;
2713
Matt Carlson520b2752011-06-13 13:39:02 +00002714 if (need_vaux)
2715 tg3_pwrsrc_switch_to_vaux(tp);
2716 else
2717 tg3_pwrsrc_die_with_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718}
2719
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002720static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2721{
2722 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2723 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00002724 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002725 if (speed != SPEED_10)
2726 return 1;
2727 } else if (speed == SPEED_10)
2728 return 1;
2729
2730 return 0;
2731}
2732
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733static int tg3_setup_phy(struct tg3 *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734static int tg3_halt_cpu(struct tg3 *, u32);
2735
Matt Carlson0a459aa2008-11-03 16:54:15 -08002736static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002737{
Matt Carlsonce057f02007-11-12 21:08:03 -08002738 u32 val;
2739
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002740 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Michael Chan51297242007-02-13 12:17:57 -08002741 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2742 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2743 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2744
2745 sg_dig_ctrl |=
2746 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2747 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2748 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2749 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002750 return;
Michael Chan51297242007-02-13 12:17:57 -08002751 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002752
Michael Chan60189dd2006-12-17 17:08:07 -08002753 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002754 tg3_bmcr_reset(tp);
2755 val = tr32(GRC_MISC_CFG);
2756 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2757 udelay(40);
2758 return;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002759 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson0e5f7842009-11-02 14:26:38 +00002760 u32 phytest;
2761 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2762 u32 phy;
2763
2764 tg3_writephy(tp, MII_ADVERTISE, 0);
2765 tg3_writephy(tp, MII_BMCR,
2766 BMCR_ANENABLE | BMCR_ANRESTART);
2767
2768 tg3_writephy(tp, MII_TG3_FET_TEST,
2769 phytest | MII_TG3_FET_SHADOW_EN);
2770 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
2771 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
2772 tg3_writephy(tp,
2773 MII_TG3_FET_SHDW_AUXMODE4,
2774 phy);
2775 }
2776 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2777 }
2778 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002779 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07002780 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2781 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002782
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002783 val = MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2784 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2785 MII_TG3_AUXCTL_PCTL_VREG_11V;
2786 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, val);
Michael Chan715116a2006-09-27 16:09:25 -07002787 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002788
Michael Chan15c3b692006-03-22 01:06:52 -08002789 /* The PHY should not be powered down on some chips because
2790 * of bugs.
2791 */
2792 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2793 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2794 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002795 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Michael Chan15c3b692006-03-22 01:06:52 -08002796 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08002797
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002798 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2799 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002800 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2801 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2802 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2803 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2804 }
2805
Michael Chan15c3b692006-03-22 01:06:52 -08002806 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2807}
2808
Matt Carlson3f007892008-11-03 16:51:36 -08002809/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002810static int tg3_nvram_lock(struct tg3 *tp)
2811{
Joe Perches63c3a662011-04-26 08:12:10 +00002812 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002813 int i;
2814
2815 if (tp->nvram_lock_cnt == 0) {
2816 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2817 for (i = 0; i < 8000; i++) {
2818 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2819 break;
2820 udelay(20);
2821 }
2822 if (i == 8000) {
2823 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2824 return -ENODEV;
2825 }
2826 }
2827 tp->nvram_lock_cnt++;
2828 }
2829 return 0;
2830}
2831
2832/* tp->lock is held. */
2833static void tg3_nvram_unlock(struct tg3 *tp)
2834{
Joe Perches63c3a662011-04-26 08:12:10 +00002835 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002836 if (tp->nvram_lock_cnt > 0)
2837 tp->nvram_lock_cnt--;
2838 if (tp->nvram_lock_cnt == 0)
2839 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
2840 }
2841}
2842
2843/* tp->lock is held. */
2844static void tg3_enable_nvram_access(struct tg3 *tp)
2845{
Joe Perches63c3a662011-04-26 08:12:10 +00002846 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002847 u32 nvaccess = tr32(NVRAM_ACCESS);
2848
2849 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
2850 }
2851}
2852
2853/* tp->lock is held. */
2854static void tg3_disable_nvram_access(struct tg3 *tp)
2855{
Joe Perches63c3a662011-04-26 08:12:10 +00002856 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002857 u32 nvaccess = tr32(NVRAM_ACCESS);
2858
2859 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
2860 }
2861}
2862
2863static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
2864 u32 offset, u32 *val)
2865{
2866 u32 tmp;
2867 int i;
2868
2869 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
2870 return -EINVAL;
2871
2872 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
2873 EEPROM_ADDR_DEVID_MASK |
2874 EEPROM_ADDR_READ);
2875 tw32(GRC_EEPROM_ADDR,
2876 tmp |
2877 (0 << EEPROM_ADDR_DEVID_SHIFT) |
2878 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
2879 EEPROM_ADDR_ADDR_MASK) |
2880 EEPROM_ADDR_READ | EEPROM_ADDR_START);
2881
2882 for (i = 0; i < 1000; i++) {
2883 tmp = tr32(GRC_EEPROM_ADDR);
2884
2885 if (tmp & EEPROM_ADDR_COMPLETE)
2886 break;
2887 msleep(1);
2888 }
2889 if (!(tmp & EEPROM_ADDR_COMPLETE))
2890 return -EBUSY;
2891
Matt Carlson62cedd12009-04-20 14:52:29 -07002892 tmp = tr32(GRC_EEPROM_DATA);
2893
2894 /*
2895 * The data will always be opposite the native endian
2896 * format. Perform a blind byteswap to compensate.
2897 */
2898 *val = swab32(tmp);
2899
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002900 return 0;
2901}
2902
2903#define NVRAM_CMD_TIMEOUT 10000
2904
2905static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
2906{
2907 int i;
2908
2909 tw32(NVRAM_CMD, nvram_cmd);
2910 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
2911 udelay(10);
2912 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
2913 udelay(10);
2914 break;
2915 }
2916 }
2917
2918 if (i == NVRAM_CMD_TIMEOUT)
2919 return -EBUSY;
2920
2921 return 0;
2922}
2923
2924static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
2925{
Joe Perches63c3a662011-04-26 08:12:10 +00002926 if (tg3_flag(tp, NVRAM) &&
2927 tg3_flag(tp, NVRAM_BUFFERED) &&
2928 tg3_flag(tp, FLASH) &&
2929 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002930 (tp->nvram_jedecnum == JEDEC_ATMEL))
2931
2932 addr = ((addr / tp->nvram_pagesize) <<
2933 ATMEL_AT45DB0X1B_PAGE_POS) +
2934 (addr % tp->nvram_pagesize);
2935
2936 return addr;
2937}
2938
2939static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
2940{
Joe Perches63c3a662011-04-26 08:12:10 +00002941 if (tg3_flag(tp, NVRAM) &&
2942 tg3_flag(tp, NVRAM_BUFFERED) &&
2943 tg3_flag(tp, FLASH) &&
2944 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002945 (tp->nvram_jedecnum == JEDEC_ATMEL))
2946
2947 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
2948 tp->nvram_pagesize) +
2949 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
2950
2951 return addr;
2952}
2953
Matt Carlsone4f34112009-02-25 14:25:00 +00002954/* NOTE: Data read in from NVRAM is byteswapped according to
2955 * the byteswapping settings for all other register accesses.
2956 * tg3 devices are BE devices, so on a BE machine, the data
2957 * returned will be exactly as it is seen in NVRAM. On a LE
2958 * machine, the 32-bit value will be byteswapped.
2959 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002960static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
2961{
2962 int ret;
2963
Joe Perches63c3a662011-04-26 08:12:10 +00002964 if (!tg3_flag(tp, NVRAM))
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002965 return tg3_nvram_read_using_eeprom(tp, offset, val);
2966
2967 offset = tg3_nvram_phys_addr(tp, offset);
2968
2969 if (offset > NVRAM_ADDR_MSK)
2970 return -EINVAL;
2971
2972 ret = tg3_nvram_lock(tp);
2973 if (ret)
2974 return ret;
2975
2976 tg3_enable_nvram_access(tp);
2977
2978 tw32(NVRAM_ADDR, offset);
2979 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
2980 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
2981
2982 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00002983 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002984
2985 tg3_disable_nvram_access(tp);
2986
2987 tg3_nvram_unlock(tp);
2988
2989 return ret;
2990}
2991
Matt Carlsona9dc5292009-02-25 14:25:30 +00002992/* Ensures NVRAM data is in bytestream format. */
2993static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002994{
2995 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00002996 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002997 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00002998 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002999 return res;
3000}
3001
Matt Carlson997b4f12011-08-31 11:44:53 +00003002#define RX_CPU_SCRATCH_BASE 0x30000
3003#define RX_CPU_SCRATCH_SIZE 0x04000
3004#define TX_CPU_SCRATCH_BASE 0x34000
3005#define TX_CPU_SCRATCH_SIZE 0x04000
3006
3007/* tp->lock is held. */
3008static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
3009{
3010 int i;
3011
3012 BUG_ON(offset == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS));
3013
3014 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
3015 u32 val = tr32(GRC_VCPU_EXT_CTRL);
3016
3017 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
3018 return 0;
3019 }
3020 if (offset == RX_CPU_BASE) {
3021 for (i = 0; i < 10000; i++) {
3022 tw32(offset + CPU_STATE, 0xffffffff);
3023 tw32(offset + CPU_MODE, CPU_MODE_HALT);
3024 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
3025 break;
3026 }
3027
3028 tw32(offset + CPU_STATE, 0xffffffff);
3029 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
3030 udelay(10);
3031 } else {
3032 for (i = 0; i < 10000; i++) {
3033 tw32(offset + CPU_STATE, 0xffffffff);
3034 tw32(offset + CPU_MODE, CPU_MODE_HALT);
3035 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
3036 break;
3037 }
3038 }
3039
3040 if (i >= 10000) {
3041 netdev_err(tp->dev, "%s timed out, %s CPU\n",
3042 __func__, offset == RX_CPU_BASE ? "RX" : "TX");
3043 return -ENODEV;
3044 }
3045
3046 /* Clear firmware's nvram arbitration. */
3047 if (tg3_flag(tp, NVRAM))
3048 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
3049 return 0;
3050}
3051
3052struct fw_info {
3053 unsigned int fw_base;
3054 unsigned int fw_len;
3055 const __be32 *fw_data;
3056};
3057
3058/* tp->lock is held. */
3059static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base,
3060 u32 cpu_scratch_base, int cpu_scratch_size,
3061 struct fw_info *info)
3062{
3063 int err, lock_err, i;
3064 void (*write_op)(struct tg3 *, u32, u32);
3065
3066 if (cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS)) {
3067 netdev_err(tp->dev,
3068 "%s: Trying to load TX cpu firmware which is 5705\n",
3069 __func__);
3070 return -EINVAL;
3071 }
3072
3073 if (tg3_flag(tp, 5705_PLUS))
3074 write_op = tg3_write_mem;
3075 else
3076 write_op = tg3_write_indirect_reg32;
3077
3078 /* It is possible that bootcode is still loading at this point.
3079 * Get the nvram lock first before halting the cpu.
3080 */
3081 lock_err = tg3_nvram_lock(tp);
3082 err = tg3_halt_cpu(tp, cpu_base);
3083 if (!lock_err)
3084 tg3_nvram_unlock(tp);
3085 if (err)
3086 goto out;
3087
3088 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
3089 write_op(tp, cpu_scratch_base + i, 0);
3090 tw32(cpu_base + CPU_STATE, 0xffffffff);
3091 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
3092 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
3093 write_op(tp, (cpu_scratch_base +
3094 (info->fw_base & 0xffff) +
3095 (i * sizeof(u32))),
3096 be32_to_cpu(info->fw_data[i]));
3097
3098 err = 0;
3099
3100out:
3101 return err;
3102}
3103
3104/* tp->lock is held. */
3105static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
3106{
3107 struct fw_info info;
3108 const __be32 *fw_data;
3109 int err, i;
3110
3111 fw_data = (void *)tp->fw->data;
3112
3113 /* Firmware blob starts with version numbers, followed by
3114 start address and length. We are setting complete length.
3115 length = end_address_of_bss - start_address_of_text.
3116 Remainder is the blob to be loaded contiguously
3117 from start address. */
3118
3119 info.fw_base = be32_to_cpu(fw_data[1]);
3120 info.fw_len = tp->fw->size - 12;
3121 info.fw_data = &fw_data[3];
3122
3123 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
3124 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
3125 &info);
3126 if (err)
3127 return err;
3128
3129 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
3130 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
3131 &info);
3132 if (err)
3133 return err;
3134
3135 /* Now startup only the RX cpu. */
3136 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3137 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
3138
3139 for (i = 0; i < 5; i++) {
3140 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
3141 break;
3142 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3143 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
3144 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
3145 udelay(1000);
3146 }
3147 if (i >= 5) {
3148 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
3149 "should be %08x\n", __func__,
3150 tr32(RX_CPU_BASE + CPU_PC), info.fw_base);
3151 return -ENODEV;
3152 }
3153 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3154 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
3155
3156 return 0;
3157}
3158
3159/* tp->lock is held. */
3160static int tg3_load_tso_firmware(struct tg3 *tp)
3161{
3162 struct fw_info info;
3163 const __be32 *fw_data;
3164 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
3165 int err, i;
3166
3167 if (tg3_flag(tp, HW_TSO_1) ||
3168 tg3_flag(tp, HW_TSO_2) ||
3169 tg3_flag(tp, HW_TSO_3))
3170 return 0;
3171
3172 fw_data = (void *)tp->fw->data;
3173
3174 /* Firmware blob starts with version numbers, followed by
3175 start address and length. We are setting complete length.
3176 length = end_address_of_bss - start_address_of_text.
3177 Remainder is the blob to be loaded contiguously
3178 from start address. */
3179
3180 info.fw_base = be32_to_cpu(fw_data[1]);
3181 cpu_scratch_size = tp->fw_len;
3182 info.fw_len = tp->fw->size - 12;
3183 info.fw_data = &fw_data[3];
3184
3185 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
3186 cpu_base = RX_CPU_BASE;
3187 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
3188 } else {
3189 cpu_base = TX_CPU_BASE;
3190 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
3191 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
3192 }
3193
3194 err = tg3_load_firmware_cpu(tp, cpu_base,
3195 cpu_scratch_base, cpu_scratch_size,
3196 &info);
3197 if (err)
3198 return err;
3199
3200 /* Now startup the cpu. */
3201 tw32(cpu_base + CPU_STATE, 0xffffffff);
3202 tw32_f(cpu_base + CPU_PC, info.fw_base);
3203
3204 for (i = 0; i < 5; i++) {
3205 if (tr32(cpu_base + CPU_PC) == info.fw_base)
3206 break;
3207 tw32(cpu_base + CPU_STATE, 0xffffffff);
3208 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
3209 tw32_f(cpu_base + CPU_PC, info.fw_base);
3210 udelay(1000);
3211 }
3212 if (i >= 5) {
3213 netdev_err(tp->dev,
3214 "%s fails to set CPU PC, is %08x should be %08x\n",
3215 __func__, tr32(cpu_base + CPU_PC), info.fw_base);
3216 return -ENODEV;
3217 }
3218 tw32(cpu_base + CPU_STATE, 0xffffffff);
3219 tw32_f(cpu_base + CPU_MODE, 0x00000000);
3220 return 0;
3221}
3222
3223
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003224/* tp->lock is held. */
Matt Carlson3f007892008-11-03 16:51:36 -08003225static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
3226{
3227 u32 addr_high, addr_low;
3228 int i;
3229
3230 addr_high = ((tp->dev->dev_addr[0] << 8) |
3231 tp->dev->dev_addr[1]);
3232 addr_low = ((tp->dev->dev_addr[2] << 24) |
3233 (tp->dev->dev_addr[3] << 16) |
3234 (tp->dev->dev_addr[4] << 8) |
3235 (tp->dev->dev_addr[5] << 0));
3236 for (i = 0; i < 4; i++) {
3237 if (i == 1 && skip_mac_1)
3238 continue;
3239 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
3240 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
3241 }
3242
3243 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3244 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
3245 for (i = 0; i < 12; i++) {
3246 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
3247 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
3248 }
3249 }
3250
3251 addr_high = (tp->dev->dev_addr[0] +
3252 tp->dev->dev_addr[1] +
3253 tp->dev->dev_addr[2] +
3254 tp->dev->dev_addr[3] +
3255 tp->dev->dev_addr[4] +
3256 tp->dev->dev_addr[5]) &
3257 TX_BACKOFF_SEED_MASK;
3258 tw32(MAC_TX_BACKOFF_SEED, addr_high);
3259}
3260
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003261static void tg3_enable_register_access(struct tg3 *tp)
3262{
3263 /*
3264 * Make sure register accesses (indirect or otherwise) will function
3265 * correctly.
3266 */
3267 pci_write_config_dword(tp->pdev,
3268 TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
3269}
3270
3271static int tg3_power_up(struct tg3 *tp)
3272{
Matt Carlsonbed98292011-07-13 09:27:29 +00003273 int err;
3274
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003275 tg3_enable_register_access(tp);
3276
Matt Carlsonbed98292011-07-13 09:27:29 +00003277 err = pci_set_power_state(tp->pdev, PCI_D0);
3278 if (!err) {
3279 /* Switch out of Vaux if it is a NIC */
3280 tg3_pwrsrc_switch_to_vmain(tp);
3281 } else {
3282 netdev_err(tp->dev, "Transition to D0 failed\n");
3283 }
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003284
Matt Carlsonbed98292011-07-13 09:27:29 +00003285 return err;
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003286}
3287
3288static int tg3_power_down_prepare(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289{
3290 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003291 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003293 tg3_enable_register_access(tp);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003294
3295 /* Restore the CLKREQ setting. */
Joe Perches63c3a662011-04-26 08:12:10 +00003296 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003297 u16 lnkctl;
3298
3299 pci_read_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00003300 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003301 &lnkctl);
3302 lnkctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
3303 pci_write_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00003304 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003305 lnkctl);
3306 }
3307
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
3309 tw32(TG3PCI_MISC_HOST_CTRL,
3310 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
3311
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003312 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00003313 tg3_flag(tp, WOL_ENABLE);
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003314
Joe Perches63c3a662011-04-26 08:12:10 +00003315 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08003316 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003317 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson80096062010-08-02 11:26:06 +00003318 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003319 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003320 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003321
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00003322 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003323
Matt Carlson80096062010-08-02 11:26:06 +00003324 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003325
3326 tp->link_config.orig_speed = phydev->speed;
3327 tp->link_config.orig_duplex = phydev->duplex;
3328 tp->link_config.orig_autoneg = phydev->autoneg;
3329 tp->link_config.orig_advertising = phydev->advertising;
3330
3331 advertising = ADVERTISED_TP |
3332 ADVERTISED_Pause |
3333 ADVERTISED_Autoneg |
3334 ADVERTISED_10baseT_Half;
3335
Joe Perches63c3a662011-04-26 08:12:10 +00003336 if (tg3_flag(tp, ENABLE_ASF) || device_should_wake) {
3337 if (tg3_flag(tp, WOL_SPEED_100MB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003338 advertising |=
3339 ADVERTISED_100baseT_Half |
3340 ADVERTISED_100baseT_Full |
3341 ADVERTISED_10baseT_Full;
3342 else
3343 advertising |= ADVERTISED_10baseT_Full;
3344 }
3345
3346 phydev->advertising = advertising;
3347
3348 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08003349
3350 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00003351 if (phyid != PHY_ID_BCMAC131) {
3352 phyid &= PHY_BCM_OUI_MASK;
3353 if (phyid == PHY_BCM_OUI_1 ||
3354 phyid == PHY_BCM_OUI_2 ||
3355 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08003356 do_low_power = true;
3357 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003358 }
Matt Carlsondd477002008-05-25 23:45:58 -07003359 } else {
Matt Carlson20232762008-12-21 20:18:56 -08003360 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003361
Matt Carlson80096062010-08-02 11:26:06 +00003362 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
3363 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07003364 tp->link_config.orig_speed = tp->link_config.speed;
3365 tp->link_config.orig_duplex = tp->link_config.duplex;
3366 tp->link_config.orig_autoneg = tp->link_config.autoneg;
3367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003369 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Matt Carlsondd477002008-05-25 23:45:58 -07003370 tp->link_config.speed = SPEED_10;
3371 tp->link_config.duplex = DUPLEX_HALF;
3372 tp->link_config.autoneg = AUTONEG_ENABLE;
3373 tg3_setup_phy(tp, 0);
3374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 }
3376
Michael Chanb5d37722006-09-27 16:06:21 -07003377 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
3378 u32 val;
3379
3380 val = tr32(GRC_VCPU_EXT_CTRL);
3381 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
Joe Perches63c3a662011-04-26 08:12:10 +00003382 } else if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08003383 int i;
3384 u32 val;
3385
3386 for (i = 0; i < 200; i++) {
3387 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
3388 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
3389 break;
3390 msleep(1);
3391 }
3392 }
Joe Perches63c3a662011-04-26 08:12:10 +00003393 if (tg3_flag(tp, WOL_CAP))
Gary Zambranoa85feb82007-05-05 11:52:19 -07003394 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
3395 WOL_DRV_STATE_SHUTDOWN |
3396 WOL_DRV_WOL |
3397 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08003398
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003399 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 u32 mac_mode;
3401
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003402 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003403 if (do_low_power &&
3404 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
3405 tg3_phy_auxctl_write(tp,
3406 MII_TG3_AUXCTL_SHDWSEL_PWRCTL,
3407 MII_TG3_AUXCTL_PCTL_WOL_EN |
3408 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
3409 MII_TG3_AUXCTL_PCTL_CL_AB_TXDAC);
Matt Carlsondd477002008-05-25 23:45:58 -07003410 udelay(40);
3411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003413 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07003414 mac_mode = MAC_MODE_PORT_MODE_GMII;
3415 else
3416 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003418 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
3419 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
3420 ASIC_REV_5700) {
Joe Perches63c3a662011-04-26 08:12:10 +00003421 u32 speed = tg3_flag(tp, WOL_SPEED_100MB) ?
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003422 SPEED_100 : SPEED_10;
3423 if (tg3_5700_link_polarity(tp, speed))
3424 mac_mode |= MAC_MODE_LINK_POLARITY;
3425 else
3426 mac_mode &= ~MAC_MODE_LINK_POLARITY;
3427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 } else {
3429 mac_mode = MAC_MODE_PORT_MODE_TBI;
3430 }
3431
Joe Perches63c3a662011-04-26 08:12:10 +00003432 if (!tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 tw32(MAC_LED_CTRL, tp->led_ctrl);
3434
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003435 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00003436 if ((tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS)) &&
3437 (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)))
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003438 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439
Joe Perches63c3a662011-04-26 08:12:10 +00003440 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00003441 mac_mode |= MAC_MODE_APE_TX_EN |
3442 MAC_MODE_APE_RX_EN |
3443 MAC_MODE_TDE_ENABLE;
Matt Carlson3bda1252008-08-15 14:08:22 -07003444
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 tw32_f(MAC_MODE, mac_mode);
3446 udelay(100);
3447
3448 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
3449 udelay(10);
3450 }
3451
Joe Perches63c3a662011-04-26 08:12:10 +00003452 if (!tg3_flag(tp, WOL_SPEED_100MB) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3454 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
3455 u32 base_val;
3456
3457 base_val = tp->pci_clock_ctrl;
3458 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
3459 CLOCK_CTRL_TXCLK_DISABLE);
3460
Michael Chanb401e9e2005-12-19 16:27:04 -08003461 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
3462 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Joe Perches63c3a662011-04-26 08:12:10 +00003463 } else if (tg3_flag(tp, 5780_CLASS) ||
3464 tg3_flag(tp, CPMU_PRESENT) ||
Matt Carlson6ff6f812011-05-19 12:12:54 +00003465 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan4cf78e42005-07-25 12:29:19 -07003466 /* do nothing */
Joe Perches63c3a662011-04-26 08:12:10 +00003467 } else if (!(tg3_flag(tp, 5750_PLUS) && tg3_flag(tp, ENABLE_ASF))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 u32 newbits1, newbits2;
3469
3470 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3471 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3472 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
3473 CLOCK_CTRL_TXCLK_DISABLE |
3474 CLOCK_CTRL_ALTCLK);
3475 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
Joe Perches63c3a662011-04-26 08:12:10 +00003476 } else if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 newbits1 = CLOCK_CTRL_625_CORE;
3478 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
3479 } else {
3480 newbits1 = CLOCK_CTRL_ALTCLK;
3481 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
3482 }
3483
Michael Chanb401e9e2005-12-19 16:27:04 -08003484 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
3485 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486
Michael Chanb401e9e2005-12-19 16:27:04 -08003487 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
3488 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489
Joe Perches63c3a662011-04-26 08:12:10 +00003490 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 u32 newbits3;
3492
3493 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3494 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3495 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
3496 CLOCK_CTRL_TXCLK_DISABLE |
3497 CLOCK_CTRL_44MHZ_CORE);
3498 } else {
3499 newbits3 = CLOCK_CTRL_44MHZ_CORE;
3500 }
3501
Michael Chanb401e9e2005-12-19 16:27:04 -08003502 tw32_wait_f(TG3PCI_CLOCK_CTRL,
3503 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 }
3505 }
3506
Joe Perches63c3a662011-04-26 08:12:10 +00003507 if (!(device_should_wake) && !tg3_flag(tp, ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08003508 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08003509
Matt Carlsoncd0d7222011-07-13 09:27:33 +00003510 tg3_frob_aux_power(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511
3512 /* Workaround for unstable PLL clock */
3513 if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
3514 (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
3515 u32 val = tr32(0x7d00);
3516
3517 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
3518 tw32(0x7d00, val);
Joe Perches63c3a662011-04-26 08:12:10 +00003519 if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08003520 int err;
3521
3522 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08003524 if (!err)
3525 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08003526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 }
3528
Michael Chanbbadf502006-04-06 21:46:34 -07003529 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
3530
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 return 0;
3532}
3533
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003534static void tg3_power_down(struct tg3 *tp)
3535{
3536 tg3_power_down_prepare(tp);
3537
Joe Perches63c3a662011-04-26 08:12:10 +00003538 pci_wake_from_d3(tp->pdev, tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003539 pci_set_power_state(tp->pdev, PCI_D3hot);
3540}
3541
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
3543{
3544 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
3545 case MII_TG3_AUX_STAT_10HALF:
3546 *speed = SPEED_10;
3547 *duplex = DUPLEX_HALF;
3548 break;
3549
3550 case MII_TG3_AUX_STAT_10FULL:
3551 *speed = SPEED_10;
3552 *duplex = DUPLEX_FULL;
3553 break;
3554
3555 case MII_TG3_AUX_STAT_100HALF:
3556 *speed = SPEED_100;
3557 *duplex = DUPLEX_HALF;
3558 break;
3559
3560 case MII_TG3_AUX_STAT_100FULL:
3561 *speed = SPEED_100;
3562 *duplex = DUPLEX_FULL;
3563 break;
3564
3565 case MII_TG3_AUX_STAT_1000HALF:
3566 *speed = SPEED_1000;
3567 *duplex = DUPLEX_HALF;
3568 break;
3569
3570 case MII_TG3_AUX_STAT_1000FULL:
3571 *speed = SPEED_1000;
3572 *duplex = DUPLEX_FULL;
3573 break;
3574
3575 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003576 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07003577 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
3578 SPEED_10;
3579 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
3580 DUPLEX_HALF;
3581 break;
3582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 *speed = SPEED_INVALID;
3584 *duplex = DUPLEX_INVALID;
3585 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587}
3588
Matt Carlson42b64a42011-05-19 12:12:49 +00003589static int tg3_phy_autoneg_cfg(struct tg3 *tp, u32 advertise, u32 flowctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590{
Matt Carlson42b64a42011-05-19 12:12:49 +00003591 int err = 0;
3592 u32 val, new_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593
Matt Carlson42b64a42011-05-19 12:12:49 +00003594 new_adv = ADVERTISE_CSMA;
3595 if (advertise & ADVERTISED_10baseT_Half)
3596 new_adv |= ADVERTISE_10HALF;
3597 if (advertise & ADVERTISED_10baseT_Full)
3598 new_adv |= ADVERTISE_10FULL;
3599 if (advertise & ADVERTISED_100baseT_Half)
3600 new_adv |= ADVERTISE_100HALF;
3601 if (advertise & ADVERTISED_100baseT_Full)
3602 new_adv |= ADVERTISE_100FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603
Matt Carlson42b64a42011-05-19 12:12:49 +00003604 new_adv |= tg3_advert_flowctrl_1000T(flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605
Matt Carlson42b64a42011-05-19 12:12:49 +00003606 err = tg3_writephy(tp, MII_ADVERTISE, new_adv);
3607 if (err)
3608 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609
Matt Carlson42b64a42011-05-19 12:12:49 +00003610 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
3611 goto done;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003612
Matt Carlson42b64a42011-05-19 12:12:49 +00003613 new_adv = 0;
3614 if (advertise & ADVERTISED_1000baseT_Half)
Matt Carlson221c5632011-06-13 13:39:01 +00003615 new_adv |= ADVERTISE_1000HALF;
Matt Carlson42b64a42011-05-19 12:12:49 +00003616 if (advertise & ADVERTISED_1000baseT_Full)
Matt Carlson221c5632011-06-13 13:39:01 +00003617 new_adv |= ADVERTISE_1000FULL;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003618
Matt Carlson42b64a42011-05-19 12:12:49 +00003619 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3620 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
Matt Carlson221c5632011-06-13 13:39:01 +00003621 new_adv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622
Matt Carlson221c5632011-06-13 13:39:01 +00003623 err = tg3_writephy(tp, MII_CTRL1000, new_adv);
Matt Carlson42b64a42011-05-19 12:12:49 +00003624 if (err)
3625 goto done;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003626
Matt Carlson42b64a42011-05-19 12:12:49 +00003627 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
3628 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629
Matt Carlson42b64a42011-05-19 12:12:49 +00003630 tw32(TG3_CPMU_EEE_MODE,
3631 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003632
Matt Carlson42b64a42011-05-19 12:12:49 +00003633 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
3634 if (!err) {
3635 u32 err2;
Matt Carlson52b02d02010-10-14 10:37:41 +00003636
Matt Carlsona6b68da2010-12-06 08:28:52 +00003637 val = 0;
Matt Carlson42b64a42011-05-19 12:12:49 +00003638 /* Advertise 100-BaseTX EEE ability */
3639 if (advertise & ADVERTISED_100baseT_Full)
3640 val |= MDIO_AN_EEE_ADV_100TX;
3641 /* Advertise 1000-BaseT EEE ability */
3642 if (advertise & ADVERTISED_1000baseT_Full)
3643 val |= MDIO_AN_EEE_ADV_1000T;
3644 err = tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlsonb715ce92011-07-20 10:20:52 +00003645 if (err)
3646 val = 0;
3647
3648 switch (GET_ASIC_REV(tp->pci_chip_rev_id)) {
3649 case ASIC_REV_5717:
3650 case ASIC_REV_57765:
3651 case ASIC_REV_5719:
3652 /* If we advertised any eee advertisements above... */
3653 if (val)
3654 val = MII_TG3_DSP_TAP26_ALNOKO |
3655 MII_TG3_DSP_TAP26_RMRXSTO |
3656 MII_TG3_DSP_TAP26_OPCSINPT;
3657 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
3658 /* Fall through */
3659 case ASIC_REV_5720:
3660 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
3661 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
3662 MII_TG3_DSP_CH34TP2_HIBW01);
3663 }
Matt Carlson52b02d02010-10-14 10:37:41 +00003664
Matt Carlson42b64a42011-05-19 12:12:49 +00003665 err2 = TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
3666 if (!err)
3667 err = err2;
3668 }
3669
3670done:
3671 return err;
3672}
3673
3674static void tg3_phy_copper_begin(struct tg3 *tp)
3675{
3676 u32 new_adv;
3677 int i;
3678
3679 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
3680 new_adv = ADVERTISED_10baseT_Half |
3681 ADVERTISED_10baseT_Full;
3682 if (tg3_flag(tp, WOL_SPEED_100MB))
3683 new_adv |= ADVERTISED_100baseT_Half |
3684 ADVERTISED_100baseT_Full;
3685
3686 tg3_phy_autoneg_cfg(tp, new_adv,
3687 FLOW_CTRL_TX | FLOW_CTRL_RX);
3688 } else if (tp->link_config.speed == SPEED_INVALID) {
3689 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
3690 tp->link_config.advertising &=
3691 ~(ADVERTISED_1000baseT_Half |
3692 ADVERTISED_1000baseT_Full);
3693
3694 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
3695 tp->link_config.flowctrl);
3696 } else {
3697 /* Asking for a specific link mode. */
3698 if (tp->link_config.speed == SPEED_1000) {
3699 if (tp->link_config.duplex == DUPLEX_FULL)
3700 new_adv = ADVERTISED_1000baseT_Full;
3701 else
3702 new_adv = ADVERTISED_1000baseT_Half;
3703 } else if (tp->link_config.speed == SPEED_100) {
3704 if (tp->link_config.duplex == DUPLEX_FULL)
3705 new_adv = ADVERTISED_100baseT_Full;
3706 else
3707 new_adv = ADVERTISED_100baseT_Half;
3708 } else {
3709 if (tp->link_config.duplex == DUPLEX_FULL)
3710 new_adv = ADVERTISED_10baseT_Full;
3711 else
3712 new_adv = ADVERTISED_10baseT_Half;
3713 }
3714
3715 tg3_phy_autoneg_cfg(tp, new_adv,
3716 tp->link_config.flowctrl);
Matt Carlson52b02d02010-10-14 10:37:41 +00003717 }
3718
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 if (tp->link_config.autoneg == AUTONEG_DISABLE &&
3720 tp->link_config.speed != SPEED_INVALID) {
3721 u32 bmcr, orig_bmcr;
3722
3723 tp->link_config.active_speed = tp->link_config.speed;
3724 tp->link_config.active_duplex = tp->link_config.duplex;
3725
3726 bmcr = 0;
3727 switch (tp->link_config.speed) {
3728 default:
3729 case SPEED_10:
3730 break;
3731
3732 case SPEED_100:
3733 bmcr |= BMCR_SPEED100;
3734 break;
3735
3736 case SPEED_1000:
Matt Carlson221c5632011-06-13 13:39:01 +00003737 bmcr |= BMCR_SPEED1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003739 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740
3741 if (tp->link_config.duplex == DUPLEX_FULL)
3742 bmcr |= BMCR_FULLDPLX;
3743
3744 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
3745 (bmcr != orig_bmcr)) {
3746 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
3747 for (i = 0; i < 1500; i++) {
3748 u32 tmp;
3749
3750 udelay(10);
3751 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
3752 tg3_readphy(tp, MII_BMSR, &tmp))
3753 continue;
3754 if (!(tmp & BMSR_LSTATUS)) {
3755 udelay(40);
3756 break;
3757 }
3758 }
3759 tg3_writephy(tp, MII_BMCR, bmcr);
3760 udelay(40);
3761 }
3762 } else {
3763 tg3_writephy(tp, MII_BMCR,
3764 BMCR_ANENABLE | BMCR_ANRESTART);
3765 }
3766}
3767
3768static int tg3_init_5401phy_dsp(struct tg3 *tp)
3769{
3770 int err;
3771
3772 /* Turn off tap power management. */
3773 /* Set Extended packet length bit */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003774 err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00003776 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
3777 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
3778 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
3779 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
3780 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781
3782 udelay(40);
3783
3784 return err;
3785}
3786
Michael Chan3600d912006-12-07 00:21:48 -08003787static int tg3_copper_is_advertising_all(struct tg3 *tp, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788{
Michael Chan3600d912006-12-07 00:21:48 -08003789 u32 adv_reg, all_mask = 0;
3790
3791 if (mask & ADVERTISED_10baseT_Half)
3792 all_mask |= ADVERTISE_10HALF;
3793 if (mask & ADVERTISED_10baseT_Full)
3794 all_mask |= ADVERTISE_10FULL;
3795 if (mask & ADVERTISED_100baseT_Half)
3796 all_mask |= ADVERTISE_100HALF;
3797 if (mask & ADVERTISED_100baseT_Full)
3798 all_mask |= ADVERTISE_100FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799
3800 if (tg3_readphy(tp, MII_ADVERTISE, &adv_reg))
3801 return 0;
3802
Matt Carlsonb99d2a52011-08-31 11:44:47 +00003803 if ((adv_reg & ADVERTISE_ALL) != all_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804 return 0;
Matt Carlsonb99d2a52011-08-31 11:44:47 +00003805
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003806 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 u32 tg3_ctrl;
3808
Michael Chan3600d912006-12-07 00:21:48 -08003809 all_mask = 0;
3810 if (mask & ADVERTISED_1000baseT_Half)
3811 all_mask |= ADVERTISE_1000HALF;
3812 if (mask & ADVERTISED_1000baseT_Full)
3813 all_mask |= ADVERTISE_1000FULL;
3814
Matt Carlson221c5632011-06-13 13:39:01 +00003815 if (tg3_readphy(tp, MII_CTRL1000, &tg3_ctrl))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 return 0;
3817
Matt Carlsonb99d2a52011-08-31 11:44:47 +00003818 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL);
3819 if (tg3_ctrl != all_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 return 0;
3821 }
Matt Carlson93a700a2011-08-31 11:44:54 +00003822
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823 return 1;
3824}
3825
Matt Carlsonef167e22007-12-20 20:10:01 -08003826static int tg3_adv_1000T_flowctrl_ok(struct tg3 *tp, u32 *lcladv, u32 *rmtadv)
3827{
3828 u32 curadv, reqadv;
3829
3830 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
3831 return 1;
3832
3833 curadv = *lcladv & (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
3834 reqadv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
3835
3836 if (tp->link_config.active_duplex == DUPLEX_FULL) {
3837 if (curadv != reqadv)
3838 return 0;
3839
Joe Perches63c3a662011-04-26 08:12:10 +00003840 if (tg3_flag(tp, PAUSE_AUTONEG))
Matt Carlsonef167e22007-12-20 20:10:01 -08003841 tg3_readphy(tp, MII_LPA, rmtadv);
3842 } else {
3843 /* Reprogram the advertisement register, even if it
3844 * does not affect the current link. If the link
3845 * gets renegotiated in the future, we can save an
3846 * additional renegotiation cycle by advertising
3847 * it correctly in the first place.
3848 */
3849 if (curadv != reqadv) {
3850 *lcladv &= ~(ADVERTISE_PAUSE_CAP |
3851 ADVERTISE_PAUSE_ASYM);
3852 tg3_writephy(tp, MII_ADVERTISE, *lcladv | reqadv);
3853 }
3854 }
3855
3856 return 1;
3857}
3858
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
3860{
3861 int current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003862 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08003863 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864 u16 current_speed;
3865 u8 current_duplex;
3866 int i, err;
3867
3868 tw32(MAC_EVENT, 0);
3869
3870 tw32_f(MAC_STATUS,
3871 (MAC_STATUS_SYNC_CHANGED |
3872 MAC_STATUS_CFG_CHANGED |
3873 MAC_STATUS_MI_COMPLETION |
3874 MAC_STATUS_LNKSTATE_CHANGED));
3875 udelay(40);
3876
Matt Carlson8ef21422008-05-02 16:47:53 -07003877 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
3878 tw32_f(MAC_MI_MODE,
3879 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
3880 udelay(80);
3881 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003883 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884
3885 /* Some third-party PHYs need to be reset on link going
3886 * down.
3887 */
3888 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3889 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
3890 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
3891 netif_carrier_ok(tp->dev)) {
3892 tg3_readphy(tp, MII_BMSR, &bmsr);
3893 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3894 !(bmsr & BMSR_LSTATUS))
3895 force_reset = 1;
3896 }
3897 if (force_reset)
3898 tg3_phy_reset(tp);
3899
Matt Carlson79eb6902010-02-17 15:17:03 +00003900 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 tg3_readphy(tp, MII_BMSR, &bmsr);
3902 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
Joe Perches63c3a662011-04-26 08:12:10 +00003903 !tg3_flag(tp, INIT_COMPLETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 bmsr = 0;
3905
3906 if (!(bmsr & BMSR_LSTATUS)) {
3907 err = tg3_init_5401phy_dsp(tp);
3908 if (err)
3909 return err;
3910
3911 tg3_readphy(tp, MII_BMSR, &bmsr);
3912 for (i = 0; i < 1000; i++) {
3913 udelay(10);
3914 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3915 (bmsr & BMSR_LSTATUS)) {
3916 udelay(40);
3917 break;
3918 }
3919 }
3920
Matt Carlson79eb6902010-02-17 15:17:03 +00003921 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
3922 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 !(bmsr & BMSR_LSTATUS) &&
3924 tp->link_config.active_speed == SPEED_1000) {
3925 err = tg3_phy_reset(tp);
3926 if (!err)
3927 err = tg3_init_5401phy_dsp(tp);
3928 if (err)
3929 return err;
3930 }
3931 }
3932 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3933 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
3934 /* 5701 {A0,B0} CRC bug workaround */
3935 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00003936 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
3937 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
3938 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 }
3940
3941 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003942 tg3_readphy(tp, MII_TG3_ISTAT, &val);
3943 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003945 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003947 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 tg3_writephy(tp, MII_TG3_IMASK, ~0);
3949
3950 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3951 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3952 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
3953 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3954 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
3955 else
3956 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
3957 }
3958
3959 current_link_up = 0;
3960 current_speed = SPEED_INVALID;
3961 current_duplex = DUPLEX_INVALID;
3962
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003963 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Matt Carlson15ee95c2011-04-20 07:57:40 +00003964 err = tg3_phy_auxctl_read(tp,
3965 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
3966 &val);
3967 if (!err && !(val & (1 << 10))) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003968 tg3_phy_auxctl_write(tp,
3969 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
3970 val | (1 << 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 goto relink;
3972 }
3973 }
3974
3975 bmsr = 0;
3976 for (i = 0; i < 100; i++) {
3977 tg3_readphy(tp, MII_BMSR, &bmsr);
3978 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3979 (bmsr & BMSR_LSTATUS))
3980 break;
3981 udelay(40);
3982 }
3983
3984 if (bmsr & BMSR_LSTATUS) {
3985 u32 aux_stat, bmcr;
3986
3987 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
3988 for (i = 0; i < 2000; i++) {
3989 udelay(10);
3990 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
3991 aux_stat)
3992 break;
3993 }
3994
3995 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
3996 &current_speed,
3997 &current_duplex);
3998
3999 bmcr = 0;
4000 for (i = 0; i < 200; i++) {
4001 tg3_readphy(tp, MII_BMCR, &bmcr);
4002 if (tg3_readphy(tp, MII_BMCR, &bmcr))
4003 continue;
4004 if (bmcr && bmcr != 0x7fff)
4005 break;
4006 udelay(10);
4007 }
4008
Matt Carlsonef167e22007-12-20 20:10:01 -08004009 lcl_adv = 0;
4010 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011
Matt Carlsonef167e22007-12-20 20:10:01 -08004012 tp->link_config.active_speed = current_speed;
4013 tp->link_config.active_duplex = current_duplex;
4014
4015 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4016 if ((bmcr & BMCR_ANENABLE) &&
4017 tg3_copper_is_advertising_all(tp,
4018 tp->link_config.advertising)) {
4019 if (tg3_adv_1000T_flowctrl_ok(tp, &lcl_adv,
4020 &rmt_adv))
4021 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022 }
4023 } else {
4024 if (!(bmcr & BMCR_ANENABLE) &&
4025 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08004026 tp->link_config.duplex == current_duplex &&
4027 tp->link_config.flowctrl ==
4028 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 }
4031 }
4032
Matt Carlsonef167e22007-12-20 20:10:01 -08004033 if (current_link_up == 1 &&
4034 tp->link_config.active_duplex == DUPLEX_FULL)
4035 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 }
4037
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038relink:
Matt Carlson80096062010-08-02 11:26:06 +00004039 if (current_link_up == 0 || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 tg3_phy_copper_begin(tp);
4041
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004042 tg3_readphy(tp, MII_BMSR, &bmsr);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00004043 if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
4044 (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 current_link_up = 1;
4046 }
4047
4048 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
4049 if (current_link_up == 1) {
4050 if (tp->link_config.active_speed == SPEED_100 ||
4051 tp->link_config.active_speed == SPEED_10)
4052 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4053 else
4054 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004055 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00004056 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4057 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4059
4060 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4061 if (tp->link_config.active_duplex == DUPLEX_HALF)
4062 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4063
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004065 if (current_link_up == 1 &&
4066 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004068 else
4069 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 }
4071
4072 /* ??? Without this setting Netgear GA302T PHY does not
4073 * ??? send/receive packets...
4074 */
Matt Carlson79eb6902010-02-17 15:17:03 +00004075 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
4077 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
4078 tw32_f(MAC_MI_MODE, tp->mi_mode);
4079 udelay(80);
4080 }
4081
4082 tw32_f(MAC_MODE, tp->mac_mode);
4083 udelay(40);
4084
Matt Carlson52b02d02010-10-14 10:37:41 +00004085 tg3_phy_eee_adjust(tp, current_link_up);
4086
Joe Perches63c3a662011-04-26 08:12:10 +00004087 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 /* Polled via timer. */
4089 tw32_f(MAC_EVENT, 0);
4090 } else {
4091 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4092 }
4093 udelay(40);
4094
4095 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
4096 current_link_up == 1 &&
4097 tp->link_config.active_speed == SPEED_1000 &&
Joe Perches63c3a662011-04-26 08:12:10 +00004098 (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 udelay(120);
4100 tw32_f(MAC_STATUS,
4101 (MAC_STATUS_SYNC_CHANGED |
4102 MAC_STATUS_CFG_CHANGED));
4103 udelay(40);
4104 tg3_write_mem(tp,
4105 NIC_SRAM_FIRMWARE_MBOX,
4106 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
4107 }
4108
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004109 /* Prevent send BD corruption. */
Joe Perches63c3a662011-04-26 08:12:10 +00004110 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004111 u16 oldlnkctl, newlnkctl;
4112
4113 pci_read_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00004114 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004115 &oldlnkctl);
4116 if (tp->link_config.active_speed == SPEED_100 ||
4117 tp->link_config.active_speed == SPEED_10)
4118 newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
4119 else
4120 newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
4121 if (newlnkctl != oldlnkctl)
4122 pci_write_config_word(tp->pdev,
Matt Carlson93a700a2011-08-31 11:44:54 +00004123 pci_pcie_cap(tp->pdev) +
4124 PCI_EXP_LNKCTL, newlnkctl);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004125 }
4126
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 if (current_link_up != netif_carrier_ok(tp->dev)) {
4128 if (current_link_up)
4129 netif_carrier_on(tp->dev);
4130 else
4131 netif_carrier_off(tp->dev);
4132 tg3_link_report(tp);
4133 }
4134
4135 return 0;
4136}
4137
4138struct tg3_fiber_aneginfo {
4139 int state;
4140#define ANEG_STATE_UNKNOWN 0
4141#define ANEG_STATE_AN_ENABLE 1
4142#define ANEG_STATE_RESTART_INIT 2
4143#define ANEG_STATE_RESTART 3
4144#define ANEG_STATE_DISABLE_LINK_OK 4
4145#define ANEG_STATE_ABILITY_DETECT_INIT 5
4146#define ANEG_STATE_ABILITY_DETECT 6
4147#define ANEG_STATE_ACK_DETECT_INIT 7
4148#define ANEG_STATE_ACK_DETECT 8
4149#define ANEG_STATE_COMPLETE_ACK_INIT 9
4150#define ANEG_STATE_COMPLETE_ACK 10
4151#define ANEG_STATE_IDLE_DETECT_INIT 11
4152#define ANEG_STATE_IDLE_DETECT 12
4153#define ANEG_STATE_LINK_OK 13
4154#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
4155#define ANEG_STATE_NEXT_PAGE_WAIT 15
4156
4157 u32 flags;
4158#define MR_AN_ENABLE 0x00000001
4159#define MR_RESTART_AN 0x00000002
4160#define MR_AN_COMPLETE 0x00000004
4161#define MR_PAGE_RX 0x00000008
4162#define MR_NP_LOADED 0x00000010
4163#define MR_TOGGLE_TX 0x00000020
4164#define MR_LP_ADV_FULL_DUPLEX 0x00000040
4165#define MR_LP_ADV_HALF_DUPLEX 0x00000080
4166#define MR_LP_ADV_SYM_PAUSE 0x00000100
4167#define MR_LP_ADV_ASYM_PAUSE 0x00000200
4168#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
4169#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
4170#define MR_LP_ADV_NEXT_PAGE 0x00001000
4171#define MR_TOGGLE_RX 0x00002000
4172#define MR_NP_RX 0x00004000
4173
4174#define MR_LINK_OK 0x80000000
4175
4176 unsigned long link_time, cur_time;
4177
4178 u32 ability_match_cfg;
4179 int ability_match_count;
4180
4181 char ability_match, idle_match, ack_match;
4182
4183 u32 txconfig, rxconfig;
4184#define ANEG_CFG_NP 0x00000080
4185#define ANEG_CFG_ACK 0x00000040
4186#define ANEG_CFG_RF2 0x00000020
4187#define ANEG_CFG_RF1 0x00000010
4188#define ANEG_CFG_PS2 0x00000001
4189#define ANEG_CFG_PS1 0x00008000
4190#define ANEG_CFG_HD 0x00004000
4191#define ANEG_CFG_FD 0x00002000
4192#define ANEG_CFG_INVAL 0x00001f06
4193
4194};
4195#define ANEG_OK 0
4196#define ANEG_DONE 1
4197#define ANEG_TIMER_ENAB 2
4198#define ANEG_FAILED -1
4199
4200#define ANEG_STATE_SETTLE_TIME 10000
4201
4202static int tg3_fiber_aneg_smachine(struct tg3 *tp,
4203 struct tg3_fiber_aneginfo *ap)
4204{
Matt Carlson5be73b42007-12-20 20:09:29 -08004205 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 unsigned long delta;
4207 u32 rx_cfg_reg;
4208 int ret;
4209
4210 if (ap->state == ANEG_STATE_UNKNOWN) {
4211 ap->rxconfig = 0;
4212 ap->link_time = 0;
4213 ap->cur_time = 0;
4214 ap->ability_match_cfg = 0;
4215 ap->ability_match_count = 0;
4216 ap->ability_match = 0;
4217 ap->idle_match = 0;
4218 ap->ack_match = 0;
4219 }
4220 ap->cur_time++;
4221
4222 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
4223 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
4224
4225 if (rx_cfg_reg != ap->ability_match_cfg) {
4226 ap->ability_match_cfg = rx_cfg_reg;
4227 ap->ability_match = 0;
4228 ap->ability_match_count = 0;
4229 } else {
4230 if (++ap->ability_match_count > 1) {
4231 ap->ability_match = 1;
4232 ap->ability_match_cfg = rx_cfg_reg;
4233 }
4234 }
4235 if (rx_cfg_reg & ANEG_CFG_ACK)
4236 ap->ack_match = 1;
4237 else
4238 ap->ack_match = 0;
4239
4240 ap->idle_match = 0;
4241 } else {
4242 ap->idle_match = 1;
4243 ap->ability_match_cfg = 0;
4244 ap->ability_match_count = 0;
4245 ap->ability_match = 0;
4246 ap->ack_match = 0;
4247
4248 rx_cfg_reg = 0;
4249 }
4250
4251 ap->rxconfig = rx_cfg_reg;
4252 ret = ANEG_OK;
4253
Matt Carlson33f401a2010-04-05 10:19:27 +00004254 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 case ANEG_STATE_UNKNOWN:
4256 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
4257 ap->state = ANEG_STATE_AN_ENABLE;
4258
4259 /* fallthru */
4260 case ANEG_STATE_AN_ENABLE:
4261 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
4262 if (ap->flags & MR_AN_ENABLE) {
4263 ap->link_time = 0;
4264 ap->cur_time = 0;
4265 ap->ability_match_cfg = 0;
4266 ap->ability_match_count = 0;
4267 ap->ability_match = 0;
4268 ap->idle_match = 0;
4269 ap->ack_match = 0;
4270
4271 ap->state = ANEG_STATE_RESTART_INIT;
4272 } else {
4273 ap->state = ANEG_STATE_DISABLE_LINK_OK;
4274 }
4275 break;
4276
4277 case ANEG_STATE_RESTART_INIT:
4278 ap->link_time = ap->cur_time;
4279 ap->flags &= ~(MR_NP_LOADED);
4280 ap->txconfig = 0;
4281 tw32(MAC_TX_AUTO_NEG, 0);
4282 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4283 tw32_f(MAC_MODE, tp->mac_mode);
4284 udelay(40);
4285
4286 ret = ANEG_TIMER_ENAB;
4287 ap->state = ANEG_STATE_RESTART;
4288
4289 /* fallthru */
4290 case ANEG_STATE_RESTART:
4291 delta = ap->cur_time - ap->link_time;
Matt Carlson859a5882010-04-05 10:19:28 +00004292 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a5882010-04-05 10:19:28 +00004294 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 break;
4297
4298 case ANEG_STATE_DISABLE_LINK_OK:
4299 ret = ANEG_DONE;
4300 break;
4301
4302 case ANEG_STATE_ABILITY_DETECT_INIT:
4303 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08004304 ap->txconfig = ANEG_CFG_FD;
4305 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
4306 if (flowctrl & ADVERTISE_1000XPAUSE)
4307 ap->txconfig |= ANEG_CFG_PS1;
4308 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
4309 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
4311 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4312 tw32_f(MAC_MODE, tp->mac_mode);
4313 udelay(40);
4314
4315 ap->state = ANEG_STATE_ABILITY_DETECT;
4316 break;
4317
4318 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a5882010-04-05 10:19:28 +00004319 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 break;
4322
4323 case ANEG_STATE_ACK_DETECT_INIT:
4324 ap->txconfig |= ANEG_CFG_ACK;
4325 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
4326 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4327 tw32_f(MAC_MODE, tp->mac_mode);
4328 udelay(40);
4329
4330 ap->state = ANEG_STATE_ACK_DETECT;
4331
4332 /* fallthru */
4333 case ANEG_STATE_ACK_DETECT:
4334 if (ap->ack_match != 0) {
4335 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
4336 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
4337 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
4338 } else {
4339 ap->state = ANEG_STATE_AN_ENABLE;
4340 }
4341 } else if (ap->ability_match != 0 &&
4342 ap->rxconfig == 0) {
4343 ap->state = ANEG_STATE_AN_ENABLE;
4344 }
4345 break;
4346
4347 case ANEG_STATE_COMPLETE_ACK_INIT:
4348 if (ap->rxconfig & ANEG_CFG_INVAL) {
4349 ret = ANEG_FAILED;
4350 break;
4351 }
4352 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
4353 MR_LP_ADV_HALF_DUPLEX |
4354 MR_LP_ADV_SYM_PAUSE |
4355 MR_LP_ADV_ASYM_PAUSE |
4356 MR_LP_ADV_REMOTE_FAULT1 |
4357 MR_LP_ADV_REMOTE_FAULT2 |
4358 MR_LP_ADV_NEXT_PAGE |
4359 MR_TOGGLE_RX |
4360 MR_NP_RX);
4361 if (ap->rxconfig & ANEG_CFG_FD)
4362 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
4363 if (ap->rxconfig & ANEG_CFG_HD)
4364 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
4365 if (ap->rxconfig & ANEG_CFG_PS1)
4366 ap->flags |= MR_LP_ADV_SYM_PAUSE;
4367 if (ap->rxconfig & ANEG_CFG_PS2)
4368 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
4369 if (ap->rxconfig & ANEG_CFG_RF1)
4370 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
4371 if (ap->rxconfig & ANEG_CFG_RF2)
4372 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
4373 if (ap->rxconfig & ANEG_CFG_NP)
4374 ap->flags |= MR_LP_ADV_NEXT_PAGE;
4375
4376 ap->link_time = ap->cur_time;
4377
4378 ap->flags ^= (MR_TOGGLE_TX);
4379 if (ap->rxconfig & 0x0008)
4380 ap->flags |= MR_TOGGLE_RX;
4381 if (ap->rxconfig & ANEG_CFG_NP)
4382 ap->flags |= MR_NP_RX;
4383 ap->flags |= MR_PAGE_RX;
4384
4385 ap->state = ANEG_STATE_COMPLETE_ACK;
4386 ret = ANEG_TIMER_ENAB;
4387 break;
4388
4389 case ANEG_STATE_COMPLETE_ACK:
4390 if (ap->ability_match != 0 &&
4391 ap->rxconfig == 0) {
4392 ap->state = ANEG_STATE_AN_ENABLE;
4393 break;
4394 }
4395 delta = ap->cur_time - ap->link_time;
4396 if (delta > ANEG_STATE_SETTLE_TIME) {
4397 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
4398 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
4399 } else {
4400 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
4401 !(ap->flags & MR_NP_RX)) {
4402 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
4403 } else {
4404 ret = ANEG_FAILED;
4405 }
4406 }
4407 }
4408 break;
4409
4410 case ANEG_STATE_IDLE_DETECT_INIT:
4411 ap->link_time = ap->cur_time;
4412 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
4413 tw32_f(MAC_MODE, tp->mac_mode);
4414 udelay(40);
4415
4416 ap->state = ANEG_STATE_IDLE_DETECT;
4417 ret = ANEG_TIMER_ENAB;
4418 break;
4419
4420 case ANEG_STATE_IDLE_DETECT:
4421 if (ap->ability_match != 0 &&
4422 ap->rxconfig == 0) {
4423 ap->state = ANEG_STATE_AN_ENABLE;
4424 break;
4425 }
4426 delta = ap->cur_time - ap->link_time;
4427 if (delta > ANEG_STATE_SETTLE_TIME) {
4428 /* XXX another gem from the Broadcom driver :( */
4429 ap->state = ANEG_STATE_LINK_OK;
4430 }
4431 break;
4432
4433 case ANEG_STATE_LINK_OK:
4434 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
4435 ret = ANEG_DONE;
4436 break;
4437
4438 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
4439 /* ??? unimplemented */
4440 break;
4441
4442 case ANEG_STATE_NEXT_PAGE_WAIT:
4443 /* ??? unimplemented */
4444 break;
4445
4446 default:
4447 ret = ANEG_FAILED;
4448 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450
4451 return ret;
4452}
4453
Matt Carlson5be73b42007-12-20 20:09:29 -08004454static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455{
4456 int res = 0;
4457 struct tg3_fiber_aneginfo aninfo;
4458 int status = ANEG_FAILED;
4459 unsigned int tick;
4460 u32 tmp;
4461
4462 tw32_f(MAC_TX_AUTO_NEG, 0);
4463
4464 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
4465 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
4466 udelay(40);
4467
4468 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
4469 udelay(40);
4470
4471 memset(&aninfo, 0, sizeof(aninfo));
4472 aninfo.flags |= MR_AN_ENABLE;
4473 aninfo.state = ANEG_STATE_UNKNOWN;
4474 aninfo.cur_time = 0;
4475 tick = 0;
4476 while (++tick < 195000) {
4477 status = tg3_fiber_aneg_smachine(tp, &aninfo);
4478 if (status == ANEG_DONE || status == ANEG_FAILED)
4479 break;
4480
4481 udelay(1);
4482 }
4483
4484 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
4485 tw32_f(MAC_MODE, tp->mac_mode);
4486 udelay(40);
4487
Matt Carlson5be73b42007-12-20 20:09:29 -08004488 *txflags = aninfo.txconfig;
4489 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490
4491 if (status == ANEG_DONE &&
4492 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
4493 MR_LP_ADV_FULL_DUPLEX)))
4494 res = 1;
4495
4496 return res;
4497}
4498
4499static void tg3_init_bcm8002(struct tg3 *tp)
4500{
4501 u32 mac_status = tr32(MAC_STATUS);
4502 int i;
4503
4504 /* Reset when initting first time or we have a link. */
Joe Perches63c3a662011-04-26 08:12:10 +00004505 if (tg3_flag(tp, INIT_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 !(mac_status & MAC_STATUS_PCS_SYNCED))
4507 return;
4508
4509 /* Set PLL lock range. */
4510 tg3_writephy(tp, 0x16, 0x8007);
4511
4512 /* SW reset */
4513 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
4514
4515 /* Wait for reset to complete. */
4516 /* XXX schedule_timeout() ... */
4517 for (i = 0; i < 500; i++)
4518 udelay(10);
4519
4520 /* Config mode; select PMA/Ch 1 regs. */
4521 tg3_writephy(tp, 0x10, 0x8411);
4522
4523 /* Enable auto-lock and comdet, select txclk for tx. */
4524 tg3_writephy(tp, 0x11, 0x0a10);
4525
4526 tg3_writephy(tp, 0x18, 0x00a0);
4527 tg3_writephy(tp, 0x16, 0x41ff);
4528
4529 /* Assert and deassert POR. */
4530 tg3_writephy(tp, 0x13, 0x0400);
4531 udelay(40);
4532 tg3_writephy(tp, 0x13, 0x0000);
4533
4534 tg3_writephy(tp, 0x11, 0x0a50);
4535 udelay(40);
4536 tg3_writephy(tp, 0x11, 0x0a10);
4537
4538 /* Wait for signal to stabilize */
4539 /* XXX schedule_timeout() ... */
4540 for (i = 0; i < 15000; i++)
4541 udelay(10);
4542
4543 /* Deselect the channel register so we can read the PHYID
4544 * later.
4545 */
4546 tg3_writephy(tp, 0x10, 0x8011);
4547}
4548
4549static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
4550{
Matt Carlson82cd3d12007-12-20 20:09:00 -08004551 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552 u32 sg_dig_ctrl, sg_dig_status;
4553 u32 serdes_cfg, expected_sg_dig_ctrl;
4554 int workaround, port_a;
4555 int current_link_up;
4556
4557 serdes_cfg = 0;
4558 expected_sg_dig_ctrl = 0;
4559 workaround = 0;
4560 port_a = 1;
4561 current_link_up = 0;
4562
4563 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
4564 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
4565 workaround = 1;
4566 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
4567 port_a = 0;
4568
4569 /* preserve bits 0-11,13,14 for signal pre-emphasis */
4570 /* preserve bits 20-23 for voltage regulator */
4571 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
4572 }
4573
4574 sg_dig_ctrl = tr32(SG_DIG_CTRL);
4575
4576 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004577 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578 if (workaround) {
4579 u32 val = serdes_cfg;
4580
4581 if (port_a)
4582 val |= 0xc010000;
4583 else
4584 val |= 0x4010000;
4585 tw32_f(MAC_SERDES_CFG, val);
4586 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004587
4588 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589 }
4590 if (mac_status & MAC_STATUS_PCS_SYNCED) {
4591 tg3_setup_flow_control(tp, 0, 0);
4592 current_link_up = 1;
4593 }
4594 goto out;
4595 }
4596
4597 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004598 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599
Matt Carlson82cd3d12007-12-20 20:09:00 -08004600 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
4601 if (flowctrl & ADVERTISE_1000XPAUSE)
4602 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
4603 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
4604 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605
4606 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004607 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07004608 tp->serdes_counter &&
4609 ((mac_status & (MAC_STATUS_PCS_SYNCED |
4610 MAC_STATUS_RCVD_CFG)) ==
4611 MAC_STATUS_PCS_SYNCED)) {
4612 tp->serdes_counter--;
4613 current_link_up = 1;
4614 goto out;
4615 }
4616restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 if (workaround)
4618 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004619 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620 udelay(5);
4621 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
4622
Michael Chan3d3ebe72006-09-27 15:59:15 -07004623 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004624 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
4626 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07004627 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628 mac_status = tr32(MAC_STATUS);
4629
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004630 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08004632 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633
Matt Carlson82cd3d12007-12-20 20:09:00 -08004634 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
4635 local_adv |= ADVERTISE_1000XPAUSE;
4636 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
4637 local_adv |= ADVERTISE_1000XPSE_ASYM;
4638
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004639 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08004640 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004641 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08004642 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643
4644 tg3_setup_flow_control(tp, local_adv, remote_adv);
4645 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004646 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004647 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004648 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07004649 if (tp->serdes_counter)
4650 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 else {
4652 if (workaround) {
4653 u32 val = serdes_cfg;
4654
4655 if (port_a)
4656 val |= 0xc010000;
4657 else
4658 val |= 0x4010000;
4659
4660 tw32_f(MAC_SERDES_CFG, val);
4661 }
4662
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004663 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 udelay(40);
4665
4666 /* Link parallel detection - link is up */
4667 /* only if we have PCS_SYNC and not */
4668 /* receiving config code words */
4669 mac_status = tr32(MAC_STATUS);
4670 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
4671 !(mac_status & MAC_STATUS_RCVD_CFG)) {
4672 tg3_setup_flow_control(tp, 0, 0);
4673 current_link_up = 1;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004674 tp->phy_flags |=
4675 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004676 tp->serdes_counter =
4677 SERDES_PARALLEL_DET_TIMEOUT;
4678 } else
4679 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680 }
4681 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07004682 } else {
4683 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004684 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685 }
4686
4687out:
4688 return current_link_up;
4689}
4690
4691static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
4692{
4693 int current_link_up = 0;
4694
Michael Chan5cf64b82007-05-05 12:11:21 -07004695 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697
4698 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08004699 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004701
Matt Carlson5be73b42007-12-20 20:09:29 -08004702 if (fiber_autoneg(tp, &txflags, &rxflags)) {
4703 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704
Matt Carlson5be73b42007-12-20 20:09:29 -08004705 if (txflags & ANEG_CFG_PS1)
4706 local_adv |= ADVERTISE_1000XPAUSE;
4707 if (txflags & ANEG_CFG_PS2)
4708 local_adv |= ADVERTISE_1000XPSE_ASYM;
4709
4710 if (rxflags & MR_LP_ADV_SYM_PAUSE)
4711 remote_adv |= LPA_1000XPAUSE;
4712 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
4713 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714
4715 tg3_setup_flow_control(tp, local_adv, remote_adv);
4716
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717 current_link_up = 1;
4718 }
4719 for (i = 0; i < 30; i++) {
4720 udelay(20);
4721 tw32_f(MAC_STATUS,
4722 (MAC_STATUS_SYNC_CHANGED |
4723 MAC_STATUS_CFG_CHANGED));
4724 udelay(40);
4725 if ((tr32(MAC_STATUS) &
4726 (MAC_STATUS_SYNC_CHANGED |
4727 MAC_STATUS_CFG_CHANGED)) == 0)
4728 break;
4729 }
4730
4731 mac_status = tr32(MAC_STATUS);
4732 if (current_link_up == 0 &&
4733 (mac_status & MAC_STATUS_PCS_SYNCED) &&
4734 !(mac_status & MAC_STATUS_RCVD_CFG))
4735 current_link_up = 1;
4736 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08004737 tg3_setup_flow_control(tp, 0, 0);
4738
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739 /* Forcing 1000FD link up. */
4740 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741
4742 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
4743 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004744
4745 tw32_f(MAC_MODE, tp->mac_mode);
4746 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747 }
4748
4749out:
4750 return current_link_up;
4751}
4752
4753static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
4754{
4755 u32 orig_pause_cfg;
4756 u16 orig_active_speed;
4757 u8 orig_active_duplex;
4758 u32 mac_status;
4759 int current_link_up;
4760 int i;
4761
Matt Carlson8d018622007-12-20 20:05:44 -08004762 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763 orig_active_speed = tp->link_config.active_speed;
4764 orig_active_duplex = tp->link_config.active_duplex;
4765
Joe Perches63c3a662011-04-26 08:12:10 +00004766 if (!tg3_flag(tp, HW_AUTONEG) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767 netif_carrier_ok(tp->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00004768 tg3_flag(tp, INIT_COMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769 mac_status = tr32(MAC_STATUS);
4770 mac_status &= (MAC_STATUS_PCS_SYNCED |
4771 MAC_STATUS_SIGNAL_DET |
4772 MAC_STATUS_CFG_CHANGED |
4773 MAC_STATUS_RCVD_CFG);
4774 if (mac_status == (MAC_STATUS_PCS_SYNCED |
4775 MAC_STATUS_SIGNAL_DET)) {
4776 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4777 MAC_STATUS_CFG_CHANGED));
4778 return 0;
4779 }
4780 }
4781
4782 tw32_f(MAC_TX_AUTO_NEG, 0);
4783
4784 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
4785 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
4786 tw32_f(MAC_MODE, tp->mac_mode);
4787 udelay(40);
4788
Matt Carlson79eb6902010-02-17 15:17:03 +00004789 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790 tg3_init_bcm8002(tp);
4791
4792 /* Enable link change event even when serdes polling. */
4793 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4794 udelay(40);
4795
4796 current_link_up = 0;
4797 mac_status = tr32(MAC_STATUS);
4798
Joe Perches63c3a662011-04-26 08:12:10 +00004799 if (tg3_flag(tp, HW_AUTONEG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
4801 else
4802 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
4803
Matt Carlson898a56f2009-08-28 14:02:40 +00004804 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00004806 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807
4808 for (i = 0; i < 100; i++) {
4809 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4810 MAC_STATUS_CFG_CHANGED));
4811 udelay(5);
4812 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07004813 MAC_STATUS_CFG_CHANGED |
4814 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815 break;
4816 }
4817
4818 mac_status = tr32(MAC_STATUS);
4819 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
4820 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004821 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
4822 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823 tw32_f(MAC_MODE, (tp->mac_mode |
4824 MAC_MODE_SEND_CONFIGS));
4825 udelay(1);
4826 tw32_f(MAC_MODE, tp->mac_mode);
4827 }
4828 }
4829
4830 if (current_link_up == 1) {
4831 tp->link_config.active_speed = SPEED_1000;
4832 tp->link_config.active_duplex = DUPLEX_FULL;
4833 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4834 LED_CTRL_LNKLED_OVERRIDE |
4835 LED_CTRL_1000MBPS_ON));
4836 } else {
4837 tp->link_config.active_speed = SPEED_INVALID;
4838 tp->link_config.active_duplex = DUPLEX_INVALID;
4839 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4840 LED_CTRL_LNKLED_OVERRIDE |
4841 LED_CTRL_TRAFFIC_OVERRIDE));
4842 }
4843
4844 if (current_link_up != netif_carrier_ok(tp->dev)) {
4845 if (current_link_up)
4846 netif_carrier_on(tp->dev);
4847 else
4848 netif_carrier_off(tp->dev);
4849 tg3_link_report(tp);
4850 } else {
Matt Carlson8d018622007-12-20 20:05:44 -08004851 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852 if (orig_pause_cfg != now_pause_cfg ||
4853 orig_active_speed != tp->link_config.active_speed ||
4854 orig_active_duplex != tp->link_config.active_duplex)
4855 tg3_link_report(tp);
4856 }
4857
4858 return 0;
4859}
4860
Michael Chan747e8f82005-07-25 12:33:22 -07004861static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
4862{
4863 int current_link_up, err = 0;
4864 u32 bmsr, bmcr;
4865 u16 current_speed;
4866 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08004867 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07004868
4869 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4870 tw32_f(MAC_MODE, tp->mac_mode);
4871 udelay(40);
4872
4873 tw32(MAC_EVENT, 0);
4874
4875 tw32_f(MAC_STATUS,
4876 (MAC_STATUS_SYNC_CHANGED |
4877 MAC_STATUS_CFG_CHANGED |
4878 MAC_STATUS_MI_COMPLETION |
4879 MAC_STATUS_LNKSTATE_CHANGED));
4880 udelay(40);
4881
4882 if (force_reset)
4883 tg3_phy_reset(tp);
4884
4885 current_link_up = 0;
4886 current_speed = SPEED_INVALID;
4887 current_duplex = DUPLEX_INVALID;
4888
4889 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4890 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004891 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
4892 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4893 bmsr |= BMSR_LSTATUS;
4894 else
4895 bmsr &= ~BMSR_LSTATUS;
4896 }
Michael Chan747e8f82005-07-25 12:33:22 -07004897
4898 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
4899
4900 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004901 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004902 /* do nothing, just check for link up at the end */
4903 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4904 u32 adv, new_adv;
4905
4906 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4907 new_adv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
4908 ADVERTISE_1000XPAUSE |
4909 ADVERTISE_1000XPSE_ASYM |
4910 ADVERTISE_SLCT);
4911
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004912 new_adv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Michael Chan747e8f82005-07-25 12:33:22 -07004913
4914 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
4915 new_adv |= ADVERTISE_1000XHALF;
4916 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
4917 new_adv |= ADVERTISE_1000XFULL;
4918
4919 if ((new_adv != adv) || !(bmcr & BMCR_ANENABLE)) {
4920 tg3_writephy(tp, MII_ADVERTISE, new_adv);
4921 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
4922 tg3_writephy(tp, MII_BMCR, bmcr);
4923
4924 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07004925 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004926 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004927
4928 return err;
4929 }
4930 } else {
4931 u32 new_bmcr;
4932
4933 bmcr &= ~BMCR_SPEED1000;
4934 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
4935
4936 if (tp->link_config.duplex == DUPLEX_FULL)
4937 new_bmcr |= BMCR_FULLDPLX;
4938
4939 if (new_bmcr != bmcr) {
4940 /* BMCR_SPEED1000 is a reserved bit that needs
4941 * to be set on write.
4942 */
4943 new_bmcr |= BMCR_SPEED1000;
4944
4945 /* Force a linkdown */
4946 if (netif_carrier_ok(tp->dev)) {
4947 u32 adv;
4948
4949 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4950 adv &= ~(ADVERTISE_1000XFULL |
4951 ADVERTISE_1000XHALF |
4952 ADVERTISE_SLCT);
4953 tg3_writephy(tp, MII_ADVERTISE, adv);
4954 tg3_writephy(tp, MII_BMCR, bmcr |
4955 BMCR_ANRESTART |
4956 BMCR_ANENABLE);
4957 udelay(10);
4958 netif_carrier_off(tp->dev);
4959 }
4960 tg3_writephy(tp, MII_BMCR, new_bmcr);
4961 bmcr = new_bmcr;
4962 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4963 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004964 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
4965 ASIC_REV_5714) {
4966 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4967 bmsr |= BMSR_LSTATUS;
4968 else
4969 bmsr &= ~BMSR_LSTATUS;
4970 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004971 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004972 }
4973 }
4974
4975 if (bmsr & BMSR_LSTATUS) {
4976 current_speed = SPEED_1000;
4977 current_link_up = 1;
4978 if (bmcr & BMCR_FULLDPLX)
4979 current_duplex = DUPLEX_FULL;
4980 else
4981 current_duplex = DUPLEX_HALF;
4982
Matt Carlsonef167e22007-12-20 20:10:01 -08004983 local_adv = 0;
4984 remote_adv = 0;
4985
Michael Chan747e8f82005-07-25 12:33:22 -07004986 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08004987 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07004988
4989 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
4990 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
4991 common = local_adv & remote_adv;
4992 if (common & (ADVERTISE_1000XHALF |
4993 ADVERTISE_1000XFULL)) {
4994 if (common & ADVERTISE_1000XFULL)
4995 current_duplex = DUPLEX_FULL;
4996 else
4997 current_duplex = DUPLEX_HALF;
Joe Perches63c3a662011-04-26 08:12:10 +00004998 } else if (!tg3_flag(tp, 5780_CLASS)) {
Matt Carlson57d8b882010-06-05 17:24:35 +00004999 /* Link is up via parallel detect */
Matt Carlson859a5882010-04-05 10:19:28 +00005000 } else {
Michael Chan747e8f82005-07-25 12:33:22 -07005001 current_link_up = 0;
Matt Carlson859a5882010-04-05 10:19:28 +00005002 }
Michael Chan747e8f82005-07-25 12:33:22 -07005003 }
5004 }
5005
Matt Carlsonef167e22007-12-20 20:10:01 -08005006 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
5007 tg3_setup_flow_control(tp, local_adv, remote_adv);
5008
Michael Chan747e8f82005-07-25 12:33:22 -07005009 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
5010 if (tp->link_config.active_duplex == DUPLEX_HALF)
5011 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
5012
5013 tw32_f(MAC_MODE, tp->mac_mode);
5014 udelay(40);
5015
5016 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5017
5018 tp->link_config.active_speed = current_speed;
5019 tp->link_config.active_duplex = current_duplex;
5020
5021 if (current_link_up != netif_carrier_ok(tp->dev)) {
5022 if (current_link_up)
5023 netif_carrier_on(tp->dev);
5024 else {
5025 netif_carrier_off(tp->dev);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005026 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005027 }
5028 tg3_link_report(tp);
5029 }
5030 return err;
5031}
5032
5033static void tg3_serdes_parallel_detect(struct tg3 *tp)
5034{
Michael Chan3d3ebe72006-09-27 15:59:15 -07005035 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07005036 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07005037 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07005038 return;
5039 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005040
Michael Chan747e8f82005-07-25 12:33:22 -07005041 if (!netif_carrier_ok(tp->dev) &&
5042 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
5043 u32 bmcr;
5044
5045 tg3_readphy(tp, MII_BMCR, &bmcr);
5046 if (bmcr & BMCR_ANENABLE) {
5047 u32 phy1, phy2;
5048
5049 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005050 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
5051 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07005052
5053 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005054 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5055 MII_TG3_DSP_EXP1_INT_STAT);
5056 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
5057 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005058
5059 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
5060 /* We have signal detect and not receiving
5061 * config code words, link is up by parallel
5062 * detection.
5063 */
5064
5065 bmcr &= ~BMCR_ANENABLE;
5066 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
5067 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005068 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005069 }
5070 }
Matt Carlson859a5882010-04-05 10:19:28 +00005071 } else if (netif_carrier_ok(tp->dev) &&
5072 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005073 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005074 u32 phy2;
5075
5076 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005077 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5078 MII_TG3_DSP_EXP1_INT_STAT);
5079 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005080 if (phy2 & 0x20) {
5081 u32 bmcr;
5082
5083 /* Config code words received, turn on autoneg. */
5084 tg3_readphy(tp, MII_BMCR, &bmcr);
5085 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
5086
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005087 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005088
5089 }
5090 }
5091}
5092
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093static int tg3_setup_phy(struct tg3 *tp, int force_reset)
5094{
Matt Carlsonf2096f92011-04-05 14:22:48 +00005095 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096 int err;
5097
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005098 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005100 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07005101 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a5882010-04-05 10:19:28 +00005102 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104
Matt Carlsonbcb37f62008-11-03 16:52:09 -08005105 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00005106 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08005107
5108 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
5109 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
5110 scale = 65;
5111 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
5112 scale = 6;
5113 else
5114 scale = 12;
5115
5116 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
5117 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
5118 tw32(GRC_MISC_CFG, val);
5119 }
5120
Matt Carlsonf2096f92011-04-05 14:22:48 +00005121 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
5122 (6 << TX_LENGTHS_IPG_SHIFT);
5123 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
5124 val |= tr32(MAC_TX_LENGTHS) &
5125 (TX_LENGTHS_JMB_FRM_LEN_MSK |
5126 TX_LENGTHS_CNT_DWN_VAL_MSK);
5127
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128 if (tp->link_config.active_speed == SPEED_1000 &&
5129 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00005130 tw32(MAC_TX_LENGTHS, val |
5131 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00005133 tw32(MAC_TX_LENGTHS, val |
5134 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135
Joe Perches63c3a662011-04-26 08:12:10 +00005136 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137 if (netif_carrier_ok(tp->dev)) {
5138 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07005139 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140 } else {
5141 tw32(HOSTCC_STAT_COAL_TICKS, 0);
5142 }
5143 }
5144
Joe Perches63c3a662011-04-26 08:12:10 +00005145 if (tg3_flag(tp, ASPM_WORKAROUND)) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00005146 val = tr32(PCIE_PWR_MGMT_THRESH);
Matt Carlson8ed5d972007-05-07 00:25:49 -07005147 if (!netif_carrier_ok(tp->dev))
5148 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
5149 tp->pwrmgmt_thresh;
5150 else
5151 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
5152 tw32(PCIE_PWR_MGMT_THRESH, val);
5153 }
5154
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155 return err;
5156}
5157
Matt Carlson66cfd1b2010-09-30 10:34:30 +00005158static inline int tg3_irq_sync(struct tg3 *tp)
5159{
5160 return tp->irq_sync;
5161}
5162
Matt Carlson97bd8e42011-04-13 11:05:04 +00005163static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
5164{
5165 int i;
5166
5167 dst = (u32 *)((u8 *)dst + off);
5168 for (i = 0; i < len; i += sizeof(u32))
5169 *dst++ = tr32(off + i);
5170}
5171
5172static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
5173{
5174 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
5175 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
5176 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
5177 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
5178 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
5179 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
5180 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
5181 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
5182 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
5183 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
5184 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
5185 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
5186 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
5187 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
5188 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
5189 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
5190 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
5191 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
5192 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
5193
Joe Perches63c3a662011-04-26 08:12:10 +00005194 if (tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson97bd8e42011-04-13 11:05:04 +00005195 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
5196
5197 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
5198 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
5199 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
5200 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
5201 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
5202 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
5203 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
5204 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
5205
Joe Perches63c3a662011-04-26 08:12:10 +00005206 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00005207 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
5208 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
5209 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
5210 }
5211
5212 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
5213 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
5214 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
5215 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
5216 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
5217
Joe Perches63c3a662011-04-26 08:12:10 +00005218 if (tg3_flag(tp, NVRAM))
Matt Carlson97bd8e42011-04-13 11:05:04 +00005219 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
5220}
5221
5222static void tg3_dump_state(struct tg3 *tp)
5223{
5224 int i;
5225 u32 *regs;
5226
5227 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
5228 if (!regs) {
5229 netdev_err(tp->dev, "Failed allocating register dump buffer\n");
5230 return;
5231 }
5232
Joe Perches63c3a662011-04-26 08:12:10 +00005233 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00005234 /* Read up to but not including private PCI registers */
5235 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
5236 regs[i / sizeof(u32)] = tr32(i);
5237 } else
5238 tg3_dump_legacy_regs(tp, regs);
5239
5240 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
5241 if (!regs[i + 0] && !regs[i + 1] &&
5242 !regs[i + 2] && !regs[i + 3])
5243 continue;
5244
5245 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
5246 i * 4,
5247 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
5248 }
5249
5250 kfree(regs);
5251
5252 for (i = 0; i < tp->irq_cnt; i++) {
5253 struct tg3_napi *tnapi = &tp->napi[i];
5254
5255 /* SW status block */
5256 netdev_err(tp->dev,
5257 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
5258 i,
5259 tnapi->hw_status->status,
5260 tnapi->hw_status->status_tag,
5261 tnapi->hw_status->rx_jumbo_consumer,
5262 tnapi->hw_status->rx_consumer,
5263 tnapi->hw_status->rx_mini_consumer,
5264 tnapi->hw_status->idx[0].rx_producer,
5265 tnapi->hw_status->idx[0].tx_consumer);
5266
5267 netdev_err(tp->dev,
5268 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
5269 i,
5270 tnapi->last_tag, tnapi->last_irq_tag,
5271 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
5272 tnapi->rx_rcb_ptr,
5273 tnapi->prodring.rx_std_prod_idx,
5274 tnapi->prodring.rx_std_cons_idx,
5275 tnapi->prodring.rx_jmb_prod_idx,
5276 tnapi->prodring.rx_jmb_cons_idx);
5277 }
5278}
5279
Michael Chandf3e6542006-05-26 17:48:07 -07005280/* This is called whenever we suspect that the system chipset is re-
5281 * ordering the sequence of MMIO to the tx send mailbox. The symptom
5282 * is bogus tx completions. We try to recover by setting the
5283 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
5284 * in the workqueue.
5285 */
5286static void tg3_tx_recover(struct tg3 *tp)
5287{
Joe Perches63c3a662011-04-26 08:12:10 +00005288 BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
Michael Chandf3e6542006-05-26 17:48:07 -07005289 tp->write32_tx_mbox == tg3_write_indirect_mbox);
5290
Matt Carlson5129c3a2010-04-05 10:19:23 +00005291 netdev_warn(tp->dev,
5292 "The system may be re-ordering memory-mapped I/O "
5293 "cycles to the network device, attempting to recover. "
5294 "Please report the problem to the driver maintainer "
5295 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07005296
5297 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00005298 tg3_flag_set(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07005299 spin_unlock(&tp->lock);
5300}
5301
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005302static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07005303{
Matt Carlsonf65aac12010-08-02 11:26:03 +00005304 /* Tell compiler to fetch tx indices from memory. */
5305 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005306 return tnapi->tx_pending -
5307 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07005308}
5309
Linus Torvalds1da177e2005-04-16 15:20:36 -07005310/* Tigon3 never reports partial packet sends. So we do not
5311 * need special logic to handle SKBs that have not had all
5312 * of their frags sent yet, like SunGEM does.
5313 */
Matt Carlson17375d22009-08-28 14:02:18 +00005314static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005315{
Matt Carlson17375d22009-08-28 14:02:18 +00005316 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005317 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005318 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005319 struct netdev_queue *txq;
5320 int index = tnapi - tp->napi;
5321
Joe Perches63c3a662011-04-26 08:12:10 +00005322 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005323 index--;
5324
5325 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326
5327 while (sw_idx != hw_idx) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00005328 struct tg3_tx_ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07005330 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331
Michael Chandf3e6542006-05-26 17:48:07 -07005332 if (unlikely(skb == NULL)) {
5333 tg3_tx_recover(tp);
5334 return;
5335 }
5336
Alexander Duyckf4188d82009-12-02 16:48:38 +00005337 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005338 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005339 skb_headlen(skb),
5340 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341
5342 ri->skb = NULL;
5343
Matt Carlsone01ee142011-07-27 14:20:50 +00005344 while (ri->fragmented) {
5345 ri->fragmented = false;
5346 sw_idx = NEXT_TX(sw_idx);
5347 ri = &tnapi->tx_buffers[sw_idx];
5348 }
5349
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350 sw_idx = NEXT_TX(sw_idx);
5351
5352 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005353 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07005354 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
5355 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005356
5357 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005358 dma_unmap_addr(ri, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00005359 skb_frag_size(&skb_shinfo(skb)->frags[i]),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005360 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00005361
5362 while (ri->fragmented) {
5363 ri->fragmented = false;
5364 sw_idx = NEXT_TX(sw_idx);
5365 ri = &tnapi->tx_buffers[sw_idx];
5366 }
5367
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368 sw_idx = NEXT_TX(sw_idx);
5369 }
5370
David S. Millerf47c11e2005-06-24 20:18:35 -07005371 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07005372
5373 if (unlikely(tx_bug)) {
5374 tg3_tx_recover(tp);
5375 return;
5376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377 }
5378
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005379 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380
Michael Chan1b2a7202006-08-07 21:46:02 -07005381 /* Need to make the tx_cons update visible to tg3_start_xmit()
5382 * before checking for netif_queue_stopped(). Without the
5383 * memory barrier, there is a small possibility that tg3_start_xmit()
5384 * will miss it and cause the queue to be stopped forever.
5385 */
5386 smp_mb();
5387
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005388 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005389 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005390 __netif_tx_lock(txq, smp_processor_id());
5391 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005392 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005393 netif_tx_wake_queue(txq);
5394 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07005395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396}
5397
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005398static void tg3_rx_skb_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
5399{
5400 if (!ri->skb)
5401 return;
5402
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005403 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005404 map_sz, PCI_DMA_FROMDEVICE);
5405 dev_kfree_skb_any(ri->skb);
5406 ri->skb = NULL;
5407}
5408
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409/* Returns size of skb allocated or < 0 on error.
5410 *
5411 * We only need to fill in the address because the other members
5412 * of the RX descriptor are invariant, see tg3_init_rings.
5413 *
5414 * Note the purposeful assymetry of cpu vs. chip accesses. For
5415 * posting buffers we only dirty the first cache line of the RX
5416 * descriptor (containing the address). Whereas for the RX status
5417 * buffers the cpu only reads the last cacheline of the RX descriptor
5418 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
5419 */
Matt Carlson86b21e52009-11-13 13:03:45 +00005420static int tg3_alloc_rx_skb(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Matt Carlsona3896162009-11-13 13:03:44 +00005421 u32 opaque_key, u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422{
5423 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00005424 struct ring_info *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425 struct sk_buff *skb;
5426 dma_addr_t mapping;
5427 int skb_size, dest_idx;
5428
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429 switch (opaque_key) {
5430 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00005431 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00005432 desc = &tpr->rx_std[dest_idx];
5433 map = &tpr->rx_std_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00005434 skb_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435 break;
5436
5437 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00005438 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00005439 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00005440 map = &tpr->rx_jmb_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00005441 skb_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442 break;
5443
5444 default:
5445 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447
5448 /* Do not overwrite any of the map or rp information
5449 * until we are sure we can commit to a new buffer.
5450 *
5451 * Callers depend upon this behavior and assume that
5452 * we leave everything unchanged if we fail.
5453 */
Matt Carlson81389f52011-08-31 11:44:49 +00005454 skb = netdev_alloc_skb(tp->dev, skb_size + TG3_RX_OFFSET(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455 if (skb == NULL)
5456 return -ENOMEM;
5457
Matt Carlson81389f52011-08-31 11:44:49 +00005458 skb_reserve(skb, TG3_RX_OFFSET(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459
Matt Carlson287be122009-08-28 13:58:46 +00005460 mapping = pci_map_single(tp->pdev, skb->data, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461 PCI_DMA_FROMDEVICE);
Matt Carlsona21771d2009-11-02 14:25:31 +00005462 if (pci_dma_mapping_error(tp->pdev, mapping)) {
5463 dev_kfree_skb(skb);
5464 return -EIO;
5465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466
5467 map->skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005468 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470 desc->addr_hi = ((u64)mapping >> 32);
5471 desc->addr_lo = ((u64)mapping & 0xffffffff);
5472
5473 return skb_size;
5474}
5475
5476/* We only need to move over in the address because the other
5477 * members of the RX descriptor are invariant. See notes above
5478 * tg3_alloc_rx_skb for full details.
5479 */
Matt Carlsona3896162009-11-13 13:03:44 +00005480static void tg3_recycle_rx(struct tg3_napi *tnapi,
5481 struct tg3_rx_prodring_set *dpr,
5482 u32 opaque_key, int src_idx,
5483 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484{
Matt Carlson17375d22009-08-28 14:02:18 +00005485 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
5487 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00005488 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005489 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490
5491 switch (opaque_key) {
5492 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00005493 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00005494 dest_desc = &dpr->rx_std[dest_idx];
5495 dest_map = &dpr->rx_std_buffers[dest_idx];
5496 src_desc = &spr->rx_std[src_idx];
5497 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498 break;
5499
5500 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00005501 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00005502 dest_desc = &dpr->rx_jmb[dest_idx].std;
5503 dest_map = &dpr->rx_jmb_buffers[dest_idx];
5504 src_desc = &spr->rx_jmb[src_idx].std;
5505 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506 break;
5507
5508 default:
5509 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511
5512 dest_map->skb = src_map->skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005513 dma_unmap_addr_set(dest_map, mapping,
5514 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515 dest_desc->addr_hi = src_desc->addr_hi;
5516 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00005517
5518 /* Ensure that the update to the skb happens after the physical
5519 * addresses have been transferred to the new BD location.
5520 */
5521 smp_wmb();
5522
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523 src_map->skb = NULL;
5524}
5525
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526/* The RX ring scheme is composed of multiple rings which post fresh
5527 * buffers to the chip, and one special ring the chip uses to report
5528 * status back to the host.
5529 *
5530 * The special ring reports the status of received packets to the
5531 * host. The chip does not write into the original descriptor the
5532 * RX buffer was obtained from. The chip simply takes the original
5533 * descriptor as provided by the host, updates the status and length
5534 * field, then writes this into the next status ring entry.
5535 *
5536 * Each ring the host uses to post buffers to the chip is described
5537 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
5538 * it is first placed into the on-chip ram. When the packet's length
5539 * is known, it walks down the TG3_BDINFO entries to select the ring.
5540 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
5541 * which is within the range of the new packet's length is chosen.
5542 *
5543 * The "separate ring for rx status" scheme may sound queer, but it makes
5544 * sense from a cache coherency perspective. If only the host writes
5545 * to the buffer post rings, and only the chip writes to the rx status
5546 * rings, then cache lines never move beyond shared-modified state.
5547 * If both the host and chip were to write into the same ring, cache line
5548 * eviction could occur since both entities want it in an exclusive state.
5549 */
Matt Carlson17375d22009-08-28 14:02:18 +00005550static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551{
Matt Carlson17375d22009-08-28 14:02:18 +00005552 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07005553 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00005554 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00005555 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07005556 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00005558 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005560 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561 /*
5562 * We need to order the read of hw_idx and the read of
5563 * the opaque cookie.
5564 */
5565 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566 work_mask = 0;
5567 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00005568 std_prod_idx = tpr->rx_std_prod_idx;
5569 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00005571 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00005572 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573 unsigned int len;
5574 struct sk_buff *skb;
5575 dma_addr_t dma_addr;
5576 u32 opaque_key, desc_idx, *post_ptr;
5577
5578 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
5579 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
5580 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005581 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005582 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00005583 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00005584 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07005585 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005587 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005588 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00005589 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00005590 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00005591 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593
5594 work_mask |= opaque_key;
5595
5596 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
5597 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
5598 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00005599 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600 desc_idx, *post_ptr);
5601 drop_it_no_recycle:
5602 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00005603 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604 goto next_pkt;
5605 }
5606
Matt Carlsonad829262008-11-21 17:16:16 -08005607 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
5608 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609
Matt Carlsond2757fc2010-04-12 06:58:27 +00005610 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611 int skb_size;
5612
Matt Carlson86b21e52009-11-13 13:03:45 +00005613 skb_size = tg3_alloc_rx_skb(tp, tpr, opaque_key,
Matt Carlsonafc081f2009-11-13 13:03:43 +00005614 *post_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615 if (skb_size < 0)
5616 goto drop_it;
5617
Matt Carlson287be122009-08-28 13:58:46 +00005618 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619 PCI_DMA_FROMDEVICE);
5620
Matt Carlson61e800c2010-02-17 15:16:54 +00005621 /* Ensure that the update to the skb happens
5622 * after the usage of the old DMA mapping.
5623 */
5624 smp_wmb();
5625
5626 ri->skb = NULL;
5627
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628 skb_put(skb, len);
5629 } else {
5630 struct sk_buff *copy_skb;
5631
Matt Carlsona3896162009-11-13 13:03:44 +00005632 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005633 desc_idx, *post_ptr);
5634
Matt Carlsonbf933c82011-01-25 15:58:49 +00005635 copy_skb = netdev_alloc_skb(tp->dev, len +
Matt Carlson9dc7a112010-04-12 06:58:28 +00005636 TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637 if (copy_skb == NULL)
5638 goto drop_it_no_recycle;
5639
Matt Carlsonbf933c82011-01-25 15:58:49 +00005640 skb_reserve(copy_skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641 skb_put(copy_skb, len);
5642 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03005643 skb_copy_from_linear_data(skb, copy_skb->data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
5645
5646 /* We'll reuse the original ring buffer. */
5647 skb = copy_skb;
5648 }
5649
Michał Mirosławdc668912011-04-07 03:35:07 +00005650 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
5652 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
5653 >> RXD_TCPCSUM_SHIFT) == 0xffff))
5654 skb->ip_summed = CHECKSUM_UNNECESSARY;
5655 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005656 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657
5658 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00005659
5660 if (len > (tp->dev->mtu + ETH_HLEN) &&
5661 skb->protocol != htons(ETH_P_8021Q)) {
5662 dev_kfree_skb(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00005663 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00005664 }
5665
Matt Carlson9dc7a112010-04-12 06:58:28 +00005666 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00005667 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
5668 __vlan_hwaccel_put_tag(skb,
5669 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00005670
Matt Carlsonbf933c82011-01-25 15:58:49 +00005671 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673 received++;
5674 budget--;
5675
5676next_pkt:
5677 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07005678
5679 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005680 tpr->rx_std_prod_idx = std_prod_idx &
5681 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00005682 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5683 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07005684 work_mask &= ~RXD_OPAQUE_RING_STD;
5685 rx_std_posted = 0;
5686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07005688 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00005689 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07005690
5691 /* Refresh hw_idx to see if there is new work */
5692 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005693 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07005694 rmb();
5695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696 }
5697
5698 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00005699 tnapi->rx_rcb_ptr = sw_idx;
5700 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701
5702 /* Refill RX ring(s). */
Joe Perches63c3a662011-04-26 08:12:10 +00005703 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005704 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005705 tpr->rx_std_prod_idx = std_prod_idx &
5706 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005707 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5708 tpr->rx_std_prod_idx);
5709 }
5710 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005711 tpr->rx_jmb_prod_idx = jmb_prod_idx &
5712 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005713 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5714 tpr->rx_jmb_prod_idx);
5715 }
5716 mmiowb();
5717 } else if (work_mask) {
5718 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
5719 * updated before the producer indices can be updated.
5720 */
5721 smp_wmb();
5722
Matt Carlson2c49a442010-09-30 10:34:35 +00005723 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
5724 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005725
Matt Carlsone4af1af2010-02-12 14:47:05 +00005726 if (tnapi != &tp->napi[1])
5727 napi_schedule(&tp->napi[1].napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729
5730 return received;
5731}
5732
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005733static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735 /* handle link change and other phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00005736 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005737 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
5738
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739 if (sblk->status & SD_STATUS_LINK_CHG) {
5740 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005741 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07005742 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00005743 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsondd477002008-05-25 23:45:58 -07005744 tw32_f(MAC_STATUS,
5745 (MAC_STATUS_SYNC_CHANGED |
5746 MAC_STATUS_CFG_CHANGED |
5747 MAC_STATUS_MI_COMPLETION |
5748 MAC_STATUS_LNKSTATE_CHANGED));
5749 udelay(40);
5750 } else
5751 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07005752 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005753 }
5754 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005755}
5756
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005757static int tg3_rx_prodring_xfer(struct tg3 *tp,
5758 struct tg3_rx_prodring_set *dpr,
5759 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005760{
5761 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005762 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005763
5764 while (1) {
5765 src_prod_idx = spr->rx_std_prod_idx;
5766
5767 /* Make sure updates to the rx_std_buffers[] entries and the
5768 * standard producer index are seen in the correct order.
5769 */
5770 smp_rmb();
5771
5772 if (spr->rx_std_cons_idx == src_prod_idx)
5773 break;
5774
5775 if (spr->rx_std_cons_idx < src_prod_idx)
5776 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
5777 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005778 cpycnt = tp->rx_std_ring_mask + 1 -
5779 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005780
Matt Carlson2c49a442010-09-30 10:34:35 +00005781 cpycnt = min(cpycnt,
5782 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005783
5784 si = spr->rx_std_cons_idx;
5785 di = dpr->rx_std_prod_idx;
5786
Matt Carlsone92967b2010-02-12 14:47:06 +00005787 for (i = di; i < di + cpycnt; i++) {
5788 if (dpr->rx_std_buffers[i].skb) {
5789 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005790 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005791 break;
5792 }
5793 }
5794
5795 if (!cpycnt)
5796 break;
5797
5798 /* Ensure that updates to the rx_std_buffers ring and the
5799 * shadowed hardware producer ring from tg3_recycle_skb() are
5800 * ordered correctly WRT the skb check above.
5801 */
5802 smp_rmb();
5803
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005804 memcpy(&dpr->rx_std_buffers[di],
5805 &spr->rx_std_buffers[si],
5806 cpycnt * sizeof(struct ring_info));
5807
5808 for (i = 0; i < cpycnt; i++, di++, si++) {
5809 struct tg3_rx_buffer_desc *sbd, *dbd;
5810 sbd = &spr->rx_std[si];
5811 dbd = &dpr->rx_std[di];
5812 dbd->addr_hi = sbd->addr_hi;
5813 dbd->addr_lo = sbd->addr_lo;
5814 }
5815
Matt Carlson2c49a442010-09-30 10:34:35 +00005816 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
5817 tp->rx_std_ring_mask;
5818 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
5819 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005820 }
5821
5822 while (1) {
5823 src_prod_idx = spr->rx_jmb_prod_idx;
5824
5825 /* Make sure updates to the rx_jmb_buffers[] entries and
5826 * the jumbo producer index are seen in the correct order.
5827 */
5828 smp_rmb();
5829
5830 if (spr->rx_jmb_cons_idx == src_prod_idx)
5831 break;
5832
5833 if (spr->rx_jmb_cons_idx < src_prod_idx)
5834 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
5835 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005836 cpycnt = tp->rx_jmb_ring_mask + 1 -
5837 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005838
5839 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00005840 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005841
5842 si = spr->rx_jmb_cons_idx;
5843 di = dpr->rx_jmb_prod_idx;
5844
Matt Carlsone92967b2010-02-12 14:47:06 +00005845 for (i = di; i < di + cpycnt; i++) {
5846 if (dpr->rx_jmb_buffers[i].skb) {
5847 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005848 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005849 break;
5850 }
5851 }
5852
5853 if (!cpycnt)
5854 break;
5855
5856 /* Ensure that updates to the rx_jmb_buffers ring and the
5857 * shadowed hardware producer ring from tg3_recycle_skb() are
5858 * ordered correctly WRT the skb check above.
5859 */
5860 smp_rmb();
5861
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005862 memcpy(&dpr->rx_jmb_buffers[di],
5863 &spr->rx_jmb_buffers[si],
5864 cpycnt * sizeof(struct ring_info));
5865
5866 for (i = 0; i < cpycnt; i++, di++, si++) {
5867 struct tg3_rx_buffer_desc *sbd, *dbd;
5868 sbd = &spr->rx_jmb[si].std;
5869 dbd = &dpr->rx_jmb[di].std;
5870 dbd->addr_hi = sbd->addr_hi;
5871 dbd->addr_lo = sbd->addr_lo;
5872 }
5873
Matt Carlson2c49a442010-09-30 10:34:35 +00005874 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
5875 tp->rx_jmb_ring_mask;
5876 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
5877 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005878 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005879
5880 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005881}
5882
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005883static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
5884{
5885 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005886
5887 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005888 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00005889 tg3_tx(tnapi);
Joe Perches63c3a662011-04-26 08:12:10 +00005890 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Michael Chan4fd7ab52007-10-12 01:39:50 -07005891 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892 }
5893
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894 /* run RX thread, within the bounds set by NAPI.
5895 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005896 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07005897 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005898 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00005899 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900
Joe Perches63c3a662011-04-26 08:12:10 +00005901 if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005902 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005903 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00005904 u32 std_prod_idx = dpr->rx_std_prod_idx;
5905 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005906
Matt Carlsone4af1af2010-02-12 14:47:05 +00005907 for (i = 1; i < tp->irq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005908 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00005909 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005910
5911 wmb();
5912
Matt Carlsone4af1af2010-02-12 14:47:05 +00005913 if (std_prod_idx != dpr->rx_std_prod_idx)
5914 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5915 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005916
Matt Carlsone4af1af2010-02-12 14:47:05 +00005917 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
5918 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5919 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005920
5921 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005922
5923 if (err)
5924 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005925 }
5926
David S. Miller6f535762007-10-11 18:08:29 -07005927 return work_done;
5928}
David S. Millerf7383c22005-05-18 22:50:53 -07005929
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005930static int tg3_poll_msix(struct napi_struct *napi, int budget)
5931{
5932 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5933 struct tg3 *tp = tnapi->tp;
5934 int work_done = 0;
5935 struct tg3_hw_status *sblk = tnapi->hw_status;
5936
5937 while (1) {
5938 work_done = tg3_poll_work(tnapi, work_done, budget);
5939
Joe Perches63c3a662011-04-26 08:12:10 +00005940 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005941 goto tx_recovery;
5942
5943 if (unlikely(work_done >= budget))
5944 break;
5945
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005946 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005947 * to tell the hw how much work has been processed,
5948 * so we must read it before checking for more work.
5949 */
5950 tnapi->last_tag = sblk->status_tag;
5951 tnapi->last_irq_tag = tnapi->last_tag;
5952 rmb();
5953
5954 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00005955 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
5956 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005957 napi_complete(napi);
5958 /* Reenable interrupts. */
5959 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
5960 mmiowb();
5961 break;
5962 }
5963 }
5964
5965 return work_done;
5966
5967tx_recovery:
5968 /* work_done is guaranteed to be less than budget. */
5969 napi_complete(napi);
5970 schedule_work(&tp->reset_task);
5971 return work_done;
5972}
5973
Matt Carlsone64de4e2011-04-13 11:05:05 +00005974static void tg3_process_error(struct tg3 *tp)
5975{
5976 u32 val;
5977 bool real_error = false;
5978
Joe Perches63c3a662011-04-26 08:12:10 +00005979 if (tg3_flag(tp, ERROR_PROCESSED))
Matt Carlsone64de4e2011-04-13 11:05:05 +00005980 return;
5981
5982 /* Check Flow Attention register */
5983 val = tr32(HOSTCC_FLOW_ATTN);
5984 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
5985 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
5986 real_error = true;
5987 }
5988
5989 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
5990 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
5991 real_error = true;
5992 }
5993
5994 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
5995 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
5996 real_error = true;
5997 }
5998
5999 if (!real_error)
6000 return;
6001
6002 tg3_dump_state(tp);
6003
Joe Perches63c3a662011-04-26 08:12:10 +00006004 tg3_flag_set(tp, ERROR_PROCESSED);
Matt Carlsone64de4e2011-04-13 11:05:05 +00006005 schedule_work(&tp->reset_task);
6006}
6007
David S. Miller6f535762007-10-11 18:08:29 -07006008static int tg3_poll(struct napi_struct *napi, int budget)
6009{
Matt Carlson8ef04422009-08-28 14:01:37 +00006010 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
6011 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07006012 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00006013 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07006014
6015 while (1) {
Matt Carlsone64de4e2011-04-13 11:05:05 +00006016 if (sblk->status & SD_STATUS_ERROR)
6017 tg3_process_error(tp);
6018
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006019 tg3_poll_link(tp);
6020
Matt Carlson17375d22009-08-28 14:02:18 +00006021 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07006022
Joe Perches63c3a662011-04-26 08:12:10 +00006023 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
David S. Miller6f535762007-10-11 18:08:29 -07006024 goto tx_recovery;
6025
6026 if (unlikely(work_done >= budget))
6027 break;
6028
Joe Perches63c3a662011-04-26 08:12:10 +00006029 if (tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson17375d22009-08-28 14:02:18 +00006030 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07006031 * to tell the hw how much work has been processed,
6032 * so we must read it before checking for more work.
6033 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006034 tnapi->last_tag = sblk->status_tag;
6035 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07006036 rmb();
6037 } else
6038 sblk->status &= ~SD_STATUS_UPDATED;
6039
Matt Carlson17375d22009-08-28 14:02:18 +00006040 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08006041 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00006042 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07006043 break;
6044 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045 }
6046
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006047 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07006048
6049tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07006050 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08006051 napi_complete(napi);
David S. Miller6f535762007-10-11 18:08:29 -07006052 schedule_work(&tp->reset_task);
Michael Chan4fd7ab52007-10-12 01:39:50 -07006053 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006054}
6055
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006056static void tg3_napi_disable(struct tg3 *tp)
6057{
6058 int i;
6059
6060 for (i = tp->irq_cnt - 1; i >= 0; i--)
6061 napi_disable(&tp->napi[i].napi);
6062}
6063
6064static void tg3_napi_enable(struct tg3 *tp)
6065{
6066 int i;
6067
6068 for (i = 0; i < tp->irq_cnt; i++)
6069 napi_enable(&tp->napi[i].napi);
6070}
6071
6072static void tg3_napi_init(struct tg3 *tp)
6073{
6074 int i;
6075
6076 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
6077 for (i = 1; i < tp->irq_cnt; i++)
6078 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
6079}
6080
6081static void tg3_napi_fini(struct tg3 *tp)
6082{
6083 int i;
6084
6085 for (i = 0; i < tp->irq_cnt; i++)
6086 netif_napi_del(&tp->napi[i].napi);
6087}
6088
6089static inline void tg3_netif_stop(struct tg3 *tp)
6090{
6091 tp->dev->trans_start = jiffies; /* prevent tx timeout */
6092 tg3_napi_disable(tp);
6093 netif_tx_disable(tp->dev);
6094}
6095
6096static inline void tg3_netif_start(struct tg3 *tp)
6097{
6098 /* NOTE: unconditional netif_tx_wake_all_queues is only
6099 * appropriate so long as all callers are assured to
6100 * have free tx slots (such as after tg3_init_hw)
6101 */
6102 netif_tx_wake_all_queues(tp->dev);
6103
6104 tg3_napi_enable(tp);
6105 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
6106 tg3_enable_ints(tp);
6107}
6108
David S. Millerf47c11e2005-06-24 20:18:35 -07006109static void tg3_irq_quiesce(struct tg3 *tp)
6110{
Matt Carlson4f125f42009-09-01 12:55:02 +00006111 int i;
6112
David S. Millerf47c11e2005-06-24 20:18:35 -07006113 BUG_ON(tp->irq_sync);
6114
6115 tp->irq_sync = 1;
6116 smp_mb();
6117
Matt Carlson4f125f42009-09-01 12:55:02 +00006118 for (i = 0; i < tp->irq_cnt; i++)
6119 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07006120}
6121
David S. Millerf47c11e2005-06-24 20:18:35 -07006122/* Fully shutdown all tg3 driver activity elsewhere in the system.
6123 * If irq_sync is non-zero, then the IRQ handler must be synchronized
6124 * with as well. Most of the time, this is not necessary except when
6125 * shutting down the device.
6126 */
6127static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
6128{
Michael Chan46966542007-07-11 19:47:19 -07006129 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07006130 if (irq_sync)
6131 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07006132}
6133
6134static inline void tg3_full_unlock(struct tg3 *tp)
6135{
David S. Millerf47c11e2005-06-24 20:18:35 -07006136 spin_unlock_bh(&tp->lock);
6137}
6138
Michael Chanfcfa0a32006-03-20 22:28:41 -08006139/* One-shot MSI handler - Chip automatically disables interrupt
6140 * after sending MSI so driver doesn't have to do it.
6141 */
David Howells7d12e782006-10-05 14:55:46 +01006142static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08006143{
Matt Carlson09943a12009-08-28 14:01:57 +00006144 struct tg3_napi *tnapi = dev_id;
6145 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08006146
Matt Carlson898a56f2009-08-28 14:02:40 +00006147 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006148 if (tnapi->rx_rcb)
6149 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08006150
6151 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00006152 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08006153
6154 return IRQ_HANDLED;
6155}
6156
Michael Chan88b06bc2005-04-21 17:13:25 -07006157/* MSI ISR - No need to check for interrupt sharing and no need to
6158 * flush status block and interrupt mailbox. PCI ordering rules
6159 * guarantee that MSI will arrive after the status block.
6160 */
David Howells7d12e782006-10-05 14:55:46 +01006161static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc2005-04-21 17:13:25 -07006162{
Matt Carlson09943a12009-08-28 14:01:57 +00006163 struct tg3_napi *tnapi = dev_id;
6164 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc2005-04-21 17:13:25 -07006165
Matt Carlson898a56f2009-08-28 14:02:40 +00006166 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006167 if (tnapi->rx_rcb)
6168 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc2005-04-21 17:13:25 -07006169 /*
David S. Millerfac9b832005-05-18 22:46:34 -07006170 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc2005-04-21 17:13:25 -07006171 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07006172 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc2005-04-21 17:13:25 -07006173 * NIC to stop sending us irqs, engaging "in-intr-handler"
6174 * event coalescing.
6175 */
Matt Carlson5b39de92011-08-31 11:44:50 +00006176 tw32_mailbox(tnapi->int_mbox, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07006177 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00006178 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07006179
Michael Chan88b06bc2005-04-21 17:13:25 -07006180 return IRQ_RETVAL(1);
6181}
6182
David Howells7d12e782006-10-05 14:55:46 +01006183static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006184{
Matt Carlson09943a12009-08-28 14:01:57 +00006185 struct tg3_napi *tnapi = dev_id;
6186 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006187 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006188 unsigned int handled = 1;
6189
Linus Torvalds1da177e2005-04-16 15:20:36 -07006190 /* In INTx mode, it is possible for the interrupt to arrive at
6191 * the CPU before the status block posted prior to the interrupt.
6192 * Reading the PCI State register will confirm whether the
6193 * interrupt is ours and will flush the status block.
6194 */
Michael Chand18edcb2007-03-24 20:57:11 -07006195 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
Joe Perches63c3a662011-04-26 08:12:10 +00006196 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07006197 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
6198 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07006199 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07006200 }
Michael Chand18edcb2007-03-24 20:57:11 -07006201 }
6202
6203 /*
6204 * Writing any value to intr-mbox-0 clears PCI INTA# and
6205 * chip-internal interrupt pending events.
6206 * Writing non-zero to intr-mbox-0 additional tells the
6207 * NIC to stop sending us irqs, engaging "in-intr-handler"
6208 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07006209 *
6210 * Flush the mailbox to de-assert the IRQ immediately to prevent
6211 * spurious interrupts. The flush impacts performance but
6212 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07006213 */
Michael Chanc04cb342007-05-07 00:26:15 -07006214 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07006215 if (tg3_irq_sync(tp))
6216 goto out;
6217 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00006218 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00006219 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00006220 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07006221 } else {
6222 /* No work, shared interrupt perhaps? re-enable
6223 * interrupts, and flush that PCI write
6224 */
6225 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
6226 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07006227 }
David S. Millerf47c11e2005-06-24 20:18:35 -07006228out:
David S. Millerfac9b832005-05-18 22:46:34 -07006229 return IRQ_RETVAL(handled);
6230}
6231
David Howells7d12e782006-10-05 14:55:46 +01006232static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07006233{
Matt Carlson09943a12009-08-28 14:01:57 +00006234 struct tg3_napi *tnapi = dev_id;
6235 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006236 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07006237 unsigned int handled = 1;
6238
David S. Millerfac9b832005-05-18 22:46:34 -07006239 /* In INTx mode, it is possible for the interrupt to arrive at
6240 * the CPU before the status block posted prior to the interrupt.
6241 * Reading the PCI State register will confirm whether the
6242 * interrupt is ours and will flush the status block.
6243 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006244 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Joe Perches63c3a662011-04-26 08:12:10 +00006245 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07006246 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
6247 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07006248 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249 }
Michael Chand18edcb2007-03-24 20:57:11 -07006250 }
6251
6252 /*
6253 * writing any value to intr-mbox-0 clears PCI INTA# and
6254 * chip-internal interrupt pending events.
6255 * writing non-zero to intr-mbox-0 additional tells the
6256 * NIC to stop sending us irqs, engaging "in-intr-handler"
6257 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07006258 *
6259 * Flush the mailbox to de-assert the IRQ immediately to prevent
6260 * spurious interrupts. The flush impacts performance but
6261 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07006262 */
Michael Chanc04cb342007-05-07 00:26:15 -07006263 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00006264
6265 /*
6266 * In a shared interrupt configuration, sometimes other devices'
6267 * interrupts will scream. We record the current status tag here
6268 * so that the above check can report that the screaming interrupts
6269 * are unhandled. Eventually they will be silenced.
6270 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006271 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00006272
Michael Chand18edcb2007-03-24 20:57:11 -07006273 if (tg3_irq_sync(tp))
6274 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00006275
Matt Carlson72334482009-08-28 14:03:01 +00006276 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00006277
Matt Carlson09943a12009-08-28 14:01:57 +00006278 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00006279
David S. Millerf47c11e2005-06-24 20:18:35 -07006280out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006281 return IRQ_RETVAL(handled);
6282}
6283
Michael Chan79381092005-04-21 17:13:59 -07006284/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01006285static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07006286{
Matt Carlson09943a12009-08-28 14:01:57 +00006287 struct tg3_napi *tnapi = dev_id;
6288 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006289 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07006290
Michael Chanf9804dd2005-09-27 12:13:10 -07006291 if ((sblk->status & SD_STATUS_UPDATED) ||
6292 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07006293 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07006294 return IRQ_RETVAL(1);
6295 }
6296 return IRQ_RETVAL(0);
6297}
6298
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07006299static int tg3_init_hw(struct tg3 *, int);
Michael Chan944d9802005-05-29 14:57:48 -07006300static int tg3_halt(struct tg3 *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006301
Michael Chanb9ec6c12006-07-25 16:37:27 -07006302/* Restart hardware after configuration changes, self-test, etc.
6303 * Invoked with tp->lock held.
6304 */
6305static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
Eric Dumazet78c61462008-04-24 23:33:06 -07006306 __releases(tp->lock)
6307 __acquires(tp->lock)
Michael Chanb9ec6c12006-07-25 16:37:27 -07006308{
6309 int err;
6310
6311 err = tg3_init_hw(tp, reset_phy);
6312 if (err) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006313 netdev_err(tp->dev,
6314 "Failed to re-initialize device, aborting\n");
Michael Chanb9ec6c12006-07-25 16:37:27 -07006315 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
6316 tg3_full_unlock(tp);
6317 del_timer_sync(&tp->timer);
6318 tp->irq_sync = 0;
Matt Carlsonfed97812009-09-01 13:10:19 +00006319 tg3_napi_enable(tp);
Michael Chanb9ec6c12006-07-25 16:37:27 -07006320 dev_close(tp->dev);
6321 tg3_full_lock(tp, 0);
6322 }
6323 return err;
6324}
6325
Linus Torvalds1da177e2005-04-16 15:20:36 -07006326#ifdef CONFIG_NET_POLL_CONTROLLER
6327static void tg3_poll_controller(struct net_device *dev)
6328{
Matt Carlson4f125f42009-09-01 12:55:02 +00006329 int i;
Michael Chan88b06bc2005-04-21 17:13:25 -07006330 struct tg3 *tp = netdev_priv(dev);
6331
Matt Carlson4f125f42009-09-01 12:55:02 +00006332 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00006333 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006334}
6335#endif
6336
David Howellsc4028952006-11-22 14:57:56 +00006337static void tg3_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006338{
David Howellsc4028952006-11-22 14:57:56 +00006339 struct tg3 *tp = container_of(work, struct tg3, reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006340 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006341 unsigned int restart_timer;
6342
Michael Chan7faa0062006-02-02 17:29:28 -08006343 tg3_full_lock(tp, 0);
Michael Chan7faa0062006-02-02 17:29:28 -08006344
6345 if (!netif_running(tp->dev)) {
Michael Chan7faa0062006-02-02 17:29:28 -08006346 tg3_full_unlock(tp);
6347 return;
6348 }
6349
6350 tg3_full_unlock(tp);
6351
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006352 tg3_phy_stop(tp);
6353
Linus Torvalds1da177e2005-04-16 15:20:36 -07006354 tg3_netif_stop(tp);
6355
David S. Millerf47c11e2005-06-24 20:18:35 -07006356 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006357
Joe Perches63c3a662011-04-26 08:12:10 +00006358 restart_timer = tg3_flag(tp, RESTART_TIMER);
6359 tg3_flag_clear(tp, RESTART_TIMER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006360
Joe Perches63c3a662011-04-26 08:12:10 +00006361 if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
Michael Chandf3e6542006-05-26 17:48:07 -07006362 tp->write32_tx_mbox = tg3_write32_tx_mbox;
6363 tp->write32_rx_mbox = tg3_write_flush_reg32;
Joe Perches63c3a662011-04-26 08:12:10 +00006364 tg3_flag_set(tp, MBOX_WRITE_REORDER);
6365 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07006366 }
6367
Michael Chan944d9802005-05-29 14:57:48 -07006368 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006369 err = tg3_init_hw(tp, 1);
6370 if (err)
Michael Chanb9ec6c12006-07-25 16:37:27 -07006371 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006372
6373 tg3_netif_start(tp);
6374
Linus Torvalds1da177e2005-04-16 15:20:36 -07006375 if (restart_timer)
6376 mod_timer(&tp->timer, jiffies + 1);
Michael Chan7faa0062006-02-02 17:29:28 -08006377
Michael Chanb9ec6c12006-07-25 16:37:27 -07006378out:
Michael Chan7faa0062006-02-02 17:29:28 -08006379 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006380
6381 if (!err)
6382 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006383}
6384
6385static void tg3_tx_timeout(struct net_device *dev)
6386{
6387 struct tg3 *tp = netdev_priv(dev);
6388
Michael Chanb0408752007-02-13 12:18:30 -08006389 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00006390 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00006391 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08006392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006393
6394 schedule_work(&tp->reset_task);
6395}
6396
Michael Chanc58ec932005-09-17 00:46:27 -07006397/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
6398static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
6399{
6400 u32 base = (u32) mapping & 0xffffffff;
6401
Eric Dumazet807540b2010-09-23 05:40:09 +00006402 return (base > 0xffffdcc0) && (base + len + 8 < base);
Michael Chanc58ec932005-09-17 00:46:27 -07006403}
6404
Michael Chan72f2afb2006-03-06 19:28:35 -08006405/* Test for DMA addresses > 40-bit */
6406static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
6407 int len)
6408{
6409#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Joe Perches63c3a662011-04-26 08:12:10 +00006410 if (tg3_flag(tp, 40BIT_DMA_BUG))
Eric Dumazet807540b2010-09-23 05:40:09 +00006411 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08006412 return 0;
6413#else
6414 return 0;
6415#endif
6416}
6417
Matt Carlsond1a3b732011-07-27 14:20:51 +00006418static inline void tg3_tx_set_bd(struct tg3_tx_buffer_desc *txbd,
Matt Carlson92cd3a12011-07-27 14:20:47 +00006419 dma_addr_t mapping, u32 len, u32 flags,
6420 u32 mss, u32 vlan)
Matt Carlson2ffcc982011-05-19 12:12:44 +00006421{
Matt Carlson92cd3a12011-07-27 14:20:47 +00006422 txbd->addr_hi = ((u64) mapping >> 32);
6423 txbd->addr_lo = ((u64) mapping & 0xffffffff);
6424 txbd->len_flags = (len << TXD_LEN_SHIFT) | (flags & 0x0000ffff);
6425 txbd->vlan_tag = (mss << TXD_MSS_SHIFT) | (vlan << TXD_VLAN_TAG_SHIFT);
Matt Carlson2ffcc982011-05-19 12:12:44 +00006426}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006427
Matt Carlson84b67b22011-07-27 14:20:52 +00006428static bool tg3_tx_frag_set(struct tg3_napi *tnapi, u32 *entry, u32 *budget,
Matt Carlsond1a3b732011-07-27 14:20:51 +00006429 dma_addr_t map, u32 len, u32 flags,
6430 u32 mss, u32 vlan)
6431{
6432 struct tg3 *tp = tnapi->tp;
6433 bool hwbug = false;
6434
6435 if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
6436 hwbug = 1;
6437
6438 if (tg3_4g_overflow_test(map, len))
6439 hwbug = 1;
6440
6441 if (tg3_40bit_overflow_test(tp, map, len))
6442 hwbug = 1;
6443
Matt Carlsone31aa982011-07-27 14:20:53 +00006444 if (tg3_flag(tp, 4K_FIFO_LIMIT)) {
6445 u32 tmp_flag = flags & ~TXD_FLAG_END;
6446 while (len > TG3_TX_BD_DMA_MAX) {
6447 u32 frag_len = TG3_TX_BD_DMA_MAX;
6448 len -= TG3_TX_BD_DMA_MAX;
6449
6450 if (len) {
6451 tnapi->tx_buffers[*entry].fragmented = true;
6452 /* Avoid the 8byte DMA problem */
6453 if (len <= 8) {
6454 len += TG3_TX_BD_DMA_MAX / 2;
6455 frag_len = TG3_TX_BD_DMA_MAX / 2;
6456 }
6457 } else
6458 tmp_flag = flags;
6459
6460 if (*budget) {
6461 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6462 frag_len, tmp_flag, mss, vlan);
6463 (*budget)--;
6464 *entry = NEXT_TX(*entry);
6465 } else {
6466 hwbug = 1;
6467 break;
6468 }
6469
6470 map += frag_len;
6471 }
6472
6473 if (len) {
6474 if (*budget) {
6475 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6476 len, flags, mss, vlan);
6477 (*budget)--;
6478 *entry = NEXT_TX(*entry);
6479 } else {
6480 hwbug = 1;
6481 }
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 Carlson9a2e0fb2011-06-02 13:01:39 +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 Carlson41588ba2008-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 Carlson92cd3a12011-07-27 14:20:47 +00006562 base_flags |= TXD_FLAG_END;
6563
Matt Carlson84b67b22011-07-27 14:20:52 +00006564 tnapi->tx_buffers[*entry].skb = new_skb;
6565 dma_unmap_addr_set(&tnapi->tx_buffers[*entry],
Matt Carlson432aa7e2011-05-19 12:12:45 +00006566 mapping, new_addr);
6567
Matt Carlson84b67b22011-07-27 14:20:52 +00006568 if (tg3_tx_frag_set(tnapi, entry, budget, new_addr,
Matt Carlsond1a3b732011-07-27 14:20:51 +00006569 new_skb->len, base_flags,
6570 mss, vlan)) {
Matt Carlson84b67b22011-07-27 14:20:52 +00006571 tg3_tx_skb_unmap(tnapi, *entry, 0);
Matt Carlsond1a3b732011-07-27 14:20:51 +00006572 dev_kfree_skb(new_skb);
6573 ret = -1;
6574 }
Michael Chanc58ec932005-09-17 00:46:27 -07006575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006576 }
6577
Linus Torvalds1da177e2005-04-16 15:20:36 -07006578 dev_kfree_skb(skb);
David S. Miller1805b2f2011-10-24 18:18:09 -04006579 *pskb = new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07006580 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006581}
6582
Matt Carlson2ffcc982011-05-19 12:12:44 +00006583static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07006584
6585/* Use GSO to workaround a rare TSO bug that may be triggered when the
6586 * TSO header is greater than 80 bytes.
6587 */
6588static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
6589{
6590 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006591 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07006592
6593 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006594 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07006595 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006596
6597 /* netif_tx_stop_queue() must be done before checking
6598 * checking tx index in tg3_tx_avail() below, because in
6599 * tg3_tx(), we update tx index before checking for
6600 * netif_tx_queue_stopped().
6601 */
6602 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006603 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08006604 return NETDEV_TX_BUSY;
6605
6606 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07006607 }
6608
6609 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07006610 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07006611 goto tg3_tso_bug_end;
6612
6613 do {
6614 nskb = segs;
6615 segs = segs->next;
6616 nskb->next = NULL;
Matt Carlson2ffcc982011-05-19 12:12:44 +00006617 tg3_start_xmit(nskb, tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07006618 } while (segs);
6619
6620tg3_tso_bug_end:
6621 dev_kfree_skb(skb);
6622
6623 return NETDEV_TX_OK;
6624}
Michael Chan52c0fd82006-06-29 20:15:54 -07006625
Michael Chan5a6f3072006-03-20 22:28:05 -08006626/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
Joe Perches63c3a662011-04-26 08:12:10 +00006627 * support TG3_FLAG_HW_TSO_1 or firmware TSO only.
Michael Chan5a6f3072006-03-20 22:28:05 -08006628 */
Matt Carlson2ffcc982011-05-19 12:12:44 +00006629static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08006630{
6631 struct tg3 *tp = netdev_priv(dev);
Matt Carlson92cd3a12011-07-27 14:20:47 +00006632 u32 len, entry, base_flags, mss, vlan = 0;
Matt Carlson84b67b22011-07-27 14:20:52 +00006633 u32 budget;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006634 int i = -1, would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07006635 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00006636 struct tg3_napi *tnapi;
6637 struct netdev_queue *txq;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006638 unsigned int last;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006639
Matt Carlson24f4efd2009-11-13 13:03:35 +00006640 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
6641 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Joe Perches63c3a662011-04-26 08:12:10 +00006642 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006643 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006644
Matt Carlson84b67b22011-07-27 14:20:52 +00006645 budget = tg3_tx_avail(tnapi);
6646
Michael Chan00b70502006-06-17 21:58:45 -07006647 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006648 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07006649 * interrupt. Furthermore, IRQ processing runs lockless so we have
6650 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07006651 */
Matt Carlson84b67b22011-07-27 14:20:52 +00006652 if (unlikely(budget <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006653 if (!netif_tx_queue_stopped(txq)) {
6654 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08006655
6656 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00006657 netdev_err(dev,
6658 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08006659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006660 return NETDEV_TX_BUSY;
6661 }
6662
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006663 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006664 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07006665 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006666 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00006667
Matt Carlsonbe98da62010-07-11 09:31:46 +00006668 mss = skb_shinfo(skb)->gso_size;
6669 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006670 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00006671 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006672
6673 if (skb_header_cloned(skb) &&
Eric Dumazet48855432011-10-24 07:53:03 +00006674 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
6675 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006676
Matt Carlson34195c32010-07-11 09:31:42 +00006677 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07006678 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006679
Matt Carlson02e96082010-09-15 08:59:59 +00006680 if (skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00006681 hdr_len = skb_headlen(skb) - ETH_HLEN;
6682 } else {
6683 u32 ip_tcp_len;
6684
6685 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
6686 hdr_len = ip_tcp_len + tcp_opt_len;
6687
6688 iph->check = 0;
6689 iph->tot_len = htons(mss + hdr_len);
6690 }
6691
Michael Chan52c0fd82006-06-29 20:15:54 -07006692 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Joe Perches63c3a662011-04-26 08:12:10 +00006693 tg3_flag(tp, TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00006694 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07006695
Linus Torvalds1da177e2005-04-16 15:20:36 -07006696 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
6697 TXD_FLAG_CPU_POST_DMA);
6698
Joe Perches63c3a662011-04-26 08:12:10 +00006699 if (tg3_flag(tp, HW_TSO_1) ||
6700 tg3_flag(tp, HW_TSO_2) ||
6701 tg3_flag(tp, HW_TSO_3)) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006702 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006703 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006704 } else
6705 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
6706 iph->daddr, 0,
6707 IPPROTO_TCP,
6708 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006709
Joe Perches63c3a662011-04-26 08:12:10 +00006710 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlson615774f2009-11-13 13:03:39 +00006711 mss |= (hdr_len & 0xc) << 12;
6712 if (hdr_len & 0x10)
6713 base_flags |= 0x00000010;
6714 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +00006715 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006716 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +00006717 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006718 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006719 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006720 int tsflags;
6721
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006722 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006723 mss |= (tsflags << 11);
6724 }
6725 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006726 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006727 int tsflags;
6728
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006729 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006730 base_flags |= tsflags << 12;
6731 }
6732 }
6733 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00006734
Matt Carlson93a700a2011-08-31 11:44:54 +00006735 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
6736 !mss && skb->len > VLAN_ETH_FRAME_LEN)
6737 base_flags |= TXD_FLAG_JMB_PKT;
6738
Matt Carlson92cd3a12011-07-27 14:20:47 +00006739 if (vlan_tx_tag_present(skb)) {
6740 base_flags |= TXD_FLAG_VLAN;
6741 vlan = vlan_tx_tag_get(skb);
6742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006743
Alexander Duyckf4188d82009-12-02 16:48:38 +00006744 len = skb_headlen(skb);
6745
6746 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
Eric Dumazet48855432011-10-24 07:53:03 +00006747 if (pci_dma_mapping_error(tp->pdev, mapping))
6748 goto drop;
6749
David S. Miller90079ce2008-09-11 04:52:51 -07006750
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006751 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006752 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006753
6754 would_hit_hwbug = 0;
6755
Joe Perches63c3a662011-04-26 08:12:10 +00006756 if (tg3_flag(tp, 5701_DMA_BUG))
Michael Chanc58ec932005-09-17 00:46:27 -07006757 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006758
Matt Carlson84b67b22011-07-27 14:20:52 +00006759 if (tg3_tx_frag_set(tnapi, &entry, &budget, mapping, len, base_flags |
Matt Carlsond1a3b732011-07-27 14:20:51 +00006760 ((skb_shinfo(skb)->nr_frags == 0) ? TXD_FLAG_END : 0),
6761 mss, vlan))
6762 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006763
Linus Torvalds1da177e2005-04-16 15:20:36 -07006764 /* Now loop through additional data fragments, and queue them. */
6765 if (skb_shinfo(skb)->nr_frags > 0) {
Matt Carlson92cd3a12011-07-27 14:20:47 +00006766 u32 tmp_mss = mss;
6767
6768 if (!tg3_flag(tp, HW_TSO_1) &&
6769 !tg3_flag(tp, HW_TSO_2) &&
6770 !tg3_flag(tp, HW_TSO_3))
6771 tmp_mss = 0;
6772
Linus Torvalds1da177e2005-04-16 15:20:36 -07006773 last = skb_shinfo(skb)->nr_frags - 1;
6774 for (i = 0; i <= last; i++) {
6775 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6776
Eric Dumazet9e903e02011-10-18 21:00:24 +00006777 len = skb_frag_size(frag);
Ian Campbelldc234d02011-08-24 22:28:11 +00006778 mapping = skb_frag_dma_map(&tp->pdev->dev, frag, 0,
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01006779 len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006780
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006781 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006782 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00006783 mapping);
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01006784 if (dma_mapping_error(&tp->pdev->dev, mapping))
Alexander Duyckf4188d82009-12-02 16:48:38 +00006785 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006786
Matt Carlson84b67b22011-07-27 14:20:52 +00006787 if (tg3_tx_frag_set(tnapi, &entry, &budget, mapping,
6788 len, base_flags |
6789 ((i == last) ? TXD_FLAG_END : 0),
Matt Carlsond1a3b732011-07-27 14:20:51 +00006790 tmp_mss, vlan))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006791 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006792 }
6793 }
6794
6795 if (would_hit_hwbug) {
Matt Carlson0d681b22011-07-27 14:20:49 +00006796 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006797
6798 /* If the workaround fails due to memory/mapping
6799 * failure, silently drop this packet.
6800 */
Matt Carlson84b67b22011-07-27 14:20:52 +00006801 entry = tnapi->tx_prod;
6802 budget = tg3_tx_avail(tnapi);
David S. Miller1805b2f2011-10-24 18:18:09 -04006803 if (tigon3_dma_hwbug_workaround(tnapi, &skb, &entry, &budget,
Matt Carlson84b67b22011-07-27 14:20:52 +00006804 base_flags, mss, vlan))
Eric Dumazet48855432011-10-24 07:53:03 +00006805 goto drop_nofree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006806 }
6807
Richard Cochrand515b452011-06-19 03:31:41 +00006808 skb_tx_timestamp(skb);
6809
Linus Torvalds1da177e2005-04-16 15:20:36 -07006810 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006811 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006812
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006813 tnapi->tx_prod = entry;
6814 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006815 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006816
6817 /* netif_tx_stop_queue() must be done before checking
6818 * checking tx index in tg3_tx_avail() below, because in
6819 * tg3_tx(), we update tx index before checking for
6820 * netif_tx_queue_stopped().
6821 */
6822 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006823 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006824 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07006825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006826
Eric Dumazetcdd0db02009-05-28 00:00:41 +00006827 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006828 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006829
6830dma_error:
Matt Carlson0d681b22011-07-27 14:20:49 +00006831 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, i);
Matt Carlson432aa7e2011-05-19 12:12:45 +00006832 tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
Eric Dumazet48855432011-10-24 07:53:03 +00006833drop:
6834 dev_kfree_skb(skb);
6835drop_nofree:
6836 tp->tx_dropped++;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006837 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006838}
6839
Matt Carlson6e01b202011-08-19 13:58:20 +00006840static void tg3_mac_loopback(struct tg3 *tp, bool enable)
6841{
6842 if (enable) {
6843 tp->mac_mode &= ~(MAC_MODE_HALF_DUPLEX |
6844 MAC_MODE_PORT_MODE_MASK);
6845
6846 tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
6847
6848 if (!tg3_flag(tp, 5705_PLUS))
6849 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
6850
6851 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
6852 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
6853 else
6854 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
6855 } else {
6856 tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
6857
6858 if (tg3_flag(tp, 5705_PLUS) ||
6859 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) ||
6860 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
6861 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
6862 }
6863
6864 tw32(MAC_MODE, tp->mac_mode);
6865 udelay(40);
6866}
6867
Matt Carlson941ec902011-08-19 13:58:23 +00006868static int tg3_phy_lpbk_set(struct tg3 *tp, u32 speed, bool extlpbk)
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006869{
Matt Carlson941ec902011-08-19 13:58:23 +00006870 u32 val, bmcr, mac_mode, ptest = 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006871
6872 tg3_phy_toggle_apd(tp, false);
6873 tg3_phy_toggle_automdix(tp, 0);
6874
Matt Carlson941ec902011-08-19 13:58:23 +00006875 if (extlpbk && tg3_phy_set_extloopbk(tp))
6876 return -EIO;
6877
6878 bmcr = BMCR_FULLDPLX;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006879 switch (speed) {
6880 case SPEED_10:
6881 break;
6882 case SPEED_100:
6883 bmcr |= BMCR_SPEED100;
6884 break;
6885 case SPEED_1000:
6886 default:
6887 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
6888 speed = SPEED_100;
6889 bmcr |= BMCR_SPEED100;
6890 } else {
6891 speed = SPEED_1000;
6892 bmcr |= BMCR_SPEED1000;
6893 }
6894 }
6895
Matt Carlson941ec902011-08-19 13:58:23 +00006896 if (extlpbk) {
6897 if (!(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
6898 tg3_readphy(tp, MII_CTRL1000, &val);
6899 val |= CTL1000_AS_MASTER |
6900 CTL1000_ENABLE_MASTER;
6901 tg3_writephy(tp, MII_CTRL1000, val);
6902 } else {
6903 ptest = MII_TG3_FET_PTEST_TRIM_SEL |
6904 MII_TG3_FET_PTEST_TRIM_2;
6905 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest);
6906 }
6907 } else
6908 bmcr |= BMCR_LOOPBACK;
6909
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006910 tg3_writephy(tp, MII_BMCR, bmcr);
6911
6912 /* The write needs to be flushed for the FETs */
6913 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
6914 tg3_readphy(tp, MII_BMCR, &bmcr);
6915
6916 udelay(40);
6917
6918 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
6919 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Matt Carlson941ec902011-08-19 13:58:23 +00006920 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest |
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006921 MII_TG3_FET_PTEST_FRC_TX_LINK |
6922 MII_TG3_FET_PTEST_FRC_TX_LOCK);
6923
6924 /* The write needs to be flushed for the AC131 */
6925 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
6926 }
6927
6928 /* Reset to prevent losing 1st rx packet intermittently */
6929 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
6930 tg3_flag(tp, 5780_CLASS)) {
6931 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
6932 udelay(10);
6933 tw32_f(MAC_RX_MODE, tp->rx_mode);
6934 }
6935
6936 mac_mode = tp->mac_mode &
6937 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
6938 if (speed == SPEED_1000)
6939 mac_mode |= MAC_MODE_PORT_MODE_GMII;
6940 else
6941 mac_mode |= MAC_MODE_PORT_MODE_MII;
6942
6943 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
6944 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
6945
6946 if (masked_phy_id == TG3_PHY_ID_BCM5401)
6947 mac_mode &= ~MAC_MODE_LINK_POLARITY;
6948 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
6949 mac_mode |= MAC_MODE_LINK_POLARITY;
6950
6951 tg3_writephy(tp, MII_TG3_EXT_CTRL,
6952 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
6953 }
6954
6955 tw32(MAC_MODE, mac_mode);
6956 udelay(40);
Matt Carlson941ec902011-08-19 13:58:23 +00006957
6958 return 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006959}
6960
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006961static void tg3_set_loopback(struct net_device *dev, u32 features)
6962{
6963 struct tg3 *tp = netdev_priv(dev);
6964
6965 if (features & NETIF_F_LOOPBACK) {
6966 if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
6967 return;
6968
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006969 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00006970 tg3_mac_loopback(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006971 netif_carrier_on(tp->dev);
6972 spin_unlock_bh(&tp->lock);
6973 netdev_info(dev, "Internal MAC loopback mode enabled.\n");
6974 } else {
6975 if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
6976 return;
6977
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006978 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00006979 tg3_mac_loopback(tp, false);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006980 /* Force link status check */
6981 tg3_setup_phy(tp, 1);
6982 spin_unlock_bh(&tp->lock);
6983 netdev_info(dev, "Internal MAC loopback mode disabled.\n");
6984 }
6985}
6986
Michał Mirosławdc668912011-04-07 03:35:07 +00006987static u32 tg3_fix_features(struct net_device *dev, u32 features)
6988{
6989 struct tg3 *tp = netdev_priv(dev);
6990
Joe Perches63c3a662011-04-26 08:12:10 +00006991 if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
Michał Mirosławdc668912011-04-07 03:35:07 +00006992 features &= ~NETIF_F_ALL_TSO;
6993
6994 return features;
6995}
6996
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006997static int tg3_set_features(struct net_device *dev, u32 features)
6998{
6999 u32 changed = dev->features ^ features;
7000
7001 if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
7002 tg3_set_loopback(dev, features);
7003
7004 return 0;
7005}
7006
Linus Torvalds1da177e2005-04-16 15:20:36 -07007007static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
7008 int new_mtu)
7009{
7010 dev->mtu = new_mtu;
7011
Michael Chanef7f5ec2005-07-25 12:32:25 -07007012 if (new_mtu > ETH_DATA_LEN) {
Joe Perches63c3a662011-04-26 08:12:10 +00007013 if (tg3_flag(tp, 5780_CLASS)) {
Michał Mirosławdc668912011-04-07 03:35:07 +00007014 netdev_update_features(dev);
Joe Perches63c3a662011-04-26 08:12:10 +00007015 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson859a5882010-04-05 10:19:28 +00007016 } else {
Joe Perches63c3a662011-04-26 08:12:10 +00007017 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Matt Carlson859a5882010-04-05 10:19:28 +00007018 }
Michael Chanef7f5ec2005-07-25 12:32:25 -07007019 } else {
Joe Perches63c3a662011-04-26 08:12:10 +00007020 if (tg3_flag(tp, 5780_CLASS)) {
7021 tg3_flag_set(tp, TSO_CAPABLE);
Michał Mirosławdc668912011-04-07 03:35:07 +00007022 netdev_update_features(dev);
7023 }
Joe Perches63c3a662011-04-26 08:12:10 +00007024 tg3_flag_clear(tp, JUMBO_RING_ENABLE);
Michael Chanef7f5ec2005-07-25 12:32:25 -07007025 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007026}
7027
7028static int tg3_change_mtu(struct net_device *dev, int new_mtu)
7029{
7030 struct tg3 *tp = netdev_priv(dev);
Michael Chanb9ec6c12006-07-25 16:37:27 -07007031 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007032
7033 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
7034 return -EINVAL;
7035
7036 if (!netif_running(dev)) {
7037 /* We'll just catch it later when the
7038 * device is up'd.
7039 */
7040 tg3_set_mtu(dev, tp, new_mtu);
7041 return 0;
7042 }
7043
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07007044 tg3_phy_stop(tp);
7045
Linus Torvalds1da177e2005-04-16 15:20:36 -07007046 tg3_netif_stop(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07007047
7048 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007049
Michael Chan944d9802005-05-29 14:57:48 -07007050 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007051
7052 tg3_set_mtu(dev, tp, new_mtu);
7053
Michael Chanb9ec6c12006-07-25 16:37:27 -07007054 err = tg3_restart_hw(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007055
Michael Chanb9ec6c12006-07-25 16:37:27 -07007056 if (!err)
7057 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007058
David S. Millerf47c11e2005-06-24 20:18:35 -07007059 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007060
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07007061 if (!err)
7062 tg3_phy_start(tp);
7063
Michael Chanb9ec6c12006-07-25 16:37:27 -07007064 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007065}
7066
Matt Carlson21f581a2009-08-28 14:00:25 +00007067static void tg3_rx_prodring_free(struct tg3 *tp,
7068 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007069{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007070 int i;
7071
Matt Carlson8fea32b2010-09-15 08:59:58 +00007072 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007073 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00007074 i = (i + 1) & tp->rx_std_ring_mask)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007075 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
7076 tp->rx_pkt_map_sz);
7077
Joe Perches63c3a662011-04-26 08:12:10 +00007078 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007079 for (i = tpr->rx_jmb_cons_idx;
7080 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00007081 i = (i + 1) & tp->rx_jmb_ring_mask) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007082 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
7083 TG3_RX_JMB_MAP_SZ);
7084 }
7085 }
7086
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007087 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007089
Matt Carlson2c49a442010-09-30 10:34:35 +00007090 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007091 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
7092 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007093
Joe Perches63c3a662011-04-26 08:12:10 +00007094 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007095 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007096 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
7097 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007098 }
7099}
7100
Matt Carlsonc6cdf432010-04-05 10:19:26 +00007101/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007102 *
7103 * The chip has been shut down and the driver detached from
7104 * the networking, so no interrupts or new tx packets will
7105 * end up in the driver. tp->{tx,}lock are held and thus
7106 * we may not sleep.
7107 */
Matt Carlson21f581a2009-08-28 14:00:25 +00007108static int tg3_rx_prodring_alloc(struct tg3 *tp,
7109 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007110{
Matt Carlson287be122009-08-28 13:58:46 +00007111 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007112
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007113 tpr->rx_std_cons_idx = 0;
7114 tpr->rx_std_prod_idx = 0;
7115 tpr->rx_jmb_cons_idx = 0;
7116 tpr->rx_jmb_prod_idx = 0;
7117
Matt Carlson8fea32b2010-09-15 08:59:58 +00007118 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007119 memset(&tpr->rx_std_buffers[0], 0,
7120 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00007121 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007122 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00007123 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007124 goto done;
7125 }
7126
Linus Torvalds1da177e2005-04-16 15:20:36 -07007127 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00007128 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007129
Matt Carlson287be122009-08-28 13:58:46 +00007130 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00007131 if (tg3_flag(tp, 5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00007132 tp->dev->mtu > ETH_DATA_LEN)
7133 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
7134 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07007135
Linus Torvalds1da177e2005-04-16 15:20:36 -07007136 /* Initialize invariants of the rings, we only set this
7137 * stuff once. This works because the card does not
7138 * write into the rx buffer posting rings.
7139 */
Matt Carlson2c49a442010-09-30 10:34:35 +00007140 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007141 struct tg3_rx_buffer_desc *rxd;
7142
Matt Carlson21f581a2009-08-28 14:00:25 +00007143 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00007144 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007145 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
7146 rxd->opaque = (RXD_OPAQUE_RING_STD |
7147 (i << RXD_OPAQUE_INDEX_SHIFT));
7148 }
7149
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007150 /* Now allocate fresh SKBs for each rx ring. */
7151 for (i = 0; i < tp->rx_pending; i++) {
Matt Carlson86b21e52009-11-13 13:03:45 +00007152 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_STD, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007153 netdev_warn(tp->dev,
7154 "Using a smaller RX standard ring. Only "
7155 "%d out of %d buffers were allocated "
7156 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007157 if (i == 0)
7158 goto initfail;
7159 tp->rx_pending = i;
7160 break;
7161 }
7162 }
7163
Joe Perches63c3a662011-04-26 08:12:10 +00007164 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007165 goto done;
7166
Matt Carlson2c49a442010-09-30 10:34:35 +00007167 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007168
Joe Perches63c3a662011-04-26 08:12:10 +00007169 if (!tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson0d86df82010-02-17 15:17:00 +00007170 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007171
Matt Carlson2c49a442010-09-30 10:34:35 +00007172 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00007173 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007174
Matt Carlson0d86df82010-02-17 15:17:00 +00007175 rxd = &tpr->rx_jmb[i].std;
7176 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
7177 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
7178 RXD_FLAG_JUMBO;
7179 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
7180 (i << RXD_OPAQUE_INDEX_SHIFT));
7181 }
7182
7183 for (i = 0; i < tp->rx_jumbo_pending; i++) {
7184 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_JUMBO, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007185 netdev_warn(tp->dev,
7186 "Using a smaller RX jumbo ring. Only %d "
7187 "out of %d buffers were allocated "
7188 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00007189 if (i == 0)
7190 goto initfail;
7191 tp->rx_jumbo_pending = i;
7192 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007193 }
7194 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007195
7196done:
Michael Chan32d8c572006-07-25 16:38:29 -07007197 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007198
7199initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00007200 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007201 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007202}
7203
Matt Carlson21f581a2009-08-28 14:00:25 +00007204static void tg3_rx_prodring_fini(struct tg3 *tp,
7205 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007206{
Matt Carlson21f581a2009-08-28 14:00:25 +00007207 kfree(tpr->rx_std_buffers);
7208 tpr->rx_std_buffers = NULL;
7209 kfree(tpr->rx_jmb_buffers);
7210 tpr->rx_jmb_buffers = NULL;
7211 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007212 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
7213 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00007214 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007215 }
Matt Carlson21f581a2009-08-28 14:00:25 +00007216 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007217 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
7218 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00007219 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007220 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007221}
7222
Matt Carlson21f581a2009-08-28 14:00:25 +00007223static int tg3_rx_prodring_init(struct tg3 *tp,
7224 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007225{
Matt Carlson2c49a442010-09-30 10:34:35 +00007226 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
7227 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007228 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007229 return -ENOMEM;
7230
Matt Carlson4bae65c2010-11-24 08:31:52 +00007231 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
7232 TG3_RX_STD_RING_BYTES(tp),
7233 &tpr->rx_std_mapping,
7234 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007235 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007236 goto err_out;
7237
Joe Perches63c3a662011-04-26 08:12:10 +00007238 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007239 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00007240 GFP_KERNEL);
7241 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007242 goto err_out;
7243
Matt Carlson4bae65c2010-11-24 08:31:52 +00007244 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
7245 TG3_RX_JMB_RING_BYTES(tp),
7246 &tpr->rx_jmb_mapping,
7247 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007248 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007249 goto err_out;
7250 }
7251
7252 return 0;
7253
7254err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00007255 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007256 return -ENOMEM;
7257}
7258
7259/* Free up pending packets in all rx/tx rings.
7260 *
7261 * The chip has been shut down and the driver detached from
7262 * the networking, so no interrupts or new tx packets will
7263 * end up in the driver. tp->{tx,}lock is not held and we are not
7264 * in an interrupt context and thus may sleep.
7265 */
7266static void tg3_free_rings(struct tg3 *tp)
7267{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007268 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007269
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007270 for (j = 0; j < tp->irq_cnt; j++) {
7271 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007272
Matt Carlson8fea32b2010-09-15 08:59:58 +00007273 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00007274
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007275 if (!tnapi->tx_buffers)
7276 continue;
7277
Matt Carlson0d681b22011-07-27 14:20:49 +00007278 for (i = 0; i < TG3_TX_RING_SIZE; i++) {
7279 struct sk_buff *skb = tnapi->tx_buffers[i].skb;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007280
Matt Carlson0d681b22011-07-27 14:20:49 +00007281 if (!skb)
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007282 continue;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007283
Matt Carlson0d681b22011-07-27 14:20:49 +00007284 tg3_tx_skb_unmap(tnapi, i, skb_shinfo(skb)->nr_frags);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007285
7286 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007287 }
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007288 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007289}
7290
7291/* Initialize tx/rx rings for packet processing.
7292 *
7293 * The chip has been shut down and the driver detached from
7294 * the networking, so no interrupts or new tx packets will
7295 * end up in the driver. tp->{tx,}lock are held and thus
7296 * we may not sleep.
7297 */
7298static int tg3_init_rings(struct tg3 *tp)
7299{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007300 int i;
Matt Carlson72334482009-08-28 14:03:01 +00007301
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007302 /* Free up all the SKBs. */
7303 tg3_free_rings(tp);
7304
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007305 for (i = 0; i < tp->irq_cnt; i++) {
7306 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007307
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007308 tnapi->last_tag = 0;
7309 tnapi->last_irq_tag = 0;
7310 tnapi->hw_status->status = 0;
7311 tnapi->hw_status->status_tag = 0;
7312 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7313
7314 tnapi->tx_prod = 0;
7315 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007316 if (tnapi->tx_ring)
7317 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007318
7319 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007320 if (tnapi->rx_rcb)
7321 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007322
Matt Carlson8fea32b2010-09-15 08:59:58 +00007323 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00007324 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007325 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00007326 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007327 }
Matt Carlson72334482009-08-28 14:03:01 +00007328
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007329 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007330}
7331
7332/*
7333 * Must not be invoked with interrupt sources disabled and
7334 * the hardware shutdown down.
7335 */
7336static void tg3_free_consistent(struct tg3 *tp)
7337{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007338 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00007339
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007340 for (i = 0; i < tp->irq_cnt; i++) {
7341 struct tg3_napi *tnapi = &tp->napi[i];
7342
7343 if (tnapi->tx_ring) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007344 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007345 tnapi->tx_ring, tnapi->tx_desc_mapping);
7346 tnapi->tx_ring = NULL;
7347 }
7348
7349 kfree(tnapi->tx_buffers);
7350 tnapi->tx_buffers = NULL;
7351
7352 if (tnapi->rx_rcb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007353 dma_free_coherent(&tp->pdev->dev,
7354 TG3_RX_RCB_RING_BYTES(tp),
7355 tnapi->rx_rcb,
7356 tnapi->rx_rcb_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007357 tnapi->rx_rcb = NULL;
7358 }
7359
Matt Carlson8fea32b2010-09-15 08:59:58 +00007360 tg3_rx_prodring_fini(tp, &tnapi->prodring);
7361
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007362 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007363 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
7364 tnapi->hw_status,
7365 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007366 tnapi->hw_status = NULL;
7367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007368 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007369
Linus Torvalds1da177e2005-04-16 15:20:36 -07007370 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007371 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
7372 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007373 tp->hw_stats = NULL;
7374 }
7375}
7376
7377/*
7378 * Must not be invoked with interrupt sources disabled and
7379 * the hardware shutdown down. Can sleep.
7380 */
7381static int tg3_alloc_consistent(struct tg3 *tp)
7382{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007383 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00007384
Matt Carlson4bae65c2010-11-24 08:31:52 +00007385 tp->hw_stats = dma_alloc_coherent(&tp->pdev->dev,
7386 sizeof(struct tg3_hw_stats),
7387 &tp->stats_mapping,
7388 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007389 if (!tp->hw_stats)
7390 goto err_out;
7391
Linus Torvalds1da177e2005-04-16 15:20:36 -07007392 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
7393
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007394 for (i = 0; i < tp->irq_cnt; i++) {
7395 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007396 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007397
Matt Carlson4bae65c2010-11-24 08:31:52 +00007398 tnapi->hw_status = dma_alloc_coherent(&tp->pdev->dev,
7399 TG3_HW_STATUS_SIZE,
7400 &tnapi->status_mapping,
7401 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007402 if (!tnapi->hw_status)
7403 goto err_out;
7404
7405 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007406 sblk = tnapi->hw_status;
7407
Matt Carlson8fea32b2010-09-15 08:59:58 +00007408 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
7409 goto err_out;
7410
Matt Carlson19cfaec2009-12-03 08:36:20 +00007411 /* If multivector TSS is enabled, vector 0 does not handle
7412 * tx interrupts. Don't allocate any resources for it.
7413 */
Joe Perches63c3a662011-04-26 08:12:10 +00007414 if ((!i && !tg3_flag(tp, ENABLE_TSS)) ||
7415 (i && tg3_flag(tp, ENABLE_TSS))) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00007416 tnapi->tx_buffers = kzalloc(
7417 sizeof(struct tg3_tx_ring_info) *
7418 TG3_TX_RING_SIZE, GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007419 if (!tnapi->tx_buffers)
7420 goto err_out;
7421
Matt Carlson4bae65c2010-11-24 08:31:52 +00007422 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
7423 TG3_TX_RING_BYTES,
7424 &tnapi->tx_desc_mapping,
7425 GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007426 if (!tnapi->tx_ring)
7427 goto err_out;
7428 }
7429
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007430 /*
7431 * When RSS is enabled, the status block format changes
7432 * slightly. The "rx_jumbo_consumer", "reserved",
7433 * and "rx_mini_consumer" members get mapped to the
7434 * other three rx return ring producer indexes.
7435 */
7436 switch (i) {
7437 default:
7438 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
7439 break;
7440 case 2:
7441 tnapi->rx_rcb_prod_idx = &sblk->rx_jumbo_consumer;
7442 break;
7443 case 3:
7444 tnapi->rx_rcb_prod_idx = &sblk->reserved;
7445 break;
7446 case 4:
7447 tnapi->rx_rcb_prod_idx = &sblk->rx_mini_consumer;
7448 break;
7449 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007450
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007451 /*
7452 * If multivector RSS is enabled, vector 0 does not handle
7453 * rx or tx interrupts. Don't allocate any resources for it.
7454 */
Joe Perches63c3a662011-04-26 08:12:10 +00007455 if (!i && tg3_flag(tp, ENABLE_RSS))
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007456 continue;
7457
Matt Carlson4bae65c2010-11-24 08:31:52 +00007458 tnapi->rx_rcb = dma_alloc_coherent(&tp->pdev->dev,
7459 TG3_RX_RCB_RING_BYTES(tp),
7460 &tnapi->rx_rcb_mapping,
7461 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007462 if (!tnapi->rx_rcb)
7463 goto err_out;
7464
7465 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007466 }
7467
Linus Torvalds1da177e2005-04-16 15:20:36 -07007468 return 0;
7469
7470err_out:
7471 tg3_free_consistent(tp);
7472 return -ENOMEM;
7473}
7474
7475#define MAX_WAIT_CNT 1000
7476
7477/* To stop a block, clear the enable bit and poll till it
7478 * clears. tp->lock is held.
7479 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007480static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007481{
7482 unsigned int i;
7483 u32 val;
7484
Joe Perches63c3a662011-04-26 08:12:10 +00007485 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007486 switch (ofs) {
7487 case RCVLSC_MODE:
7488 case DMAC_MODE:
7489 case MBFREE_MODE:
7490 case BUFMGR_MODE:
7491 case MEMARB_MODE:
7492 /* We can't enable/disable these bits of the
7493 * 5705/5750, just say success.
7494 */
7495 return 0;
7496
7497 default:
7498 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007500 }
7501
7502 val = tr32(ofs);
7503 val &= ~enable_bit;
7504 tw32_f(ofs, val);
7505
7506 for (i = 0; i < MAX_WAIT_CNT; i++) {
7507 udelay(100);
7508 val = tr32(ofs);
7509 if ((val & enable_bit) == 0)
7510 break;
7511 }
7512
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007513 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00007514 dev_err(&tp->pdev->dev,
7515 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
7516 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007517 return -ENODEV;
7518 }
7519
7520 return 0;
7521}
7522
7523/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007524static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007525{
7526 int i, err;
7527
7528 tg3_disable_ints(tp);
7529
7530 tp->rx_mode &= ~RX_MODE_ENABLE;
7531 tw32_f(MAC_RX_MODE, tp->rx_mode);
7532 udelay(10);
7533
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007534 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
7535 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
7536 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
7537 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
7538 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
7539 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007540
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007541 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
7542 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
7543 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
7544 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
7545 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
7546 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
7547 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007548
7549 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
7550 tw32_f(MAC_MODE, tp->mac_mode);
7551 udelay(40);
7552
7553 tp->tx_mode &= ~TX_MODE_ENABLE;
7554 tw32_f(MAC_TX_MODE, tp->tx_mode);
7555
7556 for (i = 0; i < MAX_WAIT_CNT; i++) {
7557 udelay(100);
7558 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
7559 break;
7560 }
7561 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00007562 dev_err(&tp->pdev->dev,
7563 "%s timed out, TX_MODE_ENABLE will not clear "
7564 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07007565 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007566 }
7567
Michael Chane6de8ad2005-05-05 14:42:41 -07007568 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007569 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
7570 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007571
7572 tw32(FTQ_RESET, 0xffffffff);
7573 tw32(FTQ_RESET, 0x00000000);
7574
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007575 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
7576 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007577
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007578 for (i = 0; i < tp->irq_cnt; i++) {
7579 struct tg3_napi *tnapi = &tp->napi[i];
7580 if (tnapi->hw_status)
7581 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007583 if (tp->hw_stats)
7584 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
7585
Linus Torvalds1da177e2005-04-16 15:20:36 -07007586 return err;
7587}
7588
Michael Chanee6a99b2007-07-18 21:49:10 -07007589/* Save PCI command register before chip reset */
7590static void tg3_save_pci_state(struct tg3 *tp)
7591{
Matt Carlson8a6eac92007-10-21 16:17:55 -07007592 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007593}
7594
7595/* Restore PCI state after chip reset */
7596static void tg3_restore_pci_state(struct tg3 *tp)
7597{
7598 u32 val;
7599
7600 /* Re-enable indirect register accesses. */
7601 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
7602 tp->misc_host_ctrl);
7603
7604 /* Set MAX PCI retry to zero. */
7605 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
7606 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007607 tg3_flag(tp, PCIX_MODE))
Michael Chanee6a99b2007-07-18 21:49:10 -07007608 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07007609 /* Allow reads and writes to the APE register and memory space. */
Joe Perches63c3a662011-04-26 08:12:10 +00007610 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07007611 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc2010-06-05 17:24:30 +00007612 PCISTATE_ALLOW_APE_SHMEM_WR |
7613 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07007614 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
7615
Matt Carlson8a6eac92007-10-21 16:17:55 -07007616 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007617
Matt Carlsonfcb389d2008-11-03 16:55:44 -08007618 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785) {
Joe Perches63c3a662011-04-26 08:12:10 +00007619 if (tg3_flag(tp, PCI_EXPRESS))
Matt Carlsoncf790032010-11-24 08:31:48 +00007620 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlsonfcb389d2008-11-03 16:55:44 -08007621 else {
7622 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
7623 tp->pci_cacheline_sz);
7624 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
7625 tp->pci_lat_timer);
7626 }
Michael Chan114342f2007-10-15 02:12:26 -07007627 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08007628
Michael Chanee6a99b2007-07-18 21:49:10 -07007629 /* Make sure PCI-X relaxed ordering bit is clear. */
Joe Perches63c3a662011-04-26 08:12:10 +00007630 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07007631 u16 pcix_cmd;
7632
7633 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7634 &pcix_cmd);
7635 pcix_cmd &= ~PCI_X_CMD_ERO;
7636 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7637 pcix_cmd);
7638 }
Michael Chanee6a99b2007-07-18 21:49:10 -07007639
Joe Perches63c3a662011-04-26 08:12:10 +00007640 if (tg3_flag(tp, 5780_CLASS)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007641
7642 /* Chip reset on 5780 will reset MSI enable bit,
7643 * so need to restore it.
7644 */
Joe Perches63c3a662011-04-26 08:12:10 +00007645 if (tg3_flag(tp, USING_MSI)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007646 u16 ctrl;
7647
7648 pci_read_config_word(tp->pdev,
7649 tp->msi_cap + PCI_MSI_FLAGS,
7650 &ctrl);
7651 pci_write_config_word(tp->pdev,
7652 tp->msi_cap + PCI_MSI_FLAGS,
7653 ctrl | PCI_MSI_FLAGS_ENABLE);
7654 val = tr32(MSGINT_MODE);
7655 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
7656 }
7657 }
7658}
7659
Linus Torvalds1da177e2005-04-16 15:20:36 -07007660/* tp->lock is held. */
7661static int tg3_chip_reset(struct tg3 *tp)
7662{
7663 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07007664 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00007665 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007666
David S. Millerf49639e2006-06-09 11:58:36 -07007667 tg3_nvram_lock(tp);
7668
Matt Carlson77b483f2008-08-15 14:07:24 -07007669 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
7670
David S. Millerf49639e2006-06-09 11:58:36 -07007671 /* No matching tg3_nvram_unlock() after this because
7672 * chip reset below will undo the nvram lock.
7673 */
7674 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007675
Michael Chanee6a99b2007-07-18 21:49:10 -07007676 /* GRC_MISC_CFG core clock reset will clear the memory
7677 * enable bit in PCI register 4 and the MSI enable bit
7678 * on some chips, so we save relevant registers here.
7679 */
7680 tg3_save_pci_state(tp);
7681
Michael Chand9ab5ad2006-03-20 22:27:35 -08007682 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Joe Perches63c3a662011-04-26 08:12:10 +00007683 tg3_flag(tp, 5755_PLUS))
Michael Chand9ab5ad2006-03-20 22:27:35 -08007684 tw32(GRC_FASTBOOT_PC, 0);
7685
Linus Torvalds1da177e2005-04-16 15:20:36 -07007686 /*
7687 * We must avoid the readl() that normally takes place.
7688 * It locks machines, causes machine checks, and other
7689 * fun things. So, temporarily disable the 5701
7690 * hardware workaround, while we do the reset.
7691 */
Michael Chan1ee582d2005-08-09 20:16:46 -07007692 write_op = tp->write32;
7693 if (write_op == tg3_write_flush_reg32)
7694 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007695
Michael Chand18edcb2007-03-24 20:57:11 -07007696 /* Prevent the irq handler from reading or writing PCI registers
7697 * during chip reset when the memory enable bit in the PCI command
7698 * register may be cleared. The chip does not generate interrupt
7699 * at this time, but the irq handler may still be called due to irq
7700 * sharing or irqpoll.
7701 */
Joe Perches63c3a662011-04-26 08:12:10 +00007702 tg3_flag_set(tp, CHIP_RESETTING);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007703 for (i = 0; i < tp->irq_cnt; i++) {
7704 struct tg3_napi *tnapi = &tp->napi[i];
7705 if (tnapi->hw_status) {
7706 tnapi->hw_status->status = 0;
7707 tnapi->hw_status->status_tag = 0;
7708 }
7709 tnapi->last_tag = 0;
7710 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07007711 }
Michael Chand18edcb2007-03-24 20:57:11 -07007712 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00007713
7714 for (i = 0; i < tp->irq_cnt; i++)
7715 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07007716
Matt Carlson255ca312009-08-25 10:07:27 +00007717 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7718 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7719 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
7720 }
7721
Linus Torvalds1da177e2005-04-16 15:20:36 -07007722 /* do the reset */
7723 val = GRC_MISC_CFG_CORECLK_RESET;
7724
Joe Perches63c3a662011-04-26 08:12:10 +00007725 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson88075d92010-08-02 11:25:58 +00007726 /* Force PCIe 1.0a mode */
7727 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007728 !tg3_flag(tp, 57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00007729 tr32(TG3_PCIE_PHY_TSTCTL) ==
7730 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
7731 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
7732
Linus Torvalds1da177e2005-04-16 15:20:36 -07007733 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
7734 tw32(GRC_MISC_CFG, (1 << 29));
7735 val |= (1 << 29);
7736 }
7737 }
7738
Michael Chanb5d37722006-09-27 16:06:21 -07007739 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7740 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
7741 tw32(GRC_VCPU_EXT_CTRL,
7742 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
7743 }
7744
Matt Carlsonf37500d2010-08-02 11:25:59 +00007745 /* Manage gphy power for all CPMU absent PCIe devices. */
Joe Perches63c3a662011-04-26 08:12:10 +00007746 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007747 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00007748
Linus Torvalds1da177e2005-04-16 15:20:36 -07007749 tw32(GRC_MISC_CFG, val);
7750
Michael Chan1ee582d2005-08-09 20:16:46 -07007751 /* restore 5701 hardware bug workaround write method */
7752 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007753
7754 /* Unfortunately, we have to delay before the PCI read back.
7755 * Some 575X chips even will not respond to a PCI cfg access
7756 * when the reset command is given to the chip.
7757 *
7758 * How do these hardware designers expect things to work
7759 * properly if the PCI write is posted for a long period
7760 * of time? It is always necessary to have some method by
7761 * which a register read back can occur to push the write
7762 * out which does the reset.
7763 *
7764 * For most tg3 variants the trick below was working.
7765 * Ho hum...
7766 */
7767 udelay(120);
7768
7769 /* Flush PCI posted writes. The normal MMIO registers
7770 * are inaccessible at this time so this is the only
7771 * way to make this reliably (actually, this is no longer
7772 * the case, see above). I tried to use indirect
7773 * register read/write but this upset some 5701 variants.
7774 */
7775 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
7776
7777 udelay(120);
7778
Jon Mason708ebb32011-06-27 12:56:50 +00007779 if (tg3_flag(tp, PCI_EXPRESS) && pci_pcie_cap(tp->pdev)) {
Matt Carlsone7126992009-08-25 10:08:16 +00007780 u16 val16;
7781
Linus Torvalds1da177e2005-04-16 15:20:36 -07007782 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
7783 int i;
7784 u32 cfg_val;
7785
7786 /* Wait for link training to complete. */
7787 for (i = 0; i < 5000; i++)
7788 udelay(100);
7789
7790 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
7791 pci_write_config_dword(tp->pdev, 0xc4,
7792 cfg_val | (1 << 15));
7793 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007794
Matt Carlsone7126992009-08-25 10:08:16 +00007795 /* Clear the "no snoop" and "relaxed ordering" bits. */
7796 pci_read_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00007797 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007798 &val16);
7799 val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
7800 PCI_EXP_DEVCTL_NOSNOOP_EN);
7801 /*
7802 * Older PCIe devices only support the 128 byte
7803 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007804 */
Joe Perches63c3a662011-04-26 08:12:10 +00007805 if (!tg3_flag(tp, CPMU_PRESENT))
Matt Carlsone7126992009-08-25 10:08:16 +00007806 val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007807 pci_write_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00007808 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007809 val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007810
Matt Carlsoncf790032010-11-24 08:31:48 +00007811 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007812
7813 /* Clear error status */
7814 pci_write_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00007815 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVSTA,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007816 PCI_EXP_DEVSTA_CED |
7817 PCI_EXP_DEVSTA_NFED |
7818 PCI_EXP_DEVSTA_FED |
7819 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007820 }
7821
Michael Chanee6a99b2007-07-18 21:49:10 -07007822 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007823
Joe Perches63c3a662011-04-26 08:12:10 +00007824 tg3_flag_clear(tp, CHIP_RESETTING);
7825 tg3_flag_clear(tp, ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07007826
Michael Chanee6a99b2007-07-18 21:49:10 -07007827 val = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00007828 if (tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07007829 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07007830 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007831
7832 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
7833 tg3_stop_fw(tp);
7834 tw32(0x5000, 0x400);
7835 }
7836
7837 tw32(GRC_MODE, tp->grc_mode);
7838
7839 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007840 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007841
7842 tw32(0xc4, val | (1 << 15));
7843 }
7844
7845 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
7846 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
7847 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
7848 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
7849 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
7850 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7851 }
7852
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007853 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00007854 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007855 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007856 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00007857 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007858 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007859 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007860 val = 0;
7861
7862 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007863 udelay(40);
7864
Matt Carlson77b483f2008-08-15 14:07:24 -07007865 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
7866
Michael Chan7a6f4362006-09-27 16:03:31 -07007867 err = tg3_poll_fw(tp);
7868 if (err)
7869 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007870
Matt Carlson0a9140c2009-08-28 12:27:50 +00007871 tg3_mdio_start(tp);
7872
Joe Perches63c3a662011-04-26 08:12:10 +00007873 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007874 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
7875 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007876 !tg3_flag(tp, 57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007877 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007878
7879 tw32(0x7c00, val | (1 << 25));
7880 }
7881
Matt Carlsond78b59f2011-04-05 14:22:46 +00007882 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
7883 val = tr32(TG3_CPMU_CLCK_ORIDE);
7884 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
7885 }
7886
Linus Torvalds1da177e2005-04-16 15:20:36 -07007887 /* Reprobe ASF enable state. */
Joe Perches63c3a662011-04-26 08:12:10 +00007888 tg3_flag_clear(tp, ENABLE_ASF);
7889 tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007890 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
7891 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
7892 u32 nic_cfg;
7893
7894 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
7895 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +00007896 tg3_flag_set(tp, ENABLE_ASF);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007897 tp->last_event_jiffies = jiffies;
Joe Perches63c3a662011-04-26 08:12:10 +00007898 if (tg3_flag(tp, 5750_PLUS))
7899 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007900 }
7901 }
7902
7903 return 0;
7904}
7905
7906/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07007907static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007908{
7909 int err;
7910
7911 tg3_stop_fw(tp);
7912
Michael Chan944d9802005-05-29 14:57:48 -07007913 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007914
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007915 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007916 err = tg3_chip_reset(tp);
7917
Matt Carlsondaba2a62009-04-20 06:58:52 +00007918 __tg3_set_mac_addr(tp, 0);
7919
Michael Chan944d9802005-05-29 14:57:48 -07007920 tg3_write_sig_legacy(tp, kind);
7921 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007922
7923 if (err)
7924 return err;
7925
7926 return 0;
7927}
7928
Linus Torvalds1da177e2005-04-16 15:20:36 -07007929static int tg3_set_mac_addr(struct net_device *dev, void *p)
7930{
7931 struct tg3 *tp = netdev_priv(dev);
7932 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07007933 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007934
Michael Chanf9804dd2005-09-27 12:13:10 -07007935 if (!is_valid_ether_addr(addr->sa_data))
7936 return -EINVAL;
7937
Linus Torvalds1da177e2005-04-16 15:20:36 -07007938 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7939
Michael Chane75f7c92006-03-20 21:33:26 -08007940 if (!netif_running(dev))
7941 return 0;
7942
Joe Perches63c3a662011-04-26 08:12:10 +00007943 if (tg3_flag(tp, ENABLE_ASF)) {
Michael Chan986e0ae2007-05-05 12:10:20 -07007944 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07007945
Michael Chan986e0ae2007-05-05 12:10:20 -07007946 addr0_high = tr32(MAC_ADDR_0_HIGH);
7947 addr0_low = tr32(MAC_ADDR_0_LOW);
7948 addr1_high = tr32(MAC_ADDR_1_HIGH);
7949 addr1_low = tr32(MAC_ADDR_1_LOW);
7950
7951 /* Skip MAC addr 1 if ASF is using it. */
7952 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
7953 !(addr1_high == 0 && addr1_low == 0))
7954 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07007955 }
Michael Chan986e0ae2007-05-05 12:10:20 -07007956 spin_lock_bh(&tp->lock);
7957 __tg3_set_mac_addr(tp, skip_mac_1);
7958 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007959
Michael Chanb9ec6c12006-07-25 16:37:27 -07007960 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007961}
7962
7963/* tp->lock is held. */
7964static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
7965 dma_addr_t mapping, u32 maxlen_flags,
7966 u32 nic_addr)
7967{
7968 tg3_write_mem(tp,
7969 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
7970 ((u64) mapping >> 32));
7971 tg3_write_mem(tp,
7972 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
7973 ((u64) mapping & 0xffffffff));
7974 tg3_write_mem(tp,
7975 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
7976 maxlen_flags);
7977
Joe Perches63c3a662011-04-26 08:12:10 +00007978 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007979 tg3_write_mem(tp,
7980 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
7981 nic_addr);
7982}
7983
7984static void __tg3_set_rx_mode(struct net_device *);
Michael Chand244c892005-07-05 14:42:33 -07007985static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07007986{
Matt Carlsonb6080e12009-09-01 13:12:00 +00007987 int i;
7988
Joe Perches63c3a662011-04-26 08:12:10 +00007989 if (!tg3_flag(tp, ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007990 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
7991 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
7992 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007993 } else {
7994 tw32(HOSTCC_TXCOL_TICKS, 0);
7995 tw32(HOSTCC_TXMAX_FRAMES, 0);
7996 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007997 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007998
Joe Perches63c3a662011-04-26 08:12:10 +00007999 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008000 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
8001 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
8002 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
8003 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00008004 tw32(HOSTCC_RXCOL_TICKS, 0);
8005 tw32(HOSTCC_RXMAX_FRAMES, 0);
8006 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07008007 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008008
Joe Perches63c3a662011-04-26 08:12:10 +00008009 if (!tg3_flag(tp, 5705_PLUS)) {
David S. Miller15f98502005-05-18 22:49:26 -07008010 u32 val = ec->stats_block_coalesce_usecs;
8011
Matt Carlsonb6080e12009-09-01 13:12:00 +00008012 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
8013 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
8014
David S. Miller15f98502005-05-18 22:49:26 -07008015 if (!netif_carrier_ok(tp->dev))
8016 val = 0;
8017
8018 tw32(HOSTCC_STAT_COAL_TICKS, val);
8019 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008020
8021 for (i = 0; i < tp->irq_cnt - 1; i++) {
8022 u32 reg;
8023
8024 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
8025 tw32(reg, ec->rx_coalesce_usecs);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008026 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
8027 tw32(reg, ec->rx_max_coalesced_frames);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008028 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
8029 tw32(reg, ec->rx_max_coalesced_frames_irq);
Matt Carlson19cfaec2009-12-03 08:36:20 +00008030
Joe Perches63c3a662011-04-26 08:12:10 +00008031 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008032 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
8033 tw32(reg, ec->tx_coalesce_usecs);
8034 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
8035 tw32(reg, ec->tx_max_coalesced_frames);
8036 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
8037 tw32(reg, ec->tx_max_coalesced_frames_irq);
8038 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008039 }
8040
8041 for (; i < tp->irq_max - 1; i++) {
8042 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008043 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008044 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00008045
Joe Perches63c3a662011-04-26 08:12:10 +00008046 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008047 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
8048 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
8049 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
8050 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008051 }
David S. Miller15f98502005-05-18 22:49:26 -07008052}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008053
8054/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00008055static void tg3_rings_reset(struct tg3 *tp)
8056{
8057 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008058 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008059 struct tg3_napi *tnapi = &tp->napi[0];
8060
8061 /* Disable all transmit rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008062 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008063 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Joe Perches63c3a662011-04-26 08:12:10 +00008064 else if (tg3_flag(tp, 5717_PLUS))
Matt Carlson3d377282010-10-14 10:37:39 +00008065 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
Matt Carlsonb703df62009-12-03 08:36:21 +00008066 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8067 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008068 else
8069 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8070
8071 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8072 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
8073 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
8074 BDINFO_FLAGS_DISABLED);
8075
8076
8077 /* Disable all receive return rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008078 if (tg3_flag(tp, 5717_PLUS))
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008079 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
Joe Perches63c3a662011-04-26 08:12:10 +00008080 else if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008081 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00008082 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
8083 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson2d31eca2009-09-01 12:53:31 +00008084 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
8085 else
8086 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8087
8088 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8089 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
8090 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
8091 BDINFO_FLAGS_DISABLED);
8092
8093 /* Disable interrupts */
8094 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008095 tp->napi[0].chk_msi_cnt = 0;
8096 tp->napi[0].last_rx_cons = 0;
8097 tp->napi[0].last_tx_cons = 0;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008098
8099 /* Zero mailbox registers. */
Joe Perches63c3a662011-04-26 08:12:10 +00008100 if (tg3_flag(tp, SUPPORT_MSIX)) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00008101 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008102 tp->napi[i].tx_prod = 0;
8103 tp->napi[i].tx_cons = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00008104 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008105 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008106 tw32_rx_mbox(tp->napi[i].consmbox, 0);
8107 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
Matt Carlson7f230732011-08-31 11:44:48 +00008108 tp->napi[i].chk_msi_cnt = 0;
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008109 tp->napi[i].last_rx_cons = 0;
8110 tp->napi[i].last_tx_cons = 0;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008111 }
Joe Perches63c3a662011-04-26 08:12:10 +00008112 if (!tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008113 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008114 } else {
8115 tp->napi[0].tx_prod = 0;
8116 tp->napi[0].tx_cons = 0;
8117 tw32_mailbox(tp->napi[0].prodmbox, 0);
8118 tw32_rx_mbox(tp->napi[0].consmbox, 0);
8119 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008120
8121 /* Make sure the NIC-based send BD rings are disabled. */
Joe Perches63c3a662011-04-26 08:12:10 +00008122 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson2d31eca2009-09-01 12:53:31 +00008123 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
8124 for (i = 0; i < 16; i++)
8125 tw32_tx_mbox(mbox + i * 8, 0);
8126 }
8127
8128 txrcb = NIC_SRAM_SEND_RCB;
8129 rxrcb = NIC_SRAM_RCV_RET_RCB;
8130
8131 /* Clear status block in ram. */
8132 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8133
8134 /* Set status block DMA address */
8135 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8136 ((u64) tnapi->status_mapping >> 32));
8137 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8138 ((u64) tnapi->status_mapping & 0xffffffff));
8139
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008140 if (tnapi->tx_ring) {
8141 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8142 (TG3_TX_RING_SIZE <<
8143 BDINFO_FLAGS_MAXLEN_SHIFT),
8144 NIC_SRAM_TX_BUFFER_DESC);
8145 txrcb += TG3_BDINFO_SIZE;
8146 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008147
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008148 if (tnapi->rx_rcb) {
8149 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008150 (tp->rx_ret_ring_mask + 1) <<
8151 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008152 rxrcb += TG3_BDINFO_SIZE;
8153 }
8154
8155 stblk = HOSTCC_STATBLCK_RING1;
8156
8157 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
8158 u64 mapping = (u64)tnapi->status_mapping;
8159 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
8160 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
8161
8162 /* Clear status block in ram. */
8163 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8164
Matt Carlson19cfaec2009-12-03 08:36:20 +00008165 if (tnapi->tx_ring) {
8166 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8167 (TG3_TX_RING_SIZE <<
8168 BDINFO_FLAGS_MAXLEN_SHIFT),
8169 NIC_SRAM_TX_BUFFER_DESC);
8170 txrcb += TG3_BDINFO_SIZE;
8171 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008172
8173 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008174 ((tp->rx_ret_ring_mask + 1) <<
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008175 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
8176
8177 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008178 rxrcb += TG3_BDINFO_SIZE;
8179 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008180}
8181
Matt Carlsoneb07a942011-04-20 07:57:36 +00008182static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
8183{
8184 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
8185
Joe Perches63c3a662011-04-26 08:12:10 +00008186 if (!tg3_flag(tp, 5750_PLUS) ||
8187 tg3_flag(tp, 5780_CLASS) ||
Matt Carlsoneb07a942011-04-20 07:57:36 +00008188 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
8189 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
8190 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
8191 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
8192 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787)
8193 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
8194 else
8195 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
8196
8197 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
8198 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
8199
8200 val = min(nic_rep_thresh, host_rep_thresh);
8201 tw32(RCVBDI_STD_THRESH, val);
8202
Joe Perches63c3a662011-04-26 08:12:10 +00008203 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008204 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
8205
Joe Perches63c3a662011-04-26 08:12:10 +00008206 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008207 return;
8208
Joe Perches63c3a662011-04-26 08:12:10 +00008209 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008210 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
8211 else
8212 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5717;
8213
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 Carlson33466d92009-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 Carlson33466d92009-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 Carlson614b05902010-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 Carlsonf92d9dc2010-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 Carlson7cb32cf2010-09-30 10:34:36 +00008573 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlsonde9f5232011-04-05 14:22:43 +00008574 val = TG3_RX_STD_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008575 else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008576 val = TG3_RX_STD_MAX_SIZE_5717;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008577 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
8578 val |= (TG3_RX_STD_DMA_SZ << 2);
8579 } else
Matt Carlson04380d42010-04-12 06:58:29 +00008580 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008581 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008582 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008583
8584 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008585
Matt Carlson411da642009-11-13 13:03:46 +00008586 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e62009-11-13 13:03:49 +00008587 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008588
Joe Perches63c3a662011-04-26 08:12:10 +00008589 tpr->rx_jmb_prod_idx =
8590 tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
Matt Carlson66711e62009-11-13 13:03:49 +00008591 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008592
Matt Carlson2d31eca2009-09-01 12:53:31 +00008593 tg3_rings_reset(tp);
8594
Linus Torvalds1da177e2005-04-16 15:20:36 -07008595 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07008596 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008597
8598 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00008599 tw32(MAC_RX_MTU_SIZE,
8600 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008601
8602 /* The slot time is changed by tg3_setup_phy if we
8603 * run at gigabit with half duplex.
8604 */
Matt Carlsonf2096f92011-04-05 14:22:48 +00008605 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
8606 (6 << TX_LENGTHS_IPG_SHIFT) |
8607 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
8608
8609 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8610 val |= tr32(MAC_TX_LENGTHS) &
8611 (TX_LENGTHS_JMB_FRM_LEN_MSK |
8612 TX_LENGTHS_CNT_DWN_VAL_MSK);
8613
8614 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008615
8616 /* Receive rules. */
8617 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
8618 tw32(RCVLPC_CONFIG, 0x0181);
8619
8620 /* Calculate RDMAC_MODE setting early, we need it to determine
8621 * the RCVLPC_STATE_ENABLE mask.
8622 */
8623 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
8624 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
8625 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
8626 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
8627 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07008628
Matt Carlsondeabaac2010-11-24 08:31:50 +00008629 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +00008630 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
8631
Matt Carlson57e69832008-05-25 23:48:31 -07008632 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08008633 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8634 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07008635 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
8636 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
8637 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
8638
Matt Carlsonc5908932011-03-09 16:58:25 +00008639 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8640 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00008641 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07008642 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008643 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
8644 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008645 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008646 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8647 }
8648 }
8649
Joe Perches63c3a662011-04-26 08:12:10 +00008650 if (tg3_flag(tp, PCI_EXPRESS))
Michael Chan85e94ce2005-04-21 17:05:28 -07008651 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8652
Joe Perches63c3a662011-04-26 08:12:10 +00008653 if (tg3_flag(tp, HW_TSO_1) ||
8654 tg3_flag(tp, HW_TSO_2) ||
8655 tg3_flag(tp, HW_TSO_3))
Matt Carlson027455a2008-12-21 20:19:30 -08008656 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
8657
Matt Carlson108a6c12011-05-19 12:12:47 +00008658 if (tg3_flag(tp, 57765_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +00008659 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson027455a2008-12-21 20:19:30 -08008660 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
8661 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008662
Matt Carlsonf2096f92011-04-05 14:22:48 +00008663 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8664 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
8665
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008666 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
8667 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
8668 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8669 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008670 tg3_flag(tp, 57765_PLUS)) {
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008671 val = tr32(TG3_RDMA_RSRVCTRL_REG);
Matt Carlsond78b59f2011-04-05 14:22:46 +00008672 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8673 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +00008674 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
8675 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
8676 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
8677 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
8678 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
8679 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +00008680 }
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008681 tw32(TG3_RDMA_RSRVCTRL_REG,
8682 val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
8683 }
8684
Matt Carlsond78b59f2011-04-05 14:22:46 +00008685 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8686 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsond309a462010-09-30 10:34:31 +00008687 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
8688 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val |
8689 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
8690 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
8691 }
8692
Linus Torvalds1da177e2005-04-16 15:20:36 -07008693 /* Receive/send statistics. */
Joe Perches63c3a662011-04-26 08:12:10 +00008694 if (tg3_flag(tp, 5750_PLUS)) {
Michael Chan16613942006-06-29 20:15:13 -07008695 val = tr32(RCVLPC_STATS_ENABLE);
8696 val &= ~RCVLPC_STATSENAB_DACK_FIX;
8697 tw32(RCVLPC_STATS_ENABLE, val);
8698 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008699 tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008700 val = tr32(RCVLPC_STATS_ENABLE);
8701 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
8702 tw32(RCVLPC_STATS_ENABLE, val);
8703 } else {
8704 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
8705 }
8706 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
8707 tw32(SNDDATAI_STATSENAB, 0xffffff);
8708 tw32(SNDDATAI_STATSCTRL,
8709 (SNDDATAI_SCTRL_ENABLE |
8710 SNDDATAI_SCTRL_FASTUPD));
8711
8712 /* Setup host coalescing engine. */
8713 tw32(HOSTCC_MODE, 0);
8714 for (i = 0; i < 2000; i++) {
8715 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
8716 break;
8717 udelay(10);
8718 }
8719
Michael Chand244c892005-07-05 14:42:33 -07008720 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008721
Joe Perches63c3a662011-04-26 08:12:10 +00008722 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008723 /* Status/statistics block address. See tg3_timer,
8724 * the tg3_periodic_fetch_stats call there, and
8725 * tg3_get_stats to see how this works for 5705/5750 chips.
8726 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008727 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8728 ((u64) tp->stats_mapping >> 32));
8729 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8730 ((u64) tp->stats_mapping & 0xffffffff));
8731 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008732
Linus Torvalds1da177e2005-04-16 15:20:36 -07008733 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008734
8735 /* Clear statistics and status block memory areas */
8736 for (i = NIC_SRAM_STATS_BLK;
8737 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
8738 i += sizeof(u32)) {
8739 tg3_write_mem(tp, i, 0);
8740 udelay(40);
8741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008742 }
8743
8744 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
8745
8746 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
8747 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008748 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008749 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
8750
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008751 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
8752 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -07008753 /* reset to prevent losing 1st rx packet intermittently */
8754 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8755 udelay(10);
8756 }
8757
Matt Carlson3bda1252008-08-15 14:08:22 -07008758 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Matt Carlson9e975cc2011-07-20 10:20:50 +00008759 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE |
8760 MAC_MODE_FHDE_ENABLE;
8761 if (tg3_flag(tp, ENABLE_APE))
8762 tp->mac_mode |= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Joe Perches63c3a662011-04-26 08:12:10 +00008763 if (!tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008764 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07008765 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
8766 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008767 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
8768 udelay(40);
8769
Michael Chan314fba32005-04-21 17:07:04 -07008770 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Joe Perches63c3a662011-04-26 08:12:10 +00008771 * If TG3_FLAG_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07008772 * register to preserve the GPIO settings for LOMs. The GPIOs,
8773 * whether used as inputs or outputs, are set by boot code after
8774 * reset.
8775 */
Joe Perches63c3a662011-04-26 08:12:10 +00008776 if (!tg3_flag(tp, IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07008777 u32 gpio_mask;
8778
Michael Chan9d26e212006-12-07 00:21:14 -08008779 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
8780 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
8781 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07008782
8783 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
8784 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
8785 GRC_LCLCTRL_GPIO_OUTPUT3;
8786
Michael Chanaf36e6b2006-03-23 01:28:06 -08008787 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
8788 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
8789
Gary Zambranoaaf84462007-05-05 11:51:45 -07008790 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07008791 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
8792
8793 /* GPIO1 must be driven high for eeprom write protect */
Joe Perches63c3a662011-04-26 08:12:10 +00008794 if (tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan9d26e212006-12-07 00:21:14 -08008795 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
8796 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07008797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008798 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8799 udelay(100);
8800
Joe Perches63c3a662011-04-26 08:12:10 +00008801 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008802 val = tr32(MSGINT_MODE);
8803 val |= MSGINT_MODE_MULTIVEC_EN | MSGINT_MODE_ENABLE;
Matt Carlson5b39de92011-08-31 11:44:50 +00008804 if (!tg3_flag(tp, 1SHOT_MSI))
8805 val |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008806 tw32(MSGINT_MODE, val);
8807 }
8808
Joe Perches63c3a662011-04-26 08:12:10 +00008809 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008810 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
8811 udelay(40);
8812 }
8813
8814 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
8815 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
8816 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
8817 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
8818 WDMAC_MODE_LNGREAD_ENAB);
8819
Matt Carlsonc5908932011-03-09 16:58:25 +00008820 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8821 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00008822 if (tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008823 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
8824 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
8825 /* nothing */
8826 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008827 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008828 val |= WDMAC_MODE_RX_ACCEL;
8829 }
8830 }
8831
Michael Chand9ab5ad2006-03-20 22:27:35 -08008832 /* Enable host coalescing bug fix */
Joe Perches63c3a662011-04-26 08:12:10 +00008833 if (tg3_flag(tp, 5755_PLUS))
Matt Carlsonf51f3562008-05-25 23:45:08 -07008834 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad2006-03-20 22:27:35 -08008835
Matt Carlson788a0352009-11-02 14:26:03 +00008836 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
8837 val |= WDMAC_MODE_BURST_ALL_DATA;
8838
Linus Torvalds1da177e2005-04-16 15:20:36 -07008839 tw32_f(WDMAC_MODE, val);
8840 udelay(40);
8841
Joe Perches63c3a662011-04-26 08:12:10 +00008842 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07008843 u16 pcix_cmd;
8844
8845 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8846 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008847 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07008848 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
8849 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008850 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07008851 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
8852 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008853 }
Matt Carlson9974a352007-10-07 23:27:28 -07008854 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8855 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008856 }
8857
8858 tw32_f(RDMAC_MODE, rdmac_mode);
8859 udelay(40);
8860
8861 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008862 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008863 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07008864
8865 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
8866 tw32(SNDDATAC_MODE,
8867 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
8868 else
8869 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
8870
Linus Torvalds1da177e2005-04-16 15:20:36 -07008871 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
8872 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008873 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00008874 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008875 val |= RCVDBDI_MODE_LRG_RING_SZ;
8876 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008877 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008878 if (tg3_flag(tp, HW_TSO_1) ||
8879 tg3_flag(tp, HW_TSO_2) ||
8880 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008881 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008882 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00008883 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008884 val |= SNDBDI_MODE_MULTI_TXQ_EN;
8885 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008886 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
8887
8888 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
8889 err = tg3_load_5701_a0_firmware_fix(tp);
8890 if (err)
8891 return err;
8892 }
8893
Joe Perches63c3a662011-04-26 08:12:10 +00008894 if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008895 err = tg3_load_tso_firmware(tp);
8896 if (err)
8897 return err;
8898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008899
8900 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +00008901
Joe Perches63c3a662011-04-26 08:12:10 +00008902 if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsonb1d05212010-06-05 17:24:31 +00008903 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
8904 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +00008905
8906 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
8907 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
8908 tp->tx_mode &= ~val;
8909 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
8910 }
8911
Linus Torvalds1da177e2005-04-16 15:20:36 -07008912 tw32_f(MAC_TX_MODE, tp->tx_mode);
8913 udelay(100);
8914
Joe Perches63c3a662011-04-26 08:12:10 +00008915 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson9d53fa12011-07-20 10:20:54 +00008916 int i = 0;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008917 u32 reg = MAC_RSS_INDIR_TBL_0;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008918
Matt Carlson9d53fa12011-07-20 10:20:54 +00008919 if (tp->irq_cnt == 2) {
8920 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i += 8) {
8921 tw32(reg, 0x0);
8922 reg += 4;
8923 }
8924 } else {
8925 u32 val;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008926
Matt Carlson9d53fa12011-07-20 10:20:54 +00008927 while (i < TG3_RSS_INDIR_TBL_SIZE) {
8928 val = i % (tp->irq_cnt - 1);
8929 i++;
8930 for (; i % 8; i++) {
8931 val <<= 4;
8932 val |= (i % (tp->irq_cnt - 1));
8933 }
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008934 tw32(reg, val);
8935 reg += 4;
8936 }
8937 }
8938
8939 /* Setup the "secret" hash key. */
8940 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
8941 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
8942 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
8943 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
8944 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
8945 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
8946 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
8947 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
8948 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
8949 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
8950 }
8951
Linus Torvalds1da177e2005-04-16 15:20:36 -07008952 tp->rx_mode = RX_MODE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00008953 if (tg3_flag(tp, 5755_PLUS))
Michael Chanaf36e6b2006-03-23 01:28:06 -08008954 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
8955
Joe Perches63c3a662011-04-26 08:12:10 +00008956 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008957 tp->rx_mode |= RX_MODE_RSS_ENABLE |
8958 RX_MODE_RSS_ITBL_HASH_BITS_7 |
8959 RX_MODE_RSS_IPV6_HASH_EN |
8960 RX_MODE_RSS_TCP_IPV6_HASH_EN |
8961 RX_MODE_RSS_IPV4_HASH_EN |
8962 RX_MODE_RSS_TCP_IPV4_HASH_EN;
8963
Linus Torvalds1da177e2005-04-16 15:20:36 -07008964 tw32_f(MAC_RX_MODE, tp->rx_mode);
8965 udelay(10);
8966
Linus Torvalds1da177e2005-04-16 15:20:36 -07008967 tw32(MAC_LED_CTRL, tp->led_ctrl);
8968
8969 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008970 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008971 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8972 udelay(10);
8973 }
8974 tw32_f(MAC_RX_MODE, tp->rx_mode);
8975 udelay(10);
8976
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008977 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008978 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008979 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008980 /* Set drive transmission level to 1.2V */
8981 /* only if the signal pre-emphasis bit is not set */
8982 val = tr32(MAC_SERDES_CFG);
8983 val &= 0xfffff000;
8984 val |= 0x880;
8985 tw32(MAC_SERDES_CFG, val);
8986 }
8987 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
8988 tw32(MAC_SERDES_CFG, 0x616000);
8989 }
8990
8991 /* Prevent chip from dropping frames when flow control
8992 * is enabled.
8993 */
Matt Carlson666bc832010-01-20 16:58:03 +00008994 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8995 val = 1;
8996 else
8997 val = 2;
8998 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008999
9000 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009001 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009002 /* Use hardware link auto-negotiation */
Joe Perches63c3a662011-04-26 08:12:10 +00009003 tg3_flag_set(tp, HW_AUTONEG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009004 }
9005
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009006 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +00009007 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08009008 u32 tmp;
9009
9010 tmp = tr32(SERDES_RX_CTRL);
9011 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
9012 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
9013 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
9014 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
9015 }
9016
Joe Perches63c3a662011-04-26 08:12:10 +00009017 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson80096062010-08-02 11:26:06 +00009018 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
9019 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07009020 tp->link_config.speed = tp->link_config.orig_speed;
9021 tp->link_config.duplex = tp->link_config.orig_duplex;
9022 tp->link_config.autoneg = tp->link_config.orig_autoneg;
9023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009024
Matt Carlsondd477002008-05-25 23:45:58 -07009025 err = tg3_setup_phy(tp, 0);
9026 if (err)
9027 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009028
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009029 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
9030 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07009031 u32 tmp;
9032
9033 /* Clear CRC stats. */
9034 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
9035 tg3_writephy(tp, MII_TG3_TEST1,
9036 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009037 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -07009038 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009039 }
9040 }
9041
9042 __tg3_set_rx_mode(tp->dev);
9043
9044 /* Initialize receive rules. */
9045 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
9046 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
9047 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
9048 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
9049
Joe Perches63c3a662011-04-26 08:12:10 +00009050 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009051 limit = 8;
9052 else
9053 limit = 16;
Joe Perches63c3a662011-04-26 08:12:10 +00009054 if (tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009055 limit -= 4;
9056 switch (limit) {
9057 case 16:
9058 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
9059 case 15:
9060 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
9061 case 14:
9062 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
9063 case 13:
9064 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
9065 case 12:
9066 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
9067 case 11:
9068 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
9069 case 10:
9070 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
9071 case 9:
9072 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
9073 case 8:
9074 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
9075 case 7:
9076 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
9077 case 6:
9078 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
9079 case 5:
9080 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
9081 case 4:
9082 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
9083 case 3:
9084 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
9085 case 2:
9086 case 1:
9087
9088 default:
9089 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07009090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009091
Joe Perches63c3a662011-04-26 08:12:10 +00009092 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson9ce768e2007-10-11 19:49:11 -07009093 /* Write our heartbeat update interval to APE. */
9094 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
9095 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07009096
Linus Torvalds1da177e2005-04-16 15:20:36 -07009097 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
9098
Linus Torvalds1da177e2005-04-16 15:20:36 -07009099 return 0;
9100}
9101
9102/* Called at device open time to get the chip ready for
9103 * packet processing. Invoked with tp->lock held.
9104 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009105static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009106{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009107 tg3_switch_clocks(tp);
9108
9109 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
9110
Matt Carlson2f751b62008-08-04 23:17:34 -07009111 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009112}
9113
9114#define TG3_STAT_ADD32(PSTAT, REG) \
9115do { u32 __val = tr32(REG); \
9116 (PSTAT)->low += __val; \
9117 if ((PSTAT)->low < __val) \
9118 (PSTAT)->high += 1; \
9119} while (0)
9120
9121static void tg3_periodic_fetch_stats(struct tg3 *tp)
9122{
9123 struct tg3_hw_stats *sp = tp->hw_stats;
9124
9125 if (!netif_carrier_ok(tp->dev))
9126 return;
9127
9128 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
9129 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
9130 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
9131 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
9132 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
9133 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
9134 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
9135 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
9136 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
9137 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
9138 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
9139 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
9140 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
9141
9142 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
9143 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
9144 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
9145 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
9146 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
9147 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
9148 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
9149 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
9150 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
9151 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
9152 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
9153 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
9154 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
9155 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07009156
9157 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
Matt Carlson310050f2011-05-19 12:12:55 +00009158 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
9159 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0 &&
9160 tp->pci_chip_rev_id != CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +00009161 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
9162 } else {
9163 u32 val = tr32(HOSTCC_FLOW_ATTN);
9164 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
9165 if (val) {
9166 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
9167 sp->rx_discards.low += val;
9168 if (sp->rx_discards.low < val)
9169 sp->rx_discards.high += 1;
9170 }
9171 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
9172 }
Michael Chan463d3052006-05-22 16:36:27 -07009173 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009174}
9175
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009176static void tg3_chk_missed_msi(struct tg3 *tp)
9177{
9178 u32 i;
9179
9180 for (i = 0; i < tp->irq_cnt; i++) {
9181 struct tg3_napi *tnapi = &tp->napi[i];
9182
9183 if (tg3_has_work(tnapi)) {
9184 if (tnapi->last_rx_cons == tnapi->rx_rcb_ptr &&
9185 tnapi->last_tx_cons == tnapi->tx_cons) {
9186 if (tnapi->chk_msi_cnt < 1) {
9187 tnapi->chk_msi_cnt++;
9188 return;
9189 }
Matt Carlson7f230732011-08-31 11:44:48 +00009190 tg3_msi(0, tnapi);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009191 }
9192 }
9193 tnapi->chk_msi_cnt = 0;
9194 tnapi->last_rx_cons = tnapi->rx_rcb_ptr;
9195 tnapi->last_tx_cons = tnapi->tx_cons;
9196 }
9197}
9198
Linus Torvalds1da177e2005-04-16 15:20:36 -07009199static void tg3_timer(unsigned long __opaque)
9200{
9201 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009202
Michael Chanf475f162006-03-27 23:20:14 -08009203 if (tp->irq_sync)
9204 goto restart_timer;
9205
David S. Millerf47c11e2005-06-24 20:18:35 -07009206 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009207
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009208 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
9209 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
9210 tg3_chk_missed_msi(tp);
9211
Joe Perches63c3a662011-04-26 08:12:10 +00009212 if (!tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -07009213 /* All of this garbage is because when using non-tagged
9214 * IRQ status the mailbox/status_block protocol the chip
9215 * uses with the cpu is race prone.
9216 */
Matt Carlson898a56f2009-08-28 14:02:40 +00009217 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -07009218 tw32(GRC_LOCAL_CTRL,
9219 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
9220 } else {
9221 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00009222 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -07009223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009224
David S. Millerfac9b832005-05-18 22:46:34 -07009225 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +00009226 tg3_flag_set(tp, RESTART_TIMER);
David S. Millerf47c11e2005-06-24 20:18:35 -07009227 spin_unlock(&tp->lock);
David S. Millerfac9b832005-05-18 22:46:34 -07009228 schedule_work(&tp->reset_task);
9229 return;
9230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009231 }
9232
Linus Torvalds1da177e2005-04-16 15:20:36 -07009233 /* This part only runs once per second. */
9234 if (!--tp->timer_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00009235 if (tg3_flag(tp, 5705_PLUS))
David S. Millerfac9b832005-05-18 22:46:34 -07009236 tg3_periodic_fetch_stats(tp);
9237
Matt Carlsonb0c59432011-05-19 12:12:48 +00009238 if (tp->setlpicnt && !--tp->setlpicnt)
9239 tg3_phy_eee_enable(tp);
Matt Carlson52b02d02010-10-14 10:37:41 +00009240
Joe Perches63c3a662011-04-26 08:12:10 +00009241 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009242 u32 mac_stat;
9243 int phy_event;
9244
9245 mac_stat = tr32(MAC_STATUS);
9246
9247 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009248 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009249 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
9250 phy_event = 1;
9251 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
9252 phy_event = 1;
9253
9254 if (phy_event)
9255 tg3_setup_phy(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +00009256 } else if (tg3_flag(tp, POLL_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009257 u32 mac_stat = tr32(MAC_STATUS);
9258 int need_setup = 0;
9259
9260 if (netif_carrier_ok(tp->dev) &&
9261 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
9262 need_setup = 1;
9263 }
Matt Carlsonbe98da62010-07-11 09:31:46 +00009264 if (!netif_carrier_ok(tp->dev) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009265 (mac_stat & (MAC_STATUS_PCS_SYNCED |
9266 MAC_STATUS_SIGNAL_DET))) {
9267 need_setup = 1;
9268 }
9269 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07009270 if (!tp->serdes_counter) {
9271 tw32_f(MAC_MODE,
9272 (tp->mac_mode &
9273 ~MAC_MODE_PORT_MODE_MASK));
9274 udelay(40);
9275 tw32_f(MAC_MODE, tp->mac_mode);
9276 udelay(40);
9277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009278 tg3_setup_phy(tp, 0);
9279 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009280 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009281 tg3_flag(tp, 5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -07009282 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +00009283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009284
9285 tp->timer_counter = tp->timer_multiplier;
9286 }
9287
Michael Chan130b8e42006-09-27 16:00:40 -07009288 /* Heartbeat is only sent once every 2 seconds.
9289 *
9290 * The heartbeat is to tell the ASF firmware that the host
9291 * driver is still alive. In the event that the OS crashes,
9292 * ASF needs to reset the hardware to free up the FIFO space
9293 * that may be filled with rx packets destined for the host.
9294 * If the FIFO is full, ASF will no longer function properly.
9295 *
9296 * Unintended resets have been reported on real time kernels
9297 * where the timer doesn't run on time. Netpoll will also have
9298 * same problem.
9299 *
9300 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
9301 * to check the ring condition when the heartbeat is expiring
9302 * before doing the reset. This will prevent most unintended
9303 * resets.
9304 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009305 if (!--tp->asf_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00009306 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07009307 tg3_wait_for_event_ack(tp);
9308
Michael Chanbbadf502006-04-06 21:46:34 -07009309 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07009310 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07009311 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +00009312 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
9313 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -07009314
9315 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009316 }
9317 tp->asf_counter = tp->asf_multiplier;
9318 }
9319
David S. Millerf47c11e2005-06-24 20:18:35 -07009320 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009321
Michael Chanf475f162006-03-27 23:20:14 -08009322restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07009323 tp->timer.expires = jiffies + tp->timer_offset;
9324 add_timer(&tp->timer);
9325}
9326
Matt Carlson4f125f42009-09-01 12:55:02 +00009327static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -08009328{
David Howells7d12e782006-10-05 14:55:46 +01009329 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009330 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +00009331 char *name;
9332 struct tg3_napi *tnapi = &tp->napi[irq_num];
9333
9334 if (tp->irq_cnt == 1)
9335 name = tp->dev->name;
9336 else {
9337 name = &tnapi->irq_lbl[0];
9338 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
9339 name[IFNAMSIZ-1] = 0;
9340 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009341
Joe Perches63c3a662011-04-26 08:12:10 +00009342 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Michael Chanfcfa0a32006-03-20 22:28:41 -08009343 fn = tg3_msi;
Joe Perches63c3a662011-04-26 08:12:10 +00009344 if (tg3_flag(tp, 1SHOT_MSI))
Michael Chanfcfa0a32006-03-20 22:28:41 -08009345 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009346 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009347 } else {
9348 fn = tg3_interrupt;
Joe Perches63c3a662011-04-26 08:12:10 +00009349 if (tg3_flag(tp, TAGGED_STATUS))
Michael Chanfcfa0a32006-03-20 22:28:41 -08009350 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009351 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009352 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009353
9354 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009355}
9356
Michael Chan79381092005-04-21 17:13:59 -07009357static int tg3_test_interrupt(struct tg3 *tp)
9358{
Matt Carlson09943a12009-08-28 14:01:57 +00009359 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -07009360 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -07009361 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009362 u32 val;
Michael Chan79381092005-04-21 17:13:59 -07009363
Michael Chand4bc3922005-05-29 14:59:20 -07009364 if (!netif_running(dev))
9365 return -ENODEV;
9366
Michael Chan79381092005-04-21 17:13:59 -07009367 tg3_disable_ints(tp);
9368
Matt Carlson4f125f42009-09-01 12:55:02 +00009369 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009370
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009371 /*
9372 * Turn off MSI one shot mode. Otherwise this test has no
9373 * observable way to know whether the interrupt was delivered.
9374 */
Matt Carlson3aa1cdf2011-07-20 10:20:55 +00009375 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009376 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
9377 tw32(MSGINT_MODE, val);
9378 }
9379
Matt Carlson4f125f42009-09-01 12:55:02 +00009380 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Matt Carlson09943a12009-08-28 14:01:57 +00009381 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009382 if (err)
9383 return err;
9384
Matt Carlson898a56f2009-08-28 14:02:40 +00009385 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -07009386 tg3_enable_ints(tp);
9387
9388 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00009389 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -07009390
9391 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -07009392 u32 int_mbox, misc_host_ctrl;
9393
Matt Carlson898a56f2009-08-28 14:02:40 +00009394 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -07009395 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
9396
9397 if ((int_mbox != 0) ||
9398 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
9399 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -07009400 break;
Michael Chanb16250e2006-09-27 16:10:14 -07009401 }
9402
Matt Carlson3aa1cdf2011-07-20 10:20:55 +00009403 if (tg3_flag(tp, 57765_PLUS) &&
9404 tnapi->hw_status->status_tag != tnapi->last_tag)
9405 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
9406
Michael Chan79381092005-04-21 17:13:59 -07009407 msleep(10);
9408 }
9409
9410 tg3_disable_ints(tp);
9411
Matt Carlson4f125f42009-09-01 12:55:02 +00009412 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009413
Matt Carlson4f125f42009-09-01 12:55:02 +00009414 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009415
9416 if (err)
9417 return err;
9418
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009419 if (intr_ok) {
9420 /* Reenable MSI one shot mode. */
Matt Carlson5b39de92011-08-31 11:44:50 +00009421 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, 1SHOT_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009422 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
9423 tw32(MSGINT_MODE, val);
9424 }
Michael Chan79381092005-04-21 17:13:59 -07009425 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009426 }
Michael Chan79381092005-04-21 17:13:59 -07009427
9428 return -EIO;
9429}
9430
9431/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
9432 * successfully restored
9433 */
9434static int tg3_test_msi(struct tg3 *tp)
9435{
Michael Chan79381092005-04-21 17:13:59 -07009436 int err;
9437 u16 pci_cmd;
9438
Joe Perches63c3a662011-04-26 08:12:10 +00009439 if (!tg3_flag(tp, USING_MSI))
Michael Chan79381092005-04-21 17:13:59 -07009440 return 0;
9441
9442 /* Turn off SERR reporting in case MSI terminates with Master
9443 * Abort.
9444 */
9445 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
9446 pci_write_config_word(tp->pdev, PCI_COMMAND,
9447 pci_cmd & ~PCI_COMMAND_SERR);
9448
9449 err = tg3_test_interrupt(tp);
9450
9451 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
9452
9453 if (!err)
9454 return 0;
9455
9456 /* other failures */
9457 if (err != -EIO)
9458 return err;
9459
9460 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009461 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
9462 "to INTx mode. Please report this failure to the PCI "
9463 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -07009464
Matt Carlson4f125f42009-09-01 12:55:02 +00009465 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +00009466
Michael Chan79381092005-04-21 17:13:59 -07009467 pci_disable_msi(tp->pdev);
9468
Joe Perches63c3a662011-04-26 08:12:10 +00009469 tg3_flag_clear(tp, USING_MSI);
Andre Detschdc8bf1b2010-04-26 07:27:07 +00009470 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -07009471
Matt Carlson4f125f42009-09-01 12:55:02 +00009472 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009473 if (err)
9474 return err;
9475
9476 /* Need to reset the chip because the MSI cycle may have terminated
9477 * with Master Abort.
9478 */
David S. Millerf47c11e2005-06-24 20:18:35 -07009479 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009480
Michael Chan944d9802005-05-29 14:57:48 -07009481 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009482 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009483
David S. Millerf47c11e2005-06-24 20:18:35 -07009484 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009485
9486 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +00009487 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -07009488
9489 return err;
9490}
9491
Matt Carlson9e9fd122009-01-19 16:57:45 -08009492static int tg3_request_firmware(struct tg3 *tp)
9493{
9494 const __be32 *fw_data;
9495
9496 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009497 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
9498 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009499 return -ENOENT;
9500 }
9501
9502 fw_data = (void *)tp->fw->data;
9503
9504 /* Firmware blob starts with version numbers, followed by
9505 * start address and _full_ length including BSS sections
9506 * (which must be longer than the actual data, of course
9507 */
9508
9509 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
9510 if (tp->fw_len < (tp->fw->size - 12)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009511 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
9512 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009513 release_firmware(tp->fw);
9514 tp->fw = NULL;
9515 return -EINVAL;
9516 }
9517
9518 /* We no longer need firmware; we have it. */
9519 tp->fw_needed = NULL;
9520 return 0;
9521}
9522
Matt Carlson679563f2009-09-01 12:55:46 +00009523static bool tg3_enable_msix(struct tg3 *tp)
9524{
9525 int i, rc, cpus = num_online_cpus();
9526 struct msix_entry msix_ent[tp->irq_max];
9527
9528 if (cpus == 1)
9529 /* Just fallback to the simpler MSI mode. */
9530 return false;
9531
9532 /*
9533 * We want as many rx rings enabled as there are cpus.
9534 * The first MSIX vector only deals with link interrupts, etc,
9535 * so we add one to the number of vectors we are requesting.
9536 */
9537 tp->irq_cnt = min_t(unsigned, cpus + 1, tp->irq_max);
9538
9539 for (i = 0; i < tp->irq_max; i++) {
9540 msix_ent[i].entry = i;
9541 msix_ent[i].vector = 0;
9542 }
9543
9544 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +00009545 if (rc < 0) {
9546 return false;
9547 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +00009548 if (pci_enable_msix(tp->pdev, msix_ent, rc))
9549 return false;
Joe Perches05dbe002010-02-17 19:44:19 +00009550 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
9551 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +00009552 tp->irq_cnt = rc;
9553 }
9554
9555 for (i = 0; i < tp->irq_max; i++)
9556 tp->napi[i].irq_vec = msix_ent[i].vector;
9557
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009558 netif_set_real_num_tx_queues(tp->dev, 1);
9559 rc = tp->irq_cnt > 1 ? tp->irq_cnt - 1 : 1;
9560 if (netif_set_real_num_rx_queues(tp->dev, rc)) {
9561 pci_disable_msix(tp->pdev);
9562 return false;
9563 }
Matt Carlsonb92b9042010-11-24 08:31:51 +00009564
9565 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +00009566 tg3_flag_set(tp, ENABLE_RSS);
Matt Carlsond78b59f2011-04-05 14:22:46 +00009567
9568 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
9569 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Joe Perches63c3a662011-04-26 08:12:10 +00009570 tg3_flag_set(tp, ENABLE_TSS);
Matt Carlsonb92b9042010-11-24 08:31:51 +00009571 netif_set_real_num_tx_queues(tp->dev, tp->irq_cnt - 1);
9572 }
9573 }
Matt Carlson2430b032010-06-05 17:24:34 +00009574
Matt Carlson679563f2009-09-01 12:55:46 +00009575 return true;
9576}
9577
Matt Carlson07b01732009-08-28 14:01:15 +00009578static void tg3_ints_init(struct tg3 *tp)
9579{
Joe Perches63c3a662011-04-26 08:12:10 +00009580 if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
9581 !tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +00009582 /* All MSI supporting chips should support tagged
9583 * status. Assert that this is the case.
9584 */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009585 netdev_warn(tp->dev,
9586 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +00009587 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +00009588 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009589
Joe Perches63c3a662011-04-26 08:12:10 +00009590 if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
9591 tg3_flag_set(tp, USING_MSIX);
9592 else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
9593 tg3_flag_set(tp, USING_MSI);
Matt Carlson679563f2009-09-01 12:55:46 +00009594
Joe Perches63c3a662011-04-26 08:12:10 +00009595 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009596 u32 msi_mode = tr32(MSGINT_MODE);
Joe Perches63c3a662011-04-26 08:12:10 +00009597 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009598 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +00009599 if (!tg3_flag(tp, 1SHOT_MSI))
9600 msi_mode |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlson679563f2009-09-01 12:55:46 +00009601 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
9602 }
9603defcfg:
Joe Perches63c3a662011-04-26 08:12:10 +00009604 if (!tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009605 tp->irq_cnt = 1;
9606 tp->napi[0].irq_vec = tp->pdev->irq;
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009607 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -07009608 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +00009609 }
Matt Carlson07b01732009-08-28 14:01:15 +00009610}
9611
9612static void tg3_ints_fini(struct tg3 *tp)
9613{
Joe Perches63c3a662011-04-26 08:12:10 +00009614 if (tg3_flag(tp, USING_MSIX))
Matt Carlson679563f2009-09-01 12:55:46 +00009615 pci_disable_msix(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009616 else if (tg3_flag(tp, USING_MSI))
Matt Carlson679563f2009-09-01 12:55:46 +00009617 pci_disable_msi(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009618 tg3_flag_clear(tp, USING_MSI);
9619 tg3_flag_clear(tp, USING_MSIX);
9620 tg3_flag_clear(tp, ENABLE_RSS);
9621 tg3_flag_clear(tp, ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +00009622}
9623
Linus Torvalds1da177e2005-04-16 15:20:36 -07009624static int tg3_open(struct net_device *dev)
9625{
9626 struct tg3 *tp = netdev_priv(dev);
Matt Carlson4f125f42009-09-01 12:55:02 +00009627 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009628
Matt Carlson9e9fd122009-01-19 16:57:45 -08009629 if (tp->fw_needed) {
9630 err = tg3_request_firmware(tp);
9631 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
9632 if (err)
9633 return err;
9634 } else if (err) {
Joe Perches05dbe002010-02-17 19:44:19 +00009635 netdev_warn(tp->dev, "TSO capability disabled\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009636 tg3_flag_clear(tp, TSO_CAPABLE);
9637 } else if (!tg3_flag(tp, TSO_CAPABLE)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009638 netdev_notice(tp->dev, "TSO capability restored\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009639 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009640 }
9641 }
9642
Michael Chanc49a1562006-12-17 17:07:29 -08009643 netif_carrier_off(tp->dev);
9644
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009645 err = tg3_power_up(tp);
Matt Carlson2f751b62008-08-04 23:17:34 -07009646 if (err)
Michael Chanbc1c7562006-03-20 17:48:03 -08009647 return err;
Matt Carlson2f751b62008-08-04 23:17:34 -07009648
9649 tg3_full_lock(tp, 0);
Michael Chanbc1c7562006-03-20 17:48:03 -08009650
Linus Torvalds1da177e2005-04-16 15:20:36 -07009651 tg3_disable_ints(tp);
Joe Perches63c3a662011-04-26 08:12:10 +00009652 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009653
David S. Millerf47c11e2005-06-24 20:18:35 -07009654 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009655
Matt Carlson679563f2009-09-01 12:55:46 +00009656 /*
9657 * Setup interrupts first so we know how
9658 * many NAPI resources to allocate
9659 */
9660 tg3_ints_init(tp);
9661
Linus Torvalds1da177e2005-04-16 15:20:36 -07009662 /* The placement of this call is tied
9663 * to the setup and use of Host TX descriptors.
9664 */
9665 err = tg3_alloc_consistent(tp);
9666 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009667 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009668
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009669 tg3_napi_init(tp);
9670
Matt Carlsonfed97812009-09-01 13:10:19 +00009671 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07009672
Matt Carlson4f125f42009-09-01 12:55:02 +00009673 for (i = 0; i < tp->irq_cnt; i++) {
9674 struct tg3_napi *tnapi = &tp->napi[i];
9675 err = tg3_request_irq(tp, i);
9676 if (err) {
9677 for (i--; i >= 0; i--)
9678 free_irq(tnapi->irq_vec, tnapi);
9679 break;
9680 }
9681 }
Matt Carlson07b01732009-08-28 14:01:15 +00009682
9683 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009684 goto err_out2;
Matt Carlson07b01732009-08-28 14:01:15 +00009685
David S. Millerf47c11e2005-06-24 20:18:35 -07009686 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009687
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009688 err = tg3_init_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009689 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -07009690 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009691 tg3_free_rings(tp);
9692 } else {
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009693 if (tg3_flag(tp, TAGGED_STATUS) &&
9694 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
9695 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57765)
David S. Millerfac9b832005-05-18 22:46:34 -07009696 tp->timer_offset = HZ;
9697 else
9698 tp->timer_offset = HZ / 10;
9699
9700 BUG_ON(tp->timer_offset > HZ);
9701 tp->timer_counter = tp->timer_multiplier =
9702 (HZ / tp->timer_offset);
9703 tp->asf_counter = tp->asf_multiplier =
Michael Chan28fbef72005-10-26 15:48:35 -07009704 ((HZ / tp->timer_offset) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009705
9706 init_timer(&tp->timer);
9707 tp->timer.expires = jiffies + tp->timer_offset;
9708 tp->timer.data = (unsigned long) tp;
9709 tp->timer.function = tg3_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009710 }
9711
David S. Millerf47c11e2005-06-24 20:18:35 -07009712 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009713
Matt Carlson07b01732009-08-28 14:01:15 +00009714 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009715 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009716
Joe Perches63c3a662011-04-26 08:12:10 +00009717 if (tg3_flag(tp, USING_MSI)) {
Michael Chan79381092005-04-21 17:13:59 -07009718 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -07009719
Michael Chan79381092005-04-21 17:13:59 -07009720 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -07009721 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -07009722 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -07009723 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07009724 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009725
Matt Carlson679563f2009-09-01 12:55:46 +00009726 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -07009727 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009728
Joe Perches63c3a662011-04-26 08:12:10 +00009729 if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009730 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009731
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009732 tw32(PCIE_TRANSACTION_CFG,
9733 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009734 }
Michael Chan79381092005-04-21 17:13:59 -07009735 }
9736
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009737 tg3_phy_start(tp);
9738
David S. Millerf47c11e2005-06-24 20:18:35 -07009739 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009740
Michael Chan79381092005-04-21 17:13:59 -07009741 add_timer(&tp->timer);
Joe Perches63c3a662011-04-26 08:12:10 +00009742 tg3_flag_set(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009743 tg3_enable_ints(tp);
9744
David S. Millerf47c11e2005-06-24 20:18:35 -07009745 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009746
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009747 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009748
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00009749 /*
9750 * Reset loopback feature if it was turned on while the device was down
9751 * make sure that it's installed properly now.
9752 */
9753 if (dev->features & NETIF_F_LOOPBACK)
9754 tg3_set_loopback(dev, dev->features);
9755
Linus Torvalds1da177e2005-04-16 15:20:36 -07009756 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +00009757
Matt Carlson679563f2009-09-01 12:55:46 +00009758err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +00009759 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9760 struct tg3_napi *tnapi = &tp->napi[i];
9761 free_irq(tnapi->irq_vec, tnapi);
9762 }
Matt Carlson07b01732009-08-28 14:01:15 +00009763
Matt Carlson679563f2009-09-01 12:55:46 +00009764err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +00009765 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009766 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00009767 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +00009768
9769err_out1:
9770 tg3_ints_fini(tp);
Matt Carlsoncd0d7222011-07-13 09:27:33 +00009771 tg3_frob_aux_power(tp, false);
9772 pci_set_power_state(tp->pdev, PCI_D3hot);
Matt Carlson07b01732009-08-28 14:01:15 +00009773 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009774}
9775
Eric Dumazet511d2222010-07-07 20:44:24 +00009776static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *,
9777 struct rtnl_link_stats64 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009778static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *);
9779
9780static int tg3_close(struct net_device *dev)
9781{
Matt Carlson4f125f42009-09-01 12:55:02 +00009782 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009783 struct tg3 *tp = netdev_priv(dev);
9784
Matt Carlsonfed97812009-09-01 13:10:19 +00009785 tg3_napi_disable(tp);
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07009786 cancel_work_sync(&tp->reset_task);
Michael Chan7faa0062006-02-02 17:29:28 -08009787
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009788 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009789
9790 del_timer_sync(&tp->timer);
9791
Matt Carlson24bb4fb2009-10-05 17:55:29 +00009792 tg3_phy_stop(tp);
9793
David S. Millerf47c11e2005-06-24 20:18:35 -07009794 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009795
9796 tg3_disable_ints(tp);
9797
Michael Chan944d9802005-05-29 14:57:48 -07009798 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009799 tg3_free_rings(tp);
Joe Perches63c3a662011-04-26 08:12:10 +00009800 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009801
David S. Millerf47c11e2005-06-24 20:18:35 -07009802 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009803
Matt Carlson4f125f42009-09-01 12:55:02 +00009804 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9805 struct tg3_napi *tnapi = &tp->napi[i];
9806 free_irq(tnapi->irq_vec, tnapi);
9807 }
Matt Carlson07b01732009-08-28 14:01:15 +00009808
9809 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009810
Eric Dumazet511d2222010-07-07 20:44:24 +00009811 tg3_get_stats64(tp->dev, &tp->net_stats_prev);
9812
Linus Torvalds1da177e2005-04-16 15:20:36 -07009813 memcpy(&tp->estats_prev, tg3_get_estats(tp),
9814 sizeof(tp->estats_prev));
9815
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009816 tg3_napi_fini(tp);
9817
Linus Torvalds1da177e2005-04-16 15:20:36 -07009818 tg3_free_consistent(tp);
9819
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009820 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -08009821
9822 netif_carrier_off(tp->dev);
9823
Linus Torvalds1da177e2005-04-16 15:20:36 -07009824 return 0;
9825}
9826
Eric Dumazet511d2222010-07-07 20:44:24 +00009827static inline u64 get_stat64(tg3_stat64_t *val)
Stefan Buehler816f8b82008-08-15 14:10:54 -07009828{
9829 return ((u64)val->high << 32) | ((u64)val->low);
9830}
9831
Eric Dumazet511d2222010-07-07 20:44:24 +00009832static u64 calc_crc_errors(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009833{
9834 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9835
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009836 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009837 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
9838 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009839 u32 val;
9840
David S. Millerf47c11e2005-06-24 20:18:35 -07009841 spin_lock_bh(&tp->lock);
Michael Chan569a5df2007-02-13 12:18:15 -08009842 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
9843 tg3_writephy(tp, MII_TG3_TEST1,
9844 val | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009845 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009846 } else
9847 val = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07009848 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009849
9850 tp->phy_crc_errors += val;
9851
9852 return tp->phy_crc_errors;
9853 }
9854
9855 return get_stat64(&hw_stats->rx_fcs_errors);
9856}
9857
9858#define ESTAT_ADD(member) \
9859 estats->member = old_estats->member + \
Eric Dumazet511d2222010-07-07 20:44:24 +00009860 get_stat64(&hw_stats->member)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009861
9862static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *tp)
9863{
9864 struct tg3_ethtool_stats *estats = &tp->estats;
9865 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
9866 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9867
9868 if (!hw_stats)
9869 return old_estats;
9870
9871 ESTAT_ADD(rx_octets);
9872 ESTAT_ADD(rx_fragments);
9873 ESTAT_ADD(rx_ucast_packets);
9874 ESTAT_ADD(rx_mcast_packets);
9875 ESTAT_ADD(rx_bcast_packets);
9876 ESTAT_ADD(rx_fcs_errors);
9877 ESTAT_ADD(rx_align_errors);
9878 ESTAT_ADD(rx_xon_pause_rcvd);
9879 ESTAT_ADD(rx_xoff_pause_rcvd);
9880 ESTAT_ADD(rx_mac_ctrl_rcvd);
9881 ESTAT_ADD(rx_xoff_entered);
9882 ESTAT_ADD(rx_frame_too_long_errors);
9883 ESTAT_ADD(rx_jabbers);
9884 ESTAT_ADD(rx_undersize_packets);
9885 ESTAT_ADD(rx_in_length_errors);
9886 ESTAT_ADD(rx_out_length_errors);
9887 ESTAT_ADD(rx_64_or_less_octet_packets);
9888 ESTAT_ADD(rx_65_to_127_octet_packets);
9889 ESTAT_ADD(rx_128_to_255_octet_packets);
9890 ESTAT_ADD(rx_256_to_511_octet_packets);
9891 ESTAT_ADD(rx_512_to_1023_octet_packets);
9892 ESTAT_ADD(rx_1024_to_1522_octet_packets);
9893 ESTAT_ADD(rx_1523_to_2047_octet_packets);
9894 ESTAT_ADD(rx_2048_to_4095_octet_packets);
9895 ESTAT_ADD(rx_4096_to_8191_octet_packets);
9896 ESTAT_ADD(rx_8192_to_9022_octet_packets);
9897
9898 ESTAT_ADD(tx_octets);
9899 ESTAT_ADD(tx_collisions);
9900 ESTAT_ADD(tx_xon_sent);
9901 ESTAT_ADD(tx_xoff_sent);
9902 ESTAT_ADD(tx_flow_control);
9903 ESTAT_ADD(tx_mac_errors);
9904 ESTAT_ADD(tx_single_collisions);
9905 ESTAT_ADD(tx_mult_collisions);
9906 ESTAT_ADD(tx_deferred);
9907 ESTAT_ADD(tx_excessive_collisions);
9908 ESTAT_ADD(tx_late_collisions);
9909 ESTAT_ADD(tx_collide_2times);
9910 ESTAT_ADD(tx_collide_3times);
9911 ESTAT_ADD(tx_collide_4times);
9912 ESTAT_ADD(tx_collide_5times);
9913 ESTAT_ADD(tx_collide_6times);
9914 ESTAT_ADD(tx_collide_7times);
9915 ESTAT_ADD(tx_collide_8times);
9916 ESTAT_ADD(tx_collide_9times);
9917 ESTAT_ADD(tx_collide_10times);
9918 ESTAT_ADD(tx_collide_11times);
9919 ESTAT_ADD(tx_collide_12times);
9920 ESTAT_ADD(tx_collide_13times);
9921 ESTAT_ADD(tx_collide_14times);
9922 ESTAT_ADD(tx_collide_15times);
9923 ESTAT_ADD(tx_ucast_packets);
9924 ESTAT_ADD(tx_mcast_packets);
9925 ESTAT_ADD(tx_bcast_packets);
9926 ESTAT_ADD(tx_carrier_sense_errors);
9927 ESTAT_ADD(tx_discards);
9928 ESTAT_ADD(tx_errors);
9929
9930 ESTAT_ADD(dma_writeq_full);
9931 ESTAT_ADD(dma_write_prioq_full);
9932 ESTAT_ADD(rxbds_empty);
9933 ESTAT_ADD(rx_discards);
9934 ESTAT_ADD(rx_errors);
9935 ESTAT_ADD(rx_threshold_hit);
9936
9937 ESTAT_ADD(dma_readq_full);
9938 ESTAT_ADD(dma_read_prioq_full);
9939 ESTAT_ADD(tx_comp_queue_full);
9940
9941 ESTAT_ADD(ring_set_send_prod_index);
9942 ESTAT_ADD(ring_status_update);
9943 ESTAT_ADD(nic_irqs);
9944 ESTAT_ADD(nic_avoided_irqs);
9945 ESTAT_ADD(nic_tx_threshold_hit);
9946
Matt Carlson4452d092011-05-19 12:12:51 +00009947 ESTAT_ADD(mbuf_lwm_thresh_hit);
9948
Linus Torvalds1da177e2005-04-16 15:20:36 -07009949 return estats;
9950}
9951
Eric Dumazet511d2222010-07-07 20:44:24 +00009952static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
9953 struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009954{
9955 struct tg3 *tp = netdev_priv(dev);
Eric Dumazet511d2222010-07-07 20:44:24 +00009956 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009957 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9958
9959 if (!hw_stats)
9960 return old_stats;
9961
9962 stats->rx_packets = old_stats->rx_packets +
9963 get_stat64(&hw_stats->rx_ucast_packets) +
9964 get_stat64(&hw_stats->rx_mcast_packets) +
9965 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009966
Linus Torvalds1da177e2005-04-16 15:20:36 -07009967 stats->tx_packets = old_stats->tx_packets +
9968 get_stat64(&hw_stats->tx_ucast_packets) +
9969 get_stat64(&hw_stats->tx_mcast_packets) +
9970 get_stat64(&hw_stats->tx_bcast_packets);
9971
9972 stats->rx_bytes = old_stats->rx_bytes +
9973 get_stat64(&hw_stats->rx_octets);
9974 stats->tx_bytes = old_stats->tx_bytes +
9975 get_stat64(&hw_stats->tx_octets);
9976
9977 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -07009978 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009979 stats->tx_errors = old_stats->tx_errors +
9980 get_stat64(&hw_stats->tx_errors) +
9981 get_stat64(&hw_stats->tx_mac_errors) +
9982 get_stat64(&hw_stats->tx_carrier_sense_errors) +
9983 get_stat64(&hw_stats->tx_discards);
9984
9985 stats->multicast = old_stats->multicast +
9986 get_stat64(&hw_stats->rx_mcast_packets);
9987 stats->collisions = old_stats->collisions +
9988 get_stat64(&hw_stats->tx_collisions);
9989
9990 stats->rx_length_errors = old_stats->rx_length_errors +
9991 get_stat64(&hw_stats->rx_frame_too_long_errors) +
9992 get_stat64(&hw_stats->rx_undersize_packets);
9993
9994 stats->rx_over_errors = old_stats->rx_over_errors +
9995 get_stat64(&hw_stats->rxbds_empty);
9996 stats->rx_frame_errors = old_stats->rx_frame_errors +
9997 get_stat64(&hw_stats->rx_align_errors);
9998 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
9999 get_stat64(&hw_stats->tx_discards);
10000 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
10001 get_stat64(&hw_stats->tx_carrier_sense_errors);
10002
10003 stats->rx_crc_errors = old_stats->rx_crc_errors +
10004 calc_crc_errors(tp);
10005
John W. Linville4f63b872005-09-12 14:43:18 -070010006 stats->rx_missed_errors = old_stats->rx_missed_errors +
10007 get_stat64(&hw_stats->rx_discards);
10008
Eric Dumazetb0057c52010-10-10 19:55:52 +000010009 stats->rx_dropped = tp->rx_dropped;
Eric Dumazet48855432011-10-24 07:53:03 +000010010 stats->tx_dropped = tp->tx_dropped;
Eric Dumazetb0057c52010-10-10 19:55:52 +000010011
Linus Torvalds1da177e2005-04-16 15:20:36 -070010012 return stats;
10013}
10014
10015static inline u32 calc_crc(unsigned char *buf, int len)
10016{
10017 u32 reg;
10018 u32 tmp;
10019 int j, k;
10020
10021 reg = 0xffffffff;
10022
10023 for (j = 0; j < len; j++) {
10024 reg ^= buf[j];
10025
10026 for (k = 0; k < 8; k++) {
10027 tmp = reg & 0x01;
10028
10029 reg >>= 1;
10030
Matt Carlson859a5882010-04-05 10:19:28 +000010031 if (tmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010032 reg ^= 0xedb88320;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010033 }
10034 }
10035
10036 return ~reg;
10037}
10038
10039static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
10040{
10041 /* accept or reject all multicast frames */
10042 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
10043 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
10044 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
10045 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
10046}
10047
10048static void __tg3_set_rx_mode(struct net_device *dev)
10049{
10050 struct tg3 *tp = netdev_priv(dev);
10051 u32 rx_mode;
10052
10053 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
10054 RX_MODE_KEEP_VLAN_TAG);
10055
Matt Carlsonbf933c82011-01-25 15:58:49 +000010056#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010057 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
10058 * flag clear.
10059 */
Joe Perches63c3a662011-04-26 08:12:10 +000010060 if (!tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010061 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
10062#endif
10063
10064 if (dev->flags & IFF_PROMISC) {
10065 /* Promiscuous mode. */
10066 rx_mode |= RX_MODE_PROMISC;
10067 } else if (dev->flags & IFF_ALLMULTI) {
10068 /* Accept all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010069 tg3_set_multi(tp, 1);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +000010070 } else if (netdev_mc_empty(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010071 /* Reject all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010072 tg3_set_multi(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010073 } else {
10074 /* Accept one or more multicast(s). */
Jiri Pirko22bedad2010-04-01 21:22:57 +000010075 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010076 u32 mc_filter[4] = { 0, };
10077 u32 regidx;
10078 u32 bit;
10079 u32 crc;
10080
Jiri Pirko22bedad2010-04-01 21:22:57 +000010081 netdev_for_each_mc_addr(ha, dev) {
10082 crc = calc_crc(ha->addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010083 bit = ~crc & 0x7f;
10084 regidx = (bit & 0x60) >> 5;
10085 bit &= 0x1f;
10086 mc_filter[regidx] |= (1 << bit);
10087 }
10088
10089 tw32(MAC_HASH_REG_0, mc_filter[0]);
10090 tw32(MAC_HASH_REG_1, mc_filter[1]);
10091 tw32(MAC_HASH_REG_2, mc_filter[2]);
10092 tw32(MAC_HASH_REG_3, mc_filter[3]);
10093 }
10094
10095 if (rx_mode != tp->rx_mode) {
10096 tp->rx_mode = rx_mode;
10097 tw32_f(MAC_RX_MODE, rx_mode);
10098 udelay(10);
10099 }
10100}
10101
10102static void tg3_set_rx_mode(struct net_device *dev)
10103{
10104 struct tg3 *tp = netdev_priv(dev);
10105
Michael Chane75f7c92006-03-20 21:33:26 -080010106 if (!netif_running(dev))
10107 return;
10108
David S. Millerf47c11e2005-06-24 20:18:35 -070010109 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010110 __tg3_set_rx_mode(dev);
David S. Millerf47c11e2005-06-24 20:18:35 -070010111 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010112}
10113
Linus Torvalds1da177e2005-04-16 15:20:36 -070010114static int tg3_get_regs_len(struct net_device *dev)
10115{
Matt Carlson97bd8e42011-04-13 11:05:04 +000010116 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010117}
10118
10119static void tg3_get_regs(struct net_device *dev,
10120 struct ethtool_regs *regs, void *_p)
10121{
Linus Torvalds1da177e2005-04-16 15:20:36 -070010122 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010123
10124 regs->version = 0;
10125
Matt Carlson97bd8e42011-04-13 11:05:04 +000010126 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010127
Matt Carlson80096062010-08-02 11:26:06 +000010128 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010129 return;
10130
David S. Millerf47c11e2005-06-24 20:18:35 -070010131 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010132
Matt Carlson97bd8e42011-04-13 11:05:04 +000010133 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010134
David S. Millerf47c11e2005-06-24 20:18:35 -070010135 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010136}
10137
10138static int tg3_get_eeprom_len(struct net_device *dev)
10139{
10140 struct tg3 *tp = netdev_priv(dev);
10141
10142 return tp->nvram_size;
10143}
10144
Linus Torvalds1da177e2005-04-16 15:20:36 -070010145static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
10146{
10147 struct tg3 *tp = netdev_priv(dev);
10148 int ret;
10149 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -080010150 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010151 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010152
Joe Perches63c3a662011-04-26 08:12:10 +000010153 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000010154 return -EINVAL;
10155
Matt Carlson80096062010-08-02 11:26:06 +000010156 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010157 return -EAGAIN;
10158
Linus Torvalds1da177e2005-04-16 15:20:36 -070010159 offset = eeprom->offset;
10160 len = eeprom->len;
10161 eeprom->len = 0;
10162
10163 eeprom->magic = TG3_EEPROM_MAGIC;
10164
10165 if (offset & 3) {
10166 /* adjustments to start on required 4 byte boundary */
10167 b_offset = offset & 3;
10168 b_count = 4 - b_offset;
10169 if (b_count > len) {
10170 /* i.e. offset=1 len=2 */
10171 b_count = len;
10172 }
Matt Carlsona9dc5292009-02-25 14:25:30 +000010173 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010174 if (ret)
10175 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +000010176 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010177 len -= b_count;
10178 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010179 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010180 }
10181
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010182 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010183 pd = &data[eeprom->len];
10184 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010185 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010186 if (ret) {
10187 eeprom->len += i;
10188 return ret;
10189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010190 memcpy(pd + i, &val, 4);
10191 }
10192 eeprom->len += i;
10193
10194 if (len & 3) {
10195 /* read last bytes not ending on 4 byte boundary */
10196 pd = &data[eeprom->len];
10197 b_count = len & 3;
10198 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010199 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010200 if (ret)
10201 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080010202 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010203 eeprom->len += b_count;
10204 }
10205 return 0;
10206}
10207
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010208static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010209
10210static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
10211{
10212 struct tg3 *tp = netdev_priv(dev);
10213 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080010214 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010215 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010216 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010217
Matt Carlson80096062010-08-02 11:26:06 +000010218 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010219 return -EAGAIN;
10220
Joe Perches63c3a662011-04-26 08:12:10 +000010221 if (tg3_flag(tp, NO_NVRAM) ||
Matt Carlsondf259d82009-04-20 06:57:14 +000010222 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010223 return -EINVAL;
10224
10225 offset = eeprom->offset;
10226 len = eeprom->len;
10227
10228 if ((b_offset = (offset & 3))) {
10229 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010230 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010231 if (ret)
10232 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010233 len += b_offset;
10234 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -070010235 if (len < 4)
10236 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010237 }
10238
10239 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -070010240 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010241 /* adjustments to end on required 4 byte boundary */
10242 odd_len = 1;
10243 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010244 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010245 if (ret)
10246 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010247 }
10248
10249 buf = data;
10250 if (b_offset || odd_len) {
10251 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010252 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010253 return -ENOMEM;
10254 if (b_offset)
10255 memcpy(buf, &start, 4);
10256 if (odd_len)
10257 memcpy(buf+len-4, &end, 4);
10258 memcpy(buf + b_offset, data, eeprom->len);
10259 }
10260
10261 ret = tg3_nvram_write_block(tp, offset, len, buf);
10262
10263 if (buf != data)
10264 kfree(buf);
10265
10266 return ret;
10267}
10268
10269static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
10270{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010271 struct tg3 *tp = netdev_priv(dev);
10272
Joe Perches63c3a662011-04-26 08:12:10 +000010273 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010274 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010275 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010276 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010277 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10278 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010279 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010280
Linus Torvalds1da177e2005-04-16 15:20:36 -070010281 cmd->supported = (SUPPORTED_Autoneg);
10282
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010283 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010284 cmd->supported |= (SUPPORTED_1000baseT_Half |
10285 SUPPORTED_1000baseT_Full);
10286
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010287 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010288 cmd->supported |= (SUPPORTED_100baseT_Half |
10289 SUPPORTED_100baseT_Full |
10290 SUPPORTED_10baseT_Half |
10291 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -080010292 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -070010293 cmd->port = PORT_TP;
10294 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010295 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -070010296 cmd->port = PORT_FIBRE;
10297 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010298
Linus Torvalds1da177e2005-04-16 15:20:36 -070010299 cmd->advertising = tp->link_config.advertising;
Matt Carlson5bb09772011-06-13 13:39:00 +000010300 if (tg3_flag(tp, PAUSE_AUTONEG)) {
10301 if (tp->link_config.flowctrl & FLOW_CTRL_RX) {
10302 if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
10303 cmd->advertising |= ADVERTISED_Pause;
10304 } else {
10305 cmd->advertising |= ADVERTISED_Pause |
10306 ADVERTISED_Asym_Pause;
10307 }
10308 } else if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
10309 cmd->advertising |= ADVERTISED_Asym_Pause;
10310 }
10311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010312 if (netif_running(dev)) {
David Decotigny70739492011-04-27 18:32:40 +000010313 ethtool_cmd_speed_set(cmd, tp->link_config.active_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010314 cmd->duplex = tp->link_config.active_duplex;
Matt Carlson64c22182010-10-14 10:37:44 +000010315 } else {
David Decotigny70739492011-04-27 18:32:40 +000010316 ethtool_cmd_speed_set(cmd, SPEED_INVALID);
Matt Carlson64c22182010-10-14 10:37:44 +000010317 cmd->duplex = DUPLEX_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010318 }
Matt Carlson882e9792009-09-01 13:21:36 +000010319 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +000010320 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010321 cmd->autoneg = tp->link_config.autoneg;
10322 cmd->maxtxpkt = 0;
10323 cmd->maxrxpkt = 0;
10324 return 0;
10325}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010326
Linus Torvalds1da177e2005-04-16 15:20:36 -070010327static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
10328{
10329 struct tg3 *tp = netdev_priv(dev);
David Decotigny25db0332011-04-27 18:32:39 +000010330 u32 speed = ethtool_cmd_speed(cmd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010331
Joe Perches63c3a662011-04-26 08:12:10 +000010332 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010333 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010334 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010335 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010336 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10337 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010338 }
10339
Matt Carlson7e5856b2009-02-25 14:23:01 +000010340 if (cmd->autoneg != AUTONEG_ENABLE &&
10341 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -070010342 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +000010343
10344 if (cmd->autoneg == AUTONEG_DISABLE &&
10345 cmd->duplex != DUPLEX_FULL &&
10346 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -070010347 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010348
Matt Carlson7e5856b2009-02-25 14:23:01 +000010349 if (cmd->autoneg == AUTONEG_ENABLE) {
10350 u32 mask = ADVERTISED_Autoneg |
10351 ADVERTISED_Pause |
10352 ADVERTISED_Asym_Pause;
10353
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010354 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010355 mask |= ADVERTISED_1000baseT_Half |
10356 ADVERTISED_1000baseT_Full;
10357
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010358 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010359 mask |= ADVERTISED_100baseT_Half |
10360 ADVERTISED_100baseT_Full |
10361 ADVERTISED_10baseT_Half |
10362 ADVERTISED_10baseT_Full |
10363 ADVERTISED_TP;
10364 else
10365 mask |= ADVERTISED_FIBRE;
10366
10367 if (cmd->advertising & ~mask)
10368 return -EINVAL;
10369
10370 mask &= (ADVERTISED_1000baseT_Half |
10371 ADVERTISED_1000baseT_Full |
10372 ADVERTISED_100baseT_Half |
10373 ADVERTISED_100baseT_Full |
10374 ADVERTISED_10baseT_Half |
10375 ADVERTISED_10baseT_Full);
10376
10377 cmd->advertising &= mask;
10378 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010379 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
David Decotigny25db0332011-04-27 18:32:39 +000010380 if (speed != SPEED_1000)
Matt Carlson7e5856b2009-02-25 14:23:01 +000010381 return -EINVAL;
10382
10383 if (cmd->duplex != DUPLEX_FULL)
10384 return -EINVAL;
10385 } else {
David Decotigny25db0332011-04-27 18:32:39 +000010386 if (speed != SPEED_100 &&
10387 speed != SPEED_10)
Matt Carlson7e5856b2009-02-25 14:23:01 +000010388 return -EINVAL;
10389 }
10390 }
10391
David S. Millerf47c11e2005-06-24 20:18:35 -070010392 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010393
10394 tp->link_config.autoneg = cmd->autoneg;
10395 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -070010396 tp->link_config.advertising = (cmd->advertising |
10397 ADVERTISED_Autoneg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010398 tp->link_config.speed = SPEED_INVALID;
10399 tp->link_config.duplex = DUPLEX_INVALID;
10400 } else {
10401 tp->link_config.advertising = 0;
David Decotigny25db0332011-04-27 18:32:39 +000010402 tp->link_config.speed = speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010403 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010404 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010405
Michael Chan24fcad62006-12-17 17:06:46 -080010406 tp->link_config.orig_speed = tp->link_config.speed;
10407 tp->link_config.orig_duplex = tp->link_config.duplex;
10408 tp->link_config.orig_autoneg = tp->link_config.autoneg;
10409
Linus Torvalds1da177e2005-04-16 15:20:36 -070010410 if (netif_running(dev))
10411 tg3_setup_phy(tp, 1);
10412
David S. Millerf47c11e2005-06-24 20:18:35 -070010413 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010414
Linus Torvalds1da177e2005-04-16 15:20:36 -070010415 return 0;
10416}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010417
Linus Torvalds1da177e2005-04-16 15:20:36 -070010418static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
10419{
10420 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010421
Linus Torvalds1da177e2005-04-16 15:20:36 -070010422 strcpy(info->driver, DRV_MODULE_NAME);
10423 strcpy(info->version, DRV_MODULE_VERSION);
Michael Chanc4e65752006-03-20 22:29:32 -080010424 strcpy(info->fw_version, tp->fw_ver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010425 strcpy(info->bus_info, pci_name(tp->pdev));
10426}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010427
Linus Torvalds1da177e2005-04-16 15:20:36 -070010428static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10429{
10430 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010431
Joe Perches63c3a662011-04-26 08:12:10 +000010432 if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070010433 wol->supported = WAKE_MAGIC;
10434 else
10435 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010436 wol->wolopts = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010437 if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010438 wol->wolopts = WAKE_MAGIC;
10439 memset(&wol->sopass, 0, sizeof(wol->sopass));
10440}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010441
Linus Torvalds1da177e2005-04-16 15:20:36 -070010442static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10443{
10444 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070010445 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010446
Linus Torvalds1da177e2005-04-16 15:20:36 -070010447 if (wol->wolopts & ~WAKE_MAGIC)
10448 return -EINVAL;
10449 if ((wol->wolopts & WAKE_MAGIC) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010450 !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010451 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010452
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010453 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
10454
David S. Millerf47c11e2005-06-24 20:18:35 -070010455 spin_lock_bh(&tp->lock);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010456 if (device_may_wakeup(dp))
Joe Perches63c3a662011-04-26 08:12:10 +000010457 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010458 else
Joe Perches63c3a662011-04-26 08:12:10 +000010459 tg3_flag_clear(tp, WOL_ENABLE);
David S. Millerf47c11e2005-06-24 20:18:35 -070010460 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010461
Linus Torvalds1da177e2005-04-16 15:20:36 -070010462 return 0;
10463}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010464
Linus Torvalds1da177e2005-04-16 15:20:36 -070010465static u32 tg3_get_msglevel(struct net_device *dev)
10466{
10467 struct tg3 *tp = netdev_priv(dev);
10468 return tp->msg_enable;
10469}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010470
Linus Torvalds1da177e2005-04-16 15:20:36 -070010471static void tg3_set_msglevel(struct net_device *dev, u32 value)
10472{
10473 struct tg3 *tp = netdev_priv(dev);
10474 tp->msg_enable = value;
10475}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010476
Linus Torvalds1da177e2005-04-16 15:20:36 -070010477static int tg3_nway_reset(struct net_device *dev)
10478{
10479 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010480 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010481
Linus Torvalds1da177e2005-04-16 15:20:36 -070010482 if (!netif_running(dev))
10483 return -EAGAIN;
10484
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010485 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070010486 return -EINVAL;
10487
Joe Perches63c3a662011-04-26 08:12:10 +000010488 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010489 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010490 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010491 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010492 } else {
10493 u32 bmcr;
10494
10495 spin_lock_bh(&tp->lock);
10496 r = -EINVAL;
10497 tg3_readphy(tp, MII_BMCR, &bmcr);
10498 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
10499 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010500 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010501 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
10502 BMCR_ANENABLE);
10503 r = 0;
10504 }
10505 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010506 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010507
Linus Torvalds1da177e2005-04-16 15:20:36 -070010508 return r;
10509}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010510
Linus Torvalds1da177e2005-04-16 15:20:36 -070010511static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10512{
10513 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010514
Matt Carlson2c49a442010-09-30 10:34:35 +000010515 ering->rx_max_pending = tp->rx_std_ring_mask;
Joe Perches63c3a662011-04-26 08:12:10 +000010516 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson2c49a442010-09-30 10:34:35 +000010517 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080010518 else
10519 ering->rx_jumbo_max_pending = 0;
10520
10521 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010522
10523 ering->rx_pending = tp->rx_pending;
Joe Perches63c3a662011-04-26 08:12:10 +000010524 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Michael Chan4f81c322006-03-20 21:33:42 -080010525 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
10526 else
10527 ering->rx_jumbo_pending = 0;
10528
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010529 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010530}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010531
Linus Torvalds1da177e2005-04-16 15:20:36 -070010532static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10533{
10534 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000010535 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010536
Matt Carlson2c49a442010-09-30 10:34:35 +000010537 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
10538 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070010539 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
10540 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Joe Perches63c3a662011-04-26 08:12:10 +000010541 (tg3_flag(tp, TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070010542 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010543 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010544
Michael Chanbbe832c2005-06-24 20:20:04 -070010545 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010546 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010547 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010548 irq_sync = 1;
10549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010550
Michael Chanbbe832c2005-06-24 20:20:04 -070010551 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010552
Linus Torvalds1da177e2005-04-16 15:20:36 -070010553 tp->rx_pending = ering->rx_pending;
10554
Joe Perches63c3a662011-04-26 08:12:10 +000010555 if (tg3_flag(tp, MAX_RXPEND_64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010556 tp->rx_pending > 63)
10557 tp->rx_pending = 63;
10558 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000010559
Matt Carlson6fd45cb2010-09-15 08:59:57 +000010560 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000010561 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010562
10563 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070010564 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -070010565 err = tg3_restart_hw(tp, 1);
10566 if (!err)
10567 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010568 }
10569
David S. Millerf47c11e2005-06-24 20:18:35 -070010570 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010571
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010572 if (irq_sync && !err)
10573 tg3_phy_start(tp);
10574
Michael Chanb9ec6c12006-07-25 16:37:27 -070010575 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010576}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010577
Linus Torvalds1da177e2005-04-16 15:20:36 -070010578static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10579{
10580 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010581
Joe Perches63c3a662011-04-26 08:12:10 +000010582 epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
Matt Carlson8d018622007-12-20 20:05:44 -080010583
Steve Glendinninge18ce342008-12-16 02:00:00 -080010584 if (tp->link_config.active_flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080010585 epause->rx_pause = 1;
10586 else
10587 epause->rx_pause = 0;
10588
Steve Glendinninge18ce342008-12-16 02:00:00 -080010589 if (tp->link_config.active_flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080010590 epause->tx_pause = 1;
10591 else
10592 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010593}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010594
Linus Torvalds1da177e2005-04-16 15:20:36 -070010595static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10596{
10597 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010598 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010599
Joe Perches63c3a662011-04-26 08:12:10 +000010600 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson27121682010-02-17 15:16:57 +000010601 u32 newadv;
10602 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010603
Matt Carlson27121682010-02-17 15:16:57 +000010604 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010605
Matt Carlson27121682010-02-17 15:16:57 +000010606 if (!(phydev->supported & SUPPORTED_Pause) ||
10607 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000010608 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000010609 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010610
Matt Carlson27121682010-02-17 15:16:57 +000010611 tp->link_config.flowctrl = 0;
10612 if (epause->rx_pause) {
10613 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010614
Matt Carlson27121682010-02-17 15:16:57 +000010615 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080010616 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000010617 newadv = ADVERTISED_Pause;
10618 } else
10619 newadv = ADVERTISED_Pause |
10620 ADVERTISED_Asym_Pause;
10621 } else if (epause->tx_pause) {
10622 tp->link_config.flowctrl |= FLOW_CTRL_TX;
10623 newadv = ADVERTISED_Asym_Pause;
10624 } else
10625 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010626
Matt Carlson27121682010-02-17 15:16:57 +000010627 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010628 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010629 else
Joe Perches63c3a662011-04-26 08:12:10 +000010630 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010631
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010632 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000010633 u32 oldadv = phydev->advertising &
10634 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
10635 if (oldadv != newadv) {
10636 phydev->advertising &=
10637 ~(ADVERTISED_Pause |
10638 ADVERTISED_Asym_Pause);
10639 phydev->advertising |= newadv;
10640 if (phydev->autoneg) {
10641 /*
10642 * Always renegotiate the link to
10643 * inform our link partner of our
10644 * flow control settings, even if the
10645 * flow control is forced. Let
10646 * tg3_adjust_link() do the final
10647 * flow control setup.
10648 */
10649 return phy_start_aneg(phydev);
10650 }
10651 }
10652
10653 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010654 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000010655 } else {
10656 tp->link_config.orig_advertising &=
10657 ~(ADVERTISED_Pause |
10658 ADVERTISED_Asym_Pause);
10659 tp->link_config.orig_advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010660 }
10661 } else {
10662 int irq_sync = 0;
10663
10664 if (netif_running(dev)) {
10665 tg3_netif_stop(tp);
10666 irq_sync = 1;
10667 }
10668
10669 tg3_full_lock(tp, irq_sync);
10670
10671 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010672 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010673 else
Joe Perches63c3a662011-04-26 08:12:10 +000010674 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010675 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010676 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010677 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010678 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010679 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010680 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010681 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010682 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010683
10684 if (netif_running(dev)) {
10685 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10686 err = tg3_restart_hw(tp, 1);
10687 if (!err)
10688 tg3_netif_start(tp);
10689 }
10690
10691 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010693
Michael Chanb9ec6c12006-07-25 16:37:27 -070010694 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010695}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010696
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010697static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010698{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010699 switch (sset) {
10700 case ETH_SS_TEST:
10701 return TG3_NUM_TEST;
10702 case ETH_SS_STATS:
10703 return TG3_NUM_STATS;
10704 default:
10705 return -EOPNOTSUPP;
10706 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070010707}
10708
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010709static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010710{
10711 switch (stringset) {
10712 case ETH_SS_STATS:
10713 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
10714 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070010715 case ETH_SS_TEST:
10716 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
10717 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010718 default:
10719 WARN_ON(1); /* we need a WARN() */
10720 break;
10721 }
10722}
10723
stephen hemminger81b87092011-04-04 08:43:50 +000010724static int tg3_set_phys_id(struct net_device *dev,
10725 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070010726{
10727 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070010728
10729 if (!netif_running(tp->dev))
10730 return -EAGAIN;
10731
stephen hemminger81b87092011-04-04 08:43:50 +000010732 switch (state) {
10733 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +000010734 return 1; /* cycle on/off once per second */
Michael Chan4009a932005-09-05 17:52:54 -070010735
stephen hemminger81b87092011-04-04 08:43:50 +000010736 case ETHTOOL_ID_ON:
10737 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10738 LED_CTRL_1000MBPS_ON |
10739 LED_CTRL_100MBPS_ON |
10740 LED_CTRL_10MBPS_ON |
10741 LED_CTRL_TRAFFIC_OVERRIDE |
10742 LED_CTRL_TRAFFIC_BLINK |
10743 LED_CTRL_TRAFFIC_LED);
10744 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010745
stephen hemminger81b87092011-04-04 08:43:50 +000010746 case ETHTOOL_ID_OFF:
10747 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10748 LED_CTRL_TRAFFIC_OVERRIDE);
10749 break;
Michael Chan4009a932005-09-05 17:52:54 -070010750
stephen hemminger81b87092011-04-04 08:43:50 +000010751 case ETHTOOL_ID_INACTIVE:
10752 tw32(MAC_LED_CTRL, tp->led_ctrl);
10753 break;
Michael Chan4009a932005-09-05 17:52:54 -070010754 }
stephen hemminger81b87092011-04-04 08:43:50 +000010755
Michael Chan4009a932005-09-05 17:52:54 -070010756 return 0;
10757}
10758
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010759static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010760 struct ethtool_stats *estats, u64 *tmp_stats)
10761{
10762 struct tg3 *tp = netdev_priv(dev);
10763 memcpy(tmp_stats, tg3_get_estats(tp), sizeof(tp->estats));
10764}
10765
Matt Carlson535a4902011-07-20 10:20:56 +000010766static __be32 *tg3_vpd_readblock(struct tg3 *tp, u32 *vpdlen)
Matt Carlsonc3e94502011-04-13 11:05:08 +000010767{
10768 int i;
10769 __be32 *buf;
10770 u32 offset = 0, len = 0;
10771 u32 magic, val;
10772
Joe Perches63c3a662011-04-26 08:12:10 +000010773 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
Matt Carlsonc3e94502011-04-13 11:05:08 +000010774 return NULL;
10775
10776 if (magic == TG3_EEPROM_MAGIC) {
10777 for (offset = TG3_NVM_DIR_START;
10778 offset < TG3_NVM_DIR_END;
10779 offset += TG3_NVM_DIRENT_SIZE) {
10780 if (tg3_nvram_read(tp, offset, &val))
10781 return NULL;
10782
10783 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
10784 TG3_NVM_DIRTYPE_EXTVPD)
10785 break;
10786 }
10787
10788 if (offset != TG3_NVM_DIR_END) {
10789 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
10790 if (tg3_nvram_read(tp, offset + 4, &offset))
10791 return NULL;
10792
10793 offset = tg3_nvram_logical_addr(tp, offset);
10794 }
10795 }
10796
10797 if (!offset || !len) {
10798 offset = TG3_NVM_VPD_OFF;
10799 len = TG3_NVM_VPD_LEN;
10800 }
10801
10802 buf = kmalloc(len, GFP_KERNEL);
10803 if (buf == NULL)
10804 return NULL;
10805
10806 if (magic == TG3_EEPROM_MAGIC) {
10807 for (i = 0; i < len; i += 4) {
10808 /* The data is in little-endian format in NVRAM.
10809 * Use the big-endian read routines to preserve
10810 * the byte order as it exists in NVRAM.
10811 */
10812 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
10813 goto error;
10814 }
10815 } else {
10816 u8 *ptr;
10817 ssize_t cnt;
10818 unsigned int pos = 0;
10819
10820 ptr = (u8 *)&buf[0];
10821 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
10822 cnt = pci_read_vpd(tp->pdev, pos,
10823 len - pos, ptr);
10824 if (cnt == -ETIMEDOUT || cnt == -EINTR)
10825 cnt = 0;
10826 else if (cnt < 0)
10827 goto error;
10828 }
10829 if (pos != len)
10830 goto error;
10831 }
10832
Matt Carlson535a4902011-07-20 10:20:56 +000010833 *vpdlen = len;
10834
Matt Carlsonc3e94502011-04-13 11:05:08 +000010835 return buf;
10836
10837error:
10838 kfree(buf);
10839 return NULL;
10840}
10841
Michael Chan566f86a2005-05-29 14:56:58 -070010842#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080010843#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
10844#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
10845#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Matt Carlson727a6d92011-06-13 13:38:58 +000010846#define NVRAM_SELFBOOT_FORMAT1_4_SIZE 0x20
10847#define NVRAM_SELFBOOT_FORMAT1_5_SIZE 0x24
Matt Carlsonbda18fa2011-07-20 10:20:57 +000010848#define NVRAM_SELFBOOT_FORMAT1_6_SIZE 0x50
Michael Chanb16250e2006-09-27 16:10:14 -070010849#define NVRAM_SELFBOOT_HW_SIZE 0x20
10850#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070010851
10852static int tg3_test_nvram(struct tg3 *tp)
10853{
Matt Carlson535a4902011-07-20 10:20:56 +000010854 u32 csum, magic, len;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010855 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010856 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070010857
Joe Perches63c3a662011-04-26 08:12:10 +000010858 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000010859 return 0;
10860
Matt Carlsone4f34112009-02-25 14:25:00 +000010861 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080010862 return -EIO;
10863
Michael Chan1b277772006-03-20 22:27:48 -080010864 if (magic == TG3_EEPROM_MAGIC)
10865 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070010866 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080010867 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
10868 TG3_EEPROM_SB_FORMAT_1) {
10869 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
10870 case TG3_EEPROM_SB_REVISION_0:
10871 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
10872 break;
10873 case TG3_EEPROM_SB_REVISION_2:
10874 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
10875 break;
10876 case TG3_EEPROM_SB_REVISION_3:
10877 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
10878 break;
Matt Carlson727a6d92011-06-13 13:38:58 +000010879 case TG3_EEPROM_SB_REVISION_4:
10880 size = NVRAM_SELFBOOT_FORMAT1_4_SIZE;
10881 break;
10882 case TG3_EEPROM_SB_REVISION_5:
10883 size = NVRAM_SELFBOOT_FORMAT1_5_SIZE;
10884 break;
10885 case TG3_EEPROM_SB_REVISION_6:
10886 size = NVRAM_SELFBOOT_FORMAT1_6_SIZE;
10887 break;
Matt Carlsona5767de2007-11-12 21:10:58 -080010888 default:
Matt Carlson727a6d92011-06-13 13:38:58 +000010889 return -EIO;
Matt Carlsona5767de2007-11-12 21:10:58 -080010890 }
10891 } else
Michael Chan1b277772006-03-20 22:27:48 -080010892 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070010893 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
10894 size = NVRAM_SELFBOOT_HW_SIZE;
10895 else
Michael Chan1b277772006-03-20 22:27:48 -080010896 return -EIO;
10897
10898 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070010899 if (buf == NULL)
10900 return -ENOMEM;
10901
Michael Chan1b277772006-03-20 22:27:48 -080010902 err = -EIO;
10903 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010904 err = tg3_nvram_read_be32(tp, i, &buf[j]);
10905 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070010906 break;
Michael Chan566f86a2005-05-29 14:56:58 -070010907 }
Michael Chan1b277772006-03-20 22:27:48 -080010908 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070010909 goto out;
10910
Michael Chan1b277772006-03-20 22:27:48 -080010911 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010912 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080010913 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010914 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080010915 u8 *buf8 = (u8 *) buf, csum8 = 0;
10916
Al Virob9fc7dc2007-12-17 22:59:57 -080010917 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080010918 TG3_EEPROM_SB_REVISION_2) {
10919 /* For rev 2, the csum doesn't include the MBA. */
10920 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
10921 csum8 += buf8[i];
10922 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
10923 csum8 += buf8[i];
10924 } else {
10925 for (i = 0; i < size; i++)
10926 csum8 += buf8[i];
10927 }
Michael Chan1b277772006-03-20 22:27:48 -080010928
Adrian Bunkad96b482006-04-05 22:21:04 -070010929 if (csum8 == 0) {
10930 err = 0;
10931 goto out;
10932 }
10933
10934 err = -EIO;
10935 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080010936 }
Michael Chan566f86a2005-05-29 14:56:58 -070010937
Al Virob9fc7dc2007-12-17 22:59:57 -080010938 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010939 TG3_EEPROM_MAGIC_HW) {
10940 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000010941 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070010942 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070010943
10944 /* Separate the parity bits and the data bytes. */
10945 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
10946 if ((i == 0) || (i == 8)) {
10947 int l;
10948 u8 msk;
10949
10950 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
10951 parity[k++] = buf8[i] & msk;
10952 i++;
Matt Carlson859a5882010-04-05 10:19:28 +000010953 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070010954 int l;
10955 u8 msk;
10956
10957 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
10958 parity[k++] = buf8[i] & msk;
10959 i++;
10960
10961 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
10962 parity[k++] = buf8[i] & msk;
10963 i++;
10964 }
10965 data[j++] = buf8[i];
10966 }
10967
10968 err = -EIO;
10969 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
10970 u8 hw8 = hweight8(data[i]);
10971
10972 if ((hw8 & 0x1) && parity[i])
10973 goto out;
10974 else if (!(hw8 & 0x1) && !parity[i])
10975 goto out;
10976 }
10977 err = 0;
10978 goto out;
10979 }
10980
Matt Carlson01c3a392011-03-09 16:58:20 +000010981 err = -EIO;
10982
Michael Chan566f86a2005-05-29 14:56:58 -070010983 /* Bootstrap checksum at offset 0x10 */
10984 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000010985 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070010986 goto out;
10987
10988 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
10989 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000010990 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000010991 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070010992
Matt Carlsonc3e94502011-04-13 11:05:08 +000010993 kfree(buf);
10994
Matt Carlson535a4902011-07-20 10:20:56 +000010995 buf = tg3_vpd_readblock(tp, &len);
Matt Carlsonc3e94502011-04-13 11:05:08 +000010996 if (!buf)
10997 return -ENOMEM;
Matt Carlsond4894f32011-03-09 16:58:21 +000010998
Matt Carlson535a4902011-07-20 10:20:56 +000010999 i = pci_vpd_find_tag((u8 *)buf, 0, len, PCI_VPD_LRDT_RO_DATA);
Matt Carlsond4894f32011-03-09 16:58:21 +000011000 if (i > 0) {
11001 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
11002 if (j < 0)
11003 goto out;
11004
Matt Carlson535a4902011-07-20 10:20:56 +000011005 if (i + PCI_VPD_LRDT_TAG_SIZE + j > len)
Matt Carlsond4894f32011-03-09 16:58:21 +000011006 goto out;
11007
11008 i += PCI_VPD_LRDT_TAG_SIZE;
11009 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
11010 PCI_VPD_RO_KEYWORD_CHKSUM);
11011 if (j > 0) {
11012 u8 csum8 = 0;
11013
11014 j += PCI_VPD_INFO_FLD_HDR_SIZE;
11015
11016 for (i = 0; i <= j; i++)
11017 csum8 += ((u8 *)buf)[i];
11018
11019 if (csum8)
11020 goto out;
11021 }
11022 }
11023
Michael Chan566f86a2005-05-29 14:56:58 -070011024 err = 0;
11025
11026out:
11027 kfree(buf);
11028 return err;
11029}
11030
Michael Chanca430072005-05-29 14:57:23 -070011031#define TG3_SERDES_TIMEOUT_SEC 2
11032#define TG3_COPPER_TIMEOUT_SEC 6
11033
11034static int tg3_test_link(struct tg3 *tp)
11035{
11036 int i, max;
11037
11038 if (!netif_running(tp->dev))
11039 return -ENODEV;
11040
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011041 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070011042 max = TG3_SERDES_TIMEOUT_SEC;
11043 else
11044 max = TG3_COPPER_TIMEOUT_SEC;
11045
11046 for (i = 0; i < max; i++) {
11047 if (netif_carrier_ok(tp->dev))
11048 return 0;
11049
11050 if (msleep_interruptible(1000))
11051 break;
11052 }
11053
11054 return -EIO;
11055}
11056
Michael Chana71116d2005-05-29 14:58:11 -070011057/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080011058static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070011059{
Michael Chanb16250e2006-09-27 16:10:14 -070011060 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070011061 u32 offset, read_mask, write_mask, val, save_val, read_val;
11062 static struct {
11063 u16 offset;
11064 u16 flags;
11065#define TG3_FL_5705 0x1
11066#define TG3_FL_NOT_5705 0x2
11067#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070011068#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070011069 u32 read_mask;
11070 u32 write_mask;
11071 } reg_tbl[] = {
11072 /* MAC Control Registers */
11073 { MAC_MODE, TG3_FL_NOT_5705,
11074 0x00000000, 0x00ef6f8c },
11075 { MAC_MODE, TG3_FL_5705,
11076 0x00000000, 0x01ef6b8c },
11077 { MAC_STATUS, TG3_FL_NOT_5705,
11078 0x03800107, 0x00000000 },
11079 { MAC_STATUS, TG3_FL_5705,
11080 0x03800100, 0x00000000 },
11081 { MAC_ADDR_0_HIGH, 0x0000,
11082 0x00000000, 0x0000ffff },
11083 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011084 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070011085 { MAC_RX_MTU_SIZE, 0x0000,
11086 0x00000000, 0x0000ffff },
11087 { MAC_TX_MODE, 0x0000,
11088 0x00000000, 0x00000070 },
11089 { MAC_TX_LENGTHS, 0x0000,
11090 0x00000000, 0x00003fff },
11091 { MAC_RX_MODE, TG3_FL_NOT_5705,
11092 0x00000000, 0x000007fc },
11093 { MAC_RX_MODE, TG3_FL_5705,
11094 0x00000000, 0x000007dc },
11095 { MAC_HASH_REG_0, 0x0000,
11096 0x00000000, 0xffffffff },
11097 { MAC_HASH_REG_1, 0x0000,
11098 0x00000000, 0xffffffff },
11099 { MAC_HASH_REG_2, 0x0000,
11100 0x00000000, 0xffffffff },
11101 { MAC_HASH_REG_3, 0x0000,
11102 0x00000000, 0xffffffff },
11103
11104 /* Receive Data and Receive BD Initiator Control Registers. */
11105 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
11106 0x00000000, 0xffffffff },
11107 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
11108 0x00000000, 0xffffffff },
11109 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
11110 0x00000000, 0x00000003 },
11111 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
11112 0x00000000, 0xffffffff },
11113 { RCVDBDI_STD_BD+0, 0x0000,
11114 0x00000000, 0xffffffff },
11115 { RCVDBDI_STD_BD+4, 0x0000,
11116 0x00000000, 0xffffffff },
11117 { RCVDBDI_STD_BD+8, 0x0000,
11118 0x00000000, 0xffff0002 },
11119 { RCVDBDI_STD_BD+0xc, 0x0000,
11120 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011121
Michael Chana71116d2005-05-29 14:58:11 -070011122 /* Receive BD Initiator Control Registers. */
11123 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
11124 0x00000000, 0xffffffff },
11125 { RCVBDI_STD_THRESH, TG3_FL_5705,
11126 0x00000000, 0x000003ff },
11127 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
11128 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011129
Michael Chana71116d2005-05-29 14:58:11 -070011130 /* Host Coalescing Control Registers. */
11131 { HOSTCC_MODE, TG3_FL_NOT_5705,
11132 0x00000000, 0x00000004 },
11133 { HOSTCC_MODE, TG3_FL_5705,
11134 0x00000000, 0x000000f6 },
11135 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
11136 0x00000000, 0xffffffff },
11137 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
11138 0x00000000, 0x000003ff },
11139 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
11140 0x00000000, 0xffffffff },
11141 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
11142 0x00000000, 0x000003ff },
11143 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
11144 0x00000000, 0xffffffff },
11145 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
11146 0x00000000, 0x000000ff },
11147 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
11148 0x00000000, 0xffffffff },
11149 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
11150 0x00000000, 0x000000ff },
11151 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
11152 0x00000000, 0xffffffff },
11153 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
11154 0x00000000, 0xffffffff },
11155 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
11156 0x00000000, 0xffffffff },
11157 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
11158 0x00000000, 0x000000ff },
11159 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
11160 0x00000000, 0xffffffff },
11161 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
11162 0x00000000, 0x000000ff },
11163 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
11164 0x00000000, 0xffffffff },
11165 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
11166 0x00000000, 0xffffffff },
11167 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
11168 0x00000000, 0xffffffff },
11169 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
11170 0x00000000, 0xffffffff },
11171 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
11172 0x00000000, 0xffffffff },
11173 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
11174 0xffffffff, 0x00000000 },
11175 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
11176 0xffffffff, 0x00000000 },
11177
11178 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070011179 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070011180 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070011181 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070011182 0x00000000, 0x007fffff },
11183 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
11184 0x00000000, 0x0000003f },
11185 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
11186 0x00000000, 0x000001ff },
11187 { BUFMGR_MB_HIGH_WATER, 0x0000,
11188 0x00000000, 0x000001ff },
11189 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
11190 0xffffffff, 0x00000000 },
11191 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
11192 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011193
Michael Chana71116d2005-05-29 14:58:11 -070011194 /* Mailbox Registers */
11195 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
11196 0x00000000, 0x000001ff },
11197 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
11198 0x00000000, 0x000001ff },
11199 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
11200 0x00000000, 0x000007ff },
11201 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
11202 0x00000000, 0x000001ff },
11203
11204 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
11205 };
11206
Michael Chanb16250e2006-09-27 16:10:14 -070011207 is_5705 = is_5750 = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000011208 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chana71116d2005-05-29 14:58:11 -070011209 is_5705 = 1;
Joe Perches63c3a662011-04-26 08:12:10 +000011210 if (tg3_flag(tp, 5750_PLUS))
Michael Chanb16250e2006-09-27 16:10:14 -070011211 is_5750 = 1;
11212 }
Michael Chana71116d2005-05-29 14:58:11 -070011213
11214 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
11215 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
11216 continue;
11217
11218 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
11219 continue;
11220
Joe Perches63c3a662011-04-26 08:12:10 +000011221 if (tg3_flag(tp, IS_5788) &&
Michael Chana71116d2005-05-29 14:58:11 -070011222 (reg_tbl[i].flags & TG3_FL_NOT_5788))
11223 continue;
11224
Michael Chanb16250e2006-09-27 16:10:14 -070011225 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
11226 continue;
11227
Michael Chana71116d2005-05-29 14:58:11 -070011228 offset = (u32) reg_tbl[i].offset;
11229 read_mask = reg_tbl[i].read_mask;
11230 write_mask = reg_tbl[i].write_mask;
11231
11232 /* Save the original register content */
11233 save_val = tr32(offset);
11234
11235 /* Determine the read-only value. */
11236 read_val = save_val & read_mask;
11237
11238 /* Write zero to the register, then make sure the read-only bits
11239 * are not changed and the read/write bits are all zeros.
11240 */
11241 tw32(offset, 0);
11242
11243 val = tr32(offset);
11244
11245 /* Test the read-only and read/write bits. */
11246 if (((val & read_mask) != read_val) || (val & write_mask))
11247 goto out;
11248
11249 /* Write ones to all the bits defined by RdMask and WrMask, then
11250 * make sure the read-only bits are not changed and the
11251 * read/write bits are all ones.
11252 */
11253 tw32(offset, read_mask | write_mask);
11254
11255 val = tr32(offset);
11256
11257 /* Test the read-only bits. */
11258 if ((val & read_mask) != read_val)
11259 goto out;
11260
11261 /* Test the read/write bits. */
11262 if ((val & write_mask) != write_mask)
11263 goto out;
11264
11265 tw32(offset, save_val);
11266 }
11267
11268 return 0;
11269
11270out:
Michael Chan9f88f292006-12-07 00:22:54 -080011271 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000011272 netdev_err(tp->dev,
11273 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070011274 tw32(offset, save_val);
11275 return -EIO;
11276}
11277
Michael Chan7942e1d2005-05-29 14:58:36 -070011278static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
11279{
Arjan van de Venf71e1302006-03-03 21:33:57 -050011280 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070011281 int i;
11282 u32 j;
11283
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020011284 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070011285 for (j = 0; j < len; j += 4) {
11286 u32 val;
11287
11288 tg3_write_mem(tp, offset + j, test_pattern[i]);
11289 tg3_read_mem(tp, offset + j, &val);
11290 if (val != test_pattern[i])
11291 return -EIO;
11292 }
11293 }
11294 return 0;
11295}
11296
11297static int tg3_test_memory(struct tg3 *tp)
11298{
11299 static struct mem_entry {
11300 u32 offset;
11301 u32 len;
11302 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080011303 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070011304 { 0x00002000, 0x1c000},
11305 { 0xffffffff, 0x00000}
11306 }, mem_tbl_5705[] = {
11307 { 0x00000100, 0x0000c},
11308 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070011309 { 0x00004000, 0x00800},
11310 { 0x00006000, 0x01000},
11311 { 0x00008000, 0x02000},
11312 { 0x00010000, 0x0e000},
11313 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080011314 }, mem_tbl_5755[] = {
11315 { 0x00000200, 0x00008},
11316 { 0x00004000, 0x00800},
11317 { 0x00006000, 0x00800},
11318 { 0x00008000, 0x02000},
11319 { 0x00010000, 0x0c000},
11320 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070011321 }, mem_tbl_5906[] = {
11322 { 0x00000200, 0x00008},
11323 { 0x00004000, 0x00400},
11324 { 0x00006000, 0x00400},
11325 { 0x00008000, 0x01000},
11326 { 0x00010000, 0x01000},
11327 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011328 }, mem_tbl_5717[] = {
11329 { 0x00000200, 0x00008},
11330 { 0x00010000, 0x0a000},
11331 { 0x00020000, 0x13c00},
11332 { 0xffffffff, 0x00000}
11333 }, mem_tbl_57765[] = {
11334 { 0x00000200, 0x00008},
11335 { 0x00004000, 0x00800},
11336 { 0x00006000, 0x09800},
11337 { 0x00010000, 0x0a000},
11338 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070011339 };
11340 struct mem_entry *mem_tbl;
11341 int err = 0;
11342 int i;
11343
Joe Perches63c3a662011-04-26 08:12:10 +000011344 if (tg3_flag(tp, 5717_PLUS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011345 mem_tbl = mem_tbl_5717;
11346 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
11347 mem_tbl = mem_tbl_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000011348 else if (tg3_flag(tp, 5755_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080011349 mem_tbl = mem_tbl_5755;
11350 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
11351 mem_tbl = mem_tbl_5906;
Joe Perches63c3a662011-04-26 08:12:10 +000011352 else if (tg3_flag(tp, 5705_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080011353 mem_tbl = mem_tbl_5705;
11354 else
Michael Chan7942e1d2005-05-29 14:58:36 -070011355 mem_tbl = mem_tbl_570x;
11356
11357 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000011358 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
11359 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070011360 break;
11361 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011362
Michael Chan7942e1d2005-05-29 14:58:36 -070011363 return err;
11364}
11365
Matt Carlsonbb158d62011-04-25 12:42:47 +000011366#define TG3_TSO_MSS 500
11367
11368#define TG3_TSO_IP_HDR_LEN 20
11369#define TG3_TSO_TCP_HDR_LEN 20
11370#define TG3_TSO_TCP_OPT_LEN 12
11371
11372static const u8 tg3_tso_header[] = {
113730x08, 0x00,
113740x45, 0x00, 0x00, 0x00,
113750x00, 0x00, 0x40, 0x00,
113760x40, 0x06, 0x00, 0x00,
113770x0a, 0x00, 0x00, 0x01,
113780x0a, 0x00, 0x00, 0x02,
113790x0d, 0x00, 0xe0, 0x00,
113800x00, 0x00, 0x01, 0x00,
113810x00, 0x00, 0x02, 0x00,
113820x80, 0x10, 0x10, 0x00,
113830x14, 0x09, 0x00, 0x00,
113840x01, 0x01, 0x08, 0x0a,
113850x11, 0x11, 0x11, 0x11,
113860x11, 0x11, 0x11, 0x11,
11387};
Michael Chan9f40dea2005-09-05 17:53:06 -070011388
Matt Carlson28a45952011-08-19 13:58:22 +000011389static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, bool tso_loopback)
Michael Chanc76949a2005-05-29 14:58:59 -070011390{
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011391 u32 rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011392 u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
Matt Carlson84b67b22011-07-27 14:20:52 +000011393 u32 budget;
Michael Chanc76949a2005-05-29 14:58:59 -070011394 struct sk_buff *skb, *rx_skb;
11395 u8 *tx_data;
11396 dma_addr_t map;
11397 int num_pkts, tx_len, rx_len, i, err;
11398 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000011399 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000011400 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070011401
Matt Carlsonc8873402010-02-12 14:47:11 +000011402 tnapi = &tp->napi[0];
11403 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011404 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +000011405 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlson1da85aa2010-09-30 10:34:34 +000011406 rnapi = &tp->napi[1];
Joe Perches63c3a662011-04-26 08:12:10 +000011407 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc8873402010-02-12 14:47:11 +000011408 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011409 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011410 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000011411
Michael Chanc76949a2005-05-29 14:58:59 -070011412 err = -EIO;
11413
Matt Carlson4852a862011-04-13 11:05:07 +000011414 tx_len = pktsz;
David S. Millera20e9c62006-07-31 22:38:16 -070011415 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070011416 if (!skb)
11417 return -ENOMEM;
11418
Michael Chanc76949a2005-05-29 14:58:59 -070011419 tx_data = skb_put(skb, tx_len);
11420 memcpy(tx_data, tp->dev->dev_addr, 6);
11421 memset(tx_data + 6, 0x0, 8);
11422
Matt Carlson4852a862011-04-13 11:05:07 +000011423 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
Michael Chanc76949a2005-05-29 14:58:59 -070011424
Matt Carlson28a45952011-08-19 13:58:22 +000011425 if (tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011426 struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
11427
11428 u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
11429 TG3_TSO_TCP_OPT_LEN;
11430
11431 memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
11432 sizeof(tg3_tso_header));
11433 mss = TG3_TSO_MSS;
11434
11435 val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
11436 num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
11437
11438 /* Set the total length field in the IP header */
11439 iph->tot_len = htons((u16)(mss + hdr_len));
11440
11441 base_flags = (TXD_FLAG_CPU_PRE_DMA |
11442 TXD_FLAG_CPU_POST_DMA);
11443
Joe Perches63c3a662011-04-26 08:12:10 +000011444 if (tg3_flag(tp, HW_TSO_1) ||
11445 tg3_flag(tp, HW_TSO_2) ||
11446 tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011447 struct tcphdr *th;
11448 val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
11449 th = (struct tcphdr *)&tx_data[val];
11450 th->check = 0;
11451 } else
11452 base_flags |= TXD_FLAG_TCPUDP_CSUM;
11453
Joe Perches63c3a662011-04-26 08:12:10 +000011454 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011455 mss |= (hdr_len & 0xc) << 12;
11456 if (hdr_len & 0x10)
11457 base_flags |= 0x00000010;
11458 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +000011459 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlsonbb158d62011-04-25 12:42:47 +000011460 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +000011461 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlsonbb158d62011-04-25 12:42:47 +000011462 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
11463 mss |= (TG3_TSO_TCP_OPT_LEN << 9);
11464 } else {
11465 base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
11466 }
11467
11468 data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
11469 } else {
11470 num_pkts = 1;
11471 data_off = ETH_HLEN;
11472 }
11473
11474 for (i = data_off; i < tx_len; i++)
Michael Chanc76949a2005-05-29 14:58:59 -070011475 tx_data[i] = (u8) (i & 0xff);
11476
Alexander Duyckf4188d82009-12-02 16:48:38 +000011477 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
11478 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000011479 dev_kfree_skb(skb);
11480 return -EIO;
11481 }
Michael Chanc76949a2005-05-29 14:58:59 -070011482
Matt Carlson0d681b22011-07-27 14:20:49 +000011483 val = tnapi->tx_prod;
11484 tnapi->tx_buffers[val].skb = skb;
11485 dma_unmap_addr_set(&tnapi->tx_buffers[val], mapping, map);
11486
Michael Chanc76949a2005-05-29 14:58:59 -070011487 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011488 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011489
11490 udelay(10);
11491
Matt Carlson898a56f2009-08-28 14:02:40 +000011492 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070011493
Matt Carlson84b67b22011-07-27 14:20:52 +000011494 budget = tg3_tx_avail(tnapi);
11495 if (tg3_tx_frag_set(tnapi, &val, &budget, map, tx_len,
Matt Carlsond1a3b732011-07-27 14:20:51 +000011496 base_flags | TXD_FLAG_END, mss, 0)) {
11497 tnapi->tx_buffers[val].skb = NULL;
11498 dev_kfree_skb(skb);
11499 return -EIO;
11500 }
Michael Chanc76949a2005-05-29 14:58:59 -070011501
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011502 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070011503
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011504 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
11505 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070011506
11507 udelay(10);
11508
Matt Carlson303fc922009-11-02 14:27:34 +000011509 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
11510 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070011511 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011512 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011513
11514 udelay(10);
11515
Matt Carlson898a56f2009-08-28 14:02:40 +000011516 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
11517 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011518 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070011519 (rx_idx == (rx_start_idx + num_pkts)))
11520 break;
11521 }
11522
Matt Carlson0d681b22011-07-27 14:20:49 +000011523 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod - 1, 0);
Michael Chanc76949a2005-05-29 14:58:59 -070011524 dev_kfree_skb(skb);
11525
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011526 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070011527 goto out;
11528
11529 if (rx_idx != rx_start_idx + num_pkts)
11530 goto out;
11531
Matt Carlsonbb158d62011-04-25 12:42:47 +000011532 val = data_off;
11533 while (rx_idx != rx_start_idx) {
11534 desc = &rnapi->rx_rcb[rx_start_idx++];
11535 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
11536 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
Michael Chanc76949a2005-05-29 14:58:59 -070011537
Matt Carlsonbb158d62011-04-25 12:42:47 +000011538 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
11539 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
Matt Carlson4852a862011-04-13 11:05:07 +000011540 goto out;
Michael Chanc76949a2005-05-29 14:58:59 -070011541
Matt Carlsonbb158d62011-04-25 12:42:47 +000011542 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
11543 - ETH_FCS_LEN;
11544
Matt Carlson28a45952011-08-19 13:58:22 +000011545 if (!tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011546 if (rx_len != tx_len)
11547 goto out;
11548
11549 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
11550 if (opaque_key != RXD_OPAQUE_RING_STD)
11551 goto out;
11552 } else {
11553 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
11554 goto out;
11555 }
11556 } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
11557 (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
Matt Carlson54e0a672011-05-19 12:12:50 +000011558 >> RXD_TCPCSUM_SHIFT != 0xffff) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011559 goto out;
11560 }
11561
11562 if (opaque_key == RXD_OPAQUE_RING_STD) {
11563 rx_skb = tpr->rx_std_buffers[desc_idx].skb;
11564 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
11565 mapping);
11566 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
11567 rx_skb = tpr->rx_jmb_buffers[desc_idx].skb;
11568 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
11569 mapping);
11570 } else
Matt Carlson4852a862011-04-13 11:05:07 +000011571 goto out;
11572
Matt Carlsonbb158d62011-04-25 12:42:47 +000011573 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
11574 PCI_DMA_FROMDEVICE);
11575
11576 for (i = data_off; i < rx_len; i++, val++) {
11577 if (*(rx_skb->data + i) != (u8) (val & 0xff))
11578 goto out;
11579 }
Matt Carlson4852a862011-04-13 11:05:07 +000011580 }
11581
Michael Chanc76949a2005-05-29 14:58:59 -070011582 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011583
Michael Chanc76949a2005-05-29 14:58:59 -070011584 /* tg3_free_rings will unmap and free the rx_skb */
11585out:
11586 return err;
11587}
11588
Matt Carlson00c266b2011-04-25 12:42:46 +000011589#define TG3_STD_LOOPBACK_FAILED 1
11590#define TG3_JMB_LOOPBACK_FAILED 2
Matt Carlsonbb158d62011-04-25 12:42:47 +000011591#define TG3_TSO_LOOPBACK_FAILED 4
Matt Carlson28a45952011-08-19 13:58:22 +000011592#define TG3_LOOPBACK_FAILED \
11593 (TG3_STD_LOOPBACK_FAILED | \
11594 TG3_JMB_LOOPBACK_FAILED | \
11595 TG3_TSO_LOOPBACK_FAILED)
Matt Carlson00c266b2011-04-25 12:42:46 +000011596
Matt Carlson941ec902011-08-19 13:58:23 +000011597static int tg3_test_loopback(struct tg3 *tp, u64 *data, bool do_extlpbk)
Michael Chan9f40dea2005-09-05 17:53:06 -070011598{
Matt Carlson28a45952011-08-19 13:58:22 +000011599 int err = -EIO;
Matt Carlson2215e242011-08-19 13:58:19 +000011600 u32 eee_cap;
Michael Chan9f40dea2005-09-05 17:53:06 -070011601
Matt Carlsonab789042011-01-25 15:58:54 +000011602 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
11603 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
11604
Matt Carlson28a45952011-08-19 13:58:22 +000011605 if (!netif_running(tp->dev)) {
11606 data[0] = TG3_LOOPBACK_FAILED;
11607 data[1] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000011608 if (do_extlpbk)
11609 data[2] = TG3_LOOPBACK_FAILED;
Matt Carlson28a45952011-08-19 13:58:22 +000011610 goto done;
11611 }
11612
Michael Chanb9ec6c12006-07-25 16:37:27 -070011613 err = tg3_reset_hw(tp, 1);
Matt Carlsonab789042011-01-25 15:58:54 +000011614 if (err) {
Matt Carlson28a45952011-08-19 13:58:22 +000011615 data[0] = TG3_LOOPBACK_FAILED;
11616 data[1] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000011617 if (do_extlpbk)
11618 data[2] = TG3_LOOPBACK_FAILED;
Matt Carlsonab789042011-01-25 15:58:54 +000011619 goto done;
11620 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011621
Joe Perches63c3a662011-04-26 08:12:10 +000011622 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson4a85f092011-04-20 07:57:37 +000011623 int i;
11624
11625 /* Reroute all rx packets to the 1st queue */
11626 for (i = MAC_RSS_INDIR_TBL_0;
11627 i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
11628 tw32(i, 0x0);
11629 }
11630
Matt Carlson6e01b202011-08-19 13:58:20 +000011631 /* HW errata - mac loopback fails in some cases on 5780.
11632 * Normal traffic and PHY loopback are not affected by
11633 * errata. Also, the MAC loopback test is deprecated for
11634 * all newer ASIC revisions.
11635 */
11636 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
11637 !tg3_flag(tp, CPMU_PRESENT)) {
11638 tg3_mac_loopback(tp, true);
Matt Carlson9936bcf2007-10-10 18:03:07 -070011639
Matt Carlson28a45952011-08-19 13:58:22 +000011640 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
11641 data[0] |= TG3_STD_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000011642
11643 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000011644 tg3_run_loopback(tp, 9000 + ETH_HLEN, false))
11645 data[0] |= TG3_JMB_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000011646
11647 tg3_mac_loopback(tp, false);
11648 }
Matt Carlson4852a862011-04-13 11:05:07 +000011649
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011650 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000011651 !tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011652 int i;
11653
Matt Carlson941ec902011-08-19 13:58:23 +000011654 tg3_phy_lpbk_set(tp, 0, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011655
11656 /* Wait for link */
11657 for (i = 0; i < 100; i++) {
11658 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
11659 break;
11660 mdelay(1);
11661 }
11662
Matt Carlson28a45952011-08-19 13:58:22 +000011663 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
11664 data[1] |= TG3_STD_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000011665 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000011666 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
11667 data[1] |= TG3_TSO_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000011668 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000011669 tg3_run_loopback(tp, 9000 + ETH_HLEN, false))
11670 data[1] |= TG3_JMB_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070011671
Matt Carlson941ec902011-08-19 13:58:23 +000011672 if (do_extlpbk) {
11673 tg3_phy_lpbk_set(tp, 0, true);
11674
11675 /* All link indications report up, but the hardware
11676 * isn't really ready for about 20 msec. Double it
11677 * to be sure.
11678 */
11679 mdelay(40);
11680
11681 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
11682 data[2] |= TG3_STD_LOOPBACK_FAILED;
11683 if (tg3_flag(tp, TSO_CAPABLE) &&
11684 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
11685 data[2] |= TG3_TSO_LOOPBACK_FAILED;
11686 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
11687 tg3_run_loopback(tp, 9000 + ETH_HLEN, false))
11688 data[2] |= TG3_JMB_LOOPBACK_FAILED;
11689 }
11690
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011691 /* Re-enable gphy autopowerdown. */
11692 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
11693 tg3_phy_toggle_apd(tp, true);
11694 }
Matt Carlson6833c042008-11-21 17:18:59 -080011695
Matt Carlson941ec902011-08-19 13:58:23 +000011696 err = (data[0] | data[1] | data[2]) ? -EIO : 0;
Matt Carlson28a45952011-08-19 13:58:22 +000011697
Matt Carlsonab789042011-01-25 15:58:54 +000011698done:
11699 tp->phy_flags |= eee_cap;
11700
Michael Chan9f40dea2005-09-05 17:53:06 -070011701 return err;
11702}
11703
Michael Chan4cafd3f2005-05-29 14:56:34 -070011704static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
11705 u64 *data)
11706{
Michael Chan566f86a2005-05-29 14:56:58 -070011707 struct tg3 *tp = netdev_priv(dev);
Matt Carlson941ec902011-08-19 13:58:23 +000011708 bool doextlpbk = etest->flags & ETH_TEST_FL_EXTERNAL_LB;
Michael Chan566f86a2005-05-29 14:56:58 -070011709
Matt Carlsonbed98292011-07-13 09:27:29 +000011710 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
11711 tg3_power_up(tp)) {
11712 etest->flags |= ETH_TEST_FL_FAILED;
11713 memset(data, 1, sizeof(u64) * TG3_NUM_TEST);
11714 return;
11715 }
Michael Chanbc1c7562006-03-20 17:48:03 -080011716
Michael Chan566f86a2005-05-29 14:56:58 -070011717 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
11718
11719 if (tg3_test_nvram(tp) != 0) {
11720 etest->flags |= ETH_TEST_FL_FAILED;
11721 data[0] = 1;
11722 }
Matt Carlson941ec902011-08-19 13:58:23 +000011723 if (!doextlpbk && tg3_test_link(tp)) {
Michael Chanca430072005-05-29 14:57:23 -070011724 etest->flags |= ETH_TEST_FL_FAILED;
11725 data[1] = 1;
11726 }
Michael Chana71116d2005-05-29 14:58:11 -070011727 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011728 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070011729
Michael Chanbbe832c2005-06-24 20:20:04 -070011730 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011731 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070011732 tg3_netif_stop(tp);
11733 irq_sync = 1;
11734 }
11735
11736 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070011737
11738 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080011739 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011740 tg3_halt_cpu(tp, RX_CPU_BASE);
Joe Perches63c3a662011-04-26 08:12:10 +000011741 if (!tg3_flag(tp, 5705_PLUS))
Michael Chana71116d2005-05-29 14:58:11 -070011742 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080011743 if (!err)
11744 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011745
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011746 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad2006-03-20 22:27:35 -080011747 tg3_phy_reset(tp);
11748
Michael Chana71116d2005-05-29 14:58:11 -070011749 if (tg3_test_registers(tp) != 0) {
11750 etest->flags |= ETH_TEST_FL_FAILED;
11751 data[2] = 1;
11752 }
Matt Carlson28a45952011-08-19 13:58:22 +000011753
Michael Chan7942e1d2005-05-29 14:58:36 -070011754 if (tg3_test_memory(tp) != 0) {
11755 etest->flags |= ETH_TEST_FL_FAILED;
11756 data[3] = 1;
11757 }
Matt Carlson28a45952011-08-19 13:58:22 +000011758
Matt Carlson941ec902011-08-19 13:58:23 +000011759 if (doextlpbk)
11760 etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
11761
11762 if (tg3_test_loopback(tp, &data[4], doextlpbk))
Michael Chanc76949a2005-05-29 14:58:59 -070011763 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070011764
David S. Millerf47c11e2005-06-24 20:18:35 -070011765 tg3_full_unlock(tp);
11766
Michael Chand4bc3922005-05-29 14:59:20 -070011767 if (tg3_test_interrupt(tp) != 0) {
11768 etest->flags |= ETH_TEST_FL_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000011769 data[7] = 1;
Michael Chand4bc3922005-05-29 14:59:20 -070011770 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011771
11772 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070011773
Michael Chana71116d2005-05-29 14:58:11 -070011774 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
11775 if (netif_running(dev)) {
Joe Perches63c3a662011-04-26 08:12:10 +000011776 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011777 err2 = tg3_restart_hw(tp, 1);
11778 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070011779 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011780 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011781
11782 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011783
11784 if (irq_sync && !err2)
11785 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011786 }
Matt Carlson80096062010-08-02 11:26:06 +000011787 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000011788 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080011789
Michael Chan4cafd3f2005-05-29 14:56:34 -070011790}
11791
Linus Torvalds1da177e2005-04-16 15:20:36 -070011792static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
11793{
11794 struct mii_ioctl_data *data = if_mii(ifr);
11795 struct tg3 *tp = netdev_priv(dev);
11796 int err;
11797
Joe Perches63c3a662011-04-26 08:12:10 +000011798 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011799 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011800 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011801 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011802 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000011803 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011804 }
11805
Matt Carlson33f401a2010-04-05 10:19:27 +000011806 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011807 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000011808 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011809
11810 /* fallthru */
11811 case SIOCGMIIREG: {
11812 u32 mii_regval;
11813
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011814 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011815 break; /* We have no PHY */
11816
Matt Carlson34eea5a2011-04-20 07:57:38 +000011817 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080011818 return -EAGAIN;
11819
David S. Millerf47c11e2005-06-24 20:18:35 -070011820 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011821 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070011822 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011823
11824 data->val_out = mii_regval;
11825
11826 return err;
11827 }
11828
11829 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011830 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011831 break; /* We have no PHY */
11832
Matt Carlson34eea5a2011-04-20 07:57:38 +000011833 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080011834 return -EAGAIN;
11835
David S. Millerf47c11e2005-06-24 20:18:35 -070011836 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011837 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070011838 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011839
11840 return err;
11841
11842 default:
11843 /* do nothing */
11844 break;
11845 }
11846 return -EOPNOTSUPP;
11847}
11848
David S. Miller15f98502005-05-18 22:49:26 -070011849static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11850{
11851 struct tg3 *tp = netdev_priv(dev);
11852
11853 memcpy(ec, &tp->coal, sizeof(*ec));
11854 return 0;
11855}
11856
Michael Chand244c892005-07-05 14:42:33 -070011857static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11858{
11859 struct tg3 *tp = netdev_priv(dev);
11860 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
11861 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
11862
Joe Perches63c3a662011-04-26 08:12:10 +000011863 if (!tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070011864 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
11865 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
11866 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
11867 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
11868 }
11869
11870 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
11871 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
11872 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
11873 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
11874 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
11875 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
11876 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
11877 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
11878 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
11879 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
11880 return -EINVAL;
11881
11882 /* No rx interrupts will be generated if both are zero */
11883 if ((ec->rx_coalesce_usecs == 0) &&
11884 (ec->rx_max_coalesced_frames == 0))
11885 return -EINVAL;
11886
11887 /* No tx interrupts will be generated if both are zero */
11888 if ((ec->tx_coalesce_usecs == 0) &&
11889 (ec->tx_max_coalesced_frames == 0))
11890 return -EINVAL;
11891
11892 /* Only copy relevant parameters, ignore all others. */
11893 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
11894 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
11895 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
11896 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
11897 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
11898 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
11899 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
11900 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
11901 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
11902
11903 if (netif_running(dev)) {
11904 tg3_full_lock(tp, 0);
11905 __tg3_set_coalesce(tp, &tp->coal);
11906 tg3_full_unlock(tp);
11907 }
11908 return 0;
11909}
11910
Jeff Garzik7282d492006-09-13 14:30:00 -040011911static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011912 .get_settings = tg3_get_settings,
11913 .set_settings = tg3_set_settings,
11914 .get_drvinfo = tg3_get_drvinfo,
11915 .get_regs_len = tg3_get_regs_len,
11916 .get_regs = tg3_get_regs,
11917 .get_wol = tg3_get_wol,
11918 .set_wol = tg3_set_wol,
11919 .get_msglevel = tg3_get_msglevel,
11920 .set_msglevel = tg3_set_msglevel,
11921 .nway_reset = tg3_nway_reset,
11922 .get_link = ethtool_op_get_link,
11923 .get_eeprom_len = tg3_get_eeprom_len,
11924 .get_eeprom = tg3_get_eeprom,
11925 .set_eeprom = tg3_set_eeprom,
11926 .get_ringparam = tg3_get_ringparam,
11927 .set_ringparam = tg3_set_ringparam,
11928 .get_pauseparam = tg3_get_pauseparam,
11929 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070011930 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011931 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000011932 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011933 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070011934 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070011935 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070011936 .get_sset_count = tg3_get_sset_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011937};
11938
11939static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
11940{
Michael Chan1b277772006-03-20 22:27:48 -080011941 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011942
11943 tp->nvram_size = EEPROM_CHIP_SIZE;
11944
Matt Carlsone4f34112009-02-25 14:25:00 +000011945 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011946 return;
11947
Michael Chanb16250e2006-09-27 16:10:14 -070011948 if ((magic != TG3_EEPROM_MAGIC) &&
11949 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
11950 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011951 return;
11952
11953 /*
11954 * Size the chip by reading offsets at increasing powers of two.
11955 * When we encounter our validation signature, we know the addressing
11956 * has wrapped around, and thus have our chip size.
11957 */
Michael Chan1b277772006-03-20 22:27:48 -080011958 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011959
11960 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000011961 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011962 return;
11963
Michael Chan18201802006-03-20 22:29:15 -080011964 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011965 break;
11966
11967 cursize <<= 1;
11968 }
11969
11970 tp->nvram_size = cursize;
11971}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011972
Linus Torvalds1da177e2005-04-16 15:20:36 -070011973static void __devinit tg3_get_nvram_size(struct tg3 *tp)
11974{
11975 u32 val;
11976
Joe Perches63c3a662011-04-26 08:12:10 +000011977 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080011978 return;
11979
11980 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080011981 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080011982 tg3_get_eeprom_size(tp);
11983 return;
11984 }
11985
Matt Carlson6d348f22009-02-25 14:25:52 +000011986 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011987 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000011988 /* This is confusing. We want to operate on the
11989 * 16-bit value at offset 0xf2. The tg3_nvram_read()
11990 * call will read from NVRAM and byteswap the data
11991 * according to the byteswapping settings for all
11992 * other register accesses. This ensures the data we
11993 * want will always reside in the lower 16-bits.
11994 * However, the data in NVRAM is in LE format, which
11995 * means the data from the NVRAM read will always be
11996 * opposite the endianness of the CPU. The 16-bit
11997 * byteswap then brings the data to CPU endianness.
11998 */
11999 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012000 return;
12001 }
12002 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070012003 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012004}
12005
12006static void __devinit tg3_get_nvram_info(struct tg3 *tp)
12007{
12008 u32 nvcfg1;
12009
12010 nvcfg1 = tr32(NVRAM_CFG1);
12011 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
Joe Perches63c3a662011-04-26 08:12:10 +000012012 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012013 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012014 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12015 tw32(NVRAM_CFG1, nvcfg1);
12016 }
12017
Matt Carlson6ff6f812011-05-19 12:12:54 +000012018 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
Joe Perches63c3a662011-04-26 08:12:10 +000012019 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012020 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012021 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
12022 tp->nvram_jedecnum = JEDEC_ATMEL;
12023 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012024 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012025 break;
12026 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
12027 tp->nvram_jedecnum = JEDEC_ATMEL;
12028 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
12029 break;
12030 case FLASH_VENDOR_ATMEL_EEPROM:
12031 tp->nvram_jedecnum = JEDEC_ATMEL;
12032 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012033 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012034 break;
12035 case FLASH_VENDOR_ST:
12036 tp->nvram_jedecnum = JEDEC_ST;
12037 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012038 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012039 break;
12040 case FLASH_VENDOR_SAIFUN:
12041 tp->nvram_jedecnum = JEDEC_SAIFUN;
12042 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
12043 break;
12044 case FLASH_VENDOR_SST_SMALL:
12045 case FLASH_VENDOR_SST_LARGE:
12046 tp->nvram_jedecnum = JEDEC_SST;
12047 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
12048 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012049 }
Matt Carlson8590a602009-08-28 12:29:16 +000012050 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012051 tp->nvram_jedecnum = JEDEC_ATMEL;
12052 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012053 tg3_flag_set(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012054 }
12055}
12056
Matt Carlsona1b950d2009-09-01 13:20:17 +000012057static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
12058{
12059 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
12060 case FLASH_5752PAGE_SIZE_256:
12061 tp->nvram_pagesize = 256;
12062 break;
12063 case FLASH_5752PAGE_SIZE_512:
12064 tp->nvram_pagesize = 512;
12065 break;
12066 case FLASH_5752PAGE_SIZE_1K:
12067 tp->nvram_pagesize = 1024;
12068 break;
12069 case FLASH_5752PAGE_SIZE_2K:
12070 tp->nvram_pagesize = 2048;
12071 break;
12072 case FLASH_5752PAGE_SIZE_4K:
12073 tp->nvram_pagesize = 4096;
12074 break;
12075 case FLASH_5752PAGE_SIZE_264:
12076 tp->nvram_pagesize = 264;
12077 break;
12078 case FLASH_5752PAGE_SIZE_528:
12079 tp->nvram_pagesize = 528;
12080 break;
12081 }
12082}
12083
Michael Chan361b4ac2005-04-21 17:11:21 -070012084static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
12085{
12086 u32 nvcfg1;
12087
12088 nvcfg1 = tr32(NVRAM_CFG1);
12089
Michael Chane6af3012005-04-21 17:12:05 -070012090 /* NVRAM protection for TPM */
12091 if (nvcfg1 & (1 << 27))
Joe Perches63c3a662011-04-26 08:12:10 +000012092 tg3_flag_set(tp, PROTECTED_NVRAM);
Michael Chane6af3012005-04-21 17:12:05 -070012093
Michael Chan361b4ac2005-04-21 17:11:21 -070012094 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012095 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
12096 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
12097 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012098 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012099 break;
12100 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12101 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012102 tg3_flag_set(tp, NVRAM_BUFFERED);
12103 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012104 break;
12105 case FLASH_5752VENDOR_ST_M45PE10:
12106 case FLASH_5752VENDOR_ST_M45PE20:
12107 case FLASH_5752VENDOR_ST_M45PE40:
12108 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012109 tg3_flag_set(tp, NVRAM_BUFFERED);
12110 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012111 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070012112 }
12113
Joe Perches63c3a662011-04-26 08:12:10 +000012114 if (tg3_flag(tp, FLASH)) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000012115 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000012116 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070012117 /* For eeprom, set pagesize to maximum eeprom size */
12118 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12119
12120 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12121 tw32(NVRAM_CFG1, nvcfg1);
12122 }
12123}
12124
Michael Chand3c7b882006-03-23 01:28:25 -080012125static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
12126{
Matt Carlson989a9d22007-05-05 11:51:05 -070012127 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080012128
12129 nvcfg1 = tr32(NVRAM_CFG1);
12130
12131 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070012132 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012133 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson989a9d22007-05-05 11:51:05 -070012134 protect = 1;
12135 }
Michael Chand3c7b882006-03-23 01:28:25 -080012136
Matt Carlson989a9d22007-05-05 11:51:05 -070012137 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
12138 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012139 case FLASH_5755VENDOR_ATMEL_FLASH_1:
12140 case FLASH_5755VENDOR_ATMEL_FLASH_2:
12141 case FLASH_5755VENDOR_ATMEL_FLASH_3:
12142 case FLASH_5755VENDOR_ATMEL_FLASH_5:
12143 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012144 tg3_flag_set(tp, NVRAM_BUFFERED);
12145 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012146 tp->nvram_pagesize = 264;
12147 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
12148 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
12149 tp->nvram_size = (protect ? 0x3e200 :
12150 TG3_NVRAM_SIZE_512KB);
12151 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
12152 tp->nvram_size = (protect ? 0x1f200 :
12153 TG3_NVRAM_SIZE_256KB);
12154 else
12155 tp->nvram_size = (protect ? 0x1f200 :
12156 TG3_NVRAM_SIZE_128KB);
12157 break;
12158 case FLASH_5752VENDOR_ST_M45PE10:
12159 case FLASH_5752VENDOR_ST_M45PE20:
12160 case FLASH_5752VENDOR_ST_M45PE40:
12161 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012162 tg3_flag_set(tp, NVRAM_BUFFERED);
12163 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012164 tp->nvram_pagesize = 256;
12165 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
12166 tp->nvram_size = (protect ?
12167 TG3_NVRAM_SIZE_64KB :
12168 TG3_NVRAM_SIZE_128KB);
12169 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
12170 tp->nvram_size = (protect ?
12171 TG3_NVRAM_SIZE_64KB :
12172 TG3_NVRAM_SIZE_256KB);
12173 else
12174 tp->nvram_size = (protect ?
12175 TG3_NVRAM_SIZE_128KB :
12176 TG3_NVRAM_SIZE_512KB);
12177 break;
Michael Chand3c7b882006-03-23 01:28:25 -080012178 }
12179}
12180
Michael Chan1b277772006-03-20 22:27:48 -080012181static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
12182{
12183 u32 nvcfg1;
12184
12185 nvcfg1 = tr32(NVRAM_CFG1);
12186
12187 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012188 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
12189 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
12190 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
12191 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
12192 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012193 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012194 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080012195
Matt Carlson8590a602009-08-28 12:29:16 +000012196 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12197 tw32(NVRAM_CFG1, nvcfg1);
12198 break;
12199 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12200 case FLASH_5755VENDOR_ATMEL_FLASH_1:
12201 case FLASH_5755VENDOR_ATMEL_FLASH_2:
12202 case FLASH_5755VENDOR_ATMEL_FLASH_3:
12203 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012204 tg3_flag_set(tp, NVRAM_BUFFERED);
12205 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012206 tp->nvram_pagesize = 264;
12207 break;
12208 case FLASH_5752VENDOR_ST_M45PE10:
12209 case FLASH_5752VENDOR_ST_M45PE20:
12210 case FLASH_5752VENDOR_ST_M45PE40:
12211 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012212 tg3_flag_set(tp, NVRAM_BUFFERED);
12213 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012214 tp->nvram_pagesize = 256;
12215 break;
Michael Chan1b277772006-03-20 22:27:48 -080012216 }
12217}
12218
Matt Carlson6b91fa02007-10-10 18:01:09 -070012219static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
12220{
12221 u32 nvcfg1, protect = 0;
12222
12223 nvcfg1 = tr32(NVRAM_CFG1);
12224
12225 /* NVRAM protection for TPM */
12226 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012227 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson6b91fa02007-10-10 18:01:09 -070012228 protect = 1;
12229 }
12230
12231 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
12232 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012233 case FLASH_5761VENDOR_ATMEL_ADB021D:
12234 case FLASH_5761VENDOR_ATMEL_ADB041D:
12235 case FLASH_5761VENDOR_ATMEL_ADB081D:
12236 case FLASH_5761VENDOR_ATMEL_ADB161D:
12237 case FLASH_5761VENDOR_ATMEL_MDB021D:
12238 case FLASH_5761VENDOR_ATMEL_MDB041D:
12239 case FLASH_5761VENDOR_ATMEL_MDB081D:
12240 case FLASH_5761VENDOR_ATMEL_MDB161D:
12241 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012242 tg3_flag_set(tp, NVRAM_BUFFERED);
12243 tg3_flag_set(tp, FLASH);
12244 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson8590a602009-08-28 12:29:16 +000012245 tp->nvram_pagesize = 256;
12246 break;
12247 case FLASH_5761VENDOR_ST_A_M45PE20:
12248 case FLASH_5761VENDOR_ST_A_M45PE40:
12249 case FLASH_5761VENDOR_ST_A_M45PE80:
12250 case FLASH_5761VENDOR_ST_A_M45PE16:
12251 case FLASH_5761VENDOR_ST_M_M45PE20:
12252 case FLASH_5761VENDOR_ST_M_M45PE40:
12253 case FLASH_5761VENDOR_ST_M_M45PE80:
12254 case FLASH_5761VENDOR_ST_M_M45PE16:
12255 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012256 tg3_flag_set(tp, NVRAM_BUFFERED);
12257 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012258 tp->nvram_pagesize = 256;
12259 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070012260 }
12261
12262 if (protect) {
12263 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
12264 } else {
12265 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012266 case FLASH_5761VENDOR_ATMEL_ADB161D:
12267 case FLASH_5761VENDOR_ATMEL_MDB161D:
12268 case FLASH_5761VENDOR_ST_A_M45PE16:
12269 case FLASH_5761VENDOR_ST_M_M45PE16:
12270 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
12271 break;
12272 case FLASH_5761VENDOR_ATMEL_ADB081D:
12273 case FLASH_5761VENDOR_ATMEL_MDB081D:
12274 case FLASH_5761VENDOR_ST_A_M45PE80:
12275 case FLASH_5761VENDOR_ST_M_M45PE80:
12276 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12277 break;
12278 case FLASH_5761VENDOR_ATMEL_ADB041D:
12279 case FLASH_5761VENDOR_ATMEL_MDB041D:
12280 case FLASH_5761VENDOR_ST_A_M45PE40:
12281 case FLASH_5761VENDOR_ST_M_M45PE40:
12282 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12283 break;
12284 case FLASH_5761VENDOR_ATMEL_ADB021D:
12285 case FLASH_5761VENDOR_ATMEL_MDB021D:
12286 case FLASH_5761VENDOR_ST_A_M45PE20:
12287 case FLASH_5761VENDOR_ST_M_M45PE20:
12288 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12289 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070012290 }
12291 }
12292}
12293
Michael Chanb5d37722006-09-27 16:06:21 -070012294static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
12295{
12296 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012297 tg3_flag_set(tp, NVRAM_BUFFERED);
Michael Chanb5d37722006-09-27 16:06:21 -070012298 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12299}
12300
Matt Carlson321d32a2008-11-21 17:22:19 -080012301static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
12302{
12303 u32 nvcfg1;
12304
12305 nvcfg1 = tr32(NVRAM_CFG1);
12306
12307 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12308 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
12309 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
12310 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012311 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson321d32a2008-11-21 17:22:19 -080012312 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12313
12314 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12315 tw32(NVRAM_CFG1, nvcfg1);
12316 return;
12317 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12318 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
12319 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
12320 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
12321 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
12322 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
12323 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
12324 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012325 tg3_flag_set(tp, NVRAM_BUFFERED);
12326 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080012327
12328 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12329 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12330 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
12331 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
12332 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12333 break;
12334 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
12335 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
12336 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12337 break;
12338 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
12339 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
12340 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12341 break;
12342 }
12343 break;
12344 case FLASH_5752VENDOR_ST_M45PE10:
12345 case FLASH_5752VENDOR_ST_M45PE20:
12346 case FLASH_5752VENDOR_ST_M45PE40:
12347 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012348 tg3_flag_set(tp, NVRAM_BUFFERED);
12349 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080012350
12351 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12352 case FLASH_5752VENDOR_ST_M45PE10:
12353 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12354 break;
12355 case FLASH_5752VENDOR_ST_M45PE20:
12356 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12357 break;
12358 case FLASH_5752VENDOR_ST_M45PE40:
12359 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12360 break;
12361 }
12362 break;
12363 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012364 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson321d32a2008-11-21 17:22:19 -080012365 return;
12366 }
12367
Matt Carlsona1b950d2009-09-01 13:20:17 +000012368 tg3_nvram_get_pagesize(tp, nvcfg1);
12369 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012370 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012371}
12372
12373
12374static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
12375{
12376 u32 nvcfg1;
12377
12378 nvcfg1 = tr32(NVRAM_CFG1);
12379
12380 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12381 case FLASH_5717VENDOR_ATMEL_EEPROM:
12382 case FLASH_5717VENDOR_MICRO_EEPROM:
12383 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012384 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012385 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12386
12387 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12388 tw32(NVRAM_CFG1, nvcfg1);
12389 return;
12390 case FLASH_5717VENDOR_ATMEL_MDB011D:
12391 case FLASH_5717VENDOR_ATMEL_ADB011B:
12392 case FLASH_5717VENDOR_ATMEL_ADB011D:
12393 case FLASH_5717VENDOR_ATMEL_MDB021D:
12394 case FLASH_5717VENDOR_ATMEL_ADB021B:
12395 case FLASH_5717VENDOR_ATMEL_ADB021D:
12396 case FLASH_5717VENDOR_ATMEL_45USPT:
12397 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012398 tg3_flag_set(tp, NVRAM_BUFFERED);
12399 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012400
12401 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12402 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012403 /* Detect size with tg3_nvram_get_size() */
12404 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012405 case FLASH_5717VENDOR_ATMEL_ADB021B:
12406 case FLASH_5717VENDOR_ATMEL_ADB021D:
12407 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12408 break;
12409 default:
12410 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12411 break;
12412 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012413 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012414 case FLASH_5717VENDOR_ST_M_M25PE10:
12415 case FLASH_5717VENDOR_ST_A_M25PE10:
12416 case FLASH_5717VENDOR_ST_M_M45PE10:
12417 case FLASH_5717VENDOR_ST_A_M45PE10:
12418 case FLASH_5717VENDOR_ST_M_M25PE20:
12419 case FLASH_5717VENDOR_ST_A_M25PE20:
12420 case FLASH_5717VENDOR_ST_M_M45PE20:
12421 case FLASH_5717VENDOR_ST_A_M45PE20:
12422 case FLASH_5717VENDOR_ST_25USPT:
12423 case FLASH_5717VENDOR_ST_45USPT:
12424 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012425 tg3_flag_set(tp, NVRAM_BUFFERED);
12426 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012427
12428 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12429 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012430 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012431 /* Detect size with tg3_nvram_get_size() */
12432 break;
12433 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012434 case FLASH_5717VENDOR_ST_A_M45PE20:
12435 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12436 break;
12437 default:
12438 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12439 break;
12440 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012441 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012442 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012443 tg3_flag_set(tp, NO_NVRAM);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012444 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080012445 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000012446
12447 tg3_nvram_get_pagesize(tp, nvcfg1);
12448 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012449 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson321d32a2008-11-21 17:22:19 -080012450}
12451
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012452static void __devinit tg3_get_5720_nvram_info(struct tg3 *tp)
12453{
12454 u32 nvcfg1, nvmpinstrp;
12455
12456 nvcfg1 = tr32(NVRAM_CFG1);
12457 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
12458
12459 switch (nvmpinstrp) {
12460 case FLASH_5720_EEPROM_HD:
12461 case FLASH_5720_EEPROM_LD:
12462 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012463 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012464
12465 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12466 tw32(NVRAM_CFG1, nvcfg1);
12467 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
12468 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12469 else
12470 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
12471 return;
12472 case FLASH_5720VENDOR_M_ATMEL_DB011D:
12473 case FLASH_5720VENDOR_A_ATMEL_DB011B:
12474 case FLASH_5720VENDOR_A_ATMEL_DB011D:
12475 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12476 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12477 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12478 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12479 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12480 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12481 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12482 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12483 case FLASH_5720VENDOR_ATMEL_45USPT:
12484 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012485 tg3_flag_set(tp, NVRAM_BUFFERED);
12486 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012487
12488 switch (nvmpinstrp) {
12489 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12490 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12491 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12492 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12493 break;
12494 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12495 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12496 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12497 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12498 break;
12499 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12500 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12501 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12502 break;
12503 default:
12504 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12505 break;
12506 }
12507 break;
12508 case FLASH_5720VENDOR_M_ST_M25PE10:
12509 case FLASH_5720VENDOR_M_ST_M45PE10:
12510 case FLASH_5720VENDOR_A_ST_M25PE10:
12511 case FLASH_5720VENDOR_A_ST_M45PE10:
12512 case FLASH_5720VENDOR_M_ST_M25PE20:
12513 case FLASH_5720VENDOR_M_ST_M45PE20:
12514 case FLASH_5720VENDOR_A_ST_M25PE20:
12515 case FLASH_5720VENDOR_A_ST_M45PE20:
12516 case FLASH_5720VENDOR_M_ST_M25PE40:
12517 case FLASH_5720VENDOR_M_ST_M45PE40:
12518 case FLASH_5720VENDOR_A_ST_M25PE40:
12519 case FLASH_5720VENDOR_A_ST_M45PE40:
12520 case FLASH_5720VENDOR_M_ST_M25PE80:
12521 case FLASH_5720VENDOR_M_ST_M45PE80:
12522 case FLASH_5720VENDOR_A_ST_M25PE80:
12523 case FLASH_5720VENDOR_A_ST_M45PE80:
12524 case FLASH_5720VENDOR_ST_25USPT:
12525 case FLASH_5720VENDOR_ST_45USPT:
12526 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012527 tg3_flag_set(tp, NVRAM_BUFFERED);
12528 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012529
12530 switch (nvmpinstrp) {
12531 case FLASH_5720VENDOR_M_ST_M25PE20:
12532 case FLASH_5720VENDOR_M_ST_M45PE20:
12533 case FLASH_5720VENDOR_A_ST_M25PE20:
12534 case FLASH_5720VENDOR_A_ST_M45PE20:
12535 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12536 break;
12537 case FLASH_5720VENDOR_M_ST_M25PE40:
12538 case FLASH_5720VENDOR_M_ST_M45PE40:
12539 case FLASH_5720VENDOR_A_ST_M25PE40:
12540 case FLASH_5720VENDOR_A_ST_M45PE40:
12541 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12542 break;
12543 case FLASH_5720VENDOR_M_ST_M25PE80:
12544 case FLASH_5720VENDOR_M_ST_M45PE80:
12545 case FLASH_5720VENDOR_A_ST_M25PE80:
12546 case FLASH_5720VENDOR_A_ST_M45PE80:
12547 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12548 break;
12549 default:
12550 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12551 break;
12552 }
12553 break;
12554 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012555 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012556 return;
12557 }
12558
12559 tg3_nvram_get_pagesize(tp, nvcfg1);
12560 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012561 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012562}
12563
Linus Torvalds1da177e2005-04-16 15:20:36 -070012564/* Chips other than 5700/5701 use the NVRAM for fetching info. */
12565static void __devinit tg3_nvram_init(struct tg3 *tp)
12566{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012567 tw32_f(GRC_EEPROM_ADDR,
12568 (EEPROM_ADDR_FSM_RESET |
12569 (EEPROM_DEFAULT_CLOCK_PERIOD <<
12570 EEPROM_ADDR_CLKPERD_SHIFT)));
12571
Michael Chan9d57f012006-12-07 00:23:25 -080012572 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012573
12574 /* Enable seeprom accesses. */
12575 tw32_f(GRC_LOCAL_CTRL,
12576 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
12577 udelay(100);
12578
12579 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
12580 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
Joe Perches63c3a662011-04-26 08:12:10 +000012581 tg3_flag_set(tp, NVRAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012582
Michael Chanec41c7d2006-01-17 02:40:55 -080012583 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000012584 netdev_warn(tp->dev,
12585 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000012586 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080012587 return;
12588 }
Michael Chane6af3012005-04-21 17:12:05 -070012589 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012590
Matt Carlson989a9d22007-05-05 11:51:05 -070012591 tp->nvram_size = 0;
12592
Michael Chan361b4ac2005-04-21 17:11:21 -070012593 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
12594 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080012595 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
12596 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070012597 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070012598 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
12599 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080012600 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070012601 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
12602 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070012603 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12604 tg3_get_5906_nvram_info(tp);
Matt Carlsonb703df62009-12-03 08:36:21 +000012605 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
12606 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson321d32a2008-11-21 17:22:19 -080012607 tg3_get_57780_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012608 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
12609 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000012610 tg3_get_5717_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012611 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
12612 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070012613 else
12614 tg3_get_nvram_info(tp);
12615
Matt Carlson989a9d22007-05-05 11:51:05 -070012616 if (tp->nvram_size == 0)
12617 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012618
Michael Chane6af3012005-04-21 17:12:05 -070012619 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080012620 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012621
12622 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000012623 tg3_flag_clear(tp, NVRAM);
12624 tg3_flag_clear(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012625
12626 tg3_get_eeprom_size(tp);
12627 }
12628}
12629
Linus Torvalds1da177e2005-04-16 15:20:36 -070012630static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
12631 u32 offset, u32 len, u8 *buf)
12632{
12633 int i, j, rc = 0;
12634 u32 val;
12635
12636 for (i = 0; i < len; i += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012637 u32 addr;
Matt Carlsona9dc5292009-02-25 14:25:30 +000012638 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012639
12640 addr = offset + i;
12641
12642 memcpy(&data, buf + i, 4);
12643
Matt Carlson62cedd12009-04-20 14:52:29 -070012644 /*
12645 * The SEEPROM interface expects the data to always be opposite
12646 * the native endian format. We accomplish this by reversing
12647 * all the operations that would have been performed on the
12648 * data from a call to tg3_nvram_read_be32().
12649 */
12650 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012651
12652 val = tr32(GRC_EEPROM_ADDR);
12653 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
12654
12655 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
12656 EEPROM_ADDR_READ);
12657 tw32(GRC_EEPROM_ADDR, val |
12658 (0 << EEPROM_ADDR_DEVID_SHIFT) |
12659 (addr & EEPROM_ADDR_ADDR_MASK) |
12660 EEPROM_ADDR_START |
12661 EEPROM_ADDR_WRITE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012662
Michael Chan9d57f012006-12-07 00:23:25 -080012663 for (j = 0; j < 1000; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012664 val = tr32(GRC_EEPROM_ADDR);
12665
12666 if (val & EEPROM_ADDR_COMPLETE)
12667 break;
Michael Chan9d57f012006-12-07 00:23:25 -080012668 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012669 }
12670 if (!(val & EEPROM_ADDR_COMPLETE)) {
12671 rc = -EBUSY;
12672 break;
12673 }
12674 }
12675
12676 return rc;
12677}
12678
12679/* offset and length are dword aligned */
12680static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
12681 u8 *buf)
12682{
12683 int ret = 0;
12684 u32 pagesize = tp->nvram_pagesize;
12685 u32 pagemask = pagesize - 1;
12686 u32 nvram_cmd;
12687 u8 *tmp;
12688
12689 tmp = kmalloc(pagesize, GFP_KERNEL);
12690 if (tmp == NULL)
12691 return -ENOMEM;
12692
12693 while (len) {
12694 int j;
Michael Chane6af3012005-04-21 17:12:05 -070012695 u32 phy_addr, page_off, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012696
12697 phy_addr = offset & ~pagemask;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012698
Linus Torvalds1da177e2005-04-16 15:20:36 -070012699 for (j = 0; j < pagesize; j += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012700 ret = tg3_nvram_read_be32(tp, phy_addr + j,
12701 (__be32 *) (tmp + j));
12702 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012703 break;
12704 }
12705 if (ret)
12706 break;
12707
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012708 page_off = offset & pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012709 size = pagesize;
12710 if (len < size)
12711 size = len;
12712
12713 len -= size;
12714
12715 memcpy(tmp + page_off, buf, size);
12716
12717 offset = offset + (pagesize - page_off);
12718
Michael Chane6af3012005-04-21 17:12:05 -070012719 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012720
12721 /*
12722 * Before we can erase the flash page, we need
12723 * to issue a special "write enable" command.
12724 */
12725 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12726
12727 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12728 break;
12729
12730 /* Erase the target page */
12731 tw32(NVRAM_ADDR, phy_addr);
12732
12733 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
12734 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
12735
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012736 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012737 break;
12738
12739 /* Issue another write enable to start the write. */
12740 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12741
12742 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12743 break;
12744
12745 for (j = 0; j < pagesize; j += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012746 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012747
Al Virob9fc7dc2007-12-17 22:59:57 -080012748 data = *((__be32 *) (tmp + j));
Matt Carlsona9dc5292009-02-25 14:25:30 +000012749
Al Virob9fc7dc2007-12-17 22:59:57 -080012750 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012751
12752 tw32(NVRAM_ADDR, phy_addr + j);
12753
12754 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
12755 NVRAM_CMD_WR;
12756
12757 if (j == 0)
12758 nvram_cmd |= NVRAM_CMD_FIRST;
12759 else if (j == (pagesize - 4))
12760 nvram_cmd |= NVRAM_CMD_LAST;
12761
12762 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12763 break;
12764 }
12765 if (ret)
12766 break;
12767 }
12768
12769 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12770 tg3_nvram_exec_cmd(tp, nvram_cmd);
12771
12772 kfree(tmp);
12773
12774 return ret;
12775}
12776
12777/* offset and length are dword aligned */
12778static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
12779 u8 *buf)
12780{
12781 int i, ret = 0;
12782
12783 for (i = 0; i < len; i += 4, offset += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012784 u32 page_off, phy_addr, nvram_cmd;
12785 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012786
12787 memcpy(&data, buf + i, 4);
Al Virob9fc7dc2007-12-17 22:59:57 -080012788 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012789
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012790 page_off = offset % tp->nvram_pagesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012791
Michael Chan18201802006-03-20 22:29:15 -080012792 phy_addr = tg3_nvram_phys_addr(tp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012793
12794 tw32(NVRAM_ADDR, phy_addr);
12795
12796 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
12797
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012798 if (page_off == 0 || i == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012799 nvram_cmd |= NVRAM_CMD_FIRST;
Michael Chanf6d9a252006-04-29 19:00:24 -070012800 if (page_off == (tp->nvram_pagesize - 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012801 nvram_cmd |= NVRAM_CMD_LAST;
12802
12803 if (i == (len - 4))
12804 nvram_cmd |= NVRAM_CMD_LAST;
12805
Matt Carlson321d32a2008-11-21 17:22:19 -080012806 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
Joe Perches63c3a662011-04-26 08:12:10 +000012807 !tg3_flag(tp, 5755_PLUS) &&
Michael Chan4c987482005-09-05 17:52:38 -070012808 (tp->nvram_jedecnum == JEDEC_ST) &&
12809 (nvram_cmd & NVRAM_CMD_FIRST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012810
12811 if ((ret = tg3_nvram_exec_cmd(tp,
12812 NVRAM_CMD_WREN | NVRAM_CMD_GO |
12813 NVRAM_CMD_DONE)))
12814
12815 break;
12816 }
Joe Perches63c3a662011-04-26 08:12:10 +000012817 if (!tg3_flag(tp, FLASH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012818 /* We always do complete word writes to eeprom. */
12819 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
12820 }
12821
12822 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12823 break;
12824 }
12825 return ret;
12826}
12827
12828/* offset and length are dword aligned */
12829static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
12830{
12831 int ret;
12832
Joe Perches63c3a662011-04-26 08:12:10 +000012833 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
Michael Chan314fba32005-04-21 17:07:04 -070012834 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
12835 ~GRC_LCLCTRL_GPIO_OUTPUT1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012836 udelay(40);
12837 }
12838
Joe Perches63c3a662011-04-26 08:12:10 +000012839 if (!tg3_flag(tp, NVRAM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012840 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
Matt Carlson859a5882010-04-05 10:19:28 +000012841 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012842 u32 grc_mode;
12843
Michael Chanec41c7d2006-01-17 02:40:55 -080012844 ret = tg3_nvram_lock(tp);
12845 if (ret)
12846 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012847
Michael Chane6af3012005-04-21 17:12:05 -070012848 tg3_enable_nvram_access(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000012849 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012850 tw32(NVRAM_WRITE1, 0x406);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012851
12852 grc_mode = tr32(GRC_MODE);
12853 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
12854
Joe Perches63c3a662011-04-26 08:12:10 +000012855 if (tg3_flag(tp, NVRAM_BUFFERED) || !tg3_flag(tp, FLASH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012856 ret = tg3_nvram_write_block_buffered(tp, offset, len,
12857 buf);
Matt Carlson859a5882010-04-05 10:19:28 +000012858 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012859 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
12860 buf);
12861 }
12862
12863 grc_mode = tr32(GRC_MODE);
12864 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
12865
Michael Chane6af3012005-04-21 17:12:05 -070012866 tg3_disable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012867 tg3_nvram_unlock(tp);
12868 }
12869
Joe Perches63c3a662011-04-26 08:12:10 +000012870 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
Michael Chan314fba32005-04-21 17:07:04 -070012871 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012872 udelay(40);
12873 }
12874
12875 return ret;
12876}
12877
12878struct subsys_tbl_ent {
12879 u16 subsys_vendor, subsys_devid;
12880 u32 phy_id;
12881};
12882
Matt Carlson24daf2b2010-02-17 15:17:02 +000012883static struct subsys_tbl_ent subsys_id_to_phy_id[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012884 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012885 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012886 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012887 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012888 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012889 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012890 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012891 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12892 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
12893 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012894 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012895 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012896 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012897 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12898 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
12899 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012900 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012901 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012902 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012903 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012904 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012905 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012906 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012907
12908 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012909 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012910 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012911 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012912 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012913 { TG3PCI_SUBVENDOR_ID_3COM,
12914 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
12915 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012916 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012917 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012918 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012919
12920 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012921 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012922 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012923 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012924 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012925 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012926 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012927 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012928 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012929
12930 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012931 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012932 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012933 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012934 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012935 { TG3PCI_SUBVENDOR_ID_COMPAQ,
12936 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
12937 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012938 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012939 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012940 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012941
12942 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012943 { TG3PCI_SUBVENDOR_ID_IBM,
12944 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012945};
12946
Matt Carlson24daf2b2010-02-17 15:17:02 +000012947static struct subsys_tbl_ent * __devinit tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012948{
12949 int i;
12950
12951 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
12952 if ((subsys_id_to_phy_id[i].subsys_vendor ==
12953 tp->pdev->subsystem_vendor) &&
12954 (subsys_id_to_phy_id[i].subsys_devid ==
12955 tp->pdev->subsystem_device))
12956 return &subsys_id_to_phy_id[i];
12957 }
12958 return NULL;
12959}
12960
Michael Chan7d0c41e2005-04-21 17:06:20 -070012961static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012962{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012963 u32 val;
David S. Millerf49639e2006-06-09 11:58:36 -070012964
Matt Carlson79eb6902010-02-17 15:17:03 +000012965 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012966 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12967
Gary Zambranoa85feb82007-05-05 11:52:19 -070012968 /* Assume an onboard device and WOL capable by default. */
Joe Perches63c3a662011-04-26 08:12:10 +000012969 tg3_flag_set(tp, EEPROM_WRITE_PROT);
12970 tg3_flag_set(tp, WOL_CAP);
David S. Miller72b845e2006-03-14 14:11:48 -080012971
Michael Chanb5d37722006-09-27 16:06:21 -070012972 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080012973 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012974 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
12975 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080012976 }
Matt Carlson0527ba32007-10-10 18:03:30 -070012977 val = tr32(VCPU_CFGSHDW);
12978 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Joe Perches63c3a662011-04-26 08:12:10 +000012979 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson0527ba32007-10-10 18:03:30 -070012980 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000012981 (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012982 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000012983 device_set_wakeup_enable(&tp->pdev->dev, true);
12984 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012985 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070012986 }
12987
Linus Torvalds1da177e2005-04-16 15:20:36 -070012988 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
12989 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
12990 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070012991 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012992 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012993
12994 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
12995 tp->nic_sram_data_cfg = nic_cfg;
12996
12997 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
12998 ver >>= NIC_SRAM_DATA_VER_SHIFT;
Matt Carlson6ff6f812011-05-19 12:12:54 +000012999 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13000 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13001 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070013002 (ver > 0) && (ver < 0x100))
13003 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
13004
Matt Carlsona9daf362008-05-25 23:49:44 -070013005 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
13006 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
13007
Linus Torvalds1da177e2005-04-16 15:20:36 -070013008 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
13009 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
13010 eeprom_phy_serdes = 1;
13011
13012 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
13013 if (nic_phy_id != 0) {
13014 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
13015 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
13016
13017 eeprom_phy_id = (id1 >> 16) << 10;
13018 eeprom_phy_id |= (id2 & 0xfc00) << 16;
13019 eeprom_phy_id |= (id2 & 0x03ff) << 0;
13020 } else
13021 eeprom_phy_id = 0;
13022
Michael Chan7d0c41e2005-04-21 17:06:20 -070013023 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070013024 if (eeprom_phy_serdes) {
Joe Perches63c3a662011-04-26 08:12:10 +000013025 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013026 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000013027 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013028 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070013029 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070013030
Joe Perches63c3a662011-04-26 08:12:10 +000013031 if (tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013032 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
13033 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070013034 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070013035 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
13036
13037 switch (led_cfg) {
13038 default:
13039 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
13040 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13041 break;
13042
13043 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
13044 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
13045 break;
13046
13047 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
13048 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070013049
13050 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
13051 * read on some older 5700/5701 bootcode.
13052 */
13053 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
13054 ASIC_REV_5700 ||
13055 GET_ASIC_REV(tp->pci_chip_rev_id) ==
13056 ASIC_REV_5701)
13057 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13058
Linus Torvalds1da177e2005-04-16 15:20:36 -070013059 break;
13060
13061 case SHASTA_EXT_LED_SHARED:
13062 tp->led_ctrl = LED_CTRL_MODE_SHARED;
13063 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
13064 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
13065 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
13066 LED_CTRL_MODE_PHY_2);
13067 break;
13068
13069 case SHASTA_EXT_LED_MAC:
13070 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
13071 break;
13072
13073 case SHASTA_EXT_LED_COMBO:
13074 tp->led_ctrl = LED_CTRL_MODE_COMBO;
13075 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
13076 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
13077 LED_CTRL_MODE_PHY_2);
13078 break;
13079
Stephen Hemminger855e1112008-04-16 16:37:28 -070013080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013081
13082 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13083 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
13084 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
13085 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
13086
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013087 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
13088 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080013089
Michael Chan9d26e212006-12-07 00:21:14 -080013090 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Joe Perches63c3a662011-04-26 08:12:10 +000013091 tg3_flag_set(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080013092 if ((tp->pdev->subsystem_vendor ==
13093 PCI_VENDOR_ID_ARIMA) &&
13094 (tp->pdev->subsystem_device == 0x205a ||
13095 tp->pdev->subsystem_device == 0x2063))
Joe Perches63c3a662011-04-26 08:12:10 +000013096 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080013097 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000013098 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
13099 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080013100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013101
13102 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +000013103 tg3_flag_set(tp, ENABLE_ASF);
13104 if (tg3_flag(tp, 5750_PLUS))
13105 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013106 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080013107
13108 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013109 tg3_flag(tp, 5750_PLUS))
13110 tg3_flag_set(tp, ENABLE_APE);
Matt Carlsonb2b98d42008-11-03 16:52:32 -080013111
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013112 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070013113 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
Joe Perches63c3a662011-04-26 08:12:10 +000013114 tg3_flag_clear(tp, WOL_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013115
Joe Perches63c3a662011-04-26 08:12:10 +000013116 if (tg3_flag(tp, WOL_CAP) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013117 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013118 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013119 device_set_wakeup_enable(&tp->pdev->dev, true);
13120 }
Matt Carlson0527ba32007-10-10 18:03:30 -070013121
Linus Torvalds1da177e2005-04-16 15:20:36 -070013122 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013123 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013124
13125 /* serdes signal pre-emphasis in register 0x590 set by */
13126 /* bootcode if bit 18 is set */
13127 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013128 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070013129
Joe Perches63c3a662011-04-26 08:12:10 +000013130 if ((tg3_flag(tp, 57765_PLUS) ||
13131 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
13132 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080013133 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013134 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080013135
Joe Perches63c3a662011-04-26 08:12:10 +000013136 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson8c69b1e2010-08-02 11:26:00 +000013137 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +000013138 !tg3_flag(tp, 57765_PLUS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070013139 u32 cfg3;
13140
13141 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
13142 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
Joe Perches63c3a662011-04-26 08:12:10 +000013143 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson8ed5d972007-05-07 00:25:49 -070013144 }
Matt Carlsona9daf362008-05-25 23:49:44 -070013145
Matt Carlson14417062010-02-17 15:16:59 +000013146 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
Joe Perches63c3a662011-04-26 08:12:10 +000013147 tg3_flag_set(tp, RGMII_INBAND_DISABLE);
Matt Carlsona9daf362008-05-25 23:49:44 -070013148 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000013149 tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
Matt Carlsona9daf362008-05-25 23:49:44 -070013150 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000013151 tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013152 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080013153done:
Joe Perches63c3a662011-04-26 08:12:10 +000013154 if (tg3_flag(tp, WOL_CAP))
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000013155 device_set_wakeup_enable(&tp->pdev->dev,
Joe Perches63c3a662011-04-26 08:12:10 +000013156 tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000013157 else
13158 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070013159}
13160
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013161static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
13162{
13163 int i;
13164 u32 val;
13165
13166 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
13167 tw32(OTP_CTRL, cmd);
13168
13169 /* Wait for up to 1 ms for command to execute. */
13170 for (i = 0; i < 100; i++) {
13171 val = tr32(OTP_STATUS);
13172 if (val & OTP_STATUS_CMD_DONE)
13173 break;
13174 udelay(10);
13175 }
13176
13177 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
13178}
13179
13180/* Read the gphy configuration from the OTP region of the chip. The gphy
13181 * configuration is a 32-bit value that straddles the alignment boundary.
13182 * We do two 32-bit reads and then shift and merge the results.
13183 */
13184static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
13185{
13186 u32 bhalf_otp, thalf_otp;
13187
13188 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
13189
13190 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
13191 return 0;
13192
13193 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
13194
13195 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
13196 return 0;
13197
13198 thalf_otp = tr32(OTP_READ_DATA);
13199
13200 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
13201
13202 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
13203 return 0;
13204
13205 bhalf_otp = tr32(OTP_READ_DATA);
13206
13207 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
13208}
13209
Matt Carlsone256f8a2011-03-09 16:58:24 +000013210static void __devinit tg3_phy_init_link_config(struct tg3 *tp)
13211{
13212 u32 adv = ADVERTISED_Autoneg |
13213 ADVERTISED_Pause;
13214
13215 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
13216 adv |= ADVERTISED_1000baseT_Half |
13217 ADVERTISED_1000baseT_Full;
13218
13219 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
13220 adv |= ADVERTISED_100baseT_Half |
13221 ADVERTISED_100baseT_Full |
13222 ADVERTISED_10baseT_Half |
13223 ADVERTISED_10baseT_Full |
13224 ADVERTISED_TP;
13225 else
13226 adv |= ADVERTISED_FIBRE;
13227
13228 tp->link_config.advertising = adv;
13229 tp->link_config.speed = SPEED_INVALID;
13230 tp->link_config.duplex = DUPLEX_INVALID;
13231 tp->link_config.autoneg = AUTONEG_ENABLE;
13232 tp->link_config.active_speed = SPEED_INVALID;
13233 tp->link_config.active_duplex = DUPLEX_INVALID;
13234 tp->link_config.orig_speed = SPEED_INVALID;
13235 tp->link_config.orig_duplex = DUPLEX_INVALID;
13236 tp->link_config.orig_autoneg = AUTONEG_INVALID;
13237}
13238
Michael Chan7d0c41e2005-04-21 17:06:20 -070013239static int __devinit tg3_phy_probe(struct tg3 *tp)
13240{
13241 u32 hw_phy_id_1, hw_phy_id_2;
13242 u32 hw_phy_id, hw_phy_id_masked;
13243 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013244
Matt Carlsone256f8a2011-03-09 16:58:24 +000013245 /* flow control autonegotiation is default behavior */
Joe Perches63c3a662011-04-26 08:12:10 +000013246 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsone256f8a2011-03-09 16:58:24 +000013247 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
13248
Joe Perches63c3a662011-04-26 08:12:10 +000013249 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013250 return tg3_phy_init(tp);
13251
Linus Torvalds1da177e2005-04-16 15:20:36 -070013252 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010013253 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013254 */
13255 err = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000013256 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000013257 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013258 } else {
13259 /* Now read the physical PHY_ID from the chip and verify
13260 * that it is sane. If it doesn't look good, we fall back
13261 * to either the hard-coded table based PHY_ID and failing
13262 * that the value found in the eeprom area.
13263 */
13264 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
13265 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
13266
13267 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
13268 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
13269 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
13270
Matt Carlson79eb6902010-02-17 15:17:03 +000013271 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013272 }
13273
Matt Carlson79eb6902010-02-17 15:17:03 +000013274 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013275 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000013276 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013277 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070013278 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013279 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013280 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000013281 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070013282 /* Do nothing, phy ID already set up in
13283 * tg3_get_eeprom_hw_cfg().
13284 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070013285 } else {
13286 struct subsys_tbl_ent *p;
13287
13288 /* No eeprom signature? Try the hardcoded
13289 * subsys device table.
13290 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013291 p = tg3_lookup_by_subsys(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013292 if (!p)
13293 return -ENODEV;
13294
13295 tp->phy_id = p->phy_id;
13296 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000013297 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013298 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013299 }
13300 }
13301
Matt Carlsona6b68da2010-12-06 08:28:52 +000013302 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Matt Carlson5baa5e92011-07-20 10:20:53 +000013303 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13304 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720 ||
13305 (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +000013306 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0) ||
13307 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
13308 tp->pci_chip_rev_id != CHIPREV_ID_57765_A0)))
Matt Carlson52b02d02010-10-14 10:37:41 +000013309 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
13310
Matt Carlsone256f8a2011-03-09 16:58:24 +000013311 tg3_phy_init_link_config(tp);
13312
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013313 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013314 !tg3_flag(tp, ENABLE_APE) &&
13315 !tg3_flag(tp, ENABLE_ASF)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000013316 u32 bmsr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013317
13318 tg3_readphy(tp, MII_BMSR, &bmsr);
13319 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
13320 (bmsr & BMSR_LSTATUS))
13321 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013322
Linus Torvalds1da177e2005-04-16 15:20:36 -070013323 err = tg3_phy_reset(tp);
13324 if (err)
13325 return err;
13326
Matt Carlson42b64a42011-05-19 12:12:49 +000013327 tg3_phy_set_wirespeed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013328
Michael Chan3600d912006-12-07 00:21:48 -080013329 mask = (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
13330 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
13331 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full);
13332 if (!tg3_copper_is_advertising_all(tp, mask)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000013333 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
13334 tp->link_config.flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013335
13336 tg3_writephy(tp, MII_BMCR,
13337 BMCR_ANENABLE | BMCR_ANRESTART);
13338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013339 }
13340
13341skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000013342 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013343 err = tg3_init_5401phy_dsp(tp);
13344 if (err)
13345 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013346
Linus Torvalds1da177e2005-04-16 15:20:36 -070013347 err = tg3_init_5401phy_dsp(tp);
13348 }
13349
Linus Torvalds1da177e2005-04-16 15:20:36 -070013350 return err;
13351}
13352
Matt Carlson184b8902010-04-05 10:19:25 +000013353static void __devinit tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013354{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013355 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013356 unsigned int block_end, rosize, len;
Matt Carlson535a4902011-07-20 10:20:56 +000013357 u32 vpdlen;
Matt Carlson184b8902010-04-05 10:19:25 +000013358 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013359
Matt Carlson535a4902011-07-20 10:20:56 +000013360 vpd_data = (u8 *)tg3_vpd_readblock(tp, &vpdlen);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013361 if (!vpd_data)
13362 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013363
Matt Carlson535a4902011-07-20 10:20:56 +000013364 i = pci_vpd_find_tag(vpd_data, 0, vpdlen, PCI_VPD_LRDT_RO_DATA);
Matt Carlson4181b2c2010-02-26 14:04:45 +000013365 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013366 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013367
13368 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
13369 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
13370 i += PCI_VPD_LRDT_TAG_SIZE;
13371
Matt Carlson535a4902011-07-20 10:20:56 +000013372 if (block_end > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000013373 goto out_not_found;
13374
Matt Carlson184b8902010-04-05 10:19:25 +000013375 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13376 PCI_VPD_RO_KEYWORD_MFR_ID);
13377 if (j > 0) {
13378 len = pci_vpd_info_field_size(&vpd_data[j]);
13379
13380 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13381 if (j + len > block_end || len != 4 ||
13382 memcmp(&vpd_data[j], "1028", 4))
13383 goto partno;
13384
13385 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13386 PCI_VPD_RO_KEYWORD_VENDOR0);
13387 if (j < 0)
13388 goto partno;
13389
13390 len = pci_vpd_info_field_size(&vpd_data[j]);
13391
13392 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13393 if (j + len > block_end)
13394 goto partno;
13395
13396 memcpy(tp->fw_ver, &vpd_data[j], len);
Matt Carlson535a4902011-07-20 10:20:56 +000013397 strncat(tp->fw_ver, " bc ", vpdlen - len - 1);
Matt Carlson184b8902010-04-05 10:19:25 +000013398 }
13399
13400partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000013401 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13402 PCI_VPD_RO_KEYWORD_PARTNO);
13403 if (i < 0)
13404 goto out_not_found;
13405
13406 len = pci_vpd_info_field_size(&vpd_data[i]);
13407
13408 i += PCI_VPD_INFO_FLD_HDR_SIZE;
13409 if (len > TG3_BPN_SIZE ||
Matt Carlson535a4902011-07-20 10:20:56 +000013410 (len + i) > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000013411 goto out_not_found;
13412
13413 memcpy(tp->board_part_number, &vpd_data[i], len);
13414
Linus Torvalds1da177e2005-04-16 15:20:36 -070013415out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013416 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000013417 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013418 return;
13419
13420out_no_vpd:
Matt Carlson37a949c2010-09-30 10:34:33 +000013421 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
13422 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717)
13423 strcpy(tp->board_part_number, "BCM5717");
13424 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
13425 strcpy(tp->board_part_number, "BCM5718");
13426 else
13427 goto nomatch;
13428 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
13429 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
13430 strcpy(tp->board_part_number, "BCM57780");
13431 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
13432 strcpy(tp->board_part_number, "BCM57760");
13433 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
13434 strcpy(tp->board_part_number, "BCM57790");
13435 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
13436 strcpy(tp->board_part_number, "BCM57788");
13437 else
13438 goto nomatch;
13439 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
13440 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
13441 strcpy(tp->board_part_number, "BCM57761");
13442 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
13443 strcpy(tp->board_part_number, "BCM57765");
13444 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
13445 strcpy(tp->board_part_number, "BCM57781");
13446 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
13447 strcpy(tp->board_part_number, "BCM57785");
13448 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
13449 strcpy(tp->board_part_number, "BCM57791");
13450 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13451 strcpy(tp->board_part_number, "BCM57795");
13452 else
13453 goto nomatch;
13454 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070013455 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000013456 } else {
13457nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070013458 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000013459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013460}
13461
Matt Carlson9c8a6202007-10-21 16:16:08 -070013462static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
13463{
13464 u32 val;
13465
Matt Carlsone4f34112009-02-25 14:25:00 +000013466 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013467 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013468 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013469 val != 0)
13470 return 0;
13471
13472 return 1;
13473}
13474
Matt Carlsonacd9c112009-02-25 14:26:33 +000013475static void __devinit tg3_read_bc_ver(struct tg3 *tp)
13476{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013477 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000013478 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013479 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013480
13481 if (tg3_nvram_read(tp, 0xc, &offset) ||
13482 tg3_nvram_read(tp, 0x4, &start))
13483 return;
13484
13485 offset = tg3_nvram_logical_addr(tp, offset);
13486
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013487 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013488 return;
13489
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013490 if ((val & 0xfc000000) == 0x0c000000) {
13491 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013492 return;
13493
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013494 if (val == 0)
13495 newver = true;
13496 }
13497
Matt Carlson75f99362010-04-05 10:19:24 +000013498 dst_off = strlen(tp->fw_ver);
13499
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013500 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000013501 if (TG3_VER_SIZE - dst_off < 16 ||
13502 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013503 return;
13504
13505 offset = offset + ver_offset - start;
13506 for (i = 0; i < 16; i += 4) {
13507 __be32 v;
13508 if (tg3_nvram_read_be32(tp, offset + i, &v))
13509 return;
13510
Matt Carlson75f99362010-04-05 10:19:24 +000013511 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013512 }
13513 } else {
13514 u32 major, minor;
13515
13516 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
13517 return;
13518
13519 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
13520 TG3_NVM_BCVER_MAJSFT;
13521 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000013522 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
13523 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013524 }
13525}
13526
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013527static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
13528{
13529 u32 val, major, minor;
13530
13531 /* Use native endian representation */
13532 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
13533 return;
13534
13535 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
13536 TG3_NVM_HWSB_CFG1_MAJSFT;
13537 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
13538 TG3_NVM_HWSB_CFG1_MINSFT;
13539
13540 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
13541}
13542
Matt Carlsondfe00d72008-11-21 17:19:41 -080013543static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
13544{
13545 u32 offset, major, minor, build;
13546
Matt Carlson75f99362010-04-05 10:19:24 +000013547 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013548
13549 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
13550 return;
13551
13552 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
13553 case TG3_EEPROM_SB_REVISION_0:
13554 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
13555 break;
13556 case TG3_EEPROM_SB_REVISION_2:
13557 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
13558 break;
13559 case TG3_EEPROM_SB_REVISION_3:
13560 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
13561 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000013562 case TG3_EEPROM_SB_REVISION_4:
13563 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
13564 break;
13565 case TG3_EEPROM_SB_REVISION_5:
13566 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
13567 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000013568 case TG3_EEPROM_SB_REVISION_6:
13569 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
13570 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013571 default:
13572 return;
13573 }
13574
Matt Carlsone4f34112009-02-25 14:25:00 +000013575 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080013576 return;
13577
13578 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
13579 TG3_EEPROM_SB_EDH_BLD_SHFT;
13580 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
13581 TG3_EEPROM_SB_EDH_MAJ_SHFT;
13582 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
13583
13584 if (minor > 99 || build > 26)
13585 return;
13586
Matt Carlson75f99362010-04-05 10:19:24 +000013587 offset = strlen(tp->fw_ver);
13588 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
13589 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013590
13591 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000013592 offset = strlen(tp->fw_ver);
13593 if (offset < TG3_VER_SIZE - 1)
13594 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013595 }
13596}
13597
Matt Carlsonacd9c112009-02-25 14:26:33 +000013598static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080013599{
13600 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013601 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070013602
13603 for (offset = TG3_NVM_DIR_START;
13604 offset < TG3_NVM_DIR_END;
13605 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000013606 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013607 return;
13608
13609 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
13610 break;
13611 }
13612
13613 if (offset == TG3_NVM_DIR_END)
13614 return;
13615
Joe Perches63c3a662011-04-26 08:12:10 +000013616 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013617 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000013618 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013619 return;
13620
Matt Carlsone4f34112009-02-25 14:25:00 +000013621 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013622 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013623 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013624 return;
13625
13626 offset += val - start;
13627
Matt Carlsonacd9c112009-02-25 14:26:33 +000013628 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013629
Matt Carlsonacd9c112009-02-25 14:26:33 +000013630 tp->fw_ver[vlen++] = ',';
13631 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070013632
13633 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000013634 __be32 v;
13635 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013636 return;
13637
Al Virob9fc7dc2007-12-17 22:59:57 -080013638 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013639
Matt Carlsonacd9c112009-02-25 14:26:33 +000013640 if (vlen > TG3_VER_SIZE - sizeof(v)) {
13641 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013642 break;
13643 }
13644
Matt Carlsonacd9c112009-02-25 14:26:33 +000013645 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
13646 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013647 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000013648}
13649
Matt Carlson7fd76442009-02-25 14:27:20 +000013650static void __devinit tg3_read_dash_ver(struct tg3 *tp)
13651{
13652 int vlen;
13653 u32 apedata;
Matt Carlsonecc79642010-08-02 11:26:01 +000013654 char *fwtype;
Matt Carlson7fd76442009-02-25 14:27:20 +000013655
Joe Perches63c3a662011-04-26 08:12:10 +000013656 if (!tg3_flag(tp, ENABLE_APE) || !tg3_flag(tp, ENABLE_ASF))
Matt Carlson7fd76442009-02-25 14:27:20 +000013657 return;
13658
13659 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
13660 if (apedata != APE_SEG_SIG_MAGIC)
13661 return;
13662
13663 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
13664 if (!(apedata & APE_FW_STATUS_READY))
13665 return;
13666
13667 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
13668
Matt Carlsondc6d0742010-09-15 08:59:55 +000013669 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI) {
Joe Perches63c3a662011-04-26 08:12:10 +000013670 tg3_flag_set(tp, APE_HAS_NCSI);
Matt Carlsonecc79642010-08-02 11:26:01 +000013671 fwtype = "NCSI";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013672 } else {
Matt Carlsonecc79642010-08-02 11:26:01 +000013673 fwtype = "DASH";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013674 }
Matt Carlsonecc79642010-08-02 11:26:01 +000013675
Matt Carlson7fd76442009-02-25 14:27:20 +000013676 vlen = strlen(tp->fw_ver);
13677
Matt Carlsonecc79642010-08-02 11:26:01 +000013678 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
13679 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000013680 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
13681 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
13682 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
13683 (apedata & APE_FW_VERSION_BLDMSK));
13684}
13685
Matt Carlsonacd9c112009-02-25 14:26:33 +000013686static void __devinit tg3_read_fw_ver(struct tg3 *tp)
13687{
13688 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000013689 bool vpd_vers = false;
13690
13691 if (tp->fw_ver[0] != 0)
13692 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013693
Joe Perches63c3a662011-04-26 08:12:10 +000013694 if (tg3_flag(tp, NO_NVRAM)) {
Matt Carlson75f99362010-04-05 10:19:24 +000013695 strcat(tp->fw_ver, "sb");
Matt Carlsondf259d82009-04-20 06:57:14 +000013696 return;
13697 }
13698
Matt Carlsonacd9c112009-02-25 14:26:33 +000013699 if (tg3_nvram_read(tp, 0, &val))
13700 return;
13701
13702 if (val == TG3_EEPROM_MAGIC)
13703 tg3_read_bc_ver(tp);
13704 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
13705 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013706 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
13707 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013708 else
13709 return;
13710
Matt Carlsonc9cab242011-07-13 09:27:27 +000013711 if (vpd_vers)
Matt Carlson75f99362010-04-05 10:19:24 +000013712 goto done;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013713
Matt Carlsonc9cab242011-07-13 09:27:27 +000013714 if (tg3_flag(tp, ENABLE_APE)) {
13715 if (tg3_flag(tp, ENABLE_ASF))
13716 tg3_read_dash_ver(tp);
13717 } else if (tg3_flag(tp, ENABLE_ASF)) {
13718 tg3_read_mgmtfw_ver(tp);
13719 }
Matt Carlson9c8a6202007-10-21 16:16:08 -070013720
Matt Carlson75f99362010-04-05 10:19:24 +000013721done:
Matt Carlson9c8a6202007-10-21 16:16:08 -070013722 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080013723}
13724
Michael Chan7544b092007-05-05 13:08:32 -070013725static struct pci_dev * __devinit tg3_find_peer(struct tg3 *);
13726
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013727static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
13728{
Joe Perches63c3a662011-04-26 08:12:10 +000013729 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013730 return TG3_RX_RET_MAX_SIZE_5717;
Joe Perches63c3a662011-04-26 08:12:10 +000013731 else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013732 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013733 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000013734 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013735}
13736
Matt Carlson41434702011-03-09 16:58:22 +000013737static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080013738 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
13739 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
13740 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
13741 { },
13742};
13743
Linus Torvalds1da177e2005-04-16 15:20:36 -070013744static int __devinit tg3_get_invariants(struct tg3 *tp)
13745{
Linus Torvalds1da177e2005-04-16 15:20:36 -070013746 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013747 u32 pci_state_reg, grc_misc_cfg;
13748 u32 val;
13749 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013750 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013751
Linus Torvalds1da177e2005-04-16 15:20:36 -070013752 /* Force memory write invalidate off. If we leave it on,
13753 * then on 5700_BX chips we have to enable a workaround.
13754 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
13755 * to match the cacheline size. The Broadcom driver have this
13756 * workaround but turns MWI off all the times so never uses
13757 * it. This seems to suggest that the workaround is insufficient.
13758 */
13759 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13760 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
13761 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13762
Matt Carlson16821282011-07-13 09:27:28 +000013763 /* Important! -- Make sure register accesses are byteswapped
13764 * correctly. Also, for those chips that require it, make
13765 * sure that indirect register accesses are enabled before
13766 * the first operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013767 */
13768 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13769 &misc_ctrl_reg);
Matt Carlson16821282011-07-13 09:27:28 +000013770 tp->misc_host_ctrl |= (misc_ctrl_reg &
13771 MISC_HOST_CTRL_CHIPREV);
13772 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13773 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013774
13775 tp->pci_chip_rev_id = (misc_ctrl_reg >>
13776 MISC_HOST_CTRL_CHIPREV_SHIFT);
Matt Carlson795d01c2007-10-07 23:28:17 -070013777 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
13778 u32 prod_id_asic_rev;
13779
Matt Carlson5001e2f2009-11-13 13:03:51 +000013780 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
13781 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013782 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
13783 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013784 pci_read_config_dword(tp->pdev,
13785 TG3PCI_GEN2_PRODID_ASICREV,
13786 &prod_id_asic_rev);
Matt Carlsonb703df62009-12-03 08:36:21 +000013787 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
13788 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
13789 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
13790 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
13791 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
13792 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13793 pci_read_config_dword(tp->pdev,
13794 TG3PCI_GEN15_PRODID_ASICREV,
13795 &prod_id_asic_rev);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013796 else
13797 pci_read_config_dword(tp->pdev, TG3PCI_PRODID_ASICREV,
13798 &prod_id_asic_rev);
13799
Matt Carlson321d32a2008-11-21 17:22:19 -080013800 tp->pci_chip_rev_id = prod_id_asic_rev;
Matt Carlson795d01c2007-10-07 23:28:17 -070013801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013802
Michael Chanff645be2005-04-21 17:09:53 -070013803 /* Wrong chip ID in 5752 A0. This code can be removed later
13804 * as A0 is not in production.
13805 */
13806 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
13807 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
13808
Michael Chan68929142005-08-09 20:17:14 -070013809 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
13810 * we need to disable memory and use config. cycles
13811 * only to access all registers. The 5702/03 chips
13812 * can mistakenly decode the special cycles from the
13813 * ICH chipsets as memory write cycles, causing corruption
13814 * of register and memory space. Only certain ICH bridges
13815 * will drive special cycles with non-zero data during the
13816 * address phase which can fall within the 5703's address
13817 * range. This is not an ICH bug as the PCI spec allows
13818 * non-zero address during special cycles. However, only
13819 * these ICH bridges are known to drive non-zero addresses
13820 * during special cycles.
13821 *
13822 * Since special cycles do not cross PCI bridges, we only
13823 * enable this workaround if the 5703 is on the secondary
13824 * bus of these ICH bridges.
13825 */
13826 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
13827 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
13828 static struct tg3_dev_id {
13829 u32 vendor;
13830 u32 device;
13831 u32 rev;
13832 } ich_chipsets[] = {
13833 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
13834 PCI_ANY_ID },
13835 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
13836 PCI_ANY_ID },
13837 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
13838 0xa },
13839 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
13840 PCI_ANY_ID },
13841 { },
13842 };
13843 struct tg3_dev_id *pci_id = &ich_chipsets[0];
13844 struct pci_dev *bridge = NULL;
13845
13846 while (pci_id->vendor != 0) {
13847 bridge = pci_get_device(pci_id->vendor, pci_id->device,
13848 bridge);
13849 if (!bridge) {
13850 pci_id++;
13851 continue;
13852 }
13853 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070013854 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070013855 continue;
13856 }
13857 if (bridge->subordinate &&
13858 (bridge->subordinate->number ==
13859 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013860 tg3_flag_set(tp, ICH_WORKAROUND);
Michael Chan68929142005-08-09 20:17:14 -070013861 pci_dev_put(bridge);
13862 break;
13863 }
13864 }
13865 }
13866
Matt Carlson6ff6f812011-05-19 12:12:54 +000013867 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Matt Carlson41588ba2008-04-19 18:12:33 -070013868 static struct tg3_dev_id {
13869 u32 vendor;
13870 u32 device;
13871 } bridge_chipsets[] = {
13872 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
13873 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
13874 { },
13875 };
13876 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
13877 struct pci_dev *bridge = NULL;
13878
13879 while (pci_id->vendor != 0) {
13880 bridge = pci_get_device(pci_id->vendor,
13881 pci_id->device,
13882 bridge);
13883 if (!bridge) {
13884 pci_id++;
13885 continue;
13886 }
13887 if (bridge->subordinate &&
13888 (bridge->subordinate->number <=
13889 tp->pdev->bus->number) &&
13890 (bridge->subordinate->subordinate >=
13891 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013892 tg3_flag_set(tp, 5701_DMA_BUG);
Matt Carlson41588ba2008-04-19 18:12:33 -070013893 pci_dev_put(bridge);
13894 break;
13895 }
13896 }
13897 }
13898
Michael Chan4a29cc22006-03-19 13:21:12 -080013899 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
13900 * DMA addresses > 40-bit. This bridge may have other additional
13901 * 57xx devices behind it in some 4-port NIC designs for example.
13902 * Any tg3 device found behind the bridge will also need the 40-bit
13903 * DMA workaround.
13904 */
Michael Chana4e2b342005-10-26 15:46:52 -070013905 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
13906 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Joe Perches63c3a662011-04-26 08:12:10 +000013907 tg3_flag_set(tp, 5780_CLASS);
13908 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4cf78e42005-07-25 12:29:19 -070013909 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a5882010-04-05 10:19:28 +000013910 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080013911 struct pci_dev *bridge = NULL;
13912
13913 do {
13914 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
13915 PCI_DEVICE_ID_SERVERWORKS_EPB,
13916 bridge);
13917 if (bridge && bridge->subordinate &&
13918 (bridge->subordinate->number <=
13919 tp->pdev->bus->number) &&
13920 (bridge->subordinate->subordinate >=
13921 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013922 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4a29cc22006-03-19 13:21:12 -080013923 pci_dev_put(bridge);
13924 break;
13925 }
13926 } while (bridge);
13927 }
Michael Chan4cf78e42005-07-25 12:29:19 -070013928
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013929 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Matt Carlson3a1e19d2011-07-13 09:27:32 +000013930 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)
Michael Chan7544b092007-05-05 13:08:32 -070013931 tp->pdev_peer = tg3_find_peer(tp);
13932
Matt Carlsonc885e822010-08-02 11:25:57 +000013933 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013934 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13935 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Joe Perches63c3a662011-04-26 08:12:10 +000013936 tg3_flag_set(tp, 5717_PLUS);
Matt Carlson0a58d662011-04-05 14:22:45 +000013937
13938 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013939 tg3_flag(tp, 5717_PLUS))
13940 tg3_flag_set(tp, 57765_PLUS);
Matt Carlsonc885e822010-08-02 11:25:57 +000013941
Matt Carlson321d32a2008-11-21 17:22:19 -080013942 /* Intentionally exclude ASIC_REV_5906 */
13943 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Michael Chand9ab5ad2006-03-20 22:27:35 -080013944 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013945 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013946 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013947 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013948 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013949 tg3_flag(tp, 57765_PLUS))
13950 tg3_flag_set(tp, 5755_PLUS);
Matt Carlson321d32a2008-11-21 17:22:19 -080013951
13952 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13953 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Michael Chanb5d37722006-09-27 16:06:21 -070013954 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013955 tg3_flag(tp, 5755_PLUS) ||
13956 tg3_flag(tp, 5780_CLASS))
13957 tg3_flag_set(tp, 5750_PLUS);
John W. Linville6708e5c2005-04-21 17:00:52 -070013958
Matt Carlson6ff6f812011-05-19 12:12:54 +000013959 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013960 tg3_flag(tp, 5750_PLUS))
13961 tg3_flag_set(tp, 5705_PLUS);
John W. Linville1b440c562005-04-21 17:03:18 -070013962
Matt Carlson507399f2009-11-13 13:03:37 +000013963 /* Determine TSO capabilities */
Matt Carlsona0512942011-07-27 14:20:54 +000013964 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0)
Matt Carlson4d163b72011-01-25 15:58:48 +000013965 ; /* Do nothing. HW bug. */
Joe Perches63c3a662011-04-26 08:12:10 +000013966 else if (tg3_flag(tp, 57765_PLUS))
13967 tg3_flag_set(tp, HW_TSO_3);
13968 else if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000013969 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000013970 tg3_flag_set(tp, HW_TSO_2);
13971 else if (tg3_flag(tp, 5750_PLUS)) {
13972 tg3_flag_set(tp, HW_TSO_1);
13973 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013974 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
13975 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
Joe Perches63c3a662011-04-26 08:12:10 +000013976 tg3_flag_clear(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013977 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13978 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13979 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000013980 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013981 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
13982 tp->fw_needed = FIRMWARE_TG3TSO5;
13983 else
13984 tp->fw_needed = FIRMWARE_TG3TSO;
13985 }
13986
Matt Carlsondabc5c62011-05-19 12:12:52 +000013987 /* Selectively allow TSO based on operating conditions */
Matt Carlson6ff6f812011-05-19 12:12:54 +000013988 if (tg3_flag(tp, HW_TSO_1) ||
13989 tg3_flag(tp, HW_TSO_2) ||
13990 tg3_flag(tp, HW_TSO_3) ||
Matt Carlsondabc5c62011-05-19 12:12:52 +000013991 (tp->fw_needed && !tg3_flag(tp, ENABLE_ASF)))
13992 tg3_flag_set(tp, TSO_CAPABLE);
13993 else {
13994 tg3_flag_clear(tp, TSO_CAPABLE);
13995 tg3_flag_clear(tp, TSO_BUG);
13996 tp->fw_needed = NULL;
13997 }
13998
13999 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
14000 tp->fw_needed = FIRMWARE_TG3;
14001
Matt Carlson507399f2009-11-13 13:03:37 +000014002 tp->irq_max = 1;
14003
Joe Perches63c3a662011-04-26 08:12:10 +000014004 if (tg3_flag(tp, 5750_PLUS)) {
14005 tg3_flag_set(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070014006 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
14007 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
14008 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
14009 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
14010 tp->pdev_peer == tp->pdev))
Joe Perches63c3a662011-04-26 08:12:10 +000014011 tg3_flag_clear(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070014012
Joe Perches63c3a662011-04-26 08:12:10 +000014013 if (tg3_flag(tp, 5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070014014 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000014015 tg3_flag_set(tp, 1SHOT_MSI);
Michael Chan52c0fd82006-06-29 20:15:54 -070014016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014017
Joe Perches63c3a662011-04-26 08:12:10 +000014018 if (tg3_flag(tp, 57765_PLUS)) {
14019 tg3_flag_set(tp, SUPPORT_MSIX);
Matt Carlson507399f2009-11-13 13:03:37 +000014020 tp->irq_max = TG3_IRQ_MAX_VECS;
14021 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014022 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000014023
Matt Carlson2ffcc982011-05-19 12:12:44 +000014024 if (tg3_flag(tp, 5755_PLUS))
Joe Perches63c3a662011-04-26 08:12:10 +000014025 tg3_flag_set(tp, SHORT_DMA_BUG);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014026
Matt Carlsone31aa982011-07-27 14:20:53 +000014027 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
14028 tg3_flag_set(tp, 4K_FIFO_LIMIT);
14029
Joe Perches63c3a662011-04-26 08:12:10 +000014030 if (tg3_flag(tp, 5717_PLUS))
14031 tg3_flag_set(tp, LRG_PROD_RING_CAP);
Matt Carlsonde9f5232011-04-05 14:22:43 +000014032
Joe Perches63c3a662011-04-26 08:12:10 +000014033 if (tg3_flag(tp, 57765_PLUS) &&
Matt Carlsona0512942011-07-27 14:20:54 +000014034 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0)
Joe Perches63c3a662011-04-26 08:12:10 +000014035 tg3_flag_set(tp, USE_JUMBO_BDFLAG);
Matt Carlsonb703df62009-12-03 08:36:21 +000014036
Joe Perches63c3a662011-04-26 08:12:10 +000014037 if (!tg3_flag(tp, 5705_PLUS) ||
14038 tg3_flag(tp, 5780_CLASS) ||
14039 tg3_flag(tp, USE_JUMBO_BDFLAG))
14040 tg3_flag_set(tp, JUMBO_CAPABLE);
Michael Chan0f893dc2005-07-25 12:30:38 -070014041
Matt Carlson52f44902008-11-21 17:17:04 -080014042 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14043 &pci_state_reg);
14044
Jon Mason708ebb32011-06-27 12:56:50 +000014045 if (pci_is_pcie(tp->pdev)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014046 u16 lnkctl;
14047
Joe Perches63c3a662011-04-26 08:12:10 +000014048 tg3_flag_set(tp, PCI_EXPRESS);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080014049
Matt Carlsoncf790032010-11-24 08:31:48 +000014050 tp->pcie_readrq = 4096;
Matt Carlsond78b59f2011-04-05 14:22:46 +000014051 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
14052 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Matt Carlsonb4495ed2011-01-25 15:58:47 +000014053 tp->pcie_readrq = 2048;
Matt Carlsoncf790032010-11-24 08:31:48 +000014054
14055 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080014056
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014057 pci_read_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +000014058 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014059 &lnkctl);
14060 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
Matt Carlson7196cd62011-05-19 16:02:44 +000014061 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
14062 ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000014063 tg3_flag_clear(tp, HW_TSO_2);
Matt Carlsondabc5c62011-05-19 12:12:52 +000014064 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson7196cd62011-05-19 16:02:44 +000014065 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014066 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014067 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000014068 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
14069 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Joe Perches63c3a662011-04-26 08:12:10 +000014070 tg3_flag_set(tp, CLKREQ_BUG);
Matt Carlson614b05902010-01-20 16:58:02 +000014071 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000014072 tg3_flag_set(tp, L1PLLPD_EN);
Michael Chanc7835a72006-11-15 21:14:42 -080014073 }
Matt Carlson52f44902008-11-21 17:17:04 -080014074 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Jon Mason708ebb32011-06-27 12:56:50 +000014075 /* BCM5785 devices are effectively PCIe devices, and should
14076 * follow PCIe codepaths, but do not have a PCIe capabilities
14077 * section.
Matt Carlson93a700a2011-08-31 11:44:54 +000014078 */
Joe Perches63c3a662011-04-26 08:12:10 +000014079 tg3_flag_set(tp, PCI_EXPRESS);
14080 } else if (!tg3_flag(tp, 5705_PLUS) ||
14081 tg3_flag(tp, 5780_CLASS)) {
Matt Carlson52f44902008-11-21 17:17:04 -080014082 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
14083 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000014084 dev_err(&tp->pdev->dev,
14085 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080014086 return -EIO;
14087 }
14088
14089 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
Joe Perches63c3a662011-04-26 08:12:10 +000014090 tg3_flag_set(tp, PCIX_MODE);
Matt Carlson52f44902008-11-21 17:17:04 -080014091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014092
Michael Chan399de502005-10-03 14:02:39 -070014093 /* If we have an AMD 762 or VIA K8T800 chipset, write
14094 * reordering to the mailbox registers done by the host
14095 * controller can cause major troubles. We read back from
14096 * every mailbox register write to force the writes to be
14097 * posted to the chip in order.
14098 */
Matt Carlson41434702011-03-09 16:58:22 +000014099 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Joe Perches63c3a662011-04-26 08:12:10 +000014100 !tg3_flag(tp, PCI_EXPRESS))
14101 tg3_flag_set(tp, MBOX_WRITE_REORDER);
Michael Chan399de502005-10-03 14:02:39 -070014102
Matt Carlson69fc4052008-12-21 20:19:57 -080014103 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
14104 &tp->pci_cacheline_sz);
14105 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
14106 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014107 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14108 tp->pci_lat_timer < 64) {
14109 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080014110 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
14111 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014112 }
14113
Matt Carlson16821282011-07-13 09:27:28 +000014114 /* Important! -- It is critical that the PCI-X hw workaround
14115 * situation is decided before the first MMIO register access.
14116 */
Matt Carlson52f44902008-11-21 17:17:04 -080014117 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
14118 /* 5700 BX chips need to have their TX producer index
14119 * mailboxes written twice to workaround a bug.
14120 */
Joe Perches63c3a662011-04-26 08:12:10 +000014121 tg3_flag_set(tp, TXD_MBOX_HWBUG);
Matt Carlson9974a352007-10-07 23:27:28 -070014122
Matt Carlson52f44902008-11-21 17:17:04 -080014123 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014124 *
14125 * The workaround is to use indirect register accesses
14126 * for all chip writes not to mailbox registers.
14127 */
Joe Perches63c3a662011-04-26 08:12:10 +000014128 if (tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014129 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014130
Joe Perches63c3a662011-04-26 08:12:10 +000014131 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014132
14133 /* The chip can have it's power management PCI config
14134 * space registers clobbered due to this bug.
14135 * So explicitly force the chip into D0 here.
14136 */
Matt Carlson9974a352007-10-07 23:27:28 -070014137 pci_read_config_dword(tp->pdev,
14138 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014139 &pm_reg);
14140 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
14141 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070014142 pci_write_config_dword(tp->pdev,
14143 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014144 pm_reg);
14145
14146 /* Also, force SERR#/PERR# in PCI command. */
14147 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
14148 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
14149 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
14150 }
14151 }
14152
Linus Torvalds1da177e2005-04-16 15:20:36 -070014153 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000014154 tg3_flag_set(tp, PCI_HIGH_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014155 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000014156 tg3_flag_set(tp, PCI_32BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014157
14158 /* Chip-specific fixup from Broadcom driver */
14159 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
14160 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
14161 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
14162 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
14163 }
14164
Michael Chan1ee582d2005-08-09 20:16:46 -070014165 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070014166 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070014167 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070014168 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070014169 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070014170 tp->write32_tx_mbox = tg3_write32;
14171 tp->write32_rx_mbox = tg3_write32;
14172
14173 /* Various workaround register access methods */
Joe Perches63c3a662011-04-26 08:12:10 +000014174 if (tg3_flag(tp, PCIX_TARGET_HWBUG))
Michael Chan1ee582d2005-08-09 20:16:46 -070014175 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070014176 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014177 (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson98efd8a2007-05-05 12:47:25 -070014178 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
14179 /*
14180 * Back to back register writes can cause problems on these
14181 * chips, the workaround is to read back all reg writes
14182 * except those to mailbox regs.
14183 *
14184 * See tg3_write_indirect_reg32().
14185 */
Michael Chan1ee582d2005-08-09 20:16:46 -070014186 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070014187 }
14188
Joe Perches63c3a662011-04-26 08:12:10 +000014189 if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
Michael Chan1ee582d2005-08-09 20:16:46 -070014190 tp->write32_tx_mbox = tg3_write32_tx_mbox;
Joe Perches63c3a662011-04-26 08:12:10 +000014191 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Michael Chan1ee582d2005-08-09 20:16:46 -070014192 tp->write32_rx_mbox = tg3_write_flush_reg32;
14193 }
Michael Chan20094932005-08-09 20:16:32 -070014194
Joe Perches63c3a662011-04-26 08:12:10 +000014195 if (tg3_flag(tp, ICH_WORKAROUND)) {
Michael Chan68929142005-08-09 20:17:14 -070014196 tp->read32 = tg3_read_indirect_reg32;
14197 tp->write32 = tg3_write_indirect_reg32;
14198 tp->read32_mbox = tg3_read_indirect_mbox;
14199 tp->write32_mbox = tg3_write_indirect_mbox;
14200 tp->write32_tx_mbox = tg3_write_indirect_mbox;
14201 tp->write32_rx_mbox = tg3_write_indirect_mbox;
14202
14203 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070014204 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070014205
14206 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
14207 pci_cmd &= ~PCI_COMMAND_MEMORY;
14208 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
14209 }
Michael Chanb5d37722006-09-27 16:06:21 -070014210 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
14211 tp->read32_mbox = tg3_read32_mbox_5906;
14212 tp->write32_mbox = tg3_write32_mbox_5906;
14213 tp->write32_tx_mbox = tg3_write32_mbox_5906;
14214 tp->write32_rx_mbox = tg3_write32_mbox_5906;
14215 }
Michael Chan68929142005-08-09 20:17:14 -070014216
Michael Chanbbadf502006-04-06 21:46:34 -070014217 if (tp->write32 == tg3_write_indirect_reg32 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014218 (tg3_flag(tp, PCIX_MODE) &&
Michael Chanbbadf502006-04-06 21:46:34 -070014219 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070014220 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Joe Perches63c3a662011-04-26 08:12:10 +000014221 tg3_flag_set(tp, SRAM_USE_CONFIG);
Michael Chanbbadf502006-04-06 21:46:34 -070014222
Matt Carlson16821282011-07-13 09:27:28 +000014223 /* The memory arbiter has to be enabled in order for SRAM accesses
14224 * to succeed. Normally on powerup the tg3 chip firmware will make
14225 * sure it is enabled, but other entities such as system netboot
14226 * code might disable it.
14227 */
14228 val = tr32(MEMARB_MODE);
14229 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
14230
Matt Carlson69f11c92011-07-13 09:27:30 +000014231 if (tg3_flag(tp, PCIX_MODE)) {
14232 pci_read_config_dword(tp->pdev,
14233 tp->pcix_cap + PCI_X_STATUS, &val);
14234 tp->pci_fn = val & 0x7;
14235 } else {
14236 tp->pci_fn = PCI_FUNC(tp->pdev->devfn) & 3;
14237 }
14238
Michael Chan7d0c41e2005-04-21 17:06:20 -070014239 /* Get eeprom hw config before calling tg3_set_power_state().
Joe Perches63c3a662011-04-26 08:12:10 +000014240 * In particular, the TG3_FLAG_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070014241 * determined before calling tg3_set_power_state() so that
14242 * we know whether or not to switch out of Vaux power.
14243 * When the flag is set, it means that GPIO1 is used for eeprom
14244 * write protect and also implies that it is a LOM where GPIOs
14245 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040014246 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070014247 tg3_get_eeprom_hw_cfg(tp);
14248
Joe Perches63c3a662011-04-26 08:12:10 +000014249 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070014250 /* Allow reads and writes to the
14251 * APE register and memory space.
14252 */
14253 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc2010-06-05 17:24:30 +000014254 PCISTATE_ALLOW_APE_SHMEM_WR |
14255 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070014256 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
14257 pci_state_reg);
Matt Carlsonc9cab242011-07-13 09:27:27 +000014258
14259 tg3_ape_lock_init(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070014260 }
14261
Matt Carlson9936bcf2007-10-10 18:03:07 -070014262 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson57e69832008-05-25 23:48:31 -070014263 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014264 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014265 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014266 tg3_flag(tp, 57765_PLUS))
14267 tg3_flag_set(tp, CPMU_PRESENT);
Matt Carlsond30cdd22007-10-07 23:28:35 -070014268
Matt Carlson16821282011-07-13 09:27:28 +000014269 /* Set up tp->grc_local_ctrl before calling
14270 * tg3_pwrsrc_switch_to_vmain(). GPIO1 driven high
14271 * will bring 5700's external PHY out of reset.
Michael Chan314fba32005-04-21 17:07:04 -070014272 * It is also used as eeprom write protect on LOMs.
14273 */
14274 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
Matt Carlson6ff6f812011-05-19 12:12:54 +000014275 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014276 tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan314fba32005-04-21 17:07:04 -070014277 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
14278 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070014279 /* Unused GPIO3 must be driven as output on 5752 because there
14280 * are no pull-up resistors on unused GPIO pins.
14281 */
14282 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
14283 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070014284
Matt Carlson321d32a2008-11-21 17:22:19 -080014285 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsoncb4ed1f2010-01-20 16:58:09 +000014286 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
14287 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Michael Chanaf36e6b2006-03-23 01:28:06 -080014288 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
14289
Matt Carlson8d519ab2009-04-20 06:58:01 +000014290 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
14291 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070014292 /* Turn off the debug UART. */
14293 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014294 if (tg3_flag(tp, IS_NIC))
Matt Carlson5f0c4a32008-06-09 15:41:12 -070014295 /* Keep VMain power. */
14296 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
14297 GRC_LCLCTRL_GPIO_OUTPUT0;
14298 }
14299
Matt Carlson16821282011-07-13 09:27:28 +000014300 /* Switch out of Vaux if it is a NIC */
14301 tg3_pwrsrc_switch_to_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014302
Linus Torvalds1da177e2005-04-16 15:20:36 -070014303 /* Derive initial jumbo mode from MTU assigned in
14304 * ether_setup() via the alloc_etherdev() call
14305 */
Joe Perches63c3a662011-04-26 08:12:10 +000014306 if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
14307 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014308
14309 /* Determine WakeOnLan speed to use. */
14310 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14311 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
14312 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
14313 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
Joe Perches63c3a662011-04-26 08:12:10 +000014314 tg3_flag_clear(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014315 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000014316 tg3_flag_set(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014317 }
14318
Matt Carlson7f97a4b2009-08-25 10:10:03 +000014319 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014320 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000014321
Linus Torvalds1da177e2005-04-16 15:20:36 -070014322 /* A few boards don't want Ethernet@WireSpeed phy feature */
Matt Carlson6ff6f812011-05-19 12:12:54 +000014323 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14324 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070014325 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070014326 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014327 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
14328 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
14329 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014330
14331 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
14332 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014333 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014334 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014335 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014336
Joe Perches63c3a662011-04-26 08:12:10 +000014337 if (tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014338 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080014339 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014340 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014341 !tg3_flag(tp, 57765_PLUS)) {
Michael Chanc424cb22006-04-29 18:56:34 -070014342 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070014343 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070014344 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
14345 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080014346 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
14347 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014348 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080014349 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014350 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080014351 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014352 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070014353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014354
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014355 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
14356 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
14357 tp->phy_otp = tg3_read_otp_phycfg(tp);
14358 if (tp->phy_otp == 0)
14359 tp->phy_otp = TG3_OTP_DEFAULT;
14360 }
14361
Joe Perches63c3a662011-04-26 08:12:10 +000014362 if (tg3_flag(tp, CPMU_PRESENT))
Matt Carlson8ef21422008-05-02 16:47:53 -070014363 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
14364 else
14365 tp->mi_mode = MAC_MI_MODE_BASE;
14366
Linus Torvalds1da177e2005-04-16 15:20:36 -070014367 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014368 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
14369 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
14370 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
14371
Matt Carlson4d958472011-04-20 07:57:35 +000014372 /* Set these bits to enable statistics workaround. */
14373 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14374 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
14375 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0) {
14376 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
14377 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
14378 }
14379
Matt Carlson321d32a2008-11-21 17:22:19 -080014380 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
14381 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Joe Perches63c3a662011-04-26 08:12:10 +000014382 tg3_flag_set(tp, USE_PHYLIB);
Matt Carlson57e69832008-05-25 23:48:31 -070014383
Matt Carlson158d7ab2008-05-29 01:37:54 -070014384 err = tg3_mdio_init(tp);
14385 if (err)
14386 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014387
14388 /* Initialize data/descriptor byte/word swapping. */
14389 val = tr32(GRC_MODE);
Matt Carlsonf2096f92011-04-05 14:22:48 +000014390 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
14391 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
14392 GRC_MODE_WORD_SWAP_B2HRX_DATA |
14393 GRC_MODE_B2HRX_ENABLE |
14394 GRC_MODE_HTX2B_ENABLE |
14395 GRC_MODE_HOST_STACKUP);
14396 else
14397 val &= GRC_MODE_HOST_STACKUP;
14398
Linus Torvalds1da177e2005-04-16 15:20:36 -070014399 tw32(GRC_MODE, val | tp->grc_mode);
14400
14401 tg3_switch_clocks(tp);
14402
14403 /* Clear this out for sanity. */
14404 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
14405
14406 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14407 &pci_state_reg);
14408 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014409 !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014410 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
14411
14412 if (chiprevid == CHIPREV_ID_5701_A0 ||
14413 chiprevid == CHIPREV_ID_5701_B0 ||
14414 chiprevid == CHIPREV_ID_5701_B2 ||
14415 chiprevid == CHIPREV_ID_5701_B5) {
14416 void __iomem *sram_base;
14417
14418 /* Write some dummy words into the SRAM status block
14419 * area, see if it reads back correctly. If the return
14420 * value is bad, force enable the PCIX workaround.
14421 */
14422 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
14423
14424 writel(0x00000000, sram_base);
14425 writel(0x00000000, sram_base + 4);
14426 writel(0xffffffff, sram_base + 4);
14427 if (readl(sram_base) != 0x00000000)
Joe Perches63c3a662011-04-26 08:12:10 +000014428 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014429 }
14430 }
14431
14432 udelay(50);
14433 tg3_nvram_init(tp);
14434
14435 grc_misc_cfg = tr32(GRC_MISC_CFG);
14436 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
14437
Linus Torvalds1da177e2005-04-16 15:20:36 -070014438 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14439 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
14440 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
Joe Perches63c3a662011-04-26 08:12:10 +000014441 tg3_flag_set(tp, IS_5788);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014442
Joe Perches63c3a662011-04-26 08:12:10 +000014443 if (!tg3_flag(tp, IS_5788) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +000014444 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014445 tg3_flag_set(tp, TAGGED_STATUS);
14446 if (tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070014447 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
14448 HOSTCC_MODE_CLRTICK_TXBD);
14449
14450 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
14451 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
14452 tp->misc_host_ctrl);
14453 }
14454
Matt Carlson3bda1252008-08-15 14:08:22 -070014455 /* Preserve the APE MAC_MODE bits */
Joe Perches63c3a662011-04-26 08:12:10 +000014456 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +000014457 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070014458 else
Matt Carlson6e01b202011-08-19 13:58:20 +000014459 tp->mac_mode = 0;
Matt Carlson3bda1252008-08-15 14:08:22 -070014460
Linus Torvalds1da177e2005-04-16 15:20:36 -070014461 /* these are limited to 10/100 only */
14462 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14463 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
14464 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14465 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14466 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
14467 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
14468 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
14469 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14470 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
Michael Chan676917d2006-12-07 00:20:22 -080014471 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
14472 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014473 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
Matt Carlsond1101142010-02-17 15:16:55 +000014474 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
14475 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014476 (tp->phy_flags & TG3_PHYFLG_IS_FET))
14477 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014478
14479 err = tg3_phy_probe(tp);
14480 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014481 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014482 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014483 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014484 }
14485
Matt Carlson184b8902010-04-05 10:19:25 +000014486 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080014487 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014488
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014489 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
14490 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014491 } else {
14492 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014493 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014494 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014495 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014496 }
14497
14498 /* 5700 {AX,BX} chips have a broken status block link
14499 * change bit implementation, so we must use the
14500 * status register in those cases.
14501 */
14502 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014503 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014504 else
Joe Perches63c3a662011-04-26 08:12:10 +000014505 tg3_flag_clear(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014506
14507 /* The led_ctrl is set during tg3_phy_probe, here we might
14508 * have to force the link status polling mechanism based
14509 * upon subsystem IDs.
14510 */
14511 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070014512 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014513 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
14514 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Joe Perches63c3a662011-04-26 08:12:10 +000014515 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014516 }
14517
14518 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014519 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Joe Perches63c3a662011-04-26 08:12:10 +000014520 tg3_flag_set(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014521 else
Joe Perches63c3a662011-04-26 08:12:10 +000014522 tg3_flag_clear(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014523
Matt Carlsonbf933c82011-01-25 15:58:49 +000014524 tp->rx_offset = NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014525 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014526 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014527 tg3_flag(tp, PCIX_MODE)) {
Matt Carlsonbf933c82011-01-25 15:58:49 +000014528 tp->rx_offset = 0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014529#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000014530 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014531#endif
14532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014533
Matt Carlson2c49a442010-09-30 10:34:35 +000014534 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
14535 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000014536 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
14537
Matt Carlson2c49a442010-09-30 10:34:35 +000014538 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070014539
14540 /* Increment the rx prod index on the rx std ring by at most
14541 * 8 for these chips to workaround hw errata.
14542 */
14543 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
14544 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
14545 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
14546 tp->rx_std_max_post = 8;
14547
Joe Perches63c3a662011-04-26 08:12:10 +000014548 if (tg3_flag(tp, ASPM_WORKAROUND))
Matt Carlson8ed5d972007-05-07 00:25:49 -070014549 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
14550 PCIE_PWR_MGMT_L1_THRESH_MSK;
14551
Linus Torvalds1da177e2005-04-16 15:20:36 -070014552 return err;
14553}
14554
David S. Miller49b6e95f2007-03-29 01:38:42 -070014555#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014556static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
14557{
14558 struct net_device *dev = tp->dev;
14559 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014560 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070014561 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014562 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014563
David S. Miller49b6e95f2007-03-29 01:38:42 -070014564 addr = of_get_property(dp, "local-mac-address", &len);
14565 if (addr && len == 6) {
14566 memcpy(dev->dev_addr, addr, 6);
14567 memcpy(dev->perm_addr, dev->dev_addr, 6);
14568 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014569 }
14570 return -ENODEV;
14571}
14572
14573static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
14574{
14575 struct net_device *dev = tp->dev;
14576
14577 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070014578 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014579 return 0;
14580}
14581#endif
14582
14583static int __devinit tg3_get_device_address(struct tg3 *tp)
14584{
14585 struct net_device *dev = tp->dev;
14586 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080014587 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014588
David S. Miller49b6e95f2007-03-29 01:38:42 -070014589#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014590 if (!tg3_get_macaddr_sparc(tp))
14591 return 0;
14592#endif
14593
14594 mac_offset = 0x7c;
Matt Carlson6ff6f812011-05-19 12:12:54 +000014595 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014596 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014597 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
14598 mac_offset = 0xcc;
14599 if (tg3_nvram_lock(tp))
14600 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
14601 else
14602 tg3_nvram_unlock(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000014603 } else if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson69f11c92011-07-13 09:27:30 +000014604 if (tp->pci_fn & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014605 mac_offset = 0xcc;
Matt Carlson69f11c92011-07-13 09:27:30 +000014606 if (tp->pci_fn > 1)
Matt Carlsona50d0792010-06-05 17:24:37 +000014607 mac_offset += 0x18c;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014608 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070014609 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014610
14611 /* First try to get it from MAC address mailbox. */
14612 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
14613 if ((hi >> 16) == 0x484b) {
14614 dev->dev_addr[0] = (hi >> 8) & 0xff;
14615 dev->dev_addr[1] = (hi >> 0) & 0xff;
14616
14617 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
14618 dev->dev_addr[2] = (lo >> 24) & 0xff;
14619 dev->dev_addr[3] = (lo >> 16) & 0xff;
14620 dev->dev_addr[4] = (lo >> 8) & 0xff;
14621 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014622
Michael Chan008652b2006-03-27 23:14:53 -080014623 /* Some old bootcode may report a 0 MAC address in SRAM */
14624 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
14625 }
14626 if (!addr_ok) {
14627 /* Next, try NVRAM. */
Joe Perches63c3a662011-04-26 08:12:10 +000014628 if (!tg3_flag(tp, NO_NVRAM) &&
Matt Carlsondf259d82009-04-20 06:57:14 +000014629 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000014630 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070014631 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
14632 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080014633 }
14634 /* Finally just fetch it out of the MAC control regs. */
14635 else {
14636 hi = tr32(MAC_ADDR_0_HIGH);
14637 lo = tr32(MAC_ADDR_0_LOW);
14638
14639 dev->dev_addr[5] = lo & 0xff;
14640 dev->dev_addr[4] = (lo >> 8) & 0xff;
14641 dev->dev_addr[3] = (lo >> 16) & 0xff;
14642 dev->dev_addr[2] = (lo >> 24) & 0xff;
14643 dev->dev_addr[1] = hi & 0xff;
14644 dev->dev_addr[0] = (hi >> 8) & 0xff;
14645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014646 }
14647
14648 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070014649#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014650 if (!tg3_get_default_macaddr_sparc(tp))
14651 return 0;
14652#endif
14653 return -EINVAL;
14654 }
John W. Linville2ff43692005-09-12 14:44:20 -070014655 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014656 return 0;
14657}
14658
David S. Miller59e6b432005-05-18 22:50:10 -070014659#define BOUNDARY_SINGLE_CACHELINE 1
14660#define BOUNDARY_MULTI_CACHELINE 2
14661
14662static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
14663{
14664 int cacheline_size;
14665 u8 byte;
14666 int goal;
14667
14668 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
14669 if (byte == 0)
14670 cacheline_size = 1024;
14671 else
14672 cacheline_size = (int) byte * 4;
14673
14674 /* On 5703 and later chips, the boundary bits have no
14675 * effect.
14676 */
14677 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14678 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014679 !tg3_flag(tp, PCI_EXPRESS))
David S. Miller59e6b432005-05-18 22:50:10 -070014680 goto out;
14681
14682#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
14683 goal = BOUNDARY_MULTI_CACHELINE;
14684#else
14685#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
14686 goal = BOUNDARY_SINGLE_CACHELINE;
14687#else
14688 goal = 0;
14689#endif
14690#endif
14691
Joe Perches63c3a662011-04-26 08:12:10 +000014692 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014693 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
14694 goto out;
14695 }
14696
David S. Miller59e6b432005-05-18 22:50:10 -070014697 if (!goal)
14698 goto out;
14699
14700 /* PCI controllers on most RISC systems tend to disconnect
14701 * when a device tries to burst across a cache-line boundary.
14702 * Therefore, letting tg3 do so just wastes PCI bandwidth.
14703 *
14704 * Unfortunately, for PCI-E there are only limited
14705 * write-side controls for this, and thus for reads
14706 * we will still get the disconnects. We'll also waste
14707 * these PCI cycles for both read and write for chips
14708 * other than 5700 and 5701 which do not implement the
14709 * boundary bits.
14710 */
Joe Perches63c3a662011-04-26 08:12:10 +000014711 if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014712 switch (cacheline_size) {
14713 case 16:
14714 case 32:
14715 case 64:
14716 case 128:
14717 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14718 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
14719 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
14720 } else {
14721 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14722 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14723 }
14724 break;
14725
14726 case 256:
14727 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
14728 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
14729 break;
14730
14731 default:
14732 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14733 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14734 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014735 }
Joe Perches63c3a662011-04-26 08:12:10 +000014736 } else if (tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014737 switch (cacheline_size) {
14738 case 16:
14739 case 32:
14740 case 64:
14741 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14742 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14743 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
14744 break;
14745 }
14746 /* fallthrough */
14747 case 128:
14748 default:
14749 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14750 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
14751 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014752 }
David S. Miller59e6b432005-05-18 22:50:10 -070014753 } else {
14754 switch (cacheline_size) {
14755 case 16:
14756 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14757 val |= (DMA_RWCTRL_READ_BNDRY_16 |
14758 DMA_RWCTRL_WRITE_BNDRY_16);
14759 break;
14760 }
14761 /* fallthrough */
14762 case 32:
14763 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14764 val |= (DMA_RWCTRL_READ_BNDRY_32 |
14765 DMA_RWCTRL_WRITE_BNDRY_32);
14766 break;
14767 }
14768 /* fallthrough */
14769 case 64:
14770 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14771 val |= (DMA_RWCTRL_READ_BNDRY_64 |
14772 DMA_RWCTRL_WRITE_BNDRY_64);
14773 break;
14774 }
14775 /* fallthrough */
14776 case 128:
14777 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14778 val |= (DMA_RWCTRL_READ_BNDRY_128 |
14779 DMA_RWCTRL_WRITE_BNDRY_128);
14780 break;
14781 }
14782 /* fallthrough */
14783 case 256:
14784 val |= (DMA_RWCTRL_READ_BNDRY_256 |
14785 DMA_RWCTRL_WRITE_BNDRY_256);
14786 break;
14787 case 512:
14788 val |= (DMA_RWCTRL_READ_BNDRY_512 |
14789 DMA_RWCTRL_WRITE_BNDRY_512);
14790 break;
14791 case 1024:
14792 default:
14793 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
14794 DMA_RWCTRL_WRITE_BNDRY_1024);
14795 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014796 }
David S. Miller59e6b432005-05-18 22:50:10 -070014797 }
14798
14799out:
14800 return val;
14801}
14802
Linus Torvalds1da177e2005-04-16 15:20:36 -070014803static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
14804{
14805 struct tg3_internal_buffer_desc test_desc;
14806 u32 sram_dma_descs;
14807 int i, ret;
14808
14809 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
14810
14811 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
14812 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
14813 tw32(RDMAC_STATUS, 0);
14814 tw32(WDMAC_STATUS, 0);
14815
14816 tw32(BUFMGR_MODE, 0);
14817 tw32(FTQ_RESET, 0);
14818
14819 test_desc.addr_hi = ((u64) buf_dma) >> 32;
14820 test_desc.addr_lo = buf_dma & 0xffffffff;
14821 test_desc.nic_mbuf = 0x00002100;
14822 test_desc.len = size;
14823
14824 /*
14825 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
14826 * the *second* time the tg3 driver was getting loaded after an
14827 * initial scan.
14828 *
14829 * Broadcom tells me:
14830 * ...the DMA engine is connected to the GRC block and a DMA
14831 * reset may affect the GRC block in some unpredictable way...
14832 * The behavior of resets to individual blocks has not been tested.
14833 *
14834 * Broadcom noted the GRC reset will also reset all sub-components.
14835 */
14836 if (to_device) {
14837 test_desc.cqid_sqid = (13 << 8) | 2;
14838
14839 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
14840 udelay(40);
14841 } else {
14842 test_desc.cqid_sqid = (16 << 8) | 7;
14843
14844 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
14845 udelay(40);
14846 }
14847 test_desc.flags = 0x00000005;
14848
14849 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
14850 u32 val;
14851
14852 val = *(((u32 *)&test_desc) + i);
14853 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
14854 sram_dma_descs + (i * sizeof(u32)));
14855 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
14856 }
14857 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
14858
Matt Carlson859a5882010-04-05 10:19:28 +000014859 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014860 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a5882010-04-05 10:19:28 +000014861 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070014862 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014863
14864 ret = -ENODEV;
14865 for (i = 0; i < 40; i++) {
14866 u32 val;
14867
14868 if (to_device)
14869 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
14870 else
14871 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
14872 if ((val & 0xffff) == sram_dma_descs) {
14873 ret = 0;
14874 break;
14875 }
14876
14877 udelay(100);
14878 }
14879
14880 return ret;
14881}
14882
David S. Millerded73402005-05-23 13:59:47 -070014883#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070014884
Matt Carlson41434702011-03-09 16:58:22 +000014885static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080014886 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
14887 { },
14888};
14889
Linus Torvalds1da177e2005-04-16 15:20:36 -070014890static int __devinit tg3_test_dma(struct tg3 *tp)
14891{
14892 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070014893 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014894 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014895
Matt Carlson4bae65c2010-11-24 08:31:52 +000014896 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
14897 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014898 if (!buf) {
14899 ret = -ENOMEM;
14900 goto out_nofree;
14901 }
14902
14903 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
14904 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
14905
David S. Miller59e6b432005-05-18 22:50:10 -070014906 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014907
Joe Perches63c3a662011-04-26 08:12:10 +000014908 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014909 goto out;
14910
Joe Perches63c3a662011-04-26 08:12:10 +000014911 if (tg3_flag(tp, PCI_EXPRESS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014912 /* DMA read watermark not used on PCIE */
14913 tp->dma_rwctrl |= 0x00180000;
Joe Perches63c3a662011-04-26 08:12:10 +000014914 } else if (!tg3_flag(tp, PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070014915 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
14916 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014917 tp->dma_rwctrl |= 0x003f0000;
14918 else
14919 tp->dma_rwctrl |= 0x003f000f;
14920 } else {
14921 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14922 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
14923 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080014924 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014925
Michael Chan4a29cc22006-03-19 13:21:12 -080014926 /* If the 5704 is behind the EPB bridge, we can
14927 * do the less restrictive ONE_DMA workaround for
14928 * better performance.
14929 */
Joe Perches63c3a662011-04-26 08:12:10 +000014930 if (tg3_flag(tp, 40BIT_DMA_BUG) &&
Michael Chan4a29cc22006-03-19 13:21:12 -080014931 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14932 tp->dma_rwctrl |= 0x8000;
14933 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014934 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
14935
Michael Chan49afdeb2007-02-13 12:17:03 -080014936 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
14937 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070014938 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080014939 tp->dma_rwctrl |=
14940 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
14941 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
14942 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070014943 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
14944 /* 5780 always in PCIX mode */
14945 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070014946 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
14947 /* 5714 always in PCIX mode */
14948 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014949 } else {
14950 tp->dma_rwctrl |= 0x001b000f;
14951 }
14952 }
14953
14954 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14955 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14956 tp->dma_rwctrl &= 0xfffffff0;
14957
14958 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14959 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
14960 /* Remove this if it causes problems for some boards. */
14961 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
14962
14963 /* On 5700/5701 chips, we need to set this bit.
14964 * Otherwise the chip will issue cacheline transactions
14965 * to streamable DMA memory with not all the byte
14966 * enables turned on. This is an error on several
14967 * RISC PCI controllers, in particular sparc64.
14968 *
14969 * On 5703/5704 chips, this bit has been reassigned
14970 * a different meaning. In particular, it is used
14971 * on those chips to enable a PCI-X workaround.
14972 */
14973 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
14974 }
14975
14976 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14977
14978#if 0
14979 /* Unneeded, already done by tg3_get_invariants. */
14980 tg3_switch_clocks(tp);
14981#endif
14982
Linus Torvalds1da177e2005-04-16 15:20:36 -070014983 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14984 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
14985 goto out;
14986
David S. Miller59e6b432005-05-18 22:50:10 -070014987 /* It is best to perform DMA test with maximum write burst size
14988 * to expose the 5700/5701 write DMA bug.
14989 */
14990 saved_dma_rwctrl = tp->dma_rwctrl;
14991 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14992 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14993
Linus Torvalds1da177e2005-04-16 15:20:36 -070014994 while (1) {
14995 u32 *p = buf, i;
14996
14997 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
14998 p[i] = i;
14999
15000 /* Send the buffer to the chip. */
15001 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
15002 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000015003 dev_err(&tp->pdev->dev,
15004 "%s: Buffer write failed. err = %d\n",
15005 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015006 break;
15007 }
15008
15009#if 0
15010 /* validate data reached card RAM correctly. */
15011 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
15012 u32 val;
15013 tg3_read_mem(tp, 0x2100 + (i*4), &val);
15014 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000015015 dev_err(&tp->pdev->dev,
15016 "%s: Buffer corrupted on device! "
15017 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015018 /* ret = -ENODEV here? */
15019 }
15020 p[i] = 0;
15021 }
15022#endif
15023 /* Now read it back. */
15024 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
15025 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000015026 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
15027 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015028 break;
15029 }
15030
15031 /* Verify it. */
15032 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
15033 if (p[i] == i)
15034 continue;
15035
David S. Miller59e6b432005-05-18 22:50:10 -070015036 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
15037 DMA_RWCTRL_WRITE_BNDRY_16) {
15038 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015039 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
15040 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15041 break;
15042 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000015043 dev_err(&tp->pdev->dev,
15044 "%s: Buffer corrupted on read back! "
15045 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015046 ret = -ENODEV;
15047 goto out;
15048 }
15049 }
15050
15051 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
15052 /* Success. */
15053 ret = 0;
15054 break;
15055 }
15056 }
David S. Miller59e6b432005-05-18 22:50:10 -070015057 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
15058 DMA_RWCTRL_WRITE_BNDRY_16) {
15059 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070015060 * now look for chipsets that are known to expose the
15061 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070015062 */
Matt Carlson41434702011-03-09 16:58:22 +000015063 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070015064 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
15065 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a5882010-04-05 10:19:28 +000015066 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070015067 /* Safe to use the calculated DMA boundary. */
15068 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a5882010-04-05 10:19:28 +000015069 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070015070
David S. Miller59e6b432005-05-18 22:50:10 -070015071 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015073
15074out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000015075 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015076out_nofree:
15077 return ret;
15078}
15079
Linus Torvalds1da177e2005-04-16 15:20:36 -070015080static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
15081{
Joe Perches63c3a662011-04-26 08:12:10 +000015082 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson666bc832010-01-20 16:58:03 +000015083 tp->bufmgr_config.mbuf_read_dma_low_water =
15084 DEFAULT_MB_RDMA_LOW_WATER_5705;
15085 tp->bufmgr_config.mbuf_mac_rx_low_water =
15086 DEFAULT_MB_MACRX_LOW_WATER_57765;
15087 tp->bufmgr_config.mbuf_high_water =
15088 DEFAULT_MB_HIGH_WATER_57765;
15089
15090 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15091 DEFAULT_MB_RDMA_LOW_WATER_5705;
15092 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15093 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
15094 tp->bufmgr_config.mbuf_high_water_jumbo =
15095 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000015096 } else if (tg3_flag(tp, 5705_PLUS)) {
Michael Chanfdfec172005-07-25 12:31:48 -070015097 tp->bufmgr_config.mbuf_read_dma_low_water =
15098 DEFAULT_MB_RDMA_LOW_WATER_5705;
15099 tp->bufmgr_config.mbuf_mac_rx_low_water =
15100 DEFAULT_MB_MACRX_LOW_WATER_5705;
15101 tp->bufmgr_config.mbuf_high_water =
15102 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070015103 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
15104 tp->bufmgr_config.mbuf_mac_rx_low_water =
15105 DEFAULT_MB_MACRX_LOW_WATER_5906;
15106 tp->bufmgr_config.mbuf_high_water =
15107 DEFAULT_MB_HIGH_WATER_5906;
15108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015109
Michael Chanfdfec172005-07-25 12:31:48 -070015110 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15111 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
15112 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15113 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
15114 tp->bufmgr_config.mbuf_high_water_jumbo =
15115 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
15116 } else {
15117 tp->bufmgr_config.mbuf_read_dma_low_water =
15118 DEFAULT_MB_RDMA_LOW_WATER;
15119 tp->bufmgr_config.mbuf_mac_rx_low_water =
15120 DEFAULT_MB_MACRX_LOW_WATER;
15121 tp->bufmgr_config.mbuf_high_water =
15122 DEFAULT_MB_HIGH_WATER;
15123
15124 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15125 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
15126 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15127 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
15128 tp->bufmgr_config.mbuf_high_water_jumbo =
15129 DEFAULT_MB_HIGH_WATER_JUMBO;
15130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015131
15132 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
15133 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
15134}
15135
15136static char * __devinit tg3_phy_string(struct tg3 *tp)
15137{
Matt Carlson79eb6902010-02-17 15:17:03 +000015138 switch (tp->phy_id & TG3_PHY_ID_MASK) {
15139 case TG3_PHY_ID_BCM5400: return "5400";
15140 case TG3_PHY_ID_BCM5401: return "5401";
15141 case TG3_PHY_ID_BCM5411: return "5411";
15142 case TG3_PHY_ID_BCM5701: return "5701";
15143 case TG3_PHY_ID_BCM5703: return "5703";
15144 case TG3_PHY_ID_BCM5704: return "5704";
15145 case TG3_PHY_ID_BCM5705: return "5705";
15146 case TG3_PHY_ID_BCM5750: return "5750";
15147 case TG3_PHY_ID_BCM5752: return "5752";
15148 case TG3_PHY_ID_BCM5714: return "5714";
15149 case TG3_PHY_ID_BCM5780: return "5780";
15150 case TG3_PHY_ID_BCM5755: return "5755";
15151 case TG3_PHY_ID_BCM5787: return "5787";
15152 case TG3_PHY_ID_BCM5784: return "5784";
15153 case TG3_PHY_ID_BCM5756: return "5722/5756";
15154 case TG3_PHY_ID_BCM5906: return "5906";
15155 case TG3_PHY_ID_BCM5761: return "5761";
15156 case TG3_PHY_ID_BCM5718C: return "5718C";
15157 case TG3_PHY_ID_BCM5718S: return "5718S";
15158 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000015159 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000015160 case TG3_PHY_ID_BCM5720C: return "5720C";
Matt Carlson79eb6902010-02-17 15:17:03 +000015161 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070015162 case 0: return "serdes";
15163 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070015164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015165}
15166
Michael Chanf9804dd2005-09-27 12:13:10 -070015167static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
15168{
Joe Perches63c3a662011-04-26 08:12:10 +000015169 if (tg3_flag(tp, PCI_EXPRESS)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070015170 strcpy(str, "PCI Express");
15171 return str;
Joe Perches63c3a662011-04-26 08:12:10 +000015172 } else if (tg3_flag(tp, PCIX_MODE)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070015173 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
15174
15175 strcpy(str, "PCIX:");
15176
15177 if ((clock_ctrl == 7) ||
15178 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
15179 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
15180 strcat(str, "133MHz");
15181 else if (clock_ctrl == 0)
15182 strcat(str, "33MHz");
15183 else if (clock_ctrl == 2)
15184 strcat(str, "50MHz");
15185 else if (clock_ctrl == 4)
15186 strcat(str, "66MHz");
15187 else if (clock_ctrl == 6)
15188 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070015189 } else {
15190 strcpy(str, "PCI:");
Joe Perches63c3a662011-04-26 08:12:10 +000015191 if (tg3_flag(tp, PCI_HIGH_SPEED))
Michael Chanf9804dd2005-09-27 12:13:10 -070015192 strcat(str, "66MHz");
15193 else
15194 strcat(str, "33MHz");
15195 }
Joe Perches63c3a662011-04-26 08:12:10 +000015196 if (tg3_flag(tp, PCI_32BIT))
Michael Chanf9804dd2005-09-27 12:13:10 -070015197 strcat(str, ":32-bit");
15198 else
15199 strcat(str, ":64-bit");
15200 return str;
15201}
15202
Michael Chan8c2dc7e2005-12-19 16:26:02 -080015203static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015204{
15205 struct pci_dev *peer;
15206 unsigned int func, devnr = tp->pdev->devfn & ~7;
15207
15208 for (func = 0; func < 8; func++) {
15209 peer = pci_get_slot(tp->pdev->bus, devnr | func);
15210 if (peer && peer != tp->pdev)
15211 break;
15212 pci_dev_put(peer);
15213 }
Michael Chan16fe9d72005-12-13 21:09:54 -080015214 /* 5704 can be configured in single-port mode, set peer to
15215 * tp->pdev in that case.
15216 */
15217 if (!peer) {
15218 peer = tp->pdev;
15219 return peer;
15220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015221
15222 /*
15223 * We don't need to keep the refcount elevated; there's no way
15224 * to remove one half of this device without removing the other
15225 */
15226 pci_dev_put(peer);
15227
15228 return peer;
15229}
15230
David S. Miller15f98502005-05-18 22:49:26 -070015231static void __devinit tg3_init_coal(struct tg3 *tp)
15232{
15233 struct ethtool_coalesce *ec = &tp->coal;
15234
15235 memset(ec, 0, sizeof(*ec));
15236 ec->cmd = ETHTOOL_GCOALESCE;
15237 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
15238 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
15239 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
15240 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
15241 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
15242 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
15243 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
15244 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
15245 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
15246
15247 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
15248 HOSTCC_MODE_CLRTICK_TXBD)) {
15249 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
15250 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
15251 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
15252 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
15253 }
Michael Chand244c892005-07-05 14:42:33 -070015254
Joe Perches63c3a662011-04-26 08:12:10 +000015255 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070015256 ec->rx_coalesce_usecs_irq = 0;
15257 ec->tx_coalesce_usecs_irq = 0;
15258 ec->stats_block_coalesce_usecs = 0;
15259 }
David S. Miller15f98502005-05-18 22:49:26 -070015260}
15261
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080015262static const struct net_device_ops tg3_netdev_ops = {
15263 .ndo_open = tg3_open,
15264 .ndo_stop = tg3_close,
Stephen Hemminger00829822008-11-20 20:14:53 -080015265 .ndo_start_xmit = tg3_start_xmit,
Eric Dumazet511d2222010-07-07 20:44:24 +000015266 .ndo_get_stats64 = tg3_get_stats64,
Stephen Hemminger00829822008-11-20 20:14:53 -080015267 .ndo_validate_addr = eth_validate_addr,
Jiri Pirkoafc4b132011-08-16 06:29:01 +000015268 .ndo_set_rx_mode = tg3_set_rx_mode,
Stephen Hemminger00829822008-11-20 20:14:53 -080015269 .ndo_set_mac_address = tg3_set_mac_addr,
15270 .ndo_do_ioctl = tg3_ioctl,
15271 .ndo_tx_timeout = tg3_tx_timeout,
15272 .ndo_change_mtu = tg3_change_mtu,
Michał Mirosławdc668912011-04-07 03:35:07 +000015273 .ndo_fix_features = tg3_fix_features,
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015274 .ndo_set_features = tg3_set_features,
Stephen Hemminger00829822008-11-20 20:14:53 -080015275#ifdef CONFIG_NET_POLL_CONTROLLER
15276 .ndo_poll_controller = tg3_poll_controller,
15277#endif
15278};
15279
Linus Torvalds1da177e2005-04-16 15:20:36 -070015280static int __devinit tg3_init_one(struct pci_dev *pdev,
15281 const struct pci_device_id *ent)
15282{
Linus Torvalds1da177e2005-04-16 15:20:36 -070015283 struct net_device *dev;
15284 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000015285 int i, err, pm_cap;
15286 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070015287 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080015288 u64 dma_mask, persist_dma_mask;
Matt Carlson0da06062011-05-19 12:12:53 +000015289 u32 features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015290
Joe Perches05dbe002010-02-17 19:44:19 +000015291 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015292
15293 err = pci_enable_device(pdev);
15294 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015295 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015296 return err;
15297 }
15298
Linus Torvalds1da177e2005-04-16 15:20:36 -070015299 err = pci_request_regions(pdev, DRV_MODULE_NAME);
15300 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015301 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015302 goto err_out_disable_pdev;
15303 }
15304
15305 pci_set_master(pdev);
15306
15307 /* Find power-management capability. */
15308 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
15309 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000015310 dev_err(&pdev->dev,
15311 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015312 err = -EIO;
15313 goto err_out_free_res;
15314 }
15315
Matt Carlson16821282011-07-13 09:27:28 +000015316 err = pci_set_power_state(pdev, PCI_D0);
15317 if (err) {
15318 dev_err(&pdev->dev, "Transition to D0 failed, aborting\n");
15319 goto err_out_free_res;
15320 }
15321
Matt Carlsonfe5f5782009-09-01 13:09:39 +000015322 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015323 if (!dev) {
Matt Carlson2445e462010-04-05 10:19:21 +000015324 dev_err(&pdev->dev, "Etherdev alloc failed, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015325 err = -ENOMEM;
Matt Carlson16821282011-07-13 09:27:28 +000015326 goto err_out_power_down;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015327 }
15328
Linus Torvalds1da177e2005-04-16 15:20:36 -070015329 SET_NETDEV_DEV(dev, &pdev->dev);
15330
Linus Torvalds1da177e2005-04-16 15:20:36 -070015331 tp = netdev_priv(dev);
15332 tp->pdev = pdev;
15333 tp->dev = dev;
15334 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015335 tp->rx_mode = TG3_DEF_RX_MODE;
15336 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070015337
Linus Torvalds1da177e2005-04-16 15:20:36 -070015338 if (tg3_debug > 0)
15339 tp->msg_enable = tg3_debug;
15340 else
15341 tp->msg_enable = TG3_DEF_MSG_ENABLE;
15342
15343 /* The word/byte swap controls here control register access byte
15344 * swapping. DMA data byte swapping is controlled in the GRC_MODE
15345 * setting below.
15346 */
15347 tp->misc_host_ctrl =
15348 MISC_HOST_CTRL_MASK_PCI_INT |
15349 MISC_HOST_CTRL_WORD_SWAP |
15350 MISC_HOST_CTRL_INDIR_ACCESS |
15351 MISC_HOST_CTRL_PCISTATE_RW;
15352
15353 /* The NONFRM (non-frame) byte/word swap controls take effect
15354 * on descriptor entries, anything which isn't packet data.
15355 *
15356 * The StrongARM chips on the board (one for tx, one for rx)
15357 * are running in big-endian mode.
15358 */
15359 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
15360 GRC_MODE_WSWAP_NONFRM_DATA);
15361#ifdef __BIG_ENDIAN
15362 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
15363#endif
15364 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015365 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000015366 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015367
Matt Carlsond5fe4882008-11-21 17:20:32 -080015368 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010015369 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015370 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015371 err = -ENOMEM;
15372 goto err_out_free_dev;
15373 }
15374
Matt Carlsonc9cab242011-07-13 09:27:27 +000015375 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
15376 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761E ||
15377 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S ||
15378 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761SE ||
15379 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
15380 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
15381 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
15382 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720) {
15383 tg3_flag_set(tp, ENABLE_APE);
15384 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
15385 if (!tp->aperegs) {
15386 dev_err(&pdev->dev,
15387 "Cannot map APE registers, aborting\n");
15388 err = -ENOMEM;
15389 goto err_out_iounmap;
15390 }
15391 }
15392
Linus Torvalds1da177e2005-04-16 15:20:36 -070015393 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
15394 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015395
Linus Torvalds1da177e2005-04-16 15:20:36 -070015396 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015397 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Matt Carlson2ffcc982011-05-19 12:12:44 +000015398 dev->netdev_ops = &tg3_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015399 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015400
15401 err = tg3_get_invariants(tp);
15402 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015403 dev_err(&pdev->dev,
15404 "Problem fetching invariants of chip, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015405 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015406 }
15407
Michael Chan4a29cc22006-03-19 13:21:12 -080015408 /* The EPB bridge inside 5714, 5715, and 5780 and any
15409 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080015410 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
15411 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
15412 * do DMA address check in tg3_start_xmit().
15413 */
Joe Perches63c3a662011-04-26 08:12:10 +000015414 if (tg3_flag(tp, IS_5788))
Yang Hongyang284901a2009-04-06 19:01:15 -070015415 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Joe Perches63c3a662011-04-26 08:12:10 +000015416 else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070015417 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080015418#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070015419 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015420#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080015421 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070015422 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015423
15424 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070015425 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080015426 err = pci_set_dma_mask(pdev, dma_mask);
15427 if (!err) {
Matt Carlson0da06062011-05-19 12:12:53 +000015428 features |= NETIF_F_HIGHDMA;
Michael Chan72f2afb2006-03-06 19:28:35 -080015429 err = pci_set_consistent_dma_mask(pdev,
15430 persist_dma_mask);
15431 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015432 dev_err(&pdev->dev, "Unable to obtain 64 bit "
15433 "DMA for consistent allocations\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015434 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080015435 }
15436 }
15437 }
Yang Hongyang284901a2009-04-06 19:01:15 -070015438 if (err || dma_mask == DMA_BIT_MASK(32)) {
15439 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080015440 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015441 dev_err(&pdev->dev,
15442 "No usable DMA configuration, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015443 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080015444 }
15445 }
15446
Michael Chanfdfec172005-07-25 12:31:48 -070015447 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015448
Matt Carlson0da06062011-05-19 12:12:53 +000015449 features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
15450
15451 /* 5700 B0 chips do not support checksumming correctly due
15452 * to hardware bugs.
15453 */
15454 if (tp->pci_chip_rev_id != CHIPREV_ID_5700_B0) {
15455 features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
15456
15457 if (tg3_flag(tp, 5755_PLUS))
15458 features |= NETIF_F_IPV6_CSUM;
15459 }
15460
Michael Chan4e3a7aa2006-03-20 17:47:44 -080015461 /* TSO is on by default on chips that support hardware TSO.
15462 * Firmware TSO on older chips gives lower performance, so it
15463 * is off by default, but can be enabled using ethtool.
15464 */
Joe Perches63c3a662011-04-26 08:12:10 +000015465 if ((tg3_flag(tp, HW_TSO_1) ||
15466 tg3_flag(tp, HW_TSO_2) ||
15467 tg3_flag(tp, HW_TSO_3)) &&
Matt Carlson0da06062011-05-19 12:12:53 +000015468 (features & NETIF_F_IP_CSUM))
15469 features |= NETIF_F_TSO;
Joe Perches63c3a662011-04-26 08:12:10 +000015470 if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
Matt Carlson0da06062011-05-19 12:12:53 +000015471 if (features & NETIF_F_IPV6_CSUM)
15472 features |= NETIF_F_TSO6;
Joe Perches63c3a662011-04-26 08:12:10 +000015473 if (tg3_flag(tp, HW_TSO_3) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000015474 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070015475 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
15476 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Joe Perches63c3a662011-04-26 08:12:10 +000015477 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Michał Mirosławdc668912011-04-07 03:35:07 +000015478 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson0da06062011-05-19 12:12:53 +000015479 features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070015480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015481
Matt Carlsond542fe22011-05-19 16:02:43 +000015482 dev->features |= features;
15483 dev->vlan_features |= features;
15484
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015485 /*
15486 * Add loopback capability only for a subset of devices that support
15487 * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
15488 * loopback for the remaining devices.
15489 */
15490 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
15491 !tg3_flag(tp, CPMU_PRESENT))
15492 /* Add the loopback capability */
Matt Carlson0da06062011-05-19 12:12:53 +000015493 features |= NETIF_F_LOOPBACK;
15494
Matt Carlson0da06062011-05-19 12:12:53 +000015495 dev->hw_features |= features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015496
Linus Torvalds1da177e2005-04-16 15:20:36 -070015497 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015498 !tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015499 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015500 tg3_flag_set(tp, MAX_RXPEND_64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015501 tp->rx_pending = 63;
15502 }
15503
Linus Torvalds1da177e2005-04-16 15:20:36 -070015504 err = tg3_get_device_address(tp);
15505 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015506 dev_err(&pdev->dev,
15507 "Could not obtain valid ethernet address, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015508 goto err_out_apeunmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070015509 }
15510
Matt Carlsonc88864d2007-11-12 21:07:01 -080015511 /*
15512 * Reset chip in case UNDI or EFI driver did not shutdown
15513 * DMA self test will enable WDMAC and we'll see (spurious)
15514 * pending DMA on the PCI bus at that point.
15515 */
15516 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
15517 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
15518 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
15519 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
15520 }
15521
15522 err = tg3_test_dma(tp);
15523 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015524 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080015525 goto err_out_apeunmap;
15526 }
15527
Matt Carlson78f90dc2009-11-13 13:03:42 +000015528 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
15529 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
15530 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000015531 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000015532 struct tg3_napi *tnapi = &tp->napi[i];
15533
15534 tnapi->tp = tp;
15535 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
15536
15537 tnapi->int_mbox = intmbx;
Matt Carlson93a700a2011-08-31 11:44:54 +000015538 if (i <= 4)
Matt Carlson78f90dc2009-11-13 13:03:42 +000015539 intmbx += 0x8;
15540 else
15541 intmbx += 0x4;
15542
15543 tnapi->consmbox = rcvmbx;
15544 tnapi->prodmbox = sndmbx;
15545
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015546 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000015547 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015548 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000015549 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000015550
Joe Perches63c3a662011-04-26 08:12:10 +000015551 if (!tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson78f90dc2009-11-13 13:03:42 +000015552 break;
15553
15554 /*
15555 * If we support MSIX, we'll be using RSS. If we're using
15556 * RSS, the first vector only handles link interrupts and the
15557 * remaining vectors handle rx and tx interrupts. Reuse the
15558 * mailbox values for the next iteration. The values we setup
15559 * above are still useful for the single vectored mode.
15560 */
15561 if (!i)
15562 continue;
15563
15564 rcvmbx += 0x8;
15565
15566 if (sndmbx & 0x4)
15567 sndmbx -= 0x4;
15568 else
15569 sndmbx += 0xc;
15570 }
15571
Matt Carlsonc88864d2007-11-12 21:07:01 -080015572 tg3_init_coal(tp);
15573
Michael Chanc49a1562006-12-17 17:07:29 -080015574 pci_set_drvdata(pdev, dev);
15575
Matt Carlsoncd0d7222011-07-13 09:27:33 +000015576 if (tg3_flag(tp, 5717_PLUS)) {
15577 /* Resume a low-power mode */
15578 tg3_frob_aux_power(tp, false);
15579 }
15580
Linus Torvalds1da177e2005-04-16 15:20:36 -070015581 err = register_netdev(dev);
15582 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015583 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070015584 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015585 }
15586
Joe Perches05dbe002010-02-17 19:44:19 +000015587 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
15588 tp->board_part_number,
15589 tp->pci_chip_rev_id,
15590 tg3_bus_string(tp, str),
15591 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015592
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015593 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000015594 struct phy_device *phydev;
15595 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000015596 netdev_info(dev,
15597 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000015598 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015599 } else {
15600 char *ethtype;
15601
15602 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
15603 ethtype = "10/100Base-TX";
15604 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
15605 ethtype = "1000Base-SX";
15606 else
15607 ethtype = "10/100/1000Base-T";
15608
Matt Carlson5129c3a2010-04-05 10:19:23 +000015609 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlson47007832011-04-20 07:57:43 +000015610 "(WireSpeed[%d], EEE[%d])\n",
15611 tg3_phy_string(tp), ethtype,
15612 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
15613 (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015614 }
Matt Carlsondf59c942008-11-03 16:52:56 -080015615
Joe Perches05dbe002010-02-17 19:44:19 +000015616 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000015617 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015618 tg3_flag(tp, USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015619 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015620 tg3_flag(tp, ENABLE_ASF) != 0,
15621 tg3_flag(tp, TSO_CAPABLE) != 0);
Joe Perches05dbe002010-02-17 19:44:19 +000015622 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
15623 tp->dma_rwctrl,
15624 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
15625 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015626
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015627 pci_save_state(pdev);
15628
Linus Torvalds1da177e2005-04-16 15:20:36 -070015629 return 0;
15630
Matt Carlson0d3031d2007-10-10 18:02:43 -070015631err_out_apeunmap:
15632 if (tp->aperegs) {
15633 iounmap(tp->aperegs);
15634 tp->aperegs = NULL;
15635 }
15636
Linus Torvalds1da177e2005-04-16 15:20:36 -070015637err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070015638 if (tp->regs) {
15639 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015640 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015642
15643err_out_free_dev:
15644 free_netdev(dev);
15645
Matt Carlson16821282011-07-13 09:27:28 +000015646err_out_power_down:
15647 pci_set_power_state(pdev, PCI_D3hot);
15648
Linus Torvalds1da177e2005-04-16 15:20:36 -070015649err_out_free_res:
15650 pci_release_regions(pdev);
15651
15652err_out_disable_pdev:
15653 pci_disable_device(pdev);
15654 pci_set_drvdata(pdev, NULL);
15655 return err;
15656}
15657
15658static void __devexit tg3_remove_one(struct pci_dev *pdev)
15659{
15660 struct net_device *dev = pci_get_drvdata(pdev);
15661
15662 if (dev) {
15663 struct tg3 *tp = netdev_priv(dev);
15664
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080015665 if (tp->fw)
15666 release_firmware(tp->fw);
15667
Tejun Heo23f333a2010-12-12 16:45:14 +010015668 cancel_work_sync(&tp->reset_task);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015669
David S. Miller1805b2f2011-10-24 18:18:09 -040015670 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015671 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015672 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015673 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070015674
Linus Torvalds1da177e2005-04-16 15:20:36 -070015675 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015676 if (tp->aperegs) {
15677 iounmap(tp->aperegs);
15678 tp->aperegs = NULL;
15679 }
Michael Chan68929142005-08-09 20:17:14 -070015680 if (tp->regs) {
15681 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015682 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015683 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015684 free_netdev(dev);
15685 pci_release_regions(pdev);
15686 pci_disable_device(pdev);
15687 pci_set_drvdata(pdev, NULL);
15688 }
15689}
15690
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015691#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015692static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015693{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015694 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015695 struct net_device *dev = pci_get_drvdata(pdev);
15696 struct tg3 *tp = netdev_priv(dev);
15697 int err;
15698
15699 if (!netif_running(dev))
15700 return 0;
15701
Tejun Heo23f333a2010-12-12 16:45:14 +010015702 flush_work_sync(&tp->reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015703 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015704 tg3_netif_stop(tp);
15705
15706 del_timer_sync(&tp->timer);
15707
David S. Millerf47c11e2005-06-24 20:18:35 -070015708 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015709 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070015710 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015711
15712 netif_device_detach(dev);
15713
David S. Millerf47c11e2005-06-24 20:18:35 -070015714 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070015715 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches63c3a662011-04-26 08:12:10 +000015716 tg3_flag_clear(tp, INIT_COMPLETE);
David S. Millerf47c11e2005-06-24 20:18:35 -070015717 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015718
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015719 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015720 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015721 int err2;
15722
David S. Millerf47c11e2005-06-24 20:18:35 -070015723 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015724
Joe Perches63c3a662011-04-26 08:12:10 +000015725 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015726 err2 = tg3_restart_hw(tp, 1);
15727 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070015728 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015729
15730 tp->timer.expires = jiffies + tp->timer_offset;
15731 add_timer(&tp->timer);
15732
15733 netif_device_attach(dev);
15734 tg3_netif_start(tp);
15735
Michael Chanb9ec6c12006-07-25 16:37:27 -070015736out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015737 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015738
15739 if (!err2)
15740 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015741 }
15742
15743 return err;
15744}
15745
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015746static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015747{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015748 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015749 struct net_device *dev = pci_get_drvdata(pdev);
15750 struct tg3 *tp = netdev_priv(dev);
15751 int err;
15752
15753 if (!netif_running(dev))
15754 return 0;
15755
Linus Torvalds1da177e2005-04-16 15:20:36 -070015756 netif_device_attach(dev);
15757
David S. Millerf47c11e2005-06-24 20:18:35 -070015758 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015759
Joe Perches63c3a662011-04-26 08:12:10 +000015760 tg3_flag_set(tp, INIT_COMPLETE);
Michael Chanb9ec6c12006-07-25 16:37:27 -070015761 err = tg3_restart_hw(tp, 1);
15762 if (err)
15763 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015764
15765 tp->timer.expires = jiffies + tp->timer_offset;
15766 add_timer(&tp->timer);
15767
Linus Torvalds1da177e2005-04-16 15:20:36 -070015768 tg3_netif_start(tp);
15769
Michael Chanb9ec6c12006-07-25 16:37:27 -070015770out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015771 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015772
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015773 if (!err)
15774 tg3_phy_start(tp);
15775
Michael Chanb9ec6c12006-07-25 16:37:27 -070015776 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015777}
15778
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015779static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015780#define TG3_PM_OPS (&tg3_pm_ops)
15781
15782#else
15783
15784#define TG3_PM_OPS NULL
15785
15786#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015787
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015788/**
15789 * tg3_io_error_detected - called when PCI error is detected
15790 * @pdev: Pointer to PCI device
15791 * @state: The current pci connection state
15792 *
15793 * This function is called after a PCI bus error affecting
15794 * this device has been detected.
15795 */
15796static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
15797 pci_channel_state_t state)
15798{
15799 struct net_device *netdev = pci_get_drvdata(pdev);
15800 struct tg3 *tp = netdev_priv(netdev);
15801 pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
15802
15803 netdev_info(netdev, "PCI I/O error detected\n");
15804
15805 rtnl_lock();
15806
15807 if (!netif_running(netdev))
15808 goto done;
15809
15810 tg3_phy_stop(tp);
15811
15812 tg3_netif_stop(tp);
15813
15814 del_timer_sync(&tp->timer);
Joe Perches63c3a662011-04-26 08:12:10 +000015815 tg3_flag_clear(tp, RESTART_TIMER);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015816
15817 /* Want to make sure that the reset task doesn't run */
15818 cancel_work_sync(&tp->reset_task);
Joe Perches63c3a662011-04-26 08:12:10 +000015819 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
15820 tg3_flag_clear(tp, RESTART_TIMER);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015821
15822 netif_device_detach(netdev);
15823
15824 /* Clean up software state, even if MMIO is blocked */
15825 tg3_full_lock(tp, 0);
15826 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
15827 tg3_full_unlock(tp);
15828
15829done:
15830 if (state == pci_channel_io_perm_failure)
15831 err = PCI_ERS_RESULT_DISCONNECT;
15832 else
15833 pci_disable_device(pdev);
15834
15835 rtnl_unlock();
15836
15837 return err;
15838}
15839
15840/**
15841 * tg3_io_slot_reset - called after the pci bus has been reset.
15842 * @pdev: Pointer to PCI device
15843 *
15844 * Restart the card from scratch, as if from a cold-boot.
15845 * At this point, the card has exprienced a hard reset,
15846 * followed by fixups by BIOS, and has its config space
15847 * set up identically to what it was at cold boot.
15848 */
15849static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
15850{
15851 struct net_device *netdev = pci_get_drvdata(pdev);
15852 struct tg3 *tp = netdev_priv(netdev);
15853 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
15854 int err;
15855
15856 rtnl_lock();
15857
15858 if (pci_enable_device(pdev)) {
15859 netdev_err(netdev, "Cannot re-enable PCI device after reset.\n");
15860 goto done;
15861 }
15862
15863 pci_set_master(pdev);
15864 pci_restore_state(pdev);
15865 pci_save_state(pdev);
15866
15867 if (!netif_running(netdev)) {
15868 rc = PCI_ERS_RESULT_RECOVERED;
15869 goto done;
15870 }
15871
15872 err = tg3_power_up(tp);
Matt Carlsonbed98292011-07-13 09:27:29 +000015873 if (err)
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015874 goto done;
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015875
15876 rc = PCI_ERS_RESULT_RECOVERED;
15877
15878done:
15879 rtnl_unlock();
15880
15881 return rc;
15882}
15883
15884/**
15885 * tg3_io_resume - called when traffic can start flowing again.
15886 * @pdev: Pointer to PCI device
15887 *
15888 * This callback is called when the error recovery driver tells
15889 * us that its OK to resume normal operation.
15890 */
15891static void tg3_io_resume(struct pci_dev *pdev)
15892{
15893 struct net_device *netdev = pci_get_drvdata(pdev);
15894 struct tg3 *tp = netdev_priv(netdev);
15895 int err;
15896
15897 rtnl_lock();
15898
15899 if (!netif_running(netdev))
15900 goto done;
15901
15902 tg3_full_lock(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +000015903 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015904 err = tg3_restart_hw(tp, 1);
15905 tg3_full_unlock(tp);
15906 if (err) {
15907 netdev_err(netdev, "Cannot restart hardware after reset.\n");
15908 goto done;
15909 }
15910
15911 netif_device_attach(netdev);
15912
15913 tp->timer.expires = jiffies + tp->timer_offset;
15914 add_timer(&tp->timer);
15915
15916 tg3_netif_start(tp);
15917
15918 tg3_phy_start(tp);
15919
15920done:
15921 rtnl_unlock();
15922}
15923
15924static struct pci_error_handlers tg3_err_handler = {
15925 .error_detected = tg3_io_error_detected,
15926 .slot_reset = tg3_io_slot_reset,
15927 .resume = tg3_io_resume
15928};
15929
Linus Torvalds1da177e2005-04-16 15:20:36 -070015930static struct pci_driver tg3_driver = {
15931 .name = DRV_MODULE_NAME,
15932 .id_table = tg3_pci_tbl,
15933 .probe = tg3_init_one,
15934 .remove = __devexit_p(tg3_remove_one),
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015935 .err_handler = &tg3_err_handler,
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015936 .driver.pm = TG3_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070015937};
15938
15939static int __init tg3_init(void)
15940{
Jeff Garzik29917622006-08-19 17:48:59 -040015941 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015942}
15943
15944static void __exit tg3_cleanup(void)
15945{
15946 pci_unregister_driver(&tg3_driver);
15947}
15948
15949module_init(tg3_init);
15950module_exit(tg3_cleanup);