blob: 5af560966c7061720e44c58da96f3412c19b7422 [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 Carlsonf92d9dc12010-06-05 17:24:30 +0000624
625 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
626 regbase = TG3_APE_LOCK_GRANT;
627 else
628 regbase = TG3_APE_PER_LOCK_GRANT;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700629
630 /* Make sure the driver hasn't any stale locks. */
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000631 for (i = 0; i < 8; i++) {
632 if (i == TG3_APE_LOCK_GPIO)
633 continue;
Matt Carlsonf92d9dc12010-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 Carlsonf92d9dc12010-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 Carlsonf92d9dc12010-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 Carlsonf92d9dc12010-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 Carlsoncdd4e09d2009-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 }
3822 return 1;
3823}
3824
Matt Carlsonef167e22007-12-20 20:10:01 -08003825static int tg3_adv_1000T_flowctrl_ok(struct tg3 *tp, u32 *lcladv, u32 *rmtadv)
3826{
3827 u32 curadv, reqadv;
3828
3829 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
3830 return 1;
3831
3832 curadv = *lcladv & (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
3833 reqadv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
3834
3835 if (tp->link_config.active_duplex == DUPLEX_FULL) {
3836 if (curadv != reqadv)
3837 return 0;
3838
Joe Perches63c3a662011-04-26 08:12:10 +00003839 if (tg3_flag(tp, PAUSE_AUTONEG))
Matt Carlsonef167e22007-12-20 20:10:01 -08003840 tg3_readphy(tp, MII_LPA, rmtadv);
3841 } else {
3842 /* Reprogram the advertisement register, even if it
3843 * does not affect the current link. If the link
3844 * gets renegotiated in the future, we can save an
3845 * additional renegotiation cycle by advertising
3846 * it correctly in the first place.
3847 */
3848 if (curadv != reqadv) {
3849 *lcladv &= ~(ADVERTISE_PAUSE_CAP |
3850 ADVERTISE_PAUSE_ASYM);
3851 tg3_writephy(tp, MII_ADVERTISE, *lcladv | reqadv);
3852 }
3853 }
3854
3855 return 1;
3856}
3857
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
3859{
3860 int current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003861 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08003862 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 u16 current_speed;
3864 u8 current_duplex;
3865 int i, err;
3866
3867 tw32(MAC_EVENT, 0);
3868
3869 tw32_f(MAC_STATUS,
3870 (MAC_STATUS_SYNC_CHANGED |
3871 MAC_STATUS_CFG_CHANGED |
3872 MAC_STATUS_MI_COMPLETION |
3873 MAC_STATUS_LNKSTATE_CHANGED));
3874 udelay(40);
3875
Matt Carlson8ef21422008-05-02 16:47:53 -07003876 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
3877 tw32_f(MAC_MI_MODE,
3878 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
3879 udelay(80);
3880 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003882 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883
3884 /* Some third-party PHYs need to be reset on link going
3885 * down.
3886 */
3887 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3888 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
3889 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
3890 netif_carrier_ok(tp->dev)) {
3891 tg3_readphy(tp, MII_BMSR, &bmsr);
3892 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3893 !(bmsr & BMSR_LSTATUS))
3894 force_reset = 1;
3895 }
3896 if (force_reset)
3897 tg3_phy_reset(tp);
3898
Matt Carlson79eb6902010-02-17 15:17:03 +00003899 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 tg3_readphy(tp, MII_BMSR, &bmsr);
3901 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
Joe Perches63c3a662011-04-26 08:12:10 +00003902 !tg3_flag(tp, INIT_COMPLETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903 bmsr = 0;
3904
3905 if (!(bmsr & BMSR_LSTATUS)) {
3906 err = tg3_init_5401phy_dsp(tp);
3907 if (err)
3908 return err;
3909
3910 tg3_readphy(tp, MII_BMSR, &bmsr);
3911 for (i = 0; i < 1000; i++) {
3912 udelay(10);
3913 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3914 (bmsr & BMSR_LSTATUS)) {
3915 udelay(40);
3916 break;
3917 }
3918 }
3919
Matt Carlson79eb6902010-02-17 15:17:03 +00003920 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
3921 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922 !(bmsr & BMSR_LSTATUS) &&
3923 tp->link_config.active_speed == SPEED_1000) {
3924 err = tg3_phy_reset(tp);
3925 if (!err)
3926 err = tg3_init_5401phy_dsp(tp);
3927 if (err)
3928 return err;
3929 }
3930 }
3931 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3932 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
3933 /* 5701 {A0,B0} CRC bug workaround */
3934 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00003935 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
3936 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
3937 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 }
3939
3940 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003941 tg3_readphy(tp, MII_TG3_ISTAT, &val);
3942 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003944 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003946 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 tg3_writephy(tp, MII_TG3_IMASK, ~0);
3948
3949 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3950 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3951 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
3952 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3953 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
3954 else
3955 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
3956 }
3957
3958 current_link_up = 0;
3959 current_speed = SPEED_INVALID;
3960 current_duplex = DUPLEX_INVALID;
3961
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003962 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Matt Carlson15ee95c2011-04-20 07:57:40 +00003963 err = tg3_phy_auxctl_read(tp,
3964 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
3965 &val);
3966 if (!err && !(val & (1 << 10))) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003967 tg3_phy_auxctl_write(tp,
3968 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
3969 val | (1 << 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 goto relink;
3971 }
3972 }
3973
3974 bmsr = 0;
3975 for (i = 0; i < 100; i++) {
3976 tg3_readphy(tp, MII_BMSR, &bmsr);
3977 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3978 (bmsr & BMSR_LSTATUS))
3979 break;
3980 udelay(40);
3981 }
3982
3983 if (bmsr & BMSR_LSTATUS) {
3984 u32 aux_stat, bmcr;
3985
3986 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
3987 for (i = 0; i < 2000; i++) {
3988 udelay(10);
3989 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
3990 aux_stat)
3991 break;
3992 }
3993
3994 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
3995 &current_speed,
3996 &current_duplex);
3997
3998 bmcr = 0;
3999 for (i = 0; i < 200; i++) {
4000 tg3_readphy(tp, MII_BMCR, &bmcr);
4001 if (tg3_readphy(tp, MII_BMCR, &bmcr))
4002 continue;
4003 if (bmcr && bmcr != 0x7fff)
4004 break;
4005 udelay(10);
4006 }
4007
Matt Carlsonef167e22007-12-20 20:10:01 -08004008 lcl_adv = 0;
4009 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010
Matt Carlsonef167e22007-12-20 20:10:01 -08004011 tp->link_config.active_speed = current_speed;
4012 tp->link_config.active_duplex = current_duplex;
4013
4014 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4015 if ((bmcr & BMCR_ANENABLE) &&
4016 tg3_copper_is_advertising_all(tp,
4017 tp->link_config.advertising)) {
4018 if (tg3_adv_1000T_flowctrl_ok(tp, &lcl_adv,
4019 &rmt_adv))
4020 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 }
4022 } else {
4023 if (!(bmcr & BMCR_ANENABLE) &&
4024 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08004025 tp->link_config.duplex == current_duplex &&
4026 tp->link_config.flowctrl ==
4027 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 }
4030 }
4031
Matt Carlsonef167e22007-12-20 20:10:01 -08004032 if (current_link_up == 1 &&
4033 tp->link_config.active_duplex == DUPLEX_FULL)
4034 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 }
4036
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037relink:
Matt Carlson80096062010-08-02 11:26:06 +00004038 if (current_link_up == 0 || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 tg3_phy_copper_begin(tp);
4040
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004041 tg3_readphy(tp, MII_BMSR, &bmsr);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00004042 if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
4043 (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 current_link_up = 1;
4045 }
4046
4047 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
4048 if (current_link_up == 1) {
4049 if (tp->link_config.active_speed == SPEED_100 ||
4050 tp->link_config.active_speed == SPEED_10)
4051 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4052 else
4053 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004054 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00004055 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4056 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4058
4059 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4060 if (tp->link_config.active_duplex == DUPLEX_HALF)
4061 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4062
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004064 if (current_link_up == 1 &&
4065 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004067 else
4068 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 }
4070
4071 /* ??? Without this setting Netgear GA302T PHY does not
4072 * ??? send/receive packets...
4073 */
Matt Carlson79eb6902010-02-17 15:17:03 +00004074 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
4076 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
4077 tw32_f(MAC_MI_MODE, tp->mi_mode);
4078 udelay(80);
4079 }
4080
4081 tw32_f(MAC_MODE, tp->mac_mode);
4082 udelay(40);
4083
Matt Carlson52b02d02010-10-14 10:37:41 +00004084 tg3_phy_eee_adjust(tp, current_link_up);
4085
Joe Perches63c3a662011-04-26 08:12:10 +00004086 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 /* Polled via timer. */
4088 tw32_f(MAC_EVENT, 0);
4089 } else {
4090 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4091 }
4092 udelay(40);
4093
4094 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
4095 current_link_up == 1 &&
4096 tp->link_config.active_speed == SPEED_1000 &&
Joe Perches63c3a662011-04-26 08:12:10 +00004097 (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098 udelay(120);
4099 tw32_f(MAC_STATUS,
4100 (MAC_STATUS_SYNC_CHANGED |
4101 MAC_STATUS_CFG_CHANGED));
4102 udelay(40);
4103 tg3_write_mem(tp,
4104 NIC_SRAM_FIRMWARE_MBOX,
4105 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
4106 }
4107
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004108 /* Prevent send BD corruption. */
Joe Perches63c3a662011-04-26 08:12:10 +00004109 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004110 u16 oldlnkctl, newlnkctl;
4111
4112 pci_read_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00004113 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004114 &oldlnkctl);
4115 if (tp->link_config.active_speed == SPEED_100 ||
4116 tp->link_config.active_speed == SPEED_10)
4117 newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
4118 else
4119 newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
4120 if (newlnkctl != oldlnkctl)
4121 pci_write_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00004122 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004123 newlnkctl);
4124 }
4125
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 if (current_link_up != netif_carrier_ok(tp->dev)) {
4127 if (current_link_up)
4128 netif_carrier_on(tp->dev);
4129 else
4130 netif_carrier_off(tp->dev);
4131 tg3_link_report(tp);
4132 }
4133
4134 return 0;
4135}
4136
4137struct tg3_fiber_aneginfo {
4138 int state;
4139#define ANEG_STATE_UNKNOWN 0
4140#define ANEG_STATE_AN_ENABLE 1
4141#define ANEG_STATE_RESTART_INIT 2
4142#define ANEG_STATE_RESTART 3
4143#define ANEG_STATE_DISABLE_LINK_OK 4
4144#define ANEG_STATE_ABILITY_DETECT_INIT 5
4145#define ANEG_STATE_ABILITY_DETECT 6
4146#define ANEG_STATE_ACK_DETECT_INIT 7
4147#define ANEG_STATE_ACK_DETECT 8
4148#define ANEG_STATE_COMPLETE_ACK_INIT 9
4149#define ANEG_STATE_COMPLETE_ACK 10
4150#define ANEG_STATE_IDLE_DETECT_INIT 11
4151#define ANEG_STATE_IDLE_DETECT 12
4152#define ANEG_STATE_LINK_OK 13
4153#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
4154#define ANEG_STATE_NEXT_PAGE_WAIT 15
4155
4156 u32 flags;
4157#define MR_AN_ENABLE 0x00000001
4158#define MR_RESTART_AN 0x00000002
4159#define MR_AN_COMPLETE 0x00000004
4160#define MR_PAGE_RX 0x00000008
4161#define MR_NP_LOADED 0x00000010
4162#define MR_TOGGLE_TX 0x00000020
4163#define MR_LP_ADV_FULL_DUPLEX 0x00000040
4164#define MR_LP_ADV_HALF_DUPLEX 0x00000080
4165#define MR_LP_ADV_SYM_PAUSE 0x00000100
4166#define MR_LP_ADV_ASYM_PAUSE 0x00000200
4167#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
4168#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
4169#define MR_LP_ADV_NEXT_PAGE 0x00001000
4170#define MR_TOGGLE_RX 0x00002000
4171#define MR_NP_RX 0x00004000
4172
4173#define MR_LINK_OK 0x80000000
4174
4175 unsigned long link_time, cur_time;
4176
4177 u32 ability_match_cfg;
4178 int ability_match_count;
4179
4180 char ability_match, idle_match, ack_match;
4181
4182 u32 txconfig, rxconfig;
4183#define ANEG_CFG_NP 0x00000080
4184#define ANEG_CFG_ACK 0x00000040
4185#define ANEG_CFG_RF2 0x00000020
4186#define ANEG_CFG_RF1 0x00000010
4187#define ANEG_CFG_PS2 0x00000001
4188#define ANEG_CFG_PS1 0x00008000
4189#define ANEG_CFG_HD 0x00004000
4190#define ANEG_CFG_FD 0x00002000
4191#define ANEG_CFG_INVAL 0x00001f06
4192
4193};
4194#define ANEG_OK 0
4195#define ANEG_DONE 1
4196#define ANEG_TIMER_ENAB 2
4197#define ANEG_FAILED -1
4198
4199#define ANEG_STATE_SETTLE_TIME 10000
4200
4201static int tg3_fiber_aneg_smachine(struct tg3 *tp,
4202 struct tg3_fiber_aneginfo *ap)
4203{
Matt Carlson5be73b42007-12-20 20:09:29 -08004204 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205 unsigned long delta;
4206 u32 rx_cfg_reg;
4207 int ret;
4208
4209 if (ap->state == ANEG_STATE_UNKNOWN) {
4210 ap->rxconfig = 0;
4211 ap->link_time = 0;
4212 ap->cur_time = 0;
4213 ap->ability_match_cfg = 0;
4214 ap->ability_match_count = 0;
4215 ap->ability_match = 0;
4216 ap->idle_match = 0;
4217 ap->ack_match = 0;
4218 }
4219 ap->cur_time++;
4220
4221 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
4222 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
4223
4224 if (rx_cfg_reg != ap->ability_match_cfg) {
4225 ap->ability_match_cfg = rx_cfg_reg;
4226 ap->ability_match = 0;
4227 ap->ability_match_count = 0;
4228 } else {
4229 if (++ap->ability_match_count > 1) {
4230 ap->ability_match = 1;
4231 ap->ability_match_cfg = rx_cfg_reg;
4232 }
4233 }
4234 if (rx_cfg_reg & ANEG_CFG_ACK)
4235 ap->ack_match = 1;
4236 else
4237 ap->ack_match = 0;
4238
4239 ap->idle_match = 0;
4240 } else {
4241 ap->idle_match = 1;
4242 ap->ability_match_cfg = 0;
4243 ap->ability_match_count = 0;
4244 ap->ability_match = 0;
4245 ap->ack_match = 0;
4246
4247 rx_cfg_reg = 0;
4248 }
4249
4250 ap->rxconfig = rx_cfg_reg;
4251 ret = ANEG_OK;
4252
Matt Carlson33f401a2010-04-05 10:19:27 +00004253 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 case ANEG_STATE_UNKNOWN:
4255 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
4256 ap->state = ANEG_STATE_AN_ENABLE;
4257
4258 /* fallthru */
4259 case ANEG_STATE_AN_ENABLE:
4260 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
4261 if (ap->flags & MR_AN_ENABLE) {
4262 ap->link_time = 0;
4263 ap->cur_time = 0;
4264 ap->ability_match_cfg = 0;
4265 ap->ability_match_count = 0;
4266 ap->ability_match = 0;
4267 ap->idle_match = 0;
4268 ap->ack_match = 0;
4269
4270 ap->state = ANEG_STATE_RESTART_INIT;
4271 } else {
4272 ap->state = ANEG_STATE_DISABLE_LINK_OK;
4273 }
4274 break;
4275
4276 case ANEG_STATE_RESTART_INIT:
4277 ap->link_time = ap->cur_time;
4278 ap->flags &= ~(MR_NP_LOADED);
4279 ap->txconfig = 0;
4280 tw32(MAC_TX_AUTO_NEG, 0);
4281 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4282 tw32_f(MAC_MODE, tp->mac_mode);
4283 udelay(40);
4284
4285 ret = ANEG_TIMER_ENAB;
4286 ap->state = ANEG_STATE_RESTART;
4287
4288 /* fallthru */
4289 case ANEG_STATE_RESTART:
4290 delta = ap->cur_time - ap->link_time;
Matt Carlson859a5882010-04-05 10:19:28 +00004291 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a5882010-04-05 10:19:28 +00004293 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 break;
4296
4297 case ANEG_STATE_DISABLE_LINK_OK:
4298 ret = ANEG_DONE;
4299 break;
4300
4301 case ANEG_STATE_ABILITY_DETECT_INIT:
4302 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08004303 ap->txconfig = ANEG_CFG_FD;
4304 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
4305 if (flowctrl & ADVERTISE_1000XPAUSE)
4306 ap->txconfig |= ANEG_CFG_PS1;
4307 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
4308 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
4310 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4311 tw32_f(MAC_MODE, tp->mac_mode);
4312 udelay(40);
4313
4314 ap->state = ANEG_STATE_ABILITY_DETECT;
4315 break;
4316
4317 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a5882010-04-05 10:19:28 +00004318 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 break;
4321
4322 case ANEG_STATE_ACK_DETECT_INIT:
4323 ap->txconfig |= ANEG_CFG_ACK;
4324 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
4325 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4326 tw32_f(MAC_MODE, tp->mac_mode);
4327 udelay(40);
4328
4329 ap->state = ANEG_STATE_ACK_DETECT;
4330
4331 /* fallthru */
4332 case ANEG_STATE_ACK_DETECT:
4333 if (ap->ack_match != 0) {
4334 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
4335 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
4336 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
4337 } else {
4338 ap->state = ANEG_STATE_AN_ENABLE;
4339 }
4340 } else if (ap->ability_match != 0 &&
4341 ap->rxconfig == 0) {
4342 ap->state = ANEG_STATE_AN_ENABLE;
4343 }
4344 break;
4345
4346 case ANEG_STATE_COMPLETE_ACK_INIT:
4347 if (ap->rxconfig & ANEG_CFG_INVAL) {
4348 ret = ANEG_FAILED;
4349 break;
4350 }
4351 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
4352 MR_LP_ADV_HALF_DUPLEX |
4353 MR_LP_ADV_SYM_PAUSE |
4354 MR_LP_ADV_ASYM_PAUSE |
4355 MR_LP_ADV_REMOTE_FAULT1 |
4356 MR_LP_ADV_REMOTE_FAULT2 |
4357 MR_LP_ADV_NEXT_PAGE |
4358 MR_TOGGLE_RX |
4359 MR_NP_RX);
4360 if (ap->rxconfig & ANEG_CFG_FD)
4361 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
4362 if (ap->rxconfig & ANEG_CFG_HD)
4363 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
4364 if (ap->rxconfig & ANEG_CFG_PS1)
4365 ap->flags |= MR_LP_ADV_SYM_PAUSE;
4366 if (ap->rxconfig & ANEG_CFG_PS2)
4367 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
4368 if (ap->rxconfig & ANEG_CFG_RF1)
4369 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
4370 if (ap->rxconfig & ANEG_CFG_RF2)
4371 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
4372 if (ap->rxconfig & ANEG_CFG_NP)
4373 ap->flags |= MR_LP_ADV_NEXT_PAGE;
4374
4375 ap->link_time = ap->cur_time;
4376
4377 ap->flags ^= (MR_TOGGLE_TX);
4378 if (ap->rxconfig & 0x0008)
4379 ap->flags |= MR_TOGGLE_RX;
4380 if (ap->rxconfig & ANEG_CFG_NP)
4381 ap->flags |= MR_NP_RX;
4382 ap->flags |= MR_PAGE_RX;
4383
4384 ap->state = ANEG_STATE_COMPLETE_ACK;
4385 ret = ANEG_TIMER_ENAB;
4386 break;
4387
4388 case ANEG_STATE_COMPLETE_ACK:
4389 if (ap->ability_match != 0 &&
4390 ap->rxconfig == 0) {
4391 ap->state = ANEG_STATE_AN_ENABLE;
4392 break;
4393 }
4394 delta = ap->cur_time - ap->link_time;
4395 if (delta > ANEG_STATE_SETTLE_TIME) {
4396 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
4397 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
4398 } else {
4399 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
4400 !(ap->flags & MR_NP_RX)) {
4401 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
4402 } else {
4403 ret = ANEG_FAILED;
4404 }
4405 }
4406 }
4407 break;
4408
4409 case ANEG_STATE_IDLE_DETECT_INIT:
4410 ap->link_time = ap->cur_time;
4411 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
4412 tw32_f(MAC_MODE, tp->mac_mode);
4413 udelay(40);
4414
4415 ap->state = ANEG_STATE_IDLE_DETECT;
4416 ret = ANEG_TIMER_ENAB;
4417 break;
4418
4419 case ANEG_STATE_IDLE_DETECT:
4420 if (ap->ability_match != 0 &&
4421 ap->rxconfig == 0) {
4422 ap->state = ANEG_STATE_AN_ENABLE;
4423 break;
4424 }
4425 delta = ap->cur_time - ap->link_time;
4426 if (delta > ANEG_STATE_SETTLE_TIME) {
4427 /* XXX another gem from the Broadcom driver :( */
4428 ap->state = ANEG_STATE_LINK_OK;
4429 }
4430 break;
4431
4432 case ANEG_STATE_LINK_OK:
4433 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
4434 ret = ANEG_DONE;
4435 break;
4436
4437 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
4438 /* ??? unimplemented */
4439 break;
4440
4441 case ANEG_STATE_NEXT_PAGE_WAIT:
4442 /* ??? unimplemented */
4443 break;
4444
4445 default:
4446 ret = ANEG_FAILED;
4447 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449
4450 return ret;
4451}
4452
Matt Carlson5be73b42007-12-20 20:09:29 -08004453static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454{
4455 int res = 0;
4456 struct tg3_fiber_aneginfo aninfo;
4457 int status = ANEG_FAILED;
4458 unsigned int tick;
4459 u32 tmp;
4460
4461 tw32_f(MAC_TX_AUTO_NEG, 0);
4462
4463 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
4464 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
4465 udelay(40);
4466
4467 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
4468 udelay(40);
4469
4470 memset(&aninfo, 0, sizeof(aninfo));
4471 aninfo.flags |= MR_AN_ENABLE;
4472 aninfo.state = ANEG_STATE_UNKNOWN;
4473 aninfo.cur_time = 0;
4474 tick = 0;
4475 while (++tick < 195000) {
4476 status = tg3_fiber_aneg_smachine(tp, &aninfo);
4477 if (status == ANEG_DONE || status == ANEG_FAILED)
4478 break;
4479
4480 udelay(1);
4481 }
4482
4483 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
4484 tw32_f(MAC_MODE, tp->mac_mode);
4485 udelay(40);
4486
Matt Carlson5be73b42007-12-20 20:09:29 -08004487 *txflags = aninfo.txconfig;
4488 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489
4490 if (status == ANEG_DONE &&
4491 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
4492 MR_LP_ADV_FULL_DUPLEX)))
4493 res = 1;
4494
4495 return res;
4496}
4497
4498static void tg3_init_bcm8002(struct tg3 *tp)
4499{
4500 u32 mac_status = tr32(MAC_STATUS);
4501 int i;
4502
4503 /* Reset when initting first time or we have a link. */
Joe Perches63c3a662011-04-26 08:12:10 +00004504 if (tg3_flag(tp, INIT_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 !(mac_status & MAC_STATUS_PCS_SYNCED))
4506 return;
4507
4508 /* Set PLL lock range. */
4509 tg3_writephy(tp, 0x16, 0x8007);
4510
4511 /* SW reset */
4512 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
4513
4514 /* Wait for reset to complete. */
4515 /* XXX schedule_timeout() ... */
4516 for (i = 0; i < 500; i++)
4517 udelay(10);
4518
4519 /* Config mode; select PMA/Ch 1 regs. */
4520 tg3_writephy(tp, 0x10, 0x8411);
4521
4522 /* Enable auto-lock and comdet, select txclk for tx. */
4523 tg3_writephy(tp, 0x11, 0x0a10);
4524
4525 tg3_writephy(tp, 0x18, 0x00a0);
4526 tg3_writephy(tp, 0x16, 0x41ff);
4527
4528 /* Assert and deassert POR. */
4529 tg3_writephy(tp, 0x13, 0x0400);
4530 udelay(40);
4531 tg3_writephy(tp, 0x13, 0x0000);
4532
4533 tg3_writephy(tp, 0x11, 0x0a50);
4534 udelay(40);
4535 tg3_writephy(tp, 0x11, 0x0a10);
4536
4537 /* Wait for signal to stabilize */
4538 /* XXX schedule_timeout() ... */
4539 for (i = 0; i < 15000; i++)
4540 udelay(10);
4541
4542 /* Deselect the channel register so we can read the PHYID
4543 * later.
4544 */
4545 tg3_writephy(tp, 0x10, 0x8011);
4546}
4547
4548static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
4549{
Matt Carlson82cd3d12007-12-20 20:09:00 -08004550 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 u32 sg_dig_ctrl, sg_dig_status;
4552 u32 serdes_cfg, expected_sg_dig_ctrl;
4553 int workaround, port_a;
4554 int current_link_up;
4555
4556 serdes_cfg = 0;
4557 expected_sg_dig_ctrl = 0;
4558 workaround = 0;
4559 port_a = 1;
4560 current_link_up = 0;
4561
4562 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
4563 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
4564 workaround = 1;
4565 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
4566 port_a = 0;
4567
4568 /* preserve bits 0-11,13,14 for signal pre-emphasis */
4569 /* preserve bits 20-23 for voltage regulator */
4570 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
4571 }
4572
4573 sg_dig_ctrl = tr32(SG_DIG_CTRL);
4574
4575 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004576 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 if (workaround) {
4578 u32 val = serdes_cfg;
4579
4580 if (port_a)
4581 val |= 0xc010000;
4582 else
4583 val |= 0x4010000;
4584 tw32_f(MAC_SERDES_CFG, val);
4585 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004586
4587 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588 }
4589 if (mac_status & MAC_STATUS_PCS_SYNCED) {
4590 tg3_setup_flow_control(tp, 0, 0);
4591 current_link_up = 1;
4592 }
4593 goto out;
4594 }
4595
4596 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004597 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598
Matt Carlson82cd3d12007-12-20 20:09:00 -08004599 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
4600 if (flowctrl & ADVERTISE_1000XPAUSE)
4601 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
4602 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
4603 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604
4605 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004606 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07004607 tp->serdes_counter &&
4608 ((mac_status & (MAC_STATUS_PCS_SYNCED |
4609 MAC_STATUS_RCVD_CFG)) ==
4610 MAC_STATUS_PCS_SYNCED)) {
4611 tp->serdes_counter--;
4612 current_link_up = 1;
4613 goto out;
4614 }
4615restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616 if (workaround)
4617 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004618 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619 udelay(5);
4620 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
4621
Michael Chan3d3ebe72006-09-27 15:59:15 -07004622 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004623 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
4625 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07004626 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627 mac_status = tr32(MAC_STATUS);
4628
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004629 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08004631 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632
Matt Carlson82cd3d12007-12-20 20:09:00 -08004633 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
4634 local_adv |= ADVERTISE_1000XPAUSE;
4635 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
4636 local_adv |= ADVERTISE_1000XPSE_ASYM;
4637
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004638 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08004639 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004640 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08004641 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642
4643 tg3_setup_flow_control(tp, local_adv, remote_adv);
4644 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004645 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004646 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004647 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07004648 if (tp->serdes_counter)
4649 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650 else {
4651 if (workaround) {
4652 u32 val = serdes_cfg;
4653
4654 if (port_a)
4655 val |= 0xc010000;
4656 else
4657 val |= 0x4010000;
4658
4659 tw32_f(MAC_SERDES_CFG, val);
4660 }
4661
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004662 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 udelay(40);
4664
4665 /* Link parallel detection - link is up */
4666 /* only if we have PCS_SYNC and not */
4667 /* receiving config code words */
4668 mac_status = tr32(MAC_STATUS);
4669 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
4670 !(mac_status & MAC_STATUS_RCVD_CFG)) {
4671 tg3_setup_flow_control(tp, 0, 0);
4672 current_link_up = 1;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004673 tp->phy_flags |=
4674 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004675 tp->serdes_counter =
4676 SERDES_PARALLEL_DET_TIMEOUT;
4677 } else
4678 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 }
4680 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07004681 } else {
4682 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004683 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 }
4685
4686out:
4687 return current_link_up;
4688}
4689
4690static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
4691{
4692 int current_link_up = 0;
4693
Michael Chan5cf64b82007-05-05 12:11:21 -07004694 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696
4697 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08004698 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004700
Matt Carlson5be73b42007-12-20 20:09:29 -08004701 if (fiber_autoneg(tp, &txflags, &rxflags)) {
4702 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703
Matt Carlson5be73b42007-12-20 20:09:29 -08004704 if (txflags & ANEG_CFG_PS1)
4705 local_adv |= ADVERTISE_1000XPAUSE;
4706 if (txflags & ANEG_CFG_PS2)
4707 local_adv |= ADVERTISE_1000XPSE_ASYM;
4708
4709 if (rxflags & MR_LP_ADV_SYM_PAUSE)
4710 remote_adv |= LPA_1000XPAUSE;
4711 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
4712 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713
4714 tg3_setup_flow_control(tp, local_adv, remote_adv);
4715
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716 current_link_up = 1;
4717 }
4718 for (i = 0; i < 30; i++) {
4719 udelay(20);
4720 tw32_f(MAC_STATUS,
4721 (MAC_STATUS_SYNC_CHANGED |
4722 MAC_STATUS_CFG_CHANGED));
4723 udelay(40);
4724 if ((tr32(MAC_STATUS) &
4725 (MAC_STATUS_SYNC_CHANGED |
4726 MAC_STATUS_CFG_CHANGED)) == 0)
4727 break;
4728 }
4729
4730 mac_status = tr32(MAC_STATUS);
4731 if (current_link_up == 0 &&
4732 (mac_status & MAC_STATUS_PCS_SYNCED) &&
4733 !(mac_status & MAC_STATUS_RCVD_CFG))
4734 current_link_up = 1;
4735 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08004736 tg3_setup_flow_control(tp, 0, 0);
4737
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738 /* Forcing 1000FD link up. */
4739 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740
4741 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
4742 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004743
4744 tw32_f(MAC_MODE, tp->mac_mode);
4745 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746 }
4747
4748out:
4749 return current_link_up;
4750}
4751
4752static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
4753{
4754 u32 orig_pause_cfg;
4755 u16 orig_active_speed;
4756 u8 orig_active_duplex;
4757 u32 mac_status;
4758 int current_link_up;
4759 int i;
4760
Matt Carlson8d018622007-12-20 20:05:44 -08004761 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762 orig_active_speed = tp->link_config.active_speed;
4763 orig_active_duplex = tp->link_config.active_duplex;
4764
Joe Perches63c3a662011-04-26 08:12:10 +00004765 if (!tg3_flag(tp, HW_AUTONEG) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 netif_carrier_ok(tp->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00004767 tg3_flag(tp, INIT_COMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768 mac_status = tr32(MAC_STATUS);
4769 mac_status &= (MAC_STATUS_PCS_SYNCED |
4770 MAC_STATUS_SIGNAL_DET |
4771 MAC_STATUS_CFG_CHANGED |
4772 MAC_STATUS_RCVD_CFG);
4773 if (mac_status == (MAC_STATUS_PCS_SYNCED |
4774 MAC_STATUS_SIGNAL_DET)) {
4775 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4776 MAC_STATUS_CFG_CHANGED));
4777 return 0;
4778 }
4779 }
4780
4781 tw32_f(MAC_TX_AUTO_NEG, 0);
4782
4783 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
4784 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
4785 tw32_f(MAC_MODE, tp->mac_mode);
4786 udelay(40);
4787
Matt Carlson79eb6902010-02-17 15:17:03 +00004788 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789 tg3_init_bcm8002(tp);
4790
4791 /* Enable link change event even when serdes polling. */
4792 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4793 udelay(40);
4794
4795 current_link_up = 0;
4796 mac_status = tr32(MAC_STATUS);
4797
Joe Perches63c3a662011-04-26 08:12:10 +00004798 if (tg3_flag(tp, HW_AUTONEG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
4800 else
4801 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
4802
Matt Carlson898a56f2009-08-28 14:02:40 +00004803 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00004805 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806
4807 for (i = 0; i < 100; i++) {
4808 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4809 MAC_STATUS_CFG_CHANGED));
4810 udelay(5);
4811 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07004812 MAC_STATUS_CFG_CHANGED |
4813 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814 break;
4815 }
4816
4817 mac_status = tr32(MAC_STATUS);
4818 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
4819 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004820 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
4821 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822 tw32_f(MAC_MODE, (tp->mac_mode |
4823 MAC_MODE_SEND_CONFIGS));
4824 udelay(1);
4825 tw32_f(MAC_MODE, tp->mac_mode);
4826 }
4827 }
4828
4829 if (current_link_up == 1) {
4830 tp->link_config.active_speed = SPEED_1000;
4831 tp->link_config.active_duplex = DUPLEX_FULL;
4832 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4833 LED_CTRL_LNKLED_OVERRIDE |
4834 LED_CTRL_1000MBPS_ON));
4835 } else {
4836 tp->link_config.active_speed = SPEED_INVALID;
4837 tp->link_config.active_duplex = DUPLEX_INVALID;
4838 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4839 LED_CTRL_LNKLED_OVERRIDE |
4840 LED_CTRL_TRAFFIC_OVERRIDE));
4841 }
4842
4843 if (current_link_up != netif_carrier_ok(tp->dev)) {
4844 if (current_link_up)
4845 netif_carrier_on(tp->dev);
4846 else
4847 netif_carrier_off(tp->dev);
4848 tg3_link_report(tp);
4849 } else {
Matt Carlson8d018622007-12-20 20:05:44 -08004850 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851 if (orig_pause_cfg != now_pause_cfg ||
4852 orig_active_speed != tp->link_config.active_speed ||
4853 orig_active_duplex != tp->link_config.active_duplex)
4854 tg3_link_report(tp);
4855 }
4856
4857 return 0;
4858}
4859
Michael Chan747e8f82005-07-25 12:33:22 -07004860static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
4861{
4862 int current_link_up, err = 0;
4863 u32 bmsr, bmcr;
4864 u16 current_speed;
4865 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08004866 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07004867
4868 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4869 tw32_f(MAC_MODE, tp->mac_mode);
4870 udelay(40);
4871
4872 tw32(MAC_EVENT, 0);
4873
4874 tw32_f(MAC_STATUS,
4875 (MAC_STATUS_SYNC_CHANGED |
4876 MAC_STATUS_CFG_CHANGED |
4877 MAC_STATUS_MI_COMPLETION |
4878 MAC_STATUS_LNKSTATE_CHANGED));
4879 udelay(40);
4880
4881 if (force_reset)
4882 tg3_phy_reset(tp);
4883
4884 current_link_up = 0;
4885 current_speed = SPEED_INVALID;
4886 current_duplex = DUPLEX_INVALID;
4887
4888 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4889 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004890 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
4891 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4892 bmsr |= BMSR_LSTATUS;
4893 else
4894 bmsr &= ~BMSR_LSTATUS;
4895 }
Michael Chan747e8f82005-07-25 12:33:22 -07004896
4897 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
4898
4899 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004900 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004901 /* do nothing, just check for link up at the end */
4902 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4903 u32 adv, new_adv;
4904
4905 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4906 new_adv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
4907 ADVERTISE_1000XPAUSE |
4908 ADVERTISE_1000XPSE_ASYM |
4909 ADVERTISE_SLCT);
4910
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004911 new_adv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Michael Chan747e8f82005-07-25 12:33:22 -07004912
4913 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
4914 new_adv |= ADVERTISE_1000XHALF;
4915 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
4916 new_adv |= ADVERTISE_1000XFULL;
4917
4918 if ((new_adv != adv) || !(bmcr & BMCR_ANENABLE)) {
4919 tg3_writephy(tp, MII_ADVERTISE, new_adv);
4920 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
4921 tg3_writephy(tp, MII_BMCR, bmcr);
4922
4923 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07004924 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004925 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004926
4927 return err;
4928 }
4929 } else {
4930 u32 new_bmcr;
4931
4932 bmcr &= ~BMCR_SPEED1000;
4933 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
4934
4935 if (tp->link_config.duplex == DUPLEX_FULL)
4936 new_bmcr |= BMCR_FULLDPLX;
4937
4938 if (new_bmcr != bmcr) {
4939 /* BMCR_SPEED1000 is a reserved bit that needs
4940 * to be set on write.
4941 */
4942 new_bmcr |= BMCR_SPEED1000;
4943
4944 /* Force a linkdown */
4945 if (netif_carrier_ok(tp->dev)) {
4946 u32 adv;
4947
4948 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4949 adv &= ~(ADVERTISE_1000XFULL |
4950 ADVERTISE_1000XHALF |
4951 ADVERTISE_SLCT);
4952 tg3_writephy(tp, MII_ADVERTISE, adv);
4953 tg3_writephy(tp, MII_BMCR, bmcr |
4954 BMCR_ANRESTART |
4955 BMCR_ANENABLE);
4956 udelay(10);
4957 netif_carrier_off(tp->dev);
4958 }
4959 tg3_writephy(tp, MII_BMCR, new_bmcr);
4960 bmcr = new_bmcr;
4961 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4962 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004963 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
4964 ASIC_REV_5714) {
4965 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4966 bmsr |= BMSR_LSTATUS;
4967 else
4968 bmsr &= ~BMSR_LSTATUS;
4969 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004970 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004971 }
4972 }
4973
4974 if (bmsr & BMSR_LSTATUS) {
4975 current_speed = SPEED_1000;
4976 current_link_up = 1;
4977 if (bmcr & BMCR_FULLDPLX)
4978 current_duplex = DUPLEX_FULL;
4979 else
4980 current_duplex = DUPLEX_HALF;
4981
Matt Carlsonef167e22007-12-20 20:10:01 -08004982 local_adv = 0;
4983 remote_adv = 0;
4984
Michael Chan747e8f82005-07-25 12:33:22 -07004985 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08004986 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07004987
4988 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
4989 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
4990 common = local_adv & remote_adv;
4991 if (common & (ADVERTISE_1000XHALF |
4992 ADVERTISE_1000XFULL)) {
4993 if (common & ADVERTISE_1000XFULL)
4994 current_duplex = DUPLEX_FULL;
4995 else
4996 current_duplex = DUPLEX_HALF;
Joe Perches63c3a662011-04-26 08:12:10 +00004997 } else if (!tg3_flag(tp, 5780_CLASS)) {
Matt Carlson57d8b882010-06-05 17:24:35 +00004998 /* Link is up via parallel detect */
Matt Carlson859a5882010-04-05 10:19:28 +00004999 } else {
Michael Chan747e8f82005-07-25 12:33:22 -07005000 current_link_up = 0;
Matt Carlson859a5882010-04-05 10:19:28 +00005001 }
Michael Chan747e8f82005-07-25 12:33:22 -07005002 }
5003 }
5004
Matt Carlsonef167e22007-12-20 20:10:01 -08005005 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
5006 tg3_setup_flow_control(tp, local_adv, remote_adv);
5007
Michael Chan747e8f82005-07-25 12:33:22 -07005008 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
5009 if (tp->link_config.active_duplex == DUPLEX_HALF)
5010 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
5011
5012 tw32_f(MAC_MODE, tp->mac_mode);
5013 udelay(40);
5014
5015 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5016
5017 tp->link_config.active_speed = current_speed;
5018 tp->link_config.active_duplex = current_duplex;
5019
5020 if (current_link_up != netif_carrier_ok(tp->dev)) {
5021 if (current_link_up)
5022 netif_carrier_on(tp->dev);
5023 else {
5024 netif_carrier_off(tp->dev);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005025 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005026 }
5027 tg3_link_report(tp);
5028 }
5029 return err;
5030}
5031
5032static void tg3_serdes_parallel_detect(struct tg3 *tp)
5033{
Michael Chan3d3ebe72006-09-27 15:59:15 -07005034 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07005035 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07005036 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07005037 return;
5038 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005039
Michael Chan747e8f82005-07-25 12:33:22 -07005040 if (!netif_carrier_ok(tp->dev) &&
5041 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
5042 u32 bmcr;
5043
5044 tg3_readphy(tp, MII_BMCR, &bmcr);
5045 if (bmcr & BMCR_ANENABLE) {
5046 u32 phy1, phy2;
5047
5048 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005049 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
5050 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07005051
5052 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005053 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5054 MII_TG3_DSP_EXP1_INT_STAT);
5055 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
5056 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005057
5058 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
5059 /* We have signal detect and not receiving
5060 * config code words, link is up by parallel
5061 * detection.
5062 */
5063
5064 bmcr &= ~BMCR_ANENABLE;
5065 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
5066 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005067 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005068 }
5069 }
Matt Carlson859a5882010-04-05 10:19:28 +00005070 } else if (netif_carrier_ok(tp->dev) &&
5071 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005072 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005073 u32 phy2;
5074
5075 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005076 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5077 MII_TG3_DSP_EXP1_INT_STAT);
5078 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005079 if (phy2 & 0x20) {
5080 u32 bmcr;
5081
5082 /* Config code words received, turn on autoneg. */
5083 tg3_readphy(tp, MII_BMCR, &bmcr);
5084 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
5085
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005086 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005087
5088 }
5089 }
5090}
5091
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092static int tg3_setup_phy(struct tg3 *tp, int force_reset)
5093{
Matt Carlsonf2096f92011-04-05 14:22:48 +00005094 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 int err;
5096
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005097 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005099 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07005100 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a5882010-04-05 10:19:28 +00005101 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103
Matt Carlsonbcb37f62008-11-03 16:52:09 -08005104 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00005105 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08005106
5107 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
5108 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
5109 scale = 65;
5110 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
5111 scale = 6;
5112 else
5113 scale = 12;
5114
5115 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
5116 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
5117 tw32(GRC_MISC_CFG, val);
5118 }
5119
Matt Carlsonf2096f92011-04-05 14:22:48 +00005120 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
5121 (6 << TX_LENGTHS_IPG_SHIFT);
5122 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
5123 val |= tr32(MAC_TX_LENGTHS) &
5124 (TX_LENGTHS_JMB_FRM_LEN_MSK |
5125 TX_LENGTHS_CNT_DWN_VAL_MSK);
5126
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127 if (tp->link_config.active_speed == SPEED_1000 &&
5128 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00005129 tw32(MAC_TX_LENGTHS, val |
5130 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00005132 tw32(MAC_TX_LENGTHS, val |
5133 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134
Joe Perches63c3a662011-04-26 08:12:10 +00005135 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136 if (netif_carrier_ok(tp->dev)) {
5137 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07005138 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139 } else {
5140 tw32(HOSTCC_STAT_COAL_TICKS, 0);
5141 }
5142 }
5143
Joe Perches63c3a662011-04-26 08:12:10 +00005144 if (tg3_flag(tp, ASPM_WORKAROUND)) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00005145 val = tr32(PCIE_PWR_MGMT_THRESH);
Matt Carlson8ed5d972007-05-07 00:25:49 -07005146 if (!netif_carrier_ok(tp->dev))
5147 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
5148 tp->pwrmgmt_thresh;
5149 else
5150 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
5151 tw32(PCIE_PWR_MGMT_THRESH, val);
5152 }
5153
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154 return err;
5155}
5156
Matt Carlson66cfd1b2010-09-30 10:34:30 +00005157static inline int tg3_irq_sync(struct tg3 *tp)
5158{
5159 return tp->irq_sync;
5160}
5161
Matt Carlson97bd8e42011-04-13 11:05:04 +00005162static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
5163{
5164 int i;
5165
5166 dst = (u32 *)((u8 *)dst + off);
5167 for (i = 0; i < len; i += sizeof(u32))
5168 *dst++ = tr32(off + i);
5169}
5170
5171static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
5172{
5173 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
5174 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
5175 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
5176 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
5177 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
5178 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
5179 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
5180 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
5181 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
5182 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
5183 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
5184 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
5185 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
5186 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
5187 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
5188 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
5189 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
5190 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
5191 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
5192
Joe Perches63c3a662011-04-26 08:12:10 +00005193 if (tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson97bd8e42011-04-13 11:05:04 +00005194 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
5195
5196 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
5197 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
5198 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
5199 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
5200 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
5201 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
5202 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
5203 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
5204
Joe Perches63c3a662011-04-26 08:12:10 +00005205 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00005206 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
5207 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
5208 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
5209 }
5210
5211 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
5212 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
5213 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
5214 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
5215 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
5216
Joe Perches63c3a662011-04-26 08:12:10 +00005217 if (tg3_flag(tp, NVRAM))
Matt Carlson97bd8e42011-04-13 11:05:04 +00005218 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
5219}
5220
5221static void tg3_dump_state(struct tg3 *tp)
5222{
5223 int i;
5224 u32 *regs;
5225
5226 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
5227 if (!regs) {
5228 netdev_err(tp->dev, "Failed allocating register dump buffer\n");
5229 return;
5230 }
5231
Joe Perches63c3a662011-04-26 08:12:10 +00005232 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00005233 /* Read up to but not including private PCI registers */
5234 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
5235 regs[i / sizeof(u32)] = tr32(i);
5236 } else
5237 tg3_dump_legacy_regs(tp, regs);
5238
5239 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
5240 if (!regs[i + 0] && !regs[i + 1] &&
5241 !regs[i + 2] && !regs[i + 3])
5242 continue;
5243
5244 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
5245 i * 4,
5246 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
5247 }
5248
5249 kfree(regs);
5250
5251 for (i = 0; i < tp->irq_cnt; i++) {
5252 struct tg3_napi *tnapi = &tp->napi[i];
5253
5254 /* SW status block */
5255 netdev_err(tp->dev,
5256 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
5257 i,
5258 tnapi->hw_status->status,
5259 tnapi->hw_status->status_tag,
5260 tnapi->hw_status->rx_jumbo_consumer,
5261 tnapi->hw_status->rx_consumer,
5262 tnapi->hw_status->rx_mini_consumer,
5263 tnapi->hw_status->idx[0].rx_producer,
5264 tnapi->hw_status->idx[0].tx_consumer);
5265
5266 netdev_err(tp->dev,
5267 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
5268 i,
5269 tnapi->last_tag, tnapi->last_irq_tag,
5270 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
5271 tnapi->rx_rcb_ptr,
5272 tnapi->prodring.rx_std_prod_idx,
5273 tnapi->prodring.rx_std_cons_idx,
5274 tnapi->prodring.rx_jmb_prod_idx,
5275 tnapi->prodring.rx_jmb_cons_idx);
5276 }
5277}
5278
Michael Chandf3e6542006-05-26 17:48:07 -07005279/* This is called whenever we suspect that the system chipset is re-
5280 * ordering the sequence of MMIO to the tx send mailbox. The symptom
5281 * is bogus tx completions. We try to recover by setting the
5282 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
5283 * in the workqueue.
5284 */
5285static void tg3_tx_recover(struct tg3 *tp)
5286{
Joe Perches63c3a662011-04-26 08:12:10 +00005287 BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
Michael Chandf3e6542006-05-26 17:48:07 -07005288 tp->write32_tx_mbox == tg3_write_indirect_mbox);
5289
Matt Carlson5129c3a2010-04-05 10:19:23 +00005290 netdev_warn(tp->dev,
5291 "The system may be re-ordering memory-mapped I/O "
5292 "cycles to the network device, attempting to recover. "
5293 "Please report the problem to the driver maintainer "
5294 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07005295
5296 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00005297 tg3_flag_set(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07005298 spin_unlock(&tp->lock);
5299}
5300
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005301static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07005302{
Matt Carlsonf65aac12010-08-02 11:26:03 +00005303 /* Tell compiler to fetch tx indices from memory. */
5304 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005305 return tnapi->tx_pending -
5306 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07005307}
5308
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309/* Tigon3 never reports partial packet sends. So we do not
5310 * need special logic to handle SKBs that have not had all
5311 * of their frags sent yet, like SunGEM does.
5312 */
Matt Carlson17375d22009-08-28 14:02:18 +00005313static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314{
Matt Carlson17375d22009-08-28 14:02:18 +00005315 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005316 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005317 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005318 struct netdev_queue *txq;
5319 int index = tnapi - tp->napi;
5320
Joe Perches63c3a662011-04-26 08:12:10 +00005321 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005322 index--;
5323
5324 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325
5326 while (sw_idx != hw_idx) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00005327 struct tg3_tx_ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07005329 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330
Michael Chandf3e6542006-05-26 17:48:07 -07005331 if (unlikely(skb == NULL)) {
5332 tg3_tx_recover(tp);
5333 return;
5334 }
5335
Alexander Duyckf4188d82009-12-02 16:48:38 +00005336 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005337 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005338 skb_headlen(skb),
5339 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340
5341 ri->skb = NULL;
5342
Matt Carlsone01ee142011-07-27 14:20:50 +00005343 while (ri->fragmented) {
5344 ri->fragmented = false;
5345 sw_idx = NEXT_TX(sw_idx);
5346 ri = &tnapi->tx_buffers[sw_idx];
5347 }
5348
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349 sw_idx = NEXT_TX(sw_idx);
5350
5351 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005352 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07005353 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
5354 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005355
5356 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005357 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005358 skb_shinfo(skb)->frags[i].size,
5359 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00005360
5361 while (ri->fragmented) {
5362 ri->fragmented = false;
5363 sw_idx = NEXT_TX(sw_idx);
5364 ri = &tnapi->tx_buffers[sw_idx];
5365 }
5366
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367 sw_idx = NEXT_TX(sw_idx);
5368 }
5369
David S. Millerf47c11e2005-06-24 20:18:35 -07005370 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07005371
5372 if (unlikely(tx_bug)) {
5373 tg3_tx_recover(tp);
5374 return;
5375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376 }
5377
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005378 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379
Michael Chan1b2a7202006-08-07 21:46:02 -07005380 /* Need to make the tx_cons update visible to tg3_start_xmit()
5381 * before checking for netif_queue_stopped(). Without the
5382 * memory barrier, there is a small possibility that tg3_start_xmit()
5383 * will miss it and cause the queue to be stopped forever.
5384 */
5385 smp_mb();
5386
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005387 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005388 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005389 __netif_tx_lock(txq, smp_processor_id());
5390 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005391 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005392 netif_tx_wake_queue(txq);
5393 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07005394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395}
5396
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005397static void tg3_rx_skb_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
5398{
5399 if (!ri->skb)
5400 return;
5401
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005402 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005403 map_sz, PCI_DMA_FROMDEVICE);
5404 dev_kfree_skb_any(ri->skb);
5405 ri->skb = NULL;
5406}
5407
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408/* Returns size of skb allocated or < 0 on error.
5409 *
5410 * We only need to fill in the address because the other members
5411 * of the RX descriptor are invariant, see tg3_init_rings.
5412 *
5413 * Note the purposeful assymetry of cpu vs. chip accesses. For
5414 * posting buffers we only dirty the first cache line of the RX
5415 * descriptor (containing the address). Whereas for the RX status
5416 * buffers the cpu only reads the last cacheline of the RX descriptor
5417 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
5418 */
Matt Carlson86b21e52009-11-13 13:03:45 +00005419static int tg3_alloc_rx_skb(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Matt Carlsona3896162009-11-13 13:03:44 +00005420 u32 opaque_key, u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421{
5422 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00005423 struct ring_info *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424 struct sk_buff *skb;
5425 dma_addr_t mapping;
5426 int skb_size, dest_idx;
5427
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428 switch (opaque_key) {
5429 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00005430 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00005431 desc = &tpr->rx_std[dest_idx];
5432 map = &tpr->rx_std_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00005433 skb_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434 break;
5435
5436 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00005437 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00005438 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00005439 map = &tpr->rx_jmb_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00005440 skb_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441 break;
5442
5443 default:
5444 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446
5447 /* Do not overwrite any of the map or rp information
5448 * until we are sure we can commit to a new buffer.
5449 *
5450 * Callers depend upon this behavior and assume that
5451 * we leave everything unchanged if we fail.
5452 */
Matt Carlson81389f52011-08-31 11:44:49 +00005453 skb = netdev_alloc_skb(tp->dev, skb_size + TG3_RX_OFFSET(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454 if (skb == NULL)
5455 return -ENOMEM;
5456
Matt Carlson81389f52011-08-31 11:44:49 +00005457 skb_reserve(skb, TG3_RX_OFFSET(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458
Matt Carlson287be122009-08-28 13:58:46 +00005459 mapping = pci_map_single(tp->pdev, skb->data, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460 PCI_DMA_FROMDEVICE);
Matt Carlsona21771d2009-11-02 14:25:31 +00005461 if (pci_dma_mapping_error(tp->pdev, mapping)) {
5462 dev_kfree_skb(skb);
5463 return -EIO;
5464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465
5466 map->skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005467 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469 desc->addr_hi = ((u64)mapping >> 32);
5470 desc->addr_lo = ((u64)mapping & 0xffffffff);
5471
5472 return skb_size;
5473}
5474
5475/* We only need to move over in the address because the other
5476 * members of the RX descriptor are invariant. See notes above
5477 * tg3_alloc_rx_skb for full details.
5478 */
Matt Carlsona3896162009-11-13 13:03:44 +00005479static void tg3_recycle_rx(struct tg3_napi *tnapi,
5480 struct tg3_rx_prodring_set *dpr,
5481 u32 opaque_key, int src_idx,
5482 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483{
Matt Carlson17375d22009-08-28 14:02:18 +00005484 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
5486 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00005487 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005488 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489
5490 switch (opaque_key) {
5491 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00005492 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00005493 dest_desc = &dpr->rx_std[dest_idx];
5494 dest_map = &dpr->rx_std_buffers[dest_idx];
5495 src_desc = &spr->rx_std[src_idx];
5496 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497 break;
5498
5499 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00005500 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00005501 dest_desc = &dpr->rx_jmb[dest_idx].std;
5502 dest_map = &dpr->rx_jmb_buffers[dest_idx];
5503 src_desc = &spr->rx_jmb[src_idx].std;
5504 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505 break;
5506
5507 default:
5508 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510
5511 dest_map->skb = src_map->skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005512 dma_unmap_addr_set(dest_map, mapping,
5513 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 dest_desc->addr_hi = src_desc->addr_hi;
5515 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00005516
5517 /* Ensure that the update to the skb happens after the physical
5518 * addresses have been transferred to the new BD location.
5519 */
5520 smp_wmb();
5521
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522 src_map->skb = NULL;
5523}
5524
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525/* The RX ring scheme is composed of multiple rings which post fresh
5526 * buffers to the chip, and one special ring the chip uses to report
5527 * status back to the host.
5528 *
5529 * The special ring reports the status of received packets to the
5530 * host. The chip does not write into the original descriptor the
5531 * RX buffer was obtained from. The chip simply takes the original
5532 * descriptor as provided by the host, updates the status and length
5533 * field, then writes this into the next status ring entry.
5534 *
5535 * Each ring the host uses to post buffers to the chip is described
5536 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
5537 * it is first placed into the on-chip ram. When the packet's length
5538 * is known, it walks down the TG3_BDINFO entries to select the ring.
5539 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
5540 * which is within the range of the new packet's length is chosen.
5541 *
5542 * The "separate ring for rx status" scheme may sound queer, but it makes
5543 * sense from a cache coherency perspective. If only the host writes
5544 * to the buffer post rings, and only the chip writes to the rx status
5545 * rings, then cache lines never move beyond shared-modified state.
5546 * If both the host and chip were to write into the same ring, cache line
5547 * eviction could occur since both entities want it in an exclusive state.
5548 */
Matt Carlson17375d22009-08-28 14:02:18 +00005549static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550{
Matt Carlson17375d22009-08-28 14:02:18 +00005551 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07005552 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00005553 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00005554 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07005555 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00005557 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005559 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005560 /*
5561 * We need to order the read of hw_idx and the read of
5562 * the opaque cookie.
5563 */
5564 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565 work_mask = 0;
5566 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00005567 std_prod_idx = tpr->rx_std_prod_idx;
5568 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00005570 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00005571 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572 unsigned int len;
5573 struct sk_buff *skb;
5574 dma_addr_t dma_addr;
5575 u32 opaque_key, desc_idx, *post_ptr;
5576
5577 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
5578 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
5579 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005580 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005581 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00005582 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00005583 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07005584 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005586 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005587 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00005588 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00005589 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00005590 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592
5593 work_mask |= opaque_key;
5594
5595 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
5596 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
5597 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00005598 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599 desc_idx, *post_ptr);
5600 drop_it_no_recycle:
5601 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00005602 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603 goto next_pkt;
5604 }
5605
Matt Carlsonad829262008-11-21 17:16:16 -08005606 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
5607 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608
Matt Carlsond2757fc2010-04-12 06:58:27 +00005609 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610 int skb_size;
5611
Matt Carlson86b21e52009-11-13 13:03:45 +00005612 skb_size = tg3_alloc_rx_skb(tp, tpr, opaque_key,
Matt Carlsonafc081f2009-11-13 13:03:43 +00005613 *post_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614 if (skb_size < 0)
5615 goto drop_it;
5616
Matt Carlson287be122009-08-28 13:58:46 +00005617 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618 PCI_DMA_FROMDEVICE);
5619
Matt Carlson61e800c2010-02-17 15:16:54 +00005620 /* Ensure that the update to the skb happens
5621 * after the usage of the old DMA mapping.
5622 */
5623 smp_wmb();
5624
5625 ri->skb = NULL;
5626
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627 skb_put(skb, len);
5628 } else {
5629 struct sk_buff *copy_skb;
5630
Matt Carlsona3896162009-11-13 13:03:44 +00005631 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632 desc_idx, *post_ptr);
5633
Matt Carlsonbf933c82011-01-25 15:58:49 +00005634 copy_skb = netdev_alloc_skb(tp->dev, len +
Matt Carlson9dc7a112010-04-12 06:58:28 +00005635 TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636 if (copy_skb == NULL)
5637 goto drop_it_no_recycle;
5638
Matt Carlsonbf933c82011-01-25 15:58:49 +00005639 skb_reserve(copy_skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640 skb_put(copy_skb, len);
5641 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03005642 skb_copy_from_linear_data(skb, copy_skb->data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
5644
5645 /* We'll reuse the original ring buffer. */
5646 skb = copy_skb;
5647 }
5648
Michał Mirosławdc668912011-04-07 03:35:07 +00005649 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
5651 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
5652 >> RXD_TCPCSUM_SHIFT) == 0xffff))
5653 skb->ip_summed = CHECKSUM_UNNECESSARY;
5654 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005655 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656
5657 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00005658
5659 if (len > (tp->dev->mtu + ETH_HLEN) &&
5660 skb->protocol != htons(ETH_P_8021Q)) {
5661 dev_kfree_skb(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00005662 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00005663 }
5664
Matt Carlson9dc7a112010-04-12 06:58:28 +00005665 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00005666 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
5667 __vlan_hwaccel_put_tag(skb,
5668 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00005669
Matt Carlsonbf933c82011-01-25 15:58:49 +00005670 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672 received++;
5673 budget--;
5674
5675next_pkt:
5676 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07005677
5678 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005679 tpr->rx_std_prod_idx = std_prod_idx &
5680 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00005681 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5682 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07005683 work_mask &= ~RXD_OPAQUE_RING_STD;
5684 rx_std_posted = 0;
5685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07005687 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00005688 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07005689
5690 /* Refresh hw_idx to see if there is new work */
5691 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005692 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07005693 rmb();
5694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695 }
5696
5697 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00005698 tnapi->rx_rcb_ptr = sw_idx;
5699 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700
5701 /* Refill RX ring(s). */
Joe Perches63c3a662011-04-26 08:12:10 +00005702 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005703 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005704 tpr->rx_std_prod_idx = std_prod_idx &
5705 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005706 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5707 tpr->rx_std_prod_idx);
5708 }
5709 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005710 tpr->rx_jmb_prod_idx = jmb_prod_idx &
5711 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005712 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5713 tpr->rx_jmb_prod_idx);
5714 }
5715 mmiowb();
5716 } else if (work_mask) {
5717 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
5718 * updated before the producer indices can be updated.
5719 */
5720 smp_wmb();
5721
Matt Carlson2c49a442010-09-30 10:34:35 +00005722 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
5723 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005724
Matt Carlsone4af1af2010-02-12 14:47:05 +00005725 if (tnapi != &tp->napi[1])
5726 napi_schedule(&tp->napi[1].napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728
5729 return received;
5730}
5731
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005732static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005733{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734 /* handle link change and other phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00005735 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005736 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
5737
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738 if (sblk->status & SD_STATUS_LINK_CHG) {
5739 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005740 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07005741 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00005742 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsondd477002008-05-25 23:45:58 -07005743 tw32_f(MAC_STATUS,
5744 (MAC_STATUS_SYNC_CHANGED |
5745 MAC_STATUS_CFG_CHANGED |
5746 MAC_STATUS_MI_COMPLETION |
5747 MAC_STATUS_LNKSTATE_CHANGED));
5748 udelay(40);
5749 } else
5750 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07005751 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752 }
5753 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005754}
5755
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005756static int tg3_rx_prodring_xfer(struct tg3 *tp,
5757 struct tg3_rx_prodring_set *dpr,
5758 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005759{
5760 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005761 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005762
5763 while (1) {
5764 src_prod_idx = spr->rx_std_prod_idx;
5765
5766 /* Make sure updates to the rx_std_buffers[] entries and the
5767 * standard producer index are seen in the correct order.
5768 */
5769 smp_rmb();
5770
5771 if (spr->rx_std_cons_idx == src_prod_idx)
5772 break;
5773
5774 if (spr->rx_std_cons_idx < src_prod_idx)
5775 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
5776 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005777 cpycnt = tp->rx_std_ring_mask + 1 -
5778 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005779
Matt Carlson2c49a442010-09-30 10:34:35 +00005780 cpycnt = min(cpycnt,
5781 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005782
5783 si = spr->rx_std_cons_idx;
5784 di = dpr->rx_std_prod_idx;
5785
Matt Carlsone92967b2010-02-12 14:47:06 +00005786 for (i = di; i < di + cpycnt; i++) {
5787 if (dpr->rx_std_buffers[i].skb) {
5788 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005789 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005790 break;
5791 }
5792 }
5793
5794 if (!cpycnt)
5795 break;
5796
5797 /* Ensure that updates to the rx_std_buffers ring and the
5798 * shadowed hardware producer ring from tg3_recycle_skb() are
5799 * ordered correctly WRT the skb check above.
5800 */
5801 smp_rmb();
5802
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005803 memcpy(&dpr->rx_std_buffers[di],
5804 &spr->rx_std_buffers[si],
5805 cpycnt * sizeof(struct ring_info));
5806
5807 for (i = 0; i < cpycnt; i++, di++, si++) {
5808 struct tg3_rx_buffer_desc *sbd, *dbd;
5809 sbd = &spr->rx_std[si];
5810 dbd = &dpr->rx_std[di];
5811 dbd->addr_hi = sbd->addr_hi;
5812 dbd->addr_lo = sbd->addr_lo;
5813 }
5814
Matt Carlson2c49a442010-09-30 10:34:35 +00005815 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
5816 tp->rx_std_ring_mask;
5817 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
5818 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005819 }
5820
5821 while (1) {
5822 src_prod_idx = spr->rx_jmb_prod_idx;
5823
5824 /* Make sure updates to the rx_jmb_buffers[] entries and
5825 * the jumbo producer index are seen in the correct order.
5826 */
5827 smp_rmb();
5828
5829 if (spr->rx_jmb_cons_idx == src_prod_idx)
5830 break;
5831
5832 if (spr->rx_jmb_cons_idx < src_prod_idx)
5833 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
5834 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005835 cpycnt = tp->rx_jmb_ring_mask + 1 -
5836 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005837
5838 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00005839 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005840
5841 si = spr->rx_jmb_cons_idx;
5842 di = dpr->rx_jmb_prod_idx;
5843
Matt Carlsone92967b2010-02-12 14:47:06 +00005844 for (i = di; i < di + cpycnt; i++) {
5845 if (dpr->rx_jmb_buffers[i].skb) {
5846 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005847 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005848 break;
5849 }
5850 }
5851
5852 if (!cpycnt)
5853 break;
5854
5855 /* Ensure that updates to the rx_jmb_buffers ring and the
5856 * shadowed hardware producer ring from tg3_recycle_skb() are
5857 * ordered correctly WRT the skb check above.
5858 */
5859 smp_rmb();
5860
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005861 memcpy(&dpr->rx_jmb_buffers[di],
5862 &spr->rx_jmb_buffers[si],
5863 cpycnt * sizeof(struct ring_info));
5864
5865 for (i = 0; i < cpycnt; i++, di++, si++) {
5866 struct tg3_rx_buffer_desc *sbd, *dbd;
5867 sbd = &spr->rx_jmb[si].std;
5868 dbd = &dpr->rx_jmb[di].std;
5869 dbd->addr_hi = sbd->addr_hi;
5870 dbd->addr_lo = sbd->addr_lo;
5871 }
5872
Matt Carlson2c49a442010-09-30 10:34:35 +00005873 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
5874 tp->rx_jmb_ring_mask;
5875 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
5876 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005877 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005878
5879 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005880}
5881
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005882static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
5883{
5884 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005885
5886 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005887 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00005888 tg3_tx(tnapi);
Joe Perches63c3a662011-04-26 08:12:10 +00005889 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Michael Chan4fd7ab52007-10-12 01:39:50 -07005890 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891 }
5892
Linus Torvalds1da177e2005-04-16 15:20:36 -07005893 /* run RX thread, within the bounds set by NAPI.
5894 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005895 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07005896 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005897 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00005898 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899
Joe Perches63c3a662011-04-26 08:12:10 +00005900 if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005901 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005902 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00005903 u32 std_prod_idx = dpr->rx_std_prod_idx;
5904 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005905
Matt Carlsone4af1af2010-02-12 14:47:05 +00005906 for (i = 1; i < tp->irq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005907 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00005908 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005909
5910 wmb();
5911
Matt Carlsone4af1af2010-02-12 14:47:05 +00005912 if (std_prod_idx != dpr->rx_std_prod_idx)
5913 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5914 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005915
Matt Carlsone4af1af2010-02-12 14:47:05 +00005916 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
5917 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5918 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005919
5920 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005921
5922 if (err)
5923 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005924 }
5925
David S. Miller6f535762007-10-11 18:08:29 -07005926 return work_done;
5927}
David S. Millerf7383c22005-05-18 22:50:53 -07005928
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005929static int tg3_poll_msix(struct napi_struct *napi, int budget)
5930{
5931 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5932 struct tg3 *tp = tnapi->tp;
5933 int work_done = 0;
5934 struct tg3_hw_status *sblk = tnapi->hw_status;
5935
5936 while (1) {
5937 work_done = tg3_poll_work(tnapi, work_done, budget);
5938
Joe Perches63c3a662011-04-26 08:12:10 +00005939 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005940 goto tx_recovery;
5941
5942 if (unlikely(work_done >= budget))
5943 break;
5944
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005945 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005946 * to tell the hw how much work has been processed,
5947 * so we must read it before checking for more work.
5948 */
5949 tnapi->last_tag = sblk->status_tag;
5950 tnapi->last_irq_tag = tnapi->last_tag;
5951 rmb();
5952
5953 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00005954 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
5955 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005956 napi_complete(napi);
5957 /* Reenable interrupts. */
5958 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
5959 mmiowb();
5960 break;
5961 }
5962 }
5963
5964 return work_done;
5965
5966tx_recovery:
5967 /* work_done is guaranteed to be less than budget. */
5968 napi_complete(napi);
5969 schedule_work(&tp->reset_task);
5970 return work_done;
5971}
5972
Matt Carlsone64de4e2011-04-13 11:05:05 +00005973static void tg3_process_error(struct tg3 *tp)
5974{
5975 u32 val;
5976 bool real_error = false;
5977
Joe Perches63c3a662011-04-26 08:12:10 +00005978 if (tg3_flag(tp, ERROR_PROCESSED))
Matt Carlsone64de4e2011-04-13 11:05:05 +00005979 return;
5980
5981 /* Check Flow Attention register */
5982 val = tr32(HOSTCC_FLOW_ATTN);
5983 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
5984 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
5985 real_error = true;
5986 }
5987
5988 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
5989 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
5990 real_error = true;
5991 }
5992
5993 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
5994 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
5995 real_error = true;
5996 }
5997
5998 if (!real_error)
5999 return;
6000
6001 tg3_dump_state(tp);
6002
Joe Perches63c3a662011-04-26 08:12:10 +00006003 tg3_flag_set(tp, ERROR_PROCESSED);
Matt Carlsone64de4e2011-04-13 11:05:05 +00006004 schedule_work(&tp->reset_task);
6005}
6006
David S. Miller6f535762007-10-11 18:08:29 -07006007static int tg3_poll(struct napi_struct *napi, int budget)
6008{
Matt Carlson8ef04422009-08-28 14:01:37 +00006009 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
6010 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07006011 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00006012 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07006013
6014 while (1) {
Matt Carlsone64de4e2011-04-13 11:05:05 +00006015 if (sblk->status & SD_STATUS_ERROR)
6016 tg3_process_error(tp);
6017
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006018 tg3_poll_link(tp);
6019
Matt Carlson17375d22009-08-28 14:02:18 +00006020 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07006021
Joe Perches63c3a662011-04-26 08:12:10 +00006022 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
David S. Miller6f535762007-10-11 18:08:29 -07006023 goto tx_recovery;
6024
6025 if (unlikely(work_done >= budget))
6026 break;
6027
Joe Perches63c3a662011-04-26 08:12:10 +00006028 if (tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson17375d22009-08-28 14:02:18 +00006029 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07006030 * to tell the hw how much work has been processed,
6031 * so we must read it before checking for more work.
6032 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006033 tnapi->last_tag = sblk->status_tag;
6034 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07006035 rmb();
6036 } else
6037 sblk->status &= ~SD_STATUS_UPDATED;
6038
Matt Carlson17375d22009-08-28 14:02:18 +00006039 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08006040 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00006041 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07006042 break;
6043 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044 }
6045
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006046 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07006047
6048tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07006049 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08006050 napi_complete(napi);
David S. Miller6f535762007-10-11 18:08:29 -07006051 schedule_work(&tp->reset_task);
Michael Chan4fd7ab52007-10-12 01:39:50 -07006052 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053}
6054
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006055static void tg3_napi_disable(struct tg3 *tp)
6056{
6057 int i;
6058
6059 for (i = tp->irq_cnt - 1; i >= 0; i--)
6060 napi_disable(&tp->napi[i].napi);
6061}
6062
6063static void tg3_napi_enable(struct tg3 *tp)
6064{
6065 int i;
6066
6067 for (i = 0; i < tp->irq_cnt; i++)
6068 napi_enable(&tp->napi[i].napi);
6069}
6070
6071static void tg3_napi_init(struct tg3 *tp)
6072{
6073 int i;
6074
6075 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
6076 for (i = 1; i < tp->irq_cnt; i++)
6077 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
6078}
6079
6080static void tg3_napi_fini(struct tg3 *tp)
6081{
6082 int i;
6083
6084 for (i = 0; i < tp->irq_cnt; i++)
6085 netif_napi_del(&tp->napi[i].napi);
6086}
6087
6088static inline void tg3_netif_stop(struct tg3 *tp)
6089{
6090 tp->dev->trans_start = jiffies; /* prevent tx timeout */
6091 tg3_napi_disable(tp);
6092 netif_tx_disable(tp->dev);
6093}
6094
6095static inline void tg3_netif_start(struct tg3 *tp)
6096{
6097 /* NOTE: unconditional netif_tx_wake_all_queues is only
6098 * appropriate so long as all callers are assured to
6099 * have free tx slots (such as after tg3_init_hw)
6100 */
6101 netif_tx_wake_all_queues(tp->dev);
6102
6103 tg3_napi_enable(tp);
6104 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
6105 tg3_enable_ints(tp);
6106}
6107
David S. Millerf47c11e2005-06-24 20:18:35 -07006108static void tg3_irq_quiesce(struct tg3 *tp)
6109{
Matt Carlson4f125f42009-09-01 12:55:02 +00006110 int i;
6111
David S. Millerf47c11e2005-06-24 20:18:35 -07006112 BUG_ON(tp->irq_sync);
6113
6114 tp->irq_sync = 1;
6115 smp_mb();
6116
Matt Carlson4f125f42009-09-01 12:55:02 +00006117 for (i = 0; i < tp->irq_cnt; i++)
6118 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07006119}
6120
David S. Millerf47c11e2005-06-24 20:18:35 -07006121/* Fully shutdown all tg3 driver activity elsewhere in the system.
6122 * If irq_sync is non-zero, then the IRQ handler must be synchronized
6123 * with as well. Most of the time, this is not necessary except when
6124 * shutting down the device.
6125 */
6126static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
6127{
Michael Chan46966542007-07-11 19:47:19 -07006128 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07006129 if (irq_sync)
6130 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07006131}
6132
6133static inline void tg3_full_unlock(struct tg3 *tp)
6134{
David S. Millerf47c11e2005-06-24 20:18:35 -07006135 spin_unlock_bh(&tp->lock);
6136}
6137
Michael Chanfcfa0a32006-03-20 22:28:41 -08006138/* One-shot MSI handler - Chip automatically disables interrupt
6139 * after sending MSI so driver doesn't have to do it.
6140 */
David Howells7d12e782006-10-05 14:55:46 +01006141static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08006142{
Matt Carlson09943a12009-08-28 14:01:57 +00006143 struct tg3_napi *tnapi = dev_id;
6144 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08006145
Matt Carlson898a56f2009-08-28 14:02:40 +00006146 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006147 if (tnapi->rx_rcb)
6148 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08006149
6150 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00006151 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08006152
6153 return IRQ_HANDLED;
6154}
6155
Michael Chan88b06bc2005-04-21 17:13:25 -07006156/* MSI ISR - No need to check for interrupt sharing and no need to
6157 * flush status block and interrupt mailbox. PCI ordering rules
6158 * guarantee that MSI will arrive after the status block.
6159 */
David Howells7d12e782006-10-05 14:55:46 +01006160static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc2005-04-21 17:13:25 -07006161{
Matt Carlson09943a12009-08-28 14:01:57 +00006162 struct tg3_napi *tnapi = dev_id;
6163 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc2005-04-21 17:13:25 -07006164
Matt Carlson898a56f2009-08-28 14:02:40 +00006165 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006166 if (tnapi->rx_rcb)
6167 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc2005-04-21 17:13:25 -07006168 /*
David S. Millerfac9b832005-05-18 22:46:34 -07006169 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc2005-04-21 17:13:25 -07006170 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07006171 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc2005-04-21 17:13:25 -07006172 * NIC to stop sending us irqs, engaging "in-intr-handler"
6173 * event coalescing.
6174 */
Matt Carlson5b39de92011-08-31 11:44:50 +00006175 tw32_mailbox(tnapi->int_mbox, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07006176 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00006177 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07006178
Michael Chan88b06bc2005-04-21 17:13:25 -07006179 return IRQ_RETVAL(1);
6180}
6181
David Howells7d12e782006-10-05 14:55:46 +01006182static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006183{
Matt Carlson09943a12009-08-28 14:01:57 +00006184 struct tg3_napi *tnapi = dev_id;
6185 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006186 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006187 unsigned int handled = 1;
6188
Linus Torvalds1da177e2005-04-16 15:20:36 -07006189 /* In INTx mode, it is possible for the interrupt to arrive at
6190 * the CPU before the status block posted prior to the interrupt.
6191 * Reading the PCI State register will confirm whether the
6192 * interrupt is ours and will flush the status block.
6193 */
Michael Chand18edcb2007-03-24 20:57:11 -07006194 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
Joe Perches63c3a662011-04-26 08:12:10 +00006195 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07006196 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
6197 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07006198 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07006199 }
Michael Chand18edcb2007-03-24 20:57:11 -07006200 }
6201
6202 /*
6203 * Writing any value to intr-mbox-0 clears PCI INTA# and
6204 * chip-internal interrupt pending events.
6205 * Writing non-zero to intr-mbox-0 additional tells the
6206 * NIC to stop sending us irqs, engaging "in-intr-handler"
6207 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07006208 *
6209 * Flush the mailbox to de-assert the IRQ immediately to prevent
6210 * spurious interrupts. The flush impacts performance but
6211 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07006212 */
Michael Chanc04cb342007-05-07 00:26:15 -07006213 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07006214 if (tg3_irq_sync(tp))
6215 goto out;
6216 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00006217 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00006218 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00006219 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07006220 } else {
6221 /* No work, shared interrupt perhaps? re-enable
6222 * interrupts, and flush that PCI write
6223 */
6224 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
6225 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07006226 }
David S. Millerf47c11e2005-06-24 20:18:35 -07006227out:
David S. Millerfac9b832005-05-18 22:46:34 -07006228 return IRQ_RETVAL(handled);
6229}
6230
David Howells7d12e782006-10-05 14:55:46 +01006231static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07006232{
Matt Carlson09943a12009-08-28 14:01:57 +00006233 struct tg3_napi *tnapi = dev_id;
6234 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006235 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07006236 unsigned int handled = 1;
6237
David S. Millerfac9b832005-05-18 22:46:34 -07006238 /* In INTx mode, it is possible for the interrupt to arrive at
6239 * the CPU before the status block posted prior to the interrupt.
6240 * Reading the PCI State register will confirm whether the
6241 * interrupt is ours and will flush the status block.
6242 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006243 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Joe Perches63c3a662011-04-26 08:12:10 +00006244 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07006245 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
6246 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07006247 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006248 }
Michael Chand18edcb2007-03-24 20:57:11 -07006249 }
6250
6251 /*
6252 * writing any value to intr-mbox-0 clears PCI INTA# and
6253 * chip-internal interrupt pending events.
6254 * writing non-zero to intr-mbox-0 additional tells the
6255 * NIC to stop sending us irqs, engaging "in-intr-handler"
6256 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07006257 *
6258 * Flush the mailbox to de-assert the IRQ immediately to prevent
6259 * spurious interrupts. The flush impacts performance but
6260 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07006261 */
Michael Chanc04cb342007-05-07 00:26:15 -07006262 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00006263
6264 /*
6265 * In a shared interrupt configuration, sometimes other devices'
6266 * interrupts will scream. We record the current status tag here
6267 * so that the above check can report that the screaming interrupts
6268 * are unhandled. Eventually they will be silenced.
6269 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006270 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00006271
Michael Chand18edcb2007-03-24 20:57:11 -07006272 if (tg3_irq_sync(tp))
6273 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00006274
Matt Carlson72334482009-08-28 14:03:01 +00006275 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00006276
Matt Carlson09943a12009-08-28 14:01:57 +00006277 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00006278
David S. Millerf47c11e2005-06-24 20:18:35 -07006279out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006280 return IRQ_RETVAL(handled);
6281}
6282
Michael Chan79381092005-04-21 17:13:59 -07006283/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01006284static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07006285{
Matt Carlson09943a12009-08-28 14:01:57 +00006286 struct tg3_napi *tnapi = dev_id;
6287 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006288 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07006289
Michael Chanf9804dd2005-09-27 12:13:10 -07006290 if ((sblk->status & SD_STATUS_UPDATED) ||
6291 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07006292 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07006293 return IRQ_RETVAL(1);
6294 }
6295 return IRQ_RETVAL(0);
6296}
6297
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07006298static int tg3_init_hw(struct tg3 *, int);
Michael Chan944d9802005-05-29 14:57:48 -07006299static int tg3_halt(struct tg3 *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006300
Michael Chanb9ec6c12006-07-25 16:37:27 -07006301/* Restart hardware after configuration changes, self-test, etc.
6302 * Invoked with tp->lock held.
6303 */
6304static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
Eric Dumazet78c61462008-04-24 23:33:06 -07006305 __releases(tp->lock)
6306 __acquires(tp->lock)
Michael Chanb9ec6c12006-07-25 16:37:27 -07006307{
6308 int err;
6309
6310 err = tg3_init_hw(tp, reset_phy);
6311 if (err) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006312 netdev_err(tp->dev,
6313 "Failed to re-initialize device, aborting\n");
Michael Chanb9ec6c12006-07-25 16:37:27 -07006314 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
6315 tg3_full_unlock(tp);
6316 del_timer_sync(&tp->timer);
6317 tp->irq_sync = 0;
Matt Carlsonfed97812009-09-01 13:10:19 +00006318 tg3_napi_enable(tp);
Michael Chanb9ec6c12006-07-25 16:37:27 -07006319 dev_close(tp->dev);
6320 tg3_full_lock(tp, 0);
6321 }
6322 return err;
6323}
6324
Linus Torvalds1da177e2005-04-16 15:20:36 -07006325#ifdef CONFIG_NET_POLL_CONTROLLER
6326static void tg3_poll_controller(struct net_device *dev)
6327{
Matt Carlson4f125f42009-09-01 12:55:02 +00006328 int i;
Michael Chan88b06bc2005-04-21 17:13:25 -07006329 struct tg3 *tp = netdev_priv(dev);
6330
Matt Carlson4f125f42009-09-01 12:55:02 +00006331 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00006332 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006333}
6334#endif
6335
David Howellsc4028952006-11-22 14:57:56 +00006336static void tg3_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006337{
David Howellsc4028952006-11-22 14:57:56 +00006338 struct tg3 *tp = container_of(work, struct tg3, reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006339 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006340 unsigned int restart_timer;
6341
Michael Chan7faa0062006-02-02 17:29:28 -08006342 tg3_full_lock(tp, 0);
Michael Chan7faa0062006-02-02 17:29:28 -08006343
6344 if (!netif_running(tp->dev)) {
Michael Chan7faa0062006-02-02 17:29:28 -08006345 tg3_full_unlock(tp);
6346 return;
6347 }
6348
6349 tg3_full_unlock(tp);
6350
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006351 tg3_phy_stop(tp);
6352
Linus Torvalds1da177e2005-04-16 15:20:36 -07006353 tg3_netif_stop(tp);
6354
David S. Millerf47c11e2005-06-24 20:18:35 -07006355 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006356
Joe Perches63c3a662011-04-26 08:12:10 +00006357 restart_timer = tg3_flag(tp, RESTART_TIMER);
6358 tg3_flag_clear(tp, RESTART_TIMER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006359
Joe Perches63c3a662011-04-26 08:12:10 +00006360 if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
Michael Chandf3e6542006-05-26 17:48:07 -07006361 tp->write32_tx_mbox = tg3_write32_tx_mbox;
6362 tp->write32_rx_mbox = tg3_write_flush_reg32;
Joe Perches63c3a662011-04-26 08:12:10 +00006363 tg3_flag_set(tp, MBOX_WRITE_REORDER);
6364 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07006365 }
6366
Michael Chan944d9802005-05-29 14:57:48 -07006367 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006368 err = tg3_init_hw(tp, 1);
6369 if (err)
Michael Chanb9ec6c12006-07-25 16:37:27 -07006370 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006371
6372 tg3_netif_start(tp);
6373
Linus Torvalds1da177e2005-04-16 15:20:36 -07006374 if (restart_timer)
6375 mod_timer(&tp->timer, jiffies + 1);
Michael Chan7faa0062006-02-02 17:29:28 -08006376
Michael Chanb9ec6c12006-07-25 16:37:27 -07006377out:
Michael Chan7faa0062006-02-02 17:29:28 -08006378 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006379
6380 if (!err)
6381 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006382}
6383
6384static void tg3_tx_timeout(struct net_device *dev)
6385{
6386 struct tg3 *tp = netdev_priv(dev);
6387
Michael Chanb0408752007-02-13 12:18:30 -08006388 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00006389 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00006390 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08006391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006392
6393 schedule_work(&tp->reset_task);
6394}
6395
Michael Chanc58ec932005-09-17 00:46:27 -07006396/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
6397static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
6398{
6399 u32 base = (u32) mapping & 0xffffffff;
6400
Eric Dumazet807540b2010-09-23 05:40:09 +00006401 return (base > 0xffffdcc0) && (base + len + 8 < base);
Michael Chanc58ec932005-09-17 00:46:27 -07006402}
6403
Michael Chan72f2afb2006-03-06 19:28:35 -08006404/* Test for DMA addresses > 40-bit */
6405static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
6406 int len)
6407{
6408#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Joe Perches63c3a662011-04-26 08:12:10 +00006409 if (tg3_flag(tp, 40BIT_DMA_BUG))
Eric Dumazet807540b2010-09-23 05:40:09 +00006410 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08006411 return 0;
6412#else
6413 return 0;
6414#endif
6415}
6416
Matt Carlsond1a3b732011-07-27 14:20:51 +00006417static inline void tg3_tx_set_bd(struct tg3_tx_buffer_desc *txbd,
Matt Carlson92cd3a12011-07-27 14:20:47 +00006418 dma_addr_t mapping, u32 len, u32 flags,
6419 u32 mss, u32 vlan)
Matt Carlson2ffcc982011-05-19 12:12:44 +00006420{
Matt Carlson92cd3a12011-07-27 14:20:47 +00006421 txbd->addr_hi = ((u64) mapping >> 32);
6422 txbd->addr_lo = ((u64) mapping & 0xffffffff);
6423 txbd->len_flags = (len << TXD_LEN_SHIFT) | (flags & 0x0000ffff);
6424 txbd->vlan_tag = (mss << TXD_MSS_SHIFT) | (vlan << TXD_VLAN_TAG_SHIFT);
Matt Carlson2ffcc982011-05-19 12:12:44 +00006425}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006426
Matt Carlson84b67b22011-07-27 14:20:52 +00006427static bool tg3_tx_frag_set(struct tg3_napi *tnapi, u32 *entry, u32 *budget,
Matt Carlsond1a3b732011-07-27 14:20:51 +00006428 dma_addr_t map, u32 len, u32 flags,
6429 u32 mss, u32 vlan)
6430{
6431 struct tg3 *tp = tnapi->tp;
6432 bool hwbug = false;
6433
6434 if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
6435 hwbug = 1;
6436
6437 if (tg3_4g_overflow_test(map, len))
6438 hwbug = 1;
6439
6440 if (tg3_40bit_overflow_test(tp, map, len))
6441 hwbug = 1;
6442
Matt Carlsone31aa982011-07-27 14:20:53 +00006443 if (tg3_flag(tp, 4K_FIFO_LIMIT)) {
6444 u32 tmp_flag = flags & ~TXD_FLAG_END;
6445 while (len > TG3_TX_BD_DMA_MAX) {
6446 u32 frag_len = TG3_TX_BD_DMA_MAX;
6447 len -= TG3_TX_BD_DMA_MAX;
6448
6449 if (len) {
6450 tnapi->tx_buffers[*entry].fragmented = true;
6451 /* Avoid the 8byte DMA problem */
6452 if (len <= 8) {
6453 len += TG3_TX_BD_DMA_MAX / 2;
6454 frag_len = TG3_TX_BD_DMA_MAX / 2;
6455 }
6456 } else
6457 tmp_flag = flags;
6458
6459 if (*budget) {
6460 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6461 frag_len, tmp_flag, mss, vlan);
6462 (*budget)--;
6463 *entry = NEXT_TX(*entry);
6464 } else {
6465 hwbug = 1;
6466 break;
6467 }
6468
6469 map += frag_len;
6470 }
6471
6472 if (len) {
6473 if (*budget) {
6474 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6475 len, flags, mss, vlan);
6476 (*budget)--;
6477 *entry = NEXT_TX(*entry);
6478 } else {
6479 hwbug = 1;
6480 }
6481 }
6482 } else {
Matt Carlson84b67b22011-07-27 14:20:52 +00006483 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6484 len, flags, mss, vlan);
Matt Carlsone31aa982011-07-27 14:20:53 +00006485 *entry = NEXT_TX(*entry);
6486 }
Matt Carlsond1a3b732011-07-27 14:20:51 +00006487
6488 return hwbug;
6489}
6490
Matt Carlson0d681b22011-07-27 14:20:49 +00006491static void tg3_tx_skb_unmap(struct tg3_napi *tnapi, u32 entry, int last)
Matt Carlson432aa7e2011-05-19 12:12:45 +00006492{
6493 int i;
Matt Carlson0d681b22011-07-27 14:20:49 +00006494 struct sk_buff *skb;
Matt Carlsondf8944c2011-07-27 14:20:46 +00006495 struct tg3_tx_ring_info *txb = &tnapi->tx_buffers[entry];
Matt Carlson432aa7e2011-05-19 12:12:45 +00006496
Matt Carlson0d681b22011-07-27 14:20:49 +00006497 skb = txb->skb;
6498 txb->skb = NULL;
6499
Matt Carlson432aa7e2011-05-19 12:12:45 +00006500 pci_unmap_single(tnapi->tp->pdev,
6501 dma_unmap_addr(txb, mapping),
6502 skb_headlen(skb),
6503 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00006504
6505 while (txb->fragmented) {
6506 txb->fragmented = false;
6507 entry = NEXT_TX(entry);
6508 txb = &tnapi->tx_buffers[entry];
6509 }
6510
Matt Carlson9a2e0fb2011-06-02 13:01:39 +00006511 for (i = 0; i < last; i++) {
Matt Carlson432aa7e2011-05-19 12:12:45 +00006512 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6513
6514 entry = NEXT_TX(entry);
6515 txb = &tnapi->tx_buffers[entry];
6516
6517 pci_unmap_page(tnapi->tp->pdev,
6518 dma_unmap_addr(txb, mapping),
6519 frag->size, PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00006520
6521 while (txb->fragmented) {
6522 txb->fragmented = false;
6523 entry = NEXT_TX(entry);
6524 txb = &tnapi->tx_buffers[entry];
6525 }
Matt Carlson432aa7e2011-05-19 12:12:45 +00006526 }
6527}
6528
Michael Chan72f2afb2006-03-06 19:28:35 -08006529/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006530static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
Matt Carlson432aa7e2011-05-19 12:12:45 +00006531 struct sk_buff *skb,
Matt Carlson84b67b22011-07-27 14:20:52 +00006532 u32 *entry, u32 *budget,
Matt Carlson92cd3a12011-07-27 14:20:47 +00006533 u32 base_flags, u32 mss, u32 vlan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006534{
Matt Carlson24f4efd2009-11-13 13:03:35 +00006535 struct tg3 *tp = tnapi->tp;
Matt Carlson41588ba2008-04-19 18:12:33 -07006536 struct sk_buff *new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07006537 dma_addr_t new_addr = 0;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006538 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006539
Matt Carlson41588ba2008-04-19 18:12:33 -07006540 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
6541 new_skb = skb_copy(skb, GFP_ATOMIC);
6542 else {
6543 int more_headroom = 4 - ((unsigned long)skb->data & 3);
6544
6545 new_skb = skb_copy_expand(skb,
6546 skb_headroom(skb) + more_headroom,
6547 skb_tailroom(skb), GFP_ATOMIC);
6548 }
6549
Linus Torvalds1da177e2005-04-16 15:20:36 -07006550 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07006551 ret = -1;
6552 } else {
6553 /* New SKB is guaranteed to be linear. */
Alexander Duyckf4188d82009-12-02 16:48:38 +00006554 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
6555 PCI_DMA_TODEVICE);
6556 /* Make sure the mapping succeeded */
6557 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00006558 dev_kfree_skb(new_skb);
Michael Chanc58ec932005-09-17 00:46:27 -07006559 ret = -1;
Michael Chanc58ec932005-09-17 00:46:27 -07006560 } else {
Matt Carlson92cd3a12011-07-27 14:20:47 +00006561 base_flags |= TXD_FLAG_END;
6562
Matt Carlson84b67b22011-07-27 14:20:52 +00006563 tnapi->tx_buffers[*entry].skb = new_skb;
6564 dma_unmap_addr_set(&tnapi->tx_buffers[*entry],
Matt Carlson432aa7e2011-05-19 12:12:45 +00006565 mapping, new_addr);
6566
Matt Carlson84b67b22011-07-27 14:20:52 +00006567 if (tg3_tx_frag_set(tnapi, entry, budget, new_addr,
Matt Carlsond1a3b732011-07-27 14:20:51 +00006568 new_skb->len, base_flags,
6569 mss, vlan)) {
Matt Carlson84b67b22011-07-27 14:20:52 +00006570 tg3_tx_skb_unmap(tnapi, *entry, 0);
Matt Carlsond1a3b732011-07-27 14:20:51 +00006571 dev_kfree_skb(new_skb);
6572 ret = -1;
6573 }
Michael Chanc58ec932005-09-17 00:46:27 -07006574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006575 }
6576
Linus Torvalds1da177e2005-04-16 15:20:36 -07006577 dev_kfree_skb(skb);
6578
Michael Chanc58ec932005-09-17 00:46:27 -07006579 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006580}
6581
Matt Carlson2ffcc982011-05-19 12:12:44 +00006582static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07006583
6584/* Use GSO to workaround a rare TSO bug that may be triggered when the
6585 * TSO header is greater than 80 bytes.
6586 */
6587static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
6588{
6589 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006590 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07006591
6592 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006593 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07006594 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006595
6596 /* netif_tx_stop_queue() must be done before checking
6597 * checking tx index in tg3_tx_avail() below, because in
6598 * tg3_tx(), we update tx index before checking for
6599 * netif_tx_queue_stopped().
6600 */
6601 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006602 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08006603 return NETDEV_TX_BUSY;
6604
6605 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07006606 }
6607
6608 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07006609 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07006610 goto tg3_tso_bug_end;
6611
6612 do {
6613 nskb = segs;
6614 segs = segs->next;
6615 nskb->next = NULL;
Matt Carlson2ffcc982011-05-19 12:12:44 +00006616 tg3_start_xmit(nskb, tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07006617 } while (segs);
6618
6619tg3_tso_bug_end:
6620 dev_kfree_skb(skb);
6621
6622 return NETDEV_TX_OK;
6623}
Michael Chan52c0fd82006-06-29 20:15:54 -07006624
Michael Chan5a6f3072006-03-20 22:28:05 -08006625/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
Joe Perches63c3a662011-04-26 08:12:10 +00006626 * support TG3_FLAG_HW_TSO_1 or firmware TSO only.
Michael Chan5a6f3072006-03-20 22:28:05 -08006627 */
Matt Carlson2ffcc982011-05-19 12:12:44 +00006628static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08006629{
6630 struct tg3 *tp = netdev_priv(dev);
Matt Carlson92cd3a12011-07-27 14:20:47 +00006631 u32 len, entry, base_flags, mss, vlan = 0;
Matt Carlson84b67b22011-07-27 14:20:52 +00006632 u32 budget;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006633 int i = -1, would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07006634 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00006635 struct tg3_napi *tnapi;
6636 struct netdev_queue *txq;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006637 unsigned int last;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006638
Matt Carlson24f4efd2009-11-13 13:03:35 +00006639 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
6640 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Joe Perches63c3a662011-04-26 08:12:10 +00006641 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006642 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006643
Matt Carlson84b67b22011-07-27 14:20:52 +00006644 budget = tg3_tx_avail(tnapi);
6645
Michael Chan00b70502006-06-17 21:58:45 -07006646 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006647 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07006648 * interrupt. Furthermore, IRQ processing runs lockless so we have
6649 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07006650 */
Matt Carlson84b67b22011-07-27 14:20:52 +00006651 if (unlikely(budget <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006652 if (!netif_tx_queue_stopped(txq)) {
6653 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08006654
6655 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00006656 netdev_err(dev,
6657 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08006658 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006659 return NETDEV_TX_BUSY;
6660 }
6661
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006662 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006663 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07006664 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006665 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00006666
Matt Carlsonbe98da62010-07-11 09:31:46 +00006667 mss = skb_shinfo(skb)->gso_size;
6668 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006669 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00006670 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006671
6672 if (skb_header_cloned(skb) &&
6673 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
6674 dev_kfree_skb(skb);
6675 goto out_unlock;
6676 }
6677
Matt Carlson34195c32010-07-11 09:31:42 +00006678 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07006679 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006680
Matt Carlson02e96082010-09-15 08:59:59 +00006681 if (skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00006682 hdr_len = skb_headlen(skb) - ETH_HLEN;
6683 } else {
6684 u32 ip_tcp_len;
6685
6686 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
6687 hdr_len = ip_tcp_len + tcp_opt_len;
6688
6689 iph->check = 0;
6690 iph->tot_len = htons(mss + hdr_len);
6691 }
6692
Michael Chan52c0fd82006-06-29 20:15:54 -07006693 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Joe Perches63c3a662011-04-26 08:12:10 +00006694 tg3_flag(tp, TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00006695 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07006696
Linus Torvalds1da177e2005-04-16 15:20:36 -07006697 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
6698 TXD_FLAG_CPU_POST_DMA);
6699
Joe Perches63c3a662011-04-26 08:12:10 +00006700 if (tg3_flag(tp, HW_TSO_1) ||
6701 tg3_flag(tp, HW_TSO_2) ||
6702 tg3_flag(tp, HW_TSO_3)) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006703 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006704 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006705 } else
6706 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
6707 iph->daddr, 0,
6708 IPPROTO_TCP,
6709 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006710
Joe Perches63c3a662011-04-26 08:12:10 +00006711 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlson615774f2009-11-13 13:03:39 +00006712 mss |= (hdr_len & 0xc) << 12;
6713 if (hdr_len & 0x10)
6714 base_flags |= 0x00000010;
6715 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +00006716 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006717 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +00006718 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006719 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006720 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006721 int tsflags;
6722
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006723 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006724 mss |= (tsflags << 11);
6725 }
6726 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006727 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006728 int tsflags;
6729
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006730 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006731 base_flags |= tsflags << 12;
6732 }
6733 }
6734 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00006735
Matt Carlson92cd3a12011-07-27 14:20:47 +00006736#ifdef BCM_KERNEL_SUPPORTS_8021Q
6737 if (vlan_tx_tag_present(skb)) {
6738 base_flags |= TXD_FLAG_VLAN;
6739 vlan = vlan_tx_tag_get(skb);
6740 }
6741#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006742
Joe Perches63c3a662011-04-26 08:12:10 +00006743 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
Matt Carlson8fc2f992010-12-06 08:28:49 +00006744 !mss && skb->len > VLAN_ETH_FRAME_LEN)
Matt Carlson615774f2009-11-13 13:03:39 +00006745 base_flags |= TXD_FLAG_JMB_PKT;
6746
Alexander Duyckf4188d82009-12-02 16:48:38 +00006747 len = skb_headlen(skb);
6748
6749 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
6750 if (pci_dma_mapping_error(tp->pdev, mapping)) {
David S. Miller90079ce2008-09-11 04:52:51 -07006751 dev_kfree_skb(skb);
6752 goto out_unlock;
6753 }
6754
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006755 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006756 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006757
6758 would_hit_hwbug = 0;
6759
Joe Perches63c3a662011-04-26 08:12:10 +00006760 if (tg3_flag(tp, 5701_DMA_BUG))
Michael Chanc58ec932005-09-17 00:46:27 -07006761 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006762
Matt Carlson84b67b22011-07-27 14:20:52 +00006763 if (tg3_tx_frag_set(tnapi, &entry, &budget, mapping, len, base_flags |
Matt Carlsond1a3b732011-07-27 14:20:51 +00006764 ((skb_shinfo(skb)->nr_frags == 0) ? TXD_FLAG_END : 0),
6765 mss, vlan))
6766 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006767
Linus Torvalds1da177e2005-04-16 15:20:36 -07006768 /* Now loop through additional data fragments, and queue them. */
6769 if (skb_shinfo(skb)->nr_frags > 0) {
Matt Carlson92cd3a12011-07-27 14:20:47 +00006770 u32 tmp_mss = mss;
6771
6772 if (!tg3_flag(tp, HW_TSO_1) &&
6773 !tg3_flag(tp, HW_TSO_2) &&
6774 !tg3_flag(tp, HW_TSO_3))
6775 tmp_mss = 0;
6776
Linus Torvalds1da177e2005-04-16 15:20:36 -07006777 last = skb_shinfo(skb)->nr_frags - 1;
6778 for (i = 0; i <= last; i++) {
6779 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6780
6781 len = frag->size;
Ian Campbelldc234d02011-08-24 22:28:11 +00006782 mapping = skb_frag_dma_map(&tp->pdev->dev, frag, 0,
6783 len, PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006784
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006785 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006786 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00006787 mapping);
6788 if (pci_dma_mapping_error(tp->pdev, mapping))
6789 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006790
Matt Carlson84b67b22011-07-27 14:20:52 +00006791 if (tg3_tx_frag_set(tnapi, &entry, &budget, mapping,
6792 len, base_flags |
6793 ((i == last) ? TXD_FLAG_END : 0),
Matt Carlsond1a3b732011-07-27 14:20:51 +00006794 tmp_mss, vlan))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006795 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006796 }
6797 }
6798
6799 if (would_hit_hwbug) {
Matt Carlson0d681b22011-07-27 14:20:49 +00006800 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006801
6802 /* If the workaround fails due to memory/mapping
6803 * failure, silently drop this packet.
6804 */
Matt Carlson84b67b22011-07-27 14:20:52 +00006805 entry = tnapi->tx_prod;
6806 budget = tg3_tx_avail(tnapi);
6807 if (tigon3_dma_hwbug_workaround(tnapi, skb, &entry, &budget,
6808 base_flags, mss, vlan))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006809 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006810 }
6811
Richard Cochrand515b452011-06-19 03:31:41 +00006812 skb_tx_timestamp(skb);
6813
Linus Torvalds1da177e2005-04-16 15:20:36 -07006814 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006815 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006816
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006817 tnapi->tx_prod = entry;
6818 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006819 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006820
6821 /* netif_tx_stop_queue() must be done before checking
6822 * checking tx index in tg3_tx_avail() below, because in
6823 * tg3_tx(), we update tx index before checking for
6824 * netif_tx_queue_stopped().
6825 */
6826 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006827 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006828 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07006829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006830
6831out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00006832 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006833
6834 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006835
6836dma_error:
Matt Carlson0d681b22011-07-27 14:20:49 +00006837 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, i);
Alexander Duyckf4188d82009-12-02 16:48:38 +00006838 dev_kfree_skb(skb);
Matt Carlson432aa7e2011-05-19 12:12:45 +00006839 tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006840 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006841}
6842
Matt Carlson6e01b202011-08-19 13:58:20 +00006843static void tg3_mac_loopback(struct tg3 *tp, bool enable)
6844{
6845 if (enable) {
6846 tp->mac_mode &= ~(MAC_MODE_HALF_DUPLEX |
6847 MAC_MODE_PORT_MODE_MASK);
6848
6849 tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
6850
6851 if (!tg3_flag(tp, 5705_PLUS))
6852 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
6853
6854 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
6855 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
6856 else
6857 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
6858 } else {
6859 tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
6860
6861 if (tg3_flag(tp, 5705_PLUS) ||
6862 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) ||
6863 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
6864 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
6865 }
6866
6867 tw32(MAC_MODE, tp->mac_mode);
6868 udelay(40);
6869}
6870
Matt Carlson941ec902011-08-19 13:58:23 +00006871static int tg3_phy_lpbk_set(struct tg3 *tp, u32 speed, bool extlpbk)
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006872{
Matt Carlson941ec902011-08-19 13:58:23 +00006873 u32 val, bmcr, mac_mode, ptest = 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006874
6875 tg3_phy_toggle_apd(tp, false);
6876 tg3_phy_toggle_automdix(tp, 0);
6877
Matt Carlson941ec902011-08-19 13:58:23 +00006878 if (extlpbk && tg3_phy_set_extloopbk(tp))
6879 return -EIO;
6880
6881 bmcr = BMCR_FULLDPLX;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006882 switch (speed) {
6883 case SPEED_10:
6884 break;
6885 case SPEED_100:
6886 bmcr |= BMCR_SPEED100;
6887 break;
6888 case SPEED_1000:
6889 default:
6890 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
6891 speed = SPEED_100;
6892 bmcr |= BMCR_SPEED100;
6893 } else {
6894 speed = SPEED_1000;
6895 bmcr |= BMCR_SPEED1000;
6896 }
6897 }
6898
Matt Carlson941ec902011-08-19 13:58:23 +00006899 if (extlpbk) {
6900 if (!(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
6901 tg3_readphy(tp, MII_CTRL1000, &val);
6902 val |= CTL1000_AS_MASTER |
6903 CTL1000_ENABLE_MASTER;
6904 tg3_writephy(tp, MII_CTRL1000, val);
6905 } else {
6906 ptest = MII_TG3_FET_PTEST_TRIM_SEL |
6907 MII_TG3_FET_PTEST_TRIM_2;
6908 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest);
6909 }
6910 } else
6911 bmcr |= BMCR_LOOPBACK;
6912
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006913 tg3_writephy(tp, MII_BMCR, bmcr);
6914
6915 /* The write needs to be flushed for the FETs */
6916 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
6917 tg3_readphy(tp, MII_BMCR, &bmcr);
6918
6919 udelay(40);
6920
6921 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
6922 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Matt Carlson941ec902011-08-19 13:58:23 +00006923 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest |
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006924 MII_TG3_FET_PTEST_FRC_TX_LINK |
6925 MII_TG3_FET_PTEST_FRC_TX_LOCK);
6926
6927 /* The write needs to be flushed for the AC131 */
6928 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
6929 }
6930
6931 /* Reset to prevent losing 1st rx packet intermittently */
6932 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
6933 tg3_flag(tp, 5780_CLASS)) {
6934 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
6935 udelay(10);
6936 tw32_f(MAC_RX_MODE, tp->rx_mode);
6937 }
6938
6939 mac_mode = tp->mac_mode &
6940 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
6941 if (speed == SPEED_1000)
6942 mac_mode |= MAC_MODE_PORT_MODE_GMII;
6943 else
6944 mac_mode |= MAC_MODE_PORT_MODE_MII;
6945
6946 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
6947 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
6948
6949 if (masked_phy_id == TG3_PHY_ID_BCM5401)
6950 mac_mode &= ~MAC_MODE_LINK_POLARITY;
6951 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
6952 mac_mode |= MAC_MODE_LINK_POLARITY;
6953
6954 tg3_writephy(tp, MII_TG3_EXT_CTRL,
6955 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
6956 }
6957
6958 tw32(MAC_MODE, mac_mode);
6959 udelay(40);
Matt Carlson941ec902011-08-19 13:58:23 +00006960
6961 return 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006962}
6963
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006964static void tg3_set_loopback(struct net_device *dev, u32 features)
6965{
6966 struct tg3 *tp = netdev_priv(dev);
6967
6968 if (features & NETIF_F_LOOPBACK) {
6969 if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
6970 return;
6971
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006972 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00006973 tg3_mac_loopback(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006974 netif_carrier_on(tp->dev);
6975 spin_unlock_bh(&tp->lock);
6976 netdev_info(dev, "Internal MAC loopback mode enabled.\n");
6977 } else {
6978 if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
6979 return;
6980
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006981 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00006982 tg3_mac_loopback(tp, false);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006983 /* Force link status check */
6984 tg3_setup_phy(tp, 1);
6985 spin_unlock_bh(&tp->lock);
6986 netdev_info(dev, "Internal MAC loopback mode disabled.\n");
6987 }
6988}
6989
Michał Mirosławdc668912011-04-07 03:35:07 +00006990static u32 tg3_fix_features(struct net_device *dev, u32 features)
6991{
6992 struct tg3 *tp = netdev_priv(dev);
6993
Joe Perches63c3a662011-04-26 08:12:10 +00006994 if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
Michał Mirosławdc668912011-04-07 03:35:07 +00006995 features &= ~NETIF_F_ALL_TSO;
6996
6997 return features;
6998}
6999
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007000static int tg3_set_features(struct net_device *dev, u32 features)
7001{
7002 u32 changed = dev->features ^ features;
7003
7004 if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
7005 tg3_set_loopback(dev, features);
7006
7007 return 0;
7008}
7009
Linus Torvalds1da177e2005-04-16 15:20:36 -07007010static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
7011 int new_mtu)
7012{
7013 dev->mtu = new_mtu;
7014
Michael Chanef7f5ec2005-07-25 12:32:25 -07007015 if (new_mtu > ETH_DATA_LEN) {
Joe Perches63c3a662011-04-26 08:12:10 +00007016 if (tg3_flag(tp, 5780_CLASS)) {
Michał Mirosławdc668912011-04-07 03:35:07 +00007017 netdev_update_features(dev);
Joe Perches63c3a662011-04-26 08:12:10 +00007018 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson859a5882010-04-05 10:19:28 +00007019 } else {
Joe Perches63c3a662011-04-26 08:12:10 +00007020 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Matt Carlson859a5882010-04-05 10:19:28 +00007021 }
Michael Chanef7f5ec2005-07-25 12:32:25 -07007022 } else {
Joe Perches63c3a662011-04-26 08:12:10 +00007023 if (tg3_flag(tp, 5780_CLASS)) {
7024 tg3_flag_set(tp, TSO_CAPABLE);
Michał Mirosławdc668912011-04-07 03:35:07 +00007025 netdev_update_features(dev);
7026 }
Joe Perches63c3a662011-04-26 08:12:10 +00007027 tg3_flag_clear(tp, JUMBO_RING_ENABLE);
Michael Chanef7f5ec2005-07-25 12:32:25 -07007028 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007029}
7030
7031static int tg3_change_mtu(struct net_device *dev, int new_mtu)
7032{
7033 struct tg3 *tp = netdev_priv(dev);
Michael Chanb9ec6c12006-07-25 16:37:27 -07007034 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007035
7036 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
7037 return -EINVAL;
7038
7039 if (!netif_running(dev)) {
7040 /* We'll just catch it later when the
7041 * device is up'd.
7042 */
7043 tg3_set_mtu(dev, tp, new_mtu);
7044 return 0;
7045 }
7046
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07007047 tg3_phy_stop(tp);
7048
Linus Torvalds1da177e2005-04-16 15:20:36 -07007049 tg3_netif_stop(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07007050
7051 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007052
Michael Chan944d9802005-05-29 14:57:48 -07007053 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007054
7055 tg3_set_mtu(dev, tp, new_mtu);
7056
Michael Chanb9ec6c12006-07-25 16:37:27 -07007057 err = tg3_restart_hw(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007058
Michael Chanb9ec6c12006-07-25 16:37:27 -07007059 if (!err)
7060 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007061
David S. Millerf47c11e2005-06-24 20:18:35 -07007062 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007063
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07007064 if (!err)
7065 tg3_phy_start(tp);
7066
Michael Chanb9ec6c12006-07-25 16:37:27 -07007067 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007068}
7069
Matt Carlson21f581a2009-08-28 14:00:25 +00007070static void tg3_rx_prodring_free(struct tg3 *tp,
7071 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007072{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007073 int i;
7074
Matt Carlson8fea32b2010-09-15 08:59:58 +00007075 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007076 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00007077 i = (i + 1) & tp->rx_std_ring_mask)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007078 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
7079 tp->rx_pkt_map_sz);
7080
Joe Perches63c3a662011-04-26 08:12:10 +00007081 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007082 for (i = tpr->rx_jmb_cons_idx;
7083 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00007084 i = (i + 1) & tp->rx_jmb_ring_mask) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007085 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
7086 TG3_RX_JMB_MAP_SZ);
7087 }
7088 }
7089
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007090 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007092
Matt Carlson2c49a442010-09-30 10:34:35 +00007093 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007094 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
7095 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007096
Joe Perches63c3a662011-04-26 08:12:10 +00007097 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007098 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007099 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
7100 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007101 }
7102}
7103
Matt Carlsonc6cdf432010-04-05 10:19:26 +00007104/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007105 *
7106 * The chip has been shut down and the driver detached from
7107 * the networking, so no interrupts or new tx packets will
7108 * end up in the driver. tp->{tx,}lock are held and thus
7109 * we may not sleep.
7110 */
Matt Carlson21f581a2009-08-28 14:00:25 +00007111static int tg3_rx_prodring_alloc(struct tg3 *tp,
7112 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007113{
Matt Carlson287be122009-08-28 13:58:46 +00007114 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007115
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007116 tpr->rx_std_cons_idx = 0;
7117 tpr->rx_std_prod_idx = 0;
7118 tpr->rx_jmb_cons_idx = 0;
7119 tpr->rx_jmb_prod_idx = 0;
7120
Matt Carlson8fea32b2010-09-15 08:59:58 +00007121 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007122 memset(&tpr->rx_std_buffers[0], 0,
7123 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00007124 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007125 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00007126 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007127 goto done;
7128 }
7129
Linus Torvalds1da177e2005-04-16 15:20:36 -07007130 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00007131 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007132
Matt Carlson287be122009-08-28 13:58:46 +00007133 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00007134 if (tg3_flag(tp, 5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00007135 tp->dev->mtu > ETH_DATA_LEN)
7136 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
7137 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07007138
Linus Torvalds1da177e2005-04-16 15:20:36 -07007139 /* Initialize invariants of the rings, we only set this
7140 * stuff once. This works because the card does not
7141 * write into the rx buffer posting rings.
7142 */
Matt Carlson2c49a442010-09-30 10:34:35 +00007143 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007144 struct tg3_rx_buffer_desc *rxd;
7145
Matt Carlson21f581a2009-08-28 14:00:25 +00007146 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00007147 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007148 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
7149 rxd->opaque = (RXD_OPAQUE_RING_STD |
7150 (i << RXD_OPAQUE_INDEX_SHIFT));
7151 }
7152
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007153 /* Now allocate fresh SKBs for each rx ring. */
7154 for (i = 0; i < tp->rx_pending; i++) {
Matt Carlson86b21e52009-11-13 13:03:45 +00007155 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_STD, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007156 netdev_warn(tp->dev,
7157 "Using a smaller RX standard ring. Only "
7158 "%d out of %d buffers were allocated "
7159 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007160 if (i == 0)
7161 goto initfail;
7162 tp->rx_pending = i;
7163 break;
7164 }
7165 }
7166
Joe Perches63c3a662011-04-26 08:12:10 +00007167 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007168 goto done;
7169
Matt Carlson2c49a442010-09-30 10:34:35 +00007170 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007171
Joe Perches63c3a662011-04-26 08:12:10 +00007172 if (!tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson0d86df82010-02-17 15:17:00 +00007173 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007174
Matt Carlson2c49a442010-09-30 10:34:35 +00007175 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00007176 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007177
Matt Carlson0d86df82010-02-17 15:17:00 +00007178 rxd = &tpr->rx_jmb[i].std;
7179 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
7180 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
7181 RXD_FLAG_JUMBO;
7182 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
7183 (i << RXD_OPAQUE_INDEX_SHIFT));
7184 }
7185
7186 for (i = 0; i < tp->rx_jumbo_pending; i++) {
7187 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_JUMBO, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007188 netdev_warn(tp->dev,
7189 "Using a smaller RX jumbo ring. Only %d "
7190 "out of %d buffers were allocated "
7191 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00007192 if (i == 0)
7193 goto initfail;
7194 tp->rx_jumbo_pending = i;
7195 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007196 }
7197 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007198
7199done:
Michael Chan32d8c572006-07-25 16:38:29 -07007200 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007201
7202initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00007203 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007204 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007205}
7206
Matt Carlson21f581a2009-08-28 14:00:25 +00007207static void tg3_rx_prodring_fini(struct tg3 *tp,
7208 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007209{
Matt Carlson21f581a2009-08-28 14:00:25 +00007210 kfree(tpr->rx_std_buffers);
7211 tpr->rx_std_buffers = NULL;
7212 kfree(tpr->rx_jmb_buffers);
7213 tpr->rx_jmb_buffers = NULL;
7214 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007215 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
7216 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00007217 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007218 }
Matt Carlson21f581a2009-08-28 14:00:25 +00007219 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007220 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
7221 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00007222 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007223 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007224}
7225
Matt Carlson21f581a2009-08-28 14:00:25 +00007226static int tg3_rx_prodring_init(struct tg3 *tp,
7227 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007228{
Matt Carlson2c49a442010-09-30 10:34:35 +00007229 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
7230 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007231 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007232 return -ENOMEM;
7233
Matt Carlson4bae65c2010-11-24 08:31:52 +00007234 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
7235 TG3_RX_STD_RING_BYTES(tp),
7236 &tpr->rx_std_mapping,
7237 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007238 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007239 goto err_out;
7240
Joe Perches63c3a662011-04-26 08:12:10 +00007241 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007242 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00007243 GFP_KERNEL);
7244 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007245 goto err_out;
7246
Matt Carlson4bae65c2010-11-24 08:31:52 +00007247 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
7248 TG3_RX_JMB_RING_BYTES(tp),
7249 &tpr->rx_jmb_mapping,
7250 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007251 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007252 goto err_out;
7253 }
7254
7255 return 0;
7256
7257err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00007258 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007259 return -ENOMEM;
7260}
7261
7262/* Free up pending packets in all rx/tx rings.
7263 *
7264 * The chip has been shut down and the driver detached from
7265 * the networking, so no interrupts or new tx packets will
7266 * end up in the driver. tp->{tx,}lock is not held and we are not
7267 * in an interrupt context and thus may sleep.
7268 */
7269static void tg3_free_rings(struct tg3 *tp)
7270{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007271 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007272
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007273 for (j = 0; j < tp->irq_cnt; j++) {
7274 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007275
Matt Carlson8fea32b2010-09-15 08:59:58 +00007276 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00007277
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007278 if (!tnapi->tx_buffers)
7279 continue;
7280
Matt Carlson0d681b22011-07-27 14:20:49 +00007281 for (i = 0; i < TG3_TX_RING_SIZE; i++) {
7282 struct sk_buff *skb = tnapi->tx_buffers[i].skb;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007283
Matt Carlson0d681b22011-07-27 14:20:49 +00007284 if (!skb)
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007285 continue;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007286
Matt Carlson0d681b22011-07-27 14:20:49 +00007287 tg3_tx_skb_unmap(tnapi, i, skb_shinfo(skb)->nr_frags);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007288
7289 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007290 }
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007291 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007292}
7293
7294/* Initialize tx/rx rings for packet processing.
7295 *
7296 * The chip has been shut down and the driver detached from
7297 * the networking, so no interrupts or new tx packets will
7298 * end up in the driver. tp->{tx,}lock are held and thus
7299 * we may not sleep.
7300 */
7301static int tg3_init_rings(struct tg3 *tp)
7302{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007303 int i;
Matt Carlson72334482009-08-28 14:03:01 +00007304
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007305 /* Free up all the SKBs. */
7306 tg3_free_rings(tp);
7307
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007308 for (i = 0; i < tp->irq_cnt; i++) {
7309 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007310
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007311 tnapi->last_tag = 0;
7312 tnapi->last_irq_tag = 0;
7313 tnapi->hw_status->status = 0;
7314 tnapi->hw_status->status_tag = 0;
7315 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7316
7317 tnapi->tx_prod = 0;
7318 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007319 if (tnapi->tx_ring)
7320 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007321
7322 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007323 if (tnapi->rx_rcb)
7324 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007325
Matt Carlson8fea32b2010-09-15 08:59:58 +00007326 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00007327 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007328 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00007329 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007330 }
Matt Carlson72334482009-08-28 14:03:01 +00007331
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007332 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007333}
7334
7335/*
7336 * Must not be invoked with interrupt sources disabled and
7337 * the hardware shutdown down.
7338 */
7339static void tg3_free_consistent(struct tg3 *tp)
7340{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007341 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00007342
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007343 for (i = 0; i < tp->irq_cnt; i++) {
7344 struct tg3_napi *tnapi = &tp->napi[i];
7345
7346 if (tnapi->tx_ring) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007347 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007348 tnapi->tx_ring, tnapi->tx_desc_mapping);
7349 tnapi->tx_ring = NULL;
7350 }
7351
7352 kfree(tnapi->tx_buffers);
7353 tnapi->tx_buffers = NULL;
7354
7355 if (tnapi->rx_rcb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007356 dma_free_coherent(&tp->pdev->dev,
7357 TG3_RX_RCB_RING_BYTES(tp),
7358 tnapi->rx_rcb,
7359 tnapi->rx_rcb_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007360 tnapi->rx_rcb = NULL;
7361 }
7362
Matt Carlson8fea32b2010-09-15 08:59:58 +00007363 tg3_rx_prodring_fini(tp, &tnapi->prodring);
7364
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007365 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007366 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
7367 tnapi->hw_status,
7368 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007369 tnapi->hw_status = NULL;
7370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007371 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007372
Linus Torvalds1da177e2005-04-16 15:20:36 -07007373 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007374 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
7375 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007376 tp->hw_stats = NULL;
7377 }
7378}
7379
7380/*
7381 * Must not be invoked with interrupt sources disabled and
7382 * the hardware shutdown down. Can sleep.
7383 */
7384static int tg3_alloc_consistent(struct tg3 *tp)
7385{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007386 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00007387
Matt Carlson4bae65c2010-11-24 08:31:52 +00007388 tp->hw_stats = dma_alloc_coherent(&tp->pdev->dev,
7389 sizeof(struct tg3_hw_stats),
7390 &tp->stats_mapping,
7391 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007392 if (!tp->hw_stats)
7393 goto err_out;
7394
Linus Torvalds1da177e2005-04-16 15:20:36 -07007395 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
7396
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007397 for (i = 0; i < tp->irq_cnt; i++) {
7398 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007399 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007400
Matt Carlson4bae65c2010-11-24 08:31:52 +00007401 tnapi->hw_status = dma_alloc_coherent(&tp->pdev->dev,
7402 TG3_HW_STATUS_SIZE,
7403 &tnapi->status_mapping,
7404 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007405 if (!tnapi->hw_status)
7406 goto err_out;
7407
7408 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007409 sblk = tnapi->hw_status;
7410
Matt Carlson8fea32b2010-09-15 08:59:58 +00007411 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
7412 goto err_out;
7413
Matt Carlson19cfaec2009-12-03 08:36:20 +00007414 /* If multivector TSS is enabled, vector 0 does not handle
7415 * tx interrupts. Don't allocate any resources for it.
7416 */
Joe Perches63c3a662011-04-26 08:12:10 +00007417 if ((!i && !tg3_flag(tp, ENABLE_TSS)) ||
7418 (i && tg3_flag(tp, ENABLE_TSS))) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00007419 tnapi->tx_buffers = kzalloc(
7420 sizeof(struct tg3_tx_ring_info) *
7421 TG3_TX_RING_SIZE, GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007422 if (!tnapi->tx_buffers)
7423 goto err_out;
7424
Matt Carlson4bae65c2010-11-24 08:31:52 +00007425 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
7426 TG3_TX_RING_BYTES,
7427 &tnapi->tx_desc_mapping,
7428 GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007429 if (!tnapi->tx_ring)
7430 goto err_out;
7431 }
7432
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007433 /*
7434 * When RSS is enabled, the status block format changes
7435 * slightly. The "rx_jumbo_consumer", "reserved",
7436 * and "rx_mini_consumer" members get mapped to the
7437 * other three rx return ring producer indexes.
7438 */
7439 switch (i) {
7440 default:
7441 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
7442 break;
7443 case 2:
7444 tnapi->rx_rcb_prod_idx = &sblk->rx_jumbo_consumer;
7445 break;
7446 case 3:
7447 tnapi->rx_rcb_prod_idx = &sblk->reserved;
7448 break;
7449 case 4:
7450 tnapi->rx_rcb_prod_idx = &sblk->rx_mini_consumer;
7451 break;
7452 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007453
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007454 /*
7455 * If multivector RSS is enabled, vector 0 does not handle
7456 * rx or tx interrupts. Don't allocate any resources for it.
7457 */
Joe Perches63c3a662011-04-26 08:12:10 +00007458 if (!i && tg3_flag(tp, ENABLE_RSS))
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007459 continue;
7460
Matt Carlson4bae65c2010-11-24 08:31:52 +00007461 tnapi->rx_rcb = dma_alloc_coherent(&tp->pdev->dev,
7462 TG3_RX_RCB_RING_BYTES(tp),
7463 &tnapi->rx_rcb_mapping,
7464 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007465 if (!tnapi->rx_rcb)
7466 goto err_out;
7467
7468 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007469 }
7470
Linus Torvalds1da177e2005-04-16 15:20:36 -07007471 return 0;
7472
7473err_out:
7474 tg3_free_consistent(tp);
7475 return -ENOMEM;
7476}
7477
7478#define MAX_WAIT_CNT 1000
7479
7480/* To stop a block, clear the enable bit and poll till it
7481 * clears. tp->lock is held.
7482 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007483static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007484{
7485 unsigned int i;
7486 u32 val;
7487
Joe Perches63c3a662011-04-26 08:12:10 +00007488 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007489 switch (ofs) {
7490 case RCVLSC_MODE:
7491 case DMAC_MODE:
7492 case MBFREE_MODE:
7493 case BUFMGR_MODE:
7494 case MEMARB_MODE:
7495 /* We can't enable/disable these bits of the
7496 * 5705/5750, just say success.
7497 */
7498 return 0;
7499
7500 default:
7501 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007502 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007503 }
7504
7505 val = tr32(ofs);
7506 val &= ~enable_bit;
7507 tw32_f(ofs, val);
7508
7509 for (i = 0; i < MAX_WAIT_CNT; i++) {
7510 udelay(100);
7511 val = tr32(ofs);
7512 if ((val & enable_bit) == 0)
7513 break;
7514 }
7515
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007516 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00007517 dev_err(&tp->pdev->dev,
7518 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
7519 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007520 return -ENODEV;
7521 }
7522
7523 return 0;
7524}
7525
7526/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007527static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007528{
7529 int i, err;
7530
7531 tg3_disable_ints(tp);
7532
7533 tp->rx_mode &= ~RX_MODE_ENABLE;
7534 tw32_f(MAC_RX_MODE, tp->rx_mode);
7535 udelay(10);
7536
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007537 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
7538 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
7539 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
7540 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
7541 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
7542 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007543
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007544 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
7545 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
7546 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
7547 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
7548 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
7549 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
7550 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007551
7552 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
7553 tw32_f(MAC_MODE, tp->mac_mode);
7554 udelay(40);
7555
7556 tp->tx_mode &= ~TX_MODE_ENABLE;
7557 tw32_f(MAC_TX_MODE, tp->tx_mode);
7558
7559 for (i = 0; i < MAX_WAIT_CNT; i++) {
7560 udelay(100);
7561 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
7562 break;
7563 }
7564 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00007565 dev_err(&tp->pdev->dev,
7566 "%s timed out, TX_MODE_ENABLE will not clear "
7567 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07007568 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007569 }
7570
Michael Chane6de8ad2005-05-05 14:42:41 -07007571 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007572 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
7573 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007574
7575 tw32(FTQ_RESET, 0xffffffff);
7576 tw32(FTQ_RESET, 0x00000000);
7577
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007578 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
7579 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007580
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007581 for (i = 0; i < tp->irq_cnt; i++) {
7582 struct tg3_napi *tnapi = &tp->napi[i];
7583 if (tnapi->hw_status)
7584 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007586 if (tp->hw_stats)
7587 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
7588
Linus Torvalds1da177e2005-04-16 15:20:36 -07007589 return err;
7590}
7591
Michael Chanee6a99b2007-07-18 21:49:10 -07007592/* Save PCI command register before chip reset */
7593static void tg3_save_pci_state(struct tg3 *tp)
7594{
Matt Carlson8a6eac92007-10-21 16:17:55 -07007595 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007596}
7597
7598/* Restore PCI state after chip reset */
7599static void tg3_restore_pci_state(struct tg3 *tp)
7600{
7601 u32 val;
7602
7603 /* Re-enable indirect register accesses. */
7604 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
7605 tp->misc_host_ctrl);
7606
7607 /* Set MAX PCI retry to zero. */
7608 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
7609 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007610 tg3_flag(tp, PCIX_MODE))
Michael Chanee6a99b2007-07-18 21:49:10 -07007611 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07007612 /* Allow reads and writes to the APE register and memory space. */
Joe Perches63c3a662011-04-26 08:12:10 +00007613 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07007614 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00007615 PCISTATE_ALLOW_APE_SHMEM_WR |
7616 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07007617 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
7618
Matt Carlson8a6eac92007-10-21 16:17:55 -07007619 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007620
Matt Carlsonfcb389d2008-11-03 16:55:44 -08007621 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785) {
Joe Perches63c3a662011-04-26 08:12:10 +00007622 if (tg3_flag(tp, PCI_EXPRESS))
Matt Carlsoncf790032010-11-24 08:31:48 +00007623 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlsonfcb389d2008-11-03 16:55:44 -08007624 else {
7625 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
7626 tp->pci_cacheline_sz);
7627 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
7628 tp->pci_lat_timer);
7629 }
Michael Chan114342f2007-10-15 02:12:26 -07007630 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08007631
Michael Chanee6a99b2007-07-18 21:49:10 -07007632 /* Make sure PCI-X relaxed ordering bit is clear. */
Joe Perches63c3a662011-04-26 08:12:10 +00007633 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07007634 u16 pcix_cmd;
7635
7636 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7637 &pcix_cmd);
7638 pcix_cmd &= ~PCI_X_CMD_ERO;
7639 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7640 pcix_cmd);
7641 }
Michael Chanee6a99b2007-07-18 21:49:10 -07007642
Joe Perches63c3a662011-04-26 08:12:10 +00007643 if (tg3_flag(tp, 5780_CLASS)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007644
7645 /* Chip reset on 5780 will reset MSI enable bit,
7646 * so need to restore it.
7647 */
Joe Perches63c3a662011-04-26 08:12:10 +00007648 if (tg3_flag(tp, USING_MSI)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007649 u16 ctrl;
7650
7651 pci_read_config_word(tp->pdev,
7652 tp->msi_cap + PCI_MSI_FLAGS,
7653 &ctrl);
7654 pci_write_config_word(tp->pdev,
7655 tp->msi_cap + PCI_MSI_FLAGS,
7656 ctrl | PCI_MSI_FLAGS_ENABLE);
7657 val = tr32(MSGINT_MODE);
7658 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
7659 }
7660 }
7661}
7662
Linus Torvalds1da177e2005-04-16 15:20:36 -07007663/* tp->lock is held. */
7664static int tg3_chip_reset(struct tg3 *tp)
7665{
7666 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07007667 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00007668 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007669
David S. Millerf49639e2006-06-09 11:58:36 -07007670 tg3_nvram_lock(tp);
7671
Matt Carlson77b483f2008-08-15 14:07:24 -07007672 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
7673
David S. Millerf49639e2006-06-09 11:58:36 -07007674 /* No matching tg3_nvram_unlock() after this because
7675 * chip reset below will undo the nvram lock.
7676 */
7677 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007678
Michael Chanee6a99b2007-07-18 21:49:10 -07007679 /* GRC_MISC_CFG core clock reset will clear the memory
7680 * enable bit in PCI register 4 and the MSI enable bit
7681 * on some chips, so we save relevant registers here.
7682 */
7683 tg3_save_pci_state(tp);
7684
Michael Chand9ab5ad2006-03-20 22:27:35 -08007685 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Joe Perches63c3a662011-04-26 08:12:10 +00007686 tg3_flag(tp, 5755_PLUS))
Michael Chand9ab5ad2006-03-20 22:27:35 -08007687 tw32(GRC_FASTBOOT_PC, 0);
7688
Linus Torvalds1da177e2005-04-16 15:20:36 -07007689 /*
7690 * We must avoid the readl() that normally takes place.
7691 * It locks machines, causes machine checks, and other
7692 * fun things. So, temporarily disable the 5701
7693 * hardware workaround, while we do the reset.
7694 */
Michael Chan1ee582d2005-08-09 20:16:46 -07007695 write_op = tp->write32;
7696 if (write_op == tg3_write_flush_reg32)
7697 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007698
Michael Chand18edcb2007-03-24 20:57:11 -07007699 /* Prevent the irq handler from reading or writing PCI registers
7700 * during chip reset when the memory enable bit in the PCI command
7701 * register may be cleared. The chip does not generate interrupt
7702 * at this time, but the irq handler may still be called due to irq
7703 * sharing or irqpoll.
7704 */
Joe Perches63c3a662011-04-26 08:12:10 +00007705 tg3_flag_set(tp, CHIP_RESETTING);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007706 for (i = 0; i < tp->irq_cnt; i++) {
7707 struct tg3_napi *tnapi = &tp->napi[i];
7708 if (tnapi->hw_status) {
7709 tnapi->hw_status->status = 0;
7710 tnapi->hw_status->status_tag = 0;
7711 }
7712 tnapi->last_tag = 0;
7713 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07007714 }
Michael Chand18edcb2007-03-24 20:57:11 -07007715 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00007716
7717 for (i = 0; i < tp->irq_cnt; i++)
7718 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07007719
Matt Carlson255ca312009-08-25 10:07:27 +00007720 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7721 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7722 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
7723 }
7724
Linus Torvalds1da177e2005-04-16 15:20:36 -07007725 /* do the reset */
7726 val = GRC_MISC_CFG_CORECLK_RESET;
7727
Joe Perches63c3a662011-04-26 08:12:10 +00007728 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson88075d92010-08-02 11:25:58 +00007729 /* Force PCIe 1.0a mode */
7730 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007731 !tg3_flag(tp, 57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00007732 tr32(TG3_PCIE_PHY_TSTCTL) ==
7733 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
7734 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
7735
Linus Torvalds1da177e2005-04-16 15:20:36 -07007736 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
7737 tw32(GRC_MISC_CFG, (1 << 29));
7738 val |= (1 << 29);
7739 }
7740 }
7741
Michael Chanb5d37722006-09-27 16:06:21 -07007742 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7743 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
7744 tw32(GRC_VCPU_EXT_CTRL,
7745 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
7746 }
7747
Matt Carlsonf37500d2010-08-02 11:25:59 +00007748 /* Manage gphy power for all CPMU absent PCIe devices. */
Joe Perches63c3a662011-04-26 08:12:10 +00007749 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007750 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00007751
Linus Torvalds1da177e2005-04-16 15:20:36 -07007752 tw32(GRC_MISC_CFG, val);
7753
Michael Chan1ee582d2005-08-09 20:16:46 -07007754 /* restore 5701 hardware bug workaround write method */
7755 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007756
7757 /* Unfortunately, we have to delay before the PCI read back.
7758 * Some 575X chips even will not respond to a PCI cfg access
7759 * when the reset command is given to the chip.
7760 *
7761 * How do these hardware designers expect things to work
7762 * properly if the PCI write is posted for a long period
7763 * of time? It is always necessary to have some method by
7764 * which a register read back can occur to push the write
7765 * out which does the reset.
7766 *
7767 * For most tg3 variants the trick below was working.
7768 * Ho hum...
7769 */
7770 udelay(120);
7771
7772 /* Flush PCI posted writes. The normal MMIO registers
7773 * are inaccessible at this time so this is the only
7774 * way to make this reliably (actually, this is no longer
7775 * the case, see above). I tried to use indirect
7776 * register read/write but this upset some 5701 variants.
7777 */
7778 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
7779
7780 udelay(120);
7781
Jon Mason708ebb32011-06-27 12:56:50 +00007782 if (tg3_flag(tp, PCI_EXPRESS) && pci_pcie_cap(tp->pdev)) {
Matt Carlsone7126992009-08-25 10:08:16 +00007783 u16 val16;
7784
Linus Torvalds1da177e2005-04-16 15:20:36 -07007785 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
7786 int i;
7787 u32 cfg_val;
7788
7789 /* Wait for link training to complete. */
7790 for (i = 0; i < 5000; i++)
7791 udelay(100);
7792
7793 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
7794 pci_write_config_dword(tp->pdev, 0xc4,
7795 cfg_val | (1 << 15));
7796 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007797
Matt Carlsone7126992009-08-25 10:08:16 +00007798 /* Clear the "no snoop" and "relaxed ordering" bits. */
7799 pci_read_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00007800 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007801 &val16);
7802 val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
7803 PCI_EXP_DEVCTL_NOSNOOP_EN);
7804 /*
7805 * Older PCIe devices only support the 128 byte
7806 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007807 */
Joe Perches63c3a662011-04-26 08:12:10 +00007808 if (!tg3_flag(tp, CPMU_PRESENT))
Matt Carlsone7126992009-08-25 10:08:16 +00007809 val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007810 pci_write_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00007811 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007812 val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007813
Matt Carlsoncf790032010-11-24 08:31:48 +00007814 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007815
7816 /* Clear error status */
7817 pci_write_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00007818 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVSTA,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007819 PCI_EXP_DEVSTA_CED |
7820 PCI_EXP_DEVSTA_NFED |
7821 PCI_EXP_DEVSTA_FED |
7822 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007823 }
7824
Michael Chanee6a99b2007-07-18 21:49:10 -07007825 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007826
Joe Perches63c3a662011-04-26 08:12:10 +00007827 tg3_flag_clear(tp, CHIP_RESETTING);
7828 tg3_flag_clear(tp, ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07007829
Michael Chanee6a99b2007-07-18 21:49:10 -07007830 val = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00007831 if (tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07007832 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07007833 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007834
7835 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
7836 tg3_stop_fw(tp);
7837 tw32(0x5000, 0x400);
7838 }
7839
7840 tw32(GRC_MODE, tp->grc_mode);
7841
7842 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007843 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007844
7845 tw32(0xc4, val | (1 << 15));
7846 }
7847
7848 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
7849 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
7850 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
7851 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
7852 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
7853 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7854 }
7855
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007856 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00007857 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007858 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007859 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00007860 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007861 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007862 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007863 val = 0;
7864
7865 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007866 udelay(40);
7867
Matt Carlson77b483f2008-08-15 14:07:24 -07007868 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
7869
Michael Chan7a6f4362006-09-27 16:03:31 -07007870 err = tg3_poll_fw(tp);
7871 if (err)
7872 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007873
Matt Carlson0a9140c2009-08-28 12:27:50 +00007874 tg3_mdio_start(tp);
7875
Joe Perches63c3a662011-04-26 08:12:10 +00007876 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007877 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
7878 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007879 !tg3_flag(tp, 57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007880 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007881
7882 tw32(0x7c00, val | (1 << 25));
7883 }
7884
Matt Carlsond78b59f2011-04-05 14:22:46 +00007885 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
7886 val = tr32(TG3_CPMU_CLCK_ORIDE);
7887 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
7888 }
7889
Linus Torvalds1da177e2005-04-16 15:20:36 -07007890 /* Reprobe ASF enable state. */
Joe Perches63c3a662011-04-26 08:12:10 +00007891 tg3_flag_clear(tp, ENABLE_ASF);
7892 tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007893 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
7894 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
7895 u32 nic_cfg;
7896
7897 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
7898 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +00007899 tg3_flag_set(tp, ENABLE_ASF);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007900 tp->last_event_jiffies = jiffies;
Joe Perches63c3a662011-04-26 08:12:10 +00007901 if (tg3_flag(tp, 5750_PLUS))
7902 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007903 }
7904 }
7905
7906 return 0;
7907}
7908
7909/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07007910static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007911{
7912 int err;
7913
7914 tg3_stop_fw(tp);
7915
Michael Chan944d9802005-05-29 14:57:48 -07007916 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007917
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007918 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007919 err = tg3_chip_reset(tp);
7920
Matt Carlsondaba2a62009-04-20 06:58:52 +00007921 __tg3_set_mac_addr(tp, 0);
7922
Michael Chan944d9802005-05-29 14:57:48 -07007923 tg3_write_sig_legacy(tp, kind);
7924 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007925
7926 if (err)
7927 return err;
7928
7929 return 0;
7930}
7931
Linus Torvalds1da177e2005-04-16 15:20:36 -07007932static int tg3_set_mac_addr(struct net_device *dev, void *p)
7933{
7934 struct tg3 *tp = netdev_priv(dev);
7935 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07007936 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007937
Michael Chanf9804dd2005-09-27 12:13:10 -07007938 if (!is_valid_ether_addr(addr->sa_data))
7939 return -EINVAL;
7940
Linus Torvalds1da177e2005-04-16 15:20:36 -07007941 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7942
Michael Chane75f7c92006-03-20 21:33:26 -08007943 if (!netif_running(dev))
7944 return 0;
7945
Joe Perches63c3a662011-04-26 08:12:10 +00007946 if (tg3_flag(tp, ENABLE_ASF)) {
Michael Chan986e0ae2007-05-05 12:10:20 -07007947 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07007948
Michael Chan986e0ae2007-05-05 12:10:20 -07007949 addr0_high = tr32(MAC_ADDR_0_HIGH);
7950 addr0_low = tr32(MAC_ADDR_0_LOW);
7951 addr1_high = tr32(MAC_ADDR_1_HIGH);
7952 addr1_low = tr32(MAC_ADDR_1_LOW);
7953
7954 /* Skip MAC addr 1 if ASF is using it. */
7955 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
7956 !(addr1_high == 0 && addr1_low == 0))
7957 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07007958 }
Michael Chan986e0ae2007-05-05 12:10:20 -07007959 spin_lock_bh(&tp->lock);
7960 __tg3_set_mac_addr(tp, skip_mac_1);
7961 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007962
Michael Chanb9ec6c12006-07-25 16:37:27 -07007963 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007964}
7965
7966/* tp->lock is held. */
7967static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
7968 dma_addr_t mapping, u32 maxlen_flags,
7969 u32 nic_addr)
7970{
7971 tg3_write_mem(tp,
7972 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
7973 ((u64) mapping >> 32));
7974 tg3_write_mem(tp,
7975 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
7976 ((u64) mapping & 0xffffffff));
7977 tg3_write_mem(tp,
7978 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
7979 maxlen_flags);
7980
Joe Perches63c3a662011-04-26 08:12:10 +00007981 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007982 tg3_write_mem(tp,
7983 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
7984 nic_addr);
7985}
7986
7987static void __tg3_set_rx_mode(struct net_device *);
Michael Chand244c892005-07-05 14:42:33 -07007988static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07007989{
Matt Carlsonb6080e12009-09-01 13:12:00 +00007990 int i;
7991
Joe Perches63c3a662011-04-26 08:12:10 +00007992 if (!tg3_flag(tp, ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007993 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
7994 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
7995 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007996 } else {
7997 tw32(HOSTCC_TXCOL_TICKS, 0);
7998 tw32(HOSTCC_TXMAX_FRAMES, 0);
7999 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00008000 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008001
Joe Perches63c3a662011-04-26 08:12:10 +00008002 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008003 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
8004 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
8005 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
8006 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00008007 tw32(HOSTCC_RXCOL_TICKS, 0);
8008 tw32(HOSTCC_RXMAX_FRAMES, 0);
8009 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07008010 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008011
Joe Perches63c3a662011-04-26 08:12:10 +00008012 if (!tg3_flag(tp, 5705_PLUS)) {
David S. Miller15f98502005-05-18 22:49:26 -07008013 u32 val = ec->stats_block_coalesce_usecs;
8014
Matt Carlsonb6080e12009-09-01 13:12:00 +00008015 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
8016 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
8017
David S. Miller15f98502005-05-18 22:49:26 -07008018 if (!netif_carrier_ok(tp->dev))
8019 val = 0;
8020
8021 tw32(HOSTCC_STAT_COAL_TICKS, val);
8022 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008023
8024 for (i = 0; i < tp->irq_cnt - 1; i++) {
8025 u32 reg;
8026
8027 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
8028 tw32(reg, ec->rx_coalesce_usecs);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008029 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
8030 tw32(reg, ec->rx_max_coalesced_frames);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008031 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
8032 tw32(reg, ec->rx_max_coalesced_frames_irq);
Matt Carlson19cfaec2009-12-03 08:36:20 +00008033
Joe Perches63c3a662011-04-26 08:12:10 +00008034 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008035 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
8036 tw32(reg, ec->tx_coalesce_usecs);
8037 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
8038 tw32(reg, ec->tx_max_coalesced_frames);
8039 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
8040 tw32(reg, ec->tx_max_coalesced_frames_irq);
8041 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008042 }
8043
8044 for (; i < tp->irq_max - 1; i++) {
8045 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008046 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008047 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00008048
Joe Perches63c3a662011-04-26 08:12:10 +00008049 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008050 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
8051 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
8052 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
8053 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008054 }
David S. Miller15f98502005-05-18 22:49:26 -07008055}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008056
8057/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00008058static void tg3_rings_reset(struct tg3 *tp)
8059{
8060 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008061 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008062 struct tg3_napi *tnapi = &tp->napi[0];
8063
8064 /* Disable all transmit rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008065 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008066 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Joe Perches63c3a662011-04-26 08:12:10 +00008067 else if (tg3_flag(tp, 5717_PLUS))
Matt Carlson3d377282010-10-14 10:37:39 +00008068 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
Matt Carlsonb703df62009-12-03 08:36:21 +00008069 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8070 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008071 else
8072 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8073
8074 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8075 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
8076 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
8077 BDINFO_FLAGS_DISABLED);
8078
8079
8080 /* Disable all receive return rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008081 if (tg3_flag(tp, 5717_PLUS))
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008082 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
Joe Perches63c3a662011-04-26 08:12:10 +00008083 else if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008084 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00008085 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
8086 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson2d31eca2009-09-01 12:53:31 +00008087 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
8088 else
8089 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8090
8091 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8092 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
8093 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
8094 BDINFO_FLAGS_DISABLED);
8095
8096 /* Disable interrupts */
8097 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008098 tp->napi[0].chk_msi_cnt = 0;
8099 tp->napi[0].last_rx_cons = 0;
8100 tp->napi[0].last_tx_cons = 0;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008101
8102 /* Zero mailbox registers. */
Joe Perches63c3a662011-04-26 08:12:10 +00008103 if (tg3_flag(tp, SUPPORT_MSIX)) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00008104 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008105 tp->napi[i].tx_prod = 0;
8106 tp->napi[i].tx_cons = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00008107 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008108 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008109 tw32_rx_mbox(tp->napi[i].consmbox, 0);
8110 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
Matt Carlson7f230732011-08-31 11:44:48 +00008111 tp->napi[i].chk_msi_cnt = 0;
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008112 tp->napi[i].last_rx_cons = 0;
8113 tp->napi[i].last_tx_cons = 0;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008114 }
Joe Perches63c3a662011-04-26 08:12:10 +00008115 if (!tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008116 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008117 } else {
8118 tp->napi[0].tx_prod = 0;
8119 tp->napi[0].tx_cons = 0;
8120 tw32_mailbox(tp->napi[0].prodmbox, 0);
8121 tw32_rx_mbox(tp->napi[0].consmbox, 0);
8122 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008123
8124 /* Make sure the NIC-based send BD rings are disabled. */
Joe Perches63c3a662011-04-26 08:12:10 +00008125 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson2d31eca2009-09-01 12:53:31 +00008126 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
8127 for (i = 0; i < 16; i++)
8128 tw32_tx_mbox(mbox + i * 8, 0);
8129 }
8130
8131 txrcb = NIC_SRAM_SEND_RCB;
8132 rxrcb = NIC_SRAM_RCV_RET_RCB;
8133
8134 /* Clear status block in ram. */
8135 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8136
8137 /* Set status block DMA address */
8138 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8139 ((u64) tnapi->status_mapping >> 32));
8140 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8141 ((u64) tnapi->status_mapping & 0xffffffff));
8142
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008143 if (tnapi->tx_ring) {
8144 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8145 (TG3_TX_RING_SIZE <<
8146 BDINFO_FLAGS_MAXLEN_SHIFT),
8147 NIC_SRAM_TX_BUFFER_DESC);
8148 txrcb += TG3_BDINFO_SIZE;
8149 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008150
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008151 if (tnapi->rx_rcb) {
8152 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008153 (tp->rx_ret_ring_mask + 1) <<
8154 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008155 rxrcb += TG3_BDINFO_SIZE;
8156 }
8157
8158 stblk = HOSTCC_STATBLCK_RING1;
8159
8160 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
8161 u64 mapping = (u64)tnapi->status_mapping;
8162 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
8163 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
8164
8165 /* Clear status block in ram. */
8166 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8167
Matt Carlson19cfaec2009-12-03 08:36:20 +00008168 if (tnapi->tx_ring) {
8169 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8170 (TG3_TX_RING_SIZE <<
8171 BDINFO_FLAGS_MAXLEN_SHIFT),
8172 NIC_SRAM_TX_BUFFER_DESC);
8173 txrcb += TG3_BDINFO_SIZE;
8174 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008175
8176 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008177 ((tp->rx_ret_ring_mask + 1) <<
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008178 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
8179
8180 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008181 rxrcb += TG3_BDINFO_SIZE;
8182 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008183}
8184
Matt Carlsoneb07a942011-04-20 07:57:36 +00008185static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
8186{
8187 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
8188
Joe Perches63c3a662011-04-26 08:12:10 +00008189 if (!tg3_flag(tp, 5750_PLUS) ||
8190 tg3_flag(tp, 5780_CLASS) ||
Matt Carlsoneb07a942011-04-20 07:57:36 +00008191 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
8192 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
8193 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
8194 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
8195 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787)
8196 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
8197 else
8198 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
8199
8200 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
8201 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
8202
8203 val = min(nic_rep_thresh, host_rep_thresh);
8204 tw32(RCVBDI_STD_THRESH, val);
8205
Joe Perches63c3a662011-04-26 08:12:10 +00008206 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008207 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
8208
Joe Perches63c3a662011-04-26 08:12:10 +00008209 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008210 return;
8211
Joe Perches63c3a662011-04-26 08:12:10 +00008212 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008213 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
8214 else
8215 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5717;
8216
8217 host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
8218
8219 val = min(bdcache_maxcnt / 2, host_rep_thresh);
8220 tw32(RCVBDI_JUMBO_THRESH, val);
8221
Joe Perches63c3a662011-04-26 08:12:10 +00008222 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008223 tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
8224}
8225
Matt Carlson2d31eca2009-09-01 12:53:31 +00008226/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008227static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008228{
8229 u32 val, rdmac_mode;
8230 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00008231 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008232
8233 tg3_disable_ints(tp);
8234
8235 tg3_stop_fw(tp);
8236
8237 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
8238
Joe Perches63c3a662011-04-26 08:12:10 +00008239 if (tg3_flag(tp, INIT_COMPLETE))
Michael Chane6de8ad2005-05-05 14:42:41 -07008240 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008241
Matt Carlson699c0192010-12-06 08:28:51 +00008242 /* Enable MAC control of LPI */
8243 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
8244 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL,
8245 TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
8246 TG3_CPMU_EEE_LNKIDL_UART_IDL);
8247
8248 tw32_f(TG3_CPMU_EEE_CTRL,
8249 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
8250
Matt Carlsona386b902010-12-06 08:28:53 +00008251 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
8252 TG3_CPMU_EEEMD_LPI_IN_TX |
8253 TG3_CPMU_EEEMD_LPI_IN_RX |
8254 TG3_CPMU_EEEMD_EEE_ENABLE;
8255
8256 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8257 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
8258
Joe Perches63c3a662011-04-26 08:12:10 +00008259 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsona386b902010-12-06 08:28:53 +00008260 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
8261
8262 tw32_f(TG3_CPMU_EEE_MODE, val);
8263
8264 tw32_f(TG3_CPMU_EEE_DBTMR1,
8265 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
8266 TG3_CPMU_DBTMR1_LNKIDLE_2047US);
8267
8268 tw32_f(TG3_CPMU_EEE_DBTMR2,
Matt Carlsond7f2ab22011-01-25 15:58:56 +00008269 TG3_CPMU_DBTMR2_APE_TX_2047US |
Matt Carlsona386b902010-12-06 08:28:53 +00008270 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
Matt Carlson699c0192010-12-06 08:28:51 +00008271 }
8272
Matt Carlson603f1172010-02-12 14:47:10 +00008273 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08008274 tg3_phy_reset(tp);
8275
Linus Torvalds1da177e2005-04-16 15:20:36 -07008276 err = tg3_chip_reset(tp);
8277 if (err)
8278 return err;
8279
8280 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
8281
Matt Carlsonbcb37f62008-11-03 16:52:09 -08008282 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008283 val = tr32(TG3_CPMU_CTRL);
8284 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
8285 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08008286
8287 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8288 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8289 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8290 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
8291
8292 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
8293 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
8294 val |= CPMU_LNK_AWARE_MACCLK_6_25;
8295 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
8296
8297 val = tr32(TG3_CPMU_HST_ACC);
8298 val &= ~CPMU_HST_ACC_MACCLK_MASK;
8299 val |= CPMU_HST_ACC_MACCLK_6_25;
8300 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07008301 }
8302
Matt Carlson33466d92009-04-20 06:57:41 +00008303 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
8304 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
8305 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
8306 PCIE_PWR_MGMT_L1_THRESH_4MS;
8307 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00008308
8309 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
8310 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
8311
8312 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00008313
Matt Carlsonf40386c2009-11-02 14:24:02 +00008314 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
8315 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00008316 }
8317
Joe Perches63c3a662011-04-26 08:12:10 +00008318 if (tg3_flag(tp, L1PLLPD_EN)) {
Matt Carlson614b0592010-01-20 16:58:02 +00008319 u32 grc_mode = tr32(GRC_MODE);
8320
8321 /* Access the lower 1K of PL PCIE block registers. */
8322 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8323 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
8324
8325 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
8326 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
8327 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
8328
8329 tw32(GRC_MODE, grc_mode);
8330 }
8331
Matt Carlson5093eed2010-11-24 08:31:45 +00008332 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
8333 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
8334 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00008335
Matt Carlson5093eed2010-11-24 08:31:45 +00008336 /* Access the lower 1K of PL PCIE block registers. */
8337 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8338 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00008339
Matt Carlson5093eed2010-11-24 08:31:45 +00008340 val = tr32(TG3_PCIE_TLDLPL_PORT +
8341 TG3_PCIE_PL_LO_PHYCTL5);
8342 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
8343 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00008344
Matt Carlson5093eed2010-11-24 08:31:45 +00008345 tw32(GRC_MODE, grc_mode);
8346 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00008347
Matt Carlson1ff30a52011-05-19 12:12:46 +00008348 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_57765_AX) {
8349 u32 grc_mode = tr32(GRC_MODE);
8350
8351 /* Access the lower 1K of DL PCIE block registers. */
8352 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8353 tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
8354
8355 val = tr32(TG3_PCIE_TLDLPL_PORT +
8356 TG3_PCIE_DL_LO_FTSMAX);
8357 val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
8358 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
8359 val | TG3_PCIE_DL_LO_FTSMAX_VAL);
8360
8361 tw32(GRC_MODE, grc_mode);
8362 }
8363
Matt Carlsona977dbe2010-04-12 06:58:26 +00008364 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8365 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8366 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8367 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00008368 }
8369
Linus Torvalds1da177e2005-04-16 15:20:36 -07008370 /* This works around an issue with Athlon chipsets on
8371 * B3 tigon3 silicon. This bit has no effect on any
8372 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07008373 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008374 */
Joe Perches63c3a662011-04-26 08:12:10 +00008375 if (!tg3_flag(tp, CPMU_PRESENT)) {
8376 if (!tg3_flag(tp, PCI_EXPRESS))
Matt Carlson795d01c2007-10-07 23:28:17 -07008377 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
8378 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
8379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008380
8381 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008382 tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008383 val = tr32(TG3PCI_PCISTATE);
8384 val |= PCISTATE_RETRY_SAME_DMA;
8385 tw32(TG3PCI_PCISTATE, val);
8386 }
8387
Joe Perches63c3a662011-04-26 08:12:10 +00008388 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -07008389 /* Allow reads and writes to the
8390 * APE register and memory space.
8391 */
8392 val = tr32(TG3PCI_PCISTATE);
8393 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00008394 PCISTATE_ALLOW_APE_SHMEM_WR |
8395 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008396 tw32(TG3PCI_PCISTATE, val);
8397 }
8398
Linus Torvalds1da177e2005-04-16 15:20:36 -07008399 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
8400 /* Enable some hw fixes. */
8401 val = tr32(TG3PCI_MSI_DATA);
8402 val |= (1 << 26) | (1 << 28) | (1 << 29);
8403 tw32(TG3PCI_MSI_DATA, val);
8404 }
8405
8406 /* Descriptor ring init may make accesses to the
8407 * NIC SRAM area to setup the TX descriptors, so we
8408 * can only do this after the hardware has been
8409 * successfully reset.
8410 */
Michael Chan32d8c572006-07-25 16:38:29 -07008411 err = tg3_init_rings(tp);
8412 if (err)
8413 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008414
Joe Perches63c3a662011-04-26 08:12:10 +00008415 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008416 val = tr32(TG3PCI_DMA_RW_CTRL) &
8417 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Matt Carlson1a319022010-04-12 06:58:25 +00008418 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
8419 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlson0aebff42011-04-25 12:42:45 +00008420 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57765 &&
8421 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8422 val |= DMA_RWCTRL_TAGGED_STAT_WA;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008423 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
8424 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
8425 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008426 /* This value is determined during the probe time DMA
8427 * engine test, tg3_test_dma.
8428 */
8429 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
8430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008431
8432 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
8433 GRC_MODE_4X_NIC_SEND_RINGS |
8434 GRC_MODE_NO_TX_PHDR_CSUM |
8435 GRC_MODE_NO_RX_PHDR_CSUM);
8436 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07008437
8438 /* Pseudo-header checksum is done by hardware logic and not
8439 * the offload processers, so make the chip do the pseudo-
8440 * header checksums on receive. For transmit it is more
8441 * convenient to do the pseudo-header checksum in software
8442 * as Linux does that on transmit for us in all cases.
8443 */
8444 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008445
8446 tw32(GRC_MODE,
8447 tp->grc_mode |
8448 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
8449
8450 /* Setup the timer prescalar register. Clock is always 66Mhz. */
8451 val = tr32(GRC_MISC_CFG);
8452 val &= ~0xff;
8453 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
8454 tw32(GRC_MISC_CFG, val);
8455
8456 /* Initialize MBUF/DESC pool. */
Joe Perches63c3a662011-04-26 08:12:10 +00008457 if (tg3_flag(tp, 5750_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008458 /* Do nothing. */
8459 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
8460 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
8461 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
8462 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
8463 else
8464 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
8465 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
8466 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Joe Perches63c3a662011-04-26 08:12:10 +00008467 } else if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008468 int fw_len;
8469
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08008470 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008471 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
8472 tw32(BUFMGR_MB_POOL_ADDR,
8473 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
8474 tw32(BUFMGR_MB_POOL_SIZE,
8475 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
8476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008477
Michael Chan0f893dc2005-07-25 12:30:38 -07008478 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008479 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8480 tp->bufmgr_config.mbuf_read_dma_low_water);
8481 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8482 tp->bufmgr_config.mbuf_mac_rx_low_water);
8483 tw32(BUFMGR_MB_HIGH_WATER,
8484 tp->bufmgr_config.mbuf_high_water);
8485 } else {
8486 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8487 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
8488 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8489 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
8490 tw32(BUFMGR_MB_HIGH_WATER,
8491 tp->bufmgr_config.mbuf_high_water_jumbo);
8492 }
8493 tw32(BUFMGR_DMA_LOW_WATER,
8494 tp->bufmgr_config.dma_low_water);
8495 tw32(BUFMGR_DMA_HIGH_WATER,
8496 tp->bufmgr_config.dma_high_water);
8497
Matt Carlsond309a462010-09-30 10:34:31 +00008498 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
8499 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
8500 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
Matt Carlson4d958472011-04-20 07:57:35 +00008501 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
8502 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
8503 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0)
8504 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
Matt Carlsond309a462010-09-30 10:34:31 +00008505 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008506 for (i = 0; i < 2000; i++) {
8507 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
8508 break;
8509 udelay(10);
8510 }
8511 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00008512 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008513 return -ENODEV;
8514 }
8515
Matt Carlsoneb07a942011-04-20 07:57:36 +00008516 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
8517 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
Michael Chanb5d37722006-09-27 16:06:21 -07008518
Matt Carlsoneb07a942011-04-20 07:57:36 +00008519 tg3_setup_rxbd_thresholds(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008520
8521 /* Initialize TG3_BDINFO's at:
8522 * RCVDBDI_STD_BD: standard eth size rx ring
8523 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
8524 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
8525 *
8526 * like so:
8527 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
8528 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
8529 * ring attribute flags
8530 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
8531 *
8532 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
8533 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
8534 *
8535 * The size of each ring is fixed in the firmware, but the location is
8536 * configurable.
8537 */
8538 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008539 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008540 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008541 ((u64) tpr->rx_std_mapping & 0xffffffff));
Joe Perches63c3a662011-04-26 08:12:10 +00008542 if (!tg3_flag(tp, 5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +00008543 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
8544 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008545
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008546 /* Disable the mini ring */
Joe Perches63c3a662011-04-26 08:12:10 +00008547 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008548 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
8549 BDINFO_FLAGS_DISABLED);
8550
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008551 /* Program the jumbo buffer descriptor ring control
8552 * blocks on those devices that have them.
8553 */
Matt Carlsona0512942011-07-27 14:20:54 +00008554 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008555 (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008556
Joe Perches63c3a662011-04-26 08:12:10 +00008557 if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008558 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008559 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008560 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008561 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +00008562 val = TG3_RX_JMB_RING_SIZE(tp) <<
8563 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008564 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +00008565 val | BDINFO_FLAGS_USE_EXT_RECV);
Joe Perches63c3a662011-04-26 08:12:10 +00008566 if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
Matt Carlsona50d0792010-06-05 17:24:37 +00008567 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson87668d32009-11-13 13:03:34 +00008568 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
8569 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008570 } else {
8571 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
8572 BDINFO_FLAGS_DISABLED);
8573 }
8574
Joe Perches63c3a662011-04-26 08:12:10 +00008575 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008576 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlsonde9f5232011-04-05 14:22:43 +00008577 val = TG3_RX_STD_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008578 else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008579 val = TG3_RX_STD_MAX_SIZE_5717;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008580 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
8581 val |= (TG3_RX_STD_DMA_SZ << 2);
8582 } else
Matt Carlson04380d42010-04-12 06:58:29 +00008583 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008584 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008585 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008586
8587 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008588
Matt Carlson411da642009-11-13 13:03:46 +00008589 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e62009-11-13 13:03:49 +00008590 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008591
Joe Perches63c3a662011-04-26 08:12:10 +00008592 tpr->rx_jmb_prod_idx =
8593 tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
Matt Carlson66711e62009-11-13 13:03:49 +00008594 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008595
Matt Carlson2d31eca2009-09-01 12:53:31 +00008596 tg3_rings_reset(tp);
8597
Linus Torvalds1da177e2005-04-16 15:20:36 -07008598 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07008599 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008600
8601 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00008602 tw32(MAC_RX_MTU_SIZE,
8603 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008604
8605 /* The slot time is changed by tg3_setup_phy if we
8606 * run at gigabit with half duplex.
8607 */
Matt Carlsonf2096f92011-04-05 14:22:48 +00008608 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
8609 (6 << TX_LENGTHS_IPG_SHIFT) |
8610 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
8611
8612 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8613 val |= tr32(MAC_TX_LENGTHS) &
8614 (TX_LENGTHS_JMB_FRM_LEN_MSK |
8615 TX_LENGTHS_CNT_DWN_VAL_MSK);
8616
8617 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008618
8619 /* Receive rules. */
8620 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
8621 tw32(RCVLPC_CONFIG, 0x0181);
8622
8623 /* Calculate RDMAC_MODE setting early, we need it to determine
8624 * the RCVLPC_STATE_ENABLE mask.
8625 */
8626 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
8627 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
8628 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
8629 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
8630 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07008631
Matt Carlsondeabaac2010-11-24 08:31:50 +00008632 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +00008633 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
8634
Matt Carlson57e69832008-05-25 23:48:31 -07008635 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08008636 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8637 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07008638 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
8639 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
8640 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
8641
Matt Carlsonc5908932011-03-09 16:58:25 +00008642 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8643 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00008644 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07008645 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008646 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
8647 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008648 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008649 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8650 }
8651 }
8652
Joe Perches63c3a662011-04-26 08:12:10 +00008653 if (tg3_flag(tp, PCI_EXPRESS))
Michael Chan85e94ce2005-04-21 17:05:28 -07008654 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8655
Joe Perches63c3a662011-04-26 08:12:10 +00008656 if (tg3_flag(tp, HW_TSO_1) ||
8657 tg3_flag(tp, HW_TSO_2) ||
8658 tg3_flag(tp, HW_TSO_3))
Matt Carlson027455a2008-12-21 20:19:30 -08008659 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
8660
Matt Carlson108a6c12011-05-19 12:12:47 +00008661 if (tg3_flag(tp, 57765_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +00008662 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson027455a2008-12-21 20:19:30 -08008663 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
8664 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008665
Matt Carlsonf2096f92011-04-05 14:22:48 +00008666 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8667 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
8668
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008669 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
8670 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
8671 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8672 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008673 tg3_flag(tp, 57765_PLUS)) {
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008674 val = tr32(TG3_RDMA_RSRVCTRL_REG);
Matt Carlsond78b59f2011-04-05 14:22:46 +00008675 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8676 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +00008677 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
8678 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
8679 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
8680 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
8681 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
8682 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +00008683 }
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008684 tw32(TG3_RDMA_RSRVCTRL_REG,
8685 val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
8686 }
8687
Matt Carlsond78b59f2011-04-05 14:22:46 +00008688 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8689 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsond309a462010-09-30 10:34:31 +00008690 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
8691 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val |
8692 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
8693 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
8694 }
8695
Linus Torvalds1da177e2005-04-16 15:20:36 -07008696 /* Receive/send statistics. */
Joe Perches63c3a662011-04-26 08:12:10 +00008697 if (tg3_flag(tp, 5750_PLUS)) {
Michael Chan16613942006-06-29 20:15:13 -07008698 val = tr32(RCVLPC_STATS_ENABLE);
8699 val &= ~RCVLPC_STATSENAB_DACK_FIX;
8700 tw32(RCVLPC_STATS_ENABLE, val);
8701 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008702 tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008703 val = tr32(RCVLPC_STATS_ENABLE);
8704 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
8705 tw32(RCVLPC_STATS_ENABLE, val);
8706 } else {
8707 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
8708 }
8709 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
8710 tw32(SNDDATAI_STATSENAB, 0xffffff);
8711 tw32(SNDDATAI_STATSCTRL,
8712 (SNDDATAI_SCTRL_ENABLE |
8713 SNDDATAI_SCTRL_FASTUPD));
8714
8715 /* Setup host coalescing engine. */
8716 tw32(HOSTCC_MODE, 0);
8717 for (i = 0; i < 2000; i++) {
8718 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
8719 break;
8720 udelay(10);
8721 }
8722
Michael Chand244c892005-07-05 14:42:33 -07008723 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008724
Joe Perches63c3a662011-04-26 08:12:10 +00008725 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008726 /* Status/statistics block address. See tg3_timer,
8727 * the tg3_periodic_fetch_stats call there, and
8728 * tg3_get_stats to see how this works for 5705/5750 chips.
8729 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008730 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8731 ((u64) tp->stats_mapping >> 32));
8732 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8733 ((u64) tp->stats_mapping & 0xffffffff));
8734 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008735
Linus Torvalds1da177e2005-04-16 15:20:36 -07008736 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008737
8738 /* Clear statistics and status block memory areas */
8739 for (i = NIC_SRAM_STATS_BLK;
8740 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
8741 i += sizeof(u32)) {
8742 tg3_write_mem(tp, i, 0);
8743 udelay(40);
8744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008745 }
8746
8747 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
8748
8749 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
8750 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008751 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008752 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
8753
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008754 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
8755 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -07008756 /* reset to prevent losing 1st rx packet intermittently */
8757 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8758 udelay(10);
8759 }
8760
Matt Carlson3bda1252008-08-15 14:08:22 -07008761 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Matt Carlson9e975cc2011-07-20 10:20:50 +00008762 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE |
8763 MAC_MODE_FHDE_ENABLE;
8764 if (tg3_flag(tp, ENABLE_APE))
8765 tp->mac_mode |= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Joe Perches63c3a662011-04-26 08:12:10 +00008766 if (!tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008767 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07008768 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
8769 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008770 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
8771 udelay(40);
8772
Michael Chan314fba32005-04-21 17:07:04 -07008773 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Joe Perches63c3a662011-04-26 08:12:10 +00008774 * If TG3_FLAG_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07008775 * register to preserve the GPIO settings for LOMs. The GPIOs,
8776 * whether used as inputs or outputs, are set by boot code after
8777 * reset.
8778 */
Joe Perches63c3a662011-04-26 08:12:10 +00008779 if (!tg3_flag(tp, IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07008780 u32 gpio_mask;
8781
Michael Chan9d26e212006-12-07 00:21:14 -08008782 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
8783 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
8784 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07008785
8786 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
8787 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
8788 GRC_LCLCTRL_GPIO_OUTPUT3;
8789
Michael Chanaf36e6b2006-03-23 01:28:06 -08008790 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
8791 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
8792
Gary Zambranoaaf84462007-05-05 11:51:45 -07008793 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07008794 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
8795
8796 /* GPIO1 must be driven high for eeprom write protect */
Joe Perches63c3a662011-04-26 08:12:10 +00008797 if (tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan9d26e212006-12-07 00:21:14 -08008798 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
8799 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07008800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008801 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8802 udelay(100);
8803
Joe Perches63c3a662011-04-26 08:12:10 +00008804 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008805 val = tr32(MSGINT_MODE);
8806 val |= MSGINT_MODE_MULTIVEC_EN | MSGINT_MODE_ENABLE;
Matt Carlson5b39de92011-08-31 11:44:50 +00008807 if (!tg3_flag(tp, 1SHOT_MSI))
8808 val |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008809 tw32(MSGINT_MODE, val);
8810 }
8811
Joe Perches63c3a662011-04-26 08:12:10 +00008812 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008813 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
8814 udelay(40);
8815 }
8816
8817 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
8818 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
8819 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
8820 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
8821 WDMAC_MODE_LNGREAD_ENAB);
8822
Matt Carlsonc5908932011-03-09 16:58:25 +00008823 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8824 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00008825 if (tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008826 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
8827 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
8828 /* nothing */
8829 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008830 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008831 val |= WDMAC_MODE_RX_ACCEL;
8832 }
8833 }
8834
Michael Chand9ab5ad2006-03-20 22:27:35 -08008835 /* Enable host coalescing bug fix */
Joe Perches63c3a662011-04-26 08:12:10 +00008836 if (tg3_flag(tp, 5755_PLUS))
Matt Carlsonf51f3562008-05-25 23:45:08 -07008837 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad2006-03-20 22:27:35 -08008838
Matt Carlson788a0352009-11-02 14:26:03 +00008839 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
8840 val |= WDMAC_MODE_BURST_ALL_DATA;
8841
Linus Torvalds1da177e2005-04-16 15:20:36 -07008842 tw32_f(WDMAC_MODE, val);
8843 udelay(40);
8844
Joe Perches63c3a662011-04-26 08:12:10 +00008845 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07008846 u16 pcix_cmd;
8847
8848 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8849 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008850 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07008851 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
8852 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008853 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07008854 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
8855 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008856 }
Matt Carlson9974a352007-10-07 23:27:28 -07008857 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8858 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008859 }
8860
8861 tw32_f(RDMAC_MODE, rdmac_mode);
8862 udelay(40);
8863
8864 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008865 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008866 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07008867
8868 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
8869 tw32(SNDDATAC_MODE,
8870 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
8871 else
8872 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
8873
Linus Torvalds1da177e2005-04-16 15:20:36 -07008874 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
8875 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008876 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00008877 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008878 val |= RCVDBDI_MODE_LRG_RING_SZ;
8879 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008880 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008881 if (tg3_flag(tp, HW_TSO_1) ||
8882 tg3_flag(tp, HW_TSO_2) ||
8883 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008884 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008885 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00008886 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008887 val |= SNDBDI_MODE_MULTI_TXQ_EN;
8888 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008889 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
8890
8891 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
8892 err = tg3_load_5701_a0_firmware_fix(tp);
8893 if (err)
8894 return err;
8895 }
8896
Joe Perches63c3a662011-04-26 08:12:10 +00008897 if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008898 err = tg3_load_tso_firmware(tp);
8899 if (err)
8900 return err;
8901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008902
8903 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +00008904
Joe Perches63c3a662011-04-26 08:12:10 +00008905 if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsonb1d05212010-06-05 17:24:31 +00008906 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
8907 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +00008908
8909 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
8910 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
8911 tp->tx_mode &= ~val;
8912 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
8913 }
8914
Linus Torvalds1da177e2005-04-16 15:20:36 -07008915 tw32_f(MAC_TX_MODE, tp->tx_mode);
8916 udelay(100);
8917
Joe Perches63c3a662011-04-26 08:12:10 +00008918 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson9d53fa12011-07-20 10:20:54 +00008919 int i = 0;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008920 u32 reg = MAC_RSS_INDIR_TBL_0;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008921
Matt Carlson9d53fa12011-07-20 10:20:54 +00008922 if (tp->irq_cnt == 2) {
8923 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i += 8) {
8924 tw32(reg, 0x0);
8925 reg += 4;
8926 }
8927 } else {
8928 u32 val;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008929
Matt Carlson9d53fa12011-07-20 10:20:54 +00008930 while (i < TG3_RSS_INDIR_TBL_SIZE) {
8931 val = i % (tp->irq_cnt - 1);
8932 i++;
8933 for (; i % 8; i++) {
8934 val <<= 4;
8935 val |= (i % (tp->irq_cnt - 1));
8936 }
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008937 tw32(reg, val);
8938 reg += 4;
8939 }
8940 }
8941
8942 /* Setup the "secret" hash key. */
8943 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
8944 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
8945 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
8946 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
8947 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
8948 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
8949 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
8950 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
8951 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
8952 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
8953 }
8954
Linus Torvalds1da177e2005-04-16 15:20:36 -07008955 tp->rx_mode = RX_MODE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00008956 if (tg3_flag(tp, 5755_PLUS))
Michael Chanaf36e6b2006-03-23 01:28:06 -08008957 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
8958
Joe Perches63c3a662011-04-26 08:12:10 +00008959 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008960 tp->rx_mode |= RX_MODE_RSS_ENABLE |
8961 RX_MODE_RSS_ITBL_HASH_BITS_7 |
8962 RX_MODE_RSS_IPV6_HASH_EN |
8963 RX_MODE_RSS_TCP_IPV6_HASH_EN |
8964 RX_MODE_RSS_IPV4_HASH_EN |
8965 RX_MODE_RSS_TCP_IPV4_HASH_EN;
8966
Linus Torvalds1da177e2005-04-16 15:20:36 -07008967 tw32_f(MAC_RX_MODE, tp->rx_mode);
8968 udelay(10);
8969
Linus Torvalds1da177e2005-04-16 15:20:36 -07008970 tw32(MAC_LED_CTRL, tp->led_ctrl);
8971
8972 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008973 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008974 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8975 udelay(10);
8976 }
8977 tw32_f(MAC_RX_MODE, tp->rx_mode);
8978 udelay(10);
8979
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008980 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008981 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008982 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008983 /* Set drive transmission level to 1.2V */
8984 /* only if the signal pre-emphasis bit is not set */
8985 val = tr32(MAC_SERDES_CFG);
8986 val &= 0xfffff000;
8987 val |= 0x880;
8988 tw32(MAC_SERDES_CFG, val);
8989 }
8990 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
8991 tw32(MAC_SERDES_CFG, 0x616000);
8992 }
8993
8994 /* Prevent chip from dropping frames when flow control
8995 * is enabled.
8996 */
Matt Carlson666bc832010-01-20 16:58:03 +00008997 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8998 val = 1;
8999 else
9000 val = 2;
9001 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009002
9003 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009004 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009005 /* Use hardware link auto-negotiation */
Joe Perches63c3a662011-04-26 08:12:10 +00009006 tg3_flag_set(tp, HW_AUTONEG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009007 }
9008
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009009 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +00009010 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08009011 u32 tmp;
9012
9013 tmp = tr32(SERDES_RX_CTRL);
9014 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
9015 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
9016 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
9017 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
9018 }
9019
Joe Perches63c3a662011-04-26 08:12:10 +00009020 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson80096062010-08-02 11:26:06 +00009021 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
9022 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07009023 tp->link_config.speed = tp->link_config.orig_speed;
9024 tp->link_config.duplex = tp->link_config.orig_duplex;
9025 tp->link_config.autoneg = tp->link_config.orig_autoneg;
9026 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009027
Matt Carlsondd477002008-05-25 23:45:58 -07009028 err = tg3_setup_phy(tp, 0);
9029 if (err)
9030 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009031
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009032 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
9033 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07009034 u32 tmp;
9035
9036 /* Clear CRC stats. */
9037 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
9038 tg3_writephy(tp, MII_TG3_TEST1,
9039 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009040 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -07009041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009042 }
9043 }
9044
9045 __tg3_set_rx_mode(tp->dev);
9046
9047 /* Initialize receive rules. */
9048 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
9049 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
9050 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
9051 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
9052
Joe Perches63c3a662011-04-26 08:12:10 +00009053 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009054 limit = 8;
9055 else
9056 limit = 16;
Joe Perches63c3a662011-04-26 08:12:10 +00009057 if (tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009058 limit -= 4;
9059 switch (limit) {
9060 case 16:
9061 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
9062 case 15:
9063 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
9064 case 14:
9065 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
9066 case 13:
9067 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
9068 case 12:
9069 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
9070 case 11:
9071 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
9072 case 10:
9073 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
9074 case 9:
9075 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
9076 case 8:
9077 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
9078 case 7:
9079 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
9080 case 6:
9081 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
9082 case 5:
9083 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
9084 case 4:
9085 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
9086 case 3:
9087 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
9088 case 2:
9089 case 1:
9090
9091 default:
9092 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07009093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009094
Joe Perches63c3a662011-04-26 08:12:10 +00009095 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson9ce768e2007-10-11 19:49:11 -07009096 /* Write our heartbeat update interval to APE. */
9097 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
9098 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07009099
Linus Torvalds1da177e2005-04-16 15:20:36 -07009100 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
9101
Linus Torvalds1da177e2005-04-16 15:20:36 -07009102 return 0;
9103}
9104
9105/* Called at device open time to get the chip ready for
9106 * packet processing. Invoked with tp->lock held.
9107 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009108static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009109{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009110 tg3_switch_clocks(tp);
9111
9112 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
9113
Matt Carlson2f751b62008-08-04 23:17:34 -07009114 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009115}
9116
9117#define TG3_STAT_ADD32(PSTAT, REG) \
9118do { u32 __val = tr32(REG); \
9119 (PSTAT)->low += __val; \
9120 if ((PSTAT)->low < __val) \
9121 (PSTAT)->high += 1; \
9122} while (0)
9123
9124static void tg3_periodic_fetch_stats(struct tg3 *tp)
9125{
9126 struct tg3_hw_stats *sp = tp->hw_stats;
9127
9128 if (!netif_carrier_ok(tp->dev))
9129 return;
9130
9131 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
9132 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
9133 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
9134 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
9135 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
9136 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
9137 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
9138 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
9139 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
9140 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
9141 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
9142 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
9143 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
9144
9145 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
9146 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
9147 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
9148 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
9149 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
9150 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
9151 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
9152 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
9153 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
9154 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
9155 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
9156 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
9157 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
9158 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07009159
9160 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
Matt Carlson310050f2011-05-19 12:12:55 +00009161 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
9162 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0 &&
9163 tp->pci_chip_rev_id != CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +00009164 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
9165 } else {
9166 u32 val = tr32(HOSTCC_FLOW_ATTN);
9167 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
9168 if (val) {
9169 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
9170 sp->rx_discards.low += val;
9171 if (sp->rx_discards.low < val)
9172 sp->rx_discards.high += 1;
9173 }
9174 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
9175 }
Michael Chan463d3052006-05-22 16:36:27 -07009176 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009177}
9178
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009179static void tg3_chk_missed_msi(struct tg3 *tp)
9180{
9181 u32 i;
9182
9183 for (i = 0; i < tp->irq_cnt; i++) {
9184 struct tg3_napi *tnapi = &tp->napi[i];
9185
9186 if (tg3_has_work(tnapi)) {
9187 if (tnapi->last_rx_cons == tnapi->rx_rcb_ptr &&
9188 tnapi->last_tx_cons == tnapi->tx_cons) {
9189 if (tnapi->chk_msi_cnt < 1) {
9190 tnapi->chk_msi_cnt++;
9191 return;
9192 }
Matt Carlson7f230732011-08-31 11:44:48 +00009193 tg3_msi(0, tnapi);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009194 }
9195 }
9196 tnapi->chk_msi_cnt = 0;
9197 tnapi->last_rx_cons = tnapi->rx_rcb_ptr;
9198 tnapi->last_tx_cons = tnapi->tx_cons;
9199 }
9200}
9201
Linus Torvalds1da177e2005-04-16 15:20:36 -07009202static void tg3_timer(unsigned long __opaque)
9203{
9204 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009205
Michael Chanf475f162006-03-27 23:20:14 -08009206 if (tp->irq_sync)
9207 goto restart_timer;
9208
David S. Millerf47c11e2005-06-24 20:18:35 -07009209 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009210
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009211 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
9212 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
9213 tg3_chk_missed_msi(tp);
9214
Joe Perches63c3a662011-04-26 08:12:10 +00009215 if (!tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -07009216 /* All of this garbage is because when using non-tagged
9217 * IRQ status the mailbox/status_block protocol the chip
9218 * uses with the cpu is race prone.
9219 */
Matt Carlson898a56f2009-08-28 14:02:40 +00009220 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -07009221 tw32(GRC_LOCAL_CTRL,
9222 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
9223 } else {
9224 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00009225 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -07009226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009227
David S. Millerfac9b832005-05-18 22:46:34 -07009228 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +00009229 tg3_flag_set(tp, RESTART_TIMER);
David S. Millerf47c11e2005-06-24 20:18:35 -07009230 spin_unlock(&tp->lock);
David S. Millerfac9b832005-05-18 22:46:34 -07009231 schedule_work(&tp->reset_task);
9232 return;
9233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009234 }
9235
Linus Torvalds1da177e2005-04-16 15:20:36 -07009236 /* This part only runs once per second. */
9237 if (!--tp->timer_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00009238 if (tg3_flag(tp, 5705_PLUS))
David S. Millerfac9b832005-05-18 22:46:34 -07009239 tg3_periodic_fetch_stats(tp);
9240
Matt Carlsonb0c59432011-05-19 12:12:48 +00009241 if (tp->setlpicnt && !--tp->setlpicnt)
9242 tg3_phy_eee_enable(tp);
Matt Carlson52b02d02010-10-14 10:37:41 +00009243
Joe Perches63c3a662011-04-26 08:12:10 +00009244 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009245 u32 mac_stat;
9246 int phy_event;
9247
9248 mac_stat = tr32(MAC_STATUS);
9249
9250 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009251 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009252 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
9253 phy_event = 1;
9254 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
9255 phy_event = 1;
9256
9257 if (phy_event)
9258 tg3_setup_phy(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +00009259 } else if (tg3_flag(tp, POLL_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009260 u32 mac_stat = tr32(MAC_STATUS);
9261 int need_setup = 0;
9262
9263 if (netif_carrier_ok(tp->dev) &&
9264 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
9265 need_setup = 1;
9266 }
Matt Carlsonbe98da62010-07-11 09:31:46 +00009267 if (!netif_carrier_ok(tp->dev) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009268 (mac_stat & (MAC_STATUS_PCS_SYNCED |
9269 MAC_STATUS_SIGNAL_DET))) {
9270 need_setup = 1;
9271 }
9272 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07009273 if (!tp->serdes_counter) {
9274 tw32_f(MAC_MODE,
9275 (tp->mac_mode &
9276 ~MAC_MODE_PORT_MODE_MASK));
9277 udelay(40);
9278 tw32_f(MAC_MODE, tp->mac_mode);
9279 udelay(40);
9280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009281 tg3_setup_phy(tp, 0);
9282 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009283 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009284 tg3_flag(tp, 5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -07009285 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +00009286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009287
9288 tp->timer_counter = tp->timer_multiplier;
9289 }
9290
Michael Chan130b8e42006-09-27 16:00:40 -07009291 /* Heartbeat is only sent once every 2 seconds.
9292 *
9293 * The heartbeat is to tell the ASF firmware that the host
9294 * driver is still alive. In the event that the OS crashes,
9295 * ASF needs to reset the hardware to free up the FIFO space
9296 * that may be filled with rx packets destined for the host.
9297 * If the FIFO is full, ASF will no longer function properly.
9298 *
9299 * Unintended resets have been reported on real time kernels
9300 * where the timer doesn't run on time. Netpoll will also have
9301 * same problem.
9302 *
9303 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
9304 * to check the ring condition when the heartbeat is expiring
9305 * before doing the reset. This will prevent most unintended
9306 * resets.
9307 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009308 if (!--tp->asf_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00009309 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07009310 tg3_wait_for_event_ack(tp);
9311
Michael Chanbbadf502006-04-06 21:46:34 -07009312 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07009313 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07009314 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +00009315 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
9316 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -07009317
9318 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009319 }
9320 tp->asf_counter = tp->asf_multiplier;
9321 }
9322
David S. Millerf47c11e2005-06-24 20:18:35 -07009323 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009324
Michael Chanf475f162006-03-27 23:20:14 -08009325restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07009326 tp->timer.expires = jiffies + tp->timer_offset;
9327 add_timer(&tp->timer);
9328}
9329
Matt Carlson4f125f42009-09-01 12:55:02 +00009330static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -08009331{
David Howells7d12e782006-10-05 14:55:46 +01009332 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009333 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +00009334 char *name;
9335 struct tg3_napi *tnapi = &tp->napi[irq_num];
9336
9337 if (tp->irq_cnt == 1)
9338 name = tp->dev->name;
9339 else {
9340 name = &tnapi->irq_lbl[0];
9341 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
9342 name[IFNAMSIZ-1] = 0;
9343 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009344
Joe Perches63c3a662011-04-26 08:12:10 +00009345 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Michael Chanfcfa0a32006-03-20 22:28:41 -08009346 fn = tg3_msi;
Joe Perches63c3a662011-04-26 08:12:10 +00009347 if (tg3_flag(tp, 1SHOT_MSI))
Michael Chanfcfa0a32006-03-20 22:28:41 -08009348 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009349 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009350 } else {
9351 fn = tg3_interrupt;
Joe Perches63c3a662011-04-26 08:12:10 +00009352 if (tg3_flag(tp, TAGGED_STATUS))
Michael Chanfcfa0a32006-03-20 22:28:41 -08009353 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009354 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009355 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009356
9357 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009358}
9359
Michael Chan79381092005-04-21 17:13:59 -07009360static int tg3_test_interrupt(struct tg3 *tp)
9361{
Matt Carlson09943a12009-08-28 14:01:57 +00009362 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -07009363 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -07009364 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009365 u32 val;
Michael Chan79381092005-04-21 17:13:59 -07009366
Michael Chand4bc3922005-05-29 14:59:20 -07009367 if (!netif_running(dev))
9368 return -ENODEV;
9369
Michael Chan79381092005-04-21 17:13:59 -07009370 tg3_disable_ints(tp);
9371
Matt Carlson4f125f42009-09-01 12:55:02 +00009372 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009373
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009374 /*
9375 * Turn off MSI one shot mode. Otherwise this test has no
9376 * observable way to know whether the interrupt was delivered.
9377 */
Matt Carlson3aa1cdf2011-07-20 10:20:55 +00009378 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009379 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
9380 tw32(MSGINT_MODE, val);
9381 }
9382
Matt Carlson4f125f42009-09-01 12:55:02 +00009383 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Matt Carlson09943a12009-08-28 14:01:57 +00009384 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009385 if (err)
9386 return err;
9387
Matt Carlson898a56f2009-08-28 14:02:40 +00009388 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -07009389 tg3_enable_ints(tp);
9390
9391 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00009392 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -07009393
9394 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -07009395 u32 int_mbox, misc_host_ctrl;
9396
Matt Carlson898a56f2009-08-28 14:02:40 +00009397 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -07009398 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
9399
9400 if ((int_mbox != 0) ||
9401 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
9402 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -07009403 break;
Michael Chanb16250e2006-09-27 16:10:14 -07009404 }
9405
Matt Carlson3aa1cdf2011-07-20 10:20:55 +00009406 if (tg3_flag(tp, 57765_PLUS) &&
9407 tnapi->hw_status->status_tag != tnapi->last_tag)
9408 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
9409
Michael Chan79381092005-04-21 17:13:59 -07009410 msleep(10);
9411 }
9412
9413 tg3_disable_ints(tp);
9414
Matt Carlson4f125f42009-09-01 12:55:02 +00009415 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009416
Matt Carlson4f125f42009-09-01 12:55:02 +00009417 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009418
9419 if (err)
9420 return err;
9421
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009422 if (intr_ok) {
9423 /* Reenable MSI one shot mode. */
Matt Carlson5b39de92011-08-31 11:44:50 +00009424 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, 1SHOT_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009425 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
9426 tw32(MSGINT_MODE, val);
9427 }
Michael Chan79381092005-04-21 17:13:59 -07009428 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009429 }
Michael Chan79381092005-04-21 17:13:59 -07009430
9431 return -EIO;
9432}
9433
9434/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
9435 * successfully restored
9436 */
9437static int tg3_test_msi(struct tg3 *tp)
9438{
Michael Chan79381092005-04-21 17:13:59 -07009439 int err;
9440 u16 pci_cmd;
9441
Joe Perches63c3a662011-04-26 08:12:10 +00009442 if (!tg3_flag(tp, USING_MSI))
Michael Chan79381092005-04-21 17:13:59 -07009443 return 0;
9444
9445 /* Turn off SERR reporting in case MSI terminates with Master
9446 * Abort.
9447 */
9448 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
9449 pci_write_config_word(tp->pdev, PCI_COMMAND,
9450 pci_cmd & ~PCI_COMMAND_SERR);
9451
9452 err = tg3_test_interrupt(tp);
9453
9454 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
9455
9456 if (!err)
9457 return 0;
9458
9459 /* other failures */
9460 if (err != -EIO)
9461 return err;
9462
9463 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009464 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
9465 "to INTx mode. Please report this failure to the PCI "
9466 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -07009467
Matt Carlson4f125f42009-09-01 12:55:02 +00009468 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +00009469
Michael Chan79381092005-04-21 17:13:59 -07009470 pci_disable_msi(tp->pdev);
9471
Joe Perches63c3a662011-04-26 08:12:10 +00009472 tg3_flag_clear(tp, USING_MSI);
Andre Detschdc8bf1b2010-04-26 07:27:07 +00009473 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -07009474
Matt Carlson4f125f42009-09-01 12:55:02 +00009475 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009476 if (err)
9477 return err;
9478
9479 /* Need to reset the chip because the MSI cycle may have terminated
9480 * with Master Abort.
9481 */
David S. Millerf47c11e2005-06-24 20:18:35 -07009482 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009483
Michael Chan944d9802005-05-29 14:57:48 -07009484 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009485 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009486
David S. Millerf47c11e2005-06-24 20:18:35 -07009487 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009488
9489 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +00009490 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -07009491
9492 return err;
9493}
9494
Matt Carlson9e9fd122009-01-19 16:57:45 -08009495static int tg3_request_firmware(struct tg3 *tp)
9496{
9497 const __be32 *fw_data;
9498
9499 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009500 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
9501 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009502 return -ENOENT;
9503 }
9504
9505 fw_data = (void *)tp->fw->data;
9506
9507 /* Firmware blob starts with version numbers, followed by
9508 * start address and _full_ length including BSS sections
9509 * (which must be longer than the actual data, of course
9510 */
9511
9512 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
9513 if (tp->fw_len < (tp->fw->size - 12)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009514 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
9515 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009516 release_firmware(tp->fw);
9517 tp->fw = NULL;
9518 return -EINVAL;
9519 }
9520
9521 /* We no longer need firmware; we have it. */
9522 tp->fw_needed = NULL;
9523 return 0;
9524}
9525
Matt Carlson679563f2009-09-01 12:55:46 +00009526static bool tg3_enable_msix(struct tg3 *tp)
9527{
9528 int i, rc, cpus = num_online_cpus();
9529 struct msix_entry msix_ent[tp->irq_max];
9530
9531 if (cpus == 1)
9532 /* Just fallback to the simpler MSI mode. */
9533 return false;
9534
9535 /*
9536 * We want as many rx rings enabled as there are cpus.
9537 * The first MSIX vector only deals with link interrupts, etc,
9538 * so we add one to the number of vectors we are requesting.
9539 */
9540 tp->irq_cnt = min_t(unsigned, cpus + 1, tp->irq_max);
9541
9542 for (i = 0; i < tp->irq_max; i++) {
9543 msix_ent[i].entry = i;
9544 msix_ent[i].vector = 0;
9545 }
9546
9547 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +00009548 if (rc < 0) {
9549 return false;
9550 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +00009551 if (pci_enable_msix(tp->pdev, msix_ent, rc))
9552 return false;
Joe Perches05dbe002010-02-17 19:44:19 +00009553 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
9554 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +00009555 tp->irq_cnt = rc;
9556 }
9557
9558 for (i = 0; i < tp->irq_max; i++)
9559 tp->napi[i].irq_vec = msix_ent[i].vector;
9560
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009561 netif_set_real_num_tx_queues(tp->dev, 1);
9562 rc = tp->irq_cnt > 1 ? tp->irq_cnt - 1 : 1;
9563 if (netif_set_real_num_rx_queues(tp->dev, rc)) {
9564 pci_disable_msix(tp->pdev);
9565 return false;
9566 }
Matt Carlsonb92b9042010-11-24 08:31:51 +00009567
9568 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +00009569 tg3_flag_set(tp, ENABLE_RSS);
Matt Carlsond78b59f2011-04-05 14:22:46 +00009570
9571 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
9572 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Joe Perches63c3a662011-04-26 08:12:10 +00009573 tg3_flag_set(tp, ENABLE_TSS);
Matt Carlsonb92b9042010-11-24 08:31:51 +00009574 netif_set_real_num_tx_queues(tp->dev, tp->irq_cnt - 1);
9575 }
9576 }
Matt Carlson2430b032010-06-05 17:24:34 +00009577
Matt Carlson679563f2009-09-01 12:55:46 +00009578 return true;
9579}
9580
Matt Carlson07b01732009-08-28 14:01:15 +00009581static void tg3_ints_init(struct tg3 *tp)
9582{
Joe Perches63c3a662011-04-26 08:12:10 +00009583 if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
9584 !tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +00009585 /* All MSI supporting chips should support tagged
9586 * status. Assert that this is the case.
9587 */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009588 netdev_warn(tp->dev,
9589 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +00009590 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +00009591 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009592
Joe Perches63c3a662011-04-26 08:12:10 +00009593 if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
9594 tg3_flag_set(tp, USING_MSIX);
9595 else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
9596 tg3_flag_set(tp, USING_MSI);
Matt Carlson679563f2009-09-01 12:55:46 +00009597
Joe Perches63c3a662011-04-26 08:12:10 +00009598 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009599 u32 msi_mode = tr32(MSGINT_MODE);
Joe Perches63c3a662011-04-26 08:12:10 +00009600 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009601 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +00009602 if (!tg3_flag(tp, 1SHOT_MSI))
9603 msi_mode |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlson679563f2009-09-01 12:55:46 +00009604 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
9605 }
9606defcfg:
Joe Perches63c3a662011-04-26 08:12:10 +00009607 if (!tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009608 tp->irq_cnt = 1;
9609 tp->napi[0].irq_vec = tp->pdev->irq;
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009610 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -07009611 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +00009612 }
Matt Carlson07b01732009-08-28 14:01:15 +00009613}
9614
9615static void tg3_ints_fini(struct tg3 *tp)
9616{
Joe Perches63c3a662011-04-26 08:12:10 +00009617 if (tg3_flag(tp, USING_MSIX))
Matt Carlson679563f2009-09-01 12:55:46 +00009618 pci_disable_msix(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009619 else if (tg3_flag(tp, USING_MSI))
Matt Carlson679563f2009-09-01 12:55:46 +00009620 pci_disable_msi(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009621 tg3_flag_clear(tp, USING_MSI);
9622 tg3_flag_clear(tp, USING_MSIX);
9623 tg3_flag_clear(tp, ENABLE_RSS);
9624 tg3_flag_clear(tp, ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +00009625}
9626
Linus Torvalds1da177e2005-04-16 15:20:36 -07009627static int tg3_open(struct net_device *dev)
9628{
9629 struct tg3 *tp = netdev_priv(dev);
Matt Carlson4f125f42009-09-01 12:55:02 +00009630 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009631
Matt Carlson9e9fd122009-01-19 16:57:45 -08009632 if (tp->fw_needed) {
9633 err = tg3_request_firmware(tp);
9634 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
9635 if (err)
9636 return err;
9637 } else if (err) {
Joe Perches05dbe002010-02-17 19:44:19 +00009638 netdev_warn(tp->dev, "TSO capability disabled\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009639 tg3_flag_clear(tp, TSO_CAPABLE);
9640 } else if (!tg3_flag(tp, TSO_CAPABLE)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009641 netdev_notice(tp->dev, "TSO capability restored\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009642 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009643 }
9644 }
9645
Michael Chanc49a1562006-12-17 17:07:29 -08009646 netif_carrier_off(tp->dev);
9647
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009648 err = tg3_power_up(tp);
Matt Carlson2f751b62008-08-04 23:17:34 -07009649 if (err)
Michael Chanbc1c7562006-03-20 17:48:03 -08009650 return err;
Matt Carlson2f751b62008-08-04 23:17:34 -07009651
9652 tg3_full_lock(tp, 0);
Michael Chanbc1c7562006-03-20 17:48:03 -08009653
Linus Torvalds1da177e2005-04-16 15:20:36 -07009654 tg3_disable_ints(tp);
Joe Perches63c3a662011-04-26 08:12:10 +00009655 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009656
David S. Millerf47c11e2005-06-24 20:18:35 -07009657 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009658
Matt Carlson679563f2009-09-01 12:55:46 +00009659 /*
9660 * Setup interrupts first so we know how
9661 * many NAPI resources to allocate
9662 */
9663 tg3_ints_init(tp);
9664
Linus Torvalds1da177e2005-04-16 15:20:36 -07009665 /* The placement of this call is tied
9666 * to the setup and use of Host TX descriptors.
9667 */
9668 err = tg3_alloc_consistent(tp);
9669 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009670 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009671
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009672 tg3_napi_init(tp);
9673
Matt Carlsonfed97812009-09-01 13:10:19 +00009674 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07009675
Matt Carlson4f125f42009-09-01 12:55:02 +00009676 for (i = 0; i < tp->irq_cnt; i++) {
9677 struct tg3_napi *tnapi = &tp->napi[i];
9678 err = tg3_request_irq(tp, i);
9679 if (err) {
9680 for (i--; i >= 0; i--)
9681 free_irq(tnapi->irq_vec, tnapi);
9682 break;
9683 }
9684 }
Matt Carlson07b01732009-08-28 14:01:15 +00009685
9686 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009687 goto err_out2;
Matt Carlson07b01732009-08-28 14:01:15 +00009688
David S. Millerf47c11e2005-06-24 20:18:35 -07009689 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009690
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009691 err = tg3_init_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009692 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -07009693 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009694 tg3_free_rings(tp);
9695 } else {
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009696 if (tg3_flag(tp, TAGGED_STATUS) &&
9697 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
9698 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57765)
David S. Millerfac9b832005-05-18 22:46:34 -07009699 tp->timer_offset = HZ;
9700 else
9701 tp->timer_offset = HZ / 10;
9702
9703 BUG_ON(tp->timer_offset > HZ);
9704 tp->timer_counter = tp->timer_multiplier =
9705 (HZ / tp->timer_offset);
9706 tp->asf_counter = tp->asf_multiplier =
Michael Chan28fbef72005-10-26 15:48:35 -07009707 ((HZ / tp->timer_offset) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009708
9709 init_timer(&tp->timer);
9710 tp->timer.expires = jiffies + tp->timer_offset;
9711 tp->timer.data = (unsigned long) tp;
9712 tp->timer.function = tg3_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009713 }
9714
David S. Millerf47c11e2005-06-24 20:18:35 -07009715 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009716
Matt Carlson07b01732009-08-28 14:01:15 +00009717 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009718 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009719
Joe Perches63c3a662011-04-26 08:12:10 +00009720 if (tg3_flag(tp, USING_MSI)) {
Michael Chan79381092005-04-21 17:13:59 -07009721 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -07009722
Michael Chan79381092005-04-21 17:13:59 -07009723 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -07009724 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -07009725 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -07009726 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07009727 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009728
Matt Carlson679563f2009-09-01 12:55:46 +00009729 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -07009730 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009731
Joe Perches63c3a662011-04-26 08:12:10 +00009732 if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009733 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009734
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009735 tw32(PCIE_TRANSACTION_CFG,
9736 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009737 }
Michael Chan79381092005-04-21 17:13:59 -07009738 }
9739
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009740 tg3_phy_start(tp);
9741
David S. Millerf47c11e2005-06-24 20:18:35 -07009742 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009743
Michael Chan79381092005-04-21 17:13:59 -07009744 add_timer(&tp->timer);
Joe Perches63c3a662011-04-26 08:12:10 +00009745 tg3_flag_set(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009746 tg3_enable_ints(tp);
9747
David S. Millerf47c11e2005-06-24 20:18:35 -07009748 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009749
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009750 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009751
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00009752 /*
9753 * Reset loopback feature if it was turned on while the device was down
9754 * make sure that it's installed properly now.
9755 */
9756 if (dev->features & NETIF_F_LOOPBACK)
9757 tg3_set_loopback(dev, dev->features);
9758
Linus Torvalds1da177e2005-04-16 15:20:36 -07009759 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +00009760
Matt Carlson679563f2009-09-01 12:55:46 +00009761err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +00009762 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9763 struct tg3_napi *tnapi = &tp->napi[i];
9764 free_irq(tnapi->irq_vec, tnapi);
9765 }
Matt Carlson07b01732009-08-28 14:01:15 +00009766
Matt Carlson679563f2009-09-01 12:55:46 +00009767err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +00009768 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009769 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00009770 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +00009771
9772err_out1:
9773 tg3_ints_fini(tp);
Matt Carlsoncd0d7222011-07-13 09:27:33 +00009774 tg3_frob_aux_power(tp, false);
9775 pci_set_power_state(tp->pdev, PCI_D3hot);
Matt Carlson07b01732009-08-28 14:01:15 +00009776 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009777}
9778
Eric Dumazet511d2222010-07-07 20:44:24 +00009779static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *,
9780 struct rtnl_link_stats64 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009781static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *);
9782
9783static int tg3_close(struct net_device *dev)
9784{
Matt Carlson4f125f42009-09-01 12:55:02 +00009785 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009786 struct tg3 *tp = netdev_priv(dev);
9787
Matt Carlsonfed97812009-09-01 13:10:19 +00009788 tg3_napi_disable(tp);
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07009789 cancel_work_sync(&tp->reset_task);
Michael Chan7faa0062006-02-02 17:29:28 -08009790
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009791 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009792
9793 del_timer_sync(&tp->timer);
9794
Matt Carlson24bb4fb2009-10-05 17:55:29 +00009795 tg3_phy_stop(tp);
9796
David S. Millerf47c11e2005-06-24 20:18:35 -07009797 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009798
9799 tg3_disable_ints(tp);
9800
Michael Chan944d9802005-05-29 14:57:48 -07009801 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009802 tg3_free_rings(tp);
Joe Perches63c3a662011-04-26 08:12:10 +00009803 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009804
David S. Millerf47c11e2005-06-24 20:18:35 -07009805 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009806
Matt Carlson4f125f42009-09-01 12:55:02 +00009807 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9808 struct tg3_napi *tnapi = &tp->napi[i];
9809 free_irq(tnapi->irq_vec, tnapi);
9810 }
Matt Carlson07b01732009-08-28 14:01:15 +00009811
9812 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009813
Eric Dumazet511d2222010-07-07 20:44:24 +00009814 tg3_get_stats64(tp->dev, &tp->net_stats_prev);
9815
Linus Torvalds1da177e2005-04-16 15:20:36 -07009816 memcpy(&tp->estats_prev, tg3_get_estats(tp),
9817 sizeof(tp->estats_prev));
9818
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009819 tg3_napi_fini(tp);
9820
Linus Torvalds1da177e2005-04-16 15:20:36 -07009821 tg3_free_consistent(tp);
9822
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009823 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -08009824
9825 netif_carrier_off(tp->dev);
9826
Linus Torvalds1da177e2005-04-16 15:20:36 -07009827 return 0;
9828}
9829
Eric Dumazet511d2222010-07-07 20:44:24 +00009830static inline u64 get_stat64(tg3_stat64_t *val)
Stefan Buehler816f8b82008-08-15 14:10:54 -07009831{
9832 return ((u64)val->high << 32) | ((u64)val->low);
9833}
9834
Eric Dumazet511d2222010-07-07 20:44:24 +00009835static u64 calc_crc_errors(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009836{
9837 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9838
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009839 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009840 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
9841 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009842 u32 val;
9843
David S. Millerf47c11e2005-06-24 20:18:35 -07009844 spin_lock_bh(&tp->lock);
Michael Chan569a5df2007-02-13 12:18:15 -08009845 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
9846 tg3_writephy(tp, MII_TG3_TEST1,
9847 val | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009848 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009849 } else
9850 val = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07009851 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009852
9853 tp->phy_crc_errors += val;
9854
9855 return tp->phy_crc_errors;
9856 }
9857
9858 return get_stat64(&hw_stats->rx_fcs_errors);
9859}
9860
9861#define ESTAT_ADD(member) \
9862 estats->member = old_estats->member + \
Eric Dumazet511d2222010-07-07 20:44:24 +00009863 get_stat64(&hw_stats->member)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009864
9865static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *tp)
9866{
9867 struct tg3_ethtool_stats *estats = &tp->estats;
9868 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
9869 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9870
9871 if (!hw_stats)
9872 return old_estats;
9873
9874 ESTAT_ADD(rx_octets);
9875 ESTAT_ADD(rx_fragments);
9876 ESTAT_ADD(rx_ucast_packets);
9877 ESTAT_ADD(rx_mcast_packets);
9878 ESTAT_ADD(rx_bcast_packets);
9879 ESTAT_ADD(rx_fcs_errors);
9880 ESTAT_ADD(rx_align_errors);
9881 ESTAT_ADD(rx_xon_pause_rcvd);
9882 ESTAT_ADD(rx_xoff_pause_rcvd);
9883 ESTAT_ADD(rx_mac_ctrl_rcvd);
9884 ESTAT_ADD(rx_xoff_entered);
9885 ESTAT_ADD(rx_frame_too_long_errors);
9886 ESTAT_ADD(rx_jabbers);
9887 ESTAT_ADD(rx_undersize_packets);
9888 ESTAT_ADD(rx_in_length_errors);
9889 ESTAT_ADD(rx_out_length_errors);
9890 ESTAT_ADD(rx_64_or_less_octet_packets);
9891 ESTAT_ADD(rx_65_to_127_octet_packets);
9892 ESTAT_ADD(rx_128_to_255_octet_packets);
9893 ESTAT_ADD(rx_256_to_511_octet_packets);
9894 ESTAT_ADD(rx_512_to_1023_octet_packets);
9895 ESTAT_ADD(rx_1024_to_1522_octet_packets);
9896 ESTAT_ADD(rx_1523_to_2047_octet_packets);
9897 ESTAT_ADD(rx_2048_to_4095_octet_packets);
9898 ESTAT_ADD(rx_4096_to_8191_octet_packets);
9899 ESTAT_ADD(rx_8192_to_9022_octet_packets);
9900
9901 ESTAT_ADD(tx_octets);
9902 ESTAT_ADD(tx_collisions);
9903 ESTAT_ADD(tx_xon_sent);
9904 ESTAT_ADD(tx_xoff_sent);
9905 ESTAT_ADD(tx_flow_control);
9906 ESTAT_ADD(tx_mac_errors);
9907 ESTAT_ADD(tx_single_collisions);
9908 ESTAT_ADD(tx_mult_collisions);
9909 ESTAT_ADD(tx_deferred);
9910 ESTAT_ADD(tx_excessive_collisions);
9911 ESTAT_ADD(tx_late_collisions);
9912 ESTAT_ADD(tx_collide_2times);
9913 ESTAT_ADD(tx_collide_3times);
9914 ESTAT_ADD(tx_collide_4times);
9915 ESTAT_ADD(tx_collide_5times);
9916 ESTAT_ADD(tx_collide_6times);
9917 ESTAT_ADD(tx_collide_7times);
9918 ESTAT_ADD(tx_collide_8times);
9919 ESTAT_ADD(tx_collide_9times);
9920 ESTAT_ADD(tx_collide_10times);
9921 ESTAT_ADD(tx_collide_11times);
9922 ESTAT_ADD(tx_collide_12times);
9923 ESTAT_ADD(tx_collide_13times);
9924 ESTAT_ADD(tx_collide_14times);
9925 ESTAT_ADD(tx_collide_15times);
9926 ESTAT_ADD(tx_ucast_packets);
9927 ESTAT_ADD(tx_mcast_packets);
9928 ESTAT_ADD(tx_bcast_packets);
9929 ESTAT_ADD(tx_carrier_sense_errors);
9930 ESTAT_ADD(tx_discards);
9931 ESTAT_ADD(tx_errors);
9932
9933 ESTAT_ADD(dma_writeq_full);
9934 ESTAT_ADD(dma_write_prioq_full);
9935 ESTAT_ADD(rxbds_empty);
9936 ESTAT_ADD(rx_discards);
9937 ESTAT_ADD(rx_errors);
9938 ESTAT_ADD(rx_threshold_hit);
9939
9940 ESTAT_ADD(dma_readq_full);
9941 ESTAT_ADD(dma_read_prioq_full);
9942 ESTAT_ADD(tx_comp_queue_full);
9943
9944 ESTAT_ADD(ring_set_send_prod_index);
9945 ESTAT_ADD(ring_status_update);
9946 ESTAT_ADD(nic_irqs);
9947 ESTAT_ADD(nic_avoided_irqs);
9948 ESTAT_ADD(nic_tx_threshold_hit);
9949
Matt Carlson4452d092011-05-19 12:12:51 +00009950 ESTAT_ADD(mbuf_lwm_thresh_hit);
9951
Linus Torvalds1da177e2005-04-16 15:20:36 -07009952 return estats;
9953}
9954
Eric Dumazet511d2222010-07-07 20:44:24 +00009955static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
9956 struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009957{
9958 struct tg3 *tp = netdev_priv(dev);
Eric Dumazet511d2222010-07-07 20:44:24 +00009959 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009960 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9961
9962 if (!hw_stats)
9963 return old_stats;
9964
9965 stats->rx_packets = old_stats->rx_packets +
9966 get_stat64(&hw_stats->rx_ucast_packets) +
9967 get_stat64(&hw_stats->rx_mcast_packets) +
9968 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009969
Linus Torvalds1da177e2005-04-16 15:20:36 -07009970 stats->tx_packets = old_stats->tx_packets +
9971 get_stat64(&hw_stats->tx_ucast_packets) +
9972 get_stat64(&hw_stats->tx_mcast_packets) +
9973 get_stat64(&hw_stats->tx_bcast_packets);
9974
9975 stats->rx_bytes = old_stats->rx_bytes +
9976 get_stat64(&hw_stats->rx_octets);
9977 stats->tx_bytes = old_stats->tx_bytes +
9978 get_stat64(&hw_stats->tx_octets);
9979
9980 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -07009981 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009982 stats->tx_errors = old_stats->tx_errors +
9983 get_stat64(&hw_stats->tx_errors) +
9984 get_stat64(&hw_stats->tx_mac_errors) +
9985 get_stat64(&hw_stats->tx_carrier_sense_errors) +
9986 get_stat64(&hw_stats->tx_discards);
9987
9988 stats->multicast = old_stats->multicast +
9989 get_stat64(&hw_stats->rx_mcast_packets);
9990 stats->collisions = old_stats->collisions +
9991 get_stat64(&hw_stats->tx_collisions);
9992
9993 stats->rx_length_errors = old_stats->rx_length_errors +
9994 get_stat64(&hw_stats->rx_frame_too_long_errors) +
9995 get_stat64(&hw_stats->rx_undersize_packets);
9996
9997 stats->rx_over_errors = old_stats->rx_over_errors +
9998 get_stat64(&hw_stats->rxbds_empty);
9999 stats->rx_frame_errors = old_stats->rx_frame_errors +
10000 get_stat64(&hw_stats->rx_align_errors);
10001 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
10002 get_stat64(&hw_stats->tx_discards);
10003 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
10004 get_stat64(&hw_stats->tx_carrier_sense_errors);
10005
10006 stats->rx_crc_errors = old_stats->rx_crc_errors +
10007 calc_crc_errors(tp);
10008
John W. Linville4f63b872005-09-12 14:43:18 -070010009 stats->rx_missed_errors = old_stats->rx_missed_errors +
10010 get_stat64(&hw_stats->rx_discards);
10011
Eric Dumazetb0057c52010-10-10 19:55:52 +000010012 stats->rx_dropped = tp->rx_dropped;
10013
Linus Torvalds1da177e2005-04-16 15:20:36 -070010014 return stats;
10015}
10016
10017static inline u32 calc_crc(unsigned char *buf, int len)
10018{
10019 u32 reg;
10020 u32 tmp;
10021 int j, k;
10022
10023 reg = 0xffffffff;
10024
10025 for (j = 0; j < len; j++) {
10026 reg ^= buf[j];
10027
10028 for (k = 0; k < 8; k++) {
10029 tmp = reg & 0x01;
10030
10031 reg >>= 1;
10032
Matt Carlson859a5882010-04-05 10:19:28 +000010033 if (tmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010034 reg ^= 0xedb88320;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010035 }
10036 }
10037
10038 return ~reg;
10039}
10040
10041static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
10042{
10043 /* accept or reject all multicast frames */
10044 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
10045 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
10046 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
10047 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
10048}
10049
10050static void __tg3_set_rx_mode(struct net_device *dev)
10051{
10052 struct tg3 *tp = netdev_priv(dev);
10053 u32 rx_mode;
10054
10055 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
10056 RX_MODE_KEEP_VLAN_TAG);
10057
Matt Carlsonbf933c82011-01-25 15:58:49 +000010058#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010059 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
10060 * flag clear.
10061 */
Joe Perches63c3a662011-04-26 08:12:10 +000010062 if (!tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010063 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
10064#endif
10065
10066 if (dev->flags & IFF_PROMISC) {
10067 /* Promiscuous mode. */
10068 rx_mode |= RX_MODE_PROMISC;
10069 } else if (dev->flags & IFF_ALLMULTI) {
10070 /* Accept all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010071 tg3_set_multi(tp, 1);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +000010072 } else if (netdev_mc_empty(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010073 /* Reject all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010074 tg3_set_multi(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010075 } else {
10076 /* Accept one or more multicast(s). */
Jiri Pirko22bedad2010-04-01 21:22:57 +000010077 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010078 u32 mc_filter[4] = { 0, };
10079 u32 regidx;
10080 u32 bit;
10081 u32 crc;
10082
Jiri Pirko22bedad2010-04-01 21:22:57 +000010083 netdev_for_each_mc_addr(ha, dev) {
10084 crc = calc_crc(ha->addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010085 bit = ~crc & 0x7f;
10086 regidx = (bit & 0x60) >> 5;
10087 bit &= 0x1f;
10088 mc_filter[regidx] |= (1 << bit);
10089 }
10090
10091 tw32(MAC_HASH_REG_0, mc_filter[0]);
10092 tw32(MAC_HASH_REG_1, mc_filter[1]);
10093 tw32(MAC_HASH_REG_2, mc_filter[2]);
10094 tw32(MAC_HASH_REG_3, mc_filter[3]);
10095 }
10096
10097 if (rx_mode != tp->rx_mode) {
10098 tp->rx_mode = rx_mode;
10099 tw32_f(MAC_RX_MODE, rx_mode);
10100 udelay(10);
10101 }
10102}
10103
10104static void tg3_set_rx_mode(struct net_device *dev)
10105{
10106 struct tg3 *tp = netdev_priv(dev);
10107
Michael Chane75f7c92006-03-20 21:33:26 -080010108 if (!netif_running(dev))
10109 return;
10110
David S. Millerf47c11e2005-06-24 20:18:35 -070010111 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010112 __tg3_set_rx_mode(dev);
David S. Millerf47c11e2005-06-24 20:18:35 -070010113 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010114}
10115
Linus Torvalds1da177e2005-04-16 15:20:36 -070010116static int tg3_get_regs_len(struct net_device *dev)
10117{
Matt Carlson97bd8e42011-04-13 11:05:04 +000010118 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010119}
10120
10121static void tg3_get_regs(struct net_device *dev,
10122 struct ethtool_regs *regs, void *_p)
10123{
Linus Torvalds1da177e2005-04-16 15:20:36 -070010124 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010125
10126 regs->version = 0;
10127
Matt Carlson97bd8e42011-04-13 11:05:04 +000010128 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010129
Matt Carlson80096062010-08-02 11:26:06 +000010130 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010131 return;
10132
David S. Millerf47c11e2005-06-24 20:18:35 -070010133 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010134
Matt Carlson97bd8e42011-04-13 11:05:04 +000010135 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010136
David S. Millerf47c11e2005-06-24 20:18:35 -070010137 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010138}
10139
10140static int tg3_get_eeprom_len(struct net_device *dev)
10141{
10142 struct tg3 *tp = netdev_priv(dev);
10143
10144 return tp->nvram_size;
10145}
10146
Linus Torvalds1da177e2005-04-16 15:20:36 -070010147static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
10148{
10149 struct tg3 *tp = netdev_priv(dev);
10150 int ret;
10151 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -080010152 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010153 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010154
Joe Perches63c3a662011-04-26 08:12:10 +000010155 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000010156 return -EINVAL;
10157
Matt Carlson80096062010-08-02 11:26:06 +000010158 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010159 return -EAGAIN;
10160
Linus Torvalds1da177e2005-04-16 15:20:36 -070010161 offset = eeprom->offset;
10162 len = eeprom->len;
10163 eeprom->len = 0;
10164
10165 eeprom->magic = TG3_EEPROM_MAGIC;
10166
10167 if (offset & 3) {
10168 /* adjustments to start on required 4 byte boundary */
10169 b_offset = offset & 3;
10170 b_count = 4 - b_offset;
10171 if (b_count > len) {
10172 /* i.e. offset=1 len=2 */
10173 b_count = len;
10174 }
Matt Carlsona9dc5292009-02-25 14:25:30 +000010175 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010176 if (ret)
10177 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +000010178 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010179 len -= b_count;
10180 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010181 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010182 }
10183
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010184 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010185 pd = &data[eeprom->len];
10186 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010187 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010188 if (ret) {
10189 eeprom->len += i;
10190 return ret;
10191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010192 memcpy(pd + i, &val, 4);
10193 }
10194 eeprom->len += i;
10195
10196 if (len & 3) {
10197 /* read last bytes not ending on 4 byte boundary */
10198 pd = &data[eeprom->len];
10199 b_count = len & 3;
10200 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010201 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010202 if (ret)
10203 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080010204 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010205 eeprom->len += b_count;
10206 }
10207 return 0;
10208}
10209
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010210static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010211
10212static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
10213{
10214 struct tg3 *tp = netdev_priv(dev);
10215 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080010216 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010217 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010218 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010219
Matt Carlson80096062010-08-02 11:26:06 +000010220 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010221 return -EAGAIN;
10222
Joe Perches63c3a662011-04-26 08:12:10 +000010223 if (tg3_flag(tp, NO_NVRAM) ||
Matt Carlsondf259d82009-04-20 06:57:14 +000010224 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010225 return -EINVAL;
10226
10227 offset = eeprom->offset;
10228 len = eeprom->len;
10229
10230 if ((b_offset = (offset & 3))) {
10231 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010232 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010233 if (ret)
10234 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010235 len += b_offset;
10236 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -070010237 if (len < 4)
10238 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010239 }
10240
10241 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -070010242 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010243 /* adjustments to end on required 4 byte boundary */
10244 odd_len = 1;
10245 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010246 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010247 if (ret)
10248 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010249 }
10250
10251 buf = data;
10252 if (b_offset || odd_len) {
10253 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010254 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010255 return -ENOMEM;
10256 if (b_offset)
10257 memcpy(buf, &start, 4);
10258 if (odd_len)
10259 memcpy(buf+len-4, &end, 4);
10260 memcpy(buf + b_offset, data, eeprom->len);
10261 }
10262
10263 ret = tg3_nvram_write_block(tp, offset, len, buf);
10264
10265 if (buf != data)
10266 kfree(buf);
10267
10268 return ret;
10269}
10270
10271static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
10272{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010273 struct tg3 *tp = netdev_priv(dev);
10274
Joe Perches63c3a662011-04-26 08:12:10 +000010275 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010276 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010277 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010278 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010279 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10280 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010281 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010282
Linus Torvalds1da177e2005-04-16 15:20:36 -070010283 cmd->supported = (SUPPORTED_Autoneg);
10284
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010285 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010286 cmd->supported |= (SUPPORTED_1000baseT_Half |
10287 SUPPORTED_1000baseT_Full);
10288
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010289 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010290 cmd->supported |= (SUPPORTED_100baseT_Half |
10291 SUPPORTED_100baseT_Full |
10292 SUPPORTED_10baseT_Half |
10293 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -080010294 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -070010295 cmd->port = PORT_TP;
10296 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010297 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -070010298 cmd->port = PORT_FIBRE;
10299 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010300
Linus Torvalds1da177e2005-04-16 15:20:36 -070010301 cmd->advertising = tp->link_config.advertising;
Matt Carlson5bb09772011-06-13 13:39:00 +000010302 if (tg3_flag(tp, PAUSE_AUTONEG)) {
10303 if (tp->link_config.flowctrl & FLOW_CTRL_RX) {
10304 if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
10305 cmd->advertising |= ADVERTISED_Pause;
10306 } else {
10307 cmd->advertising |= ADVERTISED_Pause |
10308 ADVERTISED_Asym_Pause;
10309 }
10310 } else if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
10311 cmd->advertising |= ADVERTISED_Asym_Pause;
10312 }
10313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010314 if (netif_running(dev)) {
David Decotigny70739492011-04-27 18:32:40 +000010315 ethtool_cmd_speed_set(cmd, tp->link_config.active_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010316 cmd->duplex = tp->link_config.active_duplex;
Matt Carlson64c22182010-10-14 10:37:44 +000010317 } else {
David Decotigny70739492011-04-27 18:32:40 +000010318 ethtool_cmd_speed_set(cmd, SPEED_INVALID);
Matt Carlson64c22182010-10-14 10:37:44 +000010319 cmd->duplex = DUPLEX_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010320 }
Matt Carlson882e9792009-09-01 13:21:36 +000010321 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +000010322 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010323 cmd->autoneg = tp->link_config.autoneg;
10324 cmd->maxtxpkt = 0;
10325 cmd->maxrxpkt = 0;
10326 return 0;
10327}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010328
Linus Torvalds1da177e2005-04-16 15:20:36 -070010329static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
10330{
10331 struct tg3 *tp = netdev_priv(dev);
David Decotigny25db0332011-04-27 18:32:39 +000010332 u32 speed = ethtool_cmd_speed(cmd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010333
Joe Perches63c3a662011-04-26 08:12:10 +000010334 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010335 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010336 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010337 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010338 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10339 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010340 }
10341
Matt Carlson7e5856b2009-02-25 14:23:01 +000010342 if (cmd->autoneg != AUTONEG_ENABLE &&
10343 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -070010344 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +000010345
10346 if (cmd->autoneg == AUTONEG_DISABLE &&
10347 cmd->duplex != DUPLEX_FULL &&
10348 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -070010349 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010350
Matt Carlson7e5856b2009-02-25 14:23:01 +000010351 if (cmd->autoneg == AUTONEG_ENABLE) {
10352 u32 mask = ADVERTISED_Autoneg |
10353 ADVERTISED_Pause |
10354 ADVERTISED_Asym_Pause;
10355
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010356 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010357 mask |= ADVERTISED_1000baseT_Half |
10358 ADVERTISED_1000baseT_Full;
10359
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010360 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010361 mask |= ADVERTISED_100baseT_Half |
10362 ADVERTISED_100baseT_Full |
10363 ADVERTISED_10baseT_Half |
10364 ADVERTISED_10baseT_Full |
10365 ADVERTISED_TP;
10366 else
10367 mask |= ADVERTISED_FIBRE;
10368
10369 if (cmd->advertising & ~mask)
10370 return -EINVAL;
10371
10372 mask &= (ADVERTISED_1000baseT_Half |
10373 ADVERTISED_1000baseT_Full |
10374 ADVERTISED_100baseT_Half |
10375 ADVERTISED_100baseT_Full |
10376 ADVERTISED_10baseT_Half |
10377 ADVERTISED_10baseT_Full);
10378
10379 cmd->advertising &= mask;
10380 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010381 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
David Decotigny25db0332011-04-27 18:32:39 +000010382 if (speed != SPEED_1000)
Matt Carlson7e5856b2009-02-25 14:23:01 +000010383 return -EINVAL;
10384
10385 if (cmd->duplex != DUPLEX_FULL)
10386 return -EINVAL;
10387 } else {
David Decotigny25db0332011-04-27 18:32:39 +000010388 if (speed != SPEED_100 &&
10389 speed != SPEED_10)
Matt Carlson7e5856b2009-02-25 14:23:01 +000010390 return -EINVAL;
10391 }
10392 }
10393
David S. Millerf47c11e2005-06-24 20:18:35 -070010394 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010395
10396 tp->link_config.autoneg = cmd->autoneg;
10397 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -070010398 tp->link_config.advertising = (cmd->advertising |
10399 ADVERTISED_Autoneg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010400 tp->link_config.speed = SPEED_INVALID;
10401 tp->link_config.duplex = DUPLEX_INVALID;
10402 } else {
10403 tp->link_config.advertising = 0;
David Decotigny25db0332011-04-27 18:32:39 +000010404 tp->link_config.speed = speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010405 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010406 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010407
Michael Chan24fcad62006-12-17 17:06:46 -080010408 tp->link_config.orig_speed = tp->link_config.speed;
10409 tp->link_config.orig_duplex = tp->link_config.duplex;
10410 tp->link_config.orig_autoneg = tp->link_config.autoneg;
10411
Linus Torvalds1da177e2005-04-16 15:20:36 -070010412 if (netif_running(dev))
10413 tg3_setup_phy(tp, 1);
10414
David S. Millerf47c11e2005-06-24 20:18:35 -070010415 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010416
Linus Torvalds1da177e2005-04-16 15:20:36 -070010417 return 0;
10418}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010419
Linus Torvalds1da177e2005-04-16 15:20:36 -070010420static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
10421{
10422 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010423
Linus Torvalds1da177e2005-04-16 15:20:36 -070010424 strcpy(info->driver, DRV_MODULE_NAME);
10425 strcpy(info->version, DRV_MODULE_VERSION);
Michael Chanc4e65752006-03-20 22:29:32 -080010426 strcpy(info->fw_version, tp->fw_ver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010427 strcpy(info->bus_info, pci_name(tp->pdev));
10428}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010429
Linus Torvalds1da177e2005-04-16 15:20:36 -070010430static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10431{
10432 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010433
Joe Perches63c3a662011-04-26 08:12:10 +000010434 if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070010435 wol->supported = WAKE_MAGIC;
10436 else
10437 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010438 wol->wolopts = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010439 if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010440 wol->wolopts = WAKE_MAGIC;
10441 memset(&wol->sopass, 0, sizeof(wol->sopass));
10442}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010443
Linus Torvalds1da177e2005-04-16 15:20:36 -070010444static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10445{
10446 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070010447 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010448
Linus Torvalds1da177e2005-04-16 15:20:36 -070010449 if (wol->wolopts & ~WAKE_MAGIC)
10450 return -EINVAL;
10451 if ((wol->wolopts & WAKE_MAGIC) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010452 !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010453 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010454
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010455 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
10456
David S. Millerf47c11e2005-06-24 20:18:35 -070010457 spin_lock_bh(&tp->lock);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010458 if (device_may_wakeup(dp))
Joe Perches63c3a662011-04-26 08:12:10 +000010459 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010460 else
Joe Perches63c3a662011-04-26 08:12:10 +000010461 tg3_flag_clear(tp, WOL_ENABLE);
David S. Millerf47c11e2005-06-24 20:18:35 -070010462 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010463
Linus Torvalds1da177e2005-04-16 15:20:36 -070010464 return 0;
10465}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010466
Linus Torvalds1da177e2005-04-16 15:20:36 -070010467static u32 tg3_get_msglevel(struct net_device *dev)
10468{
10469 struct tg3 *tp = netdev_priv(dev);
10470 return tp->msg_enable;
10471}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010472
Linus Torvalds1da177e2005-04-16 15:20:36 -070010473static void tg3_set_msglevel(struct net_device *dev, u32 value)
10474{
10475 struct tg3 *tp = netdev_priv(dev);
10476 tp->msg_enable = value;
10477}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010478
Linus Torvalds1da177e2005-04-16 15:20:36 -070010479static int tg3_nway_reset(struct net_device *dev)
10480{
10481 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010482 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010483
Linus Torvalds1da177e2005-04-16 15:20:36 -070010484 if (!netif_running(dev))
10485 return -EAGAIN;
10486
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010487 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070010488 return -EINVAL;
10489
Joe Perches63c3a662011-04-26 08:12:10 +000010490 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010491 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010492 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010493 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010494 } else {
10495 u32 bmcr;
10496
10497 spin_lock_bh(&tp->lock);
10498 r = -EINVAL;
10499 tg3_readphy(tp, MII_BMCR, &bmcr);
10500 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
10501 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010502 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010503 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
10504 BMCR_ANENABLE);
10505 r = 0;
10506 }
10507 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010508 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010509
Linus Torvalds1da177e2005-04-16 15:20:36 -070010510 return r;
10511}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010512
Linus Torvalds1da177e2005-04-16 15:20:36 -070010513static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10514{
10515 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010516
Matt Carlson2c49a442010-09-30 10:34:35 +000010517 ering->rx_max_pending = tp->rx_std_ring_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010518 ering->rx_mini_max_pending = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010519 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson2c49a442010-09-30 10:34:35 +000010520 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080010521 else
10522 ering->rx_jumbo_max_pending = 0;
10523
10524 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010525
10526 ering->rx_pending = tp->rx_pending;
10527 ering->rx_mini_pending = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010528 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Michael Chan4f81c322006-03-20 21:33:42 -080010529 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
10530 else
10531 ering->rx_jumbo_pending = 0;
10532
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010533 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010534}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010535
Linus Torvalds1da177e2005-04-16 15:20:36 -070010536static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10537{
10538 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000010539 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010540
Matt Carlson2c49a442010-09-30 10:34:35 +000010541 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
10542 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070010543 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
10544 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Joe Perches63c3a662011-04-26 08:12:10 +000010545 (tg3_flag(tp, TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070010546 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010547 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010548
Michael Chanbbe832c2005-06-24 20:20:04 -070010549 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010550 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010551 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010552 irq_sync = 1;
10553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010554
Michael Chanbbe832c2005-06-24 20:20:04 -070010555 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010556
Linus Torvalds1da177e2005-04-16 15:20:36 -070010557 tp->rx_pending = ering->rx_pending;
10558
Joe Perches63c3a662011-04-26 08:12:10 +000010559 if (tg3_flag(tp, MAX_RXPEND_64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010560 tp->rx_pending > 63)
10561 tp->rx_pending = 63;
10562 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000010563
Matt Carlson6fd45cb2010-09-15 08:59:57 +000010564 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000010565 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010566
10567 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070010568 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -070010569 err = tg3_restart_hw(tp, 1);
10570 if (!err)
10571 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010572 }
10573
David S. Millerf47c11e2005-06-24 20:18:35 -070010574 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010575
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010576 if (irq_sync && !err)
10577 tg3_phy_start(tp);
10578
Michael Chanb9ec6c12006-07-25 16:37:27 -070010579 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010580}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010581
Linus Torvalds1da177e2005-04-16 15:20:36 -070010582static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10583{
10584 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010585
Joe Perches63c3a662011-04-26 08:12:10 +000010586 epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
Matt Carlson8d018622007-12-20 20:05:44 -080010587
Steve Glendinninge18ce342008-12-16 02:00:00 -080010588 if (tp->link_config.active_flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080010589 epause->rx_pause = 1;
10590 else
10591 epause->rx_pause = 0;
10592
Steve Glendinninge18ce342008-12-16 02:00:00 -080010593 if (tp->link_config.active_flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080010594 epause->tx_pause = 1;
10595 else
10596 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010597}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010598
Linus Torvalds1da177e2005-04-16 15:20:36 -070010599static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10600{
10601 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010602 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010603
Joe Perches63c3a662011-04-26 08:12:10 +000010604 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson27121682010-02-17 15:16:57 +000010605 u32 newadv;
10606 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010607
Matt Carlson27121682010-02-17 15:16:57 +000010608 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010609
Matt Carlson27121682010-02-17 15:16:57 +000010610 if (!(phydev->supported & SUPPORTED_Pause) ||
10611 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000010612 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000010613 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010614
Matt Carlson27121682010-02-17 15:16:57 +000010615 tp->link_config.flowctrl = 0;
10616 if (epause->rx_pause) {
10617 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010618
Matt Carlson27121682010-02-17 15:16:57 +000010619 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080010620 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000010621 newadv = ADVERTISED_Pause;
10622 } else
10623 newadv = ADVERTISED_Pause |
10624 ADVERTISED_Asym_Pause;
10625 } else if (epause->tx_pause) {
10626 tp->link_config.flowctrl |= FLOW_CTRL_TX;
10627 newadv = ADVERTISED_Asym_Pause;
10628 } else
10629 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010630
Matt Carlson27121682010-02-17 15:16:57 +000010631 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010632 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010633 else
Joe Perches63c3a662011-04-26 08:12:10 +000010634 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010635
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010636 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000010637 u32 oldadv = phydev->advertising &
10638 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
10639 if (oldadv != newadv) {
10640 phydev->advertising &=
10641 ~(ADVERTISED_Pause |
10642 ADVERTISED_Asym_Pause);
10643 phydev->advertising |= newadv;
10644 if (phydev->autoneg) {
10645 /*
10646 * Always renegotiate the link to
10647 * inform our link partner of our
10648 * flow control settings, even if the
10649 * flow control is forced. Let
10650 * tg3_adjust_link() do the final
10651 * flow control setup.
10652 */
10653 return phy_start_aneg(phydev);
10654 }
10655 }
10656
10657 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010658 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000010659 } else {
10660 tp->link_config.orig_advertising &=
10661 ~(ADVERTISED_Pause |
10662 ADVERTISED_Asym_Pause);
10663 tp->link_config.orig_advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010664 }
10665 } else {
10666 int irq_sync = 0;
10667
10668 if (netif_running(dev)) {
10669 tg3_netif_stop(tp);
10670 irq_sync = 1;
10671 }
10672
10673 tg3_full_lock(tp, irq_sync);
10674
10675 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010676 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010677 else
Joe Perches63c3a662011-04-26 08:12:10 +000010678 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010679 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010680 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010681 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010682 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010683 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010684 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010685 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010686 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010687
10688 if (netif_running(dev)) {
10689 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10690 err = tg3_restart_hw(tp, 1);
10691 if (!err)
10692 tg3_netif_start(tp);
10693 }
10694
10695 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010697
Michael Chanb9ec6c12006-07-25 16:37:27 -070010698 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010699}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010700
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010701static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010702{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010703 switch (sset) {
10704 case ETH_SS_TEST:
10705 return TG3_NUM_TEST;
10706 case ETH_SS_STATS:
10707 return TG3_NUM_STATS;
10708 default:
10709 return -EOPNOTSUPP;
10710 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070010711}
10712
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010713static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010714{
10715 switch (stringset) {
10716 case ETH_SS_STATS:
10717 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
10718 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070010719 case ETH_SS_TEST:
10720 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
10721 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010722 default:
10723 WARN_ON(1); /* we need a WARN() */
10724 break;
10725 }
10726}
10727
stephen hemminger81b87092011-04-04 08:43:50 +000010728static int tg3_set_phys_id(struct net_device *dev,
10729 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070010730{
10731 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070010732
10733 if (!netif_running(tp->dev))
10734 return -EAGAIN;
10735
stephen hemminger81b87092011-04-04 08:43:50 +000010736 switch (state) {
10737 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +000010738 return 1; /* cycle on/off once per second */
Michael Chan4009a932005-09-05 17:52:54 -070010739
stephen hemminger81b87092011-04-04 08:43:50 +000010740 case ETHTOOL_ID_ON:
10741 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10742 LED_CTRL_1000MBPS_ON |
10743 LED_CTRL_100MBPS_ON |
10744 LED_CTRL_10MBPS_ON |
10745 LED_CTRL_TRAFFIC_OVERRIDE |
10746 LED_CTRL_TRAFFIC_BLINK |
10747 LED_CTRL_TRAFFIC_LED);
10748 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010749
stephen hemminger81b87092011-04-04 08:43:50 +000010750 case ETHTOOL_ID_OFF:
10751 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10752 LED_CTRL_TRAFFIC_OVERRIDE);
10753 break;
Michael Chan4009a932005-09-05 17:52:54 -070010754
stephen hemminger81b87092011-04-04 08:43:50 +000010755 case ETHTOOL_ID_INACTIVE:
10756 tw32(MAC_LED_CTRL, tp->led_ctrl);
10757 break;
Michael Chan4009a932005-09-05 17:52:54 -070010758 }
stephen hemminger81b87092011-04-04 08:43:50 +000010759
Michael Chan4009a932005-09-05 17:52:54 -070010760 return 0;
10761}
10762
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010763static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010764 struct ethtool_stats *estats, u64 *tmp_stats)
10765{
10766 struct tg3 *tp = netdev_priv(dev);
10767 memcpy(tmp_stats, tg3_get_estats(tp), sizeof(tp->estats));
10768}
10769
Matt Carlson535a4902011-07-20 10:20:56 +000010770static __be32 *tg3_vpd_readblock(struct tg3 *tp, u32 *vpdlen)
Matt Carlsonc3e94502011-04-13 11:05:08 +000010771{
10772 int i;
10773 __be32 *buf;
10774 u32 offset = 0, len = 0;
10775 u32 magic, val;
10776
Joe Perches63c3a662011-04-26 08:12:10 +000010777 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
Matt Carlsonc3e94502011-04-13 11:05:08 +000010778 return NULL;
10779
10780 if (magic == TG3_EEPROM_MAGIC) {
10781 for (offset = TG3_NVM_DIR_START;
10782 offset < TG3_NVM_DIR_END;
10783 offset += TG3_NVM_DIRENT_SIZE) {
10784 if (tg3_nvram_read(tp, offset, &val))
10785 return NULL;
10786
10787 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
10788 TG3_NVM_DIRTYPE_EXTVPD)
10789 break;
10790 }
10791
10792 if (offset != TG3_NVM_DIR_END) {
10793 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
10794 if (tg3_nvram_read(tp, offset + 4, &offset))
10795 return NULL;
10796
10797 offset = tg3_nvram_logical_addr(tp, offset);
10798 }
10799 }
10800
10801 if (!offset || !len) {
10802 offset = TG3_NVM_VPD_OFF;
10803 len = TG3_NVM_VPD_LEN;
10804 }
10805
10806 buf = kmalloc(len, GFP_KERNEL);
10807 if (buf == NULL)
10808 return NULL;
10809
10810 if (magic == TG3_EEPROM_MAGIC) {
10811 for (i = 0; i < len; i += 4) {
10812 /* The data is in little-endian format in NVRAM.
10813 * Use the big-endian read routines to preserve
10814 * the byte order as it exists in NVRAM.
10815 */
10816 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
10817 goto error;
10818 }
10819 } else {
10820 u8 *ptr;
10821 ssize_t cnt;
10822 unsigned int pos = 0;
10823
10824 ptr = (u8 *)&buf[0];
10825 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
10826 cnt = pci_read_vpd(tp->pdev, pos,
10827 len - pos, ptr);
10828 if (cnt == -ETIMEDOUT || cnt == -EINTR)
10829 cnt = 0;
10830 else if (cnt < 0)
10831 goto error;
10832 }
10833 if (pos != len)
10834 goto error;
10835 }
10836
Matt Carlson535a4902011-07-20 10:20:56 +000010837 *vpdlen = len;
10838
Matt Carlsonc3e94502011-04-13 11:05:08 +000010839 return buf;
10840
10841error:
10842 kfree(buf);
10843 return NULL;
10844}
10845
Michael Chan566f86a2005-05-29 14:56:58 -070010846#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080010847#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
10848#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
10849#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Matt Carlson727a6d92011-06-13 13:38:58 +000010850#define NVRAM_SELFBOOT_FORMAT1_4_SIZE 0x20
10851#define NVRAM_SELFBOOT_FORMAT1_5_SIZE 0x24
Matt Carlsonbda18fa2011-07-20 10:20:57 +000010852#define NVRAM_SELFBOOT_FORMAT1_6_SIZE 0x50
Michael Chanb16250e2006-09-27 16:10:14 -070010853#define NVRAM_SELFBOOT_HW_SIZE 0x20
10854#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070010855
10856static int tg3_test_nvram(struct tg3 *tp)
10857{
Matt Carlson535a4902011-07-20 10:20:56 +000010858 u32 csum, magic, len;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010859 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010860 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070010861
Joe Perches63c3a662011-04-26 08:12:10 +000010862 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000010863 return 0;
10864
Matt Carlsone4f34112009-02-25 14:25:00 +000010865 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080010866 return -EIO;
10867
Michael Chan1b277772006-03-20 22:27:48 -080010868 if (magic == TG3_EEPROM_MAGIC)
10869 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070010870 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080010871 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
10872 TG3_EEPROM_SB_FORMAT_1) {
10873 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
10874 case TG3_EEPROM_SB_REVISION_0:
10875 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
10876 break;
10877 case TG3_EEPROM_SB_REVISION_2:
10878 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
10879 break;
10880 case TG3_EEPROM_SB_REVISION_3:
10881 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
10882 break;
Matt Carlson727a6d92011-06-13 13:38:58 +000010883 case TG3_EEPROM_SB_REVISION_4:
10884 size = NVRAM_SELFBOOT_FORMAT1_4_SIZE;
10885 break;
10886 case TG3_EEPROM_SB_REVISION_5:
10887 size = NVRAM_SELFBOOT_FORMAT1_5_SIZE;
10888 break;
10889 case TG3_EEPROM_SB_REVISION_6:
10890 size = NVRAM_SELFBOOT_FORMAT1_6_SIZE;
10891 break;
Matt Carlsona5767de2007-11-12 21:10:58 -080010892 default:
Matt Carlson727a6d92011-06-13 13:38:58 +000010893 return -EIO;
Matt Carlsona5767de2007-11-12 21:10:58 -080010894 }
10895 } else
Michael Chan1b277772006-03-20 22:27:48 -080010896 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070010897 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
10898 size = NVRAM_SELFBOOT_HW_SIZE;
10899 else
Michael Chan1b277772006-03-20 22:27:48 -080010900 return -EIO;
10901
10902 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070010903 if (buf == NULL)
10904 return -ENOMEM;
10905
Michael Chan1b277772006-03-20 22:27:48 -080010906 err = -EIO;
10907 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010908 err = tg3_nvram_read_be32(tp, i, &buf[j]);
10909 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070010910 break;
Michael Chan566f86a2005-05-29 14:56:58 -070010911 }
Michael Chan1b277772006-03-20 22:27:48 -080010912 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070010913 goto out;
10914
Michael Chan1b277772006-03-20 22:27:48 -080010915 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010916 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080010917 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010918 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080010919 u8 *buf8 = (u8 *) buf, csum8 = 0;
10920
Al Virob9fc7dc2007-12-17 22:59:57 -080010921 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080010922 TG3_EEPROM_SB_REVISION_2) {
10923 /* For rev 2, the csum doesn't include the MBA. */
10924 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
10925 csum8 += buf8[i];
10926 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
10927 csum8 += buf8[i];
10928 } else {
10929 for (i = 0; i < size; i++)
10930 csum8 += buf8[i];
10931 }
Michael Chan1b277772006-03-20 22:27:48 -080010932
Adrian Bunkad96b482006-04-05 22:21:04 -070010933 if (csum8 == 0) {
10934 err = 0;
10935 goto out;
10936 }
10937
10938 err = -EIO;
10939 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080010940 }
Michael Chan566f86a2005-05-29 14:56:58 -070010941
Al Virob9fc7dc2007-12-17 22:59:57 -080010942 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010943 TG3_EEPROM_MAGIC_HW) {
10944 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000010945 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070010946 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070010947
10948 /* Separate the parity bits and the data bytes. */
10949 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
10950 if ((i == 0) || (i == 8)) {
10951 int l;
10952 u8 msk;
10953
10954 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
10955 parity[k++] = buf8[i] & msk;
10956 i++;
Matt Carlson859a5882010-04-05 10:19:28 +000010957 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070010958 int l;
10959 u8 msk;
10960
10961 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
10962 parity[k++] = buf8[i] & msk;
10963 i++;
10964
10965 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
10966 parity[k++] = buf8[i] & msk;
10967 i++;
10968 }
10969 data[j++] = buf8[i];
10970 }
10971
10972 err = -EIO;
10973 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
10974 u8 hw8 = hweight8(data[i]);
10975
10976 if ((hw8 & 0x1) && parity[i])
10977 goto out;
10978 else if (!(hw8 & 0x1) && !parity[i])
10979 goto out;
10980 }
10981 err = 0;
10982 goto out;
10983 }
10984
Matt Carlson01c3a392011-03-09 16:58:20 +000010985 err = -EIO;
10986
Michael Chan566f86a2005-05-29 14:56:58 -070010987 /* Bootstrap checksum at offset 0x10 */
10988 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000010989 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070010990 goto out;
10991
10992 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
10993 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000010994 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000010995 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070010996
Matt Carlsonc3e94502011-04-13 11:05:08 +000010997 kfree(buf);
10998
Matt Carlson535a4902011-07-20 10:20:56 +000010999 buf = tg3_vpd_readblock(tp, &len);
Matt Carlsonc3e94502011-04-13 11:05:08 +000011000 if (!buf)
11001 return -ENOMEM;
Matt Carlsond4894f32011-03-09 16:58:21 +000011002
Matt Carlson535a4902011-07-20 10:20:56 +000011003 i = pci_vpd_find_tag((u8 *)buf, 0, len, PCI_VPD_LRDT_RO_DATA);
Matt Carlsond4894f32011-03-09 16:58:21 +000011004 if (i > 0) {
11005 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
11006 if (j < 0)
11007 goto out;
11008
Matt Carlson535a4902011-07-20 10:20:56 +000011009 if (i + PCI_VPD_LRDT_TAG_SIZE + j > len)
Matt Carlsond4894f32011-03-09 16:58:21 +000011010 goto out;
11011
11012 i += PCI_VPD_LRDT_TAG_SIZE;
11013 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
11014 PCI_VPD_RO_KEYWORD_CHKSUM);
11015 if (j > 0) {
11016 u8 csum8 = 0;
11017
11018 j += PCI_VPD_INFO_FLD_HDR_SIZE;
11019
11020 for (i = 0; i <= j; i++)
11021 csum8 += ((u8 *)buf)[i];
11022
11023 if (csum8)
11024 goto out;
11025 }
11026 }
11027
Michael Chan566f86a2005-05-29 14:56:58 -070011028 err = 0;
11029
11030out:
11031 kfree(buf);
11032 return err;
11033}
11034
Michael Chanca430072005-05-29 14:57:23 -070011035#define TG3_SERDES_TIMEOUT_SEC 2
11036#define TG3_COPPER_TIMEOUT_SEC 6
11037
11038static int tg3_test_link(struct tg3 *tp)
11039{
11040 int i, max;
11041
11042 if (!netif_running(tp->dev))
11043 return -ENODEV;
11044
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011045 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070011046 max = TG3_SERDES_TIMEOUT_SEC;
11047 else
11048 max = TG3_COPPER_TIMEOUT_SEC;
11049
11050 for (i = 0; i < max; i++) {
11051 if (netif_carrier_ok(tp->dev))
11052 return 0;
11053
11054 if (msleep_interruptible(1000))
11055 break;
11056 }
11057
11058 return -EIO;
11059}
11060
Michael Chana71116d2005-05-29 14:58:11 -070011061/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080011062static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070011063{
Michael Chanb16250e2006-09-27 16:10:14 -070011064 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070011065 u32 offset, read_mask, write_mask, val, save_val, read_val;
11066 static struct {
11067 u16 offset;
11068 u16 flags;
11069#define TG3_FL_5705 0x1
11070#define TG3_FL_NOT_5705 0x2
11071#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070011072#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070011073 u32 read_mask;
11074 u32 write_mask;
11075 } reg_tbl[] = {
11076 /* MAC Control Registers */
11077 { MAC_MODE, TG3_FL_NOT_5705,
11078 0x00000000, 0x00ef6f8c },
11079 { MAC_MODE, TG3_FL_5705,
11080 0x00000000, 0x01ef6b8c },
11081 { MAC_STATUS, TG3_FL_NOT_5705,
11082 0x03800107, 0x00000000 },
11083 { MAC_STATUS, TG3_FL_5705,
11084 0x03800100, 0x00000000 },
11085 { MAC_ADDR_0_HIGH, 0x0000,
11086 0x00000000, 0x0000ffff },
11087 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011088 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070011089 { MAC_RX_MTU_SIZE, 0x0000,
11090 0x00000000, 0x0000ffff },
11091 { MAC_TX_MODE, 0x0000,
11092 0x00000000, 0x00000070 },
11093 { MAC_TX_LENGTHS, 0x0000,
11094 0x00000000, 0x00003fff },
11095 { MAC_RX_MODE, TG3_FL_NOT_5705,
11096 0x00000000, 0x000007fc },
11097 { MAC_RX_MODE, TG3_FL_5705,
11098 0x00000000, 0x000007dc },
11099 { MAC_HASH_REG_0, 0x0000,
11100 0x00000000, 0xffffffff },
11101 { MAC_HASH_REG_1, 0x0000,
11102 0x00000000, 0xffffffff },
11103 { MAC_HASH_REG_2, 0x0000,
11104 0x00000000, 0xffffffff },
11105 { MAC_HASH_REG_3, 0x0000,
11106 0x00000000, 0xffffffff },
11107
11108 /* Receive Data and Receive BD Initiator Control Registers. */
11109 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
11110 0x00000000, 0xffffffff },
11111 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
11112 0x00000000, 0xffffffff },
11113 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
11114 0x00000000, 0x00000003 },
11115 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
11116 0x00000000, 0xffffffff },
11117 { RCVDBDI_STD_BD+0, 0x0000,
11118 0x00000000, 0xffffffff },
11119 { RCVDBDI_STD_BD+4, 0x0000,
11120 0x00000000, 0xffffffff },
11121 { RCVDBDI_STD_BD+8, 0x0000,
11122 0x00000000, 0xffff0002 },
11123 { RCVDBDI_STD_BD+0xc, 0x0000,
11124 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011125
Michael Chana71116d2005-05-29 14:58:11 -070011126 /* Receive BD Initiator Control Registers. */
11127 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
11128 0x00000000, 0xffffffff },
11129 { RCVBDI_STD_THRESH, TG3_FL_5705,
11130 0x00000000, 0x000003ff },
11131 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
11132 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011133
Michael Chana71116d2005-05-29 14:58:11 -070011134 /* Host Coalescing Control Registers. */
11135 { HOSTCC_MODE, TG3_FL_NOT_5705,
11136 0x00000000, 0x00000004 },
11137 { HOSTCC_MODE, TG3_FL_5705,
11138 0x00000000, 0x000000f6 },
11139 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
11140 0x00000000, 0xffffffff },
11141 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
11142 0x00000000, 0x000003ff },
11143 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
11144 0x00000000, 0xffffffff },
11145 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
11146 0x00000000, 0x000003ff },
11147 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
11148 0x00000000, 0xffffffff },
11149 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
11150 0x00000000, 0x000000ff },
11151 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
11152 0x00000000, 0xffffffff },
11153 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
11154 0x00000000, 0x000000ff },
11155 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
11156 0x00000000, 0xffffffff },
11157 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
11158 0x00000000, 0xffffffff },
11159 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
11160 0x00000000, 0xffffffff },
11161 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
11162 0x00000000, 0x000000ff },
11163 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
11164 0x00000000, 0xffffffff },
11165 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
11166 0x00000000, 0x000000ff },
11167 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
11168 0x00000000, 0xffffffff },
11169 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
11170 0x00000000, 0xffffffff },
11171 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
11172 0x00000000, 0xffffffff },
11173 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
11174 0x00000000, 0xffffffff },
11175 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
11176 0x00000000, 0xffffffff },
11177 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
11178 0xffffffff, 0x00000000 },
11179 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
11180 0xffffffff, 0x00000000 },
11181
11182 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070011183 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070011184 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070011185 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070011186 0x00000000, 0x007fffff },
11187 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
11188 0x00000000, 0x0000003f },
11189 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
11190 0x00000000, 0x000001ff },
11191 { BUFMGR_MB_HIGH_WATER, 0x0000,
11192 0x00000000, 0x000001ff },
11193 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
11194 0xffffffff, 0x00000000 },
11195 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
11196 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011197
Michael Chana71116d2005-05-29 14:58:11 -070011198 /* Mailbox Registers */
11199 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
11200 0x00000000, 0x000001ff },
11201 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
11202 0x00000000, 0x000001ff },
11203 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
11204 0x00000000, 0x000007ff },
11205 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
11206 0x00000000, 0x000001ff },
11207
11208 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
11209 };
11210
Michael Chanb16250e2006-09-27 16:10:14 -070011211 is_5705 = is_5750 = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000011212 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chana71116d2005-05-29 14:58:11 -070011213 is_5705 = 1;
Joe Perches63c3a662011-04-26 08:12:10 +000011214 if (tg3_flag(tp, 5750_PLUS))
Michael Chanb16250e2006-09-27 16:10:14 -070011215 is_5750 = 1;
11216 }
Michael Chana71116d2005-05-29 14:58:11 -070011217
11218 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
11219 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
11220 continue;
11221
11222 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
11223 continue;
11224
Joe Perches63c3a662011-04-26 08:12:10 +000011225 if (tg3_flag(tp, IS_5788) &&
Michael Chana71116d2005-05-29 14:58:11 -070011226 (reg_tbl[i].flags & TG3_FL_NOT_5788))
11227 continue;
11228
Michael Chanb16250e2006-09-27 16:10:14 -070011229 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
11230 continue;
11231
Michael Chana71116d2005-05-29 14:58:11 -070011232 offset = (u32) reg_tbl[i].offset;
11233 read_mask = reg_tbl[i].read_mask;
11234 write_mask = reg_tbl[i].write_mask;
11235
11236 /* Save the original register content */
11237 save_val = tr32(offset);
11238
11239 /* Determine the read-only value. */
11240 read_val = save_val & read_mask;
11241
11242 /* Write zero to the register, then make sure the read-only bits
11243 * are not changed and the read/write bits are all zeros.
11244 */
11245 tw32(offset, 0);
11246
11247 val = tr32(offset);
11248
11249 /* Test the read-only and read/write bits. */
11250 if (((val & read_mask) != read_val) || (val & write_mask))
11251 goto out;
11252
11253 /* Write ones to all the bits defined by RdMask and WrMask, then
11254 * make sure the read-only bits are not changed and the
11255 * read/write bits are all ones.
11256 */
11257 tw32(offset, read_mask | write_mask);
11258
11259 val = tr32(offset);
11260
11261 /* Test the read-only bits. */
11262 if ((val & read_mask) != read_val)
11263 goto out;
11264
11265 /* Test the read/write bits. */
11266 if ((val & write_mask) != write_mask)
11267 goto out;
11268
11269 tw32(offset, save_val);
11270 }
11271
11272 return 0;
11273
11274out:
Michael Chan9f88f292006-12-07 00:22:54 -080011275 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000011276 netdev_err(tp->dev,
11277 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070011278 tw32(offset, save_val);
11279 return -EIO;
11280}
11281
Michael Chan7942e1d2005-05-29 14:58:36 -070011282static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
11283{
Arjan van de Venf71e1302006-03-03 21:33:57 -050011284 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070011285 int i;
11286 u32 j;
11287
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020011288 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070011289 for (j = 0; j < len; j += 4) {
11290 u32 val;
11291
11292 tg3_write_mem(tp, offset + j, test_pattern[i]);
11293 tg3_read_mem(tp, offset + j, &val);
11294 if (val != test_pattern[i])
11295 return -EIO;
11296 }
11297 }
11298 return 0;
11299}
11300
11301static int tg3_test_memory(struct tg3 *tp)
11302{
11303 static struct mem_entry {
11304 u32 offset;
11305 u32 len;
11306 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080011307 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070011308 { 0x00002000, 0x1c000},
11309 { 0xffffffff, 0x00000}
11310 }, mem_tbl_5705[] = {
11311 { 0x00000100, 0x0000c},
11312 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070011313 { 0x00004000, 0x00800},
11314 { 0x00006000, 0x01000},
11315 { 0x00008000, 0x02000},
11316 { 0x00010000, 0x0e000},
11317 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080011318 }, mem_tbl_5755[] = {
11319 { 0x00000200, 0x00008},
11320 { 0x00004000, 0x00800},
11321 { 0x00006000, 0x00800},
11322 { 0x00008000, 0x02000},
11323 { 0x00010000, 0x0c000},
11324 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070011325 }, mem_tbl_5906[] = {
11326 { 0x00000200, 0x00008},
11327 { 0x00004000, 0x00400},
11328 { 0x00006000, 0x00400},
11329 { 0x00008000, 0x01000},
11330 { 0x00010000, 0x01000},
11331 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011332 }, mem_tbl_5717[] = {
11333 { 0x00000200, 0x00008},
11334 { 0x00010000, 0x0a000},
11335 { 0x00020000, 0x13c00},
11336 { 0xffffffff, 0x00000}
11337 }, mem_tbl_57765[] = {
11338 { 0x00000200, 0x00008},
11339 { 0x00004000, 0x00800},
11340 { 0x00006000, 0x09800},
11341 { 0x00010000, 0x0a000},
11342 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070011343 };
11344 struct mem_entry *mem_tbl;
11345 int err = 0;
11346 int i;
11347
Joe Perches63c3a662011-04-26 08:12:10 +000011348 if (tg3_flag(tp, 5717_PLUS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011349 mem_tbl = mem_tbl_5717;
11350 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
11351 mem_tbl = mem_tbl_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000011352 else if (tg3_flag(tp, 5755_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080011353 mem_tbl = mem_tbl_5755;
11354 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
11355 mem_tbl = mem_tbl_5906;
Joe Perches63c3a662011-04-26 08:12:10 +000011356 else if (tg3_flag(tp, 5705_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080011357 mem_tbl = mem_tbl_5705;
11358 else
Michael Chan7942e1d2005-05-29 14:58:36 -070011359 mem_tbl = mem_tbl_570x;
11360
11361 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000011362 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
11363 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070011364 break;
11365 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011366
Michael Chan7942e1d2005-05-29 14:58:36 -070011367 return err;
11368}
11369
Matt Carlsonbb158d62011-04-25 12:42:47 +000011370#define TG3_TSO_MSS 500
11371
11372#define TG3_TSO_IP_HDR_LEN 20
11373#define TG3_TSO_TCP_HDR_LEN 20
11374#define TG3_TSO_TCP_OPT_LEN 12
11375
11376static const u8 tg3_tso_header[] = {
113770x08, 0x00,
113780x45, 0x00, 0x00, 0x00,
113790x00, 0x00, 0x40, 0x00,
113800x40, 0x06, 0x00, 0x00,
113810x0a, 0x00, 0x00, 0x01,
113820x0a, 0x00, 0x00, 0x02,
113830x0d, 0x00, 0xe0, 0x00,
113840x00, 0x00, 0x01, 0x00,
113850x00, 0x00, 0x02, 0x00,
113860x80, 0x10, 0x10, 0x00,
113870x14, 0x09, 0x00, 0x00,
113880x01, 0x01, 0x08, 0x0a,
113890x11, 0x11, 0x11, 0x11,
113900x11, 0x11, 0x11, 0x11,
11391};
Michael Chan9f40dea2005-09-05 17:53:06 -070011392
Matt Carlson28a45952011-08-19 13:58:22 +000011393static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, bool tso_loopback)
Michael Chanc76949a2005-05-29 14:58:59 -070011394{
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011395 u32 rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011396 u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
Matt Carlson84b67b22011-07-27 14:20:52 +000011397 u32 budget;
Michael Chanc76949a2005-05-29 14:58:59 -070011398 struct sk_buff *skb, *rx_skb;
11399 u8 *tx_data;
11400 dma_addr_t map;
11401 int num_pkts, tx_len, rx_len, i, err;
11402 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000011403 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000011404 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070011405
Matt Carlsonc8873402010-02-12 14:47:11 +000011406 tnapi = &tp->napi[0];
11407 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011408 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +000011409 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlson1da85aa2010-09-30 10:34:34 +000011410 rnapi = &tp->napi[1];
Joe Perches63c3a662011-04-26 08:12:10 +000011411 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc8873402010-02-12 14:47:11 +000011412 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011413 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011414 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000011415
Michael Chanc76949a2005-05-29 14:58:59 -070011416 err = -EIO;
11417
Matt Carlson4852a862011-04-13 11:05:07 +000011418 tx_len = pktsz;
David S. Millera20e9c62006-07-31 22:38:16 -070011419 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070011420 if (!skb)
11421 return -ENOMEM;
11422
Michael Chanc76949a2005-05-29 14:58:59 -070011423 tx_data = skb_put(skb, tx_len);
11424 memcpy(tx_data, tp->dev->dev_addr, 6);
11425 memset(tx_data + 6, 0x0, 8);
11426
Matt Carlson4852a862011-04-13 11:05:07 +000011427 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
Michael Chanc76949a2005-05-29 14:58:59 -070011428
Matt Carlson28a45952011-08-19 13:58:22 +000011429 if (tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011430 struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
11431
11432 u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
11433 TG3_TSO_TCP_OPT_LEN;
11434
11435 memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
11436 sizeof(tg3_tso_header));
11437 mss = TG3_TSO_MSS;
11438
11439 val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
11440 num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
11441
11442 /* Set the total length field in the IP header */
11443 iph->tot_len = htons((u16)(mss + hdr_len));
11444
11445 base_flags = (TXD_FLAG_CPU_PRE_DMA |
11446 TXD_FLAG_CPU_POST_DMA);
11447
Joe Perches63c3a662011-04-26 08:12:10 +000011448 if (tg3_flag(tp, HW_TSO_1) ||
11449 tg3_flag(tp, HW_TSO_2) ||
11450 tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011451 struct tcphdr *th;
11452 val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
11453 th = (struct tcphdr *)&tx_data[val];
11454 th->check = 0;
11455 } else
11456 base_flags |= TXD_FLAG_TCPUDP_CSUM;
11457
Joe Perches63c3a662011-04-26 08:12:10 +000011458 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011459 mss |= (hdr_len & 0xc) << 12;
11460 if (hdr_len & 0x10)
11461 base_flags |= 0x00000010;
11462 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +000011463 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlsonbb158d62011-04-25 12:42:47 +000011464 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +000011465 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlsonbb158d62011-04-25 12:42:47 +000011466 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
11467 mss |= (TG3_TSO_TCP_OPT_LEN << 9);
11468 } else {
11469 base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
11470 }
11471
11472 data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
11473 } else {
11474 num_pkts = 1;
11475 data_off = ETH_HLEN;
11476 }
11477
11478 for (i = data_off; i < tx_len; i++)
Michael Chanc76949a2005-05-29 14:58:59 -070011479 tx_data[i] = (u8) (i & 0xff);
11480
Alexander Duyckf4188d82009-12-02 16:48:38 +000011481 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
11482 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000011483 dev_kfree_skb(skb);
11484 return -EIO;
11485 }
Michael Chanc76949a2005-05-29 14:58:59 -070011486
Matt Carlson0d681b22011-07-27 14:20:49 +000011487 val = tnapi->tx_prod;
11488 tnapi->tx_buffers[val].skb = skb;
11489 dma_unmap_addr_set(&tnapi->tx_buffers[val], mapping, map);
11490
Michael Chanc76949a2005-05-29 14:58:59 -070011491 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011492 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011493
11494 udelay(10);
11495
Matt Carlson898a56f2009-08-28 14:02:40 +000011496 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070011497
Matt Carlson84b67b22011-07-27 14:20:52 +000011498 budget = tg3_tx_avail(tnapi);
11499 if (tg3_tx_frag_set(tnapi, &val, &budget, map, tx_len,
Matt Carlsond1a3b732011-07-27 14:20:51 +000011500 base_flags | TXD_FLAG_END, mss, 0)) {
11501 tnapi->tx_buffers[val].skb = NULL;
11502 dev_kfree_skb(skb);
11503 return -EIO;
11504 }
Michael Chanc76949a2005-05-29 14:58:59 -070011505
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011506 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070011507
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011508 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
11509 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070011510
11511 udelay(10);
11512
Matt Carlson303fc922009-11-02 14:27:34 +000011513 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
11514 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070011515 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011516 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011517
11518 udelay(10);
11519
Matt Carlson898a56f2009-08-28 14:02:40 +000011520 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
11521 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011522 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070011523 (rx_idx == (rx_start_idx + num_pkts)))
11524 break;
11525 }
11526
Matt Carlson0d681b22011-07-27 14:20:49 +000011527 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod - 1, 0);
Michael Chanc76949a2005-05-29 14:58:59 -070011528 dev_kfree_skb(skb);
11529
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011530 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070011531 goto out;
11532
11533 if (rx_idx != rx_start_idx + num_pkts)
11534 goto out;
11535
Matt Carlsonbb158d62011-04-25 12:42:47 +000011536 val = data_off;
11537 while (rx_idx != rx_start_idx) {
11538 desc = &rnapi->rx_rcb[rx_start_idx++];
11539 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
11540 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
Michael Chanc76949a2005-05-29 14:58:59 -070011541
Matt Carlsonbb158d62011-04-25 12:42:47 +000011542 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
11543 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
Matt Carlson4852a862011-04-13 11:05:07 +000011544 goto out;
Michael Chanc76949a2005-05-29 14:58:59 -070011545
Matt Carlsonbb158d62011-04-25 12:42:47 +000011546 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
11547 - ETH_FCS_LEN;
11548
Matt Carlson28a45952011-08-19 13:58:22 +000011549 if (!tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011550 if (rx_len != tx_len)
11551 goto out;
11552
11553 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
11554 if (opaque_key != RXD_OPAQUE_RING_STD)
11555 goto out;
11556 } else {
11557 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
11558 goto out;
11559 }
11560 } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
11561 (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
Matt Carlson54e0a672011-05-19 12:12:50 +000011562 >> RXD_TCPCSUM_SHIFT != 0xffff) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011563 goto out;
11564 }
11565
11566 if (opaque_key == RXD_OPAQUE_RING_STD) {
11567 rx_skb = tpr->rx_std_buffers[desc_idx].skb;
11568 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
11569 mapping);
11570 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
11571 rx_skb = tpr->rx_jmb_buffers[desc_idx].skb;
11572 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
11573 mapping);
11574 } else
Matt Carlson4852a862011-04-13 11:05:07 +000011575 goto out;
11576
Matt Carlsonbb158d62011-04-25 12:42:47 +000011577 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
11578 PCI_DMA_FROMDEVICE);
11579
11580 for (i = data_off; i < rx_len; i++, val++) {
11581 if (*(rx_skb->data + i) != (u8) (val & 0xff))
11582 goto out;
11583 }
Matt Carlson4852a862011-04-13 11:05:07 +000011584 }
11585
Michael Chanc76949a2005-05-29 14:58:59 -070011586 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011587
Michael Chanc76949a2005-05-29 14:58:59 -070011588 /* tg3_free_rings will unmap and free the rx_skb */
11589out:
11590 return err;
11591}
11592
Matt Carlson00c266b2011-04-25 12:42:46 +000011593#define TG3_STD_LOOPBACK_FAILED 1
11594#define TG3_JMB_LOOPBACK_FAILED 2
Matt Carlsonbb158d62011-04-25 12:42:47 +000011595#define TG3_TSO_LOOPBACK_FAILED 4
Matt Carlson28a45952011-08-19 13:58:22 +000011596#define TG3_LOOPBACK_FAILED \
11597 (TG3_STD_LOOPBACK_FAILED | \
11598 TG3_JMB_LOOPBACK_FAILED | \
11599 TG3_TSO_LOOPBACK_FAILED)
Matt Carlson00c266b2011-04-25 12:42:46 +000011600
Matt Carlson941ec902011-08-19 13:58:23 +000011601static int tg3_test_loopback(struct tg3 *tp, u64 *data, bool do_extlpbk)
Michael Chan9f40dea2005-09-05 17:53:06 -070011602{
Matt Carlson28a45952011-08-19 13:58:22 +000011603 int err = -EIO;
Matt Carlson2215e242011-08-19 13:58:19 +000011604 u32 eee_cap;
Michael Chan9f40dea2005-09-05 17:53:06 -070011605
Matt Carlsonab789042011-01-25 15:58:54 +000011606 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
11607 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
11608
Matt Carlson28a45952011-08-19 13:58:22 +000011609 if (!netif_running(tp->dev)) {
11610 data[0] = TG3_LOOPBACK_FAILED;
11611 data[1] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000011612 if (do_extlpbk)
11613 data[2] = TG3_LOOPBACK_FAILED;
Matt Carlson28a45952011-08-19 13:58:22 +000011614 goto done;
11615 }
11616
Michael Chanb9ec6c12006-07-25 16:37:27 -070011617 err = tg3_reset_hw(tp, 1);
Matt Carlsonab789042011-01-25 15:58:54 +000011618 if (err) {
Matt Carlson28a45952011-08-19 13:58:22 +000011619 data[0] = TG3_LOOPBACK_FAILED;
11620 data[1] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000011621 if (do_extlpbk)
11622 data[2] = TG3_LOOPBACK_FAILED;
Matt Carlsonab789042011-01-25 15:58:54 +000011623 goto done;
11624 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011625
Joe Perches63c3a662011-04-26 08:12:10 +000011626 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson4a85f092011-04-20 07:57:37 +000011627 int i;
11628
11629 /* Reroute all rx packets to the 1st queue */
11630 for (i = MAC_RSS_INDIR_TBL_0;
11631 i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
11632 tw32(i, 0x0);
11633 }
11634
Matt Carlson6e01b202011-08-19 13:58:20 +000011635 /* HW errata - mac loopback fails in some cases on 5780.
11636 * Normal traffic and PHY loopback are not affected by
11637 * errata. Also, the MAC loopback test is deprecated for
11638 * all newer ASIC revisions.
11639 */
11640 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
11641 !tg3_flag(tp, CPMU_PRESENT)) {
11642 tg3_mac_loopback(tp, true);
Matt Carlson9936bcf2007-10-10 18:03:07 -070011643
Matt Carlson28a45952011-08-19 13:58:22 +000011644 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
11645 data[0] |= TG3_STD_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000011646
11647 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000011648 tg3_run_loopback(tp, 9000 + ETH_HLEN, false))
11649 data[0] |= TG3_JMB_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000011650
11651 tg3_mac_loopback(tp, false);
11652 }
Matt Carlson4852a862011-04-13 11:05:07 +000011653
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011654 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000011655 !tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011656 int i;
11657
Matt Carlson941ec902011-08-19 13:58:23 +000011658 tg3_phy_lpbk_set(tp, 0, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011659
11660 /* Wait for link */
11661 for (i = 0; i < 100; i++) {
11662 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
11663 break;
11664 mdelay(1);
11665 }
11666
Matt Carlson28a45952011-08-19 13:58:22 +000011667 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
11668 data[1] |= TG3_STD_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000011669 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000011670 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
11671 data[1] |= TG3_TSO_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000011672 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000011673 tg3_run_loopback(tp, 9000 + ETH_HLEN, false))
11674 data[1] |= TG3_JMB_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070011675
Matt Carlson941ec902011-08-19 13:58:23 +000011676 if (do_extlpbk) {
11677 tg3_phy_lpbk_set(tp, 0, true);
11678
11679 /* All link indications report up, but the hardware
11680 * isn't really ready for about 20 msec. Double it
11681 * to be sure.
11682 */
11683 mdelay(40);
11684
11685 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
11686 data[2] |= TG3_STD_LOOPBACK_FAILED;
11687 if (tg3_flag(tp, TSO_CAPABLE) &&
11688 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
11689 data[2] |= TG3_TSO_LOOPBACK_FAILED;
11690 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
11691 tg3_run_loopback(tp, 9000 + ETH_HLEN, false))
11692 data[2] |= TG3_JMB_LOOPBACK_FAILED;
11693 }
11694
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011695 /* Re-enable gphy autopowerdown. */
11696 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
11697 tg3_phy_toggle_apd(tp, true);
11698 }
Matt Carlson6833c042008-11-21 17:18:59 -080011699
Matt Carlson941ec902011-08-19 13:58:23 +000011700 err = (data[0] | data[1] | data[2]) ? -EIO : 0;
Matt Carlson28a45952011-08-19 13:58:22 +000011701
Matt Carlsonab789042011-01-25 15:58:54 +000011702done:
11703 tp->phy_flags |= eee_cap;
11704
Michael Chan9f40dea2005-09-05 17:53:06 -070011705 return err;
11706}
11707
Michael Chan4cafd3f2005-05-29 14:56:34 -070011708static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
11709 u64 *data)
11710{
Michael Chan566f86a2005-05-29 14:56:58 -070011711 struct tg3 *tp = netdev_priv(dev);
Matt Carlson941ec902011-08-19 13:58:23 +000011712 bool doextlpbk = etest->flags & ETH_TEST_FL_EXTERNAL_LB;
Michael Chan566f86a2005-05-29 14:56:58 -070011713
Matt Carlsonbed98292011-07-13 09:27:29 +000011714 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
11715 tg3_power_up(tp)) {
11716 etest->flags |= ETH_TEST_FL_FAILED;
11717 memset(data, 1, sizeof(u64) * TG3_NUM_TEST);
11718 return;
11719 }
Michael Chanbc1c7562006-03-20 17:48:03 -080011720
Michael Chan566f86a2005-05-29 14:56:58 -070011721 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
11722
11723 if (tg3_test_nvram(tp) != 0) {
11724 etest->flags |= ETH_TEST_FL_FAILED;
11725 data[0] = 1;
11726 }
Matt Carlson941ec902011-08-19 13:58:23 +000011727 if (!doextlpbk && tg3_test_link(tp)) {
Michael Chanca430072005-05-29 14:57:23 -070011728 etest->flags |= ETH_TEST_FL_FAILED;
11729 data[1] = 1;
11730 }
Michael Chana71116d2005-05-29 14:58:11 -070011731 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011732 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070011733
Michael Chanbbe832c2005-06-24 20:20:04 -070011734 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011735 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070011736 tg3_netif_stop(tp);
11737 irq_sync = 1;
11738 }
11739
11740 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070011741
11742 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080011743 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011744 tg3_halt_cpu(tp, RX_CPU_BASE);
Joe Perches63c3a662011-04-26 08:12:10 +000011745 if (!tg3_flag(tp, 5705_PLUS))
Michael Chana71116d2005-05-29 14:58:11 -070011746 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080011747 if (!err)
11748 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011749
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011750 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad2006-03-20 22:27:35 -080011751 tg3_phy_reset(tp);
11752
Michael Chana71116d2005-05-29 14:58:11 -070011753 if (tg3_test_registers(tp) != 0) {
11754 etest->flags |= ETH_TEST_FL_FAILED;
11755 data[2] = 1;
11756 }
Matt Carlson28a45952011-08-19 13:58:22 +000011757
Michael Chan7942e1d2005-05-29 14:58:36 -070011758 if (tg3_test_memory(tp) != 0) {
11759 etest->flags |= ETH_TEST_FL_FAILED;
11760 data[3] = 1;
11761 }
Matt Carlson28a45952011-08-19 13:58:22 +000011762
Matt Carlson941ec902011-08-19 13:58:23 +000011763 if (doextlpbk)
11764 etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
11765
11766 if (tg3_test_loopback(tp, &data[4], doextlpbk))
Michael Chanc76949a2005-05-29 14:58:59 -070011767 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070011768
David S. Millerf47c11e2005-06-24 20:18:35 -070011769 tg3_full_unlock(tp);
11770
Michael Chand4bc3922005-05-29 14:59:20 -070011771 if (tg3_test_interrupt(tp) != 0) {
11772 etest->flags |= ETH_TEST_FL_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000011773 data[7] = 1;
Michael Chand4bc3922005-05-29 14:59:20 -070011774 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011775
11776 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070011777
Michael Chana71116d2005-05-29 14:58:11 -070011778 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
11779 if (netif_running(dev)) {
Joe Perches63c3a662011-04-26 08:12:10 +000011780 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011781 err2 = tg3_restart_hw(tp, 1);
11782 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070011783 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011784 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011785
11786 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011787
11788 if (irq_sync && !err2)
11789 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011790 }
Matt Carlson80096062010-08-02 11:26:06 +000011791 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000011792 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080011793
Michael Chan4cafd3f2005-05-29 14:56:34 -070011794}
11795
Linus Torvalds1da177e2005-04-16 15:20:36 -070011796static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
11797{
11798 struct mii_ioctl_data *data = if_mii(ifr);
11799 struct tg3 *tp = netdev_priv(dev);
11800 int err;
11801
Joe Perches63c3a662011-04-26 08:12:10 +000011802 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011803 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011804 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011805 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011806 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000011807 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011808 }
11809
Matt Carlson33f401a2010-04-05 10:19:27 +000011810 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011811 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000011812 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011813
11814 /* fallthru */
11815 case SIOCGMIIREG: {
11816 u32 mii_regval;
11817
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011818 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011819 break; /* We have no PHY */
11820
Matt Carlson34eea5a2011-04-20 07:57:38 +000011821 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080011822 return -EAGAIN;
11823
David S. Millerf47c11e2005-06-24 20:18:35 -070011824 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011825 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070011826 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011827
11828 data->val_out = mii_regval;
11829
11830 return err;
11831 }
11832
11833 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011834 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011835 break; /* We have no PHY */
11836
Matt Carlson34eea5a2011-04-20 07:57:38 +000011837 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080011838 return -EAGAIN;
11839
David S. Millerf47c11e2005-06-24 20:18:35 -070011840 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011841 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070011842 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011843
11844 return err;
11845
11846 default:
11847 /* do nothing */
11848 break;
11849 }
11850 return -EOPNOTSUPP;
11851}
11852
David S. Miller15f98502005-05-18 22:49:26 -070011853static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11854{
11855 struct tg3 *tp = netdev_priv(dev);
11856
11857 memcpy(ec, &tp->coal, sizeof(*ec));
11858 return 0;
11859}
11860
Michael Chand244c892005-07-05 14:42:33 -070011861static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11862{
11863 struct tg3 *tp = netdev_priv(dev);
11864 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
11865 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
11866
Joe Perches63c3a662011-04-26 08:12:10 +000011867 if (!tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070011868 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
11869 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
11870 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
11871 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
11872 }
11873
11874 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
11875 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
11876 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
11877 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
11878 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
11879 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
11880 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
11881 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
11882 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
11883 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
11884 return -EINVAL;
11885
11886 /* No rx interrupts will be generated if both are zero */
11887 if ((ec->rx_coalesce_usecs == 0) &&
11888 (ec->rx_max_coalesced_frames == 0))
11889 return -EINVAL;
11890
11891 /* No tx interrupts will be generated if both are zero */
11892 if ((ec->tx_coalesce_usecs == 0) &&
11893 (ec->tx_max_coalesced_frames == 0))
11894 return -EINVAL;
11895
11896 /* Only copy relevant parameters, ignore all others. */
11897 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
11898 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
11899 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
11900 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
11901 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
11902 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
11903 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
11904 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
11905 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
11906
11907 if (netif_running(dev)) {
11908 tg3_full_lock(tp, 0);
11909 __tg3_set_coalesce(tp, &tp->coal);
11910 tg3_full_unlock(tp);
11911 }
11912 return 0;
11913}
11914
Jeff Garzik7282d492006-09-13 14:30:00 -040011915static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011916 .get_settings = tg3_get_settings,
11917 .set_settings = tg3_set_settings,
11918 .get_drvinfo = tg3_get_drvinfo,
11919 .get_regs_len = tg3_get_regs_len,
11920 .get_regs = tg3_get_regs,
11921 .get_wol = tg3_get_wol,
11922 .set_wol = tg3_set_wol,
11923 .get_msglevel = tg3_get_msglevel,
11924 .set_msglevel = tg3_set_msglevel,
11925 .nway_reset = tg3_nway_reset,
11926 .get_link = ethtool_op_get_link,
11927 .get_eeprom_len = tg3_get_eeprom_len,
11928 .get_eeprom = tg3_get_eeprom,
11929 .set_eeprom = tg3_set_eeprom,
11930 .get_ringparam = tg3_get_ringparam,
11931 .set_ringparam = tg3_set_ringparam,
11932 .get_pauseparam = tg3_get_pauseparam,
11933 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070011934 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011935 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000011936 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011937 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070011938 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070011939 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070011940 .get_sset_count = tg3_get_sset_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011941};
11942
11943static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
11944{
Michael Chan1b277772006-03-20 22:27:48 -080011945 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011946
11947 tp->nvram_size = EEPROM_CHIP_SIZE;
11948
Matt Carlsone4f34112009-02-25 14:25:00 +000011949 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011950 return;
11951
Michael Chanb16250e2006-09-27 16:10:14 -070011952 if ((magic != TG3_EEPROM_MAGIC) &&
11953 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
11954 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011955 return;
11956
11957 /*
11958 * Size the chip by reading offsets at increasing powers of two.
11959 * When we encounter our validation signature, we know the addressing
11960 * has wrapped around, and thus have our chip size.
11961 */
Michael Chan1b277772006-03-20 22:27:48 -080011962 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011963
11964 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000011965 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011966 return;
11967
Michael Chan18201802006-03-20 22:29:15 -080011968 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011969 break;
11970
11971 cursize <<= 1;
11972 }
11973
11974 tp->nvram_size = cursize;
11975}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011976
Linus Torvalds1da177e2005-04-16 15:20:36 -070011977static void __devinit tg3_get_nvram_size(struct tg3 *tp)
11978{
11979 u32 val;
11980
Joe Perches63c3a662011-04-26 08:12:10 +000011981 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080011982 return;
11983
11984 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080011985 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080011986 tg3_get_eeprom_size(tp);
11987 return;
11988 }
11989
Matt Carlson6d348f22009-02-25 14:25:52 +000011990 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011991 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000011992 /* This is confusing. We want to operate on the
11993 * 16-bit value at offset 0xf2. The tg3_nvram_read()
11994 * call will read from NVRAM and byteswap the data
11995 * according to the byteswapping settings for all
11996 * other register accesses. This ensures the data we
11997 * want will always reside in the lower 16-bits.
11998 * However, the data in NVRAM is in LE format, which
11999 * means the data from the NVRAM read will always be
12000 * opposite the endianness of the CPU. The 16-bit
12001 * byteswap then brings the data to CPU endianness.
12002 */
12003 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012004 return;
12005 }
12006 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070012007 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012008}
12009
12010static void __devinit tg3_get_nvram_info(struct tg3 *tp)
12011{
12012 u32 nvcfg1;
12013
12014 nvcfg1 = tr32(NVRAM_CFG1);
12015 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
Joe Perches63c3a662011-04-26 08:12:10 +000012016 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012017 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012018 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12019 tw32(NVRAM_CFG1, nvcfg1);
12020 }
12021
Matt Carlson6ff6f812011-05-19 12:12:54 +000012022 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
Joe Perches63c3a662011-04-26 08:12:10 +000012023 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012024 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012025 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
12026 tp->nvram_jedecnum = JEDEC_ATMEL;
12027 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012028 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012029 break;
12030 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
12031 tp->nvram_jedecnum = JEDEC_ATMEL;
12032 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
12033 break;
12034 case FLASH_VENDOR_ATMEL_EEPROM:
12035 tp->nvram_jedecnum = JEDEC_ATMEL;
12036 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012037 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012038 break;
12039 case FLASH_VENDOR_ST:
12040 tp->nvram_jedecnum = JEDEC_ST;
12041 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012042 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012043 break;
12044 case FLASH_VENDOR_SAIFUN:
12045 tp->nvram_jedecnum = JEDEC_SAIFUN;
12046 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
12047 break;
12048 case FLASH_VENDOR_SST_SMALL:
12049 case FLASH_VENDOR_SST_LARGE:
12050 tp->nvram_jedecnum = JEDEC_SST;
12051 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
12052 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012053 }
Matt Carlson8590a602009-08-28 12:29:16 +000012054 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012055 tp->nvram_jedecnum = JEDEC_ATMEL;
12056 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012057 tg3_flag_set(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012058 }
12059}
12060
Matt Carlsona1b950d2009-09-01 13:20:17 +000012061static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
12062{
12063 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
12064 case FLASH_5752PAGE_SIZE_256:
12065 tp->nvram_pagesize = 256;
12066 break;
12067 case FLASH_5752PAGE_SIZE_512:
12068 tp->nvram_pagesize = 512;
12069 break;
12070 case FLASH_5752PAGE_SIZE_1K:
12071 tp->nvram_pagesize = 1024;
12072 break;
12073 case FLASH_5752PAGE_SIZE_2K:
12074 tp->nvram_pagesize = 2048;
12075 break;
12076 case FLASH_5752PAGE_SIZE_4K:
12077 tp->nvram_pagesize = 4096;
12078 break;
12079 case FLASH_5752PAGE_SIZE_264:
12080 tp->nvram_pagesize = 264;
12081 break;
12082 case FLASH_5752PAGE_SIZE_528:
12083 tp->nvram_pagesize = 528;
12084 break;
12085 }
12086}
12087
Michael Chan361b4ac2005-04-21 17:11:21 -070012088static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
12089{
12090 u32 nvcfg1;
12091
12092 nvcfg1 = tr32(NVRAM_CFG1);
12093
Michael Chane6af3012005-04-21 17:12:05 -070012094 /* NVRAM protection for TPM */
12095 if (nvcfg1 & (1 << 27))
Joe Perches63c3a662011-04-26 08:12:10 +000012096 tg3_flag_set(tp, PROTECTED_NVRAM);
Michael Chane6af3012005-04-21 17:12:05 -070012097
Michael Chan361b4ac2005-04-21 17:11:21 -070012098 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012099 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
12100 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
12101 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012102 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012103 break;
12104 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12105 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012106 tg3_flag_set(tp, NVRAM_BUFFERED);
12107 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012108 break;
12109 case FLASH_5752VENDOR_ST_M45PE10:
12110 case FLASH_5752VENDOR_ST_M45PE20:
12111 case FLASH_5752VENDOR_ST_M45PE40:
12112 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012113 tg3_flag_set(tp, NVRAM_BUFFERED);
12114 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012115 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070012116 }
12117
Joe Perches63c3a662011-04-26 08:12:10 +000012118 if (tg3_flag(tp, FLASH)) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000012119 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000012120 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070012121 /* For eeprom, set pagesize to maximum eeprom size */
12122 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12123
12124 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12125 tw32(NVRAM_CFG1, nvcfg1);
12126 }
12127}
12128
Michael Chand3c7b882006-03-23 01:28:25 -080012129static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
12130{
Matt Carlson989a9d22007-05-05 11:51:05 -070012131 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080012132
12133 nvcfg1 = tr32(NVRAM_CFG1);
12134
12135 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070012136 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012137 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson989a9d22007-05-05 11:51:05 -070012138 protect = 1;
12139 }
Michael Chand3c7b882006-03-23 01:28:25 -080012140
Matt Carlson989a9d22007-05-05 11:51:05 -070012141 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
12142 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012143 case FLASH_5755VENDOR_ATMEL_FLASH_1:
12144 case FLASH_5755VENDOR_ATMEL_FLASH_2:
12145 case FLASH_5755VENDOR_ATMEL_FLASH_3:
12146 case FLASH_5755VENDOR_ATMEL_FLASH_5:
12147 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012148 tg3_flag_set(tp, NVRAM_BUFFERED);
12149 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012150 tp->nvram_pagesize = 264;
12151 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
12152 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
12153 tp->nvram_size = (protect ? 0x3e200 :
12154 TG3_NVRAM_SIZE_512KB);
12155 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
12156 tp->nvram_size = (protect ? 0x1f200 :
12157 TG3_NVRAM_SIZE_256KB);
12158 else
12159 tp->nvram_size = (protect ? 0x1f200 :
12160 TG3_NVRAM_SIZE_128KB);
12161 break;
12162 case FLASH_5752VENDOR_ST_M45PE10:
12163 case FLASH_5752VENDOR_ST_M45PE20:
12164 case FLASH_5752VENDOR_ST_M45PE40:
12165 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012166 tg3_flag_set(tp, NVRAM_BUFFERED);
12167 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012168 tp->nvram_pagesize = 256;
12169 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
12170 tp->nvram_size = (protect ?
12171 TG3_NVRAM_SIZE_64KB :
12172 TG3_NVRAM_SIZE_128KB);
12173 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
12174 tp->nvram_size = (protect ?
12175 TG3_NVRAM_SIZE_64KB :
12176 TG3_NVRAM_SIZE_256KB);
12177 else
12178 tp->nvram_size = (protect ?
12179 TG3_NVRAM_SIZE_128KB :
12180 TG3_NVRAM_SIZE_512KB);
12181 break;
Michael Chand3c7b882006-03-23 01:28:25 -080012182 }
12183}
12184
Michael Chan1b277772006-03-20 22:27:48 -080012185static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
12186{
12187 u32 nvcfg1;
12188
12189 nvcfg1 = tr32(NVRAM_CFG1);
12190
12191 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012192 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
12193 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
12194 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
12195 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
12196 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012197 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012198 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080012199
Matt Carlson8590a602009-08-28 12:29:16 +000012200 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12201 tw32(NVRAM_CFG1, nvcfg1);
12202 break;
12203 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12204 case FLASH_5755VENDOR_ATMEL_FLASH_1:
12205 case FLASH_5755VENDOR_ATMEL_FLASH_2:
12206 case FLASH_5755VENDOR_ATMEL_FLASH_3:
12207 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012208 tg3_flag_set(tp, NVRAM_BUFFERED);
12209 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012210 tp->nvram_pagesize = 264;
12211 break;
12212 case FLASH_5752VENDOR_ST_M45PE10:
12213 case FLASH_5752VENDOR_ST_M45PE20:
12214 case FLASH_5752VENDOR_ST_M45PE40:
12215 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012216 tg3_flag_set(tp, NVRAM_BUFFERED);
12217 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012218 tp->nvram_pagesize = 256;
12219 break;
Michael Chan1b277772006-03-20 22:27:48 -080012220 }
12221}
12222
Matt Carlson6b91fa02007-10-10 18:01:09 -070012223static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
12224{
12225 u32 nvcfg1, protect = 0;
12226
12227 nvcfg1 = tr32(NVRAM_CFG1);
12228
12229 /* NVRAM protection for TPM */
12230 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012231 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson6b91fa02007-10-10 18:01:09 -070012232 protect = 1;
12233 }
12234
12235 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
12236 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012237 case FLASH_5761VENDOR_ATMEL_ADB021D:
12238 case FLASH_5761VENDOR_ATMEL_ADB041D:
12239 case FLASH_5761VENDOR_ATMEL_ADB081D:
12240 case FLASH_5761VENDOR_ATMEL_ADB161D:
12241 case FLASH_5761VENDOR_ATMEL_MDB021D:
12242 case FLASH_5761VENDOR_ATMEL_MDB041D:
12243 case FLASH_5761VENDOR_ATMEL_MDB081D:
12244 case FLASH_5761VENDOR_ATMEL_MDB161D:
12245 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012246 tg3_flag_set(tp, NVRAM_BUFFERED);
12247 tg3_flag_set(tp, FLASH);
12248 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson8590a602009-08-28 12:29:16 +000012249 tp->nvram_pagesize = 256;
12250 break;
12251 case FLASH_5761VENDOR_ST_A_M45PE20:
12252 case FLASH_5761VENDOR_ST_A_M45PE40:
12253 case FLASH_5761VENDOR_ST_A_M45PE80:
12254 case FLASH_5761VENDOR_ST_A_M45PE16:
12255 case FLASH_5761VENDOR_ST_M_M45PE20:
12256 case FLASH_5761VENDOR_ST_M_M45PE40:
12257 case FLASH_5761VENDOR_ST_M_M45PE80:
12258 case FLASH_5761VENDOR_ST_M_M45PE16:
12259 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012260 tg3_flag_set(tp, NVRAM_BUFFERED);
12261 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012262 tp->nvram_pagesize = 256;
12263 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070012264 }
12265
12266 if (protect) {
12267 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
12268 } else {
12269 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012270 case FLASH_5761VENDOR_ATMEL_ADB161D:
12271 case FLASH_5761VENDOR_ATMEL_MDB161D:
12272 case FLASH_5761VENDOR_ST_A_M45PE16:
12273 case FLASH_5761VENDOR_ST_M_M45PE16:
12274 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
12275 break;
12276 case FLASH_5761VENDOR_ATMEL_ADB081D:
12277 case FLASH_5761VENDOR_ATMEL_MDB081D:
12278 case FLASH_5761VENDOR_ST_A_M45PE80:
12279 case FLASH_5761VENDOR_ST_M_M45PE80:
12280 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12281 break;
12282 case FLASH_5761VENDOR_ATMEL_ADB041D:
12283 case FLASH_5761VENDOR_ATMEL_MDB041D:
12284 case FLASH_5761VENDOR_ST_A_M45PE40:
12285 case FLASH_5761VENDOR_ST_M_M45PE40:
12286 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12287 break;
12288 case FLASH_5761VENDOR_ATMEL_ADB021D:
12289 case FLASH_5761VENDOR_ATMEL_MDB021D:
12290 case FLASH_5761VENDOR_ST_A_M45PE20:
12291 case FLASH_5761VENDOR_ST_M_M45PE20:
12292 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12293 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070012294 }
12295 }
12296}
12297
Michael Chanb5d37722006-09-27 16:06:21 -070012298static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
12299{
12300 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012301 tg3_flag_set(tp, NVRAM_BUFFERED);
Michael Chanb5d37722006-09-27 16:06:21 -070012302 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12303}
12304
Matt Carlson321d32a2008-11-21 17:22:19 -080012305static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
12306{
12307 u32 nvcfg1;
12308
12309 nvcfg1 = tr32(NVRAM_CFG1);
12310
12311 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12312 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
12313 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
12314 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012315 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson321d32a2008-11-21 17:22:19 -080012316 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12317
12318 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12319 tw32(NVRAM_CFG1, nvcfg1);
12320 return;
12321 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12322 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
12323 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
12324 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
12325 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
12326 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
12327 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
12328 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012329 tg3_flag_set(tp, NVRAM_BUFFERED);
12330 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080012331
12332 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12333 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12334 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
12335 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
12336 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12337 break;
12338 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
12339 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
12340 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12341 break;
12342 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
12343 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
12344 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12345 break;
12346 }
12347 break;
12348 case FLASH_5752VENDOR_ST_M45PE10:
12349 case FLASH_5752VENDOR_ST_M45PE20:
12350 case FLASH_5752VENDOR_ST_M45PE40:
12351 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012352 tg3_flag_set(tp, NVRAM_BUFFERED);
12353 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080012354
12355 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12356 case FLASH_5752VENDOR_ST_M45PE10:
12357 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12358 break;
12359 case FLASH_5752VENDOR_ST_M45PE20:
12360 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12361 break;
12362 case FLASH_5752VENDOR_ST_M45PE40:
12363 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12364 break;
12365 }
12366 break;
12367 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012368 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson321d32a2008-11-21 17:22:19 -080012369 return;
12370 }
12371
Matt Carlsona1b950d2009-09-01 13:20:17 +000012372 tg3_nvram_get_pagesize(tp, nvcfg1);
12373 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012374 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012375}
12376
12377
12378static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
12379{
12380 u32 nvcfg1;
12381
12382 nvcfg1 = tr32(NVRAM_CFG1);
12383
12384 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12385 case FLASH_5717VENDOR_ATMEL_EEPROM:
12386 case FLASH_5717VENDOR_MICRO_EEPROM:
12387 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012388 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012389 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12390
12391 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12392 tw32(NVRAM_CFG1, nvcfg1);
12393 return;
12394 case FLASH_5717VENDOR_ATMEL_MDB011D:
12395 case FLASH_5717VENDOR_ATMEL_ADB011B:
12396 case FLASH_5717VENDOR_ATMEL_ADB011D:
12397 case FLASH_5717VENDOR_ATMEL_MDB021D:
12398 case FLASH_5717VENDOR_ATMEL_ADB021B:
12399 case FLASH_5717VENDOR_ATMEL_ADB021D:
12400 case FLASH_5717VENDOR_ATMEL_45USPT:
12401 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012402 tg3_flag_set(tp, NVRAM_BUFFERED);
12403 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012404
12405 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12406 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012407 /* Detect size with tg3_nvram_get_size() */
12408 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012409 case FLASH_5717VENDOR_ATMEL_ADB021B:
12410 case FLASH_5717VENDOR_ATMEL_ADB021D:
12411 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12412 break;
12413 default:
12414 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12415 break;
12416 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012417 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012418 case FLASH_5717VENDOR_ST_M_M25PE10:
12419 case FLASH_5717VENDOR_ST_A_M25PE10:
12420 case FLASH_5717VENDOR_ST_M_M45PE10:
12421 case FLASH_5717VENDOR_ST_A_M45PE10:
12422 case FLASH_5717VENDOR_ST_M_M25PE20:
12423 case FLASH_5717VENDOR_ST_A_M25PE20:
12424 case FLASH_5717VENDOR_ST_M_M45PE20:
12425 case FLASH_5717VENDOR_ST_A_M45PE20:
12426 case FLASH_5717VENDOR_ST_25USPT:
12427 case FLASH_5717VENDOR_ST_45USPT:
12428 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012429 tg3_flag_set(tp, NVRAM_BUFFERED);
12430 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012431
12432 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12433 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012434 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012435 /* Detect size with tg3_nvram_get_size() */
12436 break;
12437 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012438 case FLASH_5717VENDOR_ST_A_M45PE20:
12439 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12440 break;
12441 default:
12442 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12443 break;
12444 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012445 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012446 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012447 tg3_flag_set(tp, NO_NVRAM);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012448 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080012449 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000012450
12451 tg3_nvram_get_pagesize(tp, nvcfg1);
12452 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012453 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson321d32a2008-11-21 17:22:19 -080012454}
12455
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012456static void __devinit tg3_get_5720_nvram_info(struct tg3 *tp)
12457{
12458 u32 nvcfg1, nvmpinstrp;
12459
12460 nvcfg1 = tr32(NVRAM_CFG1);
12461 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
12462
12463 switch (nvmpinstrp) {
12464 case FLASH_5720_EEPROM_HD:
12465 case FLASH_5720_EEPROM_LD:
12466 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012467 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012468
12469 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12470 tw32(NVRAM_CFG1, nvcfg1);
12471 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
12472 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12473 else
12474 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
12475 return;
12476 case FLASH_5720VENDOR_M_ATMEL_DB011D:
12477 case FLASH_5720VENDOR_A_ATMEL_DB011B:
12478 case FLASH_5720VENDOR_A_ATMEL_DB011D:
12479 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12480 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12481 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12482 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12483 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12484 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12485 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12486 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12487 case FLASH_5720VENDOR_ATMEL_45USPT:
12488 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012489 tg3_flag_set(tp, NVRAM_BUFFERED);
12490 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012491
12492 switch (nvmpinstrp) {
12493 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12494 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12495 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12496 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12497 break;
12498 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12499 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12500 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12501 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12502 break;
12503 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12504 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12505 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12506 break;
12507 default:
12508 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12509 break;
12510 }
12511 break;
12512 case FLASH_5720VENDOR_M_ST_M25PE10:
12513 case FLASH_5720VENDOR_M_ST_M45PE10:
12514 case FLASH_5720VENDOR_A_ST_M25PE10:
12515 case FLASH_5720VENDOR_A_ST_M45PE10:
12516 case FLASH_5720VENDOR_M_ST_M25PE20:
12517 case FLASH_5720VENDOR_M_ST_M45PE20:
12518 case FLASH_5720VENDOR_A_ST_M25PE20:
12519 case FLASH_5720VENDOR_A_ST_M45PE20:
12520 case FLASH_5720VENDOR_M_ST_M25PE40:
12521 case FLASH_5720VENDOR_M_ST_M45PE40:
12522 case FLASH_5720VENDOR_A_ST_M25PE40:
12523 case FLASH_5720VENDOR_A_ST_M45PE40:
12524 case FLASH_5720VENDOR_M_ST_M25PE80:
12525 case FLASH_5720VENDOR_M_ST_M45PE80:
12526 case FLASH_5720VENDOR_A_ST_M25PE80:
12527 case FLASH_5720VENDOR_A_ST_M45PE80:
12528 case FLASH_5720VENDOR_ST_25USPT:
12529 case FLASH_5720VENDOR_ST_45USPT:
12530 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012531 tg3_flag_set(tp, NVRAM_BUFFERED);
12532 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012533
12534 switch (nvmpinstrp) {
12535 case FLASH_5720VENDOR_M_ST_M25PE20:
12536 case FLASH_5720VENDOR_M_ST_M45PE20:
12537 case FLASH_5720VENDOR_A_ST_M25PE20:
12538 case FLASH_5720VENDOR_A_ST_M45PE20:
12539 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12540 break;
12541 case FLASH_5720VENDOR_M_ST_M25PE40:
12542 case FLASH_5720VENDOR_M_ST_M45PE40:
12543 case FLASH_5720VENDOR_A_ST_M25PE40:
12544 case FLASH_5720VENDOR_A_ST_M45PE40:
12545 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12546 break;
12547 case FLASH_5720VENDOR_M_ST_M25PE80:
12548 case FLASH_5720VENDOR_M_ST_M45PE80:
12549 case FLASH_5720VENDOR_A_ST_M25PE80:
12550 case FLASH_5720VENDOR_A_ST_M45PE80:
12551 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12552 break;
12553 default:
12554 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12555 break;
12556 }
12557 break;
12558 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012559 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012560 return;
12561 }
12562
12563 tg3_nvram_get_pagesize(tp, nvcfg1);
12564 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012565 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012566}
12567
Linus Torvalds1da177e2005-04-16 15:20:36 -070012568/* Chips other than 5700/5701 use the NVRAM for fetching info. */
12569static void __devinit tg3_nvram_init(struct tg3 *tp)
12570{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012571 tw32_f(GRC_EEPROM_ADDR,
12572 (EEPROM_ADDR_FSM_RESET |
12573 (EEPROM_DEFAULT_CLOCK_PERIOD <<
12574 EEPROM_ADDR_CLKPERD_SHIFT)));
12575
Michael Chan9d57f012006-12-07 00:23:25 -080012576 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012577
12578 /* Enable seeprom accesses. */
12579 tw32_f(GRC_LOCAL_CTRL,
12580 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
12581 udelay(100);
12582
12583 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
12584 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
Joe Perches63c3a662011-04-26 08:12:10 +000012585 tg3_flag_set(tp, NVRAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012586
Michael Chanec41c7d2006-01-17 02:40:55 -080012587 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000012588 netdev_warn(tp->dev,
12589 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000012590 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080012591 return;
12592 }
Michael Chane6af3012005-04-21 17:12:05 -070012593 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012594
Matt Carlson989a9d22007-05-05 11:51:05 -070012595 tp->nvram_size = 0;
12596
Michael Chan361b4ac2005-04-21 17:11:21 -070012597 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
12598 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080012599 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
12600 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070012601 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070012602 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
12603 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080012604 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070012605 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
12606 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070012607 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12608 tg3_get_5906_nvram_info(tp);
Matt Carlsonb703df62009-12-03 08:36:21 +000012609 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
12610 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson321d32a2008-11-21 17:22:19 -080012611 tg3_get_57780_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012612 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
12613 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000012614 tg3_get_5717_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012615 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
12616 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070012617 else
12618 tg3_get_nvram_info(tp);
12619
Matt Carlson989a9d22007-05-05 11:51:05 -070012620 if (tp->nvram_size == 0)
12621 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012622
Michael Chane6af3012005-04-21 17:12:05 -070012623 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080012624 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012625
12626 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000012627 tg3_flag_clear(tp, NVRAM);
12628 tg3_flag_clear(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012629
12630 tg3_get_eeprom_size(tp);
12631 }
12632}
12633
Linus Torvalds1da177e2005-04-16 15:20:36 -070012634static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
12635 u32 offset, u32 len, u8 *buf)
12636{
12637 int i, j, rc = 0;
12638 u32 val;
12639
12640 for (i = 0; i < len; i += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012641 u32 addr;
Matt Carlsona9dc5292009-02-25 14:25:30 +000012642 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012643
12644 addr = offset + i;
12645
12646 memcpy(&data, buf + i, 4);
12647
Matt Carlson62cedd12009-04-20 14:52:29 -070012648 /*
12649 * The SEEPROM interface expects the data to always be opposite
12650 * the native endian format. We accomplish this by reversing
12651 * all the operations that would have been performed on the
12652 * data from a call to tg3_nvram_read_be32().
12653 */
12654 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012655
12656 val = tr32(GRC_EEPROM_ADDR);
12657 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
12658
12659 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
12660 EEPROM_ADDR_READ);
12661 tw32(GRC_EEPROM_ADDR, val |
12662 (0 << EEPROM_ADDR_DEVID_SHIFT) |
12663 (addr & EEPROM_ADDR_ADDR_MASK) |
12664 EEPROM_ADDR_START |
12665 EEPROM_ADDR_WRITE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012666
Michael Chan9d57f012006-12-07 00:23:25 -080012667 for (j = 0; j < 1000; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012668 val = tr32(GRC_EEPROM_ADDR);
12669
12670 if (val & EEPROM_ADDR_COMPLETE)
12671 break;
Michael Chan9d57f012006-12-07 00:23:25 -080012672 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012673 }
12674 if (!(val & EEPROM_ADDR_COMPLETE)) {
12675 rc = -EBUSY;
12676 break;
12677 }
12678 }
12679
12680 return rc;
12681}
12682
12683/* offset and length are dword aligned */
12684static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
12685 u8 *buf)
12686{
12687 int ret = 0;
12688 u32 pagesize = tp->nvram_pagesize;
12689 u32 pagemask = pagesize - 1;
12690 u32 nvram_cmd;
12691 u8 *tmp;
12692
12693 tmp = kmalloc(pagesize, GFP_KERNEL);
12694 if (tmp == NULL)
12695 return -ENOMEM;
12696
12697 while (len) {
12698 int j;
Michael Chane6af3012005-04-21 17:12:05 -070012699 u32 phy_addr, page_off, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012700
12701 phy_addr = offset & ~pagemask;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012702
Linus Torvalds1da177e2005-04-16 15:20:36 -070012703 for (j = 0; j < pagesize; j += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012704 ret = tg3_nvram_read_be32(tp, phy_addr + j,
12705 (__be32 *) (tmp + j));
12706 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012707 break;
12708 }
12709 if (ret)
12710 break;
12711
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012712 page_off = offset & pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012713 size = pagesize;
12714 if (len < size)
12715 size = len;
12716
12717 len -= size;
12718
12719 memcpy(tmp + page_off, buf, size);
12720
12721 offset = offset + (pagesize - page_off);
12722
Michael Chane6af3012005-04-21 17:12:05 -070012723 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012724
12725 /*
12726 * Before we can erase the flash page, we need
12727 * to issue a special "write enable" command.
12728 */
12729 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12730
12731 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12732 break;
12733
12734 /* Erase the target page */
12735 tw32(NVRAM_ADDR, phy_addr);
12736
12737 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
12738 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
12739
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012740 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012741 break;
12742
12743 /* Issue another write enable to start the write. */
12744 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12745
12746 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12747 break;
12748
12749 for (j = 0; j < pagesize; j += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012750 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012751
Al Virob9fc7dc2007-12-17 22:59:57 -080012752 data = *((__be32 *) (tmp + j));
Matt Carlsona9dc5292009-02-25 14:25:30 +000012753
Al Virob9fc7dc2007-12-17 22:59:57 -080012754 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012755
12756 tw32(NVRAM_ADDR, phy_addr + j);
12757
12758 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
12759 NVRAM_CMD_WR;
12760
12761 if (j == 0)
12762 nvram_cmd |= NVRAM_CMD_FIRST;
12763 else if (j == (pagesize - 4))
12764 nvram_cmd |= NVRAM_CMD_LAST;
12765
12766 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12767 break;
12768 }
12769 if (ret)
12770 break;
12771 }
12772
12773 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12774 tg3_nvram_exec_cmd(tp, nvram_cmd);
12775
12776 kfree(tmp);
12777
12778 return ret;
12779}
12780
12781/* offset and length are dword aligned */
12782static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
12783 u8 *buf)
12784{
12785 int i, ret = 0;
12786
12787 for (i = 0; i < len; i += 4, offset += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012788 u32 page_off, phy_addr, nvram_cmd;
12789 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012790
12791 memcpy(&data, buf + i, 4);
Al Virob9fc7dc2007-12-17 22:59:57 -080012792 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012793
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012794 page_off = offset % tp->nvram_pagesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012795
Michael Chan18201802006-03-20 22:29:15 -080012796 phy_addr = tg3_nvram_phys_addr(tp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012797
12798 tw32(NVRAM_ADDR, phy_addr);
12799
12800 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
12801
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012802 if (page_off == 0 || i == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012803 nvram_cmd |= NVRAM_CMD_FIRST;
Michael Chanf6d9a252006-04-29 19:00:24 -070012804 if (page_off == (tp->nvram_pagesize - 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012805 nvram_cmd |= NVRAM_CMD_LAST;
12806
12807 if (i == (len - 4))
12808 nvram_cmd |= NVRAM_CMD_LAST;
12809
Matt Carlson321d32a2008-11-21 17:22:19 -080012810 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
Joe Perches63c3a662011-04-26 08:12:10 +000012811 !tg3_flag(tp, 5755_PLUS) &&
Michael Chan4c987482005-09-05 17:52:38 -070012812 (tp->nvram_jedecnum == JEDEC_ST) &&
12813 (nvram_cmd & NVRAM_CMD_FIRST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012814
12815 if ((ret = tg3_nvram_exec_cmd(tp,
12816 NVRAM_CMD_WREN | NVRAM_CMD_GO |
12817 NVRAM_CMD_DONE)))
12818
12819 break;
12820 }
Joe Perches63c3a662011-04-26 08:12:10 +000012821 if (!tg3_flag(tp, FLASH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012822 /* We always do complete word writes to eeprom. */
12823 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
12824 }
12825
12826 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12827 break;
12828 }
12829 return ret;
12830}
12831
12832/* offset and length are dword aligned */
12833static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
12834{
12835 int ret;
12836
Joe Perches63c3a662011-04-26 08:12:10 +000012837 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
Michael Chan314fba32005-04-21 17:07:04 -070012838 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
12839 ~GRC_LCLCTRL_GPIO_OUTPUT1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012840 udelay(40);
12841 }
12842
Joe Perches63c3a662011-04-26 08:12:10 +000012843 if (!tg3_flag(tp, NVRAM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012844 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
Matt Carlson859a5882010-04-05 10:19:28 +000012845 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012846 u32 grc_mode;
12847
Michael Chanec41c7d2006-01-17 02:40:55 -080012848 ret = tg3_nvram_lock(tp);
12849 if (ret)
12850 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012851
Michael Chane6af3012005-04-21 17:12:05 -070012852 tg3_enable_nvram_access(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000012853 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012854 tw32(NVRAM_WRITE1, 0x406);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012855
12856 grc_mode = tr32(GRC_MODE);
12857 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
12858
Joe Perches63c3a662011-04-26 08:12:10 +000012859 if (tg3_flag(tp, NVRAM_BUFFERED) || !tg3_flag(tp, FLASH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012860 ret = tg3_nvram_write_block_buffered(tp, offset, len,
12861 buf);
Matt Carlson859a5882010-04-05 10:19:28 +000012862 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012863 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
12864 buf);
12865 }
12866
12867 grc_mode = tr32(GRC_MODE);
12868 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
12869
Michael Chane6af3012005-04-21 17:12:05 -070012870 tg3_disable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012871 tg3_nvram_unlock(tp);
12872 }
12873
Joe Perches63c3a662011-04-26 08:12:10 +000012874 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
Michael Chan314fba32005-04-21 17:07:04 -070012875 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012876 udelay(40);
12877 }
12878
12879 return ret;
12880}
12881
12882struct subsys_tbl_ent {
12883 u16 subsys_vendor, subsys_devid;
12884 u32 phy_id;
12885};
12886
Matt Carlson24daf2b2010-02-17 15:17:02 +000012887static struct subsys_tbl_ent subsys_id_to_phy_id[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012888 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012889 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012890 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012891 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012892 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012893 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012894 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012895 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12896 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
12897 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012898 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012899 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012900 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012901 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12902 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
12903 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012904 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012905 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012906 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012907 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012908 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012909 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012910 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012911
12912 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012913 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012914 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012915 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012916 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012917 { TG3PCI_SUBVENDOR_ID_3COM,
12918 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
12919 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012920 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012921 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012922 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012923
12924 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012925 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012926 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012927 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012928 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012929 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012930 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012931 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012932 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012933
12934 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012935 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012936 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012937 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012938 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012939 { TG3PCI_SUBVENDOR_ID_COMPAQ,
12940 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
12941 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012942 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012943 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012944 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012945
12946 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012947 { TG3PCI_SUBVENDOR_ID_IBM,
12948 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012949};
12950
Matt Carlson24daf2b2010-02-17 15:17:02 +000012951static struct subsys_tbl_ent * __devinit tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012952{
12953 int i;
12954
12955 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
12956 if ((subsys_id_to_phy_id[i].subsys_vendor ==
12957 tp->pdev->subsystem_vendor) &&
12958 (subsys_id_to_phy_id[i].subsys_devid ==
12959 tp->pdev->subsystem_device))
12960 return &subsys_id_to_phy_id[i];
12961 }
12962 return NULL;
12963}
12964
Michael Chan7d0c41e2005-04-21 17:06:20 -070012965static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012966{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012967 u32 val;
David S. Millerf49639e2006-06-09 11:58:36 -070012968
Matt Carlson79eb6902010-02-17 15:17:03 +000012969 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012970 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12971
Gary Zambranoa85feb82007-05-05 11:52:19 -070012972 /* Assume an onboard device and WOL capable by default. */
Joe Perches63c3a662011-04-26 08:12:10 +000012973 tg3_flag_set(tp, EEPROM_WRITE_PROT);
12974 tg3_flag_set(tp, WOL_CAP);
David S. Miller72b845e2006-03-14 14:11:48 -080012975
Michael Chanb5d37722006-09-27 16:06:21 -070012976 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080012977 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012978 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
12979 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080012980 }
Matt Carlson0527ba32007-10-10 18:03:30 -070012981 val = tr32(VCPU_CFGSHDW);
12982 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Joe Perches63c3a662011-04-26 08:12:10 +000012983 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson0527ba32007-10-10 18:03:30 -070012984 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000012985 (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012986 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000012987 device_set_wakeup_enable(&tp->pdev->dev, true);
12988 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012989 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070012990 }
12991
Linus Torvalds1da177e2005-04-16 15:20:36 -070012992 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
12993 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
12994 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070012995 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012996 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012997
12998 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
12999 tp->nic_sram_data_cfg = nic_cfg;
13000
13001 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
13002 ver >>= NIC_SRAM_DATA_VER_SHIFT;
Matt Carlson6ff6f812011-05-19 12:12:54 +000013003 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13004 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13005 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070013006 (ver > 0) && (ver < 0x100))
13007 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
13008
Matt Carlsona9daf362008-05-25 23:49:44 -070013009 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
13010 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
13011
Linus Torvalds1da177e2005-04-16 15:20:36 -070013012 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
13013 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
13014 eeprom_phy_serdes = 1;
13015
13016 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
13017 if (nic_phy_id != 0) {
13018 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
13019 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
13020
13021 eeprom_phy_id = (id1 >> 16) << 10;
13022 eeprom_phy_id |= (id2 & 0xfc00) << 16;
13023 eeprom_phy_id |= (id2 & 0x03ff) << 0;
13024 } else
13025 eeprom_phy_id = 0;
13026
Michael Chan7d0c41e2005-04-21 17:06:20 -070013027 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070013028 if (eeprom_phy_serdes) {
Joe Perches63c3a662011-04-26 08:12:10 +000013029 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013030 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000013031 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013032 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070013033 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070013034
Joe Perches63c3a662011-04-26 08:12:10 +000013035 if (tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013036 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
13037 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070013038 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070013039 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
13040
13041 switch (led_cfg) {
13042 default:
13043 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
13044 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13045 break;
13046
13047 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
13048 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
13049 break;
13050
13051 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
13052 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070013053
13054 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
13055 * read on some older 5700/5701 bootcode.
13056 */
13057 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
13058 ASIC_REV_5700 ||
13059 GET_ASIC_REV(tp->pci_chip_rev_id) ==
13060 ASIC_REV_5701)
13061 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13062
Linus Torvalds1da177e2005-04-16 15:20:36 -070013063 break;
13064
13065 case SHASTA_EXT_LED_SHARED:
13066 tp->led_ctrl = LED_CTRL_MODE_SHARED;
13067 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
13068 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
13069 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
13070 LED_CTRL_MODE_PHY_2);
13071 break;
13072
13073 case SHASTA_EXT_LED_MAC:
13074 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
13075 break;
13076
13077 case SHASTA_EXT_LED_COMBO:
13078 tp->led_ctrl = LED_CTRL_MODE_COMBO;
13079 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
13080 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
13081 LED_CTRL_MODE_PHY_2);
13082 break;
13083
Stephen Hemminger855e1112008-04-16 16:37:28 -070013084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013085
13086 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13087 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
13088 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
13089 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
13090
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013091 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
13092 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080013093
Michael Chan9d26e212006-12-07 00:21:14 -080013094 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Joe Perches63c3a662011-04-26 08:12:10 +000013095 tg3_flag_set(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080013096 if ((tp->pdev->subsystem_vendor ==
13097 PCI_VENDOR_ID_ARIMA) &&
13098 (tp->pdev->subsystem_device == 0x205a ||
13099 tp->pdev->subsystem_device == 0x2063))
Joe Perches63c3a662011-04-26 08:12:10 +000013100 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080013101 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000013102 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
13103 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080013104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013105
13106 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +000013107 tg3_flag_set(tp, ENABLE_ASF);
13108 if (tg3_flag(tp, 5750_PLUS))
13109 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013110 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080013111
13112 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013113 tg3_flag(tp, 5750_PLUS))
13114 tg3_flag_set(tp, ENABLE_APE);
Matt Carlsonb2b98d42008-11-03 16:52:32 -080013115
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013116 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070013117 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
Joe Perches63c3a662011-04-26 08:12:10 +000013118 tg3_flag_clear(tp, WOL_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013119
Joe Perches63c3a662011-04-26 08:12:10 +000013120 if (tg3_flag(tp, WOL_CAP) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013121 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013122 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013123 device_set_wakeup_enable(&tp->pdev->dev, true);
13124 }
Matt Carlson0527ba32007-10-10 18:03:30 -070013125
Linus Torvalds1da177e2005-04-16 15:20:36 -070013126 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013127 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013128
13129 /* serdes signal pre-emphasis in register 0x590 set by */
13130 /* bootcode if bit 18 is set */
13131 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013132 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070013133
Joe Perches63c3a662011-04-26 08:12:10 +000013134 if ((tg3_flag(tp, 57765_PLUS) ||
13135 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
13136 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080013137 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013138 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080013139
Joe Perches63c3a662011-04-26 08:12:10 +000013140 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson8c69b1e2010-08-02 11:26:00 +000013141 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +000013142 !tg3_flag(tp, 57765_PLUS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070013143 u32 cfg3;
13144
13145 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
13146 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
Joe Perches63c3a662011-04-26 08:12:10 +000013147 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson8ed5d972007-05-07 00:25:49 -070013148 }
Matt Carlsona9daf362008-05-25 23:49:44 -070013149
Matt Carlson14417062010-02-17 15:16:59 +000013150 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
Joe Perches63c3a662011-04-26 08:12:10 +000013151 tg3_flag_set(tp, RGMII_INBAND_DISABLE);
Matt Carlsona9daf362008-05-25 23:49:44 -070013152 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000013153 tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
Matt Carlsona9daf362008-05-25 23:49:44 -070013154 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000013155 tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013156 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080013157done:
Joe Perches63c3a662011-04-26 08:12:10 +000013158 if (tg3_flag(tp, WOL_CAP))
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000013159 device_set_wakeup_enable(&tp->pdev->dev,
Joe Perches63c3a662011-04-26 08:12:10 +000013160 tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000013161 else
13162 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070013163}
13164
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013165static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
13166{
13167 int i;
13168 u32 val;
13169
13170 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
13171 tw32(OTP_CTRL, cmd);
13172
13173 /* Wait for up to 1 ms for command to execute. */
13174 for (i = 0; i < 100; i++) {
13175 val = tr32(OTP_STATUS);
13176 if (val & OTP_STATUS_CMD_DONE)
13177 break;
13178 udelay(10);
13179 }
13180
13181 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
13182}
13183
13184/* Read the gphy configuration from the OTP region of the chip. The gphy
13185 * configuration is a 32-bit value that straddles the alignment boundary.
13186 * We do two 32-bit reads and then shift and merge the results.
13187 */
13188static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
13189{
13190 u32 bhalf_otp, thalf_otp;
13191
13192 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
13193
13194 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
13195 return 0;
13196
13197 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
13198
13199 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
13200 return 0;
13201
13202 thalf_otp = tr32(OTP_READ_DATA);
13203
13204 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
13205
13206 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
13207 return 0;
13208
13209 bhalf_otp = tr32(OTP_READ_DATA);
13210
13211 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
13212}
13213
Matt Carlsone256f8a2011-03-09 16:58:24 +000013214static void __devinit tg3_phy_init_link_config(struct tg3 *tp)
13215{
13216 u32 adv = ADVERTISED_Autoneg |
13217 ADVERTISED_Pause;
13218
13219 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
13220 adv |= ADVERTISED_1000baseT_Half |
13221 ADVERTISED_1000baseT_Full;
13222
13223 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
13224 adv |= ADVERTISED_100baseT_Half |
13225 ADVERTISED_100baseT_Full |
13226 ADVERTISED_10baseT_Half |
13227 ADVERTISED_10baseT_Full |
13228 ADVERTISED_TP;
13229 else
13230 adv |= ADVERTISED_FIBRE;
13231
13232 tp->link_config.advertising = adv;
13233 tp->link_config.speed = SPEED_INVALID;
13234 tp->link_config.duplex = DUPLEX_INVALID;
13235 tp->link_config.autoneg = AUTONEG_ENABLE;
13236 tp->link_config.active_speed = SPEED_INVALID;
13237 tp->link_config.active_duplex = DUPLEX_INVALID;
13238 tp->link_config.orig_speed = SPEED_INVALID;
13239 tp->link_config.orig_duplex = DUPLEX_INVALID;
13240 tp->link_config.orig_autoneg = AUTONEG_INVALID;
13241}
13242
Michael Chan7d0c41e2005-04-21 17:06:20 -070013243static int __devinit tg3_phy_probe(struct tg3 *tp)
13244{
13245 u32 hw_phy_id_1, hw_phy_id_2;
13246 u32 hw_phy_id, hw_phy_id_masked;
13247 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013248
Matt Carlsone256f8a2011-03-09 16:58:24 +000013249 /* flow control autonegotiation is default behavior */
Joe Perches63c3a662011-04-26 08:12:10 +000013250 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsone256f8a2011-03-09 16:58:24 +000013251 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
13252
Joe Perches63c3a662011-04-26 08:12:10 +000013253 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013254 return tg3_phy_init(tp);
13255
Linus Torvalds1da177e2005-04-16 15:20:36 -070013256 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010013257 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013258 */
13259 err = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000013260 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000013261 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013262 } else {
13263 /* Now read the physical PHY_ID from the chip and verify
13264 * that it is sane. If it doesn't look good, we fall back
13265 * to either the hard-coded table based PHY_ID and failing
13266 * that the value found in the eeprom area.
13267 */
13268 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
13269 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
13270
13271 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
13272 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
13273 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
13274
Matt Carlson79eb6902010-02-17 15:17:03 +000013275 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013276 }
13277
Matt Carlson79eb6902010-02-17 15:17:03 +000013278 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013279 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000013280 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013281 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070013282 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013283 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013284 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000013285 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070013286 /* Do nothing, phy ID already set up in
13287 * tg3_get_eeprom_hw_cfg().
13288 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070013289 } else {
13290 struct subsys_tbl_ent *p;
13291
13292 /* No eeprom signature? Try the hardcoded
13293 * subsys device table.
13294 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013295 p = tg3_lookup_by_subsys(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013296 if (!p)
13297 return -ENODEV;
13298
13299 tp->phy_id = p->phy_id;
13300 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000013301 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013302 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013303 }
13304 }
13305
Matt Carlsona6b68da2010-12-06 08:28:52 +000013306 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Matt Carlson5baa5e92011-07-20 10:20:53 +000013307 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13308 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720 ||
13309 (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +000013310 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0) ||
13311 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
13312 tp->pci_chip_rev_id != CHIPREV_ID_57765_A0)))
Matt Carlson52b02d02010-10-14 10:37:41 +000013313 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
13314
Matt Carlsone256f8a2011-03-09 16:58:24 +000013315 tg3_phy_init_link_config(tp);
13316
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013317 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013318 !tg3_flag(tp, ENABLE_APE) &&
13319 !tg3_flag(tp, ENABLE_ASF)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000013320 u32 bmsr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013321
13322 tg3_readphy(tp, MII_BMSR, &bmsr);
13323 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
13324 (bmsr & BMSR_LSTATUS))
13325 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013326
Linus Torvalds1da177e2005-04-16 15:20:36 -070013327 err = tg3_phy_reset(tp);
13328 if (err)
13329 return err;
13330
Matt Carlson42b64a42011-05-19 12:12:49 +000013331 tg3_phy_set_wirespeed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013332
Michael Chan3600d912006-12-07 00:21:48 -080013333 mask = (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
13334 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
13335 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full);
13336 if (!tg3_copper_is_advertising_all(tp, mask)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000013337 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
13338 tp->link_config.flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013339
13340 tg3_writephy(tp, MII_BMCR,
13341 BMCR_ANENABLE | BMCR_ANRESTART);
13342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013343 }
13344
13345skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000013346 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013347 err = tg3_init_5401phy_dsp(tp);
13348 if (err)
13349 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013350
Linus Torvalds1da177e2005-04-16 15:20:36 -070013351 err = tg3_init_5401phy_dsp(tp);
13352 }
13353
Linus Torvalds1da177e2005-04-16 15:20:36 -070013354 return err;
13355}
13356
Matt Carlson184b8902010-04-05 10:19:25 +000013357static void __devinit tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013358{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013359 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013360 unsigned int block_end, rosize, len;
Matt Carlson535a4902011-07-20 10:20:56 +000013361 u32 vpdlen;
Matt Carlson184b8902010-04-05 10:19:25 +000013362 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013363
Matt Carlson535a4902011-07-20 10:20:56 +000013364 vpd_data = (u8 *)tg3_vpd_readblock(tp, &vpdlen);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013365 if (!vpd_data)
13366 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013367
Matt Carlson535a4902011-07-20 10:20:56 +000013368 i = pci_vpd_find_tag(vpd_data, 0, vpdlen, PCI_VPD_LRDT_RO_DATA);
Matt Carlson4181b2c2010-02-26 14:04:45 +000013369 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013370 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013371
13372 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
13373 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
13374 i += PCI_VPD_LRDT_TAG_SIZE;
13375
Matt Carlson535a4902011-07-20 10:20:56 +000013376 if (block_end > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000013377 goto out_not_found;
13378
Matt Carlson184b8902010-04-05 10:19:25 +000013379 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13380 PCI_VPD_RO_KEYWORD_MFR_ID);
13381 if (j > 0) {
13382 len = pci_vpd_info_field_size(&vpd_data[j]);
13383
13384 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13385 if (j + len > block_end || len != 4 ||
13386 memcmp(&vpd_data[j], "1028", 4))
13387 goto partno;
13388
13389 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13390 PCI_VPD_RO_KEYWORD_VENDOR0);
13391 if (j < 0)
13392 goto partno;
13393
13394 len = pci_vpd_info_field_size(&vpd_data[j]);
13395
13396 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13397 if (j + len > block_end)
13398 goto partno;
13399
13400 memcpy(tp->fw_ver, &vpd_data[j], len);
Matt Carlson535a4902011-07-20 10:20:56 +000013401 strncat(tp->fw_ver, " bc ", vpdlen - len - 1);
Matt Carlson184b8902010-04-05 10:19:25 +000013402 }
13403
13404partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000013405 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13406 PCI_VPD_RO_KEYWORD_PARTNO);
13407 if (i < 0)
13408 goto out_not_found;
13409
13410 len = pci_vpd_info_field_size(&vpd_data[i]);
13411
13412 i += PCI_VPD_INFO_FLD_HDR_SIZE;
13413 if (len > TG3_BPN_SIZE ||
Matt Carlson535a4902011-07-20 10:20:56 +000013414 (len + i) > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000013415 goto out_not_found;
13416
13417 memcpy(tp->board_part_number, &vpd_data[i], len);
13418
Linus Torvalds1da177e2005-04-16 15:20:36 -070013419out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013420 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000013421 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013422 return;
13423
13424out_no_vpd:
Matt Carlson37a949c2010-09-30 10:34:33 +000013425 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
13426 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717)
13427 strcpy(tp->board_part_number, "BCM5717");
13428 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
13429 strcpy(tp->board_part_number, "BCM5718");
13430 else
13431 goto nomatch;
13432 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
13433 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
13434 strcpy(tp->board_part_number, "BCM57780");
13435 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
13436 strcpy(tp->board_part_number, "BCM57760");
13437 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
13438 strcpy(tp->board_part_number, "BCM57790");
13439 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
13440 strcpy(tp->board_part_number, "BCM57788");
13441 else
13442 goto nomatch;
13443 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
13444 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
13445 strcpy(tp->board_part_number, "BCM57761");
13446 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
13447 strcpy(tp->board_part_number, "BCM57765");
13448 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
13449 strcpy(tp->board_part_number, "BCM57781");
13450 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
13451 strcpy(tp->board_part_number, "BCM57785");
13452 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
13453 strcpy(tp->board_part_number, "BCM57791");
13454 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13455 strcpy(tp->board_part_number, "BCM57795");
13456 else
13457 goto nomatch;
13458 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070013459 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000013460 } else {
13461nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070013462 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000013463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013464}
13465
Matt Carlson9c8a6202007-10-21 16:16:08 -070013466static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
13467{
13468 u32 val;
13469
Matt Carlsone4f34112009-02-25 14:25:00 +000013470 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013471 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013472 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013473 val != 0)
13474 return 0;
13475
13476 return 1;
13477}
13478
Matt Carlsonacd9c112009-02-25 14:26:33 +000013479static void __devinit tg3_read_bc_ver(struct tg3 *tp)
13480{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013481 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000013482 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013483 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013484
13485 if (tg3_nvram_read(tp, 0xc, &offset) ||
13486 tg3_nvram_read(tp, 0x4, &start))
13487 return;
13488
13489 offset = tg3_nvram_logical_addr(tp, offset);
13490
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013491 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013492 return;
13493
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013494 if ((val & 0xfc000000) == 0x0c000000) {
13495 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013496 return;
13497
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013498 if (val == 0)
13499 newver = true;
13500 }
13501
Matt Carlson75f99362010-04-05 10:19:24 +000013502 dst_off = strlen(tp->fw_ver);
13503
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013504 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000013505 if (TG3_VER_SIZE - dst_off < 16 ||
13506 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013507 return;
13508
13509 offset = offset + ver_offset - start;
13510 for (i = 0; i < 16; i += 4) {
13511 __be32 v;
13512 if (tg3_nvram_read_be32(tp, offset + i, &v))
13513 return;
13514
Matt Carlson75f99362010-04-05 10:19:24 +000013515 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013516 }
13517 } else {
13518 u32 major, minor;
13519
13520 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
13521 return;
13522
13523 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
13524 TG3_NVM_BCVER_MAJSFT;
13525 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000013526 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
13527 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013528 }
13529}
13530
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013531static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
13532{
13533 u32 val, major, minor;
13534
13535 /* Use native endian representation */
13536 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
13537 return;
13538
13539 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
13540 TG3_NVM_HWSB_CFG1_MAJSFT;
13541 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
13542 TG3_NVM_HWSB_CFG1_MINSFT;
13543
13544 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
13545}
13546
Matt Carlsondfe00d72008-11-21 17:19:41 -080013547static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
13548{
13549 u32 offset, major, minor, build;
13550
Matt Carlson75f99362010-04-05 10:19:24 +000013551 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013552
13553 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
13554 return;
13555
13556 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
13557 case TG3_EEPROM_SB_REVISION_0:
13558 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
13559 break;
13560 case TG3_EEPROM_SB_REVISION_2:
13561 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
13562 break;
13563 case TG3_EEPROM_SB_REVISION_3:
13564 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
13565 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000013566 case TG3_EEPROM_SB_REVISION_4:
13567 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
13568 break;
13569 case TG3_EEPROM_SB_REVISION_5:
13570 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
13571 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000013572 case TG3_EEPROM_SB_REVISION_6:
13573 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
13574 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013575 default:
13576 return;
13577 }
13578
Matt Carlsone4f34112009-02-25 14:25:00 +000013579 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080013580 return;
13581
13582 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
13583 TG3_EEPROM_SB_EDH_BLD_SHFT;
13584 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
13585 TG3_EEPROM_SB_EDH_MAJ_SHFT;
13586 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
13587
13588 if (minor > 99 || build > 26)
13589 return;
13590
Matt Carlson75f99362010-04-05 10:19:24 +000013591 offset = strlen(tp->fw_ver);
13592 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
13593 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013594
13595 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000013596 offset = strlen(tp->fw_ver);
13597 if (offset < TG3_VER_SIZE - 1)
13598 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013599 }
13600}
13601
Matt Carlsonacd9c112009-02-25 14:26:33 +000013602static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080013603{
13604 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013605 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070013606
13607 for (offset = TG3_NVM_DIR_START;
13608 offset < TG3_NVM_DIR_END;
13609 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000013610 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013611 return;
13612
13613 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
13614 break;
13615 }
13616
13617 if (offset == TG3_NVM_DIR_END)
13618 return;
13619
Joe Perches63c3a662011-04-26 08:12:10 +000013620 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013621 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000013622 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013623 return;
13624
Matt Carlsone4f34112009-02-25 14:25:00 +000013625 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013626 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013627 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013628 return;
13629
13630 offset += val - start;
13631
Matt Carlsonacd9c112009-02-25 14:26:33 +000013632 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013633
Matt Carlsonacd9c112009-02-25 14:26:33 +000013634 tp->fw_ver[vlen++] = ',';
13635 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070013636
13637 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000013638 __be32 v;
13639 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013640 return;
13641
Al Virob9fc7dc2007-12-17 22:59:57 -080013642 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013643
Matt Carlsonacd9c112009-02-25 14:26:33 +000013644 if (vlen > TG3_VER_SIZE - sizeof(v)) {
13645 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013646 break;
13647 }
13648
Matt Carlsonacd9c112009-02-25 14:26:33 +000013649 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
13650 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013651 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000013652}
13653
Matt Carlson7fd76442009-02-25 14:27:20 +000013654static void __devinit tg3_read_dash_ver(struct tg3 *tp)
13655{
13656 int vlen;
13657 u32 apedata;
Matt Carlsonecc79642010-08-02 11:26:01 +000013658 char *fwtype;
Matt Carlson7fd76442009-02-25 14:27:20 +000013659
Joe Perches63c3a662011-04-26 08:12:10 +000013660 if (!tg3_flag(tp, ENABLE_APE) || !tg3_flag(tp, ENABLE_ASF))
Matt Carlson7fd76442009-02-25 14:27:20 +000013661 return;
13662
13663 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
13664 if (apedata != APE_SEG_SIG_MAGIC)
13665 return;
13666
13667 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
13668 if (!(apedata & APE_FW_STATUS_READY))
13669 return;
13670
13671 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
13672
Matt Carlsondc6d0742010-09-15 08:59:55 +000013673 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI) {
Joe Perches63c3a662011-04-26 08:12:10 +000013674 tg3_flag_set(tp, APE_HAS_NCSI);
Matt Carlsonecc79642010-08-02 11:26:01 +000013675 fwtype = "NCSI";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013676 } else {
Matt Carlsonecc79642010-08-02 11:26:01 +000013677 fwtype = "DASH";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013678 }
Matt Carlsonecc79642010-08-02 11:26:01 +000013679
Matt Carlson7fd76442009-02-25 14:27:20 +000013680 vlen = strlen(tp->fw_ver);
13681
Matt Carlsonecc79642010-08-02 11:26:01 +000013682 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
13683 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000013684 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
13685 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
13686 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
13687 (apedata & APE_FW_VERSION_BLDMSK));
13688}
13689
Matt Carlsonacd9c112009-02-25 14:26:33 +000013690static void __devinit tg3_read_fw_ver(struct tg3 *tp)
13691{
13692 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000013693 bool vpd_vers = false;
13694
13695 if (tp->fw_ver[0] != 0)
13696 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013697
Joe Perches63c3a662011-04-26 08:12:10 +000013698 if (tg3_flag(tp, NO_NVRAM)) {
Matt Carlson75f99362010-04-05 10:19:24 +000013699 strcat(tp->fw_ver, "sb");
Matt Carlsondf259d82009-04-20 06:57:14 +000013700 return;
13701 }
13702
Matt Carlsonacd9c112009-02-25 14:26:33 +000013703 if (tg3_nvram_read(tp, 0, &val))
13704 return;
13705
13706 if (val == TG3_EEPROM_MAGIC)
13707 tg3_read_bc_ver(tp);
13708 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
13709 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013710 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
13711 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013712 else
13713 return;
13714
Matt Carlsonc9cab242011-07-13 09:27:27 +000013715 if (vpd_vers)
Matt Carlson75f99362010-04-05 10:19:24 +000013716 goto done;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013717
Matt Carlsonc9cab242011-07-13 09:27:27 +000013718 if (tg3_flag(tp, ENABLE_APE)) {
13719 if (tg3_flag(tp, ENABLE_ASF))
13720 tg3_read_dash_ver(tp);
13721 } else if (tg3_flag(tp, ENABLE_ASF)) {
13722 tg3_read_mgmtfw_ver(tp);
13723 }
Matt Carlson9c8a6202007-10-21 16:16:08 -070013724
Matt Carlson75f99362010-04-05 10:19:24 +000013725done:
Matt Carlson9c8a6202007-10-21 16:16:08 -070013726 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080013727}
13728
Michael Chan7544b092007-05-05 13:08:32 -070013729static struct pci_dev * __devinit tg3_find_peer(struct tg3 *);
13730
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013731static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
13732{
Joe Perches63c3a662011-04-26 08:12:10 +000013733 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013734 return TG3_RX_RET_MAX_SIZE_5717;
Joe Perches63c3a662011-04-26 08:12:10 +000013735 else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013736 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013737 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000013738 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013739}
13740
Matt Carlson41434702011-03-09 16:58:22 +000013741static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080013742 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
13743 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
13744 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
13745 { },
13746};
13747
Linus Torvalds1da177e2005-04-16 15:20:36 -070013748static int __devinit tg3_get_invariants(struct tg3 *tp)
13749{
Linus Torvalds1da177e2005-04-16 15:20:36 -070013750 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013751 u32 pci_state_reg, grc_misc_cfg;
13752 u32 val;
13753 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013754 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013755
Linus Torvalds1da177e2005-04-16 15:20:36 -070013756 /* Force memory write invalidate off. If we leave it on,
13757 * then on 5700_BX chips we have to enable a workaround.
13758 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
13759 * to match the cacheline size. The Broadcom driver have this
13760 * workaround but turns MWI off all the times so never uses
13761 * it. This seems to suggest that the workaround is insufficient.
13762 */
13763 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13764 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
13765 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13766
Matt Carlson16821282011-07-13 09:27:28 +000013767 /* Important! -- Make sure register accesses are byteswapped
13768 * correctly. Also, for those chips that require it, make
13769 * sure that indirect register accesses are enabled before
13770 * the first operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013771 */
13772 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13773 &misc_ctrl_reg);
Matt Carlson16821282011-07-13 09:27:28 +000013774 tp->misc_host_ctrl |= (misc_ctrl_reg &
13775 MISC_HOST_CTRL_CHIPREV);
13776 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13777 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013778
13779 tp->pci_chip_rev_id = (misc_ctrl_reg >>
13780 MISC_HOST_CTRL_CHIPREV_SHIFT);
Matt Carlson795d01c2007-10-07 23:28:17 -070013781 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
13782 u32 prod_id_asic_rev;
13783
Matt Carlson5001e2f2009-11-13 13:03:51 +000013784 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
13785 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013786 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
13787 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013788 pci_read_config_dword(tp->pdev,
13789 TG3PCI_GEN2_PRODID_ASICREV,
13790 &prod_id_asic_rev);
Matt Carlsonb703df62009-12-03 08:36:21 +000013791 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
13792 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
13793 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
13794 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
13795 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
13796 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13797 pci_read_config_dword(tp->pdev,
13798 TG3PCI_GEN15_PRODID_ASICREV,
13799 &prod_id_asic_rev);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013800 else
13801 pci_read_config_dword(tp->pdev, TG3PCI_PRODID_ASICREV,
13802 &prod_id_asic_rev);
13803
Matt Carlson321d32a2008-11-21 17:22:19 -080013804 tp->pci_chip_rev_id = prod_id_asic_rev;
Matt Carlson795d01c2007-10-07 23:28:17 -070013805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013806
Michael Chanff645be2005-04-21 17:09:53 -070013807 /* Wrong chip ID in 5752 A0. This code can be removed later
13808 * as A0 is not in production.
13809 */
13810 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
13811 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
13812
Michael Chan68929142005-08-09 20:17:14 -070013813 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
13814 * we need to disable memory and use config. cycles
13815 * only to access all registers. The 5702/03 chips
13816 * can mistakenly decode the special cycles from the
13817 * ICH chipsets as memory write cycles, causing corruption
13818 * of register and memory space. Only certain ICH bridges
13819 * will drive special cycles with non-zero data during the
13820 * address phase which can fall within the 5703's address
13821 * range. This is not an ICH bug as the PCI spec allows
13822 * non-zero address during special cycles. However, only
13823 * these ICH bridges are known to drive non-zero addresses
13824 * during special cycles.
13825 *
13826 * Since special cycles do not cross PCI bridges, we only
13827 * enable this workaround if the 5703 is on the secondary
13828 * bus of these ICH bridges.
13829 */
13830 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
13831 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
13832 static struct tg3_dev_id {
13833 u32 vendor;
13834 u32 device;
13835 u32 rev;
13836 } ich_chipsets[] = {
13837 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
13838 PCI_ANY_ID },
13839 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
13840 PCI_ANY_ID },
13841 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
13842 0xa },
13843 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
13844 PCI_ANY_ID },
13845 { },
13846 };
13847 struct tg3_dev_id *pci_id = &ich_chipsets[0];
13848 struct pci_dev *bridge = NULL;
13849
13850 while (pci_id->vendor != 0) {
13851 bridge = pci_get_device(pci_id->vendor, pci_id->device,
13852 bridge);
13853 if (!bridge) {
13854 pci_id++;
13855 continue;
13856 }
13857 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070013858 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070013859 continue;
13860 }
13861 if (bridge->subordinate &&
13862 (bridge->subordinate->number ==
13863 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013864 tg3_flag_set(tp, ICH_WORKAROUND);
Michael Chan68929142005-08-09 20:17:14 -070013865 pci_dev_put(bridge);
13866 break;
13867 }
13868 }
13869 }
13870
Matt Carlson6ff6f812011-05-19 12:12:54 +000013871 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Matt Carlson41588ba2008-04-19 18:12:33 -070013872 static struct tg3_dev_id {
13873 u32 vendor;
13874 u32 device;
13875 } bridge_chipsets[] = {
13876 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
13877 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
13878 { },
13879 };
13880 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
13881 struct pci_dev *bridge = NULL;
13882
13883 while (pci_id->vendor != 0) {
13884 bridge = pci_get_device(pci_id->vendor,
13885 pci_id->device,
13886 bridge);
13887 if (!bridge) {
13888 pci_id++;
13889 continue;
13890 }
13891 if (bridge->subordinate &&
13892 (bridge->subordinate->number <=
13893 tp->pdev->bus->number) &&
13894 (bridge->subordinate->subordinate >=
13895 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013896 tg3_flag_set(tp, 5701_DMA_BUG);
Matt Carlson41588ba2008-04-19 18:12:33 -070013897 pci_dev_put(bridge);
13898 break;
13899 }
13900 }
13901 }
13902
Michael Chan4a29cc22006-03-19 13:21:12 -080013903 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
13904 * DMA addresses > 40-bit. This bridge may have other additional
13905 * 57xx devices behind it in some 4-port NIC designs for example.
13906 * Any tg3 device found behind the bridge will also need the 40-bit
13907 * DMA workaround.
13908 */
Michael Chana4e2b342005-10-26 15:46:52 -070013909 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
13910 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Joe Perches63c3a662011-04-26 08:12:10 +000013911 tg3_flag_set(tp, 5780_CLASS);
13912 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4cf78e42005-07-25 12:29:19 -070013913 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a5882010-04-05 10:19:28 +000013914 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080013915 struct pci_dev *bridge = NULL;
13916
13917 do {
13918 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
13919 PCI_DEVICE_ID_SERVERWORKS_EPB,
13920 bridge);
13921 if (bridge && bridge->subordinate &&
13922 (bridge->subordinate->number <=
13923 tp->pdev->bus->number) &&
13924 (bridge->subordinate->subordinate >=
13925 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013926 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4a29cc22006-03-19 13:21:12 -080013927 pci_dev_put(bridge);
13928 break;
13929 }
13930 } while (bridge);
13931 }
Michael Chan4cf78e42005-07-25 12:29:19 -070013932
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013933 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Matt Carlson3a1e19d2011-07-13 09:27:32 +000013934 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)
Michael Chan7544b092007-05-05 13:08:32 -070013935 tp->pdev_peer = tg3_find_peer(tp);
13936
Matt Carlsonc885e822010-08-02 11:25:57 +000013937 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013938 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13939 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Joe Perches63c3a662011-04-26 08:12:10 +000013940 tg3_flag_set(tp, 5717_PLUS);
Matt Carlson0a58d662011-04-05 14:22:45 +000013941
13942 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013943 tg3_flag(tp, 5717_PLUS))
13944 tg3_flag_set(tp, 57765_PLUS);
Matt Carlsonc885e822010-08-02 11:25:57 +000013945
Matt Carlson321d32a2008-11-21 17:22:19 -080013946 /* Intentionally exclude ASIC_REV_5906 */
13947 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Michael Chand9ab5ad2006-03-20 22:27:35 -080013948 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013949 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013950 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013951 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013952 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013953 tg3_flag(tp, 57765_PLUS))
13954 tg3_flag_set(tp, 5755_PLUS);
Matt Carlson321d32a2008-11-21 17:22:19 -080013955
13956 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13957 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Michael Chanb5d37722006-09-27 16:06:21 -070013958 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013959 tg3_flag(tp, 5755_PLUS) ||
13960 tg3_flag(tp, 5780_CLASS))
13961 tg3_flag_set(tp, 5750_PLUS);
John W. Linville6708e5c2005-04-21 17:00:52 -070013962
Matt Carlson6ff6f812011-05-19 12:12:54 +000013963 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013964 tg3_flag(tp, 5750_PLUS))
13965 tg3_flag_set(tp, 5705_PLUS);
John W. Linville1b440c562005-04-21 17:03:18 -070013966
Matt Carlson507399f2009-11-13 13:03:37 +000013967 /* Determine TSO capabilities */
Matt Carlsona0512942011-07-27 14:20:54 +000013968 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0)
Matt Carlson4d163b72011-01-25 15:58:48 +000013969 ; /* Do nothing. HW bug. */
Joe Perches63c3a662011-04-26 08:12:10 +000013970 else if (tg3_flag(tp, 57765_PLUS))
13971 tg3_flag_set(tp, HW_TSO_3);
13972 else if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000013973 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000013974 tg3_flag_set(tp, HW_TSO_2);
13975 else if (tg3_flag(tp, 5750_PLUS)) {
13976 tg3_flag_set(tp, HW_TSO_1);
13977 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013978 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
13979 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
Joe Perches63c3a662011-04-26 08:12:10 +000013980 tg3_flag_clear(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013981 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13982 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13983 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000013984 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013985 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
13986 tp->fw_needed = FIRMWARE_TG3TSO5;
13987 else
13988 tp->fw_needed = FIRMWARE_TG3TSO;
13989 }
13990
Matt Carlsondabc5c62011-05-19 12:12:52 +000013991 /* Selectively allow TSO based on operating conditions */
Matt Carlson6ff6f812011-05-19 12:12:54 +000013992 if (tg3_flag(tp, HW_TSO_1) ||
13993 tg3_flag(tp, HW_TSO_2) ||
13994 tg3_flag(tp, HW_TSO_3) ||
Matt Carlsondabc5c62011-05-19 12:12:52 +000013995 (tp->fw_needed && !tg3_flag(tp, ENABLE_ASF)))
13996 tg3_flag_set(tp, TSO_CAPABLE);
13997 else {
13998 tg3_flag_clear(tp, TSO_CAPABLE);
13999 tg3_flag_clear(tp, TSO_BUG);
14000 tp->fw_needed = NULL;
14001 }
14002
14003 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
14004 tp->fw_needed = FIRMWARE_TG3;
14005
Matt Carlson507399f2009-11-13 13:03:37 +000014006 tp->irq_max = 1;
14007
Joe Perches63c3a662011-04-26 08:12:10 +000014008 if (tg3_flag(tp, 5750_PLUS)) {
14009 tg3_flag_set(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070014010 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
14011 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
14012 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
14013 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
14014 tp->pdev_peer == tp->pdev))
Joe Perches63c3a662011-04-26 08:12:10 +000014015 tg3_flag_clear(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070014016
Joe Perches63c3a662011-04-26 08:12:10 +000014017 if (tg3_flag(tp, 5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070014018 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000014019 tg3_flag_set(tp, 1SHOT_MSI);
Michael Chan52c0fd82006-06-29 20:15:54 -070014020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014021
Joe Perches63c3a662011-04-26 08:12:10 +000014022 if (tg3_flag(tp, 57765_PLUS)) {
14023 tg3_flag_set(tp, SUPPORT_MSIX);
Matt Carlson507399f2009-11-13 13:03:37 +000014024 tp->irq_max = TG3_IRQ_MAX_VECS;
14025 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014026 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000014027
Matt Carlson2ffcc982011-05-19 12:12:44 +000014028 if (tg3_flag(tp, 5755_PLUS))
Joe Perches63c3a662011-04-26 08:12:10 +000014029 tg3_flag_set(tp, SHORT_DMA_BUG);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014030
Matt Carlsone31aa982011-07-27 14:20:53 +000014031 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
14032 tg3_flag_set(tp, 4K_FIFO_LIMIT);
14033
Joe Perches63c3a662011-04-26 08:12:10 +000014034 if (tg3_flag(tp, 5717_PLUS))
14035 tg3_flag_set(tp, LRG_PROD_RING_CAP);
Matt Carlsonde9f5232011-04-05 14:22:43 +000014036
Joe Perches63c3a662011-04-26 08:12:10 +000014037 if (tg3_flag(tp, 57765_PLUS) &&
Matt Carlsona0512942011-07-27 14:20:54 +000014038 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0)
Joe Perches63c3a662011-04-26 08:12:10 +000014039 tg3_flag_set(tp, USE_JUMBO_BDFLAG);
Matt Carlsonb703df62009-12-03 08:36:21 +000014040
Joe Perches63c3a662011-04-26 08:12:10 +000014041 if (!tg3_flag(tp, 5705_PLUS) ||
14042 tg3_flag(tp, 5780_CLASS) ||
14043 tg3_flag(tp, USE_JUMBO_BDFLAG))
14044 tg3_flag_set(tp, JUMBO_CAPABLE);
Michael Chan0f893dc2005-07-25 12:30:38 -070014045
Matt Carlson52f44902008-11-21 17:17:04 -080014046 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14047 &pci_state_reg);
14048
Jon Mason708ebb32011-06-27 12:56:50 +000014049 if (pci_is_pcie(tp->pdev)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014050 u16 lnkctl;
14051
Joe Perches63c3a662011-04-26 08:12:10 +000014052 tg3_flag_set(tp, PCI_EXPRESS);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080014053
Matt Carlsoncf790032010-11-24 08:31:48 +000014054 tp->pcie_readrq = 4096;
Matt Carlsond78b59f2011-04-05 14:22:46 +000014055 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
14056 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Matt Carlsonb4495ed2011-01-25 15:58:47 +000014057 tp->pcie_readrq = 2048;
Matt Carlsoncf790032010-11-24 08:31:48 +000014058
14059 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080014060
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014061 pci_read_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +000014062 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014063 &lnkctl);
14064 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
Matt Carlson7196cd62011-05-19 16:02:44 +000014065 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
14066 ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000014067 tg3_flag_clear(tp, HW_TSO_2);
Matt Carlsondabc5c62011-05-19 12:12:52 +000014068 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson7196cd62011-05-19 16:02:44 +000014069 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014070 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014071 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000014072 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
14073 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Joe Perches63c3a662011-04-26 08:12:10 +000014074 tg3_flag_set(tp, CLKREQ_BUG);
Matt Carlson614b0592010-01-20 16:58:02 +000014075 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000014076 tg3_flag_set(tp, L1PLLPD_EN);
Michael Chanc7835a72006-11-15 21:14:42 -080014077 }
Matt Carlson52f44902008-11-21 17:17:04 -080014078 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Jon Mason708ebb32011-06-27 12:56:50 +000014079 /* BCM5785 devices are effectively PCIe devices, and should
14080 * follow PCIe codepaths, but do not have a PCIe capabilities
14081 * section.
14082 */
Joe Perches63c3a662011-04-26 08:12:10 +000014083 tg3_flag_set(tp, PCI_EXPRESS);
14084 } else if (!tg3_flag(tp, 5705_PLUS) ||
14085 tg3_flag(tp, 5780_CLASS)) {
Matt Carlson52f44902008-11-21 17:17:04 -080014086 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
14087 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000014088 dev_err(&tp->pdev->dev,
14089 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080014090 return -EIO;
14091 }
14092
14093 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
Joe Perches63c3a662011-04-26 08:12:10 +000014094 tg3_flag_set(tp, PCIX_MODE);
Matt Carlson52f44902008-11-21 17:17:04 -080014095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014096
Michael Chan399de502005-10-03 14:02:39 -070014097 /* If we have an AMD 762 or VIA K8T800 chipset, write
14098 * reordering to the mailbox registers done by the host
14099 * controller can cause major troubles. We read back from
14100 * every mailbox register write to force the writes to be
14101 * posted to the chip in order.
14102 */
Matt Carlson41434702011-03-09 16:58:22 +000014103 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Joe Perches63c3a662011-04-26 08:12:10 +000014104 !tg3_flag(tp, PCI_EXPRESS))
14105 tg3_flag_set(tp, MBOX_WRITE_REORDER);
Michael Chan399de502005-10-03 14:02:39 -070014106
Matt Carlson69fc4052008-12-21 20:19:57 -080014107 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
14108 &tp->pci_cacheline_sz);
14109 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
14110 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014111 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14112 tp->pci_lat_timer < 64) {
14113 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080014114 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
14115 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014116 }
14117
Matt Carlson16821282011-07-13 09:27:28 +000014118 /* Important! -- It is critical that the PCI-X hw workaround
14119 * situation is decided before the first MMIO register access.
14120 */
Matt Carlson52f44902008-11-21 17:17:04 -080014121 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
14122 /* 5700 BX chips need to have their TX producer index
14123 * mailboxes written twice to workaround a bug.
14124 */
Joe Perches63c3a662011-04-26 08:12:10 +000014125 tg3_flag_set(tp, TXD_MBOX_HWBUG);
Matt Carlson9974a352007-10-07 23:27:28 -070014126
Matt Carlson52f44902008-11-21 17:17:04 -080014127 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014128 *
14129 * The workaround is to use indirect register accesses
14130 * for all chip writes not to mailbox registers.
14131 */
Joe Perches63c3a662011-04-26 08:12:10 +000014132 if (tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014133 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014134
Joe Perches63c3a662011-04-26 08:12:10 +000014135 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014136
14137 /* The chip can have it's power management PCI config
14138 * space registers clobbered due to this bug.
14139 * So explicitly force the chip into D0 here.
14140 */
Matt Carlson9974a352007-10-07 23:27:28 -070014141 pci_read_config_dword(tp->pdev,
14142 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014143 &pm_reg);
14144 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
14145 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070014146 pci_write_config_dword(tp->pdev,
14147 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014148 pm_reg);
14149
14150 /* Also, force SERR#/PERR# in PCI command. */
14151 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
14152 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
14153 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
14154 }
14155 }
14156
Linus Torvalds1da177e2005-04-16 15:20:36 -070014157 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000014158 tg3_flag_set(tp, PCI_HIGH_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014159 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000014160 tg3_flag_set(tp, PCI_32BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014161
14162 /* Chip-specific fixup from Broadcom driver */
14163 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
14164 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
14165 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
14166 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
14167 }
14168
Michael Chan1ee582d2005-08-09 20:16:46 -070014169 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070014170 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070014171 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070014172 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070014173 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070014174 tp->write32_tx_mbox = tg3_write32;
14175 tp->write32_rx_mbox = tg3_write32;
14176
14177 /* Various workaround register access methods */
Joe Perches63c3a662011-04-26 08:12:10 +000014178 if (tg3_flag(tp, PCIX_TARGET_HWBUG))
Michael Chan1ee582d2005-08-09 20:16:46 -070014179 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070014180 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014181 (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson98efd8a2007-05-05 12:47:25 -070014182 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
14183 /*
14184 * Back to back register writes can cause problems on these
14185 * chips, the workaround is to read back all reg writes
14186 * except those to mailbox regs.
14187 *
14188 * See tg3_write_indirect_reg32().
14189 */
Michael Chan1ee582d2005-08-09 20:16:46 -070014190 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070014191 }
14192
Joe Perches63c3a662011-04-26 08:12:10 +000014193 if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
Michael Chan1ee582d2005-08-09 20:16:46 -070014194 tp->write32_tx_mbox = tg3_write32_tx_mbox;
Joe Perches63c3a662011-04-26 08:12:10 +000014195 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Michael Chan1ee582d2005-08-09 20:16:46 -070014196 tp->write32_rx_mbox = tg3_write_flush_reg32;
14197 }
Michael Chan20094932005-08-09 20:16:32 -070014198
Joe Perches63c3a662011-04-26 08:12:10 +000014199 if (tg3_flag(tp, ICH_WORKAROUND)) {
Michael Chan68929142005-08-09 20:17:14 -070014200 tp->read32 = tg3_read_indirect_reg32;
14201 tp->write32 = tg3_write_indirect_reg32;
14202 tp->read32_mbox = tg3_read_indirect_mbox;
14203 tp->write32_mbox = tg3_write_indirect_mbox;
14204 tp->write32_tx_mbox = tg3_write_indirect_mbox;
14205 tp->write32_rx_mbox = tg3_write_indirect_mbox;
14206
14207 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070014208 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070014209
14210 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
14211 pci_cmd &= ~PCI_COMMAND_MEMORY;
14212 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
14213 }
Michael Chanb5d37722006-09-27 16:06:21 -070014214 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
14215 tp->read32_mbox = tg3_read32_mbox_5906;
14216 tp->write32_mbox = tg3_write32_mbox_5906;
14217 tp->write32_tx_mbox = tg3_write32_mbox_5906;
14218 tp->write32_rx_mbox = tg3_write32_mbox_5906;
14219 }
Michael Chan68929142005-08-09 20:17:14 -070014220
Michael Chanbbadf502006-04-06 21:46:34 -070014221 if (tp->write32 == tg3_write_indirect_reg32 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014222 (tg3_flag(tp, PCIX_MODE) &&
Michael Chanbbadf502006-04-06 21:46:34 -070014223 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070014224 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Joe Perches63c3a662011-04-26 08:12:10 +000014225 tg3_flag_set(tp, SRAM_USE_CONFIG);
Michael Chanbbadf502006-04-06 21:46:34 -070014226
Matt Carlson16821282011-07-13 09:27:28 +000014227 /* The memory arbiter has to be enabled in order for SRAM accesses
14228 * to succeed. Normally on powerup the tg3 chip firmware will make
14229 * sure it is enabled, but other entities such as system netboot
14230 * code might disable it.
14231 */
14232 val = tr32(MEMARB_MODE);
14233 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
14234
Matt Carlson69f11c92011-07-13 09:27:30 +000014235 if (tg3_flag(tp, PCIX_MODE)) {
14236 pci_read_config_dword(tp->pdev,
14237 tp->pcix_cap + PCI_X_STATUS, &val);
14238 tp->pci_fn = val & 0x7;
14239 } else {
14240 tp->pci_fn = PCI_FUNC(tp->pdev->devfn) & 3;
14241 }
14242
Michael Chan7d0c41e2005-04-21 17:06:20 -070014243 /* Get eeprom hw config before calling tg3_set_power_state().
Joe Perches63c3a662011-04-26 08:12:10 +000014244 * In particular, the TG3_FLAG_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070014245 * determined before calling tg3_set_power_state() so that
14246 * we know whether or not to switch out of Vaux power.
14247 * When the flag is set, it means that GPIO1 is used for eeprom
14248 * write protect and also implies that it is a LOM where GPIOs
14249 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040014250 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070014251 tg3_get_eeprom_hw_cfg(tp);
14252
Joe Perches63c3a662011-04-26 08:12:10 +000014253 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070014254 /* Allow reads and writes to the
14255 * APE register and memory space.
14256 */
14257 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +000014258 PCISTATE_ALLOW_APE_SHMEM_WR |
14259 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070014260 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
14261 pci_state_reg);
Matt Carlsonc9cab242011-07-13 09:27:27 +000014262
14263 tg3_ape_lock_init(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070014264 }
14265
Matt Carlson9936bcf2007-10-10 18:03:07 -070014266 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson57e69832008-05-25 23:48:31 -070014267 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014268 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014269 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014270 tg3_flag(tp, 57765_PLUS))
14271 tg3_flag_set(tp, CPMU_PRESENT);
Matt Carlsond30cdd22007-10-07 23:28:35 -070014272
Matt Carlson16821282011-07-13 09:27:28 +000014273 /* Set up tp->grc_local_ctrl before calling
14274 * tg3_pwrsrc_switch_to_vmain(). GPIO1 driven high
14275 * will bring 5700's external PHY out of reset.
Michael Chan314fba32005-04-21 17:07:04 -070014276 * It is also used as eeprom write protect on LOMs.
14277 */
14278 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
Matt Carlson6ff6f812011-05-19 12:12:54 +000014279 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014280 tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan314fba32005-04-21 17:07:04 -070014281 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
14282 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070014283 /* Unused GPIO3 must be driven as output on 5752 because there
14284 * are no pull-up resistors on unused GPIO pins.
14285 */
14286 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
14287 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070014288
Matt Carlson321d32a2008-11-21 17:22:19 -080014289 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsoncb4ed1f2010-01-20 16:58:09 +000014290 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
14291 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Michael Chanaf36e6b2006-03-23 01:28:06 -080014292 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
14293
Matt Carlson8d519ab2009-04-20 06:58:01 +000014294 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
14295 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070014296 /* Turn off the debug UART. */
14297 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014298 if (tg3_flag(tp, IS_NIC))
Matt Carlson5f0c4a32008-06-09 15:41:12 -070014299 /* Keep VMain power. */
14300 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
14301 GRC_LCLCTRL_GPIO_OUTPUT0;
14302 }
14303
Matt Carlson16821282011-07-13 09:27:28 +000014304 /* Switch out of Vaux if it is a NIC */
14305 tg3_pwrsrc_switch_to_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014306
Linus Torvalds1da177e2005-04-16 15:20:36 -070014307 /* Derive initial jumbo mode from MTU assigned in
14308 * ether_setup() via the alloc_etherdev() call
14309 */
Joe Perches63c3a662011-04-26 08:12:10 +000014310 if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
14311 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014312
14313 /* Determine WakeOnLan speed to use. */
14314 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14315 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
14316 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
14317 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
Joe Perches63c3a662011-04-26 08:12:10 +000014318 tg3_flag_clear(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014319 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000014320 tg3_flag_set(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014321 }
14322
Matt Carlson7f97a4b2009-08-25 10:10:03 +000014323 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014324 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000014325
Linus Torvalds1da177e2005-04-16 15:20:36 -070014326 /* A few boards don't want Ethernet@WireSpeed phy feature */
Matt Carlson6ff6f812011-05-19 12:12:54 +000014327 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14328 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070014329 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070014330 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014331 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
14332 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
14333 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014334
14335 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
14336 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014337 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014338 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014339 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014340
Joe Perches63c3a662011-04-26 08:12:10 +000014341 if (tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014342 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080014343 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014344 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014345 !tg3_flag(tp, 57765_PLUS)) {
Michael Chanc424cb22006-04-29 18:56:34 -070014346 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070014347 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070014348 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
14349 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080014350 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
14351 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014352 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080014353 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014354 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080014355 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014356 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070014357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014358
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014359 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
14360 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
14361 tp->phy_otp = tg3_read_otp_phycfg(tp);
14362 if (tp->phy_otp == 0)
14363 tp->phy_otp = TG3_OTP_DEFAULT;
14364 }
14365
Joe Perches63c3a662011-04-26 08:12:10 +000014366 if (tg3_flag(tp, CPMU_PRESENT))
Matt Carlson8ef21422008-05-02 16:47:53 -070014367 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
14368 else
14369 tp->mi_mode = MAC_MI_MODE_BASE;
14370
Linus Torvalds1da177e2005-04-16 15:20:36 -070014371 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014372 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
14373 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
14374 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
14375
Matt Carlson4d958472011-04-20 07:57:35 +000014376 /* Set these bits to enable statistics workaround. */
14377 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14378 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
14379 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0) {
14380 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
14381 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
14382 }
14383
Matt Carlson321d32a2008-11-21 17:22:19 -080014384 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
14385 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Joe Perches63c3a662011-04-26 08:12:10 +000014386 tg3_flag_set(tp, USE_PHYLIB);
Matt Carlson57e69832008-05-25 23:48:31 -070014387
Matt Carlson158d7ab2008-05-29 01:37:54 -070014388 err = tg3_mdio_init(tp);
14389 if (err)
14390 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014391
14392 /* Initialize data/descriptor byte/word swapping. */
14393 val = tr32(GRC_MODE);
Matt Carlsonf2096f92011-04-05 14:22:48 +000014394 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
14395 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
14396 GRC_MODE_WORD_SWAP_B2HRX_DATA |
14397 GRC_MODE_B2HRX_ENABLE |
14398 GRC_MODE_HTX2B_ENABLE |
14399 GRC_MODE_HOST_STACKUP);
14400 else
14401 val &= GRC_MODE_HOST_STACKUP;
14402
Linus Torvalds1da177e2005-04-16 15:20:36 -070014403 tw32(GRC_MODE, val | tp->grc_mode);
14404
14405 tg3_switch_clocks(tp);
14406
14407 /* Clear this out for sanity. */
14408 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
14409
14410 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14411 &pci_state_reg);
14412 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014413 !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014414 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
14415
14416 if (chiprevid == CHIPREV_ID_5701_A0 ||
14417 chiprevid == CHIPREV_ID_5701_B0 ||
14418 chiprevid == CHIPREV_ID_5701_B2 ||
14419 chiprevid == CHIPREV_ID_5701_B5) {
14420 void __iomem *sram_base;
14421
14422 /* Write some dummy words into the SRAM status block
14423 * area, see if it reads back correctly. If the return
14424 * value is bad, force enable the PCIX workaround.
14425 */
14426 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
14427
14428 writel(0x00000000, sram_base);
14429 writel(0x00000000, sram_base + 4);
14430 writel(0xffffffff, sram_base + 4);
14431 if (readl(sram_base) != 0x00000000)
Joe Perches63c3a662011-04-26 08:12:10 +000014432 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014433 }
14434 }
14435
14436 udelay(50);
14437 tg3_nvram_init(tp);
14438
14439 grc_misc_cfg = tr32(GRC_MISC_CFG);
14440 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
14441
Linus Torvalds1da177e2005-04-16 15:20:36 -070014442 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14443 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
14444 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
Joe Perches63c3a662011-04-26 08:12:10 +000014445 tg3_flag_set(tp, IS_5788);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014446
Joe Perches63c3a662011-04-26 08:12:10 +000014447 if (!tg3_flag(tp, IS_5788) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +000014448 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014449 tg3_flag_set(tp, TAGGED_STATUS);
14450 if (tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070014451 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
14452 HOSTCC_MODE_CLRTICK_TXBD);
14453
14454 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
14455 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
14456 tp->misc_host_ctrl);
14457 }
14458
Matt Carlson3bda1252008-08-15 14:08:22 -070014459 /* Preserve the APE MAC_MODE bits */
Joe Perches63c3a662011-04-26 08:12:10 +000014460 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +000014461 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070014462 else
Matt Carlson6e01b202011-08-19 13:58:20 +000014463 tp->mac_mode = 0;
Matt Carlson3bda1252008-08-15 14:08:22 -070014464
Linus Torvalds1da177e2005-04-16 15:20:36 -070014465 /* these are limited to 10/100 only */
14466 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14467 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
14468 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14469 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14470 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
14471 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
14472 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
14473 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14474 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
Michael Chan676917d2006-12-07 00:20:22 -080014475 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
14476 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014477 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
Matt Carlsond1101142010-02-17 15:16:55 +000014478 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
14479 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014480 (tp->phy_flags & TG3_PHYFLG_IS_FET))
14481 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014482
14483 err = tg3_phy_probe(tp);
14484 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014485 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014486 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014487 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014488 }
14489
Matt Carlson184b8902010-04-05 10:19:25 +000014490 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080014491 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014492
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014493 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
14494 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014495 } else {
14496 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014497 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014498 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014499 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014500 }
14501
14502 /* 5700 {AX,BX} chips have a broken status block link
14503 * change bit implementation, so we must use the
14504 * status register in those cases.
14505 */
14506 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014507 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014508 else
Joe Perches63c3a662011-04-26 08:12:10 +000014509 tg3_flag_clear(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014510
14511 /* The led_ctrl is set during tg3_phy_probe, here we might
14512 * have to force the link status polling mechanism based
14513 * upon subsystem IDs.
14514 */
14515 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070014516 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014517 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
14518 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Joe Perches63c3a662011-04-26 08:12:10 +000014519 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014520 }
14521
14522 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014523 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Joe Perches63c3a662011-04-26 08:12:10 +000014524 tg3_flag_set(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014525 else
Joe Perches63c3a662011-04-26 08:12:10 +000014526 tg3_flag_clear(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014527
Matt Carlsonbf933c82011-01-25 15:58:49 +000014528 tp->rx_offset = NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014529 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014530 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014531 tg3_flag(tp, PCIX_MODE)) {
Matt Carlsonbf933c82011-01-25 15:58:49 +000014532 tp->rx_offset = 0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014533#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000014534 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014535#endif
14536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014537
Matt Carlson2c49a442010-09-30 10:34:35 +000014538 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
14539 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000014540 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
14541
Matt Carlson2c49a442010-09-30 10:34:35 +000014542 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070014543
14544 /* Increment the rx prod index on the rx std ring by at most
14545 * 8 for these chips to workaround hw errata.
14546 */
14547 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
14548 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
14549 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
14550 tp->rx_std_max_post = 8;
14551
Joe Perches63c3a662011-04-26 08:12:10 +000014552 if (tg3_flag(tp, ASPM_WORKAROUND))
Matt Carlson8ed5d972007-05-07 00:25:49 -070014553 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
14554 PCIE_PWR_MGMT_L1_THRESH_MSK;
14555
Linus Torvalds1da177e2005-04-16 15:20:36 -070014556 return err;
14557}
14558
David S. Miller49b6e95f2007-03-29 01:38:42 -070014559#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014560static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
14561{
14562 struct net_device *dev = tp->dev;
14563 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014564 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070014565 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014566 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014567
David S. Miller49b6e95f2007-03-29 01:38:42 -070014568 addr = of_get_property(dp, "local-mac-address", &len);
14569 if (addr && len == 6) {
14570 memcpy(dev->dev_addr, addr, 6);
14571 memcpy(dev->perm_addr, dev->dev_addr, 6);
14572 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014573 }
14574 return -ENODEV;
14575}
14576
14577static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
14578{
14579 struct net_device *dev = tp->dev;
14580
14581 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070014582 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014583 return 0;
14584}
14585#endif
14586
14587static int __devinit tg3_get_device_address(struct tg3 *tp)
14588{
14589 struct net_device *dev = tp->dev;
14590 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080014591 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014592
David S. Miller49b6e95f2007-03-29 01:38:42 -070014593#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014594 if (!tg3_get_macaddr_sparc(tp))
14595 return 0;
14596#endif
14597
14598 mac_offset = 0x7c;
Matt Carlson6ff6f812011-05-19 12:12:54 +000014599 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014600 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014601 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
14602 mac_offset = 0xcc;
14603 if (tg3_nvram_lock(tp))
14604 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
14605 else
14606 tg3_nvram_unlock(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000014607 } else if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson69f11c92011-07-13 09:27:30 +000014608 if (tp->pci_fn & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014609 mac_offset = 0xcc;
Matt Carlson69f11c92011-07-13 09:27:30 +000014610 if (tp->pci_fn > 1)
Matt Carlsona50d0792010-06-05 17:24:37 +000014611 mac_offset += 0x18c;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014612 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070014613 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014614
14615 /* First try to get it from MAC address mailbox. */
14616 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
14617 if ((hi >> 16) == 0x484b) {
14618 dev->dev_addr[0] = (hi >> 8) & 0xff;
14619 dev->dev_addr[1] = (hi >> 0) & 0xff;
14620
14621 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
14622 dev->dev_addr[2] = (lo >> 24) & 0xff;
14623 dev->dev_addr[3] = (lo >> 16) & 0xff;
14624 dev->dev_addr[4] = (lo >> 8) & 0xff;
14625 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014626
Michael Chan008652b2006-03-27 23:14:53 -080014627 /* Some old bootcode may report a 0 MAC address in SRAM */
14628 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
14629 }
14630 if (!addr_ok) {
14631 /* Next, try NVRAM. */
Joe Perches63c3a662011-04-26 08:12:10 +000014632 if (!tg3_flag(tp, NO_NVRAM) &&
Matt Carlsondf259d82009-04-20 06:57:14 +000014633 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000014634 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070014635 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
14636 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080014637 }
14638 /* Finally just fetch it out of the MAC control regs. */
14639 else {
14640 hi = tr32(MAC_ADDR_0_HIGH);
14641 lo = tr32(MAC_ADDR_0_LOW);
14642
14643 dev->dev_addr[5] = lo & 0xff;
14644 dev->dev_addr[4] = (lo >> 8) & 0xff;
14645 dev->dev_addr[3] = (lo >> 16) & 0xff;
14646 dev->dev_addr[2] = (lo >> 24) & 0xff;
14647 dev->dev_addr[1] = hi & 0xff;
14648 dev->dev_addr[0] = (hi >> 8) & 0xff;
14649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014650 }
14651
14652 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070014653#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014654 if (!tg3_get_default_macaddr_sparc(tp))
14655 return 0;
14656#endif
14657 return -EINVAL;
14658 }
John W. Linville2ff43692005-09-12 14:44:20 -070014659 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014660 return 0;
14661}
14662
David S. Miller59e6b432005-05-18 22:50:10 -070014663#define BOUNDARY_SINGLE_CACHELINE 1
14664#define BOUNDARY_MULTI_CACHELINE 2
14665
14666static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
14667{
14668 int cacheline_size;
14669 u8 byte;
14670 int goal;
14671
14672 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
14673 if (byte == 0)
14674 cacheline_size = 1024;
14675 else
14676 cacheline_size = (int) byte * 4;
14677
14678 /* On 5703 and later chips, the boundary bits have no
14679 * effect.
14680 */
14681 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14682 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014683 !tg3_flag(tp, PCI_EXPRESS))
David S. Miller59e6b432005-05-18 22:50:10 -070014684 goto out;
14685
14686#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
14687 goal = BOUNDARY_MULTI_CACHELINE;
14688#else
14689#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
14690 goal = BOUNDARY_SINGLE_CACHELINE;
14691#else
14692 goal = 0;
14693#endif
14694#endif
14695
Joe Perches63c3a662011-04-26 08:12:10 +000014696 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014697 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
14698 goto out;
14699 }
14700
David S. Miller59e6b432005-05-18 22:50:10 -070014701 if (!goal)
14702 goto out;
14703
14704 /* PCI controllers on most RISC systems tend to disconnect
14705 * when a device tries to burst across a cache-line boundary.
14706 * Therefore, letting tg3 do so just wastes PCI bandwidth.
14707 *
14708 * Unfortunately, for PCI-E there are only limited
14709 * write-side controls for this, and thus for reads
14710 * we will still get the disconnects. We'll also waste
14711 * these PCI cycles for both read and write for chips
14712 * other than 5700 and 5701 which do not implement the
14713 * boundary bits.
14714 */
Joe Perches63c3a662011-04-26 08:12:10 +000014715 if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014716 switch (cacheline_size) {
14717 case 16:
14718 case 32:
14719 case 64:
14720 case 128:
14721 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14722 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
14723 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
14724 } else {
14725 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14726 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14727 }
14728 break;
14729
14730 case 256:
14731 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
14732 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
14733 break;
14734
14735 default:
14736 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14737 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14738 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014739 }
Joe Perches63c3a662011-04-26 08:12:10 +000014740 } else if (tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014741 switch (cacheline_size) {
14742 case 16:
14743 case 32:
14744 case 64:
14745 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14746 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14747 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
14748 break;
14749 }
14750 /* fallthrough */
14751 case 128:
14752 default:
14753 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14754 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
14755 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014756 }
David S. Miller59e6b432005-05-18 22:50:10 -070014757 } else {
14758 switch (cacheline_size) {
14759 case 16:
14760 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14761 val |= (DMA_RWCTRL_READ_BNDRY_16 |
14762 DMA_RWCTRL_WRITE_BNDRY_16);
14763 break;
14764 }
14765 /* fallthrough */
14766 case 32:
14767 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14768 val |= (DMA_RWCTRL_READ_BNDRY_32 |
14769 DMA_RWCTRL_WRITE_BNDRY_32);
14770 break;
14771 }
14772 /* fallthrough */
14773 case 64:
14774 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14775 val |= (DMA_RWCTRL_READ_BNDRY_64 |
14776 DMA_RWCTRL_WRITE_BNDRY_64);
14777 break;
14778 }
14779 /* fallthrough */
14780 case 128:
14781 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14782 val |= (DMA_RWCTRL_READ_BNDRY_128 |
14783 DMA_RWCTRL_WRITE_BNDRY_128);
14784 break;
14785 }
14786 /* fallthrough */
14787 case 256:
14788 val |= (DMA_RWCTRL_READ_BNDRY_256 |
14789 DMA_RWCTRL_WRITE_BNDRY_256);
14790 break;
14791 case 512:
14792 val |= (DMA_RWCTRL_READ_BNDRY_512 |
14793 DMA_RWCTRL_WRITE_BNDRY_512);
14794 break;
14795 case 1024:
14796 default:
14797 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
14798 DMA_RWCTRL_WRITE_BNDRY_1024);
14799 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014800 }
David S. Miller59e6b432005-05-18 22:50:10 -070014801 }
14802
14803out:
14804 return val;
14805}
14806
Linus Torvalds1da177e2005-04-16 15:20:36 -070014807static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
14808{
14809 struct tg3_internal_buffer_desc test_desc;
14810 u32 sram_dma_descs;
14811 int i, ret;
14812
14813 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
14814
14815 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
14816 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
14817 tw32(RDMAC_STATUS, 0);
14818 tw32(WDMAC_STATUS, 0);
14819
14820 tw32(BUFMGR_MODE, 0);
14821 tw32(FTQ_RESET, 0);
14822
14823 test_desc.addr_hi = ((u64) buf_dma) >> 32;
14824 test_desc.addr_lo = buf_dma & 0xffffffff;
14825 test_desc.nic_mbuf = 0x00002100;
14826 test_desc.len = size;
14827
14828 /*
14829 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
14830 * the *second* time the tg3 driver was getting loaded after an
14831 * initial scan.
14832 *
14833 * Broadcom tells me:
14834 * ...the DMA engine is connected to the GRC block and a DMA
14835 * reset may affect the GRC block in some unpredictable way...
14836 * The behavior of resets to individual blocks has not been tested.
14837 *
14838 * Broadcom noted the GRC reset will also reset all sub-components.
14839 */
14840 if (to_device) {
14841 test_desc.cqid_sqid = (13 << 8) | 2;
14842
14843 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
14844 udelay(40);
14845 } else {
14846 test_desc.cqid_sqid = (16 << 8) | 7;
14847
14848 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
14849 udelay(40);
14850 }
14851 test_desc.flags = 0x00000005;
14852
14853 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
14854 u32 val;
14855
14856 val = *(((u32 *)&test_desc) + i);
14857 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
14858 sram_dma_descs + (i * sizeof(u32)));
14859 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
14860 }
14861 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
14862
Matt Carlson859a5882010-04-05 10:19:28 +000014863 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014864 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a5882010-04-05 10:19:28 +000014865 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070014866 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014867
14868 ret = -ENODEV;
14869 for (i = 0; i < 40; i++) {
14870 u32 val;
14871
14872 if (to_device)
14873 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
14874 else
14875 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
14876 if ((val & 0xffff) == sram_dma_descs) {
14877 ret = 0;
14878 break;
14879 }
14880
14881 udelay(100);
14882 }
14883
14884 return ret;
14885}
14886
David S. Millerded73402005-05-23 13:59:47 -070014887#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070014888
Matt Carlson41434702011-03-09 16:58:22 +000014889static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080014890 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
14891 { },
14892};
14893
Linus Torvalds1da177e2005-04-16 15:20:36 -070014894static int __devinit tg3_test_dma(struct tg3 *tp)
14895{
14896 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070014897 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014898 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014899
Matt Carlson4bae65c2010-11-24 08:31:52 +000014900 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
14901 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014902 if (!buf) {
14903 ret = -ENOMEM;
14904 goto out_nofree;
14905 }
14906
14907 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
14908 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
14909
David S. Miller59e6b432005-05-18 22:50:10 -070014910 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014911
Joe Perches63c3a662011-04-26 08:12:10 +000014912 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014913 goto out;
14914
Joe Perches63c3a662011-04-26 08:12:10 +000014915 if (tg3_flag(tp, PCI_EXPRESS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014916 /* DMA read watermark not used on PCIE */
14917 tp->dma_rwctrl |= 0x00180000;
Joe Perches63c3a662011-04-26 08:12:10 +000014918 } else if (!tg3_flag(tp, PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070014919 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
14920 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014921 tp->dma_rwctrl |= 0x003f0000;
14922 else
14923 tp->dma_rwctrl |= 0x003f000f;
14924 } else {
14925 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14926 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
14927 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080014928 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014929
Michael Chan4a29cc22006-03-19 13:21:12 -080014930 /* If the 5704 is behind the EPB bridge, we can
14931 * do the less restrictive ONE_DMA workaround for
14932 * better performance.
14933 */
Joe Perches63c3a662011-04-26 08:12:10 +000014934 if (tg3_flag(tp, 40BIT_DMA_BUG) &&
Michael Chan4a29cc22006-03-19 13:21:12 -080014935 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14936 tp->dma_rwctrl |= 0x8000;
14937 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014938 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
14939
Michael Chan49afdeb2007-02-13 12:17:03 -080014940 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
14941 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070014942 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080014943 tp->dma_rwctrl |=
14944 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
14945 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
14946 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070014947 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
14948 /* 5780 always in PCIX mode */
14949 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070014950 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
14951 /* 5714 always in PCIX mode */
14952 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014953 } else {
14954 tp->dma_rwctrl |= 0x001b000f;
14955 }
14956 }
14957
14958 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14959 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14960 tp->dma_rwctrl &= 0xfffffff0;
14961
14962 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14963 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
14964 /* Remove this if it causes problems for some boards. */
14965 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
14966
14967 /* On 5700/5701 chips, we need to set this bit.
14968 * Otherwise the chip will issue cacheline transactions
14969 * to streamable DMA memory with not all the byte
14970 * enables turned on. This is an error on several
14971 * RISC PCI controllers, in particular sparc64.
14972 *
14973 * On 5703/5704 chips, this bit has been reassigned
14974 * a different meaning. In particular, it is used
14975 * on those chips to enable a PCI-X workaround.
14976 */
14977 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
14978 }
14979
14980 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14981
14982#if 0
14983 /* Unneeded, already done by tg3_get_invariants. */
14984 tg3_switch_clocks(tp);
14985#endif
14986
Linus Torvalds1da177e2005-04-16 15:20:36 -070014987 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14988 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
14989 goto out;
14990
David S. Miller59e6b432005-05-18 22:50:10 -070014991 /* It is best to perform DMA test with maximum write burst size
14992 * to expose the 5700/5701 write DMA bug.
14993 */
14994 saved_dma_rwctrl = tp->dma_rwctrl;
14995 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14996 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14997
Linus Torvalds1da177e2005-04-16 15:20:36 -070014998 while (1) {
14999 u32 *p = buf, i;
15000
15001 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
15002 p[i] = i;
15003
15004 /* Send the buffer to the chip. */
15005 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
15006 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000015007 dev_err(&tp->pdev->dev,
15008 "%s: Buffer write failed. err = %d\n",
15009 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015010 break;
15011 }
15012
15013#if 0
15014 /* validate data reached card RAM correctly. */
15015 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
15016 u32 val;
15017 tg3_read_mem(tp, 0x2100 + (i*4), &val);
15018 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000015019 dev_err(&tp->pdev->dev,
15020 "%s: Buffer corrupted on device! "
15021 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015022 /* ret = -ENODEV here? */
15023 }
15024 p[i] = 0;
15025 }
15026#endif
15027 /* Now read it back. */
15028 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
15029 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000015030 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
15031 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015032 break;
15033 }
15034
15035 /* Verify it. */
15036 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
15037 if (p[i] == i)
15038 continue;
15039
David S. Miller59e6b432005-05-18 22:50:10 -070015040 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
15041 DMA_RWCTRL_WRITE_BNDRY_16) {
15042 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015043 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
15044 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15045 break;
15046 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000015047 dev_err(&tp->pdev->dev,
15048 "%s: Buffer corrupted on read back! "
15049 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015050 ret = -ENODEV;
15051 goto out;
15052 }
15053 }
15054
15055 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
15056 /* Success. */
15057 ret = 0;
15058 break;
15059 }
15060 }
David S. Miller59e6b432005-05-18 22:50:10 -070015061 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
15062 DMA_RWCTRL_WRITE_BNDRY_16) {
15063 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070015064 * now look for chipsets that are known to expose the
15065 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070015066 */
Matt Carlson41434702011-03-09 16:58:22 +000015067 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070015068 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
15069 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a5882010-04-05 10:19:28 +000015070 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070015071 /* Safe to use the calculated DMA boundary. */
15072 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a5882010-04-05 10:19:28 +000015073 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070015074
David S. Miller59e6b432005-05-18 22:50:10 -070015075 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015077
15078out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000015079 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015080out_nofree:
15081 return ret;
15082}
15083
Linus Torvalds1da177e2005-04-16 15:20:36 -070015084static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
15085{
Joe Perches63c3a662011-04-26 08:12:10 +000015086 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson666bc832010-01-20 16:58:03 +000015087 tp->bufmgr_config.mbuf_read_dma_low_water =
15088 DEFAULT_MB_RDMA_LOW_WATER_5705;
15089 tp->bufmgr_config.mbuf_mac_rx_low_water =
15090 DEFAULT_MB_MACRX_LOW_WATER_57765;
15091 tp->bufmgr_config.mbuf_high_water =
15092 DEFAULT_MB_HIGH_WATER_57765;
15093
15094 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15095 DEFAULT_MB_RDMA_LOW_WATER_5705;
15096 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15097 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
15098 tp->bufmgr_config.mbuf_high_water_jumbo =
15099 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000015100 } else if (tg3_flag(tp, 5705_PLUS)) {
Michael Chanfdfec172005-07-25 12:31:48 -070015101 tp->bufmgr_config.mbuf_read_dma_low_water =
15102 DEFAULT_MB_RDMA_LOW_WATER_5705;
15103 tp->bufmgr_config.mbuf_mac_rx_low_water =
15104 DEFAULT_MB_MACRX_LOW_WATER_5705;
15105 tp->bufmgr_config.mbuf_high_water =
15106 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070015107 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
15108 tp->bufmgr_config.mbuf_mac_rx_low_water =
15109 DEFAULT_MB_MACRX_LOW_WATER_5906;
15110 tp->bufmgr_config.mbuf_high_water =
15111 DEFAULT_MB_HIGH_WATER_5906;
15112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015113
Michael Chanfdfec172005-07-25 12:31:48 -070015114 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15115 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
15116 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15117 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
15118 tp->bufmgr_config.mbuf_high_water_jumbo =
15119 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
15120 } else {
15121 tp->bufmgr_config.mbuf_read_dma_low_water =
15122 DEFAULT_MB_RDMA_LOW_WATER;
15123 tp->bufmgr_config.mbuf_mac_rx_low_water =
15124 DEFAULT_MB_MACRX_LOW_WATER;
15125 tp->bufmgr_config.mbuf_high_water =
15126 DEFAULT_MB_HIGH_WATER;
15127
15128 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15129 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
15130 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15131 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
15132 tp->bufmgr_config.mbuf_high_water_jumbo =
15133 DEFAULT_MB_HIGH_WATER_JUMBO;
15134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015135
15136 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
15137 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
15138}
15139
15140static char * __devinit tg3_phy_string(struct tg3 *tp)
15141{
Matt Carlson79eb6902010-02-17 15:17:03 +000015142 switch (tp->phy_id & TG3_PHY_ID_MASK) {
15143 case TG3_PHY_ID_BCM5400: return "5400";
15144 case TG3_PHY_ID_BCM5401: return "5401";
15145 case TG3_PHY_ID_BCM5411: return "5411";
15146 case TG3_PHY_ID_BCM5701: return "5701";
15147 case TG3_PHY_ID_BCM5703: return "5703";
15148 case TG3_PHY_ID_BCM5704: return "5704";
15149 case TG3_PHY_ID_BCM5705: return "5705";
15150 case TG3_PHY_ID_BCM5750: return "5750";
15151 case TG3_PHY_ID_BCM5752: return "5752";
15152 case TG3_PHY_ID_BCM5714: return "5714";
15153 case TG3_PHY_ID_BCM5780: return "5780";
15154 case TG3_PHY_ID_BCM5755: return "5755";
15155 case TG3_PHY_ID_BCM5787: return "5787";
15156 case TG3_PHY_ID_BCM5784: return "5784";
15157 case TG3_PHY_ID_BCM5756: return "5722/5756";
15158 case TG3_PHY_ID_BCM5906: return "5906";
15159 case TG3_PHY_ID_BCM5761: return "5761";
15160 case TG3_PHY_ID_BCM5718C: return "5718C";
15161 case TG3_PHY_ID_BCM5718S: return "5718S";
15162 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000015163 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000015164 case TG3_PHY_ID_BCM5720C: return "5720C";
Matt Carlson79eb6902010-02-17 15:17:03 +000015165 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070015166 case 0: return "serdes";
15167 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070015168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015169}
15170
Michael Chanf9804dd2005-09-27 12:13:10 -070015171static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
15172{
Joe Perches63c3a662011-04-26 08:12:10 +000015173 if (tg3_flag(tp, PCI_EXPRESS)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070015174 strcpy(str, "PCI Express");
15175 return str;
Joe Perches63c3a662011-04-26 08:12:10 +000015176 } else if (tg3_flag(tp, PCIX_MODE)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070015177 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
15178
15179 strcpy(str, "PCIX:");
15180
15181 if ((clock_ctrl == 7) ||
15182 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
15183 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
15184 strcat(str, "133MHz");
15185 else if (clock_ctrl == 0)
15186 strcat(str, "33MHz");
15187 else if (clock_ctrl == 2)
15188 strcat(str, "50MHz");
15189 else if (clock_ctrl == 4)
15190 strcat(str, "66MHz");
15191 else if (clock_ctrl == 6)
15192 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070015193 } else {
15194 strcpy(str, "PCI:");
Joe Perches63c3a662011-04-26 08:12:10 +000015195 if (tg3_flag(tp, PCI_HIGH_SPEED))
Michael Chanf9804dd2005-09-27 12:13:10 -070015196 strcat(str, "66MHz");
15197 else
15198 strcat(str, "33MHz");
15199 }
Joe Perches63c3a662011-04-26 08:12:10 +000015200 if (tg3_flag(tp, PCI_32BIT))
Michael Chanf9804dd2005-09-27 12:13:10 -070015201 strcat(str, ":32-bit");
15202 else
15203 strcat(str, ":64-bit");
15204 return str;
15205}
15206
Michael Chan8c2dc7e2005-12-19 16:26:02 -080015207static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015208{
15209 struct pci_dev *peer;
15210 unsigned int func, devnr = tp->pdev->devfn & ~7;
15211
15212 for (func = 0; func < 8; func++) {
15213 peer = pci_get_slot(tp->pdev->bus, devnr | func);
15214 if (peer && peer != tp->pdev)
15215 break;
15216 pci_dev_put(peer);
15217 }
Michael Chan16fe9d72005-12-13 21:09:54 -080015218 /* 5704 can be configured in single-port mode, set peer to
15219 * tp->pdev in that case.
15220 */
15221 if (!peer) {
15222 peer = tp->pdev;
15223 return peer;
15224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015225
15226 /*
15227 * We don't need to keep the refcount elevated; there's no way
15228 * to remove one half of this device without removing the other
15229 */
15230 pci_dev_put(peer);
15231
15232 return peer;
15233}
15234
David S. Miller15f98502005-05-18 22:49:26 -070015235static void __devinit tg3_init_coal(struct tg3 *tp)
15236{
15237 struct ethtool_coalesce *ec = &tp->coal;
15238
15239 memset(ec, 0, sizeof(*ec));
15240 ec->cmd = ETHTOOL_GCOALESCE;
15241 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
15242 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
15243 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
15244 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
15245 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
15246 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
15247 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
15248 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
15249 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
15250
15251 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
15252 HOSTCC_MODE_CLRTICK_TXBD)) {
15253 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
15254 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
15255 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
15256 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
15257 }
Michael Chand244c892005-07-05 14:42:33 -070015258
Joe Perches63c3a662011-04-26 08:12:10 +000015259 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070015260 ec->rx_coalesce_usecs_irq = 0;
15261 ec->tx_coalesce_usecs_irq = 0;
15262 ec->stats_block_coalesce_usecs = 0;
15263 }
David S. Miller15f98502005-05-18 22:49:26 -070015264}
15265
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080015266static const struct net_device_ops tg3_netdev_ops = {
15267 .ndo_open = tg3_open,
15268 .ndo_stop = tg3_close,
Stephen Hemminger00829822008-11-20 20:14:53 -080015269 .ndo_start_xmit = tg3_start_xmit,
Eric Dumazet511d2222010-07-07 20:44:24 +000015270 .ndo_get_stats64 = tg3_get_stats64,
Stephen Hemminger00829822008-11-20 20:14:53 -080015271 .ndo_validate_addr = eth_validate_addr,
Jiri Pirkoafc4b132011-08-16 06:29:01 +000015272 .ndo_set_rx_mode = tg3_set_rx_mode,
Stephen Hemminger00829822008-11-20 20:14:53 -080015273 .ndo_set_mac_address = tg3_set_mac_addr,
15274 .ndo_do_ioctl = tg3_ioctl,
15275 .ndo_tx_timeout = tg3_tx_timeout,
15276 .ndo_change_mtu = tg3_change_mtu,
Michał Mirosławdc668912011-04-07 03:35:07 +000015277 .ndo_fix_features = tg3_fix_features,
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015278 .ndo_set_features = tg3_set_features,
Stephen Hemminger00829822008-11-20 20:14:53 -080015279#ifdef CONFIG_NET_POLL_CONTROLLER
15280 .ndo_poll_controller = tg3_poll_controller,
15281#endif
15282};
15283
Linus Torvalds1da177e2005-04-16 15:20:36 -070015284static int __devinit tg3_init_one(struct pci_dev *pdev,
15285 const struct pci_device_id *ent)
15286{
Linus Torvalds1da177e2005-04-16 15:20:36 -070015287 struct net_device *dev;
15288 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000015289 int i, err, pm_cap;
15290 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070015291 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080015292 u64 dma_mask, persist_dma_mask;
Matt Carlson0da06062011-05-19 12:12:53 +000015293 u32 features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015294
Joe Perches05dbe002010-02-17 19:44:19 +000015295 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015296
15297 err = pci_enable_device(pdev);
15298 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015299 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015300 return err;
15301 }
15302
Linus Torvalds1da177e2005-04-16 15:20:36 -070015303 err = pci_request_regions(pdev, DRV_MODULE_NAME);
15304 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015305 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015306 goto err_out_disable_pdev;
15307 }
15308
15309 pci_set_master(pdev);
15310
15311 /* Find power-management capability. */
15312 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
15313 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000015314 dev_err(&pdev->dev,
15315 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015316 err = -EIO;
15317 goto err_out_free_res;
15318 }
15319
Matt Carlson16821282011-07-13 09:27:28 +000015320 err = pci_set_power_state(pdev, PCI_D0);
15321 if (err) {
15322 dev_err(&pdev->dev, "Transition to D0 failed, aborting\n");
15323 goto err_out_free_res;
15324 }
15325
Matt Carlsonfe5f5782009-09-01 13:09:39 +000015326 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015327 if (!dev) {
Matt Carlson2445e462010-04-05 10:19:21 +000015328 dev_err(&pdev->dev, "Etherdev alloc failed, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015329 err = -ENOMEM;
Matt Carlson16821282011-07-13 09:27:28 +000015330 goto err_out_power_down;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015331 }
15332
Linus Torvalds1da177e2005-04-16 15:20:36 -070015333 SET_NETDEV_DEV(dev, &pdev->dev);
15334
Linus Torvalds1da177e2005-04-16 15:20:36 -070015335 tp = netdev_priv(dev);
15336 tp->pdev = pdev;
15337 tp->dev = dev;
15338 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015339 tp->rx_mode = TG3_DEF_RX_MODE;
15340 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070015341
Linus Torvalds1da177e2005-04-16 15:20:36 -070015342 if (tg3_debug > 0)
15343 tp->msg_enable = tg3_debug;
15344 else
15345 tp->msg_enable = TG3_DEF_MSG_ENABLE;
15346
15347 /* The word/byte swap controls here control register access byte
15348 * swapping. DMA data byte swapping is controlled in the GRC_MODE
15349 * setting below.
15350 */
15351 tp->misc_host_ctrl =
15352 MISC_HOST_CTRL_MASK_PCI_INT |
15353 MISC_HOST_CTRL_WORD_SWAP |
15354 MISC_HOST_CTRL_INDIR_ACCESS |
15355 MISC_HOST_CTRL_PCISTATE_RW;
15356
15357 /* The NONFRM (non-frame) byte/word swap controls take effect
15358 * on descriptor entries, anything which isn't packet data.
15359 *
15360 * The StrongARM chips on the board (one for tx, one for rx)
15361 * are running in big-endian mode.
15362 */
15363 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
15364 GRC_MODE_WSWAP_NONFRM_DATA);
15365#ifdef __BIG_ENDIAN
15366 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
15367#endif
15368 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015369 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000015370 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015371
Matt Carlsond5fe4882008-11-21 17:20:32 -080015372 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010015373 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015374 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015375 err = -ENOMEM;
15376 goto err_out_free_dev;
15377 }
15378
Matt Carlsonc9cab242011-07-13 09:27:27 +000015379 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
15380 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761E ||
15381 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S ||
15382 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761SE ||
15383 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
15384 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
15385 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
15386 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720) {
15387 tg3_flag_set(tp, ENABLE_APE);
15388 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
15389 if (!tp->aperegs) {
15390 dev_err(&pdev->dev,
15391 "Cannot map APE registers, aborting\n");
15392 err = -ENOMEM;
15393 goto err_out_iounmap;
15394 }
15395 }
15396
Linus Torvalds1da177e2005-04-16 15:20:36 -070015397 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
15398 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015399
Linus Torvalds1da177e2005-04-16 15:20:36 -070015400 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015401 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Matt Carlson2ffcc982011-05-19 12:12:44 +000015402 dev->netdev_ops = &tg3_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015403 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015404
15405 err = tg3_get_invariants(tp);
15406 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015407 dev_err(&pdev->dev,
15408 "Problem fetching invariants of chip, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015409 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015410 }
15411
Michael Chan4a29cc22006-03-19 13:21:12 -080015412 /* The EPB bridge inside 5714, 5715, and 5780 and any
15413 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080015414 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
15415 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
15416 * do DMA address check in tg3_start_xmit().
15417 */
Joe Perches63c3a662011-04-26 08:12:10 +000015418 if (tg3_flag(tp, IS_5788))
Yang Hongyang284901a2009-04-06 19:01:15 -070015419 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Joe Perches63c3a662011-04-26 08:12:10 +000015420 else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070015421 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080015422#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070015423 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015424#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080015425 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070015426 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015427
15428 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070015429 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080015430 err = pci_set_dma_mask(pdev, dma_mask);
15431 if (!err) {
Matt Carlson0da06062011-05-19 12:12:53 +000015432 features |= NETIF_F_HIGHDMA;
Michael Chan72f2afb2006-03-06 19:28:35 -080015433 err = pci_set_consistent_dma_mask(pdev,
15434 persist_dma_mask);
15435 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015436 dev_err(&pdev->dev, "Unable to obtain 64 bit "
15437 "DMA for consistent allocations\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015438 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080015439 }
15440 }
15441 }
Yang Hongyang284901a2009-04-06 19:01:15 -070015442 if (err || dma_mask == DMA_BIT_MASK(32)) {
15443 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080015444 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015445 dev_err(&pdev->dev,
15446 "No usable DMA configuration, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015447 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080015448 }
15449 }
15450
Michael Chanfdfec172005-07-25 12:31:48 -070015451 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015452
Matt Carlson0da06062011-05-19 12:12:53 +000015453 features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
15454
15455 /* 5700 B0 chips do not support checksumming correctly due
15456 * to hardware bugs.
15457 */
15458 if (tp->pci_chip_rev_id != CHIPREV_ID_5700_B0) {
15459 features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
15460
15461 if (tg3_flag(tp, 5755_PLUS))
15462 features |= NETIF_F_IPV6_CSUM;
15463 }
15464
Michael Chan4e3a7aa2006-03-20 17:47:44 -080015465 /* TSO is on by default on chips that support hardware TSO.
15466 * Firmware TSO on older chips gives lower performance, so it
15467 * is off by default, but can be enabled using ethtool.
15468 */
Joe Perches63c3a662011-04-26 08:12:10 +000015469 if ((tg3_flag(tp, HW_TSO_1) ||
15470 tg3_flag(tp, HW_TSO_2) ||
15471 tg3_flag(tp, HW_TSO_3)) &&
Matt Carlson0da06062011-05-19 12:12:53 +000015472 (features & NETIF_F_IP_CSUM))
15473 features |= NETIF_F_TSO;
Joe Perches63c3a662011-04-26 08:12:10 +000015474 if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
Matt Carlson0da06062011-05-19 12:12:53 +000015475 if (features & NETIF_F_IPV6_CSUM)
15476 features |= NETIF_F_TSO6;
Joe Perches63c3a662011-04-26 08:12:10 +000015477 if (tg3_flag(tp, HW_TSO_3) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000015478 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070015479 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
15480 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Joe Perches63c3a662011-04-26 08:12:10 +000015481 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Michał Mirosławdc668912011-04-07 03:35:07 +000015482 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson0da06062011-05-19 12:12:53 +000015483 features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070015484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015485
Matt Carlsond542fe22011-05-19 16:02:43 +000015486 dev->features |= features;
15487 dev->vlan_features |= features;
15488
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015489 /*
15490 * Add loopback capability only for a subset of devices that support
15491 * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
15492 * loopback for the remaining devices.
15493 */
15494 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
15495 !tg3_flag(tp, CPMU_PRESENT))
15496 /* Add the loopback capability */
Matt Carlson0da06062011-05-19 12:12:53 +000015497 features |= NETIF_F_LOOPBACK;
15498
Matt Carlson0da06062011-05-19 12:12:53 +000015499 dev->hw_features |= features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015500
Linus Torvalds1da177e2005-04-16 15:20:36 -070015501 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015502 !tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015503 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015504 tg3_flag_set(tp, MAX_RXPEND_64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015505 tp->rx_pending = 63;
15506 }
15507
Linus Torvalds1da177e2005-04-16 15:20:36 -070015508 err = tg3_get_device_address(tp);
15509 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015510 dev_err(&pdev->dev,
15511 "Could not obtain valid ethernet address, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015512 goto err_out_apeunmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070015513 }
15514
Matt Carlsonc88864d2007-11-12 21:07:01 -080015515 /*
15516 * Reset chip in case UNDI or EFI driver did not shutdown
15517 * DMA self test will enable WDMAC and we'll see (spurious)
15518 * pending DMA on the PCI bus at that point.
15519 */
15520 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
15521 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
15522 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
15523 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
15524 }
15525
15526 err = tg3_test_dma(tp);
15527 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015528 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080015529 goto err_out_apeunmap;
15530 }
15531
Matt Carlson78f90dc2009-11-13 13:03:42 +000015532 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
15533 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
15534 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000015535 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000015536 struct tg3_napi *tnapi = &tp->napi[i];
15537
15538 tnapi->tp = tp;
15539 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
15540
15541 tnapi->int_mbox = intmbx;
15542 if (i < 4)
15543 intmbx += 0x8;
15544 else
15545 intmbx += 0x4;
15546
15547 tnapi->consmbox = rcvmbx;
15548 tnapi->prodmbox = sndmbx;
15549
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015550 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000015551 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015552 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000015553 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000015554
Joe Perches63c3a662011-04-26 08:12:10 +000015555 if (!tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson78f90dc2009-11-13 13:03:42 +000015556 break;
15557
15558 /*
15559 * If we support MSIX, we'll be using RSS. If we're using
15560 * RSS, the first vector only handles link interrupts and the
15561 * remaining vectors handle rx and tx interrupts. Reuse the
15562 * mailbox values for the next iteration. The values we setup
15563 * above are still useful for the single vectored mode.
15564 */
15565 if (!i)
15566 continue;
15567
15568 rcvmbx += 0x8;
15569
15570 if (sndmbx & 0x4)
15571 sndmbx -= 0x4;
15572 else
15573 sndmbx += 0xc;
15574 }
15575
Matt Carlsonc88864d2007-11-12 21:07:01 -080015576 tg3_init_coal(tp);
15577
Michael Chanc49a1562006-12-17 17:07:29 -080015578 pci_set_drvdata(pdev, dev);
15579
Matt Carlsoncd0d7222011-07-13 09:27:33 +000015580 if (tg3_flag(tp, 5717_PLUS)) {
15581 /* Resume a low-power mode */
15582 tg3_frob_aux_power(tp, false);
15583 }
15584
Linus Torvalds1da177e2005-04-16 15:20:36 -070015585 err = register_netdev(dev);
15586 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015587 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070015588 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015589 }
15590
Joe Perches05dbe002010-02-17 19:44:19 +000015591 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
15592 tp->board_part_number,
15593 tp->pci_chip_rev_id,
15594 tg3_bus_string(tp, str),
15595 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015596
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015597 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000015598 struct phy_device *phydev;
15599 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000015600 netdev_info(dev,
15601 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000015602 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015603 } else {
15604 char *ethtype;
15605
15606 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
15607 ethtype = "10/100Base-TX";
15608 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
15609 ethtype = "1000Base-SX";
15610 else
15611 ethtype = "10/100/1000Base-T";
15612
Matt Carlson5129c3a2010-04-05 10:19:23 +000015613 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlson47007832011-04-20 07:57:43 +000015614 "(WireSpeed[%d], EEE[%d])\n",
15615 tg3_phy_string(tp), ethtype,
15616 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
15617 (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015618 }
Matt Carlsondf59c942008-11-03 16:52:56 -080015619
Joe Perches05dbe002010-02-17 19:44:19 +000015620 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000015621 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015622 tg3_flag(tp, USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015623 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015624 tg3_flag(tp, ENABLE_ASF) != 0,
15625 tg3_flag(tp, TSO_CAPABLE) != 0);
Joe Perches05dbe002010-02-17 19:44:19 +000015626 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
15627 tp->dma_rwctrl,
15628 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
15629 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015630
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015631 pci_save_state(pdev);
15632
Linus Torvalds1da177e2005-04-16 15:20:36 -070015633 return 0;
15634
Matt Carlson0d3031d2007-10-10 18:02:43 -070015635err_out_apeunmap:
15636 if (tp->aperegs) {
15637 iounmap(tp->aperegs);
15638 tp->aperegs = NULL;
15639 }
15640
Linus Torvalds1da177e2005-04-16 15:20:36 -070015641err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070015642 if (tp->regs) {
15643 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015644 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015646
15647err_out_free_dev:
15648 free_netdev(dev);
15649
Matt Carlson16821282011-07-13 09:27:28 +000015650err_out_power_down:
15651 pci_set_power_state(pdev, PCI_D3hot);
15652
Linus Torvalds1da177e2005-04-16 15:20:36 -070015653err_out_free_res:
15654 pci_release_regions(pdev);
15655
15656err_out_disable_pdev:
15657 pci_disable_device(pdev);
15658 pci_set_drvdata(pdev, NULL);
15659 return err;
15660}
15661
15662static void __devexit tg3_remove_one(struct pci_dev *pdev)
15663{
15664 struct net_device *dev = pci_get_drvdata(pdev);
15665
15666 if (dev) {
15667 struct tg3 *tp = netdev_priv(dev);
15668
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080015669 if (tp->fw)
15670 release_firmware(tp->fw);
15671
Tejun Heo23f333a2010-12-12 16:45:14 +010015672 cancel_work_sync(&tp->reset_task);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015673
Joe Perches63c3a662011-04-26 08:12:10 +000015674 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015675 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015676 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015677 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070015678
Linus Torvalds1da177e2005-04-16 15:20:36 -070015679 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015680 if (tp->aperegs) {
15681 iounmap(tp->aperegs);
15682 tp->aperegs = NULL;
15683 }
Michael Chan68929142005-08-09 20:17:14 -070015684 if (tp->regs) {
15685 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015686 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015688 free_netdev(dev);
15689 pci_release_regions(pdev);
15690 pci_disable_device(pdev);
15691 pci_set_drvdata(pdev, NULL);
15692 }
15693}
15694
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015695#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015696static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015697{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015698 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015699 struct net_device *dev = pci_get_drvdata(pdev);
15700 struct tg3 *tp = netdev_priv(dev);
15701 int err;
15702
15703 if (!netif_running(dev))
15704 return 0;
15705
Tejun Heo23f333a2010-12-12 16:45:14 +010015706 flush_work_sync(&tp->reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015707 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015708 tg3_netif_stop(tp);
15709
15710 del_timer_sync(&tp->timer);
15711
David S. Millerf47c11e2005-06-24 20:18:35 -070015712 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015713 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070015714 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015715
15716 netif_device_detach(dev);
15717
David S. Millerf47c11e2005-06-24 20:18:35 -070015718 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070015719 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches63c3a662011-04-26 08:12:10 +000015720 tg3_flag_clear(tp, INIT_COMPLETE);
David S. Millerf47c11e2005-06-24 20:18:35 -070015721 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015722
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015723 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015724 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015725 int err2;
15726
David S. Millerf47c11e2005-06-24 20:18:35 -070015727 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015728
Joe Perches63c3a662011-04-26 08:12:10 +000015729 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015730 err2 = tg3_restart_hw(tp, 1);
15731 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070015732 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015733
15734 tp->timer.expires = jiffies + tp->timer_offset;
15735 add_timer(&tp->timer);
15736
15737 netif_device_attach(dev);
15738 tg3_netif_start(tp);
15739
Michael Chanb9ec6c12006-07-25 16:37:27 -070015740out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015741 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015742
15743 if (!err2)
15744 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015745 }
15746
15747 return err;
15748}
15749
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015750static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015751{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015752 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015753 struct net_device *dev = pci_get_drvdata(pdev);
15754 struct tg3 *tp = netdev_priv(dev);
15755 int err;
15756
15757 if (!netif_running(dev))
15758 return 0;
15759
Linus Torvalds1da177e2005-04-16 15:20:36 -070015760 netif_device_attach(dev);
15761
David S. Millerf47c11e2005-06-24 20:18:35 -070015762 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015763
Joe Perches63c3a662011-04-26 08:12:10 +000015764 tg3_flag_set(tp, INIT_COMPLETE);
Michael Chanb9ec6c12006-07-25 16:37:27 -070015765 err = tg3_restart_hw(tp, 1);
15766 if (err)
15767 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015768
15769 tp->timer.expires = jiffies + tp->timer_offset;
15770 add_timer(&tp->timer);
15771
Linus Torvalds1da177e2005-04-16 15:20:36 -070015772 tg3_netif_start(tp);
15773
Michael Chanb9ec6c12006-07-25 16:37:27 -070015774out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015775 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015776
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015777 if (!err)
15778 tg3_phy_start(tp);
15779
Michael Chanb9ec6c12006-07-25 16:37:27 -070015780 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015781}
15782
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015783static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015784#define TG3_PM_OPS (&tg3_pm_ops)
15785
15786#else
15787
15788#define TG3_PM_OPS NULL
15789
15790#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015791
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015792/**
15793 * tg3_io_error_detected - called when PCI error is detected
15794 * @pdev: Pointer to PCI device
15795 * @state: The current pci connection state
15796 *
15797 * This function is called after a PCI bus error affecting
15798 * this device has been detected.
15799 */
15800static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
15801 pci_channel_state_t state)
15802{
15803 struct net_device *netdev = pci_get_drvdata(pdev);
15804 struct tg3 *tp = netdev_priv(netdev);
15805 pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
15806
15807 netdev_info(netdev, "PCI I/O error detected\n");
15808
15809 rtnl_lock();
15810
15811 if (!netif_running(netdev))
15812 goto done;
15813
15814 tg3_phy_stop(tp);
15815
15816 tg3_netif_stop(tp);
15817
15818 del_timer_sync(&tp->timer);
Joe Perches63c3a662011-04-26 08:12:10 +000015819 tg3_flag_clear(tp, RESTART_TIMER);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015820
15821 /* Want to make sure that the reset task doesn't run */
15822 cancel_work_sync(&tp->reset_task);
Joe Perches63c3a662011-04-26 08:12:10 +000015823 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
15824 tg3_flag_clear(tp, RESTART_TIMER);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015825
15826 netif_device_detach(netdev);
15827
15828 /* Clean up software state, even if MMIO is blocked */
15829 tg3_full_lock(tp, 0);
15830 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
15831 tg3_full_unlock(tp);
15832
15833done:
15834 if (state == pci_channel_io_perm_failure)
15835 err = PCI_ERS_RESULT_DISCONNECT;
15836 else
15837 pci_disable_device(pdev);
15838
15839 rtnl_unlock();
15840
15841 return err;
15842}
15843
15844/**
15845 * tg3_io_slot_reset - called after the pci bus has been reset.
15846 * @pdev: Pointer to PCI device
15847 *
15848 * Restart the card from scratch, as if from a cold-boot.
15849 * At this point, the card has exprienced a hard reset,
15850 * followed by fixups by BIOS, and has its config space
15851 * set up identically to what it was at cold boot.
15852 */
15853static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
15854{
15855 struct net_device *netdev = pci_get_drvdata(pdev);
15856 struct tg3 *tp = netdev_priv(netdev);
15857 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
15858 int err;
15859
15860 rtnl_lock();
15861
15862 if (pci_enable_device(pdev)) {
15863 netdev_err(netdev, "Cannot re-enable PCI device after reset.\n");
15864 goto done;
15865 }
15866
15867 pci_set_master(pdev);
15868 pci_restore_state(pdev);
15869 pci_save_state(pdev);
15870
15871 if (!netif_running(netdev)) {
15872 rc = PCI_ERS_RESULT_RECOVERED;
15873 goto done;
15874 }
15875
15876 err = tg3_power_up(tp);
Matt Carlsonbed98292011-07-13 09:27:29 +000015877 if (err)
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015878 goto done;
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015879
15880 rc = PCI_ERS_RESULT_RECOVERED;
15881
15882done:
15883 rtnl_unlock();
15884
15885 return rc;
15886}
15887
15888/**
15889 * tg3_io_resume - called when traffic can start flowing again.
15890 * @pdev: Pointer to PCI device
15891 *
15892 * This callback is called when the error recovery driver tells
15893 * us that its OK to resume normal operation.
15894 */
15895static void tg3_io_resume(struct pci_dev *pdev)
15896{
15897 struct net_device *netdev = pci_get_drvdata(pdev);
15898 struct tg3 *tp = netdev_priv(netdev);
15899 int err;
15900
15901 rtnl_lock();
15902
15903 if (!netif_running(netdev))
15904 goto done;
15905
15906 tg3_full_lock(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +000015907 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015908 err = tg3_restart_hw(tp, 1);
15909 tg3_full_unlock(tp);
15910 if (err) {
15911 netdev_err(netdev, "Cannot restart hardware after reset.\n");
15912 goto done;
15913 }
15914
15915 netif_device_attach(netdev);
15916
15917 tp->timer.expires = jiffies + tp->timer_offset;
15918 add_timer(&tp->timer);
15919
15920 tg3_netif_start(tp);
15921
15922 tg3_phy_start(tp);
15923
15924done:
15925 rtnl_unlock();
15926}
15927
15928static struct pci_error_handlers tg3_err_handler = {
15929 .error_detected = tg3_io_error_detected,
15930 .slot_reset = tg3_io_slot_reset,
15931 .resume = tg3_io_resume
15932};
15933
Linus Torvalds1da177e2005-04-16 15:20:36 -070015934static struct pci_driver tg3_driver = {
15935 .name = DRV_MODULE_NAME,
15936 .id_table = tg3_pci_tbl,
15937 .probe = tg3_init_one,
15938 .remove = __devexit_p(tg3_remove_one),
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015939 .err_handler = &tg3_err_handler,
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015940 .driver.pm = TG3_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070015941};
15942
15943static int __init tg3_init(void)
15944{
Jeff Garzik29917622006-08-19 17:48:59 -040015945 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015946}
15947
15948static void __exit tg3_cleanup(void)
15949{
15950 pci_unregister_driver(&tg3_driver);
15951}
15952
15953module_init(tg3_init);
15954module_exit(tg3_cleanup);